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 unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
40 static inline unsigned int xfrm_dst_hash(struct net
*net
,
41 const xfrm_address_t
*daddr
,
42 const xfrm_address_t
*saddr
,
44 unsigned short family
)
46 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, net
->xfrm
.state_hmask
);
49 static inline unsigned int xfrm_src_hash(struct net
*net
,
50 const xfrm_address_t
*daddr
,
51 const xfrm_address_t
*saddr
,
52 unsigned short family
)
54 return __xfrm_src_hash(daddr
, saddr
, family
, net
->xfrm
.state_hmask
);
57 static inline unsigned int
58 xfrm_spi_hash(struct net
*net
, const xfrm_address_t
*daddr
,
59 __be32 spi
, u8 proto
, unsigned short family
)
61 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, net
->xfrm
.state_hmask
);
64 static void xfrm_hash_transfer(struct hlist_head
*list
,
65 struct hlist_head
*ndsttable
,
66 struct hlist_head
*nsrctable
,
67 struct hlist_head
*nspitable
,
68 unsigned int nhashmask
)
70 struct hlist_node
*tmp
;
73 hlist_for_each_entry_safe(x
, tmp
, list
, bydst
) {
76 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
77 x
->props
.reqid
, x
->props
.family
,
79 hlist_add_head(&x
->bydst
, ndsttable
+h
);
81 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
84 hlist_add_head(&x
->bysrc
, nsrctable
+h
);
87 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
88 x
->id
.proto
, x
->props
.family
,
90 hlist_add_head(&x
->byspi
, nspitable
+h
);
95 static unsigned long xfrm_hash_new_size(unsigned int state_hmask
)
97 return ((state_hmask
+ 1) << 1) * sizeof(struct hlist_head
);
100 static DEFINE_MUTEX(hash_resize_mutex
);
102 static void xfrm_hash_resize(struct work_struct
*work
)
104 struct net
*net
= container_of(work
, struct net
, xfrm
.state_hash_work
);
105 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
106 unsigned long nsize
, osize
;
107 unsigned int nhashmask
, ohashmask
;
110 mutex_lock(&hash_resize_mutex
);
112 nsize
= xfrm_hash_new_size(net
->xfrm
.state_hmask
);
113 ndst
= xfrm_hash_alloc(nsize
);
116 nsrc
= xfrm_hash_alloc(nsize
);
118 xfrm_hash_free(ndst
, nsize
);
121 nspi
= xfrm_hash_alloc(nsize
);
123 xfrm_hash_free(ndst
, nsize
);
124 xfrm_hash_free(nsrc
, nsize
);
128 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
130 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
131 for (i
= net
->xfrm
.state_hmask
; i
>= 0; i
--)
132 xfrm_hash_transfer(net
->xfrm
.state_bydst
+i
, ndst
, nsrc
, nspi
,
135 odst
= net
->xfrm
.state_bydst
;
136 osrc
= net
->xfrm
.state_bysrc
;
137 ospi
= net
->xfrm
.state_byspi
;
138 ohashmask
= net
->xfrm
.state_hmask
;
140 net
->xfrm
.state_bydst
= ndst
;
141 net
->xfrm
.state_bysrc
= nsrc
;
142 net
->xfrm
.state_byspi
= nspi
;
143 net
->xfrm
.state_hmask
= nhashmask
;
145 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
147 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
148 xfrm_hash_free(odst
, osize
);
149 xfrm_hash_free(osrc
, osize
);
150 xfrm_hash_free(ospi
, osize
);
153 mutex_unlock(&hash_resize_mutex
);
156 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock
);
157 static struct xfrm_state_afinfo __rcu
*xfrm_state_afinfo
[NPROTO
];
159 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
161 int __xfrm_state_delete(struct xfrm_state
*x
);
163 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
164 bool km_is_alive(const struct km_event
*c
);
165 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
);
167 static DEFINE_SPINLOCK(xfrm_type_lock
);
168 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
170 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
171 const struct xfrm_type
**typemap
;
174 if (unlikely(afinfo
== NULL
))
175 return -EAFNOSUPPORT
;
176 typemap
= afinfo
->type_map
;
177 spin_lock_bh(&xfrm_type_lock
);
179 if (likely(typemap
[type
->proto
] == NULL
))
180 typemap
[type
->proto
] = type
;
183 spin_unlock_bh(&xfrm_type_lock
);
184 xfrm_state_put_afinfo(afinfo
);
187 EXPORT_SYMBOL(xfrm_register_type
);
189 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
191 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
192 const struct xfrm_type
**typemap
;
195 if (unlikely(afinfo
== NULL
))
196 return -EAFNOSUPPORT
;
197 typemap
= afinfo
->type_map
;
198 spin_lock_bh(&xfrm_type_lock
);
200 if (unlikely(typemap
[type
->proto
] != type
))
203 typemap
[type
->proto
] = NULL
;
204 spin_unlock_bh(&xfrm_type_lock
);
205 xfrm_state_put_afinfo(afinfo
);
208 EXPORT_SYMBOL(xfrm_unregister_type
);
210 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
212 struct xfrm_state_afinfo
*afinfo
;
213 const struct xfrm_type
**typemap
;
214 const struct xfrm_type
*type
;
215 int modload_attempted
= 0;
218 afinfo
= xfrm_state_get_afinfo(family
);
219 if (unlikely(afinfo
== NULL
))
221 typemap
= afinfo
->type_map
;
223 type
= typemap
[proto
];
224 if (unlikely(type
&& !try_module_get(type
->owner
)))
226 if (!type
&& !modload_attempted
) {
227 xfrm_state_put_afinfo(afinfo
);
228 request_module("xfrm-type-%d-%d", family
, proto
);
229 modload_attempted
= 1;
233 xfrm_state_put_afinfo(afinfo
);
237 static void xfrm_put_type(const struct xfrm_type
*type
)
239 module_put(type
->owner
);
242 static DEFINE_SPINLOCK(xfrm_mode_lock
);
243 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
245 struct xfrm_state_afinfo
*afinfo
;
246 struct xfrm_mode
**modemap
;
249 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
252 afinfo
= xfrm_state_get_afinfo(family
);
253 if (unlikely(afinfo
== NULL
))
254 return -EAFNOSUPPORT
;
257 modemap
= afinfo
->mode_map
;
258 spin_lock_bh(&xfrm_mode_lock
);
259 if (modemap
[mode
->encap
])
263 if (!try_module_get(afinfo
->owner
))
266 mode
->afinfo
= afinfo
;
267 modemap
[mode
->encap
] = mode
;
271 spin_unlock_bh(&xfrm_mode_lock
);
272 xfrm_state_put_afinfo(afinfo
);
275 EXPORT_SYMBOL(xfrm_register_mode
);
277 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
279 struct xfrm_state_afinfo
*afinfo
;
280 struct xfrm_mode
**modemap
;
283 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
286 afinfo
= xfrm_state_get_afinfo(family
);
287 if (unlikely(afinfo
== NULL
))
288 return -EAFNOSUPPORT
;
291 modemap
= afinfo
->mode_map
;
292 spin_lock_bh(&xfrm_mode_lock
);
293 if (likely(modemap
[mode
->encap
] == mode
)) {
294 modemap
[mode
->encap
] = NULL
;
295 module_put(mode
->afinfo
->owner
);
299 spin_unlock_bh(&xfrm_mode_lock
);
300 xfrm_state_put_afinfo(afinfo
);
303 EXPORT_SYMBOL(xfrm_unregister_mode
);
305 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
307 struct xfrm_state_afinfo
*afinfo
;
308 struct xfrm_mode
*mode
;
309 int modload_attempted
= 0;
311 if (unlikely(encap
>= XFRM_MODE_MAX
))
315 afinfo
= xfrm_state_get_afinfo(family
);
316 if (unlikely(afinfo
== NULL
))
319 mode
= afinfo
->mode_map
[encap
];
320 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
322 if (!mode
&& !modload_attempted
) {
323 xfrm_state_put_afinfo(afinfo
);
324 request_module("xfrm-mode-%d-%d", family
, encap
);
325 modload_attempted
= 1;
329 xfrm_state_put_afinfo(afinfo
);
333 static void xfrm_put_mode(struct xfrm_mode
*mode
)
335 module_put(mode
->owner
);
338 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
340 tasklet_hrtimer_cancel(&x
->mtimer
);
341 del_timer_sync(&x
->rtimer
);
348 kfree(x
->replay_esn
);
349 kfree(x
->preplay_esn
);
351 xfrm_put_mode(x
->inner_mode
);
352 if (x
->inner_mode_iaf
)
353 xfrm_put_mode(x
->inner_mode_iaf
);
355 xfrm_put_mode(x
->outer_mode
);
357 x
->type
->destructor(x
);
358 xfrm_put_type(x
->type
);
360 security_xfrm_state_free(x
);
364 static void xfrm_state_gc_task(struct work_struct
*work
)
366 struct net
*net
= container_of(work
, struct net
, xfrm
.state_gc_work
);
367 struct xfrm_state
*x
;
368 struct hlist_node
*tmp
;
369 struct hlist_head gc_list
;
371 spin_lock_bh(&xfrm_state_gc_lock
);
372 hlist_move_list(&net
->xfrm
.state_gc_list
, &gc_list
);
373 spin_unlock_bh(&xfrm_state_gc_lock
);
375 hlist_for_each_entry_safe(x
, tmp
, &gc_list
, gclist
)
376 xfrm_state_gc_destroy(x
);
379 static inline unsigned long make_jiffies(long secs
)
381 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
382 return MAX_SCHEDULE_TIMEOUT
-1;
387 static enum hrtimer_restart
xfrm_timer_handler(struct hrtimer
*me
)
389 struct tasklet_hrtimer
*thr
= container_of(me
, struct tasklet_hrtimer
, timer
);
390 struct xfrm_state
*x
= container_of(thr
, struct xfrm_state
, mtimer
);
391 unsigned long now
= get_seconds();
392 long next
= LONG_MAX
;
397 if (x
->km
.state
== XFRM_STATE_DEAD
)
399 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
401 if (x
->lft
.hard_add_expires_seconds
) {
402 long tmo
= x
->lft
.hard_add_expires_seconds
+
403 x
->curlft
.add_time
- now
;
405 if (x
->xflags
& XFRM_SOFT_EXPIRE
) {
406 /* enter hard expire without soft expire first?!
407 * setting a new date could trigger this.
408 * workarbound: fix x->curflt.add_time by below:
410 x
->curlft
.add_time
= now
- x
->saved_tmo
- 1;
411 tmo
= x
->lft
.hard_add_expires_seconds
- x
->saved_tmo
;
418 if (x
->lft
.hard_use_expires_seconds
) {
419 long tmo
= x
->lft
.hard_use_expires_seconds
+
420 (x
->curlft
.use_time
? : now
) - now
;
428 if (x
->lft
.soft_add_expires_seconds
) {
429 long tmo
= x
->lft
.soft_add_expires_seconds
+
430 x
->curlft
.add_time
- now
;
433 x
->xflags
&= ~XFRM_SOFT_EXPIRE
;
434 } else if (tmo
< next
) {
436 x
->xflags
|= XFRM_SOFT_EXPIRE
;
440 if (x
->lft
.soft_use_expires_seconds
) {
441 long tmo
= x
->lft
.soft_use_expires_seconds
+
442 (x
->curlft
.use_time
? : now
) - now
;
451 km_state_expired(x
, 0, 0);
453 if (next
!= LONG_MAX
) {
454 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(next
, 0), HRTIMER_MODE_REL
);
460 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0)
461 x
->km
.state
= XFRM_STATE_EXPIRED
;
463 err
= __xfrm_state_delete(x
);
465 km_state_expired(x
, 1, 0);
467 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
470 spin_unlock(&x
->lock
);
471 return HRTIMER_NORESTART
;
474 static void xfrm_replay_timer_handler(unsigned long data
);
476 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
478 struct xfrm_state
*x
;
480 x
= kzalloc(sizeof(struct xfrm_state
), GFP_ATOMIC
);
483 write_pnet(&x
->xs_net
, net
);
484 atomic_set(&x
->refcnt
, 1);
485 atomic_set(&x
->tunnel_users
, 0);
486 INIT_LIST_HEAD(&x
->km
.all
);
487 INIT_HLIST_NODE(&x
->bydst
);
488 INIT_HLIST_NODE(&x
->bysrc
);
489 INIT_HLIST_NODE(&x
->byspi
);
490 tasklet_hrtimer_init(&x
->mtimer
, xfrm_timer_handler
,
491 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
492 setup_timer(&x
->rtimer
, xfrm_replay_timer_handler
,
494 x
->curlft
.add_time
= get_seconds();
495 x
->lft
.soft_byte_limit
= XFRM_INF
;
496 x
->lft
.soft_packet_limit
= XFRM_INF
;
497 x
->lft
.hard_byte_limit
= XFRM_INF
;
498 x
->lft
.hard_packet_limit
= XFRM_INF
;
499 x
->replay_maxage
= 0;
500 x
->replay_maxdiff
= 0;
501 x
->inner_mode
= NULL
;
502 x
->inner_mode_iaf
= NULL
;
503 spin_lock_init(&x
->lock
);
507 EXPORT_SYMBOL(xfrm_state_alloc
);
509 void __xfrm_state_destroy(struct xfrm_state
*x
)
511 struct net
*net
= xs_net(x
);
513 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
515 spin_lock_bh(&xfrm_state_gc_lock
);
516 hlist_add_head(&x
->gclist
, &net
->xfrm
.state_gc_list
);
517 spin_unlock_bh(&xfrm_state_gc_lock
);
518 schedule_work(&net
->xfrm
.state_gc_work
);
520 EXPORT_SYMBOL(__xfrm_state_destroy
);
522 int __xfrm_state_delete(struct xfrm_state
*x
)
524 struct net
*net
= xs_net(x
);
527 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
528 x
->km
.state
= XFRM_STATE_DEAD
;
529 spin_lock(&net
->xfrm
.xfrm_state_lock
);
530 list_del(&x
->km
.all
);
531 hlist_del(&x
->bydst
);
532 hlist_del(&x
->bysrc
);
534 hlist_del(&x
->byspi
);
535 net
->xfrm
.state_num
--;
536 spin_unlock(&net
->xfrm
.xfrm_state_lock
);
538 /* All xfrm_state objects are created by xfrm_state_alloc.
539 * The xfrm_state_alloc call gives a reference, and that
540 * is what we are dropping here.
548 EXPORT_SYMBOL(__xfrm_state_delete
);
550 int xfrm_state_delete(struct xfrm_state
*x
)
554 spin_lock_bh(&x
->lock
);
555 err
= __xfrm_state_delete(x
);
556 spin_unlock_bh(&x
->lock
);
560 EXPORT_SYMBOL(xfrm_state_delete
);
562 #ifdef CONFIG_SECURITY_NETWORK_XFRM
564 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
568 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
569 struct xfrm_state
*x
;
571 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
572 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
573 (err
= security_xfrm_state_delete(x
)) != 0) {
574 xfrm_audit_state_delete(x
, 0, task_valid
);
584 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
590 int xfrm_state_flush(struct net
*net
, u8 proto
, bool task_valid
)
592 int i
, err
= 0, cnt
= 0;
594 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
595 err
= xfrm_state_flush_secctx_check(net
, proto
, task_valid
);
600 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
601 struct xfrm_state
*x
;
603 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
604 if (!xfrm_state_kern(x
) &&
605 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
607 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
609 err
= xfrm_state_delete(x
);
610 xfrm_audit_state_delete(x
, err
? 0 : 1,
616 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
625 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
628 EXPORT_SYMBOL(xfrm_state_flush
);
630 void xfrm_sad_getinfo(struct net
*net
, struct xfrmk_sadinfo
*si
)
632 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
633 si
->sadcnt
= net
->xfrm
.state_num
;
634 si
->sadhcnt
= net
->xfrm
.state_hmask
;
635 si
->sadhmcnt
= xfrm_state_hashmax
;
636 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
638 EXPORT_SYMBOL(xfrm_sad_getinfo
);
641 xfrm_init_tempstate(struct xfrm_state
*x
, const struct flowi
*fl
,
642 const struct xfrm_tmpl
*tmpl
,
643 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
644 unsigned short family
)
646 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
649 afinfo
->init_tempsel(&x
->sel
, fl
);
651 if (family
!= tmpl
->encap_family
) {
652 xfrm_state_put_afinfo(afinfo
);
653 afinfo
= xfrm_state_get_afinfo(tmpl
->encap_family
);
657 afinfo
->init_temprop(x
, tmpl
, daddr
, saddr
);
658 xfrm_state_put_afinfo(afinfo
);
662 static struct xfrm_state
*__xfrm_state_lookup(struct net
*net
, u32 mark
,
663 const xfrm_address_t
*daddr
,
664 __be32 spi
, u8 proto
,
665 unsigned short family
)
667 unsigned int h
= xfrm_spi_hash(net
, daddr
, spi
, proto
, family
);
668 struct xfrm_state
*x
;
670 hlist_for_each_entry(x
, net
->xfrm
.state_byspi
+h
, byspi
) {
671 if (x
->props
.family
!= family
||
673 x
->id
.proto
!= proto
||
674 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
))
677 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
686 static struct xfrm_state
*__xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
687 const xfrm_address_t
*daddr
,
688 const xfrm_address_t
*saddr
,
689 u8 proto
, unsigned short family
)
691 unsigned int h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
692 struct xfrm_state
*x
;
694 hlist_for_each_entry(x
, net
->xfrm
.state_bysrc
+h
, bysrc
) {
695 if (x
->props
.family
!= family
||
696 x
->id
.proto
!= proto
||
697 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
698 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
701 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
710 static inline struct xfrm_state
*
711 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
713 struct net
*net
= xs_net(x
);
714 u32 mark
= x
->mark
.v
& x
->mark
.m
;
717 return __xfrm_state_lookup(net
, mark
, &x
->id
.daddr
,
718 x
->id
.spi
, x
->id
.proto
, family
);
720 return __xfrm_state_lookup_byaddr(net
, mark
,
723 x
->id
.proto
, family
);
726 static void xfrm_hash_grow_check(struct net
*net
, int have_hash_collision
)
728 if (have_hash_collision
&&
729 (net
->xfrm
.state_hmask
+ 1) < xfrm_state_hashmax
&&
730 net
->xfrm
.state_num
> net
->xfrm
.state_hmask
)
731 schedule_work(&net
->xfrm
.state_hash_work
);
734 static void xfrm_state_look_at(struct xfrm_policy
*pol
, struct xfrm_state
*x
,
735 const struct flowi
*fl
, unsigned short family
,
736 struct xfrm_state
**best
, int *acq_in_progress
,
740 * 1. There is a valid state with matching selector. Done.
741 * 2. Valid state with inappropriate selector. Skip.
743 * Entering area of "sysdeps".
745 * 3. If state is not valid, selector is temporary, it selects
746 * only session which triggered previous resolution. Key
747 * manager will do something to install a state with proper
750 if (x
->km
.state
== XFRM_STATE_VALID
) {
751 if ((x
->sel
.family
&&
752 !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
753 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
757 (*best
)->km
.dying
> x
->km
.dying
||
758 ((*best
)->km
.dying
== x
->km
.dying
&&
759 (*best
)->curlft
.add_time
< x
->curlft
.add_time
))
761 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
762 *acq_in_progress
= 1;
763 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
764 x
->km
.state
== XFRM_STATE_EXPIRED
) {
765 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
766 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
772 xfrm_state_find(const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
773 const struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
774 struct xfrm_policy
*pol
, int *err
,
775 unsigned short family
)
777 static xfrm_address_t saddr_wildcard
= { };
778 struct net
*net
= xp_net(pol
);
779 unsigned int h
, h_wildcard
;
780 struct xfrm_state
*x
, *x0
, *to_put
;
781 int acquire_in_progress
= 0;
783 struct xfrm_state
*best
= NULL
;
784 u32 mark
= pol
->mark
.v
& pol
->mark
.m
;
785 unsigned short encap_family
= tmpl
->encap_family
;
790 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
791 h
= xfrm_dst_hash(net
, daddr
, saddr
, tmpl
->reqid
, encap_family
);
792 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
793 if (x
->props
.family
== encap_family
&&
794 x
->props
.reqid
== tmpl
->reqid
&&
795 (mark
& x
->mark
.m
) == x
->mark
.v
&&
796 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
797 xfrm_state_addr_check(x
, daddr
, saddr
, encap_family
) &&
798 tmpl
->mode
== x
->props
.mode
&&
799 tmpl
->id
.proto
== x
->id
.proto
&&
800 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
801 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
802 &best
, &acquire_in_progress
, &error
);
804 if (best
|| acquire_in_progress
)
807 h_wildcard
= xfrm_dst_hash(net
, daddr
, &saddr_wildcard
, tmpl
->reqid
, encap_family
);
808 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h_wildcard
, bydst
) {
809 if (x
->props
.family
== encap_family
&&
810 x
->props
.reqid
== tmpl
->reqid
&&
811 (mark
& x
->mark
.m
) == x
->mark
.v
&&
812 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
813 xfrm_addr_equal(&x
->id
.daddr
, daddr
, encap_family
) &&
814 tmpl
->mode
== x
->props
.mode
&&
815 tmpl
->id
.proto
== x
->id
.proto
&&
816 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
817 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
818 &best
, &acquire_in_progress
, &error
);
823 if (!x
&& !error
&& !acquire_in_progress
) {
825 (x0
= __xfrm_state_lookup(net
, mark
, daddr
, tmpl
->id
.spi
,
826 tmpl
->id
.proto
, encap_family
)) != NULL
) {
833 /* If the KMs have no listeners (yet...), avoid allocating an SA
834 * for each and every packet - garbage collection might not
837 if (!km_is_alive(&c
)) {
842 x
= xfrm_state_alloc(net
);
847 /* Initialize temporary state matching only
848 * to current session. */
849 xfrm_init_tempstate(x
, fl
, tmpl
, daddr
, saddr
, family
);
850 memcpy(&x
->mark
, &pol
->mark
, sizeof(x
->mark
));
852 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->flowi_secid
);
854 x
->km
.state
= XFRM_STATE_DEAD
;
860 if (km_query(x
, tmpl
, pol
) == 0) {
861 x
->km
.state
= XFRM_STATE_ACQ
;
862 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
863 hlist_add_head(&x
->bydst
, net
->xfrm
.state_bydst
+h
);
864 h
= xfrm_src_hash(net
, daddr
, saddr
, encap_family
);
865 hlist_add_head(&x
->bysrc
, net
->xfrm
.state_bysrc
+h
);
867 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, encap_family
);
868 hlist_add_head(&x
->byspi
, net
->xfrm
.state_byspi
+h
);
870 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
871 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(net
->xfrm
.sysctl_acq_expires
, 0), HRTIMER_MODE_REL
);
872 net
->xfrm
.state_num
++;
873 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
875 x
->km
.state
= XFRM_STATE_DEAD
;
885 *err
= acquire_in_progress
? -EAGAIN
: error
;
886 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
888 xfrm_state_put(to_put
);
893 xfrm_stateonly_find(struct net
*net
, u32 mark
,
894 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
895 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
898 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
900 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
901 h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
902 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
903 if (x
->props
.family
== family
&&
904 x
->props
.reqid
== reqid
&&
905 (mark
& x
->mark
.m
) == x
->mark
.v
&&
906 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
907 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
908 mode
== x
->props
.mode
&&
909 proto
== x
->id
.proto
&&
910 x
->km
.state
== XFRM_STATE_VALID
) {
918 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
923 EXPORT_SYMBOL(xfrm_stateonly_find
);
925 struct xfrm_state
*xfrm_state_lookup_byspi(struct net
*net
, __be32 spi
,
926 unsigned short family
)
928 struct xfrm_state
*x
;
929 struct xfrm_state_walk
*w
;
931 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
932 list_for_each_entry(w
, &net
->xfrm
.state_all
, all
) {
933 x
= container_of(w
, struct xfrm_state
, km
);
934 if (x
->props
.family
!= family
||
939 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
942 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
945 EXPORT_SYMBOL(xfrm_state_lookup_byspi
);
947 static void __xfrm_state_insert(struct xfrm_state
*x
)
949 struct net
*net
= xs_net(x
);
952 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
954 h
= xfrm_dst_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
,
955 x
->props
.reqid
, x
->props
.family
);
956 hlist_add_head(&x
->bydst
, net
->xfrm
.state_bydst
+h
);
958 h
= xfrm_src_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
959 hlist_add_head(&x
->bysrc
, net
->xfrm
.state_bysrc
+h
);
962 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
965 hlist_add_head(&x
->byspi
, net
->xfrm
.state_byspi
+h
);
968 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL
);
969 if (x
->replay_maxage
)
970 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
972 net
->xfrm
.state_num
++;
974 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
977 /* net->xfrm.xfrm_state_lock is held */
978 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
980 struct net
*net
= xs_net(xnew
);
981 unsigned short family
= xnew
->props
.family
;
982 u32 reqid
= xnew
->props
.reqid
;
983 struct xfrm_state
*x
;
985 u32 mark
= xnew
->mark
.v
& xnew
->mark
.m
;
987 h
= xfrm_dst_hash(net
, &xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
988 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
989 if (x
->props
.family
== family
&&
990 x
->props
.reqid
== reqid
&&
991 (mark
& x
->mark
.m
) == x
->mark
.v
&&
992 xfrm_addr_equal(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
993 xfrm_addr_equal(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
998 void xfrm_state_insert(struct xfrm_state
*x
)
1000 struct net
*net
= xs_net(x
);
1002 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1003 __xfrm_state_bump_genids(x
);
1004 __xfrm_state_insert(x
);
1005 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1007 EXPORT_SYMBOL(xfrm_state_insert
);
1009 /* net->xfrm.xfrm_state_lock is held */
1010 static struct xfrm_state
*__find_acq_core(struct net
*net
,
1011 const struct xfrm_mark
*m
,
1012 unsigned short family
, u8 mode
,
1013 u32 reqid
, u8 proto
,
1014 const xfrm_address_t
*daddr
,
1015 const xfrm_address_t
*saddr
,
1018 unsigned int h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1019 struct xfrm_state
*x
;
1020 u32 mark
= m
->v
& m
->m
;
1022 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1023 if (x
->props
.reqid
!= reqid
||
1024 x
->props
.mode
!= mode
||
1025 x
->props
.family
!= family
||
1026 x
->km
.state
!= XFRM_STATE_ACQ
||
1028 x
->id
.proto
!= proto
||
1029 (mark
& x
->mark
.m
) != x
->mark
.v
||
1030 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
1031 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
1041 x
= xfrm_state_alloc(net
);
1045 x
->sel
.daddr
.a4
= daddr
->a4
;
1046 x
->sel
.saddr
.a4
= saddr
->a4
;
1047 x
->sel
.prefixlen_d
= 32;
1048 x
->sel
.prefixlen_s
= 32;
1049 x
->props
.saddr
.a4
= saddr
->a4
;
1050 x
->id
.daddr
.a4
= daddr
->a4
;
1054 *(struct in6_addr
*)x
->sel
.daddr
.a6
= *(struct in6_addr
*)daddr
;
1055 *(struct in6_addr
*)x
->sel
.saddr
.a6
= *(struct in6_addr
*)saddr
;
1056 x
->sel
.prefixlen_d
= 128;
1057 x
->sel
.prefixlen_s
= 128;
1058 *(struct in6_addr
*)x
->props
.saddr
.a6
= *(struct in6_addr
*)saddr
;
1059 *(struct in6_addr
*)x
->id
.daddr
.a6
= *(struct in6_addr
*)daddr
;
1063 x
->km
.state
= XFRM_STATE_ACQ
;
1064 x
->id
.proto
= proto
;
1065 x
->props
.family
= family
;
1066 x
->props
.mode
= mode
;
1067 x
->props
.reqid
= reqid
;
1070 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1072 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(net
->xfrm
.sysctl_acq_expires
, 0), HRTIMER_MODE_REL
);
1073 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1074 hlist_add_head(&x
->bydst
, net
->xfrm
.state_bydst
+h
);
1075 h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
1076 hlist_add_head(&x
->bysrc
, net
->xfrm
.state_bysrc
+h
);
1078 net
->xfrm
.state_num
++;
1080 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1086 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
);
1088 int xfrm_state_add(struct xfrm_state
*x
)
1090 struct net
*net
= xs_net(x
);
1091 struct xfrm_state
*x1
, *to_put
;
1094 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1095 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1097 family
= x
->props
.family
;
1101 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1103 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1111 if (use_spi
&& x
->km
.seq
) {
1112 x1
= __xfrm_find_acq_byseq(net
, mark
, x
->km
.seq
);
1113 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1114 !xfrm_addr_equal(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1121 x1
= __find_acq_core(net
, &x
->mark
, family
, x
->props
.mode
,
1122 x
->props
.reqid
, x
->id
.proto
,
1123 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1125 __xfrm_state_bump_genids(x
);
1126 __xfrm_state_insert(x
);
1130 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1133 xfrm_state_delete(x1
);
1138 xfrm_state_put(to_put
);
1142 EXPORT_SYMBOL(xfrm_state_add
);
1144 #ifdef CONFIG_XFRM_MIGRATE
1145 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
)
1147 struct net
*net
= xs_net(orig
);
1148 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1152 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1153 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1154 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1155 x
->props
.mode
= orig
->props
.mode
;
1156 x
->props
.replay_window
= orig
->props
.replay_window
;
1157 x
->props
.reqid
= orig
->props
.reqid
;
1158 x
->props
.family
= orig
->props
.family
;
1159 x
->props
.saddr
= orig
->props
.saddr
;
1162 x
->aalg
= xfrm_algo_auth_clone(orig
->aalg
);
1166 x
->props
.aalgo
= orig
->props
.aalgo
;
1169 x
->aead
= xfrm_algo_aead_clone(orig
->aead
);
1174 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1178 x
->props
.ealgo
= orig
->props
.ealgo
;
1181 x
->calg
= xfrm_algo_clone(orig
->calg
);
1185 x
->props
.calgo
= orig
->props
.calgo
;
1188 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
), GFP_KERNEL
);
1194 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1200 if (orig
->replay_esn
) {
1201 if (xfrm_replay_clone(x
, orig
))
1205 memcpy(&x
->mark
, &orig
->mark
, sizeof(x
->mark
));
1207 if (xfrm_init_state(x
) < 0)
1210 x
->props
.flags
= orig
->props
.flags
;
1211 x
->props
.extra_flags
= orig
->props
.extra_flags
;
1213 x
->tfcpad
= orig
->tfcpad
;
1214 x
->replay_maxdiff
= orig
->replay_maxdiff
;
1215 x
->replay_maxage
= orig
->replay_maxage
;
1216 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1217 x
->km
.state
= orig
->km
.state
;
1218 x
->km
.seq
= orig
->km
.seq
;
1228 struct xfrm_state
*xfrm_migrate_state_find(struct xfrm_migrate
*m
, struct net
*net
)
1231 struct xfrm_state
*x
= NULL
;
1233 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1236 h
= xfrm_dst_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1237 m
->reqid
, m
->old_family
);
1238 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1239 if (x
->props
.mode
!= m
->mode
||
1240 x
->id
.proto
!= m
->proto
)
1242 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1244 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1246 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1253 h
= xfrm_src_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1255 hlist_for_each_entry(x
, net
->xfrm
.state_bysrc
+h
, bysrc
) {
1256 if (x
->props
.mode
!= m
->mode
||
1257 x
->id
.proto
!= m
->proto
)
1259 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1261 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1269 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1273 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1275 struct xfrm_state
*xfrm_state_migrate(struct xfrm_state
*x
,
1276 struct xfrm_migrate
*m
)
1278 struct xfrm_state
*xc
;
1280 xc
= xfrm_state_clone(x
);
1284 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1285 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1288 if (xfrm_addr_equal(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1289 /* a care is needed when the destination address of the
1290 state is to be updated as it is a part of triplet */
1291 xfrm_state_insert(xc
);
1293 if (xfrm_state_add(xc
) < 0)
1302 EXPORT_SYMBOL(xfrm_state_migrate
);
1305 int xfrm_state_update(struct xfrm_state
*x
)
1307 struct xfrm_state
*x1
, *to_put
;
1309 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1310 struct net
*net
= xs_net(x
);
1314 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1315 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1321 if (xfrm_state_kern(x1
)) {
1327 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1328 __xfrm_state_insert(x
);
1334 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1337 xfrm_state_put(to_put
);
1343 xfrm_state_delete(x1
);
1349 spin_lock_bh(&x1
->lock
);
1350 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1351 if (x
->encap
&& x1
->encap
)
1352 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1353 if (x
->coaddr
&& x1
->coaddr
) {
1354 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1356 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1357 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1358 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1361 tasklet_hrtimer_start(&x1
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL
);
1362 if (x1
->curlft
.use_time
)
1363 xfrm_state_check_expire(x1
);
1366 x
->km
.state
= XFRM_STATE_DEAD
;
1367 __xfrm_state_put(x
);
1369 spin_unlock_bh(&x1
->lock
);
1375 EXPORT_SYMBOL(xfrm_state_update
);
1377 int xfrm_state_check_expire(struct xfrm_state
*x
)
1379 if (!x
->curlft
.use_time
)
1380 x
->curlft
.use_time
= get_seconds();
1382 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1383 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1384 x
->km
.state
= XFRM_STATE_EXPIRED
;
1385 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(0, 0), HRTIMER_MODE_REL
);
1390 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1391 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1393 km_state_expired(x
, 0, 0);
1397 EXPORT_SYMBOL(xfrm_state_check_expire
);
1400 xfrm_state_lookup(struct net
*net
, u32 mark
, const xfrm_address_t
*daddr
, __be32 spi
,
1401 u8 proto
, unsigned short family
)
1403 struct xfrm_state
*x
;
1405 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1406 x
= __xfrm_state_lookup(net
, mark
, daddr
, spi
, proto
, family
);
1407 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1410 EXPORT_SYMBOL(xfrm_state_lookup
);
1413 xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
1414 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
1415 u8 proto
, unsigned short family
)
1417 struct xfrm_state
*x
;
1419 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1420 x
= __xfrm_state_lookup_byaddr(net
, mark
, daddr
, saddr
, proto
, family
);
1421 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1424 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1427 xfrm_find_acq(struct net
*net
, const struct xfrm_mark
*mark
, u8 mode
, u32 reqid
,
1428 u8 proto
, const xfrm_address_t
*daddr
,
1429 const xfrm_address_t
*saddr
, int create
, unsigned short family
)
1431 struct xfrm_state
*x
;
1433 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1434 x
= __find_acq_core(net
, mark
, family
, mode
, reqid
, proto
, daddr
, saddr
, create
);
1435 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1439 EXPORT_SYMBOL(xfrm_find_acq
);
1441 #ifdef CONFIG_XFRM_SUB_POLICY
1443 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1444 unsigned short family
, struct net
*net
)
1447 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1449 return -EAFNOSUPPORT
;
1451 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
); /*FIXME*/
1452 if (afinfo
->tmpl_sort
)
1453 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1454 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1455 xfrm_state_put_afinfo(afinfo
);
1458 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1461 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1462 unsigned short family
)
1465 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1466 struct net
*net
= xs_net(*src
);
1469 return -EAFNOSUPPORT
;
1471 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1472 if (afinfo
->state_sort
)
1473 err
= afinfo
->state_sort(dst
, src
, n
);
1474 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1475 xfrm_state_put_afinfo(afinfo
);
1478 EXPORT_SYMBOL(xfrm_state_sort
);
1481 /* Silly enough, but I'm lazy to build resolution list */
1483 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1487 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
1488 struct xfrm_state
*x
;
1490 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
1491 if (x
->km
.seq
== seq
&&
1492 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1493 x
->km
.state
== XFRM_STATE_ACQ
) {
1502 struct xfrm_state
*xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1504 struct xfrm_state
*x
;
1506 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1507 x
= __xfrm_find_acq_byseq(net
, mark
, seq
);
1508 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1511 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1513 u32
xfrm_get_acqseq(void)
1516 static atomic_t acqseq
;
1519 res
= atomic_inc_return(&acqseq
);
1524 EXPORT_SYMBOL(xfrm_get_acqseq
);
1526 int verify_spi_info(u8 proto
, u32 min
, u32 max
)
1534 /* IPCOMP spi is 16-bits. */
1548 EXPORT_SYMBOL(verify_spi_info
);
1550 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1552 struct net
*net
= xs_net(x
);
1554 struct xfrm_state
*x0
;
1556 __be32 minspi
= htonl(low
);
1557 __be32 maxspi
= htonl(high
);
1558 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1560 spin_lock_bh(&x
->lock
);
1561 if (x
->km
.state
== XFRM_STATE_DEAD
)
1570 if (minspi
== maxspi
) {
1571 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1579 for (h
= 0; h
< high
-low
+1; h
++) {
1580 spi
= low
+ prandom_u32()%(high
-low
+1);
1581 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1583 x
->id
.spi
= htonl(spi
);
1590 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1591 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1592 hlist_add_head(&x
->byspi
, net
->xfrm
.state_byspi
+h
);
1593 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1599 spin_unlock_bh(&x
->lock
);
1603 EXPORT_SYMBOL(xfrm_alloc_spi
);
1605 static bool __xfrm_state_filter_match(struct xfrm_state
*x
,
1606 struct xfrm_address_filter
*filter
)
1609 if ((filter
->family
== AF_INET
||
1610 filter
->family
== AF_INET6
) &&
1611 x
->props
.family
!= filter
->family
)
1614 return addr_match(&x
->props
.saddr
, &filter
->saddr
,
1616 addr_match(&x
->id
.daddr
, &filter
->daddr
,
1622 int xfrm_state_walk(struct net
*net
, struct xfrm_state_walk
*walk
,
1623 int (*func
)(struct xfrm_state
*, int, void*),
1626 struct xfrm_state
*state
;
1627 struct xfrm_state_walk
*x
;
1630 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
1633 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1634 if (list_empty(&walk
->all
))
1635 x
= list_first_entry(&net
->xfrm
.state_all
, struct xfrm_state_walk
, all
);
1637 x
= list_entry(&walk
->all
, struct xfrm_state_walk
, all
);
1638 list_for_each_entry_from(x
, &net
->xfrm
.state_all
, all
) {
1639 if (x
->state
== XFRM_STATE_DEAD
)
1641 state
= container_of(x
, struct xfrm_state
, km
);
1642 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
1644 if (!__xfrm_state_filter_match(state
, walk
->filter
))
1646 err
= func(state
, walk
->seq
, data
);
1648 list_move_tail(&walk
->all
, &x
->all
);
1653 if (walk
->seq
== 0) {
1657 list_del_init(&walk
->all
);
1659 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1662 EXPORT_SYMBOL(xfrm_state_walk
);
1664 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
,
1665 struct xfrm_address_filter
*filter
)
1667 INIT_LIST_HEAD(&walk
->all
);
1668 walk
->proto
= proto
;
1669 walk
->state
= XFRM_STATE_DEAD
;
1671 walk
->filter
= filter
;
1673 EXPORT_SYMBOL(xfrm_state_walk_init
);
1675 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
, struct net
*net
)
1677 kfree(walk
->filter
);
1679 if (list_empty(&walk
->all
))
1682 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1683 list_del(&walk
->all
);
1684 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1686 EXPORT_SYMBOL(xfrm_state_walk_done
);
1688 static void xfrm_replay_timer_handler(unsigned long data
)
1690 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
1692 spin_lock(&x
->lock
);
1694 if (x
->km
.state
== XFRM_STATE_VALID
) {
1695 if (xfrm_aevent_is_on(xs_net(x
)))
1696 x
->repl
->notify(x
, XFRM_REPLAY_TIMEOUT
);
1698 x
->xflags
|= XFRM_TIME_DEFER
;
1701 spin_unlock(&x
->lock
);
1704 static LIST_HEAD(xfrm_km_list
);
1706 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
1708 struct xfrm_mgr
*km
;
1711 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1712 if (km
->notify_policy
)
1713 km
->notify_policy(xp
, dir
, c
);
1717 void km_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
1719 struct xfrm_mgr
*km
;
1721 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1727 EXPORT_SYMBOL(km_policy_notify
);
1728 EXPORT_SYMBOL(km_state_notify
);
1730 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
)
1736 c
.event
= XFRM_MSG_EXPIRE
;
1737 km_state_notify(x
, &c
);
1740 EXPORT_SYMBOL(km_state_expired
);
1742 * We send to all registered managers regardless of failure
1743 * We are happy with one success
1745 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1747 int err
= -EINVAL
, acqret
;
1748 struct xfrm_mgr
*km
;
1751 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1752 acqret
= km
->acquire(x
, t
, pol
);
1759 EXPORT_SYMBOL(km_query
);
1761 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1764 struct xfrm_mgr
*km
;
1767 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1768 if (km
->new_mapping
)
1769 err
= km
->new_mapping(x
, ipaddr
, sport
);
1776 EXPORT_SYMBOL(km_new_mapping
);
1778 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 portid
)
1784 c
.event
= XFRM_MSG_POLEXPIRE
;
1785 km_policy_notify(pol
, dir
, &c
);
1787 EXPORT_SYMBOL(km_policy_expired
);
1789 #ifdef CONFIG_XFRM_MIGRATE
1790 int km_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1791 const struct xfrm_migrate
*m
, int num_migrate
,
1792 const struct xfrm_kmaddress
*k
)
1796 struct xfrm_mgr
*km
;
1799 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1801 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
);
1809 EXPORT_SYMBOL(km_migrate
);
1812 int km_report(struct net
*net
, u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
1816 struct xfrm_mgr
*km
;
1819 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1821 ret
= km
->report(net
, proto
, sel
, addr
);
1829 EXPORT_SYMBOL(km_report
);
1831 bool km_is_alive(const struct km_event
*c
)
1833 struct xfrm_mgr
*km
;
1834 bool is_alive
= false;
1837 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1838 if (km
->is_alive
&& km
->is_alive(c
)) {
1847 EXPORT_SYMBOL(km_is_alive
);
1849 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
1853 struct xfrm_mgr
*km
;
1854 struct xfrm_policy
*pol
= NULL
;
1856 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
1859 data
= kmalloc(optlen
, GFP_KERNEL
);
1864 if (copy_from_user(data
, optval
, optlen
))
1869 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1870 pol
= km
->compile_policy(sk
, optname
, data
,
1878 xfrm_sk_policy_insert(sk
, err
, pol
);
1888 EXPORT_SYMBOL(xfrm_user_policy
);
1890 static DEFINE_SPINLOCK(xfrm_km_lock
);
1892 int xfrm_register_km(struct xfrm_mgr
*km
)
1894 spin_lock_bh(&xfrm_km_lock
);
1895 list_add_tail_rcu(&km
->list
, &xfrm_km_list
);
1896 spin_unlock_bh(&xfrm_km_lock
);
1899 EXPORT_SYMBOL(xfrm_register_km
);
1901 int xfrm_unregister_km(struct xfrm_mgr
*km
)
1903 spin_lock_bh(&xfrm_km_lock
);
1904 list_del_rcu(&km
->list
);
1905 spin_unlock_bh(&xfrm_km_lock
);
1909 EXPORT_SYMBOL(xfrm_unregister_km
);
1911 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
1914 if (unlikely(afinfo
== NULL
))
1916 if (unlikely(afinfo
->family
>= NPROTO
))
1917 return -EAFNOSUPPORT
;
1918 spin_lock_bh(&xfrm_state_afinfo_lock
);
1919 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
1922 rcu_assign_pointer(xfrm_state_afinfo
[afinfo
->family
], afinfo
);
1923 spin_unlock_bh(&xfrm_state_afinfo_lock
);
1926 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
1928 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
1931 if (unlikely(afinfo
== NULL
))
1933 if (unlikely(afinfo
->family
>= NPROTO
))
1934 return -EAFNOSUPPORT
;
1935 spin_lock_bh(&xfrm_state_afinfo_lock
);
1936 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
1937 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != afinfo
))
1940 RCU_INIT_POINTER(xfrm_state_afinfo
[afinfo
->family
], NULL
);
1942 spin_unlock_bh(&xfrm_state_afinfo_lock
);
1946 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
1948 struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
1950 struct xfrm_state_afinfo
*afinfo
;
1951 if (unlikely(family
>= NPROTO
))
1954 afinfo
= rcu_dereference(xfrm_state_afinfo
[family
]);
1955 if (unlikely(!afinfo
))
1960 void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
)
1965 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1966 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
1969 struct xfrm_state
*t
= x
->tunnel
;
1971 if (atomic_read(&t
->tunnel_users
) == 2)
1972 xfrm_state_delete(t
);
1973 atomic_dec(&t
->tunnel_users
);
1978 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
1980 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
1984 spin_lock_bh(&x
->lock
);
1985 if (x
->km
.state
== XFRM_STATE_VALID
&&
1986 x
->type
&& x
->type
->get_mtu
)
1987 res
= x
->type
->get_mtu(x
, mtu
);
1989 res
= mtu
- x
->props
.header_len
;
1990 spin_unlock_bh(&x
->lock
);
1994 int __xfrm_init_state(struct xfrm_state
*x
, bool init_replay
)
1996 struct xfrm_state_afinfo
*afinfo
;
1997 struct xfrm_mode
*inner_mode
;
1998 int family
= x
->props
.family
;
2001 err
= -EAFNOSUPPORT
;
2002 afinfo
= xfrm_state_get_afinfo(family
);
2007 if (afinfo
->init_flags
)
2008 err
= afinfo
->init_flags(x
);
2010 xfrm_state_put_afinfo(afinfo
);
2015 err
= -EPROTONOSUPPORT
;
2017 if (x
->sel
.family
!= AF_UNSPEC
) {
2018 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
2019 if (inner_mode
== NULL
)
2022 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2023 family
!= x
->sel
.family
) {
2024 xfrm_put_mode(inner_mode
);
2028 x
->inner_mode
= inner_mode
;
2030 struct xfrm_mode
*inner_mode_iaf
;
2031 int iafamily
= AF_INET
;
2033 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->props
.family
);
2034 if (inner_mode
== NULL
)
2037 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2038 xfrm_put_mode(inner_mode
);
2041 x
->inner_mode
= inner_mode
;
2043 if (x
->props
.family
== AF_INET
)
2044 iafamily
= AF_INET6
;
2046 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, iafamily
);
2047 if (inner_mode_iaf
) {
2048 if (inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)
2049 x
->inner_mode_iaf
= inner_mode_iaf
;
2051 xfrm_put_mode(inner_mode_iaf
);
2055 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2056 if (x
->type
== NULL
)
2059 err
= x
->type
->init_state(x
);
2063 x
->outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2064 if (x
->outer_mode
== NULL
) {
2065 err
= -EPROTONOSUPPORT
;
2070 err
= xfrm_init_replay(x
);
2075 x
->km
.state
= XFRM_STATE_VALID
;
2081 EXPORT_SYMBOL(__xfrm_init_state
);
2083 int xfrm_init_state(struct xfrm_state
*x
)
2085 return __xfrm_init_state(x
, true);
2088 EXPORT_SYMBOL(xfrm_init_state
);
2090 int __net_init
xfrm_state_init(struct net
*net
)
2094 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2096 sz
= sizeof(struct hlist_head
) * 8;
2098 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2099 if (!net
->xfrm
.state_bydst
)
2101 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2102 if (!net
->xfrm
.state_bysrc
)
2104 net
->xfrm
.state_byspi
= xfrm_hash_alloc(sz
);
2105 if (!net
->xfrm
.state_byspi
)
2107 net
->xfrm
.state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2109 net
->xfrm
.state_num
= 0;
2110 INIT_WORK(&net
->xfrm
.state_hash_work
, xfrm_hash_resize
);
2111 INIT_HLIST_HEAD(&net
->xfrm
.state_gc_list
);
2112 INIT_WORK(&net
->xfrm
.state_gc_work
, xfrm_state_gc_task
);
2113 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
2117 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2119 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2124 void xfrm_state_fini(struct net
*net
)
2128 flush_work(&net
->xfrm
.state_hash_work
);
2129 xfrm_state_flush(net
, IPSEC_PROTO_ANY
, false);
2130 flush_work(&net
->xfrm
.state_gc_work
);
2132 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2134 sz
= (net
->xfrm
.state_hmask
+ 1) * sizeof(struct hlist_head
);
2135 WARN_ON(!hlist_empty(net
->xfrm
.state_byspi
));
2136 xfrm_hash_free(net
->xfrm
.state_byspi
, sz
);
2137 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2138 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2139 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2140 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2143 #ifdef CONFIG_AUDITSYSCALL
2144 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2145 struct audit_buffer
*audit_buf
)
2147 struct xfrm_sec_ctx
*ctx
= x
->security
;
2148 u32 spi
= ntohl(x
->id
.spi
);
2151 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2152 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2154 switch (x
->props
.family
) {
2156 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2157 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2160 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2161 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2165 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2168 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2169 struct audit_buffer
*audit_buf
)
2171 const struct iphdr
*iph4
;
2172 const struct ipv6hdr
*iph6
;
2177 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2178 &iph4
->saddr
, &iph4
->daddr
);
2181 iph6
= ipv6_hdr(skb
);
2182 audit_log_format(audit_buf
,
2183 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2184 &iph6
->saddr
, &iph6
->daddr
,
2185 iph6
->flow_lbl
[0] & 0x0f,
2192 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
, bool task_valid
)
2194 struct audit_buffer
*audit_buf
;
2196 audit_buf
= xfrm_audit_start("SAD-add");
2197 if (audit_buf
== NULL
)
2199 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2200 xfrm_audit_helper_sainfo(x
, audit_buf
);
2201 audit_log_format(audit_buf
, " res=%u", result
);
2202 audit_log_end(audit_buf
);
2204 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2206 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
, bool task_valid
)
2208 struct audit_buffer
*audit_buf
;
2210 audit_buf
= xfrm_audit_start("SAD-delete");
2211 if (audit_buf
== NULL
)
2213 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2214 xfrm_audit_helper_sainfo(x
, audit_buf
);
2215 audit_log_format(audit_buf
, " res=%u", result
);
2216 audit_log_end(audit_buf
);
2218 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2220 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2221 struct sk_buff
*skb
)
2223 struct audit_buffer
*audit_buf
;
2226 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2227 if (audit_buf
== NULL
)
2229 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2230 /* don't record the sequence number because it's inherent in this kind
2231 * of audit message */
2232 spi
= ntohl(x
->id
.spi
);
2233 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2234 audit_log_end(audit_buf
);
2236 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2238 void xfrm_audit_state_replay(struct xfrm_state
*x
,
2239 struct sk_buff
*skb
, __be32 net_seq
)
2241 struct audit_buffer
*audit_buf
;
2244 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2245 if (audit_buf
== NULL
)
2247 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2248 spi
= ntohl(x
->id
.spi
);
2249 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2250 spi
, spi
, ntohl(net_seq
));
2251 audit_log_end(audit_buf
);
2253 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay
);
2255 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2257 struct audit_buffer
*audit_buf
;
2259 audit_buf
= xfrm_audit_start("SA-notfound");
2260 if (audit_buf
== NULL
)
2262 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2263 audit_log_end(audit_buf
);
2265 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2267 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2268 __be32 net_spi
, __be32 net_seq
)
2270 struct audit_buffer
*audit_buf
;
2273 audit_buf
= xfrm_audit_start("SA-notfound");
2274 if (audit_buf
== NULL
)
2276 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2277 spi
= ntohl(net_spi
);
2278 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2279 spi
, spi
, ntohl(net_seq
));
2280 audit_log_end(audit_buf
);
2282 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2284 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2285 struct sk_buff
*skb
, u8 proto
)
2287 struct audit_buffer
*audit_buf
;
2291 audit_buf
= xfrm_audit_start("SA-icv-failure");
2292 if (audit_buf
== NULL
)
2294 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2295 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2296 u32 spi
= ntohl(net_spi
);
2297 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2298 spi
, spi
, ntohl(net_seq
));
2300 audit_log_end(audit_buf
);
2302 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2303 #endif /* CONFIG_AUDITSYSCALL */