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 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
308 void xfrm_policy_destroy(struct xfrm_policy *policy)
310 BUG_ON(!policy->walk.dead);
312 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
315 security_xfrm_policy_free(policy->security);
318 EXPORT_SYMBOL(xfrm_policy_destroy);
320 /* Rule must be locked. Release descentant resources, announce
321 * entry dead. The rule must be unlinked from lists to the moment.
324 static void xfrm_policy_kill(struct xfrm_policy *policy)
326 policy->walk.dead = 1;
328 atomic_inc(&policy->genid);
330 if (del_timer(&policy->polq.hold_timer))
331 xfrm_pol_put(policy);
332 skb_queue_purge(&policy->polq.hold_queue);
334 if (del_timer(&policy->timer))
335 xfrm_pol_put(policy);
337 xfrm_pol_put(policy);
340 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
342 static inline unsigned int idx_hash(struct net *net, u32 index)
344 return __idx_hash(index, net->xfrm.policy_idx_hmask);
347 /* calculate policy hash thresholds */
348 static void __get_hash_thresh(struct net *net,
349 unsigned short family, int dir,
350 u8 *dbits, u8 *sbits)
354 *dbits = net->xfrm.policy_bydst[dir].dbits4;
355 *sbits = net->xfrm.policy_bydst[dir].sbits4;
359 *dbits = net->xfrm.policy_bydst[dir].dbits6;
360 *sbits = net->xfrm.policy_bydst[dir].sbits6;
369 static struct hlist_head *policy_hash_bysel(struct net *net,
370 const struct xfrm_selector *sel,
371 unsigned short family, int dir)
373 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
378 __get_hash_thresh(net, family, dir, &dbits, &sbits);
379 hash = __sel_hash(sel, family, hmask, dbits, sbits);
381 return (hash == hmask + 1 ?
382 &net->xfrm.policy_inexact[dir] :
383 net->xfrm.policy_bydst[dir].table + hash);
386 static struct hlist_head *policy_hash_direct(struct net *net,
387 const xfrm_address_t *daddr,
388 const xfrm_address_t *saddr,
389 unsigned short family, int dir)
391 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
396 __get_hash_thresh(net, family, dir, &dbits, &sbits);
397 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
399 return net->xfrm.policy_bydst[dir].table + hash;
402 static void xfrm_dst_hash_transfer(struct net *net,
403 struct hlist_head *list,
404 struct hlist_head *ndsttable,
405 unsigned int nhashmask,
408 struct hlist_node *tmp, *entry0 = NULL;
409 struct xfrm_policy *pol;
415 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
418 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
419 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
420 pol->family, nhashmask, dbits, sbits);
422 hlist_del(&pol->bydst);
423 hlist_add_head(&pol->bydst, ndsttable+h);
428 hlist_del(&pol->bydst);
429 hlist_add_behind(&pol->bydst, entry0);
431 entry0 = &pol->bydst;
433 if (!hlist_empty(list)) {
439 static void xfrm_idx_hash_transfer(struct hlist_head *list,
440 struct hlist_head *nidxtable,
441 unsigned int nhashmask)
443 struct hlist_node *tmp;
444 struct xfrm_policy *pol;
446 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
449 h = __idx_hash(pol->index, nhashmask);
450 hlist_add_head(&pol->byidx, nidxtable+h);
454 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
456 return ((old_hmask + 1) << 1) - 1;
459 static void xfrm_bydst_resize(struct net *net, int dir)
461 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
462 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
463 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
464 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
465 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
471 write_lock_bh(&net->xfrm.xfrm_policy_lock);
473 for (i = hmask; i >= 0; i--)
474 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
476 net->xfrm.policy_bydst[dir].table = ndst;
477 net->xfrm.policy_bydst[dir].hmask = nhashmask;
479 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
481 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
484 static void xfrm_byidx_resize(struct net *net, int total)
486 unsigned int hmask = net->xfrm.policy_idx_hmask;
487 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
488 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
489 struct hlist_head *oidx = net->xfrm.policy_byidx;
490 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
496 write_lock_bh(&net->xfrm.xfrm_policy_lock);
498 for (i = hmask; i >= 0; i--)
499 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
501 net->xfrm.policy_byidx = nidx;
502 net->xfrm.policy_idx_hmask = nhashmask;
504 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
506 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
509 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
511 unsigned int cnt = net->xfrm.policy_count[dir];
512 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
517 if ((hmask + 1) < xfrm_policy_hashmax &&
524 static inline int xfrm_byidx_should_resize(struct net *net, int total)
526 unsigned int hmask = net->xfrm.policy_idx_hmask;
528 if ((hmask + 1) < xfrm_policy_hashmax &&
535 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
537 read_lock_bh(&net->xfrm.xfrm_policy_lock);
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;
546 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
548 EXPORT_SYMBOL(xfrm_spd_getinfo);
550 static DEFINE_MUTEX(hash_resize_mutex);
551 static void xfrm_hash_resize(struct work_struct *work)
553 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
556 mutex_lock(&hash_resize_mutex);
559 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
560 if (xfrm_bydst_should_resize(net, dir, &total))
561 xfrm_bydst_resize(net, dir);
563 if (xfrm_byidx_should_resize(net, total))
564 xfrm_byidx_resize(net, total);
566 mutex_unlock(&hash_resize_mutex);
569 static void xfrm_hash_rebuild(struct work_struct *work)
571 struct net *net = container_of(work, struct net,
572 xfrm.policy_hthresh.work);
574 struct xfrm_policy *pol;
575 struct xfrm_policy *policy;
576 struct hlist_head *chain;
577 struct hlist_head *odst;
578 struct hlist_node *newpos;
582 u8 lbits4, rbits4, lbits6, rbits6;
584 mutex_lock(&hash_resize_mutex);
586 /* read selector prefixlen thresholds */
588 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
590 lbits4 = net->xfrm.policy_hthresh.lbits4;
591 rbits4 = net->xfrm.policy_hthresh.rbits4;
592 lbits6 = net->xfrm.policy_hthresh.lbits6;
593 rbits6 = net->xfrm.policy_hthresh.rbits6;
594 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
596 write_lock_bh(&net->xfrm.xfrm_policy_lock);
598 /* reset the bydst and inexact table in all directions */
599 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
600 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
601 hmask = net->xfrm.policy_bydst[dir].hmask;
602 odst = net->xfrm.policy_bydst[dir].table;
603 for (i = hmask; i >= 0; i--)
604 INIT_HLIST_HEAD(odst + i);
605 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
606 /* dir out => dst = remote, src = local */
607 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
608 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
609 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
610 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
612 /* dir in/fwd => dst = local, src = remote */
613 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
614 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
615 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
616 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
620 /* re-insert all policies by order of creation */
621 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
623 chain = policy_hash_bysel(net, &policy->selector,
625 xfrm_policy_id2dir(policy->index));
626 hlist_for_each_entry(pol, chain, bydst) {
627 if (policy->priority >= pol->priority)
628 newpos = &pol->bydst;
633 hlist_add_behind(&policy->bydst, newpos);
635 hlist_add_head(&policy->bydst, chain);
638 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
640 mutex_unlock(&hash_resize_mutex);
643 void xfrm_policy_hash_rebuild(struct net *net)
645 schedule_work(&net->xfrm.policy_hthresh.work);
647 EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
649 /* Generate new index... KAME seems to generate them ordered by cost
650 * of an absolute inpredictability of ordering of rules. This will not pass. */
651 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
653 static u32 idx_generator;
656 struct hlist_head *list;
657 struct xfrm_policy *p;
662 idx = (idx_generator | dir);
671 list = net->xfrm.policy_byidx + idx_hash(net, idx);
673 hlist_for_each_entry(p, list, byidx) {
674 if (p->index == idx) {
684 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
686 u32 *p1 = (u32 *) s1;
687 u32 *p2 = (u32 *) s2;
688 int len = sizeof(struct xfrm_selector) / sizeof(u32);
691 for (i = 0; i < len; i++) {
699 static void xfrm_policy_requeue(struct xfrm_policy *old,
700 struct xfrm_policy *new)
702 struct xfrm_policy_queue *pq = &old->polq;
703 struct sk_buff_head list;
705 if (skb_queue_empty(&pq->hold_queue))
708 __skb_queue_head_init(&list);
710 spin_lock_bh(&pq->hold_queue.lock);
711 skb_queue_splice_init(&pq->hold_queue, &list);
712 if (del_timer(&pq->hold_timer))
714 spin_unlock_bh(&pq->hold_queue.lock);
718 spin_lock_bh(&pq->hold_queue.lock);
719 skb_queue_splice(&list, &pq->hold_queue);
720 pq->timeout = XFRM_QUEUE_TMO_MIN;
721 if (!mod_timer(&pq->hold_timer, jiffies))
723 spin_unlock_bh(&pq->hold_queue.lock);
726 static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
727 struct xfrm_policy *pol)
729 u32 mark = policy->mark.v & policy->mark.m;
731 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
734 if ((mark & pol->mark.m) == pol->mark.v &&
735 policy->priority == pol->priority)
741 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
743 struct net *net = xp_net(policy);
744 struct xfrm_policy *pol;
745 struct xfrm_policy *delpol;
746 struct hlist_head *chain;
747 struct hlist_node *newpos;
749 write_lock_bh(&net->xfrm.xfrm_policy_lock);
750 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
753 hlist_for_each_entry(pol, chain, bydst) {
754 if (pol->type == policy->type &&
755 !selector_cmp(&pol->selector, &policy->selector) &&
756 xfrm_policy_mark_match(policy, pol) &&
757 xfrm_sec_ctx_match(pol->security, policy->security) &&
760 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
764 if (policy->priority > pol->priority)
766 } else if (policy->priority >= pol->priority) {
767 newpos = &pol->bydst;
774 hlist_add_behind(&policy->bydst, newpos);
776 hlist_add_head(&policy->bydst, chain);
777 __xfrm_policy_link(policy, dir);
778 atomic_inc(&net->xfrm.flow_cache_genid);
780 /* After previous checking, family can either be AF_INET or AF_INET6 */
781 if (policy->family == AF_INET)
782 rt_genid_bump_ipv4(net);
784 rt_genid_bump_ipv6(net);
787 xfrm_policy_requeue(delpol, policy);
788 __xfrm_policy_unlink(delpol, dir);
790 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
791 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
792 policy->curlft.add_time = get_seconds();
793 policy->curlft.use_time = 0;
794 if (!mod_timer(&policy->timer, jiffies + HZ))
795 xfrm_pol_hold(policy);
796 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
799 xfrm_policy_kill(delpol);
800 else if (xfrm_bydst_should_resize(net, dir, NULL))
801 schedule_work(&net->xfrm.policy_hash_work);
805 EXPORT_SYMBOL(xfrm_policy_insert);
807 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
808 int dir, struct xfrm_selector *sel,
809 struct xfrm_sec_ctx *ctx, int delete,
812 struct xfrm_policy *pol, *ret;
813 struct hlist_head *chain;
816 write_lock_bh(&net->xfrm.xfrm_policy_lock);
817 chain = policy_hash_bysel(net, sel, sel->family, dir);
819 hlist_for_each_entry(pol, chain, bydst) {
820 if (pol->type == type &&
821 (mark & pol->mark.m) == pol->mark.v &&
822 !selector_cmp(sel, &pol->selector) &&
823 xfrm_sec_ctx_match(ctx, pol->security)) {
826 *err = security_xfrm_policy_delete(
829 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
832 __xfrm_policy_unlink(pol, dir);
838 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
841 xfrm_policy_kill(ret);
844 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
846 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
847 int dir, u32 id, int delete, int *err)
849 struct xfrm_policy *pol, *ret;
850 struct hlist_head *chain;
853 if (xfrm_policy_id2dir(id) != dir)
857 write_lock_bh(&net->xfrm.xfrm_policy_lock);
858 chain = net->xfrm.policy_byidx + idx_hash(net, id);
860 hlist_for_each_entry(pol, chain, byidx) {
861 if (pol->type == type && pol->index == id &&
862 (mark & pol->mark.m) == pol->mark.v) {
865 *err = security_xfrm_policy_delete(
868 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
871 __xfrm_policy_unlink(pol, dir);
877 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
880 xfrm_policy_kill(ret);
883 EXPORT_SYMBOL(xfrm_policy_byid);
885 #ifdef CONFIG_SECURITY_NETWORK_XFRM
887 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
891 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
892 struct xfrm_policy *pol;
895 hlist_for_each_entry(pol,
896 &net->xfrm.policy_inexact[dir], bydst) {
897 if (pol->type != type)
899 err = security_xfrm_policy_delete(pol->security);
901 xfrm_audit_policy_delete(pol, 0, task_valid);
905 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
906 hlist_for_each_entry(pol,
907 net->xfrm.policy_bydst[dir].table + i,
909 if (pol->type != type)
911 err = security_xfrm_policy_delete(
914 xfrm_audit_policy_delete(pol, 0,
925 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
931 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
933 int dir, err = 0, cnt = 0;
935 write_lock_bh(&net->xfrm.xfrm_policy_lock);
937 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
941 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
942 struct xfrm_policy *pol;
946 hlist_for_each_entry(pol,
947 &net->xfrm.policy_inexact[dir], bydst) {
948 if (pol->type != type)
950 __xfrm_policy_unlink(pol, dir);
951 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
954 xfrm_audit_policy_delete(pol, 1, task_valid);
956 xfrm_policy_kill(pol);
958 write_lock_bh(&net->xfrm.xfrm_policy_lock);
962 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
964 hlist_for_each_entry(pol,
965 net->xfrm.policy_bydst[dir].table + i,
967 if (pol->type != type)
969 __xfrm_policy_unlink(pol, dir);
970 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
973 xfrm_audit_policy_delete(pol, 1, task_valid);
974 xfrm_policy_kill(pol);
976 write_lock_bh(&net->xfrm.xfrm_policy_lock);
985 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
988 EXPORT_SYMBOL(xfrm_policy_flush);
990 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
991 int (*func)(struct xfrm_policy *, int, int, void*),
994 struct xfrm_policy *pol;
995 struct xfrm_policy_walk_entry *x;
998 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
999 walk->type != XFRM_POLICY_TYPE_ANY)
1002 if (list_empty(&walk->walk.all) && walk->seq != 0)
1005 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1006 if (list_empty(&walk->walk.all))
1007 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
1009 x = list_first_entry(&walk->walk.all,
1010 struct xfrm_policy_walk_entry, all);
1012 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
1015 pol = container_of(x, struct xfrm_policy, walk);
1016 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1017 walk->type != pol->type)
1019 error = func(pol, xfrm_policy_id2dir(pol->index),
1022 list_move_tail(&walk->walk.all, &x->all);
1027 if (walk->seq == 0) {
1031 list_del_init(&walk->walk.all);
1033 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1036 EXPORT_SYMBOL(xfrm_policy_walk);
1038 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1040 INIT_LIST_HEAD(&walk->walk.all);
1041 walk->walk.dead = 1;
1045 EXPORT_SYMBOL(xfrm_policy_walk_init);
1047 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
1049 if (list_empty(&walk->walk.all))
1052 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
1053 list_del(&walk->walk.all);
1054 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1056 EXPORT_SYMBOL(xfrm_policy_walk_done);
1059 * Find policy to apply to this flow.
1061 * Returns 0 if policy found, else an -errno.
1063 static int xfrm_policy_match(const struct xfrm_policy *pol,
1064 const struct flowi *fl,
1065 u8 type, u16 family, int dir)
1067 const struct xfrm_selector *sel = &pol->selector;
1071 if (pol->family != family ||
1072 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
1076 match = xfrm_selector_match(sel, fl, family);
1078 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1084 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
1085 const struct flowi *fl,
1089 struct xfrm_policy *pol, *ret;
1090 const xfrm_address_t *daddr, *saddr;
1091 struct hlist_head *chain;
1094 daddr = xfrm_flowi_daddr(fl, family);
1095 saddr = xfrm_flowi_saddr(fl, family);
1096 if (unlikely(!daddr || !saddr))
1099 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1100 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1102 hlist_for_each_entry(pol, chain, bydst) {
1103 err = xfrm_policy_match(pol, fl, type, family, dir);
1113 priority = ret->priority;
1117 chain = &net->xfrm.policy_inexact[dir];
1118 hlist_for_each_entry(pol, chain, bydst) {
1119 if ((pol->priority >= priority) && ret)
1122 err = xfrm_policy_match(pol, fl, type, family, dir);
1138 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1143 static struct xfrm_policy *
1144 __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
1146 #ifdef CONFIG_XFRM_SUB_POLICY
1147 struct xfrm_policy *pol;
1149 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1153 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1156 static int flow_to_policy_dir(int dir)
1158 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1159 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1160 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1166 return XFRM_POLICY_IN;
1168 return XFRM_POLICY_OUT;
1170 return XFRM_POLICY_FWD;
1174 static struct flow_cache_object *
1175 xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
1176 u8 dir, struct flow_cache_object *old_obj, void *ctx)
1178 struct xfrm_policy *pol;
1181 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
1183 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
1184 if (IS_ERR_OR_NULL(pol))
1185 return ERR_CAST(pol);
1187 /* Resolver returns two references:
1188 * one for cache and one for caller of flow_cache_lookup() */
1194 static inline int policy_to_flow_dir(int dir)
1196 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1197 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1198 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1202 case XFRM_POLICY_IN:
1204 case XFRM_POLICY_OUT:
1205 return FLOW_DIR_OUT;
1206 case XFRM_POLICY_FWD:
1207 return FLOW_DIR_FWD;
1211 static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
1212 const struct flowi *fl)
1214 struct xfrm_policy *pol;
1215 struct net *net = sock_net(sk);
1217 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1218 if ((pol = sk->sk_policy[dir]) != NULL) {
1219 bool match = xfrm_selector_match(&pol->selector, fl,
1224 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1228 err = security_xfrm_policy_lookup(pol->security,
1230 policy_to_flow_dir(dir));
1233 else if (err == -ESRCH)
1241 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1245 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1247 struct net *net = xp_net(pol);
1249 list_add(&pol->walk.all, &net->xfrm.policy_all);
1250 net->xfrm.policy_count[dir]++;
1254 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1257 struct net *net = xp_net(pol);
1259 if (list_empty(&pol->walk.all))
1262 /* Socket policies are not hashed. */
1263 if (!hlist_unhashed(&pol->bydst)) {
1264 hlist_del(&pol->bydst);
1265 hlist_del(&pol->byidx);
1268 list_del_init(&pol->walk.all);
1269 net->xfrm.policy_count[dir]--;
1274 static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1276 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1279 static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1281 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1284 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1286 struct net *net = xp_net(pol);
1288 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1289 pol = __xfrm_policy_unlink(pol, dir);
1290 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1292 xfrm_policy_kill(pol);
1297 EXPORT_SYMBOL(xfrm_policy_delete);
1299 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1301 struct net *net = xp_net(pol);
1302 struct xfrm_policy *old_pol;
1304 #ifdef CONFIG_XFRM_SUB_POLICY
1305 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1309 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1310 old_pol = sk->sk_policy[dir];
1311 sk->sk_policy[dir] = pol;
1313 pol->curlft.add_time = get_seconds();
1314 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
1315 xfrm_sk_policy_link(pol, dir);
1319 xfrm_policy_requeue(old_pol, pol);
1321 /* Unlinking succeeds always. This is the only function
1322 * allowed to delete or replace socket policy.
1324 xfrm_sk_policy_unlink(old_pol, dir);
1326 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1329 xfrm_policy_kill(old_pol);
1334 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1336 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1337 struct net *net = xp_net(old);
1340 newp->selector = old->selector;
1341 if (security_xfrm_policy_clone(old->security,
1344 return NULL; /* ENOMEM */
1346 newp->lft = old->lft;
1347 newp->curlft = old->curlft;
1348 newp->mark = old->mark;
1349 newp->action = old->action;
1350 newp->flags = old->flags;
1351 newp->xfrm_nr = old->xfrm_nr;
1352 newp->index = old->index;
1353 newp->type = old->type;
1354 memcpy(newp->xfrm_vec, old->xfrm_vec,
1355 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1356 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1357 xfrm_sk_policy_link(newp, dir);
1358 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1364 int __xfrm_sk_clone_policy(struct sock *sk)
1366 struct xfrm_policy *p0 = sk->sk_policy[0],
1367 *p1 = sk->sk_policy[1];
1369 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1370 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1372 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1378 xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1379 xfrm_address_t *remote, unsigned short family)
1382 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1384 if (unlikely(afinfo == NULL))
1386 err = afinfo->get_saddr(net, oif, local, remote);
1387 xfrm_policy_put_afinfo(afinfo);
1391 /* Resolve list of templates for the flow, given policy. */
1394 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1395 struct xfrm_state **xfrm, unsigned short family)
1397 struct net *net = xp_net(policy);
1400 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1401 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1404 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1405 struct xfrm_state *x;
1406 xfrm_address_t *remote = daddr;
1407 xfrm_address_t *local = saddr;
1408 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1410 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1411 tmpl->mode == XFRM_MODE_BEET) {
1412 remote = &tmpl->id.daddr;
1413 local = &tmpl->saddr;
1414 if (xfrm_addr_any(local, tmpl->encap_family)) {
1415 error = xfrm_get_saddr(net, fl->flowi_oif,
1417 tmpl->encap_family);
1424 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1426 if (x && x->km.state == XFRM_STATE_VALID) {
1433 error = (x->km.state == XFRM_STATE_ERROR ?
1436 } else if (error == -ESRCH) {
1440 if (!tmpl->optional)
1446 for (nx--; nx >= 0; nx--)
1447 xfrm_state_put(xfrm[nx]);
1452 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1453 struct xfrm_state **xfrm, unsigned short family)
1455 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1456 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1462 for (i = 0; i < npols; i++) {
1463 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1468 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1476 /* found states are sorted for outbound processing */
1478 xfrm_state_sort(xfrm, tpp, cnx, family);
1483 for (cnx--; cnx >= 0; cnx--)
1484 xfrm_state_put(tpp[cnx]);
1489 /* Check that the bundle accepts the flow and its components are
1493 static inline int xfrm_get_tos(const struct flowi *fl, int family)
1495 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1501 tos = afinfo->get_tos(fl);
1503 xfrm_policy_put_afinfo(afinfo);
1508 static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1510 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1511 struct dst_entry *dst = &xdst->u.dst;
1513 if (xdst->route == NULL) {
1514 /* Dummy bundle - if it has xfrms we were not
1515 * able to build bundle as template resolution failed.
1516 * It means we need to try again resolving. */
1517 if (xdst->num_xfrms > 0)
1519 } else if (dst->flags & DST_XFRM_QUEUE) {
1523 if (stale_bundle(dst))
1531 static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1533 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1534 struct dst_entry *dst = &xdst->u.dst;
1538 if (stale_bundle(dst))
1544 static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1546 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1547 struct dst_entry *dst = &xdst->u.dst;
1552 static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1553 .get = xfrm_bundle_flo_get,
1554 .check = xfrm_bundle_flo_check,
1555 .delete = xfrm_bundle_flo_delete,
1558 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1560 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1561 struct dst_ops *dst_ops;
1562 struct xfrm_dst *xdst;
1565 return ERR_PTR(-EINVAL);
1569 dst_ops = &net->xfrm.xfrm4_dst_ops;
1571 #if IS_ENABLED(CONFIG_IPV6)
1573 dst_ops = &net->xfrm.xfrm6_dst_ops;
1579 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
1582 struct dst_entry *dst = &xdst->u.dst;
1584 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1585 xdst->flo.ops = &xfrm_bundle_fc_ops;
1587 xdst = ERR_PTR(-ENOBUFS);
1589 xfrm_policy_put_afinfo(afinfo);
1594 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1597 struct xfrm_policy_afinfo *afinfo =
1598 xfrm_policy_get_afinfo(dst->ops->family);
1604 err = afinfo->init_path(path, dst, nfheader_len);
1606 xfrm_policy_put_afinfo(afinfo);
1611 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1612 const struct flowi *fl)
1614 struct xfrm_policy_afinfo *afinfo =
1615 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1621 err = afinfo->fill_dst(xdst, dev, fl);
1623 xfrm_policy_put_afinfo(afinfo);
1629 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1630 * all the metrics... Shortly, bundle a bundle.
1633 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1634 struct xfrm_state **xfrm, int nx,
1635 const struct flowi *fl,
1636 struct dst_entry *dst)
1638 struct net *net = xp_net(policy);
1639 unsigned long now = jiffies;
1640 struct net_device *dev;
1641 struct xfrm_mode *inner_mode;
1642 struct dst_entry *dst_prev = NULL;
1643 struct dst_entry *dst0 = NULL;
1647 int nfheader_len = 0;
1648 int trailer_len = 0;
1650 int family = policy->selector.family;
1651 xfrm_address_t saddr, daddr;
1653 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1655 tos = xfrm_get_tos(fl, family);
1662 for (; i < nx; i++) {
1663 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1664 struct dst_entry *dst1 = &xdst->u.dst;
1666 err = PTR_ERR(xdst);
1672 if (xfrm[i]->sel.family == AF_UNSPEC) {
1673 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1674 xfrm_af2proto(family));
1676 err = -EAFNOSUPPORT;
1681 inner_mode = xfrm[i]->inner_mode;
1686 dst_prev->child = dst_clone(dst1);
1687 dst1->flags |= DST_NOHASH;
1691 dst_copy_metrics(dst1, dst);
1693 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1694 family = xfrm[i]->props.family;
1695 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1696 &saddr, &daddr, family);
1703 dst1->xfrm = xfrm[i];
1704 xdst->xfrm_genid = xfrm[i]->genid;
1706 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1707 dst1->flags |= DST_HOST;
1708 dst1->lastuse = now;
1710 dst1->input = dst_discard;
1711 dst1->output = inner_mode->afinfo->output;
1713 dst1->next = dst_prev;
1716 header_len += xfrm[i]->props.header_len;
1717 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1718 nfheader_len += xfrm[i]->props.header_len;
1719 trailer_len += xfrm[i]->props.trailer_len;
1722 dst_prev->child = dst;
1730 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
1731 xfrm_init_pmtu(dst_prev);
1733 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1734 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1736 err = xfrm_fill_dst(xdst, dev, fl);
1740 dst_prev->header_len = header_len;
1741 dst_prev->trailer_len = trailer_len;
1742 header_len -= xdst->u.dst.xfrm->props.header_len;
1743 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1751 xfrm_state_put(xfrm[i]);
1755 dst0 = ERR_PTR(err);
1759 #ifdef CONFIG_XFRM_SUB_POLICY
1760 static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
1763 *target = kmalloc(size, GFP_ATOMIC);
1768 memcpy(*target, src, size);
1773 static int xfrm_dst_update_parent(struct dst_entry *dst,
1774 const struct xfrm_selector *sel)
1776 #ifdef CONFIG_XFRM_SUB_POLICY
1777 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1778 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1785 static int xfrm_dst_update_origin(struct dst_entry *dst,
1786 const struct flowi *fl)
1788 #ifdef CONFIG_XFRM_SUB_POLICY
1789 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1790 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1796 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1797 struct xfrm_policy **pols,
1798 int *num_pols, int *num_xfrms)
1802 if (*num_pols == 0 || !pols[0]) {
1807 if (IS_ERR(pols[0]))
1808 return PTR_ERR(pols[0]);
1810 *num_xfrms = pols[0]->xfrm_nr;
1812 #ifdef CONFIG_XFRM_SUB_POLICY
1813 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1814 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1815 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1816 XFRM_POLICY_TYPE_MAIN,
1820 if (IS_ERR(pols[1])) {
1821 xfrm_pols_put(pols, *num_pols);
1822 return PTR_ERR(pols[1]);
1825 (*num_xfrms) += pols[1]->xfrm_nr;
1829 for (i = 0; i < *num_pols; i++) {
1830 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1840 static struct xfrm_dst *
1841 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1842 const struct flowi *fl, u16 family,
1843 struct dst_entry *dst_orig)
1845 struct net *net = xp_net(pols[0]);
1846 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1847 struct dst_entry *dst;
1848 struct xfrm_dst *xdst;
1851 /* Try to instantiate a bundle */
1852 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1854 if (err != 0 && err != -EAGAIN)
1855 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1856 return ERR_PTR(err);
1859 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1861 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1862 return ERR_CAST(dst);
1865 xdst = (struct xfrm_dst *)dst;
1866 xdst->num_xfrms = err;
1868 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1870 err = xfrm_dst_update_origin(dst, fl);
1871 if (unlikely(err)) {
1873 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1874 return ERR_PTR(err);
1877 xdst->num_pols = num_pols;
1878 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
1879 xdst->policy_genid = atomic_read(&pols[0]->genid);
1884 static void xfrm_policy_queue_process(unsigned long arg)
1886 struct sk_buff *skb;
1888 struct dst_entry *dst;
1889 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1890 struct xfrm_policy_queue *pq = &pol->polq;
1892 struct sk_buff_head list;
1894 spin_lock(&pq->hold_queue.lock);
1895 skb = skb_peek(&pq->hold_queue);
1897 spin_unlock(&pq->hold_queue.lock);
1902 xfrm_decode_session(skb, &fl, dst->ops->family);
1903 spin_unlock(&pq->hold_queue.lock);
1905 dst_hold(dst->path);
1906 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1911 if (dst->flags & DST_XFRM_QUEUE) {
1914 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1917 pq->timeout = pq->timeout << 1;
1918 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1925 __skb_queue_head_init(&list);
1927 spin_lock(&pq->hold_queue.lock);
1929 skb_queue_splice_init(&pq->hold_queue, &list);
1930 spin_unlock(&pq->hold_queue.lock);
1932 while (!skb_queue_empty(&list)) {
1933 skb = __skb_dequeue(&list);
1935 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1936 dst_hold(skb_dst(skb)->path);
1937 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1946 skb_dst_set(skb, dst);
1948 dst_output(skb->sk, skb);
1957 skb_queue_purge(&pq->hold_queue);
1961 static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
1963 unsigned long sched_next;
1964 struct dst_entry *dst = skb_dst(skb);
1965 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1966 struct xfrm_policy *pol = xdst->pols[0];
1967 struct xfrm_policy_queue *pq = &pol->polq;
1969 if (unlikely(skb_fclone_busy(sk, skb))) {
1974 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1981 spin_lock_bh(&pq->hold_queue.lock);
1984 pq->timeout = XFRM_QUEUE_TMO_MIN;
1986 sched_next = jiffies + pq->timeout;
1988 if (del_timer(&pq->hold_timer)) {
1989 if (time_before(pq->hold_timer.expires, sched_next))
1990 sched_next = pq->hold_timer.expires;
1994 __skb_queue_tail(&pq->hold_queue, skb);
1995 if (!mod_timer(&pq->hold_timer, sched_next))
1998 spin_unlock_bh(&pq->hold_queue.lock);
2003 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
2004 struct xfrm_flo *xflo,
2005 const struct flowi *fl,
2010 struct net_device *dev;
2011 struct dst_entry *dst;
2012 struct dst_entry *dst1;
2013 struct xfrm_dst *xdst;
2015 xdst = xfrm_alloc_dst(net, family);
2019 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2020 net->xfrm.sysctl_larval_drop ||
2024 dst = xflo->dst_orig;
2025 dst1 = &xdst->u.dst;
2029 dst_copy_metrics(dst1, dst);
2031 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2032 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2033 dst1->lastuse = jiffies;
2035 dst1->input = dst_discard;
2036 dst1->output = xdst_queue_output;
2042 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2049 err = xfrm_fill_dst(xdst, dev, fl);
2058 xdst = ERR_PTR(err);
2062 static struct flow_cache_object *
2063 xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
2064 struct flow_cache_object *oldflo, void *ctx)
2066 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
2067 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2068 struct xfrm_dst *xdst, *new_xdst;
2069 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2071 /* Check if the policies from old bundle are usable */
2074 xdst = container_of(oldflo, struct xfrm_dst, flo);
2075 num_pols = xdst->num_pols;
2076 num_xfrms = xdst->num_xfrms;
2078 for (i = 0; i < num_pols; i++) {
2079 pols[i] = xdst->pols[i];
2080 pol_dead |= pols[i]->walk.dead;
2083 dst_free(&xdst->u.dst);
2091 /* Resolve policies to use if we couldn't get them from
2092 * previous cache entry */
2095 pols[0] = __xfrm_policy_lookup(net, fl, family,
2096 flow_to_policy_dir(dir));
2097 err = xfrm_expand_policies(fl, family, pols,
2098 &num_pols, &num_xfrms);
2104 goto make_dummy_bundle;
2107 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2109 if (IS_ERR(new_xdst)) {
2110 err = PTR_ERR(new_xdst);
2114 goto make_dummy_bundle;
2115 dst_hold(&xdst->u.dst);
2117 } else if (new_xdst == NULL) {
2120 goto make_dummy_bundle;
2121 xdst->num_xfrms = 0;
2122 dst_hold(&xdst->u.dst);
2126 /* Kill the previous bundle */
2128 /* The policies were stolen for newly generated bundle */
2130 dst_free(&xdst->u.dst);
2133 /* Flow cache does not have reference, it dst_free()'s,
2134 * but we do need to return one reference for original caller */
2135 dst_hold(&new_xdst->u.dst);
2136 return &new_xdst->flo;
2139 /* We found policies, but there's no bundles to instantiate:
2140 * either because the policy blocks, has no transformations or
2141 * we could not build template (no xfrm_states).*/
2142 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
2144 xfrm_pols_put(pols, num_pols);
2145 return ERR_CAST(xdst);
2147 xdst->num_pols = num_pols;
2148 xdst->num_xfrms = num_xfrms;
2149 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2151 dst_hold(&xdst->u.dst);
2155 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2158 dst_free(&xdst->u.dst);
2160 xfrm_pols_put(pols, num_pols);
2161 return ERR_PTR(err);
2164 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2165 struct dst_entry *dst_orig)
2167 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2168 struct dst_entry *ret;
2171 dst_release(dst_orig);
2172 return ERR_PTR(-EINVAL);
2174 ret = afinfo->blackhole_route(net, dst_orig);
2176 xfrm_policy_put_afinfo(afinfo);
2181 /* Main function: finds/creates a bundle for given flow.
2183 * At the moment we eat a raw IP route. Mostly to speed up lookups
2184 * on interfaces with disabled IPsec.
2186 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2187 const struct flowi *fl,
2188 const struct sock *sk, int flags)
2190 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2191 struct flow_cache_object *flo;
2192 struct xfrm_dst *xdst;
2193 struct dst_entry *dst, *route;
2194 u16 family = dst_orig->ops->family;
2195 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
2196 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2202 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2204 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2205 err = xfrm_expand_policies(fl, family, pols,
2206 &num_pols, &num_xfrms);
2211 if (num_xfrms <= 0) {
2212 drop_pols = num_pols;
2216 xdst = xfrm_resolve_and_create_bundle(
2220 xfrm_pols_put(pols, num_pols);
2221 err = PTR_ERR(xdst);
2223 } else if (xdst == NULL) {
2225 drop_pols = num_pols;
2229 dst_hold(&xdst->u.dst);
2230 xdst->u.dst.flags |= DST_NOCACHE;
2231 route = xdst->route;
2236 struct xfrm_flo xflo;
2238 xflo.dst_orig = dst_orig;
2241 /* To accelerate a bit... */
2242 if ((dst_orig->flags & DST_NOXFRM) ||
2243 !net->xfrm.policy_count[XFRM_POLICY_OUT])
2246 flo = flow_cache_lookup(net, fl, family, dir,
2247 xfrm_bundle_lookup, &xflo);
2254 xdst = container_of(flo, struct xfrm_dst, flo);
2256 num_pols = xdst->num_pols;
2257 num_xfrms = xdst->num_xfrms;
2258 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
2259 route = xdst->route;
2263 if (route == NULL && num_xfrms > 0) {
2264 /* The only case when xfrm_bundle_lookup() returns a
2265 * bundle with null route, is when the template could
2266 * not be resolved. It means policies are there, but
2267 * bundle could not be created, since we don't yet
2268 * have the xfrm_state's. We need to wait for KM to
2269 * negotiate new SA's or bail out with error.*/
2270 if (net->xfrm.sysctl_larval_drop) {
2271 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2278 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2286 if ((flags & XFRM_LOOKUP_ICMP) &&
2287 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2292 for (i = 0; i < num_pols; i++)
2293 pols[i]->curlft.use_time = get_seconds();
2295 if (num_xfrms < 0) {
2296 /* Prohibit the flow */
2297 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2300 } else if (num_xfrms > 0) {
2301 /* Flow transformed */
2302 dst_release(dst_orig);
2304 /* Flow passes untransformed */
2309 xfrm_pols_put(pols, drop_pols);
2310 if (dst && dst->xfrm &&
2311 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2312 dst->flags |= DST_XFRM_TUNNEL;
2316 if (!(flags & XFRM_LOOKUP_ICMP)) {
2324 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2325 dst_release(dst_orig);
2326 xfrm_pols_put(pols, drop_pols);
2327 return ERR_PTR(err);
2329 EXPORT_SYMBOL(xfrm_lookup);
2331 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2332 * Otherwise we may send out blackholed packets.
2334 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2335 const struct flowi *fl,
2336 const struct sock *sk, int flags)
2338 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2339 flags | XFRM_LOOKUP_QUEUE |
2340 XFRM_LOOKUP_KEEP_DST_REF);
2342 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2343 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2347 EXPORT_SYMBOL(xfrm_lookup_route);
2350 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2352 struct xfrm_state *x;
2354 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2356 x = skb->sp->xvec[idx];
2357 if (!x->type->reject)
2359 return x->type->reject(x, skb, fl);
2362 /* When skb is transformed back to its "native" form, we have to
2363 * check policy restrictions. At the moment we make this in maximally
2364 * stupid way. Shame on me. :-) Of course, connected sockets must
2365 * have policy cached at them.
2369 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2370 unsigned short family)
2372 if (xfrm_state_kern(x))
2373 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2374 return x->id.proto == tmpl->id.proto &&
2375 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2376 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2377 x->props.mode == tmpl->mode &&
2378 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2379 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2380 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2381 xfrm_state_addr_cmp(tmpl, x, family));
2385 * 0 or more than 0 is returned when validation is succeeded (either bypass
2386 * because of optional transport mode, or next index of the mathced secpath
2387 * state with the template.
2388 * -1 is returned when no matching template is found.
2389 * Otherwise "-2 - errored_index" is returned.
2392 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2393 unsigned short family)
2397 if (tmpl->optional) {
2398 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2402 for (; idx < sp->len; idx++) {
2403 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2405 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2414 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2415 unsigned int family, int reverse)
2417 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2420 if (unlikely(afinfo == NULL))
2421 return -EAFNOSUPPORT;
2423 afinfo->decode_session(skb, fl, reverse);
2424 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2425 xfrm_policy_put_afinfo(afinfo);
2428 EXPORT_SYMBOL(__xfrm_decode_session);
2430 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2432 for (; k < sp->len; k++) {
2433 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2442 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2443 unsigned short family)
2445 struct net *net = dev_net(skb->dev);
2446 struct xfrm_policy *pol;
2447 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2456 reverse = dir & ~XFRM_POLICY_MASK;
2457 dir &= XFRM_POLICY_MASK;
2458 fl_dir = policy_to_flow_dir(dir);
2460 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2461 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2465 nf_nat_decode_session(skb, &fl, family);
2467 /* First, check used SA against their selectors. */
2471 for (i = skb->sp->len-1; i >= 0; i--) {
2472 struct xfrm_state *x = skb->sp->xvec[i];
2473 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2474 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2481 if (sk && sk->sk_policy[dir]) {
2482 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
2484 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2490 struct flow_cache_object *flo;
2492 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2493 xfrm_policy_lookup, NULL);
2494 if (IS_ERR_OR_NULL(flo))
2495 pol = ERR_CAST(flo);
2497 pol = container_of(flo, struct xfrm_policy, flo);
2501 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2506 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2507 xfrm_secpath_reject(xerr_idx, skb, &fl);
2508 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2514 pol->curlft.use_time = get_seconds();
2518 #ifdef CONFIG_XFRM_SUB_POLICY
2519 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2520 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2524 if (IS_ERR(pols[1])) {
2525 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2528 pols[1]->curlft.use_time = get_seconds();
2534 if (pol->action == XFRM_POLICY_ALLOW) {
2535 struct sec_path *sp;
2536 static struct sec_path dummy;
2537 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2538 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2539 struct xfrm_tmpl **tpp = tp;
2543 if ((sp = skb->sp) == NULL)
2546 for (pi = 0; pi < npols; pi++) {
2547 if (pols[pi] != pol &&
2548 pols[pi]->action != XFRM_POLICY_ALLOW) {
2549 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2552 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2553 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2556 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2557 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2561 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
2565 /* For each tunnel xfrm, find the first matching tmpl.
2566 * For each tmpl before that, find corresponding xfrm.
2567 * Order is _important_. Later we will implement
2568 * some barriers, but at the moment barriers
2569 * are implied between each two transformations.
2571 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2572 k = xfrm_policy_ok(tpp[i], sp, k, family);
2575 /* "-2 - errored_index" returned */
2577 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2582 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2583 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2587 xfrm_pols_put(pols, npols);
2590 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2593 xfrm_secpath_reject(xerr_idx, skb, &fl);
2595 xfrm_pols_put(pols, npols);
2598 EXPORT_SYMBOL(__xfrm_policy_check);
2600 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2602 struct net *net = dev_net(skb->dev);
2604 struct dst_entry *dst;
2607 if (xfrm_decode_session(skb, &fl, family) < 0) {
2608 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2614 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
2619 skb_dst_set(skb, dst);
2622 EXPORT_SYMBOL(__xfrm_route_forward);
2624 /* Optimize later using cookies and generation ids. */
2626 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2628 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2629 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2630 * get validated by dst_ops->check on every use. We do this
2631 * because when a normal route referenced by an XFRM dst is
2632 * obsoleted we do not go looking around for all parent
2633 * referencing XFRM dsts so that we can invalidate them. It
2634 * is just too much work. Instead we make the checks here on
2635 * every use. For example:
2637 * XFRM dst A --> IPv4 dst X
2639 * X is the "xdst->route" of A (X is also the "dst->path" of A
2640 * in this example). If X is marked obsolete, "A" will not
2641 * notice. That's what we are validating here via the
2642 * stale_bundle() check.
2644 * When a policy's bundle is pruned, we dst_free() the XFRM
2645 * dst which causes it's ->obsolete field to be set to
2646 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2647 * this, we want to force a new route lookup.
2649 if (dst->obsolete < 0 && !stale_bundle(dst))
2655 static int stale_bundle(struct dst_entry *dst)
2657 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2660 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2662 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
2663 dst->dev = dev_net(dev)->loopback_dev;
2668 EXPORT_SYMBOL(xfrm_dst_ifdown);
2670 static void xfrm_link_failure(struct sk_buff *skb)
2672 /* Impossible. Such dst must be popped before reaches point of failure. */
2675 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2678 if (dst->obsolete) {
2686 void xfrm_garbage_collect(struct net *net)
2688 flow_cache_flush(net);
2690 EXPORT_SYMBOL(xfrm_garbage_collect);
2692 static void xfrm_garbage_collect_deferred(struct net *net)
2694 flow_cache_flush_deferred(net);
2697 static void xfrm_init_pmtu(struct dst_entry *dst)
2700 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2701 u32 pmtu, route_mtu_cached;
2703 pmtu = dst_mtu(dst->child);
2704 xdst->child_mtu_cached = pmtu;
2706 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2708 route_mtu_cached = dst_mtu(xdst->route);
2709 xdst->route_mtu_cached = route_mtu_cached;
2711 if (pmtu > route_mtu_cached)
2712 pmtu = route_mtu_cached;
2714 dst_metric_set(dst, RTAX_MTU, pmtu);
2715 } while ((dst = dst->next));
2718 /* Check that the bundle accepts the flow and its components are
2722 static int xfrm_bundle_ok(struct xfrm_dst *first)
2724 struct dst_entry *dst = &first->u.dst;
2725 struct xfrm_dst *last;
2728 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2729 (dst->dev && !netif_running(dst->dev)))
2732 if (dst->flags & DST_XFRM_QUEUE)
2738 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2740 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2742 if (xdst->xfrm_genid != dst->xfrm->genid)
2744 if (xdst->num_pols > 0 &&
2745 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2748 mtu = dst_mtu(dst->child);
2749 if (xdst->child_mtu_cached != mtu) {
2751 xdst->child_mtu_cached = mtu;
2754 if (!dst_check(xdst->route, xdst->route_cookie))
2756 mtu = dst_mtu(xdst->route);
2757 if (xdst->route_mtu_cached != mtu) {
2759 xdst->route_mtu_cached = mtu;
2763 } while (dst->xfrm);
2768 mtu = last->child_mtu_cached;
2772 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2773 if (mtu > last->route_mtu_cached)
2774 mtu = last->route_mtu_cached;
2775 dst_metric_set(dst, RTAX_MTU, mtu);
2780 last = (struct xfrm_dst *)last->u.dst.next;
2781 last->child_mtu_cached = mtu;
2787 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2789 return dst_metric_advmss(dst->path);
2792 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2794 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2796 return mtu ? : dst_mtu(dst->path);
2799 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2800 struct sk_buff *skb,
2803 return dst->path->ops->neigh_lookup(dst, skb, daddr);
2806 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2810 if (unlikely(afinfo == NULL))
2812 if (unlikely(afinfo->family >= NPROTO))
2813 return -EAFNOSUPPORT;
2814 spin_lock(&xfrm_policy_afinfo_lock);
2815 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2818 struct dst_ops *dst_ops = afinfo->dst_ops;
2819 if (likely(dst_ops->kmem_cachep == NULL))
2820 dst_ops->kmem_cachep = xfrm_dst_cache;
2821 if (likely(dst_ops->check == NULL))
2822 dst_ops->check = xfrm_dst_check;
2823 if (likely(dst_ops->default_advmss == NULL))
2824 dst_ops->default_advmss = xfrm_default_advmss;
2825 if (likely(dst_ops->mtu == NULL))
2826 dst_ops->mtu = xfrm_mtu;
2827 if (likely(dst_ops->negative_advice == NULL))
2828 dst_ops->negative_advice = xfrm_negative_advice;
2829 if (likely(dst_ops->link_failure == NULL))
2830 dst_ops->link_failure = xfrm_link_failure;
2831 if (likely(dst_ops->neigh_lookup == NULL))
2832 dst_ops->neigh_lookup = xfrm_neigh_lookup;
2833 if (likely(afinfo->garbage_collect == NULL))
2834 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
2835 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
2837 spin_unlock(&xfrm_policy_afinfo_lock);
2841 struct dst_ops *xfrm_dst_ops;
2843 switch (afinfo->family) {
2845 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2847 #if IS_ENABLED(CONFIG_IPV6)
2849 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2855 *xfrm_dst_ops = *afinfo->dst_ops;
2861 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2863 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2866 if (unlikely(afinfo == NULL))
2868 if (unlikely(afinfo->family >= NPROTO))
2869 return -EAFNOSUPPORT;
2870 spin_lock(&xfrm_policy_afinfo_lock);
2871 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2872 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2875 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2878 spin_unlock(&xfrm_policy_afinfo_lock);
2880 struct dst_ops *dst_ops = afinfo->dst_ops;
2884 dst_ops->kmem_cachep = NULL;
2885 dst_ops->check = NULL;
2886 dst_ops->negative_advice = NULL;
2887 dst_ops->link_failure = NULL;
2888 afinfo->garbage_collect = NULL;
2892 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2894 static void __net_init xfrm_dst_ops_init(struct net *net)
2896 struct xfrm_policy_afinfo *afinfo;
2899 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
2901 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
2902 #if IS_ENABLED(CONFIG_IPV6)
2903 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
2905 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2910 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2912 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2916 xfrm_garbage_collect(dev_net(dev));
2921 static struct notifier_block xfrm_dev_notifier = {
2922 .notifier_call = xfrm_dev_event,
2925 #ifdef CONFIG_XFRM_STATISTICS
2926 static int __net_init xfrm_statistics_init(struct net *net)
2929 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2930 if (!net->mib.xfrm_statistics)
2932 rv = xfrm_proc_init(net);
2934 free_percpu(net->mib.xfrm_statistics);
2938 static void xfrm_statistics_fini(struct net *net)
2940 xfrm_proc_fini(net);
2941 free_percpu(net->mib.xfrm_statistics);
2944 static int __net_init xfrm_statistics_init(struct net *net)
2949 static void xfrm_statistics_fini(struct net *net)
2954 static int __net_init xfrm_policy_init(struct net *net)
2956 unsigned int hmask, sz;
2959 if (net_eq(net, &init_net))
2960 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2961 sizeof(struct xfrm_dst),
2962 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2966 sz = (hmask+1) * sizeof(struct hlist_head);
2968 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2969 if (!net->xfrm.policy_byidx)
2971 net->xfrm.policy_idx_hmask = hmask;
2973 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2974 struct xfrm_policy_hash *htab;
2976 net->xfrm.policy_count[dir] = 0;
2977 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
2978 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2980 htab = &net->xfrm.policy_bydst[dir];
2981 htab->table = xfrm_hash_alloc(sz);
2984 htab->hmask = hmask;
2990 net->xfrm.policy_hthresh.lbits4 = 32;
2991 net->xfrm.policy_hthresh.rbits4 = 32;
2992 net->xfrm.policy_hthresh.lbits6 = 128;
2993 net->xfrm.policy_hthresh.rbits6 = 128;
2995 seqlock_init(&net->xfrm.policy_hthresh.lock);
2997 INIT_LIST_HEAD(&net->xfrm.policy_all);
2998 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
2999 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
3000 if (net_eq(net, &init_net))
3001 register_netdevice_notifier(&xfrm_dev_notifier);
3005 for (dir--; dir >= 0; dir--) {
3006 struct xfrm_policy_hash *htab;
3008 htab = &net->xfrm.policy_bydst[dir];
3009 xfrm_hash_free(htab->table, sz);
3011 xfrm_hash_free(net->xfrm.policy_byidx, sz);
3016 static void xfrm_policy_fini(struct net *net)
3021 flush_work(&net->xfrm.policy_hash_work);
3022 #ifdef CONFIG_XFRM_SUB_POLICY
3023 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
3025 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
3027 WARN_ON(!list_empty(&net->xfrm.policy_all));
3029 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
3030 struct xfrm_policy_hash *htab;
3032 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
3034 htab = &net->xfrm.policy_bydst[dir];
3035 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
3036 WARN_ON(!hlist_empty(htab->table));
3037 xfrm_hash_free(htab->table, sz);
3040 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
3041 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3042 xfrm_hash_free(net->xfrm.policy_byidx, sz);
3045 static int __net_init xfrm_net_init(struct net *net)
3049 rv = xfrm_statistics_init(net);
3051 goto out_statistics;
3052 rv = xfrm_state_init(net);
3055 rv = xfrm_policy_init(net);
3058 xfrm_dst_ops_init(net);
3059 rv = xfrm_sysctl_init(net);
3062 rv = flow_cache_init(net);
3066 /* Initialize the per-net locks here */
3067 spin_lock_init(&net->xfrm.xfrm_state_lock);
3068 rwlock_init(&net->xfrm.xfrm_policy_lock);
3069 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3074 xfrm_sysctl_fini(net);
3076 xfrm_policy_fini(net);
3078 xfrm_state_fini(net);
3080 xfrm_statistics_fini(net);
3085 static void __net_exit xfrm_net_exit(struct net *net)
3087 flow_cache_fini(net);
3088 xfrm_sysctl_fini(net);
3089 xfrm_policy_fini(net);
3090 xfrm_state_fini(net);
3091 xfrm_statistics_fini(net);
3094 static struct pernet_operations __net_initdata xfrm_net_ops = {
3095 .init = xfrm_net_init,
3096 .exit = xfrm_net_exit,
3099 void __init xfrm_init(void)
3101 register_pernet_subsys(&xfrm_net_ops);
3105 #ifdef CONFIG_AUDITSYSCALL
3106 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3107 struct audit_buffer *audit_buf)
3109 struct xfrm_sec_ctx *ctx = xp->security;
3110 struct xfrm_selector *sel = &xp->selector;
3113 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3114 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3116 switch (sel->family) {
3118 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
3119 if (sel->prefixlen_s != 32)
3120 audit_log_format(audit_buf, " src_prefixlen=%d",
3122 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
3123 if (sel->prefixlen_d != 32)
3124 audit_log_format(audit_buf, " dst_prefixlen=%d",
3128 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
3129 if (sel->prefixlen_s != 128)
3130 audit_log_format(audit_buf, " src_prefixlen=%d",
3132 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
3133 if (sel->prefixlen_d != 128)
3134 audit_log_format(audit_buf, " dst_prefixlen=%d",
3140 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
3142 struct audit_buffer *audit_buf;
3144 audit_buf = xfrm_audit_start("SPD-add");
3145 if (audit_buf == NULL)
3147 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3148 audit_log_format(audit_buf, " res=%u", result);
3149 xfrm_audit_common_policyinfo(xp, audit_buf);
3150 audit_log_end(audit_buf);
3152 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3154 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
3157 struct audit_buffer *audit_buf;
3159 audit_buf = xfrm_audit_start("SPD-delete");
3160 if (audit_buf == NULL)
3162 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3163 audit_log_format(audit_buf, " res=%u", result);
3164 xfrm_audit_common_policyinfo(xp, audit_buf);
3165 audit_log_end(audit_buf);
3167 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3170 #ifdef CONFIG_XFRM_MIGRATE
3171 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3172 const struct xfrm_selector *sel_tgt)
3174 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3175 if (sel_tgt->family == sel_cmp->family &&
3176 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3178 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3180 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3181 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
3185 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
3192 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3193 u8 dir, u8 type, struct net *net)
3195 struct xfrm_policy *pol, *ret = NULL;
3196 struct hlist_head *chain;
3199 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
3200 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
3201 hlist_for_each_entry(pol, chain, bydst) {
3202 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3203 pol->type == type) {
3205 priority = ret->priority;
3209 chain = &net->xfrm.policy_inexact[dir];
3210 hlist_for_each_entry(pol, chain, bydst) {
3211 if ((pol->priority >= priority) && ret)
3214 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3215 pol->type == type) {
3223 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
3228 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3232 if (t->mode == m->mode && t->id.proto == m->proto &&
3233 (m->reqid == 0 || t->reqid == m->reqid)) {
3235 case XFRM_MODE_TUNNEL:
3236 case XFRM_MODE_BEET:
3237 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3239 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3244 case XFRM_MODE_TRANSPORT:
3245 /* in case of transport mode, template does not store
3246 any IP addresses, hence we just compare mode and
3257 /* update endpoint address(es) of template(s) */
3258 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3259 struct xfrm_migrate *m, int num_migrate)
3261 struct xfrm_migrate *mp;
3264 write_lock_bh(&pol->lock);
3265 if (unlikely(pol->walk.dead)) {
3266 /* target policy has been deleted */
3267 write_unlock_bh(&pol->lock);
3271 for (i = 0; i < pol->xfrm_nr; i++) {
3272 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3273 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3276 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3277 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3279 /* update endpoints */
3280 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3281 sizeof(pol->xfrm_vec[i].id.daddr));
3282 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3283 sizeof(pol->xfrm_vec[i].saddr));
3284 pol->xfrm_vec[i].encap_family = mp->new_family;
3286 atomic_inc(&pol->genid);
3290 write_unlock_bh(&pol->lock);
3298 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3302 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3305 for (i = 0; i < num_migrate; i++) {
3306 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3308 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3311 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3312 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3315 /* check if there is any duplicated entry */
3316 for (j = i + 1; j < num_migrate; j++) {
3317 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3318 sizeof(m[i].old_daddr)) &&
3319 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3320 sizeof(m[i].old_saddr)) &&
3321 m[i].proto == m[j].proto &&
3322 m[i].mode == m[j].mode &&
3323 m[i].reqid == m[j].reqid &&
3324 m[i].old_family == m[j].old_family)
3332 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3333 struct xfrm_migrate *m, int num_migrate,
3334 struct xfrm_kmaddress *k, struct net *net)
3336 int i, err, nx_cur = 0, nx_new = 0;
3337 struct xfrm_policy *pol = NULL;
3338 struct xfrm_state *x, *xc;
3339 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3340 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3341 struct xfrm_migrate *mp;
3343 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3346 /* Stage 1 - find policy */
3347 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
3352 /* Stage 2 - find and update state(s) */
3353 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3354 if ((x = xfrm_migrate_state_find(mp, net))) {
3357 if ((xc = xfrm_state_migrate(x, mp))) {
3367 /* Stage 3 - update policy */
3368 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3371 /* Stage 4 - delete old state(s) */
3373 xfrm_states_put(x_cur, nx_cur);
3374 xfrm_states_delete(x_cur, nx_cur);
3377 /* Stage 5 - announce */
3378 km_migrate(sel, dir, type, m, num_migrate, k);
3390 xfrm_states_put(x_cur, nx_cur);
3392 xfrm_states_delete(x_new, nx_new);
3396 EXPORT_SYMBOL(xfrm_migrate);