[NETFILTER]: Export ip6_masked_addrcmp, don't pass IPv6 addresses on stack
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / xfrm / xfrm_policy.c
blob64a447375fdb7a976e4fabe58da292d3c5817d31
1 /*
2 * xfrm_policy.c
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <asm/bug.h>
17 #include <linux/config.h>
18 #include <linux/slab.h>
19 #include <linux/kmod.h>
20 #include <linux/list.h>
21 #include <linux/spinlock.h>
22 #include <linux/workqueue.h>
23 #include <linux/notifier.h>
24 #include <linux/netdevice.h>
25 #include <linux/module.h>
26 #include <net/xfrm.h>
27 #include <net/ip.h>
29 DECLARE_MUTEX(xfrm_cfg_sem);
30 EXPORT_SYMBOL(xfrm_cfg_sem);
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);
50 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
52 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
53 struct xfrm_type_map *typemap;
54 int err = 0;
56 if (unlikely(afinfo == NULL))
57 return -EAFNOSUPPORT;
58 typemap = afinfo->type_map;
60 write_lock(&typemap->lock);
61 if (likely(typemap->map[type->proto] == NULL))
62 typemap->map[type->proto] = type;
63 else
64 err = -EEXIST;
65 write_unlock(&typemap->lock);
66 xfrm_policy_put_afinfo(afinfo);
67 return err;
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_get_afinfo(family);
74 struct xfrm_type_map *typemap;
75 int err = 0;
77 if (unlikely(afinfo == NULL))
78 return -EAFNOSUPPORT;
79 typemap = afinfo->type_map;
81 write_lock(&typemap->lock);
82 if (unlikely(typemap->map[type->proto] != type))
83 err = -ENOENT;
84 else
85 typemap->map[type->proto] = NULL;
86 write_unlock(&typemap->lock);
87 xfrm_policy_put_afinfo(afinfo);
88 return err;
90 EXPORT_SYMBOL(xfrm_unregister_type);
92 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
94 struct xfrm_policy_afinfo *afinfo;
95 struct xfrm_type_map *typemap;
96 struct xfrm_type *type;
97 int modload_attempted = 0;
99 retry:
100 afinfo = xfrm_policy_get_afinfo(family);
101 if (unlikely(afinfo == NULL))
102 return NULL;
103 typemap = afinfo->type_map;
105 read_lock(&typemap->lock);
106 type = typemap->map[proto];
107 if (unlikely(type && !try_module_get(type->owner)))
108 type = NULL;
109 read_unlock(&typemap->lock);
110 if (!type && !modload_attempted) {
111 xfrm_policy_put_afinfo(afinfo);
112 request_module("xfrm-type-%d-%d",
113 (int) family, (int) proto);
114 modload_attempted = 1;
115 goto retry;
118 xfrm_policy_put_afinfo(afinfo);
119 return type;
122 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
123 unsigned short family)
125 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
126 int err = 0;
128 if (unlikely(afinfo == NULL))
129 return -EAFNOSUPPORT;
131 if (likely(afinfo->dst_lookup != NULL))
132 err = afinfo->dst_lookup(dst, fl);
133 else
134 err = -EINVAL;
135 xfrm_policy_put_afinfo(afinfo);
136 return err;
138 EXPORT_SYMBOL(xfrm_dst_lookup);
140 void xfrm_put_type(struct xfrm_type *type)
142 module_put(type->owner);
145 static inline unsigned long make_jiffies(long secs)
147 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
148 return MAX_SCHEDULE_TIMEOUT-1;
149 else
150 return secs*HZ;
153 static void xfrm_policy_timer(unsigned long data)
155 struct xfrm_policy *xp = (struct xfrm_policy*)data;
156 unsigned long now = (unsigned long)xtime.tv_sec;
157 long next = LONG_MAX;
158 int warn = 0;
159 int dir;
161 read_lock(&xp->lock);
163 if (xp->dead)
164 goto out;
166 dir = xfrm_policy_id2dir(xp->index);
168 if (xp->lft.hard_add_expires_seconds) {
169 long tmo = xp->lft.hard_add_expires_seconds +
170 xp->curlft.add_time - now;
171 if (tmo <= 0)
172 goto expired;
173 if (tmo < next)
174 next = tmo;
176 if (xp->lft.hard_use_expires_seconds) {
177 long tmo = xp->lft.hard_use_expires_seconds +
178 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
179 if (tmo <= 0)
180 goto expired;
181 if (tmo < next)
182 next = tmo;
184 if (xp->lft.soft_add_expires_seconds) {
185 long tmo = xp->lft.soft_add_expires_seconds +
186 xp->curlft.add_time - now;
187 if (tmo <= 0) {
188 warn = 1;
189 tmo = XFRM_KM_TIMEOUT;
191 if (tmo < next)
192 next = tmo;
194 if (xp->lft.soft_use_expires_seconds) {
195 long tmo = xp->lft.soft_use_expires_seconds +
196 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
197 if (tmo <= 0) {
198 warn = 1;
199 tmo = XFRM_KM_TIMEOUT;
201 if (tmo < next)
202 next = tmo;
205 if (warn)
206 km_policy_expired(xp, dir, 0);
207 if (next != LONG_MAX &&
208 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
209 xfrm_pol_hold(xp);
211 out:
212 read_unlock(&xp->lock);
213 xfrm_pol_put(xp);
214 return;
216 expired:
217 read_unlock(&xp->lock);
218 if (!xfrm_policy_delete(xp, dir))
219 km_policy_expired(xp, dir, 1);
220 xfrm_pol_put(xp);
224 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
225 * SPD calls.
228 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
230 struct xfrm_policy *policy;
232 policy = kmalloc(sizeof(struct xfrm_policy), gfp);
234 if (policy) {
235 memset(policy, 0, sizeof(struct xfrm_policy));
236 atomic_set(&policy->refcnt, 1);
237 rwlock_init(&policy->lock);
238 init_timer(&policy->timer);
239 policy->timer.data = (unsigned long)policy;
240 policy->timer.function = xfrm_policy_timer;
242 return policy;
244 EXPORT_SYMBOL(xfrm_policy_alloc);
246 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
248 void __xfrm_policy_destroy(struct xfrm_policy *policy)
250 if (!policy->dead)
251 BUG();
253 if (policy->bundles)
254 BUG();
256 if (del_timer(&policy->timer))
257 BUG();
259 security_xfrm_policy_free(policy);
260 kfree(policy);
262 EXPORT_SYMBOL(__xfrm_policy_destroy);
264 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
266 struct dst_entry *dst;
268 while ((dst = policy->bundles) != NULL) {
269 policy->bundles = dst->next;
270 dst_free(dst);
273 if (del_timer(&policy->timer))
274 atomic_dec(&policy->refcnt);
276 if (atomic_read(&policy->refcnt) > 1)
277 flow_cache_flush();
279 xfrm_pol_put(policy);
282 static void xfrm_policy_gc_task(void *data)
284 struct xfrm_policy *policy;
285 struct list_head *entry, *tmp;
286 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
288 spin_lock_bh(&xfrm_policy_gc_lock);
289 list_splice_init(&xfrm_policy_gc_list, &gc_list);
290 spin_unlock_bh(&xfrm_policy_gc_lock);
292 list_for_each_safe(entry, tmp, &gc_list) {
293 policy = list_entry(entry, struct xfrm_policy, list);
294 xfrm_policy_gc_kill(policy);
298 /* Rule must be locked. Release descentant resources, announce
299 * entry dead. The rule must be unlinked from lists to the moment.
302 static void xfrm_policy_kill(struct xfrm_policy *policy)
304 int dead;
306 write_lock_bh(&policy->lock);
307 dead = policy->dead;
308 policy->dead = 1;
309 write_unlock_bh(&policy->lock);
311 if (unlikely(dead)) {
312 WARN_ON(1);
313 return;
316 spin_lock(&xfrm_policy_gc_lock);
317 list_add(&policy->list, &xfrm_policy_gc_list);
318 spin_unlock(&xfrm_policy_gc_lock);
320 schedule_work(&xfrm_policy_gc_work);
323 /* Generate new index... KAME seems to generate them ordered by cost
324 * of an absolute inpredictability of ordering of rules. This will not pass. */
325 static u32 xfrm_gen_index(int dir)
327 u32 idx;
328 struct xfrm_policy *p;
329 static u32 idx_generator;
331 for (;;) {
332 idx = (idx_generator | dir);
333 idx_generator += 8;
334 if (idx == 0)
335 idx = 8;
336 for (p = xfrm_policy_list[dir]; p; p = p->next) {
337 if (p->index == idx)
338 break;
340 if (!p)
341 return idx;
345 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
347 struct xfrm_policy *pol, **p;
348 struct xfrm_policy *delpol = NULL;
349 struct xfrm_policy **newpos = NULL;
350 struct dst_entry *gc_list;
352 write_lock_bh(&xfrm_policy_lock);
353 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL;) {
354 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
355 xfrm_sec_ctx_match(pol->security, policy->security)) {
356 if (excl) {
357 write_unlock_bh(&xfrm_policy_lock);
358 return -EEXIST;
360 *p = pol->next;
361 delpol = pol;
362 if (policy->priority > pol->priority)
363 continue;
364 } else if (policy->priority >= pol->priority) {
365 p = &pol->next;
366 continue;
368 if (!newpos)
369 newpos = p;
370 if (delpol)
371 break;
372 p = &pol->next;
374 if (newpos)
375 p = newpos;
376 xfrm_pol_hold(policy);
377 policy->next = *p;
378 *p = policy;
379 atomic_inc(&flow_cache_genid);
380 policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
381 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
382 policy->curlft.use_time = 0;
383 if (!mod_timer(&policy->timer, jiffies + HZ))
384 xfrm_pol_hold(policy);
385 write_unlock_bh(&xfrm_policy_lock);
387 if (delpol)
388 xfrm_policy_kill(delpol);
390 read_lock_bh(&xfrm_policy_lock);
391 gc_list = NULL;
392 for (policy = policy->next; policy; policy = policy->next) {
393 struct dst_entry *dst;
395 write_lock(&policy->lock);
396 dst = policy->bundles;
397 if (dst) {
398 struct dst_entry *tail = dst;
399 while (tail->next)
400 tail = tail->next;
401 tail->next = gc_list;
402 gc_list = dst;
404 policy->bundles = NULL;
406 write_unlock(&policy->lock);
408 read_unlock_bh(&xfrm_policy_lock);
410 while (gc_list) {
411 struct dst_entry *dst = gc_list;
413 gc_list = dst->next;
414 dst_free(dst);
417 return 0;
419 EXPORT_SYMBOL(xfrm_policy_insert);
421 struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
422 struct xfrm_sec_ctx *ctx, int delete)
424 struct xfrm_policy *pol, **p;
426 write_lock_bh(&xfrm_policy_lock);
427 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
428 if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
429 (xfrm_sec_ctx_match(ctx, pol->security))) {
430 xfrm_pol_hold(pol);
431 if (delete)
432 *p = pol->next;
433 break;
436 write_unlock_bh(&xfrm_policy_lock);
438 if (pol && delete) {
439 atomic_inc(&flow_cache_genid);
440 xfrm_policy_kill(pol);
442 return pol;
444 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
446 struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
448 struct xfrm_policy *pol, **p;
450 write_lock_bh(&xfrm_policy_lock);
451 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
452 if (pol->index == id) {
453 xfrm_pol_hold(pol);
454 if (delete)
455 *p = pol->next;
456 break;
459 write_unlock_bh(&xfrm_policy_lock);
461 if (pol && delete) {
462 atomic_inc(&flow_cache_genid);
463 xfrm_policy_kill(pol);
465 return pol;
467 EXPORT_SYMBOL(xfrm_policy_byid);
469 void xfrm_policy_flush(void)
471 struct xfrm_policy *xp;
472 int dir;
474 write_lock_bh(&xfrm_policy_lock);
475 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
476 while ((xp = xfrm_policy_list[dir]) != NULL) {
477 xfrm_policy_list[dir] = xp->next;
478 write_unlock_bh(&xfrm_policy_lock);
480 xfrm_policy_kill(xp);
482 write_lock_bh(&xfrm_policy_lock);
485 atomic_inc(&flow_cache_genid);
486 write_unlock_bh(&xfrm_policy_lock);
488 EXPORT_SYMBOL(xfrm_policy_flush);
490 int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
491 void *data)
493 struct xfrm_policy *xp;
494 int dir;
495 int count = 0;
496 int error = 0;
498 read_lock_bh(&xfrm_policy_lock);
499 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
500 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
501 count++;
504 if (count == 0) {
505 error = -ENOENT;
506 goto out;
509 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
510 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
511 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
512 if (error)
513 goto out;
517 out:
518 read_unlock_bh(&xfrm_policy_lock);
519 return error;
521 EXPORT_SYMBOL(xfrm_policy_walk);
523 /* Find policy to apply to this flow. */
525 static void xfrm_policy_lookup(struct flowi *fl, u32 sk_sid, u16 family, u8 dir,
526 void **objp, atomic_t **obj_refp)
528 struct xfrm_policy *pol;
530 read_lock_bh(&xfrm_policy_lock);
531 for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
532 struct xfrm_selector *sel = &pol->selector;
533 int match;
535 if (pol->family != family)
536 continue;
538 match = xfrm_selector_match(sel, fl, family);
540 if (match) {
541 if (!security_xfrm_policy_lookup(pol, sk_sid, dir)) {
542 xfrm_pol_hold(pol);
543 break;
547 read_unlock_bh(&xfrm_policy_lock);
548 if ((*objp = (void *) pol) != NULL)
549 *obj_refp = &pol->refcnt;
552 static inline int policy_to_flow_dir(int dir)
554 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
555 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
556 XFRM_POLICY_FWD == FLOW_DIR_FWD)
557 return dir;
558 switch (dir) {
559 default:
560 case XFRM_POLICY_IN:
561 return FLOW_DIR_IN;
562 case XFRM_POLICY_OUT:
563 return FLOW_DIR_OUT;
564 case XFRM_POLICY_FWD:
565 return FLOW_DIR_FWD;
569 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl, u32 sk_sid)
571 struct xfrm_policy *pol;
573 read_lock_bh(&xfrm_policy_lock);
574 if ((pol = sk->sk_policy[dir]) != NULL) {
575 int match = xfrm_selector_match(&pol->selector, fl,
576 sk->sk_family);
577 int err = 0;
579 if (match)
580 err = security_xfrm_policy_lookup(pol, sk_sid, policy_to_flow_dir(dir));
582 if (match && !err)
583 xfrm_pol_hold(pol);
584 else
585 pol = NULL;
587 read_unlock_bh(&xfrm_policy_lock);
588 return pol;
591 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
593 pol->next = xfrm_policy_list[dir];
594 xfrm_policy_list[dir] = pol;
595 xfrm_pol_hold(pol);
598 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
599 int dir)
601 struct xfrm_policy **polp;
603 for (polp = &xfrm_policy_list[dir];
604 *polp != NULL; polp = &(*polp)->next) {
605 if (*polp == pol) {
606 *polp = pol->next;
607 return pol;
610 return NULL;
613 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
615 write_lock_bh(&xfrm_policy_lock);
616 pol = __xfrm_policy_unlink(pol, dir);
617 write_unlock_bh(&xfrm_policy_lock);
618 if (pol) {
619 if (dir < XFRM_POLICY_MAX)
620 atomic_inc(&flow_cache_genid);
621 xfrm_policy_kill(pol);
622 return 0;
624 return -ENOENT;
627 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
629 struct xfrm_policy *old_pol;
631 write_lock_bh(&xfrm_policy_lock);
632 old_pol = sk->sk_policy[dir];
633 sk->sk_policy[dir] = pol;
634 if (pol) {
635 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
636 pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
637 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
639 if (old_pol)
640 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
641 write_unlock_bh(&xfrm_policy_lock);
643 if (old_pol) {
644 xfrm_policy_kill(old_pol);
646 return 0;
649 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
651 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
653 if (newp) {
654 newp->selector = old->selector;
655 if (security_xfrm_policy_clone(old, newp)) {
656 kfree(newp);
657 return NULL; /* ENOMEM */
659 newp->lft = old->lft;
660 newp->curlft = old->curlft;
661 newp->action = old->action;
662 newp->flags = old->flags;
663 newp->xfrm_nr = old->xfrm_nr;
664 newp->index = old->index;
665 memcpy(newp->xfrm_vec, old->xfrm_vec,
666 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
667 write_lock_bh(&xfrm_policy_lock);
668 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
669 write_unlock_bh(&xfrm_policy_lock);
670 xfrm_pol_put(newp);
672 return newp;
675 int __xfrm_sk_clone_policy(struct sock *sk)
677 struct xfrm_policy *p0 = sk->sk_policy[0],
678 *p1 = sk->sk_policy[1];
680 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
681 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
682 return -ENOMEM;
683 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
684 return -ENOMEM;
685 return 0;
688 /* Resolve list of templates for the flow, given policy. */
690 static int
691 xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
692 struct xfrm_state **xfrm,
693 unsigned short family)
695 int nx;
696 int i, error;
697 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
698 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
700 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
701 struct xfrm_state *x;
702 xfrm_address_t *remote = daddr;
703 xfrm_address_t *local = saddr;
704 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
706 if (tmpl->mode) {
707 remote = &tmpl->id.daddr;
708 local = &tmpl->saddr;
711 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
713 if (x && x->km.state == XFRM_STATE_VALID) {
714 xfrm[nx++] = x;
715 daddr = remote;
716 saddr = local;
717 continue;
719 if (x) {
720 error = (x->km.state == XFRM_STATE_ERROR ?
721 -EINVAL : -EAGAIN);
722 xfrm_state_put(x);
725 if (!tmpl->optional)
726 goto fail;
728 return nx;
730 fail:
731 for (nx--; nx>=0; nx--)
732 xfrm_state_put(xfrm[nx]);
733 return error;
736 /* Check that the bundle accepts the flow and its components are
737 * still valid.
740 static struct dst_entry *
741 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
743 struct dst_entry *x;
744 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
745 if (unlikely(afinfo == NULL))
746 return ERR_PTR(-EINVAL);
747 x = afinfo->find_bundle(fl, policy);
748 xfrm_policy_put_afinfo(afinfo);
749 return x;
752 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
753 * all the metrics... Shortly, bundle a bundle.
756 static int
757 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
758 struct flowi *fl, struct dst_entry **dst_p,
759 unsigned short family)
761 int err;
762 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
763 if (unlikely(afinfo == NULL))
764 return -EINVAL;
765 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
766 xfrm_policy_put_afinfo(afinfo);
767 return err;
771 static int stale_bundle(struct dst_entry *dst);
773 /* Main function: finds/creates a bundle for given flow.
775 * At the moment we eat a raw IP route. Mostly to speed up lookups
776 * on interfaces with disabled IPsec.
778 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
779 struct sock *sk, int flags)
781 struct xfrm_policy *policy;
782 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
783 struct dst_entry *dst, *dst_orig = *dst_p;
784 int nx = 0;
785 int err;
786 u32 genid;
787 u16 family = dst_orig->ops->family;
788 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
789 u32 sk_sid = security_sk_sid(sk, fl, dir);
790 restart:
791 genid = atomic_read(&flow_cache_genid);
792 policy = NULL;
793 if (sk && sk->sk_policy[1])
794 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, sk_sid);
796 if (!policy) {
797 /* To accelerate a bit... */
798 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
799 return 0;
801 policy = flow_cache_lookup(fl, sk_sid, family, dir,
802 xfrm_policy_lookup);
805 if (!policy)
806 return 0;
808 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
810 switch (policy->action) {
811 case XFRM_POLICY_BLOCK:
812 /* Prohibit the flow */
813 err = -EPERM;
814 goto error;
816 case XFRM_POLICY_ALLOW:
817 if (policy->xfrm_nr == 0) {
818 /* Flow passes not transformed. */
819 xfrm_pol_put(policy);
820 return 0;
823 /* Try to find matching bundle.
825 * LATER: help from flow cache. It is optional, this
826 * is required only for output policy.
828 dst = xfrm_find_bundle(fl, policy, family);
829 if (IS_ERR(dst)) {
830 err = PTR_ERR(dst);
831 goto error;
834 if (dst)
835 break;
837 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
839 if (unlikely(nx<0)) {
840 err = nx;
841 if (err == -EAGAIN && flags) {
842 DECLARE_WAITQUEUE(wait, current);
844 add_wait_queue(&km_waitq, &wait);
845 set_current_state(TASK_INTERRUPTIBLE);
846 schedule();
847 set_current_state(TASK_RUNNING);
848 remove_wait_queue(&km_waitq, &wait);
850 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
852 if (nx == -EAGAIN && signal_pending(current)) {
853 err = -ERESTART;
854 goto error;
856 if (nx == -EAGAIN ||
857 genid != atomic_read(&flow_cache_genid)) {
858 xfrm_pol_put(policy);
859 goto restart;
861 err = nx;
863 if (err < 0)
864 goto error;
866 if (nx == 0) {
867 /* Flow passes not transformed. */
868 xfrm_pol_put(policy);
869 return 0;
872 dst = dst_orig;
873 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
875 if (unlikely(err)) {
876 int i;
877 for (i=0; i<nx; i++)
878 xfrm_state_put(xfrm[i]);
879 goto error;
882 write_lock_bh(&policy->lock);
883 if (unlikely(policy->dead || stale_bundle(dst))) {
884 /* Wow! While we worked on resolving, this
885 * policy has gone. Retry. It is not paranoia,
886 * we just cannot enlist new bundle to dead object.
887 * We can't enlist stable bundles either.
889 write_unlock_bh(&policy->lock);
891 xfrm_pol_put(policy);
892 if (dst)
893 dst_free(dst);
894 goto restart;
896 dst->next = policy->bundles;
897 policy->bundles = dst;
898 dst_hold(dst);
899 write_unlock_bh(&policy->lock);
901 *dst_p = dst;
902 dst_release(dst_orig);
903 xfrm_pol_put(policy);
904 return 0;
906 error:
907 dst_release(dst_orig);
908 xfrm_pol_put(policy);
909 *dst_p = NULL;
910 return err;
912 EXPORT_SYMBOL(xfrm_lookup);
914 /* When skb is transformed back to its "native" form, we have to
915 * check policy restrictions. At the moment we make this in maximally
916 * stupid way. Shame on me. :-) Of course, connected sockets must
917 * have policy cached at them.
920 static inline int
921 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
922 unsigned short family)
924 if (xfrm_state_kern(x))
925 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
926 return x->id.proto == tmpl->id.proto &&
927 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
928 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
929 x->props.mode == tmpl->mode &&
930 (tmpl->aalgos & (1<<x->props.aalgo)) &&
931 !(x->props.mode && xfrm_state_addr_cmp(tmpl, x, family));
934 static inline int
935 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
936 unsigned short family)
938 int idx = start;
940 if (tmpl->optional) {
941 if (!tmpl->mode)
942 return start;
943 } else
944 start = -1;
945 for (; idx < sp->len; idx++) {
946 if (xfrm_state_ok(tmpl, sp->x[idx].xvec, family))
947 return ++idx;
948 if (sp->x[idx].xvec->props.mode)
949 break;
951 return start;
954 static int
955 _decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
957 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
959 if (unlikely(afinfo == NULL))
960 return -EAFNOSUPPORT;
962 afinfo->decode_session(skb, fl);
963 xfrm_policy_put_afinfo(afinfo);
964 return 0;
967 static inline int secpath_has_tunnel(struct sec_path *sp, int k)
969 for (; k < sp->len; k++) {
970 if (sp->x[k].xvec->props.mode)
971 return 1;
974 return 0;
977 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
978 unsigned short family)
980 struct xfrm_policy *pol;
981 struct flowi fl;
982 u8 fl_dir = policy_to_flow_dir(dir);
983 u32 sk_sid;
985 if (_decode_session(skb, &fl, family) < 0)
986 return 0;
988 sk_sid = security_sk_sid(sk, &fl, fl_dir);
990 /* First, check used SA against their selectors. */
991 if (skb->sp) {
992 int i;
994 for (i=skb->sp->len-1; i>=0; i--) {
995 struct sec_decap_state *xvec = &(skb->sp->x[i]);
996 if (!xfrm_selector_match(&xvec->xvec->sel, &fl, family))
997 return 0;
999 /* If there is a post_input processor, try running it */
1000 if (xvec->xvec->type->post_input &&
1001 (xvec->xvec->type->post_input)(xvec->xvec,
1002 &(xvec->decap),
1003 skb) != 0)
1004 return 0;
1008 pol = NULL;
1009 if (sk && sk->sk_policy[dir])
1010 pol = xfrm_sk_policy_lookup(sk, dir, &fl, sk_sid);
1012 if (!pol)
1013 pol = flow_cache_lookup(&fl, sk_sid, family, fl_dir,
1014 xfrm_policy_lookup);
1016 if (!pol)
1017 return !skb->sp || !secpath_has_tunnel(skb->sp, 0);
1019 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1021 if (pol->action == XFRM_POLICY_ALLOW) {
1022 struct sec_path *sp;
1023 static struct sec_path dummy;
1024 int i, k;
1026 if ((sp = skb->sp) == NULL)
1027 sp = &dummy;
1029 /* For each tunnel xfrm, find the first matching tmpl.
1030 * For each tmpl before that, find corresponding xfrm.
1031 * Order is _important_. Later we will implement
1032 * some barriers, but at the moment barriers
1033 * are implied between each two transformations.
1035 for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
1036 k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
1037 if (k < 0)
1038 goto reject;
1041 if (secpath_has_tunnel(sp, k))
1042 goto reject;
1044 xfrm_pol_put(pol);
1045 return 1;
1048 reject:
1049 xfrm_pol_put(pol);
1050 return 0;
1052 EXPORT_SYMBOL(__xfrm_policy_check);
1054 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1056 struct flowi fl;
1058 if (_decode_session(skb, &fl, family) < 0)
1059 return 0;
1061 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1063 EXPORT_SYMBOL(__xfrm_route_forward);
1065 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1067 /* If it is marked obsolete, which is how we even get here,
1068 * then we have purged it from the policy bundle list and we
1069 * did that for a good reason.
1071 return NULL;
1074 static int stale_bundle(struct dst_entry *dst)
1076 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC);
1079 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1081 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1082 dst->dev = &loopback_dev;
1083 dev_hold(&loopback_dev);
1084 dev_put(dev);
1087 EXPORT_SYMBOL(xfrm_dst_ifdown);
1089 static void xfrm_link_failure(struct sk_buff *skb)
1091 /* Impossible. Such dst must be popped before reaches point of failure. */
1092 return;
1095 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1097 if (dst) {
1098 if (dst->obsolete) {
1099 dst_release(dst);
1100 dst = NULL;
1103 return dst;
1106 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1108 int i;
1109 struct xfrm_policy *pol;
1110 struct dst_entry *dst, **dstp, *gc_list = NULL;
1112 read_lock_bh(&xfrm_policy_lock);
1113 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
1114 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1115 write_lock(&pol->lock);
1116 dstp = &pol->bundles;
1117 while ((dst=*dstp) != NULL) {
1118 if (func(dst)) {
1119 *dstp = dst->next;
1120 dst->next = gc_list;
1121 gc_list = dst;
1122 } else {
1123 dstp = &dst->next;
1126 write_unlock(&pol->lock);
1129 read_unlock_bh(&xfrm_policy_lock);
1131 while (gc_list) {
1132 dst = gc_list;
1133 gc_list = dst->next;
1134 dst_free(dst);
1138 static int unused_bundle(struct dst_entry *dst)
1140 return !atomic_read(&dst->__refcnt);
1143 static void __xfrm_garbage_collect(void)
1145 xfrm_prune_bundles(unused_bundle);
1148 int xfrm_flush_bundles(void)
1150 xfrm_prune_bundles(stale_bundle);
1151 return 0;
1154 static int always_true(struct dst_entry *dst)
1156 return 1;
1159 void xfrm_flush_all_bundles(void)
1161 xfrm_prune_bundles(always_true);
1164 void xfrm_init_pmtu(struct dst_entry *dst)
1166 do {
1167 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1168 u32 pmtu, route_mtu_cached;
1170 pmtu = dst_mtu(dst->child);
1171 xdst->child_mtu_cached = pmtu;
1173 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1175 route_mtu_cached = dst_mtu(xdst->route);
1176 xdst->route_mtu_cached = route_mtu_cached;
1178 if (pmtu > route_mtu_cached)
1179 pmtu = route_mtu_cached;
1181 dst->metrics[RTAX_MTU-1] = pmtu;
1182 } while ((dst = dst->next));
1185 EXPORT_SYMBOL(xfrm_init_pmtu);
1187 /* Check that the bundle accepts the flow and its components are
1188 * still valid.
1191 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family)
1193 struct dst_entry *dst = &first->u.dst;
1194 struct xfrm_dst *last;
1195 u32 mtu;
1197 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1198 (dst->dev && !netif_running(dst->dev)))
1199 return 0;
1201 last = NULL;
1203 do {
1204 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1206 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1207 return 0;
1208 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1209 return 0;
1211 mtu = dst_mtu(dst->child);
1212 if (xdst->child_mtu_cached != mtu) {
1213 last = xdst;
1214 xdst->child_mtu_cached = mtu;
1217 if (!dst_check(xdst->route, xdst->route_cookie))
1218 return 0;
1219 mtu = dst_mtu(xdst->route);
1220 if (xdst->route_mtu_cached != mtu) {
1221 last = xdst;
1222 xdst->route_mtu_cached = mtu;
1225 dst = dst->child;
1226 } while (dst->xfrm);
1228 if (likely(!last))
1229 return 1;
1231 mtu = last->child_mtu_cached;
1232 for (;;) {
1233 dst = &last->u.dst;
1235 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1236 if (mtu > last->route_mtu_cached)
1237 mtu = last->route_mtu_cached;
1238 dst->metrics[RTAX_MTU-1] = mtu;
1240 if (last == first)
1241 break;
1243 last = last->u.next;
1244 last->child_mtu_cached = mtu;
1247 return 1;
1250 EXPORT_SYMBOL(xfrm_bundle_ok);
1252 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1254 int err = 0;
1255 if (unlikely(afinfo == NULL))
1256 return -EINVAL;
1257 if (unlikely(afinfo->family >= NPROTO))
1258 return -EAFNOSUPPORT;
1259 write_lock(&xfrm_policy_afinfo_lock);
1260 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1261 err = -ENOBUFS;
1262 else {
1263 struct dst_ops *dst_ops = afinfo->dst_ops;
1264 if (likely(dst_ops->kmem_cachep == NULL))
1265 dst_ops->kmem_cachep = xfrm_dst_cache;
1266 if (likely(dst_ops->check == NULL))
1267 dst_ops->check = xfrm_dst_check;
1268 if (likely(dst_ops->negative_advice == NULL))
1269 dst_ops->negative_advice = xfrm_negative_advice;
1270 if (likely(dst_ops->link_failure == NULL))
1271 dst_ops->link_failure = xfrm_link_failure;
1272 if (likely(afinfo->garbage_collect == NULL))
1273 afinfo->garbage_collect = __xfrm_garbage_collect;
1274 xfrm_policy_afinfo[afinfo->family] = afinfo;
1276 write_unlock(&xfrm_policy_afinfo_lock);
1277 return err;
1279 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1281 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1283 int err = 0;
1284 if (unlikely(afinfo == NULL))
1285 return -EINVAL;
1286 if (unlikely(afinfo->family >= NPROTO))
1287 return -EAFNOSUPPORT;
1288 write_lock(&xfrm_policy_afinfo_lock);
1289 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1290 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1291 err = -EINVAL;
1292 else {
1293 struct dst_ops *dst_ops = afinfo->dst_ops;
1294 xfrm_policy_afinfo[afinfo->family] = NULL;
1295 dst_ops->kmem_cachep = NULL;
1296 dst_ops->check = NULL;
1297 dst_ops->negative_advice = NULL;
1298 dst_ops->link_failure = NULL;
1299 afinfo->garbage_collect = NULL;
1302 write_unlock(&xfrm_policy_afinfo_lock);
1303 return err;
1305 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1307 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1309 struct xfrm_policy_afinfo *afinfo;
1310 if (unlikely(family >= NPROTO))
1311 return NULL;
1312 read_lock(&xfrm_policy_afinfo_lock);
1313 afinfo = xfrm_policy_afinfo[family];
1314 if (likely(afinfo != NULL))
1315 read_lock(&afinfo->lock);
1316 read_unlock(&xfrm_policy_afinfo_lock);
1317 return afinfo;
1320 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1322 if (unlikely(afinfo == NULL))
1323 return;
1324 read_unlock(&afinfo->lock);
1327 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1329 switch (event) {
1330 case NETDEV_DOWN:
1331 xfrm_flush_bundles();
1333 return NOTIFY_DONE;
1336 static struct notifier_block xfrm_dev_notifier = {
1337 xfrm_dev_event,
1338 NULL,
1342 static void __init xfrm_policy_init(void)
1344 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1345 sizeof(struct xfrm_dst),
1346 0, SLAB_HWCACHE_ALIGN,
1347 NULL, NULL);
1348 if (!xfrm_dst_cache)
1349 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1351 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1352 register_netdevice_notifier(&xfrm_dev_notifier);
1355 void __init xfrm_init(void)
1357 xfrm_state_init();
1358 xfrm_policy_init();
1359 xfrm_input_init();