KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / net / xfrm / xfrm_policy.c
blobc829a204ba8cc148e0733838dd0bb195653f5033
1 /*
2 * xfrm_policy.c
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
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>
28 #include <net/dst.h>
29 #include <net/flow.h>
30 #include <net/xfrm.h>
31 #include <net/ip.h>
32 #ifdef CONFIG_XFRM_STATISTICS
33 #include <net/snmp.h>
34 #endif
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
42 struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
47 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48 static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __read_mostly;
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,
59 int dir);
61 static inline bool
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);
74 static inline bool
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)
90 switch (family) {
91 case AF_INET:
92 return __xfrm4_selector_match(sel, fl);
93 case AF_INET6:
94 return __xfrm6_selector_match(sel, fl);
96 return false;
99 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
101 struct xfrm_policy_afinfo *afinfo;
103 if (unlikely(family >= NPROTO))
104 return NULL;
105 rcu_read_lock();
106 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
107 if (unlikely(!afinfo))
108 rcu_read_unlock();
109 return afinfo;
112 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
114 rcu_read_unlock();
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,
120 int family)
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);
133 return dst;
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,
139 int family)
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) {
147 saddr = x->coaddr;
148 daddr = prev_daddr;
150 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
151 saddr = prev_saddr;
152 daddr = x->coaddr;
155 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
157 if (!IS_ERR(dst)) {
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));
164 return dst;
167 static inline unsigned long make_jiffies(long secs)
169 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
170 return MAX_SCHEDULE_TIMEOUT-1;
171 else
172 return secs*HZ;
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;
180 int warn = 0;
181 int dir;
183 read_lock(&xp->lock);
185 if (unlikely(xp->walk.dead))
186 goto out;
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;
193 if (tmo <= 0)
194 goto expired;
195 if (tmo < next)
196 next = tmo;
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;
201 if (tmo <= 0)
202 goto expired;
203 if (tmo < next)
204 next = tmo;
206 if (xp->lft.soft_add_expires_seconds) {
207 long tmo = xp->lft.soft_add_expires_seconds +
208 xp->curlft.add_time - now;
209 if (tmo <= 0) {
210 warn = 1;
211 tmo = XFRM_KM_TIMEOUT;
213 if (tmo < next)
214 next = tmo;
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;
219 if (tmo <= 0) {
220 warn = 1;
221 tmo = XFRM_KM_TIMEOUT;
223 if (tmo < next)
224 next = tmo;
227 if (warn)
228 km_policy_expired(xp, dir, 0, 0);
229 if (next != LONG_MAX &&
230 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
231 xfrm_pol_hold(xp);
233 out:
234 read_unlock(&xp->lock);
235 xfrm_pol_put(xp);
236 return;
238 expired:
239 read_unlock(&xp->lock);
240 if (!xfrm_policy_delete(xp, dir))
241 km_policy_expired(xp, dir, 1, 0);
242 xfrm_pol_put(xp);
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))
250 flo = NULL;
251 else
252 xfrm_pol_hold(pol);
254 return flo;
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
276 * SPD calls.
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);
285 if (policy) {
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;
299 return policy;
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))
310 BUG();
312 security_xfrm_policy_free(policy->security);
313 kfree(policy);
315 EXPORT_SYMBOL(xfrm_policy_destroy);
317 static void xfrm_queue_purge(struct sk_buff_head *list)
319 struct sk_buff *skb;
321 while ((skb = skb_dequeue(list)) != NULL)
322 kfree_skb(skb);
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 static struct hlist_head *policy_hash_bysel(struct net *net,
353 const struct xfrm_selector *sel,
354 unsigned short family, int dir)
356 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
357 unsigned int hash = __sel_hash(sel, family, hmask);
359 return (hash == hmask + 1 ?
360 &net->xfrm.policy_inexact[dir] :
361 net->xfrm.policy_bydst[dir].table + hash);
364 static struct hlist_head *policy_hash_direct(struct net *net,
365 const xfrm_address_t *daddr,
366 const xfrm_address_t *saddr,
367 unsigned short family, int dir)
369 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
370 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
372 return net->xfrm.policy_bydst[dir].table + hash;
375 static void xfrm_dst_hash_transfer(struct hlist_head *list,
376 struct hlist_head *ndsttable,
377 unsigned int nhashmask)
379 struct hlist_node *tmp, *entry0 = NULL;
380 struct xfrm_policy *pol;
381 unsigned int h0 = 0;
383 redo:
384 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
385 unsigned int h;
387 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
388 pol->family, nhashmask);
389 if (!entry0) {
390 hlist_del(&pol->bydst);
391 hlist_add_head(&pol->bydst, ndsttable+h);
392 h0 = h;
393 } else {
394 if (h != h0)
395 continue;
396 hlist_del(&pol->bydst);
397 hlist_add_after(entry0, &pol->bydst);
399 entry0 = &pol->bydst;
401 if (!hlist_empty(list)) {
402 entry0 = NULL;
403 goto redo;
407 static void xfrm_idx_hash_transfer(struct hlist_head *list,
408 struct hlist_head *nidxtable,
409 unsigned int nhashmask)
411 struct hlist_node *tmp;
412 struct xfrm_policy *pol;
414 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
415 unsigned int h;
417 h = __idx_hash(pol->index, nhashmask);
418 hlist_add_head(&pol->byidx, nidxtable+h);
422 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
424 return ((old_hmask + 1) << 1) - 1;
427 static void xfrm_bydst_resize(struct net *net, int dir)
429 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
430 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
431 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
432 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
433 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
434 int i;
436 if (!ndst)
437 return;
439 write_lock_bh(&net->xfrm.xfrm_policy_lock);
441 for (i = hmask; i >= 0; i--)
442 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
444 net->xfrm.policy_bydst[dir].table = ndst;
445 net->xfrm.policy_bydst[dir].hmask = nhashmask;
447 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
449 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
452 static void xfrm_byidx_resize(struct net *net, int total)
454 unsigned int hmask = net->xfrm.policy_idx_hmask;
455 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
456 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
457 struct hlist_head *oidx = net->xfrm.policy_byidx;
458 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
459 int i;
461 if (!nidx)
462 return;
464 write_lock_bh(&net->xfrm.xfrm_policy_lock);
466 for (i = hmask; i >= 0; i--)
467 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
469 net->xfrm.policy_byidx = nidx;
470 net->xfrm.policy_idx_hmask = nhashmask;
472 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
474 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
477 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
479 unsigned int cnt = net->xfrm.policy_count[dir];
480 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
482 if (total)
483 *total += cnt;
485 if ((hmask + 1) < xfrm_policy_hashmax &&
486 cnt > hmask)
487 return 1;
489 return 0;
492 static inline int xfrm_byidx_should_resize(struct net *net, int total)
494 unsigned int hmask = net->xfrm.policy_idx_hmask;
496 if ((hmask + 1) < xfrm_policy_hashmax &&
497 total > hmask)
498 return 1;
500 return 0;
503 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
505 read_lock_bh(&net->xfrm.xfrm_policy_lock);
506 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
507 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
508 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
509 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
510 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
511 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
512 si->spdhcnt = net->xfrm.policy_idx_hmask;
513 si->spdhmcnt = xfrm_policy_hashmax;
514 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
516 EXPORT_SYMBOL(xfrm_spd_getinfo);
518 static DEFINE_MUTEX(hash_resize_mutex);
519 static void xfrm_hash_resize(struct work_struct *work)
521 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
522 int dir, total;
524 mutex_lock(&hash_resize_mutex);
526 total = 0;
527 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
528 if (xfrm_bydst_should_resize(net, dir, &total))
529 xfrm_bydst_resize(net, dir);
531 if (xfrm_byidx_should_resize(net, total))
532 xfrm_byidx_resize(net, total);
534 mutex_unlock(&hash_resize_mutex);
537 /* Generate new index... KAME seems to generate them ordered by cost
538 * of an absolute inpredictability of ordering of rules. This will not pass. */
539 static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
541 static u32 idx_generator;
543 for (;;) {
544 struct hlist_head *list;
545 struct xfrm_policy *p;
546 u32 idx;
547 int found;
549 if (!index) {
550 idx = (idx_generator | dir);
551 idx_generator += 8;
552 } else {
553 idx = index;
554 index = 0;
557 if (idx == 0)
558 idx = 8;
559 list = net->xfrm.policy_byidx + idx_hash(net, idx);
560 found = 0;
561 hlist_for_each_entry(p, list, byidx) {
562 if (p->index == idx) {
563 found = 1;
564 break;
567 if (!found)
568 return idx;
572 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
574 u32 *p1 = (u32 *) s1;
575 u32 *p2 = (u32 *) s2;
576 int len = sizeof(struct xfrm_selector) / sizeof(u32);
577 int i;
579 for (i = 0; i < len; i++) {
580 if (p1[i] != p2[i])
581 return 1;
584 return 0;
587 static void xfrm_policy_requeue(struct xfrm_policy *old,
588 struct xfrm_policy *new)
590 struct xfrm_policy_queue *pq = &old->polq;
591 struct sk_buff_head list;
593 __skb_queue_head_init(&list);
595 spin_lock_bh(&pq->hold_queue.lock);
596 skb_queue_splice_init(&pq->hold_queue, &list);
597 if (del_timer(&pq->hold_timer))
598 xfrm_pol_put(old);
599 spin_unlock_bh(&pq->hold_queue.lock);
601 if (skb_queue_empty(&list))
602 return;
604 pq = &new->polq;
606 spin_lock_bh(&pq->hold_queue.lock);
607 skb_queue_splice(&list, &pq->hold_queue);
608 pq->timeout = XFRM_QUEUE_TMO_MIN;
609 if (!mod_timer(&pq->hold_timer, jiffies))
610 xfrm_pol_hold(new);
611 spin_unlock_bh(&pq->hold_queue.lock);
614 static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
615 struct xfrm_policy *pol)
617 u32 mark = policy->mark.v & policy->mark.m;
619 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
620 return true;
622 if ((mark & pol->mark.m) == pol->mark.v &&
623 policy->priority == pol->priority)
624 return true;
626 return false;
629 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
631 struct net *net = xp_net(policy);
632 struct xfrm_policy *pol;
633 struct xfrm_policy *delpol;
634 struct hlist_head *chain;
635 struct hlist_node *newpos;
637 write_lock_bh(&net->xfrm.xfrm_policy_lock);
638 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
639 delpol = NULL;
640 newpos = NULL;
641 hlist_for_each_entry(pol, chain, bydst) {
642 if (pol->type == policy->type &&
643 !selector_cmp(&pol->selector, &policy->selector) &&
644 xfrm_policy_mark_match(policy, pol) &&
645 xfrm_sec_ctx_match(pol->security, policy->security) &&
646 !WARN_ON(delpol)) {
647 if (excl) {
648 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
649 return -EEXIST;
651 delpol = pol;
652 if (policy->priority > pol->priority)
653 continue;
654 } else if (policy->priority >= pol->priority) {
655 newpos = &pol->bydst;
656 continue;
658 if (delpol)
659 break;
661 if (newpos)
662 hlist_add_after(newpos, &policy->bydst);
663 else
664 hlist_add_head(&policy->bydst, chain);
665 xfrm_pol_hold(policy);
666 net->xfrm.policy_count[dir]++;
667 atomic_inc(&net->xfrm.flow_cache_genid);
669 /* After previous checking, family can either be AF_INET or AF_INET6 */
670 if (policy->family == AF_INET)
671 rt_genid_bump_ipv4(net);
672 else
673 rt_genid_bump_ipv6(net);
675 if (delpol) {
676 xfrm_policy_requeue(delpol, policy);
677 __xfrm_policy_unlink(delpol, dir);
679 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
680 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
681 policy->curlft.add_time = get_seconds();
682 policy->curlft.use_time = 0;
683 if (!mod_timer(&policy->timer, jiffies + HZ))
684 xfrm_pol_hold(policy);
685 list_add(&policy->walk.all, &net->xfrm.policy_all);
686 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
688 if (delpol)
689 xfrm_policy_kill(delpol);
690 else if (xfrm_bydst_should_resize(net, dir, NULL))
691 schedule_work(&net->xfrm.policy_hash_work);
693 return 0;
695 EXPORT_SYMBOL(xfrm_policy_insert);
697 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
698 int dir, struct xfrm_selector *sel,
699 struct xfrm_sec_ctx *ctx, int delete,
700 int *err)
702 struct xfrm_policy *pol, *ret;
703 struct hlist_head *chain;
705 *err = 0;
706 write_lock_bh(&net->xfrm.xfrm_policy_lock);
707 chain = policy_hash_bysel(net, sel, sel->family, dir);
708 ret = NULL;
709 hlist_for_each_entry(pol, chain, bydst) {
710 if (pol->type == type &&
711 (mark & pol->mark.m) == pol->mark.v &&
712 !selector_cmp(sel, &pol->selector) &&
713 xfrm_sec_ctx_match(ctx, pol->security)) {
714 xfrm_pol_hold(pol);
715 if (delete) {
716 *err = security_xfrm_policy_delete(
717 pol->security);
718 if (*err) {
719 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
720 return pol;
722 __xfrm_policy_unlink(pol, dir);
724 ret = pol;
725 break;
728 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
730 if (ret && delete)
731 xfrm_policy_kill(ret);
732 return ret;
734 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
736 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
737 int dir, u32 id, int delete, int *err)
739 struct xfrm_policy *pol, *ret;
740 struct hlist_head *chain;
742 *err = -ENOENT;
743 if (xfrm_policy_id2dir(id) != dir)
744 return NULL;
746 *err = 0;
747 write_lock_bh(&net->xfrm.xfrm_policy_lock);
748 chain = net->xfrm.policy_byidx + idx_hash(net, id);
749 ret = NULL;
750 hlist_for_each_entry(pol, chain, byidx) {
751 if (pol->type == type && pol->index == id &&
752 (mark & pol->mark.m) == pol->mark.v) {
753 xfrm_pol_hold(pol);
754 if (delete) {
755 *err = security_xfrm_policy_delete(
756 pol->security);
757 if (*err) {
758 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
759 return pol;
761 __xfrm_policy_unlink(pol, dir);
763 ret = pol;
764 break;
767 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
769 if (ret && delete)
770 xfrm_policy_kill(ret);
771 return ret;
773 EXPORT_SYMBOL(xfrm_policy_byid);
775 #ifdef CONFIG_SECURITY_NETWORK_XFRM
776 static inline int
777 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
779 int dir, err = 0;
781 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
782 struct xfrm_policy *pol;
783 int i;
785 hlist_for_each_entry(pol,
786 &net->xfrm.policy_inexact[dir], bydst) {
787 if (pol->type != type)
788 continue;
789 err = security_xfrm_policy_delete(pol->security);
790 if (err) {
791 xfrm_audit_policy_delete(pol, 0, task_valid);
792 return err;
795 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
796 hlist_for_each_entry(pol,
797 net->xfrm.policy_bydst[dir].table + i,
798 bydst) {
799 if (pol->type != type)
800 continue;
801 err = security_xfrm_policy_delete(
802 pol->security);
803 if (err) {
804 xfrm_audit_policy_delete(pol, 0,
805 task_valid);
806 return err;
811 return err;
813 #else
814 static inline int
815 xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
817 return 0;
819 #endif
821 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
823 int dir, err = 0, cnt = 0;
825 write_lock_bh(&net->xfrm.xfrm_policy_lock);
827 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
828 if (err)
829 goto out;
831 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
832 struct xfrm_policy *pol;
833 int i;
835 again1:
836 hlist_for_each_entry(pol,
837 &net->xfrm.policy_inexact[dir], bydst) {
838 if (pol->type != type)
839 continue;
840 __xfrm_policy_unlink(pol, dir);
841 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
842 cnt++;
844 xfrm_audit_policy_delete(pol, 1, task_valid);
846 xfrm_policy_kill(pol);
848 write_lock_bh(&net->xfrm.xfrm_policy_lock);
849 goto again1;
852 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
853 again2:
854 hlist_for_each_entry(pol,
855 net->xfrm.policy_bydst[dir].table + i,
856 bydst) {
857 if (pol->type != type)
858 continue;
859 __xfrm_policy_unlink(pol, dir);
860 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
861 cnt++;
863 xfrm_audit_policy_delete(pol, 1, task_valid);
864 xfrm_policy_kill(pol);
866 write_lock_bh(&net->xfrm.xfrm_policy_lock);
867 goto again2;
872 if (!cnt)
873 err = -ESRCH;
874 out:
875 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
876 return err;
878 EXPORT_SYMBOL(xfrm_policy_flush);
880 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
881 int (*func)(struct xfrm_policy *, int, int, void*),
882 void *data)
884 struct xfrm_policy *pol;
885 struct xfrm_policy_walk_entry *x;
886 int error = 0;
888 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
889 walk->type != XFRM_POLICY_TYPE_ANY)
890 return -EINVAL;
892 if (list_empty(&walk->walk.all) && walk->seq != 0)
893 return 0;
895 write_lock_bh(&net->xfrm.xfrm_policy_lock);
896 if (list_empty(&walk->walk.all))
897 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
898 else
899 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
900 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
901 if (x->dead)
902 continue;
903 pol = container_of(x, struct xfrm_policy, walk);
904 if (walk->type != XFRM_POLICY_TYPE_ANY &&
905 walk->type != pol->type)
906 continue;
907 error = func(pol, xfrm_policy_id2dir(pol->index),
908 walk->seq, data);
909 if (error) {
910 list_move_tail(&walk->walk.all, &x->all);
911 goto out;
913 walk->seq++;
915 if (walk->seq == 0) {
916 error = -ENOENT;
917 goto out;
919 list_del_init(&walk->walk.all);
920 out:
921 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
922 return error;
924 EXPORT_SYMBOL(xfrm_policy_walk);
926 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
928 INIT_LIST_HEAD(&walk->walk.all);
929 walk->walk.dead = 1;
930 walk->type = type;
931 walk->seq = 0;
933 EXPORT_SYMBOL(xfrm_policy_walk_init);
935 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
937 if (list_empty(&walk->walk.all))
938 return;
940 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
941 list_del(&walk->walk.all);
942 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
944 EXPORT_SYMBOL(xfrm_policy_walk_done);
947 * Find policy to apply to this flow.
949 * Returns 0 if policy found, else an -errno.
951 static int xfrm_policy_match(const struct xfrm_policy *pol,
952 const struct flowi *fl,
953 u8 type, u16 family, int dir)
955 const struct xfrm_selector *sel = &pol->selector;
956 int ret = -ESRCH;
957 bool match;
959 if (pol->family != family ||
960 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
961 pol->type != type)
962 return ret;
964 match = xfrm_selector_match(sel, fl, family);
965 if (match)
966 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
967 dir);
969 return ret;
972 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
973 const struct flowi *fl,
974 u16 family, u8 dir)
976 int err;
977 struct xfrm_policy *pol, *ret;
978 const xfrm_address_t *daddr, *saddr;
979 struct hlist_head *chain;
980 u32 priority = ~0U;
982 daddr = xfrm_flowi_daddr(fl, family);
983 saddr = xfrm_flowi_saddr(fl, family);
984 if (unlikely(!daddr || !saddr))
985 return NULL;
987 read_lock_bh(&net->xfrm.xfrm_policy_lock);
988 chain = policy_hash_direct(net, daddr, saddr, family, dir);
989 ret = NULL;
990 hlist_for_each_entry(pol, chain, bydst) {
991 err = xfrm_policy_match(pol, fl, type, family, dir);
992 if (err) {
993 if (err == -ESRCH)
994 continue;
995 else {
996 ret = ERR_PTR(err);
997 goto fail;
999 } else {
1000 ret = pol;
1001 priority = ret->priority;
1002 break;
1005 chain = &net->xfrm.policy_inexact[dir];
1006 hlist_for_each_entry(pol, chain, bydst) {
1007 err = xfrm_policy_match(pol, fl, type, family, dir);
1008 if (err) {
1009 if (err == -ESRCH)
1010 continue;
1011 else {
1012 ret = ERR_PTR(err);
1013 goto fail;
1015 } else if (pol->priority < priority) {
1016 ret = pol;
1017 break;
1020 if (ret)
1021 xfrm_pol_hold(ret);
1022 fail:
1023 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1025 return ret;
1028 static struct xfrm_policy *
1029 __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
1031 #ifdef CONFIG_XFRM_SUB_POLICY
1032 struct xfrm_policy *pol;
1034 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1035 if (pol != NULL)
1036 return pol;
1037 #endif
1038 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1041 static int flow_to_policy_dir(int dir)
1043 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1044 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1045 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1046 return dir;
1048 switch (dir) {
1049 default:
1050 case FLOW_DIR_IN:
1051 return XFRM_POLICY_IN;
1052 case FLOW_DIR_OUT:
1053 return XFRM_POLICY_OUT;
1054 case FLOW_DIR_FWD:
1055 return XFRM_POLICY_FWD;
1059 static struct flow_cache_object *
1060 xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
1061 u8 dir, struct flow_cache_object *old_obj, void *ctx)
1063 struct xfrm_policy *pol;
1065 if (old_obj)
1066 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
1068 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
1069 if (IS_ERR_OR_NULL(pol))
1070 return ERR_CAST(pol);
1072 /* Resolver returns two references:
1073 * one for cache and one for caller of flow_cache_lookup() */
1074 xfrm_pol_hold(pol);
1076 return &pol->flo;
1079 static inline int policy_to_flow_dir(int dir)
1081 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1082 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1083 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1084 return dir;
1085 switch (dir) {
1086 default:
1087 case XFRM_POLICY_IN:
1088 return FLOW_DIR_IN;
1089 case XFRM_POLICY_OUT:
1090 return FLOW_DIR_OUT;
1091 case XFRM_POLICY_FWD:
1092 return FLOW_DIR_FWD;
1096 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1097 const struct flowi *fl)
1099 struct xfrm_policy *pol;
1100 struct net *net = sock_net(sk);
1102 read_lock_bh(&net->xfrm.xfrm_policy_lock);
1103 if ((pol = sk->sk_policy[dir]) != NULL) {
1104 bool match = xfrm_selector_match(&pol->selector, fl,
1105 sk->sk_family);
1106 int err = 0;
1108 if (match) {
1109 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1110 pol = NULL;
1111 goto out;
1113 err = security_xfrm_policy_lookup(pol->security,
1114 fl->flowi_secid,
1115 policy_to_flow_dir(dir));
1116 if (!err)
1117 xfrm_pol_hold(pol);
1118 else if (err == -ESRCH)
1119 pol = NULL;
1120 else
1121 pol = ERR_PTR(err);
1122 } else
1123 pol = NULL;
1125 out:
1126 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
1127 return pol;
1130 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1132 struct net *net = xp_net(pol);
1133 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
1134 pol->family, dir);
1136 list_add(&pol->walk.all, &net->xfrm.policy_all);
1137 hlist_add_head(&pol->bydst, chain);
1138 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
1139 net->xfrm.policy_count[dir]++;
1140 xfrm_pol_hold(pol);
1142 if (xfrm_bydst_should_resize(net, dir, NULL))
1143 schedule_work(&net->xfrm.policy_hash_work);
1146 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1147 int dir)
1149 struct net *net = xp_net(pol);
1151 if (hlist_unhashed(&pol->bydst))
1152 return NULL;
1154 hlist_del_init(&pol->bydst);
1155 hlist_del(&pol->byidx);
1156 list_del(&pol->walk.all);
1157 net->xfrm.policy_count[dir]--;
1159 return pol;
1162 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1164 struct net *net = xp_net(pol);
1166 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1167 pol = __xfrm_policy_unlink(pol, dir);
1168 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1169 if (pol) {
1170 xfrm_policy_kill(pol);
1171 return 0;
1173 return -ENOENT;
1175 EXPORT_SYMBOL(xfrm_policy_delete);
1177 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1179 struct net *net = xp_net(pol);
1180 struct xfrm_policy *old_pol;
1182 #ifdef CONFIG_XFRM_SUB_POLICY
1183 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1184 return -EINVAL;
1185 #endif
1187 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1188 old_pol = sk->sk_policy[dir];
1189 sk->sk_policy[dir] = pol;
1190 if (pol) {
1191 pol->curlft.add_time = get_seconds();
1192 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
1193 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1195 if (old_pol) {
1196 if (pol)
1197 xfrm_policy_requeue(old_pol, pol);
1199 /* Unlinking succeeds always. This is the only function
1200 * allowed to delete or replace socket policy.
1202 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1204 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1206 if (old_pol) {
1207 xfrm_policy_kill(old_pol);
1209 return 0;
1212 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1214 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1215 struct net *net = xp_net(old);
1217 if (newp) {
1218 newp->selector = old->selector;
1219 if (security_xfrm_policy_clone(old->security,
1220 &newp->security)) {
1221 kfree(newp);
1222 return NULL; /* ENOMEM */
1224 newp->lft = old->lft;
1225 newp->curlft = old->curlft;
1226 newp->mark = old->mark;
1227 newp->action = old->action;
1228 newp->flags = old->flags;
1229 newp->xfrm_nr = old->xfrm_nr;
1230 newp->index = old->index;
1231 newp->type = old->type;
1232 memcpy(newp->xfrm_vec, old->xfrm_vec,
1233 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1234 write_lock_bh(&net->xfrm.xfrm_policy_lock);
1235 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1236 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
1237 xfrm_pol_put(newp);
1239 return newp;
1242 int __xfrm_sk_clone_policy(struct sock *sk)
1244 struct xfrm_policy *p0 = sk->sk_policy[0],
1245 *p1 = sk->sk_policy[1];
1247 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1248 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1249 return -ENOMEM;
1250 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1251 return -ENOMEM;
1252 return 0;
1255 static int
1256 xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
1257 unsigned short family)
1259 int err;
1260 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1262 if (unlikely(afinfo == NULL))
1263 return -EINVAL;
1264 err = afinfo->get_saddr(net, local, remote);
1265 xfrm_policy_put_afinfo(afinfo);
1266 return err;
1269 /* Resolve list of templates for the flow, given policy. */
1271 static int
1272 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1273 struct xfrm_state **xfrm, unsigned short family)
1275 struct net *net = xp_net(policy);
1276 int nx;
1277 int i, error;
1278 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1279 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1280 xfrm_address_t tmp;
1282 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1283 struct xfrm_state *x;
1284 xfrm_address_t *remote = daddr;
1285 xfrm_address_t *local = saddr;
1286 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1288 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1289 tmpl->mode == XFRM_MODE_BEET) {
1290 remote = &tmpl->id.daddr;
1291 local = &tmpl->saddr;
1292 if (xfrm_addr_any(local, tmpl->encap_family)) {
1293 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
1294 if (error)
1295 goto fail;
1296 local = &tmp;
1300 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1302 if (x && x->km.state == XFRM_STATE_VALID) {
1303 xfrm[nx++] = x;
1304 daddr = remote;
1305 saddr = local;
1306 continue;
1308 if (x) {
1309 error = (x->km.state == XFRM_STATE_ERROR ?
1310 -EINVAL : -EAGAIN);
1311 xfrm_state_put(x);
1312 } else if (error == -ESRCH) {
1313 error = -EAGAIN;
1316 if (!tmpl->optional)
1317 goto fail;
1319 return nx;
1321 fail:
1322 for (nx--; nx >= 0; nx--)
1323 xfrm_state_put(xfrm[nx]);
1324 return error;
1327 static int
1328 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1329 struct xfrm_state **xfrm, unsigned short family)
1331 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1332 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1333 int cnx = 0;
1334 int error;
1335 int ret;
1336 int i;
1338 for (i = 0; i < npols; i++) {
1339 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1340 error = -ENOBUFS;
1341 goto fail;
1344 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1345 if (ret < 0) {
1346 error = ret;
1347 goto fail;
1348 } else
1349 cnx += ret;
1352 /* found states are sorted for outbound processing */
1353 if (npols > 1)
1354 xfrm_state_sort(xfrm, tpp, cnx, family);
1356 return cnx;
1358 fail:
1359 for (cnx--; cnx >= 0; cnx--)
1360 xfrm_state_put(tpp[cnx]);
1361 return error;
1365 /* Check that the bundle accepts the flow and its components are
1366 * still valid.
1369 static inline int xfrm_get_tos(const struct flowi *fl, int family)
1371 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1372 int tos;
1374 if (!afinfo)
1375 return -EINVAL;
1377 tos = afinfo->get_tos(fl);
1379 xfrm_policy_put_afinfo(afinfo);
1381 return tos;
1384 static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1386 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1387 struct dst_entry *dst = &xdst->u.dst;
1389 if (xdst->route == NULL) {
1390 /* Dummy bundle - if it has xfrms we were not
1391 * able to build bundle as template resolution failed.
1392 * It means we need to try again resolving. */
1393 if (xdst->num_xfrms > 0)
1394 return NULL;
1395 } else if (dst->flags & DST_XFRM_QUEUE) {
1396 return NULL;
1397 } else {
1398 /* Real bundle */
1399 if (stale_bundle(dst))
1400 return NULL;
1403 dst_hold(dst);
1404 return flo;
1407 static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1409 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1410 struct dst_entry *dst = &xdst->u.dst;
1412 if (!xdst->route)
1413 return 0;
1414 if (stale_bundle(dst))
1415 return 0;
1417 return 1;
1420 static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1422 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1423 struct dst_entry *dst = &xdst->u.dst;
1425 dst_free(dst);
1428 static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1429 .get = xfrm_bundle_flo_get,
1430 .check = xfrm_bundle_flo_check,
1431 .delete = xfrm_bundle_flo_delete,
1434 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1436 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1437 struct dst_ops *dst_ops;
1438 struct xfrm_dst *xdst;
1440 if (!afinfo)
1441 return ERR_PTR(-EINVAL);
1443 switch (family) {
1444 case AF_INET:
1445 dst_ops = &net->xfrm.xfrm4_dst_ops;
1446 break;
1447 #if IS_ENABLED(CONFIG_IPV6)
1448 case AF_INET6:
1449 dst_ops = &net->xfrm.xfrm6_dst_ops;
1450 break;
1451 #endif
1452 default:
1453 BUG();
1455 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
1457 if (likely(xdst)) {
1458 struct dst_entry *dst = &xdst->u.dst;
1460 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1461 xdst->flo.ops = &xfrm_bundle_fc_ops;
1462 if (afinfo->init_dst)
1463 afinfo->init_dst(net, xdst);
1464 } else
1465 xdst = ERR_PTR(-ENOBUFS);
1467 xfrm_policy_put_afinfo(afinfo);
1469 return xdst;
1472 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1473 int nfheader_len)
1475 struct xfrm_policy_afinfo *afinfo =
1476 xfrm_policy_get_afinfo(dst->ops->family);
1477 int err;
1479 if (!afinfo)
1480 return -EINVAL;
1482 err = afinfo->init_path(path, dst, nfheader_len);
1484 xfrm_policy_put_afinfo(afinfo);
1486 return err;
1489 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1490 const struct flowi *fl)
1492 struct xfrm_policy_afinfo *afinfo =
1493 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1494 int err;
1496 if (!afinfo)
1497 return -EINVAL;
1499 err = afinfo->fill_dst(xdst, dev, fl);
1501 xfrm_policy_put_afinfo(afinfo);
1503 return err;
1507 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1508 * all the metrics... Shortly, bundle a bundle.
1511 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1512 struct xfrm_state **xfrm, int nx,
1513 const struct flowi *fl,
1514 struct dst_entry *dst)
1516 struct net *net = xp_net(policy);
1517 unsigned long now = jiffies;
1518 struct net_device *dev;
1519 struct xfrm_mode *inner_mode;
1520 struct dst_entry *dst_prev = NULL;
1521 struct dst_entry *dst0 = NULL;
1522 int i = 0;
1523 int err;
1524 int header_len = 0;
1525 int nfheader_len = 0;
1526 int trailer_len = 0;
1527 int tos;
1528 int family = policy->selector.family;
1529 xfrm_address_t saddr, daddr;
1531 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1533 tos = xfrm_get_tos(fl, family);
1534 err = tos;
1535 if (tos < 0)
1536 goto put_states;
1538 dst_hold(dst);
1540 for (; i < nx; i++) {
1541 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1542 struct dst_entry *dst1 = &xdst->u.dst;
1544 err = PTR_ERR(xdst);
1545 if (IS_ERR(xdst)) {
1546 dst_release(dst);
1547 goto put_states;
1550 if (xfrm[i]->sel.family == AF_UNSPEC) {
1551 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1552 xfrm_af2proto(family));
1553 if (!inner_mode) {
1554 err = -EAFNOSUPPORT;
1555 dst_release(dst);
1556 goto put_states;
1558 } else
1559 inner_mode = xfrm[i]->inner_mode;
1561 if (!dst_prev)
1562 dst0 = dst1;
1563 else {
1564 dst_prev->child = dst_clone(dst1);
1565 dst1->flags |= DST_NOHASH;
1568 xdst->route = dst;
1569 dst_copy_metrics(dst1, dst);
1571 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1572 family = xfrm[i]->props.family;
1573 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1574 family);
1575 err = PTR_ERR(dst);
1576 if (IS_ERR(dst))
1577 goto put_states;
1578 } else
1579 dst_hold(dst);
1581 dst1->xfrm = xfrm[i];
1582 xdst->xfrm_genid = xfrm[i]->genid;
1584 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1585 dst1->flags |= DST_HOST;
1586 dst1->lastuse = now;
1588 dst1->input = dst_discard;
1589 dst1->output = inner_mode->afinfo->output;
1591 dst1->next = dst_prev;
1592 dst_prev = dst1;
1594 header_len += xfrm[i]->props.header_len;
1595 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1596 nfheader_len += xfrm[i]->props.header_len;
1597 trailer_len += xfrm[i]->props.trailer_len;
1600 dst_prev->child = dst;
1601 dst0->path = dst;
1603 err = -ENODEV;
1604 dev = dst->dev;
1605 if (!dev)
1606 goto free_dst;
1608 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
1609 xfrm_init_pmtu(dst_prev);
1611 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1612 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1614 err = xfrm_fill_dst(xdst, dev, fl);
1615 if (err)
1616 goto free_dst;
1618 dst_prev->header_len = header_len;
1619 dst_prev->trailer_len = trailer_len;
1620 header_len -= xdst->u.dst.xfrm->props.header_len;
1621 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1624 out:
1625 return dst0;
1627 put_states:
1628 for (; i < nx; i++)
1629 xfrm_state_put(xfrm[i]);
1630 free_dst:
1631 if (dst0)
1632 dst_free(dst0);
1633 dst0 = ERR_PTR(err);
1634 goto out;
1637 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1638 struct xfrm_policy **pols,
1639 int *num_pols, int *num_xfrms)
1641 int i;
1643 if (*num_pols == 0 || !pols[0]) {
1644 *num_pols = 0;
1645 *num_xfrms = 0;
1646 return 0;
1648 if (IS_ERR(pols[0]))
1649 return PTR_ERR(pols[0]);
1651 *num_xfrms = pols[0]->xfrm_nr;
1653 #ifdef CONFIG_XFRM_SUB_POLICY
1654 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1655 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1656 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1657 XFRM_POLICY_TYPE_MAIN,
1658 fl, family,
1659 XFRM_POLICY_OUT);
1660 if (pols[1]) {
1661 if (IS_ERR(pols[1])) {
1662 xfrm_pols_put(pols, *num_pols);
1663 return PTR_ERR(pols[1]);
1665 (*num_pols)++;
1666 (*num_xfrms) += pols[1]->xfrm_nr;
1669 #endif
1670 for (i = 0; i < *num_pols; i++) {
1671 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1672 *num_xfrms = -1;
1673 break;
1677 return 0;
1681 static struct xfrm_dst *
1682 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1683 const struct flowi *fl, u16 family,
1684 struct dst_entry *dst_orig)
1686 struct net *net = xp_net(pols[0]);
1687 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1688 struct dst_entry *dst;
1689 struct xfrm_dst *xdst;
1690 int err;
1692 /* Try to instantiate a bundle */
1693 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1694 if (err <= 0) {
1695 if (err != 0 && err != -EAGAIN)
1696 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1697 return ERR_PTR(err);
1700 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1701 if (IS_ERR(dst)) {
1702 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1703 return ERR_CAST(dst);
1706 xdst = (struct xfrm_dst *)dst;
1707 xdst->num_xfrms = err;
1708 xdst->num_pols = num_pols;
1709 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
1710 xdst->policy_genid = atomic_read(&pols[0]->genid);
1712 return xdst;
1715 static void xfrm_policy_queue_process(unsigned long arg)
1717 int err = 0;
1718 struct sk_buff *skb;
1719 struct sock *sk;
1720 struct dst_entry *dst;
1721 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1722 struct xfrm_policy_queue *pq = &pol->polq;
1723 struct flowi fl;
1724 struct sk_buff_head list;
1726 spin_lock(&pq->hold_queue.lock);
1727 skb = skb_peek(&pq->hold_queue);
1728 if (!skb) {
1729 spin_unlock(&pq->hold_queue.lock);
1730 goto out;
1732 dst = skb_dst(skb);
1733 sk = skb->sk;
1734 xfrm_decode_session(skb, &fl, dst->ops->family);
1735 spin_unlock(&pq->hold_queue.lock);
1737 dst_hold(dst->path);
1738 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1739 sk, 0);
1740 if (IS_ERR(dst))
1741 goto purge_queue;
1743 if (dst->flags & DST_XFRM_QUEUE) {
1744 dst_release(dst);
1746 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1747 goto purge_queue;
1749 pq->timeout = pq->timeout << 1;
1750 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1751 xfrm_pol_hold(pol);
1752 goto out;
1755 dst_release(dst);
1757 __skb_queue_head_init(&list);
1759 spin_lock(&pq->hold_queue.lock);
1760 pq->timeout = 0;
1761 skb_queue_splice_init(&pq->hold_queue, &list);
1762 spin_unlock(&pq->hold_queue.lock);
1764 while (!skb_queue_empty(&list)) {
1765 skb = __skb_dequeue(&list);
1767 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1768 dst_hold(skb_dst(skb)->path);
1769 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1770 &fl, skb->sk, 0);
1771 if (IS_ERR(dst)) {
1772 kfree_skb(skb);
1773 continue;
1776 nf_reset(skb);
1777 skb_dst_drop(skb);
1778 skb_dst_set(skb, dst);
1780 err = dst_output(skb);
1783 out:
1784 xfrm_pol_put(pol);
1785 return;
1787 purge_queue:
1788 pq->timeout = 0;
1789 xfrm_queue_purge(&pq->hold_queue);
1790 xfrm_pol_put(pol);
1793 static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
1795 unsigned long sched_next;
1796 struct dst_entry *dst = skb_dst(skb);
1797 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1798 struct xfrm_policy *pol = xdst->pols[0];
1799 struct xfrm_policy_queue *pq = &pol->polq;
1800 const struct sk_buff *fclone = skb + 1;
1802 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1803 fclone->fclone == SKB_FCLONE_CLONE)) {
1804 kfree_skb(skb);
1805 return 0;
1808 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1809 kfree_skb(skb);
1810 return -EAGAIN;
1813 skb_dst_force(skb);
1815 spin_lock_bh(&pq->hold_queue.lock);
1817 if (!pq->timeout)
1818 pq->timeout = XFRM_QUEUE_TMO_MIN;
1820 sched_next = jiffies + pq->timeout;
1822 if (del_timer(&pq->hold_timer)) {
1823 if (time_before(pq->hold_timer.expires, sched_next))
1824 sched_next = pq->hold_timer.expires;
1825 xfrm_pol_put(pol);
1828 __skb_queue_tail(&pq->hold_queue, skb);
1829 if (!mod_timer(&pq->hold_timer, sched_next))
1830 xfrm_pol_hold(pol);
1832 spin_unlock_bh(&pq->hold_queue.lock);
1834 return 0;
1837 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1838 struct xfrm_flo *xflo,
1839 const struct flowi *fl,
1840 int num_xfrms,
1841 u16 family)
1843 int err;
1844 struct net_device *dev;
1845 struct dst_entry *dst;
1846 struct dst_entry *dst1;
1847 struct xfrm_dst *xdst;
1849 xdst = xfrm_alloc_dst(net, family);
1850 if (IS_ERR(xdst))
1851 return xdst;
1853 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
1854 net->xfrm.sysctl_larval_drop ||
1855 num_xfrms <= 0)
1856 return xdst;
1858 dst = xflo->dst_orig;
1859 dst1 = &xdst->u.dst;
1860 dst_hold(dst);
1861 xdst->route = dst;
1863 dst_copy_metrics(dst1, dst);
1865 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1866 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1867 dst1->lastuse = jiffies;
1869 dst1->input = dst_discard;
1870 dst1->output = xdst_queue_output;
1872 dst_hold(dst);
1873 dst1->child = dst;
1874 dst1->path = dst;
1876 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1878 err = -ENODEV;
1879 dev = dst->dev;
1880 if (!dev)
1881 goto free_dst;
1883 err = xfrm_fill_dst(xdst, dev, fl);
1884 if (err)
1885 goto free_dst;
1887 out:
1888 return xdst;
1890 free_dst:
1891 dst_release(dst1);
1892 xdst = ERR_PTR(err);
1893 goto out;
1896 static struct flow_cache_object *
1897 xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
1898 struct flow_cache_object *oldflo, void *ctx)
1900 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
1901 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1902 struct xfrm_dst *xdst, *new_xdst;
1903 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1905 /* Check if the policies from old bundle are usable */
1906 xdst = NULL;
1907 if (oldflo) {
1908 xdst = container_of(oldflo, struct xfrm_dst, flo);
1909 num_pols = xdst->num_pols;
1910 num_xfrms = xdst->num_xfrms;
1911 pol_dead = 0;
1912 for (i = 0; i < num_pols; i++) {
1913 pols[i] = xdst->pols[i];
1914 pol_dead |= pols[i]->walk.dead;
1916 if (pol_dead) {
1917 dst_free(&xdst->u.dst);
1918 xdst = NULL;
1919 num_pols = 0;
1920 num_xfrms = 0;
1921 oldflo = NULL;
1925 /* Resolve policies to use if we couldn't get them from
1926 * previous cache entry */
1927 if (xdst == NULL) {
1928 num_pols = 1;
1929 pols[0] = __xfrm_policy_lookup(net, fl, family,
1930 flow_to_policy_dir(dir));
1931 err = xfrm_expand_policies(fl, family, pols,
1932 &num_pols, &num_xfrms);
1933 if (err < 0)
1934 goto inc_error;
1935 if (num_pols == 0)
1936 return NULL;
1937 if (num_xfrms <= 0)
1938 goto make_dummy_bundle;
1941 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
1942 xflo->dst_orig);
1943 if (IS_ERR(new_xdst)) {
1944 err = PTR_ERR(new_xdst);
1945 if (err != -EAGAIN)
1946 goto error;
1947 if (oldflo == NULL)
1948 goto make_dummy_bundle;
1949 dst_hold(&xdst->u.dst);
1950 return oldflo;
1951 } else if (new_xdst == NULL) {
1952 num_xfrms = 0;
1953 if (oldflo == NULL)
1954 goto make_dummy_bundle;
1955 xdst->num_xfrms = 0;
1956 dst_hold(&xdst->u.dst);
1957 return oldflo;
1960 /* Kill the previous bundle */
1961 if (xdst) {
1962 /* The policies were stolen for newly generated bundle */
1963 xdst->num_pols = 0;
1964 dst_free(&xdst->u.dst);
1967 /* Flow cache does not have reference, it dst_free()'s,
1968 * but we do need to return one reference for original caller */
1969 dst_hold(&new_xdst->u.dst);
1970 return &new_xdst->flo;
1972 make_dummy_bundle:
1973 /* We found policies, but there's no bundles to instantiate:
1974 * either because the policy blocks, has no transformations or
1975 * we could not build template (no xfrm_states).*/
1976 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
1977 if (IS_ERR(xdst)) {
1978 xfrm_pols_put(pols, num_pols);
1979 return ERR_CAST(xdst);
1981 xdst->num_pols = num_pols;
1982 xdst->num_xfrms = num_xfrms;
1983 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
1985 dst_hold(&xdst->u.dst);
1986 return &xdst->flo;
1988 inc_error:
1989 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1990 error:
1991 if (xdst != NULL)
1992 dst_free(&xdst->u.dst);
1993 else
1994 xfrm_pols_put(pols, num_pols);
1995 return ERR_PTR(err);
1998 static struct dst_entry *make_blackhole(struct net *net, u16 family,
1999 struct dst_entry *dst_orig)
2001 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2002 struct dst_entry *ret;
2004 if (!afinfo) {
2005 dst_release(dst_orig);
2006 return ERR_PTR(-EINVAL);
2007 } else {
2008 ret = afinfo->blackhole_route(net, dst_orig);
2010 xfrm_policy_put_afinfo(afinfo);
2012 return ret;
2015 /* Main function: finds/creates a bundle for given flow.
2017 * At the moment we eat a raw IP route. Mostly to speed up lookups
2018 * on interfaces with disabled IPsec.
2020 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2021 const struct flowi *fl,
2022 struct sock *sk, int flags)
2024 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2025 struct flow_cache_object *flo;
2026 struct xfrm_dst *xdst;
2027 struct dst_entry *dst, *route;
2028 u16 family = dst_orig->ops->family;
2029 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
2030 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2032 dst = NULL;
2033 xdst = NULL;
2034 route = NULL;
2036 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2037 num_pols = 1;
2038 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2039 err = xfrm_expand_policies(fl, family, pols,
2040 &num_pols, &num_xfrms);
2041 if (err < 0)
2042 goto dropdst;
2044 if (num_pols) {
2045 if (num_xfrms <= 0) {
2046 drop_pols = num_pols;
2047 goto no_transform;
2050 xdst = xfrm_resolve_and_create_bundle(
2051 pols, num_pols, fl,
2052 family, dst_orig);
2053 if (IS_ERR(xdst)) {
2054 xfrm_pols_put(pols, num_pols);
2055 err = PTR_ERR(xdst);
2056 goto dropdst;
2057 } else if (xdst == NULL) {
2058 num_xfrms = 0;
2059 drop_pols = num_pols;
2060 goto no_transform;
2063 dst_hold(&xdst->u.dst);
2064 xdst->u.dst.flags |= DST_NOCACHE;
2065 route = xdst->route;
2069 if (xdst == NULL) {
2070 struct xfrm_flo xflo;
2072 xflo.dst_orig = dst_orig;
2073 xflo.flags = flags;
2075 /* To accelerate a bit... */
2076 if ((dst_orig->flags & DST_NOXFRM) ||
2077 !net->xfrm.policy_count[XFRM_POLICY_OUT])
2078 goto nopol;
2080 flo = flow_cache_lookup(net, fl, family, dir,
2081 xfrm_bundle_lookup, &xflo);
2082 if (flo == NULL)
2083 goto nopol;
2084 if (IS_ERR(flo)) {
2085 err = PTR_ERR(flo);
2086 goto dropdst;
2088 xdst = container_of(flo, struct xfrm_dst, flo);
2090 num_pols = xdst->num_pols;
2091 num_xfrms = xdst->num_xfrms;
2092 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
2093 route = xdst->route;
2096 dst = &xdst->u.dst;
2097 if (route == NULL && num_xfrms > 0) {
2098 /* The only case when xfrm_bundle_lookup() returns a
2099 * bundle with null route, is when the template could
2100 * not be resolved. It means policies are there, but
2101 * bundle could not be created, since we don't yet
2102 * have the xfrm_state's. We need to wait for KM to
2103 * negotiate new SA's or bail out with error.*/
2104 if (net->xfrm.sysctl_larval_drop) {
2105 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2106 err = -EREMOTE;
2107 goto error;
2110 err = -EAGAIN;
2112 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2113 goto error;
2116 no_transform:
2117 if (num_pols == 0)
2118 goto nopol;
2120 if ((flags & XFRM_LOOKUP_ICMP) &&
2121 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2122 err = -ENOENT;
2123 goto error;
2126 for (i = 0; i < num_pols; i++)
2127 pols[i]->curlft.use_time = get_seconds();
2129 if (num_xfrms < 0) {
2130 /* Prohibit the flow */
2131 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2132 err = -EPERM;
2133 goto error;
2134 } else if (num_xfrms > 0) {
2135 /* Flow transformed */
2136 dst_release(dst_orig);
2137 } else {
2138 /* Flow passes untransformed */
2139 dst_release(dst);
2140 dst = dst_orig;
2143 xfrm_pols_put(pols, drop_pols);
2144 if (dst && dst->xfrm &&
2145 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2146 dst->flags |= DST_XFRM_TUNNEL;
2147 return dst;
2149 nopol:
2150 if (!(flags & XFRM_LOOKUP_ICMP)) {
2151 dst = dst_orig;
2152 goto ok;
2154 err = -ENOENT;
2155 error:
2156 dst_release(dst);
2157 dropdst:
2158 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2159 dst_release(dst_orig);
2160 xfrm_pols_put(pols, drop_pols);
2161 return ERR_PTR(err);
2163 EXPORT_SYMBOL(xfrm_lookup);
2165 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2166 * Otherwise we may send out blackholed packets.
2168 struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2169 const struct flowi *fl,
2170 struct sock *sk, int flags)
2172 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2173 flags | XFRM_LOOKUP_QUEUE |
2174 XFRM_LOOKUP_KEEP_DST_REF);
2176 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2177 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2179 return dst;
2181 EXPORT_SYMBOL(xfrm_lookup_route);
2183 static inline int
2184 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2186 struct xfrm_state *x;
2188 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2189 return 0;
2190 x = skb->sp->xvec[idx];
2191 if (!x->type->reject)
2192 return 0;
2193 return x->type->reject(x, skb, fl);
2196 /* When skb is transformed back to its "native" form, we have to
2197 * check policy restrictions. At the moment we make this in maximally
2198 * stupid way. Shame on me. :-) Of course, connected sockets must
2199 * have policy cached at them.
2202 static inline int
2203 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2204 unsigned short family)
2206 if (xfrm_state_kern(x))
2207 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2208 return x->id.proto == tmpl->id.proto &&
2209 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2210 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2211 x->props.mode == tmpl->mode &&
2212 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2213 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2214 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2215 xfrm_state_addr_cmp(tmpl, x, family));
2219 * 0 or more than 0 is returned when validation is succeeded (either bypass
2220 * because of optional transport mode, or next index of the mathced secpath
2221 * state with the template.
2222 * -1 is returned when no matching template is found.
2223 * Otherwise "-2 - errored_index" is returned.
2225 static inline int
2226 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2227 unsigned short family)
2229 int idx = start;
2231 if (tmpl->optional) {
2232 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2233 return start;
2234 } else
2235 start = -1;
2236 for (; idx < sp->len; idx++) {
2237 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2238 return ++idx;
2239 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2240 if (start == -1)
2241 start = -2-idx;
2242 break;
2245 return start;
2248 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2249 unsigned int family, int reverse)
2251 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2252 int err;
2254 if (unlikely(afinfo == NULL))
2255 return -EAFNOSUPPORT;
2257 afinfo->decode_session(skb, fl, reverse);
2258 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2259 xfrm_policy_put_afinfo(afinfo);
2260 return err;
2262 EXPORT_SYMBOL(__xfrm_decode_session);
2264 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2266 for (; k < sp->len; k++) {
2267 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2268 *idxp = k;
2269 return 1;
2273 return 0;
2276 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2277 unsigned short family)
2279 struct net *net = dev_net(skb->dev);
2280 struct xfrm_policy *pol;
2281 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2282 int npols = 0;
2283 int xfrm_nr;
2284 int pi;
2285 int reverse;
2286 struct flowi fl;
2287 u8 fl_dir;
2288 int xerr_idx = -1;
2290 reverse = dir & ~XFRM_POLICY_MASK;
2291 dir &= XFRM_POLICY_MASK;
2292 fl_dir = policy_to_flow_dir(dir);
2294 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2295 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2296 return 0;
2299 nf_nat_decode_session(skb, &fl, family);
2301 /* First, check used SA against their selectors. */
2302 if (skb->sp) {
2303 int i;
2305 for (i = skb->sp->len-1; i >= 0; i--) {
2306 struct xfrm_state *x = skb->sp->xvec[i];
2307 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2308 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2309 return 0;
2314 pol = NULL;
2315 if (sk && sk->sk_policy[dir]) {
2316 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
2317 if (IS_ERR(pol)) {
2318 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2319 return 0;
2323 if (!pol) {
2324 struct flow_cache_object *flo;
2326 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2327 xfrm_policy_lookup, NULL);
2328 if (IS_ERR_OR_NULL(flo))
2329 pol = ERR_CAST(flo);
2330 else
2331 pol = container_of(flo, struct xfrm_policy, flo);
2334 if (IS_ERR(pol)) {
2335 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2336 return 0;
2339 if (!pol) {
2340 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2341 xfrm_secpath_reject(xerr_idx, skb, &fl);
2342 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2343 return 0;
2345 return 1;
2348 pol->curlft.use_time = get_seconds();
2350 pols[0] = pol;
2351 npols++;
2352 #ifdef CONFIG_XFRM_SUB_POLICY
2353 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2354 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2355 &fl, family,
2356 XFRM_POLICY_IN);
2357 if (pols[1]) {
2358 if (IS_ERR(pols[1])) {
2359 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2360 return 0;
2362 pols[1]->curlft.use_time = get_seconds();
2363 npols++;
2366 #endif
2368 if (pol->action == XFRM_POLICY_ALLOW) {
2369 struct sec_path *sp;
2370 static struct sec_path dummy;
2371 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2372 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2373 struct xfrm_tmpl **tpp = tp;
2374 int ti = 0;
2375 int i, k;
2377 if ((sp = skb->sp) == NULL)
2378 sp = &dummy;
2380 for (pi = 0; pi < npols; pi++) {
2381 if (pols[pi] != pol &&
2382 pols[pi]->action != XFRM_POLICY_ALLOW) {
2383 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2384 goto reject;
2386 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2387 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2388 goto reject_error;
2390 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2391 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2393 xfrm_nr = ti;
2394 if (npols > 1) {
2395 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
2396 tpp = stp;
2399 /* For each tunnel xfrm, find the first matching tmpl.
2400 * For each tmpl before that, find corresponding xfrm.
2401 * Order is _important_. Later we will implement
2402 * some barriers, but at the moment barriers
2403 * are implied between each two transformations.
2405 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2406 k = xfrm_policy_ok(tpp[i], sp, k, family);
2407 if (k < 0) {
2408 if (k < -1)
2409 /* "-2 - errored_index" returned */
2410 xerr_idx = -(2+k);
2411 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2412 goto reject;
2416 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2417 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2418 goto reject;
2421 xfrm_pols_put(pols, npols);
2422 return 1;
2424 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2426 reject:
2427 xfrm_secpath_reject(xerr_idx, skb, &fl);
2428 reject_error:
2429 xfrm_pols_put(pols, npols);
2430 return 0;
2432 EXPORT_SYMBOL(__xfrm_policy_check);
2434 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2436 struct net *net = dev_net(skb->dev);
2437 struct flowi fl;
2438 struct dst_entry *dst;
2439 int res = 1;
2441 if (xfrm_decode_session(skb, &fl, family) < 0) {
2442 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2443 return 0;
2446 skb_dst_force(skb);
2448 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
2449 if (IS_ERR(dst)) {
2450 res = 0;
2451 dst = NULL;
2453 skb_dst_set(skb, dst);
2454 return res;
2456 EXPORT_SYMBOL(__xfrm_route_forward);
2458 /* Optimize later using cookies and generation ids. */
2460 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2462 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2463 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2464 * get validated by dst_ops->check on every use. We do this
2465 * because when a normal route referenced by an XFRM dst is
2466 * obsoleted we do not go looking around for all parent
2467 * referencing XFRM dsts so that we can invalidate them. It
2468 * is just too much work. Instead we make the checks here on
2469 * every use. For example:
2471 * XFRM dst A --> IPv4 dst X
2473 * X is the "xdst->route" of A (X is also the "dst->path" of A
2474 * in this example). If X is marked obsolete, "A" will not
2475 * notice. That's what we are validating here via the
2476 * stale_bundle() check.
2478 * When a policy's bundle is pruned, we dst_free() the XFRM
2479 * dst which causes it's ->obsolete field to be set to
2480 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2481 * this, we want to force a new route lookup.
2483 if (dst->obsolete < 0 && !stale_bundle(dst))
2484 return dst;
2486 return NULL;
2489 static int stale_bundle(struct dst_entry *dst)
2491 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2494 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2496 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
2497 dst->dev = dev_net(dev)->loopback_dev;
2498 dev_hold(dst->dev);
2499 dev_put(dev);
2502 EXPORT_SYMBOL(xfrm_dst_ifdown);
2504 static void xfrm_link_failure(struct sk_buff *skb)
2506 /* Impossible. Such dst must be popped before reaches point of failure. */
2509 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2511 if (dst) {
2512 if (dst->obsolete) {
2513 dst_release(dst);
2514 dst = NULL;
2517 return dst;
2520 void xfrm_garbage_collect(struct net *net)
2522 flow_cache_flush(net);
2524 EXPORT_SYMBOL(xfrm_garbage_collect);
2526 static void xfrm_garbage_collect_deferred(struct net *net)
2528 flow_cache_flush_deferred(net);
2531 static void xfrm_init_pmtu(struct dst_entry *dst)
2533 do {
2534 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2535 u32 pmtu, route_mtu_cached;
2537 pmtu = dst_mtu(dst->child);
2538 xdst->child_mtu_cached = pmtu;
2540 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2542 route_mtu_cached = dst_mtu(xdst->route);
2543 xdst->route_mtu_cached = route_mtu_cached;
2545 if (pmtu > route_mtu_cached)
2546 pmtu = route_mtu_cached;
2548 dst_metric_set(dst, RTAX_MTU, pmtu);
2549 } while ((dst = dst->next));
2552 /* Check that the bundle accepts the flow and its components are
2553 * still valid.
2556 static int xfrm_bundle_ok(struct xfrm_dst *first)
2558 struct dst_entry *dst = &first->u.dst;
2559 struct xfrm_dst *last;
2560 u32 mtu;
2562 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2563 (dst->dev && !netif_running(dst->dev)))
2564 return 0;
2566 if (dst->flags & DST_XFRM_QUEUE)
2567 return 1;
2569 last = NULL;
2571 do {
2572 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2574 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2575 return 0;
2576 if (xdst->xfrm_genid != dst->xfrm->genid)
2577 return 0;
2578 if (xdst->num_pols > 0 &&
2579 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2580 return 0;
2582 mtu = dst_mtu(dst->child);
2583 if (xdst->child_mtu_cached != mtu) {
2584 last = xdst;
2585 xdst->child_mtu_cached = mtu;
2588 if (!dst_check(xdst->route, xdst->route_cookie))
2589 return 0;
2590 mtu = dst_mtu(xdst->route);
2591 if (xdst->route_mtu_cached != mtu) {
2592 last = xdst;
2593 xdst->route_mtu_cached = mtu;
2596 dst = dst->child;
2597 } while (dst->xfrm);
2599 if (likely(!last))
2600 return 1;
2602 mtu = last->child_mtu_cached;
2603 for (;;) {
2604 dst = &last->u.dst;
2606 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2607 if (mtu > last->route_mtu_cached)
2608 mtu = last->route_mtu_cached;
2609 dst_metric_set(dst, RTAX_MTU, mtu);
2611 if (last == first)
2612 break;
2614 last = (struct xfrm_dst *)last->u.dst.next;
2615 last->child_mtu_cached = mtu;
2618 return 1;
2621 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2623 return dst_metric_advmss(dst->path);
2626 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2628 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2630 return mtu ? : dst_mtu(dst->path);
2633 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2634 struct sk_buff *skb,
2635 const void *daddr)
2637 return dst->path->ops->neigh_lookup(dst, skb, daddr);
2640 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2642 int err = 0;
2643 if (unlikely(afinfo == NULL))
2644 return -EINVAL;
2645 if (unlikely(afinfo->family >= NPROTO))
2646 return -EAFNOSUPPORT;
2647 spin_lock(&xfrm_policy_afinfo_lock);
2648 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2649 err = -ENOBUFS;
2650 else {
2651 struct dst_ops *dst_ops = afinfo->dst_ops;
2652 if (likely(dst_ops->kmem_cachep == NULL))
2653 dst_ops->kmem_cachep = xfrm_dst_cache;
2654 if (likely(dst_ops->check == NULL))
2655 dst_ops->check = xfrm_dst_check;
2656 if (likely(dst_ops->default_advmss == NULL))
2657 dst_ops->default_advmss = xfrm_default_advmss;
2658 if (likely(dst_ops->mtu == NULL))
2659 dst_ops->mtu = xfrm_mtu;
2660 if (likely(dst_ops->negative_advice == NULL))
2661 dst_ops->negative_advice = xfrm_negative_advice;
2662 if (likely(dst_ops->link_failure == NULL))
2663 dst_ops->link_failure = xfrm_link_failure;
2664 if (likely(dst_ops->neigh_lookup == NULL))
2665 dst_ops->neigh_lookup = xfrm_neigh_lookup;
2666 if (likely(afinfo->garbage_collect == NULL))
2667 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
2668 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
2670 spin_unlock(&xfrm_policy_afinfo_lock);
2672 return err;
2674 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2676 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2678 int err = 0;
2679 if (unlikely(afinfo == NULL))
2680 return -EINVAL;
2681 if (unlikely(afinfo->family >= NPROTO))
2682 return -EAFNOSUPPORT;
2683 spin_lock(&xfrm_policy_afinfo_lock);
2684 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2685 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2686 err = -EINVAL;
2687 else
2688 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2689 NULL);
2691 spin_unlock(&xfrm_policy_afinfo_lock);
2692 if (!err) {
2693 struct dst_ops *dst_ops = afinfo->dst_ops;
2695 synchronize_rcu();
2697 dst_ops->kmem_cachep = NULL;
2698 dst_ops->check = NULL;
2699 dst_ops->negative_advice = NULL;
2700 dst_ops->link_failure = NULL;
2701 afinfo->garbage_collect = NULL;
2703 return err;
2705 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2707 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2709 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2711 switch (event) {
2712 case NETDEV_DOWN:
2713 xfrm_garbage_collect(dev_net(dev));
2715 return NOTIFY_DONE;
2718 static struct notifier_block xfrm_dev_notifier = {
2719 .notifier_call = xfrm_dev_event,
2722 #ifdef CONFIG_XFRM_STATISTICS
2723 static int __net_init xfrm_statistics_init(struct net *net)
2725 int rv;
2726 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2727 if (!net->mib.xfrm_statistics)
2728 return -ENOMEM;
2729 rv = xfrm_proc_init(net);
2730 if (rv < 0)
2731 free_percpu(net->mib.xfrm_statistics);
2732 return rv;
2735 static void xfrm_statistics_fini(struct net *net)
2737 xfrm_proc_fini(net);
2738 free_percpu(net->mib.xfrm_statistics);
2740 #else
2741 static int __net_init xfrm_statistics_init(struct net *net)
2743 return 0;
2746 static void xfrm_statistics_fini(struct net *net)
2749 #endif
2751 static int __net_init xfrm_policy_init(struct net *net)
2753 unsigned int hmask, sz;
2754 int dir;
2756 if (net_eq(net, &init_net))
2757 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2758 sizeof(struct xfrm_dst),
2759 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2760 NULL);
2762 hmask = 8 - 1;
2763 sz = (hmask+1) * sizeof(struct hlist_head);
2765 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2766 if (!net->xfrm.policy_byidx)
2767 goto out_byidx;
2768 net->xfrm.policy_idx_hmask = hmask;
2770 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2771 struct xfrm_policy_hash *htab;
2773 net->xfrm.policy_count[dir] = 0;
2774 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2776 htab = &net->xfrm.policy_bydst[dir];
2777 htab->table = xfrm_hash_alloc(sz);
2778 if (!htab->table)
2779 goto out_bydst;
2780 htab->hmask = hmask;
2783 INIT_LIST_HEAD(&net->xfrm.policy_all);
2784 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
2785 if (net_eq(net, &init_net))
2786 register_netdevice_notifier(&xfrm_dev_notifier);
2787 return 0;
2789 out_bydst:
2790 for (dir--; dir >= 0; dir--) {
2791 struct xfrm_policy_hash *htab;
2793 htab = &net->xfrm.policy_bydst[dir];
2794 xfrm_hash_free(htab->table, sz);
2796 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2797 out_byidx:
2798 return -ENOMEM;
2801 static void xfrm_policy_fini(struct net *net)
2803 unsigned int sz;
2804 int dir;
2806 flush_work(&net->xfrm.policy_hash_work);
2807 #ifdef CONFIG_XFRM_SUB_POLICY
2808 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
2809 #endif
2810 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
2812 WARN_ON(!list_empty(&net->xfrm.policy_all));
2814 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2815 struct xfrm_policy_hash *htab;
2817 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
2819 htab = &net->xfrm.policy_bydst[dir];
2820 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
2821 WARN_ON(!hlist_empty(htab->table));
2822 xfrm_hash_free(htab->table, sz);
2825 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
2826 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2827 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2830 static int __net_init xfrm_net_init(struct net *net)
2832 int rv;
2834 /* Initialize the per-net locks here */
2835 spin_lock_init(&net->xfrm.xfrm_state_lock);
2836 rwlock_init(&net->xfrm.xfrm_policy_lock);
2837 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2839 rv = xfrm_statistics_init(net);
2840 if (rv < 0)
2841 goto out_statistics;
2842 rv = xfrm_state_init(net);
2843 if (rv < 0)
2844 goto out_state;
2845 rv = xfrm_policy_init(net);
2846 if (rv < 0)
2847 goto out_policy;
2848 rv = xfrm_sysctl_init(net);
2849 if (rv < 0)
2850 goto out_sysctl;
2851 rv = flow_cache_init(net);
2852 if (rv < 0)
2853 goto out;
2855 return 0;
2857 out:
2858 xfrm_sysctl_fini(net);
2859 out_sysctl:
2860 xfrm_policy_fini(net);
2861 out_policy:
2862 xfrm_state_fini(net);
2863 out_state:
2864 xfrm_statistics_fini(net);
2865 out_statistics:
2866 return rv;
2869 static void __net_exit xfrm_net_exit(struct net *net)
2871 flow_cache_fini(net);
2872 xfrm_sysctl_fini(net);
2873 xfrm_policy_fini(net);
2874 xfrm_state_fini(net);
2875 xfrm_statistics_fini(net);
2878 static struct pernet_operations __net_initdata xfrm_net_ops = {
2879 .init = xfrm_net_init,
2880 .exit = xfrm_net_exit,
2883 void __init xfrm_init(void)
2885 register_pernet_subsys(&xfrm_net_ops);
2886 xfrm_input_init();
2889 #ifdef CONFIG_AUDITSYSCALL
2890 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2891 struct audit_buffer *audit_buf)
2893 struct xfrm_sec_ctx *ctx = xp->security;
2894 struct xfrm_selector *sel = &xp->selector;
2896 if (ctx)
2897 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2898 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2900 switch (sel->family) {
2901 case AF_INET:
2902 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
2903 if (sel->prefixlen_s != 32)
2904 audit_log_format(audit_buf, " src_prefixlen=%d",
2905 sel->prefixlen_s);
2906 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
2907 if (sel->prefixlen_d != 32)
2908 audit_log_format(audit_buf, " dst_prefixlen=%d",
2909 sel->prefixlen_d);
2910 break;
2911 case AF_INET6:
2912 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
2913 if (sel->prefixlen_s != 128)
2914 audit_log_format(audit_buf, " src_prefixlen=%d",
2915 sel->prefixlen_s);
2916 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
2917 if (sel->prefixlen_d != 128)
2918 audit_log_format(audit_buf, " dst_prefixlen=%d",
2919 sel->prefixlen_d);
2920 break;
2924 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
2926 struct audit_buffer *audit_buf;
2928 audit_buf = xfrm_audit_start("SPD-add");
2929 if (audit_buf == NULL)
2930 return;
2931 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2932 audit_log_format(audit_buf, " res=%u", result);
2933 xfrm_audit_common_policyinfo(xp, audit_buf);
2934 audit_log_end(audit_buf);
2936 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2938 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
2939 bool task_valid)
2941 struct audit_buffer *audit_buf;
2943 audit_buf = xfrm_audit_start("SPD-delete");
2944 if (audit_buf == NULL)
2945 return;
2946 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2947 audit_log_format(audit_buf, " res=%u", result);
2948 xfrm_audit_common_policyinfo(xp, audit_buf);
2949 audit_log_end(audit_buf);
2951 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2952 #endif
2954 #ifdef CONFIG_XFRM_MIGRATE
2955 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
2956 const struct xfrm_selector *sel_tgt)
2958 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2959 if (sel_tgt->family == sel_cmp->family &&
2960 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
2961 sel_cmp->family) &&
2962 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
2963 sel_cmp->family) &&
2964 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2965 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2966 return true;
2968 } else {
2969 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2970 return true;
2973 return false;
2976 static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
2977 u8 dir, u8 type, struct net *net)
2979 struct xfrm_policy *pol, *ret = NULL;
2980 struct hlist_head *chain;
2981 u32 priority = ~0U;
2983 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
2984 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
2985 hlist_for_each_entry(pol, chain, bydst) {
2986 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2987 pol->type == type) {
2988 ret = pol;
2989 priority = ret->priority;
2990 break;
2993 chain = &net->xfrm.policy_inexact[dir];
2994 hlist_for_each_entry(pol, chain, bydst) {
2995 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2996 pol->type == type &&
2997 pol->priority < priority) {
2998 ret = pol;
2999 break;
3003 if (ret)
3004 xfrm_pol_hold(ret);
3006 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
3008 return ret;
3011 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3013 int match = 0;
3015 if (t->mode == m->mode && t->id.proto == m->proto &&
3016 (m->reqid == 0 || t->reqid == m->reqid)) {
3017 switch (t->mode) {
3018 case XFRM_MODE_TUNNEL:
3019 case XFRM_MODE_BEET:
3020 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3021 m->old_family) &&
3022 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3023 m->old_family)) {
3024 match = 1;
3026 break;
3027 case XFRM_MODE_TRANSPORT:
3028 /* in case of transport mode, template does not store
3029 any IP addresses, hence we just compare mode and
3030 protocol */
3031 match = 1;
3032 break;
3033 default:
3034 break;
3037 return match;
3040 /* update endpoint address(es) of template(s) */
3041 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3042 struct xfrm_migrate *m, int num_migrate)
3044 struct xfrm_migrate *mp;
3045 int i, j, n = 0;
3047 write_lock_bh(&pol->lock);
3048 if (unlikely(pol->walk.dead)) {
3049 /* target policy has been deleted */
3050 write_unlock_bh(&pol->lock);
3051 return -ENOENT;
3054 for (i = 0; i < pol->xfrm_nr; i++) {
3055 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3056 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3057 continue;
3058 n++;
3059 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3060 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3061 continue;
3062 /* update endpoints */
3063 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3064 sizeof(pol->xfrm_vec[i].id.daddr));
3065 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3066 sizeof(pol->xfrm_vec[i].saddr));
3067 pol->xfrm_vec[i].encap_family = mp->new_family;
3068 /* flush bundles */
3069 atomic_inc(&pol->genid);
3073 write_unlock_bh(&pol->lock);
3075 if (!n)
3076 return -ENODATA;
3078 return 0;
3081 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3083 int i, j;
3085 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3086 return -EINVAL;
3088 for (i = 0; i < num_migrate; i++) {
3089 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3090 m[i].old_family) &&
3091 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3092 m[i].old_family))
3093 return -EINVAL;
3094 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3095 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3096 return -EINVAL;
3098 /* check if there is any duplicated entry */
3099 for (j = i + 1; j < num_migrate; j++) {
3100 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3101 sizeof(m[i].old_daddr)) &&
3102 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3103 sizeof(m[i].old_saddr)) &&
3104 m[i].proto == m[j].proto &&
3105 m[i].mode == m[j].mode &&
3106 m[i].reqid == m[j].reqid &&
3107 m[i].old_family == m[j].old_family)
3108 return -EINVAL;
3112 return 0;
3115 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3116 struct xfrm_migrate *m, int num_migrate,
3117 struct xfrm_kmaddress *k, struct net *net)
3119 int i, err, nx_cur = 0, nx_new = 0;
3120 struct xfrm_policy *pol = NULL;
3121 struct xfrm_state *x, *xc;
3122 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3123 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3124 struct xfrm_migrate *mp;
3126 /* Stage 0 - sanity checks */
3127 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3128 goto out;
3130 if (dir >= XFRM_POLICY_MAX) {
3131 err = -EINVAL;
3132 goto out;
3135 /* Stage 1 - find policy */
3136 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
3137 err = -ENOENT;
3138 goto out;
3141 /* Stage 2 - find and update state(s) */
3142 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3143 if ((x = xfrm_migrate_state_find(mp, net))) {
3144 x_cur[nx_cur] = x;
3145 nx_cur++;
3146 if ((xc = xfrm_state_migrate(x, mp))) {
3147 x_new[nx_new] = xc;
3148 nx_new++;
3149 } else {
3150 err = -ENODATA;
3151 goto restore_state;
3156 /* Stage 3 - update policy */
3157 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3158 goto restore_state;
3160 /* Stage 4 - delete old state(s) */
3161 if (nx_cur) {
3162 xfrm_states_put(x_cur, nx_cur);
3163 xfrm_states_delete(x_cur, nx_cur);
3166 /* Stage 5 - announce */
3167 km_migrate(sel, dir, type, m, num_migrate, k);
3169 xfrm_pol_put(pol);
3171 return 0;
3172 out:
3173 return err;
3175 restore_state:
3176 if (pol)
3177 xfrm_pol_put(pol);
3178 if (nx_cur)
3179 xfrm_states_put(x_cur, nx_cur);
3180 if (nx_new)
3181 xfrm_states_delete(x_new, nx_new);
3183 return err;
3185 EXPORT_SYMBOL(xfrm_migrate);
3186 #endif