6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
30 #ifdef CONFIG_XFRM_STATISTICS
34 #include "xfrm_hash.h"
36 int sysctl_xfrm_larval_drop __read_mostly;
38 #ifdef CONFIG_XFRM_STATISTICS
39 DEFINE_SNMP_STAT(struct linux_xfrm_mib, xfrm_statistics) __read_mostly;
40 EXPORT_SYMBOL(xfrm_statistics);
43 DEFINE_MUTEX(xfrm_cfg_mutex);
44 EXPORT_SYMBOL(xfrm_cfg_mutex);
46 static DEFINE_RWLOCK(xfrm_policy_lock);
48 unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
49 EXPORT_SYMBOL(xfrm_policy_count);
51 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
52 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
54 static struct kmem_cache *xfrm_dst_cache __read_mostly;
56 static struct work_struct xfrm_policy_gc_work;
57 static HLIST_HEAD(xfrm_policy_gc_list);
58 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
60 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
61 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
62 static void xfrm_init_pmtu(struct dst_entry *dst);
65 __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
67 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
68 addr_match(&fl->fl4_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);
76 __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
78 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
79 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
80 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
81 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
82 (fl->proto == sel->proto || !sel->proto) &&
83 (fl->oif == sel->ifindex || !sel->ifindex);
86 int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
87 unsigned short family)
91 return __xfrm4_selector_match(sel, fl);
93 return __xfrm6_selector_match(sel, fl);
98 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
101 xfrm_address_t *saddr = &x->props.saddr;
102 xfrm_address_t *daddr = &x->id.daddr;
103 struct xfrm_policy_afinfo *afinfo;
104 struct dst_entry *dst;
106 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
108 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
111 afinfo = xfrm_policy_get_afinfo(family);
112 if (unlikely(afinfo == NULL))
113 return ERR_PTR(-EAFNOSUPPORT);
115 dst = afinfo->dst_lookup(tos, saddr, daddr);
116 xfrm_policy_put_afinfo(afinfo);
120 static inline unsigned long make_jiffies(long secs)
122 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
123 return MAX_SCHEDULE_TIMEOUT-1;
128 static void xfrm_policy_timer(unsigned long data)
130 struct xfrm_policy *xp = (struct xfrm_policy*)data;
131 unsigned long now = get_seconds();
132 long next = LONG_MAX;
136 read_lock(&xp->lock);
141 dir = xfrm_policy_id2dir(xp->index);
143 if (xp->lft.hard_add_expires_seconds) {
144 long tmo = xp->lft.hard_add_expires_seconds +
145 xp->curlft.add_time - now;
151 if (xp->lft.hard_use_expires_seconds) {
152 long tmo = xp->lft.hard_use_expires_seconds +
153 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
159 if (xp->lft.soft_add_expires_seconds) {
160 long tmo = xp->lft.soft_add_expires_seconds +
161 xp->curlft.add_time - now;
164 tmo = XFRM_KM_TIMEOUT;
169 if (xp->lft.soft_use_expires_seconds) {
170 long tmo = xp->lft.soft_use_expires_seconds +
171 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
174 tmo = XFRM_KM_TIMEOUT;
181 km_policy_expired(xp, dir, 0, 0);
182 if (next != LONG_MAX &&
183 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
187 read_unlock(&xp->lock);
192 read_unlock(&xp->lock);
193 if (!xfrm_policy_delete(xp, dir))
194 km_policy_expired(xp, dir, 1, 0);
199 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
203 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
205 struct xfrm_policy *policy;
207 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
210 INIT_HLIST_NODE(&policy->bydst);
211 INIT_HLIST_NODE(&policy->byidx);
212 rwlock_init(&policy->lock);
213 atomic_set(&policy->refcnt, 1);
214 setup_timer(&policy->timer, xfrm_policy_timer,
215 (unsigned long)policy);
219 EXPORT_SYMBOL(xfrm_policy_alloc);
221 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
223 void __xfrm_policy_destroy(struct xfrm_policy *policy)
225 BUG_ON(!policy->dead);
227 BUG_ON(policy->bundles);
229 if (del_timer(&policy->timer))
232 security_xfrm_policy_free(policy);
235 EXPORT_SYMBOL(__xfrm_policy_destroy);
237 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
239 struct dst_entry *dst;
241 while ((dst = policy->bundles) != NULL) {
242 policy->bundles = dst->next;
246 if (del_timer(&policy->timer))
247 atomic_dec(&policy->refcnt);
249 if (atomic_read(&policy->refcnt) > 1)
252 xfrm_pol_put(policy);
255 static void xfrm_policy_gc_task(struct work_struct *work)
257 struct xfrm_policy *policy;
258 struct hlist_node *entry, *tmp;
259 struct hlist_head gc_list;
261 spin_lock_bh(&xfrm_policy_gc_lock);
262 gc_list.first = xfrm_policy_gc_list.first;
263 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
264 spin_unlock_bh(&xfrm_policy_gc_lock);
266 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
267 xfrm_policy_gc_kill(policy);
270 /* Rule must be locked. Release descentant resources, announce
271 * entry dead. The rule must be unlinked from lists to the moment.
274 static void xfrm_policy_kill(struct xfrm_policy *policy)
278 write_lock_bh(&policy->lock);
281 write_unlock_bh(&policy->lock);
283 if (unlikely(dead)) {
288 spin_lock(&xfrm_policy_gc_lock);
289 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
290 spin_unlock(&xfrm_policy_gc_lock);
292 schedule_work(&xfrm_policy_gc_work);
295 struct xfrm_policy_hash {
296 struct hlist_head *table;
300 static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
301 static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
302 static struct hlist_head *xfrm_policy_byidx __read_mostly;
303 static unsigned int xfrm_idx_hmask __read_mostly;
304 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
306 static inline unsigned int idx_hash(u32 index)
308 return __idx_hash(index, xfrm_idx_hmask);
311 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
313 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
314 unsigned int hash = __sel_hash(sel, family, hmask);
316 return (hash == hmask + 1 ?
317 &xfrm_policy_inexact[dir] :
318 xfrm_policy_bydst[dir].table + hash);
321 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
323 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
324 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
326 return xfrm_policy_bydst[dir].table + hash;
329 static void xfrm_dst_hash_transfer(struct hlist_head *list,
330 struct hlist_head *ndsttable,
331 unsigned int nhashmask)
333 struct hlist_node *entry, *tmp;
334 struct xfrm_policy *pol;
336 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
339 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
340 pol->family, nhashmask);
341 hlist_add_head(&pol->bydst, ndsttable+h);
345 static void xfrm_idx_hash_transfer(struct hlist_head *list,
346 struct hlist_head *nidxtable,
347 unsigned int nhashmask)
349 struct hlist_node *entry, *tmp;
350 struct xfrm_policy *pol;
352 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
355 h = __idx_hash(pol->index, nhashmask);
356 hlist_add_head(&pol->byidx, nidxtable+h);
360 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
362 return ((old_hmask + 1) << 1) - 1;
365 static void xfrm_bydst_resize(int dir)
367 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
368 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
369 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
370 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
371 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
377 write_lock_bh(&xfrm_policy_lock);
379 for (i = hmask; i >= 0; i--)
380 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
382 xfrm_policy_bydst[dir].table = ndst;
383 xfrm_policy_bydst[dir].hmask = nhashmask;
385 write_unlock_bh(&xfrm_policy_lock);
387 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
390 static void xfrm_byidx_resize(int total)
392 unsigned int hmask = xfrm_idx_hmask;
393 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
394 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
395 struct hlist_head *oidx = xfrm_policy_byidx;
396 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
402 write_lock_bh(&xfrm_policy_lock);
404 for (i = hmask; i >= 0; i--)
405 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
407 xfrm_policy_byidx = nidx;
408 xfrm_idx_hmask = nhashmask;
410 write_unlock_bh(&xfrm_policy_lock);
412 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
415 static inline int xfrm_bydst_should_resize(int dir, int *total)
417 unsigned int cnt = xfrm_policy_count[dir];
418 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
423 if ((hmask + 1) < xfrm_policy_hashmax &&
430 static inline int xfrm_byidx_should_resize(int total)
432 unsigned int hmask = xfrm_idx_hmask;
434 if ((hmask + 1) < xfrm_policy_hashmax &&
441 void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
443 read_lock_bh(&xfrm_policy_lock);
444 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
445 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
446 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
447 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
448 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
449 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
450 si->spdhcnt = xfrm_idx_hmask;
451 si->spdhmcnt = xfrm_policy_hashmax;
452 read_unlock_bh(&xfrm_policy_lock);
454 EXPORT_SYMBOL(xfrm_spd_getinfo);
456 static DEFINE_MUTEX(hash_resize_mutex);
457 static void xfrm_hash_resize(struct work_struct *__unused)
461 mutex_lock(&hash_resize_mutex);
464 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
465 if (xfrm_bydst_should_resize(dir, &total))
466 xfrm_bydst_resize(dir);
468 if (xfrm_byidx_should_resize(total))
469 xfrm_byidx_resize(total);
471 mutex_unlock(&hash_resize_mutex);
474 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
476 /* Generate new index... KAME seems to generate them ordered by cost
477 * of an absolute inpredictability of ordering of rules. This will not pass. */
478 static u32 xfrm_gen_index(u8 type, int dir)
480 static u32 idx_generator;
483 struct hlist_node *entry;
484 struct hlist_head *list;
485 struct xfrm_policy *p;
489 idx = (idx_generator | dir);
493 list = xfrm_policy_byidx + idx_hash(idx);
495 hlist_for_each_entry(p, entry, list, byidx) {
496 if (p->index == idx) {
506 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
508 u32 *p1 = (u32 *) s1;
509 u32 *p2 = (u32 *) s2;
510 int len = sizeof(struct xfrm_selector) / sizeof(u32);
513 for (i = 0; i < len; i++) {
521 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
523 struct xfrm_policy *pol;
524 struct xfrm_policy *delpol;
525 struct hlist_head *chain;
526 struct hlist_node *entry, *newpos;
527 struct dst_entry *gc_list;
529 write_lock_bh(&xfrm_policy_lock);
530 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
533 hlist_for_each_entry(pol, entry, chain, bydst) {
534 if (pol->type == policy->type &&
535 !selector_cmp(&pol->selector, &policy->selector) &&
536 xfrm_sec_ctx_match(pol->security, policy->security) &&
539 write_unlock_bh(&xfrm_policy_lock);
543 if (policy->priority > pol->priority)
545 } else if (policy->priority >= pol->priority) {
546 newpos = &pol->bydst;
553 hlist_add_after(newpos, &policy->bydst);
555 hlist_add_head(&policy->bydst, chain);
556 xfrm_pol_hold(policy);
557 xfrm_policy_count[dir]++;
558 atomic_inc(&flow_cache_genid);
560 hlist_del(&delpol->bydst);
561 hlist_del(&delpol->byidx);
562 xfrm_policy_count[dir]--;
564 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
565 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
566 policy->curlft.add_time = get_seconds();
567 policy->curlft.use_time = 0;
568 if (!mod_timer(&policy->timer, jiffies + HZ))
569 xfrm_pol_hold(policy);
570 write_unlock_bh(&xfrm_policy_lock);
573 xfrm_policy_kill(delpol);
574 else if (xfrm_bydst_should_resize(dir, NULL))
575 schedule_work(&xfrm_hash_work);
577 read_lock_bh(&xfrm_policy_lock);
579 entry = &policy->bydst;
580 hlist_for_each_entry_continue(policy, entry, bydst) {
581 struct dst_entry *dst;
583 write_lock(&policy->lock);
584 dst = policy->bundles;
586 struct dst_entry *tail = dst;
589 tail->next = gc_list;
592 policy->bundles = NULL;
594 write_unlock(&policy->lock);
596 read_unlock_bh(&xfrm_policy_lock);
599 struct dst_entry *dst = gc_list;
607 EXPORT_SYMBOL(xfrm_policy_insert);
609 struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
610 struct xfrm_selector *sel,
611 struct xfrm_sec_ctx *ctx, int delete,
614 struct xfrm_policy *pol, *ret;
615 struct hlist_head *chain;
616 struct hlist_node *entry;
619 write_lock_bh(&xfrm_policy_lock);
620 chain = policy_hash_bysel(sel, sel->family, dir);
622 hlist_for_each_entry(pol, entry, chain, bydst) {
623 if (pol->type == type &&
624 !selector_cmp(sel, &pol->selector) &&
625 xfrm_sec_ctx_match(ctx, pol->security)) {
628 *err = security_xfrm_policy_delete(pol);
630 write_unlock_bh(&xfrm_policy_lock);
633 hlist_del(&pol->bydst);
634 hlist_del(&pol->byidx);
635 xfrm_policy_count[dir]--;
641 write_unlock_bh(&xfrm_policy_lock);
644 atomic_inc(&flow_cache_genid);
645 xfrm_policy_kill(ret);
649 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
651 struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
654 struct xfrm_policy *pol, *ret;
655 struct hlist_head *chain;
656 struct hlist_node *entry;
659 if (xfrm_policy_id2dir(id) != dir)
663 write_lock_bh(&xfrm_policy_lock);
664 chain = xfrm_policy_byidx + idx_hash(id);
666 hlist_for_each_entry(pol, entry, chain, byidx) {
667 if (pol->type == type && pol->index == id) {
670 *err = security_xfrm_policy_delete(pol);
672 write_unlock_bh(&xfrm_policy_lock);
675 hlist_del(&pol->bydst);
676 hlist_del(&pol->byidx);
677 xfrm_policy_count[dir]--;
683 write_unlock_bh(&xfrm_policy_lock);
686 atomic_inc(&flow_cache_genid);
687 xfrm_policy_kill(ret);
691 EXPORT_SYMBOL(xfrm_policy_byid);
693 #ifdef CONFIG_SECURITY_NETWORK_XFRM
695 xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
699 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
700 struct xfrm_policy *pol;
701 struct hlist_node *entry;
704 hlist_for_each_entry(pol, entry,
705 &xfrm_policy_inexact[dir], bydst) {
706 if (pol->type != type)
708 err = security_xfrm_policy_delete(pol);
710 xfrm_audit_policy_delete(pol, 0,
711 audit_info->loginuid,
716 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
717 hlist_for_each_entry(pol, entry,
718 xfrm_policy_bydst[dir].table + i,
720 if (pol->type != type)
722 err = security_xfrm_policy_delete(pol);
724 xfrm_audit_policy_delete(pol, 0,
725 audit_info->loginuid,
736 xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
742 int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
746 write_lock_bh(&xfrm_policy_lock);
748 err = xfrm_policy_flush_secctx_check(type, audit_info);
752 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
753 struct xfrm_policy *pol;
754 struct hlist_node *entry;
759 hlist_for_each_entry(pol, entry,
760 &xfrm_policy_inexact[dir], bydst) {
761 if (pol->type != type)
763 hlist_del(&pol->bydst);
764 hlist_del(&pol->byidx);
765 write_unlock_bh(&xfrm_policy_lock);
767 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
770 xfrm_policy_kill(pol);
773 write_lock_bh(&xfrm_policy_lock);
777 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
779 hlist_for_each_entry(pol, entry,
780 xfrm_policy_bydst[dir].table + i,
782 if (pol->type != type)
784 hlist_del(&pol->bydst);
785 hlist_del(&pol->byidx);
786 write_unlock_bh(&xfrm_policy_lock);
788 xfrm_audit_policy_delete(pol, 1,
789 audit_info->loginuid,
791 xfrm_policy_kill(pol);
794 write_lock_bh(&xfrm_policy_lock);
799 xfrm_policy_count[dir] -= killed;
801 atomic_inc(&flow_cache_genid);
803 write_unlock_bh(&xfrm_policy_lock);
806 EXPORT_SYMBOL(xfrm_policy_flush);
808 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
811 struct xfrm_policy *pol, *last = NULL;
812 struct hlist_node *entry;
813 int dir, last_dir = 0, count, error;
815 read_lock_bh(&xfrm_policy_lock);
818 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
819 struct hlist_head *table = xfrm_policy_bydst[dir].table;
822 hlist_for_each_entry(pol, entry,
823 &xfrm_policy_inexact[dir], bydst) {
824 if (pol->type != type)
827 error = func(last, last_dir % XFRM_POLICY_MAX,
836 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
837 hlist_for_each_entry(pol, entry, table + i, bydst) {
838 if (pol->type != type)
841 error = func(last, last_dir % XFRM_POLICY_MAX,
856 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
858 read_unlock_bh(&xfrm_policy_lock);
861 EXPORT_SYMBOL(xfrm_policy_walk);
864 * Find policy to apply to this flow.
866 * Returns 0 if policy found, else an -errno.
868 static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
869 u8 type, u16 family, int dir)
871 struct xfrm_selector *sel = &pol->selector;
872 int match, ret = -ESRCH;
874 if (pol->family != family ||
878 match = xfrm_selector_match(sel, fl, family);
880 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
885 static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
889 struct xfrm_policy *pol, *ret;
890 xfrm_address_t *daddr, *saddr;
891 struct hlist_node *entry;
892 struct hlist_head *chain;
895 daddr = xfrm_flowi_daddr(fl, family);
896 saddr = xfrm_flowi_saddr(fl, family);
897 if (unlikely(!daddr || !saddr))
900 read_lock_bh(&xfrm_policy_lock);
901 chain = policy_hash_direct(daddr, saddr, family, dir);
903 hlist_for_each_entry(pol, entry, chain, bydst) {
904 err = xfrm_policy_match(pol, fl, type, family, dir);
914 priority = ret->priority;
918 chain = &xfrm_policy_inexact[dir];
919 hlist_for_each_entry(pol, entry, chain, bydst) {
920 err = xfrm_policy_match(pol, fl, type, family, dir);
928 } else if (pol->priority < priority) {
936 read_unlock_bh(&xfrm_policy_lock);
941 static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
942 void **objp, atomic_t **obj_refp)
944 struct xfrm_policy *pol;
947 #ifdef CONFIG_XFRM_SUB_POLICY
948 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
956 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
961 #ifdef CONFIG_XFRM_SUB_POLICY
964 if ((*objp = (void *) pol) != NULL)
965 *obj_refp = &pol->refcnt;
969 static inline int policy_to_flow_dir(int dir)
971 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
972 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
973 XFRM_POLICY_FWD == FLOW_DIR_FWD)
979 case XFRM_POLICY_OUT:
981 case XFRM_POLICY_FWD:
986 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
988 struct xfrm_policy *pol;
990 read_lock_bh(&xfrm_policy_lock);
991 if ((pol = sk->sk_policy[dir]) != NULL) {
992 int match = xfrm_selector_match(&pol->selector, fl,
997 err = security_xfrm_policy_lookup(pol, fl->secid,
998 policy_to_flow_dir(dir));
1001 else if (err == -ESRCH)
1008 read_unlock_bh(&xfrm_policy_lock);
1012 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1014 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1017 hlist_add_head(&pol->bydst, chain);
1018 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1019 xfrm_policy_count[dir]++;
1022 if (xfrm_bydst_should_resize(dir, NULL))
1023 schedule_work(&xfrm_hash_work);
1026 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1029 if (hlist_unhashed(&pol->bydst))
1032 hlist_del(&pol->bydst);
1033 hlist_del(&pol->byidx);
1034 xfrm_policy_count[dir]--;
1039 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1041 write_lock_bh(&xfrm_policy_lock);
1042 pol = __xfrm_policy_unlink(pol, dir);
1043 write_unlock_bh(&xfrm_policy_lock);
1045 if (dir < XFRM_POLICY_MAX)
1046 atomic_inc(&flow_cache_genid);
1047 xfrm_policy_kill(pol);
1052 EXPORT_SYMBOL(xfrm_policy_delete);
1054 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1056 struct xfrm_policy *old_pol;
1058 #ifdef CONFIG_XFRM_SUB_POLICY
1059 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1063 write_lock_bh(&xfrm_policy_lock);
1064 old_pol = sk->sk_policy[dir];
1065 sk->sk_policy[dir] = pol;
1067 pol->curlft.add_time = get_seconds();
1068 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1069 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1072 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1073 write_unlock_bh(&xfrm_policy_lock);
1076 xfrm_policy_kill(old_pol);
1081 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1083 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1086 newp->selector = old->selector;
1087 if (security_xfrm_policy_clone(old, newp)) {
1089 return NULL; /* ENOMEM */
1091 newp->lft = old->lft;
1092 newp->curlft = old->curlft;
1093 newp->action = old->action;
1094 newp->flags = old->flags;
1095 newp->xfrm_nr = old->xfrm_nr;
1096 newp->index = old->index;
1097 newp->type = old->type;
1098 memcpy(newp->xfrm_vec, old->xfrm_vec,
1099 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1100 write_lock_bh(&xfrm_policy_lock);
1101 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1102 write_unlock_bh(&xfrm_policy_lock);
1108 int __xfrm_sk_clone_policy(struct sock *sk)
1110 struct xfrm_policy *p0 = sk->sk_policy[0],
1111 *p1 = sk->sk_policy[1];
1113 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1114 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1116 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1122 xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1123 unsigned short family)
1126 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1128 if (unlikely(afinfo == NULL))
1130 err = afinfo->get_saddr(local, remote);
1131 xfrm_policy_put_afinfo(afinfo);
1135 /* Resolve list of templates for the flow, given policy. */
1138 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1139 struct xfrm_state **xfrm,
1140 unsigned short family)
1144 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1145 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
1148 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1149 struct xfrm_state *x;
1150 xfrm_address_t *remote = daddr;
1151 xfrm_address_t *local = saddr;
1152 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1154 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1155 tmpl->mode == XFRM_MODE_BEET) {
1156 remote = &tmpl->id.daddr;
1157 local = &tmpl->saddr;
1158 family = tmpl->encap_family;
1159 if (xfrm_addr_any(local, family)) {
1160 error = xfrm_get_saddr(&tmp, remote, family);
1167 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1169 if (x && x->km.state == XFRM_STATE_VALID) {
1176 error = (x->km.state == XFRM_STATE_ERROR ?
1181 if (!tmpl->optional)
1187 for (nx--; nx>=0; nx--)
1188 xfrm_state_put(xfrm[nx]);
1193 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1194 struct xfrm_state **xfrm,
1195 unsigned short family)
1197 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1198 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1204 for (i = 0; i < npols; i++) {
1205 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1210 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1218 /* found states are sorted for outbound processing */
1220 xfrm_state_sort(xfrm, tpp, cnx, family);
1225 for (cnx--; cnx>=0; cnx--)
1226 xfrm_state_put(tpp[cnx]);
1231 /* Check that the bundle accepts the flow and its components are
1235 static struct dst_entry *
1236 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1238 struct dst_entry *x;
1239 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1240 if (unlikely(afinfo == NULL))
1241 return ERR_PTR(-EINVAL);
1242 x = afinfo->find_bundle(fl, policy);
1243 xfrm_policy_put_afinfo(afinfo);
1247 static inline int xfrm_get_tos(struct flowi *fl, int family)
1249 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1255 tos = afinfo->get_tos(fl);
1257 xfrm_policy_put_afinfo(afinfo);
1262 static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1264 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1265 struct xfrm_dst *xdst;
1268 return ERR_PTR(-EINVAL);
1270 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1272 xfrm_policy_put_afinfo(afinfo);
1277 static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1280 struct xfrm_policy_afinfo *afinfo =
1281 xfrm_policy_get_afinfo(dst->ops->family);
1287 err = afinfo->init_path(path, dst, nfheader_len);
1289 xfrm_policy_put_afinfo(afinfo);
1294 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1296 struct xfrm_policy_afinfo *afinfo =
1297 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1303 err = afinfo->fill_dst(xdst, dev);
1305 xfrm_policy_put_afinfo(afinfo);
1310 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1311 * all the metrics... Shortly, bundle a bundle.
1314 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1315 struct xfrm_state **xfrm, int nx,
1317 struct dst_entry *dst)
1319 unsigned long now = jiffies;
1320 struct net_device *dev;
1321 struct dst_entry *dst_prev = NULL;
1322 struct dst_entry *dst0 = NULL;
1326 int nfheader_len = 0;
1327 int trailer_len = 0;
1329 int family = policy->selector.family;
1331 tos = xfrm_get_tos(fl, family);
1338 for (; i < nx; i++) {
1339 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1340 struct dst_entry *dst1 = &xdst->u.dst;
1342 err = PTR_ERR(xdst);
1351 dst_prev->child = dst_clone(dst1);
1352 dst1->flags |= DST_NOHASH;
1356 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1358 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1359 family = xfrm[i]->props.family;
1360 dst = xfrm_dst_lookup(xfrm[i], tos, family);
1367 dst1->xfrm = xfrm[i];
1368 xdst->genid = xfrm[i]->genid;
1370 dst1->obsolete = -1;
1371 dst1->flags |= DST_HOST;
1372 dst1->lastuse = now;
1374 dst1->input = dst_discard;
1375 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1377 dst1->next = dst_prev;
1380 header_len += xfrm[i]->props.header_len;
1381 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1382 nfheader_len += xfrm[i]->props.header_len;
1383 trailer_len += xfrm[i]->props.trailer_len;
1386 dst_prev->child = dst;
1394 /* Copy neighbout for reachability confirmation */
1395 dst0->neighbour = neigh_clone(dst->neighbour);
1397 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
1398 xfrm_init_pmtu(dst_prev);
1400 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1401 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1403 err = xfrm_fill_dst(xdst, dev);
1407 dst_prev->header_len = header_len;
1408 dst_prev->trailer_len = trailer_len;
1409 header_len -= xdst->u.dst.xfrm->props.header_len;
1410 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1418 xfrm_state_put(xfrm[i]);
1422 dst0 = ERR_PTR(err);
1427 xfrm_dst_alloc_copy(void **target, void *src, int size)
1430 *target = kmalloc(size, GFP_ATOMIC);
1434 memcpy(*target, src, size);
1439 xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1441 #ifdef CONFIG_XFRM_SUB_POLICY
1442 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1443 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1451 xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1453 #ifdef CONFIG_XFRM_SUB_POLICY
1454 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1455 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1461 static int stale_bundle(struct dst_entry *dst);
1463 /* Main function: finds/creates a bundle for given flow.
1465 * At the moment we eat a raw IP route. Mostly to speed up lookups
1466 * on interfaces with disabled IPsec.
1468 int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1469 struct sock *sk, int flags)
1471 struct xfrm_policy *policy;
1472 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1477 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1478 struct dst_entry *dst, *dst_orig = *dst_p;
1483 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
1486 genid = atomic_read(&flow_cache_genid);
1488 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1494 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
1495 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1496 err = PTR_ERR(policy);
1502 /* To accelerate a bit... */
1503 if ((dst_orig->flags & DST_NOXFRM) ||
1504 !xfrm_policy_count[XFRM_POLICY_OUT])
1507 policy = flow_cache_lookup(fl, dst_orig->ops->family,
1508 dir, xfrm_policy_lookup);
1509 err = PTR_ERR(policy);
1517 family = dst_orig->ops->family;
1520 xfrm_nr += pols[0]->xfrm_nr;
1523 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1526 policy->curlft.use_time = get_seconds();
1528 switch (policy->action) {
1530 case XFRM_POLICY_BLOCK:
1531 /* Prohibit the flow */
1535 case XFRM_POLICY_ALLOW:
1536 #ifndef CONFIG_XFRM_SUB_POLICY
1537 if (policy->xfrm_nr == 0) {
1538 /* Flow passes not transformed. */
1539 xfrm_pol_put(policy);
1544 /* Try to find matching bundle.
1546 * LATER: help from flow cache. It is optional, this
1547 * is required only for output policy.
1549 dst = xfrm_find_bundle(fl, policy, family);
1558 #ifdef CONFIG_XFRM_SUB_POLICY
1559 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1560 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1564 if (IS_ERR(pols[1])) {
1565 err = PTR_ERR(pols[1]);
1568 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1573 xfrm_nr += pols[1]->xfrm_nr;
1578 * Because neither flowi nor bundle information knows about
1579 * transformation template size. On more than one policy usage
1580 * we can realize whether all of them is bypass or not after
1581 * they are searched. See above not-transformed bypass
1582 * is surrounded by non-sub policy configuration, too.
1585 /* Flow passes not transformed. */
1586 xfrm_pols_put(pols, npols);
1591 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1593 if (unlikely(nx<0)) {
1595 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1596 /* EREMOTE tells the caller to generate
1597 * a one-shot blackhole route.
1599 xfrm_pol_put(policy);
1602 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
1603 DECLARE_WAITQUEUE(wait, current);
1605 add_wait_queue(&km_waitq, &wait);
1606 set_current_state(TASK_INTERRUPTIBLE);
1608 set_current_state(TASK_RUNNING);
1609 remove_wait_queue(&km_waitq, &wait);
1611 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1613 if (nx == -EAGAIN && signal_pending(current)) {
1617 if (nx == -EAGAIN ||
1618 genid != atomic_read(&flow_cache_genid)) {
1619 xfrm_pols_put(pols, npols);
1628 /* Flow passes not transformed. */
1629 xfrm_pols_put(pols, npols);
1633 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1638 for (pi = 0; pi < npols; pi++) {
1639 read_lock_bh(&pols[pi]->lock);
1640 pol_dead |= pols[pi]->dead;
1641 read_unlock_bh(&pols[pi]->lock);
1644 write_lock_bh(&policy->lock);
1645 if (unlikely(pol_dead || stale_bundle(dst))) {
1646 /* Wow! While we worked on resolving, this
1647 * policy has gone. Retry. It is not paranoia,
1648 * we just cannot enlist new bundle to dead object.
1649 * We can't enlist stable bundles either.
1651 write_unlock_bh(&policy->lock);
1655 err = -EHOSTUNREACH;
1660 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1662 err = xfrm_dst_update_origin(dst, fl);
1663 if (unlikely(err)) {
1664 write_unlock_bh(&policy->lock);
1670 dst->next = policy->bundles;
1671 policy->bundles = dst;
1673 write_unlock_bh(&policy->lock);
1676 dst_release(dst_orig);
1677 xfrm_pols_put(pols, npols);
1681 xfrm_pols_put(pols, npols);
1683 dst_release(dst_orig);
1689 if (flags & XFRM_LOOKUP_ICMP)
1693 EXPORT_SYMBOL(__xfrm_lookup);
1695 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1696 struct sock *sk, int flags)
1698 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1700 if (err == -EREMOTE) {
1701 dst_release(*dst_p);
1708 EXPORT_SYMBOL(xfrm_lookup);
1711 xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1713 struct xfrm_state *x;
1715 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1717 x = skb->sp->xvec[idx];
1718 if (!x->type->reject)
1720 return x->type->reject(x, skb, fl);
1723 /* When skb is transformed back to its "native" form, we have to
1724 * check policy restrictions. At the moment we make this in maximally
1725 * stupid way. Shame on me. :-) Of course, connected sockets must
1726 * have policy cached at them.
1730 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1731 unsigned short family)
1733 if (xfrm_state_kern(x))
1734 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
1735 return x->id.proto == tmpl->id.proto &&
1736 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1737 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1738 x->props.mode == tmpl->mode &&
1739 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1740 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
1741 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1742 xfrm_state_addr_cmp(tmpl, x, family));
1746 * 0 or more than 0 is returned when validation is succeeded (either bypass
1747 * because of optional transport mode, or next index of the mathced secpath
1748 * state with the template.
1749 * -1 is returned when no matching template is found.
1750 * Otherwise "-2 - errored_index" is returned.
1753 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1754 unsigned short family)
1758 if (tmpl->optional) {
1759 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1763 for (; idx < sp->len; idx++) {
1764 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1766 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1775 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1776 unsigned int family, int reverse)
1778 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1781 if (unlikely(afinfo == NULL))
1782 return -EAFNOSUPPORT;
1784 afinfo->decode_session(skb, fl, reverse);
1785 err = security_xfrm_decode_session(skb, &fl->secid);
1786 xfrm_policy_put_afinfo(afinfo);
1789 EXPORT_SYMBOL(__xfrm_decode_session);
1791 static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1793 for (; k < sp->len; k++) {
1794 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
1803 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1804 unsigned short family)
1806 struct xfrm_policy *pol;
1807 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1816 reverse = dir & ~XFRM_POLICY_MASK;
1817 dir &= XFRM_POLICY_MASK;
1818 fl_dir = policy_to_flow_dir(dir);
1820 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0)
1822 nf_nat_decode_session(skb, &fl, family);
1824 /* First, check used SA against their selectors. */
1828 for (i=skb->sp->len-1; i>=0; i--) {
1829 struct xfrm_state *x = skb->sp->xvec[i];
1830 if (!xfrm_selector_match(&x->sel, &fl, family))
1836 if (sk && sk->sk_policy[dir]) {
1837 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
1843 pol = flow_cache_lookup(&fl, family, fl_dir,
1844 xfrm_policy_lookup);
1850 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
1851 xfrm_secpath_reject(xerr_idx, skb, &fl);
1857 pol->curlft.use_time = get_seconds();
1861 #ifdef CONFIG_XFRM_SUB_POLICY
1862 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1863 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1867 if (IS_ERR(pols[1]))
1869 pols[1]->curlft.use_time = get_seconds();
1875 if (pol->action == XFRM_POLICY_ALLOW) {
1876 struct sec_path *sp;
1877 static struct sec_path dummy;
1878 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
1879 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
1880 struct xfrm_tmpl **tpp = tp;
1884 if ((sp = skb->sp) == NULL)
1887 for (pi = 0; pi < npols; pi++) {
1888 if (pols[pi] != pol &&
1889 pols[pi]->action != XFRM_POLICY_ALLOW)
1891 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1893 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1894 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1898 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1902 /* For each tunnel xfrm, find the first matching tmpl.
1903 * For each tmpl before that, find corresponding xfrm.
1904 * Order is _important_. Later we will implement
1905 * some barriers, but at the moment barriers
1906 * are implied between each two transformations.
1908 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1909 k = xfrm_policy_ok(tpp[i], sp, k, family);
1912 /* "-2 - errored_index" returned */
1918 if (secpath_has_nontransport(sp, k, &xerr_idx))
1921 xfrm_pols_put(pols, npols);
1926 xfrm_secpath_reject(xerr_idx, skb, &fl);
1928 xfrm_pols_put(pols, npols);
1931 EXPORT_SYMBOL(__xfrm_policy_check);
1933 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1937 if (xfrm_decode_session(skb, &fl, family) < 0)
1940 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1942 EXPORT_SYMBOL(__xfrm_route_forward);
1944 /* Optimize later using cookies and generation ids. */
1946 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1948 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1949 * to "-1" to force all XFRM destinations to get validated by
1950 * dst_ops->check on every use. We do this because when a
1951 * normal route referenced by an XFRM dst is obsoleted we do
1952 * not go looking around for all parent referencing XFRM dsts
1953 * so that we can invalidate them. It is just too much work.
1954 * Instead we make the checks here on every use. For example:
1956 * XFRM dst A --> IPv4 dst X
1958 * X is the "xdst->route" of A (X is also the "dst->path" of A
1959 * in this example). If X is marked obsolete, "A" will not
1960 * notice. That's what we are validating here via the
1961 * stale_bundle() check.
1963 * When a policy's bundle is pruned, we dst_free() the XFRM
1964 * dst which causes it's ->obsolete field to be set to a
1965 * positive non-zero integer. If an XFRM dst has been pruned
1966 * like this, we want to force a new route lookup.
1968 if (dst->obsolete < 0 && !stale_bundle(dst))
1974 static int stale_bundle(struct dst_entry *dst)
1976 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1979 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1981 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1982 dst->dev = dev->nd_net->loopback_dev;
1987 EXPORT_SYMBOL(xfrm_dst_ifdown);
1989 static void xfrm_link_failure(struct sk_buff *skb)
1991 /* Impossible. Such dst must be popped before reaches point of failure. */
1995 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1998 if (dst->obsolete) {
2006 static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2008 struct dst_entry *dst, **dstp;
2010 write_lock(&pol->lock);
2011 dstp = &pol->bundles;
2012 while ((dst=*dstp) != NULL) {
2015 dst->next = *gc_list_p;
2021 write_unlock(&pol->lock);
2024 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
2026 struct dst_entry *gc_list = NULL;
2029 read_lock_bh(&xfrm_policy_lock);
2030 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2031 struct xfrm_policy *pol;
2032 struct hlist_node *entry;
2033 struct hlist_head *table;
2036 hlist_for_each_entry(pol, entry,
2037 &xfrm_policy_inexact[dir], bydst)
2038 prune_one_bundle(pol, func, &gc_list);
2040 table = xfrm_policy_bydst[dir].table;
2041 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2042 hlist_for_each_entry(pol, entry, table + i, bydst)
2043 prune_one_bundle(pol, func, &gc_list);
2046 read_unlock_bh(&xfrm_policy_lock);
2049 struct dst_entry *dst = gc_list;
2050 gc_list = dst->next;
2055 static int unused_bundle(struct dst_entry *dst)
2057 return !atomic_read(&dst->__refcnt);
2060 static void __xfrm_garbage_collect(void)
2062 xfrm_prune_bundles(unused_bundle);
2065 static int xfrm_flush_bundles(void)
2067 xfrm_prune_bundles(stale_bundle);
2071 static void xfrm_init_pmtu(struct dst_entry *dst)
2074 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2075 u32 pmtu, route_mtu_cached;
2077 pmtu = dst_mtu(dst->child);
2078 xdst->child_mtu_cached = pmtu;
2080 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2082 route_mtu_cached = dst_mtu(xdst->route);
2083 xdst->route_mtu_cached = route_mtu_cached;
2085 if (pmtu > route_mtu_cached)
2086 pmtu = route_mtu_cached;
2088 dst->metrics[RTAX_MTU-1] = pmtu;
2089 } while ((dst = dst->next));
2092 /* Check that the bundle accepts the flow and its components are
2096 int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2097 struct flowi *fl, int family, int strict)
2099 struct dst_entry *dst = &first->u.dst;
2100 struct xfrm_dst *last;
2103 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2104 (dst->dev && !netif_running(dst->dev)))
2106 #ifdef CONFIG_XFRM_SUB_POLICY
2108 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2110 if (first->partner &&
2111 !xfrm_selector_match(first->partner, fl, family))
2119 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2121 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2124 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
2126 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2128 if (xdst->genid != dst->xfrm->genid)
2132 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2133 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2136 mtu = dst_mtu(dst->child);
2137 if (xdst->child_mtu_cached != mtu) {
2139 xdst->child_mtu_cached = mtu;
2142 if (!dst_check(xdst->route, xdst->route_cookie))
2144 mtu = dst_mtu(xdst->route);
2145 if (xdst->route_mtu_cached != mtu) {
2147 xdst->route_mtu_cached = mtu;
2151 } while (dst->xfrm);
2156 mtu = last->child_mtu_cached;
2160 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2161 if (mtu > last->route_mtu_cached)
2162 mtu = last->route_mtu_cached;
2163 dst->metrics[RTAX_MTU-1] = mtu;
2168 last = (struct xfrm_dst *)last->u.dst.next;
2169 last->child_mtu_cached = mtu;
2175 EXPORT_SYMBOL(xfrm_bundle_ok);
2177 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2180 if (unlikely(afinfo == NULL))
2182 if (unlikely(afinfo->family >= NPROTO))
2183 return -EAFNOSUPPORT;
2184 write_lock_bh(&xfrm_policy_afinfo_lock);
2185 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2188 struct dst_ops *dst_ops = afinfo->dst_ops;
2189 if (likely(dst_ops->kmem_cachep == NULL))
2190 dst_ops->kmem_cachep = xfrm_dst_cache;
2191 if (likely(dst_ops->check == NULL))
2192 dst_ops->check = xfrm_dst_check;
2193 if (likely(dst_ops->negative_advice == NULL))
2194 dst_ops->negative_advice = xfrm_negative_advice;
2195 if (likely(dst_ops->link_failure == NULL))
2196 dst_ops->link_failure = xfrm_link_failure;
2197 if (likely(afinfo->garbage_collect == NULL))
2198 afinfo->garbage_collect = __xfrm_garbage_collect;
2199 xfrm_policy_afinfo[afinfo->family] = afinfo;
2201 write_unlock_bh(&xfrm_policy_afinfo_lock);
2204 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2206 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2209 if (unlikely(afinfo == NULL))
2211 if (unlikely(afinfo->family >= NPROTO))
2212 return -EAFNOSUPPORT;
2213 write_lock_bh(&xfrm_policy_afinfo_lock);
2214 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2215 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2218 struct dst_ops *dst_ops = afinfo->dst_ops;
2219 xfrm_policy_afinfo[afinfo->family] = NULL;
2220 dst_ops->kmem_cachep = NULL;
2221 dst_ops->check = NULL;
2222 dst_ops->negative_advice = NULL;
2223 dst_ops->link_failure = NULL;
2224 afinfo->garbage_collect = NULL;
2227 write_unlock_bh(&xfrm_policy_afinfo_lock);
2230 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2232 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2234 struct xfrm_policy_afinfo *afinfo;
2235 if (unlikely(family >= NPROTO))
2237 read_lock(&xfrm_policy_afinfo_lock);
2238 afinfo = xfrm_policy_afinfo[family];
2239 if (unlikely(!afinfo))
2240 read_unlock(&xfrm_policy_afinfo_lock);
2244 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2246 read_unlock(&xfrm_policy_afinfo_lock);
2249 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2251 struct net_device *dev = ptr;
2253 if (dev->nd_net != &init_net)
2258 xfrm_flush_bundles();
2263 static struct notifier_block xfrm_dev_notifier = {
2269 #ifdef CONFIG_XFRM_STATISTICS
2270 static int __init xfrm_statistics_init(void)
2272 if (snmp_mib_init((void **)xfrm_statistics,
2273 sizeof(struct linux_xfrm_mib)) < 0)
2279 static void __init xfrm_policy_init(void)
2281 unsigned int hmask, sz;
2284 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2285 sizeof(struct xfrm_dst),
2286 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2290 sz = (hmask+1) * sizeof(struct hlist_head);
2292 xfrm_policy_byidx = xfrm_hash_alloc(sz);
2293 xfrm_idx_hmask = hmask;
2294 if (!xfrm_policy_byidx)
2295 panic("XFRM: failed to allocate byidx hash\n");
2297 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2298 struct xfrm_policy_hash *htab;
2300 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2302 htab = &xfrm_policy_bydst[dir];
2303 htab->table = xfrm_hash_alloc(sz);
2304 htab->hmask = hmask;
2306 panic("XFRM: failed to allocate bydst hash\n");
2309 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
2310 register_netdevice_notifier(&xfrm_dev_notifier);
2313 void __init xfrm_init(void)
2315 #ifdef CONFIG_XFRM_STATISTICS
2316 xfrm_statistics_init();
2321 #ifdef CONFIG_XFRM_STATISTICS
2326 #ifdef CONFIG_AUDITSYSCALL
2327 static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2328 struct audit_buffer *audit_buf)
2330 struct xfrm_sec_ctx *ctx = xp->security;
2331 struct xfrm_selector *sel = &xp->selector;
2334 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2335 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2337 switch(sel->family) {
2339 audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
2340 NIPQUAD(sel->saddr.a4));
2341 if (sel->prefixlen_s != 32)
2342 audit_log_format(audit_buf, " src_prefixlen=%d",
2344 audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
2345 NIPQUAD(sel->daddr.a4));
2346 if (sel->prefixlen_d != 32)
2347 audit_log_format(audit_buf, " dst_prefixlen=%d",
2351 audit_log_format(audit_buf, " src=" NIP6_FMT,
2352 NIP6(*(struct in6_addr *)sel->saddr.a6));
2353 if (sel->prefixlen_s != 128)
2354 audit_log_format(audit_buf, " src_prefixlen=%d",
2356 audit_log_format(audit_buf, " dst=" NIP6_FMT,
2357 NIP6(*(struct in6_addr *)sel->daddr.a6));
2358 if (sel->prefixlen_d != 128)
2359 audit_log_format(audit_buf, " dst_prefixlen=%d",
2366 xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2368 struct audit_buffer *audit_buf;
2369 extern int audit_enabled;
2371 if (audit_enabled == 0)
2373 audit_buf = xfrm_audit_start(auid, sid);
2374 if (audit_buf == NULL)
2376 audit_log_format(audit_buf, " op=SPD-add res=%u", result);
2377 xfrm_audit_common_policyinfo(xp, audit_buf);
2378 audit_log_end(audit_buf);
2380 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2383 xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2385 struct audit_buffer *audit_buf;
2386 extern int audit_enabled;
2388 if (audit_enabled == 0)
2390 audit_buf = xfrm_audit_start(auid, sid);
2391 if (audit_buf == NULL)
2393 audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
2394 xfrm_audit_common_policyinfo(xp, audit_buf);
2395 audit_log_end(audit_buf);
2397 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2400 #ifdef CONFIG_XFRM_MIGRATE
2401 static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2402 struct xfrm_selector *sel_tgt)
2404 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2405 if (sel_tgt->family == sel_cmp->family &&
2406 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
2407 sel_cmp->family) == 0 &&
2408 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2409 sel_cmp->family) == 0 &&
2410 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2411 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2415 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2422 static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2425 struct xfrm_policy *pol, *ret = NULL;
2426 struct hlist_node *entry;
2427 struct hlist_head *chain;
2430 read_lock_bh(&xfrm_policy_lock);
2431 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2432 hlist_for_each_entry(pol, entry, chain, bydst) {
2433 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2434 pol->type == type) {
2436 priority = ret->priority;
2440 chain = &xfrm_policy_inexact[dir];
2441 hlist_for_each_entry(pol, entry, chain, bydst) {
2442 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2443 pol->type == type &&
2444 pol->priority < priority) {
2453 read_unlock_bh(&xfrm_policy_lock);
2458 static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2462 if (t->mode == m->mode && t->id.proto == m->proto &&
2463 (m->reqid == 0 || t->reqid == m->reqid)) {
2465 case XFRM_MODE_TUNNEL:
2466 case XFRM_MODE_BEET:
2467 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2468 m->old_family) == 0 &&
2469 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2470 m->old_family) == 0) {
2474 case XFRM_MODE_TRANSPORT:
2475 /* in case of transport mode, template does not store
2476 any IP addresses, hence we just compare mode and
2487 /* update endpoint address(es) of template(s) */
2488 static int xfrm_policy_migrate(struct xfrm_policy *pol,
2489 struct xfrm_migrate *m, int num_migrate)
2491 struct xfrm_migrate *mp;
2492 struct dst_entry *dst;
2495 write_lock_bh(&pol->lock);
2496 if (unlikely(pol->dead)) {
2497 /* target policy has been deleted */
2498 write_unlock_bh(&pol->lock);
2502 for (i = 0; i < pol->xfrm_nr; i++) {
2503 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2504 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2507 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2508 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
2510 /* update endpoints */
2511 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2512 sizeof(pol->xfrm_vec[i].id.daddr));
2513 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2514 sizeof(pol->xfrm_vec[i].saddr));
2515 pol->xfrm_vec[i].encap_family = mp->new_family;
2517 while ((dst = pol->bundles) != NULL) {
2518 pol->bundles = dst->next;
2524 write_unlock_bh(&pol->lock);
2532 static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2536 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2539 for (i = 0; i < num_migrate; i++) {
2540 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2541 m[i].old_family) == 0) &&
2542 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2543 m[i].old_family) == 0))
2545 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2546 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2549 /* check if there is any duplicated entry */
2550 for (j = i + 1; j < num_migrate; j++) {
2551 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2552 sizeof(m[i].old_daddr)) &&
2553 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2554 sizeof(m[i].old_saddr)) &&
2555 m[i].proto == m[j].proto &&
2556 m[i].mode == m[j].mode &&
2557 m[i].reqid == m[j].reqid &&
2558 m[i].old_family == m[j].old_family)
2566 int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2567 struct xfrm_migrate *m, int num_migrate)
2569 int i, err, nx_cur = 0, nx_new = 0;
2570 struct xfrm_policy *pol = NULL;
2571 struct xfrm_state *x, *xc;
2572 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2573 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2574 struct xfrm_migrate *mp;
2576 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2579 /* Stage 1 - find policy */
2580 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2585 /* Stage 2 - find and update state(s) */
2586 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2587 if ((x = xfrm_migrate_state_find(mp))) {
2590 if ((xc = xfrm_state_migrate(x, mp))) {
2600 /* Stage 3 - update policy */
2601 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2604 /* Stage 4 - delete old state(s) */
2606 xfrm_states_put(x_cur, nx_cur);
2607 xfrm_states_delete(x_cur, nx_cur);
2610 /* Stage 5 - announce */
2611 km_migrate(sel, dir, type, m, num_migrate);
2623 xfrm_states_put(x_cur, nx_cur);
2625 xfrm_states_delete(x_new, nx_new);
2629 EXPORT_SYMBOL(xfrm_migrate);