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/cache.h>
29 #include "xfrm_hash.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);
54 __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
56 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
57 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
58 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
59 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
60 (fl->proto == sel->proto || !sel->proto) &&
61 (fl->oif == sel->ifindex || !sel->ifindex);
65 __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
67 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
68 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
69 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
71 (fl->proto == sel->proto || !sel->proto) &&
72 (fl->oif == sel->ifindex || !sel->ifindex);
75 int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
76 unsigned short family)
80 return __xfrm4_selector_match(sel, fl);
82 return __xfrm6_selector_match(sel, fl);
87 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
89 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
90 struct xfrm_type **typemap;
93 if (unlikely(afinfo == NULL))
95 typemap = afinfo->type_map;
97 if (likely(typemap[type->proto] == NULL))
98 typemap[type->proto] = type;
101 xfrm_policy_unlock_afinfo(afinfo);
104 EXPORT_SYMBOL(xfrm_register_type);
106 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
108 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
109 struct xfrm_type **typemap;
112 if (unlikely(afinfo == NULL))
113 return -EAFNOSUPPORT;
114 typemap = afinfo->type_map;
116 if (unlikely(typemap[type->proto] != type))
119 typemap[type->proto] = NULL;
120 xfrm_policy_unlock_afinfo(afinfo);
123 EXPORT_SYMBOL(xfrm_unregister_type);
125 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
127 struct xfrm_policy_afinfo *afinfo;
128 struct xfrm_type **typemap;
129 struct xfrm_type *type;
130 int modload_attempted = 0;
133 afinfo = xfrm_policy_get_afinfo(family);
134 if (unlikely(afinfo == NULL))
136 typemap = afinfo->type_map;
138 type = typemap[proto];
139 if (unlikely(type && !try_module_get(type->owner)))
141 if (!type && !modload_attempted) {
142 xfrm_policy_put_afinfo(afinfo);
143 request_module("xfrm-type-%d-%d",
144 (int) family, (int) proto);
145 modload_attempted = 1;
149 xfrm_policy_put_afinfo(afinfo);
153 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
154 unsigned short family)
156 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
159 if (unlikely(afinfo == NULL))
160 return -EAFNOSUPPORT;
162 if (likely(afinfo->dst_lookup != NULL))
163 err = afinfo->dst_lookup(dst, fl);
166 xfrm_policy_put_afinfo(afinfo);
169 EXPORT_SYMBOL(xfrm_dst_lookup);
171 void xfrm_put_type(struct xfrm_type *type)
173 module_put(type->owner);
176 int xfrm_register_mode(struct xfrm_mode *mode, int family)
178 struct xfrm_policy_afinfo *afinfo;
179 struct xfrm_mode **modemap;
182 if (unlikely(mode->encap >= XFRM_MODE_MAX))
185 afinfo = xfrm_policy_lock_afinfo(family);
186 if (unlikely(afinfo == NULL))
187 return -EAFNOSUPPORT;
190 modemap = afinfo->mode_map;
191 if (likely(modemap[mode->encap] == NULL)) {
192 modemap[mode->encap] = mode;
196 xfrm_policy_unlock_afinfo(afinfo);
199 EXPORT_SYMBOL(xfrm_register_mode);
201 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
203 struct xfrm_policy_afinfo *afinfo;
204 struct xfrm_mode **modemap;
207 if (unlikely(mode->encap >= XFRM_MODE_MAX))
210 afinfo = xfrm_policy_lock_afinfo(family);
211 if (unlikely(afinfo == NULL))
212 return -EAFNOSUPPORT;
215 modemap = afinfo->mode_map;
216 if (likely(modemap[mode->encap] == mode)) {
217 modemap[mode->encap] = NULL;
221 xfrm_policy_unlock_afinfo(afinfo);
224 EXPORT_SYMBOL(xfrm_unregister_mode);
226 struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
228 struct xfrm_policy_afinfo *afinfo;
229 struct xfrm_mode *mode;
230 int modload_attempted = 0;
232 if (unlikely(encap >= XFRM_MODE_MAX))
236 afinfo = xfrm_policy_get_afinfo(family);
237 if (unlikely(afinfo == NULL))
240 mode = afinfo->mode_map[encap];
241 if (unlikely(mode && !try_module_get(mode->owner)))
243 if (!mode && !modload_attempted) {
244 xfrm_policy_put_afinfo(afinfo);
245 request_module("xfrm-mode-%d-%d", family, encap);
246 modload_attempted = 1;
250 xfrm_policy_put_afinfo(afinfo);
254 void xfrm_put_mode(struct xfrm_mode *mode)
256 module_put(mode->owner);
259 static inline unsigned long make_jiffies(long secs)
261 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
262 return MAX_SCHEDULE_TIMEOUT-1;
267 static void xfrm_policy_timer(unsigned long data)
269 struct xfrm_policy *xp = (struct xfrm_policy*)data;
270 unsigned long now = (unsigned long)xtime.tv_sec;
271 long next = LONG_MAX;
275 read_lock(&xp->lock);
280 dir = xfrm_policy_id2dir(xp->index);
282 if (xp->lft.hard_add_expires_seconds) {
283 long tmo = xp->lft.hard_add_expires_seconds +
284 xp->curlft.add_time - now;
290 if (xp->lft.hard_use_expires_seconds) {
291 long tmo = xp->lft.hard_use_expires_seconds +
292 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
298 if (xp->lft.soft_add_expires_seconds) {
299 long tmo = xp->lft.soft_add_expires_seconds +
300 xp->curlft.add_time - now;
303 tmo = XFRM_KM_TIMEOUT;
308 if (xp->lft.soft_use_expires_seconds) {
309 long tmo = xp->lft.soft_use_expires_seconds +
310 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
313 tmo = XFRM_KM_TIMEOUT;
320 km_policy_expired(xp, dir, 0, 0);
321 if (next != LONG_MAX &&
322 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
326 read_unlock(&xp->lock);
331 read_unlock(&xp->lock);
332 if (!xfrm_policy_delete(xp, dir))
333 km_policy_expired(xp, dir, 1, 0);
338 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
342 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
344 struct xfrm_policy *policy;
346 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
349 INIT_HLIST_NODE(&policy->bydst);
350 INIT_HLIST_NODE(&policy->byidx);
351 rwlock_init(&policy->lock);
352 atomic_set(&policy->refcnt, 1);
353 init_timer(&policy->timer);
354 policy->timer.data = (unsigned long)policy;
355 policy->timer.function = xfrm_policy_timer;
359 EXPORT_SYMBOL(xfrm_policy_alloc);
361 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
363 void __xfrm_policy_destroy(struct xfrm_policy *policy)
365 BUG_ON(!policy->dead);
367 BUG_ON(policy->bundles);
369 if (del_timer(&policy->timer))
372 security_xfrm_policy_free(policy);
375 EXPORT_SYMBOL(__xfrm_policy_destroy);
377 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
379 struct dst_entry *dst;
381 while ((dst = policy->bundles) != NULL) {
382 policy->bundles = dst->next;
386 if (del_timer(&policy->timer))
387 atomic_dec(&policy->refcnt);
389 if (atomic_read(&policy->refcnt) > 1)
392 xfrm_pol_put(policy);
395 static void xfrm_policy_gc_task(struct work_struct *work)
397 struct xfrm_policy *policy;
398 struct hlist_node *entry, *tmp;
399 struct hlist_head gc_list;
401 spin_lock_bh(&xfrm_policy_gc_lock);
402 gc_list.first = xfrm_policy_gc_list.first;
403 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
404 spin_unlock_bh(&xfrm_policy_gc_lock);
406 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
407 xfrm_policy_gc_kill(policy);
410 /* Rule must be locked. Release descentant resources, announce
411 * entry dead. The rule must be unlinked from lists to the moment.
414 static void xfrm_policy_kill(struct xfrm_policy *policy)
418 write_lock_bh(&policy->lock);
421 write_unlock_bh(&policy->lock);
423 if (unlikely(dead)) {
428 spin_lock(&xfrm_policy_gc_lock);
429 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
430 spin_unlock(&xfrm_policy_gc_lock);
432 schedule_work(&xfrm_policy_gc_work);
435 struct xfrm_policy_hash {
436 struct hlist_head *table;
440 static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
441 static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
442 static struct hlist_head *xfrm_policy_byidx __read_mostly;
443 static unsigned int xfrm_idx_hmask __read_mostly;
444 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
446 static inline unsigned int idx_hash(u32 index)
448 return __idx_hash(index, xfrm_idx_hmask);
451 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
453 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
454 unsigned int hash = __sel_hash(sel, family, hmask);
456 return (hash == hmask + 1 ?
457 &xfrm_policy_inexact[dir] :
458 xfrm_policy_bydst[dir].table + hash);
461 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
463 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
464 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
466 return xfrm_policy_bydst[dir].table + hash;
469 static void xfrm_dst_hash_transfer(struct hlist_head *list,
470 struct hlist_head *ndsttable,
471 unsigned int nhashmask)
473 struct hlist_node *entry, *tmp;
474 struct xfrm_policy *pol;
476 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
479 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
480 pol->family, nhashmask);
481 hlist_add_head(&pol->bydst, ndsttable+h);
485 static void xfrm_idx_hash_transfer(struct hlist_head *list,
486 struct hlist_head *nidxtable,
487 unsigned int nhashmask)
489 struct hlist_node *entry, *tmp;
490 struct xfrm_policy *pol;
492 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
495 h = __idx_hash(pol->index, nhashmask);
496 hlist_add_head(&pol->byidx, nidxtable+h);
500 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
502 return ((old_hmask + 1) << 1) - 1;
505 static void xfrm_bydst_resize(int dir)
507 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
508 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
509 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
510 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
511 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
517 write_lock_bh(&xfrm_policy_lock);
519 for (i = hmask; i >= 0; i--)
520 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
522 xfrm_policy_bydst[dir].table = ndst;
523 xfrm_policy_bydst[dir].hmask = nhashmask;
525 write_unlock_bh(&xfrm_policy_lock);
527 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
530 static void xfrm_byidx_resize(int total)
532 unsigned int hmask = xfrm_idx_hmask;
533 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
534 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
535 struct hlist_head *oidx = xfrm_policy_byidx;
536 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
542 write_lock_bh(&xfrm_policy_lock);
544 for (i = hmask; i >= 0; i--)
545 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
547 xfrm_policy_byidx = nidx;
548 xfrm_idx_hmask = nhashmask;
550 write_unlock_bh(&xfrm_policy_lock);
552 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
555 static inline int xfrm_bydst_should_resize(int dir, int *total)
557 unsigned int cnt = xfrm_policy_count[dir];
558 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
563 if ((hmask + 1) < xfrm_policy_hashmax &&
570 static inline int xfrm_byidx_should_resize(int total)
572 unsigned int hmask = xfrm_idx_hmask;
574 if ((hmask + 1) < xfrm_policy_hashmax &&
581 static DEFINE_MUTEX(hash_resize_mutex);
583 static void xfrm_hash_resize(struct work_struct *__unused)
587 mutex_lock(&hash_resize_mutex);
590 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
591 if (xfrm_bydst_should_resize(dir, &total))
592 xfrm_bydst_resize(dir);
594 if (xfrm_byidx_should_resize(total))
595 xfrm_byidx_resize(total);
597 mutex_unlock(&hash_resize_mutex);
600 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
602 /* Generate new index... KAME seems to generate them ordered by cost
603 * of an absolute inpredictability of ordering of rules. This will not pass. */
604 static u32 xfrm_gen_index(u8 type, int dir)
606 static u32 idx_generator;
609 struct hlist_node *entry;
610 struct hlist_head *list;
611 struct xfrm_policy *p;
615 idx = (idx_generator | dir);
619 list = xfrm_policy_byidx + idx_hash(idx);
621 hlist_for_each_entry(p, entry, list, byidx) {
622 if (p->index == idx) {
632 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
634 u32 *p1 = (u32 *) s1;
635 u32 *p2 = (u32 *) s2;
636 int len = sizeof(struct xfrm_selector) / sizeof(u32);
639 for (i = 0; i < len; i++) {
647 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
649 struct xfrm_policy *pol;
650 struct xfrm_policy *delpol;
651 struct hlist_head *chain;
652 struct hlist_node *entry, *newpos, *last;
653 struct dst_entry *gc_list;
655 write_lock_bh(&xfrm_policy_lock);
656 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
660 hlist_for_each_entry(pol, entry, chain, bydst) {
662 pol->type == policy->type &&
663 !selector_cmp(&pol->selector, &policy->selector) &&
664 xfrm_sec_ctx_match(pol->security, policy->security)) {
666 write_unlock_bh(&xfrm_policy_lock);
670 if (policy->priority > pol->priority)
672 } else if (policy->priority >= pol->priority) {
677 newpos = &pol->bydst;
685 hlist_add_after(newpos, &policy->bydst);
687 hlist_add_head(&policy->bydst, chain);
688 xfrm_pol_hold(policy);
689 xfrm_policy_count[dir]++;
690 atomic_inc(&flow_cache_genid);
692 hlist_del(&delpol->bydst);
693 hlist_del(&delpol->byidx);
694 xfrm_policy_count[dir]--;
696 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
697 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
698 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
699 policy->curlft.use_time = 0;
700 if (!mod_timer(&policy->timer, jiffies + HZ))
701 xfrm_pol_hold(policy);
702 write_unlock_bh(&xfrm_policy_lock);
705 xfrm_policy_kill(delpol);
706 else if (xfrm_bydst_should_resize(dir, NULL))
707 schedule_work(&xfrm_hash_work);
709 read_lock_bh(&xfrm_policy_lock);
711 entry = &policy->bydst;
712 hlist_for_each_entry_continue(policy, entry, bydst) {
713 struct dst_entry *dst;
715 write_lock(&policy->lock);
716 dst = policy->bundles;
718 struct dst_entry *tail = dst;
721 tail->next = gc_list;
724 policy->bundles = NULL;
726 write_unlock(&policy->lock);
728 read_unlock_bh(&xfrm_policy_lock);
731 struct dst_entry *dst = gc_list;
739 EXPORT_SYMBOL(xfrm_policy_insert);
741 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
742 struct xfrm_selector *sel,
743 struct xfrm_sec_ctx *ctx, int delete)
745 struct xfrm_policy *pol, *ret;
746 struct hlist_head *chain;
747 struct hlist_node *entry;
749 write_lock_bh(&xfrm_policy_lock);
750 chain = policy_hash_bysel(sel, sel->family, dir);
752 hlist_for_each_entry(pol, entry, chain, bydst) {
753 if (pol->type == type &&
754 !selector_cmp(sel, &pol->selector) &&
755 xfrm_sec_ctx_match(ctx, pol->security)) {
758 hlist_del(&pol->bydst);
759 hlist_del(&pol->byidx);
760 xfrm_policy_count[dir]--;
766 write_unlock_bh(&xfrm_policy_lock);
769 atomic_inc(&flow_cache_genid);
770 xfrm_policy_kill(ret);
774 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
776 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
778 struct xfrm_policy *pol, *ret;
779 struct hlist_head *chain;
780 struct hlist_node *entry;
782 write_lock_bh(&xfrm_policy_lock);
783 chain = xfrm_policy_byidx + idx_hash(id);
785 hlist_for_each_entry(pol, entry, chain, byidx) {
786 if (pol->type == type && pol->index == id) {
789 hlist_del(&pol->bydst);
790 hlist_del(&pol->byidx);
791 xfrm_policy_count[dir]--;
797 write_unlock_bh(&xfrm_policy_lock);
800 atomic_inc(&flow_cache_genid);
801 xfrm_policy_kill(ret);
805 EXPORT_SYMBOL(xfrm_policy_byid);
807 void xfrm_policy_flush(u8 type)
811 write_lock_bh(&xfrm_policy_lock);
812 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
813 struct xfrm_policy *pol;
814 struct hlist_node *entry;
819 hlist_for_each_entry(pol, entry,
820 &xfrm_policy_inexact[dir], bydst) {
821 if (pol->type != type)
823 hlist_del(&pol->bydst);
824 hlist_del(&pol->byidx);
825 write_unlock_bh(&xfrm_policy_lock);
827 xfrm_policy_kill(pol);
830 write_lock_bh(&xfrm_policy_lock);
834 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
836 hlist_for_each_entry(pol, entry,
837 xfrm_policy_bydst[dir].table + i,
839 if (pol->type != type)
841 hlist_del(&pol->bydst);
842 hlist_del(&pol->byidx);
843 write_unlock_bh(&xfrm_policy_lock);
845 xfrm_policy_kill(pol);
848 write_lock_bh(&xfrm_policy_lock);
853 xfrm_policy_count[dir] -= killed;
855 atomic_inc(&flow_cache_genid);
856 write_unlock_bh(&xfrm_policy_lock);
858 EXPORT_SYMBOL(xfrm_policy_flush);
860 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
863 struct xfrm_policy *pol, *last = NULL;
864 struct hlist_node *entry;
865 int dir, last_dir = 0, count, error;
867 read_lock_bh(&xfrm_policy_lock);
870 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
871 struct hlist_head *table = xfrm_policy_bydst[dir].table;
874 hlist_for_each_entry(pol, entry,
875 &xfrm_policy_inexact[dir], bydst) {
876 if (pol->type != type)
879 error = func(last, last_dir % XFRM_POLICY_MAX,
888 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
889 hlist_for_each_entry(pol, entry, table + i, bydst) {
890 if (pol->type != type)
893 error = func(last, last_dir % XFRM_POLICY_MAX,
908 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
910 read_unlock_bh(&xfrm_policy_lock);
913 EXPORT_SYMBOL(xfrm_policy_walk);
916 * Find policy to apply to this flow.
918 * Returns 0 if policy found, else an -errno.
920 static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
921 u8 type, u16 family, int dir)
923 struct xfrm_selector *sel = &pol->selector;
924 int match, ret = -ESRCH;
926 if (pol->family != family ||
930 match = xfrm_selector_match(sel, fl, family);
932 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
937 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
941 struct xfrm_policy *pol, *ret;
942 xfrm_address_t *daddr, *saddr;
943 struct hlist_node *entry;
944 struct hlist_head *chain;
947 daddr = xfrm_flowi_daddr(fl, family);
948 saddr = xfrm_flowi_saddr(fl, family);
949 if (unlikely(!daddr || !saddr))
952 read_lock_bh(&xfrm_policy_lock);
953 chain = policy_hash_direct(daddr, saddr, family, dir);
955 hlist_for_each_entry(pol, entry, chain, bydst) {
956 err = xfrm_policy_match(pol, fl, type, family, dir);
966 priority = ret->priority;
970 chain = &xfrm_policy_inexact[dir];
971 hlist_for_each_entry(pol, entry, chain, bydst) {
972 err = xfrm_policy_match(pol, fl, type, family, dir);
980 } else if (pol->priority < priority) {
988 read_unlock_bh(&xfrm_policy_lock);
993 static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
994 void **objp, atomic_t **obj_refp)
996 struct xfrm_policy *pol;
999 #ifdef CONFIG_XFRM_SUB_POLICY
1000 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
1008 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1013 #ifdef CONFIG_XFRM_SUB_POLICY
1016 if ((*objp = (void *) pol) != NULL)
1017 *obj_refp = &pol->refcnt;
1021 static inline int policy_to_flow_dir(int dir)
1023 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1024 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1025 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1029 case XFRM_POLICY_IN:
1031 case XFRM_POLICY_OUT:
1032 return FLOW_DIR_OUT;
1033 case XFRM_POLICY_FWD:
1034 return FLOW_DIR_FWD;
1038 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1040 struct xfrm_policy *pol;
1042 read_lock_bh(&xfrm_policy_lock);
1043 if ((pol = sk->sk_policy[dir]) != NULL) {
1044 int match = xfrm_selector_match(&pol->selector, fl,
1049 err = security_xfrm_policy_lookup(pol, fl->secid,
1050 policy_to_flow_dir(dir));
1053 else if (err == -ESRCH)
1060 read_unlock_bh(&xfrm_policy_lock);
1064 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1066 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1069 hlist_add_head(&pol->bydst, chain);
1070 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1071 xfrm_policy_count[dir]++;
1074 if (xfrm_bydst_should_resize(dir, NULL))
1075 schedule_work(&xfrm_hash_work);
1078 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1081 if (hlist_unhashed(&pol->bydst))
1084 hlist_del(&pol->bydst);
1085 hlist_del(&pol->byidx);
1086 xfrm_policy_count[dir]--;
1091 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1093 write_lock_bh(&xfrm_policy_lock);
1094 pol = __xfrm_policy_unlink(pol, dir);
1095 write_unlock_bh(&xfrm_policy_lock);
1097 if (dir < XFRM_POLICY_MAX)
1098 atomic_inc(&flow_cache_genid);
1099 xfrm_policy_kill(pol);
1104 EXPORT_SYMBOL(xfrm_policy_delete);
1106 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1108 struct xfrm_policy *old_pol;
1110 #ifdef CONFIG_XFRM_SUB_POLICY
1111 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1115 write_lock_bh(&xfrm_policy_lock);
1116 old_pol = sk->sk_policy[dir];
1117 sk->sk_policy[dir] = pol;
1119 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
1120 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1121 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1124 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1125 write_unlock_bh(&xfrm_policy_lock);
1128 xfrm_policy_kill(old_pol);
1133 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1135 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1138 newp->selector = old->selector;
1139 if (security_xfrm_policy_clone(old, newp)) {
1141 return NULL; /* ENOMEM */
1143 newp->lft = old->lft;
1144 newp->curlft = old->curlft;
1145 newp->action = old->action;
1146 newp->flags = old->flags;
1147 newp->xfrm_nr = old->xfrm_nr;
1148 newp->index = old->index;
1149 newp->type = old->type;
1150 memcpy(newp->xfrm_vec, old->xfrm_vec,
1151 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1152 write_lock_bh(&xfrm_policy_lock);
1153 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1154 write_unlock_bh(&xfrm_policy_lock);
1160 int __xfrm_sk_clone_policy(struct sock *sk)
1162 struct xfrm_policy *p0 = sk->sk_policy[0],
1163 *p1 = sk->sk_policy[1];
1165 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1166 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1168 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1174 xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1175 unsigned short family)
1178 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1180 if (unlikely(afinfo == NULL))
1182 err = afinfo->get_saddr(local, remote);
1183 xfrm_policy_put_afinfo(afinfo);
1187 /* Resolve list of templates for the flow, given policy. */
1190 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1191 struct xfrm_state **xfrm,
1192 unsigned short family)
1196 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1197 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;
1209 family = tmpl->encap_family;
1210 if (xfrm_addr_any(local, family)) {
1211 error = xfrm_get_saddr(&tmp, remote, family);
1218 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1220 if (x && x->km.state == XFRM_STATE_VALID) {
1227 error = (x->km.state == XFRM_STATE_ERROR ?
1232 if (!tmpl->optional)
1238 for (nx--; nx>=0; nx--)
1239 xfrm_state_put(xfrm[nx]);
1244 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1245 struct xfrm_state **xfrm,
1246 unsigned short family)
1248 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1249 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1255 for (i = 0; i < npols; i++) {
1256 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1261 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1269 /* found states are sorted for outbound processing */
1271 xfrm_state_sort(xfrm, tpp, cnx, family);
1276 for (cnx--; cnx>=0; cnx--)
1277 xfrm_state_put(tpp[cnx]);
1282 /* Check that the bundle accepts the flow and its components are
1286 static struct dst_entry *
1287 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1289 struct dst_entry *x;
1290 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1291 if (unlikely(afinfo == NULL))
1292 return ERR_PTR(-EINVAL);
1293 x = afinfo->find_bundle(fl, policy);
1294 xfrm_policy_put_afinfo(afinfo);
1298 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1299 * all the metrics... Shortly, bundle a bundle.
1303 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1304 struct flowi *fl, struct dst_entry **dst_p,
1305 unsigned short family)
1308 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1309 if (unlikely(afinfo == NULL))
1311 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1312 xfrm_policy_put_afinfo(afinfo);
1317 static int stale_bundle(struct dst_entry *dst);
1319 /* Main function: finds/creates a bundle for given flow.
1321 * At the moment we eat a raw IP route. Mostly to speed up lookups
1322 * on interfaces with disabled IPsec.
1324 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1325 struct sock *sk, int flags)
1327 struct xfrm_policy *policy;
1328 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1333 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1334 struct dst_entry *dst, *dst_orig = *dst_p;
1339 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1342 genid = atomic_read(&flow_cache_genid);
1344 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1350 if (sk && sk->sk_policy[1]) {
1351 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1353 return PTR_ERR(policy);
1357 /* To accelerate a bit... */
1358 if ((dst_orig->flags & DST_NOXFRM) ||
1359 !xfrm_policy_count[XFRM_POLICY_OUT])
1362 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1363 dir, xfrm_policy_lookup);
1365 return PTR_ERR(policy);
1371 family = dst_orig->ops->family;
1372 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
1375 xfrm_nr += pols[0]->xfrm_nr;
1377 switch (policy->action) {
1378 case XFRM_POLICY_BLOCK:
1379 /* Prohibit the flow */
1383 case XFRM_POLICY_ALLOW:
1384 #ifndef CONFIG_XFRM_SUB_POLICY
1385 if (policy->xfrm_nr == 0) {
1386 /* Flow passes not transformed. */
1387 xfrm_pol_put(policy);
1392 /* Try to find matching bundle.
1394 * LATER: help from flow cache. It is optional, this
1395 * is required only for output policy.
1397 dst = xfrm_find_bundle(fl, policy, family);
1406 #ifdef CONFIG_XFRM_SUB_POLICY
1407 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1408 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1412 if (IS_ERR(pols[1])) {
1413 err = PTR_ERR(pols[1]);
1416 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1421 xfrm_nr += pols[1]->xfrm_nr;
1426 * Because neither flowi nor bundle information knows about
1427 * transformation template size. On more than one policy usage
1428 * we can realize whether all of them is bypass or not after
1429 * they are searched. See above not-transformed bypass
1430 * is surrounded by non-sub policy configuration, too.
1433 /* Flow passes not transformed. */
1434 xfrm_pols_put(pols, npols);
1439 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1441 if (unlikely(nx<0)) {
1443 if (err == -EAGAIN && flags) {
1444 DECLARE_WAITQUEUE(wait, current);
1446 add_wait_queue(&km_waitq, &wait);
1447 set_current_state(TASK_INTERRUPTIBLE);
1449 set_current_state(TASK_RUNNING);
1450 remove_wait_queue(&km_waitq, &wait);
1452 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1454 if (nx == -EAGAIN && signal_pending(current)) {
1458 if (nx == -EAGAIN ||
1459 genid != atomic_read(&flow_cache_genid)) {
1460 xfrm_pols_put(pols, npols);
1469 /* Flow passes not transformed. */
1470 xfrm_pols_put(pols, npols);
1475 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1477 if (unlikely(err)) {
1479 for (i=0; i<nx; i++)
1480 xfrm_state_put(xfrm[i]);
1484 for (pi = 0; pi < npols; pi++) {
1485 read_lock_bh(&pols[pi]->lock);
1486 pol_dead |= pols[pi]->dead;
1487 read_unlock_bh(&pols[pi]->lock);
1490 write_lock_bh(&policy->lock);
1491 if (unlikely(pol_dead || stale_bundle(dst))) {
1492 /* Wow! While we worked on resolving, this
1493 * policy has gone. Retry. It is not paranoia,
1494 * we just cannot enlist new bundle to dead object.
1495 * We can't enlist stable bundles either.
1497 write_unlock_bh(&policy->lock);
1501 err = -EHOSTUNREACH;
1504 dst->next = policy->bundles;
1505 policy->bundles = dst;
1507 write_unlock_bh(&policy->lock);
1510 dst_release(dst_orig);
1511 xfrm_pols_put(pols, npols);
1515 dst_release(dst_orig);
1516 xfrm_pols_put(pols, npols);
1520 EXPORT_SYMBOL(xfrm_lookup);
1523 xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1525 struct xfrm_state *x;
1528 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1530 x = skb->sp->xvec[idx];
1531 if (!x->type->reject)
1534 err = x->type->reject(x, skb, fl);
1539 /* When skb is transformed back to its "native" form, we have to
1540 * check policy restrictions. At the moment we make this in maximally
1541 * stupid way. Shame on me. :-) Of course, connected sockets must
1542 * have policy cached at them.
1546 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1547 unsigned short family)
1549 if (xfrm_state_kern(x))
1550 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1551 return x->id.proto == tmpl->id.proto &&
1552 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1553 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1554 x->props.mode == tmpl->mode &&
1555 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1556 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1557 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1558 xfrm_state_addr_cmp(tmpl, x, family));
1562 * 0 or more than 0 is returned when validation is succeeded (either bypass
1563 * because of optional transport mode, or next index of the mathced secpath
1564 * state with the template.
1565 * -1 is returned when no matching template is found.
1566 * Otherwise "-2 - errored_index" is returned.
1569 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1570 unsigned short family)
1574 if (tmpl->optional) {
1575 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1579 for (; idx < sp->len; idx++) {
1580 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1582 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1592 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1594 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1597 if (unlikely(afinfo == NULL))
1598 return -EAFNOSUPPORT;
1600 afinfo->decode_session(skb, fl);
1601 err = security_xfrm_decode_session(skb, &fl->secid);
1602 xfrm_policy_put_afinfo(afinfo);
1605 EXPORT_SYMBOL(xfrm_decode_session);
1607 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1609 for (; k < sp->len; k++) {
1610 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1619 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1620 unsigned short family)
1622 struct xfrm_policy *pol;
1623 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1628 u8 fl_dir = policy_to_flow_dir(dir);
1631 if (xfrm_decode_session(skb, &fl, family) < 0)
1633 nf_nat_decode_session(skb, &fl, family);
1635 /* First, check used SA against their selectors. */
1639 for (i=skb->sp->len-1; i>=0; i--) {
1640 struct xfrm_state *x = skb->sp->xvec[i];
1641 if (!xfrm_selector_match(&x->sel, &fl, family))
1647 if (sk && sk->sk_policy[dir]) {
1648 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1654 pol = flow_cache_lookup(&fl, family, fl_dir,
1655 xfrm_policy_lookup);
1661 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
1662 xfrm_secpath_reject(xerr_idx, skb, &fl);
1668 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1672 #ifdef CONFIG_XFRM_SUB_POLICY
1673 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1674 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1678 if (IS_ERR(pols[1]))
1680 pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
1686 if (pol->action == XFRM_POLICY_ALLOW) {
1687 struct sec_path *sp;
1688 static struct sec_path dummy;
1689 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1690 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1691 struct xfrm_tmpl **tpp = tp;
1695 if ((sp = skb->sp) == NULL)
1698 for (pi = 0; pi < npols; pi++) {
1699 if (pols[pi] != pol &&
1700 pols[pi]->action != XFRM_POLICY_ALLOW)
1702 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1704 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1705 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1709 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1713 /* For each tunnel xfrm, find the first matching tmpl.
1714 * For each tmpl before that, find corresponding xfrm.
1715 * Order is _important_. Later we will implement
1716 * some barriers, but at the moment barriers
1717 * are implied between each two transformations.
1719 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1720 k = xfrm_policy_ok(tpp[i], sp, k, family);
1723 /* "-2 - errored_index" returned */
1729 if (secpath_has_nontransport(sp, k, &xerr_idx))
1732 xfrm_pols_put(pols, npols);
1737 xfrm_secpath_reject(xerr_idx, skb, &fl);
1739 xfrm_pols_put(pols, npols);
1742 EXPORT_SYMBOL(__xfrm_policy_check);
1744 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1748 if (xfrm_decode_session(skb, &fl, family) < 0)
1751 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1753 EXPORT_SYMBOL(__xfrm_route_forward);
1755 /* Optimize later using cookies and generation ids. */
1757 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1759 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1760 * to "-1" to force all XFRM destinations to get validated by
1761 * dst_ops->check on every use. We do this because when a
1762 * normal route referenced by an XFRM dst is obsoleted we do
1763 * not go looking around for all parent referencing XFRM dsts
1764 * so that we can invalidate them. It is just too much work.
1765 * Instead we make the checks here on every use. For example:
1767 * XFRM dst A --> IPv4 dst X
1769 * X is the "xdst->route" of A (X is also the "dst->path" of A
1770 * in this example). If X is marked obsolete, "A" will not
1771 * notice. That's what we are validating here via the
1772 * stale_bundle() check.
1774 * When a policy's bundle is pruned, we dst_free() the XFRM
1775 * dst which causes it's ->obsolete field to be set to a
1776 * positive non-zero integer. If an XFRM dst has been pruned
1777 * like this, we want to force a new route lookup.
1779 if (dst->obsolete < 0 && !stale_bundle(dst))
1785 static int stale_bundle(struct dst_entry *dst)
1787 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1790 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1792 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1793 dst->dev = &loopback_dev;
1794 dev_hold(&loopback_dev);
1798 EXPORT_SYMBOL(xfrm_dst_ifdown);
1800 static void xfrm_link_failure(struct sk_buff *skb)
1802 /* Impossible. Such dst must be popped before reaches point of failure. */
1806 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1809 if (dst->obsolete) {
1817 static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1819 struct dst_entry *dst, **dstp;
1821 write_lock(&pol->lock);
1822 dstp = &pol->bundles;
1823 while ((dst=*dstp) != NULL) {
1826 dst->next = *gc_list_p;
1832 write_unlock(&pol->lock);
1835 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1837 struct dst_entry *gc_list = NULL;
1840 read_lock_bh(&xfrm_policy_lock);
1841 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
1842 struct xfrm_policy *pol;
1843 struct hlist_node *entry;
1844 struct hlist_head *table;
1847 hlist_for_each_entry(pol, entry,
1848 &xfrm_policy_inexact[dir], bydst)
1849 prune_one_bundle(pol, func, &gc_list);
1851 table = xfrm_policy_bydst[dir].table;
1852 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
1853 hlist_for_each_entry(pol, entry, table + i, bydst)
1854 prune_one_bundle(pol, func, &gc_list);
1857 read_unlock_bh(&xfrm_policy_lock);
1860 struct dst_entry *dst = gc_list;
1861 gc_list = dst->next;
1866 static int unused_bundle(struct dst_entry *dst)
1868 return !atomic_read(&dst->__refcnt);
1871 static void __xfrm_garbage_collect(void)
1873 xfrm_prune_bundles(unused_bundle);
1876 static int xfrm_flush_bundles(void)
1878 xfrm_prune_bundles(stale_bundle);
1882 void xfrm_init_pmtu(struct dst_entry *dst)
1885 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1886 u32 pmtu, route_mtu_cached;
1888 pmtu = dst_mtu(dst->child);
1889 xdst->child_mtu_cached = pmtu;
1891 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1893 route_mtu_cached = dst_mtu(xdst->route);
1894 xdst->route_mtu_cached = route_mtu_cached;
1896 if (pmtu > route_mtu_cached)
1897 pmtu = route_mtu_cached;
1899 dst->metrics[RTAX_MTU-1] = pmtu;
1900 } while ((dst = dst->next));
1903 EXPORT_SYMBOL(xfrm_init_pmtu);
1905 /* Check that the bundle accepts the flow and its components are
1909 int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
1910 struct flowi *fl, int family, int strict)
1912 struct dst_entry *dst = &first->u.dst;
1913 struct xfrm_dst *last;
1916 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1917 (dst->dev && !netif_running(dst->dev)))
1923 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1925 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1928 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
1930 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1932 if (xdst->genid != dst->xfrm->genid)
1935 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
1936 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
1939 mtu = dst_mtu(dst->child);
1940 if (xdst->child_mtu_cached != mtu) {
1942 xdst->child_mtu_cached = mtu;
1945 if (!dst_check(xdst->route, xdst->route_cookie))
1947 mtu = dst_mtu(xdst->route);
1948 if (xdst->route_mtu_cached != mtu) {
1950 xdst->route_mtu_cached = mtu;
1954 } while (dst->xfrm);
1959 mtu = last->child_mtu_cached;
1963 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1964 if (mtu > last->route_mtu_cached)
1965 mtu = last->route_mtu_cached;
1966 dst->metrics[RTAX_MTU-1] = mtu;
1971 last = last->u.next;
1972 last->child_mtu_cached = mtu;
1978 EXPORT_SYMBOL(xfrm_bundle_ok);
1980 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1983 if (unlikely(afinfo == NULL))
1985 if (unlikely(afinfo->family >= NPROTO))
1986 return -EAFNOSUPPORT;
1987 write_lock_bh(&xfrm_policy_afinfo_lock);
1988 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1991 struct dst_ops *dst_ops = afinfo->dst_ops;
1992 if (likely(dst_ops->kmem_cachep == NULL))
1993 dst_ops->kmem_cachep = xfrm_dst_cache;
1994 if (likely(dst_ops->check == NULL))
1995 dst_ops->check = xfrm_dst_check;
1996 if (likely(dst_ops->negative_advice == NULL))
1997 dst_ops->negative_advice = xfrm_negative_advice;
1998 if (likely(dst_ops->link_failure == NULL))
1999 dst_ops->link_failure = xfrm_link_failure;
2000 if (likely(afinfo->garbage_collect == NULL))
2001 afinfo->garbage_collect = __xfrm_garbage_collect;
2002 xfrm_policy_afinfo[afinfo->family] = afinfo;
2004 write_unlock_bh(&xfrm_policy_afinfo_lock);
2007 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2009 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2012 if (unlikely(afinfo == NULL))
2014 if (unlikely(afinfo->family >= NPROTO))
2015 return -EAFNOSUPPORT;
2016 write_lock_bh(&xfrm_policy_afinfo_lock);
2017 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2018 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2021 struct dst_ops *dst_ops = afinfo->dst_ops;
2022 xfrm_policy_afinfo[afinfo->family] = NULL;
2023 dst_ops->kmem_cachep = NULL;
2024 dst_ops->check = NULL;
2025 dst_ops->negative_advice = NULL;
2026 dst_ops->link_failure = NULL;
2027 afinfo->garbage_collect = NULL;
2030 write_unlock_bh(&xfrm_policy_afinfo_lock);
2033 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2035 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2037 struct xfrm_policy_afinfo *afinfo;
2038 if (unlikely(family >= NPROTO))
2040 read_lock(&xfrm_policy_afinfo_lock);
2041 afinfo = xfrm_policy_afinfo[family];
2042 if (unlikely(!afinfo))
2043 read_unlock(&xfrm_policy_afinfo_lock);
2047 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2049 read_unlock(&xfrm_policy_afinfo_lock);
2052 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2054 struct xfrm_policy_afinfo *afinfo;
2055 if (unlikely(family >= NPROTO))
2057 write_lock_bh(&xfrm_policy_afinfo_lock);
2058 afinfo = xfrm_policy_afinfo[family];
2059 if (unlikely(!afinfo))
2060 write_unlock_bh(&xfrm_policy_afinfo_lock);
2064 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2066 write_unlock_bh(&xfrm_policy_afinfo_lock);
2069 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2073 xfrm_flush_bundles();
2078 static struct notifier_block xfrm_dev_notifier = {
2084 static void __init xfrm_policy_init(void)
2086 unsigned int hmask, sz;
2089 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2090 sizeof(struct xfrm_dst),
2091 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2095 sz = (hmask+1) * sizeof(struct hlist_head);
2097 xfrm_policy_byidx = xfrm_hash_alloc(sz);
2098 xfrm_idx_hmask = hmask;
2099 if (!xfrm_policy_byidx)
2100 panic("XFRM: failed to allocate byidx hash\n");
2102 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2103 struct xfrm_policy_hash *htab;
2105 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2107 htab = &xfrm_policy_bydst[dir];
2108 htab->table = xfrm_hash_alloc(sz);
2109 htab->hmask = hmask;
2111 panic("XFRM: failed to allocate bydst hash\n");
2114 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
2115 register_netdevice_notifier(&xfrm_dev_notifier);
2118 void __init xfrm_init(void)