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/cpu.h>
28 #include <linux/audit.h>
29 #include <linux/rhashtable.h>
30 #include <linux/if_tunnel.h>
35 #if IS_ENABLED(CONFIG_IPV6_MIP6)
38 #ifdef CONFIG_XFRM_STATISTICS
42 #include "xfrm_hash.h"
44 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
45 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
46 #define XFRM_MAX_QUEUE_LEN 100
49 struct dst_entry *dst_orig;
53 /* prefixes smaller than this are stored in lists, not trees. */
54 #define INEXACT_PREFIXLEN_IPV4 16
55 #define INEXACT_PREFIXLEN_IPV6 48
57 struct xfrm_pol_inexact_node {
67 /* the policies matching this node, can be empty list */
68 struct hlist_head hhead;
71 /* xfrm inexact policy search tree:
72 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
74 * +---- root_d: sorted by daddr:prefix
76 * | xfrm_pol_inexact_node
78 * | +- root: sorted by saddr/prefix
80 * | | xfrm_pol_inexact_node
84 * | | + hhead: saddr:daddr policies
86 * | +- coarse policies and all any:daddr policies
88 * +---- root_s: sorted by saddr:prefix
90 * | xfrm_pol_inexact_node
94 * | + hhead: saddr:any policies
96 * +---- coarse policies and all any:any policies
98 * Lookups return four candidate lists:
99 * 1. any:any list from top-level xfrm_pol_inexact_bin
100 * 2. any:daddr list from daddr tree
101 * 3. saddr:daddr list from 2nd level daddr tree
102 * 4. saddr:any list from saddr tree
104 * This result set then needs to be searched for the policy with
105 * the lowest priority. If two results have same prio, youngest one wins.
108 struct xfrm_pol_inexact_key {
115 struct xfrm_pol_inexact_bin {
116 struct xfrm_pol_inexact_key k;
117 struct rhash_head head;
118 /* list containing '*:*' policies */
119 struct hlist_head hhead;
122 /* tree sorted by daddr/prefix */
123 struct rb_root root_d;
125 /* tree sorted by saddr/prefix */
126 struct rb_root root_s;
128 /* slow path below */
129 struct list_head inexact_bins;
133 enum xfrm_pol_inexact_candidate_type {
142 struct xfrm_pol_inexact_candidates {
143 struct hlist_head *res[XFRM_POL_CAND_MAX];
146 static DEFINE_SPINLOCK(xfrm_if_cb_lock);
147 static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
149 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
150 static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
153 static struct kmem_cache *xfrm_dst_cache __ro_after_init;
154 static __read_mostly seqcount_t xfrm_policy_hash_generation;
156 static struct rhashtable xfrm_policy_inexact_table;
157 static const struct rhashtable_params xfrm_pol_inexact_params;
159 static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
160 static int stale_bundle(struct dst_entry *dst);
161 static int xfrm_bundle_ok(struct xfrm_dst *xdst);
162 static void xfrm_policy_queue_process(struct timer_list *t);
164 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
165 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
168 static struct xfrm_pol_inexact_bin *
169 xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
172 static struct xfrm_pol_inexact_bin *
173 xfrm_policy_inexact_lookup_rcu(struct net *net,
174 u8 type, u16 family, u8 dir, u32 if_id);
175 static struct xfrm_policy *
176 xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
178 static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
179 struct xfrm_policy *policy);
182 xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
183 struct xfrm_pol_inexact_bin *b,
184 const xfrm_address_t *saddr,
185 const xfrm_address_t *daddr);
187 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
189 return refcount_inc_not_zero(&policy->refcnt);
193 __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
195 const struct flowi4 *fl4 = &fl->u.ip4;
197 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
198 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
199 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
200 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
201 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
202 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
206 __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
208 const struct flowi6 *fl6 = &fl->u.ip6;
210 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
211 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
212 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
213 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
214 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
215 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
218 bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
219 unsigned short family)
223 return __xfrm4_selector_match(sel, fl);
225 return __xfrm6_selector_match(sel, fl);
230 static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
232 const struct xfrm_policy_afinfo *afinfo;
234 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
237 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
238 if (unlikely(!afinfo))
243 /* Called with rcu_read_lock(). */
244 static const struct xfrm_if_cb *xfrm_if_get_cb(void)
246 return rcu_dereference(xfrm_if_cb);
249 struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
250 const xfrm_address_t *saddr,
251 const xfrm_address_t *daddr,
252 int family, u32 mark)
254 const struct xfrm_policy_afinfo *afinfo;
255 struct dst_entry *dst;
257 afinfo = xfrm_policy_get_afinfo(family);
258 if (unlikely(afinfo == NULL))
259 return ERR_PTR(-EAFNOSUPPORT);
261 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
267 EXPORT_SYMBOL(__xfrm_dst_lookup);
269 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
271 xfrm_address_t *prev_saddr,
272 xfrm_address_t *prev_daddr,
273 int family, u32 mark)
275 struct net *net = xs_net(x);
276 xfrm_address_t *saddr = &x->props.saddr;
277 xfrm_address_t *daddr = &x->id.daddr;
278 struct dst_entry *dst;
280 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
284 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
289 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
292 if (prev_saddr != saddr)
293 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
294 if (prev_daddr != daddr)
295 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
301 static inline unsigned long make_jiffies(long secs)
303 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
304 return MAX_SCHEDULE_TIMEOUT-1;
309 static void xfrm_policy_timer(struct timer_list *t)
311 struct xfrm_policy *xp = from_timer(xp, t, timer);
312 time64_t now = ktime_get_real_seconds();
313 time64_t next = TIME64_MAX;
317 read_lock(&xp->lock);
319 if (unlikely(xp->walk.dead))
322 dir = xfrm_policy_id2dir(xp->index);
324 if (xp->lft.hard_add_expires_seconds) {
325 time64_t tmo = xp->lft.hard_add_expires_seconds +
326 xp->curlft.add_time - now;
332 if (xp->lft.hard_use_expires_seconds) {
333 time64_t tmo = xp->lft.hard_use_expires_seconds +
334 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
340 if (xp->lft.soft_add_expires_seconds) {
341 time64_t tmo = xp->lft.soft_add_expires_seconds +
342 xp->curlft.add_time - now;
345 tmo = XFRM_KM_TIMEOUT;
350 if (xp->lft.soft_use_expires_seconds) {
351 time64_t tmo = xp->lft.soft_use_expires_seconds +
352 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
355 tmo = XFRM_KM_TIMEOUT;
362 km_policy_expired(xp, dir, 0, 0);
363 if (next != TIME64_MAX &&
364 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
368 read_unlock(&xp->lock);
373 read_unlock(&xp->lock);
374 if (!xfrm_policy_delete(xp, dir))
375 km_policy_expired(xp, dir, 1, 0);
379 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
383 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
385 struct xfrm_policy *policy;
387 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
390 write_pnet(&policy->xp_net, net);
391 INIT_LIST_HEAD(&policy->walk.all);
392 INIT_HLIST_NODE(&policy->bydst_inexact_list);
393 INIT_HLIST_NODE(&policy->bydst);
394 INIT_HLIST_NODE(&policy->byidx);
395 rwlock_init(&policy->lock);
396 refcount_set(&policy->refcnt, 1);
397 skb_queue_head_init(&policy->polq.hold_queue);
398 timer_setup(&policy->timer, xfrm_policy_timer, 0);
399 timer_setup(&policy->polq.hold_timer,
400 xfrm_policy_queue_process, 0);
404 EXPORT_SYMBOL(xfrm_policy_alloc);
406 static void xfrm_policy_destroy_rcu(struct rcu_head *head)
408 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
410 security_xfrm_policy_free(policy->security);
414 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
416 void xfrm_policy_destroy(struct xfrm_policy *policy)
418 BUG_ON(!policy->walk.dead);
420 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
423 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
425 EXPORT_SYMBOL(xfrm_policy_destroy);
427 /* Rule must be locked. Release descendant resources, announce
428 * entry dead. The rule must be unlinked from lists to the moment.
431 static void xfrm_policy_kill(struct xfrm_policy *policy)
433 policy->walk.dead = 1;
435 atomic_inc(&policy->genid);
437 if (del_timer(&policy->polq.hold_timer))
438 xfrm_pol_put(policy);
439 skb_queue_purge(&policy->polq.hold_queue);
441 if (del_timer(&policy->timer))
442 xfrm_pol_put(policy);
444 xfrm_pol_put(policy);
447 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
449 static inline unsigned int idx_hash(struct net *net, u32 index)
451 return __idx_hash(index, net->xfrm.policy_idx_hmask);
454 /* calculate policy hash thresholds */
455 static void __get_hash_thresh(struct net *net,
456 unsigned short family, int dir,
457 u8 *dbits, u8 *sbits)
461 *dbits = net->xfrm.policy_bydst[dir].dbits4;
462 *sbits = net->xfrm.policy_bydst[dir].sbits4;
466 *dbits = net->xfrm.policy_bydst[dir].dbits6;
467 *sbits = net->xfrm.policy_bydst[dir].sbits6;
476 static struct hlist_head *policy_hash_bysel(struct net *net,
477 const struct xfrm_selector *sel,
478 unsigned short family, int dir)
480 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
485 __get_hash_thresh(net, family, dir, &dbits, &sbits);
486 hash = __sel_hash(sel, family, hmask, dbits, sbits);
488 if (hash == hmask + 1)
491 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
492 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
495 static struct hlist_head *policy_hash_direct(struct net *net,
496 const xfrm_address_t *daddr,
497 const xfrm_address_t *saddr,
498 unsigned short family, int dir)
500 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
505 __get_hash_thresh(net, family, dir, &dbits, &sbits);
506 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
508 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
509 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
512 static void xfrm_dst_hash_transfer(struct net *net,
513 struct hlist_head *list,
514 struct hlist_head *ndsttable,
515 unsigned int nhashmask,
518 struct hlist_node *tmp, *entry0 = NULL;
519 struct xfrm_policy *pol;
525 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
528 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
529 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
530 pol->family, nhashmask, dbits, sbits);
532 hlist_del_rcu(&pol->bydst);
533 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
538 hlist_del_rcu(&pol->bydst);
539 hlist_add_behind_rcu(&pol->bydst, entry0);
541 entry0 = &pol->bydst;
543 if (!hlist_empty(list)) {
549 static void xfrm_idx_hash_transfer(struct hlist_head *list,
550 struct hlist_head *nidxtable,
551 unsigned int nhashmask)
553 struct hlist_node *tmp;
554 struct xfrm_policy *pol;
556 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
559 h = __idx_hash(pol->index, nhashmask);
560 hlist_add_head(&pol->byidx, nidxtable+h);
564 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
566 return ((old_hmask + 1) << 1) - 1;
569 static void xfrm_bydst_resize(struct net *net, int dir)
571 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
572 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
573 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
574 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
575 struct hlist_head *odst;
581 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
582 write_seqcount_begin(&xfrm_policy_hash_generation);
584 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
585 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
587 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
588 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
590 for (i = hmask; i >= 0; i--)
591 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
593 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
594 net->xfrm.policy_bydst[dir].hmask = nhashmask;
596 write_seqcount_end(&xfrm_policy_hash_generation);
597 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
601 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
604 static void xfrm_byidx_resize(struct net *net, int total)
606 unsigned int hmask = net->xfrm.policy_idx_hmask;
607 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
608 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
609 struct hlist_head *oidx = net->xfrm.policy_byidx;
610 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
616 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
618 for (i = hmask; i >= 0; i--)
619 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
621 net->xfrm.policy_byidx = nidx;
622 net->xfrm.policy_idx_hmask = nhashmask;
624 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
626 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
629 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
631 unsigned int cnt = net->xfrm.policy_count[dir];
632 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
637 if ((hmask + 1) < xfrm_policy_hashmax &&
644 static inline int xfrm_byidx_should_resize(struct net *net, int total)
646 unsigned int hmask = net->xfrm.policy_idx_hmask;
648 if ((hmask + 1) < xfrm_policy_hashmax &&
655 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
657 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
658 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
659 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
660 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
661 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
662 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
663 si->spdhcnt = net->xfrm.policy_idx_hmask;
664 si->spdhmcnt = xfrm_policy_hashmax;
666 EXPORT_SYMBOL(xfrm_spd_getinfo);
668 static DEFINE_MUTEX(hash_resize_mutex);
669 static void xfrm_hash_resize(struct work_struct *work)
671 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
674 mutex_lock(&hash_resize_mutex);
677 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
678 if (xfrm_bydst_should_resize(net, dir, &total))
679 xfrm_bydst_resize(net, dir);
681 if (xfrm_byidx_should_resize(net, total))
682 xfrm_byidx_resize(net, total);
684 mutex_unlock(&hash_resize_mutex);
687 /* Make sure *pol can be inserted into fastbin.
688 * Useful to check that later insert requests will be sucessful
689 * (provided xfrm_policy_lock is held throughout).
691 static struct xfrm_pol_inexact_bin *
692 xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
694 struct xfrm_pol_inexact_bin *bin, *prev;
695 struct xfrm_pol_inexact_key k = {
696 .family = pol->family,
701 struct net *net = xp_net(pol);
703 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
705 write_pnet(&k.net, net);
706 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
707 xfrm_pol_inexact_params);
711 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
716 INIT_HLIST_HEAD(&bin->hhead);
717 bin->root_d = RB_ROOT;
718 bin->root_s = RB_ROOT;
719 seqcount_init(&bin->count);
721 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
723 xfrm_pol_inexact_params);
725 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
731 return IS_ERR(prev) ? NULL : prev;
734 static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
735 int family, u8 prefixlen)
737 if (xfrm_addr_any(addr, family))
740 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
743 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
750 xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
752 const xfrm_address_t *addr;
753 bool saddr_any, daddr_any;
756 addr = &policy->selector.saddr;
757 prefixlen = policy->selector.prefixlen_s;
759 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
762 addr = &policy->selector.daddr;
763 prefixlen = policy->selector.prefixlen_d;
764 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
767 return saddr_any && daddr_any;
770 static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
771 const xfrm_address_t *addr, u8 prefixlen)
774 node->prefixlen = prefixlen;
777 static struct xfrm_pol_inexact_node *
778 xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
780 struct xfrm_pol_inexact_node *node;
782 node = kzalloc(sizeof(*node), GFP_ATOMIC);
784 xfrm_pol_inexact_node_init(node, addr, prefixlen);
789 static int xfrm_policy_addr_delta(const xfrm_address_t *a,
790 const xfrm_address_t *b,
791 u8 prefixlen, u16 family)
793 unsigned int pdw, pbi;
798 if (sizeof(long) == 4 && prefixlen == 0)
799 return ntohl(a->a4) - ntohl(b->a4);
800 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
801 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
803 pdw = prefixlen >> 5;
804 pbi = prefixlen & 0x1f;
807 delta = memcmp(a->a6, b->a6, pdw << 2);
812 u32 mask = ~0u << (32 - pbi);
814 delta = (ntohl(a->a6[pdw]) & mask) -
815 (ntohl(b->a6[pdw]) & mask);
825 static void xfrm_policy_inexact_list_reinsert(struct net *net,
826 struct xfrm_pol_inexact_node *n,
829 unsigned int matched_s, matched_d;
830 struct xfrm_policy *policy, *p;
835 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
836 struct hlist_node *newpos = NULL;
837 bool matches_s, matches_d;
839 if (!policy->bydst_reinsert)
842 WARN_ON_ONCE(policy->family != family);
844 policy->bydst_reinsert = false;
845 hlist_for_each_entry(p, &n->hhead, bydst) {
846 if (policy->priority > p->priority)
848 else if (policy->priority == p->priority &&
849 policy->pos > p->pos)
856 hlist_add_behind_rcu(&policy->bydst, newpos);
858 hlist_add_head_rcu(&policy->bydst, &n->hhead);
860 /* paranoia checks follow.
861 * Check that the reinserted policy matches at least
862 * saddr or daddr for current node prefix.
864 * Matching both is fine, matching saddr in one policy
865 * (but not daddr) and then matching only daddr in another
868 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
872 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
876 if (matches_s && matches_d)
879 WARN_ON_ONCE(!matches_s && !matches_d);
884 WARN_ON_ONCE(matched_s && matched_d);
888 static void xfrm_policy_inexact_node_reinsert(struct net *net,
889 struct xfrm_pol_inexact_node *n,
893 struct xfrm_pol_inexact_node *node;
894 struct rb_node **p, *parent;
896 /* we should not have another subtree here */
897 WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root));
906 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
908 prefixlen = min(node->prefixlen, n->prefixlen);
910 delta = xfrm_policy_addr_delta(&n->addr, &node->addr,
913 p = &parent->rb_left;
914 } else if (delta > 0) {
915 p = &parent->rb_right;
917 struct xfrm_policy *tmp;
919 hlist_for_each_entry(tmp, &n->hhead, bydst) {
920 tmp->bydst_reinsert = true;
921 hlist_del_rcu(&tmp->bydst);
924 xfrm_policy_inexact_list_reinsert(net, node, family);
926 if (node->prefixlen == n->prefixlen) {
934 n->prefixlen = prefixlen;
939 rb_link_node_rcu(&n->node, parent, p);
940 rb_insert_color(&n->node, new);
943 /* merge nodes v and n */
944 static void xfrm_policy_inexact_node_merge(struct net *net,
945 struct xfrm_pol_inexact_node *v,
946 struct xfrm_pol_inexact_node *n,
949 struct xfrm_pol_inexact_node *node;
950 struct xfrm_policy *tmp;
951 struct rb_node *rnode;
953 /* To-be-merged node v has a subtree.
955 * Dismantle it and insert its nodes to n->root.
957 while ((rnode = rb_first(&v->root)) != NULL) {
958 node = rb_entry(rnode, struct xfrm_pol_inexact_node, node);
959 rb_erase(&node->node, &v->root);
960 xfrm_policy_inexact_node_reinsert(net, node, &n->root,
964 hlist_for_each_entry(tmp, &v->hhead, bydst) {
965 tmp->bydst_reinsert = true;
966 hlist_del_rcu(&tmp->bydst);
969 xfrm_policy_inexact_list_reinsert(net, n, family);
972 static struct xfrm_pol_inexact_node *
973 xfrm_policy_inexact_insert_node(struct net *net,
974 struct rb_root *root,
975 xfrm_address_t *addr,
976 u16 family, u8 prefixlen, u8 dir)
978 struct xfrm_pol_inexact_node *cached = NULL;
979 struct rb_node **p, *parent = NULL;
980 struct xfrm_pol_inexact_node *node;
987 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
989 delta = xfrm_policy_addr_delta(addr, &node->addr,
992 if (delta == 0 && prefixlen >= node->prefixlen) {
993 WARN_ON_ONCE(cached); /* ipsec policies got lost */
998 p = &parent->rb_left;
1000 p = &parent->rb_right;
1002 if (prefixlen < node->prefixlen) {
1003 delta = xfrm_policy_addr_delta(addr, &node->addr,
1009 /* This node is a subnet of the new prefix. It needs
1010 * to be removed and re-inserted with the smaller
1011 * prefix and all nodes that are now also covered
1012 * by the reduced prefixlen.
1014 rb_erase(&node->node, root);
1017 xfrm_pol_inexact_node_init(node, addr,
1021 /* This node also falls within the new
1022 * prefixlen. Merge the to-be-reinserted
1023 * node and this one.
1025 xfrm_policy_inexact_node_merge(net, node,
1027 kfree_rcu(node, rcu);
1038 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
1043 rb_link_node_rcu(&node->node, parent, p);
1044 rb_insert_color(&node->node, root);
1049 static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
1051 struct xfrm_pol_inexact_node *node;
1052 struct rb_node *rn = rb_first(r);
1055 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
1057 xfrm_policy_inexact_gc_tree(&node->root, rm);
1060 if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) {
1065 rb_erase(&node->node, r);
1066 kfree_rcu(node, rcu);
1070 static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
1072 write_seqcount_begin(&b->count);
1073 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
1074 xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
1075 write_seqcount_end(&b->count);
1077 if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
1078 !hlist_empty(&b->hhead)) {
1079 WARN_ON_ONCE(net_exit);
1083 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
1084 xfrm_pol_inexact_params) == 0) {
1085 list_del(&b->inexact_bins);
1090 static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
1092 struct net *net = read_pnet(&b->k.net);
1094 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1095 __xfrm_policy_inexact_prune_bin(b, false);
1096 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1099 static void __xfrm_policy_inexact_flush(struct net *net)
1101 struct xfrm_pol_inexact_bin *bin, *t;
1103 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1105 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1106 __xfrm_policy_inexact_prune_bin(bin, false);
1109 static struct hlist_head *
1110 xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1111 struct xfrm_policy *policy, u8 dir)
1113 struct xfrm_pol_inexact_node *n;
1116 net = xp_net(policy);
1117 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1119 if (xfrm_policy_inexact_insert_use_any_list(policy))
1122 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1124 policy->selector.prefixlen_d)) {
1125 write_seqcount_begin(&bin->count);
1126 n = xfrm_policy_inexact_insert_node(net,
1128 &policy->selector.saddr,
1130 policy->selector.prefixlen_s,
1132 write_seqcount_end(&bin->count);
1139 /* daddr is fixed */
1140 write_seqcount_begin(&bin->count);
1141 n = xfrm_policy_inexact_insert_node(net,
1143 &policy->selector.daddr,
1145 policy->selector.prefixlen_d, dir);
1146 write_seqcount_end(&bin->count);
1150 /* saddr is wildcard */
1151 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
1153 policy->selector.prefixlen_s))
1156 write_seqcount_begin(&bin->count);
1157 n = xfrm_policy_inexact_insert_node(net,
1159 &policy->selector.saddr,
1161 policy->selector.prefixlen_s, dir);
1162 write_seqcount_end(&bin->count);
1169 static struct xfrm_policy *
1170 xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1172 struct xfrm_pol_inexact_bin *bin;
1173 struct xfrm_policy *delpol;
1174 struct hlist_head *chain;
1177 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1179 return ERR_PTR(-ENOMEM);
1181 net = xp_net(policy);
1182 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1184 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1186 __xfrm_policy_inexact_prune_bin(bin, false);
1187 return ERR_PTR(-ENOMEM);
1190 delpol = xfrm_policy_insert_list(chain, policy, excl);
1191 if (delpol && excl) {
1192 __xfrm_policy_inexact_prune_bin(bin, false);
1193 return ERR_PTR(-EEXIST);
1196 chain = &net->xfrm.policy_inexact[dir];
1197 xfrm_policy_insert_inexact_list(chain, policy);
1200 __xfrm_policy_inexact_prune_bin(bin, false);
1205 static void xfrm_hash_rebuild(struct work_struct *work)
1207 struct net *net = container_of(work, struct net,
1208 xfrm.policy_hthresh.work);
1210 struct xfrm_policy *pol;
1211 struct xfrm_policy *policy;
1212 struct hlist_head *chain;
1213 struct hlist_head *odst;
1214 struct hlist_node *newpos;
1218 u8 lbits4, rbits4, lbits6, rbits6;
1220 mutex_lock(&hash_resize_mutex);
1222 /* read selector prefixlen thresholds */
1224 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1226 lbits4 = net->xfrm.policy_hthresh.lbits4;
1227 rbits4 = net->xfrm.policy_hthresh.rbits4;
1228 lbits6 = net->xfrm.policy_hthresh.lbits6;
1229 rbits6 = net->xfrm.policy_hthresh.rbits6;
1230 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1232 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1233 write_seqcount_begin(&xfrm_policy_hash_generation);
1235 /* make sure that we can insert the indirect policies again before
1236 * we start with destructive action.
1238 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
1239 struct xfrm_pol_inexact_bin *bin;
1242 dir = xfrm_policy_id2dir(policy->index);
1243 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1246 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1247 if (policy->family == AF_INET) {
1255 if (policy->family == AF_INET) {
1264 if (policy->selector.prefixlen_d < dbits ||
1265 policy->selector.prefixlen_s < sbits)
1268 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1272 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1276 /* reset the bydst and inexact table in all directions */
1277 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
1278 struct hlist_node *n;
1280 hlist_for_each_entry_safe(policy, n,
1281 &net->xfrm.policy_inexact[dir],
1283 hlist_del_init(&policy->bydst_inexact_list);
1285 hmask = net->xfrm.policy_bydst[dir].hmask;
1286 odst = net->xfrm.policy_bydst[dir].table;
1287 for (i = hmask; i >= 0; i--)
1288 INIT_HLIST_HEAD(odst + i);
1289 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1290 /* dir out => dst = remote, src = local */
1291 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1292 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1293 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1294 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1296 /* dir in/fwd => dst = local, src = remote */
1297 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1298 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1299 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1300 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1304 /* re-insert all policies by order of creation */
1305 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
1306 if (policy->walk.dead)
1308 dir = xfrm_policy_id2dir(policy->index);
1309 if (dir >= XFRM_POLICY_MAX) {
1310 /* skip socket policies */
1314 chain = policy_hash_bysel(net, &policy->selector,
1315 policy->family, dir);
1317 hlist_del_rcu(&policy->bydst);
1320 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1322 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1326 hlist_for_each_entry(pol, chain, bydst) {
1327 if (policy->priority >= pol->priority)
1328 newpos = &pol->bydst;
1333 hlist_add_behind_rcu(&policy->bydst, newpos);
1335 hlist_add_head_rcu(&policy->bydst, chain);
1339 __xfrm_policy_inexact_flush(net);
1340 write_seqcount_end(&xfrm_policy_hash_generation);
1341 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1343 mutex_unlock(&hash_resize_mutex);
1346 void xfrm_policy_hash_rebuild(struct net *net)
1348 schedule_work(&net->xfrm.policy_hthresh.work);
1350 EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1352 /* Generate new index... KAME seems to generate them ordered by cost
1353 * of an absolute inpredictability of ordering of rules. This will not pass. */
1354 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
1356 static u32 idx_generator;
1359 struct hlist_head *list;
1360 struct xfrm_policy *p;
1365 idx = (idx_generator | dir);
1374 list = net->xfrm.policy_byidx + idx_hash(net, idx);
1376 hlist_for_each_entry(p, list, byidx) {
1377 if (p->index == idx) {
1387 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1389 u32 *p1 = (u32 *) s1;
1390 u32 *p2 = (u32 *) s2;
1391 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1394 for (i = 0; i < len; i++) {
1402 static void xfrm_policy_requeue(struct xfrm_policy *old,
1403 struct xfrm_policy *new)
1405 struct xfrm_policy_queue *pq = &old->polq;
1406 struct sk_buff_head list;
1408 if (skb_queue_empty(&pq->hold_queue))
1411 __skb_queue_head_init(&list);
1413 spin_lock_bh(&pq->hold_queue.lock);
1414 skb_queue_splice_init(&pq->hold_queue, &list);
1415 if (del_timer(&pq->hold_timer))
1417 spin_unlock_bh(&pq->hold_queue.lock);
1421 spin_lock_bh(&pq->hold_queue.lock);
1422 skb_queue_splice(&list, &pq->hold_queue);
1423 pq->timeout = XFRM_QUEUE_TMO_MIN;
1424 if (!mod_timer(&pq->hold_timer, jiffies))
1426 spin_unlock_bh(&pq->hold_queue.lock);
1429 static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1430 struct xfrm_policy *pol)
1432 u32 mark = policy->mark.v & policy->mark.m;
1434 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1437 if ((mark & pol->mark.m) == pol->mark.v &&
1438 policy->priority == pol->priority)
1444 static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1446 const struct xfrm_pol_inexact_key *k = data;
1447 u32 a = k->type << 24 | k->dir << 16 | k->family;
1449 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1453 static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1455 const struct xfrm_pol_inexact_bin *b = data;
1457 return xfrm_pol_bin_key(&b->k, 0, seed);
1460 static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1463 const struct xfrm_pol_inexact_key *key = arg->key;
1464 const struct xfrm_pol_inexact_bin *b = ptr;
1467 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1470 ret = b->k.dir ^ key->dir;
1474 ret = b->k.type ^ key->type;
1478 ret = b->k.family ^ key->family;
1482 return b->k.if_id ^ key->if_id;
1485 static const struct rhashtable_params xfrm_pol_inexact_params = {
1486 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1487 .hashfn = xfrm_pol_bin_key,
1488 .obj_hashfn = xfrm_pol_bin_obj,
1489 .obj_cmpfn = xfrm_pol_bin_cmp,
1490 .automatic_shrinking = true,
1493 static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1494 struct xfrm_policy *policy)
1496 struct xfrm_policy *pol, *delpol = NULL;
1497 struct hlist_node *newpos = NULL;
1500 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1501 if (pol->type == policy->type &&
1502 pol->if_id == policy->if_id &&
1503 !selector_cmp(&pol->selector, &policy->selector) &&
1504 xfrm_policy_mark_match(policy, pol) &&
1505 xfrm_sec_ctx_match(pol->security, policy->security) &&
1508 if (policy->priority > pol->priority)
1510 } else if (policy->priority >= pol->priority) {
1511 newpos = &pol->bydst_inexact_list;
1519 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1521 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
1523 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1529 static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1530 struct xfrm_policy *policy,
1533 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
1535 hlist_for_each_entry(pol, chain, bydst) {
1536 if (pol->type == policy->type &&
1537 pol->if_id == policy->if_id &&
1538 !selector_cmp(&pol->selector, &policy->selector) &&
1539 xfrm_policy_mark_match(policy, pol) &&
1540 xfrm_sec_ctx_match(pol->security, policy->security) &&
1543 return ERR_PTR(-EEXIST);
1545 if (policy->priority > pol->priority)
1547 } else if (policy->priority >= pol->priority) {
1556 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
1558 hlist_add_head_rcu(&policy->bydst, chain);
1563 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1565 struct net *net = xp_net(policy);
1566 struct xfrm_policy *delpol;
1567 struct hlist_head *chain;
1569 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1570 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
1572 delpol = xfrm_policy_insert_list(chain, policy, excl);
1574 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
1576 if (IS_ERR(delpol)) {
1577 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1578 return PTR_ERR(delpol);
1581 __xfrm_policy_link(policy, dir);
1583 /* After previous checking, family can either be AF_INET or AF_INET6 */
1584 if (policy->family == AF_INET)
1585 rt_genid_bump_ipv4(net);
1587 rt_genid_bump_ipv6(net);
1590 xfrm_policy_requeue(delpol, policy);
1591 __xfrm_policy_unlink(delpol, dir);
1593 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
1594 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
1595 policy->curlft.add_time = ktime_get_real_seconds();
1596 policy->curlft.use_time = 0;
1597 if (!mod_timer(&policy->timer, jiffies + HZ))
1598 xfrm_pol_hold(policy);
1599 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1602 xfrm_policy_kill(delpol);
1603 else if (xfrm_bydst_should_resize(net, dir, NULL))
1604 schedule_work(&net->xfrm.policy_hash_work);
1608 EXPORT_SYMBOL(xfrm_policy_insert);
1610 static struct xfrm_policy *
1611 __xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1613 struct xfrm_selector *sel,
1614 struct xfrm_sec_ctx *ctx)
1616 struct xfrm_policy *pol;
1621 hlist_for_each_entry(pol, chain, bydst) {
1622 if (pol->type == type &&
1623 pol->if_id == if_id &&
1624 (mark & pol->mark.m) == pol->mark.v &&
1625 !selector_cmp(sel, &pol->selector) &&
1626 xfrm_sec_ctx_match(ctx, pol->security))
1633 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1635 struct xfrm_selector *sel,
1636 struct xfrm_sec_ctx *ctx, int delete,
1639 struct xfrm_pol_inexact_bin *bin = NULL;
1640 struct xfrm_policy *pol, *ret = NULL;
1641 struct hlist_head *chain;
1644 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1645 chain = policy_hash_bysel(net, sel, sel->family, dir);
1647 struct xfrm_pol_inexact_candidates cand;
1650 bin = xfrm_policy_inexact_lookup(net, type,
1651 sel->family, dir, if_id);
1653 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1657 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1660 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1665 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1666 struct xfrm_policy *tmp;
1668 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1674 if (!pol || tmp->pos < pol->pos)
1678 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1685 *err = security_xfrm_policy_delete(pol->security);
1687 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1690 __xfrm_policy_unlink(pol, dir);
1694 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1697 xfrm_policy_kill(ret);
1699 xfrm_policy_inexact_prune_bin(bin);
1702 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
1704 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1705 u8 type, int dir, u32 id, int delete,
1708 struct xfrm_policy *pol, *ret;
1709 struct hlist_head *chain;
1712 if (xfrm_policy_id2dir(id) != dir)
1716 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1717 chain = net->xfrm.policy_byidx + idx_hash(net, id);
1719 hlist_for_each_entry(pol, chain, byidx) {
1720 if (pol->type == type && pol->index == id &&
1721 pol->if_id == if_id &&
1722 (mark & pol->mark.m) == pol->mark.v) {
1725 *err = security_xfrm_policy_delete(
1728 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1731 __xfrm_policy_unlink(pol, dir);
1737 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1740 xfrm_policy_kill(ret);
1743 EXPORT_SYMBOL(xfrm_policy_byid);
1745 #ifdef CONFIG_SECURITY_NETWORK_XFRM
1747 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
1749 struct xfrm_policy *pol;
1752 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1753 if (pol->walk.dead ||
1754 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1758 err = security_xfrm_policy_delete(pol->security);
1760 xfrm_audit_policy_delete(pol, 0, task_valid);
1768 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
1774 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
1776 int dir, err = 0, cnt = 0;
1777 struct xfrm_policy *pol;
1779 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1781 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
1786 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1787 dir = xfrm_policy_id2dir(pol->index);
1788 if (pol->walk.dead ||
1789 dir >= XFRM_POLICY_MAX ||
1793 __xfrm_policy_unlink(pol, dir);
1794 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1796 xfrm_audit_policy_delete(pol, 1, task_valid);
1797 xfrm_policy_kill(pol);
1798 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1802 __xfrm_policy_inexact_flush(net);
1806 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1809 EXPORT_SYMBOL(xfrm_policy_flush);
1811 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1812 int (*func)(struct xfrm_policy *, int, int, void*),
1815 struct xfrm_policy *pol;
1816 struct xfrm_policy_walk_entry *x;
1819 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1820 walk->type != XFRM_POLICY_TYPE_ANY)
1823 if (list_empty(&walk->walk.all) && walk->seq != 0)
1826 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1827 if (list_empty(&walk->walk.all))
1828 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
1830 x = list_first_entry(&walk->walk.all,
1831 struct xfrm_policy_walk_entry, all);
1833 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
1836 pol = container_of(x, struct xfrm_policy, walk);
1837 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1838 walk->type != pol->type)
1840 error = func(pol, xfrm_policy_id2dir(pol->index),
1843 list_move_tail(&walk->walk.all, &x->all);
1848 if (walk->seq == 0) {
1852 list_del_init(&walk->walk.all);
1854 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1857 EXPORT_SYMBOL(xfrm_policy_walk);
1859 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1861 INIT_LIST_HEAD(&walk->walk.all);
1862 walk->walk.dead = 1;
1866 EXPORT_SYMBOL(xfrm_policy_walk_init);
1868 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
1870 if (list_empty(&walk->walk.all))
1873 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
1874 list_del(&walk->walk.all);
1875 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1877 EXPORT_SYMBOL(xfrm_policy_walk_done);
1880 * Find policy to apply to this flow.
1882 * Returns 0 if policy found, else an -errno.
1884 static int xfrm_policy_match(const struct xfrm_policy *pol,
1885 const struct flowi *fl,
1886 u8 type, u16 family, int dir, u32 if_id)
1888 const struct xfrm_selector *sel = &pol->selector;
1892 if (pol->family != family ||
1893 pol->if_id != if_id ||
1894 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
1898 match = xfrm_selector_match(sel, fl, family);
1900 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1905 static struct xfrm_pol_inexact_node *
1906 xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
1908 const xfrm_address_t *addr, u16 family)
1910 const struct rb_node *parent;
1914 seq = read_seqcount_begin(count);
1916 parent = rcu_dereference_raw(r->rb_node);
1918 struct xfrm_pol_inexact_node *node;
1921 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1923 delta = xfrm_policy_addr_delta(addr, &node->addr,
1924 node->prefixlen, family);
1926 parent = rcu_dereference_raw(parent->rb_left);
1928 } else if (delta > 0) {
1929 parent = rcu_dereference_raw(parent->rb_right);
1936 if (read_seqcount_retry(count, seq))
1943 xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1944 struct xfrm_pol_inexact_bin *b,
1945 const xfrm_address_t *saddr,
1946 const xfrm_address_t *daddr)
1948 struct xfrm_pol_inexact_node *n;
1954 family = b->k.family;
1955 memset(cand, 0, sizeof(*cand));
1956 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
1958 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1961 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
1962 n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr,
1965 cand->res[XFRM_POL_CAND_BOTH] = &n->hhead;
1968 n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
1971 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
1976 static struct xfrm_pol_inexact_bin *
1977 xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1980 struct xfrm_pol_inexact_key k = {
1987 write_pnet(&k.net, net);
1989 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1990 xfrm_pol_inexact_params);
1993 static struct xfrm_pol_inexact_bin *
1994 xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1997 struct xfrm_pol_inexact_bin *bin;
1999 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
2002 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
2008 static struct xfrm_policy *
2009 __xfrm_policy_eval_candidates(struct hlist_head *chain,
2010 struct xfrm_policy *prefer,
2011 const struct flowi *fl,
2012 u8 type, u16 family, int dir, u32 if_id)
2014 u32 priority = prefer ? prefer->priority : ~0u;
2015 struct xfrm_policy *pol;
2020 hlist_for_each_entry_rcu(pol, chain, bydst) {
2023 if (pol->priority > priority)
2026 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
2029 return ERR_PTR(err);
2035 /* matches. Is it older than *prefer? */
2036 if (pol->priority == priority &&
2037 prefer->pos < pol->pos)
2047 static struct xfrm_policy *
2048 xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
2049 struct xfrm_policy *prefer,
2050 const struct flowi *fl,
2051 u8 type, u16 family, int dir, u32 if_id)
2053 struct xfrm_policy *tmp;
2056 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
2057 tmp = __xfrm_policy_eval_candidates(cand->res[i],
2059 fl, type, family, dir,
2072 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
2073 const struct flowi *fl,
2077 struct xfrm_pol_inexact_candidates cand;
2078 const xfrm_address_t *daddr, *saddr;
2079 struct xfrm_pol_inexact_bin *bin;
2080 struct xfrm_policy *pol, *ret;
2081 struct hlist_head *chain;
2082 unsigned int sequence;
2085 daddr = xfrm_flowi_daddr(fl, family);
2086 saddr = xfrm_flowi_saddr(fl, family);
2087 if (unlikely(!daddr || !saddr))
2093 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
2094 chain = policy_hash_direct(net, daddr, saddr, family, dir);
2095 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
2098 hlist_for_each_entry_rcu(pol, chain, bydst) {
2099 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
2112 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
2113 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
2117 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
2118 family, dir, if_id);
2126 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
2129 if (ret && !xfrm_pol_hold_rcu(ret))
2137 static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
2138 const struct flowi *fl,
2139 u16 family, u8 dir, u32 if_id)
2141 #ifdef CONFIG_XFRM_SUB_POLICY
2142 struct xfrm_policy *pol;
2144 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2149 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2153 static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
2154 const struct flowi *fl,
2155 u16 family, u32 if_id)
2157 struct xfrm_policy *pol;
2161 pol = rcu_dereference(sk->sk_policy[dir]);
2166 if (pol->family != family) {
2171 match = xfrm_selector_match(&pol->selector, fl, family);
2173 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
2174 pol->if_id != if_id) {
2178 err = security_xfrm_policy_lookup(pol->security,
2182 if (!xfrm_pol_hold_rcu(pol))
2184 } else if (err == -ESRCH) {
2197 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2199 struct net *net = xp_net(pol);
2201 list_add(&pol->walk.all, &net->xfrm.policy_all);
2202 net->xfrm.policy_count[dir]++;
2206 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2209 struct net *net = xp_net(pol);
2211 if (list_empty(&pol->walk.all))
2214 /* Socket policies are not hashed. */
2215 if (!hlist_unhashed(&pol->bydst)) {
2216 hlist_del_rcu(&pol->bydst);
2217 hlist_del_init(&pol->bydst_inexact_list);
2218 hlist_del(&pol->byidx);
2221 list_del_init(&pol->walk.all);
2222 net->xfrm.policy_count[dir]--;
2227 static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2229 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2232 static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2234 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2237 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
2239 struct net *net = xp_net(pol);
2241 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
2242 pol = __xfrm_policy_unlink(pol, dir);
2243 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
2245 xfrm_policy_kill(pol);
2250 EXPORT_SYMBOL(xfrm_policy_delete);
2252 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2254 struct net *net = sock_net(sk);
2255 struct xfrm_policy *old_pol;
2257 #ifdef CONFIG_XFRM_SUB_POLICY
2258 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2262 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
2263 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2264 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
2266 pol->curlft.add_time = ktime_get_real_seconds();
2267 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
2268 xfrm_sk_policy_link(pol, dir);
2270 rcu_assign_pointer(sk->sk_policy[dir], pol);
2273 xfrm_policy_requeue(old_pol, pol);
2275 /* Unlinking succeeds always. This is the only function
2276 * allowed to delete or replace socket policy.
2278 xfrm_sk_policy_unlink(old_pol, dir);
2280 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
2283 xfrm_policy_kill(old_pol);
2288 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
2290 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
2291 struct net *net = xp_net(old);
2294 newp->selector = old->selector;
2295 if (security_xfrm_policy_clone(old->security,
2298 return NULL; /* ENOMEM */
2300 newp->lft = old->lft;
2301 newp->curlft = old->curlft;
2302 newp->mark = old->mark;
2303 newp->if_id = old->if_id;
2304 newp->action = old->action;
2305 newp->flags = old->flags;
2306 newp->xfrm_nr = old->xfrm_nr;
2307 newp->index = old->index;
2308 newp->type = old->type;
2309 newp->family = old->family;
2310 memcpy(newp->xfrm_vec, old->xfrm_vec,
2311 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
2312 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
2313 xfrm_sk_policy_link(newp, dir);
2314 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
2320 int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
2322 const struct xfrm_policy *p;
2323 struct xfrm_policy *np;
2327 for (i = 0; i < 2; i++) {
2328 p = rcu_dereference(osk->sk_policy[i]);
2330 np = clone_policy(p, i);
2331 if (unlikely(!np)) {
2335 rcu_assign_pointer(sk->sk_policy[i], np);
2343 xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
2344 xfrm_address_t *remote, unsigned short family, u32 mark)
2347 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2349 if (unlikely(afinfo == NULL))
2351 err = afinfo->get_saddr(net, oif, local, remote, mark);
2356 /* Resolve list of templates for the flow, given policy. */
2359 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2360 struct xfrm_state **xfrm, unsigned short family)
2362 struct net *net = xp_net(policy);
2365 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2366 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
2369 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
2370 struct xfrm_state *x;
2371 xfrm_address_t *remote = daddr;
2372 xfrm_address_t *local = saddr;
2373 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2375 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2376 tmpl->mode == XFRM_MODE_BEET) {
2377 remote = &tmpl->id.daddr;
2378 local = &tmpl->saddr;
2379 if (xfrm_addr_any(local, tmpl->encap_family)) {
2380 error = xfrm_get_saddr(net, fl->flowi_oif,
2382 tmpl->encap_family, 0);
2389 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2390 family, policy->if_id);
2392 if (x && x->km.state == XFRM_STATE_VALID) {
2399 error = (x->km.state == XFRM_STATE_ERROR ?
2402 } else if (error == -ESRCH) {
2406 if (!tmpl->optional)
2412 for (nx--; nx >= 0; nx--)
2413 xfrm_state_put(xfrm[nx]);
2418 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2419 struct xfrm_state **xfrm, unsigned short family)
2421 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2422 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
2428 for (i = 0; i < npols; i++) {
2429 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2434 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
2442 /* found states are sorted for outbound processing */
2444 xfrm_state_sort(xfrm, tpp, cnx, family);
2449 for (cnx--; cnx >= 0; cnx--)
2450 xfrm_state_put(tpp[cnx]);
2455 static int xfrm_get_tos(const struct flowi *fl, int family)
2457 if (family == AF_INET)
2458 return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos;
2463 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
2465 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2466 struct dst_ops *dst_ops;
2467 struct xfrm_dst *xdst;
2470 return ERR_PTR(-EINVAL);
2474 dst_ops = &net->xfrm.xfrm4_dst_ops;
2476 #if IS_ENABLED(CONFIG_IPV6)
2478 dst_ops = &net->xfrm.xfrm6_dst_ops;
2484 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
2487 struct dst_entry *dst = &xdst->u.dst;
2489 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
2491 xdst = ERR_PTR(-ENOBUFS);
2498 static void xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2501 if (dst->ops->family == AF_INET6) {
2502 struct rt6_info *rt = (struct rt6_info *)dst;
2503 path->path_cookie = rt6_get_cookie(rt);
2504 path->u.rt6.rt6i_nfheader_len = nfheader_len;
2508 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
2509 const struct flowi *fl)
2511 const struct xfrm_policy_afinfo *afinfo =
2512 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2518 err = afinfo->fill_dst(xdst, dev, fl);
2526 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
2527 * all the metrics... Shortly, bundle a bundle.
2530 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
2531 struct xfrm_state **xfrm,
2532 struct xfrm_dst **bundle,
2534 const struct flowi *fl,
2535 struct dst_entry *dst)
2537 const struct xfrm_state_afinfo *afinfo;
2538 const struct xfrm_mode *inner_mode;
2539 struct net *net = xp_net(policy);
2540 unsigned long now = jiffies;
2541 struct net_device *dev;
2542 struct xfrm_dst *xdst_prev = NULL;
2543 struct xfrm_dst *xdst0 = NULL;
2547 int nfheader_len = 0;
2548 int trailer_len = 0;
2550 int family = policy->selector.family;
2551 xfrm_address_t saddr, daddr;
2553 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
2555 tos = xfrm_get_tos(fl, family);
2559 for (; i < nx; i++) {
2560 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
2561 struct dst_entry *dst1 = &xdst->u.dst;
2563 err = PTR_ERR(xdst);
2573 /* Ref count is taken during xfrm_alloc_dst()
2574 * No need to do dst_clone() on dst1
2576 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
2578 if (xfrm[i]->sel.family == AF_UNSPEC) {
2579 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2580 xfrm_af2proto(family));
2582 err = -EAFNOSUPPORT;
2587 inner_mode = &xfrm[i]->inner_mode;
2590 dst_copy_metrics(dst1, dst);
2592 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
2595 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
2596 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
2598 family = xfrm[i]->props.family;
2599 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
2600 &saddr, &daddr, family, mark);
2607 dst1->xfrm = xfrm[i];
2608 xdst->xfrm_genid = xfrm[i]->genid;
2610 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2611 dst1->flags |= DST_HOST;
2612 dst1->lastuse = now;
2614 dst1->input = dst_discard;
2617 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
2619 dst1->output = afinfo->output;
2621 dst1->output = dst_discard_out;
2626 header_len += xfrm[i]->props.header_len;
2627 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2628 nfheader_len += xfrm[i]->props.header_len;
2629 trailer_len += xfrm[i]->props.trailer_len;
2632 xfrm_dst_set_child(xdst_prev, dst);
2640 xfrm_init_path(xdst0, dst, nfheader_len);
2641 xfrm_init_pmtu(bundle, nx);
2643 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2644 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2645 err = xfrm_fill_dst(xdst_prev, dev, fl);
2649 xdst_prev->u.dst.header_len = header_len;
2650 xdst_prev->u.dst.trailer_len = trailer_len;
2651 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2652 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
2655 return &xdst0->u.dst;
2659 xfrm_state_put(xfrm[i]);
2662 dst_release_immediate(&xdst0->u.dst);
2664 return ERR_PTR(err);
2667 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
2668 struct xfrm_policy **pols,
2669 int *num_pols, int *num_xfrms)
2673 if (*num_pols == 0 || !pols[0]) {
2678 if (IS_ERR(pols[0]))
2679 return PTR_ERR(pols[0]);
2681 *num_xfrms = pols[0]->xfrm_nr;
2683 #ifdef CONFIG_XFRM_SUB_POLICY
2684 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2685 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2686 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2687 XFRM_POLICY_TYPE_MAIN,
2692 if (IS_ERR(pols[1])) {
2693 xfrm_pols_put(pols, *num_pols);
2694 return PTR_ERR(pols[1]);
2697 (*num_xfrms) += pols[1]->xfrm_nr;
2701 for (i = 0; i < *num_pols; i++) {
2702 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2712 static struct xfrm_dst *
2713 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
2714 const struct flowi *fl, u16 family,
2715 struct dst_entry *dst_orig)
2717 struct net *net = xp_net(pols[0]);
2718 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
2719 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
2720 struct xfrm_dst *xdst;
2721 struct dst_entry *dst;
2724 /* Try to instantiate a bundle */
2725 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
2731 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2732 return ERR_PTR(err);
2735 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
2737 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2738 return ERR_CAST(dst);
2741 xdst = (struct xfrm_dst *)dst;
2742 xdst->num_xfrms = err;
2743 xdst->num_pols = num_pols;
2744 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2745 xdst->policy_genid = atomic_read(&pols[0]->genid);
2750 static void xfrm_policy_queue_process(struct timer_list *t)
2752 struct sk_buff *skb;
2754 struct dst_entry *dst;
2755 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
2756 struct net *net = xp_net(pol);
2757 struct xfrm_policy_queue *pq = &pol->polq;
2759 struct sk_buff_head list;
2761 spin_lock(&pq->hold_queue.lock);
2762 skb = skb_peek(&pq->hold_queue);
2764 spin_unlock(&pq->hold_queue.lock);
2769 xfrm_decode_session(skb, &fl, dst->ops->family);
2770 spin_unlock(&pq->hold_queue.lock);
2772 dst_hold(xfrm_dst_path(dst));
2773 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
2777 if (dst->flags & DST_XFRM_QUEUE) {
2780 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2783 pq->timeout = pq->timeout << 1;
2784 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2791 __skb_queue_head_init(&list);
2793 spin_lock(&pq->hold_queue.lock);
2795 skb_queue_splice_init(&pq->hold_queue, &list);
2796 spin_unlock(&pq->hold_queue.lock);
2798 while (!skb_queue_empty(&list)) {
2799 skb = __skb_dequeue(&list);
2801 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
2802 dst_hold(xfrm_dst_path(skb_dst(skb)));
2803 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
2811 skb_dst_set(skb, dst);
2813 dst_output(net, skb->sk, skb);
2822 skb_queue_purge(&pq->hold_queue);
2826 static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
2828 unsigned long sched_next;
2829 struct dst_entry *dst = skb_dst(skb);
2830 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
2831 struct xfrm_policy *pol = xdst->pols[0];
2832 struct xfrm_policy_queue *pq = &pol->polq;
2834 if (unlikely(skb_fclone_busy(sk, skb))) {
2839 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2846 spin_lock_bh(&pq->hold_queue.lock);
2849 pq->timeout = XFRM_QUEUE_TMO_MIN;
2851 sched_next = jiffies + pq->timeout;
2853 if (del_timer(&pq->hold_timer)) {
2854 if (time_before(pq->hold_timer.expires, sched_next))
2855 sched_next = pq->hold_timer.expires;
2859 __skb_queue_tail(&pq->hold_queue, skb);
2860 if (!mod_timer(&pq->hold_timer, sched_next))
2863 spin_unlock_bh(&pq->hold_queue.lock);
2868 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
2869 struct xfrm_flo *xflo,
2870 const struct flowi *fl,
2875 struct net_device *dev;
2876 struct dst_entry *dst;
2877 struct dst_entry *dst1;
2878 struct xfrm_dst *xdst;
2880 xdst = xfrm_alloc_dst(net, family);
2884 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2885 net->xfrm.sysctl_larval_drop ||
2889 dst = xflo->dst_orig;
2890 dst1 = &xdst->u.dst;
2894 dst_copy_metrics(dst1, dst);
2896 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2897 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2898 dst1->lastuse = jiffies;
2900 dst1->input = dst_discard;
2901 dst1->output = xdst_queue_output;
2904 xfrm_dst_set_child(xdst, dst);
2907 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2914 err = xfrm_fill_dst(xdst, dev, fl);
2923 xdst = ERR_PTR(err);
2927 static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2928 const struct flowi *fl,
2930 struct xfrm_flo *xflo, u32 if_id)
2932 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2933 int num_pols = 0, num_xfrms = 0, err;
2934 struct xfrm_dst *xdst;
2936 /* Resolve policies to use if we couldn't get them from
2937 * previous cache entry */
2939 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
2940 err = xfrm_expand_policies(fl, family, pols,
2941 &num_pols, &num_xfrms);
2947 goto make_dummy_bundle;
2949 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2952 err = PTR_ERR(xdst);
2953 if (err == -EREMOTE) {
2954 xfrm_pols_put(pols, num_pols);
2960 goto make_dummy_bundle;
2961 } else if (xdst == NULL) {
2963 goto make_dummy_bundle;
2969 /* We found policies, but there's no bundles to instantiate:
2970 * either because the policy blocks, has no transformations or
2971 * we could not build template (no xfrm_states).*/
2972 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
2974 xfrm_pols_put(pols, num_pols);
2975 return ERR_CAST(xdst);
2977 xdst->num_pols = num_pols;
2978 xdst->num_xfrms = num_xfrms;
2979 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2984 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2986 xfrm_pols_put(pols, num_pols);
2987 return ERR_PTR(err);
2990 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2991 struct dst_entry *dst_orig)
2993 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2994 struct dst_entry *ret;
2997 dst_release(dst_orig);
2998 return ERR_PTR(-EINVAL);
3000 ret = afinfo->blackhole_route(net, dst_orig);
3007 /* Finds/creates a bundle for given flow and if_id
3009 * At the moment we eat a raw IP route. Mostly to speed up lookups
3010 * on interfaces with disabled IPsec.
3012 * xfrm_lookup uses an if_id of 0 by default, and is provided for
3015 struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
3016 struct dst_entry *dst_orig,
3017 const struct flowi *fl,
3018 const struct sock *sk,
3019 int flags, u32 if_id)
3021 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3022 struct xfrm_dst *xdst;
3023 struct dst_entry *dst, *route;
3024 u16 family = dst_orig->ops->family;
3025 u8 dir = XFRM_POLICY_OUT;
3026 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
3032 sk = sk_const_to_full_sk(sk);
3033 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
3035 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
3037 err = xfrm_expand_policies(fl, family, pols,
3038 &num_pols, &num_xfrms);
3043 if (num_xfrms <= 0) {
3044 drop_pols = num_pols;
3048 xdst = xfrm_resolve_and_create_bundle(
3053 xfrm_pols_put(pols, num_pols);
3054 err = PTR_ERR(xdst);
3055 if (err == -EREMOTE)
3059 } else if (xdst == NULL) {
3061 drop_pols = num_pols;
3065 route = xdst->route;
3070 struct xfrm_flo xflo;
3072 xflo.dst_orig = dst_orig;
3075 /* To accelerate a bit... */
3076 if ((dst_orig->flags & DST_NOXFRM) ||
3077 !net->xfrm.policy_count[XFRM_POLICY_OUT])
3080 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
3084 err = PTR_ERR(xdst);
3088 num_pols = xdst->num_pols;
3089 num_xfrms = xdst->num_xfrms;
3090 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
3091 route = xdst->route;
3095 if (route == NULL && num_xfrms > 0) {
3096 /* The only case when xfrm_bundle_lookup() returns a
3097 * bundle with null route, is when the template could
3098 * not be resolved. It means policies are there, but
3099 * bundle could not be created, since we don't yet
3100 * have the xfrm_state's. We need to wait for KM to
3101 * negotiate new SA's or bail out with error.*/
3102 if (net->xfrm.sysctl_larval_drop) {
3103 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3110 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3118 if ((flags & XFRM_LOOKUP_ICMP) &&
3119 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3124 for (i = 0; i < num_pols; i++)
3125 pols[i]->curlft.use_time = ktime_get_real_seconds();
3127 if (num_xfrms < 0) {
3128 /* Prohibit the flow */
3129 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
3132 } else if (num_xfrms > 0) {
3133 /* Flow transformed */
3134 dst_release(dst_orig);
3136 /* Flow passes untransformed */
3141 xfrm_pols_put(pols, drop_pols);
3142 if (dst && dst->xfrm &&
3143 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3144 dst->flags |= DST_XFRM_TUNNEL;
3148 if (!(flags & XFRM_LOOKUP_ICMP)) {
3156 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3157 dst_release(dst_orig);
3158 xfrm_pols_put(pols, drop_pols);
3159 return ERR_PTR(err);
3161 EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3163 /* Main function: finds/creates a bundle for given flow.
3165 * At the moment we eat a raw IP route. Mostly to speed up lookups
3166 * on interfaces with disabled IPsec.
3168 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3169 const struct flowi *fl, const struct sock *sk,
3172 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3174 EXPORT_SYMBOL(xfrm_lookup);
3176 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3177 * Otherwise we may send out blackholed packets.
3179 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3180 const struct flowi *fl,
3181 const struct sock *sk, int flags)
3183 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
3184 flags | XFRM_LOOKUP_QUEUE |
3185 XFRM_LOOKUP_KEEP_DST_REF);
3187 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
3188 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3191 dst_release(dst_orig);
3195 EXPORT_SYMBOL(xfrm_lookup_route);
3198 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
3200 struct sec_path *sp = skb_sec_path(skb);
3201 struct xfrm_state *x;
3203 if (!sp || idx < 0 || idx >= sp->len)
3206 if (!x->type->reject)
3208 return x->type->reject(x, skb, fl);
3211 /* When skb is transformed back to its "native" form, we have to
3212 * check policy restrictions. At the moment we make this in maximally
3213 * stupid way. Shame on me. :-) Of course, connected sockets must
3214 * have policy cached at them.
3218 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
3219 unsigned short family)
3221 if (xfrm_state_kern(x))
3222 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
3223 return x->id.proto == tmpl->id.proto &&
3224 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3225 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3226 x->props.mode == tmpl->mode &&
3227 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
3228 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
3229 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3230 xfrm_state_addr_cmp(tmpl, x, family));
3234 * 0 or more than 0 is returned when validation is succeeded (either bypass
3235 * because of optional transport mode, or next index of the mathced secpath
3236 * state with the template.
3237 * -1 is returned when no matching template is found.
3238 * Otherwise "-2 - errored_index" is returned.
3241 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
3242 unsigned short family)
3246 if (tmpl->optional) {
3247 if (tmpl->mode == XFRM_MODE_TRANSPORT)
3251 for (; idx < sp->len; idx++) {
3252 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
3254 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3264 decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse)
3266 const struct iphdr *iph = ip_hdr(skb);
3267 u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
3268 struct flowi4 *fl4 = &fl->u.ip4;
3272 oif = skb_dst(skb)->dev->ifindex;
3274 memset(fl4, 0, sizeof(struct flowi4));
3275 fl4->flowi4_mark = skb->mark;
3276 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
3278 if (!ip_is_fragment(iph)) {
3279 switch (iph->protocol) {
3281 case IPPROTO_UDPLITE:
3285 if (xprth + 4 < skb->data ||
3286 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3289 xprth = skb_network_header(skb) + iph->ihl * 4;
3290 ports = (__be16 *)xprth;
3292 fl4->fl4_sport = ports[!!reverse];
3293 fl4->fl4_dport = ports[!reverse];
3297 if (xprth + 2 < skb->data ||
3298 pskb_may_pull(skb, xprth + 2 - skb->data)) {
3301 xprth = skb_network_header(skb) + iph->ihl * 4;
3304 fl4->fl4_icmp_type = icmp[0];
3305 fl4->fl4_icmp_code = icmp[1];
3309 if (xprth + 4 < skb->data ||
3310 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3313 xprth = skb_network_header(skb) + iph->ihl * 4;
3314 ehdr = (__be32 *)xprth;
3316 fl4->fl4_ipsec_spi = ehdr[0];
3320 if (xprth + 8 < skb->data ||
3321 pskb_may_pull(skb, xprth + 8 - skb->data)) {
3324 xprth = skb_network_header(skb) + iph->ihl * 4;
3325 ah_hdr = (__be32 *)xprth;
3327 fl4->fl4_ipsec_spi = ah_hdr[1];
3331 if (xprth + 4 < skb->data ||
3332 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3335 xprth = skb_network_header(skb) + iph->ihl * 4;
3336 ipcomp_hdr = (__be16 *)xprth;
3338 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
3342 if (xprth + 12 < skb->data ||
3343 pskb_may_pull(skb, xprth + 12 - skb->data)) {
3347 xprth = skb_network_header(skb) + iph->ihl * 4;
3348 greflags = (__be16 *)xprth;
3349 gre_hdr = (__be32 *)xprth;
3351 if (greflags[0] & GRE_KEY) {
3352 if (greflags[0] & GRE_CSUM)
3354 fl4->fl4_gre_key = gre_hdr[1];
3359 fl4->fl4_ipsec_spi = 0;
3363 fl4->flowi4_proto = iph->protocol;
3364 fl4->daddr = reverse ? iph->saddr : iph->daddr;
3365 fl4->saddr = reverse ? iph->daddr : iph->saddr;
3366 fl4->flowi4_tos = iph->tos;
3369 #if IS_ENABLED(CONFIG_IPV6)
3371 decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse)
3373 struct flowi6 *fl6 = &fl->u.ip6;
3375 const struct ipv6hdr *hdr = ipv6_hdr(skb);
3376 u32 offset = sizeof(*hdr);
3377 struct ipv6_opt_hdr *exthdr;
3378 const unsigned char *nh = skb_network_header(skb);
3379 u16 nhoff = IP6CB(skb)->nhoff;
3384 nhoff = offsetof(struct ipv6hdr, nexthdr);
3386 nexthdr = nh[nhoff];
3389 oif = skb_dst(skb)->dev->ifindex;
3391 memset(fl6, 0, sizeof(struct flowi6));
3392 fl6->flowi6_mark = skb->mark;
3393 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
3395 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
3396 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
3398 while (nh + offset + sizeof(*exthdr) < skb->data ||
3399 pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
3400 nh = skb_network_header(skb);
3401 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3404 case NEXTHDR_FRAGMENT:
3407 case NEXTHDR_ROUTING:
3410 offset += ipv6_optlen(exthdr);
3411 nexthdr = exthdr->nexthdr;
3412 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3415 case IPPROTO_UDPLITE:
3419 if (!onlyproto && (nh + offset + 4 < skb->data ||
3420 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
3423 nh = skb_network_header(skb);
3424 ports = (__be16 *)(nh + offset);
3425 fl6->fl6_sport = ports[!!reverse];
3426 fl6->fl6_dport = ports[!reverse];
3428 fl6->flowi6_proto = nexthdr;
3430 case IPPROTO_ICMPV6:
3431 if (!onlyproto && (nh + offset + 2 < skb->data ||
3432 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
3435 nh = skb_network_header(skb);
3436 icmp = (u8 *)(nh + offset);
3437 fl6->fl6_icmp_type = icmp[0];
3438 fl6->fl6_icmp_code = icmp[1];
3440 fl6->flowi6_proto = nexthdr;
3442 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3444 offset += ipv6_optlen(exthdr);
3445 if (!onlyproto && (nh + offset + 3 < skb->data ||
3446 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
3449 nh = skb_network_header(skb);
3450 mh = (struct ip6_mh *)(nh + offset);
3451 fl6->fl6_mh_type = mh->ip6mh_type;
3453 fl6->flowi6_proto = nexthdr;
3456 /* XXX Why are there these headers? */
3461 fl6->fl6_ipsec_spi = 0;
3462 fl6->flowi6_proto = nexthdr;
3469 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3470 unsigned int family, int reverse)
3474 decode_session4(skb, fl, reverse);
3476 #if IS_ENABLED(CONFIG_IPV6)
3478 decode_session6(skb, fl, reverse);
3482 return -EAFNOSUPPORT;
3485 return security_xfrm_decode_session(skb, &fl->flowi_secid);
3487 EXPORT_SYMBOL(__xfrm_decode_session);
3489 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
3491 for (; k < sp->len; k++) {
3492 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
3501 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
3502 unsigned short family)
3504 struct net *net = dev_net(skb->dev);
3505 struct xfrm_policy *pol;
3506 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3513 const struct xfrm_if_cb *ifcb;
3514 struct sec_path *sp;
3519 ifcb = xfrm_if_get_cb();
3522 xi = ifcb->decode_session(skb, family);
3524 if_id = xi->p.if_id;
3530 reverse = dir & ~XFRM_POLICY_MASK;
3531 dir &= XFRM_POLICY_MASK;
3533 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
3534 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
3538 nf_nat_decode_session(skb, &fl, family);
3540 /* First, check used SA against their selectors. */
3541 sp = skb_sec_path(skb);
3545 for (i = sp->len - 1; i >= 0; i--) {
3546 struct xfrm_state *x = sp->xvec[i];
3547 if (!xfrm_selector_match(&x->sel, &fl, family)) {
3548 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
3555 sk = sk_to_full_sk(sk);
3556 if (sk && sk->sk_policy[dir]) {
3557 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
3559 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
3565 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
3568 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
3573 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
3574 xfrm_secpath_reject(xerr_idx, skb, &fl);
3575 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
3581 pol->curlft.use_time = ktime_get_real_seconds();
3585 #ifdef CONFIG_XFRM_SUB_POLICY
3586 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
3587 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
3589 XFRM_POLICY_IN, if_id);
3591 if (IS_ERR(pols[1])) {
3592 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
3595 pols[1]->curlft.use_time = ktime_get_real_seconds();
3601 if (pol->action == XFRM_POLICY_ALLOW) {
3602 static struct sec_path dummy;
3603 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
3604 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
3605 struct xfrm_tmpl **tpp = tp;
3609 sp = skb_sec_path(skb);
3613 for (pi = 0; pi < npols; pi++) {
3614 if (pols[pi] != pol &&
3615 pols[pi]->action != XFRM_POLICY_ALLOW) {
3616 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
3619 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
3620 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
3623 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3624 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3628 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
3632 /* For each tunnel xfrm, find the first matching tmpl.
3633 * For each tmpl before that, find corresponding xfrm.
3634 * Order is _important_. Later we will implement
3635 * some barriers, but at the moment barriers
3636 * are implied between each two transformations.
3638 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3639 k = xfrm_policy_ok(tpp[i], sp, k, family);
3642 /* "-2 - errored_index" returned */
3644 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
3649 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
3650 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
3654 xfrm_pols_put(pols, npols);
3657 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
3660 xfrm_secpath_reject(xerr_idx, skb, &fl);
3662 xfrm_pols_put(pols, npols);
3665 EXPORT_SYMBOL(__xfrm_policy_check);
3667 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3669 struct net *net = dev_net(skb->dev);
3671 struct dst_entry *dst;
3674 if (xfrm_decode_session(skb, &fl, family) < 0) {
3675 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3680 if (!skb_dst(skb)) {
3681 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3685 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
3690 skb_dst_set(skb, dst);
3693 EXPORT_SYMBOL(__xfrm_route_forward);
3695 /* Optimize later using cookies and generation ids. */
3697 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3699 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
3700 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3701 * get validated by dst_ops->check on every use. We do this
3702 * because when a normal route referenced by an XFRM dst is
3703 * obsoleted we do not go looking around for all parent
3704 * referencing XFRM dsts so that we can invalidate them. It
3705 * is just too much work. Instead we make the checks here on
3706 * every use. For example:
3708 * XFRM dst A --> IPv4 dst X
3710 * X is the "xdst->route" of A (X is also the "dst->path" of A
3711 * in this example). If X is marked obsolete, "A" will not
3712 * notice. That's what we are validating here via the
3713 * stale_bundle() check.
3715 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3717 * This will force stale_bundle() to fail on any xdst bundle with
3718 * this dst linked in it.
3720 if (dst->obsolete < 0 && !stale_bundle(dst))
3726 static int stale_bundle(struct dst_entry *dst)
3728 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
3731 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
3733 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
3734 dst->dev = dev_net(dev)->loopback_dev;
3739 EXPORT_SYMBOL(xfrm_dst_ifdown);
3741 static void xfrm_link_failure(struct sk_buff *skb)
3743 /* Impossible. Such dst must be popped before reaches point of failure. */
3746 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3749 if (dst->obsolete) {
3757 static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
3760 struct xfrm_dst *xdst = bundle[nr];
3761 u32 pmtu, route_mtu_cached;
3762 struct dst_entry *dst;
3765 pmtu = dst_mtu(xfrm_dst_child(dst));
3766 xdst->child_mtu_cached = pmtu;
3768 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3770 route_mtu_cached = dst_mtu(xdst->route);
3771 xdst->route_mtu_cached = route_mtu_cached;
3773 if (pmtu > route_mtu_cached)
3774 pmtu = route_mtu_cached;
3776 dst_metric_set(dst, RTAX_MTU, pmtu);
3780 /* Check that the bundle accepts the flow and its components are
3784 static int xfrm_bundle_ok(struct xfrm_dst *first)
3786 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
3787 struct dst_entry *dst = &first->u.dst;
3788 struct xfrm_dst *xdst;
3792 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
3793 (dst->dev && !netif_running(dst->dev)))
3796 if (dst->flags & DST_XFRM_QUEUE)
3799 start_from = nr = 0;
3801 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3803 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3805 if (xdst->xfrm_genid != dst->xfrm->genid)
3807 if (xdst->num_pols > 0 &&
3808 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
3811 bundle[nr++] = xdst;
3813 mtu = dst_mtu(xfrm_dst_child(dst));
3814 if (xdst->child_mtu_cached != mtu) {
3816 xdst->child_mtu_cached = mtu;
3819 if (!dst_check(xdst->route, xdst->route_cookie))
3821 mtu = dst_mtu(xdst->route);
3822 if (xdst->route_mtu_cached != mtu) {
3824 xdst->route_mtu_cached = mtu;
3827 dst = xfrm_dst_child(dst);
3828 } while (dst->xfrm);
3830 if (likely(!start_from))
3833 xdst = bundle[start_from - 1];
3834 mtu = xdst->child_mtu_cached;
3835 while (start_from--) {
3838 mtu = xfrm_state_mtu(dst->xfrm, mtu);
3839 if (mtu > xdst->route_mtu_cached)
3840 mtu = xdst->route_mtu_cached;
3841 dst_metric_set(dst, RTAX_MTU, mtu);
3845 xdst = bundle[start_from - 1];
3846 xdst->child_mtu_cached = mtu;
3852 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3854 return dst_metric_advmss(xfrm_dst_path(dst));
3857 static unsigned int xfrm_mtu(const struct dst_entry *dst)
3859 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3861 return mtu ? : dst_mtu(xfrm_dst_path(dst));
3864 static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3868 const struct xfrm_state *xfrm = dst->xfrm;
3870 dst = xfrm_dst_child(dst);
3872 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3874 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3875 daddr = xfrm->coaddr;
3876 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3877 daddr = &xfrm->id.daddr;
3882 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3883 struct sk_buff *skb,
3886 const struct dst_entry *path = xfrm_dst_path(dst);
3889 daddr = xfrm_get_dst_nexthop(dst, daddr);
3890 return path->ops->neigh_lookup(path, skb, daddr);
3893 static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3895 const struct dst_entry *path = xfrm_dst_path(dst);
3897 daddr = xfrm_get_dst_nexthop(dst, daddr);
3898 path->ops->confirm_neigh(path, daddr);
3901 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
3905 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
3906 return -EAFNOSUPPORT;
3908 spin_lock(&xfrm_policy_afinfo_lock);
3909 if (unlikely(xfrm_policy_afinfo[family] != NULL))
3912 struct dst_ops *dst_ops = afinfo->dst_ops;
3913 if (likely(dst_ops->kmem_cachep == NULL))
3914 dst_ops->kmem_cachep = xfrm_dst_cache;
3915 if (likely(dst_ops->check == NULL))
3916 dst_ops->check = xfrm_dst_check;
3917 if (likely(dst_ops->default_advmss == NULL))
3918 dst_ops->default_advmss = xfrm_default_advmss;
3919 if (likely(dst_ops->mtu == NULL))
3920 dst_ops->mtu = xfrm_mtu;
3921 if (likely(dst_ops->negative_advice == NULL))
3922 dst_ops->negative_advice = xfrm_negative_advice;
3923 if (likely(dst_ops->link_failure == NULL))
3924 dst_ops->link_failure = xfrm_link_failure;
3925 if (likely(dst_ops->neigh_lookup == NULL))
3926 dst_ops->neigh_lookup = xfrm_neigh_lookup;
3927 if (likely(!dst_ops->confirm_neigh))
3928 dst_ops->confirm_neigh = xfrm_confirm_neigh;
3929 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
3931 spin_unlock(&xfrm_policy_afinfo_lock);
3935 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3937 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
3939 struct dst_ops *dst_ops = afinfo->dst_ops;
3942 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3943 if (xfrm_policy_afinfo[i] != afinfo)
3945 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3951 dst_ops->kmem_cachep = NULL;
3952 dst_ops->check = NULL;
3953 dst_ops->negative_advice = NULL;
3954 dst_ops->link_failure = NULL;
3956 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3958 void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3960 spin_lock(&xfrm_if_cb_lock);
3961 rcu_assign_pointer(xfrm_if_cb, ifcb);
3962 spin_unlock(&xfrm_if_cb_lock);
3964 EXPORT_SYMBOL(xfrm_if_register_cb);
3966 void xfrm_if_unregister_cb(void)
3968 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3971 EXPORT_SYMBOL(xfrm_if_unregister_cb);
3973 #ifdef CONFIG_XFRM_STATISTICS
3974 static int __net_init xfrm_statistics_init(struct net *net)
3977 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3978 if (!net->mib.xfrm_statistics)
3980 rv = xfrm_proc_init(net);
3982 free_percpu(net->mib.xfrm_statistics);
3986 static void xfrm_statistics_fini(struct net *net)
3988 xfrm_proc_fini(net);
3989 free_percpu(net->mib.xfrm_statistics);
3992 static int __net_init xfrm_statistics_init(struct net *net)
3997 static void xfrm_statistics_fini(struct net *net)
4002 static int __net_init xfrm_policy_init(struct net *net)
4004 unsigned int hmask, sz;
4007 if (net_eq(net, &init_net)) {
4008 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
4009 sizeof(struct xfrm_dst),
4010 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4012 err = rhashtable_init(&xfrm_policy_inexact_table,
4013 &xfrm_pol_inexact_params);
4018 sz = (hmask+1) * sizeof(struct hlist_head);
4020 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
4021 if (!net->xfrm.policy_byidx)
4023 net->xfrm.policy_idx_hmask = hmask;
4025 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
4026 struct xfrm_policy_hash *htab;
4028 net->xfrm.policy_count[dir] = 0;
4029 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
4030 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
4032 htab = &net->xfrm.policy_bydst[dir];
4033 htab->table = xfrm_hash_alloc(sz);
4036 htab->hmask = hmask;
4042 net->xfrm.policy_hthresh.lbits4 = 32;
4043 net->xfrm.policy_hthresh.rbits4 = 32;
4044 net->xfrm.policy_hthresh.lbits6 = 128;
4045 net->xfrm.policy_hthresh.rbits6 = 128;
4047 seqlock_init(&net->xfrm.policy_hthresh.lock);
4049 INIT_LIST_HEAD(&net->xfrm.policy_all);
4050 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
4051 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
4052 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
4056 for (dir--; dir >= 0; dir--) {
4057 struct xfrm_policy_hash *htab;
4059 htab = &net->xfrm.policy_bydst[dir];
4060 xfrm_hash_free(htab->table, sz);
4062 xfrm_hash_free(net->xfrm.policy_byidx, sz);
4067 static void xfrm_policy_fini(struct net *net)
4069 struct xfrm_pol_inexact_bin *b, *t;
4073 flush_work(&net->xfrm.policy_hash_work);
4074 #ifdef CONFIG_XFRM_SUB_POLICY
4075 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
4077 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
4079 WARN_ON(!list_empty(&net->xfrm.policy_all));
4081 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
4082 struct xfrm_policy_hash *htab;
4084 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
4086 htab = &net->xfrm.policy_bydst[dir];
4087 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
4088 WARN_ON(!hlist_empty(htab->table));
4089 xfrm_hash_free(htab->table, sz);
4092 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
4093 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
4094 xfrm_hash_free(net->xfrm.policy_byidx, sz);
4096 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4097 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
4098 __xfrm_policy_inexact_prune_bin(b, true);
4099 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
4102 static int __net_init xfrm_net_init(struct net *net)
4106 /* Initialize the per-net locks here */
4107 spin_lock_init(&net->xfrm.xfrm_state_lock);
4108 spin_lock_init(&net->xfrm.xfrm_policy_lock);
4109 mutex_init(&net->xfrm.xfrm_cfg_mutex);
4111 rv = xfrm_statistics_init(net);
4113 goto out_statistics;
4114 rv = xfrm_state_init(net);
4117 rv = xfrm_policy_init(net);
4120 rv = xfrm_sysctl_init(net);
4127 xfrm_policy_fini(net);
4129 xfrm_state_fini(net);
4131 xfrm_statistics_fini(net);
4136 static void __net_exit xfrm_net_exit(struct net *net)
4138 xfrm_sysctl_fini(net);
4139 xfrm_policy_fini(net);
4140 xfrm_state_fini(net);
4141 xfrm_statistics_fini(net);
4144 static struct pernet_operations __net_initdata xfrm_net_ops = {
4145 .init = xfrm_net_init,
4146 .exit = xfrm_net_exit,
4149 void __init xfrm_init(void)
4151 register_pernet_subsys(&xfrm_net_ops);
4153 seqcount_init(&xfrm_policy_hash_generation);
4156 RCU_INIT_POINTER(xfrm_if_cb, NULL);
4160 #ifdef CONFIG_AUDITSYSCALL
4161 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
4162 struct audit_buffer *audit_buf)
4164 struct xfrm_sec_ctx *ctx = xp->security;
4165 struct xfrm_selector *sel = &xp->selector;
4168 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
4169 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
4171 switch (sel->family) {
4173 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
4174 if (sel->prefixlen_s != 32)
4175 audit_log_format(audit_buf, " src_prefixlen=%d",
4177 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
4178 if (sel->prefixlen_d != 32)
4179 audit_log_format(audit_buf, " dst_prefixlen=%d",
4183 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
4184 if (sel->prefixlen_s != 128)
4185 audit_log_format(audit_buf, " src_prefixlen=%d",
4187 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
4188 if (sel->prefixlen_d != 128)
4189 audit_log_format(audit_buf, " dst_prefixlen=%d",
4195 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
4197 struct audit_buffer *audit_buf;
4199 audit_buf = xfrm_audit_start("SPD-add");
4200 if (audit_buf == NULL)
4202 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
4203 audit_log_format(audit_buf, " res=%u", result);
4204 xfrm_audit_common_policyinfo(xp, audit_buf);
4205 audit_log_end(audit_buf);
4207 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
4209 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
4212 struct audit_buffer *audit_buf;
4214 audit_buf = xfrm_audit_start("SPD-delete");
4215 if (audit_buf == NULL)
4217 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
4218 audit_log_format(audit_buf, " res=%u", result);
4219 xfrm_audit_common_policyinfo(xp, audit_buf);
4220 audit_log_end(audit_buf);
4222 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
4225 #ifdef CONFIG_XFRM_MIGRATE
4226 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
4227 const struct xfrm_selector *sel_tgt)
4229 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
4230 if (sel_tgt->family == sel_cmp->family &&
4231 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
4233 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
4235 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
4236 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
4240 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
4247 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
4248 u8 dir, u8 type, struct net *net)
4250 struct xfrm_policy *pol, *ret = NULL;
4251 struct hlist_head *chain;
4254 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4255 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
4256 hlist_for_each_entry(pol, chain, bydst) {
4257 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4258 pol->type == type) {
4260 priority = ret->priority;
4264 chain = &net->xfrm.policy_inexact[dir];
4265 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
4266 if ((pol->priority >= priority) && ret)
4269 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4270 pol->type == type) {
4278 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
4283 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
4287 if (t->mode == m->mode && t->id.proto == m->proto &&
4288 (m->reqid == 0 || t->reqid == m->reqid)) {
4290 case XFRM_MODE_TUNNEL:
4291 case XFRM_MODE_BEET:
4292 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
4294 xfrm_addr_equal(&t->saddr, &m->old_saddr,
4299 case XFRM_MODE_TRANSPORT:
4300 /* in case of transport mode, template does not store
4301 any IP addresses, hence we just compare mode and
4312 /* update endpoint address(es) of template(s) */
4313 static int xfrm_policy_migrate(struct xfrm_policy *pol,
4314 struct xfrm_migrate *m, int num_migrate)
4316 struct xfrm_migrate *mp;
4319 write_lock_bh(&pol->lock);
4320 if (unlikely(pol->walk.dead)) {
4321 /* target policy has been deleted */
4322 write_unlock_bh(&pol->lock);
4326 for (i = 0; i < pol->xfrm_nr; i++) {
4327 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4328 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4331 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4332 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
4334 /* update endpoints */
4335 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4336 sizeof(pol->xfrm_vec[i].id.daddr));
4337 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4338 sizeof(pol->xfrm_vec[i].saddr));
4339 pol->xfrm_vec[i].encap_family = mp->new_family;
4341 atomic_inc(&pol->genid);
4345 write_unlock_bh(&pol->lock);
4353 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
4357 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4360 for (i = 0; i < num_migrate; i++) {
4361 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4362 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4365 /* check if there is any duplicated entry */
4366 for (j = i + 1; j < num_migrate; j++) {
4367 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4368 sizeof(m[i].old_daddr)) &&
4369 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4370 sizeof(m[i].old_saddr)) &&
4371 m[i].proto == m[j].proto &&
4372 m[i].mode == m[j].mode &&
4373 m[i].reqid == m[j].reqid &&
4374 m[i].old_family == m[j].old_family)
4382 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
4383 struct xfrm_migrate *m, int num_migrate,
4384 struct xfrm_kmaddress *k, struct net *net,
4385 struct xfrm_encap_tmpl *encap)
4387 int i, err, nx_cur = 0, nx_new = 0;
4388 struct xfrm_policy *pol = NULL;
4389 struct xfrm_state *x, *xc;
4390 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4391 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4392 struct xfrm_migrate *mp;
4394 /* Stage 0 - sanity checks */
4395 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4398 if (dir >= XFRM_POLICY_MAX) {
4403 /* Stage 1 - find policy */
4404 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
4409 /* Stage 2 - find and update state(s) */
4410 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
4411 if ((x = xfrm_migrate_state_find(mp, net))) {
4414 xc = xfrm_state_migrate(x, mp, encap);
4425 /* Stage 3 - update policy */
4426 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4429 /* Stage 4 - delete old state(s) */
4431 xfrm_states_put(x_cur, nx_cur);
4432 xfrm_states_delete(x_cur, nx_cur);
4435 /* Stage 5 - announce */
4436 km_migrate(sel, dir, type, m, num_migrate, k, encap);
4448 xfrm_states_put(x_cur, nx_cur);
4450 xfrm_states_delete(x_new, nx_new);
4454 EXPORT_SYMBOL(xfrm_migrate);