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 struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
62 __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
64 const struct flowi4 *fl4 = &fl->u.ip4;
66 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
67 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
68 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
69 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
70 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
71 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
75 __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
77 const struct flowi6 *fl6 = &fl->u.ip6;
79 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
80 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
81 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
82 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
83 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
84 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
87 bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
88 unsigned short family)
92 return __xfrm4_selector_match(sel, fl);
94 return __xfrm6_selector_match(sel, fl);
99 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
101 struct xfrm_policy_afinfo *afinfo;
103 if (unlikely(family >= NPROTO))
106 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
107 if (unlikely(!afinfo))
112 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
117 static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
118 const xfrm_address_t *saddr,
119 const xfrm_address_t *daddr,
122 struct xfrm_policy_afinfo *afinfo;
123 struct dst_entry *dst;
125 afinfo = xfrm_policy_get_afinfo(family);
126 if (unlikely(afinfo == NULL))
127 return ERR_PTR(-EAFNOSUPPORT);
129 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
131 xfrm_policy_put_afinfo(afinfo);
136 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
137 xfrm_address_t *prev_saddr,
138 xfrm_address_t *prev_daddr,
141 struct net *net = xs_net(x);
142 xfrm_address_t *saddr = &x->props.saddr;
143 xfrm_address_t *daddr = &x->id.daddr;
144 struct dst_entry *dst;
146 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
150 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
155 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
158 if (prev_saddr != saddr)
159 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
160 if (prev_daddr != daddr)
161 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
167 static inline unsigned long make_jiffies(long secs)
169 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
170 return MAX_SCHEDULE_TIMEOUT-1;
175 static void xfrm_policy_timer(unsigned long data)
177 struct xfrm_policy *xp = (struct xfrm_policy *)data;
178 unsigned long now = get_seconds();
179 long next = LONG_MAX;
183 read_lock(&xp->lock);
185 if (unlikely(xp->walk.dead))
188 dir = xfrm_policy_id2dir(xp->index);
190 if (xp->lft.hard_add_expires_seconds) {
191 long tmo = xp->lft.hard_add_expires_seconds +
192 xp->curlft.add_time - now;
198 if (xp->lft.hard_use_expires_seconds) {
199 long tmo = xp->lft.hard_use_expires_seconds +
200 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
206 if (xp->lft.soft_add_expires_seconds) {
207 long tmo = xp->lft.soft_add_expires_seconds +
208 xp->curlft.add_time - now;
211 tmo = XFRM_KM_TIMEOUT;
216 if (xp->lft.soft_use_expires_seconds) {
217 long tmo = xp->lft.soft_use_expires_seconds +
218 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
221 tmo = XFRM_KM_TIMEOUT;
228 km_policy_expired(xp, dir, 0, 0);
229 if (next != LONG_MAX &&
230 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
234 read_unlock(&xp->lock);
239 read_unlock(&xp->lock);
240 if (!xfrm_policy_delete(xp, dir))
241 km_policy_expired(xp, dir, 1, 0);
245 static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
247 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
249 if (unlikely(pol->walk.dead))
257 static int xfrm_policy_flo_check(struct flow_cache_object *flo)
259 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
261 return !pol->walk.dead;
264 static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
266 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
269 static const struct flow_cache_ops xfrm_policy_fc_ops = {
270 .get = xfrm_policy_flo_get,
271 .check = xfrm_policy_flo_check,
272 .delete = xfrm_policy_flo_delete,
275 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
279 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
281 struct xfrm_policy *policy;
283 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
286 write_pnet(&policy->xp_net, net);
287 INIT_LIST_HEAD(&policy->walk.all);
288 INIT_HLIST_NODE(&policy->bydst);
289 INIT_HLIST_NODE(&policy->byidx);
290 rwlock_init(&policy->lock);
291 atomic_set(&policy->refcnt, 1);
292 skb_queue_head_init(&policy->polq.hold_queue);
293 setup_timer(&policy->timer, xfrm_policy_timer,
294 (unsigned long)policy);
295 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
296 (unsigned long)policy);
297 policy->flo.ops = &xfrm_policy_fc_ops;
301 EXPORT_SYMBOL(xfrm_policy_alloc);
303 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
305 void xfrm_policy_destroy(struct xfrm_policy *policy)
307 BUG_ON(!policy->walk.dead);
309 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
312 security_xfrm_policy_free(policy->security);
315 EXPORT_SYMBOL(xfrm_policy_destroy);
317 static void xfrm_queue_purge(struct sk_buff_head *list)
321 while ((skb = skb_dequeue(list)) != NULL)
325 /* Rule must be locked. Release descentant resources, announce
326 * entry dead. The rule must be unlinked from lists to the moment.
329 static void xfrm_policy_kill(struct xfrm_policy *policy)
331 policy->walk.dead = 1;
333 atomic_inc(&policy->genid);
335 if (del_timer(&policy->polq.hold_timer))
336 xfrm_pol_put(policy);
337 xfrm_queue_purge(&policy->polq.hold_queue);
339 if (del_timer(&policy->timer))
340 xfrm_pol_put(policy);
342 xfrm_pol_put(policy);
345 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
347 static inline unsigned int idx_hash(struct net *net, u32 index)
349 return __idx_hash(index, net->xfrm.policy_idx_hmask);
352 /* calculate policy hash thresholds */
353 static void __get_hash_thresh(struct net *net,
354 unsigned short family, int dir,
355 u8 *dbits, u8 *sbits)
359 *dbits = net->xfrm.policy_bydst[dir].dbits4;
360 *sbits = net->xfrm.policy_bydst[dir].sbits4;
364 *dbits = net->xfrm.policy_bydst[dir].dbits6;
365 *sbits = net->xfrm.policy_bydst[dir].sbits6;
374 static struct hlist_head *policy_hash_bysel(struct net *net,
375 const struct xfrm_selector *sel,
376 unsigned short family, int dir)
378 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
383 __get_hash_thresh(net, family, dir, &dbits, &sbits);
384 hash = __sel_hash(sel, family, hmask, dbits, sbits);
386 return (hash == hmask + 1 ?
387 &net->xfrm.policy_inexact[dir] :
388 net->xfrm.policy_bydst[dir].table + hash);
391 static struct hlist_head *policy_hash_direct(struct net *net,
392 const xfrm_address_t *daddr,
393 const xfrm_address_t *saddr,
394 unsigned short family, int dir)
396 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
401 __get_hash_thresh(net, family, dir, &dbits, &sbits);
402 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
404 return net->xfrm.policy_bydst[dir].table + hash;
407 static void xfrm_dst_hash_transfer(struct net *net,
408 struct hlist_head *list,
409 struct hlist_head *ndsttable,
410 unsigned int nhashmask,
413 struct hlist_node *tmp, *entry0 = NULL;
414 struct xfrm_policy *pol;
420 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
423 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
424 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
425 pol->family, nhashmask, dbits, sbits);
427 hlist_del(&pol->bydst);
428 hlist_add_head(&pol->bydst, ndsttable+h);
433 hlist_del(&pol->bydst);
434 hlist_add_behind(&pol->bydst, entry0);
436 entry0 = &pol->bydst;
438 if (!hlist_empty(list)) {
444 static void xfrm_idx_hash_transfer(struct hlist_head *list,
445 struct hlist_head *nidxtable,
446 unsigned int nhashmask)
448 struct hlist_node *tmp;
449 struct xfrm_policy *pol;
451 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
454 h = __idx_hash(pol->index, nhashmask);
455 hlist_add_head(&pol->byidx, nidxtable+h);
459 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
461 return ((old_hmask + 1) << 1) - 1;
464 static void xfrm_bydst_resize(struct net *net, int dir)
466 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
467 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
468 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
469 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
470 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
476 write_lock_bh(&net->xfrm.xfrm_policy_lock);
478 for (i = hmask; i >= 0; i--)
479 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
481 net->xfrm.policy_bydst[dir].table = ndst;
482 net->xfrm.policy_bydst[dir].hmask = nhashmask;
484 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
486 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
489 static void xfrm_byidx_resize(struct net *net, int total)
491 unsigned int hmask = net->xfrm.policy_idx_hmask;
492 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
493 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
494 struct hlist_head *oidx = net->xfrm.policy_byidx;
495 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
501 write_lock_bh(&net->xfrm.xfrm_policy_lock);
503 for (i = hmask; i >= 0; i--)
504 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
506 net->xfrm.policy_byidx = nidx;
507 net->xfrm.policy_idx_hmask = nhashmask;
509 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
511 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
514 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
516 unsigned int cnt = net->xfrm.policy_count[dir];
517 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
522 if ((hmask + 1) < xfrm_policy_hashmax &&
529 static inline int xfrm_byidx_should_resize(struct net *net, int total)
531 unsigned int hmask = net->xfrm.policy_idx_hmask;
533 if ((hmask + 1) < xfrm_policy_hashmax &&
540 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
542 read_lock_bh(&net->xfrm.xfrm_policy_lock);
543 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
544 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
545 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
546 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
547 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
548 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
549 si->spdhcnt = net->xfrm.policy_idx_hmask;
550 si->spdhmcnt = xfrm_policy_hashmax;
551 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
553 EXPORT_SYMBOL(xfrm_spd_getinfo);
555 static DEFINE_MUTEX(hash_resize_mutex);
556 static void xfrm_hash_resize(struct work_struct *work)
558 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
561 mutex_lock(&hash_resize_mutex);
564 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
565 if (xfrm_bydst_should_resize(net, dir, &total))
566 xfrm_bydst_resize(net, dir);
568 if (xfrm_byidx_should_resize(net, total))
569 xfrm_byidx_resize(net, total);
571 mutex_unlock(&hash_resize_mutex);
574 static void xfrm_hash_rebuild(struct work_struct *work)
576 struct net *net = container_of(work, struct net,
577 xfrm.policy_hthresh.work);
579 struct xfrm_policy *pol;
580 struct xfrm_policy *policy;
581 struct hlist_head *chain;
582 struct hlist_head *odst;
583 struct hlist_node *newpos;
587 u8 lbits4, rbits4, lbits6, rbits6;
589 mutex_lock(&hash_resize_mutex);
591 /* read selector prefixlen thresholds */
593 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
595 lbits4 = net->xfrm.policy_hthresh.lbits4;
596 rbits4 = net->xfrm.policy_hthresh.rbits4;
597 lbits6 = net->xfrm.policy_hthresh.lbits6;
598 rbits6 = net->xfrm.policy_hthresh.rbits6;
599 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
601 write_lock_bh(&net->xfrm.xfrm_policy_lock);
603 /* reset the bydst and inexact table in all directions */
604 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
605 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
606 hmask = net->xfrm.policy_bydst[dir].hmask;
607 odst = net->xfrm.policy_bydst[dir].table;
608 for (i = hmask; i >= 0; i--)
609 INIT_HLIST_HEAD(odst + i);
610 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
611 /* dir out => dst = remote, src = local */
612 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
613 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
614 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
615 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
617 /* dir in/fwd => dst = local, src = remote */
618 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
619 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
620 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
621 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
625 /* re-insert all policies by order of creation */
626 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
628 chain = policy_hash_bysel(net, &policy->selector,
630 xfrm_policy_id2dir(policy->index));
631 hlist_for_each_entry(pol, chain, bydst) {
632 if (policy->priority >= pol->priority)
633 newpos = &pol->bydst;
638 hlist_add_behind(&policy->bydst, newpos);
640 hlist_add_head(&policy->bydst, chain);
643 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
645 mutex_unlock(&hash_resize_mutex);
648 void xfrm_policy_hash_rebuild(struct net *net)
650 schedule_work(&net->xfrm.policy_hthresh.work);
652 EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
654 /* Generate new index... KAME seems to generate them ordered by cost
655 * of an absolute inpredictability of ordering of rules. This will not pass. */
656 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
658 static u32 idx_generator;
661 struct hlist_head *list;
662 struct xfrm_policy *p;
667 idx = (idx_generator | dir);
676 list = net->xfrm.policy_byidx + idx_hash(net, idx);
678 hlist_for_each_entry(p, list, byidx) {
679 if (p->index == idx) {
689 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
691 u32 *p1 = (u32 *) s1;
692 u32 *p2 = (u32 *) s2;
693 int len = sizeof(struct xfrm_selector) / sizeof(u32);
696 for (i = 0; i < len; i++) {
704 static void xfrm_policy_requeue(struct xfrm_policy *old,
705 struct xfrm_policy *new)
707 struct xfrm_policy_queue *pq = &old->polq;
708 struct sk_buff_head list;
710 __skb_queue_head_init(&list);
712 spin_lock_bh(&pq->hold_queue.lock);
713 skb_queue_splice_init(&pq->hold_queue, &list);
714 if (del_timer(&pq->hold_timer))
716 spin_unlock_bh(&pq->hold_queue.lock);
718 if (skb_queue_empty(&list))
723 spin_lock_bh(&pq->hold_queue.lock);
724 skb_queue_splice(&list, &pq->hold_queue);
725 pq->timeout = XFRM_QUEUE_TMO_MIN;
726 if (!mod_timer(&pq->hold_timer, jiffies))
728 spin_unlock_bh(&pq->hold_queue.lock);
731 static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
732 struct xfrm_policy *pol)
734 u32 mark = policy->mark.v & policy->mark.m;
736 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
739 if ((mark & pol->mark.m) == pol->mark.v &&
740 policy->priority == pol->priority)
746 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
748 struct net *net = xp_net(policy);
749 struct xfrm_policy *pol;
750 struct xfrm_policy *delpol;
751 struct hlist_head *chain;
752 struct hlist_node *newpos;
754 write_lock_bh(&net->xfrm.xfrm_policy_lock);
755 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
758 hlist_for_each_entry(pol, chain, bydst) {
759 if (pol->type == policy->type &&
760 !selector_cmp(&pol->selector, &policy->selector) &&
761 xfrm_policy_mark_match(policy, pol) &&
762 xfrm_sec_ctx_match(pol->security, policy->security) &&
765 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
769 if (policy->priority > pol->priority)
771 } else if (policy->priority >= pol->priority) {
772 newpos = &pol->bydst;
779 hlist_add_behind(&policy->bydst, newpos);
781 hlist_add_head(&policy->bydst, chain);
782 xfrm_pol_hold(policy);
783 net->xfrm.policy_count[dir]++;
784 atomic_inc(&net->xfrm.flow_cache_genid);
786 /* After previous checking, family can either be AF_INET or AF_INET6 */
787 if (policy->family == AF_INET)
788 rt_genid_bump_ipv4(net);
790 rt_genid_bump_ipv6(net);
793 xfrm_policy_requeue(delpol, policy);
794 __xfrm_policy_unlink(delpol, dir);
796 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
797 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
798 policy->curlft.add_time = get_seconds();
799 policy->curlft.use_time = 0;
800 if (!mod_timer(&policy->timer, jiffies + HZ))
801 xfrm_pol_hold(policy);
802 list_add(&policy->walk.all, &net->xfrm.policy_all);
803 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
806 xfrm_policy_kill(delpol);
807 else if (xfrm_bydst_should_resize(net, dir, NULL))
808 schedule_work(&net->xfrm.policy_hash_work);
812 EXPORT_SYMBOL(xfrm_policy_insert);
814 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
815 int dir, struct xfrm_selector *sel,
816 struct xfrm_sec_ctx *ctx, int delete,
819 struct xfrm_policy *pol, *ret;
820 struct hlist_head *chain;
823 write_lock_bh(&net->xfrm.xfrm_policy_lock);
824 chain = policy_hash_bysel(net, sel, sel->family, dir);
826 hlist_for_each_entry(pol, chain, bydst) {
827 if (pol->type == type &&
828 (mark & pol->mark.m) == pol->mark.v &&
829 !selector_cmp(sel, &pol->selector) &&
830 xfrm_sec_ctx_match(ctx, pol->security)) {
833 *err = security_xfrm_policy_delete(
836 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
839 __xfrm_policy_unlink(pol, dir);
845 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
848 xfrm_policy_kill(ret);
851 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
853 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
854 int dir, u32 id, int delete, int *err)
856 struct xfrm_policy *pol, *ret;
857 struct hlist_head *chain;
860 if (xfrm_policy_id2dir(id) != dir)
864 write_lock_bh(&net->xfrm.xfrm_policy_lock);
865 chain = net->xfrm.policy_byidx + idx_hash(net, id);
867 hlist_for_each_entry(pol, chain, byidx) {
868 if (pol->type == type && pol->index == id &&
869 (mark & pol->mark.m) == pol->mark.v) {
872 *err = security_xfrm_policy_delete(
875 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
878 __xfrm_policy_unlink(pol, dir);
884 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
887 xfrm_policy_kill(ret);
890 EXPORT_SYMBOL(xfrm_policy_byid);
892 #ifdef CONFIG_SECURITY_NETWORK_XFRM
894 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
898 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
899 struct xfrm_policy *pol;
902 hlist_for_each_entry(pol,
903 &net->xfrm.policy_inexact[dir], bydst) {
904 if (pol->type != type)
906 err = security_xfrm_policy_delete(pol->security);
908 xfrm_audit_policy_delete(pol, 0, task_valid);
912 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
913 hlist_for_each_entry(pol,
914 net->xfrm.policy_bydst[dir].table + i,
916 if (pol->type != type)
918 err = security_xfrm_policy_delete(
921 xfrm_audit_policy_delete(pol, 0,
932 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
938 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
940 int dir, err = 0, cnt = 0;
942 write_lock_bh(&net->xfrm.xfrm_policy_lock);
944 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
948 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
949 struct xfrm_policy *pol;
953 hlist_for_each_entry(pol,
954 &net->xfrm.policy_inexact[dir], bydst) {
955 if (pol->type != type)
957 __xfrm_policy_unlink(pol, dir);
958 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
961 xfrm_audit_policy_delete(pol, 1, task_valid);
963 xfrm_policy_kill(pol);
965 write_lock_bh(&net->xfrm.xfrm_policy_lock);
969 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
971 hlist_for_each_entry(pol,
972 net->xfrm.policy_bydst[dir].table + i,
974 if (pol->type != type)
976 __xfrm_policy_unlink(pol, dir);
977 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
980 xfrm_audit_policy_delete(pol, 1, task_valid);
981 xfrm_policy_kill(pol);
983 write_lock_bh(&net->xfrm.xfrm_policy_lock);
992 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
995 EXPORT_SYMBOL(xfrm_policy_flush);
997 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
998 int (*func)(struct xfrm_policy *, int, int, void*),
1001 struct xfrm_policy *pol;
1002 struct xfrm_policy_walk_entry *x;
1005 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1006 walk->type != XFRM_POLICY_TYPE_ANY)
1009 if (list_empty(&walk->walk.all) && walk->seq != 0)
1012 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1013 if (list_empty(&walk->walk.all))
1014 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
1016 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
1017 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
1020 pol = container_of(x, struct xfrm_policy, walk);
1021 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1022 walk->type != pol->type)
1024 error = func(pol, xfrm_policy_id2dir(pol->index),
1027 list_move_tail(&walk->walk.all, &x->all);
1032 if (walk->seq == 0) {
1036 list_del_init(&walk->walk.all);
1038 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1041 EXPORT_SYMBOL(xfrm_policy_walk);
1043 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1045 INIT_LIST_HEAD(&walk->walk.all);
1046 walk->walk.dead = 1;
1050 EXPORT_SYMBOL(xfrm_policy_walk_init);
1052 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
1054 if (list_empty(&walk->walk.all))
1057 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
1058 list_del(&walk->walk.all);
1059 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1061 EXPORT_SYMBOL(xfrm_policy_walk_done);
1064 * Find policy to apply to this flow.
1066 * Returns 0 if policy found, else an -errno.
1068 static int xfrm_policy_match(const struct xfrm_policy *pol,
1069 const struct flowi *fl,
1070 u8 type, u16 family, int dir)
1072 const struct xfrm_selector *sel = &pol->selector;
1076 if (pol->family != family ||
1077 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
1081 match = xfrm_selector_match(sel, fl, family);
1083 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
1089 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
1090 const struct flowi *fl,
1094 struct xfrm_policy *pol, *ret;
1095 const xfrm_address_t *daddr, *saddr;
1096 struct hlist_head *chain;
1099 daddr = xfrm_flowi_daddr(fl, family);
1100 saddr = xfrm_flowi_saddr(fl, family);
1101 if (unlikely(!daddr || !saddr))
1104 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1105 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1107 hlist_for_each_entry(pol, chain, bydst) {
1108 err = xfrm_policy_match(pol, fl, type, family, dir);
1118 priority = ret->priority;
1122 chain = &net->xfrm.policy_inexact[dir];
1123 hlist_for_each_entry(pol, chain, bydst) {
1124 err = xfrm_policy_match(pol, fl, type, family, dir);
1132 } else if (pol->priority < priority) {
1140 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1145 static struct xfrm_policy *
1146 __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
1148 #ifdef CONFIG_XFRM_SUB_POLICY
1149 struct xfrm_policy *pol;
1151 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1155 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1158 static int flow_to_policy_dir(int dir)
1160 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1161 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1162 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1168 return XFRM_POLICY_IN;
1170 return XFRM_POLICY_OUT;
1172 return XFRM_POLICY_FWD;
1176 static struct flow_cache_object *
1177 xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
1178 u8 dir, struct flow_cache_object *old_obj, void *ctx)
1180 struct xfrm_policy *pol;
1183 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
1185 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
1186 if (IS_ERR_OR_NULL(pol))
1187 return ERR_CAST(pol);
1189 /* Resolver returns two references:
1190 * one for cache and one for caller of flow_cache_lookup() */
1196 static inline int policy_to_flow_dir(int dir)
1198 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1199 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1200 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1204 case XFRM_POLICY_IN:
1206 case XFRM_POLICY_OUT:
1207 return FLOW_DIR_OUT;
1208 case XFRM_POLICY_FWD:
1209 return FLOW_DIR_FWD;
1213 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1214 const struct flowi *fl)
1216 struct xfrm_policy *pol;
1217 struct net *net = sock_net(sk);
1219 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1220 if ((pol = sk->sk_policy[dir]) != NULL) {
1221 bool match = xfrm_selector_match(&pol->selector, fl,
1226 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1230 err = security_xfrm_policy_lookup(pol->security,
1232 policy_to_flow_dir(dir));
1235 else if (err == -ESRCH)
1243 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1247 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1249 struct net *net = xp_net(pol);
1250 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
1253 list_add(&pol->walk.all, &net->xfrm.policy_all);
1254 hlist_add_head(&pol->bydst, chain);
1255 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
1256 net->xfrm.policy_count[dir]++;
1259 if (xfrm_bydst_should_resize(net, dir, NULL))
1260 schedule_work(&net->xfrm.policy_hash_work);
1263 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1266 struct net *net = xp_net(pol);
1268 if (hlist_unhashed(&pol->bydst))
1271 hlist_del_init(&pol->bydst);
1272 hlist_del(&pol->byidx);
1273 list_del(&pol->walk.all);
1274 net->xfrm.policy_count[dir]--;
1279 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1281 struct net *net = xp_net(pol);
1283 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1284 pol = __xfrm_policy_unlink(pol, dir);
1285 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1287 xfrm_policy_kill(pol);
1292 EXPORT_SYMBOL(xfrm_policy_delete);
1294 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1296 struct net *net = xp_net(pol);
1297 struct xfrm_policy *old_pol;
1299 #ifdef CONFIG_XFRM_SUB_POLICY
1300 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1304 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1305 old_pol = sk->sk_policy[dir];
1306 sk->sk_policy[dir] = pol;
1308 pol->curlft.add_time = get_seconds();
1309 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
1310 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1314 xfrm_policy_requeue(old_pol, pol);
1316 /* Unlinking succeeds always. This is the only function
1317 * allowed to delete or replace socket policy.
1319 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1321 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1324 xfrm_policy_kill(old_pol);
1329 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1331 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1332 struct net *net = xp_net(old);
1335 newp->selector = old->selector;
1336 if (security_xfrm_policy_clone(old->security,
1339 return NULL; /* ENOMEM */
1341 newp->lft = old->lft;
1342 newp->curlft = old->curlft;
1343 newp->mark = old->mark;
1344 newp->action = old->action;
1345 newp->flags = old->flags;
1346 newp->xfrm_nr = old->xfrm_nr;
1347 newp->index = old->index;
1348 newp->type = old->type;
1349 memcpy(newp->xfrm_vec, old->xfrm_vec,
1350 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1351 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1352 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1353 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1359 int __xfrm_sk_clone_policy(struct sock *sk)
1361 struct xfrm_policy *p0 = sk->sk_policy[0],
1362 *p1 = sk->sk_policy[1];
1364 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1365 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1367 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1373 xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
1374 unsigned short family)
1377 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1379 if (unlikely(afinfo == NULL))
1381 err = afinfo->get_saddr(net, local, remote);
1382 xfrm_policy_put_afinfo(afinfo);
1386 /* Resolve list of templates for the flow, given policy. */
1389 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1390 struct xfrm_state **xfrm, unsigned short family)
1392 struct net *net = xp_net(policy);
1395 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1396 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1399 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1400 struct xfrm_state *x;
1401 xfrm_address_t *remote = daddr;
1402 xfrm_address_t *local = saddr;
1403 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1405 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1406 tmpl->mode == XFRM_MODE_BEET) {
1407 remote = &tmpl->id.daddr;
1408 local = &tmpl->saddr;
1409 if (xfrm_addr_any(local, tmpl->encap_family)) {
1410 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
1417 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1419 if (x && x->km.state == XFRM_STATE_VALID) {
1426 error = (x->km.state == XFRM_STATE_ERROR ?
1429 } else if (error == -ESRCH) {
1433 if (!tmpl->optional)
1439 for (nx--; nx >= 0; nx--)
1440 xfrm_state_put(xfrm[nx]);
1445 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1446 struct xfrm_state **xfrm, unsigned short family)
1448 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1449 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1455 for (i = 0; i < npols; i++) {
1456 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1461 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1469 /* found states are sorted for outbound processing */
1471 xfrm_state_sort(xfrm, tpp, cnx, family);
1476 for (cnx--; cnx >= 0; cnx--)
1477 xfrm_state_put(tpp[cnx]);
1482 /* Check that the bundle accepts the flow and its components are
1486 static inline int xfrm_get_tos(const struct flowi *fl, int family)
1488 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1494 tos = afinfo->get_tos(fl);
1496 xfrm_policy_put_afinfo(afinfo);
1501 static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1503 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1504 struct dst_entry *dst = &xdst->u.dst;
1506 if (xdst->route == NULL) {
1507 /* Dummy bundle - if it has xfrms we were not
1508 * able to build bundle as template resolution failed.
1509 * It means we need to try again resolving. */
1510 if (xdst->num_xfrms > 0)
1512 } else if (dst->flags & DST_XFRM_QUEUE) {
1516 if (stale_bundle(dst))
1524 static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1526 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1527 struct dst_entry *dst = &xdst->u.dst;
1531 if (stale_bundle(dst))
1537 static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1539 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1540 struct dst_entry *dst = &xdst->u.dst;
1545 static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1546 .get = xfrm_bundle_flo_get,
1547 .check = xfrm_bundle_flo_check,
1548 .delete = xfrm_bundle_flo_delete,
1551 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1553 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1554 struct dst_ops *dst_ops;
1555 struct xfrm_dst *xdst;
1558 return ERR_PTR(-EINVAL);
1562 dst_ops = &net->xfrm.xfrm4_dst_ops;
1564 #if IS_ENABLED(CONFIG_IPV6)
1566 dst_ops = &net->xfrm.xfrm6_dst_ops;
1572 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
1575 struct dst_entry *dst = &xdst->u.dst;
1577 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1578 xdst->flo.ops = &xfrm_bundle_fc_ops;
1579 if (afinfo->init_dst)
1580 afinfo->init_dst(net, xdst);
1582 xdst = ERR_PTR(-ENOBUFS);
1584 xfrm_policy_put_afinfo(afinfo);
1589 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1592 struct xfrm_policy_afinfo *afinfo =
1593 xfrm_policy_get_afinfo(dst->ops->family);
1599 err = afinfo->init_path(path, dst, nfheader_len);
1601 xfrm_policy_put_afinfo(afinfo);
1606 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1607 const struct flowi *fl)
1609 struct xfrm_policy_afinfo *afinfo =
1610 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1616 err = afinfo->fill_dst(xdst, dev, fl);
1618 xfrm_policy_put_afinfo(afinfo);
1624 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1625 * all the metrics... Shortly, bundle a bundle.
1628 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1629 struct xfrm_state **xfrm, int nx,
1630 const struct flowi *fl,
1631 struct dst_entry *dst)
1633 struct net *net = xp_net(policy);
1634 unsigned long now = jiffies;
1635 struct net_device *dev;
1636 struct xfrm_mode *inner_mode;
1637 struct dst_entry *dst_prev = NULL;
1638 struct dst_entry *dst0 = NULL;
1642 int nfheader_len = 0;
1643 int trailer_len = 0;
1645 int family = policy->selector.family;
1646 xfrm_address_t saddr, daddr;
1648 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1650 tos = xfrm_get_tos(fl, family);
1657 for (; i < nx; i++) {
1658 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1659 struct dst_entry *dst1 = &xdst->u.dst;
1661 err = PTR_ERR(xdst);
1667 if (xfrm[i]->sel.family == AF_UNSPEC) {
1668 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1669 xfrm_af2proto(family));
1671 err = -EAFNOSUPPORT;
1676 inner_mode = xfrm[i]->inner_mode;
1681 dst_prev->child = dst_clone(dst1);
1682 dst1->flags |= DST_NOHASH;
1686 dst_copy_metrics(dst1, dst);
1688 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1689 family = xfrm[i]->props.family;
1690 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1698 dst1->xfrm = xfrm[i];
1699 xdst->xfrm_genid = xfrm[i]->genid;
1701 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1702 dst1->flags |= DST_HOST;
1703 dst1->lastuse = now;
1705 dst1->input = dst_discard;
1706 dst1->output = inner_mode->afinfo->output;
1708 dst1->next = dst_prev;
1711 header_len += xfrm[i]->props.header_len;
1712 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1713 nfheader_len += xfrm[i]->props.header_len;
1714 trailer_len += xfrm[i]->props.trailer_len;
1717 dst_prev->child = dst;
1725 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
1726 xfrm_init_pmtu(dst_prev);
1728 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1729 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1731 err = xfrm_fill_dst(xdst, dev, fl);
1735 dst_prev->header_len = header_len;
1736 dst_prev->trailer_len = trailer_len;
1737 header_len -= xdst->u.dst.xfrm->props.header_len;
1738 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1746 xfrm_state_put(xfrm[i]);
1750 dst0 = ERR_PTR(err);
1754 #ifdef CONFIG_XFRM_SUB_POLICY
1755 static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
1758 *target = kmalloc(size, GFP_ATOMIC);
1763 memcpy(*target, src, size);
1768 static int xfrm_dst_update_parent(struct dst_entry *dst,
1769 const struct xfrm_selector *sel)
1771 #ifdef CONFIG_XFRM_SUB_POLICY
1772 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1773 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1780 static int xfrm_dst_update_origin(struct dst_entry *dst,
1781 const struct flowi *fl)
1783 #ifdef CONFIG_XFRM_SUB_POLICY
1784 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1785 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1791 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1792 struct xfrm_policy **pols,
1793 int *num_pols, int *num_xfrms)
1797 if (*num_pols == 0 || !pols[0]) {
1802 if (IS_ERR(pols[0]))
1803 return PTR_ERR(pols[0]);
1805 *num_xfrms = pols[0]->xfrm_nr;
1807 #ifdef CONFIG_XFRM_SUB_POLICY
1808 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1809 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1810 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1811 XFRM_POLICY_TYPE_MAIN,
1815 if (IS_ERR(pols[1])) {
1816 xfrm_pols_put(pols, *num_pols);
1817 return PTR_ERR(pols[1]);
1820 (*num_xfrms) += pols[1]->xfrm_nr;
1824 for (i = 0; i < *num_pols; i++) {
1825 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1835 static struct xfrm_dst *
1836 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1837 const struct flowi *fl, u16 family,
1838 struct dst_entry *dst_orig)
1840 struct net *net = xp_net(pols[0]);
1841 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1842 struct dst_entry *dst;
1843 struct xfrm_dst *xdst;
1846 /* Try to instantiate a bundle */
1847 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1849 if (err != 0 && err != -EAGAIN)
1850 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1851 return ERR_PTR(err);
1854 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1856 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1857 return ERR_CAST(dst);
1860 xdst = (struct xfrm_dst *)dst;
1861 xdst->num_xfrms = err;
1863 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1865 err = xfrm_dst_update_origin(dst, fl);
1866 if (unlikely(err)) {
1868 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1869 return ERR_PTR(err);
1872 xdst->num_pols = num_pols;
1873 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
1874 xdst->policy_genid = atomic_read(&pols[0]->genid);
1879 static void xfrm_policy_queue_process(unsigned long arg)
1882 struct sk_buff *skb;
1884 struct dst_entry *dst;
1885 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1886 struct xfrm_policy_queue *pq = &pol->polq;
1888 struct sk_buff_head list;
1890 spin_lock(&pq->hold_queue.lock);
1891 skb = skb_peek(&pq->hold_queue);
1893 spin_unlock(&pq->hold_queue.lock);
1898 xfrm_decode_session(skb, &fl, dst->ops->family);
1899 spin_unlock(&pq->hold_queue.lock);
1901 dst_hold(dst->path);
1902 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1907 if (dst->flags & DST_XFRM_QUEUE) {
1910 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1913 pq->timeout = pq->timeout << 1;
1914 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1921 __skb_queue_head_init(&list);
1923 spin_lock(&pq->hold_queue.lock);
1925 skb_queue_splice_init(&pq->hold_queue, &list);
1926 spin_unlock(&pq->hold_queue.lock);
1928 while (!skb_queue_empty(&list)) {
1929 skb = __skb_dequeue(&list);
1931 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1932 dst_hold(skb_dst(skb)->path);
1933 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1942 skb_dst_set(skb, dst);
1944 err = dst_output(skb);
1953 xfrm_queue_purge(&pq->hold_queue);
1957 static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
1959 unsigned long sched_next;
1960 struct dst_entry *dst = skb_dst(skb);
1961 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1962 struct xfrm_policy *pol = xdst->pols[0];
1963 struct xfrm_policy_queue *pq = &pol->polq;
1965 if (unlikely(skb_fclone_busy(skb))) {
1970 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1977 spin_lock_bh(&pq->hold_queue.lock);
1980 pq->timeout = XFRM_QUEUE_TMO_MIN;
1982 sched_next = jiffies + pq->timeout;
1984 if (del_timer(&pq->hold_timer)) {
1985 if (time_before(pq->hold_timer.expires, sched_next))
1986 sched_next = pq->hold_timer.expires;
1990 __skb_queue_tail(&pq->hold_queue, skb);
1991 if (!mod_timer(&pq->hold_timer, sched_next))
1994 spin_unlock_bh(&pq->hold_queue.lock);
1999 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
2000 struct xfrm_flo *xflo,
2001 const struct flowi *fl,
2006 struct net_device *dev;
2007 struct dst_entry *dst;
2008 struct dst_entry *dst1;
2009 struct xfrm_dst *xdst;
2011 xdst = xfrm_alloc_dst(net, family);
2015 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2016 net->xfrm.sysctl_larval_drop ||
2020 dst = xflo->dst_orig;
2021 dst1 = &xdst->u.dst;
2025 dst_copy_metrics(dst1, dst);
2027 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2028 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2029 dst1->lastuse = jiffies;
2031 dst1->input = dst_discard;
2032 dst1->output = xdst_queue_output;
2038 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2045 err = xfrm_fill_dst(xdst, dev, fl);
2054 xdst = ERR_PTR(err);
2058 static struct flow_cache_object *
2059 xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
2060 struct flow_cache_object *oldflo, void *ctx)
2062 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
2063 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2064 struct xfrm_dst *xdst, *new_xdst;
2065 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2067 /* Check if the policies from old bundle are usable */
2070 xdst = container_of(oldflo, struct xfrm_dst, flo);
2071 num_pols = xdst->num_pols;
2072 num_xfrms = xdst->num_xfrms;
2074 for (i = 0; i < num_pols; i++) {
2075 pols[i] = xdst->pols[i];
2076 pol_dead |= pols[i]->walk.dead;
2079 dst_free(&xdst->u.dst);
2087 /* Resolve policies to use if we couldn't get them from
2088 * previous cache entry */
2091 pols[0] = __xfrm_policy_lookup(net, fl, family,
2092 flow_to_policy_dir(dir));
2093 err = xfrm_expand_policies(fl, family, pols,
2094 &num_pols, &num_xfrms);
2100 goto make_dummy_bundle;
2103 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2105 if (IS_ERR(new_xdst)) {
2106 err = PTR_ERR(new_xdst);
2110 goto make_dummy_bundle;
2111 dst_hold(&xdst->u.dst);
2113 } else if (new_xdst == NULL) {
2116 goto make_dummy_bundle;
2117 xdst->num_xfrms = 0;
2118 dst_hold(&xdst->u.dst);
2122 /* Kill the previous bundle */
2124 /* The policies were stolen for newly generated bundle */
2126 dst_free(&xdst->u.dst);
2129 /* Flow cache does not have reference, it dst_free()'s,
2130 * but we do need to return one reference for original caller */
2131 dst_hold(&new_xdst->u.dst);
2132 return &new_xdst->flo;
2135 /* We found policies, but there's no bundles to instantiate:
2136 * either because the policy blocks, has no transformations or
2137 * we could not build template (no xfrm_states).*/
2138 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
2140 xfrm_pols_put(pols, num_pols);
2141 return ERR_CAST(xdst);
2143 xdst->num_pols = num_pols;
2144 xdst->num_xfrms = num_xfrms;
2145 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
2147 dst_hold(&xdst->u.dst);
2151 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2154 dst_free(&xdst->u.dst);
2156 xfrm_pols_put(pols, num_pols);
2157 return ERR_PTR(err);
2160 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2161 struct dst_entry *dst_orig)
2163 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2164 struct dst_entry *ret;
2167 dst_release(dst_orig);
2168 return ERR_PTR(-EINVAL);
2170 ret = afinfo->blackhole_route(net, dst_orig);
2172 xfrm_policy_put_afinfo(afinfo);
2177 /* Main function: finds/creates a bundle for given flow.
2179 * At the moment we eat a raw IP route. Mostly to speed up lookups
2180 * on interfaces with disabled IPsec.
2182 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2183 const struct flowi *fl,
2184 struct sock *sk, int flags)
2186 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2187 struct flow_cache_object *flo;
2188 struct xfrm_dst *xdst;
2189 struct dst_entry *dst, *route;
2190 u16 family = dst_orig->ops->family;
2191 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
2192 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2198 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2200 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2201 err = xfrm_expand_policies(fl, family, pols,
2202 &num_pols, &num_xfrms);
2207 if (num_xfrms <= 0) {
2208 drop_pols = num_pols;
2212 xdst = xfrm_resolve_and_create_bundle(
2216 xfrm_pols_put(pols, num_pols);
2217 err = PTR_ERR(xdst);
2219 } else if (xdst == NULL) {
2221 drop_pols = num_pols;
2225 dst_hold(&xdst->u.dst);
2226 xdst->u.dst.flags |= DST_NOCACHE;
2227 route = xdst->route;
2232 struct xfrm_flo xflo;
2234 xflo.dst_orig = dst_orig;
2237 /* To accelerate a bit... */
2238 if ((dst_orig->flags & DST_NOXFRM) ||
2239 !net->xfrm.policy_count[XFRM_POLICY_OUT])
2242 flo = flow_cache_lookup(net, fl, family, dir,
2243 xfrm_bundle_lookup, &xflo);
2250 xdst = container_of(flo, struct xfrm_dst, flo);
2252 num_pols = xdst->num_pols;
2253 num_xfrms = xdst->num_xfrms;
2254 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
2255 route = xdst->route;
2259 if (route == NULL && num_xfrms > 0) {
2260 /* The only case when xfrm_bundle_lookup() returns a
2261 * bundle with null route, is when the template could
2262 * not be resolved. It means policies are there, but
2263 * bundle could not be created, since we don't yet
2264 * have the xfrm_state's. We need to wait for KM to
2265 * negotiate new SA's or bail out with error.*/
2266 if (net->xfrm.sysctl_larval_drop) {
2268 xfrm_pols_put(pols, drop_pols);
2269 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2271 return ERR_PTR(-EREMOTE);
2276 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2284 if ((flags & XFRM_LOOKUP_ICMP) &&
2285 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2290 for (i = 0; i < num_pols; i++)
2291 pols[i]->curlft.use_time = get_seconds();
2293 if (num_xfrms < 0) {
2294 /* Prohibit the flow */
2295 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2298 } else if (num_xfrms > 0) {
2299 /* Flow transformed */
2300 dst_release(dst_orig);
2302 /* Flow passes untransformed */
2307 xfrm_pols_put(pols, drop_pols);
2308 if (dst && dst->xfrm &&
2309 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2310 dst->flags |= DST_XFRM_TUNNEL;
2314 if (!(flags & XFRM_LOOKUP_ICMP)) {
2322 dst_release(dst_orig);
2323 xfrm_pols_put(pols, drop_pols);
2324 return ERR_PTR(err);
2326 EXPORT_SYMBOL(xfrm_lookup);
2328 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2329 * Otherwise we may send out blackholed packets.
2331 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2332 const struct flowi *fl,
2333 struct sock *sk, int flags)
2335 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2336 flags | XFRM_LOOKUP_QUEUE);
2338 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2339 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2343 EXPORT_SYMBOL(xfrm_lookup_route);
2346 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2348 struct xfrm_state *x;
2350 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2352 x = skb->sp->xvec[idx];
2353 if (!x->type->reject)
2355 return x->type->reject(x, skb, fl);
2358 /* When skb is transformed back to its "native" form, we have to
2359 * check policy restrictions. At the moment we make this in maximally
2360 * stupid way. Shame on me. :-) Of course, connected sockets must
2361 * have policy cached at them.
2365 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2366 unsigned short family)
2368 if (xfrm_state_kern(x))
2369 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2370 return x->id.proto == tmpl->id.proto &&
2371 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2372 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2373 x->props.mode == tmpl->mode &&
2374 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2375 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2376 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2377 xfrm_state_addr_cmp(tmpl, x, family));
2381 * 0 or more than 0 is returned when validation is succeeded (either bypass
2382 * because of optional transport mode, or next index of the mathced secpath
2383 * state with the template.
2384 * -1 is returned when no matching template is found.
2385 * Otherwise "-2 - errored_index" is returned.
2388 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2389 unsigned short family)
2393 if (tmpl->optional) {
2394 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2398 for (; idx < sp->len; idx++) {
2399 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2401 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2410 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2411 unsigned int family, int reverse)
2413 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2416 if (unlikely(afinfo == NULL))
2417 return -EAFNOSUPPORT;
2419 afinfo->decode_session(skb, fl, reverse);
2420 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2421 xfrm_policy_put_afinfo(afinfo);
2424 EXPORT_SYMBOL(__xfrm_decode_session);
2426 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2428 for (; k < sp->len; k++) {
2429 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2438 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2439 unsigned short family)
2441 struct net *net = dev_net(skb->dev);
2442 struct xfrm_policy *pol;
2443 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2452 reverse = dir & ~XFRM_POLICY_MASK;
2453 dir &= XFRM_POLICY_MASK;
2454 fl_dir = policy_to_flow_dir(dir);
2456 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2457 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2461 nf_nat_decode_session(skb, &fl, family);
2463 /* First, check used SA against their selectors. */
2467 for (i = skb->sp->len-1; i >= 0; i--) {
2468 struct xfrm_state *x = skb->sp->xvec[i];
2469 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2470 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2477 if (sk && sk->sk_policy[dir]) {
2478 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
2480 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2486 struct flow_cache_object *flo;
2488 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2489 xfrm_policy_lookup, NULL);
2490 if (IS_ERR_OR_NULL(flo))
2491 pol = ERR_CAST(flo);
2493 pol = container_of(flo, struct xfrm_policy, flo);
2497 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2502 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2503 xfrm_secpath_reject(xerr_idx, skb, &fl);
2504 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2510 pol->curlft.use_time = get_seconds();
2514 #ifdef CONFIG_XFRM_SUB_POLICY
2515 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2516 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2520 if (IS_ERR(pols[1])) {
2521 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2524 pols[1]->curlft.use_time = get_seconds();
2530 if (pol->action == XFRM_POLICY_ALLOW) {
2531 struct sec_path *sp;
2532 static struct sec_path dummy;
2533 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2534 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2535 struct xfrm_tmpl **tpp = tp;
2539 if ((sp = skb->sp) == NULL)
2542 for (pi = 0; pi < npols; pi++) {
2543 if (pols[pi] != pol &&
2544 pols[pi]->action != XFRM_POLICY_ALLOW) {
2545 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2548 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2549 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2552 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2553 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2557 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
2561 /* For each tunnel xfrm, find the first matching tmpl.
2562 * For each tmpl before that, find corresponding xfrm.
2563 * Order is _important_. Later we will implement
2564 * some barriers, but at the moment barriers
2565 * are implied between each two transformations.
2567 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2568 k = xfrm_policy_ok(tpp[i], sp, k, family);
2571 /* "-2 - errored_index" returned */
2573 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2578 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2579 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2583 xfrm_pols_put(pols, npols);
2586 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2589 xfrm_secpath_reject(xerr_idx, skb, &fl);
2591 xfrm_pols_put(pols, npols);
2594 EXPORT_SYMBOL(__xfrm_policy_check);
2596 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2598 struct net *net = dev_net(skb->dev);
2600 struct dst_entry *dst;
2603 if (xfrm_decode_session(skb, &fl, family) < 0) {
2604 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2610 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
2615 skb_dst_set(skb, dst);
2618 EXPORT_SYMBOL(__xfrm_route_forward);
2620 /* Optimize later using cookies and generation ids. */
2622 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2624 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2625 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2626 * get validated by dst_ops->check on every use. We do this
2627 * because when a normal route referenced by an XFRM dst is
2628 * obsoleted we do not go looking around for all parent
2629 * referencing XFRM dsts so that we can invalidate them. It
2630 * is just too much work. Instead we make the checks here on
2631 * every use. For example:
2633 * XFRM dst A --> IPv4 dst X
2635 * X is the "xdst->route" of A (X is also the "dst->path" of A
2636 * in this example). If X is marked obsolete, "A" will not
2637 * notice. That's what we are validating here via the
2638 * stale_bundle() check.
2640 * When a policy's bundle is pruned, we dst_free() the XFRM
2641 * dst which causes it's ->obsolete field to be set to
2642 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2643 * this, we want to force a new route lookup.
2645 if (dst->obsolete < 0 && !stale_bundle(dst))
2651 static int stale_bundle(struct dst_entry *dst)
2653 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2656 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2658 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
2659 dst->dev = dev_net(dev)->loopback_dev;
2664 EXPORT_SYMBOL(xfrm_dst_ifdown);
2666 static void xfrm_link_failure(struct sk_buff *skb)
2668 /* Impossible. Such dst must be popped before reaches point of failure. */
2671 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2674 if (dst->obsolete) {
2682 void xfrm_garbage_collect(struct net *net)
2684 flow_cache_flush(net);
2686 EXPORT_SYMBOL(xfrm_garbage_collect);
2688 static void xfrm_garbage_collect_deferred(struct net *net)
2690 flow_cache_flush_deferred(net);
2693 static void xfrm_init_pmtu(struct dst_entry *dst)
2696 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2697 u32 pmtu, route_mtu_cached;
2699 pmtu = dst_mtu(dst->child);
2700 xdst->child_mtu_cached = pmtu;
2702 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2704 route_mtu_cached = dst_mtu(xdst->route);
2705 xdst->route_mtu_cached = route_mtu_cached;
2707 if (pmtu > route_mtu_cached)
2708 pmtu = route_mtu_cached;
2710 dst_metric_set(dst, RTAX_MTU, pmtu);
2711 } while ((dst = dst->next));
2714 /* Check that the bundle accepts the flow and its components are
2718 static int xfrm_bundle_ok(struct xfrm_dst *first)
2720 struct dst_entry *dst = &first->u.dst;
2721 struct xfrm_dst *last;
2724 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2725 (dst->dev && !netif_running(dst->dev)))
2728 if (dst->flags & DST_XFRM_QUEUE)
2734 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2736 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2738 if (xdst->xfrm_genid != dst->xfrm->genid)
2740 if (xdst->num_pols > 0 &&
2741 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2744 mtu = dst_mtu(dst->child);
2745 if (xdst->child_mtu_cached != mtu) {
2747 xdst->child_mtu_cached = mtu;
2750 if (!dst_check(xdst->route, xdst->route_cookie))
2752 mtu = dst_mtu(xdst->route);
2753 if (xdst->route_mtu_cached != mtu) {
2755 xdst->route_mtu_cached = mtu;
2759 } while (dst->xfrm);
2764 mtu = last->child_mtu_cached;
2768 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2769 if (mtu > last->route_mtu_cached)
2770 mtu = last->route_mtu_cached;
2771 dst_metric_set(dst, RTAX_MTU, mtu);
2776 last = (struct xfrm_dst *)last->u.dst.next;
2777 last->child_mtu_cached = mtu;
2783 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2785 return dst_metric_advmss(dst->path);
2788 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2790 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2792 return mtu ? : dst_mtu(dst->path);
2795 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2796 struct sk_buff *skb,
2799 return dst->path->ops->neigh_lookup(dst, skb, daddr);
2802 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2806 if (unlikely(afinfo == NULL))
2808 if (unlikely(afinfo->family >= NPROTO))
2809 return -EAFNOSUPPORT;
2810 spin_lock(&xfrm_policy_afinfo_lock);
2811 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2814 struct dst_ops *dst_ops = afinfo->dst_ops;
2815 if (likely(dst_ops->kmem_cachep == NULL))
2816 dst_ops->kmem_cachep = xfrm_dst_cache;
2817 if (likely(dst_ops->check == NULL))
2818 dst_ops->check = xfrm_dst_check;
2819 if (likely(dst_ops->default_advmss == NULL))
2820 dst_ops->default_advmss = xfrm_default_advmss;
2821 if (likely(dst_ops->mtu == NULL))
2822 dst_ops->mtu = xfrm_mtu;
2823 if (likely(dst_ops->negative_advice == NULL))
2824 dst_ops->negative_advice = xfrm_negative_advice;
2825 if (likely(dst_ops->link_failure == NULL))
2826 dst_ops->link_failure = xfrm_link_failure;
2827 if (likely(dst_ops->neigh_lookup == NULL))
2828 dst_ops->neigh_lookup = xfrm_neigh_lookup;
2829 if (likely(afinfo->garbage_collect == NULL))
2830 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
2831 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
2833 spin_unlock(&xfrm_policy_afinfo_lock);
2837 struct dst_ops *xfrm_dst_ops;
2839 switch (afinfo->family) {
2841 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2843 #if IS_ENABLED(CONFIG_IPV6)
2845 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2851 *xfrm_dst_ops = *afinfo->dst_ops;
2857 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2859 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2862 if (unlikely(afinfo == NULL))
2864 if (unlikely(afinfo->family >= NPROTO))
2865 return -EAFNOSUPPORT;
2866 spin_lock(&xfrm_policy_afinfo_lock);
2867 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2868 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2871 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2874 spin_unlock(&xfrm_policy_afinfo_lock);
2876 struct dst_ops *dst_ops = afinfo->dst_ops;
2880 dst_ops->kmem_cachep = NULL;
2881 dst_ops->check = NULL;
2882 dst_ops->negative_advice = NULL;
2883 dst_ops->link_failure = NULL;
2884 afinfo->garbage_collect = NULL;
2888 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2890 static void __net_init xfrm_dst_ops_init(struct net *net)
2892 struct xfrm_policy_afinfo *afinfo;
2895 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
2897 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
2898 #if IS_ENABLED(CONFIG_IPV6)
2899 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
2901 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2906 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2908 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2912 xfrm_garbage_collect(dev_net(dev));
2917 static struct notifier_block xfrm_dev_notifier = {
2918 .notifier_call = xfrm_dev_event,
2921 #ifdef CONFIG_XFRM_STATISTICS
2922 static int __net_init xfrm_statistics_init(struct net *net)
2925 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2926 if (!net->mib.xfrm_statistics)
2928 rv = xfrm_proc_init(net);
2930 free_percpu(net->mib.xfrm_statistics);
2934 static void xfrm_statistics_fini(struct net *net)
2936 xfrm_proc_fini(net);
2937 free_percpu(net->mib.xfrm_statistics);
2940 static int __net_init xfrm_statistics_init(struct net *net)
2945 static void xfrm_statistics_fini(struct net *net)
2950 static int __net_init xfrm_policy_init(struct net *net)
2952 unsigned int hmask, sz;
2955 if (net_eq(net, &init_net))
2956 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2957 sizeof(struct xfrm_dst),
2958 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2962 sz = (hmask+1) * sizeof(struct hlist_head);
2964 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2965 if (!net->xfrm.policy_byidx)
2967 net->xfrm.policy_idx_hmask = hmask;
2969 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2970 struct xfrm_policy_hash *htab;
2972 net->xfrm.policy_count[dir] = 0;
2973 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2975 htab = &net->xfrm.policy_bydst[dir];
2976 htab->table = xfrm_hash_alloc(sz);
2979 htab->hmask = hmask;
2985 net->xfrm.policy_hthresh.lbits4 = 32;
2986 net->xfrm.policy_hthresh.rbits4 = 32;
2987 net->xfrm.policy_hthresh.lbits6 = 128;
2988 net->xfrm.policy_hthresh.rbits6 = 128;
2990 seqlock_init(&net->xfrm.policy_hthresh.lock);
2992 INIT_LIST_HEAD(&net->xfrm.policy_all);
2993 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
2994 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
2995 if (net_eq(net, &init_net))
2996 register_netdevice_notifier(&xfrm_dev_notifier);
3000 for (dir--; dir >= 0; dir--) {
3001 struct xfrm_policy_hash *htab;
3003 htab = &net->xfrm.policy_bydst[dir];
3004 xfrm_hash_free(htab->table, sz);
3006 xfrm_hash_free(net->xfrm.policy_byidx, sz);
3011 static void xfrm_policy_fini(struct net *net)
3016 flush_work(&net->xfrm.policy_hash_work);
3017 #ifdef CONFIG_XFRM_SUB_POLICY
3018 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
3020 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
3022 WARN_ON(!list_empty(&net->xfrm.policy_all));
3024 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
3025 struct xfrm_policy_hash *htab;
3027 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
3029 htab = &net->xfrm.policy_bydst[dir];
3030 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
3031 WARN_ON(!hlist_empty(htab->table));
3032 xfrm_hash_free(htab->table, sz);
3035 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
3036 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3037 xfrm_hash_free(net->xfrm.policy_byidx, sz);
3040 static int __net_init xfrm_net_init(struct net *net)
3044 rv = xfrm_statistics_init(net);
3046 goto out_statistics;
3047 rv = xfrm_state_init(net);
3050 rv = xfrm_policy_init(net);
3053 xfrm_dst_ops_init(net);
3054 rv = xfrm_sysctl_init(net);
3057 rv = flow_cache_init(net);
3061 /* Initialize the per-net locks here */
3062 spin_lock_init(&net->xfrm.xfrm_state_lock);
3063 rwlock_init(&net->xfrm.xfrm_policy_lock);
3064 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3069 xfrm_sysctl_fini(net);
3071 xfrm_policy_fini(net);
3073 xfrm_state_fini(net);
3075 xfrm_statistics_fini(net);
3080 static void __net_exit xfrm_net_exit(struct net *net)
3082 flow_cache_fini(net);
3083 xfrm_sysctl_fini(net);
3084 xfrm_policy_fini(net);
3085 xfrm_state_fini(net);
3086 xfrm_statistics_fini(net);
3089 static struct pernet_operations __net_initdata xfrm_net_ops = {
3090 .init = xfrm_net_init,
3091 .exit = xfrm_net_exit,
3094 void __init xfrm_init(void)
3096 register_pernet_subsys(&xfrm_net_ops);
3100 #ifdef CONFIG_AUDITSYSCALL
3101 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3102 struct audit_buffer *audit_buf)
3104 struct xfrm_sec_ctx *ctx = xp->security;
3105 struct xfrm_selector *sel = &xp->selector;
3108 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3109 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3111 switch (sel->family) {
3113 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
3114 if (sel->prefixlen_s != 32)
3115 audit_log_format(audit_buf, " src_prefixlen=%d",
3117 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
3118 if (sel->prefixlen_d != 32)
3119 audit_log_format(audit_buf, " dst_prefixlen=%d",
3123 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
3124 if (sel->prefixlen_s != 128)
3125 audit_log_format(audit_buf, " src_prefixlen=%d",
3127 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
3128 if (sel->prefixlen_d != 128)
3129 audit_log_format(audit_buf, " dst_prefixlen=%d",
3135 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
3137 struct audit_buffer *audit_buf;
3139 audit_buf = xfrm_audit_start("SPD-add");
3140 if (audit_buf == NULL)
3142 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3143 audit_log_format(audit_buf, " res=%u", result);
3144 xfrm_audit_common_policyinfo(xp, audit_buf);
3145 audit_log_end(audit_buf);
3147 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3149 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
3152 struct audit_buffer *audit_buf;
3154 audit_buf = xfrm_audit_start("SPD-delete");
3155 if (audit_buf == NULL)
3157 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3158 audit_log_format(audit_buf, " res=%u", result);
3159 xfrm_audit_common_policyinfo(xp, audit_buf);
3160 audit_log_end(audit_buf);
3162 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3165 #ifdef CONFIG_XFRM_MIGRATE
3166 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3167 const struct xfrm_selector *sel_tgt)
3169 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3170 if (sel_tgt->family == sel_cmp->family &&
3171 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3173 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3175 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3176 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
3180 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
3187 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3188 u8 dir, u8 type, struct net *net)
3190 struct xfrm_policy *pol, *ret = NULL;
3191 struct hlist_head *chain;
3194 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
3195 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
3196 hlist_for_each_entry(pol, chain, bydst) {
3197 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3198 pol->type == type) {
3200 priority = ret->priority;
3204 chain = &net->xfrm.policy_inexact[dir];
3205 hlist_for_each_entry(pol, chain, bydst) {
3206 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3207 pol->type == type &&
3208 pol->priority < priority) {
3217 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
3222 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3226 if (t->mode == m->mode && t->id.proto == m->proto &&
3227 (m->reqid == 0 || t->reqid == m->reqid)) {
3229 case XFRM_MODE_TUNNEL:
3230 case XFRM_MODE_BEET:
3231 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3233 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3238 case XFRM_MODE_TRANSPORT:
3239 /* in case of transport mode, template does not store
3240 any IP addresses, hence we just compare mode and
3251 /* update endpoint address(es) of template(s) */
3252 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3253 struct xfrm_migrate *m, int num_migrate)
3255 struct xfrm_migrate *mp;
3258 write_lock_bh(&pol->lock);
3259 if (unlikely(pol->walk.dead)) {
3260 /* target policy has been deleted */
3261 write_unlock_bh(&pol->lock);
3265 for (i = 0; i < pol->xfrm_nr; i++) {
3266 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3267 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3270 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3271 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3273 /* update endpoints */
3274 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3275 sizeof(pol->xfrm_vec[i].id.daddr));
3276 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3277 sizeof(pol->xfrm_vec[i].saddr));
3278 pol->xfrm_vec[i].encap_family = mp->new_family;
3280 atomic_inc(&pol->genid);
3284 write_unlock_bh(&pol->lock);
3292 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3296 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3299 for (i = 0; i < num_migrate; i++) {
3300 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3302 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3305 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3306 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3309 /* check if there is any duplicated entry */
3310 for (j = i + 1; j < num_migrate; j++) {
3311 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3312 sizeof(m[i].old_daddr)) &&
3313 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3314 sizeof(m[i].old_saddr)) &&
3315 m[i].proto == m[j].proto &&
3316 m[i].mode == m[j].mode &&
3317 m[i].reqid == m[j].reqid &&
3318 m[i].old_family == m[j].old_family)
3326 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3327 struct xfrm_migrate *m, int num_migrate,
3328 struct xfrm_kmaddress *k, struct net *net)
3330 int i, err, nx_cur = 0, nx_new = 0;
3331 struct xfrm_policy *pol = NULL;
3332 struct xfrm_state *x, *xc;
3333 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3334 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3335 struct xfrm_migrate *mp;
3337 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3340 /* Stage 1 - find policy */
3341 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
3346 /* Stage 2 - find and update state(s) */
3347 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3348 if ((x = xfrm_migrate_state_find(mp, net))) {
3351 if ((xc = xfrm_state_migrate(x, mp))) {
3361 /* Stage 3 - update policy */
3362 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3365 /* Stage 4 - delete old state(s) */
3367 xfrm_states_put(x_cur, nx_cur);
3368 xfrm_states_delete(x_cur, nx_cur);
3371 /* Stage 5 - announce */
3372 km_migrate(sel, dir, type, m, num_migrate, k);
3384 xfrm_states_put(x_cur, nx_cur);
3386 xfrm_states_delete(x_new, nx_new);
3390 EXPORT_SYMBOL(xfrm_migrate);