6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
27 #include <linux/audit.h>
32 #ifdef CONFIG_XFRM_STATISTICS
36 #include "xfrm_hash.h"
38 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40 #define XFRM_MAX_QUEUE_LEN 100
42 DEFINE_MUTEX(xfrm_cfg_mutex
);
43 EXPORT_SYMBOL(xfrm_cfg_mutex
);
45 static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock
);
46 static struct dst_entry
*xfrm_policy_sk_bundles
;
47 static DEFINE_RWLOCK(xfrm_policy_lock
);
49 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock
);
50 static struct xfrm_policy_afinfo __rcu
*xfrm_policy_afinfo
[NPROTO
]
53 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
55 static void xfrm_init_pmtu(struct dst_entry
*dst
);
56 static int stale_bundle(struct dst_entry
*dst
);
57 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
58 static void xfrm_policy_queue_process(unsigned long arg
);
60 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
64 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
66 const struct flowi4
*fl4
= &fl
->u
.ip4
;
68 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
69 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
70 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
71 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
72 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
73 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
77 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
79 const struct flowi6
*fl6
= &fl
->u
.ip6
;
81 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
82 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
83 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
84 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
85 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
86 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
89 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
90 unsigned short family
)
94 return __xfrm4_selector_match(sel
, fl
);
96 return __xfrm6_selector_match(sel
, fl
);
101 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
103 struct xfrm_policy_afinfo
*afinfo
;
105 if (unlikely(family
>= NPROTO
))
108 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
109 if (unlikely(!afinfo
))
114 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
119 static inline struct dst_entry
*__xfrm_dst_lookup(struct net
*net
, int tos
,
120 const xfrm_address_t
*saddr
,
121 const xfrm_address_t
*daddr
,
124 struct xfrm_policy_afinfo
*afinfo
;
125 struct dst_entry
*dst
;
127 afinfo
= xfrm_policy_get_afinfo(family
);
128 if (unlikely(afinfo
== NULL
))
129 return ERR_PTR(-EAFNOSUPPORT
);
131 dst
= afinfo
->dst_lookup(net
, tos
, saddr
, daddr
);
133 xfrm_policy_put_afinfo(afinfo
);
138 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
, int tos
,
139 xfrm_address_t
*prev_saddr
,
140 xfrm_address_t
*prev_daddr
,
143 struct net
*net
= xs_net(x
);
144 xfrm_address_t
*saddr
= &x
->props
.saddr
;
145 xfrm_address_t
*daddr
= &x
->id
.daddr
;
146 struct dst_entry
*dst
;
148 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
152 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
157 dst
= __xfrm_dst_lookup(net
, tos
, saddr
, daddr
, family
);
160 if (prev_saddr
!= saddr
)
161 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
162 if (prev_daddr
!= daddr
)
163 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
169 static inline unsigned long make_jiffies(long secs
)
171 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
172 return MAX_SCHEDULE_TIMEOUT
-1;
177 static void xfrm_policy_timer(unsigned long data
)
179 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
180 unsigned long now
= get_seconds();
181 long next
= LONG_MAX
;
185 read_lock(&xp
->lock
);
187 if (unlikely(xp
->walk
.dead
))
190 dir
= xfrm_policy_id2dir(xp
->index
);
192 if (xp
->lft
.hard_add_expires_seconds
) {
193 long tmo
= xp
->lft
.hard_add_expires_seconds
+
194 xp
->curlft
.add_time
- now
;
200 if (xp
->lft
.hard_use_expires_seconds
) {
201 long tmo
= xp
->lft
.hard_use_expires_seconds
+
202 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
208 if (xp
->lft
.soft_add_expires_seconds
) {
209 long tmo
= xp
->lft
.soft_add_expires_seconds
+
210 xp
->curlft
.add_time
- now
;
213 tmo
= XFRM_KM_TIMEOUT
;
218 if (xp
->lft
.soft_use_expires_seconds
) {
219 long tmo
= xp
->lft
.soft_use_expires_seconds
+
220 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
223 tmo
= XFRM_KM_TIMEOUT
;
230 km_policy_expired(xp
, dir
, 0, 0);
231 if (next
!= LONG_MAX
&&
232 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
236 read_unlock(&xp
->lock
);
241 read_unlock(&xp
->lock
);
242 if (!xfrm_policy_delete(xp
, dir
))
243 km_policy_expired(xp
, dir
, 1, 0);
247 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
249 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
251 if (unlikely(pol
->walk
.dead
))
259 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
261 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
263 return !pol
->walk
.dead
;
266 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
268 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
271 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
272 .get
= xfrm_policy_flo_get
,
273 .check
= xfrm_policy_flo_check
,
274 .delete = xfrm_policy_flo_delete
,
277 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
281 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
283 struct xfrm_policy
*policy
;
285 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
288 write_pnet(&policy
->xp_net
, net
);
289 INIT_LIST_HEAD(&policy
->walk
.all
);
290 INIT_HLIST_NODE(&policy
->bydst
);
291 INIT_HLIST_NODE(&policy
->byidx
);
292 rwlock_init(&policy
->lock
);
293 atomic_set(&policy
->refcnt
, 1);
294 skb_queue_head_init(&policy
->polq
.hold_queue
);
295 setup_timer(&policy
->timer
, xfrm_policy_timer
,
296 (unsigned long)policy
);
297 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
298 (unsigned long)policy
);
299 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
303 EXPORT_SYMBOL(xfrm_policy_alloc
);
305 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
307 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
309 BUG_ON(!policy
->walk
.dead
);
311 if (del_timer(&policy
->timer
))
314 security_xfrm_policy_free(policy
->security
);
317 EXPORT_SYMBOL(xfrm_policy_destroy
);
319 static void xfrm_queue_purge(struct sk_buff_head
*list
)
323 while ((skb
= skb_dequeue(list
)) != NULL
) {
329 /* Rule must be locked. Release descentant resources, announce
330 * entry dead. The rule must be unlinked from lists to the moment.
333 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
335 policy
->walk
.dead
= 1;
337 atomic_inc(&policy
->genid
);
339 del_timer(&policy
->polq
.hold_timer
);
340 xfrm_queue_purge(&policy
->polq
.hold_queue
);
342 if (del_timer(&policy
->timer
))
343 xfrm_pol_put(policy
);
345 xfrm_pol_put(policy
);
348 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
350 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
352 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
355 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
356 const struct xfrm_selector
*sel
,
357 unsigned short family
, int dir
)
359 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
360 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
362 return (hash
== hmask
+ 1 ?
363 &net
->xfrm
.policy_inexact
[dir
] :
364 net
->xfrm
.policy_bydst
[dir
].table
+ hash
);
367 static struct hlist_head
*policy_hash_direct(struct net
*net
,
368 const xfrm_address_t
*daddr
,
369 const xfrm_address_t
*saddr
,
370 unsigned short family
, int dir
)
372 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
373 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
375 return net
->xfrm
.policy_bydst
[dir
].table
+ hash
;
378 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
379 struct hlist_head
*ndsttable
,
380 unsigned int nhashmask
)
382 struct hlist_node
*tmp
, *entry0
= NULL
;
383 struct xfrm_policy
*pol
;
387 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
390 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
391 pol
->family
, nhashmask
);
393 hlist_del(&pol
->bydst
);
394 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
399 hlist_del(&pol
->bydst
);
400 hlist_add_after(entry0
, &pol
->bydst
);
402 entry0
= &pol
->bydst
;
404 if (!hlist_empty(list
)) {
410 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
411 struct hlist_head
*nidxtable
,
412 unsigned int nhashmask
)
414 struct hlist_node
*tmp
;
415 struct xfrm_policy
*pol
;
417 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
420 h
= __idx_hash(pol
->index
, nhashmask
);
421 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
425 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
427 return ((old_hmask
+ 1) << 1) - 1;
430 static void xfrm_bydst_resize(struct net
*net
, int dir
)
432 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
433 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
434 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
435 struct hlist_head
*odst
= net
->xfrm
.policy_bydst
[dir
].table
;
436 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
442 write_lock_bh(&xfrm_policy_lock
);
444 for (i
= hmask
; i
>= 0; i
--)
445 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
447 net
->xfrm
.policy_bydst
[dir
].table
= ndst
;
448 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
450 write_unlock_bh(&xfrm_policy_lock
);
452 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
455 static void xfrm_byidx_resize(struct net
*net
, int total
)
457 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
458 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
459 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
460 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
461 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
467 write_lock_bh(&xfrm_policy_lock
);
469 for (i
= hmask
; i
>= 0; i
--)
470 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
472 net
->xfrm
.policy_byidx
= nidx
;
473 net
->xfrm
.policy_idx_hmask
= nhashmask
;
475 write_unlock_bh(&xfrm_policy_lock
);
477 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
480 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
482 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
483 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
488 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
495 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
497 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
499 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
506 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
508 read_lock_bh(&xfrm_policy_lock
);
509 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
510 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
511 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
512 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
513 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
514 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
515 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
516 si
->spdhmcnt
= xfrm_policy_hashmax
;
517 read_unlock_bh(&xfrm_policy_lock
);
519 EXPORT_SYMBOL(xfrm_spd_getinfo
);
521 static DEFINE_MUTEX(hash_resize_mutex
);
522 static void xfrm_hash_resize(struct work_struct
*work
)
524 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
527 mutex_lock(&hash_resize_mutex
);
530 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
531 if (xfrm_bydst_should_resize(net
, dir
, &total
))
532 xfrm_bydst_resize(net
, dir
);
534 if (xfrm_byidx_should_resize(net
, total
))
535 xfrm_byidx_resize(net
, total
);
537 mutex_unlock(&hash_resize_mutex
);
540 /* Generate new index... KAME seems to generate them ordered by cost
541 * of an absolute inpredictability of ordering of rules. This will not pass. */
542 static u32
xfrm_gen_index(struct net
*net
, int dir
)
544 static u32 idx_generator
;
547 struct hlist_head
*list
;
548 struct xfrm_policy
*p
;
552 idx
= (idx_generator
| dir
);
556 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
558 hlist_for_each_entry(p
, list
, byidx
) {
559 if (p
->index
== idx
) {
569 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
571 u32
*p1
= (u32
*) s1
;
572 u32
*p2
= (u32
*) s2
;
573 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
576 for (i
= 0; i
< len
; i
++) {
584 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
585 struct xfrm_policy
*new)
587 struct xfrm_policy_queue
*pq
= &old
->polq
;
588 struct sk_buff_head list
;
590 __skb_queue_head_init(&list
);
592 spin_lock_bh(&pq
->hold_queue
.lock
);
593 skb_queue_splice_init(&pq
->hold_queue
, &list
);
594 del_timer(&pq
->hold_timer
);
595 spin_unlock_bh(&pq
->hold_queue
.lock
);
597 if (skb_queue_empty(&list
))
602 spin_lock_bh(&pq
->hold_queue
.lock
);
603 skb_queue_splice(&list
, &pq
->hold_queue
);
604 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
605 mod_timer(&pq
->hold_timer
, jiffies
);
606 spin_unlock_bh(&pq
->hold_queue
.lock
);
609 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
610 struct xfrm_policy
*pol
)
612 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
614 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
617 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
618 policy
->priority
== pol
->priority
)
624 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
626 struct net
*net
= xp_net(policy
);
627 struct xfrm_policy
*pol
;
628 struct xfrm_policy
*delpol
;
629 struct hlist_head
*chain
;
630 struct hlist_node
*newpos
;
632 write_lock_bh(&xfrm_policy_lock
);
633 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
636 hlist_for_each_entry(pol
, chain
, bydst
) {
637 if (pol
->type
== policy
->type
&&
638 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
639 xfrm_policy_mark_match(policy
, pol
) &&
640 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
643 write_unlock_bh(&xfrm_policy_lock
);
647 if (policy
->priority
> pol
->priority
)
649 } else if (policy
->priority
>= pol
->priority
) {
650 newpos
= &pol
->bydst
;
657 hlist_add_after(newpos
, &policy
->bydst
);
659 hlist_add_head(&policy
->bydst
, chain
);
660 xfrm_pol_hold(policy
);
661 net
->xfrm
.policy_count
[dir
]++;
662 atomic_inc(&flow_cache_genid
);
665 xfrm_policy_requeue(delpol
, policy
);
666 __xfrm_policy_unlink(delpol
, dir
);
668 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
);
669 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
670 policy
->curlft
.add_time
= get_seconds();
671 policy
->curlft
.use_time
= 0;
672 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
673 xfrm_pol_hold(policy
);
674 list_add(&policy
->walk
.all
, &net
->xfrm
.policy_all
);
675 write_unlock_bh(&xfrm_policy_lock
);
678 xfrm_policy_kill(delpol
);
679 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
680 schedule_work(&net
->xfrm
.policy_hash_work
);
684 EXPORT_SYMBOL(xfrm_policy_insert
);
686 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
687 int dir
, struct xfrm_selector
*sel
,
688 struct xfrm_sec_ctx
*ctx
, int delete,
691 struct xfrm_policy
*pol
, *ret
;
692 struct hlist_head
*chain
;
695 write_lock_bh(&xfrm_policy_lock
);
696 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
698 hlist_for_each_entry(pol
, chain
, bydst
) {
699 if (pol
->type
== type
&&
700 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
701 !selector_cmp(sel
, &pol
->selector
) &&
702 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
705 *err
= security_xfrm_policy_delete(
708 write_unlock_bh(&xfrm_policy_lock
);
711 __xfrm_policy_unlink(pol
, dir
);
717 write_unlock_bh(&xfrm_policy_lock
);
720 xfrm_policy_kill(ret
);
723 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
725 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
726 int dir
, u32 id
, int delete, int *err
)
728 struct xfrm_policy
*pol
, *ret
;
729 struct hlist_head
*chain
;
732 if (xfrm_policy_id2dir(id
) != dir
)
736 write_lock_bh(&xfrm_policy_lock
);
737 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
739 hlist_for_each_entry(pol
, chain
, byidx
) {
740 if (pol
->type
== type
&& pol
->index
== id
&&
741 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
744 *err
= security_xfrm_policy_delete(
747 write_unlock_bh(&xfrm_policy_lock
);
750 __xfrm_policy_unlink(pol
, dir
);
756 write_unlock_bh(&xfrm_policy_lock
);
759 xfrm_policy_kill(ret
);
762 EXPORT_SYMBOL(xfrm_policy_byid
);
764 #ifdef CONFIG_SECURITY_NETWORK_XFRM
766 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, struct xfrm_audit
*audit_info
)
770 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
771 struct xfrm_policy
*pol
;
774 hlist_for_each_entry(pol
,
775 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
776 if (pol
->type
!= type
)
778 err
= security_xfrm_policy_delete(pol
->security
);
780 xfrm_audit_policy_delete(pol
, 0,
781 audit_info
->loginuid
,
782 audit_info
->sessionid
,
787 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
788 hlist_for_each_entry(pol
,
789 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
791 if (pol
->type
!= type
)
793 err
= security_xfrm_policy_delete(
796 xfrm_audit_policy_delete(pol
, 0,
797 audit_info
->loginuid
,
798 audit_info
->sessionid
,
809 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, struct xfrm_audit
*audit_info
)
815 int xfrm_policy_flush(struct net
*net
, u8 type
, struct xfrm_audit
*audit_info
)
817 int dir
, err
= 0, cnt
= 0;
819 write_lock_bh(&xfrm_policy_lock
);
821 err
= xfrm_policy_flush_secctx_check(net
, type
, audit_info
);
825 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
826 struct xfrm_policy
*pol
;
830 hlist_for_each_entry(pol
,
831 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
832 if (pol
->type
!= type
)
834 __xfrm_policy_unlink(pol
, dir
);
835 write_unlock_bh(&xfrm_policy_lock
);
838 xfrm_audit_policy_delete(pol
, 1, audit_info
->loginuid
,
839 audit_info
->sessionid
,
842 xfrm_policy_kill(pol
);
844 write_lock_bh(&xfrm_policy_lock
);
848 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
850 hlist_for_each_entry(pol
,
851 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
853 if (pol
->type
!= type
)
855 __xfrm_policy_unlink(pol
, dir
);
856 write_unlock_bh(&xfrm_policy_lock
);
859 xfrm_audit_policy_delete(pol
, 1,
860 audit_info
->loginuid
,
861 audit_info
->sessionid
,
863 xfrm_policy_kill(pol
);
865 write_lock_bh(&xfrm_policy_lock
);
874 write_unlock_bh(&xfrm_policy_lock
);
877 EXPORT_SYMBOL(xfrm_policy_flush
);
879 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
880 int (*func
)(struct xfrm_policy
*, int, int, void*),
883 struct xfrm_policy
*pol
;
884 struct xfrm_policy_walk_entry
*x
;
887 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
888 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
891 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
894 write_lock_bh(&xfrm_policy_lock
);
895 if (list_empty(&walk
->walk
.all
))
896 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
898 x
= list_entry(&walk
->walk
.all
, struct xfrm_policy_walk_entry
, all
);
899 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
902 pol
= container_of(x
, struct xfrm_policy
, walk
);
903 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
904 walk
->type
!= pol
->type
)
906 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
909 list_move_tail(&walk
->walk
.all
, &x
->all
);
914 if (walk
->seq
== 0) {
918 list_del_init(&walk
->walk
.all
);
920 write_unlock_bh(&xfrm_policy_lock
);
923 EXPORT_SYMBOL(xfrm_policy_walk
);
925 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
927 INIT_LIST_HEAD(&walk
->walk
.all
);
932 EXPORT_SYMBOL(xfrm_policy_walk_init
);
934 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
)
936 if (list_empty(&walk
->walk
.all
))
939 write_lock_bh(&xfrm_policy_lock
);
940 list_del(&walk
->walk
.all
);
941 write_unlock_bh(&xfrm_policy_lock
);
943 EXPORT_SYMBOL(xfrm_policy_walk_done
);
946 * Find policy to apply to this flow.
948 * Returns 0 if policy found, else an -errno.
950 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
951 const struct flowi
*fl
,
952 u8 type
, u16 family
, int dir
)
954 const struct xfrm_selector
*sel
= &pol
->selector
;
958 if (pol
->family
!= family
||
959 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
963 match
= xfrm_selector_match(sel
, fl
, family
);
965 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
971 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
972 const struct flowi
*fl
,
976 struct xfrm_policy
*pol
, *ret
;
977 const xfrm_address_t
*daddr
, *saddr
;
978 struct hlist_head
*chain
;
981 daddr
= xfrm_flowi_daddr(fl
, family
);
982 saddr
= xfrm_flowi_saddr(fl
, family
);
983 if (unlikely(!daddr
|| !saddr
))
986 read_lock_bh(&xfrm_policy_lock
);
987 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
989 hlist_for_each_entry(pol
, chain
, bydst
) {
990 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1000 priority
= ret
->priority
;
1004 chain
= &net
->xfrm
.policy_inexact
[dir
];
1005 hlist_for_each_entry(pol
, chain
, bydst
) {
1006 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1014 } else if (pol
->priority
< priority
) {
1022 read_unlock_bh(&xfrm_policy_lock
);
1027 static struct xfrm_policy
*
1028 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1030 #ifdef CONFIG_XFRM_SUB_POLICY
1031 struct xfrm_policy
*pol
;
1033 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1037 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1040 static struct flow_cache_object
*
1041 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1042 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1044 struct xfrm_policy
*pol
;
1047 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1049 pol
= __xfrm_policy_lookup(net
, fl
, family
, dir
);
1050 if (IS_ERR_OR_NULL(pol
))
1051 return ERR_CAST(pol
);
1053 /* Resolver returns two references:
1054 * one for cache and one for caller of flow_cache_lookup() */
1060 static inline int policy_to_flow_dir(int dir
)
1062 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1063 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1064 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1068 case XFRM_POLICY_IN
:
1070 case XFRM_POLICY_OUT
:
1071 return FLOW_DIR_OUT
;
1072 case XFRM_POLICY_FWD
:
1073 return FLOW_DIR_FWD
;
1077 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
,
1078 const struct flowi
*fl
)
1080 struct xfrm_policy
*pol
;
1082 read_lock_bh(&xfrm_policy_lock
);
1083 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1084 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1089 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1093 err
= security_xfrm_policy_lookup(pol
->security
,
1095 policy_to_flow_dir(dir
));
1098 else if (err
== -ESRCH
)
1106 read_unlock_bh(&xfrm_policy_lock
);
1110 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1112 struct net
*net
= xp_net(pol
);
1113 struct hlist_head
*chain
= policy_hash_bysel(net
, &pol
->selector
,
1116 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1117 hlist_add_head(&pol
->bydst
, chain
);
1118 hlist_add_head(&pol
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, pol
->index
));
1119 net
->xfrm
.policy_count
[dir
]++;
1122 if (xfrm_bydst_should_resize(net
, dir
, NULL
))
1123 schedule_work(&net
->xfrm
.policy_hash_work
);
1126 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1129 struct net
*net
= xp_net(pol
);
1131 if (hlist_unhashed(&pol
->bydst
))
1134 hlist_del(&pol
->bydst
);
1135 hlist_del(&pol
->byidx
);
1136 list_del(&pol
->walk
.all
);
1137 net
->xfrm
.policy_count
[dir
]--;
1142 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1144 write_lock_bh(&xfrm_policy_lock
);
1145 pol
= __xfrm_policy_unlink(pol
, dir
);
1146 write_unlock_bh(&xfrm_policy_lock
);
1148 xfrm_policy_kill(pol
);
1153 EXPORT_SYMBOL(xfrm_policy_delete
);
1155 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1157 struct net
*net
= xp_net(pol
);
1158 struct xfrm_policy
*old_pol
;
1160 #ifdef CONFIG_XFRM_SUB_POLICY
1161 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1165 write_lock_bh(&xfrm_policy_lock
);
1166 old_pol
= sk
->sk_policy
[dir
];
1167 sk
->sk_policy
[dir
] = pol
;
1169 pol
->curlft
.add_time
= get_seconds();
1170 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
);
1171 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1175 xfrm_policy_requeue(old_pol
, pol
);
1177 /* Unlinking succeeds always. This is the only function
1178 * allowed to delete or replace socket policy.
1180 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1182 write_unlock_bh(&xfrm_policy_lock
);
1185 xfrm_policy_kill(old_pol
);
1190 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1192 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1195 newp
->selector
= old
->selector
;
1196 if (security_xfrm_policy_clone(old
->security
,
1199 return NULL
; /* ENOMEM */
1201 newp
->lft
= old
->lft
;
1202 newp
->curlft
= old
->curlft
;
1203 newp
->mark
= old
->mark
;
1204 newp
->action
= old
->action
;
1205 newp
->flags
= old
->flags
;
1206 newp
->xfrm_nr
= old
->xfrm_nr
;
1207 newp
->index
= old
->index
;
1208 newp
->type
= old
->type
;
1209 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1210 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1211 write_lock_bh(&xfrm_policy_lock
);
1212 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1213 write_unlock_bh(&xfrm_policy_lock
);
1219 int __xfrm_sk_clone_policy(struct sock
*sk
)
1221 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1222 *p1
= sk
->sk_policy
[1];
1224 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1225 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1227 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1233 xfrm_get_saddr(struct net
*net
, xfrm_address_t
*local
, xfrm_address_t
*remote
,
1234 unsigned short family
)
1237 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1239 if (unlikely(afinfo
== NULL
))
1241 err
= afinfo
->get_saddr(net
, local
, remote
);
1242 xfrm_policy_put_afinfo(afinfo
);
1246 /* Resolve list of templates for the flow, given policy. */
1249 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1250 struct xfrm_state
**xfrm
, unsigned short family
)
1252 struct net
*net
= xp_net(policy
);
1255 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1256 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1259 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1260 struct xfrm_state
*x
;
1261 xfrm_address_t
*remote
= daddr
;
1262 xfrm_address_t
*local
= saddr
;
1263 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1265 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1266 tmpl
->mode
== XFRM_MODE_BEET
) {
1267 remote
= &tmpl
->id
.daddr
;
1268 local
= &tmpl
->saddr
;
1269 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1270 error
= xfrm_get_saddr(net
, &tmp
, remote
, tmpl
->encap_family
);
1277 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1279 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1286 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1290 else if (error
== -ESRCH
)
1293 if (!tmpl
->optional
)
1299 for (nx
--; nx
>=0; nx
--)
1300 xfrm_state_put(xfrm
[nx
]);
1305 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1306 struct xfrm_state
**xfrm
, unsigned short family
)
1308 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1309 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1315 for (i
= 0; i
< npols
; i
++) {
1316 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1321 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1329 /* found states are sorted for outbound processing */
1331 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1336 for (cnx
--; cnx
>=0; cnx
--)
1337 xfrm_state_put(tpp
[cnx
]);
1342 /* Check that the bundle accepts the flow and its components are
1346 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1348 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1354 tos
= afinfo
->get_tos(fl
);
1356 xfrm_policy_put_afinfo(afinfo
);
1361 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1363 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1364 struct dst_entry
*dst
= &xdst
->u
.dst
;
1366 if (xdst
->route
== NULL
) {
1367 /* Dummy bundle - if it has xfrms we were not
1368 * able to build bundle as template resolution failed.
1369 * It means we need to try again resolving. */
1370 if (xdst
->num_xfrms
> 0)
1372 } else if (dst
->flags
& DST_XFRM_QUEUE
) {
1376 if (stale_bundle(dst
))
1384 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1386 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1387 struct dst_entry
*dst
= &xdst
->u
.dst
;
1391 if (stale_bundle(dst
))
1397 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1399 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1400 struct dst_entry
*dst
= &xdst
->u
.dst
;
1405 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1406 .get
= xfrm_bundle_flo_get
,
1407 .check
= xfrm_bundle_flo_check
,
1408 .delete = xfrm_bundle_flo_delete
,
1411 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1413 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1414 struct dst_ops
*dst_ops
;
1415 struct xfrm_dst
*xdst
;
1418 return ERR_PTR(-EINVAL
);
1422 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1424 #if IS_ENABLED(CONFIG_IPV6)
1426 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1432 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1435 struct dst_entry
*dst
= &xdst
->u
.dst
;
1437 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1438 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1439 if (afinfo
->init_dst
)
1440 afinfo
->init_dst(net
, xdst
);
1442 xdst
= ERR_PTR(-ENOBUFS
);
1444 xfrm_policy_put_afinfo(afinfo
);
1449 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1452 struct xfrm_policy_afinfo
*afinfo
=
1453 xfrm_policy_get_afinfo(dst
->ops
->family
);
1459 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1461 xfrm_policy_put_afinfo(afinfo
);
1466 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1467 const struct flowi
*fl
)
1469 struct xfrm_policy_afinfo
*afinfo
=
1470 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1476 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1478 xfrm_policy_put_afinfo(afinfo
);
1484 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1485 * all the metrics... Shortly, bundle a bundle.
1488 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1489 struct xfrm_state
**xfrm
, int nx
,
1490 const struct flowi
*fl
,
1491 struct dst_entry
*dst
)
1493 struct net
*net
= xp_net(policy
);
1494 unsigned long now
= jiffies
;
1495 struct net_device
*dev
;
1496 struct xfrm_mode
*inner_mode
;
1497 struct dst_entry
*dst_prev
= NULL
;
1498 struct dst_entry
*dst0
= NULL
;
1502 int nfheader_len
= 0;
1503 int trailer_len
= 0;
1505 int family
= policy
->selector
.family
;
1506 xfrm_address_t saddr
, daddr
;
1508 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1510 tos
= xfrm_get_tos(fl
, family
);
1517 for (; i
< nx
; i
++) {
1518 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1519 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1521 err
= PTR_ERR(xdst
);
1527 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1528 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1529 xfrm_af2proto(family
));
1531 err
= -EAFNOSUPPORT
;
1536 inner_mode
= xfrm
[i
]->inner_mode
;
1541 dst_prev
->child
= dst_clone(dst1
);
1542 dst1
->flags
|= DST_NOHASH
;
1546 dst_copy_metrics(dst1
, dst
);
1548 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1549 family
= xfrm
[i
]->props
.family
;
1550 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, &saddr
, &daddr
,
1558 dst1
->xfrm
= xfrm
[i
];
1559 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1561 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1562 dst1
->flags
|= DST_HOST
;
1563 dst1
->lastuse
= now
;
1565 dst1
->input
= dst_discard
;
1566 dst1
->output
= inner_mode
->afinfo
->output
;
1568 dst1
->next
= dst_prev
;
1571 header_len
+= xfrm
[i
]->props
.header_len
;
1572 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1573 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1574 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1577 dst_prev
->child
= dst
;
1585 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1586 xfrm_init_pmtu(dst_prev
);
1588 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1589 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1591 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1595 dst_prev
->header_len
= header_len
;
1596 dst_prev
->trailer_len
= trailer_len
;
1597 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1598 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1606 xfrm_state_put(xfrm
[i
]);
1610 dst0
= ERR_PTR(err
);
1615 xfrm_dst_alloc_copy(void **target
, const void *src
, int size
)
1618 *target
= kmalloc(size
, GFP_ATOMIC
);
1622 memcpy(*target
, src
, size
);
1627 xfrm_dst_update_parent(struct dst_entry
*dst
, const struct xfrm_selector
*sel
)
1629 #ifdef CONFIG_XFRM_SUB_POLICY
1630 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1631 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1639 xfrm_dst_update_origin(struct dst_entry
*dst
, const struct flowi
*fl
)
1641 #ifdef CONFIG_XFRM_SUB_POLICY
1642 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1643 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1649 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1650 struct xfrm_policy
**pols
,
1651 int *num_pols
, int *num_xfrms
)
1655 if (*num_pols
== 0 || !pols
[0]) {
1660 if (IS_ERR(pols
[0]))
1661 return PTR_ERR(pols
[0]);
1663 *num_xfrms
= pols
[0]->xfrm_nr
;
1665 #ifdef CONFIG_XFRM_SUB_POLICY
1666 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1667 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1668 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1669 XFRM_POLICY_TYPE_MAIN
,
1673 if (IS_ERR(pols
[1])) {
1674 xfrm_pols_put(pols
, *num_pols
);
1675 return PTR_ERR(pols
[1]);
1678 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1682 for (i
= 0; i
< *num_pols
; i
++) {
1683 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1693 static struct xfrm_dst
*
1694 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1695 const struct flowi
*fl
, u16 family
,
1696 struct dst_entry
*dst_orig
)
1698 struct net
*net
= xp_net(pols
[0]);
1699 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1700 struct dst_entry
*dst
;
1701 struct xfrm_dst
*xdst
;
1704 /* Try to instantiate a bundle */
1705 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1707 if (err
!= 0 && err
!= -EAGAIN
)
1708 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1709 return ERR_PTR(err
);
1712 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1714 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1715 return ERR_CAST(dst
);
1718 xdst
= (struct xfrm_dst
*)dst
;
1719 xdst
->num_xfrms
= err
;
1721 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1723 err
= xfrm_dst_update_origin(dst
, fl
);
1724 if (unlikely(err
)) {
1726 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1727 return ERR_PTR(err
);
1730 xdst
->num_pols
= num_pols
;
1731 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1732 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1737 static void xfrm_policy_queue_process(unsigned long arg
)
1740 struct sk_buff
*skb
;
1742 struct dst_entry
*dst
;
1743 struct net_device
*dev
;
1744 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1745 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1747 struct sk_buff_head list
;
1749 spin_lock(&pq
->hold_queue
.lock
);
1750 skb
= skb_peek(&pq
->hold_queue
);
1753 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1754 spin_unlock(&pq
->hold_queue
.lock
);
1756 dst_hold(dst
->path
);
1757 dst
= xfrm_lookup(xp_net(pol
), dst
->path
, &fl
,
1762 if (dst
->flags
& DST_XFRM_QUEUE
) {
1765 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1768 pq
->timeout
= pq
->timeout
<< 1;
1769 mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
);
1775 __skb_queue_head_init(&list
);
1777 spin_lock(&pq
->hold_queue
.lock
);
1779 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1780 spin_unlock(&pq
->hold_queue
.lock
);
1782 while (!skb_queue_empty(&list
)) {
1783 skb
= __skb_dequeue(&list
);
1785 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1786 dst_hold(skb_dst(skb
)->path
);
1787 dst
= xfrm_lookup(xp_net(pol
), skb_dst(skb
)->path
,
1797 skb_dst_set(skb
, dst
);
1800 err
= dst_output(skb
);
1808 xfrm_queue_purge(&pq
->hold_queue
);
1811 static int xdst_queue_output(struct sk_buff
*skb
)
1813 unsigned long sched_next
;
1814 struct dst_entry
*dst
= skb_dst(skb
);
1815 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1816 struct xfrm_policy_queue
*pq
= &xdst
->pols
[0]->polq
;
1818 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
1826 spin_lock_bh(&pq
->hold_queue
.lock
);
1829 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
1831 sched_next
= jiffies
+ pq
->timeout
;
1833 if (del_timer(&pq
->hold_timer
)) {
1834 if (time_before(pq
->hold_timer
.expires
, sched_next
))
1835 sched_next
= pq
->hold_timer
.expires
;
1838 __skb_queue_tail(&pq
->hold_queue
, skb
);
1839 mod_timer(&pq
->hold_timer
, sched_next
);
1841 spin_unlock_bh(&pq
->hold_queue
.lock
);
1846 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
1847 struct dst_entry
*dst
,
1848 const struct flowi
*fl
,
1853 struct net_device
*dev
;
1854 struct dst_entry
*dst1
;
1855 struct xfrm_dst
*xdst
;
1857 xdst
= xfrm_alloc_dst(net
, family
);
1861 if (net
->xfrm
.sysctl_larval_drop
|| num_xfrms
<= 0 ||
1862 (fl
->flowi_flags
& FLOWI_FLAG_CAN_SLEEP
))
1865 dst1
= &xdst
->u
.dst
;
1869 dst_copy_metrics(dst1
, dst
);
1871 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1872 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
1873 dst1
->lastuse
= jiffies
;
1875 dst1
->input
= dst_discard
;
1876 dst1
->output
= xdst_queue_output
;
1882 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
1889 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1898 xdst
= ERR_PTR(err
);
1902 static struct flow_cache_object
*
1903 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
1904 struct flow_cache_object
*oldflo
, void *ctx
)
1906 struct dst_entry
*dst_orig
= (struct dst_entry
*)ctx
;
1907 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1908 struct xfrm_dst
*xdst
, *new_xdst
;
1909 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
1911 /* Check if the policies from old bundle are usable */
1914 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
1915 num_pols
= xdst
->num_pols
;
1916 num_xfrms
= xdst
->num_xfrms
;
1918 for (i
= 0; i
< num_pols
; i
++) {
1919 pols
[i
] = xdst
->pols
[i
];
1920 pol_dead
|= pols
[i
]->walk
.dead
;
1923 dst_free(&xdst
->u
.dst
);
1931 /* Resolve policies to use if we couldn't get them from
1932 * previous cache entry */
1935 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
, dir
);
1936 err
= xfrm_expand_policies(fl
, family
, pols
,
1937 &num_pols
, &num_xfrms
);
1943 goto make_dummy_bundle
;
1946 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
, dst_orig
);
1947 if (IS_ERR(new_xdst
)) {
1948 err
= PTR_ERR(new_xdst
);
1952 goto make_dummy_bundle
;
1953 dst_hold(&xdst
->u
.dst
);
1955 } else if (new_xdst
== NULL
) {
1958 goto make_dummy_bundle
;
1959 xdst
->num_xfrms
= 0;
1960 dst_hold(&xdst
->u
.dst
);
1964 /* Kill the previous bundle */
1966 /* The policies were stolen for newly generated bundle */
1968 dst_free(&xdst
->u
.dst
);
1971 /* Flow cache does not have reference, it dst_free()'s,
1972 * but we do need to return one reference for original caller */
1973 dst_hold(&new_xdst
->u
.dst
);
1974 return &new_xdst
->flo
;
1977 /* We found policies, but there's no bundles to instantiate:
1978 * either because the policy blocks, has no transformations or
1979 * we could not build template (no xfrm_states).*/
1980 xdst
= xfrm_create_dummy_bundle(net
, dst_orig
, fl
, num_xfrms
, family
);
1982 xfrm_pols_put(pols
, num_pols
);
1983 return ERR_CAST(xdst
);
1985 xdst
->num_pols
= num_pols
;
1986 xdst
->num_xfrms
= num_xfrms
;
1987 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1989 dst_hold(&xdst
->u
.dst
);
1993 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1996 dst_free(&xdst
->u
.dst
);
1998 xfrm_pols_put(pols
, num_pols
);
1999 return ERR_PTR(err
);
2002 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2003 struct dst_entry
*dst_orig
)
2005 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2006 struct dst_entry
*ret
;
2009 dst_release(dst_orig
);
2010 return ERR_PTR(-EINVAL
);
2012 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2014 xfrm_policy_put_afinfo(afinfo
);
2019 /* Main function: finds/creates a bundle for given flow.
2021 * At the moment we eat a raw IP route. Mostly to speed up lookups
2022 * on interfaces with disabled IPsec.
2024 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2025 const struct flowi
*fl
,
2026 struct sock
*sk
, int flags
)
2028 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2029 struct flow_cache_object
*flo
;
2030 struct xfrm_dst
*xdst
;
2031 struct dst_entry
*dst
, *route
;
2032 u16 family
= dst_orig
->ops
->family
;
2033 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2034 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2041 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2043 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
2044 err
= xfrm_expand_policies(fl
, family
, pols
,
2045 &num_pols
, &num_xfrms
);
2050 if (num_xfrms
<= 0) {
2051 drop_pols
= num_pols
;
2055 xdst
= xfrm_resolve_and_create_bundle(
2059 xfrm_pols_put(pols
, num_pols
);
2060 err
= PTR_ERR(xdst
);
2062 } else if (xdst
== NULL
) {
2064 drop_pols
= num_pols
;
2068 dst_hold(&xdst
->u
.dst
);
2070 spin_lock_bh(&xfrm_policy_sk_bundle_lock
);
2071 xdst
->u
.dst
.next
= xfrm_policy_sk_bundles
;
2072 xfrm_policy_sk_bundles
= &xdst
->u
.dst
;
2073 spin_unlock_bh(&xfrm_policy_sk_bundle_lock
);
2075 route
= xdst
->route
;
2080 /* To accelerate a bit... */
2081 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2082 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2085 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2086 xfrm_bundle_lookup
, dst_orig
);
2093 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2095 num_pols
= xdst
->num_pols
;
2096 num_xfrms
= xdst
->num_xfrms
;
2097 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2098 route
= xdst
->route
;
2102 if (route
== NULL
&& num_xfrms
> 0) {
2103 /* The only case when xfrm_bundle_lookup() returns a
2104 * bundle with null route, is when the template could
2105 * not be resolved. It means policies are there, but
2106 * bundle could not be created, since we don't yet
2107 * have the xfrm_state's. We need to wait for KM to
2108 * negotiate new SA's or bail out with error.*/
2109 if (net
->xfrm
.sysctl_larval_drop
) {
2110 /* EREMOTE tells the caller to generate
2111 * a one-shot blackhole route. */
2113 xfrm_pols_put(pols
, drop_pols
);
2114 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2116 return make_blackhole(net
, family
, dst_orig
);
2118 if (fl
->flowi_flags
& FLOWI_FLAG_CAN_SLEEP
) {
2119 DECLARE_WAITQUEUE(wait
, current
);
2121 add_wait_queue(&net
->xfrm
.km_waitq
, &wait
);
2122 set_current_state(TASK_INTERRUPTIBLE
);
2124 set_current_state(TASK_RUNNING
);
2125 remove_wait_queue(&net
->xfrm
.km_waitq
, &wait
);
2127 if (!signal_pending(current
)) {
2136 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2144 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2145 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2150 for (i
= 0; i
< num_pols
; i
++)
2151 pols
[i
]->curlft
.use_time
= get_seconds();
2153 if (num_xfrms
< 0) {
2154 /* Prohibit the flow */
2155 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2158 } else if (num_xfrms
> 0) {
2159 /* Flow transformed */
2160 dst_release(dst_orig
);
2162 /* Flow passes untransformed */
2167 xfrm_pols_put(pols
, drop_pols
);
2168 if (dst
&& dst
->xfrm
&&
2169 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2170 dst
->flags
|= DST_XFRM_TUNNEL
;
2174 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2182 dst_release(dst_orig
);
2183 xfrm_pols_put(pols
, drop_pols
);
2184 return ERR_PTR(err
);
2186 EXPORT_SYMBOL(xfrm_lookup
);
2189 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2191 struct xfrm_state
*x
;
2193 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2195 x
= skb
->sp
->xvec
[idx
];
2196 if (!x
->type
->reject
)
2198 return x
->type
->reject(x
, skb
, fl
);
2201 /* When skb is transformed back to its "native" form, we have to
2202 * check policy restrictions. At the moment we make this in maximally
2203 * stupid way. Shame on me. :-) Of course, connected sockets must
2204 * have policy cached at them.
2208 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2209 unsigned short family
)
2211 if (xfrm_state_kern(x
))
2212 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2213 return x
->id
.proto
== tmpl
->id
.proto
&&
2214 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2215 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2216 x
->props
.mode
== tmpl
->mode
&&
2217 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2218 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2219 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2220 xfrm_state_addr_cmp(tmpl
, x
, family
));
2224 * 0 or more than 0 is returned when validation is succeeded (either bypass
2225 * because of optional transport mode, or next index of the mathced secpath
2226 * state with the template.
2227 * -1 is returned when no matching template is found.
2228 * Otherwise "-2 - errored_index" is returned.
2231 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2232 unsigned short family
)
2236 if (tmpl
->optional
) {
2237 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2241 for (; idx
< sp
->len
; idx
++) {
2242 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2244 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2253 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2254 unsigned int family
, int reverse
)
2256 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2259 if (unlikely(afinfo
== NULL
))
2260 return -EAFNOSUPPORT
;
2262 afinfo
->decode_session(skb
, fl
, reverse
);
2263 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2264 xfrm_policy_put_afinfo(afinfo
);
2267 EXPORT_SYMBOL(__xfrm_decode_session
);
2269 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2271 for (; k
< sp
->len
; k
++) {
2272 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2281 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2282 unsigned short family
)
2284 struct net
*net
= dev_net(skb
->dev
);
2285 struct xfrm_policy
*pol
;
2286 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2295 reverse
= dir
& ~XFRM_POLICY_MASK
;
2296 dir
&= XFRM_POLICY_MASK
;
2297 fl_dir
= policy_to_flow_dir(dir
);
2299 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2300 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2304 nf_nat_decode_session(skb
, &fl
, family
);
2306 /* First, check used SA against their selectors. */
2310 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
2311 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2312 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2313 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2320 if (sk
&& sk
->sk_policy
[dir
]) {
2321 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2323 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2329 struct flow_cache_object
*flo
;
2331 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2332 xfrm_policy_lookup
, NULL
);
2333 if (IS_ERR_OR_NULL(flo
))
2334 pol
= ERR_CAST(flo
);
2336 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2340 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2345 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2346 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2347 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2353 pol
->curlft
.use_time
= get_seconds();
2357 #ifdef CONFIG_XFRM_SUB_POLICY
2358 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2359 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2363 if (IS_ERR(pols
[1])) {
2364 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2367 pols
[1]->curlft
.use_time
= get_seconds();
2373 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2374 struct sec_path
*sp
;
2375 static struct sec_path dummy
;
2376 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2377 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2378 struct xfrm_tmpl
**tpp
= tp
;
2382 if ((sp
= skb
->sp
) == NULL
)
2385 for (pi
= 0; pi
< npols
; pi
++) {
2386 if (pols
[pi
] != pol
&&
2387 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2388 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2391 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2392 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2395 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2396 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2400 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
2404 /* For each tunnel xfrm, find the first matching tmpl.
2405 * For each tmpl before that, find corresponding xfrm.
2406 * Order is _important_. Later we will implement
2407 * some barriers, but at the moment barriers
2408 * are implied between each two transformations.
2410 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2411 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2414 /* "-2 - errored_index" returned */
2416 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2421 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2422 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2426 xfrm_pols_put(pols
, npols
);
2429 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2432 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2434 xfrm_pols_put(pols
, npols
);
2437 EXPORT_SYMBOL(__xfrm_policy_check
);
2439 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2441 struct net
*net
= dev_net(skb
->dev
);
2443 struct dst_entry
*dst
;
2446 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2447 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2453 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, 0);
2458 skb_dst_set(skb
, dst
);
2461 EXPORT_SYMBOL(__xfrm_route_forward
);
2463 /* Optimize later using cookies and generation ids. */
2465 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2467 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2468 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2469 * get validated by dst_ops->check on every use. We do this
2470 * because when a normal route referenced by an XFRM dst is
2471 * obsoleted we do not go looking around for all parent
2472 * referencing XFRM dsts so that we can invalidate them. It
2473 * is just too much work. Instead we make the checks here on
2474 * every use. For example:
2476 * XFRM dst A --> IPv4 dst X
2478 * X is the "xdst->route" of A (X is also the "dst->path" of A
2479 * in this example). If X is marked obsolete, "A" will not
2480 * notice. That's what we are validating here via the
2481 * stale_bundle() check.
2483 * When a policy's bundle is pruned, we dst_free() the XFRM
2484 * dst which causes it's ->obsolete field to be set to
2485 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2486 * this, we want to force a new route lookup.
2488 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2494 static int stale_bundle(struct dst_entry
*dst
)
2496 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2499 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2501 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2502 dst
->dev
= dev_net(dev
)->loopback_dev
;
2507 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2509 static void xfrm_link_failure(struct sk_buff
*skb
)
2511 /* Impossible. Such dst must be popped before reaches point of failure. */
2514 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2517 if (dst
->obsolete
) {
2525 static void __xfrm_garbage_collect(struct net
*net
)
2527 struct dst_entry
*head
, *next
;
2529 spin_lock_bh(&xfrm_policy_sk_bundle_lock
);
2530 head
= xfrm_policy_sk_bundles
;
2531 xfrm_policy_sk_bundles
= NULL
;
2532 spin_unlock_bh(&xfrm_policy_sk_bundle_lock
);
2541 static void xfrm_garbage_collect(struct net
*net
)
2544 __xfrm_garbage_collect(net
);
2547 static void xfrm_garbage_collect_deferred(struct net
*net
)
2549 flow_cache_flush_deferred();
2550 __xfrm_garbage_collect(net
);
2553 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2556 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2557 u32 pmtu
, route_mtu_cached
;
2559 pmtu
= dst_mtu(dst
->child
);
2560 xdst
->child_mtu_cached
= pmtu
;
2562 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2564 route_mtu_cached
= dst_mtu(xdst
->route
);
2565 xdst
->route_mtu_cached
= route_mtu_cached
;
2567 if (pmtu
> route_mtu_cached
)
2568 pmtu
= route_mtu_cached
;
2570 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2571 } while ((dst
= dst
->next
));
2574 /* Check that the bundle accepts the flow and its components are
2578 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2580 struct dst_entry
*dst
= &first
->u
.dst
;
2581 struct xfrm_dst
*last
;
2584 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2585 (dst
->dev
&& !netif_running(dst
->dev
)))
2588 if (dst
->flags
& DST_XFRM_QUEUE
)
2594 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2596 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2598 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2600 if (xdst
->num_pols
> 0 &&
2601 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2604 mtu
= dst_mtu(dst
->child
);
2605 if (xdst
->child_mtu_cached
!= mtu
) {
2607 xdst
->child_mtu_cached
= mtu
;
2610 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2612 mtu
= dst_mtu(xdst
->route
);
2613 if (xdst
->route_mtu_cached
!= mtu
) {
2615 xdst
->route_mtu_cached
= mtu
;
2619 } while (dst
->xfrm
);
2624 mtu
= last
->child_mtu_cached
;
2628 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2629 if (mtu
> last
->route_mtu_cached
)
2630 mtu
= last
->route_mtu_cached
;
2631 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2636 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2637 last
->child_mtu_cached
= mtu
;
2643 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2645 return dst_metric_advmss(dst
->path
);
2648 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2650 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2652 return mtu
? : dst_mtu(dst
->path
);
2655 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2656 struct sk_buff
*skb
,
2659 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2662 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2666 if (unlikely(afinfo
== NULL
))
2668 if (unlikely(afinfo
->family
>= NPROTO
))
2669 return -EAFNOSUPPORT
;
2670 spin_lock(&xfrm_policy_afinfo_lock
);
2671 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2674 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2675 if (likely(dst_ops
->kmem_cachep
== NULL
))
2676 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2677 if (likely(dst_ops
->check
== NULL
))
2678 dst_ops
->check
= xfrm_dst_check
;
2679 if (likely(dst_ops
->default_advmss
== NULL
))
2680 dst_ops
->default_advmss
= xfrm_default_advmss
;
2681 if (likely(dst_ops
->mtu
== NULL
))
2682 dst_ops
->mtu
= xfrm_mtu
;
2683 if (likely(dst_ops
->negative_advice
== NULL
))
2684 dst_ops
->negative_advice
= xfrm_negative_advice
;
2685 if (likely(dst_ops
->link_failure
== NULL
))
2686 dst_ops
->link_failure
= xfrm_link_failure
;
2687 if (likely(dst_ops
->neigh_lookup
== NULL
))
2688 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2689 if (likely(afinfo
->garbage_collect
== NULL
))
2690 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2691 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2693 spin_unlock(&xfrm_policy_afinfo_lock
);
2697 struct dst_ops
*xfrm_dst_ops
;
2699 switch (afinfo
->family
) {
2701 xfrm_dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
2703 #if IS_ENABLED(CONFIG_IPV6)
2705 xfrm_dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
2711 *xfrm_dst_ops
= *afinfo
->dst_ops
;
2717 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2719 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2722 if (unlikely(afinfo
== NULL
))
2724 if (unlikely(afinfo
->family
>= NPROTO
))
2725 return -EAFNOSUPPORT
;
2726 spin_lock(&xfrm_policy_afinfo_lock
);
2727 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2728 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2731 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2734 spin_unlock(&xfrm_policy_afinfo_lock
);
2736 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2740 dst_ops
->kmem_cachep
= NULL
;
2741 dst_ops
->check
= NULL
;
2742 dst_ops
->negative_advice
= NULL
;
2743 dst_ops
->link_failure
= NULL
;
2744 afinfo
->garbage_collect
= NULL
;
2748 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2750 static void __net_init
xfrm_dst_ops_init(struct net
*net
)
2752 struct xfrm_policy_afinfo
*afinfo
;
2755 afinfo
= rcu_dereference(xfrm_policy_afinfo
[AF_INET
]);
2757 net
->xfrm
.xfrm4_dst_ops
= *afinfo
->dst_ops
;
2758 #if IS_ENABLED(CONFIG_IPV6)
2759 afinfo
= rcu_dereference(xfrm_policy_afinfo
[AF_INET6
]);
2761 net
->xfrm
.xfrm6_dst_ops
= *afinfo
->dst_ops
;
2766 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2768 struct net_device
*dev
= ptr
;
2772 xfrm_garbage_collect(dev_net(dev
));
2777 static struct notifier_block xfrm_dev_notifier
= {
2778 .notifier_call
= xfrm_dev_event
,
2781 #ifdef CONFIG_XFRM_STATISTICS
2782 static int __net_init
xfrm_statistics_init(struct net
*net
)
2786 if (snmp_mib_init((void __percpu
**)net
->mib
.xfrm_statistics
,
2787 sizeof(struct linux_xfrm_mib
),
2788 __alignof__(struct linux_xfrm_mib
)) < 0)
2790 rv
= xfrm_proc_init(net
);
2792 snmp_mib_free((void __percpu
**)net
->mib
.xfrm_statistics
);
2796 static void xfrm_statistics_fini(struct net
*net
)
2798 xfrm_proc_fini(net
);
2799 snmp_mib_free((void __percpu
**)net
->mib
.xfrm_statistics
);
2802 static int __net_init
xfrm_statistics_init(struct net
*net
)
2807 static void xfrm_statistics_fini(struct net
*net
)
2812 static int __net_init
xfrm_policy_init(struct net
*net
)
2814 unsigned int hmask
, sz
;
2817 if (net_eq(net
, &init_net
))
2818 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2819 sizeof(struct xfrm_dst
),
2820 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2824 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2826 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2827 if (!net
->xfrm
.policy_byidx
)
2829 net
->xfrm
.policy_idx_hmask
= hmask
;
2831 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2832 struct xfrm_policy_hash
*htab
;
2834 net
->xfrm
.policy_count
[dir
] = 0;
2835 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2837 htab
= &net
->xfrm
.policy_bydst
[dir
];
2838 htab
->table
= xfrm_hash_alloc(sz
);
2841 htab
->hmask
= hmask
;
2844 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2845 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2846 if (net_eq(net
, &init_net
))
2847 register_netdevice_notifier(&xfrm_dev_notifier
);
2851 for (dir
--; dir
>= 0; dir
--) {
2852 struct xfrm_policy_hash
*htab
;
2854 htab
= &net
->xfrm
.policy_bydst
[dir
];
2855 xfrm_hash_free(htab
->table
, sz
);
2857 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2862 static void xfrm_policy_fini(struct net
*net
)
2864 struct xfrm_audit audit_info
;
2868 flush_work(&net
->xfrm
.policy_hash_work
);
2869 #ifdef CONFIG_XFRM_SUB_POLICY
2870 audit_info
.loginuid
= INVALID_UID
;
2871 audit_info
.sessionid
= -1;
2872 audit_info
.secid
= 0;
2873 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, &audit_info
);
2875 audit_info
.loginuid
= INVALID_UID
;
2876 audit_info
.sessionid
= -1;
2877 audit_info
.secid
= 0;
2878 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, &audit_info
);
2880 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
2882 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2883 struct xfrm_policy_hash
*htab
;
2885 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
2887 htab
= &net
->xfrm
.policy_bydst
[dir
];
2888 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
2889 WARN_ON(!hlist_empty(htab
->table
));
2890 xfrm_hash_free(htab
->table
, sz
);
2893 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
2894 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
2895 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2898 static int __net_init
xfrm_net_init(struct net
*net
)
2902 rv
= xfrm_statistics_init(net
);
2904 goto out_statistics
;
2905 rv
= xfrm_state_init(net
);
2908 rv
= xfrm_policy_init(net
);
2911 xfrm_dst_ops_init(net
);
2912 rv
= xfrm_sysctl_init(net
);
2918 xfrm_policy_fini(net
);
2920 xfrm_state_fini(net
);
2922 xfrm_statistics_fini(net
);
2927 static void __net_exit
xfrm_net_exit(struct net
*net
)
2929 xfrm_sysctl_fini(net
);
2930 xfrm_policy_fini(net
);
2931 xfrm_state_fini(net
);
2932 xfrm_statistics_fini(net
);
2935 static struct pernet_operations __net_initdata xfrm_net_ops
= {
2936 .init
= xfrm_net_init
,
2937 .exit
= xfrm_net_exit
,
2940 void __init
xfrm_init(void)
2942 register_pernet_subsys(&xfrm_net_ops
);
2946 #ifdef CONFIG_AUDITSYSCALL
2947 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2948 struct audit_buffer
*audit_buf
)
2950 struct xfrm_sec_ctx
*ctx
= xp
->security
;
2951 struct xfrm_selector
*sel
= &xp
->selector
;
2954 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2955 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2957 switch(sel
->family
) {
2959 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
2960 if (sel
->prefixlen_s
!= 32)
2961 audit_log_format(audit_buf
, " src_prefixlen=%d",
2963 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
2964 if (sel
->prefixlen_d
!= 32)
2965 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2969 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
2970 if (sel
->prefixlen_s
!= 128)
2971 audit_log_format(audit_buf
, " src_prefixlen=%d",
2973 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
2974 if (sel
->prefixlen_d
!= 128)
2975 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2981 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
,
2982 kuid_t auid
, u32 sessionid
, u32 secid
)
2984 struct audit_buffer
*audit_buf
;
2986 audit_buf
= xfrm_audit_start("SPD-add");
2987 if (audit_buf
== NULL
)
2989 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2990 audit_log_format(audit_buf
, " res=%u", result
);
2991 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2992 audit_log_end(audit_buf
);
2994 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2996 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
2997 kuid_t auid
, u32 sessionid
, u32 secid
)
2999 struct audit_buffer
*audit_buf
;
3001 audit_buf
= xfrm_audit_start("SPD-delete");
3002 if (audit_buf
== NULL
)
3004 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
3005 audit_log_format(audit_buf
, " res=%u", result
);
3006 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3007 audit_log_end(audit_buf
);
3009 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3012 #ifdef CONFIG_XFRM_MIGRATE
3013 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3014 const struct xfrm_selector
*sel_tgt
)
3016 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3017 if (sel_tgt
->family
== sel_cmp
->family
&&
3018 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3020 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3022 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3023 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3027 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3034 static struct xfrm_policy
* xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3037 struct xfrm_policy
*pol
, *ret
= NULL
;
3038 struct hlist_head
*chain
;
3041 read_lock_bh(&xfrm_policy_lock
);
3042 chain
= policy_hash_direct(&init_net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3043 hlist_for_each_entry(pol
, chain
, bydst
) {
3044 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3045 pol
->type
== type
) {
3047 priority
= ret
->priority
;
3051 chain
= &init_net
.xfrm
.policy_inexact
[dir
];
3052 hlist_for_each_entry(pol
, chain
, bydst
) {
3053 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3054 pol
->type
== type
&&
3055 pol
->priority
< priority
) {
3064 read_unlock_bh(&xfrm_policy_lock
);
3069 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3073 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3074 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3076 case XFRM_MODE_TUNNEL
:
3077 case XFRM_MODE_BEET
:
3078 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3080 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3085 case XFRM_MODE_TRANSPORT
:
3086 /* in case of transport mode, template does not store
3087 any IP addresses, hence we just compare mode and
3098 /* update endpoint address(es) of template(s) */
3099 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3100 struct xfrm_migrate
*m
, int num_migrate
)
3102 struct xfrm_migrate
*mp
;
3105 write_lock_bh(&pol
->lock
);
3106 if (unlikely(pol
->walk
.dead
)) {
3107 /* target policy has been deleted */
3108 write_unlock_bh(&pol
->lock
);
3112 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3113 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3114 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3117 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3118 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3120 /* update endpoints */
3121 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3122 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3123 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3124 sizeof(pol
->xfrm_vec
[i
].saddr
));
3125 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3127 atomic_inc(&pol
->genid
);
3131 write_unlock_bh(&pol
->lock
);
3139 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3143 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3146 for (i
= 0; i
< num_migrate
; i
++) {
3147 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3149 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3152 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3153 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3156 /* check if there is any duplicated entry */
3157 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3158 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3159 sizeof(m
[i
].old_daddr
)) &&
3160 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3161 sizeof(m
[i
].old_saddr
)) &&
3162 m
[i
].proto
== m
[j
].proto
&&
3163 m
[i
].mode
== m
[j
].mode
&&
3164 m
[i
].reqid
== m
[j
].reqid
&&
3165 m
[i
].old_family
== m
[j
].old_family
)
3173 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3174 struct xfrm_migrate
*m
, int num_migrate
,
3175 struct xfrm_kmaddress
*k
)
3177 int i
, err
, nx_cur
= 0, nx_new
= 0;
3178 struct xfrm_policy
*pol
= NULL
;
3179 struct xfrm_state
*x
, *xc
;
3180 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3181 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3182 struct xfrm_migrate
*mp
;
3184 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3187 /* Stage 1 - find policy */
3188 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
3193 /* Stage 2 - find and update state(s) */
3194 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3195 if ((x
= xfrm_migrate_state_find(mp
))) {
3198 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3208 /* Stage 3 - update policy */
3209 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3212 /* Stage 4 - delete old state(s) */
3214 xfrm_states_put(x_cur
, nx_cur
);
3215 xfrm_states_delete(x_cur
, nx_cur
);
3218 /* Stage 5 - announce */
3219 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3231 xfrm_states_put(x_cur
, nx_cur
);
3233 xfrm_states_delete(x_new
, nx_new
);
3237 EXPORT_SYMBOL(xfrm_migrate
);