6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/config.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
27 DECLARE_MUTEX(xfrm_cfg_sem);
29 static rwlock_t xfrm_policy_lock = RW_LOCK_UNLOCKED;
31 struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
33 static rwlock_t xfrm_policy_afinfo_lock = RW_LOCK_UNLOCKED;
34 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
36 kmem_cache_t *xfrm_dst_cache;
38 static struct work_struct xfrm_policy_gc_work;
39 static struct list_head xfrm_policy_gc_list =
40 LIST_HEAD_INIT(xfrm_policy_gc_list);
41 static spinlock_t xfrm_policy_gc_lock = SPIN_LOCK_UNLOCKED;
43 int xfrm_register_type(struct xfrm_type *type, unsigned short family)
45 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
46 struct xfrm_type_map *typemap;
49 if (unlikely(afinfo == NULL))
51 typemap = afinfo->type_map;
53 write_lock(&typemap->lock);
54 if (likely(typemap->map[type->proto] == NULL))
55 typemap->map[type->proto] = type;
58 write_unlock(&typemap->lock);
59 xfrm_policy_put_afinfo(afinfo);
63 int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
65 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
66 struct xfrm_type_map *typemap;
69 if (unlikely(afinfo == NULL))
71 typemap = afinfo->type_map;
73 write_lock(&typemap->lock);
74 if (unlikely(typemap->map[type->proto] != type))
77 typemap->map[type->proto] = NULL;
78 write_unlock(&typemap->lock);
79 xfrm_policy_put_afinfo(afinfo);
83 struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
85 struct xfrm_policy_afinfo *afinfo;
86 struct xfrm_type_map *typemap;
87 struct xfrm_type *type;
88 int modload_attempted = 0;
91 afinfo = xfrm_policy_get_afinfo(family);
92 if (unlikely(afinfo == NULL))
94 typemap = afinfo->type_map;
96 read_lock(&typemap->lock);
97 type = typemap->map[proto];
98 if (unlikely(type && !try_module_get(type->owner)))
100 read_unlock(&typemap->lock);
101 if (!type && !modload_attempted) {
102 xfrm_policy_put_afinfo(afinfo);
103 request_module("xfrm-type-%d-%d",
104 (int) family, (int) proto);
105 modload_attempted = 1;
109 xfrm_policy_put_afinfo(afinfo);
113 int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
114 unsigned short family)
116 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
119 if (unlikely(afinfo == NULL))
120 return -EAFNOSUPPORT;
122 if (likely(afinfo->dst_lookup != NULL))
123 err = afinfo->dst_lookup(dst, fl);
126 xfrm_policy_put_afinfo(afinfo);
130 void xfrm_put_type(struct xfrm_type *type)
132 module_put(type->owner);
135 static inline unsigned long make_jiffies(long secs)
137 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
138 return MAX_SCHEDULE_TIMEOUT-1;
143 static void xfrm_policy_timer(unsigned long data)
145 struct xfrm_policy *xp = (struct xfrm_policy*)data;
146 unsigned long now = (unsigned long)xtime.tv_sec;
147 long next = LONG_MAX;
151 read_lock(&xp->lock);
158 if (xp->lft.hard_add_expires_seconds) {
159 long tmo = xp->lft.hard_add_expires_seconds +
160 xp->curlft.add_time - now;
166 if (xp->lft.hard_use_expires_seconds) {
167 long tmo = xp->lft.hard_use_expires_seconds +
168 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
174 if (xp->lft.soft_add_expires_seconds) {
175 long tmo = xp->lft.soft_add_expires_seconds +
176 xp->curlft.add_time - now;
179 tmo = XFRM_KM_TIMEOUT;
184 if (xp->lft.soft_use_expires_seconds) {
185 long tmo = xp->lft.soft_use_expires_seconds +
186 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
189 tmo = XFRM_KM_TIMEOUT;
196 km_policy_expired(xp, dir, 0);
197 if (next != LONG_MAX &&
198 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
202 read_unlock(&xp->lock);
207 read_unlock(&xp->lock);
208 km_policy_expired(xp, dir, 1);
209 xfrm_policy_delete(xp, dir);
214 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
218 struct xfrm_policy *xfrm_policy_alloc(int gfp)
220 struct xfrm_policy *policy;
222 policy = kmalloc(sizeof(struct xfrm_policy), gfp);
225 memset(policy, 0, sizeof(struct xfrm_policy));
226 atomic_set(&policy->refcnt, 1);
227 policy->lock = RW_LOCK_UNLOCKED;
228 init_timer(&policy->timer);
229 policy->timer.data = (unsigned long)policy;
230 policy->timer.function = xfrm_policy_timer;
235 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
237 void __xfrm_policy_destroy(struct xfrm_policy *policy)
245 if (del_timer(&policy->timer))
251 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
253 struct dst_entry *dst;
255 while ((dst = policy->bundles) != NULL) {
256 policy->bundles = dst->next;
260 if (del_timer(&policy->timer))
261 atomic_dec(&policy->refcnt);
263 if (atomic_read(&policy->refcnt) > 1)
266 xfrm_pol_put(policy);
269 static void xfrm_policy_gc_task(void *data)
271 struct xfrm_policy *policy;
272 struct list_head *entry, *tmp;
273 struct list_head gc_list = LIST_HEAD_INIT(gc_list);
275 spin_lock_bh(&xfrm_policy_gc_lock);
276 list_splice_init(&xfrm_policy_gc_list, &gc_list);
277 spin_unlock_bh(&xfrm_policy_gc_lock);
279 list_for_each_safe(entry, tmp, &gc_list) {
280 policy = list_entry(entry, struct xfrm_policy, list);
281 xfrm_policy_gc_kill(policy);
285 /* Rule must be locked. Release descentant resources, announce
286 * entry dead. The rule must be unlinked from lists to the moment.
289 void xfrm_policy_kill(struct xfrm_policy *policy)
291 write_lock_bh(&policy->lock);
297 spin_lock(&xfrm_policy_gc_lock);
298 list_add(&policy->list, &xfrm_policy_gc_list);
299 spin_unlock(&xfrm_policy_gc_lock);
300 schedule_work(&xfrm_policy_gc_work);
303 write_unlock_bh(&policy->lock);
306 /* Generate new index... KAME seems to generate them ordered by cost
307 * of an absolute inpredictability of ordering of rules. This will not pass. */
308 static u32 xfrm_gen_index(int dir)
311 struct xfrm_policy *p;
312 static u32 idx_generator;
315 idx = (idx_generator | dir);
319 for (p = xfrm_policy_list[dir]; p; p = p->next) {
328 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
330 struct xfrm_policy *pol, **p;
331 struct xfrm_policy *delpol = NULL;
332 struct xfrm_policy **newpos = NULL;
334 write_lock_bh(&xfrm_policy_lock);
335 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
336 if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0) {
338 write_unlock_bh(&xfrm_policy_lock);
343 if (policy->priority > pol->priority)
345 } else if (policy->priority >= pol->priority)
354 xfrm_pol_hold(policy);
357 atomic_inc(&flow_cache_genid);
358 policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
359 policy->curlft.add_time = (unsigned long)xtime.tv_sec;
360 policy->curlft.use_time = 0;
361 if (!mod_timer(&policy->timer, jiffies + HZ))
362 xfrm_pol_hold(policy);
363 write_unlock_bh(&xfrm_policy_lock);
366 xfrm_policy_kill(delpol);
371 struct xfrm_policy *xfrm_policy_bysel(int dir, struct xfrm_selector *sel,
374 struct xfrm_policy *pol, **p;
376 write_lock_bh(&xfrm_policy_lock);
377 for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
378 if (memcmp(sel, &pol->selector, sizeof(*sel)) == 0) {
385 write_unlock_bh(&xfrm_policy_lock);
388 atomic_inc(&flow_cache_genid);
389 xfrm_policy_kill(pol);
394 struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
396 struct xfrm_policy *pol, **p;
398 write_lock_bh(&xfrm_policy_lock);
399 for (p = &xfrm_policy_list[id & 7]; (pol=*p)!=NULL; p = &pol->next) {
400 if (pol->index == id) {
407 write_unlock_bh(&xfrm_policy_lock);
410 atomic_inc(&flow_cache_genid);
411 xfrm_policy_kill(pol);
416 void xfrm_policy_flush(void)
418 struct xfrm_policy *xp;
421 write_lock_bh(&xfrm_policy_lock);
422 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
423 while ((xp = xfrm_policy_list[dir]) != NULL) {
424 xfrm_policy_list[dir] = xp->next;
425 write_unlock_bh(&xfrm_policy_lock);
427 xfrm_policy_kill(xp);
429 write_lock_bh(&xfrm_policy_lock);
432 atomic_inc(&flow_cache_genid);
433 write_unlock_bh(&xfrm_policy_lock);
436 int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
439 struct xfrm_policy *xp;
444 read_lock_bh(&xfrm_policy_lock);
445 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
446 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
455 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
456 for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
457 error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
464 read_unlock_bh(&xfrm_policy_lock);
469 /* Find policy to apply to this flow. */
471 static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
472 void **objp, atomic_t **obj_refp)
474 struct xfrm_policy *pol;
476 read_lock_bh(&xfrm_policy_lock);
477 for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
478 struct xfrm_selector *sel = &pol->selector;
481 if (pol->family != family)
484 match = xfrm_selector_match(sel, fl, family);
490 read_unlock_bh(&xfrm_policy_lock);
491 if ((*objp = (void *) pol) != NULL)
492 *obj_refp = &pol->refcnt;
495 struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
497 struct xfrm_policy *pol;
499 read_lock_bh(&xfrm_policy_lock);
500 if ((pol = sk->sk_policy[dir]) != NULL) {
501 int match = xfrm_selector_match(&pol->selector, fl,
508 read_unlock_bh(&xfrm_policy_lock);
512 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
514 pol->next = xfrm_policy_list[dir];
515 xfrm_policy_list[dir] = pol;
519 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
522 struct xfrm_policy **polp;
524 for (polp = &xfrm_policy_list[dir];
525 *polp != NULL; polp = &(*polp)->next) {
534 void xfrm_policy_delete(struct xfrm_policy *pol, int dir)
536 write_lock_bh(&xfrm_policy_lock);
537 pol = __xfrm_policy_unlink(pol, dir);
538 write_unlock_bh(&xfrm_policy_lock);
540 if (dir < XFRM_POLICY_MAX)
541 atomic_inc(&flow_cache_genid);
542 xfrm_policy_kill(pol);
546 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
548 struct xfrm_policy *old_pol;
550 write_lock_bh(&xfrm_policy_lock);
551 old_pol = sk->sk_policy[dir];
552 sk->sk_policy[dir] = pol;
554 pol->curlft.add_time = (unsigned long)xtime.tv_sec;
555 pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
556 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
559 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
560 write_unlock_bh(&xfrm_policy_lock);
563 xfrm_policy_kill(old_pol);
568 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
570 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
573 newp->selector = old->selector;
574 newp->lft = old->lft;
575 newp->curlft = old->curlft;
576 newp->action = old->action;
577 newp->flags = old->flags;
578 newp->xfrm_nr = old->xfrm_nr;
579 newp->index = old->index;
580 memcpy(newp->xfrm_vec, old->xfrm_vec,
581 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
582 write_lock_bh(&xfrm_policy_lock);
583 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
584 write_unlock_bh(&xfrm_policy_lock);
590 int __xfrm_sk_clone_policy(struct sock *sk)
592 struct xfrm_policy *p0 = sk->sk_policy[0],
593 *p1 = sk->sk_policy[1];
595 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
596 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
598 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
603 /* Resolve list of templates for the flow, given policy. */
606 xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
607 struct xfrm_state **xfrm,
608 unsigned short family)
612 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
613 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
615 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
616 struct xfrm_state *x;
617 xfrm_address_t *remote = daddr;
618 xfrm_address_t *local = saddr;
619 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
622 remote = &tmpl->id.daddr;
623 local = &tmpl->saddr;
626 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
628 if (x && x->km.state == XFRM_STATE_VALID) {
635 error = (x->km.state == XFRM_STATE_ERROR ?
646 for (nx--; nx>=0; nx--)
647 xfrm_state_put(xfrm[nx]);
651 /* Check that the bundle accepts the flow and its components are
655 static struct dst_entry *
656 xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
659 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
660 if (unlikely(afinfo == NULL))
661 return ERR_PTR(-EINVAL);
662 x = afinfo->find_bundle(fl, policy);
663 xfrm_policy_put_afinfo(afinfo);
667 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
668 * all the metrics... Shortly, bundle a bundle.
672 xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
673 struct flowi *fl, struct dst_entry **dst_p,
674 unsigned short family)
677 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
678 if (unlikely(afinfo == NULL))
680 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
681 xfrm_policy_put_afinfo(afinfo);
685 static inline int policy_to_flow_dir(int dir)
687 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
688 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
689 XFRM_POLICY_FWD == FLOW_DIR_FWD)
695 case XFRM_POLICY_OUT:
697 case XFRM_POLICY_FWD:
702 static int stale_bundle(struct dst_entry *dst);
704 /* Main function: finds/creates a bundle for given flow.
706 * At the moment we eat a raw IP route. Mostly to speed up lookups
707 * on interfaces with disabled IPsec.
709 int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
710 struct sock *sk, int flags)
712 struct xfrm_policy *policy;
713 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
714 struct dst_entry *dst, *dst_orig = *dst_p;
718 u16 family = dst_orig->ops->family;
720 genid = atomic_read(&flow_cache_genid);
722 if (sk && sk->sk_policy[1])
723 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
726 /* To accelerate a bit... */
727 if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
730 policy = flow_cache_lookup(fl, family,
731 policy_to_flow_dir(XFRM_POLICY_OUT),
738 policy->curlft.use_time = (unsigned long)xtime.tv_sec;
740 switch (policy->action) {
741 case XFRM_POLICY_BLOCK:
742 /* Prohibit the flow */
743 xfrm_pol_put(policy);
746 case XFRM_POLICY_ALLOW:
747 if (policy->xfrm_nr == 0) {
748 /* Flow passes not transformed. */
749 xfrm_pol_put(policy);
753 /* Try to find matching bundle.
755 * LATER: help from flow cache. It is optional, this
756 * is required only for output policy.
758 dst = xfrm_find_bundle(fl, policy, family);
760 xfrm_pol_put(policy);
767 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
769 if (unlikely(nx<0)) {
771 if (err == -EAGAIN && flags) {
772 DECLARE_WAITQUEUE(wait, current);
774 add_wait_queue(&km_waitq, &wait);
775 set_current_state(TASK_INTERRUPTIBLE);
777 set_current_state(TASK_RUNNING);
778 remove_wait_queue(&km_waitq, &wait);
780 nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
782 if (nx == -EAGAIN && signal_pending(current)) {
787 genid != atomic_read(&flow_cache_genid)) {
788 xfrm_pol_put(policy);
797 /* Flow passes not transformed. */
798 xfrm_pol_put(policy);
803 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
808 xfrm_state_put(xfrm[i]);
812 write_lock_bh(&policy->lock);
813 if (unlikely(policy->dead || stale_bundle(dst))) {
814 /* Wow! While we worked on resolving, this
815 * policy has gone. Retry. It is not paranoia,
816 * we just cannot enlist new bundle to dead object.
817 * We can't enlist stable bundles either.
819 write_unlock_bh(&policy->lock);
821 xfrm_pol_put(policy);
826 dst->next = policy->bundles;
827 policy->bundles = dst;
829 write_unlock_bh(&policy->lock);
832 dst_release(dst_orig);
833 xfrm_pol_put(policy);
837 dst_release(dst_orig);
838 xfrm_pol_put(policy);
843 /* When skb is transformed back to its "native" form, we have to
844 * check policy restrictions. At the moment we make this in maximally
845 * stupid way. Shame on me. :-) Of course, connected sockets must
846 * have policy cached at them.
850 xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
851 unsigned short family)
853 if (xfrm_state_kern(x))
854 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
855 return x->id.proto == tmpl->id.proto &&
856 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
857 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
858 x->props.mode == tmpl->mode &&
859 (tmpl->aalgos & (1<<x->props.aalgo)) &&
860 !(x->props.mode && xfrm_state_addr_cmp(tmpl, x, family));
864 xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
865 unsigned short family)
869 if (tmpl->optional) {
874 for (; idx < sp->len; idx++) {
875 if (xfrm_state_ok(tmpl, sp->x[idx].xvec, family))
877 if (sp->x[idx].xvec->props.mode)
884 _decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
886 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
888 if (unlikely(afinfo == NULL))
889 return -EAFNOSUPPORT;
891 afinfo->decode_session(skb, fl);
892 xfrm_policy_put_afinfo(afinfo);
896 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
897 unsigned short family)
899 struct xfrm_policy *pol;
902 if (_decode_session(skb, &fl, family) < 0)
905 /* First, check used SA against their selectors. */
909 for (i=skb->sp->len-1; i>=0; i--) {
910 struct sec_decap_state *xvec = &(skb->sp->x[i]);
911 if (!xfrm_selector_match(&xvec->xvec->sel, &fl, family))
914 /* If there is a post_input processor, try running it */
915 if (xvec->xvec->type->post_input &&
916 (xvec->xvec->type->post_input)(xvec->xvec,
924 if (sk && sk->sk_policy[dir])
925 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
928 pol = flow_cache_lookup(&fl, family,
929 policy_to_flow_dir(dir),
935 pol->curlft.use_time = (unsigned long)xtime.tv_sec;
937 if (pol->action == XFRM_POLICY_ALLOW) {
939 static struct sec_path dummy;
942 if ((sp = skb->sp) == NULL)
945 /* For each tunnel xfrm, find the first matching tmpl.
946 * For each tmpl before that, find corresponding xfrm.
947 * Order is _important_. Later we will implement
948 * some barriers, but at the moment barriers
949 * are implied between each two transformations.
951 for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
952 k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
957 for (; k < sp->len; k++) {
958 if (sp->x[k].xvec->props.mode)
971 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
975 if (_decode_session(skb, &fl, family) < 0)
978 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
981 /* Optimize later using cookies and generation ids. */
983 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
985 if (!stale_bundle(dst))
992 static int stale_bundle(struct dst_entry *dst)
994 struct dst_entry *child = dst;
997 if (child->obsolete > 0 ||
998 (child->dev && !netif_running(child->dev)) ||
999 (child->xfrm && child->xfrm->km.state != XFRM_STATE_VALID)) {
1002 child = child->child;
1008 static void xfrm_dst_destroy(struct dst_entry *dst)
1012 xfrm_state_put(dst->xfrm);
1016 static void xfrm_link_failure(struct sk_buff *skb)
1018 /* Impossible. Such dst must be popped before reaches point of failure. */
1022 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1025 if (dst->obsolete) {
1033 static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1036 struct xfrm_policy *pol;
1037 struct dst_entry *dst, **dstp, *gc_list = NULL;
1039 read_lock_bh(&xfrm_policy_lock);
1040 for (i=0; i<2*XFRM_POLICY_MAX; i++) {
1041 for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
1042 write_lock(&pol->lock);
1043 dstp = &pol->bundles;
1044 while ((dst=*dstp) != NULL) {
1047 dst->next = gc_list;
1053 write_unlock(&pol->lock);
1056 read_unlock_bh(&xfrm_policy_lock);
1060 gc_list = dst->next;
1065 static int unused_bundle(struct dst_entry *dst)
1067 return !atomic_read(&dst->__refcnt);
1070 static void __xfrm_garbage_collect(void)
1072 xfrm_prune_bundles(unused_bundle);
1075 int xfrm_flush_bundles(void)
1077 xfrm_prune_bundles(stale_bundle);
1081 /* Well... that's _TASK_. We need to scan through transformation
1082 * list and figure out what mss tcp should generate in order to
1083 * final datagram fit to mtu. Mama mia... :-)
1085 * Apparently, some easy way exists, but we used to choose the most
1086 * bizarre ones. :-) So, raising Kalashnikov... tra-ta-ta.
1088 * Consider this function as something like dark humour. :-)
1090 static int xfrm_get_mss(struct dst_entry *dst, u32 mtu)
1092 int res = mtu - dst->header_len;
1095 struct dst_entry *d = dst;
1099 struct xfrm_state *x = d->xfrm;
1101 spin_lock_bh(&x->lock);
1102 if (x->km.state == XFRM_STATE_VALID &&
1103 x->type && x->type->get_max_size)
1104 m = x->type->get_max_size(d->xfrm, m);
1106 m += x->props.header_len;
1107 spin_unlock_bh(&x->lock);
1109 } while ((d = d->child) != NULL);
1118 return res + dst->header_len;
1121 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
1124 if (unlikely(afinfo == NULL))
1126 if (unlikely(afinfo->family >= NPROTO))
1127 return -EAFNOSUPPORT;
1128 write_lock(&xfrm_policy_afinfo_lock);
1129 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
1132 struct dst_ops *dst_ops = afinfo->dst_ops;
1133 if (likely(dst_ops->kmem_cachep == NULL))
1134 dst_ops->kmem_cachep = xfrm_dst_cache;
1135 if (likely(dst_ops->check == NULL))
1136 dst_ops->check = xfrm_dst_check;
1137 if (likely(dst_ops->destroy == NULL))
1138 dst_ops->destroy = xfrm_dst_destroy;
1139 if (likely(dst_ops->negative_advice == NULL))
1140 dst_ops->negative_advice = xfrm_negative_advice;
1141 if (likely(dst_ops->link_failure == NULL))
1142 dst_ops->link_failure = xfrm_link_failure;
1143 if (likely(dst_ops->get_mss == NULL))
1144 dst_ops->get_mss = xfrm_get_mss;
1145 if (likely(afinfo->garbage_collect == NULL))
1146 afinfo->garbage_collect = __xfrm_garbage_collect;
1147 xfrm_policy_afinfo[afinfo->family] = afinfo;
1149 write_unlock(&xfrm_policy_afinfo_lock);
1153 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
1156 if (unlikely(afinfo == NULL))
1158 if (unlikely(afinfo->family >= NPROTO))
1159 return -EAFNOSUPPORT;
1160 write_lock(&xfrm_policy_afinfo_lock);
1161 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
1162 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
1165 struct dst_ops *dst_ops = afinfo->dst_ops;
1166 xfrm_policy_afinfo[afinfo->family] = NULL;
1167 dst_ops->kmem_cachep = NULL;
1168 dst_ops->check = NULL;
1169 dst_ops->destroy = NULL;
1170 dst_ops->negative_advice = NULL;
1171 dst_ops->link_failure = NULL;
1172 dst_ops->get_mss = NULL;
1173 afinfo->garbage_collect = NULL;
1176 write_unlock(&xfrm_policy_afinfo_lock);
1180 struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
1182 struct xfrm_policy_afinfo *afinfo;
1183 if (unlikely(family >= NPROTO))
1185 read_lock(&xfrm_policy_afinfo_lock);
1186 afinfo = xfrm_policy_afinfo[family];
1187 if (likely(afinfo != NULL))
1188 read_lock(&afinfo->lock);
1189 read_unlock(&xfrm_policy_afinfo_lock);
1193 void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
1195 if (unlikely(afinfo == NULL))
1197 read_unlock(&afinfo->lock);
1200 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
1204 xfrm_flush_bundles();
1209 struct notifier_block xfrm_dev_notifier = {
1215 void __init xfrm_policy_init(void)
1217 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1218 sizeof(struct xfrm_dst),
1219 0, SLAB_HWCACHE_ALIGN,
1221 if (!xfrm_dst_cache)
1222 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1224 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
1225 register_netdevice_notifier(&xfrm_dev_notifier);
1228 void __init xfrm_init(void)