[IPSEC] Kill spurious hard expire messages
[linux-2.6/zen-sources.git] / net / xfrm / xfrm_policy.c
blob0a4260719a123113b03e0e7bfed04ea363ca655f
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;
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;
121 EXPORT_SYMBOL(xfrm_get_type);
123 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
124 unsigned short family)
126 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
127 int err = 0;
129 if (unlikely(afinfo == NULL))
130 return -EAFNOSUPPORT;
132 if (likely(afinfo->dst_lookup != NULL))
133 err = afinfo->dst_lookup(dst, fl);
134 else
135 err = -EINVAL;
136 xfrm_policy_put_afinfo(afinfo);
137 return err;
139 EXPORT_SYMBOL(xfrm_dst_lookup);
141 void xfrm_put_type(struct xfrm_type *type)
143 module_put(type->owner);
146 static inline unsigned long make_jiffies(long secs)
148 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
149 return MAX_SCHEDULE_TIMEOUT-1;
150 else
151 return secs*HZ;
154 static void xfrm_policy_timer(unsigned long data)
156 struct xfrm_policy *xp = (struct xfrm_policy*)data;
157 unsigned long now = (unsigned long)xtime.tv_sec;
158 long next = LONG_MAX;
159 int warn = 0;
160 int dir;
162 read_lock(&xp->lock);
164 if (xp->dead)
165 goto out;
167 dir = xp->index & 7;
169 if (xp->lft.hard_add_expires_seconds) {
170 long tmo = xp->lft.hard_add_expires_seconds +
171 xp->curlft.add_time - now;
172 if (tmo <= 0)
173 goto expired;
174 if (tmo < next)
175 next = tmo;
177 if (xp->lft.hard_use_expires_seconds) {
178 long tmo = xp->lft.hard_use_expires_seconds +
179 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
180 if (tmo <= 0)
181 goto expired;
182 if (tmo < next)
183 next = tmo;
185 if (xp->lft.soft_add_expires_seconds) {
186 long tmo = xp->lft.soft_add_expires_seconds +
187 xp->curlft.add_time - now;
188 if (tmo <= 0) {
189 warn = 1;
190 tmo = XFRM_KM_TIMEOUT;
192 if (tmo < next)
193 next = tmo;
195 if (xp->lft.soft_use_expires_seconds) {
196 long tmo = xp->lft.soft_use_expires_seconds +
197 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
198 if (tmo <= 0) {
199 warn = 1;
200 tmo = XFRM_KM_TIMEOUT;
202 if (tmo < next)
203 next = tmo;
206 if (warn)
207 km_policy_expired(xp, dir, 0);
208 if (next != LONG_MAX &&
209 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
210 xfrm_pol_hold(xp);
212 out:
213 read_unlock(&xp->lock);
214 xfrm_pol_put(xp);
215 return;
217 expired:
218 read_unlock(&xp->lock);
219 if (!xfrm_policy_delete(xp, dir))
220 km_policy_expired(xp, dir, 1);
221 xfrm_pol_put(xp);
225 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
226 * SPD calls.
229 struct xfrm_policy *xfrm_policy_alloc(int gfp)
231 struct xfrm_policy *policy;
233 policy = kmalloc(sizeof(struct xfrm_policy), gfp);
235 if (policy) {
236 memset(policy, 0, sizeof(struct xfrm_policy));
237 atomic_set(&policy->refcnt, 1);
238 rwlock_init(&policy->lock);
239 init_timer(&policy->timer);
240 policy->timer.data = (unsigned long)policy;
241 policy->timer.function = xfrm_policy_timer;
243 return policy;
245 EXPORT_SYMBOL(xfrm_policy_alloc);
247 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
249 void __xfrm_policy_destroy(struct xfrm_policy *policy)
251 if (!policy->dead)
252 BUG();
254 if (policy->bundles)
255 BUG();
257 if (del_timer(&policy->timer))
258 BUG();
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;
351 write_lock_bh(&xfrm_policy_lock);
352 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL;) {
353 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0) {
354 if (excl) {
355 write_unlock_bh(&xfrm_policy_lock);
356 return -EEXIST;
358 *p = pol->next;
359 delpol = pol;
360 if (policy->priority > pol->priority)
361 continue;
362 } else if (policy->priority >= pol->priority) {
363 p = &pol->next;
364 continue;
366 if (!newpos)
367 newpos = p;
368 if (delpol)
369 break;
370 p = &pol->next;
372 if (newpos)
373 p = newpos;
374 xfrm_pol_hold(policy);
375 policy->next = *p;
376 *p = policy;
377 atomic_inc(&flow_cache_genid);
378 policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
379 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
380 policy->curlft.use_time = 0;
381 if (!mod_timer(&policy->timer, jiffies + HZ))
382 xfrm_pol_hold(policy);
383 write_unlock_bh(&xfrm_policy_lock);
385 if (delpol) {
386 xfrm_policy_kill(delpol);
388 return 0;
390 EXPORT_SYMBOL(xfrm_policy_insert);
392 struct xfrm_policy *xfrm_policy_bysel(int dir, struct xfrm_selector *sel,
393 int delete)
395 struct xfrm_policy *pol, **p;
397 write_lock_bh(&xfrm_policy_lock);
398 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
399 if (memcmp(sel, &pol->selector, sizeof(*sel)) == 0) {
400 xfrm_pol_hold(pol);
401 if (delete)
402 *p = pol->next;
403 break;
406 write_unlock_bh(&xfrm_policy_lock);
408 if (pol && delete) {
409 atomic_inc(&flow_cache_genid);
410 xfrm_policy_kill(pol);
412 return pol;
414 EXPORT_SYMBOL(xfrm_policy_bysel);
416 struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
418 struct xfrm_policy *pol, **p;
420 write_lock_bh(&xfrm_policy_lock);
421 for (p = &xfrm_policy_list[id & 7]; (pol=*p)!=NULL; p = &pol->next) {
422 if (pol->index == id) {
423 xfrm_pol_hold(pol);
424 if (delete)
425 *p = pol->next;
426 break;
429 write_unlock_bh(&xfrm_policy_lock);
431 if (pol && delete) {
432 atomic_inc(&flow_cache_genid);
433 xfrm_policy_kill(pol);
435 return pol;
437 EXPORT_SYMBOL(xfrm_policy_byid);
439 void xfrm_policy_flush(void)
441 struct xfrm_policy *xp;
442 int dir;
444 write_lock_bh(&xfrm_policy_lock);
445 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
446 while ((xp = xfrm_policy_list[dir]) != NULL) {
447 xfrm_policy_list[dir] = xp->next;
448 write_unlock_bh(&xfrm_policy_lock);
450 xfrm_policy_kill(xp);
452 write_lock_bh(&xfrm_policy_lock);
455 atomic_inc(&flow_cache_genid);
456 write_unlock_bh(&xfrm_policy_lock);
458 EXPORT_SYMBOL(xfrm_policy_flush);
460 int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
461 void *data)
463 struct xfrm_policy *xp;
464 int dir;
465 int count = 0;
466 int error = 0;
468 read_lock_bh(&xfrm_policy_lock);
469 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
470 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
471 count++;
474 if (count == 0) {
475 error = -ENOENT;
476 goto out;
479 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
480 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
481 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
482 if (error)
483 goto out;
487 out:
488 read_unlock_bh(&xfrm_policy_lock);
489 return error;
491 EXPORT_SYMBOL(xfrm_policy_walk);
493 /* Find policy to apply to this flow. */
495 static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
496 void **objp, atomic_t **obj_refp)
498 struct xfrm_policy *pol;
500 read_lock_bh(&xfrm_policy_lock);
501 for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
502 struct xfrm_selector *sel = &pol->selector;
503 int match;
505 if (pol->family != family)
506 continue;
508 match = xfrm_selector_match(sel, fl, family);
509 if (match) {
510 xfrm_pol_hold(pol);
511 break;
514 read_unlock_bh(&xfrm_policy_lock);
515 if ((*objp = (void *) pol) != NULL)
516 *obj_refp = &pol->refcnt;
519 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
521 struct xfrm_policy *pol;
523 read_lock_bh(&xfrm_policy_lock);
524 if ((pol = sk->sk_policy[dir]) != NULL) {
525 int match = xfrm_selector_match(&pol->selector, fl,
526 sk->sk_family);
527 if (match)
528 xfrm_pol_hold(pol);
529 else
530 pol = NULL;
532 read_unlock_bh(&xfrm_policy_lock);
533 return pol;
536 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
538 pol->next = xfrm_policy_list[dir];
539 xfrm_policy_list[dir] = pol;
540 xfrm_pol_hold(pol);
543 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
544 int dir)
546 struct xfrm_policy **polp;
548 for (polp = &xfrm_policy_list[dir];
549 *polp != NULL; polp = &(*polp)->next) {
550 if (*polp == pol) {
551 *polp = pol->next;
552 return pol;
555 return NULL;
558 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
560 write_lock_bh(&xfrm_policy_lock);
561 pol = __xfrm_policy_unlink(pol, dir);
562 write_unlock_bh(&xfrm_policy_lock);
563 if (pol) {
564 if (dir < XFRM_POLICY_MAX)
565 atomic_inc(&flow_cache_genid);
566 xfrm_policy_kill(pol);
567 return 0;
569 return -ENOENT;
572 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
574 struct xfrm_policy *old_pol;
576 write_lock_bh(&xfrm_policy_lock);
577 old_pol = sk->sk_policy[dir];
578 sk->sk_policy[dir] = pol;
579 if (pol) {
580 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
581 pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
582 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
584 if (old_pol)
585 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
586 write_unlock_bh(&xfrm_policy_lock);
588 if (old_pol) {
589 xfrm_policy_kill(old_pol);
591 return 0;
594 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
596 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
598 if (newp) {
599 newp->selector = old->selector;
600 newp->lft = old->lft;
601 newp->curlft = old->curlft;
602 newp->action = old->action;
603 newp->flags = old->flags;
604 newp->xfrm_nr = old->xfrm_nr;
605 newp->index = old->index;
606 memcpy(newp->xfrm_vec, old->xfrm_vec,
607 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
608 write_lock_bh(&xfrm_policy_lock);
609 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
610 write_unlock_bh(&xfrm_policy_lock);
611 xfrm_pol_put(newp);
613 return newp;
616 int __xfrm_sk_clone_policy(struct sock *sk)
618 struct xfrm_policy *p0 = sk->sk_policy[0],
619 *p1 = sk->sk_policy[1];
621 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
622 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
623 return -ENOMEM;
624 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
625 return -ENOMEM;
626 return 0;
629 /* Resolve list of templates for the flow, given policy. */
631 static int
632 xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
633 struct xfrm_state **xfrm,
634 unsigned short family)
636 int nx;
637 int i, error;
638 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
639 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
641 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
642 struct xfrm_state *x;
643 xfrm_address_t *remote = daddr;
644 xfrm_address_t *local = saddr;
645 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
647 if (tmpl->mode) {
648 remote = &tmpl->id.daddr;
649 local = &tmpl->saddr;
652 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
654 if (x && x->km.state == XFRM_STATE_VALID) {
655 xfrm[nx++] = x;
656 daddr = remote;
657 saddr = local;
658 continue;
660 if (x) {
661 error = (x->km.state == XFRM_STATE_ERROR ?
662 -EINVAL : -EAGAIN);
663 xfrm_state_put(x);
666 if (!tmpl->optional)
667 goto fail;
669 return nx;
671 fail:
672 for (nx--; nx>=0; nx--)
673 xfrm_state_put(xfrm[nx]);
674 return error;
677 /* Check that the bundle accepts the flow and its components are
678 * still valid.
681 static struct dst_entry *
682 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
684 struct dst_entry *x;
685 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
686 if (unlikely(afinfo == NULL))
687 return ERR_PTR(-EINVAL);
688 x = afinfo->find_bundle(fl, policy);
689 xfrm_policy_put_afinfo(afinfo);
690 return x;
693 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
694 * all the metrics... Shortly, bundle a bundle.
697 static int
698 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
699 struct flowi *fl, struct dst_entry **dst_p,
700 unsigned short family)
702 int err;
703 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
704 if (unlikely(afinfo == NULL))
705 return -EINVAL;
706 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
707 xfrm_policy_put_afinfo(afinfo);
708 return err;
711 static inline int policy_to_flow_dir(int dir)
713 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
714 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
715 XFRM_POLICY_FWD == FLOW_DIR_FWD)
716 return dir;
717 switch (dir) {
718 default:
719 case XFRM_POLICY_IN:
720 return FLOW_DIR_IN;
721 case XFRM_POLICY_OUT:
722 return FLOW_DIR_OUT;
723 case XFRM_POLICY_FWD:
724 return FLOW_DIR_FWD;
728 static int stale_bundle(struct dst_entry *dst);
730 /* Main function: finds/creates a bundle for given flow.
732 * At the moment we eat a raw IP route. Mostly to speed up lookups
733 * on interfaces with disabled IPsec.
735 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
736 struct sock *sk, int flags)
738 struct xfrm_policy *policy;
739 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
740 struct dst_entry *dst, *dst_orig = *dst_p;
741 int nx = 0;
742 int err;
743 u32 genid;
744 u16 family = dst_orig->ops->family;
745 restart:
746 genid = atomic_read(&flow_cache_genid);
747 policy = NULL;
748 if (sk && sk->sk_policy[1])
749 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
751 if (!policy) {
752 /* To accelerate a bit... */
753 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
754 return 0;
756 policy = flow_cache_lookup(fl, family,
757 policy_to_flow_dir(XFRM_POLICY_OUT),
758 xfrm_policy_lookup);
761 if (!policy)
762 return 0;
764 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
766 switch (policy->action) {
767 case XFRM_POLICY_BLOCK:
768 /* Prohibit the flow */
769 xfrm_pol_put(policy);
770 return -EPERM;
772 case XFRM_POLICY_ALLOW:
773 if (policy->xfrm_nr == 0) {
774 /* Flow passes not transformed. */
775 xfrm_pol_put(policy);
776 return 0;
779 /* Try to find matching bundle.
781 * LATER: help from flow cache. It is optional, this
782 * is required only for output policy.
784 dst = xfrm_find_bundle(fl, policy, family);
785 if (IS_ERR(dst)) {
786 xfrm_pol_put(policy);
787 return PTR_ERR(dst);
790 if (dst)
791 break;
793 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
795 if (unlikely(nx<0)) {
796 err = nx;
797 if (err == -EAGAIN && flags) {
798 DECLARE_WAITQUEUE(wait, current);
800 add_wait_queue(&km_waitq, &wait);
801 set_current_state(TASK_INTERRUPTIBLE);
802 schedule();
803 set_current_state(TASK_RUNNING);
804 remove_wait_queue(&km_waitq, &wait);
806 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
808 if (nx == -EAGAIN && signal_pending(current)) {
809 err = -ERESTART;
810 goto error;
812 if (nx == -EAGAIN ||
813 genid != atomic_read(&flow_cache_genid)) {
814 xfrm_pol_put(policy);
815 goto restart;
817 err = nx;
819 if (err < 0)
820 goto error;
822 if (nx == 0) {
823 /* Flow passes not transformed. */
824 xfrm_pol_put(policy);
825 return 0;
828 dst = dst_orig;
829 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
831 if (unlikely(err)) {
832 int i;
833 for (i=0; i<nx; i++)
834 xfrm_state_put(xfrm[i]);
835 goto error;
838 write_lock_bh(&policy->lock);
839 if (unlikely(policy->dead || stale_bundle(dst))) {
840 /* Wow! While we worked on resolving, this
841 * policy has gone. Retry. It is not paranoia,
842 * we just cannot enlist new bundle to dead object.
843 * We can't enlist stable bundles either.
845 write_unlock_bh(&policy->lock);
847 xfrm_pol_put(policy);
848 if (dst)
849 dst_free(dst);
850 goto restart;
852 dst->next = policy->bundles;
853 policy->bundles = dst;
854 dst_hold(dst);
855 write_unlock_bh(&policy->lock);
857 *dst_p = dst;
858 dst_release(dst_orig);
859 xfrm_pol_put(policy);
860 return 0;
862 error:
863 dst_release(dst_orig);
864 xfrm_pol_put(policy);
865 *dst_p = NULL;
866 return err;
868 EXPORT_SYMBOL(xfrm_lookup);
870 /* When skb is transformed back to its "native" form, we have to
871 * check policy restrictions. At the moment we make this in maximally
872 * stupid way. Shame on me. :-) Of course, connected sockets must
873 * have policy cached at them.
876 static inline int
877 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
878 unsigned short family)
880 if (xfrm_state_kern(x))
881 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
882 return x->id.proto == tmpl->id.proto &&
883 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
884 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
885 x->props.mode == tmpl->mode &&
886 (tmpl->aalgos & (1<<x->props.aalgo)) &&
887 !(x->props.mode && xfrm_state_addr_cmp(tmpl, x, family));
890 static inline int
891 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
892 unsigned short family)
894 int idx = start;
896 if (tmpl->optional) {
897 if (!tmpl->mode)
898 return start;
899 } else
900 start = -1;
901 for (; idx < sp->len; idx++) {
902 if (xfrm_state_ok(tmpl, sp->x[idx].xvec, family))
903 return ++idx;
904 if (sp->x[idx].xvec->props.mode)
905 break;
907 return start;
910 static int
911 _decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
913 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
915 if (unlikely(afinfo == NULL))
916 return -EAFNOSUPPORT;
918 afinfo->decode_session(skb, fl);
919 xfrm_policy_put_afinfo(afinfo);
920 return 0;
923 static inline int secpath_has_tunnel(struct sec_path *sp, int k)
925 for (; k < sp->len; k++) {
926 if (sp->x[k].xvec->props.mode)
927 return 1;
930 return 0;
933 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
934 unsigned short family)
936 struct xfrm_policy *pol;
937 struct flowi fl;
939 if (_decode_session(skb, &fl, family) < 0)
940 return 0;
942 /* First, check used SA against their selectors. */
943 if (skb->sp) {
944 int i;
946 for (i=skb->sp->len-1; i>=0; i--) {
947 struct sec_decap_state *xvec = &(skb->sp->x[i]);
948 if (!xfrm_selector_match(&xvec->xvec->sel, &fl, family))
949 return 0;
951 /* If there is a post_input processor, try running it */
952 if (xvec->xvec->type->post_input &&
953 (xvec->xvec->type->post_input)(xvec->xvec,
954 &(xvec->decap),
955 skb) != 0)
956 return 0;
960 pol = NULL;
961 if (sk && sk->sk_policy[dir])
962 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
964 if (!pol)
965 pol = flow_cache_lookup(&fl, family,
966 policy_to_flow_dir(dir),
967 xfrm_policy_lookup);
969 if (!pol)
970 return !skb->sp || !secpath_has_tunnel(skb->sp, 0);
972 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
974 if (pol->action == XFRM_POLICY_ALLOW) {
975 struct sec_path *sp;
976 static struct sec_path dummy;
977 int i, k;
979 if ((sp = skb->sp) == NULL)
980 sp = &dummy;
982 /* For each tunnel xfrm, find the first matching tmpl.
983 * For each tmpl before that, find corresponding xfrm.
984 * Order is _important_. Later we will implement
985 * some barriers, but at the moment barriers
986 * are implied between each two transformations.
988 for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
989 k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
990 if (k < 0)
991 goto reject;
994 if (secpath_has_tunnel(sp, k))
995 goto reject;
997 xfrm_pol_put(pol);
998 return 1;
1001 reject:
1002 xfrm_pol_put(pol);
1003 return 0;
1005 EXPORT_SYMBOL(__xfrm_policy_check);
1007 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1009 struct flowi fl;
1011 if (_decode_session(skb, &fl, family) < 0)
1012 return 0;
1014 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1016 EXPORT_SYMBOL(__xfrm_route_forward);
1018 /* Optimize later using cookies and generation ids. */
1020 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1022 if (!stale_bundle(dst))
1023 return dst;
1025 return NULL;
1028 static int stale_bundle(struct dst_entry *dst)
1030 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC);
1033 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1035 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1036 dst->dev = &loopback_dev;
1037 dev_hold(&loopback_dev);
1038 dev_put(dev);
1041 EXPORT_SYMBOL(xfrm_dst_ifdown);
1043 static void xfrm_link_failure(struct sk_buff *skb)
1045 /* Impossible. Such dst must be popped before reaches point of failure. */
1046 return;
1049 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1051 if (dst) {
1052 if (dst->obsolete) {
1053 dst_release(dst);
1054 dst = NULL;
1057 return dst;
1060 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1062 int i;
1063 struct xfrm_policy *pol;
1064 struct dst_entry *dst, **dstp, *gc_list = NULL;
1066 read_lock_bh(&xfrm_policy_lock);
1067 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
1068 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1069 write_lock(&pol->lock);
1070 dstp = &pol->bundles;
1071 while ((dst=*dstp) != NULL) {
1072 if (func(dst)) {
1073 *dstp = dst->next;
1074 dst->next = gc_list;
1075 gc_list = dst;
1076 } else {
1077 dstp = &dst->next;
1080 write_unlock(&pol->lock);
1083 read_unlock_bh(&xfrm_policy_lock);
1085 while (gc_list) {
1086 dst = gc_list;
1087 gc_list = dst->next;
1088 dst_free(dst);
1092 static int unused_bundle(struct dst_entry *dst)
1094 return !atomic_read(&dst->__refcnt);
1097 static void __xfrm_garbage_collect(void)
1099 xfrm_prune_bundles(unused_bundle);
1102 int xfrm_flush_bundles(void)
1104 xfrm_prune_bundles(stale_bundle);
1105 return 0;
1108 void xfrm_init_pmtu(struct dst_entry *dst)
1110 do {
1111 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1112 u32 pmtu, route_mtu_cached;
1114 pmtu = dst_mtu(dst->child);
1115 xdst->child_mtu_cached = pmtu;
1117 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1119 route_mtu_cached = dst_mtu(xdst->route);
1120 xdst->route_mtu_cached = route_mtu_cached;
1122 if (pmtu > route_mtu_cached)
1123 pmtu = route_mtu_cached;
1125 dst->metrics[RTAX_MTU-1] = pmtu;
1126 } while ((dst = dst->next));
1129 EXPORT_SYMBOL(xfrm_init_pmtu);
1131 /* Check that the bundle accepts the flow and its components are
1132 * still valid.
1135 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family)
1137 struct dst_entry *dst = &first->u.dst;
1138 struct xfrm_dst *last;
1139 u32 mtu;
1141 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1142 (dst->dev && !netif_running(dst->dev)))
1143 return 0;
1145 last = NULL;
1147 do {
1148 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1150 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1151 return 0;
1152 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1153 return 0;
1155 mtu = dst_mtu(dst->child);
1156 if (xdst->child_mtu_cached != mtu) {
1157 last = xdst;
1158 xdst->child_mtu_cached = mtu;
1161 if (!dst_check(xdst->route, xdst->route_cookie))
1162 return 0;
1163 mtu = dst_mtu(xdst->route);
1164 if (xdst->route_mtu_cached != mtu) {
1165 last = xdst;
1166 xdst->route_mtu_cached = mtu;
1169 dst = dst->child;
1170 } while (dst->xfrm);
1172 if (likely(!last))
1173 return 1;
1175 mtu = last->child_mtu_cached;
1176 for (;;) {
1177 dst = &last->u.dst;
1179 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1180 if (mtu > last->route_mtu_cached)
1181 mtu = last->route_mtu_cached;
1182 dst->metrics[RTAX_MTU-1] = mtu;
1184 if (last == first)
1185 break;
1187 last = last->u.next;
1188 last->child_mtu_cached = mtu;
1191 return 1;
1194 EXPORT_SYMBOL(xfrm_bundle_ok);
1196 /* Well... that's _TASK_. We need to scan through transformation
1197 * list and figure out what mss tcp should generate in order to
1198 * final datagram fit to mtu. Mama mia... :-)
1200 * Apparently, some easy way exists, but we used to choose the most
1201 * bizarre ones. :-) So, raising Kalashnikov... tra-ta-ta.
1203 * Consider this function as something like dark humour. :-)
1205 static int xfrm_get_mss(struct dst_entry *dst, u32 mtu)
1207 int res = mtu - dst->header_len;
1209 for (;;) {
1210 struct dst_entry *d = dst;
1211 int m = res;
1213 do {
1214 struct xfrm_state *x = d->xfrm;
1215 if (x) {
1216 spin_lock_bh(&x->lock);
1217 if (x->km.state == XFRM_STATE_VALID &&
1218 x->type && x->type->get_max_size)
1219 m = x->type->get_max_size(d->xfrm, m);
1220 else
1221 m += x->props.header_len;
1222 spin_unlock_bh(&x->lock);
1224 } while ((d = d->child) != NULL);
1226 if (m <= mtu)
1227 break;
1228 res -= (m - mtu);
1229 if (res < 88)
1230 return mtu;
1233 return res + dst->header_len;
1236 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1238 int err = 0;
1239 if (unlikely(afinfo == NULL))
1240 return -EINVAL;
1241 if (unlikely(afinfo->family >= NPROTO))
1242 return -EAFNOSUPPORT;
1243 write_lock(&xfrm_policy_afinfo_lock);
1244 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1245 err = -ENOBUFS;
1246 else {
1247 struct dst_ops *dst_ops = afinfo->dst_ops;
1248 if (likely(dst_ops->kmem_cachep == NULL))
1249 dst_ops->kmem_cachep = xfrm_dst_cache;
1250 if (likely(dst_ops->check == NULL))
1251 dst_ops->check = xfrm_dst_check;
1252 if (likely(dst_ops->negative_advice == NULL))
1253 dst_ops->negative_advice = xfrm_negative_advice;
1254 if (likely(dst_ops->link_failure == NULL))
1255 dst_ops->link_failure = xfrm_link_failure;
1256 if (likely(dst_ops->get_mss == NULL))
1257 dst_ops->get_mss = xfrm_get_mss;
1258 if (likely(afinfo->garbage_collect == NULL))
1259 afinfo->garbage_collect = __xfrm_garbage_collect;
1260 xfrm_policy_afinfo[afinfo->family] = afinfo;
1262 write_unlock(&xfrm_policy_afinfo_lock);
1263 return err;
1265 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1267 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1269 int err = 0;
1270 if (unlikely(afinfo == NULL))
1271 return -EINVAL;
1272 if (unlikely(afinfo->family >= NPROTO))
1273 return -EAFNOSUPPORT;
1274 write_lock(&xfrm_policy_afinfo_lock);
1275 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1276 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1277 err = -EINVAL;
1278 else {
1279 struct dst_ops *dst_ops = afinfo->dst_ops;
1280 xfrm_policy_afinfo[afinfo->family] = NULL;
1281 dst_ops->kmem_cachep = NULL;
1282 dst_ops->check = NULL;
1283 dst_ops->negative_advice = NULL;
1284 dst_ops->link_failure = NULL;
1285 dst_ops->get_mss = NULL;
1286 afinfo->garbage_collect = NULL;
1289 write_unlock(&xfrm_policy_afinfo_lock);
1290 return err;
1292 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1294 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1296 struct xfrm_policy_afinfo *afinfo;
1297 if (unlikely(family >= NPROTO))
1298 return NULL;
1299 read_lock(&xfrm_policy_afinfo_lock);
1300 afinfo = xfrm_policy_afinfo[family];
1301 if (likely(afinfo != NULL))
1302 read_lock(&afinfo->lock);
1303 read_unlock(&xfrm_policy_afinfo_lock);
1304 return afinfo;
1307 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1309 if (unlikely(afinfo == NULL))
1310 return;
1311 read_unlock(&afinfo->lock);
1314 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1316 switch (event) {
1317 case NETDEV_DOWN:
1318 xfrm_flush_bundles();
1320 return NOTIFY_DONE;
1323 static struct notifier_block xfrm_dev_notifier = {
1324 xfrm_dev_event,
1325 NULL,
1329 static void __init xfrm_policy_init(void)
1331 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1332 sizeof(struct xfrm_dst),
1333 0, SLAB_HWCACHE_ALIGN,
1334 NULL, NULL);
1335 if (!xfrm_dst_cache)
1336 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1338 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1339 register_netdevice_notifier(&xfrm_dev_notifier);
1342 void __init xfrm_init(void)
1344 xfrm_state_init();
1345 xfrm_policy_init();
1346 xfrm_input_init();