[PATCH] VT binding: Make newport_con support binding
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_policy.c
blobb8936926c24b84dba699540b54b0630f2002d439
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 <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>
26 #include <net/xfrm.h>
27 #include <net/ip.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;
56 int err = 0;
58 if (unlikely(afinfo == NULL))
59 return -EAFNOSUPPORT;
60 typemap = afinfo->type_map;
62 if (likely(typemap[type->proto] == NULL))
63 typemap[type->proto] = type;
64 else
65 err = -EEXIST;
66 xfrm_policy_unlock_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_lock_afinfo(family);
74 struct xfrm_type **typemap;
75 int err = 0;
77 if (unlikely(afinfo == NULL))
78 return -EAFNOSUPPORT;
79 typemap = afinfo->type_map;
81 if (unlikely(typemap[type->proto] != type))
82 err = -ENOENT;
83 else
84 typemap[type->proto] = NULL;
85 xfrm_policy_unlock_afinfo(afinfo);
86 return err;
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;
97 retry:
98 afinfo = xfrm_policy_get_afinfo(family);
99 if (unlikely(afinfo == NULL))
100 return NULL;
101 typemap = afinfo->type_map;
103 type = typemap[proto];
104 if (unlikely(type && !try_module_get(type->owner)))
105 type = NULL;
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;
111 goto retry;
114 xfrm_policy_put_afinfo(afinfo);
115 return type;
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);
122 int err = 0;
124 if (unlikely(afinfo == NULL))
125 return -EAFNOSUPPORT;
127 if (likely(afinfo->dst_lookup != NULL))
128 err = afinfo->dst_lookup(dst, fl);
129 else
130 err = -EINVAL;
131 xfrm_policy_put_afinfo(afinfo);
132 return err;
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;
145 int err;
147 if (unlikely(mode->encap >= XFRM_MODE_MAX))
148 return -EINVAL;
150 afinfo = xfrm_policy_lock_afinfo(family);
151 if (unlikely(afinfo == NULL))
152 return -EAFNOSUPPORT;
154 err = -EEXIST;
155 modemap = afinfo->mode_map;
156 if (likely(modemap[mode->encap] == NULL)) {
157 modemap[mode->encap] = mode;
158 err = 0;
161 xfrm_policy_unlock_afinfo(afinfo);
162 return err;
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;
170 int err;
172 if (unlikely(mode->encap >= XFRM_MODE_MAX))
173 return -EINVAL;
175 afinfo = xfrm_policy_lock_afinfo(family);
176 if (unlikely(afinfo == NULL))
177 return -EAFNOSUPPORT;
179 err = -ENOENT;
180 modemap = afinfo->mode_map;
181 if (likely(modemap[mode->encap] == mode)) {
182 modemap[mode->encap] = NULL;
183 err = 0;
186 xfrm_policy_unlock_afinfo(afinfo);
187 return err;
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))
198 return NULL;
200 retry:
201 afinfo = xfrm_policy_get_afinfo(family);
202 if (unlikely(afinfo == NULL))
203 return NULL;
205 mode = afinfo->mode_map[encap];
206 if (unlikely(mode && !try_module_get(mode->owner)))
207 mode = NULL;
208 if (!mode && !modload_attempted) {
209 xfrm_policy_put_afinfo(afinfo);
210 request_module("xfrm-mode-%d-%d", family, encap);
211 modload_attempted = 1;
212 goto retry;
215 xfrm_policy_put_afinfo(afinfo);
216 return mode;
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;
228 else
229 return secs*HZ;
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;
237 int warn = 0;
238 int dir;
240 read_lock(&xp->lock);
242 if (xp->dead)
243 goto out;
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;
250 if (tmo <= 0)
251 goto expired;
252 if (tmo < next)
253 next = tmo;
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;
258 if (tmo <= 0)
259 goto expired;
260 if (tmo < next)
261 next = tmo;
263 if (xp->lft.soft_add_expires_seconds) {
264 long tmo = xp->lft.soft_add_expires_seconds +
265 xp->curlft.add_time - now;
266 if (tmo <= 0) {
267 warn = 1;
268 tmo = XFRM_KM_TIMEOUT;
270 if (tmo < next)
271 next = tmo;
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;
276 if (tmo <= 0) {
277 warn = 1;
278 tmo = XFRM_KM_TIMEOUT;
280 if (tmo < next)
281 next = tmo;
284 if (warn)
285 km_policy_expired(xp, dir, 0, 0);
286 if (next != LONG_MAX &&
287 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
288 xfrm_pol_hold(xp);
290 out:
291 read_unlock(&xp->lock);
292 xfrm_pol_put(xp);
293 return;
295 expired:
296 read_unlock(&xp->lock);
297 if (!xfrm_policy_delete(xp, dir))
298 km_policy_expired(xp, dir, 1, 0);
299 xfrm_pol_put(xp);
303 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
304 * SPD calls.
307 struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
309 struct xfrm_policy *policy;
311 policy = kmalloc(sizeof(struct xfrm_policy), gfp);
313 if (policy) {
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;
321 return policy;
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))
334 BUG();
336 security_xfrm_policy_free(policy);
337 kfree(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;
347 dst_free(dst);
350 if (del_timer(&policy->timer))
351 atomic_dec(&policy->refcnt);
353 if (atomic_read(&policy->refcnt) > 1)
354 flow_cache_flush();
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)
381 int dead;
383 write_lock_bh(&policy->lock);
384 dead = policy->dead;
385 policy->dead = 1;
386 write_unlock_bh(&policy->lock);
388 if (unlikely(dead)) {
389 WARN_ON(1);
390 return;
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)
404 u32 idx;
405 struct xfrm_policy *p;
406 static u32 idx_generator;
408 for (;;) {
409 idx = (idx_generator | dir);
410 idx_generator += 8;
411 if (idx == 0)
412 idx = 8;
413 for (p = xfrm_policy_list[dir]; p; p = p->next) {
414 if (p->index == idx)
415 break;
417 if (!p)
418 return idx;
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)) {
433 if (excl) {
434 write_unlock_bh(&xfrm_policy_lock);
435 return -EEXIST;
437 *p = pol->next;
438 delpol = pol;
439 if (policy->priority > pol->priority)
440 continue;
441 } else if (policy->priority >= pol->priority) {
442 p = &pol->next;
443 continue;
445 if (!newpos)
446 newpos = p;
447 if (delpol)
448 break;
449 p = &pol->next;
451 if (newpos)
452 p = newpos;
453 xfrm_pol_hold(policy);
454 policy->next = *p;
455 *p = 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);
464 if (delpol)
465 xfrm_policy_kill(delpol);
467 read_lock_bh(&xfrm_policy_lock);
468 gc_list = NULL;
469 for (policy = policy->next; policy; policy = policy->next) {
470 struct dst_entry *dst;
472 write_lock(&policy->lock);
473 dst = policy->bundles;
474 if (dst) {
475 struct dst_entry *tail = dst;
476 while (tail->next)
477 tail = tail->next;
478 tail->next = gc_list;
479 gc_list = dst;
481 policy->bundles = NULL;
483 write_unlock(&policy->lock);
485 read_unlock_bh(&xfrm_policy_lock);
487 while (gc_list) {
488 struct dst_entry *dst = gc_list;
490 gc_list = dst->next;
491 dst_free(dst);
494 return 0;
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))) {
507 xfrm_pol_hold(pol);
508 if (delete)
509 *p = pol->next;
510 break;
513 write_unlock_bh(&xfrm_policy_lock);
515 if (pol && delete) {
516 atomic_inc(&flow_cache_genid);
517 xfrm_policy_kill(pol);
519 return 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) {
530 xfrm_pol_hold(pol);
531 if (delete)
532 *p = pol->next;
533 break;
536 write_unlock_bh(&xfrm_policy_lock);
538 if (pol && delete) {
539 atomic_inc(&flow_cache_genid);
540 xfrm_policy_kill(pol);
542 return pol;
544 EXPORT_SYMBOL(xfrm_policy_byid);
546 void xfrm_policy_flush(void)
548 struct xfrm_policy *xp;
549 int dir;
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*),
568 void *data)
570 struct xfrm_policy *xp;
571 int dir;
572 int count = 0;
573 int error = 0;
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)
578 count++;
581 if (count == 0) {
582 error = -ENOENT;
583 goto out;
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);
589 if (error)
590 goto out;
594 out:
595 read_unlock_bh(&xfrm_policy_lock);
596 return error;
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;
610 int match;
612 if (pol->family != family)
613 continue;
615 match = xfrm_selector_match(sel, fl, family);
617 if (match) {
618 if (!security_xfrm_policy_lookup(pol, sk_sid, dir)) {
619 xfrm_pol_hold(pol);
620 break;
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)
634 return dir;
635 switch (dir) {
636 default:
637 case XFRM_POLICY_IN:
638 return FLOW_DIR_IN;
639 case XFRM_POLICY_OUT:
640 return FLOW_DIR_OUT;
641 case XFRM_POLICY_FWD:
642 return FLOW_DIR_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,
653 sk->sk_family);
654 int err = 0;
656 if (match)
657 err = security_xfrm_policy_lookup(pol, sk_sid, policy_to_flow_dir(dir));
659 if (match && !err)
660 xfrm_pol_hold(pol);
661 else
662 pol = NULL;
664 read_unlock_bh(&xfrm_policy_lock);
665 return pol;
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;
672 xfrm_pol_hold(pol);
675 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
676 int dir)
678 struct xfrm_policy **polp;
680 for (polp = &xfrm_policy_list[dir];
681 *polp != NULL; polp = &(*polp)->next) {
682 if (*polp == pol) {
683 *polp = pol->next;
684 return pol;
687 return NULL;
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);
695 if (pol) {
696 if (dir < XFRM_POLICY_MAX)
697 atomic_inc(&flow_cache_genid);
698 xfrm_policy_kill(pol);
699 return 0;
701 return -ENOENT;
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;
712 if (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);
717 if (old_pol)
718 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
719 write_unlock_bh(&xfrm_policy_lock);
721 if (old_pol) {
722 xfrm_policy_kill(old_pol);
724 return 0;
727 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
729 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
731 if (newp) {
732 newp->selector = old->selector;
733 if (security_xfrm_policy_clone(old, newp)) {
734 kfree(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);
748 xfrm_pol_put(newp);
750 return newp;
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)
760 return -ENOMEM;
761 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
762 return -ENOMEM;
763 return 0;
766 /* Resolve list of templates for the flow, given policy. */
768 static int
769 xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
770 struct xfrm_state **xfrm,
771 unsigned short family)
773 int nx;
774 int i, error;
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];
784 if (tmpl->mode) {
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) {
792 xfrm[nx++] = x;
793 daddr = remote;
794 saddr = local;
795 continue;
797 if (x) {
798 error = (x->km.state == XFRM_STATE_ERROR ?
799 -EINVAL : -EAGAIN);
800 xfrm_state_put(x);
803 if (!tmpl->optional)
804 goto fail;
806 return nx;
808 fail:
809 for (nx--; nx>=0; nx--)
810 xfrm_state_put(xfrm[nx]);
811 return error;
814 /* Check that the bundle accepts the flow and its components are
815 * still valid.
818 static struct dst_entry *
819 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
821 struct dst_entry *x;
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);
827 return x;
830 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
831 * all the metrics... Shortly, bundle a bundle.
834 static int
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)
839 int err;
840 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
841 if (unlikely(afinfo == NULL))
842 return -EINVAL;
843 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
844 xfrm_policy_put_afinfo(afinfo);
845 return err;
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;
862 int nx = 0;
863 int err;
864 u32 genid;
865 u16 family;
866 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
867 u32 sk_sid = security_sk_sid(sk, fl, dir);
868 restart:
869 genid = atomic_read(&flow_cache_genid);
870 policy = NULL;
871 if (sk && sk->sk_policy[1])
872 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, sk_sid);
874 if (!policy) {
875 /* To accelerate a bit... */
876 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
877 return 0;
879 policy = flow_cache_lookup(fl, sk_sid, dst_orig->ops->family,
880 dir, xfrm_policy_lookup);
883 if (!policy)
884 return 0;
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 */
892 err = -EPERM;
893 goto error;
895 case XFRM_POLICY_ALLOW:
896 if (policy->xfrm_nr == 0) {
897 /* Flow passes not transformed. */
898 xfrm_pol_put(policy);
899 return 0;
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);
908 if (IS_ERR(dst)) {
909 err = PTR_ERR(dst);
910 goto error;
913 if (dst)
914 break;
916 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
918 if (unlikely(nx<0)) {
919 err = nx;
920 if (err == -EAGAIN && flags) {
921 DECLARE_WAITQUEUE(wait, current);
923 add_wait_queue(&km_waitq, &wait);
924 set_current_state(TASK_INTERRUPTIBLE);
925 schedule();
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)) {
932 err = -ERESTART;
933 goto error;
935 if (nx == -EAGAIN ||
936 genid != atomic_read(&flow_cache_genid)) {
937 xfrm_pol_put(policy);
938 goto restart;
940 err = nx;
942 if (err < 0)
943 goto error;
945 if (nx == 0) {
946 /* Flow passes not transformed. */
947 xfrm_pol_put(policy);
948 return 0;
951 dst = dst_orig;
952 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
954 if (unlikely(err)) {
955 int i;
956 for (i=0; i<nx; i++)
957 xfrm_state_put(xfrm[i]);
958 goto error;
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);
969 if (dst)
970 dst_free(dst);
972 err = -EHOSTUNREACH;
973 goto error;
975 dst->next = policy->bundles;
976 policy->bundles = dst;
977 dst_hold(dst);
978 write_unlock_bh(&policy->lock);
980 *dst_p = dst;
981 dst_release(dst_orig);
982 xfrm_pol_put(policy);
983 return 0;
985 error:
986 dst_release(dst_orig);
987 xfrm_pol_put(policy);
988 *dst_p = NULL;
989 return err;
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.
999 static inline int
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));
1013 static inline int
1014 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1015 unsigned short family)
1017 int idx = start;
1019 if (tmpl->optional) {
1020 if (!tmpl->mode)
1021 return start;
1022 } else
1023 start = -1;
1024 for (; idx < sp->len; idx++) {
1025 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1026 return ++idx;
1027 if (sp->xvec[idx]->props.mode)
1028 break;
1030 return start;
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);
1043 return 0;
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)
1051 return 1;
1054 return 0;
1057 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1058 unsigned short family)
1060 struct xfrm_policy *pol;
1061 struct flowi fl;
1062 u8 fl_dir = policy_to_flow_dir(dir);
1063 u32 sk_sid;
1065 if (xfrm_decode_session(skb, &fl, family) < 0)
1066 return 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. */
1072 if (skb->sp) {
1073 int i;
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))
1078 return 0;
1082 pol = NULL;
1083 if (sk && sk->sk_policy[dir])
1084 pol = xfrm_sk_policy_lookup(sk, dir, &fl, sk_sid);
1086 if (!pol)
1087 pol = flow_cache_lookup(&fl, sk_sid, family, fl_dir,
1088 xfrm_policy_lookup);
1090 if (!pol)
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;
1098 int i, k;
1100 if ((sp = skb->sp) == NULL)
1101 sp = &dummy;
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);
1111 if (k < 0)
1112 goto reject;
1115 if (secpath_has_tunnel(sp, k))
1116 goto reject;
1118 xfrm_pol_put(pol);
1119 return 1;
1122 reject:
1123 xfrm_pol_put(pol);
1124 return 0;
1126 EXPORT_SYMBOL(__xfrm_policy_check);
1128 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1130 struct flowi fl;
1132 if (xfrm_decode_session(skb, &fl, family) < 0)
1133 return 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.
1145 return NULL;
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);
1158 dev_put(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. */
1166 return;
1169 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1171 if (dst) {
1172 if (dst->obsolete) {
1173 dst_release(dst);
1174 dst = NULL;
1177 return dst;
1180 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1182 int i;
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) {
1192 if (func(dst)) {
1193 *dstp = dst->next;
1194 dst->next = gc_list;
1195 gc_list = dst;
1196 } else {
1197 dstp = &dst->next;
1200 write_unlock(&pol->lock);
1203 read_unlock_bh(&xfrm_policy_lock);
1205 while (gc_list) {
1206 dst = gc_list;
1207 gc_list = dst->next;
1208 dst_free(dst);
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);
1225 return 0;
1228 static int always_true(struct dst_entry *dst)
1230 return 1;
1233 void xfrm_flush_all_bundles(void)
1235 xfrm_prune_bundles(always_true);
1238 void xfrm_init_pmtu(struct dst_entry *dst)
1240 do {
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
1262 * still valid.
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;
1269 u32 mtu;
1271 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1272 (dst->dev && !netif_running(dst->dev)))
1273 return 0;
1275 last = NULL;
1277 do {
1278 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1280 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1281 return 0;
1282 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1283 return 0;
1285 mtu = dst_mtu(dst->child);
1286 if (xdst->child_mtu_cached != mtu) {
1287 last = xdst;
1288 xdst->child_mtu_cached = mtu;
1291 if (!dst_check(xdst->route, xdst->route_cookie))
1292 return 0;
1293 mtu = dst_mtu(xdst->route);
1294 if (xdst->route_mtu_cached != mtu) {
1295 last = xdst;
1296 xdst->route_mtu_cached = mtu;
1299 dst = dst->child;
1300 } while (dst->xfrm);
1302 if (likely(!last))
1303 return 1;
1305 mtu = last->child_mtu_cached;
1306 for (;;) {
1307 dst = &last->u.dst;
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;
1314 if (last == first)
1315 break;
1317 last = last->u.next;
1318 last->child_mtu_cached = mtu;
1321 return 1;
1324 EXPORT_SYMBOL(xfrm_bundle_ok);
1326 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1328 int err = 0;
1329 if (unlikely(afinfo == NULL))
1330 return -EINVAL;
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))
1335 err = -ENOBUFS;
1336 else {
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);
1351 return err;
1353 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1355 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1357 int err = 0;
1358 if (unlikely(afinfo == NULL))
1359 return -EINVAL;
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))
1365 err = -EINVAL;
1366 else {
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);
1377 return err;
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))
1385 return NULL;
1386 read_lock(&xfrm_policy_afinfo_lock);
1387 afinfo = xfrm_policy_afinfo[family];
1388 if (unlikely(!afinfo))
1389 read_unlock(&xfrm_policy_afinfo_lock);
1390 return afinfo;
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))
1402 return NULL;
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);
1407 return afinfo;
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)
1417 switch (event) {
1418 case NETDEV_DOWN:
1419 xfrm_flush_bundles();
1421 return NOTIFY_DONE;
1424 static struct notifier_block xfrm_dev_notifier = {
1425 xfrm_dev_event,
1426 NULL,
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,
1435 NULL, NULL);
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)
1445 xfrm_state_init();
1446 xfrm_policy_init();
1447 xfrm_input_init();