1 // SPDX-License-Identifier: GPL-2.0-only
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific functions
12 * Derek Atkins <derek@ihtfp.com>
13 * Add UDP Encapsulation
17 #include <linux/workqueue.h>
19 #include <linux/pfkeyv2.h>
20 #include <linux/ipsec.h>
21 #include <linux/module.h>
22 #include <linux/cache.h>
23 #include <linux/audit.h>
24 #include <linux/uaccess.h>
25 #include <linux/ktime.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
30 #include "xfrm_hash.h"
32 #define xfrm_state_deref_prot(table, net) \
33 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
35 static void xfrm_state_gc_task(struct work_struct
*work
);
37 /* Each xfrm_state may be linked to two tables:
39 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
40 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
41 destination/tunnel endpoint. (output)
44 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
45 static __read_mostly seqcount_t xfrm_state_hash_generation
= SEQCNT_ZERO(xfrm_state_hash_generation
);
46 static struct kmem_cache
*xfrm_state_cache __ro_after_init
;
48 static DECLARE_WORK(xfrm_state_gc_work
, xfrm_state_gc_task
);
49 static HLIST_HEAD(xfrm_state_gc_list
);
51 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu
*x
)
53 return refcount_inc_not_zero(&x
->refcnt
);
56 static inline unsigned int xfrm_dst_hash(struct net
*net
,
57 const xfrm_address_t
*daddr
,
58 const xfrm_address_t
*saddr
,
60 unsigned short family
)
62 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, net
->xfrm
.state_hmask
);
65 static inline unsigned int xfrm_src_hash(struct net
*net
,
66 const xfrm_address_t
*daddr
,
67 const xfrm_address_t
*saddr
,
68 unsigned short family
)
70 return __xfrm_src_hash(daddr
, saddr
, family
, net
->xfrm
.state_hmask
);
73 static inline unsigned int
74 xfrm_spi_hash(struct net
*net
, const xfrm_address_t
*daddr
,
75 __be32 spi
, u8 proto
, unsigned short family
)
77 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, net
->xfrm
.state_hmask
);
80 static void xfrm_hash_transfer(struct hlist_head
*list
,
81 struct hlist_head
*ndsttable
,
82 struct hlist_head
*nsrctable
,
83 struct hlist_head
*nspitable
,
84 unsigned int nhashmask
)
86 struct hlist_node
*tmp
;
89 hlist_for_each_entry_safe(x
, tmp
, list
, bydst
) {
92 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
93 x
->props
.reqid
, x
->props
.family
,
95 hlist_add_head_rcu(&x
->bydst
, ndsttable
+ h
);
97 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
100 hlist_add_head_rcu(&x
->bysrc
, nsrctable
+ h
);
103 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
104 x
->id
.proto
, x
->props
.family
,
106 hlist_add_head_rcu(&x
->byspi
, nspitable
+ h
);
111 static unsigned long xfrm_hash_new_size(unsigned int state_hmask
)
113 return ((state_hmask
+ 1) << 1) * sizeof(struct hlist_head
);
116 static void xfrm_hash_resize(struct work_struct
*work
)
118 struct net
*net
= container_of(work
, struct net
, xfrm
.state_hash_work
);
119 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
120 unsigned long nsize
, osize
;
121 unsigned int nhashmask
, ohashmask
;
124 nsize
= xfrm_hash_new_size(net
->xfrm
.state_hmask
);
125 ndst
= xfrm_hash_alloc(nsize
);
128 nsrc
= xfrm_hash_alloc(nsize
);
130 xfrm_hash_free(ndst
, nsize
);
133 nspi
= xfrm_hash_alloc(nsize
);
135 xfrm_hash_free(ndst
, nsize
);
136 xfrm_hash_free(nsrc
, nsize
);
140 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
141 write_seqcount_begin(&xfrm_state_hash_generation
);
143 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
144 odst
= xfrm_state_deref_prot(net
->xfrm
.state_bydst
, net
);
145 for (i
= net
->xfrm
.state_hmask
; i
>= 0; i
--)
146 xfrm_hash_transfer(odst
+ i
, ndst
, nsrc
, nspi
, nhashmask
);
148 osrc
= xfrm_state_deref_prot(net
->xfrm
.state_bysrc
, net
);
149 ospi
= xfrm_state_deref_prot(net
->xfrm
.state_byspi
, net
);
150 ohashmask
= net
->xfrm
.state_hmask
;
152 rcu_assign_pointer(net
->xfrm
.state_bydst
, ndst
);
153 rcu_assign_pointer(net
->xfrm
.state_bysrc
, nsrc
);
154 rcu_assign_pointer(net
->xfrm
.state_byspi
, nspi
);
155 net
->xfrm
.state_hmask
= nhashmask
;
157 write_seqcount_end(&xfrm_state_hash_generation
);
158 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
160 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
164 xfrm_hash_free(odst
, osize
);
165 xfrm_hash_free(osrc
, osize
);
166 xfrm_hash_free(ospi
, osize
);
169 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock
);
170 static struct xfrm_state_afinfo __rcu
*xfrm_state_afinfo
[NPROTO
];
172 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
174 int __xfrm_state_delete(struct xfrm_state
*x
);
176 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
177 static bool km_is_alive(const struct km_event
*c
);
178 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
);
180 static DEFINE_SPINLOCK(xfrm_type_lock
);
181 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
183 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
184 const struct xfrm_type
**typemap
;
187 if (unlikely(afinfo
== NULL
))
188 return -EAFNOSUPPORT
;
189 typemap
= afinfo
->type_map
;
190 spin_lock_bh(&xfrm_type_lock
);
192 if (likely(typemap
[type
->proto
] == NULL
))
193 typemap
[type
->proto
] = type
;
196 spin_unlock_bh(&xfrm_type_lock
);
200 EXPORT_SYMBOL(xfrm_register_type
);
202 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
204 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
205 const struct xfrm_type
**typemap
;
208 if (unlikely(afinfo
== NULL
))
209 return -EAFNOSUPPORT
;
210 typemap
= afinfo
->type_map
;
211 spin_lock_bh(&xfrm_type_lock
);
213 if (unlikely(typemap
[type
->proto
] != type
))
216 typemap
[type
->proto
] = NULL
;
217 spin_unlock_bh(&xfrm_type_lock
);
221 EXPORT_SYMBOL(xfrm_unregister_type
);
223 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
225 struct xfrm_state_afinfo
*afinfo
;
226 const struct xfrm_type
**typemap
;
227 const struct xfrm_type
*type
;
228 int modload_attempted
= 0;
231 afinfo
= xfrm_state_get_afinfo(family
);
232 if (unlikely(afinfo
== NULL
))
234 typemap
= afinfo
->type_map
;
236 type
= READ_ONCE(typemap
[proto
]);
237 if (unlikely(type
&& !try_module_get(type
->owner
)))
242 if (!type
&& !modload_attempted
) {
243 request_module("xfrm-type-%d-%d", family
, proto
);
244 modload_attempted
= 1;
251 static void xfrm_put_type(const struct xfrm_type
*type
)
253 module_put(type
->owner
);
256 static DEFINE_SPINLOCK(xfrm_type_offload_lock
);
257 int xfrm_register_type_offload(const struct xfrm_type_offload
*type
,
258 unsigned short family
)
260 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
261 const struct xfrm_type_offload
**typemap
;
264 if (unlikely(afinfo
== NULL
))
265 return -EAFNOSUPPORT
;
266 typemap
= afinfo
->type_offload_map
;
267 spin_lock_bh(&xfrm_type_offload_lock
);
269 if (likely(typemap
[type
->proto
] == NULL
))
270 typemap
[type
->proto
] = type
;
273 spin_unlock_bh(&xfrm_type_offload_lock
);
277 EXPORT_SYMBOL(xfrm_register_type_offload
);
279 int xfrm_unregister_type_offload(const struct xfrm_type_offload
*type
,
280 unsigned short family
)
282 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
283 const struct xfrm_type_offload
**typemap
;
286 if (unlikely(afinfo
== NULL
))
287 return -EAFNOSUPPORT
;
288 typemap
= afinfo
->type_offload_map
;
289 spin_lock_bh(&xfrm_type_offload_lock
);
291 if (unlikely(typemap
[type
->proto
] != type
))
294 typemap
[type
->proto
] = NULL
;
295 spin_unlock_bh(&xfrm_type_offload_lock
);
299 EXPORT_SYMBOL(xfrm_unregister_type_offload
);
301 static const struct xfrm_type_offload
*
302 xfrm_get_type_offload(u8 proto
, unsigned short family
, bool try_load
)
304 struct xfrm_state_afinfo
*afinfo
;
305 const struct xfrm_type_offload
**typemap
;
306 const struct xfrm_type_offload
*type
;
309 afinfo
= xfrm_state_get_afinfo(family
);
310 if (unlikely(afinfo
== NULL
))
312 typemap
= afinfo
->type_offload_map
;
314 type
= typemap
[proto
];
315 if ((type
&& !try_module_get(type
->owner
)))
320 if (!type
&& try_load
) {
321 request_module("xfrm-offload-%d-%d", family
, proto
);
329 static void xfrm_put_type_offload(const struct xfrm_type_offload
*type
)
331 module_put(type
->owner
);
334 static const struct xfrm_mode xfrm4_mode_map
[XFRM_MODE_MAX
] = {
336 .encap
= XFRM_MODE_BEET
,
337 .flags
= XFRM_MODE_FLAG_TUNNEL
,
340 [XFRM_MODE_TRANSPORT
] = {
341 .encap
= XFRM_MODE_TRANSPORT
,
344 [XFRM_MODE_TUNNEL
] = {
345 .encap
= XFRM_MODE_TUNNEL
,
346 .flags
= XFRM_MODE_FLAG_TUNNEL
,
351 static const struct xfrm_mode xfrm6_mode_map
[XFRM_MODE_MAX
] = {
353 .encap
= XFRM_MODE_BEET
,
354 .flags
= XFRM_MODE_FLAG_TUNNEL
,
357 [XFRM_MODE_ROUTEOPTIMIZATION
] = {
358 .encap
= XFRM_MODE_ROUTEOPTIMIZATION
,
361 [XFRM_MODE_TRANSPORT
] = {
362 .encap
= XFRM_MODE_TRANSPORT
,
365 [XFRM_MODE_TUNNEL
] = {
366 .encap
= XFRM_MODE_TUNNEL
,
367 .flags
= XFRM_MODE_FLAG_TUNNEL
,
372 static const struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
374 const struct xfrm_mode
*mode
;
376 if (unlikely(encap
>= XFRM_MODE_MAX
))
381 mode
= &xfrm4_mode_map
[encap
];
382 if (mode
->family
== family
)
386 mode
= &xfrm6_mode_map
[encap
];
387 if (mode
->family
== family
)
397 void xfrm_state_free(struct xfrm_state
*x
)
399 kmem_cache_free(xfrm_state_cache
, x
);
401 EXPORT_SYMBOL(xfrm_state_free
);
403 static void ___xfrm_state_destroy(struct xfrm_state
*x
)
405 hrtimer_cancel(&x
->mtimer
);
406 del_timer_sync(&x
->rtimer
);
413 kfree(x
->replay_esn
);
414 kfree(x
->preplay_esn
);
416 xfrm_put_type_offload(x
->type_offload
);
418 x
->type
->destructor(x
);
419 xfrm_put_type(x
->type
);
421 xfrm_dev_state_free(x
);
422 security_xfrm_state_free(x
);
426 static void xfrm_state_gc_task(struct work_struct
*work
)
428 struct xfrm_state
*x
;
429 struct hlist_node
*tmp
;
430 struct hlist_head gc_list
;
432 spin_lock_bh(&xfrm_state_gc_lock
);
433 hlist_move_list(&xfrm_state_gc_list
, &gc_list
);
434 spin_unlock_bh(&xfrm_state_gc_lock
);
438 hlist_for_each_entry_safe(x
, tmp
, &gc_list
, gclist
)
439 ___xfrm_state_destroy(x
);
442 static enum hrtimer_restart
xfrm_timer_handler(struct hrtimer
*me
)
444 struct xfrm_state
*x
= container_of(me
, struct xfrm_state
, mtimer
);
445 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
446 time64_t now
= ktime_get_real_seconds();
447 time64_t next
= TIME64_MAX
;
452 if (x
->km
.state
== XFRM_STATE_DEAD
)
454 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
456 if (x
->lft
.hard_add_expires_seconds
) {
457 long tmo
= x
->lft
.hard_add_expires_seconds
+
458 x
->curlft
.add_time
- now
;
460 if (x
->xflags
& XFRM_SOFT_EXPIRE
) {
461 /* enter hard expire without soft expire first?!
462 * setting a new date could trigger this.
463 * workaround: fix x->curflt.add_time by below:
465 x
->curlft
.add_time
= now
- x
->saved_tmo
- 1;
466 tmo
= x
->lft
.hard_add_expires_seconds
- x
->saved_tmo
;
473 if (x
->lft
.hard_use_expires_seconds
) {
474 long tmo
= x
->lft
.hard_use_expires_seconds
+
475 (x
->curlft
.use_time
? : now
) - now
;
483 if (x
->lft
.soft_add_expires_seconds
) {
484 long tmo
= x
->lft
.soft_add_expires_seconds
+
485 x
->curlft
.add_time
- now
;
488 x
->xflags
&= ~XFRM_SOFT_EXPIRE
;
489 } else if (tmo
< next
) {
491 x
->xflags
|= XFRM_SOFT_EXPIRE
;
495 if (x
->lft
.soft_use_expires_seconds
) {
496 long tmo
= x
->lft
.soft_use_expires_seconds
+
497 (x
->curlft
.use_time
? : now
) - now
;
506 km_state_expired(x
, 0, 0);
508 if (next
!= TIME64_MAX
) {
509 hrtimer_forward_now(&x
->mtimer
, ktime_set(next
, 0));
510 ret
= HRTIMER_RESTART
;
516 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0)
517 x
->km
.state
= XFRM_STATE_EXPIRED
;
519 err
= __xfrm_state_delete(x
);
521 km_state_expired(x
, 1, 0);
523 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
526 spin_unlock(&x
->lock
);
530 static void xfrm_replay_timer_handler(struct timer_list
*t
);
532 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
534 struct xfrm_state
*x
;
536 x
= kmem_cache_alloc(xfrm_state_cache
, GFP_ATOMIC
| __GFP_ZERO
);
539 write_pnet(&x
->xs_net
, net
);
540 refcount_set(&x
->refcnt
, 1);
541 atomic_set(&x
->tunnel_users
, 0);
542 INIT_LIST_HEAD(&x
->km
.all
);
543 INIT_HLIST_NODE(&x
->bydst
);
544 INIT_HLIST_NODE(&x
->bysrc
);
545 INIT_HLIST_NODE(&x
->byspi
);
546 hrtimer_init(&x
->mtimer
, CLOCK_BOOTTIME
, HRTIMER_MODE_ABS_SOFT
);
547 x
->mtimer
.function
= xfrm_timer_handler
;
548 timer_setup(&x
->rtimer
, xfrm_replay_timer_handler
, 0);
549 x
->curlft
.add_time
= ktime_get_real_seconds();
550 x
->lft
.soft_byte_limit
= XFRM_INF
;
551 x
->lft
.soft_packet_limit
= XFRM_INF
;
552 x
->lft
.hard_byte_limit
= XFRM_INF
;
553 x
->lft
.hard_packet_limit
= XFRM_INF
;
554 x
->replay_maxage
= 0;
555 x
->replay_maxdiff
= 0;
556 spin_lock_init(&x
->lock
);
560 EXPORT_SYMBOL(xfrm_state_alloc
);
562 void __xfrm_state_destroy(struct xfrm_state
*x
, bool sync
)
564 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
568 ___xfrm_state_destroy(x
);
570 spin_lock_bh(&xfrm_state_gc_lock
);
571 hlist_add_head(&x
->gclist
, &xfrm_state_gc_list
);
572 spin_unlock_bh(&xfrm_state_gc_lock
);
573 schedule_work(&xfrm_state_gc_work
);
576 EXPORT_SYMBOL(__xfrm_state_destroy
);
578 int __xfrm_state_delete(struct xfrm_state
*x
)
580 struct net
*net
= xs_net(x
);
583 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
584 x
->km
.state
= XFRM_STATE_DEAD
;
585 spin_lock(&net
->xfrm
.xfrm_state_lock
);
586 list_del(&x
->km
.all
);
587 hlist_del_rcu(&x
->bydst
);
588 hlist_del_rcu(&x
->bysrc
);
590 hlist_del_rcu(&x
->byspi
);
591 net
->xfrm
.state_num
--;
592 spin_unlock(&net
->xfrm
.xfrm_state_lock
);
594 xfrm_dev_state_delete(x
);
596 /* All xfrm_state objects are created by xfrm_state_alloc.
597 * The xfrm_state_alloc call gives a reference, and that
598 * is what we are dropping here.
606 EXPORT_SYMBOL(__xfrm_state_delete
);
608 int xfrm_state_delete(struct xfrm_state
*x
)
612 spin_lock_bh(&x
->lock
);
613 err
= __xfrm_state_delete(x
);
614 spin_unlock_bh(&x
->lock
);
618 EXPORT_SYMBOL(xfrm_state_delete
);
620 #ifdef CONFIG_SECURITY_NETWORK_XFRM
622 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
626 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
627 struct xfrm_state
*x
;
629 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
630 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
631 (err
= security_xfrm_state_delete(x
)) != 0) {
632 xfrm_audit_state_delete(x
, 0, task_valid
);
642 xfrm_dev_state_flush_secctx_check(struct net
*net
, struct net_device
*dev
, bool task_valid
)
646 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
647 struct xfrm_state
*x
;
648 struct xfrm_state_offload
*xso
;
650 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
653 if (xso
->dev
== dev
&&
654 (err
= security_xfrm_state_delete(x
)) != 0) {
655 xfrm_audit_state_delete(x
, 0, task_valid
);
665 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
671 xfrm_dev_state_flush_secctx_check(struct net
*net
, struct net_device
*dev
, bool task_valid
)
677 int xfrm_state_flush(struct net
*net
, u8 proto
, bool task_valid
, bool sync
)
679 int i
, err
= 0, cnt
= 0;
681 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
682 err
= xfrm_state_flush_secctx_check(net
, proto
, task_valid
);
687 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
688 struct xfrm_state
*x
;
690 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
691 if (!xfrm_state_kern(x
) &&
692 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
694 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
696 err
= xfrm_state_delete(x
);
697 xfrm_audit_state_delete(x
, err
? 0 : 1,
700 xfrm_state_put_sync(x
);
706 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
712 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
718 EXPORT_SYMBOL(xfrm_state_flush
);
720 int xfrm_dev_state_flush(struct net
*net
, struct net_device
*dev
, bool task_valid
)
722 int i
, err
= 0, cnt
= 0;
724 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
725 err
= xfrm_dev_state_flush_secctx_check(net
, dev
, task_valid
);
730 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
731 struct xfrm_state
*x
;
732 struct xfrm_state_offload
*xso
;
734 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
737 if (!xfrm_state_kern(x
) && xso
->dev
== dev
) {
739 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
741 err
= xfrm_state_delete(x
);
742 xfrm_audit_state_delete(x
, err
? 0 : 1,
748 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
757 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
760 EXPORT_SYMBOL(xfrm_dev_state_flush
);
762 void xfrm_sad_getinfo(struct net
*net
, struct xfrmk_sadinfo
*si
)
764 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
765 si
->sadcnt
= net
->xfrm
.state_num
;
766 si
->sadhcnt
= net
->xfrm
.state_hmask
+ 1;
767 si
->sadhmcnt
= xfrm_state_hashmax
;
768 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
770 EXPORT_SYMBOL(xfrm_sad_getinfo
);
773 xfrm_init_tempstate(struct xfrm_state
*x
, const struct flowi
*fl
,
774 const struct xfrm_tmpl
*tmpl
,
775 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
776 unsigned short family
)
778 struct xfrm_state_afinfo
*afinfo
= xfrm_state_afinfo_get_rcu(family
);
783 afinfo
->init_tempsel(&x
->sel
, fl
);
785 if (family
!= tmpl
->encap_family
) {
786 afinfo
= xfrm_state_afinfo_get_rcu(tmpl
->encap_family
);
790 afinfo
->init_temprop(x
, tmpl
, daddr
, saddr
);
793 static struct xfrm_state
*__xfrm_state_lookup(struct net
*net
, u32 mark
,
794 const xfrm_address_t
*daddr
,
795 __be32 spi
, u8 proto
,
796 unsigned short family
)
798 unsigned int h
= xfrm_spi_hash(net
, daddr
, spi
, proto
, family
);
799 struct xfrm_state
*x
;
801 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_byspi
+ h
, byspi
) {
802 if (x
->props
.family
!= family
||
804 x
->id
.proto
!= proto
||
805 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
))
808 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
810 if (!xfrm_state_hold_rcu(x
))
818 static struct xfrm_state
*__xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
819 const xfrm_address_t
*daddr
,
820 const xfrm_address_t
*saddr
,
821 u8 proto
, unsigned short family
)
823 unsigned int h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
824 struct xfrm_state
*x
;
826 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bysrc
+ h
, bysrc
) {
827 if (x
->props
.family
!= family
||
828 x
->id
.proto
!= proto
||
829 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
830 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
833 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
835 if (!xfrm_state_hold_rcu(x
))
843 static inline struct xfrm_state
*
844 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
846 struct net
*net
= xs_net(x
);
847 u32 mark
= x
->mark
.v
& x
->mark
.m
;
850 return __xfrm_state_lookup(net
, mark
, &x
->id
.daddr
,
851 x
->id
.spi
, x
->id
.proto
, family
);
853 return __xfrm_state_lookup_byaddr(net
, mark
,
856 x
->id
.proto
, family
);
859 static void xfrm_hash_grow_check(struct net
*net
, int have_hash_collision
)
861 if (have_hash_collision
&&
862 (net
->xfrm
.state_hmask
+ 1) < xfrm_state_hashmax
&&
863 net
->xfrm
.state_num
> net
->xfrm
.state_hmask
)
864 schedule_work(&net
->xfrm
.state_hash_work
);
867 static void xfrm_state_look_at(struct xfrm_policy
*pol
, struct xfrm_state
*x
,
868 const struct flowi
*fl
, unsigned short family
,
869 struct xfrm_state
**best
, int *acq_in_progress
,
873 * 1. There is a valid state with matching selector. Done.
874 * 2. Valid state with inappropriate selector. Skip.
876 * Entering area of "sysdeps".
878 * 3. If state is not valid, selector is temporary, it selects
879 * only session which triggered previous resolution. Key
880 * manager will do something to install a state with proper
883 if (x
->km
.state
== XFRM_STATE_VALID
) {
884 if ((x
->sel
.family
&&
885 !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
886 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
890 (*best
)->km
.dying
> x
->km
.dying
||
891 ((*best
)->km
.dying
== x
->km
.dying
&&
892 (*best
)->curlft
.add_time
< x
->curlft
.add_time
))
894 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
895 *acq_in_progress
= 1;
896 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
897 x
->km
.state
== XFRM_STATE_EXPIRED
) {
898 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
899 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
905 xfrm_state_find(const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
906 const struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
907 struct xfrm_policy
*pol
, int *err
,
908 unsigned short family
, u32 if_id
)
910 static xfrm_address_t saddr_wildcard
= { };
911 struct net
*net
= xp_net(pol
);
912 unsigned int h
, h_wildcard
;
913 struct xfrm_state
*x
, *x0
, *to_put
;
914 int acquire_in_progress
= 0;
916 struct xfrm_state
*best
= NULL
;
917 u32 mark
= pol
->mark
.v
& pol
->mark
.m
;
918 unsigned short encap_family
= tmpl
->encap_family
;
919 unsigned int sequence
;
924 sequence
= read_seqcount_begin(&xfrm_state_hash_generation
);
927 h
= xfrm_dst_hash(net
, daddr
, saddr
, tmpl
->reqid
, encap_family
);
928 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bydst
+ h
, bydst
) {
929 if (x
->props
.family
== encap_family
&&
930 x
->props
.reqid
== tmpl
->reqid
&&
931 (mark
& x
->mark
.m
) == x
->mark
.v
&&
933 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
934 xfrm_state_addr_check(x
, daddr
, saddr
, encap_family
) &&
935 tmpl
->mode
== x
->props
.mode
&&
936 tmpl
->id
.proto
== x
->id
.proto
&&
937 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
938 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
939 &best
, &acquire_in_progress
, &error
);
941 if (best
|| acquire_in_progress
)
944 h_wildcard
= xfrm_dst_hash(net
, daddr
, &saddr_wildcard
, tmpl
->reqid
, encap_family
);
945 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bydst
+ h_wildcard
, bydst
) {
946 if (x
->props
.family
== encap_family
&&
947 x
->props
.reqid
== tmpl
->reqid
&&
948 (mark
& x
->mark
.m
) == x
->mark
.v
&&
950 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
951 xfrm_addr_equal(&x
->id
.daddr
, daddr
, encap_family
) &&
952 tmpl
->mode
== x
->props
.mode
&&
953 tmpl
->id
.proto
== x
->id
.proto
&&
954 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
955 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
956 &best
, &acquire_in_progress
, &error
);
961 if (!x
&& !error
&& !acquire_in_progress
) {
963 (x0
= __xfrm_state_lookup(net
, mark
, daddr
, tmpl
->id
.spi
,
964 tmpl
->id
.proto
, encap_family
)) != NULL
) {
971 /* If the KMs have no listeners (yet...), avoid allocating an SA
972 * for each and every packet - garbage collection might not
975 if (!km_is_alive(&c
)) {
980 x
= xfrm_state_alloc(net
);
985 /* Initialize temporary state matching only
986 * to current session. */
987 xfrm_init_tempstate(x
, fl
, tmpl
, daddr
, saddr
, family
);
988 memcpy(&x
->mark
, &pol
->mark
, sizeof(x
->mark
));
991 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->flowi_secid
);
993 x
->km
.state
= XFRM_STATE_DEAD
;
999 if (km_query(x
, tmpl
, pol
) == 0) {
1000 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1001 x
->km
.state
= XFRM_STATE_ACQ
;
1002 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1003 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1004 h
= xfrm_src_hash(net
, daddr
, saddr
, encap_family
);
1005 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1007 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, encap_family
);
1008 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1010 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1011 hrtimer_start(&x
->mtimer
,
1012 ktime_set(net
->xfrm
.sysctl_acq_expires
, 0),
1013 HRTIMER_MODE_REL_SOFT
);
1014 net
->xfrm
.state_num
++;
1015 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1016 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1018 x
->km
.state
= XFRM_STATE_DEAD
;
1026 if (!xfrm_state_hold_rcu(x
)) {
1031 *err
= acquire_in_progress
? -EAGAIN
: error
;
1035 xfrm_state_put(to_put
);
1037 if (read_seqcount_retry(&xfrm_state_hash_generation
, sequence
)) {
1049 xfrm_stateonly_find(struct net
*net
, u32 mark
, u32 if_id
,
1050 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1051 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
1054 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
1056 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1057 h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1058 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1059 if (x
->props
.family
== family
&&
1060 x
->props
.reqid
== reqid
&&
1061 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1062 x
->if_id
== if_id
&&
1063 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
1064 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
1065 mode
== x
->props
.mode
&&
1066 proto
== x
->id
.proto
&&
1067 x
->km
.state
== XFRM_STATE_VALID
) {
1074 xfrm_state_hold(rx
);
1075 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1080 EXPORT_SYMBOL(xfrm_stateonly_find
);
1082 struct xfrm_state
*xfrm_state_lookup_byspi(struct net
*net
, __be32 spi
,
1083 unsigned short family
)
1085 struct xfrm_state
*x
;
1086 struct xfrm_state_walk
*w
;
1088 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1089 list_for_each_entry(w
, &net
->xfrm
.state_all
, all
) {
1090 x
= container_of(w
, struct xfrm_state
, km
);
1091 if (x
->props
.family
!= family
||
1096 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1099 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1102 EXPORT_SYMBOL(xfrm_state_lookup_byspi
);
1104 static void __xfrm_state_insert(struct xfrm_state
*x
)
1106 struct net
*net
= xs_net(x
);
1109 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1111 h
= xfrm_dst_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
,
1112 x
->props
.reqid
, x
->props
.family
);
1113 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1115 h
= xfrm_src_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
1116 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1119 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
1122 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1125 hrtimer_start(&x
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT
);
1126 if (x
->replay_maxage
)
1127 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
1129 net
->xfrm
.state_num
++;
1131 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1134 /* net->xfrm.xfrm_state_lock is held */
1135 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
1137 struct net
*net
= xs_net(xnew
);
1138 unsigned short family
= xnew
->props
.family
;
1139 u32 reqid
= xnew
->props
.reqid
;
1140 struct xfrm_state
*x
;
1142 u32 mark
= xnew
->mark
.v
& xnew
->mark
.m
;
1143 u32 if_id
= xnew
->if_id
;
1145 h
= xfrm_dst_hash(net
, &xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
1146 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1147 if (x
->props
.family
== family
&&
1148 x
->props
.reqid
== reqid
&&
1149 x
->if_id
== if_id
&&
1150 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1151 xfrm_addr_equal(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
1152 xfrm_addr_equal(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
1157 void xfrm_state_insert(struct xfrm_state
*x
)
1159 struct net
*net
= xs_net(x
);
1161 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1162 __xfrm_state_bump_genids(x
);
1163 __xfrm_state_insert(x
);
1164 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1166 EXPORT_SYMBOL(xfrm_state_insert
);
1168 /* net->xfrm.xfrm_state_lock is held */
1169 static struct xfrm_state
*__find_acq_core(struct net
*net
,
1170 const struct xfrm_mark
*m
,
1171 unsigned short family
, u8 mode
,
1172 u32 reqid
, u32 if_id
, u8 proto
,
1173 const xfrm_address_t
*daddr
,
1174 const xfrm_address_t
*saddr
,
1177 unsigned int h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1178 struct xfrm_state
*x
;
1179 u32 mark
= m
->v
& m
->m
;
1181 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1182 if (x
->props
.reqid
!= reqid
||
1183 x
->props
.mode
!= mode
||
1184 x
->props
.family
!= family
||
1185 x
->km
.state
!= XFRM_STATE_ACQ
||
1187 x
->id
.proto
!= proto
||
1188 (mark
& x
->mark
.m
) != x
->mark
.v
||
1189 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
1190 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
1200 x
= xfrm_state_alloc(net
);
1204 x
->sel
.daddr
.a4
= daddr
->a4
;
1205 x
->sel
.saddr
.a4
= saddr
->a4
;
1206 x
->sel
.prefixlen_d
= 32;
1207 x
->sel
.prefixlen_s
= 32;
1208 x
->props
.saddr
.a4
= saddr
->a4
;
1209 x
->id
.daddr
.a4
= daddr
->a4
;
1213 x
->sel
.daddr
.in6
= daddr
->in6
;
1214 x
->sel
.saddr
.in6
= saddr
->in6
;
1215 x
->sel
.prefixlen_d
= 128;
1216 x
->sel
.prefixlen_s
= 128;
1217 x
->props
.saddr
.in6
= saddr
->in6
;
1218 x
->id
.daddr
.in6
= daddr
->in6
;
1222 x
->km
.state
= XFRM_STATE_ACQ
;
1223 x
->id
.proto
= proto
;
1224 x
->props
.family
= family
;
1225 x
->props
.mode
= mode
;
1226 x
->props
.reqid
= reqid
;
1230 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1232 hrtimer_start(&x
->mtimer
,
1233 ktime_set(net
->xfrm
.sysctl_acq_expires
, 0),
1234 HRTIMER_MODE_REL_SOFT
);
1235 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1236 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1237 h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
1238 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1240 net
->xfrm
.state_num
++;
1242 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1248 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
);
1250 int xfrm_state_add(struct xfrm_state
*x
)
1252 struct net
*net
= xs_net(x
);
1253 struct xfrm_state
*x1
, *to_put
;
1256 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1257 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1259 family
= x
->props
.family
;
1263 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1265 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1273 if (use_spi
&& x
->km
.seq
) {
1274 x1
= __xfrm_find_acq_byseq(net
, mark
, x
->km
.seq
);
1275 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1276 !xfrm_addr_equal(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1283 x1
= __find_acq_core(net
, &x
->mark
, family
, x
->props
.mode
,
1284 x
->props
.reqid
, x
->if_id
, x
->id
.proto
,
1285 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1287 __xfrm_state_bump_genids(x
);
1288 __xfrm_state_insert(x
);
1292 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1295 xfrm_state_delete(x1
);
1300 xfrm_state_put(to_put
);
1304 EXPORT_SYMBOL(xfrm_state_add
);
1306 #ifdef CONFIG_XFRM_MIGRATE
1307 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
,
1308 struct xfrm_encap_tmpl
*encap
)
1310 struct net
*net
= xs_net(orig
);
1311 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1315 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1316 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1317 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1318 x
->props
.mode
= orig
->props
.mode
;
1319 x
->props
.replay_window
= orig
->props
.replay_window
;
1320 x
->props
.reqid
= orig
->props
.reqid
;
1321 x
->props
.family
= orig
->props
.family
;
1322 x
->props
.saddr
= orig
->props
.saddr
;
1325 x
->aalg
= xfrm_algo_auth_clone(orig
->aalg
);
1329 x
->props
.aalgo
= orig
->props
.aalgo
;
1332 x
->aead
= xfrm_algo_aead_clone(orig
->aead
);
1333 x
->geniv
= orig
->geniv
;
1338 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1342 x
->props
.ealgo
= orig
->props
.ealgo
;
1345 x
->calg
= xfrm_algo_clone(orig
->calg
);
1349 x
->props
.calgo
= orig
->props
.calgo
;
1351 if (encap
|| orig
->encap
) {
1353 x
->encap
= kmemdup(encap
, sizeof(*x
->encap
),
1356 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
),
1364 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1370 if (orig
->replay_esn
) {
1371 if (xfrm_replay_clone(x
, orig
))
1375 memcpy(&x
->mark
, &orig
->mark
, sizeof(x
->mark
));
1377 if (xfrm_init_state(x
) < 0)
1380 x
->props
.flags
= orig
->props
.flags
;
1381 x
->props
.extra_flags
= orig
->props
.extra_flags
;
1383 x
->if_id
= orig
->if_id
;
1384 x
->tfcpad
= orig
->tfcpad
;
1385 x
->replay_maxdiff
= orig
->replay_maxdiff
;
1386 x
->replay_maxage
= orig
->replay_maxage
;
1387 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1388 x
->km
.state
= orig
->km
.state
;
1389 x
->km
.seq
= orig
->km
.seq
;
1390 x
->replay
= orig
->replay
;
1391 x
->preplay
= orig
->preplay
;
1401 struct xfrm_state
*xfrm_migrate_state_find(struct xfrm_migrate
*m
, struct net
*net
)
1404 struct xfrm_state
*x
= NULL
;
1406 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1409 h
= xfrm_dst_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1410 m
->reqid
, m
->old_family
);
1411 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1412 if (x
->props
.mode
!= m
->mode
||
1413 x
->id
.proto
!= m
->proto
)
1415 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1417 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1419 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1426 h
= xfrm_src_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1428 hlist_for_each_entry(x
, net
->xfrm
.state_bysrc
+h
, bysrc
) {
1429 if (x
->props
.mode
!= m
->mode
||
1430 x
->id
.proto
!= m
->proto
)
1432 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1434 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1442 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1446 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1448 struct xfrm_state
*xfrm_state_migrate(struct xfrm_state
*x
,
1449 struct xfrm_migrate
*m
,
1450 struct xfrm_encap_tmpl
*encap
)
1452 struct xfrm_state
*xc
;
1454 xc
= xfrm_state_clone(x
, encap
);
1458 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1459 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1462 if (xfrm_addr_equal(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1463 /* a care is needed when the destination address of the
1464 state is to be updated as it is a part of triplet */
1465 xfrm_state_insert(xc
);
1467 if (xfrm_state_add(xc
) < 0)
1476 EXPORT_SYMBOL(xfrm_state_migrate
);
1479 int xfrm_state_update(struct xfrm_state
*x
)
1481 struct xfrm_state
*x1
, *to_put
;
1483 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1484 struct net
*net
= xs_net(x
);
1488 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1489 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1495 if (xfrm_state_kern(x1
)) {
1501 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1502 __xfrm_state_insert(x
);
1508 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1511 xfrm_state_put(to_put
);
1517 xfrm_state_delete(x1
);
1523 spin_lock_bh(&x1
->lock
);
1524 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1525 if (x
->encap
&& x1
->encap
&&
1526 x
->encap
->encap_type
== x1
->encap
->encap_type
)
1527 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1528 else if (x
->encap
|| x1
->encap
)
1531 if (x
->coaddr
&& x1
->coaddr
) {
1532 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1534 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1535 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1536 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1539 hrtimer_start(&x1
->mtimer
, ktime_set(1, 0),
1540 HRTIMER_MODE_REL_SOFT
);
1541 if (x1
->curlft
.use_time
)
1542 xfrm_state_check_expire(x1
);
1544 if (x
->props
.smark
.m
|| x
->props
.smark
.v
|| x
->if_id
) {
1545 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1547 if (x
->props
.smark
.m
|| x
->props
.smark
.v
)
1548 x1
->props
.smark
= x
->props
.smark
;
1551 x1
->if_id
= x
->if_id
;
1553 __xfrm_state_bump_genids(x1
);
1554 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1558 x
->km
.state
= XFRM_STATE_DEAD
;
1559 __xfrm_state_put(x
);
1563 spin_unlock_bh(&x1
->lock
);
1569 EXPORT_SYMBOL(xfrm_state_update
);
1571 int xfrm_state_check_expire(struct xfrm_state
*x
)
1573 if (!x
->curlft
.use_time
)
1574 x
->curlft
.use_time
= ktime_get_real_seconds();
1576 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1577 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1578 x
->km
.state
= XFRM_STATE_EXPIRED
;
1579 hrtimer_start(&x
->mtimer
, 0, HRTIMER_MODE_REL_SOFT
);
1584 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1585 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1587 km_state_expired(x
, 0, 0);
1591 EXPORT_SYMBOL(xfrm_state_check_expire
);
1594 xfrm_state_lookup(struct net
*net
, u32 mark
, const xfrm_address_t
*daddr
, __be32 spi
,
1595 u8 proto
, unsigned short family
)
1597 struct xfrm_state
*x
;
1600 x
= __xfrm_state_lookup(net
, mark
, daddr
, spi
, proto
, family
);
1604 EXPORT_SYMBOL(xfrm_state_lookup
);
1607 xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
1608 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
1609 u8 proto
, unsigned short family
)
1611 struct xfrm_state
*x
;
1613 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1614 x
= __xfrm_state_lookup_byaddr(net
, mark
, daddr
, saddr
, proto
, family
);
1615 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1618 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1621 xfrm_find_acq(struct net
*net
, const struct xfrm_mark
*mark
, u8 mode
, u32 reqid
,
1622 u32 if_id
, u8 proto
, const xfrm_address_t
*daddr
,
1623 const xfrm_address_t
*saddr
, int create
, unsigned short family
)
1625 struct xfrm_state
*x
;
1627 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1628 x
= __find_acq_core(net
, mark
, family
, mode
, reqid
, if_id
, proto
, daddr
, saddr
, create
);
1629 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1633 EXPORT_SYMBOL(xfrm_find_acq
);
1635 #ifdef CONFIG_XFRM_SUB_POLICY
1637 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1638 unsigned short family
, struct net
*net
)
1642 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1644 return -EAFNOSUPPORT
;
1646 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
); /*FIXME*/
1647 if (afinfo
->tmpl_sort
)
1648 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1650 for (i
= 0; i
< n
; i
++)
1652 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1656 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1659 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1660 unsigned short family
)
1664 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1665 struct net
*net
= xs_net(*src
);
1668 return -EAFNOSUPPORT
;
1670 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1671 if (afinfo
->state_sort
)
1672 err
= afinfo
->state_sort(dst
, src
, n
);
1674 for (i
= 0; i
< n
; i
++)
1676 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1680 EXPORT_SYMBOL(xfrm_state_sort
);
1683 /* Silly enough, but I'm lazy to build resolution list */
1685 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1689 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
1690 struct xfrm_state
*x
;
1692 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
1693 if (x
->km
.seq
== seq
&&
1694 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1695 x
->km
.state
== XFRM_STATE_ACQ
) {
1704 struct xfrm_state
*xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1706 struct xfrm_state
*x
;
1708 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1709 x
= __xfrm_find_acq_byseq(net
, mark
, seq
);
1710 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1713 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1715 u32
xfrm_get_acqseq(void)
1718 static atomic_t acqseq
;
1721 res
= atomic_inc_return(&acqseq
);
1726 EXPORT_SYMBOL(xfrm_get_acqseq
);
1728 int verify_spi_info(u8 proto
, u32 min
, u32 max
)
1736 /* IPCOMP spi is 16-bits. */
1750 EXPORT_SYMBOL(verify_spi_info
);
1752 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1754 struct net
*net
= xs_net(x
);
1756 struct xfrm_state
*x0
;
1758 __be32 minspi
= htonl(low
);
1759 __be32 maxspi
= htonl(high
);
1760 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1762 spin_lock_bh(&x
->lock
);
1763 if (x
->km
.state
== XFRM_STATE_DEAD
)
1772 if (minspi
== maxspi
) {
1773 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1781 for (h
= 0; h
< high
-low
+1; h
++) {
1782 spi
= low
+ prandom_u32()%(high
-low
+1);
1783 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1785 x
->id
.spi
= htonl(spi
);
1792 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1793 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1794 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1795 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1801 spin_unlock_bh(&x
->lock
);
1805 EXPORT_SYMBOL(xfrm_alloc_spi
);
1807 static bool __xfrm_state_filter_match(struct xfrm_state
*x
,
1808 struct xfrm_address_filter
*filter
)
1811 if ((filter
->family
== AF_INET
||
1812 filter
->family
== AF_INET6
) &&
1813 x
->props
.family
!= filter
->family
)
1816 return addr_match(&x
->props
.saddr
, &filter
->saddr
,
1818 addr_match(&x
->id
.daddr
, &filter
->daddr
,
1824 int xfrm_state_walk(struct net
*net
, struct xfrm_state_walk
*walk
,
1825 int (*func
)(struct xfrm_state
*, int, void*),
1828 struct xfrm_state
*state
;
1829 struct xfrm_state_walk
*x
;
1832 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
1835 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1836 if (list_empty(&walk
->all
))
1837 x
= list_first_entry(&net
->xfrm
.state_all
, struct xfrm_state_walk
, all
);
1839 x
= list_first_entry(&walk
->all
, struct xfrm_state_walk
, all
);
1840 list_for_each_entry_from(x
, &net
->xfrm
.state_all
, all
) {
1841 if (x
->state
== XFRM_STATE_DEAD
)
1843 state
= container_of(x
, struct xfrm_state
, km
);
1844 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
1846 if (!__xfrm_state_filter_match(state
, walk
->filter
))
1848 err
= func(state
, walk
->seq
, data
);
1850 list_move_tail(&walk
->all
, &x
->all
);
1855 if (walk
->seq
== 0) {
1859 list_del_init(&walk
->all
);
1861 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1864 EXPORT_SYMBOL(xfrm_state_walk
);
1866 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
,
1867 struct xfrm_address_filter
*filter
)
1869 INIT_LIST_HEAD(&walk
->all
);
1870 walk
->proto
= proto
;
1871 walk
->state
= XFRM_STATE_DEAD
;
1873 walk
->filter
= filter
;
1875 EXPORT_SYMBOL(xfrm_state_walk_init
);
1877 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
, struct net
*net
)
1879 kfree(walk
->filter
);
1881 if (list_empty(&walk
->all
))
1884 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1885 list_del(&walk
->all
);
1886 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1888 EXPORT_SYMBOL(xfrm_state_walk_done
);
1890 static void xfrm_replay_timer_handler(struct timer_list
*t
)
1892 struct xfrm_state
*x
= from_timer(x
, t
, rtimer
);
1894 spin_lock(&x
->lock
);
1896 if (x
->km
.state
== XFRM_STATE_VALID
) {
1897 if (xfrm_aevent_is_on(xs_net(x
)))
1898 x
->repl
->notify(x
, XFRM_REPLAY_TIMEOUT
);
1900 x
->xflags
|= XFRM_TIME_DEFER
;
1903 spin_unlock(&x
->lock
);
1906 static LIST_HEAD(xfrm_km_list
);
1908 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
1910 struct xfrm_mgr
*km
;
1913 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1914 if (km
->notify_policy
)
1915 km
->notify_policy(xp
, dir
, c
);
1919 void km_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
1921 struct xfrm_mgr
*km
;
1923 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1929 EXPORT_SYMBOL(km_policy_notify
);
1930 EXPORT_SYMBOL(km_state_notify
);
1932 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
)
1938 c
.event
= XFRM_MSG_EXPIRE
;
1939 km_state_notify(x
, &c
);
1942 EXPORT_SYMBOL(km_state_expired
);
1944 * We send to all registered managers regardless of failure
1945 * We are happy with one success
1947 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1949 int err
= -EINVAL
, acqret
;
1950 struct xfrm_mgr
*km
;
1953 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1954 acqret
= km
->acquire(x
, t
, pol
);
1961 EXPORT_SYMBOL(km_query
);
1963 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1966 struct xfrm_mgr
*km
;
1969 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1970 if (km
->new_mapping
)
1971 err
= km
->new_mapping(x
, ipaddr
, sport
);
1978 EXPORT_SYMBOL(km_new_mapping
);
1980 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 portid
)
1986 c
.event
= XFRM_MSG_POLEXPIRE
;
1987 km_policy_notify(pol
, dir
, &c
);
1989 EXPORT_SYMBOL(km_policy_expired
);
1991 #ifdef CONFIG_XFRM_MIGRATE
1992 int km_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1993 const struct xfrm_migrate
*m
, int num_migrate
,
1994 const struct xfrm_kmaddress
*k
,
1995 const struct xfrm_encap_tmpl
*encap
)
1999 struct xfrm_mgr
*km
;
2002 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2004 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
,
2013 EXPORT_SYMBOL(km_migrate
);
2016 int km_report(struct net
*net
, u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2020 struct xfrm_mgr
*km
;
2023 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2025 ret
= km
->report(net
, proto
, sel
, addr
);
2033 EXPORT_SYMBOL(km_report
);
2035 static bool km_is_alive(const struct km_event
*c
)
2037 struct xfrm_mgr
*km
;
2038 bool is_alive
= false;
2041 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2042 if (km
->is_alive
&& km
->is_alive(c
)) {
2052 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
2056 struct xfrm_mgr
*km
;
2057 struct xfrm_policy
*pol
= NULL
;
2059 if (in_compat_syscall())
2062 if (!optval
&& !optlen
) {
2063 xfrm_sk_policy_insert(sk
, XFRM_POLICY_IN
, NULL
);
2064 xfrm_sk_policy_insert(sk
, XFRM_POLICY_OUT
, NULL
);
2069 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
2072 data
= memdup_user(optval
, optlen
);
2074 return PTR_ERR(data
);
2078 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2079 pol
= km
->compile_policy(sk
, optname
, data
,
2087 xfrm_sk_policy_insert(sk
, err
, pol
);
2096 EXPORT_SYMBOL(xfrm_user_policy
);
2098 static DEFINE_SPINLOCK(xfrm_km_lock
);
2100 int xfrm_register_km(struct xfrm_mgr
*km
)
2102 spin_lock_bh(&xfrm_km_lock
);
2103 list_add_tail_rcu(&km
->list
, &xfrm_km_list
);
2104 spin_unlock_bh(&xfrm_km_lock
);
2107 EXPORT_SYMBOL(xfrm_register_km
);
2109 int xfrm_unregister_km(struct xfrm_mgr
*km
)
2111 spin_lock_bh(&xfrm_km_lock
);
2112 list_del_rcu(&km
->list
);
2113 spin_unlock_bh(&xfrm_km_lock
);
2117 EXPORT_SYMBOL(xfrm_unregister_km
);
2119 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
2123 if (WARN_ON(afinfo
->family
>= NPROTO
))
2124 return -EAFNOSUPPORT
;
2126 spin_lock_bh(&xfrm_state_afinfo_lock
);
2127 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
2130 rcu_assign_pointer(xfrm_state_afinfo
[afinfo
->family
], afinfo
);
2131 spin_unlock_bh(&xfrm_state_afinfo_lock
);
2134 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
2136 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
2138 int err
= 0, family
= afinfo
->family
;
2140 if (WARN_ON(family
>= NPROTO
))
2141 return -EAFNOSUPPORT
;
2143 spin_lock_bh(&xfrm_state_afinfo_lock
);
2144 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
2145 if (rcu_access_pointer(xfrm_state_afinfo
[family
]) != afinfo
)
2148 RCU_INIT_POINTER(xfrm_state_afinfo
[afinfo
->family
], NULL
);
2150 spin_unlock_bh(&xfrm_state_afinfo_lock
);
2154 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
2156 struct xfrm_state_afinfo
*xfrm_state_afinfo_get_rcu(unsigned int family
)
2158 if (unlikely(family
>= NPROTO
))
2161 return rcu_dereference(xfrm_state_afinfo
[family
]);
2163 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu
);
2165 struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
2167 struct xfrm_state_afinfo
*afinfo
;
2168 if (unlikely(family
>= NPROTO
))
2171 afinfo
= rcu_dereference(xfrm_state_afinfo
[family
]);
2172 if (unlikely(!afinfo
))
2177 void xfrm_flush_gc(void)
2179 flush_work(&xfrm_state_gc_work
);
2181 EXPORT_SYMBOL(xfrm_flush_gc
);
2183 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2184 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
2187 struct xfrm_state
*t
= x
->tunnel
;
2189 if (atomic_read(&t
->tunnel_users
) == 2)
2190 xfrm_state_delete(t
);
2191 atomic_dec(&t
->tunnel_users
);
2192 xfrm_state_put_sync(t
);
2196 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
2198 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
2200 const struct xfrm_type
*type
= READ_ONCE(x
->type
);
2202 if (x
->km
.state
== XFRM_STATE_VALID
&&
2203 type
&& type
->get_mtu
)
2204 return type
->get_mtu(x
, mtu
);
2206 return mtu
- x
->props
.header_len
;
2209 int __xfrm_init_state(struct xfrm_state
*x
, bool init_replay
, bool offload
)
2211 const struct xfrm_state_afinfo
*afinfo
;
2212 const struct xfrm_mode
*inner_mode
;
2213 const struct xfrm_mode
*outer_mode
;
2214 int family
= x
->props
.family
;
2217 err
= -EAFNOSUPPORT
;
2218 afinfo
= xfrm_state_get_afinfo(family
);
2223 if (afinfo
->init_flags
)
2224 err
= afinfo
->init_flags(x
);
2231 err
= -EPROTONOSUPPORT
;
2233 if (x
->sel
.family
!= AF_UNSPEC
) {
2234 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
2235 if (inner_mode
== NULL
)
2238 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2239 family
!= x
->sel
.family
)
2242 x
->inner_mode
= *inner_mode
;
2244 const struct xfrm_mode
*inner_mode_iaf
;
2245 int iafamily
= AF_INET
;
2247 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->props
.family
);
2248 if (inner_mode
== NULL
)
2251 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
))
2254 x
->inner_mode
= *inner_mode
;
2256 if (x
->props
.family
== AF_INET
)
2257 iafamily
= AF_INET6
;
2259 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, iafamily
);
2260 if (inner_mode_iaf
) {
2261 if (inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)
2262 x
->inner_mode_iaf
= *inner_mode_iaf
;
2266 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2267 if (x
->type
== NULL
)
2270 x
->type_offload
= xfrm_get_type_offload(x
->id
.proto
, family
, offload
);
2272 err
= x
->type
->init_state(x
);
2276 outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2278 err
= -EPROTONOSUPPORT
;
2282 x
->outer_mode
= *outer_mode
;
2284 err
= xfrm_init_replay(x
);
2293 EXPORT_SYMBOL(__xfrm_init_state
);
2295 int xfrm_init_state(struct xfrm_state
*x
)
2299 err
= __xfrm_init_state(x
, true, false);
2301 x
->km
.state
= XFRM_STATE_VALID
;
2306 EXPORT_SYMBOL(xfrm_init_state
);
2308 int __net_init
xfrm_state_init(struct net
*net
)
2312 if (net_eq(net
, &init_net
))
2313 xfrm_state_cache
= KMEM_CACHE(xfrm_state
,
2314 SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
2316 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2318 sz
= sizeof(struct hlist_head
) * 8;
2320 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2321 if (!net
->xfrm
.state_bydst
)
2323 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2324 if (!net
->xfrm
.state_bysrc
)
2326 net
->xfrm
.state_byspi
= xfrm_hash_alloc(sz
);
2327 if (!net
->xfrm
.state_byspi
)
2329 net
->xfrm
.state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2331 net
->xfrm
.state_num
= 0;
2332 INIT_WORK(&net
->xfrm
.state_hash_work
, xfrm_hash_resize
);
2333 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
2337 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2339 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2344 void xfrm_state_fini(struct net
*net
)
2348 flush_work(&net
->xfrm
.state_hash_work
);
2349 flush_work(&xfrm_state_gc_work
);
2350 xfrm_state_flush(net
, 0, false, true);
2352 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2354 sz
= (net
->xfrm
.state_hmask
+ 1) * sizeof(struct hlist_head
);
2355 WARN_ON(!hlist_empty(net
->xfrm
.state_byspi
));
2356 xfrm_hash_free(net
->xfrm
.state_byspi
, sz
);
2357 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2358 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2359 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2360 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2363 #ifdef CONFIG_AUDITSYSCALL
2364 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2365 struct audit_buffer
*audit_buf
)
2367 struct xfrm_sec_ctx
*ctx
= x
->security
;
2368 u32 spi
= ntohl(x
->id
.spi
);
2371 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2372 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2374 switch (x
->props
.family
) {
2376 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2377 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2380 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2381 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2385 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2388 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2389 struct audit_buffer
*audit_buf
)
2391 const struct iphdr
*iph4
;
2392 const struct ipv6hdr
*iph6
;
2397 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2398 &iph4
->saddr
, &iph4
->daddr
);
2401 iph6
= ipv6_hdr(skb
);
2402 audit_log_format(audit_buf
,
2403 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2404 &iph6
->saddr
, &iph6
->daddr
,
2405 iph6
->flow_lbl
[0] & 0x0f,
2412 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
, bool task_valid
)
2414 struct audit_buffer
*audit_buf
;
2416 audit_buf
= xfrm_audit_start("SAD-add");
2417 if (audit_buf
== NULL
)
2419 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2420 xfrm_audit_helper_sainfo(x
, audit_buf
);
2421 audit_log_format(audit_buf
, " res=%u", result
);
2422 audit_log_end(audit_buf
);
2424 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2426 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
, bool task_valid
)
2428 struct audit_buffer
*audit_buf
;
2430 audit_buf
= xfrm_audit_start("SAD-delete");
2431 if (audit_buf
== NULL
)
2433 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2434 xfrm_audit_helper_sainfo(x
, audit_buf
);
2435 audit_log_format(audit_buf
, " res=%u", result
);
2436 audit_log_end(audit_buf
);
2438 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2440 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2441 struct sk_buff
*skb
)
2443 struct audit_buffer
*audit_buf
;
2446 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2447 if (audit_buf
== NULL
)
2449 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2450 /* don't record the sequence number because it's inherent in this kind
2451 * of audit message */
2452 spi
= ntohl(x
->id
.spi
);
2453 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2454 audit_log_end(audit_buf
);
2456 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2458 void xfrm_audit_state_replay(struct xfrm_state
*x
,
2459 struct sk_buff
*skb
, __be32 net_seq
)
2461 struct audit_buffer
*audit_buf
;
2464 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2465 if (audit_buf
== NULL
)
2467 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2468 spi
= ntohl(x
->id
.spi
);
2469 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2470 spi
, spi
, ntohl(net_seq
));
2471 audit_log_end(audit_buf
);
2473 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay
);
2475 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2477 struct audit_buffer
*audit_buf
;
2479 audit_buf
= xfrm_audit_start("SA-notfound");
2480 if (audit_buf
== NULL
)
2482 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2483 audit_log_end(audit_buf
);
2485 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2487 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2488 __be32 net_spi
, __be32 net_seq
)
2490 struct audit_buffer
*audit_buf
;
2493 audit_buf
= xfrm_audit_start("SA-notfound");
2494 if (audit_buf
== NULL
)
2496 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2497 spi
= ntohl(net_spi
);
2498 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2499 spi
, spi
, ntohl(net_seq
));
2500 audit_log_end(audit_buf
);
2502 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2504 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2505 struct sk_buff
*skb
, u8 proto
)
2507 struct audit_buffer
*audit_buf
;
2511 audit_buf
= xfrm_audit_start("SA-icv-failure");
2512 if (audit_buf
== NULL
)
2514 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2515 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2516 u32 spi
= ntohl(net_spi
);
2517 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2518 spi
, spi
, ntohl(net_seq
));
2520 audit_log_end(audit_buf
);
2522 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2523 #endif /* CONFIG_AUDITSYSCALL */