[IPSEC]: Sync series - policy expires
[linux-2.6.git] / net / xfrm / xfrm_policy.c
blob453551ec87a1d847cb6184a265d240e44b59f1eb
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 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, 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, 0);
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 BUG_ON(!policy->dead);
252 BUG_ON(policy->bundles);
254 if (del_timer(&policy->timer))
255 BUG();
257 security_xfrm_policy_free(policy);
258 kfree(policy);
260 EXPORT_SYMBOL(__xfrm_policy_destroy);
262 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
264 struct dst_entry *dst;
266 while ((dst = policy->bundles) != NULL) {
267 policy->bundles = dst->next;
268 dst_free(dst);
271 if (del_timer(&policy->timer))
272 atomic_dec(&policy->refcnt);
274 if (atomic_read(&policy->refcnt) > 1)
275 flow_cache_flush();
277 xfrm_pol_put(policy);
280 static void xfrm_policy_gc_task(void *data)
282 struct xfrm_policy *policy;
283 struct list_head *entry, *tmp;
284 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
286 spin_lock_bh(&xfrm_policy_gc_lock);
287 list_splice_init(&xfrm_policy_gc_list, &gc_list);
288 spin_unlock_bh(&xfrm_policy_gc_lock);
290 list_for_each_safe(entry, tmp, &gc_list) {
291 policy = list_entry(entry, struct xfrm_policy, list);
292 xfrm_policy_gc_kill(policy);
296 /* Rule must be locked. Release descentant resources, announce
297 * entry dead. The rule must be unlinked from lists to the moment.
300 static void xfrm_policy_kill(struct xfrm_policy *policy)
302 int dead;
304 write_lock_bh(&policy->lock);
305 dead = policy->dead;
306 policy->dead = 1;
307 write_unlock_bh(&policy->lock);
309 if (unlikely(dead)) {
310 WARN_ON(1);
311 return;
314 spin_lock(&xfrm_policy_gc_lock);
315 list_add(&policy->list, &xfrm_policy_gc_list);
316 spin_unlock(&xfrm_policy_gc_lock);
318 schedule_work(&xfrm_policy_gc_work);
321 /* Generate new index... KAME seems to generate them ordered by cost
322 * of an absolute inpredictability of ordering of rules. This will not pass. */
323 static u32 xfrm_gen_index(int dir)
325 u32 idx;
326 struct xfrm_policy *p;
327 static u32 idx_generator;
329 for (;;) {
330 idx = (idx_generator | dir);
331 idx_generator += 8;
332 if (idx == 0)
333 idx = 8;
334 for (p = xfrm_policy_list[dir]; p; p = p->next) {
335 if (p->index == idx)
336 break;
338 if (!p)
339 return idx;
343 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
345 struct xfrm_policy *pol, **p;
346 struct xfrm_policy *delpol = NULL;
347 struct xfrm_policy **newpos = NULL;
348 struct dst_entry *gc_list;
350 write_lock_bh(&xfrm_policy_lock);
351 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL;) {
352 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
353 xfrm_sec_ctx_match(pol->security, policy->security)) {
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 read_lock_bh(&xfrm_policy_lock);
389 gc_list = NULL;
390 for (policy = policy->next; policy; policy = policy->next) {
391 struct dst_entry *dst;
393 write_lock(&policy->lock);
394 dst = policy->bundles;
395 if (dst) {
396 struct dst_entry *tail = dst;
397 while (tail->next)
398 tail = tail->next;
399 tail->next = gc_list;
400 gc_list = dst;
402 policy->bundles = NULL;
404 write_unlock(&policy->lock);
406 read_unlock_bh(&xfrm_policy_lock);
408 while (gc_list) {
409 struct dst_entry *dst = gc_list;
411 gc_list = dst->next;
412 dst_free(dst);
415 return 0;
417 EXPORT_SYMBOL(xfrm_policy_insert);
419 struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
420 struct xfrm_sec_ctx *ctx, int delete)
422 struct xfrm_policy *pol, **p;
424 write_lock_bh(&xfrm_policy_lock);
425 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
426 if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
427 (xfrm_sec_ctx_match(ctx, pol->security))) {
428 xfrm_pol_hold(pol);
429 if (delete)
430 *p = pol->next;
431 break;
434 write_unlock_bh(&xfrm_policy_lock);
436 if (pol && delete) {
437 atomic_inc(&flow_cache_genid);
438 xfrm_policy_kill(pol);
440 return pol;
442 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
444 struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
446 struct xfrm_policy *pol, **p;
448 write_lock_bh(&xfrm_policy_lock);
449 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
450 if (pol->index == id) {
451 xfrm_pol_hold(pol);
452 if (delete)
453 *p = pol->next;
454 break;
457 write_unlock_bh(&xfrm_policy_lock);
459 if (pol && delete) {
460 atomic_inc(&flow_cache_genid);
461 xfrm_policy_kill(pol);
463 return pol;
465 EXPORT_SYMBOL(xfrm_policy_byid);
467 void xfrm_policy_flush(void)
469 struct xfrm_policy *xp;
470 int dir;
472 write_lock_bh(&xfrm_policy_lock);
473 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
474 while ((xp = xfrm_policy_list[dir]) != NULL) {
475 xfrm_policy_list[dir] = xp->next;
476 write_unlock_bh(&xfrm_policy_lock);
478 xfrm_policy_kill(xp);
480 write_lock_bh(&xfrm_policy_lock);
483 atomic_inc(&flow_cache_genid);
484 write_unlock_bh(&xfrm_policy_lock);
486 EXPORT_SYMBOL(xfrm_policy_flush);
488 int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
489 void *data)
491 struct xfrm_policy *xp;
492 int dir;
493 int count = 0;
494 int error = 0;
496 read_lock_bh(&xfrm_policy_lock);
497 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
498 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
499 count++;
502 if (count == 0) {
503 error = -ENOENT;
504 goto out;
507 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
508 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
509 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
510 if (error)
511 goto out;
515 out:
516 read_unlock_bh(&xfrm_policy_lock);
517 return error;
519 EXPORT_SYMBOL(xfrm_policy_walk);
521 /* Find policy to apply to this flow. */
523 static void xfrm_policy_lookup(struct flowi *fl, u32 sk_sid, u16 family, u8 dir,
524 void **objp, atomic_t **obj_refp)
526 struct xfrm_policy *pol;
528 read_lock_bh(&xfrm_policy_lock);
529 for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
530 struct xfrm_selector *sel = &pol->selector;
531 int match;
533 if (pol->family != family)
534 continue;
536 match = xfrm_selector_match(sel, fl, family);
538 if (match) {
539 if (!security_xfrm_policy_lookup(pol, sk_sid, dir)) {
540 xfrm_pol_hold(pol);
541 break;
545 read_unlock_bh(&xfrm_policy_lock);
546 if ((*objp = (void *) pol) != NULL)
547 *obj_refp = &pol->refcnt;
550 static inline int policy_to_flow_dir(int dir)
552 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
553 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
554 XFRM_POLICY_FWD == FLOW_DIR_FWD)
555 return dir;
556 switch (dir) {
557 default:
558 case XFRM_POLICY_IN:
559 return FLOW_DIR_IN;
560 case XFRM_POLICY_OUT:
561 return FLOW_DIR_OUT;
562 case XFRM_POLICY_FWD:
563 return FLOW_DIR_FWD;
567 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl, u32 sk_sid)
569 struct xfrm_policy *pol;
571 read_lock_bh(&xfrm_policy_lock);
572 if ((pol = sk->sk_policy[dir]) != NULL) {
573 int match = xfrm_selector_match(&pol->selector, fl,
574 sk->sk_family);
575 int err = 0;
577 if (match)
578 err = security_xfrm_policy_lookup(pol, sk_sid, policy_to_flow_dir(dir));
580 if (match && !err)
581 xfrm_pol_hold(pol);
582 else
583 pol = NULL;
585 read_unlock_bh(&xfrm_policy_lock);
586 return pol;
589 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
591 pol->next = xfrm_policy_list[dir];
592 xfrm_policy_list[dir] = pol;
593 xfrm_pol_hold(pol);
596 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
597 int dir)
599 struct xfrm_policy **polp;
601 for (polp = &xfrm_policy_list[dir];
602 *polp != NULL; polp = &(*polp)->next) {
603 if (*polp == pol) {
604 *polp = pol->next;
605 return pol;
608 return NULL;
611 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
613 write_lock_bh(&xfrm_policy_lock);
614 pol = __xfrm_policy_unlink(pol, dir);
615 write_unlock_bh(&xfrm_policy_lock);
616 if (pol) {
617 if (dir < XFRM_POLICY_MAX)
618 atomic_inc(&flow_cache_genid);
619 xfrm_policy_kill(pol);
620 return 0;
622 return -ENOENT;
625 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
627 struct xfrm_policy *old_pol;
629 write_lock_bh(&xfrm_policy_lock);
630 old_pol = sk->sk_policy[dir];
631 sk->sk_policy[dir] = pol;
632 if (pol) {
633 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
634 pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
635 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
637 if (old_pol)
638 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
639 write_unlock_bh(&xfrm_policy_lock);
641 if (old_pol) {
642 xfrm_policy_kill(old_pol);
644 return 0;
647 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
649 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
651 if (newp) {
652 newp->selector = old->selector;
653 if (security_xfrm_policy_clone(old, newp)) {
654 kfree(newp);
655 return NULL; /* ENOMEM */
657 newp->lft = old->lft;
658 newp->curlft = old->curlft;
659 newp->action = old->action;
660 newp->flags = old->flags;
661 newp->xfrm_nr = old->xfrm_nr;
662 newp->index = old->index;
663 memcpy(newp->xfrm_vec, old->xfrm_vec,
664 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
665 write_lock_bh(&xfrm_policy_lock);
666 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
667 write_unlock_bh(&xfrm_policy_lock);
668 xfrm_pol_put(newp);
670 return newp;
673 int __xfrm_sk_clone_policy(struct sock *sk)
675 struct xfrm_policy *p0 = sk->sk_policy[0],
676 *p1 = sk->sk_policy[1];
678 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
679 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
680 return -ENOMEM;
681 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
682 return -ENOMEM;
683 return 0;
686 /* Resolve list of templates for the flow, given policy. */
688 static int
689 xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
690 struct xfrm_state **xfrm,
691 unsigned short family)
693 int nx;
694 int i, error;
695 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
696 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
698 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
699 struct xfrm_state *x;
700 xfrm_address_t *remote = daddr;
701 xfrm_address_t *local = saddr;
702 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
704 if (tmpl->mode) {
705 remote = &tmpl->id.daddr;
706 local = &tmpl->saddr;
709 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
711 if (x && x->km.state == XFRM_STATE_VALID) {
712 xfrm[nx++] = x;
713 daddr = remote;
714 saddr = local;
715 continue;
717 if (x) {
718 error = (x->km.state == XFRM_STATE_ERROR ?
719 -EINVAL : -EAGAIN);
720 xfrm_state_put(x);
723 if (!tmpl->optional)
724 goto fail;
726 return nx;
728 fail:
729 for (nx--; nx>=0; nx--)
730 xfrm_state_put(xfrm[nx]);
731 return error;
734 /* Check that the bundle accepts the flow and its components are
735 * still valid.
738 static struct dst_entry *
739 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
741 struct dst_entry *x;
742 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
743 if (unlikely(afinfo == NULL))
744 return ERR_PTR(-EINVAL);
745 x = afinfo->find_bundle(fl, policy);
746 xfrm_policy_put_afinfo(afinfo);
747 return x;
750 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
751 * all the metrics... Shortly, bundle a bundle.
754 static int
755 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
756 struct flowi *fl, struct dst_entry **dst_p,
757 unsigned short family)
759 int err;
760 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
761 if (unlikely(afinfo == NULL))
762 return -EINVAL;
763 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
764 xfrm_policy_put_afinfo(afinfo);
765 return err;
769 static int stale_bundle(struct dst_entry *dst);
771 /* Main function: finds/creates a bundle for given flow.
773 * At the moment we eat a raw IP route. Mostly to speed up lookups
774 * on interfaces with disabled IPsec.
776 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
777 struct sock *sk, int flags)
779 struct xfrm_policy *policy;
780 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
781 struct dst_entry *dst, *dst_orig = *dst_p;
782 int nx = 0;
783 int err;
784 u32 genid;
785 u16 family;
786 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
787 u32 sk_sid = security_sk_sid(sk, fl, dir);
788 restart:
789 genid = atomic_read(&flow_cache_genid);
790 policy = NULL;
791 if (sk && sk->sk_policy[1])
792 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, sk_sid);
794 if (!policy) {
795 /* To accelerate a bit... */
796 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
797 return 0;
799 policy = flow_cache_lookup(fl, sk_sid, dst_orig->ops->family,
800 dir, xfrm_policy_lookup);
803 if (!policy)
804 return 0;
806 family = dst_orig->ops->family;
807 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
809 switch (policy->action) {
810 case XFRM_POLICY_BLOCK:
811 /* Prohibit the flow */
812 err = -EPERM;
813 goto error;
815 case XFRM_POLICY_ALLOW:
816 if (policy->xfrm_nr == 0) {
817 /* Flow passes not transformed. */
818 xfrm_pol_put(policy);
819 return 0;
822 /* Try to find matching bundle.
824 * LATER: help from flow cache. It is optional, this
825 * is required only for output policy.
827 dst = xfrm_find_bundle(fl, policy, family);
828 if (IS_ERR(dst)) {
829 err = PTR_ERR(dst);
830 goto error;
833 if (dst)
834 break;
836 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
838 if (unlikely(nx<0)) {
839 err = nx;
840 if (err == -EAGAIN && flags) {
841 DECLARE_WAITQUEUE(wait, current);
843 add_wait_queue(&km_waitq, &wait);
844 set_current_state(TASK_INTERRUPTIBLE);
845 schedule();
846 set_current_state(TASK_RUNNING);
847 remove_wait_queue(&km_waitq, &wait);
849 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
851 if (nx == -EAGAIN && signal_pending(current)) {
852 err = -ERESTART;
853 goto error;
855 if (nx == -EAGAIN ||
856 genid != atomic_read(&flow_cache_genid)) {
857 xfrm_pol_put(policy);
858 goto restart;
860 err = nx;
862 if (err < 0)
863 goto error;
865 if (nx == 0) {
866 /* Flow passes not transformed. */
867 xfrm_pol_put(policy);
868 return 0;
871 dst = dst_orig;
872 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
874 if (unlikely(err)) {
875 int i;
876 for (i=0; i<nx; i++)
877 xfrm_state_put(xfrm[i]);
878 goto error;
881 write_lock_bh(&policy->lock);
882 if (unlikely(policy->dead || stale_bundle(dst))) {
883 /* Wow! While we worked on resolving, this
884 * policy has gone. Retry. It is not paranoia,
885 * we just cannot enlist new bundle to dead object.
886 * We can't enlist stable bundles either.
888 write_unlock_bh(&policy->lock);
889 if (dst)
890 dst_free(dst);
892 err = -EHOSTUNREACH;
893 goto error;
895 dst->next = policy->bundles;
896 policy->bundles = dst;
897 dst_hold(dst);
898 write_unlock_bh(&policy->lock);
900 *dst_p = dst;
901 dst_release(dst_orig);
902 xfrm_pol_put(policy);
903 return 0;
905 error:
906 dst_release(dst_orig);
907 xfrm_pol_put(policy);
908 *dst_p = NULL;
909 return err;
911 EXPORT_SYMBOL(xfrm_lookup);
913 /* When skb is transformed back to its "native" form, we have to
914 * check policy restrictions. At the moment we make this in maximally
915 * stupid way. Shame on me. :-) Of course, connected sockets must
916 * have policy cached at them.
919 static inline int
920 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
921 unsigned short family)
923 if (xfrm_state_kern(x))
924 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
925 return x->id.proto == tmpl->id.proto &&
926 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
927 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
928 x->props.mode == tmpl->mode &&
929 (tmpl->aalgos & (1<<x->props.aalgo)) &&
930 !(x->props.mode && xfrm_state_addr_cmp(tmpl, x, family));
933 static inline int
934 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
935 unsigned short family)
937 int idx = start;
939 if (tmpl->optional) {
940 if (!tmpl->mode)
941 return start;
942 } else
943 start = -1;
944 for (; idx < sp->len; idx++) {
945 if (xfrm_state_ok(tmpl, sp->x[idx].xvec, family))
946 return ++idx;
947 if (sp->x[idx].xvec->props.mode)
948 break;
950 return start;
954 xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
956 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
958 if (unlikely(afinfo == NULL))
959 return -EAFNOSUPPORT;
961 afinfo->decode_session(skb, fl);
962 xfrm_policy_put_afinfo(afinfo);
963 return 0;
965 EXPORT_SYMBOL(xfrm_decode_session);
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 (xfrm_decode_session(skb, &fl, family) < 0)
986 return 0;
987 nf_nat_decode_session(skb, &fl, family);
989 sk_sid = security_sk_sid(sk, &fl, fl_dir);
991 /* First, check used SA against their selectors. */
992 if (skb->sp) {
993 int i;
995 for (i=skb->sp->len-1; i>=0; i--) {
996 struct sec_decap_state *xvec = &(skb->sp->x[i]);
997 if (!xfrm_selector_match(&xvec->xvec->sel, &fl, family))
998 return 0;
1002 pol = NULL;
1003 if (sk && sk->sk_policy[dir])
1004 pol = xfrm_sk_policy_lookup(sk, dir, &fl, sk_sid);
1006 if (!pol)
1007 pol = flow_cache_lookup(&fl, sk_sid, family, fl_dir,
1008 xfrm_policy_lookup);
1010 if (!pol)
1011 return !skb->sp || !secpath_has_tunnel(skb->sp, 0);
1013 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
1015 if (pol->action == XFRM_POLICY_ALLOW) {
1016 struct sec_path *sp;
1017 static struct sec_path dummy;
1018 int i, k;
1020 if ((sp = skb->sp) == NULL)
1021 sp = &dummy;
1023 /* For each tunnel xfrm, find the first matching tmpl.
1024 * For each tmpl before that, find corresponding xfrm.
1025 * Order is _important_. Later we will implement
1026 * some barriers, but at the moment barriers
1027 * are implied between each two transformations.
1029 for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
1030 k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
1031 if (k < 0)
1032 goto reject;
1035 if (secpath_has_tunnel(sp, k))
1036 goto reject;
1038 xfrm_pol_put(pol);
1039 return 1;
1042 reject:
1043 xfrm_pol_put(pol);
1044 return 0;
1046 EXPORT_SYMBOL(__xfrm_policy_check);
1048 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1050 struct flowi fl;
1052 if (xfrm_decode_session(skb, &fl, family) < 0)
1053 return 0;
1055 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1057 EXPORT_SYMBOL(__xfrm_route_forward);
1059 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1061 /* If it is marked obsolete, which is how we even get here,
1062 * then we have purged it from the policy bundle list and we
1063 * did that for a good reason.
1065 return NULL;
1068 static int stale_bundle(struct dst_entry *dst)
1070 return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC);
1073 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1075 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1076 dst->dev = &loopback_dev;
1077 dev_hold(&loopback_dev);
1078 dev_put(dev);
1081 EXPORT_SYMBOL(xfrm_dst_ifdown);
1083 static void xfrm_link_failure(struct sk_buff *skb)
1085 /* Impossible. Such dst must be popped before reaches point of failure. */
1086 return;
1089 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1091 if (dst) {
1092 if (dst->obsolete) {
1093 dst_release(dst);
1094 dst = NULL;
1097 return dst;
1100 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1102 int i;
1103 struct xfrm_policy *pol;
1104 struct dst_entry *dst, **dstp, *gc_list = NULL;
1106 read_lock_bh(&xfrm_policy_lock);
1107 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
1108 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1109 write_lock(&pol->lock);
1110 dstp = &pol->bundles;
1111 while ((dst=*dstp) != NULL) {
1112 if (func(dst)) {
1113 *dstp = dst->next;
1114 dst->next = gc_list;
1115 gc_list = dst;
1116 } else {
1117 dstp = &dst->next;
1120 write_unlock(&pol->lock);
1123 read_unlock_bh(&xfrm_policy_lock);
1125 while (gc_list) {
1126 dst = gc_list;
1127 gc_list = dst->next;
1128 dst_free(dst);
1132 static int unused_bundle(struct dst_entry *dst)
1134 return !atomic_read(&dst->__refcnt);
1137 static void __xfrm_garbage_collect(void)
1139 xfrm_prune_bundles(unused_bundle);
1142 int xfrm_flush_bundles(void)
1144 xfrm_prune_bundles(stale_bundle);
1145 return 0;
1148 static int always_true(struct dst_entry *dst)
1150 return 1;
1153 void xfrm_flush_all_bundles(void)
1155 xfrm_prune_bundles(always_true);
1158 void xfrm_init_pmtu(struct dst_entry *dst)
1160 do {
1161 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1162 u32 pmtu, route_mtu_cached;
1164 pmtu = dst_mtu(dst->child);
1165 xdst->child_mtu_cached = pmtu;
1167 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
1169 route_mtu_cached = dst_mtu(xdst->route);
1170 xdst->route_mtu_cached = route_mtu_cached;
1172 if (pmtu > route_mtu_cached)
1173 pmtu = route_mtu_cached;
1175 dst->metrics[RTAX_MTU-1] = pmtu;
1176 } while ((dst = dst->next));
1179 EXPORT_SYMBOL(xfrm_init_pmtu);
1181 /* Check that the bundle accepts the flow and its components are
1182 * still valid.
1185 int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family)
1187 struct dst_entry *dst = &first->u.dst;
1188 struct xfrm_dst *last;
1189 u32 mtu;
1191 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1192 (dst->dev && !netif_running(dst->dev)))
1193 return 0;
1195 last = NULL;
1197 do {
1198 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1200 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
1201 return 0;
1202 if (dst->xfrm->km.state != XFRM_STATE_VALID)
1203 return 0;
1205 mtu = dst_mtu(dst->child);
1206 if (xdst->child_mtu_cached != mtu) {
1207 last = xdst;
1208 xdst->child_mtu_cached = mtu;
1211 if (!dst_check(xdst->route, xdst->route_cookie))
1212 return 0;
1213 mtu = dst_mtu(xdst->route);
1214 if (xdst->route_mtu_cached != mtu) {
1215 last = xdst;
1216 xdst->route_mtu_cached = mtu;
1219 dst = dst->child;
1220 } while (dst->xfrm);
1222 if (likely(!last))
1223 return 1;
1225 mtu = last->child_mtu_cached;
1226 for (;;) {
1227 dst = &last->u.dst;
1229 mtu = xfrm_state_mtu(dst->xfrm, mtu);
1230 if (mtu > last->route_mtu_cached)
1231 mtu = last->route_mtu_cached;
1232 dst->metrics[RTAX_MTU-1] = mtu;
1234 if (last == first)
1235 break;
1237 last = last->u.next;
1238 last->child_mtu_cached = mtu;
1241 return 1;
1244 EXPORT_SYMBOL(xfrm_bundle_ok);
1246 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1248 int err = 0;
1249 if (unlikely(afinfo == NULL))
1250 return -EINVAL;
1251 if (unlikely(afinfo->family >= NPROTO))
1252 return -EAFNOSUPPORT;
1253 write_lock(&xfrm_policy_afinfo_lock);
1254 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1255 err = -ENOBUFS;
1256 else {
1257 struct dst_ops *dst_ops = afinfo->dst_ops;
1258 if (likely(dst_ops->kmem_cachep == NULL))
1259 dst_ops->kmem_cachep = xfrm_dst_cache;
1260 if (likely(dst_ops->check == NULL))
1261 dst_ops->check = xfrm_dst_check;
1262 if (likely(dst_ops->negative_advice == NULL))
1263 dst_ops->negative_advice = xfrm_negative_advice;
1264 if (likely(dst_ops->link_failure == NULL))
1265 dst_ops->link_failure = xfrm_link_failure;
1266 if (likely(afinfo->garbage_collect == NULL))
1267 afinfo->garbage_collect = __xfrm_garbage_collect;
1268 xfrm_policy_afinfo[afinfo->family] = afinfo;
1270 write_unlock(&xfrm_policy_afinfo_lock);
1271 return err;
1273 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
1275 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1277 int err = 0;
1278 if (unlikely(afinfo == NULL))
1279 return -EINVAL;
1280 if (unlikely(afinfo->family >= NPROTO))
1281 return -EAFNOSUPPORT;
1282 write_lock(&xfrm_policy_afinfo_lock);
1283 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1284 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1285 err = -EINVAL;
1286 else {
1287 struct dst_ops *dst_ops = afinfo->dst_ops;
1288 xfrm_policy_afinfo[afinfo->family] = NULL;
1289 dst_ops->kmem_cachep = NULL;
1290 dst_ops->check = NULL;
1291 dst_ops->negative_advice = NULL;
1292 dst_ops->link_failure = NULL;
1293 afinfo->garbage_collect = NULL;
1296 write_unlock(&xfrm_policy_afinfo_lock);
1297 return err;
1299 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
1301 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1303 struct xfrm_policy_afinfo *afinfo;
1304 if (unlikely(family >= NPROTO))
1305 return NULL;
1306 read_lock(&xfrm_policy_afinfo_lock);
1307 afinfo = xfrm_policy_afinfo[family];
1308 if (likely(afinfo != NULL))
1309 read_lock(&afinfo->lock);
1310 read_unlock(&xfrm_policy_afinfo_lock);
1311 return afinfo;
1314 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1316 if (unlikely(afinfo == NULL))
1317 return;
1318 read_unlock(&afinfo->lock);
1321 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1323 switch (event) {
1324 case NETDEV_DOWN:
1325 xfrm_flush_bundles();
1327 return NOTIFY_DONE;
1330 static struct notifier_block xfrm_dev_notifier = {
1331 xfrm_dev_event,
1332 NULL,
1336 static void __init xfrm_policy_init(void)
1338 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1339 sizeof(struct xfrm_dst),
1340 0, SLAB_HWCACHE_ALIGN,
1341 NULL, NULL);
1342 if (!xfrm_dst_cache)
1343 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1345 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1346 register_netdevice_notifier(&xfrm_dev_notifier);
1349 void __init xfrm_init(void)
1351 xfrm_state_init();
1352 xfrm_policy_init();
1353 xfrm_input_init();