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/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
21 #include <linux/notifier.h>
22 #include <linux/netdevice.h>
23 #include <linux/netfilter.h>
24 #include <linux/module.h>
28 DEFINE_MUTEX(xfrm_cfg_mutex
);
29 EXPORT_SYMBOL(xfrm_cfg_mutex
);
31 static DEFINE_RWLOCK(xfrm_policy_lock
);
33 struct xfrm_policy
*xfrm_policy_list
[XFRM_POLICY_MAX
*2];
34 EXPORT_SYMBOL(xfrm_policy_list
);
36 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock
);
37 static struct xfrm_policy_afinfo
*xfrm_policy_afinfo
[NPROTO
];
39 static kmem_cache_t
*xfrm_dst_cache __read_mostly
;
41 static struct work_struct xfrm_policy_gc_work
;
42 static struct list_head xfrm_policy_gc_list
=
43 LIST_HEAD_INIT(xfrm_policy_gc_list
);
44 static DEFINE_SPINLOCK(xfrm_policy_gc_lock
);
46 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
);
47 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
);
48 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
);
49 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
);
51 int xfrm_register_type(struct xfrm_type
*type
, unsigned short family
)
53 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
54 struct xfrm_type
**typemap
;
57 if (unlikely(afinfo
== NULL
))
59 typemap
= afinfo
->type_map
;
61 if (likely(typemap
[type
->proto
] == NULL
))
62 typemap
[type
->proto
] = type
;
65 xfrm_policy_unlock_afinfo(afinfo
);
68 EXPORT_SYMBOL(xfrm_register_type
);
70 int xfrm_unregister_type(struct xfrm_type
*type
, unsigned short family
)
72 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
73 struct xfrm_type
**typemap
;
76 if (unlikely(afinfo
== NULL
))
78 typemap
= afinfo
->type_map
;
80 if (unlikely(typemap
[type
->proto
] != type
))
83 typemap
[type
->proto
] = NULL
;
84 xfrm_policy_unlock_afinfo(afinfo
);
87 EXPORT_SYMBOL(xfrm_unregister_type
);
89 struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
91 struct xfrm_policy_afinfo
*afinfo
;
92 struct xfrm_type
**typemap
;
93 struct xfrm_type
*type
;
94 int modload_attempted
= 0;
97 afinfo
= xfrm_policy_get_afinfo(family
);
98 if (unlikely(afinfo
== NULL
))
100 typemap
= afinfo
->type_map
;
102 type
= typemap
[proto
];
103 if (unlikely(type
&& !try_module_get(type
->owner
)))
105 if (!type
&& !modload_attempted
) {
106 xfrm_policy_put_afinfo(afinfo
);
107 request_module("xfrm-type-%d-%d",
108 (int) family
, (int) proto
);
109 modload_attempted
= 1;
113 xfrm_policy_put_afinfo(afinfo
);
117 int xfrm_dst_lookup(struct xfrm_dst
**dst
, struct flowi
*fl
,
118 unsigned short family
)
120 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
123 if (unlikely(afinfo
== NULL
))
124 return -EAFNOSUPPORT
;
126 if (likely(afinfo
->dst_lookup
!= NULL
))
127 err
= afinfo
->dst_lookup(dst
, fl
);
130 xfrm_policy_put_afinfo(afinfo
);
133 EXPORT_SYMBOL(xfrm_dst_lookup
);
135 void xfrm_put_type(struct xfrm_type
*type
)
137 module_put(type
->owner
);
140 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
142 struct xfrm_policy_afinfo
*afinfo
;
143 struct xfrm_mode
**modemap
;
146 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
149 afinfo
= xfrm_policy_lock_afinfo(family
);
150 if (unlikely(afinfo
== NULL
))
151 return -EAFNOSUPPORT
;
154 modemap
= afinfo
->mode_map
;
155 if (likely(modemap
[mode
->encap
] == NULL
)) {
156 modemap
[mode
->encap
] = mode
;
160 xfrm_policy_unlock_afinfo(afinfo
);
163 EXPORT_SYMBOL(xfrm_register_mode
);
165 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
167 struct xfrm_policy_afinfo
*afinfo
;
168 struct xfrm_mode
**modemap
;
171 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
174 afinfo
= xfrm_policy_lock_afinfo(family
);
175 if (unlikely(afinfo
== NULL
))
176 return -EAFNOSUPPORT
;
179 modemap
= afinfo
->mode_map
;
180 if (likely(modemap
[mode
->encap
] == mode
)) {
181 modemap
[mode
->encap
] = NULL
;
185 xfrm_policy_unlock_afinfo(afinfo
);
188 EXPORT_SYMBOL(xfrm_unregister_mode
);
190 struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
192 struct xfrm_policy_afinfo
*afinfo
;
193 struct xfrm_mode
*mode
;
194 int modload_attempted
= 0;
196 if (unlikely(encap
>= XFRM_MODE_MAX
))
200 afinfo
= xfrm_policy_get_afinfo(family
);
201 if (unlikely(afinfo
== NULL
))
204 mode
= afinfo
->mode_map
[encap
];
205 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
207 if (!mode
&& !modload_attempted
) {
208 xfrm_policy_put_afinfo(afinfo
);
209 request_module("xfrm-mode-%d-%d", family
, encap
);
210 modload_attempted
= 1;
214 xfrm_policy_put_afinfo(afinfo
);
218 void xfrm_put_mode(struct xfrm_mode
*mode
)
220 module_put(mode
->owner
);
223 static inline unsigned long make_jiffies(long secs
)
225 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
226 return MAX_SCHEDULE_TIMEOUT
-1;
231 static void xfrm_policy_timer(unsigned long data
)
233 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
234 unsigned long now
= (unsigned long)xtime
.tv_sec
;
235 long next
= LONG_MAX
;
239 read_lock(&xp
->lock
);
244 dir
= xfrm_policy_id2dir(xp
->index
);
246 if (xp
->lft
.hard_add_expires_seconds
) {
247 long tmo
= xp
->lft
.hard_add_expires_seconds
+
248 xp
->curlft
.add_time
- now
;
254 if (xp
->lft
.hard_use_expires_seconds
) {
255 long tmo
= xp
->lft
.hard_use_expires_seconds
+
256 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
262 if (xp
->lft
.soft_add_expires_seconds
) {
263 long tmo
= xp
->lft
.soft_add_expires_seconds
+
264 xp
->curlft
.add_time
- now
;
267 tmo
= XFRM_KM_TIMEOUT
;
272 if (xp
->lft
.soft_use_expires_seconds
) {
273 long tmo
= xp
->lft
.soft_use_expires_seconds
+
274 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
277 tmo
= XFRM_KM_TIMEOUT
;
284 km_policy_expired(xp
, dir
, 0, 0);
285 if (next
!= LONG_MAX
&&
286 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
290 read_unlock(&xp
->lock
);
295 read_unlock(&xp
->lock
);
296 if (!xfrm_policy_delete(xp
, dir
))
297 km_policy_expired(xp
, dir
, 1, 0);
302 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
306 struct xfrm_policy
*xfrm_policy_alloc(gfp_t gfp
)
308 struct xfrm_policy
*policy
;
310 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
313 atomic_set(&policy
->refcnt
, 1);
314 rwlock_init(&policy
->lock
);
315 init_timer(&policy
->timer
);
316 policy
->timer
.data
= (unsigned long)policy
;
317 policy
->timer
.function
= xfrm_policy_timer
;
321 EXPORT_SYMBOL(xfrm_policy_alloc
);
323 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
325 void __xfrm_policy_destroy(struct xfrm_policy
*policy
)
327 BUG_ON(!policy
->dead
);
329 BUG_ON(policy
->bundles
);
331 if (del_timer(&policy
->timer
))
334 security_xfrm_policy_free(policy
);
337 EXPORT_SYMBOL(__xfrm_policy_destroy
);
339 static void xfrm_policy_gc_kill(struct xfrm_policy
*policy
)
341 struct dst_entry
*dst
;
343 while ((dst
= policy
->bundles
) != NULL
) {
344 policy
->bundles
= dst
->next
;
348 if (del_timer(&policy
->timer
))
349 atomic_dec(&policy
->refcnt
);
351 if (atomic_read(&policy
->refcnt
) > 1)
354 xfrm_pol_put(policy
);
357 static void xfrm_policy_gc_task(void *data
)
359 struct xfrm_policy
*policy
;
360 struct list_head
*entry
, *tmp
;
361 struct list_head gc_list
= LIST_HEAD_INIT(gc_list
);
363 spin_lock_bh(&xfrm_policy_gc_lock
);
364 list_splice_init(&xfrm_policy_gc_list
, &gc_list
);
365 spin_unlock_bh(&xfrm_policy_gc_lock
);
367 list_for_each_safe(entry
, tmp
, &gc_list
) {
368 policy
= list_entry(entry
, struct xfrm_policy
, list
);
369 xfrm_policy_gc_kill(policy
);
373 /* Rule must be locked. Release descentant resources, announce
374 * entry dead. The rule must be unlinked from lists to the moment.
377 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
381 write_lock_bh(&policy
->lock
);
384 write_unlock_bh(&policy
->lock
);
386 if (unlikely(dead
)) {
391 spin_lock(&xfrm_policy_gc_lock
);
392 list_add(&policy
->list
, &xfrm_policy_gc_list
);
393 spin_unlock(&xfrm_policy_gc_lock
);
395 schedule_work(&xfrm_policy_gc_work
);
398 /* Generate new index... KAME seems to generate them ordered by cost
399 * of an absolute inpredictability of ordering of rules. This will not pass. */
400 static u32
xfrm_gen_index(int dir
)
403 struct xfrm_policy
*p
;
404 static u32 idx_generator
;
407 idx
= (idx_generator
| dir
);
411 for (p
= xfrm_policy_list
[dir
]; p
; p
= p
->next
) {
420 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
422 struct xfrm_policy
*pol
, **p
;
423 struct xfrm_policy
*delpol
= NULL
;
424 struct xfrm_policy
**newpos
= NULL
;
425 struct dst_entry
*gc_list
;
427 write_lock_bh(&xfrm_policy_lock
);
428 for (p
= &xfrm_policy_list
[dir
]; (pol
=*p
)!=NULL
;) {
429 if (!delpol
&& memcmp(&policy
->selector
, &pol
->selector
, sizeof(pol
->selector
)) == 0 &&
430 xfrm_sec_ctx_match(pol
->security
, policy
->security
)) {
432 write_unlock_bh(&xfrm_policy_lock
);
437 if (policy
->priority
> pol
->priority
)
439 } else if (policy
->priority
>= pol
->priority
) {
451 xfrm_pol_hold(policy
);
454 atomic_inc(&flow_cache_genid
);
455 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(dir
);
456 policy
->curlft
.add_time
= (unsigned long)xtime
.tv_sec
;
457 policy
->curlft
.use_time
= 0;
458 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
459 xfrm_pol_hold(policy
);
460 write_unlock_bh(&xfrm_policy_lock
);
463 xfrm_policy_kill(delpol
);
465 read_lock_bh(&xfrm_policy_lock
);
467 for (policy
= policy
->next
; policy
; policy
= policy
->next
) {
468 struct dst_entry
*dst
;
470 write_lock(&policy
->lock
);
471 dst
= policy
->bundles
;
473 struct dst_entry
*tail
= dst
;
476 tail
->next
= gc_list
;
479 policy
->bundles
= NULL
;
481 write_unlock(&policy
->lock
);
483 read_unlock_bh(&xfrm_policy_lock
);
486 struct dst_entry
*dst
= gc_list
;
494 EXPORT_SYMBOL(xfrm_policy_insert
);
496 struct xfrm_policy
*xfrm_policy_bysel_ctx(int dir
, struct xfrm_selector
*sel
,
497 struct xfrm_sec_ctx
*ctx
, int delete)
499 struct xfrm_policy
*pol
, **p
;
501 write_lock_bh(&xfrm_policy_lock
);
502 for (p
= &xfrm_policy_list
[dir
]; (pol
=*p
)!=NULL
; p
= &pol
->next
) {
503 if ((memcmp(sel
, &pol
->selector
, sizeof(*sel
)) == 0) &&
504 (xfrm_sec_ctx_match(ctx
, pol
->security
))) {
511 write_unlock_bh(&xfrm_policy_lock
);
514 atomic_inc(&flow_cache_genid
);
515 xfrm_policy_kill(pol
);
519 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
521 struct xfrm_policy
*xfrm_policy_byid(int dir
, u32 id
, int delete)
523 struct xfrm_policy
*pol
, **p
;
525 write_lock_bh(&xfrm_policy_lock
);
526 for (p
= &xfrm_policy_list
[dir
]; (pol
=*p
)!=NULL
; p
= &pol
->next
) {
527 if (pol
->index
== id
) {
534 write_unlock_bh(&xfrm_policy_lock
);
537 atomic_inc(&flow_cache_genid
);
538 xfrm_policy_kill(pol
);
542 EXPORT_SYMBOL(xfrm_policy_byid
);
544 void xfrm_policy_flush(void)
546 struct xfrm_policy
*xp
;
549 write_lock_bh(&xfrm_policy_lock
);
550 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
551 while ((xp
= xfrm_policy_list
[dir
]) != NULL
) {
552 xfrm_policy_list
[dir
] = xp
->next
;
553 write_unlock_bh(&xfrm_policy_lock
);
555 xfrm_policy_kill(xp
);
557 write_lock_bh(&xfrm_policy_lock
);
560 atomic_inc(&flow_cache_genid
);
561 write_unlock_bh(&xfrm_policy_lock
);
563 EXPORT_SYMBOL(xfrm_policy_flush
);
565 int xfrm_policy_walk(int (*func
)(struct xfrm_policy
*, int, int, void*),
568 struct xfrm_policy
*xp
;
573 read_lock_bh(&xfrm_policy_lock
);
574 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
575 for (xp
= xfrm_policy_list
[dir
]; xp
; xp
= xp
->next
)
584 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
585 for (xp
= xfrm_policy_list
[dir
]; xp
; xp
= xp
->next
) {
586 error
= func(xp
, dir
%XFRM_POLICY_MAX
, --count
, data
);
593 read_unlock_bh(&xfrm_policy_lock
);
596 EXPORT_SYMBOL(xfrm_policy_walk
);
598 /* Find policy to apply to this flow. */
600 static void xfrm_policy_lookup(struct flowi
*fl
, u32 sk_sid
, u16 family
, u8 dir
,
601 void **objp
, atomic_t
**obj_refp
)
603 struct xfrm_policy
*pol
;
605 read_lock_bh(&xfrm_policy_lock
);
606 for (pol
= xfrm_policy_list
[dir
]; pol
; pol
= pol
->next
) {
607 struct xfrm_selector
*sel
= &pol
->selector
;
610 if (pol
->family
!= family
)
613 match
= xfrm_selector_match(sel
, fl
, family
);
616 if (!security_xfrm_policy_lookup(pol
, sk_sid
, dir
)) {
622 read_unlock_bh(&xfrm_policy_lock
);
623 if ((*objp
= (void *) pol
) != NULL
)
624 *obj_refp
= &pol
->refcnt
;
627 static inline int policy_to_flow_dir(int dir
)
629 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
630 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
631 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
637 case XFRM_POLICY_OUT
:
639 case XFRM_POLICY_FWD
:
644 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
, u32 sk_sid
)
646 struct xfrm_policy
*pol
;
648 read_lock_bh(&xfrm_policy_lock
);
649 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
650 int match
= xfrm_selector_match(&pol
->selector
, fl
,
655 err
= security_xfrm_policy_lookup(pol
, sk_sid
, policy_to_flow_dir(dir
));
662 read_unlock_bh(&xfrm_policy_lock
);
666 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
668 pol
->next
= xfrm_policy_list
[dir
];
669 xfrm_policy_list
[dir
] = pol
;
673 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
676 struct xfrm_policy
**polp
;
678 for (polp
= &xfrm_policy_list
[dir
];
679 *polp
!= NULL
; polp
= &(*polp
)->next
) {
688 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
690 write_lock_bh(&xfrm_policy_lock
);
691 pol
= __xfrm_policy_unlink(pol
, dir
);
692 write_unlock_bh(&xfrm_policy_lock
);
694 if (dir
< XFRM_POLICY_MAX
)
695 atomic_inc(&flow_cache_genid
);
696 xfrm_policy_kill(pol
);
701 EXPORT_SYMBOL(xfrm_policy_delete
);
703 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
705 struct xfrm_policy
*old_pol
;
707 write_lock_bh(&xfrm_policy_lock
);
708 old_pol
= sk
->sk_policy
[dir
];
709 sk
->sk_policy
[dir
] = pol
;
711 pol
->curlft
.add_time
= (unsigned long)xtime
.tv_sec
;
712 pol
->index
= xfrm_gen_index(XFRM_POLICY_MAX
+dir
);
713 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
716 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
717 write_unlock_bh(&xfrm_policy_lock
);
720 xfrm_policy_kill(old_pol
);
725 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
727 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
730 newp
->selector
= old
->selector
;
731 if (security_xfrm_policy_clone(old
, newp
)) {
733 return NULL
; /* ENOMEM */
735 newp
->lft
= old
->lft
;
736 newp
->curlft
= old
->curlft
;
737 newp
->action
= old
->action
;
738 newp
->flags
= old
->flags
;
739 newp
->xfrm_nr
= old
->xfrm_nr
;
740 newp
->index
= old
->index
;
741 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
742 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
743 write_lock_bh(&xfrm_policy_lock
);
744 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
745 write_unlock_bh(&xfrm_policy_lock
);
751 int __xfrm_sk_clone_policy(struct sock
*sk
)
753 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
754 *p1
= sk
->sk_policy
[1];
756 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
757 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
759 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
764 /* Resolve list of templates for the flow, given policy. */
767 xfrm_tmpl_resolve(struct xfrm_policy
*policy
, struct flowi
*fl
,
768 struct xfrm_state
**xfrm
,
769 unsigned short family
)
773 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
774 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
776 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
777 struct xfrm_state
*x
;
778 xfrm_address_t
*remote
= daddr
;
779 xfrm_address_t
*local
= saddr
;
780 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
783 remote
= &tmpl
->id
.daddr
;
784 local
= &tmpl
->saddr
;
787 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
789 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
796 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
807 for (nx
--; nx
>=0; nx
--)
808 xfrm_state_put(xfrm
[nx
]);
812 /* Check that the bundle accepts the flow and its components are
816 static struct dst_entry
*
817 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
820 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
821 if (unlikely(afinfo
== NULL
))
822 return ERR_PTR(-EINVAL
);
823 x
= afinfo
->find_bundle(fl
, policy
);
824 xfrm_policy_put_afinfo(afinfo
);
828 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
829 * all the metrics... Shortly, bundle a bundle.
833 xfrm_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
834 struct flowi
*fl
, struct dst_entry
**dst_p
,
835 unsigned short family
)
838 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
839 if (unlikely(afinfo
== NULL
))
841 err
= afinfo
->bundle_create(policy
, xfrm
, nx
, fl
, dst_p
);
842 xfrm_policy_put_afinfo(afinfo
);
847 static int stale_bundle(struct dst_entry
*dst
);
849 /* Main function: finds/creates a bundle for given flow.
851 * At the moment we eat a raw IP route. Mostly to speed up lookups
852 * on interfaces with disabled IPsec.
854 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
855 struct sock
*sk
, int flags
)
857 struct xfrm_policy
*policy
;
858 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
859 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
864 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
865 u32 sk_sid
= security_sk_sid(sk
, fl
, dir
);
867 genid
= atomic_read(&flow_cache_genid
);
869 if (sk
&& sk
->sk_policy
[1])
870 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
, sk_sid
);
873 /* To accelerate a bit... */
874 if ((dst_orig
->flags
& DST_NOXFRM
) || !xfrm_policy_list
[XFRM_POLICY_OUT
])
877 policy
= flow_cache_lookup(fl
, sk_sid
, dst_orig
->ops
->family
,
878 dir
, xfrm_policy_lookup
);
884 family
= dst_orig
->ops
->family
;
885 policy
->curlft
.use_time
= (unsigned long)xtime
.tv_sec
;
887 switch (policy
->action
) {
888 case XFRM_POLICY_BLOCK
:
889 /* Prohibit the flow */
893 case XFRM_POLICY_ALLOW
:
894 if (policy
->xfrm_nr
== 0) {
895 /* Flow passes not transformed. */
896 xfrm_pol_put(policy
);
900 /* Try to find matching bundle.
902 * LATER: help from flow cache. It is optional, this
903 * is required only for output policy.
905 dst
= xfrm_find_bundle(fl
, policy
, family
);
914 nx
= xfrm_tmpl_resolve(policy
, fl
, xfrm
, family
);
916 if (unlikely(nx
<0)) {
918 if (err
== -EAGAIN
&& flags
) {
919 DECLARE_WAITQUEUE(wait
, current
);
921 add_wait_queue(&km_waitq
, &wait
);
922 set_current_state(TASK_INTERRUPTIBLE
);
924 set_current_state(TASK_RUNNING
);
925 remove_wait_queue(&km_waitq
, &wait
);
927 nx
= xfrm_tmpl_resolve(policy
, fl
, xfrm
, family
);
929 if (nx
== -EAGAIN
&& signal_pending(current
)) {
934 genid
!= atomic_read(&flow_cache_genid
)) {
935 xfrm_pol_put(policy
);
944 /* Flow passes not transformed. */
945 xfrm_pol_put(policy
);
950 err
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, &dst
, family
);
955 xfrm_state_put(xfrm
[i
]);
959 write_lock_bh(&policy
->lock
);
960 if (unlikely(policy
->dead
|| stale_bundle(dst
))) {
961 /* Wow! While we worked on resolving, this
962 * policy has gone. Retry. It is not paranoia,
963 * we just cannot enlist new bundle to dead object.
964 * We can't enlist stable bundles either.
966 write_unlock_bh(&policy
->lock
);
973 dst
->next
= policy
->bundles
;
974 policy
->bundles
= dst
;
976 write_unlock_bh(&policy
->lock
);
979 dst_release(dst_orig
);
980 xfrm_pol_put(policy
);
984 dst_release(dst_orig
);
985 xfrm_pol_put(policy
);
989 EXPORT_SYMBOL(xfrm_lookup
);
991 /* When skb is transformed back to its "native" form, we have to
992 * check policy restrictions. At the moment we make this in maximally
993 * stupid way. Shame on me. :-) Of course, connected sockets must
994 * have policy cached at them.
998 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
999 unsigned short family
)
1001 if (xfrm_state_kern(x
))
1002 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, family
);
1003 return x
->id
.proto
== tmpl
->id
.proto
&&
1004 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1005 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1006 x
->props
.mode
== tmpl
->mode
&&
1007 (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) &&
1008 !(x
->props
.mode
&& xfrm_state_addr_cmp(tmpl
, x
, family
));
1012 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1013 unsigned short family
)
1017 if (tmpl
->optional
) {
1022 for (; idx
< sp
->len
; idx
++) {
1023 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1025 if (sp
->xvec
[idx
]->props
.mode
)
1032 xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, unsigned short family
)
1034 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1036 if (unlikely(afinfo
== NULL
))
1037 return -EAFNOSUPPORT
;
1039 afinfo
->decode_session(skb
, fl
);
1040 xfrm_policy_put_afinfo(afinfo
);
1043 EXPORT_SYMBOL(xfrm_decode_session
);
1045 static inline int secpath_has_tunnel(struct sec_path
*sp
, int k
)
1047 for (; k
< sp
->len
; k
++) {
1048 if (sp
->xvec
[k
]->props
.mode
)
1055 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1056 unsigned short family
)
1058 struct xfrm_policy
*pol
;
1060 u8 fl_dir
= policy_to_flow_dir(dir
);
1063 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1065 nf_nat_decode_session(skb
, &fl
, family
);
1067 sk_sid
= security_sk_sid(sk
, &fl
, fl_dir
);
1069 /* First, check used SA against their selectors. */
1073 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1074 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1075 if (!xfrm_selector_match(&x
->sel
, &fl
, family
))
1081 if (sk
&& sk
->sk_policy
[dir
])
1082 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
, sk_sid
);
1085 pol
= flow_cache_lookup(&fl
, sk_sid
, family
, fl_dir
,
1086 xfrm_policy_lookup
);
1089 return !skb
->sp
|| !secpath_has_tunnel(skb
->sp
, 0);
1091 pol
->curlft
.use_time
= (unsigned long)xtime
.tv_sec
;
1093 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1094 struct sec_path
*sp
;
1095 static struct sec_path dummy
;
1098 if ((sp
= skb
->sp
) == NULL
)
1101 /* For each tunnel xfrm, find the first matching tmpl.
1102 * For each tmpl before that, find corresponding xfrm.
1103 * Order is _important_. Later we will implement
1104 * some barriers, but at the moment barriers
1105 * are implied between each two transformations.
1107 for (i
= pol
->xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1108 k
= xfrm_policy_ok(pol
->xfrm_vec
+i
, sp
, k
, family
);
1113 if (secpath_has_tunnel(sp
, k
))
1124 EXPORT_SYMBOL(__xfrm_policy_check
);
1126 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1130 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1133 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
1135 EXPORT_SYMBOL(__xfrm_route_forward
);
1137 /* Optimize later using cookies and generation ids. */
1139 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
1141 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1142 * to "-1" to force all XFRM destinations to get validated by
1143 * dst_ops->check on every use. We do this because when a
1144 * normal route referenced by an XFRM dst is obsoleted we do
1145 * not go looking around for all parent referencing XFRM dsts
1146 * so that we can invalidate them. It is just too much work.
1147 * Instead we make the checks here on every use. For example:
1149 * XFRM dst A --> IPv4 dst X
1151 * X is the "xdst->route" of A (X is also the "dst->path" of A
1152 * in this example). If X is marked obsolete, "A" will not
1153 * notice. That's what we are validating here via the
1154 * stale_bundle() check.
1156 * When a policy's bundle is pruned, we dst_free() the XFRM
1157 * dst which causes it's ->obsolete field to be set to a
1158 * positive non-zero integer. If an XFRM dst has been pruned
1159 * like this, we want to force a new route lookup.
1161 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
1167 static int stale_bundle(struct dst_entry
*dst
)
1169 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
);
1172 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
1174 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
1175 dst
->dev
= &loopback_dev
;
1176 dev_hold(&loopback_dev
);
1180 EXPORT_SYMBOL(xfrm_dst_ifdown
);
1182 static void xfrm_link_failure(struct sk_buff
*skb
)
1184 /* Impossible. Such dst must be popped before reaches point of failure. */
1188 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
1191 if (dst
->obsolete
) {
1199 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
1202 struct xfrm_policy
*pol
;
1203 struct dst_entry
*dst
, **dstp
, *gc_list
= NULL
;
1205 read_lock_bh(&xfrm_policy_lock
);
1206 for (i
=0; i
<2*XFRM_POLICY_MAX
; i
++) {
1207 for (pol
= xfrm_policy_list
[i
]; pol
; pol
= pol
->next
) {
1208 write_lock(&pol
->lock
);
1209 dstp
= &pol
->bundles
;
1210 while ((dst
=*dstp
) != NULL
) {
1213 dst
->next
= gc_list
;
1219 write_unlock(&pol
->lock
);
1222 read_unlock_bh(&xfrm_policy_lock
);
1226 gc_list
= dst
->next
;
1231 static int unused_bundle(struct dst_entry
*dst
)
1233 return !atomic_read(&dst
->__refcnt
);
1236 static void __xfrm_garbage_collect(void)
1238 xfrm_prune_bundles(unused_bundle
);
1241 int xfrm_flush_bundles(void)
1243 xfrm_prune_bundles(stale_bundle
);
1247 static int always_true(struct dst_entry
*dst
)
1252 void xfrm_flush_all_bundles(void)
1254 xfrm_prune_bundles(always_true
);
1257 void xfrm_init_pmtu(struct dst_entry
*dst
)
1260 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1261 u32 pmtu
, route_mtu_cached
;
1263 pmtu
= dst_mtu(dst
->child
);
1264 xdst
->child_mtu_cached
= pmtu
;
1266 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
1268 route_mtu_cached
= dst_mtu(xdst
->route
);
1269 xdst
->route_mtu_cached
= route_mtu_cached
;
1271 if (pmtu
> route_mtu_cached
)
1272 pmtu
= route_mtu_cached
;
1274 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
1275 } while ((dst
= dst
->next
));
1278 EXPORT_SYMBOL(xfrm_init_pmtu
);
1280 /* Check that the bundle accepts the flow and its components are
1284 int xfrm_bundle_ok(struct xfrm_dst
*first
, struct flowi
*fl
, int family
)
1286 struct dst_entry
*dst
= &first
->u
.dst
;
1287 struct xfrm_dst
*last
;
1290 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
1291 (dst
->dev
&& !netif_running(dst
->dev
)))
1297 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1299 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
1301 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
1304 mtu
= dst_mtu(dst
->child
);
1305 if (xdst
->child_mtu_cached
!= mtu
) {
1307 xdst
->child_mtu_cached
= mtu
;
1310 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
1312 mtu
= dst_mtu(xdst
->route
);
1313 if (xdst
->route_mtu_cached
!= mtu
) {
1315 xdst
->route_mtu_cached
= mtu
;
1319 } while (dst
->xfrm
);
1324 mtu
= last
->child_mtu_cached
;
1328 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
1329 if (mtu
> last
->route_mtu_cached
)
1330 mtu
= last
->route_mtu_cached
;
1331 dst
->metrics
[RTAX_MTU
-1] = mtu
;
1336 last
= last
->u
.next
;
1337 last
->child_mtu_cached
= mtu
;
1343 EXPORT_SYMBOL(xfrm_bundle_ok
);
1345 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1348 if (unlikely(afinfo
== NULL
))
1350 if (unlikely(afinfo
->family
>= NPROTO
))
1351 return -EAFNOSUPPORT
;
1352 write_lock_bh(&xfrm_policy_afinfo_lock
);
1353 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
1356 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
1357 if (likely(dst_ops
->kmem_cachep
== NULL
))
1358 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
1359 if (likely(dst_ops
->check
== NULL
))
1360 dst_ops
->check
= xfrm_dst_check
;
1361 if (likely(dst_ops
->negative_advice
== NULL
))
1362 dst_ops
->negative_advice
= xfrm_negative_advice
;
1363 if (likely(dst_ops
->link_failure
== NULL
))
1364 dst_ops
->link_failure
= xfrm_link_failure
;
1365 if (likely(afinfo
->garbage_collect
== NULL
))
1366 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
1367 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
1369 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1372 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
1374 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1377 if (unlikely(afinfo
== NULL
))
1379 if (unlikely(afinfo
->family
>= NPROTO
))
1380 return -EAFNOSUPPORT
;
1381 write_lock_bh(&xfrm_policy_afinfo_lock
);
1382 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
1383 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
1386 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
1387 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
1388 dst_ops
->kmem_cachep
= NULL
;
1389 dst_ops
->check
= NULL
;
1390 dst_ops
->negative_advice
= NULL
;
1391 dst_ops
->link_failure
= NULL
;
1392 afinfo
->garbage_collect
= NULL
;
1395 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1398 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
1400 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
1402 struct xfrm_policy_afinfo
*afinfo
;
1403 if (unlikely(family
>= NPROTO
))
1405 read_lock(&xfrm_policy_afinfo_lock
);
1406 afinfo
= xfrm_policy_afinfo
[family
];
1407 if (unlikely(!afinfo
))
1408 read_unlock(&xfrm_policy_afinfo_lock
);
1412 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1414 read_unlock(&xfrm_policy_afinfo_lock
);
1417 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
)
1419 struct xfrm_policy_afinfo
*afinfo
;
1420 if (unlikely(family
>= NPROTO
))
1422 write_lock_bh(&xfrm_policy_afinfo_lock
);
1423 afinfo
= xfrm_policy_afinfo
[family
];
1424 if (unlikely(!afinfo
))
1425 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1429 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1431 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1434 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1438 xfrm_flush_bundles();
1443 static struct notifier_block xfrm_dev_notifier
= {
1449 static void __init
xfrm_policy_init(void)
1451 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
1452 sizeof(struct xfrm_dst
),
1453 0, SLAB_HWCACHE_ALIGN
,
1455 if (!xfrm_dst_cache
)
1456 panic("XFRM: failed to allocate xfrm_dst_cache\n");
1458 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
, NULL
);
1459 register_netdevice_notifier(&xfrm_dev_notifier
);
1462 void __init
xfrm_init(void)