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>
34 #ifdef CONFIG_XFRM_STATISTICS
38 #include "xfrm_hash.h"
40 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
41 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
42 #define XFRM_MAX_QUEUE_LEN 100
45 struct dst_entry *dst_orig;
49 struct xfrm_pol_inexact_key {
55 struct xfrm_pol_inexact_bin {
56 struct xfrm_pol_inexact_key k;
57 struct rhash_head head;
58 struct hlist_head hhead;
61 struct list_head inexact_bins;
65 static DEFINE_SPINLOCK(xfrm_if_cb_lock);
66 static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
68 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
69 static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
72 static struct kmem_cache *xfrm_dst_cache __ro_after_init;
73 static __read_mostly seqcount_t xfrm_policy_hash_generation;
75 static struct rhashtable xfrm_policy_inexact_table;
76 static const struct rhashtable_params xfrm_pol_inexact_params;
78 static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
79 static int stale_bundle(struct dst_entry *dst);
80 static int xfrm_bundle_ok(struct xfrm_dst *xdst);
81 static void xfrm_policy_queue_process(struct timer_list *t);
83 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
84 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
87 static struct xfrm_pol_inexact_bin *
88 xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir);
90 static struct xfrm_pol_inexact_bin *
91 xfrm_policy_inexact_lookup_rcu(struct net *net,
92 u8 type, u16 family, u8 dir);
93 static struct xfrm_policy *
94 xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
96 static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
97 struct xfrm_policy *policy);
99 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
101 return refcount_inc_not_zero(&policy->refcnt);
105 __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
107 const struct flowi4 *fl4 = &fl->u.ip4;
109 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
110 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
111 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
112 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
113 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
114 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
118 __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
120 const struct flowi6 *fl6 = &fl->u.ip6;
122 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
123 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
124 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
125 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
126 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
127 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
130 bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
131 unsigned short family)
135 return __xfrm4_selector_match(sel, fl);
137 return __xfrm6_selector_match(sel, fl);
142 static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
144 const struct xfrm_policy_afinfo *afinfo;
146 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
149 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
150 if (unlikely(!afinfo))
155 /* Called with rcu_read_lock(). */
156 static const struct xfrm_if_cb *xfrm_if_get_cb(void)
158 return rcu_dereference(xfrm_if_cb);
161 struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
162 const xfrm_address_t *saddr,
163 const xfrm_address_t *daddr,
164 int family, u32 mark)
166 const struct xfrm_policy_afinfo *afinfo;
167 struct dst_entry *dst;
169 afinfo = xfrm_policy_get_afinfo(family);
170 if (unlikely(afinfo == NULL))
171 return ERR_PTR(-EAFNOSUPPORT);
173 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
179 EXPORT_SYMBOL(__xfrm_dst_lookup);
181 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
183 xfrm_address_t *prev_saddr,
184 xfrm_address_t *prev_daddr,
185 int family, u32 mark)
187 struct net *net = xs_net(x);
188 xfrm_address_t *saddr = &x->props.saddr;
189 xfrm_address_t *daddr = &x->id.daddr;
190 struct dst_entry *dst;
192 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
196 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
201 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
204 if (prev_saddr != saddr)
205 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
206 if (prev_daddr != daddr)
207 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
213 static inline unsigned long make_jiffies(long secs)
215 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
216 return MAX_SCHEDULE_TIMEOUT-1;
221 static void xfrm_policy_timer(struct timer_list *t)
223 struct xfrm_policy *xp = from_timer(xp, t, timer);
224 time64_t now = ktime_get_real_seconds();
225 time64_t next = TIME64_MAX;
229 read_lock(&xp->lock);
231 if (unlikely(xp->walk.dead))
234 dir = xfrm_policy_id2dir(xp->index);
236 if (xp->lft.hard_add_expires_seconds) {
237 time64_t tmo = xp->lft.hard_add_expires_seconds +
238 xp->curlft.add_time - now;
244 if (xp->lft.hard_use_expires_seconds) {
245 time64_t tmo = xp->lft.hard_use_expires_seconds +
246 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
252 if (xp->lft.soft_add_expires_seconds) {
253 time64_t tmo = xp->lft.soft_add_expires_seconds +
254 xp->curlft.add_time - now;
257 tmo = XFRM_KM_TIMEOUT;
262 if (xp->lft.soft_use_expires_seconds) {
263 time64_t tmo = xp->lft.soft_use_expires_seconds +
264 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
267 tmo = XFRM_KM_TIMEOUT;
274 km_policy_expired(xp, dir, 0, 0);
275 if (next != TIME64_MAX &&
276 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
280 read_unlock(&xp->lock);
285 read_unlock(&xp->lock);
286 if (!xfrm_policy_delete(xp, dir))
287 km_policy_expired(xp, dir, 1, 0);
291 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
295 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
297 struct xfrm_policy *policy;
299 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
302 write_pnet(&policy->xp_net, net);
303 INIT_LIST_HEAD(&policy->walk.all);
304 INIT_HLIST_NODE(&policy->bydst_inexact_list);
305 INIT_HLIST_NODE(&policy->bydst);
306 INIT_HLIST_NODE(&policy->byidx);
307 rwlock_init(&policy->lock);
308 refcount_set(&policy->refcnt, 1);
309 skb_queue_head_init(&policy->polq.hold_queue);
310 timer_setup(&policy->timer, xfrm_policy_timer, 0);
311 timer_setup(&policy->polq.hold_timer,
312 xfrm_policy_queue_process, 0);
316 EXPORT_SYMBOL(xfrm_policy_alloc);
318 static void xfrm_policy_destroy_rcu(struct rcu_head *head)
320 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
322 security_xfrm_policy_free(policy->security);
326 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
328 void xfrm_policy_destroy(struct xfrm_policy *policy)
330 BUG_ON(!policy->walk.dead);
332 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
335 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
337 EXPORT_SYMBOL(xfrm_policy_destroy);
339 /* Rule must be locked. Release descendant resources, announce
340 * entry dead. The rule must be unlinked from lists to the moment.
343 static void xfrm_policy_kill(struct xfrm_policy *policy)
345 policy->walk.dead = 1;
347 atomic_inc(&policy->genid);
349 if (del_timer(&policy->polq.hold_timer))
350 xfrm_pol_put(policy);
351 skb_queue_purge(&policy->polq.hold_queue);
353 if (del_timer(&policy->timer))
354 xfrm_pol_put(policy);
356 xfrm_pol_put(policy);
359 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
361 static inline unsigned int idx_hash(struct net *net, u32 index)
363 return __idx_hash(index, net->xfrm.policy_idx_hmask);
366 /* calculate policy hash thresholds */
367 static void __get_hash_thresh(struct net *net,
368 unsigned short family, int dir,
369 u8 *dbits, u8 *sbits)
373 *dbits = net->xfrm.policy_bydst[dir].dbits4;
374 *sbits = net->xfrm.policy_bydst[dir].sbits4;
378 *dbits = net->xfrm.policy_bydst[dir].dbits6;
379 *sbits = net->xfrm.policy_bydst[dir].sbits6;
388 static struct hlist_head *policy_hash_bysel(struct net *net,
389 const struct xfrm_selector *sel,
390 unsigned short family, int dir)
392 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
397 __get_hash_thresh(net, family, dir, &dbits, &sbits);
398 hash = __sel_hash(sel, family, hmask, dbits, sbits);
400 if (hash == hmask + 1)
403 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
404 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
407 static struct hlist_head *policy_hash_direct(struct net *net,
408 const xfrm_address_t *daddr,
409 const xfrm_address_t *saddr,
410 unsigned short family, int dir)
412 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
417 __get_hash_thresh(net, family, dir, &dbits, &sbits);
418 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
420 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
421 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
424 static void xfrm_dst_hash_transfer(struct net *net,
425 struct hlist_head *list,
426 struct hlist_head *ndsttable,
427 unsigned int nhashmask,
430 struct hlist_node *tmp, *entry0 = NULL;
431 struct xfrm_policy *pol;
437 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
440 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
441 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
442 pol->family, nhashmask, dbits, sbits);
444 hlist_del_rcu(&pol->bydst);
445 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
450 hlist_del_rcu(&pol->bydst);
451 hlist_add_behind_rcu(&pol->bydst, entry0);
453 entry0 = &pol->bydst;
455 if (!hlist_empty(list)) {
461 static void xfrm_idx_hash_transfer(struct hlist_head *list,
462 struct hlist_head *nidxtable,
463 unsigned int nhashmask)
465 struct hlist_node *tmp;
466 struct xfrm_policy *pol;
468 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
471 h = __idx_hash(pol->index, nhashmask);
472 hlist_add_head(&pol->byidx, nidxtable+h);
476 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
478 return ((old_hmask + 1) << 1) - 1;
481 static void xfrm_bydst_resize(struct net *net, int dir)
483 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
484 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
485 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
486 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
487 struct hlist_head *odst;
493 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
494 write_seqcount_begin(&xfrm_policy_hash_generation);
496 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
497 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
499 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
500 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
502 for (i = hmask; i >= 0; i--)
503 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
505 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
506 net->xfrm.policy_bydst[dir].hmask = nhashmask;
508 write_seqcount_end(&xfrm_policy_hash_generation);
509 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
513 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
516 static void xfrm_byidx_resize(struct net *net, int total)
518 unsigned int hmask = net->xfrm.policy_idx_hmask;
519 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
520 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
521 struct hlist_head *oidx = net->xfrm.policy_byidx;
522 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
528 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
530 for (i = hmask; i >= 0; i--)
531 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
533 net->xfrm.policy_byidx = nidx;
534 net->xfrm.policy_idx_hmask = nhashmask;
536 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
538 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
541 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
543 unsigned int cnt = net->xfrm.policy_count[dir];
544 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
549 if ((hmask + 1) < xfrm_policy_hashmax &&
556 static inline int xfrm_byidx_should_resize(struct net *net, int total)
558 unsigned int hmask = net->xfrm.policy_idx_hmask;
560 if ((hmask + 1) < xfrm_policy_hashmax &&
567 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
569 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
570 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
571 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
572 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
573 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
574 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
575 si->spdhcnt = net->xfrm.policy_idx_hmask;
576 si->spdhmcnt = xfrm_policy_hashmax;
578 EXPORT_SYMBOL(xfrm_spd_getinfo);
580 static DEFINE_MUTEX(hash_resize_mutex);
581 static void xfrm_hash_resize(struct work_struct *work)
583 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
586 mutex_lock(&hash_resize_mutex);
589 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
590 if (xfrm_bydst_should_resize(net, dir, &total))
591 xfrm_bydst_resize(net, dir);
593 if (xfrm_byidx_should_resize(net, total))
594 xfrm_byidx_resize(net, total);
596 mutex_unlock(&hash_resize_mutex);
599 static void xfrm_hash_reset_inexact_table(struct net *net)
601 struct xfrm_pol_inexact_bin *b;
603 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
605 list_for_each_entry(b, &net->xfrm.inexact_bins, inexact_bins)
606 INIT_HLIST_HEAD(&b->hhead);
609 /* Make sure *pol can be inserted into fastbin.
610 * Useful to check that later insert requests will be sucessful
611 * (provided xfrm_policy_lock is held throughout).
613 static struct xfrm_pol_inexact_bin *
614 xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
616 struct xfrm_pol_inexact_bin *bin, *prev;
617 struct xfrm_pol_inexact_key k = {
618 .family = pol->family,
622 struct net *net = xp_net(pol);
624 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
626 write_pnet(&k.net, net);
627 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
628 xfrm_pol_inexact_params);
632 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
637 INIT_HLIST_HEAD(&bin->hhead);
639 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
641 xfrm_pol_inexact_params);
643 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
649 return IS_ERR(prev) ? NULL : prev;
652 static void xfrm_policy_inexact_delete_bin(struct net *net,
653 struct xfrm_pol_inexact_bin *b)
655 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
657 if (!hlist_empty(&b->hhead))
660 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
661 xfrm_pol_inexact_params) == 0) {
662 list_del(&b->inexact_bins);
667 static void __xfrm_policy_inexact_flush(struct net *net)
669 struct xfrm_pol_inexact_bin *bin;
671 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
673 list_for_each_entry(bin, &net->xfrm.inexact_bins, inexact_bins)
674 xfrm_policy_inexact_delete_bin(net, bin);
677 static struct xfrm_policy *
678 xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
680 struct xfrm_pol_inexact_bin *bin;
681 struct xfrm_policy *delpol;
682 struct hlist_head *chain;
685 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
687 return ERR_PTR(-ENOMEM);
689 delpol = xfrm_policy_insert_list(&bin->hhead, policy, excl);
691 return ERR_PTR(-EEXIST);
693 net = xp_net(policy);
694 chain = &net->xfrm.policy_inexact[dir];
695 xfrm_policy_insert_inexact_list(chain, policy);
700 static void xfrm_hash_rebuild(struct work_struct *work)
702 struct net *net = container_of(work, struct net,
703 xfrm.policy_hthresh.work);
705 struct xfrm_policy *pol;
706 struct xfrm_policy *policy;
707 struct hlist_head *chain;
708 struct hlist_head *odst;
709 struct hlist_node *newpos;
713 u8 lbits4, rbits4, lbits6, rbits6;
715 mutex_lock(&hash_resize_mutex);
717 /* read selector prefixlen thresholds */
719 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
721 lbits4 = net->xfrm.policy_hthresh.lbits4;
722 rbits4 = net->xfrm.policy_hthresh.rbits4;
723 lbits6 = net->xfrm.policy_hthresh.lbits6;
724 rbits6 = net->xfrm.policy_hthresh.rbits6;
725 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
727 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
729 /* make sure that we can insert the indirect policies again before
730 * we start with destructive action.
732 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
735 dir = xfrm_policy_id2dir(policy->index);
736 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
739 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
740 if (policy->family == AF_INET) {
748 if (policy->family == AF_INET) {
757 if (policy->selector.prefixlen_d < dbits ||
758 policy->selector.prefixlen_s < sbits)
761 if (!xfrm_policy_inexact_alloc_bin(policy, dir))
765 /* reset the bydst and inexact table in all directions */
766 xfrm_hash_reset_inexact_table(net);
768 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
769 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
770 hmask = net->xfrm.policy_bydst[dir].hmask;
771 odst = net->xfrm.policy_bydst[dir].table;
772 for (i = hmask; i >= 0; i--)
773 INIT_HLIST_HEAD(odst + i);
774 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
775 /* dir out => dst = remote, src = local */
776 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
777 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
778 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
779 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
781 /* dir in/fwd => dst = local, src = remote */
782 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
783 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
784 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
785 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
789 /* re-insert all policies by order of creation */
790 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
791 if (policy->walk.dead ||
792 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
793 /* skip socket policies */
797 chain = policy_hash_bysel(net, &policy->selector,
799 xfrm_policy_id2dir(policy->index));
801 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
803 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
807 hlist_for_each_entry(pol, chain, bydst) {
808 if (policy->priority >= pol->priority)
809 newpos = &pol->bydst;
814 hlist_add_behind_rcu(&policy->bydst, newpos);
816 hlist_add_head_rcu(&policy->bydst, chain);
820 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
822 mutex_unlock(&hash_resize_mutex);
825 void xfrm_policy_hash_rebuild(struct net *net)
827 schedule_work(&net->xfrm.policy_hthresh.work);
829 EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
831 /* Generate new index... KAME seems to generate them ordered by cost
832 * of an absolute inpredictability of ordering of rules. This will not pass. */
833 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
835 static u32 idx_generator;
838 struct hlist_head *list;
839 struct xfrm_policy *p;
844 idx = (idx_generator | dir);
853 list = net->xfrm.policy_byidx + idx_hash(net, idx);
855 hlist_for_each_entry(p, list, byidx) {
856 if (p->index == idx) {
866 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
868 u32 *p1 = (u32 *) s1;
869 u32 *p2 = (u32 *) s2;
870 int len = sizeof(struct xfrm_selector) / sizeof(u32);
873 for (i = 0; i < len; i++) {
881 static void xfrm_policy_requeue(struct xfrm_policy *old,
882 struct xfrm_policy *new)
884 struct xfrm_policy_queue *pq = &old->polq;
885 struct sk_buff_head list;
887 if (skb_queue_empty(&pq->hold_queue))
890 __skb_queue_head_init(&list);
892 spin_lock_bh(&pq->hold_queue.lock);
893 skb_queue_splice_init(&pq->hold_queue, &list);
894 if (del_timer(&pq->hold_timer))
896 spin_unlock_bh(&pq->hold_queue.lock);
900 spin_lock_bh(&pq->hold_queue.lock);
901 skb_queue_splice(&list, &pq->hold_queue);
902 pq->timeout = XFRM_QUEUE_TMO_MIN;
903 if (!mod_timer(&pq->hold_timer, jiffies))
905 spin_unlock_bh(&pq->hold_queue.lock);
908 static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
909 struct xfrm_policy *pol)
911 u32 mark = policy->mark.v & policy->mark.m;
913 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
916 if ((mark & pol->mark.m) == pol->mark.v &&
917 policy->priority == pol->priority)
923 static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
925 const struct xfrm_pol_inexact_key *k = data;
926 u32 a = k->type << 24 | k->dir << 16 | k->family;
928 return jhash_2words(a, net_hash_mix(read_pnet(&k->net)), seed);
931 static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
933 const struct xfrm_pol_inexact_bin *b = data;
935 return xfrm_pol_bin_key(&b->k, 0, seed);
938 static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
941 const struct xfrm_pol_inexact_key *key = arg->key;
942 const struct xfrm_pol_inexact_bin *b = ptr;
945 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
948 ret = b->k.dir ^ key->dir;
952 ret = b->k.type ^ key->type;
956 ret = b->k.family ^ key->family;
963 static const struct rhashtable_params xfrm_pol_inexact_params = {
964 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
965 .hashfn = xfrm_pol_bin_key,
966 .obj_hashfn = xfrm_pol_bin_obj,
967 .obj_cmpfn = xfrm_pol_bin_cmp,
968 .automatic_shrinking = true,
971 static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
972 struct xfrm_policy *policy)
974 struct xfrm_policy *pol, *delpol = NULL;
975 struct hlist_node *newpos = NULL;
977 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
978 if (pol->type == policy->type &&
979 pol->if_id == policy->if_id &&
980 !selector_cmp(&pol->selector, &policy->selector) &&
981 xfrm_policy_mark_match(policy, pol) &&
982 xfrm_sec_ctx_match(pol->security, policy->security) &&
985 if (policy->priority > pol->priority)
987 } else if (policy->priority >= pol->priority) {
988 newpos = &pol->bydst_inexact_list;
996 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
998 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
1001 static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1002 struct xfrm_policy *policy,
1005 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
1007 hlist_for_each_entry(pol, chain, bydst) {
1008 if (pol->type == policy->type &&
1009 pol->if_id == policy->if_id &&
1010 !selector_cmp(&pol->selector, &policy->selector) &&
1011 xfrm_policy_mark_match(policy, pol) &&
1012 xfrm_sec_ctx_match(pol->security, policy->security) &&
1015 return ERR_PTR(-EEXIST);
1017 if (policy->priority > pol->priority)
1019 } else if (policy->priority >= pol->priority) {
1028 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
1030 hlist_add_head_rcu(&policy->bydst, chain);
1035 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1037 struct net *net = xp_net(policy);
1038 struct xfrm_policy *delpol;
1039 struct hlist_head *chain;
1041 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1042 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
1044 delpol = xfrm_policy_insert_list(chain, policy, excl);
1046 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
1048 if (IS_ERR(delpol)) {
1049 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1050 return PTR_ERR(delpol);
1053 __xfrm_policy_link(policy, dir);
1055 /* After previous checking, family can either be AF_INET or AF_INET6 */
1056 if (policy->family == AF_INET)
1057 rt_genid_bump_ipv4(net);
1059 rt_genid_bump_ipv6(net);
1062 xfrm_policy_requeue(delpol, policy);
1063 __xfrm_policy_unlink(delpol, dir);
1065 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
1066 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
1067 policy->curlft.add_time = ktime_get_real_seconds();
1068 policy->curlft.use_time = 0;
1069 if (!mod_timer(&policy->timer, jiffies + HZ))
1070 xfrm_pol_hold(policy);
1071 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1074 xfrm_policy_kill(delpol);
1075 else if (xfrm_bydst_should_resize(net, dir, NULL))
1076 schedule_work(&net->xfrm.policy_hash_work);
1080 EXPORT_SYMBOL(xfrm_policy_insert);
1082 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1084 struct xfrm_selector *sel,
1085 struct xfrm_sec_ctx *ctx, int delete,
1088 struct xfrm_pol_inexact_bin *bin = NULL;
1089 struct xfrm_policy *pol, *ret = NULL;
1090 struct hlist_head *chain;
1093 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1094 chain = policy_hash_bysel(net, sel, sel->family, dir);
1096 bin = xfrm_policy_inexact_lookup(net, type,
1099 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1103 chain = &bin->hhead;
1107 hlist_for_each_entry(pol, chain, bydst) {
1108 if (pol->type == type &&
1109 pol->if_id == if_id &&
1110 (mark & pol->mark.m) == pol->mark.v &&
1111 !selector_cmp(sel, &pol->selector) &&
1112 xfrm_sec_ctx_match(ctx, pol->security)) {
1115 *err = security_xfrm_policy_delete(
1118 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1121 __xfrm_policy_unlink(pol, dir);
1122 xfrm_policy_inexact_delete_bin(net, bin);
1128 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1131 xfrm_policy_kill(ret);
1134 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
1136 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1137 u8 type, int dir, u32 id, int delete,
1140 struct xfrm_policy *pol, *ret;
1141 struct hlist_head *chain;
1144 if (xfrm_policy_id2dir(id) != dir)
1148 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1149 chain = net->xfrm.policy_byidx + idx_hash(net, id);
1151 hlist_for_each_entry(pol, chain, byidx) {
1152 if (pol->type == type && pol->index == id &&
1153 pol->if_id == if_id &&
1154 (mark & pol->mark.m) == pol->mark.v) {
1157 *err = security_xfrm_policy_delete(
1160 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1163 __xfrm_policy_unlink(pol, dir);
1169 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1172 xfrm_policy_kill(ret);
1175 EXPORT_SYMBOL(xfrm_policy_byid);
1177 #ifdef CONFIG_SECURITY_NETWORK_XFRM
1179 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
1181 struct xfrm_policy *pol;
1184 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1185 if (pol->walk.dead ||
1186 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1190 err = security_xfrm_policy_delete(pol->security);
1192 xfrm_audit_policy_delete(pol, 0, task_valid);
1200 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
1206 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
1208 int dir, err = 0, cnt = 0;
1209 struct xfrm_policy *pol;
1211 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1213 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
1218 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1219 dir = xfrm_policy_id2dir(pol->index);
1220 if (pol->walk.dead ||
1221 dir >= XFRM_POLICY_MAX ||
1225 __xfrm_policy_unlink(pol, dir);
1226 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1228 xfrm_audit_policy_delete(pol, 1, task_valid);
1229 xfrm_policy_kill(pol);
1230 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1234 __xfrm_policy_inexact_flush(net);
1238 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1241 EXPORT_SYMBOL(xfrm_policy_flush);
1243 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1244 int (*func)(struct xfrm_policy *, int, int, void*),
1247 struct xfrm_policy *pol;
1248 struct xfrm_policy_walk_entry *x;
1251 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1252 walk->type != XFRM_POLICY_TYPE_ANY)
1255 if (list_empty(&walk->walk.all) && walk->seq != 0)
1258 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1259 if (list_empty(&walk->walk.all))
1260 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
1262 x = list_first_entry(&walk->walk.all,
1263 struct xfrm_policy_walk_entry, all);
1265 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
1268 pol = container_of(x, struct xfrm_policy, walk);
1269 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1270 walk->type != pol->type)
1272 error = func(pol, xfrm_policy_id2dir(pol->index),
1275 list_move_tail(&walk->walk.all, &x->all);
1280 if (walk->seq == 0) {
1284 list_del_init(&walk->walk.all);
1286 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1289 EXPORT_SYMBOL(xfrm_policy_walk);
1291 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1293 INIT_LIST_HEAD(&walk->walk.all);
1294 walk->walk.dead = 1;
1298 EXPORT_SYMBOL(xfrm_policy_walk_init);
1300 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
1302 if (list_empty(&walk->walk.all))
1305 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
1306 list_del(&walk->walk.all);
1307 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1309 EXPORT_SYMBOL(xfrm_policy_walk_done);
1312 * Find policy to apply to this flow.
1314 * Returns 0 if policy found, else an -errno.
1316 static int xfrm_policy_match(const struct xfrm_policy *pol,
1317 const struct flowi *fl,
1318 u8 type, u16 family, int dir, u32 if_id)
1320 const struct xfrm_selector *sel = &pol->selector;
1324 if (pol->family != family ||
1325 pol->if_id != if_id ||
1326 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
1330 match = xfrm_selector_match(sel, fl, family);
1332 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1337 static struct xfrm_pol_inexact_bin *
1338 xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family, u8 dir)
1340 struct xfrm_pol_inexact_key k = {
1346 write_pnet(&k.net, net);
1348 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1349 xfrm_pol_inexact_params);
1352 static struct xfrm_pol_inexact_bin *
1353 xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir)
1355 struct xfrm_pol_inexact_bin *bin;
1357 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1360 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir);
1366 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
1367 const struct flowi *fl,
1371 const xfrm_address_t *daddr, *saddr;
1372 struct xfrm_pol_inexact_bin *bin;
1373 struct xfrm_policy *pol, *ret;
1374 struct hlist_head *chain;
1375 unsigned int sequence;
1379 daddr = xfrm_flowi_daddr(fl, family);
1380 saddr = xfrm_flowi_saddr(fl, family);
1381 if (unlikely(!daddr || !saddr))
1387 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1388 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1389 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1393 hlist_for_each_entry_rcu(pol, chain, bydst) {
1394 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
1404 priority = ret->priority;
1408 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir);
1411 chain = &bin->hhead;
1412 hlist_for_each_entry_rcu(pol, chain, bydst) {
1413 if ((pol->priority >= priority) && ret)
1416 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
1431 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1434 if (ret && !xfrm_pol_hold_rcu(ret))
1442 static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
1443 const struct flowi *fl,
1444 u16 family, u8 dir, u32 if_id)
1446 #ifdef CONFIG_XFRM_SUB_POLICY
1447 struct xfrm_policy *pol;
1449 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
1454 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
1458 static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
1459 const struct flowi *fl,
1460 u16 family, u32 if_id)
1462 struct xfrm_policy *pol;
1466 pol = rcu_dereference(sk->sk_policy[dir]);
1471 if (pol->family != family) {
1476 match = xfrm_selector_match(&pol->selector, fl, family);
1478 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
1479 pol->if_id != if_id) {
1483 err = security_xfrm_policy_lookup(pol->security,
1487 if (!xfrm_pol_hold_rcu(pol))
1489 } else if (err == -ESRCH) {
1502 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1504 struct net *net = xp_net(pol);
1506 list_add(&pol->walk.all, &net->xfrm.policy_all);
1507 net->xfrm.policy_count[dir]++;
1511 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1514 struct net *net = xp_net(pol);
1516 if (list_empty(&pol->walk.all))
1519 /* Socket policies are not hashed. */
1520 if (!hlist_unhashed(&pol->bydst)) {
1521 hlist_del_rcu(&pol->bydst);
1522 hlist_del_init(&pol->bydst_inexact_list);
1523 hlist_del(&pol->byidx);
1526 list_del_init(&pol->walk.all);
1527 net->xfrm.policy_count[dir]--;
1532 static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1534 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1537 static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1539 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1542 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1544 struct net *net = xp_net(pol);
1546 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1547 pol = __xfrm_policy_unlink(pol, dir);
1548 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1550 xfrm_policy_kill(pol);
1555 EXPORT_SYMBOL(xfrm_policy_delete);
1557 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1559 struct net *net = sock_net(sk);
1560 struct xfrm_policy *old_pol;
1562 #ifdef CONFIG_XFRM_SUB_POLICY
1563 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1567 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1568 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1569 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
1571 pol->curlft.add_time = ktime_get_real_seconds();
1572 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
1573 xfrm_sk_policy_link(pol, dir);
1575 rcu_assign_pointer(sk->sk_policy[dir], pol);
1578 xfrm_policy_requeue(old_pol, pol);
1580 /* Unlinking succeeds always. This is the only function
1581 * allowed to delete or replace socket policy.
1583 xfrm_sk_policy_unlink(old_pol, dir);
1585 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1588 xfrm_policy_kill(old_pol);
1593 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1595 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1596 struct net *net = xp_net(old);
1599 newp->selector = old->selector;
1600 if (security_xfrm_policy_clone(old->security,
1603 return NULL; /* ENOMEM */
1605 newp->lft = old->lft;
1606 newp->curlft = old->curlft;
1607 newp->mark = old->mark;
1608 newp->if_id = old->if_id;
1609 newp->action = old->action;
1610 newp->flags = old->flags;
1611 newp->xfrm_nr = old->xfrm_nr;
1612 newp->index = old->index;
1613 newp->type = old->type;
1614 newp->family = old->family;
1615 memcpy(newp->xfrm_vec, old->xfrm_vec,
1616 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1617 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1618 xfrm_sk_policy_link(newp, dir);
1619 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1625 int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
1627 const struct xfrm_policy *p;
1628 struct xfrm_policy *np;
1632 for (i = 0; i < 2; i++) {
1633 p = rcu_dereference(osk->sk_policy[i]);
1635 np = clone_policy(p, i);
1636 if (unlikely(!np)) {
1640 rcu_assign_pointer(sk->sk_policy[i], np);
1648 xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1649 xfrm_address_t *remote, unsigned short family, u32 mark)
1652 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1654 if (unlikely(afinfo == NULL))
1656 err = afinfo->get_saddr(net, oif, local, remote, mark);
1661 /* Resolve list of templates for the flow, given policy. */
1664 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1665 struct xfrm_state **xfrm, unsigned short family)
1667 struct net *net = xp_net(policy);
1670 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1671 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1674 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1675 struct xfrm_state *x;
1676 xfrm_address_t *remote = daddr;
1677 xfrm_address_t *local = saddr;
1678 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1680 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1681 tmpl->mode == XFRM_MODE_BEET) {
1682 remote = &tmpl->id.daddr;
1683 local = &tmpl->saddr;
1684 if (xfrm_addr_any(local, tmpl->encap_family)) {
1685 error = xfrm_get_saddr(net, fl->flowi_oif,
1687 tmpl->encap_family, 0);
1694 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
1695 family, policy->if_id);
1697 if (x && x->km.state == XFRM_STATE_VALID) {
1704 error = (x->km.state == XFRM_STATE_ERROR ?
1707 } else if (error == -ESRCH) {
1711 if (!tmpl->optional)
1717 for (nx--; nx >= 0; nx--)
1718 xfrm_state_put(xfrm[nx]);
1723 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1724 struct xfrm_state **xfrm, unsigned short family)
1726 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1727 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1733 for (i = 0; i < npols; i++) {
1734 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1739 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1747 /* found states are sorted for outbound processing */
1749 xfrm_state_sort(xfrm, tpp, cnx, family);
1754 for (cnx--; cnx >= 0; cnx--)
1755 xfrm_state_put(tpp[cnx]);
1760 static int xfrm_get_tos(const struct flowi *fl, int family)
1762 const struct xfrm_policy_afinfo *afinfo;
1765 afinfo = xfrm_policy_get_afinfo(family);
1769 tos = afinfo->get_tos(fl);
1776 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1778 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1779 struct dst_ops *dst_ops;
1780 struct xfrm_dst *xdst;
1783 return ERR_PTR(-EINVAL);
1787 dst_ops = &net->xfrm.xfrm4_dst_ops;
1789 #if IS_ENABLED(CONFIG_IPV6)
1791 dst_ops = &net->xfrm.xfrm6_dst_ops;
1797 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
1800 struct dst_entry *dst = &xdst->u.dst;
1802 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1804 xdst = ERR_PTR(-ENOBUFS);
1811 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1814 const struct xfrm_policy_afinfo *afinfo =
1815 xfrm_policy_get_afinfo(dst->ops->family);
1821 err = afinfo->init_path(path, dst, nfheader_len);
1828 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1829 const struct flowi *fl)
1831 const struct xfrm_policy_afinfo *afinfo =
1832 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1838 err = afinfo->fill_dst(xdst, dev, fl);
1846 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1847 * all the metrics... Shortly, bundle a bundle.
1850 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1851 struct xfrm_state **xfrm,
1852 struct xfrm_dst **bundle,
1854 const struct flowi *fl,
1855 struct dst_entry *dst)
1857 struct net *net = xp_net(policy);
1858 unsigned long now = jiffies;
1859 struct net_device *dev;
1860 struct xfrm_mode *inner_mode;
1861 struct xfrm_dst *xdst_prev = NULL;
1862 struct xfrm_dst *xdst0 = NULL;
1866 int nfheader_len = 0;
1867 int trailer_len = 0;
1869 int family = policy->selector.family;
1870 xfrm_address_t saddr, daddr;
1872 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1874 tos = xfrm_get_tos(fl, family);
1878 for (; i < nx; i++) {
1879 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1880 struct dst_entry *dst1 = &xdst->u.dst;
1882 err = PTR_ERR(xdst);
1892 /* Ref count is taken during xfrm_alloc_dst()
1893 * No need to do dst_clone() on dst1
1895 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
1897 if (xfrm[i]->sel.family == AF_UNSPEC) {
1898 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1899 xfrm_af2proto(family));
1901 err = -EAFNOSUPPORT;
1906 inner_mode = xfrm[i]->inner_mode;
1909 dst_copy_metrics(dst1, dst);
1911 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1912 __u32 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
1914 family = xfrm[i]->props.family;
1915 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1916 &saddr, &daddr, family, mark);
1923 dst1->xfrm = xfrm[i];
1924 xdst->xfrm_genid = xfrm[i]->genid;
1926 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1927 dst1->flags |= DST_HOST;
1928 dst1->lastuse = now;
1930 dst1->input = dst_discard;
1931 dst1->output = inner_mode->afinfo->output;
1935 header_len += xfrm[i]->props.header_len;
1936 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1937 nfheader_len += xfrm[i]->props.header_len;
1938 trailer_len += xfrm[i]->props.trailer_len;
1941 xfrm_dst_set_child(xdst_prev, dst);
1949 xfrm_init_path(xdst0, dst, nfheader_len);
1950 xfrm_init_pmtu(bundle, nx);
1952 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
1953 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
1954 err = xfrm_fill_dst(xdst_prev, dev, fl);
1958 xdst_prev->u.dst.header_len = header_len;
1959 xdst_prev->u.dst.trailer_len = trailer_len;
1960 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
1961 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
1964 return &xdst0->u.dst;
1968 xfrm_state_put(xfrm[i]);
1971 dst_release_immediate(&xdst0->u.dst);
1973 return ERR_PTR(err);
1976 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1977 struct xfrm_policy **pols,
1978 int *num_pols, int *num_xfrms)
1982 if (*num_pols == 0 || !pols[0]) {
1987 if (IS_ERR(pols[0]))
1988 return PTR_ERR(pols[0]);
1990 *num_xfrms = pols[0]->xfrm_nr;
1992 #ifdef CONFIG_XFRM_SUB_POLICY
1993 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1994 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1995 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1996 XFRM_POLICY_TYPE_MAIN,
2001 if (IS_ERR(pols[1])) {
2002 xfrm_pols_put(pols, *num_pols);
2003 return PTR_ERR(pols[1]);
2006 (*num_xfrms) += pols[1]->xfrm_nr;
2010 for (i = 0; i < *num_pols; i++) {
2011 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2021 static struct xfrm_dst *
2022 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
2023 const struct flowi *fl, u16 family,
2024 struct dst_entry *dst_orig)
2026 struct net *net = xp_net(pols[0]);
2027 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
2028 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
2029 struct xfrm_dst *xdst;
2030 struct dst_entry *dst;
2033 /* Try to instantiate a bundle */
2034 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
2040 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2041 return ERR_PTR(err);
2044 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
2046 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2047 return ERR_CAST(dst);
2050 xdst = (struct xfrm_dst *)dst;
2051 xdst->num_xfrms = err;
2052 xdst->num_pols = num_pols;
2053 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2054 xdst->policy_genid = atomic_read(&pols[0]->genid);
2059 static void xfrm_policy_queue_process(struct timer_list *t)
2061 struct sk_buff *skb;
2063 struct dst_entry *dst;
2064 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
2065 struct net *net = xp_net(pol);
2066 struct xfrm_policy_queue *pq = &pol->polq;
2068 struct sk_buff_head list;
2070 spin_lock(&pq->hold_queue.lock);
2071 skb = skb_peek(&pq->hold_queue);
2073 spin_unlock(&pq->hold_queue.lock);
2078 xfrm_decode_session(skb, &fl, dst->ops->family);
2079 spin_unlock(&pq->hold_queue.lock);
2081 dst_hold(xfrm_dst_path(dst));
2082 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
2086 if (dst->flags & DST_XFRM_QUEUE) {
2089 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2092 pq->timeout = pq->timeout << 1;
2093 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2100 __skb_queue_head_init(&list);
2102 spin_lock(&pq->hold_queue.lock);
2104 skb_queue_splice_init(&pq->hold_queue, &list);
2105 spin_unlock(&pq->hold_queue.lock);
2107 while (!skb_queue_empty(&list)) {
2108 skb = __skb_dequeue(&list);
2110 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
2111 dst_hold(xfrm_dst_path(skb_dst(skb)));
2112 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
2120 skb_dst_set(skb, dst);
2122 dst_output(net, skb->sk, skb);
2131 skb_queue_purge(&pq->hold_queue);
2135 static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
2137 unsigned long sched_next;
2138 struct dst_entry *dst = skb_dst(skb);
2139 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
2140 struct xfrm_policy *pol = xdst->pols[0];
2141 struct xfrm_policy_queue *pq = &pol->polq;
2143 if (unlikely(skb_fclone_busy(sk, skb))) {
2148 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2155 spin_lock_bh(&pq->hold_queue.lock);
2158 pq->timeout = XFRM_QUEUE_TMO_MIN;
2160 sched_next = jiffies + pq->timeout;
2162 if (del_timer(&pq->hold_timer)) {
2163 if (time_before(pq->hold_timer.expires, sched_next))
2164 sched_next = pq->hold_timer.expires;
2168 __skb_queue_tail(&pq->hold_queue, skb);
2169 if (!mod_timer(&pq->hold_timer, sched_next))
2172 spin_unlock_bh(&pq->hold_queue.lock);
2177 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
2178 struct xfrm_flo *xflo,
2179 const struct flowi *fl,
2184 struct net_device *dev;
2185 struct dst_entry *dst;
2186 struct dst_entry *dst1;
2187 struct xfrm_dst *xdst;
2189 xdst = xfrm_alloc_dst(net, family);
2193 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2194 net->xfrm.sysctl_larval_drop ||
2198 dst = xflo->dst_orig;
2199 dst1 = &xdst->u.dst;
2203 dst_copy_metrics(dst1, dst);
2205 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2206 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2207 dst1->lastuse = jiffies;
2209 dst1->input = dst_discard;
2210 dst1->output = xdst_queue_output;
2213 xfrm_dst_set_child(xdst, dst);
2216 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2223 err = xfrm_fill_dst(xdst, dev, fl);
2232 xdst = ERR_PTR(err);
2236 static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2237 const struct flowi *fl,
2239 struct xfrm_flo *xflo, u32 if_id)
2241 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2242 int num_pols = 0, num_xfrms = 0, err;
2243 struct xfrm_dst *xdst;
2245 /* Resolve policies to use if we couldn't get them from
2246 * previous cache entry */
2248 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
2249 err = xfrm_expand_policies(fl, family, pols,
2250 &num_pols, &num_xfrms);
2256 goto make_dummy_bundle;
2258 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2261 err = PTR_ERR(xdst);
2262 if (err == -EREMOTE) {
2263 xfrm_pols_put(pols, num_pols);
2269 goto make_dummy_bundle;
2270 } else if (xdst == NULL) {
2272 goto make_dummy_bundle;
2278 /* We found policies, but there's no bundles to instantiate:
2279 * either because the policy blocks, has no transformations or
2280 * we could not build template (no xfrm_states).*/
2281 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
2283 xfrm_pols_put(pols, num_pols);
2284 return ERR_CAST(xdst);
2286 xdst->num_pols = num_pols;
2287 xdst->num_xfrms = num_xfrms;
2288 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2293 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2295 xfrm_pols_put(pols, num_pols);
2296 return ERR_PTR(err);
2299 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2300 struct dst_entry *dst_orig)
2302 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2303 struct dst_entry *ret;
2306 dst_release(dst_orig);
2307 return ERR_PTR(-EINVAL);
2309 ret = afinfo->blackhole_route(net, dst_orig);
2316 /* Finds/creates a bundle for given flow and if_id
2318 * At the moment we eat a raw IP route. Mostly to speed up lookups
2319 * on interfaces with disabled IPsec.
2321 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2324 struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
2325 struct dst_entry *dst_orig,
2326 const struct flowi *fl,
2327 const struct sock *sk,
2328 int flags, u32 if_id)
2330 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2331 struct xfrm_dst *xdst;
2332 struct dst_entry *dst, *route;
2333 u16 family = dst_orig->ops->family;
2334 u8 dir = XFRM_POLICY_OUT;
2335 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2341 sk = sk_const_to_full_sk(sk);
2342 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2344 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
2346 err = xfrm_expand_policies(fl, family, pols,
2347 &num_pols, &num_xfrms);
2352 if (num_xfrms <= 0) {
2353 drop_pols = num_pols;
2357 xdst = xfrm_resolve_and_create_bundle(
2362 xfrm_pols_put(pols, num_pols);
2363 err = PTR_ERR(xdst);
2364 if (err == -EREMOTE)
2368 } else if (xdst == NULL) {
2370 drop_pols = num_pols;
2374 route = xdst->route;
2379 struct xfrm_flo xflo;
2381 xflo.dst_orig = dst_orig;
2384 /* To accelerate a bit... */
2385 if ((dst_orig->flags & DST_NOXFRM) ||
2386 !net->xfrm.policy_count[XFRM_POLICY_OUT])
2389 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
2393 err = PTR_ERR(xdst);
2397 num_pols = xdst->num_pols;
2398 num_xfrms = xdst->num_xfrms;
2399 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
2400 route = xdst->route;
2404 if (route == NULL && num_xfrms > 0) {
2405 /* The only case when xfrm_bundle_lookup() returns a
2406 * bundle with null route, is when the template could
2407 * not be resolved. It means policies are there, but
2408 * bundle could not be created, since we don't yet
2409 * have the xfrm_state's. We need to wait for KM to
2410 * negotiate new SA's or bail out with error.*/
2411 if (net->xfrm.sysctl_larval_drop) {
2412 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2419 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2427 if ((flags & XFRM_LOOKUP_ICMP) &&
2428 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2433 for (i = 0; i < num_pols; i++)
2434 pols[i]->curlft.use_time = ktime_get_real_seconds();
2436 if (num_xfrms < 0) {
2437 /* Prohibit the flow */
2438 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2441 } else if (num_xfrms > 0) {
2442 /* Flow transformed */
2443 dst_release(dst_orig);
2445 /* Flow passes untransformed */
2450 xfrm_pols_put(pols, drop_pols);
2451 if (dst && dst->xfrm &&
2452 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2453 dst->flags |= DST_XFRM_TUNNEL;
2457 if (!(flags & XFRM_LOOKUP_ICMP)) {
2465 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2466 dst_release(dst_orig);
2467 xfrm_pols_put(pols, drop_pols);
2468 return ERR_PTR(err);
2470 EXPORT_SYMBOL(xfrm_lookup_with_ifid);
2472 /* Main function: finds/creates a bundle for given flow.
2474 * At the moment we eat a raw IP route. Mostly to speed up lookups
2475 * on interfaces with disabled IPsec.
2477 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2478 const struct flowi *fl, const struct sock *sk,
2481 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
2483 EXPORT_SYMBOL(xfrm_lookup);
2485 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2486 * Otherwise we may send out blackholed packets.
2488 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2489 const struct flowi *fl,
2490 const struct sock *sk, int flags)
2492 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2493 flags | XFRM_LOOKUP_QUEUE |
2494 XFRM_LOOKUP_KEEP_DST_REF);
2496 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2497 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2500 dst_release(dst_orig);
2504 EXPORT_SYMBOL(xfrm_lookup_route);
2507 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2509 struct xfrm_state *x;
2511 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2513 x = skb->sp->xvec[idx];
2514 if (!x->type->reject)
2516 return x->type->reject(x, skb, fl);
2519 /* When skb is transformed back to its "native" form, we have to
2520 * check policy restrictions. At the moment we make this in maximally
2521 * stupid way. Shame on me. :-) Of course, connected sockets must
2522 * have policy cached at them.
2526 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2527 unsigned short family)
2529 if (xfrm_state_kern(x))
2530 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2531 return x->id.proto == tmpl->id.proto &&
2532 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2533 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2534 x->props.mode == tmpl->mode &&
2535 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2536 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2537 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2538 xfrm_state_addr_cmp(tmpl, x, family));
2542 * 0 or more than 0 is returned when validation is succeeded (either bypass
2543 * because of optional transport mode, or next index of the mathced secpath
2544 * state with the template.
2545 * -1 is returned when no matching template is found.
2546 * Otherwise "-2 - errored_index" is returned.
2549 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2550 unsigned short family)
2554 if (tmpl->optional) {
2555 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2559 for (; idx < sp->len; idx++) {
2560 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2562 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2571 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2572 unsigned int family, int reverse)
2574 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2577 if (unlikely(afinfo == NULL))
2578 return -EAFNOSUPPORT;
2580 afinfo->decode_session(skb, fl, reverse);
2582 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2586 EXPORT_SYMBOL(__xfrm_decode_session);
2588 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2590 for (; k < sp->len; k++) {
2591 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2600 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2601 unsigned short family)
2603 struct net *net = dev_net(skb->dev);
2604 struct xfrm_policy *pol;
2605 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2612 const struct xfrm_if_cb *ifcb;
2617 ifcb = xfrm_if_get_cb();
2620 xi = ifcb->decode_session(skb);
2622 if_id = xi->p.if_id;
2626 reverse = dir & ~XFRM_POLICY_MASK;
2627 dir &= XFRM_POLICY_MASK;
2629 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2630 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2634 nf_nat_decode_session(skb, &fl, family);
2636 /* First, check used SA against their selectors. */
2640 for (i = skb->sp->len-1; i >= 0; i--) {
2641 struct xfrm_state *x = skb->sp->xvec[i];
2642 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2643 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2650 sk = sk_to_full_sk(sk);
2651 if (sk && sk->sk_policy[dir]) {
2652 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
2654 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2660 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
2663 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2668 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2669 xfrm_secpath_reject(xerr_idx, skb, &fl);
2670 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2676 pol->curlft.use_time = ktime_get_real_seconds();
2680 #ifdef CONFIG_XFRM_SUB_POLICY
2681 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2682 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2684 XFRM_POLICY_IN, if_id);
2686 if (IS_ERR(pols[1])) {
2687 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2690 pols[1]->curlft.use_time = ktime_get_real_seconds();
2696 if (pol->action == XFRM_POLICY_ALLOW) {
2697 struct sec_path *sp;
2698 static struct sec_path dummy;
2699 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2700 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2701 struct xfrm_tmpl **tpp = tp;
2705 if ((sp = skb->sp) == NULL)
2708 for (pi = 0; pi < npols; pi++) {
2709 if (pols[pi] != pol &&
2710 pols[pi]->action != XFRM_POLICY_ALLOW) {
2711 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2714 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2715 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2718 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2719 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2723 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
2727 /* For each tunnel xfrm, find the first matching tmpl.
2728 * For each tmpl before that, find corresponding xfrm.
2729 * Order is _important_. Later we will implement
2730 * some barriers, but at the moment barriers
2731 * are implied between each two transformations.
2733 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2734 k = xfrm_policy_ok(tpp[i], sp, k, family);
2737 /* "-2 - errored_index" returned */
2739 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2744 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2745 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2749 xfrm_pols_put(pols, npols);
2752 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2755 xfrm_secpath_reject(xerr_idx, skb, &fl);
2757 xfrm_pols_put(pols, npols);
2760 EXPORT_SYMBOL(__xfrm_policy_check);
2762 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2764 struct net *net = dev_net(skb->dev);
2766 struct dst_entry *dst;
2769 if (xfrm_decode_session(skb, &fl, family) < 0) {
2770 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2775 if (!skb_dst(skb)) {
2776 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2780 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
2785 skb_dst_set(skb, dst);
2788 EXPORT_SYMBOL(__xfrm_route_forward);
2790 /* Optimize later using cookies and generation ids. */
2792 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2794 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2795 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2796 * get validated by dst_ops->check on every use. We do this
2797 * because when a normal route referenced by an XFRM dst is
2798 * obsoleted we do not go looking around for all parent
2799 * referencing XFRM dsts so that we can invalidate them. It
2800 * is just too much work. Instead we make the checks here on
2801 * every use. For example:
2803 * XFRM dst A --> IPv4 dst X
2805 * X is the "xdst->route" of A (X is also the "dst->path" of A
2806 * in this example). If X is marked obsolete, "A" will not
2807 * notice. That's what we are validating here via the
2808 * stale_bundle() check.
2810 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2812 * This will force stale_bundle() to fail on any xdst bundle with
2813 * this dst linked in it.
2815 if (dst->obsolete < 0 && !stale_bundle(dst))
2821 static int stale_bundle(struct dst_entry *dst)
2823 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2826 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2828 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
2829 dst->dev = dev_net(dev)->loopback_dev;
2834 EXPORT_SYMBOL(xfrm_dst_ifdown);
2836 static void xfrm_link_failure(struct sk_buff *skb)
2838 /* Impossible. Such dst must be popped before reaches point of failure. */
2841 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2844 if (dst->obsolete) {
2852 static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
2855 struct xfrm_dst *xdst = bundle[nr];
2856 u32 pmtu, route_mtu_cached;
2857 struct dst_entry *dst;
2860 pmtu = dst_mtu(xfrm_dst_child(dst));
2861 xdst->child_mtu_cached = pmtu;
2863 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2865 route_mtu_cached = dst_mtu(xdst->route);
2866 xdst->route_mtu_cached = route_mtu_cached;
2868 if (pmtu > route_mtu_cached)
2869 pmtu = route_mtu_cached;
2871 dst_metric_set(dst, RTAX_MTU, pmtu);
2875 /* Check that the bundle accepts the flow and its components are
2879 static int xfrm_bundle_ok(struct xfrm_dst *first)
2881 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
2882 struct dst_entry *dst = &first->u.dst;
2883 struct xfrm_dst *xdst;
2887 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
2888 (dst->dev && !netif_running(dst->dev)))
2891 if (dst->flags & DST_XFRM_QUEUE)
2894 start_from = nr = 0;
2896 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2898 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2900 if (xdst->xfrm_genid != dst->xfrm->genid)
2902 if (xdst->num_pols > 0 &&
2903 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2906 bundle[nr++] = xdst;
2908 mtu = dst_mtu(xfrm_dst_child(dst));
2909 if (xdst->child_mtu_cached != mtu) {
2911 xdst->child_mtu_cached = mtu;
2914 if (!dst_check(xdst->route, xdst->route_cookie))
2916 mtu = dst_mtu(xdst->route);
2917 if (xdst->route_mtu_cached != mtu) {
2919 xdst->route_mtu_cached = mtu;
2922 dst = xfrm_dst_child(dst);
2923 } while (dst->xfrm);
2925 if (likely(!start_from))
2928 xdst = bundle[start_from - 1];
2929 mtu = xdst->child_mtu_cached;
2930 while (start_from--) {
2933 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2934 if (mtu > xdst->route_mtu_cached)
2935 mtu = xdst->route_mtu_cached;
2936 dst_metric_set(dst, RTAX_MTU, mtu);
2940 xdst = bundle[start_from - 1];
2941 xdst->child_mtu_cached = mtu;
2947 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2949 return dst_metric_advmss(xfrm_dst_path(dst));
2952 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2954 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2956 return mtu ? : dst_mtu(xfrm_dst_path(dst));
2959 static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
2963 const struct xfrm_state *xfrm = dst->xfrm;
2965 dst = xfrm_dst_child(dst);
2967 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
2969 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
2970 daddr = xfrm->coaddr;
2971 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
2972 daddr = &xfrm->id.daddr;
2977 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2978 struct sk_buff *skb,
2981 const struct dst_entry *path = xfrm_dst_path(dst);
2984 daddr = xfrm_get_dst_nexthop(dst, daddr);
2985 return path->ops->neigh_lookup(path, skb, daddr);
2988 static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
2990 const struct dst_entry *path = xfrm_dst_path(dst);
2992 daddr = xfrm_get_dst_nexthop(dst, daddr);
2993 path->ops->confirm_neigh(path, daddr);
2996 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
3000 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
3001 return -EAFNOSUPPORT;
3003 spin_lock(&xfrm_policy_afinfo_lock);
3004 if (unlikely(xfrm_policy_afinfo[family] != NULL))
3007 struct dst_ops *dst_ops = afinfo->dst_ops;
3008 if (likely(dst_ops->kmem_cachep == NULL))
3009 dst_ops->kmem_cachep = xfrm_dst_cache;
3010 if (likely(dst_ops->check == NULL))
3011 dst_ops->check = xfrm_dst_check;
3012 if (likely(dst_ops->default_advmss == NULL))
3013 dst_ops->default_advmss = xfrm_default_advmss;
3014 if (likely(dst_ops->mtu == NULL))
3015 dst_ops->mtu = xfrm_mtu;
3016 if (likely(dst_ops->negative_advice == NULL))
3017 dst_ops->negative_advice = xfrm_negative_advice;
3018 if (likely(dst_ops->link_failure == NULL))
3019 dst_ops->link_failure = xfrm_link_failure;
3020 if (likely(dst_ops->neigh_lookup == NULL))
3021 dst_ops->neigh_lookup = xfrm_neigh_lookup;
3022 if (likely(!dst_ops->confirm_neigh))
3023 dst_ops->confirm_neigh = xfrm_confirm_neigh;
3024 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
3026 spin_unlock(&xfrm_policy_afinfo_lock);
3030 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3032 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
3034 struct dst_ops *dst_ops = afinfo->dst_ops;
3037 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3038 if (xfrm_policy_afinfo[i] != afinfo)
3040 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3046 dst_ops->kmem_cachep = NULL;
3047 dst_ops->check = NULL;
3048 dst_ops->negative_advice = NULL;
3049 dst_ops->link_failure = NULL;
3051 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3053 void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3055 spin_lock(&xfrm_if_cb_lock);
3056 rcu_assign_pointer(xfrm_if_cb, ifcb);
3057 spin_unlock(&xfrm_if_cb_lock);
3059 EXPORT_SYMBOL(xfrm_if_register_cb);
3061 void xfrm_if_unregister_cb(void)
3063 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3066 EXPORT_SYMBOL(xfrm_if_unregister_cb);
3068 #ifdef CONFIG_XFRM_STATISTICS
3069 static int __net_init xfrm_statistics_init(struct net *net)
3072 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3073 if (!net->mib.xfrm_statistics)
3075 rv = xfrm_proc_init(net);
3077 free_percpu(net->mib.xfrm_statistics);
3081 static void xfrm_statistics_fini(struct net *net)
3083 xfrm_proc_fini(net);
3084 free_percpu(net->mib.xfrm_statistics);
3087 static int __net_init xfrm_statistics_init(struct net *net)
3092 static void xfrm_statistics_fini(struct net *net)
3097 static int __net_init xfrm_policy_init(struct net *net)
3099 unsigned int hmask, sz;
3102 if (net_eq(net, &init_net)) {
3103 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
3104 sizeof(struct xfrm_dst),
3105 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3107 err = rhashtable_init(&xfrm_policy_inexact_table,
3108 &xfrm_pol_inexact_params);
3113 sz = (hmask+1) * sizeof(struct hlist_head);
3115 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3116 if (!net->xfrm.policy_byidx)
3118 net->xfrm.policy_idx_hmask = hmask;
3120 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
3121 struct xfrm_policy_hash *htab;
3123 net->xfrm.policy_count[dir] = 0;
3124 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
3125 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
3127 htab = &net->xfrm.policy_bydst[dir];
3128 htab->table = xfrm_hash_alloc(sz);
3131 htab->hmask = hmask;
3137 net->xfrm.policy_hthresh.lbits4 = 32;
3138 net->xfrm.policy_hthresh.rbits4 = 32;
3139 net->xfrm.policy_hthresh.lbits6 = 128;
3140 net->xfrm.policy_hthresh.rbits6 = 128;
3142 seqlock_init(&net->xfrm.policy_hthresh.lock);
3144 INIT_LIST_HEAD(&net->xfrm.policy_all);
3145 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
3146 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
3147 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
3151 for (dir--; dir >= 0; dir--) {
3152 struct xfrm_policy_hash *htab;
3154 htab = &net->xfrm.policy_bydst[dir];
3155 xfrm_hash_free(htab->table, sz);
3157 xfrm_hash_free(net->xfrm.policy_byidx, sz);
3162 static void xfrm_policy_fini(struct net *net)
3164 struct xfrm_pol_inexact_bin *bin, *tmp;
3168 flush_work(&net->xfrm.policy_hash_work);
3169 #ifdef CONFIG_XFRM_SUB_POLICY
3170 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
3172 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
3174 WARN_ON(!list_empty(&net->xfrm.policy_all));
3176 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
3177 struct xfrm_policy_hash *htab;
3179 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
3181 htab = &net->xfrm.policy_bydst[dir];
3182 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
3183 WARN_ON(!hlist_empty(htab->table));
3184 xfrm_hash_free(htab->table, sz);
3187 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
3188 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3189 xfrm_hash_free(net->xfrm.policy_byidx, sz);
3191 list_for_each_entry_safe(bin, tmp, &net->xfrm.inexact_bins,
3193 WARN_ON(!hlist_empty(&bin->hhead));
3194 xfrm_policy_inexact_delete_bin(net, bin);
3198 static int __net_init xfrm_net_init(struct net *net)
3202 /* Initialize the per-net locks here */
3203 spin_lock_init(&net->xfrm.xfrm_state_lock);
3204 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3205 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3207 rv = xfrm_statistics_init(net);
3209 goto out_statistics;
3210 rv = xfrm_state_init(net);
3213 rv = xfrm_policy_init(net);
3216 rv = xfrm_sysctl_init(net);
3223 xfrm_policy_fini(net);
3225 xfrm_state_fini(net);
3227 xfrm_statistics_fini(net);
3232 static void __net_exit xfrm_net_exit(struct net *net)
3234 xfrm_sysctl_fini(net);
3235 xfrm_policy_fini(net);
3236 xfrm_state_fini(net);
3237 xfrm_statistics_fini(net);
3240 static struct pernet_operations __net_initdata xfrm_net_ops = {
3241 .init = xfrm_net_init,
3242 .exit = xfrm_net_exit,
3245 void __init xfrm_init(void)
3247 register_pernet_subsys(&xfrm_net_ops);
3249 seqcount_init(&xfrm_policy_hash_generation);
3252 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3256 #ifdef CONFIG_AUDITSYSCALL
3257 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3258 struct audit_buffer *audit_buf)
3260 struct xfrm_sec_ctx *ctx = xp->security;
3261 struct xfrm_selector *sel = &xp->selector;
3264 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3265 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3267 switch (sel->family) {
3269 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
3270 if (sel->prefixlen_s != 32)
3271 audit_log_format(audit_buf, " src_prefixlen=%d",
3273 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
3274 if (sel->prefixlen_d != 32)
3275 audit_log_format(audit_buf, " dst_prefixlen=%d",
3279 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
3280 if (sel->prefixlen_s != 128)
3281 audit_log_format(audit_buf, " src_prefixlen=%d",
3283 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
3284 if (sel->prefixlen_d != 128)
3285 audit_log_format(audit_buf, " dst_prefixlen=%d",
3291 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
3293 struct audit_buffer *audit_buf;
3295 audit_buf = xfrm_audit_start("SPD-add");
3296 if (audit_buf == NULL)
3298 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3299 audit_log_format(audit_buf, " res=%u", result);
3300 xfrm_audit_common_policyinfo(xp, audit_buf);
3301 audit_log_end(audit_buf);
3303 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3305 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
3308 struct audit_buffer *audit_buf;
3310 audit_buf = xfrm_audit_start("SPD-delete");
3311 if (audit_buf == NULL)
3313 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3314 audit_log_format(audit_buf, " res=%u", result);
3315 xfrm_audit_common_policyinfo(xp, audit_buf);
3316 audit_log_end(audit_buf);
3318 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3321 #ifdef CONFIG_XFRM_MIGRATE
3322 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3323 const struct xfrm_selector *sel_tgt)
3325 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3326 if (sel_tgt->family == sel_cmp->family &&
3327 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3329 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3331 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3332 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
3336 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
3343 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3344 u8 dir, u8 type, struct net *net)
3346 struct xfrm_policy *pol, *ret = NULL;
3347 struct hlist_head *chain;
3350 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
3351 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
3352 hlist_for_each_entry(pol, chain, bydst) {
3353 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3354 pol->type == type) {
3356 priority = ret->priority;
3360 chain = &net->xfrm.policy_inexact[dir];
3361 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
3362 if ((pol->priority >= priority) && ret)
3365 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3366 pol->type == type) {
3374 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
3379 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3383 if (t->mode == m->mode && t->id.proto == m->proto &&
3384 (m->reqid == 0 || t->reqid == m->reqid)) {
3386 case XFRM_MODE_TUNNEL:
3387 case XFRM_MODE_BEET:
3388 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3390 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3395 case XFRM_MODE_TRANSPORT:
3396 /* in case of transport mode, template does not store
3397 any IP addresses, hence we just compare mode and
3408 /* update endpoint address(es) of template(s) */
3409 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3410 struct xfrm_migrate *m, int num_migrate)
3412 struct xfrm_migrate *mp;
3415 write_lock_bh(&pol->lock);
3416 if (unlikely(pol->walk.dead)) {
3417 /* target policy has been deleted */
3418 write_unlock_bh(&pol->lock);
3422 for (i = 0; i < pol->xfrm_nr; i++) {
3423 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3424 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3427 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3428 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3430 /* update endpoints */
3431 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3432 sizeof(pol->xfrm_vec[i].id.daddr));
3433 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3434 sizeof(pol->xfrm_vec[i].saddr));
3435 pol->xfrm_vec[i].encap_family = mp->new_family;
3437 atomic_inc(&pol->genid);
3441 write_unlock_bh(&pol->lock);
3449 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3453 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3456 for (i = 0; i < num_migrate; i++) {
3457 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3458 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3461 /* check if there is any duplicated entry */
3462 for (j = i + 1; j < num_migrate; j++) {
3463 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3464 sizeof(m[i].old_daddr)) &&
3465 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3466 sizeof(m[i].old_saddr)) &&
3467 m[i].proto == m[j].proto &&
3468 m[i].mode == m[j].mode &&
3469 m[i].reqid == m[j].reqid &&
3470 m[i].old_family == m[j].old_family)
3478 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3479 struct xfrm_migrate *m, int num_migrate,
3480 struct xfrm_kmaddress *k, struct net *net,
3481 struct xfrm_encap_tmpl *encap)
3483 int i, err, nx_cur = 0, nx_new = 0;
3484 struct xfrm_policy *pol = NULL;
3485 struct xfrm_state *x, *xc;
3486 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3487 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3488 struct xfrm_migrate *mp;
3490 /* Stage 0 - sanity checks */
3491 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3494 if (dir >= XFRM_POLICY_MAX) {
3499 /* Stage 1 - find policy */
3500 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
3505 /* Stage 2 - find and update state(s) */
3506 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3507 if ((x = xfrm_migrate_state_find(mp, net))) {
3510 xc = xfrm_state_migrate(x, mp, encap);
3521 /* Stage 3 - update policy */
3522 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3525 /* Stage 4 - delete old state(s) */
3527 xfrm_states_put(x_cur, nx_cur);
3528 xfrm_states_delete(x_cur, nx_cur);
3531 /* Stage 5 - announce */
3532 km_migrate(sel, dir, type, m, num_migrate, k, encap);
3544 xfrm_states_put(x_cur, nx_cur);
3546 xfrm_states_delete(x_new, nx_new);
3550 EXPORT_SYMBOL(xfrm_migrate);