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>
33 #ifdef CONFIG_XFRM_STATISTICS
37 #include "xfrm_hash.h"
39 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
40 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
41 #define XFRM_MAX_QUEUE_LEN 100
44 struct dst_entry *dst_orig;
48 static DEFINE_SPINLOCK(xfrm_if_cb_lock);
49 static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
51 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
52 static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
55 static struct kmem_cache *xfrm_dst_cache __ro_after_init;
56 static __read_mostly seqcount_t xfrm_policy_hash_generation;
58 static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
59 static int stale_bundle(struct dst_entry *dst);
60 static int xfrm_bundle_ok(struct xfrm_dst *xdst);
61 static void xfrm_policy_queue_process(struct timer_list *t);
63 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
64 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
67 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
69 return refcount_inc_not_zero(&policy->refcnt);
73 __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
75 const struct flowi4 *fl4 = &fl->u.ip4;
77 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
78 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
79 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
80 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
81 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
82 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
86 __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
88 const struct flowi6 *fl6 = &fl->u.ip6;
90 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
91 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
92 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
93 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
94 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
95 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
98 bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
99 unsigned short family)
103 return __xfrm4_selector_match(sel, fl);
105 return __xfrm6_selector_match(sel, fl);
110 static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
112 const struct xfrm_policy_afinfo *afinfo;
114 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
117 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
118 if (unlikely(!afinfo))
123 /* Called with rcu_read_lock(). */
124 static const struct xfrm_if_cb *xfrm_if_get_cb(void)
126 return rcu_dereference(xfrm_if_cb);
129 struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
130 const xfrm_address_t *saddr,
131 const xfrm_address_t *daddr,
132 int family, u32 mark)
134 const struct xfrm_policy_afinfo *afinfo;
135 struct dst_entry *dst;
137 afinfo = xfrm_policy_get_afinfo(family);
138 if (unlikely(afinfo == NULL))
139 return ERR_PTR(-EAFNOSUPPORT);
141 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
147 EXPORT_SYMBOL(__xfrm_dst_lookup);
149 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
151 xfrm_address_t *prev_saddr,
152 xfrm_address_t *prev_daddr,
153 int family, u32 mark)
155 struct net *net = xs_net(x);
156 xfrm_address_t *saddr = &x->props.saddr;
157 xfrm_address_t *daddr = &x->id.daddr;
158 struct dst_entry *dst;
160 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
164 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
169 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
172 if (prev_saddr != saddr)
173 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
174 if (prev_daddr != daddr)
175 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
181 static inline unsigned long make_jiffies(long secs)
183 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
184 return MAX_SCHEDULE_TIMEOUT-1;
189 static void xfrm_policy_timer(struct timer_list *t)
191 struct xfrm_policy *xp = from_timer(xp, t, timer);
192 time64_t now = ktime_get_real_seconds();
193 time64_t next = TIME64_MAX;
197 read_lock(&xp->lock);
199 if (unlikely(xp->walk.dead))
202 dir = xfrm_policy_id2dir(xp->index);
204 if (xp->lft.hard_add_expires_seconds) {
205 time64_t tmo = xp->lft.hard_add_expires_seconds +
206 xp->curlft.add_time - now;
212 if (xp->lft.hard_use_expires_seconds) {
213 time64_t tmo = xp->lft.hard_use_expires_seconds +
214 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
220 if (xp->lft.soft_add_expires_seconds) {
221 time64_t tmo = xp->lft.soft_add_expires_seconds +
222 xp->curlft.add_time - now;
225 tmo = XFRM_KM_TIMEOUT;
230 if (xp->lft.soft_use_expires_seconds) {
231 time64_t tmo = xp->lft.soft_use_expires_seconds +
232 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
235 tmo = XFRM_KM_TIMEOUT;
242 km_policy_expired(xp, dir, 0, 0);
243 if (next != TIME64_MAX &&
244 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
248 read_unlock(&xp->lock);
253 read_unlock(&xp->lock);
254 if (!xfrm_policy_delete(xp, dir))
255 km_policy_expired(xp, dir, 1, 0);
259 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
263 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
265 struct xfrm_policy *policy;
267 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
270 write_pnet(&policy->xp_net, net);
271 INIT_LIST_HEAD(&policy->walk.all);
272 INIT_HLIST_NODE(&policy->bydst);
273 INIT_HLIST_NODE(&policy->byidx);
274 rwlock_init(&policy->lock);
275 refcount_set(&policy->refcnt, 1);
276 skb_queue_head_init(&policy->polq.hold_queue);
277 timer_setup(&policy->timer, xfrm_policy_timer, 0);
278 timer_setup(&policy->polq.hold_timer,
279 xfrm_policy_queue_process, 0);
283 EXPORT_SYMBOL(xfrm_policy_alloc);
285 static void xfrm_policy_destroy_rcu(struct rcu_head *head)
287 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
289 security_xfrm_policy_free(policy->security);
293 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
295 void xfrm_policy_destroy(struct xfrm_policy *policy)
297 BUG_ON(!policy->walk.dead);
299 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
302 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
304 EXPORT_SYMBOL(xfrm_policy_destroy);
306 /* Rule must be locked. Release descendant resources, announce
307 * entry dead. The rule must be unlinked from lists to the moment.
310 static void xfrm_policy_kill(struct xfrm_policy *policy)
312 write_lock_bh(&policy->lock);
313 policy->walk.dead = 1;
314 write_unlock_bh(&policy->lock);
316 atomic_inc(&policy->genid);
318 if (del_timer(&policy->polq.hold_timer))
319 xfrm_pol_put(policy);
320 skb_queue_purge(&policy->polq.hold_queue);
322 if (del_timer(&policy->timer))
323 xfrm_pol_put(policy);
325 xfrm_pol_put(policy);
328 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
330 static inline unsigned int idx_hash(struct net *net, u32 index)
332 return __idx_hash(index, net->xfrm.policy_idx_hmask);
335 /* calculate policy hash thresholds */
336 static void __get_hash_thresh(struct net *net,
337 unsigned short family, int dir,
338 u8 *dbits, u8 *sbits)
342 *dbits = net->xfrm.policy_bydst[dir].dbits4;
343 *sbits = net->xfrm.policy_bydst[dir].sbits4;
347 *dbits = net->xfrm.policy_bydst[dir].dbits6;
348 *sbits = net->xfrm.policy_bydst[dir].sbits6;
357 static struct hlist_head *policy_hash_bysel(struct net *net,
358 const struct xfrm_selector *sel,
359 unsigned short family, int dir)
361 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
366 __get_hash_thresh(net, family, dir, &dbits, &sbits);
367 hash = __sel_hash(sel, family, hmask, dbits, sbits);
369 if (hash == hmask + 1)
370 return &net->xfrm.policy_inexact[dir];
372 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
373 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
376 static struct hlist_head *policy_hash_direct(struct net *net,
377 const xfrm_address_t *daddr,
378 const xfrm_address_t *saddr,
379 unsigned short family, int dir)
381 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
386 __get_hash_thresh(net, family, dir, &dbits, &sbits);
387 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
389 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
390 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
393 static void xfrm_dst_hash_transfer(struct net *net,
394 struct hlist_head *list,
395 struct hlist_head *ndsttable,
396 unsigned int nhashmask,
399 struct hlist_node *tmp, *entry0 = NULL;
400 struct xfrm_policy *pol;
406 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
409 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
410 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
411 pol->family, nhashmask, dbits, sbits);
413 hlist_del_rcu(&pol->bydst);
414 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
419 hlist_del_rcu(&pol->bydst);
420 hlist_add_behind_rcu(&pol->bydst, entry0);
422 entry0 = &pol->bydst;
424 if (!hlist_empty(list)) {
430 static void xfrm_idx_hash_transfer(struct hlist_head *list,
431 struct hlist_head *nidxtable,
432 unsigned int nhashmask)
434 struct hlist_node *tmp;
435 struct xfrm_policy *pol;
437 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
440 h = __idx_hash(pol->index, nhashmask);
441 hlist_add_head(&pol->byidx, nidxtable+h);
445 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
447 return ((old_hmask + 1) << 1) - 1;
450 static void xfrm_bydst_resize(struct net *net, int dir)
452 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
453 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
454 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
455 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
456 struct hlist_head *odst;
462 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
463 write_seqcount_begin(&xfrm_policy_hash_generation);
465 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
466 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
468 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
469 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
471 for (i = hmask; i >= 0; i--)
472 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
474 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
475 net->xfrm.policy_bydst[dir].hmask = nhashmask;
477 write_seqcount_end(&xfrm_policy_hash_generation);
478 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
482 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
485 static void xfrm_byidx_resize(struct net *net, int total)
487 unsigned int hmask = net->xfrm.policy_idx_hmask;
488 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
489 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
490 struct hlist_head *oidx = net->xfrm.policy_byidx;
491 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
497 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
499 for (i = hmask; i >= 0; i--)
500 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
502 net->xfrm.policy_byidx = nidx;
503 net->xfrm.policy_idx_hmask = nhashmask;
505 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
507 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
510 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
512 unsigned int cnt = net->xfrm.policy_count[dir];
513 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
518 if ((hmask + 1) < xfrm_policy_hashmax &&
525 static inline int xfrm_byidx_should_resize(struct net *net, int total)
527 unsigned int hmask = net->xfrm.policy_idx_hmask;
529 if ((hmask + 1) < xfrm_policy_hashmax &&
536 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
538 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
539 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
540 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
541 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
542 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
543 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
544 si->spdhcnt = net->xfrm.policy_idx_hmask;
545 si->spdhmcnt = xfrm_policy_hashmax;
547 EXPORT_SYMBOL(xfrm_spd_getinfo);
549 static DEFINE_MUTEX(hash_resize_mutex);
550 static void xfrm_hash_resize(struct work_struct *work)
552 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
555 mutex_lock(&hash_resize_mutex);
558 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
559 if (xfrm_bydst_should_resize(net, dir, &total))
560 xfrm_bydst_resize(net, dir);
562 if (xfrm_byidx_should_resize(net, total))
563 xfrm_byidx_resize(net, total);
565 mutex_unlock(&hash_resize_mutex);
568 static void xfrm_hash_rebuild(struct work_struct *work)
570 struct net *net = container_of(work, struct net,
571 xfrm.policy_hthresh.work);
573 struct xfrm_policy *pol;
574 struct xfrm_policy *policy;
575 struct hlist_head *chain;
576 struct hlist_head *odst;
577 struct hlist_node *newpos;
581 u8 lbits4, rbits4, lbits6, rbits6;
583 mutex_lock(&hash_resize_mutex);
585 /* read selector prefixlen thresholds */
587 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
589 lbits4 = net->xfrm.policy_hthresh.lbits4;
590 rbits4 = net->xfrm.policy_hthresh.rbits4;
591 lbits6 = net->xfrm.policy_hthresh.lbits6;
592 rbits6 = net->xfrm.policy_hthresh.rbits6;
593 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
595 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
597 /* reset the bydst and inexact table in all directions */
598 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
599 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
600 hmask = net->xfrm.policy_bydst[dir].hmask;
601 odst = net->xfrm.policy_bydst[dir].table;
602 for (i = hmask; i >= 0; i--)
603 INIT_HLIST_HEAD(odst + i);
604 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
605 /* dir out => dst = remote, src = local */
606 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
607 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
608 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
609 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
611 /* dir in/fwd => dst = local, src = remote */
612 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
613 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
614 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
615 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
619 /* re-insert all policies by order of creation */
620 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
621 if (policy->walk.dead ||
622 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
623 /* skip socket policies */
627 chain = policy_hash_bysel(net, &policy->selector,
629 xfrm_policy_id2dir(policy->index));
630 hlist_for_each_entry(pol, chain, bydst) {
631 if (policy->priority >= pol->priority)
632 newpos = &pol->bydst;
637 hlist_add_behind_rcu(&policy->bydst, newpos);
639 hlist_add_head_rcu(&policy->bydst, chain);
642 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
644 mutex_unlock(&hash_resize_mutex);
647 void xfrm_policy_hash_rebuild(struct net *net)
649 schedule_work(&net->xfrm.policy_hthresh.work);
651 EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
653 /* Generate new index... KAME seems to generate them ordered by cost
654 * of an absolute inpredictability of ordering of rules. This will not pass. */
655 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
657 static u32 idx_generator;
660 struct hlist_head *list;
661 struct xfrm_policy *p;
666 idx = (idx_generator | dir);
675 list = net->xfrm.policy_byidx + idx_hash(net, idx);
677 hlist_for_each_entry(p, list, byidx) {
678 if (p->index == idx) {
688 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
690 u32 *p1 = (u32 *) s1;
691 u32 *p2 = (u32 *) s2;
692 int len = sizeof(struct xfrm_selector) / sizeof(u32);
695 for (i = 0; i < len; i++) {
703 static void xfrm_policy_requeue(struct xfrm_policy *old,
704 struct xfrm_policy *new)
706 struct xfrm_policy_queue *pq = &old->polq;
707 struct sk_buff_head list;
709 if (skb_queue_empty(&pq->hold_queue))
712 __skb_queue_head_init(&list);
714 spin_lock_bh(&pq->hold_queue.lock);
715 skb_queue_splice_init(&pq->hold_queue, &list);
716 if (del_timer(&pq->hold_timer))
718 spin_unlock_bh(&pq->hold_queue.lock);
722 spin_lock_bh(&pq->hold_queue.lock);
723 skb_queue_splice(&list, &pq->hold_queue);
724 pq->timeout = XFRM_QUEUE_TMO_MIN;
725 if (!mod_timer(&pq->hold_timer, jiffies))
727 spin_unlock_bh(&pq->hold_queue.lock);
730 static inline bool xfrm_policy_mark_match(const struct xfrm_mark *mark,
731 struct xfrm_policy *pol)
733 return mark->v == pol->mark.v && mark->m == pol->mark.m;
736 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
738 struct net *net = xp_net(policy);
739 struct xfrm_policy *pol;
740 struct xfrm_policy *delpol;
741 struct hlist_head *chain;
742 struct hlist_node *newpos;
744 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
745 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
748 hlist_for_each_entry(pol, chain, bydst) {
749 if (pol->type == policy->type &&
750 pol->if_id == policy->if_id &&
751 !selector_cmp(&pol->selector, &policy->selector) &&
752 xfrm_policy_mark_match(&policy->mark, pol) &&
753 xfrm_sec_ctx_match(pol->security, policy->security) &&
756 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
760 if (policy->priority > pol->priority)
762 } else if (policy->priority >= pol->priority) {
763 newpos = &pol->bydst;
770 hlist_add_behind_rcu(&policy->bydst, newpos);
772 hlist_add_head_rcu(&policy->bydst, chain);
773 __xfrm_policy_link(policy, dir);
775 /* After previous checking, family can either be AF_INET or AF_INET6 */
776 if (policy->family == AF_INET)
777 rt_genid_bump_ipv4(net);
779 rt_genid_bump_ipv6(net);
782 xfrm_policy_requeue(delpol, policy);
783 __xfrm_policy_unlink(delpol, dir);
785 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
786 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
787 policy->curlft.add_time = ktime_get_real_seconds();
788 policy->curlft.use_time = 0;
789 if (!mod_timer(&policy->timer, jiffies + HZ))
790 xfrm_pol_hold(policy);
791 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
794 xfrm_policy_kill(delpol);
795 else if (xfrm_bydst_should_resize(net, dir, NULL))
796 schedule_work(&net->xfrm.policy_hash_work);
800 EXPORT_SYMBOL(xfrm_policy_insert);
803 xfrm_policy_bysel_ctx(struct net *net, const struct xfrm_mark *mark, u32 if_id,
804 u8 type, int dir, struct xfrm_selector *sel,
805 struct xfrm_sec_ctx *ctx, int delete, int *err)
807 struct xfrm_policy *pol, *ret;
808 struct hlist_head *chain;
811 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
812 chain = policy_hash_bysel(net, sel, sel->family, dir);
814 hlist_for_each_entry(pol, chain, bydst) {
815 if (pol->type == type &&
816 pol->if_id == if_id &&
817 xfrm_policy_mark_match(mark, pol) &&
818 !selector_cmp(sel, &pol->selector) &&
819 xfrm_sec_ctx_match(ctx, pol->security)) {
822 *err = security_xfrm_policy_delete(
825 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
828 __xfrm_policy_unlink(pol, dir);
834 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
837 xfrm_policy_kill(ret);
840 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
843 xfrm_policy_byid(struct net *net, const struct xfrm_mark *mark, u32 if_id,
844 u8 type, int dir, u32 id, int delete, int *err)
846 struct xfrm_policy *pol, *ret;
847 struct hlist_head *chain;
850 if (xfrm_policy_id2dir(id) != dir)
854 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
855 chain = net->xfrm.policy_byidx + idx_hash(net, id);
857 hlist_for_each_entry(pol, chain, byidx) {
858 if (pol->type == type && pol->index == id &&
859 pol->if_id == if_id && xfrm_policy_mark_match(mark, pol)) {
862 *err = security_xfrm_policy_delete(
865 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
868 __xfrm_policy_unlink(pol, dir);
874 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
877 xfrm_policy_kill(ret);
880 EXPORT_SYMBOL(xfrm_policy_byid);
882 #ifdef CONFIG_SECURITY_NETWORK_XFRM
884 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
888 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
889 struct xfrm_policy *pol;
892 hlist_for_each_entry(pol,
893 &net->xfrm.policy_inexact[dir], bydst) {
894 if (pol->type != type)
896 err = security_xfrm_policy_delete(pol->security);
898 xfrm_audit_policy_delete(pol, 0, task_valid);
902 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
903 hlist_for_each_entry(pol,
904 net->xfrm.policy_bydst[dir].table + i,
906 if (pol->type != type)
908 err = security_xfrm_policy_delete(
911 xfrm_audit_policy_delete(pol, 0,
922 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
928 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
930 int dir, err = 0, cnt = 0;
932 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
934 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
938 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
939 struct xfrm_policy *pol;
943 hlist_for_each_entry(pol,
944 &net->xfrm.policy_inexact[dir], bydst) {
945 if (pol->type != type)
947 __xfrm_policy_unlink(pol, dir);
948 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
951 xfrm_audit_policy_delete(pol, 1, task_valid);
953 xfrm_policy_kill(pol);
955 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
959 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
961 hlist_for_each_entry(pol,
962 net->xfrm.policy_bydst[dir].table + i,
964 if (pol->type != type)
966 __xfrm_policy_unlink(pol, dir);
967 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
970 xfrm_audit_policy_delete(pol, 1, task_valid);
971 xfrm_policy_kill(pol);
973 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
982 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
985 EXPORT_SYMBOL(xfrm_policy_flush);
987 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
988 int (*func)(struct xfrm_policy *, int, int, void*),
991 struct xfrm_policy *pol;
992 struct xfrm_policy_walk_entry *x;
995 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
996 walk->type != XFRM_POLICY_TYPE_ANY)
999 if (list_empty(&walk->walk.all) && walk->seq != 0)
1002 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1003 if (list_empty(&walk->walk.all))
1004 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
1006 x = list_first_entry(&walk->walk.all,
1007 struct xfrm_policy_walk_entry, all);
1009 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
1012 pol = container_of(x, struct xfrm_policy, walk);
1013 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1014 walk->type != pol->type)
1016 error = func(pol, xfrm_policy_id2dir(pol->index),
1019 list_move_tail(&walk->walk.all, &x->all);
1024 if (walk->seq == 0) {
1028 list_del_init(&walk->walk.all);
1030 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1033 EXPORT_SYMBOL(xfrm_policy_walk);
1035 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1037 INIT_LIST_HEAD(&walk->walk.all);
1038 walk->walk.dead = 1;
1042 EXPORT_SYMBOL(xfrm_policy_walk_init);
1044 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
1046 if (list_empty(&walk->walk.all))
1049 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
1050 list_del(&walk->walk.all);
1051 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1053 EXPORT_SYMBOL(xfrm_policy_walk_done);
1056 * Find policy to apply to this flow.
1058 * Returns 0 if policy found, else an -errno.
1060 static int xfrm_policy_match(const struct xfrm_policy *pol,
1061 const struct flowi *fl,
1062 u8 type, u16 family, int dir, u32 if_id)
1064 const struct xfrm_selector *sel = &pol->selector;
1068 if (pol->family != family ||
1069 pol->if_id != if_id ||
1070 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
1074 match = xfrm_selector_match(sel, fl, family);
1076 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1082 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
1083 const struct flowi *fl,
1088 struct xfrm_policy *pol, *ret;
1089 const xfrm_address_t *daddr, *saddr;
1090 struct hlist_head *chain;
1091 unsigned int sequence;
1094 daddr = xfrm_flowi_daddr(fl, family);
1095 saddr = xfrm_flowi_saddr(fl, family);
1096 if (unlikely(!daddr || !saddr))
1102 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1103 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1104 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1108 hlist_for_each_entry_rcu(pol, chain, bydst) {
1109 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
1119 priority = ret->priority;
1123 chain = &net->xfrm.policy_inexact[dir];
1124 hlist_for_each_entry_rcu(pol, chain, bydst) {
1125 if ((pol->priority >= priority) && ret)
1128 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
1142 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1145 if (ret && !xfrm_pol_hold_rcu(ret))
1153 static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
1154 const struct flowi *fl,
1155 u16 family, u8 dir, u32 if_id)
1157 #ifdef CONFIG_XFRM_SUB_POLICY
1158 struct xfrm_policy *pol;
1160 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
1165 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
1169 static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
1170 const struct flowi *fl,
1171 u16 family, u32 if_id)
1173 struct xfrm_policy *pol;
1177 pol = rcu_dereference(sk->sk_policy[dir]);
1182 if (pol->family != family) {
1187 match = xfrm_selector_match(&pol->selector, fl, family);
1189 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
1190 pol->if_id != if_id) {
1194 err = security_xfrm_policy_lookup(pol->security,
1198 if (!xfrm_pol_hold_rcu(pol))
1200 } else if (err == -ESRCH) {
1213 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1215 struct net *net = xp_net(pol);
1217 list_add(&pol->walk.all, &net->xfrm.policy_all);
1218 net->xfrm.policy_count[dir]++;
1222 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1225 struct net *net = xp_net(pol);
1227 if (list_empty(&pol->walk.all))
1230 /* Socket policies are not hashed. */
1231 if (!hlist_unhashed(&pol->bydst)) {
1232 hlist_del_rcu(&pol->bydst);
1233 hlist_del(&pol->byidx);
1236 list_del_init(&pol->walk.all);
1237 net->xfrm.policy_count[dir]--;
1242 static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1244 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1247 static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1249 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1252 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1254 struct net *net = xp_net(pol);
1256 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1257 pol = __xfrm_policy_unlink(pol, dir);
1258 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1260 xfrm_policy_kill(pol);
1265 EXPORT_SYMBOL(xfrm_policy_delete);
1267 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1269 struct net *net = sock_net(sk);
1270 struct xfrm_policy *old_pol;
1272 #ifdef CONFIG_XFRM_SUB_POLICY
1273 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1277 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1278 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1279 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
1281 pol->curlft.add_time = ktime_get_real_seconds();
1282 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
1283 xfrm_sk_policy_link(pol, dir);
1285 rcu_assign_pointer(sk->sk_policy[dir], pol);
1288 xfrm_policy_requeue(old_pol, pol);
1290 /* Unlinking succeeds always. This is the only function
1291 * allowed to delete or replace socket policy.
1293 xfrm_sk_policy_unlink(old_pol, dir);
1295 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1298 xfrm_policy_kill(old_pol);
1303 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1305 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1306 struct net *net = xp_net(old);
1309 newp->selector = old->selector;
1310 if (security_xfrm_policy_clone(old->security,
1313 return NULL; /* ENOMEM */
1315 newp->lft = old->lft;
1316 newp->curlft = old->curlft;
1317 newp->mark = old->mark;
1318 newp->if_id = old->if_id;
1319 newp->action = old->action;
1320 newp->flags = old->flags;
1321 newp->xfrm_nr = old->xfrm_nr;
1322 newp->index = old->index;
1323 newp->type = old->type;
1324 newp->family = old->family;
1325 memcpy(newp->xfrm_vec, old->xfrm_vec,
1326 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1327 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1328 xfrm_sk_policy_link(newp, dir);
1329 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1335 int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
1337 const struct xfrm_policy *p;
1338 struct xfrm_policy *np;
1342 for (i = 0; i < 2; i++) {
1343 p = rcu_dereference(osk->sk_policy[i]);
1345 np = clone_policy(p, i);
1346 if (unlikely(!np)) {
1350 rcu_assign_pointer(sk->sk_policy[i], np);
1358 xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1359 xfrm_address_t *remote, unsigned short family, u32 mark)
1362 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1364 if (unlikely(afinfo == NULL))
1366 err = afinfo->get_saddr(net, oif, local, remote, mark);
1371 /* Resolve list of templates for the flow, given policy. */
1374 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1375 struct xfrm_state **xfrm, unsigned short family)
1377 struct net *net = xp_net(policy);
1380 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1381 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1384 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1385 struct xfrm_state *x;
1386 xfrm_address_t *remote = daddr;
1387 xfrm_address_t *local = saddr;
1388 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1390 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1391 tmpl->mode == XFRM_MODE_BEET) {
1392 remote = &tmpl->id.daddr;
1393 local = &tmpl->saddr;
1394 if (xfrm_addr_any(local, tmpl->encap_family)) {
1395 error = xfrm_get_saddr(net, fl->flowi_oif,
1397 tmpl->encap_family, 0);
1404 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
1405 family, policy->if_id);
1407 if (x && x->km.state == XFRM_STATE_VALID) {
1414 error = (x->km.state == XFRM_STATE_ERROR ?
1417 } else if (error == -ESRCH) {
1421 if (!tmpl->optional)
1427 for (nx--; nx >= 0; nx--)
1428 xfrm_state_put(xfrm[nx]);
1433 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1434 struct xfrm_state **xfrm, unsigned short family)
1436 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1437 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1443 for (i = 0; i < npols; i++) {
1444 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1449 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1457 /* found states are sorted for outbound processing */
1459 xfrm_state_sort(xfrm, tpp, cnx, family);
1464 for (cnx--; cnx >= 0; cnx--)
1465 xfrm_state_put(tpp[cnx]);
1470 static int xfrm_get_tos(const struct flowi *fl, int family)
1472 const struct xfrm_policy_afinfo *afinfo;
1475 afinfo = xfrm_policy_get_afinfo(family);
1479 tos = afinfo->get_tos(fl);
1486 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1488 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1489 struct dst_ops *dst_ops;
1490 struct xfrm_dst *xdst;
1493 return ERR_PTR(-EINVAL);
1497 dst_ops = &net->xfrm.xfrm4_dst_ops;
1499 #if IS_ENABLED(CONFIG_IPV6)
1501 dst_ops = &net->xfrm.xfrm6_dst_ops;
1507 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
1510 struct dst_entry *dst = &xdst->u.dst;
1512 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1514 xdst = ERR_PTR(-ENOBUFS);
1521 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1524 const struct xfrm_policy_afinfo *afinfo =
1525 xfrm_policy_get_afinfo(dst->ops->family);
1531 err = afinfo->init_path(path, dst, nfheader_len);
1538 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1539 const struct flowi *fl)
1541 const struct xfrm_policy_afinfo *afinfo =
1542 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1548 err = afinfo->fill_dst(xdst, dev, fl);
1556 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1557 * all the metrics... Shortly, bundle a bundle.
1560 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1561 struct xfrm_state **xfrm,
1562 struct xfrm_dst **bundle,
1564 const struct flowi *fl,
1565 struct dst_entry *dst)
1567 struct net *net = xp_net(policy);
1568 unsigned long now = jiffies;
1569 struct net_device *dev;
1570 struct xfrm_mode *inner_mode;
1571 struct xfrm_dst *xdst_prev = NULL;
1572 struct xfrm_dst *xdst0 = NULL;
1576 int nfheader_len = 0;
1577 int trailer_len = 0;
1579 int family = policy->selector.family;
1580 xfrm_address_t saddr, daddr;
1582 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1584 tos = xfrm_get_tos(fl, family);
1588 for (; i < nx; i++) {
1589 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1590 struct dst_entry *dst1 = &xdst->u.dst;
1592 err = PTR_ERR(xdst);
1602 /* Ref count is taken during xfrm_alloc_dst()
1603 * No need to do dst_clone() on dst1
1605 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
1607 if (xfrm[i]->sel.family == AF_UNSPEC) {
1608 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1609 xfrm_af2proto(family));
1611 err = -EAFNOSUPPORT;
1616 inner_mode = xfrm[i]->inner_mode;
1619 dst_copy_metrics(dst1, dst);
1621 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1624 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
1625 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
1627 family = xfrm[i]->props.family;
1628 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1629 &saddr, &daddr, family, mark);
1636 dst1->xfrm = xfrm[i];
1637 xdst->xfrm_genid = xfrm[i]->genid;
1639 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1640 dst1->flags |= DST_HOST;
1641 dst1->lastuse = now;
1643 dst1->input = dst_discard;
1644 dst1->output = inner_mode->afinfo->output;
1648 header_len += xfrm[i]->props.header_len;
1649 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1650 nfheader_len += xfrm[i]->props.header_len;
1651 trailer_len += xfrm[i]->props.trailer_len;
1654 xfrm_dst_set_child(xdst_prev, dst);
1662 xfrm_init_path(xdst0, dst, nfheader_len);
1663 xfrm_init_pmtu(bundle, nx);
1665 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
1666 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
1667 err = xfrm_fill_dst(xdst_prev, dev, fl);
1671 xdst_prev->u.dst.header_len = header_len;
1672 xdst_prev->u.dst.trailer_len = trailer_len;
1673 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
1674 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
1677 return &xdst0->u.dst;
1681 xfrm_state_put(xfrm[i]);
1684 dst_release_immediate(&xdst0->u.dst);
1686 return ERR_PTR(err);
1689 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1690 struct xfrm_policy **pols,
1691 int *num_pols, int *num_xfrms)
1695 if (*num_pols == 0 || !pols[0]) {
1700 if (IS_ERR(pols[0]))
1701 return PTR_ERR(pols[0]);
1703 *num_xfrms = pols[0]->xfrm_nr;
1705 #ifdef CONFIG_XFRM_SUB_POLICY
1706 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1707 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1708 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1709 XFRM_POLICY_TYPE_MAIN,
1714 if (IS_ERR(pols[1])) {
1715 xfrm_pols_put(pols, *num_pols);
1716 return PTR_ERR(pols[1]);
1719 (*num_xfrms) += pols[1]->xfrm_nr;
1723 for (i = 0; i < *num_pols; i++) {
1724 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1734 static struct xfrm_dst *
1735 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1736 const struct flowi *fl, u16 family,
1737 struct dst_entry *dst_orig)
1739 struct net *net = xp_net(pols[0]);
1740 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1741 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
1742 struct xfrm_dst *xdst;
1743 struct dst_entry *dst;
1746 /* Try to instantiate a bundle */
1747 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1753 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1754 return ERR_PTR(err);
1757 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
1759 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1760 return ERR_CAST(dst);
1763 xdst = (struct xfrm_dst *)dst;
1764 xdst->num_xfrms = err;
1765 xdst->num_pols = num_pols;
1766 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
1767 xdst->policy_genid = atomic_read(&pols[0]->genid);
1772 static void xfrm_policy_queue_process(struct timer_list *t)
1774 struct sk_buff *skb;
1776 struct dst_entry *dst;
1777 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
1778 struct net *net = xp_net(pol);
1779 struct xfrm_policy_queue *pq = &pol->polq;
1781 struct sk_buff_head list;
1783 spin_lock(&pq->hold_queue.lock);
1784 skb = skb_peek(&pq->hold_queue);
1786 spin_unlock(&pq->hold_queue.lock);
1791 xfrm_decode_session(skb, &fl, dst->ops->family);
1792 spin_unlock(&pq->hold_queue.lock);
1794 dst_hold(xfrm_dst_path(dst));
1795 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
1799 if (dst->flags & DST_XFRM_QUEUE) {
1802 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1805 pq->timeout = pq->timeout << 1;
1806 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1813 __skb_queue_head_init(&list);
1815 spin_lock(&pq->hold_queue.lock);
1817 skb_queue_splice_init(&pq->hold_queue, &list);
1818 spin_unlock(&pq->hold_queue.lock);
1820 while (!skb_queue_empty(&list)) {
1821 skb = __skb_dequeue(&list);
1823 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1824 dst_hold(xfrm_dst_path(skb_dst(skb)));
1825 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
1833 skb_dst_set(skb, dst);
1835 dst_output(net, skb->sk, skb);
1844 skb_queue_purge(&pq->hold_queue);
1848 static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1850 unsigned long sched_next;
1851 struct dst_entry *dst = skb_dst(skb);
1852 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1853 struct xfrm_policy *pol = xdst->pols[0];
1854 struct xfrm_policy_queue *pq = &pol->polq;
1856 if (unlikely(skb_fclone_busy(sk, skb))) {
1861 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1868 spin_lock_bh(&pq->hold_queue.lock);
1871 pq->timeout = XFRM_QUEUE_TMO_MIN;
1873 sched_next = jiffies + pq->timeout;
1875 if (del_timer(&pq->hold_timer)) {
1876 if (time_before(pq->hold_timer.expires, sched_next))
1877 sched_next = pq->hold_timer.expires;
1881 __skb_queue_tail(&pq->hold_queue, skb);
1882 if (!mod_timer(&pq->hold_timer, sched_next))
1885 spin_unlock_bh(&pq->hold_queue.lock);
1890 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1891 struct xfrm_flo *xflo,
1892 const struct flowi *fl,
1897 struct net_device *dev;
1898 struct dst_entry *dst;
1899 struct dst_entry *dst1;
1900 struct xfrm_dst *xdst;
1902 xdst = xfrm_alloc_dst(net, family);
1906 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
1907 net->xfrm.sysctl_larval_drop ||
1911 dst = xflo->dst_orig;
1912 dst1 = &xdst->u.dst;
1916 dst_copy_metrics(dst1, dst);
1918 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1919 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1920 dst1->lastuse = jiffies;
1922 dst1->input = dst_discard;
1923 dst1->output = xdst_queue_output;
1926 xfrm_dst_set_child(xdst, dst);
1929 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1936 err = xfrm_fill_dst(xdst, dev, fl);
1945 xdst = ERR_PTR(err);
1949 static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
1950 const struct flowi *fl,
1952 struct xfrm_flo *xflo, u32 if_id)
1954 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1955 int num_pols = 0, num_xfrms = 0, err;
1956 struct xfrm_dst *xdst;
1958 /* Resolve policies to use if we couldn't get them from
1959 * previous cache entry */
1961 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
1962 err = xfrm_expand_policies(fl, family, pols,
1963 &num_pols, &num_xfrms);
1969 goto make_dummy_bundle;
1971 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
1974 err = PTR_ERR(xdst);
1975 if (err == -EREMOTE) {
1976 xfrm_pols_put(pols, num_pols);
1982 goto make_dummy_bundle;
1983 } else if (xdst == NULL) {
1985 goto make_dummy_bundle;
1991 /* We found policies, but there's no bundles to instantiate:
1992 * either because the policy blocks, has no transformations or
1993 * we could not build template (no xfrm_states).*/
1994 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
1996 xfrm_pols_put(pols, num_pols);
1997 return ERR_CAST(xdst);
1999 xdst->num_pols = num_pols;
2000 xdst->num_xfrms = num_xfrms;
2001 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2006 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2008 xfrm_pols_put(pols, num_pols);
2009 return ERR_PTR(err);
2012 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2013 struct dst_entry *dst_orig)
2015 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2016 struct dst_entry *ret;
2019 dst_release(dst_orig);
2020 return ERR_PTR(-EINVAL);
2022 ret = afinfo->blackhole_route(net, dst_orig);
2029 /* Finds/creates a bundle for given flow and if_id
2031 * At the moment we eat a raw IP route. Mostly to speed up lookups
2032 * on interfaces with disabled IPsec.
2034 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2037 struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
2038 struct dst_entry *dst_orig,
2039 const struct flowi *fl,
2040 const struct sock *sk,
2041 int flags, u32 if_id)
2043 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2044 struct xfrm_dst *xdst;
2045 struct dst_entry *dst, *route;
2046 u16 family = dst_orig->ops->family;
2047 u8 dir = XFRM_POLICY_OUT;
2048 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2054 sk = sk_const_to_full_sk(sk);
2055 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2057 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
2059 err = xfrm_expand_policies(fl, family, pols,
2060 &num_pols, &num_xfrms);
2065 if (num_xfrms <= 0) {
2066 drop_pols = num_pols;
2070 xdst = xfrm_resolve_and_create_bundle(
2075 xfrm_pols_put(pols, num_pols);
2076 err = PTR_ERR(xdst);
2077 if (err == -EREMOTE)
2081 } else if (xdst == NULL) {
2083 drop_pols = num_pols;
2087 route = xdst->route;
2092 struct xfrm_flo xflo;
2094 xflo.dst_orig = dst_orig;
2097 /* To accelerate a bit... */
2098 if (!if_id && ((dst_orig->flags & DST_NOXFRM) ||
2099 !net->xfrm.policy_count[XFRM_POLICY_OUT]))
2102 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
2106 err = PTR_ERR(xdst);
2110 num_pols = xdst->num_pols;
2111 num_xfrms = xdst->num_xfrms;
2112 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
2113 route = xdst->route;
2117 if (route == NULL && num_xfrms > 0) {
2118 /* The only case when xfrm_bundle_lookup() returns a
2119 * bundle with null route, is when the template could
2120 * not be resolved. It means policies are there, but
2121 * bundle could not be created, since we don't yet
2122 * have the xfrm_state's. We need to wait for KM to
2123 * negotiate new SA's or bail out with error.*/
2124 if (net->xfrm.sysctl_larval_drop) {
2125 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2132 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2140 if ((flags & XFRM_LOOKUP_ICMP) &&
2141 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2146 for (i = 0; i < num_pols; i++)
2147 pols[i]->curlft.use_time = ktime_get_real_seconds();
2149 if (num_xfrms < 0) {
2150 /* Prohibit the flow */
2151 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2154 } else if (num_xfrms > 0) {
2155 /* Flow transformed */
2156 dst_release(dst_orig);
2158 /* Flow passes untransformed */
2163 xfrm_pols_put(pols, drop_pols);
2164 if (dst && dst->xfrm &&
2165 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2166 dst->flags |= DST_XFRM_TUNNEL;
2170 if (!(flags & XFRM_LOOKUP_ICMP)) {
2178 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2179 dst_release(dst_orig);
2180 xfrm_pols_put(pols, drop_pols);
2181 return ERR_PTR(err);
2183 EXPORT_SYMBOL(xfrm_lookup_with_ifid);
2185 /* Main function: finds/creates a bundle for given flow.
2187 * At the moment we eat a raw IP route. Mostly to speed up lookups
2188 * on interfaces with disabled IPsec.
2190 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2191 const struct flowi *fl, const struct sock *sk,
2194 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
2196 EXPORT_SYMBOL(xfrm_lookup);
2198 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2199 * Otherwise we may send out blackholed packets.
2201 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2202 const struct flowi *fl,
2203 const struct sock *sk, int flags)
2205 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2206 flags | XFRM_LOOKUP_QUEUE |
2207 XFRM_LOOKUP_KEEP_DST_REF);
2209 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2210 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2213 dst_release(dst_orig);
2217 EXPORT_SYMBOL(xfrm_lookup_route);
2220 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2222 struct xfrm_state *x;
2224 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2226 x = skb->sp->xvec[idx];
2227 if (!x->type->reject)
2229 return x->type->reject(x, skb, fl);
2232 /* When skb is transformed back to its "native" form, we have to
2233 * check policy restrictions. At the moment we make this in maximally
2234 * stupid way. Shame on me. :-) Of course, connected sockets must
2235 * have policy cached at them.
2239 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2240 unsigned short family)
2242 if (xfrm_state_kern(x))
2243 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2244 return x->id.proto == tmpl->id.proto &&
2245 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2246 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2247 x->props.mode == tmpl->mode &&
2248 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2249 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2250 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2251 xfrm_state_addr_cmp(tmpl, x, family));
2255 * 0 or more than 0 is returned when validation is succeeded (either bypass
2256 * because of optional transport mode, or next index of the mathced secpath
2257 * state with the template.
2258 * -1 is returned when no matching template is found.
2259 * Otherwise "-2 - errored_index" is returned.
2262 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2263 unsigned short family)
2267 if (tmpl->optional) {
2268 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2272 for (; idx < sp->len; idx++) {
2273 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2275 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2284 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2285 unsigned int family, int reverse)
2287 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2290 if (unlikely(afinfo == NULL))
2291 return -EAFNOSUPPORT;
2293 afinfo->decode_session(skb, fl, reverse);
2295 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2299 EXPORT_SYMBOL(__xfrm_decode_session);
2301 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2303 for (; k < sp->len; k++) {
2304 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2313 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2314 unsigned short family)
2316 struct net *net = dev_net(skb->dev);
2317 struct xfrm_policy *pol;
2318 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2325 const struct xfrm_if_cb *ifcb;
2330 ifcb = xfrm_if_get_cb();
2333 xi = ifcb->decode_session(skb, family);
2335 if_id = xi->p.if_id;
2341 reverse = dir & ~XFRM_POLICY_MASK;
2342 dir &= XFRM_POLICY_MASK;
2344 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2345 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2349 nf_nat_decode_session(skb, &fl, family);
2351 /* First, check used SA against their selectors. */
2355 for (i = skb->sp->len-1; i >= 0; i--) {
2356 struct xfrm_state *x = skb->sp->xvec[i];
2357 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2358 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2365 sk = sk_to_full_sk(sk);
2366 if (sk && sk->sk_policy[dir]) {
2367 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
2369 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2375 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
2378 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2383 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2384 xfrm_secpath_reject(xerr_idx, skb, &fl);
2385 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2391 pol->curlft.use_time = ktime_get_real_seconds();
2395 #ifdef CONFIG_XFRM_SUB_POLICY
2396 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2397 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2399 XFRM_POLICY_IN, if_id);
2401 if (IS_ERR(pols[1])) {
2402 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2405 pols[1]->curlft.use_time = ktime_get_real_seconds();
2411 if (pol->action == XFRM_POLICY_ALLOW) {
2412 struct sec_path *sp;
2413 static struct sec_path dummy;
2414 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2415 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2416 struct xfrm_tmpl **tpp = tp;
2420 if ((sp = skb->sp) == NULL)
2423 for (pi = 0; pi < npols; pi++) {
2424 if (pols[pi] != pol &&
2425 pols[pi]->action != XFRM_POLICY_ALLOW) {
2426 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2429 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2430 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2433 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2434 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2438 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
2442 /* For each tunnel xfrm, find the first matching tmpl.
2443 * For each tmpl before that, find corresponding xfrm.
2444 * Order is _important_. Later we will implement
2445 * some barriers, but at the moment barriers
2446 * are implied between each two transformations.
2448 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2449 k = xfrm_policy_ok(tpp[i], sp, k, family);
2452 /* "-2 - errored_index" returned */
2454 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2459 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2460 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2464 xfrm_pols_put(pols, npols);
2467 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2470 xfrm_secpath_reject(xerr_idx, skb, &fl);
2472 xfrm_pols_put(pols, npols);
2475 EXPORT_SYMBOL(__xfrm_policy_check);
2477 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2479 struct net *net = dev_net(skb->dev);
2481 struct dst_entry *dst;
2484 if (xfrm_decode_session(skb, &fl, family) < 0) {
2485 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2490 if (!skb_dst(skb)) {
2491 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2495 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
2500 skb_dst_set(skb, dst);
2503 EXPORT_SYMBOL(__xfrm_route_forward);
2505 /* Optimize later using cookies and generation ids. */
2507 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2509 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2510 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2511 * get validated by dst_ops->check on every use. We do this
2512 * because when a normal route referenced by an XFRM dst is
2513 * obsoleted we do not go looking around for all parent
2514 * referencing XFRM dsts so that we can invalidate them. It
2515 * is just too much work. Instead we make the checks here on
2516 * every use. For example:
2518 * XFRM dst A --> IPv4 dst X
2520 * X is the "xdst->route" of A (X is also the "dst->path" of A
2521 * in this example). If X is marked obsolete, "A" will not
2522 * notice. That's what we are validating here via the
2523 * stale_bundle() check.
2525 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2527 * This will force stale_bundle() to fail on any xdst bundle with
2528 * this dst linked in it.
2530 if (dst->obsolete < 0 && !stale_bundle(dst))
2536 static int stale_bundle(struct dst_entry *dst)
2538 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2541 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2543 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
2544 dst->dev = dev_net(dev)->loopback_dev;
2549 EXPORT_SYMBOL(xfrm_dst_ifdown);
2551 static void xfrm_link_failure(struct sk_buff *skb)
2553 /* Impossible. Such dst must be popped before reaches point of failure. */
2556 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2559 if (dst->obsolete) {
2567 static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
2570 struct xfrm_dst *xdst = bundle[nr];
2571 u32 pmtu, route_mtu_cached;
2572 struct dst_entry *dst;
2575 pmtu = dst_mtu(xfrm_dst_child(dst));
2576 xdst->child_mtu_cached = pmtu;
2578 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2580 route_mtu_cached = dst_mtu(xdst->route);
2581 xdst->route_mtu_cached = route_mtu_cached;
2583 if (pmtu > route_mtu_cached)
2584 pmtu = route_mtu_cached;
2586 dst_metric_set(dst, RTAX_MTU, pmtu);
2590 /* Check that the bundle accepts the flow and its components are
2594 static int xfrm_bundle_ok(struct xfrm_dst *first)
2596 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
2597 struct dst_entry *dst = &first->u.dst;
2598 struct xfrm_dst *xdst;
2602 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
2603 (dst->dev && !netif_running(dst->dev)))
2606 if (dst->flags & DST_XFRM_QUEUE)
2609 start_from = nr = 0;
2611 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2613 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2615 if (xdst->xfrm_genid != dst->xfrm->genid)
2617 if (xdst->num_pols > 0 &&
2618 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2621 bundle[nr++] = xdst;
2623 mtu = dst_mtu(xfrm_dst_child(dst));
2624 if (xdst->child_mtu_cached != mtu) {
2626 xdst->child_mtu_cached = mtu;
2629 if (!dst_check(xdst->route, xdst->route_cookie))
2631 mtu = dst_mtu(xdst->route);
2632 if (xdst->route_mtu_cached != mtu) {
2634 xdst->route_mtu_cached = mtu;
2637 dst = xfrm_dst_child(dst);
2638 } while (dst->xfrm);
2640 if (likely(!start_from))
2643 xdst = bundle[start_from - 1];
2644 mtu = xdst->child_mtu_cached;
2645 while (start_from--) {
2648 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2649 if (mtu > xdst->route_mtu_cached)
2650 mtu = xdst->route_mtu_cached;
2651 dst_metric_set(dst, RTAX_MTU, mtu);
2655 xdst = bundle[start_from - 1];
2656 xdst->child_mtu_cached = mtu;
2662 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2664 return dst_metric_advmss(xfrm_dst_path(dst));
2667 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2669 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2671 return mtu ? : dst_mtu(xfrm_dst_path(dst));
2674 static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
2678 const struct xfrm_state *xfrm = dst->xfrm;
2680 dst = xfrm_dst_child(dst);
2682 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
2684 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
2685 daddr = xfrm->coaddr;
2686 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
2687 daddr = &xfrm->id.daddr;
2692 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2693 struct sk_buff *skb,
2696 const struct dst_entry *path = xfrm_dst_path(dst);
2699 daddr = xfrm_get_dst_nexthop(dst, daddr);
2700 return path->ops->neigh_lookup(path, skb, daddr);
2703 static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
2705 const struct dst_entry *path = xfrm_dst_path(dst);
2707 daddr = xfrm_get_dst_nexthop(dst, daddr);
2708 path->ops->confirm_neigh(path, daddr);
2711 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
2715 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
2716 return -EAFNOSUPPORT;
2718 spin_lock(&xfrm_policy_afinfo_lock);
2719 if (unlikely(xfrm_policy_afinfo[family] != NULL))
2722 struct dst_ops *dst_ops = afinfo->dst_ops;
2723 if (likely(dst_ops->kmem_cachep == NULL))
2724 dst_ops->kmem_cachep = xfrm_dst_cache;
2725 if (likely(dst_ops->check == NULL))
2726 dst_ops->check = xfrm_dst_check;
2727 if (likely(dst_ops->default_advmss == NULL))
2728 dst_ops->default_advmss = xfrm_default_advmss;
2729 if (likely(dst_ops->mtu == NULL))
2730 dst_ops->mtu = xfrm_mtu;
2731 if (likely(dst_ops->negative_advice == NULL))
2732 dst_ops->negative_advice = xfrm_negative_advice;
2733 if (likely(dst_ops->link_failure == NULL))
2734 dst_ops->link_failure = xfrm_link_failure;
2735 if (likely(dst_ops->neigh_lookup == NULL))
2736 dst_ops->neigh_lookup = xfrm_neigh_lookup;
2737 if (likely(!dst_ops->confirm_neigh))
2738 dst_ops->confirm_neigh = xfrm_confirm_neigh;
2739 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
2741 spin_unlock(&xfrm_policy_afinfo_lock);
2745 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2747 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
2749 struct dst_ops *dst_ops = afinfo->dst_ops;
2752 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
2753 if (xfrm_policy_afinfo[i] != afinfo)
2755 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
2761 dst_ops->kmem_cachep = NULL;
2762 dst_ops->check = NULL;
2763 dst_ops->negative_advice = NULL;
2764 dst_ops->link_failure = NULL;
2766 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2768 void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
2770 spin_lock(&xfrm_if_cb_lock);
2771 rcu_assign_pointer(xfrm_if_cb, ifcb);
2772 spin_unlock(&xfrm_if_cb_lock);
2774 EXPORT_SYMBOL(xfrm_if_register_cb);
2776 void xfrm_if_unregister_cb(void)
2778 RCU_INIT_POINTER(xfrm_if_cb, NULL);
2781 EXPORT_SYMBOL(xfrm_if_unregister_cb);
2783 #ifdef CONFIG_XFRM_STATISTICS
2784 static int __net_init xfrm_statistics_init(struct net *net)
2787 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2788 if (!net->mib.xfrm_statistics)
2790 rv = xfrm_proc_init(net);
2792 free_percpu(net->mib.xfrm_statistics);
2796 static void xfrm_statistics_fini(struct net *net)
2798 xfrm_proc_fini(net);
2799 free_percpu(net->mib.xfrm_statistics);
2802 static int __net_init xfrm_statistics_init(struct net *net)
2807 static void xfrm_statistics_fini(struct net *net)
2812 static int __net_init xfrm_policy_init(struct net *net)
2814 unsigned int hmask, sz;
2817 if (net_eq(net, &init_net))
2818 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2819 sizeof(struct xfrm_dst),
2820 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2824 sz = (hmask+1) * sizeof(struct hlist_head);
2826 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2827 if (!net->xfrm.policy_byidx)
2829 net->xfrm.policy_idx_hmask = hmask;
2831 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2832 struct xfrm_policy_hash *htab;
2834 net->xfrm.policy_count[dir] = 0;
2835 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
2836 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2838 htab = &net->xfrm.policy_bydst[dir];
2839 htab->table = xfrm_hash_alloc(sz);
2842 htab->hmask = hmask;
2848 net->xfrm.policy_hthresh.lbits4 = 32;
2849 net->xfrm.policy_hthresh.rbits4 = 32;
2850 net->xfrm.policy_hthresh.lbits6 = 128;
2851 net->xfrm.policy_hthresh.rbits6 = 128;
2853 seqlock_init(&net->xfrm.policy_hthresh.lock);
2855 INIT_LIST_HEAD(&net->xfrm.policy_all);
2856 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
2857 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
2861 for (dir--; dir >= 0; dir--) {
2862 struct xfrm_policy_hash *htab;
2864 htab = &net->xfrm.policy_bydst[dir];
2865 xfrm_hash_free(htab->table, sz);
2867 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2872 static void xfrm_policy_fini(struct net *net)
2877 flush_work(&net->xfrm.policy_hash_work);
2878 #ifdef CONFIG_XFRM_SUB_POLICY
2879 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
2881 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
2883 WARN_ON(!list_empty(&net->xfrm.policy_all));
2885 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2886 struct xfrm_policy_hash *htab;
2888 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
2890 htab = &net->xfrm.policy_bydst[dir];
2891 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
2892 WARN_ON(!hlist_empty(htab->table));
2893 xfrm_hash_free(htab->table, sz);
2896 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
2897 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2898 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2901 static int __net_init xfrm_net_init(struct net *net)
2905 /* Initialize the per-net locks here */
2906 spin_lock_init(&net->xfrm.xfrm_state_lock);
2907 spin_lock_init(&net->xfrm.xfrm_policy_lock);
2908 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2910 rv = xfrm_statistics_init(net);
2912 goto out_statistics;
2913 rv = xfrm_state_init(net);
2916 rv = xfrm_policy_init(net);
2919 rv = xfrm_sysctl_init(net);
2926 xfrm_policy_fini(net);
2928 xfrm_state_fini(net);
2930 xfrm_statistics_fini(net);
2935 static void __net_exit xfrm_net_exit(struct net *net)
2937 xfrm_sysctl_fini(net);
2938 xfrm_policy_fini(net);
2939 xfrm_state_fini(net);
2940 xfrm_statistics_fini(net);
2943 static struct pernet_operations __net_initdata xfrm_net_ops = {
2944 .init = xfrm_net_init,
2945 .exit = xfrm_net_exit,
2948 void __init xfrm_init(void)
2950 register_pernet_subsys(&xfrm_net_ops);
2952 seqcount_init(&xfrm_policy_hash_generation);
2955 RCU_INIT_POINTER(xfrm_if_cb, NULL);
2959 #ifdef CONFIG_AUDITSYSCALL
2960 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2961 struct audit_buffer *audit_buf)
2963 struct xfrm_sec_ctx *ctx = xp->security;
2964 struct xfrm_selector *sel = &xp->selector;
2967 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2968 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2970 switch (sel->family) {
2972 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
2973 if (sel->prefixlen_s != 32)
2974 audit_log_format(audit_buf, " src_prefixlen=%d",
2976 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
2977 if (sel->prefixlen_d != 32)
2978 audit_log_format(audit_buf, " dst_prefixlen=%d",
2982 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
2983 if (sel->prefixlen_s != 128)
2984 audit_log_format(audit_buf, " src_prefixlen=%d",
2986 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
2987 if (sel->prefixlen_d != 128)
2988 audit_log_format(audit_buf, " dst_prefixlen=%d",
2994 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
2996 struct audit_buffer *audit_buf;
2998 audit_buf = xfrm_audit_start("SPD-add");
2999 if (audit_buf == NULL)
3001 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3002 audit_log_format(audit_buf, " res=%u", result);
3003 xfrm_audit_common_policyinfo(xp, audit_buf);
3004 audit_log_end(audit_buf);
3006 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3008 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
3011 struct audit_buffer *audit_buf;
3013 audit_buf = xfrm_audit_start("SPD-delete");
3014 if (audit_buf == NULL)
3016 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3017 audit_log_format(audit_buf, " res=%u", result);
3018 xfrm_audit_common_policyinfo(xp, audit_buf);
3019 audit_log_end(audit_buf);
3021 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3024 #ifdef CONFIG_XFRM_MIGRATE
3025 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3026 const struct xfrm_selector *sel_tgt)
3028 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3029 if (sel_tgt->family == sel_cmp->family &&
3030 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3032 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3034 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3035 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
3039 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
3046 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3047 u8 dir, u8 type, struct net *net, u32 if_id)
3049 struct xfrm_policy *pol, *ret = NULL;
3050 struct hlist_head *chain;
3053 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
3054 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
3055 hlist_for_each_entry(pol, chain, bydst) {
3056 if ((if_id == 0 || pol->if_id == if_id) &&
3057 xfrm_migrate_selector_match(sel, &pol->selector) &&
3058 pol->type == type) {
3060 priority = ret->priority;
3064 chain = &net->xfrm.policy_inexact[dir];
3065 hlist_for_each_entry(pol, chain, bydst) {
3066 if ((pol->priority >= priority) && ret)
3069 if ((if_id == 0 || pol->if_id == if_id) &&
3070 xfrm_migrate_selector_match(sel, &pol->selector) &&
3071 pol->type == type) {
3079 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
3084 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3088 if (t->mode == m->mode && t->id.proto == m->proto &&
3089 (m->reqid == 0 || t->reqid == m->reqid)) {
3091 case XFRM_MODE_TUNNEL:
3092 case XFRM_MODE_BEET:
3093 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3095 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3100 case XFRM_MODE_TRANSPORT:
3101 /* in case of transport mode, template does not store
3102 any IP addresses, hence we just compare mode and
3113 /* update endpoint address(es) of template(s) */
3114 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3115 struct xfrm_migrate *m, int num_migrate)
3117 struct xfrm_migrate *mp;
3120 write_lock_bh(&pol->lock);
3121 if (unlikely(pol->walk.dead)) {
3122 /* target policy has been deleted */
3123 write_unlock_bh(&pol->lock);
3127 for (i = 0; i < pol->xfrm_nr; i++) {
3128 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3129 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3132 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3133 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3135 /* update endpoints */
3136 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3137 sizeof(pol->xfrm_vec[i].id.daddr));
3138 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3139 sizeof(pol->xfrm_vec[i].saddr));
3140 pol->xfrm_vec[i].encap_family = mp->new_family;
3142 atomic_inc(&pol->genid);
3146 write_unlock_bh(&pol->lock);
3154 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3158 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3161 for (i = 0; i < num_migrate; i++) {
3162 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3163 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3166 /* check if there is any duplicated entry */
3167 for (j = i + 1; j < num_migrate; j++) {
3168 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3169 sizeof(m[i].old_daddr)) &&
3170 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3171 sizeof(m[i].old_saddr)) &&
3172 m[i].proto == m[j].proto &&
3173 m[i].mode == m[j].mode &&
3174 m[i].reqid == m[j].reqid &&
3175 m[i].old_family == m[j].old_family)
3183 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3184 struct xfrm_migrate *m, int num_migrate,
3185 struct xfrm_kmaddress *k, struct net *net,
3186 struct xfrm_encap_tmpl *encap, u32 if_id)
3188 int i, err, nx_cur = 0, nx_new = 0;
3189 struct xfrm_policy *pol = NULL;
3190 struct xfrm_state *x, *xc;
3191 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3192 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3193 struct xfrm_migrate *mp;
3195 /* Stage 0 - sanity checks */
3196 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3199 if (dir >= XFRM_POLICY_MAX) {
3204 /* Stage 1 - find policy */
3205 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net, if_id)) == NULL) {
3210 /* Stage 2 - find and update state(s) */
3211 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3212 if ((x = xfrm_migrate_state_find(mp, net, if_id))) {
3215 xc = xfrm_state_migrate(x, mp, encap);
3226 /* Stage 3 - update policy */
3227 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3230 /* Stage 4 - delete old state(s) */
3232 xfrm_states_put(x_cur, nx_cur);
3233 xfrm_states_delete(x_cur, nx_cur);
3236 /* Stage 5 - announce */
3237 km_migrate(sel, dir, type, m, num_migrate, k, encap);
3249 xfrm_states_put(x_cur, nx_cur);
3251 xfrm_states_delete(x_new, nx_new);
3255 EXPORT_SYMBOL(xfrm_migrate);