6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
27 #include <linux/audit.h>
32 #ifdef CONFIG_XFRM_STATISTICS
36 #include "xfrm_hash.h"
38 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40 #define XFRM_MAX_QUEUE_LEN 100
43 struct dst_entry *dst_orig;
47 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48 static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
51 static struct kmem_cache *xfrm_dst_cache __read_mostly;
53 static void xfrm_init_pmtu(struct dst_entry *dst);
54 static int stale_bundle(struct dst_entry *dst);
55 static int xfrm_bundle_ok(struct xfrm_dst *xdst);
56 static void xfrm_policy_queue_process(unsigned long arg);
58 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
59 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
63 __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
65 const struct flowi4 *fl4 = &fl->u.ip4;
67 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
68 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
69 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
71 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
72 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
76 __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
78 const struct flowi6 *fl6 = &fl->u.ip6;
80 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
81 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
82 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
83 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
84 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
85 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
88 bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
89 unsigned short family)
93 return __xfrm4_selector_match(sel, fl);
95 return __xfrm6_selector_match(sel, fl);
100 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
102 struct xfrm_policy_afinfo *afinfo;
104 if (unlikely(family >= NPROTO))
107 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
108 if (unlikely(!afinfo))
113 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
118 static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
127 afinfo = xfrm_policy_get_afinfo(family);
128 if (unlikely(afinfo == NULL))
129 return ERR_PTR(-EAFNOSUPPORT);
131 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
133 xfrm_policy_put_afinfo(afinfo);
138 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
140 xfrm_address_t *prev_saddr,
141 xfrm_address_t *prev_daddr,
144 struct net *net = xs_net(x);
145 xfrm_address_t *saddr = &x->props.saddr;
146 xfrm_address_t *daddr = &x->id.daddr;
147 struct dst_entry *dst;
149 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
153 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
158 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
161 if (prev_saddr != saddr)
162 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
163 if (prev_daddr != daddr)
164 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
170 static inline unsigned long make_jiffies(long secs)
172 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
173 return MAX_SCHEDULE_TIMEOUT-1;
178 static void xfrm_policy_timer(unsigned long data)
180 struct xfrm_policy *xp = (struct xfrm_policy *)data;
181 unsigned long now = get_seconds();
182 long next = LONG_MAX;
186 read_lock(&xp->lock);
188 if (unlikely(xp->walk.dead))
191 dir = xfrm_policy_id2dir(xp->index);
193 if (xp->lft.hard_add_expires_seconds) {
194 long tmo = xp->lft.hard_add_expires_seconds +
195 xp->curlft.add_time - now;
201 if (xp->lft.hard_use_expires_seconds) {
202 long tmo = xp->lft.hard_use_expires_seconds +
203 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
209 if (xp->lft.soft_add_expires_seconds) {
210 long tmo = xp->lft.soft_add_expires_seconds +
211 xp->curlft.add_time - now;
214 tmo = XFRM_KM_TIMEOUT;
219 if (xp->lft.soft_use_expires_seconds) {
220 long tmo = xp->lft.soft_use_expires_seconds +
221 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
224 tmo = XFRM_KM_TIMEOUT;
231 km_policy_expired(xp, dir, 0, 0);
232 if (next != LONG_MAX &&
233 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
237 read_unlock(&xp->lock);
242 read_unlock(&xp->lock);
243 if (!xfrm_policy_delete(xp, dir))
244 km_policy_expired(xp, dir, 1, 0);
248 static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
250 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
252 if (unlikely(pol->walk.dead))
260 static int xfrm_policy_flo_check(struct flow_cache_object *flo)
262 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
264 return !pol->walk.dead;
267 static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
269 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
272 static const struct flow_cache_ops xfrm_policy_fc_ops = {
273 .get = xfrm_policy_flo_get,
274 .check = xfrm_policy_flo_check,
275 .delete = xfrm_policy_flo_delete,
278 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
282 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
284 struct xfrm_policy *policy;
286 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
289 write_pnet(&policy->xp_net, net);
290 INIT_LIST_HEAD(&policy->walk.all);
291 INIT_HLIST_NODE(&policy->bydst);
292 INIT_HLIST_NODE(&policy->byidx);
293 rwlock_init(&policy->lock);
294 atomic_set(&policy->refcnt, 1);
295 skb_queue_head_init(&policy->polq.hold_queue);
296 setup_timer(&policy->timer, xfrm_policy_timer,
297 (unsigned long)policy);
298 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
299 (unsigned long)policy);
300 policy->flo.ops = &xfrm_policy_fc_ops;
304 EXPORT_SYMBOL(xfrm_policy_alloc);
306 static void xfrm_policy_destroy_rcu(struct rcu_head *head)
308 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
310 security_xfrm_policy_free(policy->security);
314 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
316 void xfrm_policy_destroy(struct xfrm_policy *policy)
318 BUG_ON(!policy->walk.dead);
320 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
323 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
325 EXPORT_SYMBOL(xfrm_policy_destroy);
327 /* Rule must be locked. Release descentant resources, announce
328 * entry dead. The rule must be unlinked from lists to the moment.
331 static void xfrm_policy_kill(struct xfrm_policy *policy)
333 write_lock_bh(&policy->lock);
334 policy->walk.dead = 1;
335 write_unlock_bh(&policy->lock);
337 atomic_inc(&policy->genid);
339 if (del_timer(&policy->polq.hold_timer))
340 xfrm_pol_put(policy);
341 skb_queue_purge(&policy->polq.hold_queue);
343 if (del_timer(&policy->timer))
344 xfrm_pol_put(policy);
346 xfrm_pol_put(policy);
349 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
351 static inline unsigned int idx_hash(struct net *net, u32 index)
353 return __idx_hash(index, net->xfrm.policy_idx_hmask);
356 /* calculate policy hash thresholds */
357 static void __get_hash_thresh(struct net *net,
358 unsigned short family, int dir,
359 u8 *dbits, u8 *sbits)
363 *dbits = net->xfrm.policy_bydst[dir].dbits4;
364 *sbits = net->xfrm.policy_bydst[dir].sbits4;
368 *dbits = net->xfrm.policy_bydst[dir].dbits6;
369 *sbits = net->xfrm.policy_bydst[dir].sbits6;
378 static struct hlist_head *policy_hash_bysel(struct net *net,
379 const struct xfrm_selector *sel,
380 unsigned short family, int dir)
382 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
387 __get_hash_thresh(net, family, dir, &dbits, &sbits);
388 hash = __sel_hash(sel, family, hmask, dbits, sbits);
390 return (hash == hmask + 1 ?
391 &net->xfrm.policy_inexact[dir] :
392 net->xfrm.policy_bydst[dir].table + hash);
395 static struct hlist_head *policy_hash_direct(struct net *net,
396 const xfrm_address_t *daddr,
397 const xfrm_address_t *saddr,
398 unsigned short family, int dir)
400 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
405 __get_hash_thresh(net, family, dir, &dbits, &sbits);
406 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
408 return net->xfrm.policy_bydst[dir].table + hash;
411 static void xfrm_dst_hash_transfer(struct net *net,
412 struct hlist_head *list,
413 struct hlist_head *ndsttable,
414 unsigned int nhashmask,
417 struct hlist_node *tmp, *entry0 = NULL;
418 struct xfrm_policy *pol;
424 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
427 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
428 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
429 pol->family, nhashmask, dbits, sbits);
431 hlist_del(&pol->bydst);
432 hlist_add_head(&pol->bydst, ndsttable+h);
437 hlist_del(&pol->bydst);
438 hlist_add_behind(&pol->bydst, entry0);
440 entry0 = &pol->bydst;
442 if (!hlist_empty(list)) {
448 static void xfrm_idx_hash_transfer(struct hlist_head *list,
449 struct hlist_head *nidxtable,
450 unsigned int nhashmask)
452 struct hlist_node *tmp;
453 struct xfrm_policy *pol;
455 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
458 h = __idx_hash(pol->index, nhashmask);
459 hlist_add_head(&pol->byidx, nidxtable+h);
463 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
465 return ((old_hmask + 1) << 1) - 1;
468 static void xfrm_bydst_resize(struct net *net, int dir)
470 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
471 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
472 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
473 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
474 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
480 write_lock_bh(&net->xfrm.xfrm_policy_lock);
482 for (i = hmask; i >= 0; i--)
483 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
485 net->xfrm.policy_bydst[dir].table = ndst;
486 net->xfrm.policy_bydst[dir].hmask = nhashmask;
488 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
490 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
493 static void xfrm_byidx_resize(struct net *net, int total)
495 unsigned int hmask = net->xfrm.policy_idx_hmask;
496 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
497 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
498 struct hlist_head *oidx = net->xfrm.policy_byidx;
499 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
505 write_lock_bh(&net->xfrm.xfrm_policy_lock);
507 for (i = hmask; i >= 0; i--)
508 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
510 net->xfrm.policy_byidx = nidx;
511 net->xfrm.policy_idx_hmask = nhashmask;
513 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
515 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
518 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
520 unsigned int cnt = net->xfrm.policy_count[dir];
521 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
526 if ((hmask + 1) < xfrm_policy_hashmax &&
533 static inline int xfrm_byidx_should_resize(struct net *net, int total)
535 unsigned int hmask = net->xfrm.policy_idx_hmask;
537 if ((hmask + 1) < xfrm_policy_hashmax &&
544 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
546 read_lock_bh(&net->xfrm.xfrm_policy_lock);
547 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
548 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
549 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
550 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
551 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
552 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
553 si->spdhcnt = net->xfrm.policy_idx_hmask;
554 si->spdhmcnt = xfrm_policy_hashmax;
555 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
557 EXPORT_SYMBOL(xfrm_spd_getinfo);
559 static DEFINE_MUTEX(hash_resize_mutex);
560 static void xfrm_hash_resize(struct work_struct *work)
562 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
565 mutex_lock(&hash_resize_mutex);
568 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
569 if (xfrm_bydst_should_resize(net, dir, &total))
570 xfrm_bydst_resize(net, dir);
572 if (xfrm_byidx_should_resize(net, total))
573 xfrm_byidx_resize(net, total);
575 mutex_unlock(&hash_resize_mutex);
578 static void xfrm_hash_rebuild(struct work_struct *work)
580 struct net *net = container_of(work, struct net,
581 xfrm.policy_hthresh.work);
583 struct xfrm_policy *pol;
584 struct xfrm_policy *policy;
585 struct hlist_head *chain;
586 struct hlist_head *odst;
587 struct hlist_node *newpos;
591 u8 lbits4, rbits4, lbits6, rbits6;
593 mutex_lock(&hash_resize_mutex);
595 /* read selector prefixlen thresholds */
597 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
599 lbits4 = net->xfrm.policy_hthresh.lbits4;
600 rbits4 = net->xfrm.policy_hthresh.rbits4;
601 lbits6 = net->xfrm.policy_hthresh.lbits6;
602 rbits6 = net->xfrm.policy_hthresh.rbits6;
603 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
605 write_lock_bh(&net->xfrm.xfrm_policy_lock);
607 /* reset the bydst and inexact table in all directions */
608 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
609 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
610 hmask = net->xfrm.policy_bydst[dir].hmask;
611 odst = net->xfrm.policy_bydst[dir].table;
612 for (i = hmask; i >= 0; i--)
613 INIT_HLIST_HEAD(odst + i);
614 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
615 /* dir out => dst = remote, src = local */
616 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
617 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
618 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
619 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
621 /* dir in/fwd => dst = local, src = remote */
622 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
623 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
624 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
625 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
629 /* re-insert all policies by order of creation */
630 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
631 if (policy->walk.dead ||
632 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
633 /* skip socket policies */
637 chain = policy_hash_bysel(net, &policy->selector,
639 xfrm_policy_id2dir(policy->index));
640 hlist_for_each_entry(pol, chain, bydst) {
641 if (policy->priority >= pol->priority)
642 newpos = &pol->bydst;
647 hlist_add_behind(&policy->bydst, newpos);
649 hlist_add_head(&policy->bydst, chain);
652 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
654 mutex_unlock(&hash_resize_mutex);
657 void xfrm_policy_hash_rebuild(struct net *net)
659 schedule_work(&net->xfrm.policy_hthresh.work);
661 EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
663 /* Generate new index... KAME seems to generate them ordered by cost
664 * of an absolute inpredictability of ordering of rules. This will not pass. */
665 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
667 static u32 idx_generator;
670 struct hlist_head *list;
671 struct xfrm_policy *p;
676 idx = (idx_generator | dir);
685 list = net->xfrm.policy_byidx + idx_hash(net, idx);
687 hlist_for_each_entry(p, list, byidx) {
688 if (p->index == idx) {
698 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
700 u32 *p1 = (u32 *) s1;
701 u32 *p2 = (u32 *) s2;
702 int len = sizeof(struct xfrm_selector) / sizeof(u32);
705 for (i = 0; i < len; i++) {
713 static void xfrm_policy_requeue(struct xfrm_policy *old,
714 struct xfrm_policy *new)
716 struct xfrm_policy_queue *pq = &old->polq;
717 struct sk_buff_head list;
719 if (skb_queue_empty(&pq->hold_queue))
722 __skb_queue_head_init(&list);
724 spin_lock_bh(&pq->hold_queue.lock);
725 skb_queue_splice_init(&pq->hold_queue, &list);
726 if (del_timer(&pq->hold_timer))
728 spin_unlock_bh(&pq->hold_queue.lock);
732 spin_lock_bh(&pq->hold_queue.lock);
733 skb_queue_splice(&list, &pq->hold_queue);
734 pq->timeout = XFRM_QUEUE_TMO_MIN;
735 if (!mod_timer(&pq->hold_timer, jiffies))
737 spin_unlock_bh(&pq->hold_queue.lock);
740 static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
741 struct xfrm_policy *pol)
743 if (policy->mark.v == pol->mark.v &&
744 policy->priority == pol->priority)
750 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
752 struct net *net = xp_net(policy);
753 struct xfrm_policy *pol;
754 struct xfrm_policy *delpol;
755 struct hlist_head *chain;
756 struct hlist_node *newpos;
758 write_lock_bh(&net->xfrm.xfrm_policy_lock);
759 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
762 hlist_for_each_entry(pol, chain, bydst) {
763 if (pol->type == policy->type &&
764 !selector_cmp(&pol->selector, &policy->selector) &&
765 xfrm_policy_mark_match(policy, pol) &&
766 xfrm_sec_ctx_match(pol->security, policy->security) &&
769 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
773 if (policy->priority > pol->priority)
775 } else if (policy->priority >= pol->priority) {
776 newpos = &pol->bydst;
783 hlist_add_behind(&policy->bydst, newpos);
785 hlist_add_head(&policy->bydst, chain);
786 __xfrm_policy_link(policy, dir);
787 atomic_inc(&net->xfrm.flow_cache_genid);
789 /* After previous checking, family can either be AF_INET or AF_INET6 */
790 if (policy->family == AF_INET)
791 rt_genid_bump_ipv4(net);
793 rt_genid_bump_ipv6(net);
796 xfrm_policy_requeue(delpol, policy);
797 __xfrm_policy_unlink(delpol, dir);
799 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
800 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
801 policy->curlft.add_time = get_seconds();
802 policy->curlft.use_time = 0;
803 if (!mod_timer(&policy->timer, jiffies + HZ))
804 xfrm_pol_hold(policy);
805 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
808 xfrm_policy_kill(delpol);
809 else if (xfrm_bydst_should_resize(net, dir, NULL))
810 schedule_work(&net->xfrm.policy_hash_work);
814 EXPORT_SYMBOL(xfrm_policy_insert);
816 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
817 int dir, struct xfrm_selector *sel,
818 struct xfrm_sec_ctx *ctx, int delete,
821 struct xfrm_policy *pol, *ret;
822 struct hlist_head *chain;
825 write_lock_bh(&net->xfrm.xfrm_policy_lock);
826 chain = policy_hash_bysel(net, sel, sel->family, dir);
828 hlist_for_each_entry(pol, chain, bydst) {
829 if (pol->type == type &&
830 (mark & pol->mark.m) == pol->mark.v &&
831 !selector_cmp(sel, &pol->selector) &&
832 xfrm_sec_ctx_match(ctx, pol->security)) {
835 *err = security_xfrm_policy_delete(
838 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
841 __xfrm_policy_unlink(pol, dir);
847 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
850 xfrm_policy_kill(ret);
853 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
855 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
856 int dir, u32 id, int delete, int *err)
858 struct xfrm_policy *pol, *ret;
859 struct hlist_head *chain;
862 if (xfrm_policy_id2dir(id) != dir)
866 write_lock_bh(&net->xfrm.xfrm_policy_lock);
867 chain = net->xfrm.policy_byidx + idx_hash(net, id);
869 hlist_for_each_entry(pol, chain, byidx) {
870 if (pol->type == type && pol->index == id &&
871 (mark & pol->mark.m) == pol->mark.v) {
874 *err = security_xfrm_policy_delete(
877 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
880 __xfrm_policy_unlink(pol, dir);
886 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
889 xfrm_policy_kill(ret);
892 EXPORT_SYMBOL(xfrm_policy_byid);
894 #ifdef CONFIG_SECURITY_NETWORK_XFRM
896 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
900 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
901 struct xfrm_policy *pol;
904 hlist_for_each_entry(pol,
905 &net->xfrm.policy_inexact[dir], bydst) {
906 if (pol->type != type)
908 err = security_xfrm_policy_delete(pol->security);
910 xfrm_audit_policy_delete(pol, 0, task_valid);
914 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
915 hlist_for_each_entry(pol,
916 net->xfrm.policy_bydst[dir].table + i,
918 if (pol->type != type)
920 err = security_xfrm_policy_delete(
923 xfrm_audit_policy_delete(pol, 0,
934 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
940 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
942 int dir, err = 0, cnt = 0;
944 write_lock_bh(&net->xfrm.xfrm_policy_lock);
946 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
950 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
951 struct xfrm_policy *pol;
955 hlist_for_each_entry(pol,
956 &net->xfrm.policy_inexact[dir], bydst) {
957 if (pol->type != type)
959 __xfrm_policy_unlink(pol, dir);
960 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
963 xfrm_audit_policy_delete(pol, 1, task_valid);
965 xfrm_policy_kill(pol);
967 write_lock_bh(&net->xfrm.xfrm_policy_lock);
971 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
973 hlist_for_each_entry(pol,
974 net->xfrm.policy_bydst[dir].table + i,
976 if (pol->type != type)
978 __xfrm_policy_unlink(pol, dir);
979 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
982 xfrm_audit_policy_delete(pol, 1, task_valid);
983 xfrm_policy_kill(pol);
985 write_lock_bh(&net->xfrm.xfrm_policy_lock);
994 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
997 EXPORT_SYMBOL(xfrm_policy_flush);
999 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1000 int (*func)(struct xfrm_policy *, int, int, void*),
1003 struct xfrm_policy *pol;
1004 struct xfrm_policy_walk_entry *x;
1007 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1008 walk->type != XFRM_POLICY_TYPE_ANY)
1011 if (list_empty(&walk->walk.all) && walk->seq != 0)
1014 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1015 if (list_empty(&walk->walk.all))
1016 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
1018 x = list_first_entry(&walk->walk.all,
1019 struct xfrm_policy_walk_entry, all);
1021 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
1024 pol = container_of(x, struct xfrm_policy, walk);
1025 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1026 walk->type != pol->type)
1028 error = func(pol, xfrm_policy_id2dir(pol->index),
1031 list_move_tail(&walk->walk.all, &x->all);
1036 if (walk->seq == 0) {
1040 list_del_init(&walk->walk.all);
1042 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1045 EXPORT_SYMBOL(xfrm_policy_walk);
1047 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1049 INIT_LIST_HEAD(&walk->walk.all);
1050 walk->walk.dead = 1;
1054 EXPORT_SYMBOL(xfrm_policy_walk_init);
1056 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
1058 if (list_empty(&walk->walk.all))
1061 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
1062 list_del(&walk->walk.all);
1063 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1065 EXPORT_SYMBOL(xfrm_policy_walk_done);
1068 * Find policy to apply to this flow.
1070 * Returns 0 if policy found, else an -errno.
1072 static int xfrm_policy_match(const struct xfrm_policy *pol,
1073 const struct flowi *fl,
1074 u8 type, u16 family, int dir)
1076 const struct xfrm_selector *sel = &pol->selector;
1080 if (pol->family != family ||
1081 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
1085 match = xfrm_selector_match(sel, fl, family);
1087 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1093 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
1094 const struct flowi *fl,
1098 struct xfrm_policy *pol, *ret;
1099 const xfrm_address_t *daddr, *saddr;
1100 struct hlist_head *chain;
1103 daddr = xfrm_flowi_daddr(fl, family);
1104 saddr = xfrm_flowi_saddr(fl, family);
1105 if (unlikely(!daddr || !saddr))
1108 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1109 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1111 hlist_for_each_entry(pol, chain, bydst) {
1112 err = xfrm_policy_match(pol, fl, type, family, dir);
1122 priority = ret->priority;
1126 chain = &net->xfrm.policy_inexact[dir];
1127 hlist_for_each_entry(pol, chain, bydst) {
1128 if ((pol->priority >= priority) && ret)
1131 err = xfrm_policy_match(pol, fl, type, family, dir);
1147 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1152 static struct xfrm_policy *
1153 __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
1155 #ifdef CONFIG_XFRM_SUB_POLICY
1156 struct xfrm_policy *pol;
1158 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1162 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1165 static int flow_to_policy_dir(int dir)
1167 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1168 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1169 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1175 return XFRM_POLICY_IN;
1177 return XFRM_POLICY_OUT;
1179 return XFRM_POLICY_FWD;
1183 static struct flow_cache_object *
1184 xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
1185 u8 dir, struct flow_cache_object *old_obj, void *ctx)
1187 struct xfrm_policy *pol;
1190 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
1192 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
1193 if (IS_ERR_OR_NULL(pol))
1194 return ERR_CAST(pol);
1196 /* Resolver returns two references:
1197 * one for cache and one for caller of flow_cache_lookup() */
1203 static inline int policy_to_flow_dir(int dir)
1205 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1206 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1207 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1211 case XFRM_POLICY_IN:
1213 case XFRM_POLICY_OUT:
1214 return FLOW_DIR_OUT;
1215 case XFRM_POLICY_FWD:
1216 return FLOW_DIR_FWD;
1220 static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
1221 const struct flowi *fl, u16 family)
1223 struct xfrm_policy *pol;
1224 struct net *net = sock_net(sk);
1227 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1228 pol = rcu_dereference(sk->sk_policy[dir]);
1233 if (pol->family != family) {
1238 match = xfrm_selector_match(&pol->selector, fl, family);
1240 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1244 err = security_xfrm_policy_lookup(pol->security,
1246 policy_to_flow_dir(dir));
1249 else if (err == -ESRCH)
1257 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1262 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1264 struct net *net = xp_net(pol);
1266 list_add(&pol->walk.all, &net->xfrm.policy_all);
1267 net->xfrm.policy_count[dir]++;
1271 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1274 struct net *net = xp_net(pol);
1276 if (list_empty(&pol->walk.all))
1279 /* Socket policies are not hashed. */
1280 if (!hlist_unhashed(&pol->bydst)) {
1281 hlist_del(&pol->bydst);
1282 hlist_del(&pol->byidx);
1285 list_del_init(&pol->walk.all);
1286 net->xfrm.policy_count[dir]--;
1291 static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1293 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1296 static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1298 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1301 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1303 struct net *net = xp_net(pol);
1305 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1306 pol = __xfrm_policy_unlink(pol, dir);
1307 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1309 xfrm_policy_kill(pol);
1314 EXPORT_SYMBOL(xfrm_policy_delete);
1316 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1318 struct net *net = sock_net(sk);
1319 struct xfrm_policy *old_pol;
1321 #ifdef CONFIG_XFRM_SUB_POLICY
1322 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1326 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1327 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1328 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
1330 pol->curlft.add_time = get_seconds();
1331 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
1332 xfrm_sk_policy_link(pol, dir);
1334 rcu_assign_pointer(sk->sk_policy[dir], pol);
1337 xfrm_policy_requeue(old_pol, pol);
1339 /* Unlinking succeeds always. This is the only function
1340 * allowed to delete or replace socket policy.
1342 xfrm_sk_policy_unlink(old_pol, dir);
1344 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1347 xfrm_policy_kill(old_pol);
1352 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1354 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1355 struct net *net = xp_net(old);
1358 newp->selector = old->selector;
1359 if (security_xfrm_policy_clone(old->security,
1362 return NULL; /* ENOMEM */
1364 newp->lft = old->lft;
1365 newp->curlft = old->curlft;
1366 newp->mark = old->mark;
1367 newp->action = old->action;
1368 newp->flags = old->flags;
1369 newp->xfrm_nr = old->xfrm_nr;
1370 newp->index = old->index;
1371 newp->type = old->type;
1372 newp->family = old->family;
1373 memcpy(newp->xfrm_vec, old->xfrm_vec,
1374 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1375 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1376 xfrm_sk_policy_link(newp, dir);
1377 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1383 int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
1385 const struct xfrm_policy *p;
1386 struct xfrm_policy *np;
1390 for (i = 0; i < 2; i++) {
1391 p = rcu_dereference(osk->sk_policy[i]);
1393 np = clone_policy(p, i);
1394 if (unlikely(!np)) {
1398 rcu_assign_pointer(sk->sk_policy[i], np);
1406 xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1407 xfrm_address_t *remote, unsigned short family)
1410 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1412 if (unlikely(afinfo == NULL))
1414 err = afinfo->get_saddr(net, oif, local, remote);
1415 xfrm_policy_put_afinfo(afinfo);
1419 /* Resolve list of templates for the flow, given policy. */
1422 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1423 struct xfrm_state **xfrm, unsigned short family)
1425 struct net *net = xp_net(policy);
1428 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1429 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1432 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1433 struct xfrm_state *x;
1434 xfrm_address_t *remote = daddr;
1435 xfrm_address_t *local = saddr;
1436 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1438 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1439 tmpl->mode == XFRM_MODE_BEET) {
1440 remote = &tmpl->id.daddr;
1441 local = &tmpl->saddr;
1442 if (xfrm_addr_any(local, tmpl->encap_family)) {
1443 error = xfrm_get_saddr(net, fl->flowi_oif,
1445 tmpl->encap_family);
1452 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1454 if (x && x->km.state == XFRM_STATE_VALID) {
1461 error = (x->km.state == XFRM_STATE_ERROR ?
1464 } else if (error == -ESRCH) {
1468 if (!tmpl->optional)
1474 for (nx--; nx >= 0; nx--)
1475 xfrm_state_put(xfrm[nx]);
1480 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1481 struct xfrm_state **xfrm, unsigned short family)
1483 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1484 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1490 for (i = 0; i < npols; i++) {
1491 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1496 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1504 /* found states are sorted for outbound processing */
1506 xfrm_state_sort(xfrm, tpp, cnx, family);
1511 for (cnx--; cnx >= 0; cnx--)
1512 xfrm_state_put(tpp[cnx]);
1517 /* Check that the bundle accepts the flow and its components are
1521 static inline int xfrm_get_tos(const struct flowi *fl, int family)
1523 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1529 tos = afinfo->get_tos(fl);
1531 xfrm_policy_put_afinfo(afinfo);
1536 static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1538 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1539 struct dst_entry *dst = &xdst->u.dst;
1541 if (xdst->route == NULL) {
1542 /* Dummy bundle - if it has xfrms we were not
1543 * able to build bundle as template resolution failed.
1544 * It means we need to try again resolving. */
1545 if (xdst->num_xfrms > 0)
1547 } else if (dst->flags & DST_XFRM_QUEUE) {
1551 if (stale_bundle(dst))
1559 static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1561 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1562 struct dst_entry *dst = &xdst->u.dst;
1566 if (stale_bundle(dst))
1572 static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1574 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1575 struct dst_entry *dst = &xdst->u.dst;
1580 static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1581 .get = xfrm_bundle_flo_get,
1582 .check = xfrm_bundle_flo_check,
1583 .delete = xfrm_bundle_flo_delete,
1586 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1588 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1589 struct dst_ops *dst_ops;
1590 struct xfrm_dst *xdst;
1593 return ERR_PTR(-EINVAL);
1597 dst_ops = &net->xfrm.xfrm4_dst_ops;
1599 #if IS_ENABLED(CONFIG_IPV6)
1601 dst_ops = &net->xfrm.xfrm6_dst_ops;
1607 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
1610 struct dst_entry *dst = &xdst->u.dst;
1612 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1613 xdst->flo.ops = &xfrm_bundle_fc_ops;
1615 xdst = ERR_PTR(-ENOBUFS);
1617 xfrm_policy_put_afinfo(afinfo);
1622 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1625 struct xfrm_policy_afinfo *afinfo =
1626 xfrm_policy_get_afinfo(dst->ops->family);
1632 err = afinfo->init_path(path, dst, nfheader_len);
1634 xfrm_policy_put_afinfo(afinfo);
1639 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1640 const struct flowi *fl)
1642 struct xfrm_policy_afinfo *afinfo =
1643 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1649 err = afinfo->fill_dst(xdst, dev, fl);
1651 xfrm_policy_put_afinfo(afinfo);
1657 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1658 * all the metrics... Shortly, bundle a bundle.
1661 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1662 struct xfrm_state **xfrm, int nx,
1663 const struct flowi *fl,
1664 struct dst_entry *dst)
1666 struct net *net = xp_net(policy);
1667 unsigned long now = jiffies;
1668 struct net_device *dev;
1669 struct xfrm_mode *inner_mode;
1670 struct dst_entry *dst_prev = NULL;
1671 struct dst_entry *dst0 = NULL;
1675 int nfheader_len = 0;
1676 int trailer_len = 0;
1678 int family = policy->selector.family;
1679 xfrm_address_t saddr, daddr;
1681 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1683 tos = xfrm_get_tos(fl, family);
1690 for (; i < nx; i++) {
1691 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1692 struct dst_entry *dst1 = &xdst->u.dst;
1694 err = PTR_ERR(xdst);
1700 if (xfrm[i]->sel.family == AF_UNSPEC) {
1701 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1702 xfrm_af2proto(family));
1704 err = -EAFNOSUPPORT;
1709 inner_mode = xfrm[i]->inner_mode;
1714 dst_prev->child = dst_clone(dst1);
1715 dst1->flags |= DST_NOHASH;
1719 dst_copy_metrics(dst1, dst);
1721 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1722 family = xfrm[i]->props.family;
1723 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1724 &saddr, &daddr, family);
1731 dst1->xfrm = xfrm[i];
1732 xdst->xfrm_genid = xfrm[i]->genid;
1734 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1735 dst1->flags |= DST_HOST;
1736 dst1->lastuse = now;
1738 dst1->input = dst_discard;
1739 dst1->output = inner_mode->afinfo->output;
1741 dst1->next = dst_prev;
1744 header_len += xfrm[i]->props.header_len;
1745 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1746 nfheader_len += xfrm[i]->props.header_len;
1747 trailer_len += xfrm[i]->props.trailer_len;
1750 dst_prev->child = dst;
1758 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
1759 xfrm_init_pmtu(dst_prev);
1761 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1762 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1764 err = xfrm_fill_dst(xdst, dev, fl);
1768 dst_prev->header_len = header_len;
1769 dst_prev->trailer_len = trailer_len;
1770 header_len -= xdst->u.dst.xfrm->props.header_len;
1771 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1779 xfrm_state_put(xfrm[i]);
1783 dst0 = ERR_PTR(err);
1787 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1788 struct xfrm_policy **pols,
1789 int *num_pols, int *num_xfrms)
1793 if (*num_pols == 0 || !pols[0]) {
1798 if (IS_ERR(pols[0]))
1799 return PTR_ERR(pols[0]);
1801 *num_xfrms = pols[0]->xfrm_nr;
1803 #ifdef CONFIG_XFRM_SUB_POLICY
1804 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1805 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1806 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1807 XFRM_POLICY_TYPE_MAIN,
1811 if (IS_ERR(pols[1])) {
1812 xfrm_pols_put(pols, *num_pols);
1813 return PTR_ERR(pols[1]);
1816 (*num_xfrms) += pols[1]->xfrm_nr;
1820 for (i = 0; i < *num_pols; i++) {
1821 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1831 static struct xfrm_dst *
1832 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1833 const struct flowi *fl, u16 family,
1834 struct dst_entry *dst_orig)
1836 struct net *net = xp_net(pols[0]);
1837 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1838 struct dst_entry *dst;
1839 struct xfrm_dst *xdst;
1842 /* Try to instantiate a bundle */
1843 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1849 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1850 return ERR_PTR(err);
1853 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1855 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1856 return ERR_CAST(dst);
1859 xdst = (struct xfrm_dst *)dst;
1860 xdst->num_xfrms = err;
1861 xdst->num_pols = num_pols;
1862 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
1863 xdst->policy_genid = atomic_read(&pols[0]->genid);
1868 static void xfrm_policy_queue_process(unsigned long arg)
1870 struct sk_buff *skb;
1872 struct dst_entry *dst;
1873 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1874 struct net *net = xp_net(pol);
1875 struct xfrm_policy_queue *pq = &pol->polq;
1877 struct sk_buff_head list;
1879 spin_lock(&pq->hold_queue.lock);
1880 skb = skb_peek(&pq->hold_queue);
1882 spin_unlock(&pq->hold_queue.lock);
1887 xfrm_decode_session(skb, &fl, dst->ops->family);
1888 spin_unlock(&pq->hold_queue.lock);
1890 dst_hold(dst->path);
1891 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
1895 if (dst->flags & DST_XFRM_QUEUE) {
1898 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1901 pq->timeout = pq->timeout << 1;
1902 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1909 __skb_queue_head_init(&list);
1911 spin_lock(&pq->hold_queue.lock);
1913 skb_queue_splice_init(&pq->hold_queue, &list);
1914 spin_unlock(&pq->hold_queue.lock);
1916 while (!skb_queue_empty(&list)) {
1917 skb = __skb_dequeue(&list);
1919 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1920 dst_hold(skb_dst(skb)->path);
1921 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
1929 skb_dst_set(skb, dst);
1931 dst_output(net, skb->sk, skb);
1940 skb_queue_purge(&pq->hold_queue);
1944 static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1946 unsigned long sched_next;
1947 struct dst_entry *dst = skb_dst(skb);
1948 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1949 struct xfrm_policy *pol = xdst->pols[0];
1950 struct xfrm_policy_queue *pq = &pol->polq;
1952 if (unlikely(skb_fclone_busy(sk, skb))) {
1957 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1964 spin_lock_bh(&pq->hold_queue.lock);
1967 pq->timeout = XFRM_QUEUE_TMO_MIN;
1969 sched_next = jiffies + pq->timeout;
1971 if (del_timer(&pq->hold_timer)) {
1972 if (time_before(pq->hold_timer.expires, sched_next))
1973 sched_next = pq->hold_timer.expires;
1977 __skb_queue_tail(&pq->hold_queue, skb);
1978 if (!mod_timer(&pq->hold_timer, sched_next))
1981 spin_unlock_bh(&pq->hold_queue.lock);
1986 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1987 struct xfrm_flo *xflo,
1988 const struct flowi *fl,
1993 struct net_device *dev;
1994 struct dst_entry *dst;
1995 struct dst_entry *dst1;
1996 struct xfrm_dst *xdst;
1998 xdst = xfrm_alloc_dst(net, family);
2002 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2003 net->xfrm.sysctl_larval_drop ||
2007 dst = xflo->dst_orig;
2008 dst1 = &xdst->u.dst;
2012 dst_copy_metrics(dst1, dst);
2014 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2015 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2016 dst1->lastuse = jiffies;
2018 dst1->input = dst_discard;
2019 dst1->output = xdst_queue_output;
2025 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2032 err = xfrm_fill_dst(xdst, dev, fl);
2041 xdst = ERR_PTR(err);
2045 static struct flow_cache_object *
2046 xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
2047 struct flow_cache_object *oldflo, void *ctx)
2049 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
2050 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2051 struct xfrm_dst *xdst, *new_xdst;
2052 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2054 /* Check if the policies from old bundle are usable */
2057 xdst = container_of(oldflo, struct xfrm_dst, flo);
2058 num_pols = xdst->num_pols;
2059 num_xfrms = xdst->num_xfrms;
2061 for (i = 0; i < num_pols; i++) {
2062 pols[i] = xdst->pols[i];
2063 pol_dead |= pols[i]->walk.dead;
2066 dst_free(&xdst->u.dst);
2074 /* Resolve policies to use if we couldn't get them from
2075 * previous cache entry */
2078 pols[0] = __xfrm_policy_lookup(net, fl, family,
2079 flow_to_policy_dir(dir));
2080 err = xfrm_expand_policies(fl, family, pols,
2081 &num_pols, &num_xfrms);
2087 goto make_dummy_bundle;
2090 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2092 if (IS_ERR(new_xdst)) {
2093 err = PTR_ERR(new_xdst);
2097 goto make_dummy_bundle;
2098 dst_hold(&xdst->u.dst);
2100 } else if (new_xdst == NULL) {
2103 goto make_dummy_bundle;
2104 xdst->num_xfrms = 0;
2105 dst_hold(&xdst->u.dst);
2109 /* Kill the previous bundle */
2111 /* The policies were stolen for newly generated bundle */
2113 dst_free(&xdst->u.dst);
2116 /* Flow cache does not have reference, it dst_free()'s,
2117 * but we do need to return one reference for original caller */
2118 dst_hold(&new_xdst->u.dst);
2119 return &new_xdst->flo;
2122 /* We found policies, but there's no bundles to instantiate:
2123 * either because the policy blocks, has no transformations or
2124 * we could not build template (no xfrm_states).*/
2125 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
2127 xfrm_pols_put(pols, num_pols);
2128 return ERR_CAST(xdst);
2130 xdst->num_pols = num_pols;
2131 xdst->num_xfrms = num_xfrms;
2132 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2134 dst_hold(&xdst->u.dst);
2138 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2141 dst_free(&xdst->u.dst);
2143 xfrm_pols_put(pols, num_pols);
2144 return ERR_PTR(err);
2147 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2148 struct dst_entry *dst_orig)
2150 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2151 struct dst_entry *ret;
2154 dst_release(dst_orig);
2155 return ERR_PTR(-EINVAL);
2157 ret = afinfo->blackhole_route(net, dst_orig);
2159 xfrm_policy_put_afinfo(afinfo);
2164 /* Main function: finds/creates a bundle for given flow.
2166 * At the moment we eat a raw IP route. Mostly to speed up lookups
2167 * on interfaces with disabled IPsec.
2169 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2170 const struct flowi *fl,
2171 const struct sock *sk, int flags)
2173 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2174 struct flow_cache_object *flo;
2175 struct xfrm_dst *xdst;
2176 struct dst_entry *dst, *route;
2177 u16 family = dst_orig->ops->family;
2178 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
2179 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2185 sk = sk_const_to_full_sk(sk);
2186 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2188 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family);
2189 err = xfrm_expand_policies(fl, family, pols,
2190 &num_pols, &num_xfrms);
2195 if (num_xfrms <= 0) {
2196 drop_pols = num_pols;
2200 xdst = xfrm_resolve_and_create_bundle(
2204 xfrm_pols_put(pols, num_pols);
2205 err = PTR_ERR(xdst);
2207 } else if (xdst == NULL) {
2209 drop_pols = num_pols;
2213 dst_hold(&xdst->u.dst);
2214 xdst->u.dst.flags |= DST_NOCACHE;
2215 route = xdst->route;
2220 struct xfrm_flo xflo;
2222 xflo.dst_orig = dst_orig;
2225 /* To accelerate a bit... */
2226 if ((dst_orig->flags & DST_NOXFRM) ||
2227 !net->xfrm.policy_count[XFRM_POLICY_OUT])
2230 flo = flow_cache_lookup(net, fl, family, dir,
2231 xfrm_bundle_lookup, &xflo);
2238 xdst = container_of(flo, struct xfrm_dst, flo);
2240 num_pols = xdst->num_pols;
2241 num_xfrms = xdst->num_xfrms;
2242 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
2243 route = xdst->route;
2247 if (route == NULL && num_xfrms > 0) {
2248 /* The only case when xfrm_bundle_lookup() returns a
2249 * bundle with null route, is when the template could
2250 * not be resolved. It means policies are there, but
2251 * bundle could not be created, since we don't yet
2252 * have the xfrm_state's. We need to wait for KM to
2253 * negotiate new SA's or bail out with error.*/
2254 if (net->xfrm.sysctl_larval_drop) {
2255 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2262 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2270 if ((flags & XFRM_LOOKUP_ICMP) &&
2271 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2276 for (i = 0; i < num_pols; i++)
2277 pols[i]->curlft.use_time = get_seconds();
2279 if (num_xfrms < 0) {
2280 /* Prohibit the flow */
2281 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2284 } else if (num_xfrms > 0) {
2285 /* Flow transformed */
2286 dst_release(dst_orig);
2288 /* Flow passes untransformed */
2293 xfrm_pols_put(pols, drop_pols);
2294 if (dst && dst->xfrm &&
2295 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2296 dst->flags |= DST_XFRM_TUNNEL;
2300 if (!(flags & XFRM_LOOKUP_ICMP)) {
2308 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2309 dst_release(dst_orig);
2310 xfrm_pols_put(pols, drop_pols);
2311 return ERR_PTR(err);
2313 EXPORT_SYMBOL(xfrm_lookup);
2315 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2316 * Otherwise we may send out blackholed packets.
2318 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2319 const struct flowi *fl,
2320 const struct sock *sk, int flags)
2322 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2323 flags | XFRM_LOOKUP_QUEUE |
2324 XFRM_LOOKUP_KEEP_DST_REF);
2326 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2327 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2330 dst_release(dst_orig);
2334 EXPORT_SYMBOL(xfrm_lookup_route);
2337 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2339 struct xfrm_state *x;
2341 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2343 x = skb->sp->xvec[idx];
2344 if (!x->type->reject)
2346 return x->type->reject(x, skb, fl);
2349 /* When skb is transformed back to its "native" form, we have to
2350 * check policy restrictions. At the moment we make this in maximally
2351 * stupid way. Shame on me. :-) Of course, connected sockets must
2352 * have policy cached at them.
2356 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2357 unsigned short family)
2359 if (xfrm_state_kern(x))
2360 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2361 return x->id.proto == tmpl->id.proto &&
2362 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2363 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2364 x->props.mode == tmpl->mode &&
2365 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2366 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2367 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2368 xfrm_state_addr_cmp(tmpl, x, family));
2372 * 0 or more than 0 is returned when validation is succeeded (either bypass
2373 * because of optional transport mode, or next index of the mathced secpath
2374 * state with the template.
2375 * -1 is returned when no matching template is found.
2376 * Otherwise "-2 - errored_index" is returned.
2379 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2380 unsigned short family)
2384 if (tmpl->optional) {
2385 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2389 for (; idx < sp->len; idx++) {
2390 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2392 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2401 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2402 unsigned int family, int reverse)
2404 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2407 if (unlikely(afinfo == NULL))
2408 return -EAFNOSUPPORT;
2410 afinfo->decode_session(skb, fl, reverse);
2411 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2412 xfrm_policy_put_afinfo(afinfo);
2415 EXPORT_SYMBOL(__xfrm_decode_session);
2417 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2419 for (; k < sp->len; k++) {
2420 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2429 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2430 unsigned short family)
2432 struct net *net = dev_net(skb->dev);
2433 struct xfrm_policy *pol;
2434 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2443 reverse = dir & ~XFRM_POLICY_MASK;
2444 dir &= XFRM_POLICY_MASK;
2445 fl_dir = policy_to_flow_dir(dir);
2447 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2448 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2452 nf_nat_decode_session(skb, &fl, family);
2454 /* First, check used SA against their selectors. */
2458 for (i = skb->sp->len-1; i >= 0; i--) {
2459 struct xfrm_state *x = skb->sp->xvec[i];
2460 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2461 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2468 sk = sk_to_full_sk(sk);
2469 if (sk && sk->sk_policy[dir]) {
2470 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family);
2472 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2478 struct flow_cache_object *flo;
2480 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2481 xfrm_policy_lookup, NULL);
2482 if (IS_ERR_OR_NULL(flo))
2483 pol = ERR_CAST(flo);
2485 pol = container_of(flo, struct xfrm_policy, flo);
2489 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2494 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2495 xfrm_secpath_reject(xerr_idx, skb, &fl);
2496 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2502 pol->curlft.use_time = get_seconds();
2506 #ifdef CONFIG_XFRM_SUB_POLICY
2507 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2508 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2512 if (IS_ERR(pols[1])) {
2513 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2516 pols[1]->curlft.use_time = get_seconds();
2522 if (pol->action == XFRM_POLICY_ALLOW) {
2523 struct sec_path *sp;
2524 static struct sec_path dummy;
2525 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2526 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2527 struct xfrm_tmpl **tpp = tp;
2531 if ((sp = skb->sp) == NULL)
2534 for (pi = 0; pi < npols; pi++) {
2535 if (pols[pi] != pol &&
2536 pols[pi]->action != XFRM_POLICY_ALLOW) {
2537 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2540 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2541 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2544 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2545 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2549 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
2553 /* For each tunnel xfrm, find the first matching tmpl.
2554 * For each tmpl before that, find corresponding xfrm.
2555 * Order is _important_. Later we will implement
2556 * some barriers, but at the moment barriers
2557 * are implied between each two transformations.
2559 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2560 k = xfrm_policy_ok(tpp[i], sp, k, family);
2563 /* "-2 - errored_index" returned */
2565 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2570 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2571 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2575 xfrm_pols_put(pols, npols);
2578 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2581 xfrm_secpath_reject(xerr_idx, skb, &fl);
2583 xfrm_pols_put(pols, npols);
2586 EXPORT_SYMBOL(__xfrm_policy_check);
2588 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2590 struct net *net = dev_net(skb->dev);
2592 struct dst_entry *dst;
2595 if (xfrm_decode_session(skb, &fl, family) < 0) {
2596 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2602 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
2607 skb_dst_set(skb, dst);
2610 EXPORT_SYMBOL(__xfrm_route_forward);
2612 /* Optimize later using cookies and generation ids. */
2614 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2616 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2617 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2618 * get validated by dst_ops->check on every use. We do this
2619 * because when a normal route referenced by an XFRM dst is
2620 * obsoleted we do not go looking around for all parent
2621 * referencing XFRM dsts so that we can invalidate them. It
2622 * is just too much work. Instead we make the checks here on
2623 * every use. For example:
2625 * XFRM dst A --> IPv4 dst X
2627 * X is the "xdst->route" of A (X is also the "dst->path" of A
2628 * in this example). If X is marked obsolete, "A" will not
2629 * notice. That's what we are validating here via the
2630 * stale_bundle() check.
2632 * When a policy's bundle is pruned, we dst_free() the XFRM
2633 * dst which causes it's ->obsolete field to be set to
2634 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2635 * this, we want to force a new route lookup.
2637 if (dst->obsolete < 0 && !stale_bundle(dst))
2643 static int stale_bundle(struct dst_entry *dst)
2645 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2648 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2650 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
2651 dst->dev = dev_net(dev)->loopback_dev;
2656 EXPORT_SYMBOL(xfrm_dst_ifdown);
2658 static void xfrm_link_failure(struct sk_buff *skb)
2660 /* Impossible. Such dst must be popped before reaches point of failure. */
2663 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2666 if (dst->obsolete) {
2674 void xfrm_garbage_collect(struct net *net)
2676 flow_cache_flush(net);
2678 EXPORT_SYMBOL(xfrm_garbage_collect);
2680 static void xfrm_garbage_collect_deferred(struct net *net)
2682 flow_cache_flush_deferred(net);
2685 static void xfrm_init_pmtu(struct dst_entry *dst)
2688 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2689 u32 pmtu, route_mtu_cached;
2691 pmtu = dst_mtu(dst->child);
2692 xdst->child_mtu_cached = pmtu;
2694 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2696 route_mtu_cached = dst_mtu(xdst->route);
2697 xdst->route_mtu_cached = route_mtu_cached;
2699 if (pmtu > route_mtu_cached)
2700 pmtu = route_mtu_cached;
2702 dst_metric_set(dst, RTAX_MTU, pmtu);
2703 } while ((dst = dst->next));
2706 /* Check that the bundle accepts the flow and its components are
2710 static int xfrm_bundle_ok(struct xfrm_dst *first)
2712 struct dst_entry *dst = &first->u.dst;
2713 struct xfrm_dst *last;
2716 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2717 (dst->dev && !netif_running(dst->dev)))
2720 if (dst->flags & DST_XFRM_QUEUE)
2726 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2728 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2730 if (xdst->xfrm_genid != dst->xfrm->genid)
2732 if (xdst->num_pols > 0 &&
2733 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2736 mtu = dst_mtu(dst->child);
2737 if (xdst->child_mtu_cached != mtu) {
2739 xdst->child_mtu_cached = mtu;
2742 if (!dst_check(xdst->route, xdst->route_cookie))
2744 mtu = dst_mtu(xdst->route);
2745 if (xdst->route_mtu_cached != mtu) {
2747 xdst->route_mtu_cached = mtu;
2751 } while (dst->xfrm);
2756 mtu = last->child_mtu_cached;
2760 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2761 if (mtu > last->route_mtu_cached)
2762 mtu = last->route_mtu_cached;
2763 dst_metric_set(dst, RTAX_MTU, mtu);
2768 last = (struct xfrm_dst *)last->u.dst.next;
2769 last->child_mtu_cached = mtu;
2775 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2777 return dst_metric_advmss(dst->path);
2780 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2782 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2784 return mtu ? : dst_mtu(dst->path);
2787 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2788 struct sk_buff *skb,
2791 return dst->path->ops->neigh_lookup(dst, skb, daddr);
2794 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2797 if (unlikely(afinfo == NULL))
2799 if (unlikely(afinfo->family >= NPROTO))
2800 return -EAFNOSUPPORT;
2801 spin_lock(&xfrm_policy_afinfo_lock);
2802 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2805 struct dst_ops *dst_ops = afinfo->dst_ops;
2806 if (likely(dst_ops->kmem_cachep == NULL))
2807 dst_ops->kmem_cachep = xfrm_dst_cache;
2808 if (likely(dst_ops->check == NULL))
2809 dst_ops->check = xfrm_dst_check;
2810 if (likely(dst_ops->default_advmss == NULL))
2811 dst_ops->default_advmss = xfrm_default_advmss;
2812 if (likely(dst_ops->mtu == NULL))
2813 dst_ops->mtu = xfrm_mtu;
2814 if (likely(dst_ops->negative_advice == NULL))
2815 dst_ops->negative_advice = xfrm_negative_advice;
2816 if (likely(dst_ops->link_failure == NULL))
2817 dst_ops->link_failure = xfrm_link_failure;
2818 if (likely(dst_ops->neigh_lookup == NULL))
2819 dst_ops->neigh_lookup = xfrm_neigh_lookup;
2820 if (likely(afinfo->garbage_collect == NULL))
2821 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
2822 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
2824 spin_unlock(&xfrm_policy_afinfo_lock);
2828 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2830 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2833 if (unlikely(afinfo == NULL))
2835 if (unlikely(afinfo->family >= NPROTO))
2836 return -EAFNOSUPPORT;
2837 spin_lock(&xfrm_policy_afinfo_lock);
2838 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2839 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2842 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2845 spin_unlock(&xfrm_policy_afinfo_lock);
2847 struct dst_ops *dst_ops = afinfo->dst_ops;
2851 dst_ops->kmem_cachep = NULL;
2852 dst_ops->check = NULL;
2853 dst_ops->negative_advice = NULL;
2854 dst_ops->link_failure = NULL;
2855 afinfo->garbage_collect = NULL;
2859 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2861 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2863 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2867 xfrm_garbage_collect(dev_net(dev));
2872 static struct notifier_block xfrm_dev_notifier = {
2873 .notifier_call = xfrm_dev_event,
2876 #ifdef CONFIG_XFRM_STATISTICS
2877 static int __net_init xfrm_statistics_init(struct net *net)
2880 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2881 if (!net->mib.xfrm_statistics)
2883 rv = xfrm_proc_init(net);
2885 free_percpu(net->mib.xfrm_statistics);
2889 static void xfrm_statistics_fini(struct net *net)
2891 xfrm_proc_fini(net);
2892 free_percpu(net->mib.xfrm_statistics);
2895 static int __net_init xfrm_statistics_init(struct net *net)
2900 static void xfrm_statistics_fini(struct net *net)
2905 static int __net_init xfrm_policy_init(struct net *net)
2907 unsigned int hmask, sz;
2910 if (net_eq(net, &init_net))
2911 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2912 sizeof(struct xfrm_dst),
2913 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2917 sz = (hmask+1) * sizeof(struct hlist_head);
2919 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2920 if (!net->xfrm.policy_byidx)
2922 net->xfrm.policy_idx_hmask = hmask;
2924 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2925 struct xfrm_policy_hash *htab;
2927 net->xfrm.policy_count[dir] = 0;
2928 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
2929 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2931 htab = &net->xfrm.policy_bydst[dir];
2932 htab->table = xfrm_hash_alloc(sz);
2935 htab->hmask = hmask;
2941 net->xfrm.policy_hthresh.lbits4 = 32;
2942 net->xfrm.policy_hthresh.rbits4 = 32;
2943 net->xfrm.policy_hthresh.lbits6 = 128;
2944 net->xfrm.policy_hthresh.rbits6 = 128;
2946 seqlock_init(&net->xfrm.policy_hthresh.lock);
2948 INIT_LIST_HEAD(&net->xfrm.policy_all);
2949 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
2950 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
2951 if (net_eq(net, &init_net))
2952 register_netdevice_notifier(&xfrm_dev_notifier);
2956 for (dir--; dir >= 0; dir--) {
2957 struct xfrm_policy_hash *htab;
2959 htab = &net->xfrm.policy_bydst[dir];
2960 xfrm_hash_free(htab->table, sz);
2962 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2967 static void xfrm_policy_fini(struct net *net)
2972 flush_work(&net->xfrm.policy_hash_work);
2973 #ifdef CONFIG_XFRM_SUB_POLICY
2974 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
2976 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
2978 WARN_ON(!list_empty(&net->xfrm.policy_all));
2980 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2981 struct xfrm_policy_hash *htab;
2983 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
2985 htab = &net->xfrm.policy_bydst[dir];
2986 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
2987 WARN_ON(!hlist_empty(htab->table));
2988 xfrm_hash_free(htab->table, sz);
2991 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
2992 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2993 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2996 static int __net_init xfrm_net_init(struct net *net)
3000 /* Initialize the per-net locks here */
3001 spin_lock_init(&net->xfrm.xfrm_state_lock);
3002 rwlock_init(&net->xfrm.xfrm_policy_lock);
3003 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3005 rv = xfrm_statistics_init(net);
3007 goto out_statistics;
3008 rv = xfrm_state_init(net);
3011 rv = xfrm_policy_init(net);
3014 rv = xfrm_sysctl_init(net);
3017 rv = flow_cache_init(net);
3024 xfrm_sysctl_fini(net);
3026 xfrm_policy_fini(net);
3028 xfrm_state_fini(net);
3030 xfrm_statistics_fini(net);
3035 static void __net_exit xfrm_net_exit(struct net *net)
3037 flow_cache_fini(net);
3038 xfrm_sysctl_fini(net);
3039 xfrm_policy_fini(net);
3040 xfrm_state_fini(net);
3041 xfrm_statistics_fini(net);
3044 static struct pernet_operations __net_initdata xfrm_net_ops = {
3045 .init = xfrm_net_init,
3046 .exit = xfrm_net_exit,
3049 void __init xfrm_init(void)
3051 register_pernet_subsys(&xfrm_net_ops);
3055 #ifdef CONFIG_AUDITSYSCALL
3056 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3057 struct audit_buffer *audit_buf)
3059 struct xfrm_sec_ctx *ctx = xp->security;
3060 struct xfrm_selector *sel = &xp->selector;
3063 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3064 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3066 switch (sel->family) {
3068 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
3069 if (sel->prefixlen_s != 32)
3070 audit_log_format(audit_buf, " src_prefixlen=%d",
3072 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
3073 if (sel->prefixlen_d != 32)
3074 audit_log_format(audit_buf, " dst_prefixlen=%d",
3078 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
3079 if (sel->prefixlen_s != 128)
3080 audit_log_format(audit_buf, " src_prefixlen=%d",
3082 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
3083 if (sel->prefixlen_d != 128)
3084 audit_log_format(audit_buf, " dst_prefixlen=%d",
3090 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
3092 struct audit_buffer *audit_buf;
3094 audit_buf = xfrm_audit_start("SPD-add");
3095 if (audit_buf == NULL)
3097 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3098 audit_log_format(audit_buf, " res=%u", result);
3099 xfrm_audit_common_policyinfo(xp, audit_buf);
3100 audit_log_end(audit_buf);
3102 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3104 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
3107 struct audit_buffer *audit_buf;
3109 audit_buf = xfrm_audit_start("SPD-delete");
3110 if (audit_buf == NULL)
3112 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3113 audit_log_format(audit_buf, " res=%u", result);
3114 xfrm_audit_common_policyinfo(xp, audit_buf);
3115 audit_log_end(audit_buf);
3117 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3120 #ifdef CONFIG_XFRM_MIGRATE
3121 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3122 const struct xfrm_selector *sel_tgt)
3124 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3125 if (sel_tgt->family == sel_cmp->family &&
3126 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3128 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3130 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3131 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
3135 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
3142 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3143 u8 dir, u8 type, struct net *net)
3145 struct xfrm_policy *pol, *ret = NULL;
3146 struct hlist_head *chain;
3149 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
3150 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
3151 hlist_for_each_entry(pol, chain, bydst) {
3152 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3153 pol->type == type) {
3155 priority = ret->priority;
3159 chain = &net->xfrm.policy_inexact[dir];
3160 hlist_for_each_entry(pol, chain, bydst) {
3161 if ((pol->priority >= priority) && ret)
3164 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3165 pol->type == type) {
3173 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
3178 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3182 if (t->mode == m->mode && t->id.proto == m->proto &&
3183 (m->reqid == 0 || t->reqid == m->reqid)) {
3185 case XFRM_MODE_TUNNEL:
3186 case XFRM_MODE_BEET:
3187 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3189 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3194 case XFRM_MODE_TRANSPORT:
3195 /* in case of transport mode, template does not store
3196 any IP addresses, hence we just compare mode and
3207 /* update endpoint address(es) of template(s) */
3208 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3209 struct xfrm_migrate *m, int num_migrate)
3211 struct xfrm_migrate *mp;
3214 write_lock_bh(&pol->lock);
3215 if (unlikely(pol->walk.dead)) {
3216 /* target policy has been deleted */
3217 write_unlock_bh(&pol->lock);
3221 for (i = 0; i < pol->xfrm_nr; i++) {
3222 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3223 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3226 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3227 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3229 /* update endpoints */
3230 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3231 sizeof(pol->xfrm_vec[i].id.daddr));
3232 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3233 sizeof(pol->xfrm_vec[i].saddr));
3234 pol->xfrm_vec[i].encap_family = mp->new_family;
3236 atomic_inc(&pol->genid);
3240 write_unlock_bh(&pol->lock);
3248 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3252 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3255 for (i = 0; i < num_migrate; i++) {
3256 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3258 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3261 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3262 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3265 /* check if there is any duplicated entry */
3266 for (j = i + 1; j < num_migrate; j++) {
3267 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3268 sizeof(m[i].old_daddr)) &&
3269 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3270 sizeof(m[i].old_saddr)) &&
3271 m[i].proto == m[j].proto &&
3272 m[i].mode == m[j].mode &&
3273 m[i].reqid == m[j].reqid &&
3274 m[i].old_family == m[j].old_family)
3282 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3283 struct xfrm_migrate *m, int num_migrate,
3284 struct xfrm_kmaddress *k, struct net *net)
3286 int i, err, nx_cur = 0, nx_new = 0;
3287 struct xfrm_policy *pol = NULL;
3288 struct xfrm_state *x, *xc;
3289 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3290 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3291 struct xfrm_migrate *mp;
3293 /* Stage 0 - sanity checks */
3294 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3297 if (dir >= XFRM_POLICY_MAX) {
3302 /* Stage 1 - find policy */
3303 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
3308 /* Stage 2 - find and update state(s) */
3309 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3310 if ((x = xfrm_migrate_state_find(mp, net))) {
3313 if ((xc = xfrm_state_migrate(x, mp))) {
3323 /* Stage 3 - update policy */
3324 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3327 /* Stage 4 - delete old state(s) */
3329 xfrm_states_put(x_cur, nx_cur);
3330 xfrm_states_delete(x_cur, nx_cur);
3333 /* Stage 5 - announce */
3334 km_migrate(sel, dir, type, m, num_migrate, k);
3346 xfrm_states_put(x_cur, nx_cur);
3348 xfrm_states_delete(x_new, nx_new);
3352 EXPORT_SYMBOL(xfrm_migrate);