6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
16 #include <linux/workqueue.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
29 #include "xfrm_hash.h"
31 /* Each xfrm_state may be linked to two tables:
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
34 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
35 destination/tunnel endpoint. (output)
38 static DEFINE_SPINLOCK(xfrm_state_lock
);
40 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
42 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
);
43 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
);
45 static inline unsigned int xfrm_dst_hash(struct net
*net
,
46 const xfrm_address_t
*daddr
,
47 const xfrm_address_t
*saddr
,
49 unsigned short family
)
51 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, net
->xfrm
.state_hmask
);
54 static inline unsigned int xfrm_src_hash(struct net
*net
,
55 const xfrm_address_t
*daddr
,
56 const xfrm_address_t
*saddr
,
57 unsigned short family
)
59 return __xfrm_src_hash(daddr
, saddr
, family
, net
->xfrm
.state_hmask
);
62 static inline unsigned int
63 xfrm_spi_hash(struct net
*net
, const xfrm_address_t
*daddr
,
64 __be32 spi
, u8 proto
, unsigned short family
)
66 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, net
->xfrm
.state_hmask
);
69 static void xfrm_hash_transfer(struct hlist_head
*list
,
70 struct hlist_head
*ndsttable
,
71 struct hlist_head
*nsrctable
,
72 struct hlist_head
*nspitable
,
73 unsigned int nhashmask
)
75 struct hlist_node
*tmp
;
78 hlist_for_each_entry_safe(x
, tmp
, list
, bydst
) {
81 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
82 x
->props
.reqid
, x
->props
.family
,
84 hlist_add_head(&x
->bydst
, ndsttable
+h
);
86 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
89 hlist_add_head(&x
->bysrc
, nsrctable
+h
);
92 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
93 x
->id
.proto
, x
->props
.family
,
95 hlist_add_head(&x
->byspi
, nspitable
+h
);
100 static unsigned long xfrm_hash_new_size(unsigned int state_hmask
)
102 return ((state_hmask
+ 1) << 1) * sizeof(struct hlist_head
);
105 static DEFINE_MUTEX(hash_resize_mutex
);
107 static void xfrm_hash_resize(struct work_struct
*work
)
109 struct net
*net
= container_of(work
, struct net
, xfrm
.state_hash_work
);
110 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
111 unsigned long nsize
, osize
;
112 unsigned int nhashmask
, ohashmask
;
115 mutex_lock(&hash_resize_mutex
);
117 nsize
= xfrm_hash_new_size(net
->xfrm
.state_hmask
);
118 ndst
= xfrm_hash_alloc(nsize
);
121 nsrc
= xfrm_hash_alloc(nsize
);
123 xfrm_hash_free(ndst
, nsize
);
126 nspi
= xfrm_hash_alloc(nsize
);
128 xfrm_hash_free(ndst
, nsize
);
129 xfrm_hash_free(nsrc
, nsize
);
133 spin_lock_bh(&xfrm_state_lock
);
135 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
136 for (i
= net
->xfrm
.state_hmask
; i
>= 0; i
--)
137 xfrm_hash_transfer(net
->xfrm
.state_bydst
+i
, ndst
, nsrc
, nspi
,
140 odst
= net
->xfrm
.state_bydst
;
141 osrc
= net
->xfrm
.state_bysrc
;
142 ospi
= net
->xfrm
.state_byspi
;
143 ohashmask
= net
->xfrm
.state_hmask
;
145 net
->xfrm
.state_bydst
= ndst
;
146 net
->xfrm
.state_bysrc
= nsrc
;
147 net
->xfrm
.state_byspi
= nspi
;
148 net
->xfrm
.state_hmask
= nhashmask
;
150 spin_unlock_bh(&xfrm_state_lock
);
152 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
153 xfrm_hash_free(odst
, osize
);
154 xfrm_hash_free(osrc
, osize
);
155 xfrm_hash_free(ospi
, osize
);
158 mutex_unlock(&hash_resize_mutex
);
161 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock
);
162 static struct xfrm_state_afinfo __rcu
*xfrm_state_afinfo
[NPROTO
];
164 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
166 int __xfrm_state_delete(struct xfrm_state
*x
);
168 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
169 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
);
171 static DEFINE_SPINLOCK(xfrm_type_lock
);
172 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
174 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
175 const struct xfrm_type
**typemap
;
178 if (unlikely(afinfo
== NULL
))
179 return -EAFNOSUPPORT
;
180 typemap
= afinfo
->type_map
;
181 spin_lock_bh(&xfrm_type_lock
);
183 if (likely(typemap
[type
->proto
] == NULL
))
184 typemap
[type
->proto
] = type
;
187 spin_unlock_bh(&xfrm_type_lock
);
188 xfrm_state_put_afinfo(afinfo
);
191 EXPORT_SYMBOL(xfrm_register_type
);
193 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
195 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
196 const struct xfrm_type
**typemap
;
199 if (unlikely(afinfo
== NULL
))
200 return -EAFNOSUPPORT
;
201 typemap
= afinfo
->type_map
;
202 spin_lock_bh(&xfrm_type_lock
);
204 if (unlikely(typemap
[type
->proto
] != type
))
207 typemap
[type
->proto
] = NULL
;
208 spin_unlock_bh(&xfrm_type_lock
);
209 xfrm_state_put_afinfo(afinfo
);
212 EXPORT_SYMBOL(xfrm_unregister_type
);
214 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
216 struct xfrm_state_afinfo
*afinfo
;
217 const struct xfrm_type
**typemap
;
218 const struct xfrm_type
*type
;
219 int modload_attempted
= 0;
222 afinfo
= xfrm_state_get_afinfo(family
);
223 if (unlikely(afinfo
== NULL
))
225 typemap
= afinfo
->type_map
;
227 type
= typemap
[proto
];
228 if (unlikely(type
&& !try_module_get(type
->owner
)))
230 if (!type
&& !modload_attempted
) {
231 xfrm_state_put_afinfo(afinfo
);
232 request_module("xfrm-type-%d-%d", family
, proto
);
233 modload_attempted
= 1;
237 xfrm_state_put_afinfo(afinfo
);
241 static void xfrm_put_type(const struct xfrm_type
*type
)
243 module_put(type
->owner
);
246 static DEFINE_SPINLOCK(xfrm_mode_lock
);
247 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
249 struct xfrm_state_afinfo
*afinfo
;
250 struct xfrm_mode
**modemap
;
253 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
256 afinfo
= xfrm_state_get_afinfo(family
);
257 if (unlikely(afinfo
== NULL
))
258 return -EAFNOSUPPORT
;
261 modemap
= afinfo
->mode_map
;
262 spin_lock_bh(&xfrm_mode_lock
);
263 if (modemap
[mode
->encap
])
267 if (!try_module_get(afinfo
->owner
))
270 mode
->afinfo
= afinfo
;
271 modemap
[mode
->encap
] = mode
;
275 spin_unlock_bh(&xfrm_mode_lock
);
276 xfrm_state_put_afinfo(afinfo
);
279 EXPORT_SYMBOL(xfrm_register_mode
);
281 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
283 struct xfrm_state_afinfo
*afinfo
;
284 struct xfrm_mode
**modemap
;
287 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
290 afinfo
= xfrm_state_get_afinfo(family
);
291 if (unlikely(afinfo
== NULL
))
292 return -EAFNOSUPPORT
;
295 modemap
= afinfo
->mode_map
;
296 spin_lock_bh(&xfrm_mode_lock
);
297 if (likely(modemap
[mode
->encap
] == mode
)) {
298 modemap
[mode
->encap
] = NULL
;
299 module_put(mode
->afinfo
->owner
);
303 spin_unlock_bh(&xfrm_mode_lock
);
304 xfrm_state_put_afinfo(afinfo
);
307 EXPORT_SYMBOL(xfrm_unregister_mode
);
309 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
311 struct xfrm_state_afinfo
*afinfo
;
312 struct xfrm_mode
*mode
;
313 int modload_attempted
= 0;
315 if (unlikely(encap
>= XFRM_MODE_MAX
))
319 afinfo
= xfrm_state_get_afinfo(family
);
320 if (unlikely(afinfo
== NULL
))
323 mode
= afinfo
->mode_map
[encap
];
324 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
326 if (!mode
&& !modload_attempted
) {
327 xfrm_state_put_afinfo(afinfo
);
328 request_module("xfrm-mode-%d-%d", family
, encap
);
329 modload_attempted
= 1;
333 xfrm_state_put_afinfo(afinfo
);
337 static void xfrm_put_mode(struct xfrm_mode
*mode
)
339 module_put(mode
->owner
);
342 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
344 tasklet_hrtimer_cancel(&x
->mtimer
);
345 del_timer_sync(&x
->rtimer
);
351 kfree(x
->replay_esn
);
352 kfree(x
->preplay_esn
);
354 xfrm_put_mode(x
->inner_mode
);
355 if (x
->inner_mode_iaf
)
356 xfrm_put_mode(x
->inner_mode_iaf
);
358 xfrm_put_mode(x
->outer_mode
);
360 x
->type
->destructor(x
);
361 xfrm_put_type(x
->type
);
363 security_xfrm_state_free(x
);
367 static void xfrm_state_gc_task(struct work_struct
*work
)
369 struct net
*net
= container_of(work
, struct net
, xfrm
.state_gc_work
);
370 struct xfrm_state
*x
;
371 struct hlist_node
*tmp
;
372 struct hlist_head gc_list
;
374 spin_lock_bh(&xfrm_state_gc_lock
);
375 hlist_move_list(&net
->xfrm
.state_gc_list
, &gc_list
);
376 spin_unlock_bh(&xfrm_state_gc_lock
);
378 hlist_for_each_entry_safe(x
, tmp
, &gc_list
, gclist
)
379 xfrm_state_gc_destroy(x
);
381 wake_up(&net
->xfrm
.km_waitq
);
384 static inline unsigned long make_jiffies(long secs
)
386 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
387 return MAX_SCHEDULE_TIMEOUT
-1;
392 static enum hrtimer_restart
xfrm_timer_handler(struct hrtimer
* me
)
394 struct tasklet_hrtimer
*thr
= container_of(me
, struct tasklet_hrtimer
, timer
);
395 struct xfrm_state
*x
= container_of(thr
, struct xfrm_state
, mtimer
);
396 struct net
*net
= xs_net(x
);
397 unsigned long now
= get_seconds();
398 long next
= LONG_MAX
;
403 if (x
->km
.state
== XFRM_STATE_DEAD
)
405 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
407 if (x
->lft
.hard_add_expires_seconds
) {
408 long tmo
= x
->lft
.hard_add_expires_seconds
+
409 x
->curlft
.add_time
- now
;
411 if (x
->xflags
& XFRM_SOFT_EXPIRE
) {
412 /* enter hard expire without soft expire first?!
413 * setting a new date could trigger this.
414 * workarbound: fix x->curflt.add_time by below:
416 x
->curlft
.add_time
= now
- x
->saved_tmo
- 1;
417 tmo
= x
->lft
.hard_add_expires_seconds
- x
->saved_tmo
;
424 if (x
->lft
.hard_use_expires_seconds
) {
425 long tmo
= x
->lft
.hard_use_expires_seconds
+
426 (x
->curlft
.use_time
? : now
) - now
;
434 if (x
->lft
.soft_add_expires_seconds
) {
435 long tmo
= x
->lft
.soft_add_expires_seconds
+
436 x
->curlft
.add_time
- now
;
439 x
->xflags
&= ~XFRM_SOFT_EXPIRE
;
440 } else if (tmo
< next
) {
442 x
->xflags
|= XFRM_SOFT_EXPIRE
;
446 if (x
->lft
.soft_use_expires_seconds
) {
447 long tmo
= x
->lft
.soft_use_expires_seconds
+
448 (x
->curlft
.use_time
? : now
) - now
;
457 km_state_expired(x
, 0, 0);
459 if (next
!= LONG_MAX
){
460 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(next
, 0), HRTIMER_MODE_REL
);
466 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0) {
467 x
->km
.state
= XFRM_STATE_EXPIRED
;
468 wake_up(&net
->xfrm
.km_waitq
);
473 err
= __xfrm_state_delete(x
);
474 if (!err
&& x
->id
.spi
)
475 km_state_expired(x
, 1, 0);
477 xfrm_audit_state_delete(x
, err
? 0 : 1,
478 audit_get_loginuid(current
),
479 audit_get_sessionid(current
), 0);
482 spin_unlock(&x
->lock
);
483 return HRTIMER_NORESTART
;
486 static void xfrm_replay_timer_handler(unsigned long data
);
488 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
490 struct xfrm_state
*x
;
492 x
= kzalloc(sizeof(struct xfrm_state
), GFP_ATOMIC
);
495 write_pnet(&x
->xs_net
, net
);
496 atomic_set(&x
->refcnt
, 1);
497 atomic_set(&x
->tunnel_users
, 0);
498 INIT_LIST_HEAD(&x
->km
.all
);
499 INIT_HLIST_NODE(&x
->bydst
);
500 INIT_HLIST_NODE(&x
->bysrc
);
501 INIT_HLIST_NODE(&x
->byspi
);
502 tasklet_hrtimer_init(&x
->mtimer
, xfrm_timer_handler
,
503 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
504 setup_timer(&x
->rtimer
, xfrm_replay_timer_handler
,
506 x
->curlft
.add_time
= get_seconds();
507 x
->lft
.soft_byte_limit
= XFRM_INF
;
508 x
->lft
.soft_packet_limit
= XFRM_INF
;
509 x
->lft
.hard_byte_limit
= XFRM_INF
;
510 x
->lft
.hard_packet_limit
= XFRM_INF
;
511 x
->replay_maxage
= 0;
512 x
->replay_maxdiff
= 0;
513 x
->inner_mode
= NULL
;
514 x
->inner_mode_iaf
= NULL
;
515 spin_lock_init(&x
->lock
);
519 EXPORT_SYMBOL(xfrm_state_alloc
);
521 void __xfrm_state_destroy(struct xfrm_state
*x
)
523 struct net
*net
= xs_net(x
);
525 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
527 spin_lock_bh(&xfrm_state_gc_lock
);
528 hlist_add_head(&x
->gclist
, &net
->xfrm
.state_gc_list
);
529 spin_unlock_bh(&xfrm_state_gc_lock
);
530 schedule_work(&net
->xfrm
.state_gc_work
);
532 EXPORT_SYMBOL(__xfrm_state_destroy
);
534 int __xfrm_state_delete(struct xfrm_state
*x
)
536 struct net
*net
= xs_net(x
);
539 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
540 x
->km
.state
= XFRM_STATE_DEAD
;
541 spin_lock(&xfrm_state_lock
);
542 list_del(&x
->km
.all
);
543 hlist_del(&x
->bydst
);
544 hlist_del(&x
->bysrc
);
546 hlist_del(&x
->byspi
);
547 net
->xfrm
.state_num
--;
548 spin_unlock(&xfrm_state_lock
);
550 /* All xfrm_state objects are created by xfrm_state_alloc.
551 * The xfrm_state_alloc call gives a reference, and that
552 * is what we are dropping here.
560 EXPORT_SYMBOL(__xfrm_state_delete
);
562 int xfrm_state_delete(struct xfrm_state
*x
)
566 spin_lock_bh(&x
->lock
);
567 err
= __xfrm_state_delete(x
);
568 spin_unlock_bh(&x
->lock
);
572 EXPORT_SYMBOL(xfrm_state_delete
);
574 #ifdef CONFIG_SECURITY_NETWORK_XFRM
576 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, struct xfrm_audit
*audit_info
)
580 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
581 struct xfrm_state
*x
;
583 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
584 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
585 (err
= security_xfrm_state_delete(x
)) != 0) {
586 xfrm_audit_state_delete(x
, 0,
587 audit_info
->loginuid
,
588 audit_info
->sessionid
,
599 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, struct xfrm_audit
*audit_info
)
605 int xfrm_state_flush(struct net
*net
, u8 proto
, struct xfrm_audit
*audit_info
)
607 int i
, err
= 0, cnt
= 0;
609 spin_lock_bh(&xfrm_state_lock
);
610 err
= xfrm_state_flush_secctx_check(net
, proto
, audit_info
);
615 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
616 struct xfrm_state
*x
;
618 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
619 if (!xfrm_state_kern(x
) &&
620 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
622 spin_unlock_bh(&xfrm_state_lock
);
624 err
= xfrm_state_delete(x
);
625 xfrm_audit_state_delete(x
, err
? 0 : 1,
626 audit_info
->loginuid
,
627 audit_info
->sessionid
,
633 spin_lock_bh(&xfrm_state_lock
);
642 spin_unlock_bh(&xfrm_state_lock
);
643 wake_up(&net
->xfrm
.km_waitq
);
646 EXPORT_SYMBOL(xfrm_state_flush
);
648 void xfrm_sad_getinfo(struct net
*net
, struct xfrmk_sadinfo
*si
)
650 spin_lock_bh(&xfrm_state_lock
);
651 si
->sadcnt
= net
->xfrm
.state_num
;
652 si
->sadhcnt
= net
->xfrm
.state_hmask
;
653 si
->sadhmcnt
= xfrm_state_hashmax
;
654 spin_unlock_bh(&xfrm_state_lock
);
656 EXPORT_SYMBOL(xfrm_sad_getinfo
);
659 xfrm_init_tempstate(struct xfrm_state
*x
, const struct flowi
*fl
,
660 const struct xfrm_tmpl
*tmpl
,
661 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
662 unsigned short family
)
664 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
667 afinfo
->init_tempsel(&x
->sel
, fl
);
669 if (family
!= tmpl
->encap_family
) {
670 xfrm_state_put_afinfo(afinfo
);
671 afinfo
= xfrm_state_get_afinfo(tmpl
->encap_family
);
675 afinfo
->init_temprop(x
, tmpl
, daddr
, saddr
);
676 xfrm_state_put_afinfo(afinfo
);
680 static struct xfrm_state
*__xfrm_state_lookup(struct net
*net
, u32 mark
,
681 const xfrm_address_t
*daddr
,
682 __be32 spi
, u8 proto
,
683 unsigned short family
)
685 unsigned int h
= xfrm_spi_hash(net
, daddr
, spi
, proto
, family
);
686 struct xfrm_state
*x
;
688 hlist_for_each_entry(x
, net
->xfrm
.state_byspi
+h
, byspi
) {
689 if (x
->props
.family
!= family
||
691 x
->id
.proto
!= proto
||
692 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
))
695 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
704 static struct xfrm_state
*__xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
705 const xfrm_address_t
*daddr
,
706 const xfrm_address_t
*saddr
,
707 u8 proto
, unsigned short family
)
709 unsigned int h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
710 struct xfrm_state
*x
;
712 hlist_for_each_entry(x
, net
->xfrm
.state_bysrc
+h
, bysrc
) {
713 if (x
->props
.family
!= family
||
714 x
->id
.proto
!= proto
||
715 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
716 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
719 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
728 static inline struct xfrm_state
*
729 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
731 struct net
*net
= xs_net(x
);
732 u32 mark
= x
->mark
.v
& x
->mark
.m
;
735 return __xfrm_state_lookup(net
, mark
, &x
->id
.daddr
,
736 x
->id
.spi
, x
->id
.proto
, family
);
738 return __xfrm_state_lookup_byaddr(net
, mark
,
741 x
->id
.proto
, family
);
744 static void xfrm_hash_grow_check(struct net
*net
, int have_hash_collision
)
746 if (have_hash_collision
&&
747 (net
->xfrm
.state_hmask
+ 1) < xfrm_state_hashmax
&&
748 net
->xfrm
.state_num
> net
->xfrm
.state_hmask
)
749 schedule_work(&net
->xfrm
.state_hash_work
);
752 static void xfrm_state_look_at(struct xfrm_policy
*pol
, struct xfrm_state
*x
,
753 const struct flowi
*fl
, unsigned short family
,
754 struct xfrm_state
**best
, int *acq_in_progress
,
758 * 1. There is a valid state with matching selector. Done.
759 * 2. Valid state with inappropriate selector. Skip.
761 * Entering area of "sysdeps".
763 * 3. If state is not valid, selector is temporary, it selects
764 * only session which triggered previous resolution. Key
765 * manager will do something to install a state with proper
768 if (x
->km
.state
== XFRM_STATE_VALID
) {
769 if ((x
->sel
.family
&&
770 !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
771 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
775 (*best
)->km
.dying
> x
->km
.dying
||
776 ((*best
)->km
.dying
== x
->km
.dying
&&
777 (*best
)->curlft
.add_time
< x
->curlft
.add_time
))
779 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
780 *acq_in_progress
= 1;
781 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
782 x
->km
.state
== XFRM_STATE_EXPIRED
) {
783 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
784 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
790 xfrm_state_find(const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
791 const struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
792 struct xfrm_policy
*pol
, int *err
,
793 unsigned short family
)
795 static xfrm_address_t saddr_wildcard
= { };
796 struct net
*net
= xp_net(pol
);
797 unsigned int h
, h_wildcard
;
798 struct xfrm_state
*x
, *x0
, *to_put
;
799 int acquire_in_progress
= 0;
801 struct xfrm_state
*best
= NULL
;
802 u32 mark
= pol
->mark
.v
& pol
->mark
.m
;
803 unsigned short encap_family
= tmpl
->encap_family
;
807 spin_lock_bh(&xfrm_state_lock
);
808 h
= xfrm_dst_hash(net
, daddr
, saddr
, tmpl
->reqid
, encap_family
);
809 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
810 if (x
->props
.family
== encap_family
&&
811 x
->props
.reqid
== tmpl
->reqid
&&
812 (mark
& x
->mark
.m
) == x
->mark
.v
&&
813 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
814 xfrm_state_addr_check(x
, daddr
, saddr
, encap_family
) &&
815 tmpl
->mode
== x
->props
.mode
&&
816 tmpl
->id
.proto
== x
->id
.proto
&&
817 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
818 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
819 &best
, &acquire_in_progress
, &error
);
824 h_wildcard
= xfrm_dst_hash(net
, daddr
, &saddr_wildcard
, tmpl
->reqid
, encap_family
);
825 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h_wildcard
, bydst
) {
826 if (x
->props
.family
== encap_family
&&
827 x
->props
.reqid
== tmpl
->reqid
&&
828 (mark
& x
->mark
.m
) == x
->mark
.v
&&
829 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
830 xfrm_state_addr_check(x
, daddr
, saddr
, encap_family
) &&
831 tmpl
->mode
== x
->props
.mode
&&
832 tmpl
->id
.proto
== x
->id
.proto
&&
833 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
834 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
835 &best
, &acquire_in_progress
, &error
);
840 if (!x
&& !error
&& !acquire_in_progress
) {
842 (x0
= __xfrm_state_lookup(net
, mark
, daddr
, tmpl
->id
.spi
,
843 tmpl
->id
.proto
, encap_family
)) != NULL
) {
848 x
= xfrm_state_alloc(net
);
853 /* Initialize temporary state matching only
854 * to current session. */
855 xfrm_init_tempstate(x
, fl
, tmpl
, daddr
, saddr
, family
);
856 memcpy(&x
->mark
, &pol
->mark
, sizeof(x
->mark
));
858 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->flowi_secid
);
860 x
->km
.state
= XFRM_STATE_DEAD
;
866 if (km_query(x
, tmpl
, pol
) == 0) {
867 x
->km
.state
= XFRM_STATE_ACQ
;
868 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
869 hlist_add_head(&x
->bydst
, net
->xfrm
.state_bydst
+h
);
870 h
= xfrm_src_hash(net
, daddr
, saddr
, encap_family
);
871 hlist_add_head(&x
->bysrc
, net
->xfrm
.state_bysrc
+h
);
873 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, encap_family
);
874 hlist_add_head(&x
->byspi
, net
->xfrm
.state_byspi
+h
);
876 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
877 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(net
->xfrm
.sysctl_acq_expires
, 0), HRTIMER_MODE_REL
);
878 net
->xfrm
.state_num
++;
879 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
881 x
->km
.state
= XFRM_STATE_DEAD
;
891 *err
= acquire_in_progress
? -EAGAIN
: error
;
892 spin_unlock_bh(&xfrm_state_lock
);
894 xfrm_state_put(to_put
);
899 xfrm_stateonly_find(struct net
*net
, u32 mark
,
900 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
901 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
904 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
906 spin_lock(&xfrm_state_lock
);
907 h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
908 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
909 if (x
->props
.family
== family
&&
910 x
->props
.reqid
== reqid
&&
911 (mark
& x
->mark
.m
) == x
->mark
.v
&&
912 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
913 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
914 mode
== x
->props
.mode
&&
915 proto
== x
->id
.proto
&&
916 x
->km
.state
== XFRM_STATE_VALID
) {
924 spin_unlock(&xfrm_state_lock
);
929 EXPORT_SYMBOL(xfrm_stateonly_find
);
931 static void __xfrm_state_insert(struct xfrm_state
*x
)
933 struct net
*net
= xs_net(x
);
936 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
938 h
= xfrm_dst_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
,
939 x
->props
.reqid
, x
->props
.family
);
940 hlist_add_head(&x
->bydst
, net
->xfrm
.state_bydst
+h
);
942 h
= xfrm_src_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
943 hlist_add_head(&x
->bysrc
, net
->xfrm
.state_bysrc
+h
);
946 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
949 hlist_add_head(&x
->byspi
, net
->xfrm
.state_byspi
+h
);
952 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL
);
953 if (x
->replay_maxage
)
954 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
956 wake_up(&net
->xfrm
.km_waitq
);
958 net
->xfrm
.state_num
++;
960 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
963 /* xfrm_state_lock is held */
964 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
966 struct net
*net
= xs_net(xnew
);
967 unsigned short family
= xnew
->props
.family
;
968 u32 reqid
= xnew
->props
.reqid
;
969 struct xfrm_state
*x
;
971 u32 mark
= xnew
->mark
.v
& xnew
->mark
.m
;
973 h
= xfrm_dst_hash(net
, &xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
974 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
975 if (x
->props
.family
== family
&&
976 x
->props
.reqid
== reqid
&&
977 (mark
& x
->mark
.m
) == x
->mark
.v
&&
978 xfrm_addr_equal(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
979 xfrm_addr_equal(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
984 void xfrm_state_insert(struct xfrm_state
*x
)
986 spin_lock_bh(&xfrm_state_lock
);
987 __xfrm_state_bump_genids(x
);
988 __xfrm_state_insert(x
);
989 spin_unlock_bh(&xfrm_state_lock
);
991 EXPORT_SYMBOL(xfrm_state_insert
);
993 /* xfrm_state_lock is held */
994 static struct xfrm_state
*__find_acq_core(struct net
*net
,
995 const struct xfrm_mark
*m
,
996 unsigned short family
, u8 mode
,
998 const xfrm_address_t
*daddr
,
999 const xfrm_address_t
*saddr
,
1002 unsigned int h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1003 struct xfrm_state
*x
;
1004 u32 mark
= m
->v
& m
->m
;
1006 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1007 if (x
->props
.reqid
!= reqid
||
1008 x
->props
.mode
!= mode
||
1009 x
->props
.family
!= family
||
1010 x
->km
.state
!= XFRM_STATE_ACQ
||
1012 x
->id
.proto
!= proto
||
1013 (mark
& x
->mark
.m
) != x
->mark
.v
||
1014 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
1015 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
1025 x
= xfrm_state_alloc(net
);
1029 x
->sel
.daddr
.a4
= daddr
->a4
;
1030 x
->sel
.saddr
.a4
= saddr
->a4
;
1031 x
->sel
.prefixlen_d
= 32;
1032 x
->sel
.prefixlen_s
= 32;
1033 x
->props
.saddr
.a4
= saddr
->a4
;
1034 x
->id
.daddr
.a4
= daddr
->a4
;
1038 *(struct in6_addr
*)x
->sel
.daddr
.a6
= *(struct in6_addr
*)daddr
;
1039 *(struct in6_addr
*)x
->sel
.saddr
.a6
= *(struct in6_addr
*)saddr
;
1040 x
->sel
.prefixlen_d
= 128;
1041 x
->sel
.prefixlen_s
= 128;
1042 *(struct in6_addr
*)x
->props
.saddr
.a6
= *(struct in6_addr
*)saddr
;
1043 *(struct in6_addr
*)x
->id
.daddr
.a6
= *(struct in6_addr
*)daddr
;
1047 x
->km
.state
= XFRM_STATE_ACQ
;
1048 x
->id
.proto
= proto
;
1049 x
->props
.family
= family
;
1050 x
->props
.mode
= mode
;
1051 x
->props
.reqid
= reqid
;
1054 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1056 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(net
->xfrm
.sysctl_acq_expires
, 0), HRTIMER_MODE_REL
);
1057 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1058 hlist_add_head(&x
->bydst
, net
->xfrm
.state_bydst
+h
);
1059 h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
1060 hlist_add_head(&x
->bysrc
, net
->xfrm
.state_bysrc
+h
);
1062 net
->xfrm
.state_num
++;
1064 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1070 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
);
1072 int xfrm_state_add(struct xfrm_state
*x
)
1074 struct net
*net
= xs_net(x
);
1075 struct xfrm_state
*x1
, *to_put
;
1078 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1079 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1081 family
= x
->props
.family
;
1085 spin_lock_bh(&xfrm_state_lock
);
1087 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1095 if (use_spi
&& x
->km
.seq
) {
1096 x1
= __xfrm_find_acq_byseq(net
, mark
, x
->km
.seq
);
1097 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1098 !xfrm_addr_equal(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1105 x1
= __find_acq_core(net
, &x
->mark
, family
, x
->props
.mode
,
1106 x
->props
.reqid
, x
->id
.proto
,
1107 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1109 __xfrm_state_bump_genids(x
);
1110 __xfrm_state_insert(x
);
1114 spin_unlock_bh(&xfrm_state_lock
);
1117 xfrm_state_delete(x1
);
1122 xfrm_state_put(to_put
);
1126 EXPORT_SYMBOL(xfrm_state_add
);
1128 #ifdef CONFIG_XFRM_MIGRATE
1129 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
, int *errp
)
1131 struct net
*net
= xs_net(orig
);
1133 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1137 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1138 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1139 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1140 x
->props
.mode
= orig
->props
.mode
;
1141 x
->props
.replay_window
= orig
->props
.replay_window
;
1142 x
->props
.reqid
= orig
->props
.reqid
;
1143 x
->props
.family
= orig
->props
.family
;
1144 x
->props
.saddr
= orig
->props
.saddr
;
1147 x
->aalg
= xfrm_algo_auth_clone(orig
->aalg
);
1151 x
->props
.aalgo
= orig
->props
.aalgo
;
1154 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1158 x
->props
.ealgo
= orig
->props
.ealgo
;
1161 x
->calg
= xfrm_algo_clone(orig
->calg
);
1165 x
->props
.calgo
= orig
->props
.calgo
;
1168 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
), GFP_KERNEL
);
1174 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1180 if (orig
->replay_esn
) {
1181 err
= xfrm_replay_clone(x
, orig
);
1186 memcpy(&x
->mark
, &orig
->mark
, sizeof(x
->mark
));
1188 err
= xfrm_init_state(x
);
1192 x
->props
.flags
= orig
->props
.flags
;
1193 x
->props
.extra_flags
= orig
->props
.extra_flags
;
1195 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1196 x
->km
.state
= orig
->km
.state
;
1197 x
->km
.seq
= orig
->km
.seq
;
1209 /* xfrm_state_lock is held */
1210 struct xfrm_state
* xfrm_migrate_state_find(struct xfrm_migrate
*m
)
1213 struct xfrm_state
*x
;
1216 h
= xfrm_dst_hash(&init_net
, &m
->old_daddr
, &m
->old_saddr
,
1217 m
->reqid
, m
->old_family
);
1218 hlist_for_each_entry(x
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
1219 if (x
->props
.mode
!= m
->mode
||
1220 x
->id
.proto
!= m
->proto
)
1222 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1224 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1226 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1233 h
= xfrm_src_hash(&init_net
, &m
->old_daddr
, &m
->old_saddr
,
1235 hlist_for_each_entry(x
, init_net
.xfrm
.state_bysrc
+h
, bysrc
) {
1236 if (x
->props
.mode
!= m
->mode
||
1237 x
->id
.proto
!= m
->proto
)
1239 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1241 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1251 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1253 struct xfrm_state
* xfrm_state_migrate(struct xfrm_state
*x
,
1254 struct xfrm_migrate
*m
)
1256 struct xfrm_state
*xc
;
1259 xc
= xfrm_state_clone(x
, &err
);
1263 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1264 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1267 if (xfrm_addr_equal(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1268 /* a care is needed when the destination address of the
1269 state is to be updated as it is a part of triplet */
1270 xfrm_state_insert(xc
);
1272 if ((err
= xfrm_state_add(xc
)) < 0)
1281 EXPORT_SYMBOL(xfrm_state_migrate
);
1284 int xfrm_state_update(struct xfrm_state
*x
)
1286 struct xfrm_state
*x1
, *to_put
;
1288 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1292 spin_lock_bh(&xfrm_state_lock
);
1293 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1299 if (xfrm_state_kern(x1
)) {
1305 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1306 __xfrm_state_insert(x
);
1312 spin_unlock_bh(&xfrm_state_lock
);
1315 xfrm_state_put(to_put
);
1321 xfrm_state_delete(x1
);
1327 spin_lock_bh(&x1
->lock
);
1328 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1329 if (x
->encap
&& x1
->encap
)
1330 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1331 if (x
->coaddr
&& x1
->coaddr
) {
1332 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1334 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1335 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1336 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1339 tasklet_hrtimer_start(&x1
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL
);
1340 if (x1
->curlft
.use_time
)
1341 xfrm_state_check_expire(x1
);
1344 x
->km
.state
= XFRM_STATE_DEAD
;
1345 __xfrm_state_put(x
);
1347 spin_unlock_bh(&x1
->lock
);
1353 EXPORT_SYMBOL(xfrm_state_update
);
1355 int xfrm_state_check_expire(struct xfrm_state
*x
)
1357 if (!x
->curlft
.use_time
)
1358 x
->curlft
.use_time
= get_seconds();
1360 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1361 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1362 x
->km
.state
= XFRM_STATE_EXPIRED
;
1363 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(0,0), HRTIMER_MODE_REL
);
1368 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1369 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1371 km_state_expired(x
, 0, 0);
1375 EXPORT_SYMBOL(xfrm_state_check_expire
);
1378 xfrm_state_lookup(struct net
*net
, u32 mark
, const xfrm_address_t
*daddr
, __be32 spi
,
1379 u8 proto
, unsigned short family
)
1381 struct xfrm_state
*x
;
1383 spin_lock_bh(&xfrm_state_lock
);
1384 x
= __xfrm_state_lookup(net
, mark
, daddr
, spi
, proto
, family
);
1385 spin_unlock_bh(&xfrm_state_lock
);
1388 EXPORT_SYMBOL(xfrm_state_lookup
);
1391 xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
1392 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
1393 u8 proto
, unsigned short family
)
1395 struct xfrm_state
*x
;
1397 spin_lock_bh(&xfrm_state_lock
);
1398 x
= __xfrm_state_lookup_byaddr(net
, mark
, daddr
, saddr
, proto
, family
);
1399 spin_unlock_bh(&xfrm_state_lock
);
1402 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1405 xfrm_find_acq(struct net
*net
, const struct xfrm_mark
*mark
, u8 mode
, u32 reqid
,
1406 u8 proto
, const xfrm_address_t
*daddr
,
1407 const xfrm_address_t
*saddr
, int create
, unsigned short family
)
1409 struct xfrm_state
*x
;
1411 spin_lock_bh(&xfrm_state_lock
);
1412 x
= __find_acq_core(net
, mark
, family
, mode
, reqid
, proto
, daddr
, saddr
, create
);
1413 spin_unlock_bh(&xfrm_state_lock
);
1417 EXPORT_SYMBOL(xfrm_find_acq
);
1419 #ifdef CONFIG_XFRM_SUB_POLICY
1421 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1422 unsigned short family
)
1425 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1427 return -EAFNOSUPPORT
;
1429 spin_lock_bh(&xfrm_state_lock
);
1430 if (afinfo
->tmpl_sort
)
1431 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1432 spin_unlock_bh(&xfrm_state_lock
);
1433 xfrm_state_put_afinfo(afinfo
);
1436 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1439 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1440 unsigned short family
)
1443 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1445 return -EAFNOSUPPORT
;
1447 spin_lock_bh(&xfrm_state_lock
);
1448 if (afinfo
->state_sort
)
1449 err
= afinfo
->state_sort(dst
, src
, n
);
1450 spin_unlock_bh(&xfrm_state_lock
);
1451 xfrm_state_put_afinfo(afinfo
);
1454 EXPORT_SYMBOL(xfrm_state_sort
);
1457 /* Silly enough, but I'm lazy to build resolution list */
1459 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1463 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
1464 struct xfrm_state
*x
;
1466 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
1467 if (x
->km
.seq
== seq
&&
1468 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1469 x
->km
.state
== XFRM_STATE_ACQ
) {
1478 struct xfrm_state
*xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1480 struct xfrm_state
*x
;
1482 spin_lock_bh(&xfrm_state_lock
);
1483 x
= __xfrm_find_acq_byseq(net
, mark
, seq
);
1484 spin_unlock_bh(&xfrm_state_lock
);
1487 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1489 u32
xfrm_get_acqseq(void)
1492 static atomic_t acqseq
;
1495 res
= atomic_inc_return(&acqseq
);
1500 EXPORT_SYMBOL(xfrm_get_acqseq
);
1502 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1504 struct net
*net
= xs_net(x
);
1506 struct xfrm_state
*x0
;
1508 __be32 minspi
= htonl(low
);
1509 __be32 maxspi
= htonl(high
);
1510 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1512 spin_lock_bh(&x
->lock
);
1513 if (x
->km
.state
== XFRM_STATE_DEAD
)
1522 if (minspi
== maxspi
) {
1523 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1531 for (h
=0; h
<high
-low
+1; h
++) {
1532 spi
= low
+ net_random()%(high
-low
+1);
1533 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1535 x
->id
.spi
= htonl(spi
);
1542 spin_lock_bh(&xfrm_state_lock
);
1543 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1544 hlist_add_head(&x
->byspi
, net
->xfrm
.state_byspi
+h
);
1545 spin_unlock_bh(&xfrm_state_lock
);
1551 spin_unlock_bh(&x
->lock
);
1555 EXPORT_SYMBOL(xfrm_alloc_spi
);
1557 int xfrm_state_walk(struct net
*net
, struct xfrm_state_walk
*walk
,
1558 int (*func
)(struct xfrm_state
*, int, void*),
1561 struct xfrm_state
*state
;
1562 struct xfrm_state_walk
*x
;
1565 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
1568 spin_lock_bh(&xfrm_state_lock
);
1569 if (list_empty(&walk
->all
))
1570 x
= list_first_entry(&net
->xfrm
.state_all
, struct xfrm_state_walk
, all
);
1572 x
= list_entry(&walk
->all
, struct xfrm_state_walk
, all
);
1573 list_for_each_entry_from(x
, &net
->xfrm
.state_all
, all
) {
1574 if (x
->state
== XFRM_STATE_DEAD
)
1576 state
= container_of(x
, struct xfrm_state
, km
);
1577 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
1579 err
= func(state
, walk
->seq
, data
);
1581 list_move_tail(&walk
->all
, &x
->all
);
1586 if (walk
->seq
== 0) {
1590 list_del_init(&walk
->all
);
1592 spin_unlock_bh(&xfrm_state_lock
);
1595 EXPORT_SYMBOL(xfrm_state_walk
);
1597 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
)
1599 INIT_LIST_HEAD(&walk
->all
);
1600 walk
->proto
= proto
;
1601 walk
->state
= XFRM_STATE_DEAD
;
1604 EXPORT_SYMBOL(xfrm_state_walk_init
);
1606 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
)
1608 if (list_empty(&walk
->all
))
1611 spin_lock_bh(&xfrm_state_lock
);
1612 list_del(&walk
->all
);
1613 spin_unlock_bh(&xfrm_state_lock
);
1615 EXPORT_SYMBOL(xfrm_state_walk_done
);
1617 static void xfrm_replay_timer_handler(unsigned long data
)
1619 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
1621 spin_lock(&x
->lock
);
1623 if (x
->km
.state
== XFRM_STATE_VALID
) {
1624 if (xfrm_aevent_is_on(xs_net(x
)))
1625 x
->repl
->notify(x
, XFRM_REPLAY_TIMEOUT
);
1627 x
->xflags
|= XFRM_TIME_DEFER
;
1630 spin_unlock(&x
->lock
);
1633 static LIST_HEAD(xfrm_km_list
);
1635 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
1637 struct xfrm_mgr
*km
;
1640 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1641 if (km
->notify_policy
)
1642 km
->notify_policy(xp
, dir
, c
);
1646 void km_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
1648 struct xfrm_mgr
*km
;
1650 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1656 EXPORT_SYMBOL(km_policy_notify
);
1657 EXPORT_SYMBOL(km_state_notify
);
1659 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
)
1661 struct net
*net
= xs_net(x
);
1666 c
.event
= XFRM_MSG_EXPIRE
;
1667 km_state_notify(x
, &c
);
1670 wake_up(&net
->xfrm
.km_waitq
);
1673 EXPORT_SYMBOL(km_state_expired
);
1675 * We send to all registered managers regardless of failure
1676 * We are happy with one success
1678 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1680 int err
= -EINVAL
, acqret
;
1681 struct xfrm_mgr
*km
;
1684 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1685 acqret
= km
->acquire(x
, t
, pol
);
1692 EXPORT_SYMBOL(km_query
);
1694 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1697 struct xfrm_mgr
*km
;
1700 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1701 if (km
->new_mapping
)
1702 err
= km
->new_mapping(x
, ipaddr
, sport
);
1709 EXPORT_SYMBOL(km_new_mapping
);
1711 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 portid
)
1713 struct net
*net
= xp_net(pol
);
1718 c
.event
= XFRM_MSG_POLEXPIRE
;
1719 km_policy_notify(pol
, dir
, &c
);
1722 wake_up(&net
->xfrm
.km_waitq
);
1724 EXPORT_SYMBOL(km_policy_expired
);
1726 #ifdef CONFIG_XFRM_MIGRATE
1727 int km_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1728 const struct xfrm_migrate
*m
, int num_migrate
,
1729 const struct xfrm_kmaddress
*k
)
1733 struct xfrm_mgr
*km
;
1736 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1738 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
);
1746 EXPORT_SYMBOL(km_migrate
);
1749 int km_report(struct net
*net
, u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
1753 struct xfrm_mgr
*km
;
1756 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1758 ret
= km
->report(net
, proto
, sel
, addr
);
1766 EXPORT_SYMBOL(km_report
);
1768 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
1772 struct xfrm_mgr
*km
;
1773 struct xfrm_policy
*pol
= NULL
;
1775 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
1778 data
= kmalloc(optlen
, GFP_KERNEL
);
1783 if (copy_from_user(data
, optval
, optlen
))
1788 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1789 pol
= km
->compile_policy(sk
, optname
, data
,
1797 xfrm_sk_policy_insert(sk
, err
, pol
);
1806 EXPORT_SYMBOL(xfrm_user_policy
);
1808 static DEFINE_SPINLOCK(xfrm_km_lock
);
1810 int xfrm_register_km(struct xfrm_mgr
*km
)
1812 spin_lock_bh(&xfrm_km_lock
);
1813 list_add_tail_rcu(&km
->list
, &xfrm_km_list
);
1814 spin_unlock_bh(&xfrm_km_lock
);
1817 EXPORT_SYMBOL(xfrm_register_km
);
1819 int xfrm_unregister_km(struct xfrm_mgr
*km
)
1821 spin_lock_bh(&xfrm_km_lock
);
1822 list_del_rcu(&km
->list
);
1823 spin_unlock_bh(&xfrm_km_lock
);
1827 EXPORT_SYMBOL(xfrm_unregister_km
);
1829 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
1832 if (unlikely(afinfo
== NULL
))
1834 if (unlikely(afinfo
->family
>= NPROTO
))
1835 return -EAFNOSUPPORT
;
1836 spin_lock_bh(&xfrm_state_afinfo_lock
);
1837 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
1840 rcu_assign_pointer(xfrm_state_afinfo
[afinfo
->family
], afinfo
);
1841 spin_unlock_bh(&xfrm_state_afinfo_lock
);
1844 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
1846 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
1849 if (unlikely(afinfo
== NULL
))
1851 if (unlikely(afinfo
->family
>= NPROTO
))
1852 return -EAFNOSUPPORT
;
1853 spin_lock_bh(&xfrm_state_afinfo_lock
);
1854 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
1855 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != afinfo
))
1858 RCU_INIT_POINTER(xfrm_state_afinfo
[afinfo
->family
], NULL
);
1860 spin_unlock_bh(&xfrm_state_afinfo_lock
);
1864 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
1866 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
1868 struct xfrm_state_afinfo
*afinfo
;
1869 if (unlikely(family
>= NPROTO
))
1872 afinfo
= rcu_dereference(xfrm_state_afinfo
[family
]);
1873 if (unlikely(!afinfo
))
1878 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
)
1883 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1884 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
1887 struct xfrm_state
*t
= x
->tunnel
;
1889 if (atomic_read(&t
->tunnel_users
) == 2)
1890 xfrm_state_delete(t
);
1891 atomic_dec(&t
->tunnel_users
);
1896 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
1898 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
1902 spin_lock_bh(&x
->lock
);
1903 if (x
->km
.state
== XFRM_STATE_VALID
&&
1904 x
->type
&& x
->type
->get_mtu
)
1905 res
= x
->type
->get_mtu(x
, mtu
);
1907 res
= mtu
- x
->props
.header_len
;
1908 spin_unlock_bh(&x
->lock
);
1912 int __xfrm_init_state(struct xfrm_state
*x
, bool init_replay
)
1914 struct xfrm_state_afinfo
*afinfo
;
1915 struct xfrm_mode
*inner_mode
;
1916 int family
= x
->props
.family
;
1919 err
= -EAFNOSUPPORT
;
1920 afinfo
= xfrm_state_get_afinfo(family
);
1925 if (afinfo
->init_flags
)
1926 err
= afinfo
->init_flags(x
);
1928 xfrm_state_put_afinfo(afinfo
);
1933 err
= -EPROTONOSUPPORT
;
1935 if (x
->sel
.family
!= AF_UNSPEC
) {
1936 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
1937 if (inner_mode
== NULL
)
1940 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
1941 family
!= x
->sel
.family
) {
1942 xfrm_put_mode(inner_mode
);
1946 x
->inner_mode
= inner_mode
;
1948 struct xfrm_mode
*inner_mode_iaf
;
1949 int iafamily
= AF_INET
;
1951 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->props
.family
);
1952 if (inner_mode
== NULL
)
1955 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
1956 xfrm_put_mode(inner_mode
);
1959 x
->inner_mode
= inner_mode
;
1961 if (x
->props
.family
== AF_INET
)
1962 iafamily
= AF_INET6
;
1964 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, iafamily
);
1965 if (inner_mode_iaf
) {
1966 if (inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)
1967 x
->inner_mode_iaf
= inner_mode_iaf
;
1969 xfrm_put_mode(inner_mode_iaf
);
1973 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
1974 if (x
->type
== NULL
)
1977 err
= x
->type
->init_state(x
);
1981 x
->outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
1982 if (x
->outer_mode
== NULL
) {
1983 err
= -EPROTONOSUPPORT
;
1988 err
= xfrm_init_replay(x
);
1993 x
->km
.state
= XFRM_STATE_VALID
;
1999 EXPORT_SYMBOL(__xfrm_init_state
);
2001 int xfrm_init_state(struct xfrm_state
*x
)
2003 return __xfrm_init_state(x
, true);
2006 EXPORT_SYMBOL(xfrm_init_state
);
2008 int __net_init
xfrm_state_init(struct net
*net
)
2012 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2014 sz
= sizeof(struct hlist_head
) * 8;
2016 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2017 if (!net
->xfrm
.state_bydst
)
2019 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2020 if (!net
->xfrm
.state_bysrc
)
2022 net
->xfrm
.state_byspi
= xfrm_hash_alloc(sz
);
2023 if (!net
->xfrm
.state_byspi
)
2025 net
->xfrm
.state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2027 net
->xfrm
.state_num
= 0;
2028 INIT_WORK(&net
->xfrm
.state_hash_work
, xfrm_hash_resize
);
2029 INIT_HLIST_HEAD(&net
->xfrm
.state_gc_list
);
2030 INIT_WORK(&net
->xfrm
.state_gc_work
, xfrm_state_gc_task
);
2031 init_waitqueue_head(&net
->xfrm
.km_waitq
);
2035 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2037 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2042 void xfrm_state_fini(struct net
*net
)
2044 struct xfrm_audit audit_info
;
2047 flush_work(&net
->xfrm
.state_hash_work
);
2048 audit_info
.loginuid
= INVALID_UID
;
2049 audit_info
.sessionid
= -1;
2050 audit_info
.secid
= 0;
2051 xfrm_state_flush(net
, IPSEC_PROTO_ANY
, &audit_info
);
2052 flush_work(&net
->xfrm
.state_gc_work
);
2054 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2056 sz
= (net
->xfrm
.state_hmask
+ 1) * sizeof(struct hlist_head
);
2057 WARN_ON(!hlist_empty(net
->xfrm
.state_byspi
));
2058 xfrm_hash_free(net
->xfrm
.state_byspi
, sz
);
2059 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2060 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2061 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2062 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2065 #ifdef CONFIG_AUDITSYSCALL
2066 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2067 struct audit_buffer
*audit_buf
)
2069 struct xfrm_sec_ctx
*ctx
= x
->security
;
2070 u32 spi
= ntohl(x
->id
.spi
);
2073 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2074 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2076 switch(x
->props
.family
) {
2078 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2079 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2082 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2083 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2087 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2090 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2091 struct audit_buffer
*audit_buf
)
2093 const struct iphdr
*iph4
;
2094 const struct ipv6hdr
*iph6
;
2099 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2100 &iph4
->saddr
, &iph4
->daddr
);
2103 iph6
= ipv6_hdr(skb
);
2104 audit_log_format(audit_buf
,
2105 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2106 &iph6
->saddr
,&iph6
->daddr
,
2107 iph6
->flow_lbl
[0] & 0x0f,
2114 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
,
2115 kuid_t auid
, u32 sessionid
, u32 secid
)
2117 struct audit_buffer
*audit_buf
;
2119 audit_buf
= xfrm_audit_start("SAD-add");
2120 if (audit_buf
== NULL
)
2122 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2123 xfrm_audit_helper_sainfo(x
, audit_buf
);
2124 audit_log_format(audit_buf
, " res=%u", result
);
2125 audit_log_end(audit_buf
);
2127 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2129 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
,
2130 kuid_t auid
, u32 sessionid
, u32 secid
)
2132 struct audit_buffer
*audit_buf
;
2134 audit_buf
= xfrm_audit_start("SAD-delete");
2135 if (audit_buf
== NULL
)
2137 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2138 xfrm_audit_helper_sainfo(x
, audit_buf
);
2139 audit_log_format(audit_buf
, " res=%u", result
);
2140 audit_log_end(audit_buf
);
2142 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2144 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2145 struct sk_buff
*skb
)
2147 struct audit_buffer
*audit_buf
;
2150 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2151 if (audit_buf
== NULL
)
2153 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2154 /* don't record the sequence number because it's inherent in this kind
2155 * of audit message */
2156 spi
= ntohl(x
->id
.spi
);
2157 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2158 audit_log_end(audit_buf
);
2160 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2162 void xfrm_audit_state_replay(struct xfrm_state
*x
,
2163 struct sk_buff
*skb
, __be32 net_seq
)
2165 struct audit_buffer
*audit_buf
;
2168 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2169 if (audit_buf
== NULL
)
2171 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2172 spi
= ntohl(x
->id
.spi
);
2173 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2174 spi
, spi
, ntohl(net_seq
));
2175 audit_log_end(audit_buf
);
2177 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay
);
2179 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2181 struct audit_buffer
*audit_buf
;
2183 audit_buf
= xfrm_audit_start("SA-notfound");
2184 if (audit_buf
== NULL
)
2186 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2187 audit_log_end(audit_buf
);
2189 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2191 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2192 __be32 net_spi
, __be32 net_seq
)
2194 struct audit_buffer
*audit_buf
;
2197 audit_buf
= xfrm_audit_start("SA-notfound");
2198 if (audit_buf
== NULL
)
2200 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2201 spi
= ntohl(net_spi
);
2202 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2203 spi
, spi
, ntohl(net_seq
));
2204 audit_log_end(audit_buf
);
2206 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2208 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2209 struct sk_buff
*skb
, u8 proto
)
2211 struct audit_buffer
*audit_buf
;
2215 audit_buf
= xfrm_audit_start("SA-icv-failure");
2216 if (audit_buf
== NULL
)
2218 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2219 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2220 u32 spi
= ntohl(net_spi
);
2221 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2222 spi
, spi
, ntohl(net_seq
));
2224 audit_log_end(audit_buf
);
2226 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2227 #endif /* CONFIG_AUDITSYSCALL */