6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/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/bootmem.h>
26 #include <linux/vmalloc.h>
27 #include <linux/cache.h>
31 DEFINE_MUTEX(xfrm_cfg_mutex);
32 EXPORT_SYMBOL(xfrm_cfg_mutex);
34 static DEFINE_RWLOCK(xfrm_policy_lock);
36 unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
37 EXPORT_SYMBOL(xfrm_policy_count);
39 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
40 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
42 static kmem_cache_t *xfrm_dst_cache __read_mostly;
44 static struct work_struct xfrm_policy_gc_work;
45 static HLIST_HEAD(xfrm_policy_gc_list);
46 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
48 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
49 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
50 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
51 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
53 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
55 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
56 struct xfrm_type **typemap;
59 if (unlikely(afinfo == NULL))
61 typemap = afinfo->type_map;
63 if (likely(typemap[type->proto] == NULL))
64 typemap[type->proto] = type;
67 xfrm_policy_unlock_afinfo(afinfo);
70 EXPORT_SYMBOL(xfrm_register_type);
72 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
74 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
75 struct xfrm_type **typemap;
78 if (unlikely(afinfo == NULL))
80 typemap = afinfo->type_map;
82 if (unlikely(typemap[type->proto] != type))
85 typemap[type->proto] = NULL;
86 xfrm_policy_unlock_afinfo(afinfo);
89 EXPORT_SYMBOL(xfrm_unregister_type);
91 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
93 struct xfrm_policy_afinfo *afinfo;
94 struct xfrm_type **typemap;
95 struct xfrm_type *type;
96 int modload_attempted = 0;
99 afinfo = xfrm_policy_get_afinfo(family);
100 if (unlikely(afinfo == NULL))
102 typemap = afinfo->type_map;
104 type = typemap[proto];
105 if (unlikely(type && !try_module_get(type->owner)))
107 if (!type && !modload_attempted) {
108 xfrm_policy_put_afinfo(afinfo);
109 request_module("xfrm-type-%d-%d",
110 (int) family, (int) proto);
111 modload_attempted = 1;
115 xfrm_policy_put_afinfo(afinfo);
119 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
120 unsigned short family)
122 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
125 if (unlikely(afinfo == NULL))
126 return -EAFNOSUPPORT;
128 if (likely(afinfo->dst_lookup != NULL))
129 err = afinfo->dst_lookup(dst, fl);
132 xfrm_policy_put_afinfo(afinfo);
135 EXPORT_SYMBOL(xfrm_dst_lookup);
137 void xfrm_put_type(struct xfrm_type *type)
139 module_put(type->owner);
142 int xfrm_register_mode(struct xfrm_mode *mode, int family)
144 struct xfrm_policy_afinfo *afinfo;
145 struct xfrm_mode **modemap;
148 if (unlikely(mode->encap >= XFRM_MODE_MAX))
151 afinfo = xfrm_policy_lock_afinfo(family);
152 if (unlikely(afinfo == NULL))
153 return -EAFNOSUPPORT;
156 modemap = afinfo->mode_map;
157 if (likely(modemap[mode->encap] == NULL)) {
158 modemap[mode->encap] = mode;
162 xfrm_policy_unlock_afinfo(afinfo);
165 EXPORT_SYMBOL(xfrm_register_mode);
167 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
169 struct xfrm_policy_afinfo *afinfo;
170 struct xfrm_mode **modemap;
173 if (unlikely(mode->encap >= XFRM_MODE_MAX))
176 afinfo = xfrm_policy_lock_afinfo(family);
177 if (unlikely(afinfo == NULL))
178 return -EAFNOSUPPORT;
181 modemap = afinfo->mode_map;
182 if (likely(modemap[mode->encap] == mode)) {
183 modemap[mode->encap] = NULL;
187 xfrm_policy_unlock_afinfo(afinfo);
190 EXPORT_SYMBOL(xfrm_unregister_mode);
192 struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
194 struct xfrm_policy_afinfo *afinfo;
195 struct xfrm_mode *mode;
196 int modload_attempted = 0;
198 if (unlikely(encap >= XFRM_MODE_MAX))
202 afinfo = xfrm_policy_get_afinfo(family);
203 if (unlikely(afinfo == NULL))
206 mode = afinfo->mode_map[encap];
207 if (unlikely(mode && !try_module_get(mode->owner)))
209 if (!mode && !modload_attempted) {
210 xfrm_policy_put_afinfo(afinfo);
211 request_module("xfrm-mode-%d-%d", family, encap);
212 modload_attempted = 1;
216 xfrm_policy_put_afinfo(afinfo);
220 void xfrm_put_mode(struct xfrm_mode *mode)
222 module_put(mode->owner);
225 static inline unsigned long make_jiffies(long secs)
227 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
228 return MAX_SCHEDULE_TIMEOUT-1;
233 static void xfrm_policy_timer(unsigned long data)
235 struct xfrm_policy *xp = (struct xfrm_policy*)data;
236 unsigned long now = (unsigned long)xtime.tv_sec;
237 long next = LONG_MAX;
241 read_lock(&xp->lock);
246 dir = xfrm_policy_id2dir(xp->index);
248 if (xp->lft.hard_add_expires_seconds) {
249 long tmo = xp->lft.hard_add_expires_seconds +
250 xp->curlft.add_time - now;
256 if (xp->lft.hard_use_expires_seconds) {
257 long tmo = xp->lft.hard_use_expires_seconds +
258 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
264 if (xp->lft.soft_add_expires_seconds) {
265 long tmo = xp->lft.soft_add_expires_seconds +
266 xp->curlft.add_time - now;
269 tmo = XFRM_KM_TIMEOUT;
274 if (xp->lft.soft_use_expires_seconds) {
275 long tmo = xp->lft.soft_use_expires_seconds +
276 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
279 tmo = XFRM_KM_TIMEOUT;
286 km_policy_expired(xp, dir, 0, 0);
287 if (next != LONG_MAX &&
288 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
292 read_unlock(&xp->lock);
297 read_unlock(&xp->lock);
298 if (!xfrm_policy_delete(xp, dir))
299 km_policy_expired(xp, dir, 1, 0);
304 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
308 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
310 struct xfrm_policy *policy;
312 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
315 INIT_HLIST_NODE(&policy->bydst);
316 INIT_HLIST_NODE(&policy->byidx);
317 rwlock_init(&policy->lock);
318 atomic_set(&policy->refcnt, 1);
319 init_timer(&policy->timer);
320 policy->timer.data = (unsigned long)policy;
321 policy->timer.function = xfrm_policy_timer;
325 EXPORT_SYMBOL(xfrm_policy_alloc);
327 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
329 void __xfrm_policy_destroy(struct xfrm_policy *policy)
331 BUG_ON(!policy->dead);
333 BUG_ON(policy->bundles);
335 if (del_timer(&policy->timer))
338 security_xfrm_policy_free(policy);
341 EXPORT_SYMBOL(__xfrm_policy_destroy);
343 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
345 struct dst_entry *dst;
347 while ((dst = policy->bundles) != NULL) {
348 policy->bundles = dst->next;
352 if (del_timer(&policy->timer))
353 atomic_dec(&policy->refcnt);
355 if (atomic_read(&policy->refcnt) > 1)
358 xfrm_pol_put(policy);
361 static void xfrm_policy_gc_task(void *data)
363 struct xfrm_policy *policy;
364 struct hlist_node *entry, *tmp;
365 struct hlist_head gc_list;
367 spin_lock_bh(&xfrm_policy_gc_lock);
368 gc_list.first = xfrm_policy_gc_list.first;
369 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
370 spin_unlock_bh(&xfrm_policy_gc_lock);
372 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
373 xfrm_policy_gc_kill(policy);
376 /* Rule must be locked. Release descentant resources, announce
377 * entry dead. The rule must be unlinked from lists to the moment.
380 static void xfrm_policy_kill(struct xfrm_policy *policy)
384 write_lock_bh(&policy->lock);
387 write_unlock_bh(&policy->lock);
389 if (unlikely(dead)) {
394 spin_lock(&xfrm_policy_gc_lock);
395 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
396 spin_unlock(&xfrm_policy_gc_lock);
398 schedule_work(&xfrm_policy_gc_work);
401 struct xfrm_policy_hash {
402 struct hlist_head *table;
406 static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
407 static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
408 static struct hlist_head *xfrm_policy_byidx __read_mostly;
409 static unsigned int xfrm_idx_hmask __read_mostly;
410 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
412 static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
414 return (index ^ (index >> 8)) & hmask;
417 static inline unsigned int idx_hash(u32 index)
419 return __idx_hash(index, xfrm_idx_hmask);
422 static inline unsigned int __sel_hash(struct xfrm_selector *sel, unsigned short family, unsigned int hmask)
424 xfrm_address_t *daddr = &sel->daddr;
425 xfrm_address_t *saddr = &sel->saddr;
430 if (sel->prefixlen_d != 32 ||
431 sel->prefixlen_s != 32)
434 h = ntohl(daddr->a4 ^ saddr->a4);
438 if (sel->prefixlen_d != 128 ||
439 sel->prefixlen_s != 128)
442 h = ntohl(daddr->a6[2] ^ daddr->a6[3] ^
443 saddr->a6[2] ^ saddr->a6[3]);
450 static inline unsigned int __addr_hash(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, unsigned int hmask)
456 h = ntohl(daddr->a4 ^ saddr->a4);
460 h = ntohl(daddr->a6[2] ^ daddr->a6[3] ^
461 saddr->a6[2] ^ saddr->a6[3]);
468 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
470 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
471 unsigned int hash = __sel_hash(sel, family, hmask);
473 return (hash == hmask + 1 ?
474 &xfrm_policy_inexact[dir] :
475 xfrm_policy_bydst[dir].table + hash);
478 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
480 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
481 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
483 return xfrm_policy_bydst[dir].table + hash;
486 static struct hlist_head *xfrm_policy_hash_alloc(unsigned int sz)
488 struct hlist_head *n;
491 n = kmalloc(sz, GFP_KERNEL);
493 n = __vmalloc(sz, GFP_KERNEL, PAGE_KERNEL);
495 n = (struct hlist_head *)
496 __get_free_pages(GFP_KERNEL, get_order(sz));
504 static void xfrm_policy_hash_free(struct hlist_head *n, unsigned int sz)
511 free_pages((unsigned long)n, get_order(sz));
514 static void xfrm_dst_hash_transfer(struct hlist_head *list,
515 struct hlist_head *ndsttable,
516 unsigned int nhashmask)
518 struct hlist_node *entry, *tmp;
519 struct xfrm_policy *pol;
521 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
524 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
525 pol->family, nhashmask);
526 hlist_add_head(&pol->bydst, ndsttable+h);
530 static void xfrm_idx_hash_transfer(struct hlist_head *list,
531 struct hlist_head *nidxtable,
532 unsigned int nhashmask)
534 struct hlist_node *entry, *tmp;
535 struct xfrm_policy *pol;
537 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
540 h = __idx_hash(pol->index, nhashmask);
541 hlist_add_head(&pol->byidx, nidxtable+h);
545 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
547 return ((old_hmask + 1) << 1) - 1;
550 static void xfrm_bydst_resize(int dir)
552 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
553 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
554 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
555 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
556 struct hlist_head *ndst = xfrm_policy_hash_alloc(nsize);
562 write_lock_bh(&xfrm_policy_lock);
564 for (i = hmask; i >= 0; i--)
565 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
567 xfrm_policy_bydst[dir].table = ndst;
568 xfrm_policy_bydst[dir].hmask = nhashmask;
570 write_unlock_bh(&xfrm_policy_lock);
572 xfrm_policy_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
575 static void xfrm_byidx_resize(int total)
577 unsigned int hmask = xfrm_idx_hmask;
578 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
579 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
580 struct hlist_head *oidx = xfrm_policy_byidx;
581 struct hlist_head *nidx = xfrm_policy_hash_alloc(nsize);
587 write_lock_bh(&xfrm_policy_lock);
589 for (i = hmask; i >= 0; i--)
590 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
592 xfrm_policy_byidx = nidx;
593 xfrm_idx_hmask = nhashmask;
595 write_unlock_bh(&xfrm_policy_lock);
597 xfrm_policy_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
600 static inline int xfrm_bydst_should_resize(int dir, int *total)
602 unsigned int cnt = xfrm_policy_count[dir];
603 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
608 if ((hmask + 1) < xfrm_policy_hashmax &&
615 static inline int xfrm_byidx_should_resize(int total)
617 unsigned int hmask = xfrm_idx_hmask;
619 if ((hmask + 1) < xfrm_policy_hashmax &&
626 static DEFINE_MUTEX(hash_resize_mutex);
628 static void xfrm_hash_resize(void *__unused)
632 mutex_lock(&hash_resize_mutex);
635 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
636 if (xfrm_bydst_should_resize(dir, &total))
637 xfrm_bydst_resize(dir);
639 if (xfrm_byidx_should_resize(total))
640 xfrm_byidx_resize(total);
642 mutex_unlock(&hash_resize_mutex);
645 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL);
647 /* Generate new index... KAME seems to generate them ordered by cost
648 * of an absolute inpredictability of ordering of rules. This will not pass. */
649 static u32 xfrm_gen_index(u8 type, int dir)
651 static u32 idx_generator;
654 struct hlist_node *entry;
655 struct hlist_head *list;
656 struct xfrm_policy *p;
660 idx = (idx_generator | dir);
664 list = xfrm_policy_byidx + idx_hash(idx);
666 hlist_for_each_entry(p, entry, list, byidx) {
667 if (p->index == idx) {
677 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
679 u32 *p1 = (u32 *) s1;
680 u32 *p2 = (u32 *) s2;
681 int len = sizeof(struct xfrm_selector) / sizeof(u32);
684 for (i = 0; i < len; i++) {
692 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
694 struct xfrm_policy *pol;
695 struct xfrm_policy *delpol;
696 struct hlist_head *chain;
697 struct hlist_node *entry, *newpos, *last;
698 struct dst_entry *gc_list;
700 write_lock_bh(&xfrm_policy_lock);
701 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
705 hlist_for_each_entry(pol, entry, chain, bydst) {
707 pol->type == policy->type &&
708 !selector_cmp(&pol->selector, &policy->selector) &&
709 xfrm_sec_ctx_match(pol->security, policy->security)) {
711 write_unlock_bh(&xfrm_policy_lock);
715 if (policy->priority > pol->priority)
717 } else if (policy->priority >= pol->priority) {
722 newpos = &pol->bydst;
730 hlist_add_after(newpos, &policy->bydst);
732 hlist_add_head(&policy->bydst, chain);
733 xfrm_pol_hold(policy);
734 xfrm_policy_count[dir]++;
735 atomic_inc(&flow_cache_genid);
737 hlist_del(&delpol->bydst);
738 hlist_del(&delpol->byidx);
739 xfrm_policy_count[dir]--;
741 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
742 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
743 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
744 policy->curlft.use_time = 0;
745 if (!mod_timer(&policy->timer, jiffies + HZ))
746 xfrm_pol_hold(policy);
747 write_unlock_bh(&xfrm_policy_lock);
750 xfrm_policy_kill(delpol);
751 else if (xfrm_bydst_should_resize(dir, NULL))
752 schedule_work(&xfrm_hash_work);
754 read_lock_bh(&xfrm_policy_lock);
756 entry = &policy->bydst;
757 hlist_for_each_entry_continue(policy, entry, bydst) {
758 struct dst_entry *dst;
760 write_lock(&policy->lock);
761 dst = policy->bundles;
763 struct dst_entry *tail = dst;
766 tail->next = gc_list;
769 policy->bundles = NULL;
771 write_unlock(&policy->lock);
773 read_unlock_bh(&xfrm_policy_lock);
776 struct dst_entry *dst = gc_list;
784 EXPORT_SYMBOL(xfrm_policy_insert);
786 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
787 struct xfrm_selector *sel,
788 struct xfrm_sec_ctx *ctx, int delete)
790 struct xfrm_policy *pol, *ret;
791 struct hlist_head *chain;
792 struct hlist_node *entry;
794 write_lock_bh(&xfrm_policy_lock);
795 chain = policy_hash_bysel(sel, sel->family, dir);
797 hlist_for_each_entry(pol, entry, chain, bydst) {
798 if (pol->type == type &&
799 !selector_cmp(sel, &pol->selector) &&
800 xfrm_sec_ctx_match(ctx, pol->security)) {
803 hlist_del(&pol->bydst);
804 hlist_del(&pol->byidx);
805 xfrm_policy_count[dir]--;
811 write_unlock_bh(&xfrm_policy_lock);
814 atomic_inc(&flow_cache_genid);
815 xfrm_policy_kill(ret);
819 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
821 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
823 struct xfrm_policy *pol, *ret;
824 struct hlist_head *chain;
825 struct hlist_node *entry;
827 write_lock_bh(&xfrm_policy_lock);
828 chain = xfrm_policy_byidx + idx_hash(id);
830 hlist_for_each_entry(pol, entry, chain, byidx) {
831 if (pol->type == type && pol->index == id) {
834 hlist_del(&pol->bydst);
835 hlist_del(&pol->byidx);
836 xfrm_policy_count[dir]--;
842 write_unlock_bh(&xfrm_policy_lock);
845 atomic_inc(&flow_cache_genid);
846 xfrm_policy_kill(ret);
850 EXPORT_SYMBOL(xfrm_policy_byid);
852 void xfrm_policy_flush(u8 type)
856 write_lock_bh(&xfrm_policy_lock);
857 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
858 struct xfrm_policy *pol;
859 struct hlist_node *entry;
863 hlist_for_each_entry(pol, entry,
864 &xfrm_policy_inexact[dir], bydst) {
865 if (pol->type != type)
867 hlist_del(&pol->bydst);
868 hlist_del(&pol->byidx);
869 write_unlock_bh(&xfrm_policy_lock);
871 xfrm_policy_kill(pol);
873 write_lock_bh(&xfrm_policy_lock);
877 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
879 hlist_for_each_entry(pol, entry,
880 xfrm_policy_bydst[dir].table + i,
882 if (pol->type != type)
884 hlist_del(&pol->bydst);
885 hlist_del(&pol->byidx);
886 write_unlock_bh(&xfrm_policy_lock);
888 xfrm_policy_kill(pol);
890 write_lock_bh(&xfrm_policy_lock);
895 xfrm_policy_count[dir] = 0;
897 atomic_inc(&flow_cache_genid);
898 write_unlock_bh(&xfrm_policy_lock);
900 EXPORT_SYMBOL(xfrm_policy_flush);
902 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
905 struct xfrm_policy *pol;
906 struct hlist_node *entry;
907 int dir, count, error;
909 read_lock_bh(&xfrm_policy_lock);
911 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
912 struct hlist_head *table = xfrm_policy_bydst[dir].table;
915 hlist_for_each_entry(pol, entry,
916 &xfrm_policy_inexact[dir], bydst) {
917 if (pol->type == type)
920 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
921 hlist_for_each_entry(pol, entry, table + i, bydst) {
922 if (pol->type == type)
933 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
934 struct hlist_head *table = xfrm_policy_bydst[dir].table;
937 hlist_for_each_entry(pol, entry,
938 &xfrm_policy_inexact[dir], bydst) {
939 if (pol->type != type)
941 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
945 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
946 hlist_for_each_entry(pol, entry, table + i, bydst) {
947 if (pol->type != type)
949 error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
957 read_unlock_bh(&xfrm_policy_lock);
960 EXPORT_SYMBOL(xfrm_policy_walk);
962 /* Find policy to apply to this flow. */
964 static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
965 u8 type, u16 family, int dir)
967 struct xfrm_selector *sel = &pol->selector;
970 if (pol->family != family ||
974 match = xfrm_selector_match(sel, fl, family);
976 if (!security_xfrm_policy_lookup(pol, fl->secid, dir))
983 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
986 struct xfrm_policy *pol, *ret;
987 xfrm_address_t *daddr, *saddr;
988 struct hlist_node *entry;
989 struct hlist_head *chain;
991 daddr = xfrm_flowi_daddr(fl, family);
992 saddr = xfrm_flowi_saddr(fl, family);
993 if (unlikely(!daddr || !saddr))
996 read_lock_bh(&xfrm_policy_lock);
997 chain = policy_hash_direct(daddr, saddr, family, dir);
999 hlist_for_each_entry(pol, entry, chain, bydst) {
1000 if (xfrm_policy_match(pol, fl, type, family, dir)) {
1007 chain = &xfrm_policy_inexact[dir];
1008 hlist_for_each_entry(pol, entry, chain, bydst) {
1009 if (xfrm_policy_match(pol, fl, type, family, dir)) {
1016 read_unlock_bh(&xfrm_policy_lock);
1021 static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
1022 void **objp, atomic_t **obj_refp)
1024 struct xfrm_policy *pol;
1026 #ifdef CONFIG_XFRM_SUB_POLICY
1027 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
1031 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1033 #ifdef CONFIG_XFRM_SUB_POLICY
1036 if ((*objp = (void *) pol) != NULL)
1037 *obj_refp = &pol->refcnt;
1040 static inline int policy_to_flow_dir(int dir)
1042 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1043 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1044 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1048 case XFRM_POLICY_IN:
1050 case XFRM_POLICY_OUT:
1051 return FLOW_DIR_OUT;
1052 case XFRM_POLICY_FWD:
1053 return FLOW_DIR_FWD;
1057 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1059 struct xfrm_policy *pol;
1061 read_lock_bh(&xfrm_policy_lock);
1062 if ((pol = sk->sk_policy[dir]) != NULL) {
1063 int match = xfrm_selector_match(&pol->selector, fl,
1068 err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
1075 read_unlock_bh(&xfrm_policy_lock);
1079 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1081 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1084 hlist_add_head(&pol->bydst, chain);
1085 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1086 xfrm_policy_count[dir]++;
1089 if (xfrm_bydst_should_resize(dir, NULL))
1090 schedule_work(&xfrm_hash_work);
1093 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1096 if (hlist_unhashed(&pol->bydst))
1099 hlist_del(&pol->bydst);
1100 hlist_del(&pol->byidx);
1101 xfrm_policy_count[dir]--;
1106 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1108 write_lock_bh(&xfrm_policy_lock);
1109 pol = __xfrm_policy_unlink(pol, dir);
1110 write_unlock_bh(&xfrm_policy_lock);
1112 if (dir < XFRM_POLICY_MAX)
1113 atomic_inc(&flow_cache_genid);
1114 xfrm_policy_kill(pol);
1119 EXPORT_SYMBOL(xfrm_policy_delete);
1121 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1123 struct xfrm_policy *old_pol;
1125 #ifdef CONFIG_XFRM_SUB_POLICY
1126 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1130 write_lock_bh(&xfrm_policy_lock);
1131 old_pol = sk->sk_policy[dir];
1132 sk->sk_policy[dir] = pol;
1134 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
1135 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1136 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1139 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1140 write_unlock_bh(&xfrm_policy_lock);
1143 xfrm_policy_kill(old_pol);
1148 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1150 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1153 newp->selector = old->selector;
1154 if (security_xfrm_policy_clone(old, newp)) {
1156 return NULL; /* ENOMEM */
1158 newp->lft = old->lft;
1159 newp->curlft = old->curlft;
1160 newp->action = old->action;
1161 newp->flags = old->flags;
1162 newp->xfrm_nr = old->xfrm_nr;
1163 newp->index = old->index;
1164 newp->type = old->type;
1165 memcpy(newp->xfrm_vec, old->xfrm_vec,
1166 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1167 write_lock_bh(&xfrm_policy_lock);
1168 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1169 write_unlock_bh(&xfrm_policy_lock);
1175 int __xfrm_sk_clone_policy(struct sock *sk)
1177 struct xfrm_policy *p0 = sk->sk_policy[0],
1178 *p1 = sk->sk_policy[1];
1180 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1181 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1183 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1188 /* Resolve list of templates for the flow, given policy. */
1191 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1192 struct xfrm_state **xfrm,
1193 unsigned short family)
1197 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1198 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1200 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1201 struct xfrm_state *x;
1202 xfrm_address_t *remote = daddr;
1203 xfrm_address_t *local = saddr;
1204 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1206 if (tmpl->mode == XFRM_MODE_TUNNEL) {
1207 remote = &tmpl->id.daddr;
1208 local = &tmpl->saddr;
1211 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1213 if (x && x->km.state == XFRM_STATE_VALID) {
1220 error = (x->km.state == XFRM_STATE_ERROR ?
1225 if (!tmpl->optional)
1231 for (nx--; nx>=0; nx--)
1232 xfrm_state_put(xfrm[nx]);
1237 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1238 struct xfrm_state **xfrm,
1239 unsigned short family)
1241 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1242 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1248 for (i = 0; i < npols; i++) {
1249 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1254 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1262 /* found states are sorted for outbound processing */
1264 xfrm_state_sort(xfrm, tpp, cnx, family);
1269 for (cnx--; cnx>=0; cnx--)
1270 xfrm_state_put(tpp[cnx]);
1275 /* Check that the bundle accepts the flow and its components are
1279 static struct dst_entry *
1280 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1282 struct dst_entry *x;
1283 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1284 if (unlikely(afinfo == NULL))
1285 return ERR_PTR(-EINVAL);
1286 x = afinfo->find_bundle(fl, policy);
1287 xfrm_policy_put_afinfo(afinfo);
1291 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1292 * all the metrics... Shortly, bundle a bundle.
1296 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1297 struct flowi *fl, struct dst_entry **dst_p,
1298 unsigned short family)
1301 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1302 if (unlikely(afinfo == NULL))
1304 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1305 xfrm_policy_put_afinfo(afinfo);
1310 static int stale_bundle(struct dst_entry *dst);
1312 /* Main function: finds/creates a bundle for given flow.
1314 * At the moment we eat a raw IP route. Mostly to speed up lookups
1315 * on interfaces with disabled IPsec.
1317 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1318 struct sock *sk, int flags)
1320 struct xfrm_policy *policy;
1321 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1326 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1327 struct dst_entry *dst, *dst_orig = *dst_p;
1332 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1335 genid = atomic_read(&flow_cache_genid);
1337 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1343 if (sk && sk->sk_policy[1])
1344 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1347 /* To accelerate a bit... */
1348 if ((dst_orig->flags & DST_NOXFRM) ||
1349 !xfrm_policy_count[XFRM_POLICY_OUT])
1352 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1353 dir, xfrm_policy_lookup);
1359 family = dst_orig->ops->family;
1360 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
1363 xfrm_nr += pols[0]->xfrm_nr;
1365 switch (policy->action) {
1366 case XFRM_POLICY_BLOCK:
1367 /* Prohibit the flow */
1371 case XFRM_POLICY_ALLOW:
1372 #ifndef CONFIG_XFRM_SUB_POLICY
1373 if (policy->xfrm_nr == 0) {
1374 /* Flow passes not transformed. */
1375 xfrm_pol_put(policy);
1380 /* Try to find matching bundle.
1382 * LATER: help from flow cache. It is optional, this
1383 * is required only for output policy.
1385 dst = xfrm_find_bundle(fl, policy, family);
1394 #ifdef CONFIG_XFRM_SUB_POLICY
1395 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1396 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1400 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1405 xfrm_nr += pols[1]->xfrm_nr;
1410 * Because neither flowi nor bundle information knows about
1411 * transformation template size. On more than one policy usage
1412 * we can realize whether all of them is bypass or not after
1413 * they are searched. See above not-transformed bypass
1414 * is surrounded by non-sub policy configuration, too.
1417 /* Flow passes not transformed. */
1418 xfrm_pols_put(pols, npols);
1423 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1425 if (unlikely(nx<0)) {
1427 if (err == -EAGAIN && flags) {
1428 DECLARE_WAITQUEUE(wait, current);
1430 add_wait_queue(&km_waitq, &wait);
1431 set_current_state(TASK_INTERRUPTIBLE);
1433 set_current_state(TASK_RUNNING);
1434 remove_wait_queue(&km_waitq, &wait);
1436 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1438 if (nx == -EAGAIN && signal_pending(current)) {
1442 if (nx == -EAGAIN ||
1443 genid != atomic_read(&flow_cache_genid)) {
1444 xfrm_pols_put(pols, npols);
1453 /* Flow passes not transformed. */
1454 xfrm_pols_put(pols, npols);
1459 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1461 if (unlikely(err)) {
1463 for (i=0; i<nx; i++)
1464 xfrm_state_put(xfrm[i]);
1468 for (pi = 0; pi < npols; pi++) {
1469 read_lock_bh(&pols[pi]->lock);
1470 pol_dead |= pols[pi]->dead;
1471 read_unlock_bh(&pols[pi]->lock);
1474 write_lock_bh(&policy->lock);
1475 if (unlikely(pol_dead || stale_bundle(dst))) {
1476 /* Wow! While we worked on resolving, this
1477 * policy has gone. Retry. It is not paranoia,
1478 * we just cannot enlist new bundle to dead object.
1479 * We can't enlist stable bundles either.
1481 write_unlock_bh(&policy->lock);
1485 err = -EHOSTUNREACH;
1488 dst->next = policy->bundles;
1489 policy->bundles = dst;
1491 write_unlock_bh(&policy->lock);
1494 dst_release(dst_orig);
1495 xfrm_pols_put(pols, npols);
1499 dst_release(dst_orig);
1500 xfrm_pols_put(pols, npols);
1504 EXPORT_SYMBOL(xfrm_lookup);
1507 xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1509 struct xfrm_state *x;
1512 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1514 x = skb->sp->xvec[idx];
1515 if (!x->type->reject)
1518 err = x->type->reject(x, skb, fl);
1523 /* When skb is transformed back to its "native" form, we have to
1524 * check policy restrictions. At the moment we make this in maximally
1525 * stupid way. Shame on me. :-) Of course, connected sockets must
1526 * have policy cached at them.
1530 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1531 unsigned short family)
1533 if (xfrm_state_kern(x))
1534 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1535 return x->id.proto == tmpl->id.proto &&
1536 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1537 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1538 x->props.mode == tmpl->mode &&
1539 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1540 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1541 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1542 xfrm_state_addr_cmp(tmpl, x, family));
1546 * 0 or more than 0 is returned when validation is succeeded (either bypass
1547 * because of optional transport mode, or next index of the mathced secpath
1548 * state with the template.
1549 * -1 is returned when no matching template is found.
1550 * Otherwise "-2 - errored_index" is returned.
1553 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1554 unsigned short family)
1558 if (tmpl->optional) {
1559 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1563 for (; idx < sp->len; idx++) {
1564 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1566 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1576 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1578 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1581 if (unlikely(afinfo == NULL))
1582 return -EAFNOSUPPORT;
1584 afinfo->decode_session(skb, fl);
1585 err = security_xfrm_decode_session(skb, &fl->secid);
1586 xfrm_policy_put_afinfo(afinfo);
1589 EXPORT_SYMBOL(xfrm_decode_session);
1591 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1593 for (; k < sp->len; k++) {
1594 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1604 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1605 unsigned short family)
1607 struct xfrm_policy *pol;
1608 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1613 u8 fl_dir = policy_to_flow_dir(dir);
1615 int *xerr_idxp = &xerr_idx;
1617 if (xfrm_decode_session(skb, &fl, family) < 0)
1619 nf_nat_decode_session(skb, &fl, family);
1621 /* First, check used SA against their selectors. */
1625 for (i=skb->sp->len-1; i>=0; i--) {
1626 struct xfrm_state *x = skb->sp->xvec[i];
1627 if (!xfrm_selector_match(&x->sel, &fl, family))
1633 if (sk && sk->sk_policy[dir])
1634 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1637 pol = flow_cache_lookup(&fl, family, fl_dir,
1638 xfrm_policy_lookup);
1641 if (skb->sp && secpath_has_nontransport(skb->sp, 0, xerr_idxp)) {
1642 xfrm_secpath_reject(xerr_idx, skb, &fl);
1648 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1652 #ifdef CONFIG_XFRM_SUB_POLICY
1653 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1654 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1658 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1664 if (pol->action == XFRM_POLICY_ALLOW) {
1665 struct sec_path *sp;
1666 static struct sec_path dummy;
1667 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1668 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1669 struct xfrm_tmpl **tpp = tp;
1673 if ((sp = skb->sp) == NULL)
1676 for (pi = 0; pi < npols; pi++) {
1677 if (pols[pi] != pol &&
1678 pols[pi]->action != XFRM_POLICY_ALLOW)
1680 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1682 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1683 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1687 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1691 /* For each tunnel xfrm, find the first matching tmpl.
1692 * For each tmpl before that, find corresponding xfrm.
1693 * Order is _important_. Later we will implement
1694 * some barriers, but at the moment barriers
1695 * are implied between each two transformations.
1697 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1698 k = xfrm_policy_ok(tpp[i], sp, k, family);
1700 if (k < -1 && xerr_idxp)
1701 *xerr_idxp = -(2+k);
1706 if (secpath_has_nontransport(sp, k, xerr_idxp))
1709 xfrm_pols_put(pols, npols);
1714 xfrm_secpath_reject(xerr_idx, skb, &fl);
1716 xfrm_pols_put(pols, npols);
1719 EXPORT_SYMBOL(__xfrm_policy_check);
1721 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1725 if (xfrm_decode_session(skb, &fl, family) < 0)
1728 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1730 EXPORT_SYMBOL(__xfrm_route_forward);
1732 /* Optimize later using cookies and generation ids. */
1734 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1736 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1737 * to "-1" to force all XFRM destinations to get validated by
1738 * dst_ops->check on every use. We do this because when a
1739 * normal route referenced by an XFRM dst is obsoleted we do
1740 * not go looking around for all parent referencing XFRM dsts
1741 * so that we can invalidate them. It is just too much work.
1742 * Instead we make the checks here on every use. For example:
1744 * XFRM dst A --> IPv4 dst X
1746 * X is the "xdst->route" of A (X is also the "dst->path" of A
1747 * in this example). If X is marked obsolete, "A" will not
1748 * notice. That's what we are validating here via the
1749 * stale_bundle() check.
1751 * When a policy's bundle is pruned, we dst_free() the XFRM
1752 * dst which causes it's ->obsolete field to be set to a
1753 * positive non-zero integer. If an XFRM dst has been pruned
1754 * like this, we want to force a new route lookup.
1756 if (dst->obsolete < 0 && !stale_bundle(dst))
1762 static int stale_bundle(struct dst_entry *dst)
1764 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1767 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1769 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1770 dst->dev = &loopback_dev;
1771 dev_hold(&loopback_dev);
1775 EXPORT_SYMBOL(xfrm_dst_ifdown);
1777 static void xfrm_link_failure(struct sk_buff *skb)
1779 /* Impossible. Such dst must be popped before reaches point of failure. */
1783 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1786 if (dst->obsolete) {
1794 static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1796 struct dst_entry *dst, **dstp;
1798 write_lock(&pol->lock);
1799 dstp = &pol->bundles;
1800 while ((dst=*dstp) != NULL) {
1803 dst->next = *gc_list_p;
1809 write_unlock(&pol->lock);
1812 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1814 struct dst_entry *gc_list = NULL;
1817 read_lock_bh(&xfrm_policy_lock);
1818 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1819 struct xfrm_policy *pol;
1820 struct hlist_node *entry;
1821 struct hlist_head *table;
1824 hlist_for_each_entry(pol, entry,
1825 &xfrm_policy_inexact[dir], bydst)
1826 prune_one_bundle(pol, func, &gc_list);
1828 table = xfrm_policy_bydst[dir].table;
1829 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1830 hlist_for_each_entry(pol, entry, table + i, bydst)
1831 prune_one_bundle(pol, func, &gc_list);
1834 read_unlock_bh(&xfrm_policy_lock);
1837 struct dst_entry *dst = gc_list;
1838 gc_list = dst->next;
1843 static int unused_bundle(struct dst_entry *dst)
1845 return !atomic_read(&dst->__refcnt);
1848 static void __xfrm_garbage_collect(void)
1850 xfrm_prune_bundles(unused_bundle);
1853 static int xfrm_flush_bundles(void)
1855 xfrm_prune_bundles(stale_bundle);
1859 void xfrm_init_pmtu(struct dst_entry *dst)
1862 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1863 u32 pmtu, route_mtu_cached;
1865 pmtu = dst_mtu(dst->child);
1866 xdst->child_mtu_cached = pmtu;
1868 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1870 route_mtu_cached = dst_mtu(xdst->route);
1871 xdst->route_mtu_cached = route_mtu_cached;
1873 if (pmtu > route_mtu_cached)
1874 pmtu = route_mtu_cached;
1876 dst->metrics[RTAX_MTU-1] = pmtu;
1877 } while ((dst = dst->next));
1880 EXPORT_SYMBOL(xfrm_init_pmtu);
1882 /* Check that the bundle accepts the flow and its components are
1886 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
1888 struct dst_entry *dst = &first->u.dst;
1889 struct xfrm_dst *last;
1892 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1893 (dst->dev && !netif_running(dst->dev)))
1899 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1901 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1903 if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
1905 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1907 if (xdst->genid != dst->xfrm->genid)
1910 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1911 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1914 mtu = dst_mtu(dst->child);
1915 if (xdst->child_mtu_cached != mtu) {
1917 xdst->child_mtu_cached = mtu;
1920 if (!dst_check(xdst->route, xdst->route_cookie))
1922 mtu = dst_mtu(xdst->route);
1923 if (xdst->route_mtu_cached != mtu) {
1925 xdst->route_mtu_cached = mtu;
1929 } while (dst->xfrm);
1934 mtu = last->child_mtu_cached;
1938 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1939 if (mtu > last->route_mtu_cached)
1940 mtu = last->route_mtu_cached;
1941 dst->metrics[RTAX_MTU-1] = mtu;
1946 last = last->u.next;
1947 last->child_mtu_cached = mtu;
1953 EXPORT_SYMBOL(xfrm_bundle_ok);
1955 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1958 if (unlikely(afinfo == NULL))
1960 if (unlikely(afinfo->family >= NPROTO))
1961 return -EAFNOSUPPORT;
1962 write_lock_bh(&xfrm_policy_afinfo_lock);
1963 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1966 struct dst_ops *dst_ops = afinfo->dst_ops;
1967 if (likely(dst_ops->kmem_cachep == NULL))
1968 dst_ops->kmem_cachep = xfrm_dst_cache;
1969 if (likely(dst_ops->check == NULL))
1970 dst_ops->check = xfrm_dst_check;
1971 if (likely(dst_ops->negative_advice == NULL))
1972 dst_ops->negative_advice = xfrm_negative_advice;
1973 if (likely(dst_ops->link_failure == NULL))
1974 dst_ops->link_failure = xfrm_link_failure;
1975 if (likely(afinfo->garbage_collect == NULL))
1976 afinfo->garbage_collect = __xfrm_garbage_collect;
1977 xfrm_policy_afinfo[afinfo->family] = afinfo;
1979 write_unlock_bh(&xfrm_policy_afinfo_lock);
1982 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1984 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1987 if (unlikely(afinfo == NULL))
1989 if (unlikely(afinfo->family >= NPROTO))
1990 return -EAFNOSUPPORT;
1991 write_lock_bh(&xfrm_policy_afinfo_lock);
1992 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1993 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1996 struct dst_ops *dst_ops = afinfo->dst_ops;
1997 xfrm_policy_afinfo[afinfo->family] = NULL;
1998 dst_ops->kmem_cachep = NULL;
1999 dst_ops->check = NULL;
2000 dst_ops->negative_advice = NULL;
2001 dst_ops->link_failure = NULL;
2002 afinfo->garbage_collect = NULL;
2005 write_unlock_bh(&xfrm_policy_afinfo_lock);
2008 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2010 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2012 struct xfrm_policy_afinfo *afinfo;
2013 if (unlikely(family >= NPROTO))
2015 read_lock(&xfrm_policy_afinfo_lock);
2016 afinfo = xfrm_policy_afinfo[family];
2017 if (unlikely(!afinfo))
2018 read_unlock(&xfrm_policy_afinfo_lock);
2022 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2024 read_unlock(&xfrm_policy_afinfo_lock);
2027 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2029 struct xfrm_policy_afinfo *afinfo;
2030 if (unlikely(family >= NPROTO))
2032 write_lock_bh(&xfrm_policy_afinfo_lock);
2033 afinfo = xfrm_policy_afinfo[family];
2034 if (unlikely(!afinfo))
2035 write_unlock_bh(&xfrm_policy_afinfo_lock);
2039 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2041 write_unlock_bh(&xfrm_policy_afinfo_lock);
2044 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2048 xfrm_flush_bundles();
2053 static struct notifier_block xfrm_dev_notifier = {
2059 static void __init xfrm_policy_init(void)
2061 unsigned int hmask, sz;
2064 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2065 sizeof(struct xfrm_dst),
2066 0, SLAB_HWCACHE_ALIGN,
2068 if (!xfrm_dst_cache)
2069 panic("XFRM: failed to allocate xfrm_dst_cache\n");
2072 sz = (hmask+1) * sizeof(struct hlist_head);
2074 xfrm_policy_byidx = xfrm_policy_hash_alloc(sz);
2075 xfrm_idx_hmask = hmask;
2076 if (!xfrm_policy_byidx)
2077 panic("XFRM: failed to allocate byidx hash\n");
2079 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2080 struct xfrm_policy_hash *htab;
2082 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2084 htab = &xfrm_policy_bydst[dir];
2085 htab->table = xfrm_policy_hash_alloc(sz);
2086 htab->hmask = hmask;
2088 panic("XFRM: failed to allocate bydst hash\n");
2091 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
2092 register_netdevice_notifier(&xfrm_dev_notifier);
2095 void __init xfrm_init(void)