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>
25 #include <linux/cache.h>
29 #include "xfrm_hash.h"
31 DEFINE_MUTEX(xfrm_cfg_mutex
);
32 EXPORT_SYMBOL(xfrm_cfg_mutex
);
34 static DEFINE_RWLOCK(xfrm_policy_lock
);
36 unsigned int xfrm_policy_count
[XFRM_POLICY_MAX
*2];
37 EXPORT_SYMBOL(xfrm_policy_count
);
39 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock
);
40 static struct xfrm_policy_afinfo
*xfrm_policy_afinfo
[NPROTO
];
42 static kmem_cache_t
*xfrm_dst_cache __read_mostly
;
44 static struct work_struct xfrm_policy_gc_work
;
45 static HLIST_HEAD(xfrm_policy_gc_list
);
46 static DEFINE_SPINLOCK(xfrm_policy_gc_lock
);
48 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
);
49 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
);
50 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
);
51 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
);
53 int xfrm_register_type(struct xfrm_type
*type
, unsigned short family
)
55 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
56 struct xfrm_type
**typemap
;
59 if (unlikely(afinfo
== NULL
))
61 typemap
= afinfo
->type_map
;
63 if (likely(typemap
[type
->proto
] == NULL
))
64 typemap
[type
->proto
] = type
;
67 xfrm_policy_unlock_afinfo(afinfo
);
70 EXPORT_SYMBOL(xfrm_register_type
);
72 int xfrm_unregister_type(struct xfrm_type
*type
, unsigned short family
)
74 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
75 struct xfrm_type
**typemap
;
78 if (unlikely(afinfo
== NULL
))
80 typemap
= afinfo
->type_map
;
82 if (unlikely(typemap
[type
->proto
] != type
))
85 typemap
[type
->proto
] = NULL
;
86 xfrm_policy_unlock_afinfo(afinfo
);
89 EXPORT_SYMBOL(xfrm_unregister_type
);
91 struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
93 struct xfrm_policy_afinfo
*afinfo
;
94 struct xfrm_type
**typemap
;
95 struct xfrm_type
*type
;
96 int modload_attempted
= 0;
99 afinfo
= xfrm_policy_get_afinfo(family
);
100 if (unlikely(afinfo
== NULL
))
102 typemap
= afinfo
->type_map
;
104 type
= typemap
[proto
];
105 if (unlikely(type
&& !try_module_get(type
->owner
)))
107 if (!type
&& !modload_attempted
) {
108 xfrm_policy_put_afinfo(afinfo
);
109 request_module("xfrm-type-%d-%d",
110 (int) family
, (int) proto
);
111 modload_attempted
= 1;
115 xfrm_policy_put_afinfo(afinfo
);
119 int xfrm_dst_lookup(struct xfrm_dst
**dst
, struct flowi
*fl
,
120 unsigned short family
)
122 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
125 if (unlikely(afinfo
== NULL
))
126 return -EAFNOSUPPORT
;
128 if (likely(afinfo
->dst_lookup
!= NULL
))
129 err
= afinfo
->dst_lookup(dst
, fl
);
132 xfrm_policy_put_afinfo(afinfo
);
135 EXPORT_SYMBOL(xfrm_dst_lookup
);
137 void xfrm_put_type(struct xfrm_type
*type
)
139 module_put(type
->owner
);
142 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
144 struct xfrm_policy_afinfo
*afinfo
;
145 struct xfrm_mode
**modemap
;
148 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
151 afinfo
= xfrm_policy_lock_afinfo(family
);
152 if (unlikely(afinfo
== NULL
))
153 return -EAFNOSUPPORT
;
156 modemap
= afinfo
->mode_map
;
157 if (likely(modemap
[mode
->encap
] == NULL
)) {
158 modemap
[mode
->encap
] = mode
;
162 xfrm_policy_unlock_afinfo(afinfo
);
165 EXPORT_SYMBOL(xfrm_register_mode
);
167 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
169 struct xfrm_policy_afinfo
*afinfo
;
170 struct xfrm_mode
**modemap
;
173 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
176 afinfo
= xfrm_policy_lock_afinfo(family
);
177 if (unlikely(afinfo
== NULL
))
178 return -EAFNOSUPPORT
;
181 modemap
= afinfo
->mode_map
;
182 if (likely(modemap
[mode
->encap
] == mode
)) {
183 modemap
[mode
->encap
] = NULL
;
187 xfrm_policy_unlock_afinfo(afinfo
);
190 EXPORT_SYMBOL(xfrm_unregister_mode
);
192 struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
194 struct xfrm_policy_afinfo
*afinfo
;
195 struct xfrm_mode
*mode
;
196 int modload_attempted
= 0;
198 if (unlikely(encap
>= XFRM_MODE_MAX
))
202 afinfo
= xfrm_policy_get_afinfo(family
);
203 if (unlikely(afinfo
== NULL
))
206 mode
= afinfo
->mode_map
[encap
];
207 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
209 if (!mode
&& !modload_attempted
) {
210 xfrm_policy_put_afinfo(afinfo
);
211 request_module("xfrm-mode-%d-%d", family
, encap
);
212 modload_attempted
= 1;
216 xfrm_policy_put_afinfo(afinfo
);
220 void xfrm_put_mode(struct xfrm_mode
*mode
)
222 module_put(mode
->owner
);
225 static inline unsigned long make_jiffies(long secs
)
227 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
228 return MAX_SCHEDULE_TIMEOUT
-1;
233 static void xfrm_policy_timer(unsigned long data
)
235 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
236 unsigned long now
= (unsigned long)xtime
.tv_sec
;
237 long next
= LONG_MAX
;
241 read_lock(&xp
->lock
);
246 dir
= xfrm_policy_id2dir(xp
->index
);
248 if (xp
->lft
.hard_add_expires_seconds
) {
249 long tmo
= xp
->lft
.hard_add_expires_seconds
+
250 xp
->curlft
.add_time
- now
;
256 if (xp
->lft
.hard_use_expires_seconds
) {
257 long tmo
= xp
->lft
.hard_use_expires_seconds
+
258 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
264 if (xp
->lft
.soft_add_expires_seconds
) {
265 long tmo
= xp
->lft
.soft_add_expires_seconds
+
266 xp
->curlft
.add_time
- now
;
269 tmo
= XFRM_KM_TIMEOUT
;
274 if (xp
->lft
.soft_use_expires_seconds
) {
275 long tmo
= xp
->lft
.soft_use_expires_seconds
+
276 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
279 tmo
= XFRM_KM_TIMEOUT
;
286 km_policy_expired(xp
, dir
, 0, 0);
287 if (next
!= LONG_MAX
&&
288 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
292 read_unlock(&xp
->lock
);
297 read_unlock(&xp
->lock
);
298 if (!xfrm_policy_delete(xp
, dir
))
299 km_policy_expired(xp
, dir
, 1, 0);
304 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
308 struct xfrm_policy
*xfrm_policy_alloc(gfp_t gfp
)
310 struct xfrm_policy
*policy
;
312 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
315 INIT_HLIST_NODE(&policy
->bydst
);
316 INIT_HLIST_NODE(&policy
->byidx
);
317 rwlock_init(&policy
->lock
);
318 atomic_set(&policy
->refcnt
, 1);
319 init_timer(&policy
->timer
);
320 policy
->timer
.data
= (unsigned long)policy
;
321 policy
->timer
.function
= xfrm_policy_timer
;
325 EXPORT_SYMBOL(xfrm_policy_alloc
);
327 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
329 void __xfrm_policy_destroy(struct xfrm_policy
*policy
)
331 BUG_ON(!policy
->dead
);
333 BUG_ON(policy
->bundles
);
335 if (del_timer(&policy
->timer
))
338 security_xfrm_policy_free(policy
);
341 EXPORT_SYMBOL(__xfrm_policy_destroy
);
343 static void xfrm_policy_gc_kill(struct xfrm_policy
*policy
)
345 struct dst_entry
*dst
;
347 while ((dst
= policy
->bundles
) != NULL
) {
348 policy
->bundles
= dst
->next
;
352 if (del_timer(&policy
->timer
))
353 atomic_dec(&policy
->refcnt
);
355 if (atomic_read(&policy
->refcnt
) > 1)
358 xfrm_pol_put(policy
);
361 static void xfrm_policy_gc_task(void *data
)
363 struct xfrm_policy
*policy
;
364 struct hlist_node
*entry
, *tmp
;
365 struct hlist_head gc_list
;
367 spin_lock_bh(&xfrm_policy_gc_lock
);
368 gc_list
.first
= xfrm_policy_gc_list
.first
;
369 INIT_HLIST_HEAD(&xfrm_policy_gc_list
);
370 spin_unlock_bh(&xfrm_policy_gc_lock
);
372 hlist_for_each_entry_safe(policy
, entry
, tmp
, &gc_list
, bydst
)
373 xfrm_policy_gc_kill(policy
);
376 /* Rule must be locked. Release descentant resources, announce
377 * entry dead. The rule must be unlinked from lists to the moment.
380 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
384 write_lock_bh(&policy
->lock
);
387 write_unlock_bh(&policy
->lock
);
389 if (unlikely(dead
)) {
394 spin_lock(&xfrm_policy_gc_lock
);
395 hlist_add_head(&policy
->bydst
, &xfrm_policy_gc_list
);
396 spin_unlock(&xfrm_policy_gc_lock
);
398 schedule_work(&xfrm_policy_gc_work
);
401 struct xfrm_policy_hash
{
402 struct hlist_head
*table
;
406 static struct hlist_head xfrm_policy_inexact
[XFRM_POLICY_MAX
*2];
407 static struct xfrm_policy_hash xfrm_policy_bydst
[XFRM_POLICY_MAX
*2] __read_mostly
;
408 static struct hlist_head
*xfrm_policy_byidx __read_mostly
;
409 static unsigned int xfrm_idx_hmask __read_mostly
;
410 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
412 static inline unsigned int idx_hash(u32 index
)
414 return __idx_hash(index
, xfrm_idx_hmask
);
417 static struct hlist_head
*policy_hash_bysel(struct xfrm_selector
*sel
, unsigned short family
, int dir
)
419 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
420 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
422 return (hash
== hmask
+ 1 ?
423 &xfrm_policy_inexact
[dir
] :
424 xfrm_policy_bydst
[dir
].table
+ hash
);
427 static struct hlist_head
*policy_hash_direct(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, unsigned short family
, int dir
)
429 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
430 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
432 return xfrm_policy_bydst
[dir
].table
+ hash
;
435 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
436 struct hlist_head
*ndsttable
,
437 unsigned int nhashmask
)
439 struct hlist_node
*entry
, *tmp
;
440 struct xfrm_policy
*pol
;
442 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, bydst
) {
445 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
446 pol
->family
, nhashmask
);
447 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
451 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
452 struct hlist_head
*nidxtable
,
453 unsigned int nhashmask
)
455 struct hlist_node
*entry
, *tmp
;
456 struct xfrm_policy
*pol
;
458 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, byidx
) {
461 h
= __idx_hash(pol
->index
, nhashmask
);
462 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
466 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
468 return ((old_hmask
+ 1) << 1) - 1;
471 static void xfrm_bydst_resize(int dir
)
473 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
474 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
475 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
476 struct hlist_head
*odst
= xfrm_policy_bydst
[dir
].table
;
477 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
483 write_lock_bh(&xfrm_policy_lock
);
485 for (i
= hmask
; i
>= 0; i
--)
486 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
488 xfrm_policy_bydst
[dir
].table
= ndst
;
489 xfrm_policy_bydst
[dir
].hmask
= nhashmask
;
491 write_unlock_bh(&xfrm_policy_lock
);
493 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
496 static void xfrm_byidx_resize(int total
)
498 unsigned int hmask
= xfrm_idx_hmask
;
499 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
500 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
501 struct hlist_head
*oidx
= xfrm_policy_byidx
;
502 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
508 write_lock_bh(&xfrm_policy_lock
);
510 for (i
= hmask
; i
>= 0; i
--)
511 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
513 xfrm_policy_byidx
= nidx
;
514 xfrm_idx_hmask
= nhashmask
;
516 write_unlock_bh(&xfrm_policy_lock
);
518 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
521 static inline int xfrm_bydst_should_resize(int dir
, int *total
)
523 unsigned int cnt
= xfrm_policy_count
[dir
];
524 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
529 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
536 static inline int xfrm_byidx_should_resize(int total
)
538 unsigned int hmask
= xfrm_idx_hmask
;
540 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
547 static DEFINE_MUTEX(hash_resize_mutex
);
549 static void xfrm_hash_resize(void *__unused
)
553 mutex_lock(&hash_resize_mutex
);
556 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
557 if (xfrm_bydst_should_resize(dir
, &total
))
558 xfrm_bydst_resize(dir
);
560 if (xfrm_byidx_should_resize(total
))
561 xfrm_byidx_resize(total
);
563 mutex_unlock(&hash_resize_mutex
);
566 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
, NULL
);
568 /* Generate new index... KAME seems to generate them ordered by cost
569 * of an absolute inpredictability of ordering of rules. This will not pass. */
570 static u32
xfrm_gen_index(u8 type
, int dir
)
572 static u32 idx_generator
;
575 struct hlist_node
*entry
;
576 struct hlist_head
*list
;
577 struct xfrm_policy
*p
;
581 idx
= (idx_generator
| dir
);
585 list
= xfrm_policy_byidx
+ idx_hash(idx
);
587 hlist_for_each_entry(p
, entry
, list
, byidx
) {
588 if (p
->index
== idx
) {
598 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
600 u32
*p1
= (u32
*) s1
;
601 u32
*p2
= (u32
*) s2
;
602 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
605 for (i
= 0; i
< len
; i
++) {
613 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
615 struct xfrm_policy
*pol
;
616 struct xfrm_policy
*delpol
;
617 struct hlist_head
*chain
;
618 struct hlist_node
*entry
, *newpos
, *last
;
619 struct dst_entry
*gc_list
;
621 write_lock_bh(&xfrm_policy_lock
);
622 chain
= policy_hash_bysel(&policy
->selector
, policy
->family
, dir
);
626 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
628 pol
->type
== policy
->type
&&
629 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
630 xfrm_sec_ctx_match(pol
->security
, policy
->security
)) {
632 write_unlock_bh(&xfrm_policy_lock
);
636 if (policy
->priority
> pol
->priority
)
638 } else if (policy
->priority
>= pol
->priority
) {
643 newpos
= &pol
->bydst
;
651 hlist_add_after(newpos
, &policy
->bydst
);
653 hlist_add_head(&policy
->bydst
, chain
);
654 xfrm_pol_hold(policy
);
655 xfrm_policy_count
[dir
]++;
656 atomic_inc(&flow_cache_genid
);
658 hlist_del(&delpol
->bydst
);
659 hlist_del(&delpol
->byidx
);
660 xfrm_policy_count
[dir
]--;
662 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(policy
->type
, dir
);
663 hlist_add_head(&policy
->byidx
, xfrm_policy_byidx
+idx_hash(policy
->index
));
664 policy
->curlft
.add_time
= (unsigned long)xtime
.tv_sec
;
665 policy
->curlft
.use_time
= 0;
666 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
667 xfrm_pol_hold(policy
);
668 write_unlock_bh(&xfrm_policy_lock
);
671 xfrm_policy_kill(delpol
);
672 else if (xfrm_bydst_should_resize(dir
, NULL
))
673 schedule_work(&xfrm_hash_work
);
675 read_lock_bh(&xfrm_policy_lock
);
677 entry
= &policy
->bydst
;
678 hlist_for_each_entry_continue(policy
, entry
, bydst
) {
679 struct dst_entry
*dst
;
681 write_lock(&policy
->lock
);
682 dst
= policy
->bundles
;
684 struct dst_entry
*tail
= dst
;
687 tail
->next
= gc_list
;
690 policy
->bundles
= NULL
;
692 write_unlock(&policy
->lock
);
694 read_unlock_bh(&xfrm_policy_lock
);
697 struct dst_entry
*dst
= gc_list
;
705 EXPORT_SYMBOL(xfrm_policy_insert
);
707 struct xfrm_policy
*xfrm_policy_bysel_ctx(u8 type
, int dir
,
708 struct xfrm_selector
*sel
,
709 struct xfrm_sec_ctx
*ctx
, int delete)
711 struct xfrm_policy
*pol
, *ret
;
712 struct hlist_head
*chain
;
713 struct hlist_node
*entry
;
715 write_lock_bh(&xfrm_policy_lock
);
716 chain
= policy_hash_bysel(sel
, sel
->family
, dir
);
718 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
719 if (pol
->type
== type
&&
720 !selector_cmp(sel
, &pol
->selector
) &&
721 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
724 hlist_del(&pol
->bydst
);
725 hlist_del(&pol
->byidx
);
726 xfrm_policy_count
[dir
]--;
732 write_unlock_bh(&xfrm_policy_lock
);
735 atomic_inc(&flow_cache_genid
);
736 xfrm_policy_kill(ret
);
740 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
742 struct xfrm_policy
*xfrm_policy_byid(u8 type
, int dir
, u32 id
, int delete)
744 struct xfrm_policy
*pol
, *ret
;
745 struct hlist_head
*chain
;
746 struct hlist_node
*entry
;
748 write_lock_bh(&xfrm_policy_lock
);
749 chain
= xfrm_policy_byidx
+ idx_hash(id
);
751 hlist_for_each_entry(pol
, entry
, chain
, byidx
) {
752 if (pol
->type
== type
&& pol
->index
== id
) {
755 hlist_del(&pol
->bydst
);
756 hlist_del(&pol
->byidx
);
757 xfrm_policy_count
[dir
]--;
763 write_unlock_bh(&xfrm_policy_lock
);
766 atomic_inc(&flow_cache_genid
);
767 xfrm_policy_kill(ret
);
771 EXPORT_SYMBOL(xfrm_policy_byid
);
773 void xfrm_policy_flush(u8 type
)
777 write_lock_bh(&xfrm_policy_lock
);
778 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
779 struct xfrm_policy
*pol
;
780 struct hlist_node
*entry
;
784 hlist_for_each_entry(pol
, entry
,
785 &xfrm_policy_inexact
[dir
], bydst
) {
786 if (pol
->type
!= type
)
788 hlist_del(&pol
->bydst
);
789 hlist_del(&pol
->byidx
);
790 write_unlock_bh(&xfrm_policy_lock
);
792 xfrm_policy_kill(pol
);
794 write_lock_bh(&xfrm_policy_lock
);
798 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
800 hlist_for_each_entry(pol
, entry
,
801 xfrm_policy_bydst
[dir
].table
+ i
,
803 if (pol
->type
!= type
)
805 hlist_del(&pol
->bydst
);
806 hlist_del(&pol
->byidx
);
807 write_unlock_bh(&xfrm_policy_lock
);
809 xfrm_policy_kill(pol
);
811 write_lock_bh(&xfrm_policy_lock
);
816 xfrm_policy_count
[dir
] = 0;
818 atomic_inc(&flow_cache_genid
);
819 write_unlock_bh(&xfrm_policy_lock
);
821 EXPORT_SYMBOL(xfrm_policy_flush
);
823 int xfrm_policy_walk(u8 type
, int (*func
)(struct xfrm_policy
*, int, int, void*),
826 struct xfrm_policy
*pol
;
827 struct hlist_node
*entry
;
828 int dir
, count
, error
;
830 read_lock_bh(&xfrm_policy_lock
);
832 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
833 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
836 hlist_for_each_entry(pol
, entry
,
837 &xfrm_policy_inexact
[dir
], bydst
) {
838 if (pol
->type
== type
)
841 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
842 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
843 if (pol
->type
== type
)
854 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
855 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
858 hlist_for_each_entry(pol
, entry
,
859 &xfrm_policy_inexact
[dir
], bydst
) {
860 if (pol
->type
!= type
)
862 error
= func(pol
, dir
% XFRM_POLICY_MAX
, --count
, data
);
866 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
867 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
868 if (pol
->type
!= type
)
870 error
= func(pol
, dir
% XFRM_POLICY_MAX
, --count
, data
);
878 read_unlock_bh(&xfrm_policy_lock
);
881 EXPORT_SYMBOL(xfrm_policy_walk
);
883 /* Find policy to apply to this flow. */
885 static int xfrm_policy_match(struct xfrm_policy
*pol
, struct flowi
*fl
,
886 u8 type
, u16 family
, int dir
)
888 struct xfrm_selector
*sel
= &pol
->selector
;
891 if (pol
->family
!= family
||
895 match
= xfrm_selector_match(sel
, fl
, family
);
897 if (!security_xfrm_policy_lookup(pol
, fl
->secid
, dir
))
904 static struct xfrm_policy
*xfrm_policy_lookup_bytype(u8 type
, struct flowi
*fl
,
907 struct xfrm_policy
*pol
, *ret
;
908 xfrm_address_t
*daddr
, *saddr
;
909 struct hlist_node
*entry
;
910 struct hlist_head
*chain
;
913 daddr
= xfrm_flowi_daddr(fl
, family
);
914 saddr
= xfrm_flowi_saddr(fl
, family
);
915 if (unlikely(!daddr
|| !saddr
))
918 read_lock_bh(&xfrm_policy_lock
);
919 chain
= policy_hash_direct(daddr
, saddr
, family
, dir
);
921 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
922 if (xfrm_policy_match(pol
, fl
, type
, family
, dir
)) {
924 priority
= ret
->priority
;
928 chain
= &xfrm_policy_inexact
[dir
];
929 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
930 if (xfrm_policy_match(pol
, fl
, type
, family
, dir
) &&
931 pol
->priority
< priority
) {
938 read_unlock_bh(&xfrm_policy_lock
);
943 static void xfrm_policy_lookup(struct flowi
*fl
, u16 family
, u8 dir
,
944 void **objp
, atomic_t
**obj_refp
)
946 struct xfrm_policy
*pol
;
948 #ifdef CONFIG_XFRM_SUB_POLICY
949 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
953 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
955 #ifdef CONFIG_XFRM_SUB_POLICY
958 if ((*objp
= (void *) pol
) != NULL
)
959 *obj_refp
= &pol
->refcnt
;
962 static inline int policy_to_flow_dir(int dir
)
964 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
965 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
966 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
972 case XFRM_POLICY_OUT
:
974 case XFRM_POLICY_FWD
:
979 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
)
981 struct xfrm_policy
*pol
;
983 read_lock_bh(&xfrm_policy_lock
);
984 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
985 int match
= xfrm_selector_match(&pol
->selector
, fl
,
990 err
= security_xfrm_policy_lookup(pol
, fl
->secid
, policy_to_flow_dir(dir
));
997 read_unlock_bh(&xfrm_policy_lock
);
1001 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1003 struct hlist_head
*chain
= policy_hash_bysel(&pol
->selector
,
1006 hlist_add_head(&pol
->bydst
, chain
);
1007 hlist_add_head(&pol
->byidx
, xfrm_policy_byidx
+idx_hash(pol
->index
));
1008 xfrm_policy_count
[dir
]++;
1011 if (xfrm_bydst_should_resize(dir
, NULL
))
1012 schedule_work(&xfrm_hash_work
);
1015 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1018 if (hlist_unhashed(&pol
->bydst
))
1021 hlist_del(&pol
->bydst
);
1022 hlist_del(&pol
->byidx
);
1023 xfrm_policy_count
[dir
]--;
1028 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1030 write_lock_bh(&xfrm_policy_lock
);
1031 pol
= __xfrm_policy_unlink(pol
, dir
);
1032 write_unlock_bh(&xfrm_policy_lock
);
1034 if (dir
< XFRM_POLICY_MAX
)
1035 atomic_inc(&flow_cache_genid
);
1036 xfrm_policy_kill(pol
);
1041 EXPORT_SYMBOL(xfrm_policy_delete
);
1043 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1045 struct xfrm_policy
*old_pol
;
1047 #ifdef CONFIG_XFRM_SUB_POLICY
1048 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1052 write_lock_bh(&xfrm_policy_lock
);
1053 old_pol
= sk
->sk_policy
[dir
];
1054 sk
->sk_policy
[dir
] = pol
;
1056 pol
->curlft
.add_time
= (unsigned long)xtime
.tv_sec
;
1057 pol
->index
= xfrm_gen_index(pol
->type
, XFRM_POLICY_MAX
+dir
);
1058 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1061 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1062 write_unlock_bh(&xfrm_policy_lock
);
1065 xfrm_policy_kill(old_pol
);
1070 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
1072 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
1075 newp
->selector
= old
->selector
;
1076 if (security_xfrm_policy_clone(old
, newp
)) {
1078 return NULL
; /* ENOMEM */
1080 newp
->lft
= old
->lft
;
1081 newp
->curlft
= old
->curlft
;
1082 newp
->action
= old
->action
;
1083 newp
->flags
= old
->flags
;
1084 newp
->xfrm_nr
= old
->xfrm_nr
;
1085 newp
->index
= old
->index
;
1086 newp
->type
= old
->type
;
1087 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1088 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1089 write_lock_bh(&xfrm_policy_lock
);
1090 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1091 write_unlock_bh(&xfrm_policy_lock
);
1097 int __xfrm_sk_clone_policy(struct sock
*sk
)
1099 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1100 *p1
= sk
->sk_policy
[1];
1102 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1103 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1105 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1111 xfrm_get_saddr(xfrm_address_t
*local
, xfrm_address_t
*remote
,
1112 unsigned short family
)
1115 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1117 if (unlikely(afinfo
== NULL
))
1119 err
= afinfo
->get_saddr(local
, remote
);
1120 xfrm_policy_put_afinfo(afinfo
);
1124 /* Resolve list of templates for the flow, given policy. */
1127 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, struct flowi
*fl
,
1128 struct xfrm_state
**xfrm
,
1129 unsigned short family
)
1133 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1134 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1137 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1138 struct xfrm_state
*x
;
1139 xfrm_address_t
*remote
= daddr
;
1140 xfrm_address_t
*local
= saddr
;
1141 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1143 if (tmpl
->mode
== XFRM_MODE_TUNNEL
) {
1144 remote
= &tmpl
->id
.daddr
;
1145 local
= &tmpl
->saddr
;
1146 if (xfrm_addr_any(local
, family
)) {
1147 error
= xfrm_get_saddr(&tmp
, remote
, family
);
1154 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1156 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1163 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1168 if (!tmpl
->optional
)
1174 for (nx
--; nx
>=0; nx
--)
1175 xfrm_state_put(xfrm
[nx
]);
1180 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, struct flowi
*fl
,
1181 struct xfrm_state
**xfrm
,
1182 unsigned short family
)
1184 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1185 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1191 for (i
= 0; i
< npols
; i
++) {
1192 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1197 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1205 /* found states are sorted for outbound processing */
1207 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1212 for (cnx
--; cnx
>=0; cnx
--)
1213 xfrm_state_put(tpp
[cnx
]);
1218 /* Check that the bundle accepts the flow and its components are
1222 static struct dst_entry
*
1223 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
1225 struct dst_entry
*x
;
1226 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1227 if (unlikely(afinfo
== NULL
))
1228 return ERR_PTR(-EINVAL
);
1229 x
= afinfo
->find_bundle(fl
, policy
);
1230 xfrm_policy_put_afinfo(afinfo
);
1234 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1235 * all the metrics... Shortly, bundle a bundle.
1239 xfrm_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
1240 struct flowi
*fl
, struct dst_entry
**dst_p
,
1241 unsigned short family
)
1244 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1245 if (unlikely(afinfo
== NULL
))
1247 err
= afinfo
->bundle_create(policy
, xfrm
, nx
, fl
, dst_p
);
1248 xfrm_policy_put_afinfo(afinfo
);
1253 static int stale_bundle(struct dst_entry
*dst
);
1255 /* Main function: finds/creates a bundle for given flow.
1257 * At the moment we eat a raw IP route. Mostly to speed up lookups
1258 * on interfaces with disabled IPsec.
1260 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1261 struct sock
*sk
, int flags
)
1263 struct xfrm_policy
*policy
;
1264 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1269 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1270 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
1275 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1278 genid
= atomic_read(&flow_cache_genid
);
1280 for (pi
= 0; pi
< ARRAY_SIZE(pols
); pi
++)
1286 if (sk
&& sk
->sk_policy
[1])
1287 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1290 /* To accelerate a bit... */
1291 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1292 !xfrm_policy_count
[XFRM_POLICY_OUT
])
1295 policy
= flow_cache_lookup(fl
, dst_orig
->ops
->family
,
1296 dir
, xfrm_policy_lookup
);
1302 family
= dst_orig
->ops
->family
;
1303 policy
->curlft
.use_time
= (unsigned long)xtime
.tv_sec
;
1306 xfrm_nr
+= pols
[0]->xfrm_nr
;
1308 switch (policy
->action
) {
1309 case XFRM_POLICY_BLOCK
:
1310 /* Prohibit the flow */
1314 case XFRM_POLICY_ALLOW
:
1315 #ifndef CONFIG_XFRM_SUB_POLICY
1316 if (policy
->xfrm_nr
== 0) {
1317 /* Flow passes not transformed. */
1318 xfrm_pol_put(policy
);
1323 /* Try to find matching bundle.
1325 * LATER: help from flow cache. It is optional, this
1326 * is required only for output policy.
1328 dst
= xfrm_find_bundle(fl
, policy
, family
);
1337 #ifdef CONFIG_XFRM_SUB_POLICY
1338 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1339 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1343 if (pols
[1]->action
== XFRM_POLICY_BLOCK
) {
1348 xfrm_nr
+= pols
[1]->xfrm_nr
;
1353 * Because neither flowi nor bundle information knows about
1354 * transformation template size. On more than one policy usage
1355 * we can realize whether all of them is bypass or not after
1356 * they are searched. See above not-transformed bypass
1357 * is surrounded by non-sub policy configuration, too.
1360 /* Flow passes not transformed. */
1361 xfrm_pols_put(pols
, npols
);
1366 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1368 if (unlikely(nx
<0)) {
1370 if (err
== -EAGAIN
&& flags
) {
1371 DECLARE_WAITQUEUE(wait
, current
);
1373 add_wait_queue(&km_waitq
, &wait
);
1374 set_current_state(TASK_INTERRUPTIBLE
);
1376 set_current_state(TASK_RUNNING
);
1377 remove_wait_queue(&km_waitq
, &wait
);
1379 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1381 if (nx
== -EAGAIN
&& signal_pending(current
)) {
1385 if (nx
== -EAGAIN
||
1386 genid
!= atomic_read(&flow_cache_genid
)) {
1387 xfrm_pols_put(pols
, npols
);
1396 /* Flow passes not transformed. */
1397 xfrm_pols_put(pols
, npols
);
1402 err
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, &dst
, family
);
1404 if (unlikely(err
)) {
1406 for (i
=0; i
<nx
; i
++)
1407 xfrm_state_put(xfrm
[i
]);
1411 for (pi
= 0; pi
< npols
; pi
++) {
1412 read_lock_bh(&pols
[pi
]->lock
);
1413 pol_dead
|= pols
[pi
]->dead
;
1414 read_unlock_bh(&pols
[pi
]->lock
);
1417 write_lock_bh(&policy
->lock
);
1418 if (unlikely(pol_dead
|| stale_bundle(dst
))) {
1419 /* Wow! While we worked on resolving, this
1420 * policy has gone. Retry. It is not paranoia,
1421 * we just cannot enlist new bundle to dead object.
1422 * We can't enlist stable bundles either.
1424 write_unlock_bh(&policy
->lock
);
1428 err
= -EHOSTUNREACH
;
1431 dst
->next
= policy
->bundles
;
1432 policy
->bundles
= dst
;
1434 write_unlock_bh(&policy
->lock
);
1437 dst_release(dst_orig
);
1438 xfrm_pols_put(pols
, npols
);
1442 dst_release(dst_orig
);
1443 xfrm_pols_put(pols
, npols
);
1447 EXPORT_SYMBOL(xfrm_lookup
);
1450 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, struct flowi
*fl
)
1452 struct xfrm_state
*x
;
1455 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1457 x
= skb
->sp
->xvec
[idx
];
1458 if (!x
->type
->reject
)
1461 err
= x
->type
->reject(x
, skb
, fl
);
1466 /* When skb is transformed back to its "native" form, we have to
1467 * check policy restrictions. At the moment we make this in maximally
1468 * stupid way. Shame on me. :-) Of course, connected sockets must
1469 * have policy cached at them.
1473 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
1474 unsigned short family
)
1476 if (xfrm_state_kern(x
))
1477 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, family
);
1478 return x
->id
.proto
== tmpl
->id
.proto
&&
1479 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1480 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1481 x
->props
.mode
== tmpl
->mode
&&
1482 ((tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1483 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1484 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1485 xfrm_state_addr_cmp(tmpl
, x
, family
));
1489 * 0 or more than 0 is returned when validation is succeeded (either bypass
1490 * because of optional transport mode, or next index of the mathced secpath
1491 * state with the template.
1492 * -1 is returned when no matching template is found.
1493 * Otherwise "-2 - errored_index" is returned.
1496 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1497 unsigned short family
)
1501 if (tmpl
->optional
) {
1502 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
1506 for (; idx
< sp
->len
; idx
++) {
1507 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1509 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1519 xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, unsigned short family
)
1521 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1524 if (unlikely(afinfo
== NULL
))
1525 return -EAFNOSUPPORT
;
1527 afinfo
->decode_session(skb
, fl
);
1528 err
= security_xfrm_decode_session(skb
, &fl
->secid
);
1529 xfrm_policy_put_afinfo(afinfo
);
1532 EXPORT_SYMBOL(xfrm_decode_session
);
1534 static inline int secpath_has_nontransport(struct sec_path
*sp
, int k
, int *idxp
)
1536 for (; k
< sp
->len
; k
++) {
1537 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1546 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1547 unsigned short family
)
1549 struct xfrm_policy
*pol
;
1550 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1555 u8 fl_dir
= policy_to_flow_dir(dir
);
1558 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1560 nf_nat_decode_session(skb
, &fl
, family
);
1562 /* First, check used SA against their selectors. */
1566 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1567 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1568 if (!xfrm_selector_match(&x
->sel
, &fl
, family
))
1574 if (sk
&& sk
->sk_policy
[dir
])
1575 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
1578 pol
= flow_cache_lookup(&fl
, family
, fl_dir
,
1579 xfrm_policy_lookup
);
1582 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
1583 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1589 pol
->curlft
.use_time
= (unsigned long)xtime
.tv_sec
;
1593 #ifdef CONFIG_XFRM_SUB_POLICY
1594 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1595 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1599 pols
[1]->curlft
.use_time
= (unsigned long)xtime
.tv_sec
;
1605 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1606 struct sec_path
*sp
;
1607 static struct sec_path dummy
;
1608 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
1609 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
1610 struct xfrm_tmpl
**tpp
= tp
;
1614 if ((sp
= skb
->sp
) == NULL
)
1617 for (pi
= 0; pi
< npols
; pi
++) {
1618 if (pols
[pi
] != pol
&&
1619 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
)
1621 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
)
1623 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
1624 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
1628 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
1632 /* For each tunnel xfrm, find the first matching tmpl.
1633 * For each tmpl before that, find corresponding xfrm.
1634 * Order is _important_. Later we will implement
1635 * some barriers, but at the moment barriers
1636 * are implied between each two transformations.
1638 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1639 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
1642 /* "-2 - errored_index" returned */
1648 if (secpath_has_nontransport(sp
, k
, &xerr_idx
))
1651 xfrm_pols_put(pols
, npols
);
1656 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1658 xfrm_pols_put(pols
, npols
);
1661 EXPORT_SYMBOL(__xfrm_policy_check
);
1663 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1667 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1670 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
1672 EXPORT_SYMBOL(__xfrm_route_forward
);
1674 /* Optimize later using cookies and generation ids. */
1676 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
1678 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1679 * to "-1" to force all XFRM destinations to get validated by
1680 * dst_ops->check on every use. We do this because when a
1681 * normal route referenced by an XFRM dst is obsoleted we do
1682 * not go looking around for all parent referencing XFRM dsts
1683 * so that we can invalidate them. It is just too much work.
1684 * Instead we make the checks here on every use. For example:
1686 * XFRM dst A --> IPv4 dst X
1688 * X is the "xdst->route" of A (X is also the "dst->path" of A
1689 * in this example). If X is marked obsolete, "A" will not
1690 * notice. That's what we are validating here via the
1691 * stale_bundle() check.
1693 * When a policy's bundle is pruned, we dst_free() the XFRM
1694 * dst which causes it's ->obsolete field to be set to a
1695 * positive non-zero integer. If an XFRM dst has been pruned
1696 * like this, we want to force a new route lookup.
1698 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
1704 static int stale_bundle(struct dst_entry
*dst
)
1706 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
, 0);
1709 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
1711 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
1712 dst
->dev
= &loopback_dev
;
1713 dev_hold(&loopback_dev
);
1717 EXPORT_SYMBOL(xfrm_dst_ifdown
);
1719 static void xfrm_link_failure(struct sk_buff
*skb
)
1721 /* Impossible. Such dst must be popped before reaches point of failure. */
1725 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
1728 if (dst
->obsolete
) {
1736 static void prune_one_bundle(struct xfrm_policy
*pol
, int (*func
)(struct dst_entry
*), struct dst_entry
**gc_list_p
)
1738 struct dst_entry
*dst
, **dstp
;
1740 write_lock(&pol
->lock
);
1741 dstp
= &pol
->bundles
;
1742 while ((dst
=*dstp
) != NULL
) {
1745 dst
->next
= *gc_list_p
;
1751 write_unlock(&pol
->lock
);
1754 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
1756 struct dst_entry
*gc_list
= NULL
;
1759 read_lock_bh(&xfrm_policy_lock
);
1760 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
1761 struct xfrm_policy
*pol
;
1762 struct hlist_node
*entry
;
1763 struct hlist_head
*table
;
1766 hlist_for_each_entry(pol
, entry
,
1767 &xfrm_policy_inexact
[dir
], bydst
)
1768 prune_one_bundle(pol
, func
, &gc_list
);
1770 table
= xfrm_policy_bydst
[dir
].table
;
1771 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
1772 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
)
1773 prune_one_bundle(pol
, func
, &gc_list
);
1776 read_unlock_bh(&xfrm_policy_lock
);
1779 struct dst_entry
*dst
= gc_list
;
1780 gc_list
= dst
->next
;
1785 static int unused_bundle(struct dst_entry
*dst
)
1787 return !atomic_read(&dst
->__refcnt
);
1790 static void __xfrm_garbage_collect(void)
1792 xfrm_prune_bundles(unused_bundle
);
1795 static int xfrm_flush_bundles(void)
1797 xfrm_prune_bundles(stale_bundle
);
1801 void xfrm_init_pmtu(struct dst_entry
*dst
)
1804 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1805 u32 pmtu
, route_mtu_cached
;
1807 pmtu
= dst_mtu(dst
->child
);
1808 xdst
->child_mtu_cached
= pmtu
;
1810 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
1812 route_mtu_cached
= dst_mtu(xdst
->route
);
1813 xdst
->route_mtu_cached
= route_mtu_cached
;
1815 if (pmtu
> route_mtu_cached
)
1816 pmtu
= route_mtu_cached
;
1818 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
1819 } while ((dst
= dst
->next
));
1822 EXPORT_SYMBOL(xfrm_init_pmtu
);
1824 /* Check that the bundle accepts the flow and its components are
1828 int xfrm_bundle_ok(struct xfrm_dst
*first
, struct flowi
*fl
, int family
, int strict
)
1830 struct dst_entry
*dst
= &first
->u
.dst
;
1831 struct xfrm_dst
*last
;
1834 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
1835 (dst
->dev
&& !netif_running(dst
->dev
)))
1841 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1843 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
1845 if (fl
&& !security_xfrm_flow_state_match(fl
, dst
->xfrm
))
1847 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
1849 if (xdst
->genid
!= dst
->xfrm
->genid
)
1852 if (strict
&& fl
&& dst
->xfrm
->props
.mode
!= XFRM_MODE_TUNNEL
&&
1853 !xfrm_state_addr_flow_check(dst
->xfrm
, fl
, family
))
1856 mtu
= dst_mtu(dst
->child
);
1857 if (xdst
->child_mtu_cached
!= mtu
) {
1859 xdst
->child_mtu_cached
= mtu
;
1862 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
1864 mtu
= dst_mtu(xdst
->route
);
1865 if (xdst
->route_mtu_cached
!= mtu
) {
1867 xdst
->route_mtu_cached
= mtu
;
1871 } while (dst
->xfrm
);
1876 mtu
= last
->child_mtu_cached
;
1880 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
1881 if (mtu
> last
->route_mtu_cached
)
1882 mtu
= last
->route_mtu_cached
;
1883 dst
->metrics
[RTAX_MTU
-1] = mtu
;
1888 last
= last
->u
.next
;
1889 last
->child_mtu_cached
= mtu
;
1895 EXPORT_SYMBOL(xfrm_bundle_ok
);
1897 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1900 if (unlikely(afinfo
== NULL
))
1902 if (unlikely(afinfo
->family
>= NPROTO
))
1903 return -EAFNOSUPPORT
;
1904 write_lock_bh(&xfrm_policy_afinfo_lock
);
1905 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
1908 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
1909 if (likely(dst_ops
->kmem_cachep
== NULL
))
1910 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
1911 if (likely(dst_ops
->check
== NULL
))
1912 dst_ops
->check
= xfrm_dst_check
;
1913 if (likely(dst_ops
->negative_advice
== NULL
))
1914 dst_ops
->negative_advice
= xfrm_negative_advice
;
1915 if (likely(dst_ops
->link_failure
== NULL
))
1916 dst_ops
->link_failure
= xfrm_link_failure
;
1917 if (likely(afinfo
->garbage_collect
== NULL
))
1918 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
1919 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
1921 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1924 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
1926 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1929 if (unlikely(afinfo
== NULL
))
1931 if (unlikely(afinfo
->family
>= NPROTO
))
1932 return -EAFNOSUPPORT
;
1933 write_lock_bh(&xfrm_policy_afinfo_lock
);
1934 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
1935 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
1938 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
1939 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
1940 dst_ops
->kmem_cachep
= NULL
;
1941 dst_ops
->check
= NULL
;
1942 dst_ops
->negative_advice
= NULL
;
1943 dst_ops
->link_failure
= NULL
;
1944 afinfo
->garbage_collect
= NULL
;
1947 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1950 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
1952 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
1954 struct xfrm_policy_afinfo
*afinfo
;
1955 if (unlikely(family
>= NPROTO
))
1957 read_lock(&xfrm_policy_afinfo_lock
);
1958 afinfo
= xfrm_policy_afinfo
[family
];
1959 if (unlikely(!afinfo
))
1960 read_unlock(&xfrm_policy_afinfo_lock
);
1964 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1966 read_unlock(&xfrm_policy_afinfo_lock
);
1969 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
)
1971 struct xfrm_policy_afinfo
*afinfo
;
1972 if (unlikely(family
>= NPROTO
))
1974 write_lock_bh(&xfrm_policy_afinfo_lock
);
1975 afinfo
= xfrm_policy_afinfo
[family
];
1976 if (unlikely(!afinfo
))
1977 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1981 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
)
1983 write_unlock_bh(&xfrm_policy_afinfo_lock
);
1986 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1990 xfrm_flush_bundles();
1995 static struct notifier_block xfrm_dev_notifier
= {
2001 static void __init
xfrm_policy_init(void)
2003 unsigned int hmask
, sz
;
2006 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2007 sizeof(struct xfrm_dst
),
2008 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2012 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2014 xfrm_policy_byidx
= xfrm_hash_alloc(sz
);
2015 xfrm_idx_hmask
= hmask
;
2016 if (!xfrm_policy_byidx
)
2017 panic("XFRM: failed to allocate byidx hash\n");
2019 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2020 struct xfrm_policy_hash
*htab
;
2022 INIT_HLIST_HEAD(&xfrm_policy_inexact
[dir
]);
2024 htab
= &xfrm_policy_bydst
[dir
];
2025 htab
->table
= xfrm_hash_alloc(sz
);
2026 htab
->hmask
= hmask
;
2028 panic("XFRM: failed to allocate bydst hash\n");
2031 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
, NULL
);
2032 register_netdevice_notifier(&xfrm_dev_notifier
);
2035 void __init
xfrm_init(void)