[PATCH] IPSEC: Check validity of direction in xfrm_policy_byid
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_policy.c
blobba89293e0cd18e14543cbb26a28fe41800083ee7
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/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
21 #include <linux/notifier.h>
22 #include <linux/netdevice.h>
23 #include <linux/netfilter.h>
24 #include <linux/module.h>
25 #include <linux/cache.h>
26 #include <net/xfrm.h>
27 #include <net/ip.h>
28 #include <linux/audit.h>
30 #include "xfrm_hash.h"
32 DEFINE_MUTEX(xfrm_cfg_mutex);
33 EXPORT_SYMBOL(xfrm_cfg_mutex);
35 static DEFINE_RWLOCK(xfrm_policy_lock);
37 unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
38 EXPORT_SYMBOL(xfrm_policy_count);
40 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
41 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
43 static struct kmem_cache *xfrm_dst_cache __read_mostly;
45 static struct work_struct xfrm_policy_gc_work;
46 static HLIST_HEAD(xfrm_policy_gc_list);
47 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
49 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
50 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
51 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
52 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
54 static inline int
55 __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
57 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
58 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
59 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
60 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
61 (fl->proto == sel->proto || !sel->proto) &&
62 (fl->oif == sel->ifindex || !sel->ifindex);
65 static inline int
66 __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
68 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
69 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
70 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
72 (fl->proto == sel->proto || !sel->proto) &&
73 (fl->oif == sel->ifindex || !sel->ifindex);
76 int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
77 unsigned short family)
79 switch (family) {
80 case AF_INET:
81 return __xfrm4_selector_match(sel, fl);
82 case AF_INET6:
83 return __xfrm6_selector_match(sel, fl);
85 return 0;
88 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
90 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
91 struct xfrm_type **typemap;
92 int err = 0;
94 if (unlikely(afinfo == NULL))
95 return -EAFNOSUPPORT;
96 typemap = afinfo->type_map;
98 if (likely(typemap[type->proto] == NULL))
99 typemap[type->proto] = type;
100 else
101 err = -EEXIST;
102 xfrm_policy_unlock_afinfo(afinfo);
103 return err;
105 EXPORT_SYMBOL(xfrm_register_type);
107 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
109 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
110 struct xfrm_type **typemap;
111 int err = 0;
113 if (unlikely(afinfo == NULL))
114 return -EAFNOSUPPORT;
115 typemap = afinfo->type_map;
117 if (unlikely(typemap[type->proto] != type))
118 err = -ENOENT;
119 else
120 typemap[type->proto] = NULL;
121 xfrm_policy_unlock_afinfo(afinfo);
122 return err;
124 EXPORT_SYMBOL(xfrm_unregister_type);
126 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
128 struct xfrm_policy_afinfo *afinfo;
129 struct xfrm_type **typemap;
130 struct xfrm_type *type;
131 int modload_attempted = 0;
133 retry:
134 afinfo = xfrm_policy_get_afinfo(family);
135 if (unlikely(afinfo == NULL))
136 return NULL;
137 typemap = afinfo->type_map;
139 type = typemap[proto];
140 if (unlikely(type && !try_module_get(type->owner)))
141 type = NULL;
142 if (!type && !modload_attempted) {
143 xfrm_policy_put_afinfo(afinfo);
144 request_module("xfrm-type-%d-%d",
145 (int) family, (int) proto);
146 modload_attempted = 1;
147 goto retry;
150 xfrm_policy_put_afinfo(afinfo);
151 return type;
154 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
155 unsigned short family)
157 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
158 int err = 0;
160 if (unlikely(afinfo == NULL))
161 return -EAFNOSUPPORT;
163 if (likely(afinfo->dst_lookup != NULL))
164 err = afinfo->dst_lookup(dst, fl);
165 else
166 err = -EINVAL;
167 xfrm_policy_put_afinfo(afinfo);
168 return err;
170 EXPORT_SYMBOL(xfrm_dst_lookup);
172 void xfrm_put_type(struct xfrm_type *type)
174 module_put(type->owner);
177 int xfrm_register_mode(struct xfrm_mode *mode, int family)
179 struct xfrm_policy_afinfo *afinfo;
180 struct xfrm_mode **modemap;
181 int err;
183 if (unlikely(mode->encap >= XFRM_MODE_MAX))
184 return -EINVAL;
186 afinfo = xfrm_policy_lock_afinfo(family);
187 if (unlikely(afinfo == NULL))
188 return -EAFNOSUPPORT;
190 err = -EEXIST;
191 modemap = afinfo->mode_map;
192 if (likely(modemap[mode->encap] == NULL)) {
193 modemap[mode->encap] = mode;
194 err = 0;
197 xfrm_policy_unlock_afinfo(afinfo);
198 return err;
200 EXPORT_SYMBOL(xfrm_register_mode);
202 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
204 struct xfrm_policy_afinfo *afinfo;
205 struct xfrm_mode **modemap;
206 int err;
208 if (unlikely(mode->encap >= XFRM_MODE_MAX))
209 return -EINVAL;
211 afinfo = xfrm_policy_lock_afinfo(family);
212 if (unlikely(afinfo == NULL))
213 return -EAFNOSUPPORT;
215 err = -ENOENT;
216 modemap = afinfo->mode_map;
217 if (likely(modemap[mode->encap] == mode)) {
218 modemap[mode->encap] = NULL;
219 err = 0;
222 xfrm_policy_unlock_afinfo(afinfo);
223 return err;
225 EXPORT_SYMBOL(xfrm_unregister_mode);
227 struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
229 struct xfrm_policy_afinfo *afinfo;
230 struct xfrm_mode *mode;
231 int modload_attempted = 0;
233 if (unlikely(encap >= XFRM_MODE_MAX))
234 return NULL;
236 retry:
237 afinfo = xfrm_policy_get_afinfo(family);
238 if (unlikely(afinfo == NULL))
239 return NULL;
241 mode = afinfo->mode_map[encap];
242 if (unlikely(mode && !try_module_get(mode->owner)))
243 mode = NULL;
244 if (!mode && !modload_attempted) {
245 xfrm_policy_put_afinfo(afinfo);
246 request_module("xfrm-mode-%d-%d", family, encap);
247 modload_attempted = 1;
248 goto retry;
251 xfrm_policy_put_afinfo(afinfo);
252 return mode;
255 void xfrm_put_mode(struct xfrm_mode *mode)
257 module_put(mode->owner);
260 static inline unsigned long make_jiffies(long secs)
262 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
263 return MAX_SCHEDULE_TIMEOUT-1;
264 else
265 return secs*HZ;
268 static void xfrm_policy_timer(unsigned long data)
270 struct xfrm_policy *xp = (struct xfrm_policy*)data;
271 unsigned long now = (unsigned long)xtime.tv_sec;
272 long next = LONG_MAX;
273 int warn = 0;
274 int dir;
276 read_lock(&xp->lock);
278 if (xp->dead)
279 goto out;
281 dir = xfrm_policy_id2dir(xp->index);
283 if (xp->lft.hard_add_expires_seconds) {
284 long tmo = xp->lft.hard_add_expires_seconds +
285 xp->curlft.add_time - now;
286 if (tmo <= 0)
287 goto expired;
288 if (tmo < next)
289 next = tmo;
291 if (xp->lft.hard_use_expires_seconds) {
292 long tmo = xp->lft.hard_use_expires_seconds +
293 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
294 if (tmo <= 0)
295 goto expired;
296 if (tmo < next)
297 next = tmo;
299 if (xp->lft.soft_add_expires_seconds) {
300 long tmo = xp->lft.soft_add_expires_seconds +
301 xp->curlft.add_time - now;
302 if (tmo <= 0) {
303 warn = 1;
304 tmo = XFRM_KM_TIMEOUT;
306 if (tmo < next)
307 next = tmo;
309 if (xp->lft.soft_use_expires_seconds) {
310 long tmo = xp->lft.soft_use_expires_seconds +
311 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
312 if (tmo <= 0) {
313 warn = 1;
314 tmo = XFRM_KM_TIMEOUT;
316 if (tmo < next)
317 next = tmo;
320 if (warn)
321 km_policy_expired(xp, dir, 0, 0);
322 if (next != LONG_MAX &&
323 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
324 xfrm_pol_hold(xp);
326 out:
327 read_unlock(&xp->lock);
328 xfrm_pol_put(xp);
329 return;
331 expired:
332 read_unlock(&xp->lock);
333 if (!xfrm_policy_delete(xp, dir))
334 km_policy_expired(xp, dir, 1, 0);
335 xfrm_pol_put(xp);
339 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
340 * SPD calls.
343 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
345 struct xfrm_policy *policy;
347 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
349 if (policy) {
350 INIT_HLIST_NODE(&policy->bydst);
351 INIT_HLIST_NODE(&policy->byidx);
352 rwlock_init(&policy->lock);
353 atomic_set(&policy->refcnt, 1);
354 init_timer(&policy->timer);
355 policy->timer.data = (unsigned long)policy;
356 policy->timer.function = xfrm_policy_timer;
358 return policy;
360 EXPORT_SYMBOL(xfrm_policy_alloc);
362 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
364 void __xfrm_policy_destroy(struct xfrm_policy *policy)
366 BUG_ON(!policy->dead);
368 BUG_ON(policy->bundles);
370 if (del_timer(&policy->timer))
371 BUG();
373 security_xfrm_policy_free(policy);
374 kfree(policy);
376 EXPORT_SYMBOL(__xfrm_policy_destroy);
378 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
380 struct dst_entry *dst;
382 while ((dst = policy->bundles) != NULL) {
383 policy->bundles = dst->next;
384 dst_free(dst);
387 if (del_timer(&policy->timer))
388 atomic_dec(&policy->refcnt);
390 if (atomic_read(&policy->refcnt) > 1)
391 flow_cache_flush();
393 xfrm_pol_put(policy);
396 static void xfrm_policy_gc_task(struct work_struct *work)
398 struct xfrm_policy *policy;
399 struct hlist_node *entry, *tmp;
400 struct hlist_head gc_list;
402 spin_lock_bh(&xfrm_policy_gc_lock);
403 gc_list.first = xfrm_policy_gc_list.first;
404 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
405 spin_unlock_bh(&xfrm_policy_gc_lock);
407 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
408 xfrm_policy_gc_kill(policy);
411 /* Rule must be locked. Release descentant resources, announce
412 * entry dead. The rule must be unlinked from lists to the moment.
415 static void xfrm_policy_kill(struct xfrm_policy *policy)
417 int dead;
419 write_lock_bh(&policy->lock);
420 dead = policy->dead;
421 policy->dead = 1;
422 write_unlock_bh(&policy->lock);
424 if (unlikely(dead)) {
425 WARN_ON(1);
426 return;
429 spin_lock(&xfrm_policy_gc_lock);
430 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
431 spin_unlock(&xfrm_policy_gc_lock);
433 schedule_work(&xfrm_policy_gc_work);
436 struct xfrm_policy_hash {
437 struct hlist_head *table;
438 unsigned int hmask;
441 static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
442 static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
443 static struct hlist_head *xfrm_policy_byidx __read_mostly;
444 static unsigned int xfrm_idx_hmask __read_mostly;
445 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
447 static inline unsigned int idx_hash(u32 index)
449 return __idx_hash(index, xfrm_idx_hmask);
452 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
454 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
455 unsigned int hash = __sel_hash(sel, family, hmask);
457 return (hash == hmask + 1 ?
458 &xfrm_policy_inexact[dir] :
459 xfrm_policy_bydst[dir].table + hash);
462 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
464 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
465 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
467 return xfrm_policy_bydst[dir].table + hash;
470 static void xfrm_dst_hash_transfer(struct hlist_head *list,
471 struct hlist_head *ndsttable,
472 unsigned int nhashmask)
474 struct hlist_node *entry, *tmp;
475 struct xfrm_policy *pol;
477 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
478 unsigned int h;
480 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
481 pol->family, nhashmask);
482 hlist_add_head(&pol->bydst, ndsttable+h);
486 static void xfrm_idx_hash_transfer(struct hlist_head *list,
487 struct hlist_head *nidxtable,
488 unsigned int nhashmask)
490 struct hlist_node *entry, *tmp;
491 struct xfrm_policy *pol;
493 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
494 unsigned int h;
496 h = __idx_hash(pol->index, nhashmask);
497 hlist_add_head(&pol->byidx, nidxtable+h);
501 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
503 return ((old_hmask + 1) << 1) - 1;
506 static void xfrm_bydst_resize(int dir)
508 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
509 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
510 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
511 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
512 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
513 int i;
515 if (!ndst)
516 return;
518 write_lock_bh(&xfrm_policy_lock);
520 for (i = hmask; i >= 0; i--)
521 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
523 xfrm_policy_bydst[dir].table = ndst;
524 xfrm_policy_bydst[dir].hmask = nhashmask;
526 write_unlock_bh(&xfrm_policy_lock);
528 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
531 static void xfrm_byidx_resize(int total)
533 unsigned int hmask = xfrm_idx_hmask;
534 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
535 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
536 struct hlist_head *oidx = xfrm_policy_byidx;
537 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
538 int i;
540 if (!nidx)
541 return;
543 write_lock_bh(&xfrm_policy_lock);
545 for (i = hmask; i >= 0; i--)
546 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
548 xfrm_policy_byidx = nidx;
549 xfrm_idx_hmask = nhashmask;
551 write_unlock_bh(&xfrm_policy_lock);
553 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
556 static inline int xfrm_bydst_should_resize(int dir, int *total)
558 unsigned int cnt = xfrm_policy_count[dir];
559 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
561 if (total)
562 *total += cnt;
564 if ((hmask + 1) < xfrm_policy_hashmax &&
565 cnt > hmask)
566 return 1;
568 return 0;
571 static inline int xfrm_byidx_should_resize(int total)
573 unsigned int hmask = xfrm_idx_hmask;
575 if ((hmask + 1) < xfrm_policy_hashmax &&
576 total > hmask)
577 return 1;
579 return 0;
582 static DEFINE_MUTEX(hash_resize_mutex);
584 static void xfrm_hash_resize(struct work_struct *__unused)
586 int dir, total;
588 mutex_lock(&hash_resize_mutex);
590 total = 0;
591 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
592 if (xfrm_bydst_should_resize(dir, &total))
593 xfrm_bydst_resize(dir);
595 if (xfrm_byidx_should_resize(total))
596 xfrm_byidx_resize(total);
598 mutex_unlock(&hash_resize_mutex);
601 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
603 /* Generate new index... KAME seems to generate them ordered by cost
604 * of an absolute inpredictability of ordering of rules. This will not pass. */
605 static u32 xfrm_gen_index(u8 type, int dir)
607 static u32 idx_generator;
609 for (;;) {
610 struct hlist_node *entry;
611 struct hlist_head *list;
612 struct xfrm_policy *p;
613 u32 idx;
614 int found;
616 idx = (idx_generator | dir);
617 idx_generator += 8;
618 if (idx == 0)
619 idx = 8;
620 list = xfrm_policy_byidx + idx_hash(idx);
621 found = 0;
622 hlist_for_each_entry(p, entry, list, byidx) {
623 if (p->index == idx) {
624 found = 1;
625 break;
628 if (!found)
629 return idx;
633 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
635 u32 *p1 = (u32 *) s1;
636 u32 *p2 = (u32 *) s2;
637 int len = sizeof(struct xfrm_selector) / sizeof(u32);
638 int i;
640 for (i = 0; i < len; i++) {
641 if (p1[i] != p2[i])
642 return 1;
645 return 0;
648 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
650 struct xfrm_policy *pol;
651 struct xfrm_policy *delpol;
652 struct hlist_head *chain;
653 struct hlist_node *entry, *newpos;
654 struct dst_entry *gc_list;
656 write_lock_bh(&xfrm_policy_lock);
657 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
658 delpol = NULL;
659 newpos = NULL;
660 hlist_for_each_entry(pol, entry, chain, bydst) {
661 if (pol->type == policy->type &&
662 !selector_cmp(&pol->selector, &policy->selector) &&
663 xfrm_sec_ctx_match(pol->security, policy->security) &&
664 !WARN_ON(delpol)) {
665 if (excl) {
666 write_unlock_bh(&xfrm_policy_lock);
667 return -EEXIST;
669 delpol = pol;
670 if (policy->priority > pol->priority)
671 continue;
672 } else if (policy->priority >= pol->priority) {
673 newpos = &pol->bydst;
674 continue;
676 if (delpol)
677 break;
679 if (newpos)
680 hlist_add_after(newpos, &policy->bydst);
681 else
682 hlist_add_head(&policy->bydst, chain);
683 xfrm_pol_hold(policy);
684 xfrm_policy_count[dir]++;
685 atomic_inc(&flow_cache_genid);
686 if (delpol) {
687 hlist_del(&delpol->bydst);
688 hlist_del(&delpol->byidx);
689 xfrm_policy_count[dir]--;
691 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
692 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
693 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
694 policy->curlft.use_time = 0;
695 if (!mod_timer(&policy->timer, jiffies + HZ))
696 xfrm_pol_hold(policy);
697 write_unlock_bh(&xfrm_policy_lock);
699 if (delpol)
700 xfrm_policy_kill(delpol);
701 else if (xfrm_bydst_should_resize(dir, NULL))
702 schedule_work(&xfrm_hash_work);
704 read_lock_bh(&xfrm_policy_lock);
705 gc_list = NULL;
706 entry = &policy->bydst;
707 hlist_for_each_entry_continue(policy, entry, bydst) {
708 struct dst_entry *dst;
710 write_lock(&policy->lock);
711 dst = policy->bundles;
712 if (dst) {
713 struct dst_entry *tail = dst;
714 while (tail->next)
715 tail = tail->next;
716 tail->next = gc_list;
717 gc_list = dst;
719 policy->bundles = NULL;
721 write_unlock(&policy->lock);
723 read_unlock_bh(&xfrm_policy_lock);
725 while (gc_list) {
726 struct dst_entry *dst = gc_list;
728 gc_list = dst->next;
729 dst_free(dst);
732 return 0;
734 EXPORT_SYMBOL(xfrm_policy_insert);
736 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
737 struct xfrm_selector *sel,
738 struct xfrm_sec_ctx *ctx, int delete,
739 int *err)
741 struct xfrm_policy *pol, *ret;
742 struct hlist_head *chain;
743 struct hlist_node *entry;
745 *err = 0;
746 write_lock_bh(&xfrm_policy_lock);
747 chain = policy_hash_bysel(sel, sel->family, dir);
748 ret = NULL;
749 hlist_for_each_entry(pol, entry, chain, bydst) {
750 if (pol->type == type &&
751 !selector_cmp(sel, &pol->selector) &&
752 xfrm_sec_ctx_match(ctx, pol->security)) {
753 xfrm_pol_hold(pol);
754 if (delete) {
755 *err = security_xfrm_policy_delete(pol);
756 if (*err) {
757 write_unlock_bh(&xfrm_policy_lock);
758 return pol;
760 hlist_del(&pol->bydst);
761 hlist_del(&pol->byidx);
762 xfrm_policy_count[dir]--;
764 ret = pol;
765 break;
768 write_unlock_bh(&xfrm_policy_lock);
770 if (ret && delete) {
771 atomic_inc(&flow_cache_genid);
772 xfrm_policy_kill(ret);
774 return ret;
776 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
778 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
779 int *err)
781 struct xfrm_policy *pol, *ret;
782 struct hlist_head *chain;
783 struct hlist_node *entry;
785 *err = -ENOENT;
786 if (xfrm_policy_id2dir(id) != dir)
787 return NULL;
789 *err = 0;
790 write_lock_bh(&xfrm_policy_lock);
791 chain = xfrm_policy_byidx + idx_hash(id);
792 ret = NULL;
793 hlist_for_each_entry(pol, entry, chain, byidx) {
794 if (pol->type == type && pol->index == id) {
795 xfrm_pol_hold(pol);
796 if (delete) {
797 *err = security_xfrm_policy_delete(pol);
798 if (*err) {
799 write_unlock_bh(&xfrm_policy_lock);
800 return pol;
802 hlist_del(&pol->bydst);
803 hlist_del(&pol->byidx);
804 xfrm_policy_count[dir]--;
806 ret = pol;
807 break;
810 write_unlock_bh(&xfrm_policy_lock);
812 if (ret && delete) {
813 atomic_inc(&flow_cache_genid);
814 xfrm_policy_kill(ret);
816 return ret;
818 EXPORT_SYMBOL(xfrm_policy_byid);
820 void xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
822 int dir;
824 write_lock_bh(&xfrm_policy_lock);
825 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
826 struct xfrm_policy *pol;
827 struct hlist_node *entry;
828 int i, killed;
830 killed = 0;
831 again1:
832 hlist_for_each_entry(pol, entry,
833 &xfrm_policy_inexact[dir], bydst) {
834 if (pol->type != type)
835 continue;
836 hlist_del(&pol->bydst);
837 hlist_del(&pol->byidx);
838 write_unlock_bh(&xfrm_policy_lock);
840 xfrm_audit_log(audit_info->loginuid, audit_info->secid,
841 AUDIT_MAC_IPSEC_DELSPD, 1, pol, NULL);
843 xfrm_policy_kill(pol);
844 killed++;
846 write_lock_bh(&xfrm_policy_lock);
847 goto again1;
850 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
851 again2:
852 hlist_for_each_entry(pol, entry,
853 xfrm_policy_bydst[dir].table + i,
854 bydst) {
855 if (pol->type != type)
856 continue;
857 hlist_del(&pol->bydst);
858 hlist_del(&pol->byidx);
859 write_unlock_bh(&xfrm_policy_lock);
861 xfrm_audit_log(audit_info->loginuid,
862 audit_info->secid,
863 AUDIT_MAC_IPSEC_DELSPD, 1,
864 pol, NULL);
866 xfrm_policy_kill(pol);
867 killed++;
869 write_lock_bh(&xfrm_policy_lock);
870 goto again2;
874 xfrm_policy_count[dir] -= killed;
876 atomic_inc(&flow_cache_genid);
877 write_unlock_bh(&xfrm_policy_lock);
879 EXPORT_SYMBOL(xfrm_policy_flush);
881 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
882 void *data)
884 struct xfrm_policy *pol, *last = NULL;
885 struct hlist_node *entry;
886 int dir, last_dir = 0, count, error;
888 read_lock_bh(&xfrm_policy_lock);
889 count = 0;
891 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
892 struct hlist_head *table = xfrm_policy_bydst[dir].table;
893 int i;
895 hlist_for_each_entry(pol, entry,
896 &xfrm_policy_inexact[dir], bydst) {
897 if (pol->type != type)
898 continue;
899 if (last) {
900 error = func(last, last_dir % XFRM_POLICY_MAX,
901 count, data);
902 if (error)
903 goto out;
905 last = pol;
906 last_dir = dir;
907 count++;
909 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
910 hlist_for_each_entry(pol, entry, table + i, bydst) {
911 if (pol->type != type)
912 continue;
913 if (last) {
914 error = func(last, last_dir % XFRM_POLICY_MAX,
915 count, data);
916 if (error)
917 goto out;
919 last = pol;
920 last_dir = dir;
921 count++;
925 if (count == 0) {
926 error = -ENOENT;
927 goto out;
929 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
930 out:
931 read_unlock_bh(&xfrm_policy_lock);
932 return error;
934 EXPORT_SYMBOL(xfrm_policy_walk);
937 * Find policy to apply to this flow.
939 * Returns 0 if policy found, else an -errno.
941 static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
942 u8 type, u16 family, int dir)
944 struct xfrm_selector *sel = &pol->selector;
945 int match, ret = -ESRCH;
947 if (pol->family != family ||
948 pol->type != type)
949 return ret;
951 match = xfrm_selector_match(sel, fl, family);
952 if (match)
953 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
955 return ret;
958 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
959 u16 family, u8 dir)
961 int err;
962 struct xfrm_policy *pol, *ret;
963 xfrm_address_t *daddr, *saddr;
964 struct hlist_node *entry;
965 struct hlist_head *chain;
966 u32 priority = ~0U;
968 daddr = xfrm_flowi_daddr(fl, family);
969 saddr = xfrm_flowi_saddr(fl, family);
970 if (unlikely(!daddr || !saddr))
971 return NULL;
973 read_lock_bh(&xfrm_policy_lock);
974 chain = policy_hash_direct(daddr, saddr, family, dir);
975 ret = NULL;
976 hlist_for_each_entry(pol, entry, chain, bydst) {
977 err = xfrm_policy_match(pol, fl, type, family, dir);
978 if (err) {
979 if (err == -ESRCH)
980 continue;
981 else {
982 ret = ERR_PTR(err);
983 goto fail;
985 } else {
986 ret = pol;
987 priority = ret->priority;
988 break;
991 chain = &xfrm_policy_inexact[dir];
992 hlist_for_each_entry(pol, entry, chain, bydst) {
993 err = xfrm_policy_match(pol, fl, type, family, dir);
994 if (err) {
995 if (err == -ESRCH)
996 continue;
997 else {
998 ret = ERR_PTR(err);
999 goto fail;
1001 } else if (pol->priority < priority) {
1002 ret = pol;
1003 break;
1006 if (ret)
1007 xfrm_pol_hold(ret);
1008 fail:
1009 read_unlock_bh(&xfrm_policy_lock);
1011 return ret;
1014 static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
1015 void **objp, atomic_t **obj_refp)
1017 struct xfrm_policy *pol;
1018 int err = 0;
1020 #ifdef CONFIG_XFRM_SUB_POLICY
1021 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
1022 if (IS_ERR(pol)) {
1023 err = PTR_ERR(pol);
1024 pol = NULL;
1026 if (pol || err)
1027 goto end;
1028 #endif
1029 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1030 if (IS_ERR(pol)) {
1031 err = PTR_ERR(pol);
1032 pol = NULL;
1034 #ifdef CONFIG_XFRM_SUB_POLICY
1035 end:
1036 #endif
1037 if ((*objp = (void *) pol) != NULL)
1038 *obj_refp = &pol->refcnt;
1039 return err;
1042 static inline int policy_to_flow_dir(int dir)
1044 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1045 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1046 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1047 return dir;
1048 switch (dir) {
1049 default:
1050 case XFRM_POLICY_IN:
1051 return FLOW_DIR_IN;
1052 case XFRM_POLICY_OUT:
1053 return FLOW_DIR_OUT;
1054 case XFRM_POLICY_FWD:
1055 return FLOW_DIR_FWD;
1059 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1061 struct xfrm_policy *pol;
1063 read_lock_bh(&xfrm_policy_lock);
1064 if ((pol = sk->sk_policy[dir]) != NULL) {
1065 int match = xfrm_selector_match(&pol->selector, fl,
1066 sk->sk_family);
1067 int err = 0;
1069 if (match) {
1070 err = security_xfrm_policy_lookup(pol, fl->secid,
1071 policy_to_flow_dir(dir));
1072 if (!err)
1073 xfrm_pol_hold(pol);
1074 else if (err == -ESRCH)
1075 pol = NULL;
1076 else
1077 pol = ERR_PTR(err);
1078 } else
1079 pol = NULL;
1081 read_unlock_bh(&xfrm_policy_lock);
1082 return pol;
1085 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1087 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1088 pol->family, dir);
1090 hlist_add_head(&pol->bydst, chain);
1091 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1092 xfrm_policy_count[dir]++;
1093 xfrm_pol_hold(pol);
1095 if (xfrm_bydst_should_resize(dir, NULL))
1096 schedule_work(&xfrm_hash_work);
1099 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1100 int dir)
1102 if (hlist_unhashed(&pol->bydst))
1103 return NULL;
1105 hlist_del(&pol->bydst);
1106 hlist_del(&pol->byidx);
1107 xfrm_policy_count[dir]--;
1109 return pol;
1112 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1114 write_lock_bh(&xfrm_policy_lock);
1115 pol = __xfrm_policy_unlink(pol, dir);
1116 write_unlock_bh(&xfrm_policy_lock);
1117 if (pol) {
1118 if (dir < XFRM_POLICY_MAX)
1119 atomic_inc(&flow_cache_genid);
1120 xfrm_policy_kill(pol);
1121 return 0;
1123 return -ENOENT;
1125 EXPORT_SYMBOL(xfrm_policy_delete);
1127 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1129 struct xfrm_policy *old_pol;
1131 #ifdef CONFIG_XFRM_SUB_POLICY
1132 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1133 return -EINVAL;
1134 #endif
1136 write_lock_bh(&xfrm_policy_lock);
1137 old_pol = sk->sk_policy[dir];
1138 sk->sk_policy[dir] = pol;
1139 if (pol) {
1140 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
1141 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1142 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1144 if (old_pol)
1145 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1146 write_unlock_bh(&xfrm_policy_lock);
1148 if (old_pol) {
1149 xfrm_policy_kill(old_pol);
1151 return 0;
1154 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1156 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1158 if (newp) {
1159 newp->selector = old->selector;
1160 if (security_xfrm_policy_clone(old, newp)) {
1161 kfree(newp);
1162 return NULL; /* ENOMEM */
1164 newp->lft = old->lft;
1165 newp->curlft = old->curlft;
1166 newp->action = old->action;
1167 newp->flags = old->flags;
1168 newp->xfrm_nr = old->xfrm_nr;
1169 newp->index = old->index;
1170 newp->type = old->type;
1171 memcpy(newp->xfrm_vec, old->xfrm_vec,
1172 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1173 write_lock_bh(&xfrm_policy_lock);
1174 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1175 write_unlock_bh(&xfrm_policy_lock);
1176 xfrm_pol_put(newp);
1178 return newp;
1181 int __xfrm_sk_clone_policy(struct sock *sk)
1183 struct xfrm_policy *p0 = sk->sk_policy[0],
1184 *p1 = sk->sk_policy[1];
1186 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1187 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1188 return -ENOMEM;
1189 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1190 return -ENOMEM;
1191 return 0;
1194 static int
1195 xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1196 unsigned short family)
1198 int err;
1199 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1201 if (unlikely(afinfo == NULL))
1202 return -EINVAL;
1203 err = afinfo->get_saddr(local, remote);
1204 xfrm_policy_put_afinfo(afinfo);
1205 return err;
1208 /* Resolve list of templates for the flow, given policy. */
1210 static int
1211 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1212 struct xfrm_state **xfrm,
1213 unsigned short family)
1215 int nx;
1216 int i, error;
1217 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1218 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1219 xfrm_address_t tmp;
1221 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1222 struct xfrm_state *x;
1223 xfrm_address_t *remote = daddr;
1224 xfrm_address_t *local = saddr;
1225 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1227 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1228 remote = &tmpl->id.daddr;
1229 local = &tmpl->saddr;
1230 family = tmpl->encap_family;
1231 if (xfrm_addr_any(local, family)) {
1232 error = xfrm_get_saddr(&tmp, remote, family);
1233 if (error)
1234 goto fail;
1235 local = &tmp;
1239 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1241 if (x && x->km.state == XFRM_STATE_VALID) {
1242 xfrm[nx++] = x;
1243 daddr = remote;
1244 saddr = local;
1245 continue;
1247 if (x) {
1248 error = (x->km.state == XFRM_STATE_ERROR ?
1249 -EINVAL : -EAGAIN);
1250 xfrm_state_put(x);
1253 if (!tmpl->optional)
1254 goto fail;
1256 return nx;
1258 fail:
1259 for (nx--; nx>=0; nx--)
1260 xfrm_state_put(xfrm[nx]);
1261 return error;
1264 static int
1265 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1266 struct xfrm_state **xfrm,
1267 unsigned short family)
1269 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1270 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1271 int cnx = 0;
1272 int error;
1273 int ret;
1274 int i;
1276 for (i = 0; i < npols; i++) {
1277 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1278 error = -ENOBUFS;
1279 goto fail;
1282 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1283 if (ret < 0) {
1284 error = ret;
1285 goto fail;
1286 } else
1287 cnx += ret;
1290 /* found states are sorted for outbound processing */
1291 if (npols > 1)
1292 xfrm_state_sort(xfrm, tpp, cnx, family);
1294 return cnx;
1296 fail:
1297 for (cnx--; cnx>=0; cnx--)
1298 xfrm_state_put(tpp[cnx]);
1299 return error;
1303 /* Check that the bundle accepts the flow and its components are
1304 * still valid.
1307 static struct dst_entry *
1308 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1310 struct dst_entry *x;
1311 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1312 if (unlikely(afinfo == NULL))
1313 return ERR_PTR(-EINVAL);
1314 x = afinfo->find_bundle(fl, policy);
1315 xfrm_policy_put_afinfo(afinfo);
1316 return x;
1319 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1320 * all the metrics... Shortly, bundle a bundle.
1323 static int
1324 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1325 struct flowi *fl, struct dst_entry **dst_p,
1326 unsigned short family)
1328 int err;
1329 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1330 if (unlikely(afinfo == NULL))
1331 return -EINVAL;
1332 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1333 xfrm_policy_put_afinfo(afinfo);
1334 return err;
1338 static int stale_bundle(struct dst_entry *dst);
1340 /* Main function: finds/creates a bundle for given flow.
1342 * At the moment we eat a raw IP route. Mostly to speed up lookups
1343 * on interfaces with disabled IPsec.
1345 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1346 struct sock *sk, int flags)
1348 struct xfrm_policy *policy;
1349 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1350 int npols;
1351 int pol_dead;
1352 int xfrm_nr;
1353 int pi;
1354 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1355 struct dst_entry *dst, *dst_orig = *dst_p;
1356 int nx = 0;
1357 int err;
1358 u32 genid;
1359 u16 family;
1360 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1362 restart:
1363 genid = atomic_read(&flow_cache_genid);
1364 policy = NULL;
1365 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1366 pols[pi] = NULL;
1367 npols = 0;
1368 pol_dead = 0;
1369 xfrm_nr = 0;
1371 if (sk && sk->sk_policy[1]) {
1372 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1373 if (IS_ERR(policy))
1374 return PTR_ERR(policy);
1377 if (!policy) {
1378 /* To accelerate a bit... */
1379 if ((dst_orig->flags & DST_NOXFRM) ||
1380 !xfrm_policy_count[XFRM_POLICY_OUT])
1381 return 0;
1383 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1384 dir, xfrm_policy_lookup);
1385 if (IS_ERR(policy))
1386 return PTR_ERR(policy);
1389 if (!policy)
1390 return 0;
1392 family = dst_orig->ops->family;
1393 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
1394 pols[0] = policy;
1395 npols ++;
1396 xfrm_nr += pols[0]->xfrm_nr;
1398 switch (policy->action) {
1399 case XFRM_POLICY_BLOCK:
1400 /* Prohibit the flow */
1401 err = -EPERM;
1402 goto error;
1404 case XFRM_POLICY_ALLOW:
1405 #ifndef CONFIG_XFRM_SUB_POLICY
1406 if (policy->xfrm_nr == 0) {
1407 /* Flow passes not transformed. */
1408 xfrm_pol_put(policy);
1409 return 0;
1411 #endif
1413 /* Try to find matching bundle.
1415 * LATER: help from flow cache. It is optional, this
1416 * is required only for output policy.
1418 dst = xfrm_find_bundle(fl, policy, family);
1419 if (IS_ERR(dst)) {
1420 err = PTR_ERR(dst);
1421 goto error;
1424 if (dst)
1425 break;
1427 #ifdef CONFIG_XFRM_SUB_POLICY
1428 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1429 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1430 fl, family,
1431 XFRM_POLICY_OUT);
1432 if (pols[1]) {
1433 if (IS_ERR(pols[1])) {
1434 err = PTR_ERR(pols[1]);
1435 goto error;
1437 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1438 err = -EPERM;
1439 goto error;
1441 npols ++;
1442 xfrm_nr += pols[1]->xfrm_nr;
1447 * Because neither flowi nor bundle information knows about
1448 * transformation template size. On more than one policy usage
1449 * we can realize whether all of them is bypass or not after
1450 * they are searched. See above not-transformed bypass
1451 * is surrounded by non-sub policy configuration, too.
1453 if (xfrm_nr == 0) {
1454 /* Flow passes not transformed. */
1455 xfrm_pols_put(pols, npols);
1456 return 0;
1459 #endif
1460 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1462 if (unlikely(nx<0)) {
1463 err = nx;
1464 if (err == -EAGAIN && flags) {
1465 DECLARE_WAITQUEUE(wait, current);
1467 add_wait_queue(&km_waitq, &wait);
1468 set_current_state(TASK_INTERRUPTIBLE);
1469 schedule();
1470 set_current_state(TASK_RUNNING);
1471 remove_wait_queue(&km_waitq, &wait);
1473 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1475 if (nx == -EAGAIN && signal_pending(current)) {
1476 err = -ERESTART;
1477 goto error;
1479 if (nx == -EAGAIN ||
1480 genid != atomic_read(&flow_cache_genid)) {
1481 xfrm_pols_put(pols, npols);
1482 goto restart;
1484 err = nx;
1486 if (err < 0)
1487 goto error;
1489 if (nx == 0) {
1490 /* Flow passes not transformed. */
1491 xfrm_pols_put(pols, npols);
1492 return 0;
1495 dst = dst_orig;
1496 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1498 if (unlikely(err)) {
1499 int i;
1500 for (i=0; i<nx; i++)
1501 xfrm_state_put(xfrm[i]);
1502 goto error;
1505 for (pi = 0; pi < npols; pi++) {
1506 read_lock_bh(&pols[pi]->lock);
1507 pol_dead |= pols[pi]->dead;
1508 read_unlock_bh(&pols[pi]->lock);
1511 write_lock_bh(&policy->lock);
1512 if (unlikely(pol_dead || stale_bundle(dst))) {
1513 /* Wow! While we worked on resolving, this
1514 * policy has gone. Retry. It is not paranoia,
1515 * we just cannot enlist new bundle to dead object.
1516 * We can't enlist stable bundles either.
1518 write_unlock_bh(&policy->lock);
1519 if (dst)
1520 dst_free(dst);
1522 err = -EHOSTUNREACH;
1523 goto error;
1525 dst->next = policy->bundles;
1526 policy->bundles = dst;
1527 dst_hold(dst);
1528 write_unlock_bh(&policy->lock);
1530 *dst_p = dst;
1531 dst_release(dst_orig);
1532 xfrm_pols_put(pols, npols);
1533 return 0;
1535 error:
1536 dst_release(dst_orig);
1537 xfrm_pols_put(pols, npols);
1538 *dst_p = NULL;
1539 return err;
1541 EXPORT_SYMBOL(xfrm_lookup);
1543 static inline int
1544 xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1546 struct xfrm_state *x;
1547 int err;
1549 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1550 return 0;
1551 x = skb->sp->xvec[idx];
1552 if (!x->type->reject)
1553 return 0;
1554 xfrm_state_hold(x);
1555 err = x->type->reject(x, skb, fl);
1556 xfrm_state_put(x);
1557 return err;
1560 /* When skb is transformed back to its "native" form, we have to
1561 * check policy restrictions. At the moment we make this in maximally
1562 * stupid way. Shame on me. :-) Of course, connected sockets must
1563 * have policy cached at them.
1566 static inline int
1567 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1568 unsigned short family)
1570 if (xfrm_state_kern(x))
1571 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
1572 return x->id.proto == tmpl->id.proto &&
1573 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1574 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1575 x->props.mode == tmpl->mode &&
1576 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1577 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1578 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1579 xfrm_state_addr_cmp(tmpl, x, family));
1583 * 0 or more than 0 is returned when validation is succeeded (either bypass
1584 * because of optional transport mode, or next index of the mathced secpath
1585 * state with the template.
1586 * -1 is returned when no matching template is found.
1587 * Otherwise "-2 - errored_index" is returned.
1589 static inline int
1590 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1591 unsigned short family)
1593 int idx = start;
1595 if (tmpl->optional) {
1596 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1597 return start;
1598 } else
1599 start = -1;
1600 for (; idx < sp->len; idx++) {
1601 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1602 return ++idx;
1603 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1604 if (start == -1)
1605 start = -2-idx;
1606 break;
1609 return start;
1613 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1615 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1616 int err;
1618 if (unlikely(afinfo == NULL))
1619 return -EAFNOSUPPORT;
1621 afinfo->decode_session(skb, fl);
1622 err = security_xfrm_decode_session(skb, &fl->secid);
1623 xfrm_policy_put_afinfo(afinfo);
1624 return err;
1626 EXPORT_SYMBOL(xfrm_decode_session);
1628 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1630 for (; k < sp->len; k++) {
1631 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1632 *idxp = k;
1633 return 1;
1637 return 0;
1640 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1641 unsigned short family)
1643 struct xfrm_policy *pol;
1644 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1645 int npols = 0;
1646 int xfrm_nr;
1647 int pi;
1648 struct flowi fl;
1649 u8 fl_dir = policy_to_flow_dir(dir);
1650 int xerr_idx = -1;
1652 if (xfrm_decode_session(skb, &fl, family) < 0)
1653 return 0;
1654 nf_nat_decode_session(skb, &fl, family);
1656 /* First, check used SA against their selectors. */
1657 if (skb->sp) {
1658 int i;
1660 for (i=skb->sp->len-1; i>=0; i--) {
1661 struct xfrm_state *x = skb->sp->xvec[i];
1662 if (!xfrm_selector_match(&x->sel, &fl, family))
1663 return 0;
1667 pol = NULL;
1668 if (sk && sk->sk_policy[dir]) {
1669 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1670 if (IS_ERR(pol))
1671 return 0;
1674 if (!pol)
1675 pol = flow_cache_lookup(&fl, family, fl_dir,
1676 xfrm_policy_lookup);
1678 if (IS_ERR(pol))
1679 return 0;
1681 if (!pol) {
1682 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
1683 xfrm_secpath_reject(xerr_idx, skb, &fl);
1684 return 0;
1686 return 1;
1689 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1691 pols[0] = pol;
1692 npols ++;
1693 #ifdef CONFIG_XFRM_SUB_POLICY
1694 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1695 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1696 &fl, family,
1697 XFRM_POLICY_IN);
1698 if (pols[1]) {
1699 if (IS_ERR(pols[1]))
1700 return 0;
1701 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1702 npols ++;
1705 #endif
1707 if (pol->action == XFRM_POLICY_ALLOW) {
1708 struct sec_path *sp;
1709 static struct sec_path dummy;
1710 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1711 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1712 struct xfrm_tmpl **tpp = tp;
1713 int ti = 0;
1714 int i, k;
1716 if ((sp = skb->sp) == NULL)
1717 sp = &dummy;
1719 for (pi = 0; pi < npols; pi++) {
1720 if (pols[pi] != pol &&
1721 pols[pi]->action != XFRM_POLICY_ALLOW)
1722 goto reject;
1723 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1724 goto reject_error;
1725 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1726 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1728 xfrm_nr = ti;
1729 if (npols > 1) {
1730 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1731 tpp = stp;
1734 /* For each tunnel xfrm, find the first matching tmpl.
1735 * For each tmpl before that, find corresponding xfrm.
1736 * Order is _important_. Later we will implement
1737 * some barriers, but at the moment barriers
1738 * are implied between each two transformations.
1740 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1741 k = xfrm_policy_ok(tpp[i], sp, k, family);
1742 if (k < 0) {
1743 if (k < -1)
1744 /* "-2 - errored_index" returned */
1745 xerr_idx = -(2+k);
1746 goto reject;
1750 if (secpath_has_nontransport(sp, k, &xerr_idx))
1751 goto reject;
1753 xfrm_pols_put(pols, npols);
1754 return 1;
1757 reject:
1758 xfrm_secpath_reject(xerr_idx, skb, &fl);
1759 reject_error:
1760 xfrm_pols_put(pols, npols);
1761 return 0;
1763 EXPORT_SYMBOL(__xfrm_policy_check);
1765 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1767 struct flowi fl;
1769 if (xfrm_decode_session(skb, &fl, family) < 0)
1770 return 0;
1772 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1774 EXPORT_SYMBOL(__xfrm_route_forward);
1776 /* Optimize later using cookies and generation ids. */
1778 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1780 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1781 * to "-1" to force all XFRM destinations to get validated by
1782 * dst_ops->check on every use. We do this because when a
1783 * normal route referenced by an XFRM dst is obsoleted we do
1784 * not go looking around for all parent referencing XFRM dsts
1785 * so that we can invalidate them. It is just too much work.
1786 * Instead we make the checks here on every use. For example:
1788 * XFRM dst A --> IPv4 dst X
1790 * X is the "xdst->route" of A (X is also the "dst->path" of A
1791 * in this example). If X is marked obsolete, "A" will not
1792 * notice. That's what we are validating here via the
1793 * stale_bundle() check.
1795 * When a policy's bundle is pruned, we dst_free() the XFRM
1796 * dst which causes it's ->obsolete field to be set to a
1797 * positive non-zero integer. If an XFRM dst has been pruned
1798 * like this, we want to force a new route lookup.
1800 if (dst->obsolete < 0 && !stale_bundle(dst))
1801 return dst;
1803 return NULL;
1806 static int stale_bundle(struct dst_entry *dst)
1808 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1811 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1813 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1814 dst->dev = &loopback_dev;
1815 dev_hold(&loopback_dev);
1816 dev_put(dev);
1819 EXPORT_SYMBOL(xfrm_dst_ifdown);
1821 static void xfrm_link_failure(struct sk_buff *skb)
1823 /* Impossible. Such dst must be popped before reaches point of failure. */
1824 return;
1827 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1829 if (dst) {
1830 if (dst->obsolete) {
1831 dst_release(dst);
1832 dst = NULL;
1835 return dst;
1838 static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1840 struct dst_entry *dst, **dstp;
1842 write_lock(&pol->lock);
1843 dstp = &pol->bundles;
1844 while ((dst=*dstp) != NULL) {
1845 if (func(dst)) {
1846 *dstp = dst->next;
1847 dst->next = *gc_list_p;
1848 *gc_list_p = dst;
1849 } else {
1850 dstp = &dst->next;
1853 write_unlock(&pol->lock);
1856 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1858 struct dst_entry *gc_list = NULL;
1859 int dir;
1861 read_lock_bh(&xfrm_policy_lock);
1862 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1863 struct xfrm_policy *pol;
1864 struct hlist_node *entry;
1865 struct hlist_head *table;
1866 int i;
1868 hlist_for_each_entry(pol, entry,
1869 &xfrm_policy_inexact[dir], bydst)
1870 prune_one_bundle(pol, func, &gc_list);
1872 table = xfrm_policy_bydst[dir].table;
1873 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1874 hlist_for_each_entry(pol, entry, table + i, bydst)
1875 prune_one_bundle(pol, func, &gc_list);
1878 read_unlock_bh(&xfrm_policy_lock);
1880 while (gc_list) {
1881 struct dst_entry *dst = gc_list;
1882 gc_list = dst->next;
1883 dst_free(dst);
1887 static int unused_bundle(struct dst_entry *dst)
1889 return !atomic_read(&dst->__refcnt);
1892 static void __xfrm_garbage_collect(void)
1894 xfrm_prune_bundles(unused_bundle);
1897 static int xfrm_flush_bundles(void)
1899 xfrm_prune_bundles(stale_bundle);
1900 return 0;
1903 void xfrm_init_pmtu(struct dst_entry *dst)
1905 do {
1906 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1907 u32 pmtu, route_mtu_cached;
1909 pmtu = dst_mtu(dst->child);
1910 xdst->child_mtu_cached = pmtu;
1912 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1914 route_mtu_cached = dst_mtu(xdst->route);
1915 xdst->route_mtu_cached = route_mtu_cached;
1917 if (pmtu > route_mtu_cached)
1918 pmtu = route_mtu_cached;
1920 dst->metrics[RTAX_MTU-1] = pmtu;
1921 } while ((dst = dst->next));
1924 EXPORT_SYMBOL(xfrm_init_pmtu);
1926 /* Check that the bundle accepts the flow and its components are
1927 * still valid.
1930 int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
1931 struct flowi *fl, int family, int strict)
1933 struct dst_entry *dst = &first->u.dst;
1934 struct xfrm_dst *last;
1935 u32 mtu;
1937 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1938 (dst->dev && !netif_running(dst->dev)))
1939 return 0;
1941 last = NULL;
1943 do {
1944 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1946 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1947 return 0;
1948 if (fl && pol &&
1949 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
1950 return 0;
1951 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1952 return 0;
1953 if (xdst->genid != dst->xfrm->genid)
1954 return 0;
1956 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1957 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1958 return 0;
1960 mtu = dst_mtu(dst->child);
1961 if (xdst->child_mtu_cached != mtu) {
1962 last = xdst;
1963 xdst->child_mtu_cached = mtu;
1966 if (!dst_check(xdst->route, xdst->route_cookie))
1967 return 0;
1968 mtu = dst_mtu(xdst->route);
1969 if (xdst->route_mtu_cached != mtu) {
1970 last = xdst;
1971 xdst->route_mtu_cached = mtu;
1974 dst = dst->child;
1975 } while (dst->xfrm);
1977 if (likely(!last))
1978 return 1;
1980 mtu = last->child_mtu_cached;
1981 for (;;) {
1982 dst = &last->u.dst;
1984 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1985 if (mtu > last->route_mtu_cached)
1986 mtu = last->route_mtu_cached;
1987 dst->metrics[RTAX_MTU-1] = mtu;
1989 if (last == first)
1990 break;
1992 last = last->u.next;
1993 last->child_mtu_cached = mtu;
1996 return 1;
1999 EXPORT_SYMBOL(xfrm_bundle_ok);
2001 #ifdef CONFIG_AUDITSYSCALL
2002 /* Audit addition and deletion of SAs and ipsec policy */
2004 void xfrm_audit_log(uid_t auid, u32 sid, int type, int result,
2005 struct xfrm_policy *xp, struct xfrm_state *x)
2008 char *secctx;
2009 u32 secctx_len;
2010 struct xfrm_sec_ctx *sctx = NULL;
2011 struct audit_buffer *audit_buf;
2012 int family;
2013 extern int audit_enabled;
2015 if (audit_enabled == 0)
2016 return;
2018 BUG_ON((type == AUDIT_MAC_IPSEC_ADDSA ||
2019 type == AUDIT_MAC_IPSEC_DELSA) && !x);
2020 BUG_ON((type == AUDIT_MAC_IPSEC_ADDSPD ||
2021 type == AUDIT_MAC_IPSEC_DELSPD) && !xp);
2023 audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, type);
2024 if (audit_buf == NULL)
2025 return;
2027 switch(type) {
2028 case AUDIT_MAC_IPSEC_ADDSA:
2029 audit_log_format(audit_buf, "SAD add: auid=%u", auid);
2030 break;
2031 case AUDIT_MAC_IPSEC_DELSA:
2032 audit_log_format(audit_buf, "SAD delete: auid=%u", auid);
2033 break;
2034 case AUDIT_MAC_IPSEC_ADDSPD:
2035 audit_log_format(audit_buf, "SPD add: auid=%u", auid);
2036 break;
2037 case AUDIT_MAC_IPSEC_DELSPD:
2038 audit_log_format(audit_buf, "SPD delete: auid=%u", auid);
2039 break;
2040 default:
2041 return;
2044 if (sid != 0 &&
2045 security_secid_to_secctx(sid, &secctx, &secctx_len) == 0)
2046 audit_log_format(audit_buf, " subj=%s", secctx);
2047 else
2048 audit_log_task_context(audit_buf);
2050 if (xp) {
2051 family = xp->selector.family;
2052 if (xp->security)
2053 sctx = xp->security;
2054 } else {
2055 family = x->props.family;
2056 if (x->security)
2057 sctx = x->security;
2060 if (sctx)
2061 audit_log_format(audit_buf,
2062 " sec_alg=%u sec_doi=%u sec_obj=%s",
2063 sctx->ctx_alg, sctx->ctx_doi, sctx->ctx_str);
2065 switch(family) {
2066 case AF_INET:
2068 struct in_addr saddr, daddr;
2069 if (xp) {
2070 saddr.s_addr = xp->selector.saddr.a4;
2071 daddr.s_addr = xp->selector.daddr.a4;
2072 } else {
2073 saddr.s_addr = x->props.saddr.a4;
2074 daddr.s_addr = x->id.daddr.a4;
2076 audit_log_format(audit_buf,
2077 " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2078 NIPQUAD(saddr), NIPQUAD(daddr));
2080 break;
2081 case AF_INET6:
2083 struct in6_addr saddr6, daddr6;
2084 if (xp) {
2085 memcpy(&saddr6, xp->selector.saddr.a6,
2086 sizeof(struct in6_addr));
2087 memcpy(&daddr6, xp->selector.daddr.a6,
2088 sizeof(struct in6_addr));
2089 } else {
2090 memcpy(&saddr6, x->props.saddr.a6,
2091 sizeof(struct in6_addr));
2092 memcpy(&daddr6, x->id.daddr.a6,
2093 sizeof(struct in6_addr));
2095 audit_log_format(audit_buf,
2096 " src=" NIP6_FMT " dst=" NIP6_FMT,
2097 NIP6(saddr6), NIP6(daddr6));
2099 break;
2102 if (x)
2103 audit_log_format(audit_buf, " spi=%lu(0x%lx) protocol=%s",
2104 (unsigned long)ntohl(x->id.spi),
2105 (unsigned long)ntohl(x->id.spi),
2106 x->id.proto == IPPROTO_AH ? "AH" :
2107 (x->id.proto == IPPROTO_ESP ?
2108 "ESP" : "IPCOMP"));
2110 audit_log_format(audit_buf, " res=%u", result);
2111 audit_log_end(audit_buf);
2114 EXPORT_SYMBOL(xfrm_audit_log);
2115 #endif /* CONFIG_AUDITSYSCALL */
2117 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2119 int err = 0;
2120 if (unlikely(afinfo == NULL))
2121 return -EINVAL;
2122 if (unlikely(afinfo->family >= NPROTO))
2123 return -EAFNOSUPPORT;
2124 write_lock_bh(&xfrm_policy_afinfo_lock);
2125 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2126 err = -ENOBUFS;
2127 else {
2128 struct dst_ops *dst_ops = afinfo->dst_ops;
2129 if (likely(dst_ops->kmem_cachep == NULL))
2130 dst_ops->kmem_cachep = xfrm_dst_cache;
2131 if (likely(dst_ops->check == NULL))
2132 dst_ops->check = xfrm_dst_check;
2133 if (likely(dst_ops->negative_advice == NULL))
2134 dst_ops->negative_advice = xfrm_negative_advice;
2135 if (likely(dst_ops->link_failure == NULL))
2136 dst_ops->link_failure = xfrm_link_failure;
2137 if (likely(afinfo->garbage_collect == NULL))
2138 afinfo->garbage_collect = __xfrm_garbage_collect;
2139 xfrm_policy_afinfo[afinfo->family] = afinfo;
2141 write_unlock_bh(&xfrm_policy_afinfo_lock);
2142 return err;
2144 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2146 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2148 int err = 0;
2149 if (unlikely(afinfo == NULL))
2150 return -EINVAL;
2151 if (unlikely(afinfo->family >= NPROTO))
2152 return -EAFNOSUPPORT;
2153 write_lock_bh(&xfrm_policy_afinfo_lock);
2154 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2155 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2156 err = -EINVAL;
2157 else {
2158 struct dst_ops *dst_ops = afinfo->dst_ops;
2159 xfrm_policy_afinfo[afinfo->family] = NULL;
2160 dst_ops->kmem_cachep = NULL;
2161 dst_ops->check = NULL;
2162 dst_ops->negative_advice = NULL;
2163 dst_ops->link_failure = NULL;
2164 afinfo->garbage_collect = NULL;
2167 write_unlock_bh(&xfrm_policy_afinfo_lock);
2168 return err;
2170 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2172 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2174 struct xfrm_policy_afinfo *afinfo;
2175 if (unlikely(family >= NPROTO))
2176 return NULL;
2177 read_lock(&xfrm_policy_afinfo_lock);
2178 afinfo = xfrm_policy_afinfo[family];
2179 if (unlikely(!afinfo))
2180 read_unlock(&xfrm_policy_afinfo_lock);
2181 return afinfo;
2184 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2186 read_unlock(&xfrm_policy_afinfo_lock);
2189 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2191 struct xfrm_policy_afinfo *afinfo;
2192 if (unlikely(family >= NPROTO))
2193 return NULL;
2194 write_lock_bh(&xfrm_policy_afinfo_lock);
2195 afinfo = xfrm_policy_afinfo[family];
2196 if (unlikely(!afinfo))
2197 write_unlock_bh(&xfrm_policy_afinfo_lock);
2198 return afinfo;
2201 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2203 write_unlock_bh(&xfrm_policy_afinfo_lock);
2206 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2208 switch (event) {
2209 case NETDEV_DOWN:
2210 xfrm_flush_bundles();
2212 return NOTIFY_DONE;
2215 static struct notifier_block xfrm_dev_notifier = {
2216 xfrm_dev_event,
2217 NULL,
2221 static void __init xfrm_policy_init(void)
2223 unsigned int hmask, sz;
2224 int dir;
2226 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2227 sizeof(struct xfrm_dst),
2228 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2229 NULL, NULL);
2231 hmask = 8 - 1;
2232 sz = (hmask+1) * sizeof(struct hlist_head);
2234 xfrm_policy_byidx = xfrm_hash_alloc(sz);
2235 xfrm_idx_hmask = hmask;
2236 if (!xfrm_policy_byidx)
2237 panic("XFRM: failed to allocate byidx hash\n");
2239 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2240 struct xfrm_policy_hash *htab;
2242 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2244 htab = &xfrm_policy_bydst[dir];
2245 htab->table = xfrm_hash_alloc(sz);
2246 htab->hmask = hmask;
2247 if (!htab->table)
2248 panic("XFRM: failed to allocate bydst hash\n");
2251 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
2252 register_netdevice_notifier(&xfrm_dev_notifier);
2255 void __init xfrm_init(void)
2257 xfrm_state_init();
2258 xfrm_policy_init();
2259 xfrm_input_init();
2262 #ifdef CONFIG_XFRM_MIGRATE
2263 static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2264 struct xfrm_selector *sel_tgt)
2266 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2267 if (sel_tgt->family == sel_cmp->family &&
2268 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
2269 sel_cmp->family) == 0 &&
2270 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2271 sel_cmp->family) == 0 &&
2272 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2273 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2274 return 1;
2276 } else {
2277 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2278 return 1;
2281 return 0;
2284 static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2285 u8 dir, u8 type)
2287 struct xfrm_policy *pol, *ret = NULL;
2288 struct hlist_node *entry;
2289 struct hlist_head *chain;
2290 u32 priority = ~0U;
2292 read_lock_bh(&xfrm_policy_lock);
2293 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2294 hlist_for_each_entry(pol, entry, chain, bydst) {
2295 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2296 pol->type == type) {
2297 ret = pol;
2298 priority = ret->priority;
2299 break;
2302 chain = &xfrm_policy_inexact[dir];
2303 hlist_for_each_entry(pol, entry, chain, bydst) {
2304 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2305 pol->type == type &&
2306 pol->priority < priority) {
2307 ret = pol;
2308 break;
2312 if (ret)
2313 xfrm_pol_hold(ret);
2315 read_unlock_bh(&xfrm_policy_lock);
2317 return ret;
2320 static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2322 int match = 0;
2324 if (t->mode == m->mode && t->id.proto == m->proto &&
2325 (m->reqid == 0 || t->reqid == m->reqid)) {
2326 switch (t->mode) {
2327 case XFRM_MODE_TUNNEL:
2328 case XFRM_MODE_BEET:
2329 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2330 m->old_family) == 0 &&
2331 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2332 m->old_family) == 0) {
2333 match = 1;
2335 break;
2336 case XFRM_MODE_TRANSPORT:
2337 /* in case of transport mode, template does not store
2338 any IP addresses, hence we just compare mode and
2339 protocol */
2340 match = 1;
2341 break;
2342 default:
2343 break;
2346 return match;
2349 /* update endpoint address(es) of template(s) */
2350 static int xfrm_policy_migrate(struct xfrm_policy *pol,
2351 struct xfrm_migrate *m, int num_migrate)
2353 struct xfrm_migrate *mp;
2354 struct dst_entry *dst;
2355 int i, j, n = 0;
2357 write_lock_bh(&pol->lock);
2358 if (unlikely(pol->dead)) {
2359 /* target policy has been deleted */
2360 write_unlock_bh(&pol->lock);
2361 return -ENOENT;
2364 for (i = 0; i < pol->xfrm_nr; i++) {
2365 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2366 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2367 continue;
2368 n++;
2369 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
2370 continue;
2371 /* update endpoints */
2372 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2373 sizeof(pol->xfrm_vec[i].id.daddr));
2374 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2375 sizeof(pol->xfrm_vec[i].saddr));
2376 pol->xfrm_vec[i].encap_family = mp->new_family;
2377 /* flush bundles */
2378 while ((dst = pol->bundles) != NULL) {
2379 pol->bundles = dst->next;
2380 dst_free(dst);
2385 write_unlock_bh(&pol->lock);
2387 if (!n)
2388 return -ENODATA;
2390 return 0;
2393 static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2395 int i, j;
2397 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2398 return -EINVAL;
2400 for (i = 0; i < num_migrate; i++) {
2401 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2402 m[i].old_family) == 0) &&
2403 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2404 m[i].old_family) == 0))
2405 return -EINVAL;
2406 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2407 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2408 return -EINVAL;
2410 /* check if there is any duplicated entry */
2411 for (j = i + 1; j < num_migrate; j++) {
2412 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2413 sizeof(m[i].old_daddr)) &&
2414 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2415 sizeof(m[i].old_saddr)) &&
2416 m[i].proto == m[j].proto &&
2417 m[i].mode == m[j].mode &&
2418 m[i].reqid == m[j].reqid &&
2419 m[i].old_family == m[j].old_family)
2420 return -EINVAL;
2424 return 0;
2427 int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2428 struct xfrm_migrate *m, int num_migrate)
2430 int i, err, nx_cur = 0, nx_new = 0;
2431 struct xfrm_policy *pol = NULL;
2432 struct xfrm_state *x, *xc;
2433 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2434 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2435 struct xfrm_migrate *mp;
2437 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2438 goto out;
2440 /* Stage 1 - find policy */
2441 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2442 err = -ENOENT;
2443 goto out;
2446 /* Stage 2 - find and update state(s) */
2447 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2448 if ((x = xfrm_migrate_state_find(mp))) {
2449 x_cur[nx_cur] = x;
2450 nx_cur++;
2451 if ((xc = xfrm_state_migrate(x, mp))) {
2452 x_new[nx_new] = xc;
2453 nx_new++;
2454 } else {
2455 err = -ENODATA;
2456 goto restore_state;
2461 /* Stage 3 - update policy */
2462 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2463 goto restore_state;
2465 /* Stage 4 - delete old state(s) */
2466 if (nx_cur) {
2467 xfrm_states_put(x_cur, nx_cur);
2468 xfrm_states_delete(x_cur, nx_cur);
2471 /* Stage 5 - announce */
2472 km_migrate(sel, dir, type, m, num_migrate);
2474 xfrm_pol_put(pol);
2476 return 0;
2477 out:
2478 return err;
2480 restore_state:
2481 if (pol)
2482 xfrm_pol_put(pol);
2483 if (nx_cur)
2484 xfrm_states_put(x_cur, nx_cur);
2485 if (nx_new)
2486 xfrm_states_delete(x_new, nx_new);
2488 return err;
2490 EXPORT_SYMBOL(xfrm_migrate);
2491 #endif