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/config.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>
29 DEFINE_MUTEX(xfrm_cfg_mutex);
30 EXPORT_SYMBOL(xfrm_cfg_mutex);
32 static DEFINE_RWLOCK(xfrm_policy_lock);
34 struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
35 EXPORT_SYMBOL(xfrm_policy_list);
37 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
38 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
40 static kmem_cache_t *xfrm_dst_cache __read_mostly;
42 static struct work_struct xfrm_policy_gc_work;
43 static struct list_head xfrm_policy_gc_list =
44 LIST_HEAD_INIT(xfrm_policy_gc_list);
45 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
47 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
48 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
49 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
50 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
52 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
54 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
55 struct xfrm_type **typemap;
58 if (unlikely(afinfo == NULL))
60 typemap = afinfo->type_map;
62 if (likely(typemap[type->proto] == NULL))
63 typemap[type->proto] = type;
66 xfrm_policy_unlock_afinfo(afinfo);
69 EXPORT_SYMBOL(xfrm_register_type);
71 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
73 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
74 struct xfrm_type **typemap;
77 if (unlikely(afinfo == NULL))
79 typemap = afinfo->type_map;
81 if (unlikely(typemap[type->proto] != type))
84 typemap[type->proto] = NULL;
85 xfrm_policy_unlock_afinfo(afinfo);
88 EXPORT_SYMBOL(xfrm_unregister_type);
90 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
92 struct xfrm_policy_afinfo *afinfo;
93 struct xfrm_type **typemap;
94 struct xfrm_type *type;
95 int modload_attempted = 0;
98 afinfo = xfrm_policy_get_afinfo(family);
99 if (unlikely(afinfo == NULL))
101 typemap = afinfo->type_map;
103 type = typemap[proto];
104 if (unlikely(type && !try_module_get(type->owner)))
106 if (!type && !modload_attempted) {
107 xfrm_policy_put_afinfo(afinfo);
108 request_module("xfrm-type-%d-%d",
109 (int) family, (int) proto);
110 modload_attempted = 1;
114 xfrm_policy_put_afinfo(afinfo);
118 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
119 unsigned short family)
121 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
124 if (unlikely(afinfo == NULL))
125 return -EAFNOSUPPORT;
127 if (likely(afinfo->dst_lookup != NULL))
128 err = afinfo->dst_lookup(dst, fl);
131 xfrm_policy_put_afinfo(afinfo);
134 EXPORT_SYMBOL(xfrm_dst_lookup);
136 void xfrm_put_type(struct xfrm_type *type)
138 module_put(type->owner);
141 int xfrm_register_mode(struct xfrm_mode *mode, int family)
143 struct xfrm_policy_afinfo *afinfo;
144 struct xfrm_mode **modemap;
147 if (unlikely(mode->encap >= XFRM_MODE_MAX))
150 afinfo = xfrm_policy_lock_afinfo(family);
151 if (unlikely(afinfo == NULL))
152 return -EAFNOSUPPORT;
155 modemap = afinfo->mode_map;
156 if (likely(modemap[mode->encap] == NULL)) {
157 modemap[mode->encap] = mode;
161 xfrm_policy_unlock_afinfo(afinfo);
164 EXPORT_SYMBOL(xfrm_register_mode);
166 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
168 struct xfrm_policy_afinfo *afinfo;
169 struct xfrm_mode **modemap;
172 if (unlikely(mode->encap >= XFRM_MODE_MAX))
175 afinfo = xfrm_policy_lock_afinfo(family);
176 if (unlikely(afinfo == NULL))
177 return -EAFNOSUPPORT;
180 modemap = afinfo->mode_map;
181 if (likely(modemap[mode->encap] == mode)) {
182 modemap[mode->encap] = NULL;
186 xfrm_policy_unlock_afinfo(afinfo);
189 EXPORT_SYMBOL(xfrm_unregister_mode);
191 struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
193 struct xfrm_policy_afinfo *afinfo;
194 struct xfrm_mode *mode;
195 int modload_attempted = 0;
197 if (unlikely(encap >= XFRM_MODE_MAX))
201 afinfo = xfrm_policy_get_afinfo(family);
202 if (unlikely(afinfo == NULL))
205 mode = afinfo->mode_map[encap];
206 if (unlikely(mode && !try_module_get(mode->owner)))
208 if (!mode && !modload_attempted) {
209 xfrm_policy_put_afinfo(afinfo);
210 request_module("xfrm-mode-%d-%d", family, encap);
211 modload_attempted = 1;
215 xfrm_policy_put_afinfo(afinfo);
219 void xfrm_put_mode(struct xfrm_mode *mode)
221 module_put(mode->owner);
224 static inline unsigned long make_jiffies(long secs)
226 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
227 return MAX_SCHEDULE_TIMEOUT-1;
232 static void xfrm_policy_timer(unsigned long data)
234 struct xfrm_policy *xp = (struct xfrm_policy*)data;
235 unsigned long now = (unsigned long)xtime.tv_sec;
236 long next = LONG_MAX;
240 read_lock(&xp->lock);
245 dir = xfrm_policy_id2dir(xp->index);
247 if (xp->lft.hard_add_expires_seconds) {
248 long tmo = xp->lft.hard_add_expires_seconds +
249 xp->curlft.add_time - now;
255 if (xp->lft.hard_use_expires_seconds) {
256 long tmo = xp->lft.hard_use_expires_seconds +
257 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
263 if (xp->lft.soft_add_expires_seconds) {
264 long tmo = xp->lft.soft_add_expires_seconds +
265 xp->curlft.add_time - now;
268 tmo = XFRM_KM_TIMEOUT;
273 if (xp->lft.soft_use_expires_seconds) {
274 long tmo = xp->lft.soft_use_expires_seconds +
275 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
278 tmo = XFRM_KM_TIMEOUT;
285 km_policy_expired(xp, dir, 0, 0);
286 if (next != LONG_MAX &&
287 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
291 read_unlock(&xp->lock);
296 read_unlock(&xp->lock);
297 if (!xfrm_policy_delete(xp, dir))
298 km_policy_expired(xp, dir, 1, 0);
303 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
307 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
309 struct xfrm_policy *policy;
311 policy = kmalloc(sizeof(struct xfrm_policy), gfp);
314 memset(policy, 0, sizeof(struct xfrm_policy));
315 atomic_set(&policy->refcnt, 1);
316 rwlock_init(&policy->lock);
317 init_timer(&policy->timer);
318 policy->timer.data = (unsigned long)policy;
319 policy->timer.function = xfrm_policy_timer;
323 EXPORT_SYMBOL(xfrm_policy_alloc);
325 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
327 void __xfrm_policy_destroy(struct xfrm_policy *policy)
329 BUG_ON(!policy->dead);
331 BUG_ON(policy->bundles);
333 if (del_timer(&policy->timer))
336 security_xfrm_policy_free(policy);
339 EXPORT_SYMBOL(__xfrm_policy_destroy);
341 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
343 struct dst_entry *dst;
345 while ((dst = policy->bundles) != NULL) {
346 policy->bundles = dst->next;
350 if (del_timer(&policy->timer))
351 atomic_dec(&policy->refcnt);
353 if (atomic_read(&policy->refcnt) > 1)
356 xfrm_pol_put(policy);
359 static void xfrm_policy_gc_task(void *data)
361 struct xfrm_policy *policy;
362 struct list_head *entry, *tmp;
363 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
365 spin_lock_bh(&xfrm_policy_gc_lock);
366 list_splice_init(&xfrm_policy_gc_list, &gc_list);
367 spin_unlock_bh(&xfrm_policy_gc_lock);
369 list_for_each_safe(entry, tmp, &gc_list) {
370 policy = list_entry(entry, struct xfrm_policy, list);
371 xfrm_policy_gc_kill(policy);
375 /* Rule must be locked. Release descentant resources, announce
376 * entry dead. The rule must be unlinked from lists to the moment.
379 static void xfrm_policy_kill(struct xfrm_policy *policy)
383 write_lock_bh(&policy->lock);
386 write_unlock_bh(&policy->lock);
388 if (unlikely(dead)) {
393 spin_lock(&xfrm_policy_gc_lock);
394 list_add(&policy->list, &xfrm_policy_gc_list);
395 spin_unlock(&xfrm_policy_gc_lock);
397 schedule_work(&xfrm_policy_gc_work);
400 /* Generate new index... KAME seems to generate them ordered by cost
401 * of an absolute inpredictability of ordering of rules. This will not pass. */
402 static u32 xfrm_gen_index(int dir)
405 struct xfrm_policy *p;
406 static u32 idx_generator;
409 idx = (idx_generator | dir);
413 for (p = xfrm_policy_list[dir]; p; p = p->next) {
422 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
424 struct xfrm_policy *pol, **p;
425 struct xfrm_policy *delpol = NULL;
426 struct xfrm_policy **newpos = NULL;
427 struct dst_entry *gc_list;
429 write_lock_bh(&xfrm_policy_lock);
430 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL;) {
431 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
432 xfrm_sec_ctx_match(pol->security, policy->security)) {
434 write_unlock_bh(&xfrm_policy_lock);
439 if (policy->priority > pol->priority)
441 } else if (policy->priority >= pol->priority) {
453 xfrm_pol_hold(policy);
456 atomic_inc(&flow_cache_genid);
457 policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
458 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
459 policy->curlft.use_time = 0;
460 if (!mod_timer(&policy->timer, jiffies + HZ))
461 xfrm_pol_hold(policy);
462 write_unlock_bh(&xfrm_policy_lock);
465 xfrm_policy_kill(delpol);
467 read_lock_bh(&xfrm_policy_lock);
469 for (policy = policy->next; policy; policy = policy->next) {
470 struct dst_entry *dst;
472 write_lock(&policy->lock);
473 dst = policy->bundles;
475 struct dst_entry *tail = dst;
478 tail->next = gc_list;
481 policy->bundles = NULL;
483 write_unlock(&policy->lock);
485 read_unlock_bh(&xfrm_policy_lock);
488 struct dst_entry *dst = gc_list;
496 EXPORT_SYMBOL(xfrm_policy_insert);
498 struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
499 struct xfrm_sec_ctx *ctx, int delete)
501 struct xfrm_policy *pol, **p;
503 write_lock_bh(&xfrm_policy_lock);
504 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
505 if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
506 (xfrm_sec_ctx_match(ctx, pol->security))) {
513 write_unlock_bh(&xfrm_policy_lock);
516 atomic_inc(&flow_cache_genid);
517 xfrm_policy_kill(pol);
521 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
523 struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
525 struct xfrm_policy *pol, **p;
527 write_lock_bh(&xfrm_policy_lock);
528 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
529 if (pol->index == id) {
536 write_unlock_bh(&xfrm_policy_lock);
539 atomic_inc(&flow_cache_genid);
540 xfrm_policy_kill(pol);
544 EXPORT_SYMBOL(xfrm_policy_byid);
546 void xfrm_policy_flush(void)
548 struct xfrm_policy *xp;
551 write_lock_bh(&xfrm_policy_lock);
552 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
553 while ((xp = xfrm_policy_list[dir]) != NULL) {
554 xfrm_policy_list[dir] = xp->next;
555 write_unlock_bh(&xfrm_policy_lock);
557 xfrm_policy_kill(xp);
559 write_lock_bh(&xfrm_policy_lock);
562 atomic_inc(&flow_cache_genid);
563 write_unlock_bh(&xfrm_policy_lock);
565 EXPORT_SYMBOL(xfrm_policy_flush);
567 int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
570 struct xfrm_policy *xp;
575 read_lock_bh(&xfrm_policy_lock);
576 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
577 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
586 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
587 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
588 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
595 read_unlock_bh(&xfrm_policy_lock);
598 EXPORT_SYMBOL(xfrm_policy_walk);
600 /* Find policy to apply to this flow. */
602 static void xfrm_policy_lookup(struct flowi *fl, u32 sk_sid, u16 family, u8 dir,
603 void **objp, atomic_t **obj_refp)
605 struct xfrm_policy *pol;
607 read_lock_bh(&xfrm_policy_lock);
608 for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
609 struct xfrm_selector *sel = &pol->selector;
612 if (pol->family != family)
615 match = xfrm_selector_match(sel, fl, family);
618 if (!security_xfrm_policy_lookup(pol, sk_sid, dir)) {
624 read_unlock_bh(&xfrm_policy_lock);
625 if ((*objp = (void *) pol) != NULL)
626 *obj_refp = &pol->refcnt;
629 static inline int policy_to_flow_dir(int dir)
631 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
632 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
633 XFRM_POLICY_FWD == FLOW_DIR_FWD)
639 case XFRM_POLICY_OUT:
641 case XFRM_POLICY_FWD:
646 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl, u32 sk_sid)
648 struct xfrm_policy *pol;
650 read_lock_bh(&xfrm_policy_lock);
651 if ((pol = sk->sk_policy[dir]) != NULL) {
652 int match = xfrm_selector_match(&pol->selector, fl,
657 err = security_xfrm_policy_lookup(pol, sk_sid, policy_to_flow_dir(dir));
664 read_unlock_bh(&xfrm_policy_lock);
668 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
670 pol->next = xfrm_policy_list[dir];
671 xfrm_policy_list[dir] = pol;
675 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
678 struct xfrm_policy **polp;
680 for (polp = &xfrm_policy_list[dir];
681 *polp != NULL; polp = &(*polp)->next) {
690 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
692 write_lock_bh(&xfrm_policy_lock);
693 pol = __xfrm_policy_unlink(pol, dir);
694 write_unlock_bh(&xfrm_policy_lock);
696 if (dir < XFRM_POLICY_MAX)
697 atomic_inc(&flow_cache_genid);
698 xfrm_policy_kill(pol);
703 EXPORT_SYMBOL(xfrm_policy_delete);
705 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
707 struct xfrm_policy *old_pol;
709 write_lock_bh(&xfrm_policy_lock);
710 old_pol = sk->sk_policy[dir];
711 sk->sk_policy[dir] = pol;
713 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
714 pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
715 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
718 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
719 write_unlock_bh(&xfrm_policy_lock);
722 xfrm_policy_kill(old_pol);
727 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
729 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
732 newp->selector = old->selector;
733 if (security_xfrm_policy_clone(old, newp)) {
735 return NULL; /* ENOMEM */
737 newp->lft = old->lft;
738 newp->curlft = old->curlft;
739 newp->action = old->action;
740 newp->flags = old->flags;
741 newp->xfrm_nr = old->xfrm_nr;
742 newp->index = old->index;
743 memcpy(newp->xfrm_vec, old->xfrm_vec,
744 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
745 write_lock_bh(&xfrm_policy_lock);
746 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
747 write_unlock_bh(&xfrm_policy_lock);
753 int __xfrm_sk_clone_policy(struct sock *sk)
755 struct xfrm_policy *p0 = sk->sk_policy[0],
756 *p1 = sk->sk_policy[1];
758 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
759 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
761 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
766 /* Resolve list of templates for the flow, given policy. */
769 xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
770 struct xfrm_state **xfrm,
771 unsigned short family)
775 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
776 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
778 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
779 struct xfrm_state *x;
780 xfrm_address_t *remote = daddr;
781 xfrm_address_t *local = saddr;
782 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
785 remote = &tmpl->id.daddr;
786 local = &tmpl->saddr;
789 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
791 if (x && x->km.state == XFRM_STATE_VALID) {
798 error = (x->km.state == XFRM_STATE_ERROR ?
809 for (nx--; nx>=0; nx--)
810 xfrm_state_put(xfrm[nx]);
814 /* Check that the bundle accepts the flow and its components are
818 static struct dst_entry *
819 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
822 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
823 if (unlikely(afinfo == NULL))
824 return ERR_PTR(-EINVAL);
825 x = afinfo->find_bundle(fl, policy);
826 xfrm_policy_put_afinfo(afinfo);
830 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
831 * all the metrics... Shortly, bundle a bundle.
835 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
836 struct flowi *fl, struct dst_entry **dst_p,
837 unsigned short family)
840 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
841 if (unlikely(afinfo == NULL))
843 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
844 xfrm_policy_put_afinfo(afinfo);
849 static int stale_bundle(struct dst_entry *dst);
851 /* Main function: finds/creates a bundle for given flow.
853 * At the moment we eat a raw IP route. Mostly to speed up lookups
854 * on interfaces with disabled IPsec.
856 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
857 struct sock *sk, int flags)
859 struct xfrm_policy *policy;
860 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
861 struct dst_entry *dst, *dst_orig = *dst_p;
866 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
867 u32 sk_sid = security_sk_sid(sk, fl, dir);
869 genid = atomic_read(&flow_cache_genid);
871 if (sk && sk->sk_policy[1])
872 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, sk_sid);
875 /* To accelerate a bit... */
876 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
879 policy = flow_cache_lookup(fl, sk_sid, dst_orig->ops->family,
880 dir, xfrm_policy_lookup);
886 family = dst_orig->ops->family;
887 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
889 switch (policy->action) {
890 case XFRM_POLICY_BLOCK:
891 /* Prohibit the flow */
895 case XFRM_POLICY_ALLOW:
896 if (policy->xfrm_nr == 0) {
897 /* Flow passes not transformed. */
898 xfrm_pol_put(policy);
902 /* Try to find matching bundle.
904 * LATER: help from flow cache. It is optional, this
905 * is required only for output policy.
907 dst = xfrm_find_bundle(fl, policy, family);
916 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
918 if (unlikely(nx<0)) {
920 if (err == -EAGAIN && flags) {
921 DECLARE_WAITQUEUE(wait, current);
923 add_wait_queue(&km_waitq, &wait);
924 set_current_state(TASK_INTERRUPTIBLE);
926 set_current_state(TASK_RUNNING);
927 remove_wait_queue(&km_waitq, &wait);
929 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
931 if (nx == -EAGAIN && signal_pending(current)) {
936 genid != atomic_read(&flow_cache_genid)) {
937 xfrm_pol_put(policy);
946 /* Flow passes not transformed. */
947 xfrm_pol_put(policy);
952 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
957 xfrm_state_put(xfrm[i]);
961 write_lock_bh(&policy->lock);
962 if (unlikely(policy->dead || stale_bundle(dst))) {
963 /* Wow! While we worked on resolving, this
964 * policy has gone. Retry. It is not paranoia,
965 * we just cannot enlist new bundle to dead object.
966 * We can't enlist stable bundles either.
968 write_unlock_bh(&policy->lock);
975 dst->next = policy->bundles;
976 policy->bundles = dst;
978 write_unlock_bh(&policy->lock);
981 dst_release(dst_orig);
982 xfrm_pol_put(policy);
986 dst_release(dst_orig);
987 xfrm_pol_put(policy);
991 EXPORT_SYMBOL(xfrm_lookup);
993 /* When skb is transformed back to its "native" form, we have to
994 * check policy restrictions. At the moment we make this in maximally
995 * stupid way. Shame on me. :-) Of course, connected sockets must
996 * have policy cached at them.
1000 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1001 unsigned short family)
1003 if (xfrm_state_kern(x))
1004 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
1005 return x->id.proto == tmpl->id.proto &&
1006 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1007 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1008 x->props.mode == tmpl->mode &&
1009 (tmpl->aalgos & (1<<x->props.aalgo)) &&
1010 !(x->props.mode && xfrm_state_addr_cmp(tmpl, x, family));
1014 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1015 unsigned short family)
1019 if (tmpl->optional) {
1024 for (; idx < sp->len; idx++) {
1025 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1027 if (sp->xvec[idx]->props.mode)
1034 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1036 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1038 if (unlikely(afinfo == NULL))
1039 return -EAFNOSUPPORT;
1041 afinfo->decode_session(skb, fl);
1042 xfrm_policy_put_afinfo(afinfo);
1045 EXPORT_SYMBOL(xfrm_decode_session);
1047 static inline int secpath_has_tunnel(struct sec_path *sp, int k)
1049 for (; k < sp->len; k++) {
1050 if (sp->xvec[k]->props.mode)
1057 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1058 unsigned short family)
1060 struct xfrm_policy *pol;
1062 u8 fl_dir = policy_to_flow_dir(dir);
1065 if (xfrm_decode_session(skb, &fl, family) < 0)
1067 nf_nat_decode_session(skb, &fl, family);
1069 sk_sid = security_sk_sid(sk, &fl, fl_dir);
1071 /* First, check used SA against their selectors. */
1075 for (i=skb->sp->len-1; i>=0; i--) {
1076 struct xfrm_state *x = skb->sp->xvec[i];
1077 if (!xfrm_selector_match(&x->sel, &fl, family))
1083 if (sk && sk->sk_policy[dir])
1084 pol = xfrm_sk_policy_lookup(sk, dir, &fl, sk_sid);
1087 pol = flow_cache_lookup(&fl, sk_sid, family, fl_dir,
1088 xfrm_policy_lookup);
1091 return !skb->sp || !secpath_has_tunnel(skb->sp, 0);
1093 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1095 if (pol->action == XFRM_POLICY_ALLOW) {
1096 struct sec_path *sp;
1097 static struct sec_path dummy;
1100 if ((sp = skb->sp) == NULL)
1103 /* For each tunnel xfrm, find the first matching tmpl.
1104 * For each tmpl before that, find corresponding xfrm.
1105 * Order is _important_. Later we will implement
1106 * some barriers, but at the moment barriers
1107 * are implied between each two transformations.
1109 for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
1110 k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
1115 if (secpath_has_tunnel(sp, k))
1126 EXPORT_SYMBOL(__xfrm_policy_check);
1128 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1132 if (xfrm_decode_session(skb, &fl, family) < 0)
1135 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1137 EXPORT_SYMBOL(__xfrm_route_forward);
1139 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1141 /* If it is marked obsolete, which is how we even get here,
1142 * then we have purged it from the policy bundle list and we
1143 * did that for a good reason.
1148 static int stale_bundle(struct dst_entry *dst)
1150 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC);
1153 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1155 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1156 dst->dev = &loopback_dev;
1157 dev_hold(&loopback_dev);
1161 EXPORT_SYMBOL(xfrm_dst_ifdown);
1163 static void xfrm_link_failure(struct sk_buff *skb)
1165 /* Impossible. Such dst must be popped before reaches point of failure. */
1169 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1172 if (dst->obsolete) {
1180 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1183 struct xfrm_policy *pol;
1184 struct dst_entry *dst, **dstp, *gc_list = NULL;
1186 read_lock_bh(&xfrm_policy_lock);
1187 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
1188 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1189 write_lock(&pol->lock);
1190 dstp = &pol->bundles;
1191 while ((dst=*dstp) != NULL) {
1194 dst->next = gc_list;
1200 write_unlock(&pol->lock);
1203 read_unlock_bh(&xfrm_policy_lock);
1207 gc_list = dst->next;
1212 static int unused_bundle(struct dst_entry *dst)
1214 return !atomic_read(&dst->__refcnt);
1217 static void __xfrm_garbage_collect(void)
1219 xfrm_prune_bundles(unused_bundle);
1222 int xfrm_flush_bundles(void)
1224 xfrm_prune_bundles(stale_bundle);
1228 static int always_true(struct dst_entry *dst)
1233 void xfrm_flush_all_bundles(void)
1235 xfrm_prune_bundles(always_true);
1238 void xfrm_init_pmtu(struct dst_entry *dst)
1241 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1242 u32 pmtu, route_mtu_cached;
1244 pmtu = dst_mtu(dst->child);
1245 xdst->child_mtu_cached = pmtu;
1247 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1249 route_mtu_cached = dst_mtu(xdst->route);
1250 xdst->route_mtu_cached = route_mtu_cached;
1252 if (pmtu > route_mtu_cached)
1253 pmtu = route_mtu_cached;
1255 dst->metrics[RTAX_MTU-1] = pmtu;
1256 } while ((dst = dst->next));
1259 EXPORT_SYMBOL(xfrm_init_pmtu);
1261 /* Check that the bundle accepts the flow and its components are
1265 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family)
1267 struct dst_entry *dst = &first->u.dst;
1268 struct xfrm_dst *last;
1271 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1272 (dst->dev && !netif_running(dst->dev)))
1278 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1280 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1282 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1285 mtu = dst_mtu(dst->child);
1286 if (xdst->child_mtu_cached != mtu) {
1288 xdst->child_mtu_cached = mtu;
1291 if (!dst_check(xdst->route, xdst->route_cookie))
1293 mtu = dst_mtu(xdst->route);
1294 if (xdst->route_mtu_cached != mtu) {
1296 xdst->route_mtu_cached = mtu;
1300 } while (dst->xfrm);
1305 mtu = last->child_mtu_cached;
1309 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1310 if (mtu > last->route_mtu_cached)
1311 mtu = last->route_mtu_cached;
1312 dst->metrics[RTAX_MTU-1] = mtu;
1317 last = last->u.next;
1318 last->child_mtu_cached = mtu;
1324 EXPORT_SYMBOL(xfrm_bundle_ok);
1326 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1329 if (unlikely(afinfo == NULL))
1331 if (unlikely(afinfo->family >= NPROTO))
1332 return -EAFNOSUPPORT;
1333 write_lock_bh(&xfrm_policy_afinfo_lock);
1334 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1337 struct dst_ops *dst_ops = afinfo->dst_ops;
1338 if (likely(dst_ops->kmem_cachep == NULL))
1339 dst_ops->kmem_cachep = xfrm_dst_cache;
1340 if (likely(dst_ops->check == NULL))
1341 dst_ops->check = xfrm_dst_check;
1342 if (likely(dst_ops->negative_advice == NULL))
1343 dst_ops->negative_advice = xfrm_negative_advice;
1344 if (likely(dst_ops->link_failure == NULL))
1345 dst_ops->link_failure = xfrm_link_failure;
1346 if (likely(afinfo->garbage_collect == NULL))
1347 afinfo->garbage_collect = __xfrm_garbage_collect;
1348 xfrm_policy_afinfo[afinfo->family] = afinfo;
1350 write_unlock_bh(&xfrm_policy_afinfo_lock);
1353 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1355 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1358 if (unlikely(afinfo == NULL))
1360 if (unlikely(afinfo->family >= NPROTO))
1361 return -EAFNOSUPPORT;
1362 write_lock_bh(&xfrm_policy_afinfo_lock);
1363 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1364 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1367 struct dst_ops *dst_ops = afinfo->dst_ops;
1368 xfrm_policy_afinfo[afinfo->family] = NULL;
1369 dst_ops->kmem_cachep = NULL;
1370 dst_ops->check = NULL;
1371 dst_ops->negative_advice = NULL;
1372 dst_ops->link_failure = NULL;
1373 afinfo->garbage_collect = NULL;
1376 write_unlock_bh(&xfrm_policy_afinfo_lock);
1379 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1381 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1383 struct xfrm_policy_afinfo *afinfo;
1384 if (unlikely(family >= NPROTO))
1386 read_lock(&xfrm_policy_afinfo_lock);
1387 afinfo = xfrm_policy_afinfo[family];
1388 if (unlikely(!afinfo))
1389 read_unlock(&xfrm_policy_afinfo_lock);
1393 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1395 read_unlock(&xfrm_policy_afinfo_lock);
1398 static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1400 struct xfrm_policy_afinfo *afinfo;
1401 if (unlikely(family >= NPROTO))
1403 write_lock_bh(&xfrm_policy_afinfo_lock);
1404 afinfo = xfrm_policy_afinfo[family];
1405 if (unlikely(!afinfo))
1406 write_unlock_bh(&xfrm_policy_afinfo_lock);
1410 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1412 write_unlock_bh(&xfrm_policy_afinfo_lock);
1415 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1419 xfrm_flush_bundles();
1424 static struct notifier_block xfrm_dev_notifier = {
1430 static void __init xfrm_policy_init(void)
1432 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1433 sizeof(struct xfrm_dst),
1434 0, SLAB_HWCACHE_ALIGN,
1436 if (!xfrm_dst_cache)
1437 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1439 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1440 register_netdevice_notifier(&xfrm_dev_notifier);
1443 void __init xfrm_init(void)