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 <crypto/aead.h>
32 #include "xfrm_hash.h"
34 #define xfrm_state_deref_prot(table, net) \
35 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
37 static void xfrm_state_gc_task(struct work_struct
*work
);
39 /* Each xfrm_state may be linked to two tables:
41 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
42 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
43 destination/tunnel endpoint. (output)
46 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
47 static __read_mostly seqcount_t xfrm_state_hash_generation
= SEQCNT_ZERO(xfrm_state_hash_generation
);
48 static struct kmem_cache
*xfrm_state_cache __ro_after_init
;
50 static DECLARE_WORK(xfrm_state_gc_work
, xfrm_state_gc_task
);
51 static HLIST_HEAD(xfrm_state_gc_list
);
53 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu
*x
)
55 return refcount_inc_not_zero(&x
->refcnt
);
58 static inline unsigned int xfrm_dst_hash(struct net
*net
,
59 const xfrm_address_t
*daddr
,
60 const xfrm_address_t
*saddr
,
62 unsigned short family
)
64 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, net
->xfrm
.state_hmask
);
67 static inline unsigned int xfrm_src_hash(struct net
*net
,
68 const xfrm_address_t
*daddr
,
69 const xfrm_address_t
*saddr
,
70 unsigned short family
)
72 return __xfrm_src_hash(daddr
, saddr
, family
, net
->xfrm
.state_hmask
);
75 static inline unsigned int
76 xfrm_spi_hash(struct net
*net
, const xfrm_address_t
*daddr
,
77 __be32 spi
, u8 proto
, unsigned short family
)
79 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, net
->xfrm
.state_hmask
);
82 static void xfrm_hash_transfer(struct hlist_head
*list
,
83 struct hlist_head
*ndsttable
,
84 struct hlist_head
*nsrctable
,
85 struct hlist_head
*nspitable
,
86 unsigned int nhashmask
)
88 struct hlist_node
*tmp
;
91 hlist_for_each_entry_safe(x
, tmp
, list
, bydst
) {
94 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
95 x
->props
.reqid
, x
->props
.family
,
97 hlist_add_head_rcu(&x
->bydst
, ndsttable
+ h
);
99 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
102 hlist_add_head_rcu(&x
->bysrc
, nsrctable
+ h
);
105 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
106 x
->id
.proto
, x
->props
.family
,
108 hlist_add_head_rcu(&x
->byspi
, nspitable
+ h
);
113 static unsigned long xfrm_hash_new_size(unsigned int state_hmask
)
115 return ((state_hmask
+ 1) << 1) * sizeof(struct hlist_head
);
118 static void xfrm_hash_resize(struct work_struct
*work
)
120 struct net
*net
= container_of(work
, struct net
, xfrm
.state_hash_work
);
121 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
122 unsigned long nsize
, osize
;
123 unsigned int nhashmask
, ohashmask
;
126 nsize
= xfrm_hash_new_size(net
->xfrm
.state_hmask
);
127 ndst
= xfrm_hash_alloc(nsize
);
130 nsrc
= xfrm_hash_alloc(nsize
);
132 xfrm_hash_free(ndst
, nsize
);
135 nspi
= xfrm_hash_alloc(nsize
);
137 xfrm_hash_free(ndst
, nsize
);
138 xfrm_hash_free(nsrc
, nsize
);
142 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
143 write_seqcount_begin(&xfrm_state_hash_generation
);
145 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
146 odst
= xfrm_state_deref_prot(net
->xfrm
.state_bydst
, net
);
147 for (i
= net
->xfrm
.state_hmask
; i
>= 0; i
--)
148 xfrm_hash_transfer(odst
+ i
, ndst
, nsrc
, nspi
, nhashmask
);
150 osrc
= xfrm_state_deref_prot(net
->xfrm
.state_bysrc
, net
);
151 ospi
= xfrm_state_deref_prot(net
->xfrm
.state_byspi
, net
);
152 ohashmask
= net
->xfrm
.state_hmask
;
154 rcu_assign_pointer(net
->xfrm
.state_bydst
, ndst
);
155 rcu_assign_pointer(net
->xfrm
.state_bysrc
, nsrc
);
156 rcu_assign_pointer(net
->xfrm
.state_byspi
, nspi
);
157 net
->xfrm
.state_hmask
= nhashmask
;
159 write_seqcount_end(&xfrm_state_hash_generation
);
160 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
162 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
166 xfrm_hash_free(odst
, osize
);
167 xfrm_hash_free(osrc
, osize
);
168 xfrm_hash_free(ospi
, osize
);
171 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock
);
172 static struct xfrm_state_afinfo __rcu
*xfrm_state_afinfo
[NPROTO
];
174 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
176 int __xfrm_state_delete(struct xfrm_state
*x
);
178 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
179 static bool km_is_alive(const struct km_event
*c
);
180 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
);
182 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
184 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
188 return -EAFNOSUPPORT
;
190 #define X(afi, T, name) do { \
191 WARN_ON((afi)->type_ ## name); \
192 (afi)->type_ ## name = (T); \
195 switch (type
->proto
) {
197 X(afinfo
, type
, comp
);
203 X(afinfo
, type
, esp
);
206 X(afinfo
, type
, ipip
);
208 case IPPROTO_DSTOPTS
:
209 X(afinfo
, type
, dstopts
);
211 case IPPROTO_ROUTING
:
212 X(afinfo
, type
, routing
);
215 X(afinfo
, type
, ipip6
);
219 err
= -EPROTONOSUPPORT
;
226 EXPORT_SYMBOL(xfrm_register_type
);
228 void xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
230 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
232 if (unlikely(afinfo
== NULL
))
235 #define X(afi, T, name) do { \
236 WARN_ON((afi)->type_ ## name != (T)); \
237 (afi)->type_ ## name = NULL; \
240 switch (type
->proto
) {
242 X(afinfo
, type
, comp
);
248 X(afinfo
, type
, esp
);
251 X(afinfo
, type
, ipip
);
253 case IPPROTO_DSTOPTS
:
254 X(afinfo
, type
, dstopts
);
256 case IPPROTO_ROUTING
:
257 X(afinfo
, type
, routing
);
260 X(afinfo
, type
, ipip6
);
269 EXPORT_SYMBOL(xfrm_unregister_type
);
271 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
273 const struct xfrm_type
*type
= NULL
;
274 struct xfrm_state_afinfo
*afinfo
;
275 int modload_attempted
= 0;
278 afinfo
= xfrm_state_get_afinfo(family
);
279 if (unlikely(afinfo
== NULL
))
284 type
= afinfo
->type_comp
;
287 type
= afinfo
->type_ah
;
290 type
= afinfo
->type_esp
;
293 type
= afinfo
->type_ipip
;
295 case IPPROTO_DSTOPTS
:
296 type
= afinfo
->type_dstopts
;
298 case IPPROTO_ROUTING
:
299 type
= afinfo
->type_routing
;
302 type
= afinfo
->type_ipip6
;
308 if (unlikely(type
&& !try_module_get(type
->owner
)))
313 if (!type
&& !modload_attempted
) {
314 request_module("xfrm-type-%d-%d", family
, proto
);
315 modload_attempted
= 1;
322 static void xfrm_put_type(const struct xfrm_type
*type
)
324 module_put(type
->owner
);
327 int xfrm_register_type_offload(const struct xfrm_type_offload
*type
,
328 unsigned short family
)
330 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
333 if (unlikely(afinfo
== NULL
))
334 return -EAFNOSUPPORT
;
336 switch (type
->proto
) {
338 WARN_ON(afinfo
->type_offload_esp
);
339 afinfo
->type_offload_esp
= type
;
343 err
= -EPROTONOSUPPORT
;
350 EXPORT_SYMBOL(xfrm_register_type_offload
);
352 void xfrm_unregister_type_offload(const struct xfrm_type_offload
*type
,
353 unsigned short family
)
355 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
357 if (unlikely(afinfo
== NULL
))
360 switch (type
->proto
) {
362 WARN_ON(afinfo
->type_offload_esp
!= type
);
363 afinfo
->type_offload_esp
= NULL
;
371 EXPORT_SYMBOL(xfrm_unregister_type_offload
);
373 static const struct xfrm_type_offload
*
374 xfrm_get_type_offload(u8 proto
, unsigned short family
, bool try_load
)
376 const struct xfrm_type_offload
*type
= NULL
;
377 struct xfrm_state_afinfo
*afinfo
;
380 afinfo
= xfrm_state_get_afinfo(family
);
381 if (unlikely(afinfo
== NULL
))
386 type
= afinfo
->type_offload_esp
;
392 if ((type
&& !try_module_get(type
->owner
)))
397 if (!type
&& try_load
) {
398 request_module("xfrm-offload-%d-%d", family
, proto
);
406 static void xfrm_put_type_offload(const struct xfrm_type_offload
*type
)
408 module_put(type
->owner
);
411 static const struct xfrm_mode xfrm4_mode_map
[XFRM_MODE_MAX
] = {
413 .encap
= XFRM_MODE_BEET
,
414 .flags
= XFRM_MODE_FLAG_TUNNEL
,
417 [XFRM_MODE_TRANSPORT
] = {
418 .encap
= XFRM_MODE_TRANSPORT
,
421 [XFRM_MODE_TUNNEL
] = {
422 .encap
= XFRM_MODE_TUNNEL
,
423 .flags
= XFRM_MODE_FLAG_TUNNEL
,
428 static const struct xfrm_mode xfrm6_mode_map
[XFRM_MODE_MAX
] = {
430 .encap
= XFRM_MODE_BEET
,
431 .flags
= XFRM_MODE_FLAG_TUNNEL
,
434 [XFRM_MODE_ROUTEOPTIMIZATION
] = {
435 .encap
= XFRM_MODE_ROUTEOPTIMIZATION
,
438 [XFRM_MODE_TRANSPORT
] = {
439 .encap
= XFRM_MODE_TRANSPORT
,
442 [XFRM_MODE_TUNNEL
] = {
443 .encap
= XFRM_MODE_TUNNEL
,
444 .flags
= XFRM_MODE_FLAG_TUNNEL
,
449 static const struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
451 const struct xfrm_mode
*mode
;
453 if (unlikely(encap
>= XFRM_MODE_MAX
))
458 mode
= &xfrm4_mode_map
[encap
];
459 if (mode
->family
== family
)
463 mode
= &xfrm6_mode_map
[encap
];
464 if (mode
->family
== family
)
474 void xfrm_state_free(struct xfrm_state
*x
)
476 kmem_cache_free(xfrm_state_cache
, x
);
478 EXPORT_SYMBOL(xfrm_state_free
);
480 static void ___xfrm_state_destroy(struct xfrm_state
*x
)
482 hrtimer_cancel(&x
->mtimer
);
483 del_timer_sync(&x
->rtimer
);
490 kfree(x
->replay_esn
);
491 kfree(x
->preplay_esn
);
493 xfrm_put_type_offload(x
->type_offload
);
495 x
->type
->destructor(x
);
496 xfrm_put_type(x
->type
);
499 put_page(x
->xfrag
.page
);
500 xfrm_dev_state_free(x
);
501 security_xfrm_state_free(x
);
505 static void xfrm_state_gc_task(struct work_struct
*work
)
507 struct xfrm_state
*x
;
508 struct hlist_node
*tmp
;
509 struct hlist_head gc_list
;
511 spin_lock_bh(&xfrm_state_gc_lock
);
512 hlist_move_list(&xfrm_state_gc_list
, &gc_list
);
513 spin_unlock_bh(&xfrm_state_gc_lock
);
517 hlist_for_each_entry_safe(x
, tmp
, &gc_list
, gclist
)
518 ___xfrm_state_destroy(x
);
521 static enum hrtimer_restart
xfrm_timer_handler(struct hrtimer
*me
)
523 struct xfrm_state
*x
= container_of(me
, struct xfrm_state
, mtimer
);
524 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
525 time64_t now
= ktime_get_real_seconds();
526 time64_t next
= TIME64_MAX
;
531 if (x
->km
.state
== XFRM_STATE_DEAD
)
533 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
535 if (x
->lft
.hard_add_expires_seconds
) {
536 long tmo
= x
->lft
.hard_add_expires_seconds
+
537 x
->curlft
.add_time
- now
;
539 if (x
->xflags
& XFRM_SOFT_EXPIRE
) {
540 /* enter hard expire without soft expire first?!
541 * setting a new date could trigger this.
542 * workaround: fix x->curflt.add_time by below:
544 x
->curlft
.add_time
= now
- x
->saved_tmo
- 1;
545 tmo
= x
->lft
.hard_add_expires_seconds
- x
->saved_tmo
;
552 if (x
->lft
.hard_use_expires_seconds
) {
553 long tmo
= x
->lft
.hard_use_expires_seconds
+
554 (x
->curlft
.use_time
? : now
) - now
;
562 if (x
->lft
.soft_add_expires_seconds
) {
563 long tmo
= x
->lft
.soft_add_expires_seconds
+
564 x
->curlft
.add_time
- now
;
567 x
->xflags
&= ~XFRM_SOFT_EXPIRE
;
568 } else if (tmo
< next
) {
570 x
->xflags
|= XFRM_SOFT_EXPIRE
;
574 if (x
->lft
.soft_use_expires_seconds
) {
575 long tmo
= x
->lft
.soft_use_expires_seconds
+
576 (x
->curlft
.use_time
? : now
) - now
;
585 km_state_expired(x
, 0, 0);
587 if (next
!= TIME64_MAX
) {
588 hrtimer_forward_now(&x
->mtimer
, ktime_set(next
, 0));
589 ret
= HRTIMER_RESTART
;
595 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0)
596 x
->km
.state
= XFRM_STATE_EXPIRED
;
598 err
= __xfrm_state_delete(x
);
600 km_state_expired(x
, 1, 0);
602 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
605 spin_unlock(&x
->lock
);
609 static void xfrm_replay_timer_handler(struct timer_list
*t
);
611 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
613 struct xfrm_state
*x
;
615 x
= kmem_cache_alloc(xfrm_state_cache
, GFP_ATOMIC
| __GFP_ZERO
);
618 write_pnet(&x
->xs_net
, net
);
619 refcount_set(&x
->refcnt
, 1);
620 atomic_set(&x
->tunnel_users
, 0);
621 INIT_LIST_HEAD(&x
->km
.all
);
622 INIT_HLIST_NODE(&x
->bydst
);
623 INIT_HLIST_NODE(&x
->bysrc
);
624 INIT_HLIST_NODE(&x
->byspi
);
625 hrtimer_init(&x
->mtimer
, CLOCK_BOOTTIME
, HRTIMER_MODE_ABS_SOFT
);
626 x
->mtimer
.function
= xfrm_timer_handler
;
627 timer_setup(&x
->rtimer
, xfrm_replay_timer_handler
, 0);
628 x
->curlft
.add_time
= ktime_get_real_seconds();
629 x
->lft
.soft_byte_limit
= XFRM_INF
;
630 x
->lft
.soft_packet_limit
= XFRM_INF
;
631 x
->lft
.hard_byte_limit
= XFRM_INF
;
632 x
->lft
.hard_packet_limit
= XFRM_INF
;
633 x
->replay_maxage
= 0;
634 x
->replay_maxdiff
= 0;
635 spin_lock_init(&x
->lock
);
639 EXPORT_SYMBOL(xfrm_state_alloc
);
641 void __xfrm_state_destroy(struct xfrm_state
*x
, bool sync
)
643 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
647 ___xfrm_state_destroy(x
);
649 spin_lock_bh(&xfrm_state_gc_lock
);
650 hlist_add_head(&x
->gclist
, &xfrm_state_gc_list
);
651 spin_unlock_bh(&xfrm_state_gc_lock
);
652 schedule_work(&xfrm_state_gc_work
);
655 EXPORT_SYMBOL(__xfrm_state_destroy
);
657 int __xfrm_state_delete(struct xfrm_state
*x
)
659 struct net
*net
= xs_net(x
);
662 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
663 x
->km
.state
= XFRM_STATE_DEAD
;
664 spin_lock(&net
->xfrm
.xfrm_state_lock
);
665 list_del(&x
->km
.all
);
666 hlist_del_rcu(&x
->bydst
);
667 hlist_del_rcu(&x
->bysrc
);
669 hlist_del_rcu(&x
->byspi
);
670 net
->xfrm
.state_num
--;
671 spin_unlock(&net
->xfrm
.xfrm_state_lock
);
673 xfrm_dev_state_delete(x
);
675 /* All xfrm_state objects are created by xfrm_state_alloc.
676 * The xfrm_state_alloc call gives a reference, and that
677 * is what we are dropping here.
685 EXPORT_SYMBOL(__xfrm_state_delete
);
687 int xfrm_state_delete(struct xfrm_state
*x
)
691 spin_lock_bh(&x
->lock
);
692 err
= __xfrm_state_delete(x
);
693 spin_unlock_bh(&x
->lock
);
697 EXPORT_SYMBOL(xfrm_state_delete
);
699 #ifdef CONFIG_SECURITY_NETWORK_XFRM
701 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
705 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
706 struct xfrm_state
*x
;
708 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
709 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
710 (err
= security_xfrm_state_delete(x
)) != 0) {
711 xfrm_audit_state_delete(x
, 0, task_valid
);
721 xfrm_dev_state_flush_secctx_check(struct net
*net
, struct net_device
*dev
, bool task_valid
)
725 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
726 struct xfrm_state
*x
;
727 struct xfrm_state_offload
*xso
;
729 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
732 if (xso
->dev
== dev
&&
733 (err
= security_xfrm_state_delete(x
)) != 0) {
734 xfrm_audit_state_delete(x
, 0, task_valid
);
744 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
750 xfrm_dev_state_flush_secctx_check(struct net
*net
, struct net_device
*dev
, bool task_valid
)
756 int xfrm_state_flush(struct net
*net
, u8 proto
, bool task_valid
, bool sync
)
758 int i
, err
= 0, cnt
= 0;
760 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
761 err
= xfrm_state_flush_secctx_check(net
, proto
, task_valid
);
766 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
767 struct xfrm_state
*x
;
769 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
770 if (!xfrm_state_kern(x
) &&
771 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
773 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
775 err
= xfrm_state_delete(x
);
776 xfrm_audit_state_delete(x
, err
? 0 : 1,
779 xfrm_state_put_sync(x
);
785 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
791 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
797 EXPORT_SYMBOL(xfrm_state_flush
);
799 int xfrm_dev_state_flush(struct net
*net
, struct net_device
*dev
, bool task_valid
)
801 int i
, err
= 0, cnt
= 0;
803 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
804 err
= xfrm_dev_state_flush_secctx_check(net
, dev
, task_valid
);
809 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
810 struct xfrm_state
*x
;
811 struct xfrm_state_offload
*xso
;
813 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
816 if (!xfrm_state_kern(x
) && xso
->dev
== dev
) {
818 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
820 err
= xfrm_state_delete(x
);
821 xfrm_audit_state_delete(x
, err
? 0 : 1,
827 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
836 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
839 EXPORT_SYMBOL(xfrm_dev_state_flush
);
841 void xfrm_sad_getinfo(struct net
*net
, struct xfrmk_sadinfo
*si
)
843 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
844 si
->sadcnt
= net
->xfrm
.state_num
;
845 si
->sadhcnt
= net
->xfrm
.state_hmask
+ 1;
846 si
->sadhmcnt
= xfrm_state_hashmax
;
847 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
849 EXPORT_SYMBOL(xfrm_sad_getinfo
);
852 __xfrm4_init_tempsel(struct xfrm_selector
*sel
, const struct flowi
*fl
)
854 const struct flowi4
*fl4
= &fl
->u
.ip4
;
856 sel
->daddr
.a4
= fl4
->daddr
;
857 sel
->saddr
.a4
= fl4
->saddr
;
858 sel
->dport
= xfrm_flowi_dport(fl
, &fl4
->uli
);
859 sel
->dport_mask
= htons(0xffff);
860 sel
->sport
= xfrm_flowi_sport(fl
, &fl4
->uli
);
861 sel
->sport_mask
= htons(0xffff);
862 sel
->family
= AF_INET
;
863 sel
->prefixlen_d
= 32;
864 sel
->prefixlen_s
= 32;
865 sel
->proto
= fl4
->flowi4_proto
;
866 sel
->ifindex
= fl4
->flowi4_oif
;
870 __xfrm6_init_tempsel(struct xfrm_selector
*sel
, const struct flowi
*fl
)
872 const struct flowi6
*fl6
= &fl
->u
.ip6
;
874 /* Initialize temporary selector matching only to current session. */
875 *(struct in6_addr
*)&sel
->daddr
= fl6
->daddr
;
876 *(struct in6_addr
*)&sel
->saddr
= fl6
->saddr
;
877 sel
->dport
= xfrm_flowi_dport(fl
, &fl6
->uli
);
878 sel
->dport_mask
= htons(0xffff);
879 sel
->sport
= xfrm_flowi_sport(fl
, &fl6
->uli
);
880 sel
->sport_mask
= htons(0xffff);
881 sel
->family
= AF_INET6
;
882 sel
->prefixlen_d
= 128;
883 sel
->prefixlen_s
= 128;
884 sel
->proto
= fl6
->flowi6_proto
;
885 sel
->ifindex
= fl6
->flowi6_oif
;
889 xfrm_init_tempstate(struct xfrm_state
*x
, const struct flowi
*fl
,
890 const struct xfrm_tmpl
*tmpl
,
891 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
892 unsigned short family
)
896 __xfrm4_init_tempsel(&x
->sel
, fl
);
899 __xfrm6_init_tempsel(&x
->sel
, fl
);
905 switch (tmpl
->encap_family
) {
907 if (x
->id
.daddr
.a4
== 0)
908 x
->id
.daddr
.a4
= daddr
->a4
;
909 x
->props
.saddr
= tmpl
->saddr
;
910 if (x
->props
.saddr
.a4
== 0)
911 x
->props
.saddr
.a4
= saddr
->a4
;
914 if (ipv6_addr_any((struct in6_addr
*)&x
->id
.daddr
))
915 memcpy(&x
->id
.daddr
, daddr
, sizeof(x
->sel
.daddr
));
916 memcpy(&x
->props
.saddr
, &tmpl
->saddr
, sizeof(x
->props
.saddr
));
917 if (ipv6_addr_any((struct in6_addr
*)&x
->props
.saddr
))
918 memcpy(&x
->props
.saddr
, saddr
, sizeof(x
->props
.saddr
));
922 x
->props
.mode
= tmpl
->mode
;
923 x
->props
.reqid
= tmpl
->reqid
;
924 x
->props
.family
= tmpl
->encap_family
;
927 static struct xfrm_state
*__xfrm_state_lookup(struct net
*net
, u32 mark
,
928 const xfrm_address_t
*daddr
,
929 __be32 spi
, u8 proto
,
930 unsigned short family
)
932 unsigned int h
= xfrm_spi_hash(net
, daddr
, spi
, proto
, family
);
933 struct xfrm_state
*x
;
935 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_byspi
+ h
, byspi
) {
936 if (x
->props
.family
!= family
||
938 x
->id
.proto
!= proto
||
939 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
))
942 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
944 if (!xfrm_state_hold_rcu(x
))
952 static struct xfrm_state
*__xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
953 const xfrm_address_t
*daddr
,
954 const xfrm_address_t
*saddr
,
955 u8 proto
, unsigned short family
)
957 unsigned int h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
958 struct xfrm_state
*x
;
960 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bysrc
+ h
, bysrc
) {
961 if (x
->props
.family
!= family
||
962 x
->id
.proto
!= proto
||
963 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
964 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
967 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
969 if (!xfrm_state_hold_rcu(x
))
977 static inline struct xfrm_state
*
978 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
980 struct net
*net
= xs_net(x
);
981 u32 mark
= x
->mark
.v
& x
->mark
.m
;
984 return __xfrm_state_lookup(net
, mark
, &x
->id
.daddr
,
985 x
->id
.spi
, x
->id
.proto
, family
);
987 return __xfrm_state_lookup_byaddr(net
, mark
,
990 x
->id
.proto
, family
);
993 static void xfrm_hash_grow_check(struct net
*net
, int have_hash_collision
)
995 if (have_hash_collision
&&
996 (net
->xfrm
.state_hmask
+ 1) < xfrm_state_hashmax
&&
997 net
->xfrm
.state_num
> net
->xfrm
.state_hmask
)
998 schedule_work(&net
->xfrm
.state_hash_work
);
1001 static void xfrm_state_look_at(struct xfrm_policy
*pol
, struct xfrm_state
*x
,
1002 const struct flowi
*fl
, unsigned short family
,
1003 struct xfrm_state
**best
, int *acq_in_progress
,
1006 /* Resolution logic:
1007 * 1. There is a valid state with matching selector. Done.
1008 * 2. Valid state with inappropriate selector. Skip.
1010 * Entering area of "sysdeps".
1012 * 3. If state is not valid, selector is temporary, it selects
1013 * only session which triggered previous resolution. Key
1014 * manager will do something to install a state with proper
1017 if (x
->km
.state
== XFRM_STATE_VALID
) {
1018 if ((x
->sel
.family
&&
1019 !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
1020 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
1024 (*best
)->km
.dying
> x
->km
.dying
||
1025 ((*best
)->km
.dying
== x
->km
.dying
&&
1026 (*best
)->curlft
.add_time
< x
->curlft
.add_time
))
1028 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
1029 *acq_in_progress
= 1;
1030 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
1031 x
->km
.state
== XFRM_STATE_EXPIRED
) {
1032 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
1033 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
1039 xfrm_state_find(const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
1040 const struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
1041 struct xfrm_policy
*pol
, int *err
,
1042 unsigned short family
, u32 if_id
)
1044 static xfrm_address_t saddr_wildcard
= { };
1045 struct net
*net
= xp_net(pol
);
1046 unsigned int h
, h_wildcard
;
1047 struct xfrm_state
*x
, *x0
, *to_put
;
1048 int acquire_in_progress
= 0;
1050 struct xfrm_state
*best
= NULL
;
1051 u32 mark
= pol
->mark
.v
& pol
->mark
.m
;
1052 unsigned short encap_family
= tmpl
->encap_family
;
1053 unsigned int sequence
;
1058 sequence
= read_seqcount_begin(&xfrm_state_hash_generation
);
1061 h
= xfrm_dst_hash(net
, daddr
, saddr
, tmpl
->reqid
, encap_family
);
1062 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bydst
+ h
, bydst
) {
1063 if (x
->props
.family
== encap_family
&&
1064 x
->props
.reqid
== tmpl
->reqid
&&
1065 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1066 x
->if_id
== if_id
&&
1067 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
1068 xfrm_state_addr_check(x
, daddr
, saddr
, encap_family
) &&
1069 tmpl
->mode
== x
->props
.mode
&&
1070 tmpl
->id
.proto
== x
->id
.proto
&&
1071 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
1072 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
1073 &best
, &acquire_in_progress
, &error
);
1075 if (best
|| acquire_in_progress
)
1078 h_wildcard
= xfrm_dst_hash(net
, daddr
, &saddr_wildcard
, tmpl
->reqid
, encap_family
);
1079 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bydst
+ h_wildcard
, bydst
) {
1080 if (x
->props
.family
== encap_family
&&
1081 x
->props
.reqid
== tmpl
->reqid
&&
1082 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1083 x
->if_id
== if_id
&&
1084 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
1085 xfrm_addr_equal(&x
->id
.daddr
, daddr
, encap_family
) &&
1086 tmpl
->mode
== x
->props
.mode
&&
1087 tmpl
->id
.proto
== x
->id
.proto
&&
1088 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
1089 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
1090 &best
, &acquire_in_progress
, &error
);
1095 if (!x
&& !error
&& !acquire_in_progress
) {
1097 (x0
= __xfrm_state_lookup(net
, mark
, daddr
, tmpl
->id
.spi
,
1098 tmpl
->id
.proto
, encap_family
)) != NULL
) {
1105 /* If the KMs have no listeners (yet...), avoid allocating an SA
1106 * for each and every packet - garbage collection might not
1109 if (!km_is_alive(&c
)) {
1114 x
= xfrm_state_alloc(net
);
1119 /* Initialize temporary state matching only
1120 * to current session. */
1121 xfrm_init_tempstate(x
, fl
, tmpl
, daddr
, saddr
, family
);
1122 memcpy(&x
->mark
, &pol
->mark
, sizeof(x
->mark
));
1125 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->flowi_secid
);
1127 x
->km
.state
= XFRM_STATE_DEAD
;
1133 if (km_query(x
, tmpl
, pol
) == 0) {
1134 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1135 x
->km
.state
= XFRM_STATE_ACQ
;
1136 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1137 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1138 h
= xfrm_src_hash(net
, daddr
, saddr
, encap_family
);
1139 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1141 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, encap_family
);
1142 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1144 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1145 hrtimer_start(&x
->mtimer
,
1146 ktime_set(net
->xfrm
.sysctl_acq_expires
, 0),
1147 HRTIMER_MODE_REL_SOFT
);
1148 net
->xfrm
.state_num
++;
1149 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1150 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1152 x
->km
.state
= XFRM_STATE_DEAD
;
1160 if (!xfrm_state_hold_rcu(x
)) {
1165 *err
= acquire_in_progress
? -EAGAIN
: error
;
1169 xfrm_state_put(to_put
);
1171 if (read_seqcount_retry(&xfrm_state_hash_generation
, sequence
)) {
1183 xfrm_stateonly_find(struct net
*net
, u32 mark
, u32 if_id
,
1184 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1185 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
1188 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
1190 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1191 h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1192 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1193 if (x
->props
.family
== family
&&
1194 x
->props
.reqid
== reqid
&&
1195 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1196 x
->if_id
== if_id
&&
1197 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
1198 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
1199 mode
== x
->props
.mode
&&
1200 proto
== x
->id
.proto
&&
1201 x
->km
.state
== XFRM_STATE_VALID
) {
1208 xfrm_state_hold(rx
);
1209 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1214 EXPORT_SYMBOL(xfrm_stateonly_find
);
1216 struct xfrm_state
*xfrm_state_lookup_byspi(struct net
*net
, __be32 spi
,
1217 unsigned short family
)
1219 struct xfrm_state
*x
;
1220 struct xfrm_state_walk
*w
;
1222 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1223 list_for_each_entry(w
, &net
->xfrm
.state_all
, all
) {
1224 x
= container_of(w
, struct xfrm_state
, km
);
1225 if (x
->props
.family
!= family
||
1230 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1233 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1236 EXPORT_SYMBOL(xfrm_state_lookup_byspi
);
1238 static void __xfrm_state_insert(struct xfrm_state
*x
)
1240 struct net
*net
= xs_net(x
);
1243 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1245 h
= xfrm_dst_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
,
1246 x
->props
.reqid
, x
->props
.family
);
1247 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1249 h
= xfrm_src_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
1250 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1253 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
1256 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1259 hrtimer_start(&x
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT
);
1260 if (x
->replay_maxage
)
1261 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
1263 net
->xfrm
.state_num
++;
1265 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1268 /* net->xfrm.xfrm_state_lock is held */
1269 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
1271 struct net
*net
= xs_net(xnew
);
1272 unsigned short family
= xnew
->props
.family
;
1273 u32 reqid
= xnew
->props
.reqid
;
1274 struct xfrm_state
*x
;
1276 u32 mark
= xnew
->mark
.v
& xnew
->mark
.m
;
1277 u32 if_id
= xnew
->if_id
;
1279 h
= xfrm_dst_hash(net
, &xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
1280 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1281 if (x
->props
.family
== family
&&
1282 x
->props
.reqid
== reqid
&&
1283 x
->if_id
== if_id
&&
1284 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1285 xfrm_addr_equal(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
1286 xfrm_addr_equal(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
1291 void xfrm_state_insert(struct xfrm_state
*x
)
1293 struct net
*net
= xs_net(x
);
1295 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1296 __xfrm_state_bump_genids(x
);
1297 __xfrm_state_insert(x
);
1298 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1300 EXPORT_SYMBOL(xfrm_state_insert
);
1302 /* net->xfrm.xfrm_state_lock is held */
1303 static struct xfrm_state
*__find_acq_core(struct net
*net
,
1304 const struct xfrm_mark
*m
,
1305 unsigned short family
, u8 mode
,
1306 u32 reqid
, u32 if_id
, u8 proto
,
1307 const xfrm_address_t
*daddr
,
1308 const xfrm_address_t
*saddr
,
1311 unsigned int h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1312 struct xfrm_state
*x
;
1313 u32 mark
= m
->v
& m
->m
;
1315 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1316 if (x
->props
.reqid
!= reqid
||
1317 x
->props
.mode
!= mode
||
1318 x
->props
.family
!= family
||
1319 x
->km
.state
!= XFRM_STATE_ACQ
||
1321 x
->id
.proto
!= proto
||
1322 (mark
& x
->mark
.m
) != x
->mark
.v
||
1323 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
1324 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
1334 x
= xfrm_state_alloc(net
);
1338 x
->sel
.daddr
.a4
= daddr
->a4
;
1339 x
->sel
.saddr
.a4
= saddr
->a4
;
1340 x
->sel
.prefixlen_d
= 32;
1341 x
->sel
.prefixlen_s
= 32;
1342 x
->props
.saddr
.a4
= saddr
->a4
;
1343 x
->id
.daddr
.a4
= daddr
->a4
;
1347 x
->sel
.daddr
.in6
= daddr
->in6
;
1348 x
->sel
.saddr
.in6
= saddr
->in6
;
1349 x
->sel
.prefixlen_d
= 128;
1350 x
->sel
.prefixlen_s
= 128;
1351 x
->props
.saddr
.in6
= saddr
->in6
;
1352 x
->id
.daddr
.in6
= daddr
->in6
;
1356 x
->km
.state
= XFRM_STATE_ACQ
;
1357 x
->id
.proto
= proto
;
1358 x
->props
.family
= family
;
1359 x
->props
.mode
= mode
;
1360 x
->props
.reqid
= reqid
;
1364 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1366 hrtimer_start(&x
->mtimer
,
1367 ktime_set(net
->xfrm
.sysctl_acq_expires
, 0),
1368 HRTIMER_MODE_REL_SOFT
);
1369 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1370 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1371 h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
1372 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1374 net
->xfrm
.state_num
++;
1376 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1382 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
);
1384 int xfrm_state_add(struct xfrm_state
*x
)
1386 struct net
*net
= xs_net(x
);
1387 struct xfrm_state
*x1
, *to_put
;
1390 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1391 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1393 family
= x
->props
.family
;
1397 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1399 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1407 if (use_spi
&& x
->km
.seq
) {
1408 x1
= __xfrm_find_acq_byseq(net
, mark
, x
->km
.seq
);
1409 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1410 !xfrm_addr_equal(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1417 x1
= __find_acq_core(net
, &x
->mark
, family
, x
->props
.mode
,
1418 x
->props
.reqid
, x
->if_id
, x
->id
.proto
,
1419 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1421 __xfrm_state_bump_genids(x
);
1422 __xfrm_state_insert(x
);
1426 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1429 xfrm_state_delete(x1
);
1434 xfrm_state_put(to_put
);
1438 EXPORT_SYMBOL(xfrm_state_add
);
1440 #ifdef CONFIG_XFRM_MIGRATE
1441 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
,
1442 struct xfrm_encap_tmpl
*encap
)
1444 struct net
*net
= xs_net(orig
);
1445 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1449 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1450 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1451 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1452 x
->props
.mode
= orig
->props
.mode
;
1453 x
->props
.replay_window
= orig
->props
.replay_window
;
1454 x
->props
.reqid
= orig
->props
.reqid
;
1455 x
->props
.family
= orig
->props
.family
;
1456 x
->props
.saddr
= orig
->props
.saddr
;
1459 x
->aalg
= xfrm_algo_auth_clone(orig
->aalg
);
1463 x
->props
.aalgo
= orig
->props
.aalgo
;
1466 x
->aead
= xfrm_algo_aead_clone(orig
->aead
);
1467 x
->geniv
= orig
->geniv
;
1472 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1476 x
->props
.ealgo
= orig
->props
.ealgo
;
1479 x
->calg
= xfrm_algo_clone(orig
->calg
);
1483 x
->props
.calgo
= orig
->props
.calgo
;
1485 if (encap
|| orig
->encap
) {
1487 x
->encap
= kmemdup(encap
, sizeof(*x
->encap
),
1490 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
),
1498 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1504 if (orig
->replay_esn
) {
1505 if (xfrm_replay_clone(x
, orig
))
1509 memcpy(&x
->mark
, &orig
->mark
, sizeof(x
->mark
));
1511 if (xfrm_init_state(x
) < 0)
1514 x
->props
.flags
= orig
->props
.flags
;
1515 x
->props
.extra_flags
= orig
->props
.extra_flags
;
1517 x
->if_id
= orig
->if_id
;
1518 x
->tfcpad
= orig
->tfcpad
;
1519 x
->replay_maxdiff
= orig
->replay_maxdiff
;
1520 x
->replay_maxage
= orig
->replay_maxage
;
1521 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1522 x
->km
.state
= orig
->km
.state
;
1523 x
->km
.seq
= orig
->km
.seq
;
1524 x
->replay
= orig
->replay
;
1525 x
->preplay
= orig
->preplay
;
1535 struct xfrm_state
*xfrm_migrate_state_find(struct xfrm_migrate
*m
, struct net
*net
)
1538 struct xfrm_state
*x
= NULL
;
1540 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1543 h
= xfrm_dst_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1544 m
->reqid
, m
->old_family
);
1545 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1546 if (x
->props
.mode
!= m
->mode
||
1547 x
->id
.proto
!= m
->proto
)
1549 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1551 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1553 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1560 h
= xfrm_src_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1562 hlist_for_each_entry(x
, net
->xfrm
.state_bysrc
+h
, bysrc
) {
1563 if (x
->props
.mode
!= m
->mode
||
1564 x
->id
.proto
!= m
->proto
)
1566 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1568 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1576 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1580 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1582 struct xfrm_state
*xfrm_state_migrate(struct xfrm_state
*x
,
1583 struct xfrm_migrate
*m
,
1584 struct xfrm_encap_tmpl
*encap
)
1586 struct xfrm_state
*xc
;
1588 xc
= xfrm_state_clone(x
, encap
);
1592 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1593 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1596 if (xfrm_addr_equal(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1597 /* a care is needed when the destination address of the
1598 state is to be updated as it is a part of triplet */
1599 xfrm_state_insert(xc
);
1601 if (xfrm_state_add(xc
) < 0)
1610 EXPORT_SYMBOL(xfrm_state_migrate
);
1613 int xfrm_state_update(struct xfrm_state
*x
)
1615 struct xfrm_state
*x1
, *to_put
;
1617 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1618 struct net
*net
= xs_net(x
);
1622 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1623 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1629 if (xfrm_state_kern(x1
)) {
1635 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1636 __xfrm_state_insert(x
);
1642 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1645 xfrm_state_put(to_put
);
1651 xfrm_state_delete(x1
);
1657 spin_lock_bh(&x1
->lock
);
1658 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1659 if (x
->encap
&& x1
->encap
&&
1660 x
->encap
->encap_type
== x1
->encap
->encap_type
)
1661 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1662 else if (x
->encap
|| x1
->encap
)
1665 if (x
->coaddr
&& x1
->coaddr
) {
1666 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1668 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1669 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1670 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1673 hrtimer_start(&x1
->mtimer
, ktime_set(1, 0),
1674 HRTIMER_MODE_REL_SOFT
);
1675 if (x1
->curlft
.use_time
)
1676 xfrm_state_check_expire(x1
);
1678 if (x
->props
.smark
.m
|| x
->props
.smark
.v
|| x
->if_id
) {
1679 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1681 if (x
->props
.smark
.m
|| x
->props
.smark
.v
)
1682 x1
->props
.smark
= x
->props
.smark
;
1685 x1
->if_id
= x
->if_id
;
1687 __xfrm_state_bump_genids(x1
);
1688 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1692 x
->km
.state
= XFRM_STATE_DEAD
;
1693 __xfrm_state_put(x
);
1697 spin_unlock_bh(&x1
->lock
);
1703 EXPORT_SYMBOL(xfrm_state_update
);
1705 int xfrm_state_check_expire(struct xfrm_state
*x
)
1707 if (!x
->curlft
.use_time
)
1708 x
->curlft
.use_time
= ktime_get_real_seconds();
1710 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1711 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1712 x
->km
.state
= XFRM_STATE_EXPIRED
;
1713 hrtimer_start(&x
->mtimer
, 0, HRTIMER_MODE_REL_SOFT
);
1718 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1719 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1721 km_state_expired(x
, 0, 0);
1725 EXPORT_SYMBOL(xfrm_state_check_expire
);
1728 xfrm_state_lookup(struct net
*net
, u32 mark
, const xfrm_address_t
*daddr
, __be32 spi
,
1729 u8 proto
, unsigned short family
)
1731 struct xfrm_state
*x
;
1734 x
= __xfrm_state_lookup(net
, mark
, daddr
, spi
, proto
, family
);
1738 EXPORT_SYMBOL(xfrm_state_lookup
);
1741 xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
1742 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
1743 u8 proto
, unsigned short family
)
1745 struct xfrm_state
*x
;
1747 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1748 x
= __xfrm_state_lookup_byaddr(net
, mark
, daddr
, saddr
, proto
, family
);
1749 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1752 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1755 xfrm_find_acq(struct net
*net
, const struct xfrm_mark
*mark
, u8 mode
, u32 reqid
,
1756 u32 if_id
, u8 proto
, const xfrm_address_t
*daddr
,
1757 const xfrm_address_t
*saddr
, int create
, unsigned short family
)
1759 struct xfrm_state
*x
;
1761 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1762 x
= __find_acq_core(net
, mark
, family
, mode
, reqid
, if_id
, proto
, daddr
, saddr
, create
);
1763 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1767 EXPORT_SYMBOL(xfrm_find_acq
);
1769 #ifdef CONFIG_XFRM_SUB_POLICY
1770 #if IS_ENABLED(CONFIG_IPV6)
1771 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
1773 __xfrm6_sort(void **dst
, void **src
, int n
,
1774 int (*cmp
)(const void *p
), int maxclass
)
1776 int count
[XFRM_MAX_DEPTH
] = { };
1777 int class[XFRM_MAX_DEPTH
];
1780 for (i
= 0; i
< n
; i
++) {
1781 int c
= cmp(src
[i
]);
1787 for (i
= 2; i
< maxclass
; i
++)
1788 count
[i
] += count
[i
- 1];
1790 for (i
= 0; i
< n
; i
++) {
1791 dst
[count
[class[i
] - 1]++] = src
[i
];
1796 /* Rule for xfrm_state:
1798 * rule 1: select IPsec transport except AH
1799 * rule 2: select MIPv6 RO or inbound trigger
1800 * rule 3: select IPsec transport AH
1801 * rule 4: select IPsec tunnel
1804 static int __xfrm6_state_sort_cmp(const void *p
)
1806 const struct xfrm_state
*v
= p
;
1808 switch (v
->props
.mode
) {
1809 case XFRM_MODE_TRANSPORT
:
1810 if (v
->id
.proto
!= IPPROTO_AH
)
1814 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1815 case XFRM_MODE_ROUTEOPTIMIZATION
:
1816 case XFRM_MODE_IN_TRIGGER
:
1819 case XFRM_MODE_TUNNEL
:
1820 case XFRM_MODE_BEET
:
1826 /* Rule for xfrm_tmpl:
1828 * rule 1: select IPsec transport
1829 * rule 2: select MIPv6 RO or inbound trigger
1830 * rule 3: select IPsec tunnel
1833 static int __xfrm6_tmpl_sort_cmp(const void *p
)
1835 const struct xfrm_tmpl
*v
= p
;
1838 case XFRM_MODE_TRANSPORT
:
1840 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1841 case XFRM_MODE_ROUTEOPTIMIZATION
:
1842 case XFRM_MODE_IN_TRIGGER
:
1845 case XFRM_MODE_TUNNEL
:
1846 case XFRM_MODE_BEET
:
1852 static inline int __xfrm6_state_sort_cmp(const void *p
) { return 5; }
1853 static inline int __xfrm6_tmpl_sort_cmp(const void *p
) { return 4; }
1856 __xfrm6_sort(void **dst
, void **src
, int n
,
1857 int (*cmp
)(const void *p
), int maxclass
)
1861 for (i
= 0; i
< n
; i
++)
1864 #endif /* CONFIG_IPV6 */
1867 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1868 unsigned short family
)
1872 if (family
== AF_INET6
)
1873 __xfrm6_sort((void **)dst
, (void **)src
, n
,
1874 __xfrm6_tmpl_sort_cmp
, 5);
1876 for (i
= 0; i
< n
; i
++)
1881 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1882 unsigned short family
)
1886 if (family
== AF_INET6
)
1887 __xfrm6_sort((void **)dst
, (void **)src
, n
,
1888 __xfrm6_state_sort_cmp
, 6);
1890 for (i
= 0; i
< n
; i
++)
1895 /* Silly enough, but I'm lazy to build resolution list */
1897 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1901 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
1902 struct xfrm_state
*x
;
1904 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
1905 if (x
->km
.seq
== seq
&&
1906 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1907 x
->km
.state
== XFRM_STATE_ACQ
) {
1916 struct xfrm_state
*xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1918 struct xfrm_state
*x
;
1920 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1921 x
= __xfrm_find_acq_byseq(net
, mark
, seq
);
1922 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1925 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1927 u32
xfrm_get_acqseq(void)
1930 static atomic_t acqseq
;
1933 res
= atomic_inc_return(&acqseq
);
1938 EXPORT_SYMBOL(xfrm_get_acqseq
);
1940 int verify_spi_info(u8 proto
, u32 min
, u32 max
)
1948 /* IPCOMP spi is 16-bits. */
1962 EXPORT_SYMBOL(verify_spi_info
);
1964 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1966 struct net
*net
= xs_net(x
);
1968 struct xfrm_state
*x0
;
1970 __be32 minspi
= htonl(low
);
1971 __be32 maxspi
= htonl(high
);
1972 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1974 spin_lock_bh(&x
->lock
);
1975 if (x
->km
.state
== XFRM_STATE_DEAD
)
1984 if (minspi
== maxspi
) {
1985 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1993 for (h
= 0; h
< high
-low
+1; h
++) {
1994 spi
= low
+ prandom_u32()%(high
-low
+1);
1995 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1997 x
->id
.spi
= htonl(spi
);
2004 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
2005 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
2006 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
2007 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
2013 spin_unlock_bh(&x
->lock
);
2017 EXPORT_SYMBOL(xfrm_alloc_spi
);
2019 static bool __xfrm_state_filter_match(struct xfrm_state
*x
,
2020 struct xfrm_address_filter
*filter
)
2023 if ((filter
->family
== AF_INET
||
2024 filter
->family
== AF_INET6
) &&
2025 x
->props
.family
!= filter
->family
)
2028 return addr_match(&x
->props
.saddr
, &filter
->saddr
,
2030 addr_match(&x
->id
.daddr
, &filter
->daddr
,
2036 int xfrm_state_walk(struct net
*net
, struct xfrm_state_walk
*walk
,
2037 int (*func
)(struct xfrm_state
*, int, void*),
2040 struct xfrm_state
*state
;
2041 struct xfrm_state_walk
*x
;
2044 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
2047 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
2048 if (list_empty(&walk
->all
))
2049 x
= list_first_entry(&net
->xfrm
.state_all
, struct xfrm_state_walk
, all
);
2051 x
= list_first_entry(&walk
->all
, struct xfrm_state_walk
, all
);
2052 list_for_each_entry_from(x
, &net
->xfrm
.state_all
, all
) {
2053 if (x
->state
== XFRM_STATE_DEAD
)
2055 state
= container_of(x
, struct xfrm_state
, km
);
2056 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
2058 if (!__xfrm_state_filter_match(state
, walk
->filter
))
2060 err
= func(state
, walk
->seq
, data
);
2062 list_move_tail(&walk
->all
, &x
->all
);
2067 if (walk
->seq
== 0) {
2071 list_del_init(&walk
->all
);
2073 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
2076 EXPORT_SYMBOL(xfrm_state_walk
);
2078 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
,
2079 struct xfrm_address_filter
*filter
)
2081 INIT_LIST_HEAD(&walk
->all
);
2082 walk
->proto
= proto
;
2083 walk
->state
= XFRM_STATE_DEAD
;
2085 walk
->filter
= filter
;
2087 EXPORT_SYMBOL(xfrm_state_walk_init
);
2089 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
, struct net
*net
)
2091 kfree(walk
->filter
);
2093 if (list_empty(&walk
->all
))
2096 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
2097 list_del(&walk
->all
);
2098 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
2100 EXPORT_SYMBOL(xfrm_state_walk_done
);
2102 static void xfrm_replay_timer_handler(struct timer_list
*t
)
2104 struct xfrm_state
*x
= from_timer(x
, t
, rtimer
);
2106 spin_lock(&x
->lock
);
2108 if (x
->km
.state
== XFRM_STATE_VALID
) {
2109 if (xfrm_aevent_is_on(xs_net(x
)))
2110 x
->repl
->notify(x
, XFRM_REPLAY_TIMEOUT
);
2112 x
->xflags
|= XFRM_TIME_DEFER
;
2115 spin_unlock(&x
->lock
);
2118 static LIST_HEAD(xfrm_km_list
);
2120 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2122 struct xfrm_mgr
*km
;
2125 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
2126 if (km
->notify_policy
)
2127 km
->notify_policy(xp
, dir
, c
);
2131 void km_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2133 struct xfrm_mgr
*km
;
2135 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
2141 EXPORT_SYMBOL(km_policy_notify
);
2142 EXPORT_SYMBOL(km_state_notify
);
2144 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
)
2150 c
.event
= XFRM_MSG_EXPIRE
;
2151 km_state_notify(x
, &c
);
2154 EXPORT_SYMBOL(km_state_expired
);
2156 * We send to all registered managers regardless of failure
2157 * We are happy with one success
2159 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
2161 int err
= -EINVAL
, acqret
;
2162 struct xfrm_mgr
*km
;
2165 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2166 acqret
= km
->acquire(x
, t
, pol
);
2173 EXPORT_SYMBOL(km_query
);
2175 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
2178 struct xfrm_mgr
*km
;
2181 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2182 if (km
->new_mapping
)
2183 err
= km
->new_mapping(x
, ipaddr
, sport
);
2190 EXPORT_SYMBOL(km_new_mapping
);
2192 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 portid
)
2198 c
.event
= XFRM_MSG_POLEXPIRE
;
2199 km_policy_notify(pol
, dir
, &c
);
2201 EXPORT_SYMBOL(km_policy_expired
);
2203 #ifdef CONFIG_XFRM_MIGRATE
2204 int km_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2205 const struct xfrm_migrate
*m
, int num_migrate
,
2206 const struct xfrm_kmaddress
*k
,
2207 const struct xfrm_encap_tmpl
*encap
)
2211 struct xfrm_mgr
*km
;
2214 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2216 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
,
2225 EXPORT_SYMBOL(km_migrate
);
2228 int km_report(struct net
*net
, u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2232 struct xfrm_mgr
*km
;
2235 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2237 ret
= km
->report(net
, proto
, sel
, addr
);
2245 EXPORT_SYMBOL(km_report
);
2247 static bool km_is_alive(const struct km_event
*c
)
2249 struct xfrm_mgr
*km
;
2250 bool is_alive
= false;
2253 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2254 if (km
->is_alive
&& km
->is_alive(c
)) {
2264 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
2268 struct xfrm_mgr
*km
;
2269 struct xfrm_policy
*pol
= NULL
;
2271 if (in_compat_syscall())
2274 if (!optval
&& !optlen
) {
2275 xfrm_sk_policy_insert(sk
, XFRM_POLICY_IN
, NULL
);
2276 xfrm_sk_policy_insert(sk
, XFRM_POLICY_OUT
, NULL
);
2281 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
2284 data
= memdup_user(optval
, optlen
);
2286 return PTR_ERR(data
);
2290 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2291 pol
= km
->compile_policy(sk
, optname
, data
,
2299 xfrm_sk_policy_insert(sk
, err
, pol
);
2308 EXPORT_SYMBOL(xfrm_user_policy
);
2310 static DEFINE_SPINLOCK(xfrm_km_lock
);
2312 int xfrm_register_km(struct xfrm_mgr
*km
)
2314 spin_lock_bh(&xfrm_km_lock
);
2315 list_add_tail_rcu(&km
->list
, &xfrm_km_list
);
2316 spin_unlock_bh(&xfrm_km_lock
);
2319 EXPORT_SYMBOL(xfrm_register_km
);
2321 int xfrm_unregister_km(struct xfrm_mgr
*km
)
2323 spin_lock_bh(&xfrm_km_lock
);
2324 list_del_rcu(&km
->list
);
2325 spin_unlock_bh(&xfrm_km_lock
);
2329 EXPORT_SYMBOL(xfrm_unregister_km
);
2331 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
2335 if (WARN_ON(afinfo
->family
>= NPROTO
))
2336 return -EAFNOSUPPORT
;
2338 spin_lock_bh(&xfrm_state_afinfo_lock
);
2339 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
2342 rcu_assign_pointer(xfrm_state_afinfo
[afinfo
->family
], afinfo
);
2343 spin_unlock_bh(&xfrm_state_afinfo_lock
);
2346 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
2348 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
2350 int err
= 0, family
= afinfo
->family
;
2352 if (WARN_ON(family
>= NPROTO
))
2353 return -EAFNOSUPPORT
;
2355 spin_lock_bh(&xfrm_state_afinfo_lock
);
2356 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
2357 if (rcu_access_pointer(xfrm_state_afinfo
[family
]) != afinfo
)
2360 RCU_INIT_POINTER(xfrm_state_afinfo
[afinfo
->family
], NULL
);
2362 spin_unlock_bh(&xfrm_state_afinfo_lock
);
2366 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
2368 struct xfrm_state_afinfo
*xfrm_state_afinfo_get_rcu(unsigned int family
)
2370 if (unlikely(family
>= NPROTO
))
2373 return rcu_dereference(xfrm_state_afinfo
[family
]);
2375 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu
);
2377 struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
2379 struct xfrm_state_afinfo
*afinfo
;
2380 if (unlikely(family
>= NPROTO
))
2383 afinfo
= rcu_dereference(xfrm_state_afinfo
[family
]);
2384 if (unlikely(!afinfo
))
2389 void xfrm_flush_gc(void)
2391 flush_work(&xfrm_state_gc_work
);
2393 EXPORT_SYMBOL(xfrm_flush_gc
);
2395 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2396 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
2399 struct xfrm_state
*t
= x
->tunnel
;
2401 if (atomic_read(&t
->tunnel_users
) == 2)
2402 xfrm_state_delete(t
);
2403 atomic_dec(&t
->tunnel_users
);
2404 xfrm_state_put_sync(t
);
2408 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
2410 u32
xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
2412 const struct xfrm_type
*type
= READ_ONCE(x
->type
);
2413 struct crypto_aead
*aead
;
2414 u32 blksize
, net_adj
= 0;
2416 if (x
->km
.state
!= XFRM_STATE_VALID
||
2417 !type
|| type
->proto
!= IPPROTO_ESP
)
2418 return mtu
- x
->props
.header_len
;
2421 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
2423 switch (x
->props
.mode
) {
2424 case XFRM_MODE_TRANSPORT
:
2425 case XFRM_MODE_BEET
:
2426 if (x
->props
.family
== AF_INET
)
2427 net_adj
= sizeof(struct iphdr
);
2428 else if (x
->props
.family
== AF_INET6
)
2429 net_adj
= sizeof(struct ipv6hdr
);
2431 case XFRM_MODE_TUNNEL
:
2438 return ((mtu
- x
->props
.header_len
- crypto_aead_authsize(aead
) -
2439 net_adj
) & ~(blksize
- 1)) + net_adj
- 2;
2441 EXPORT_SYMBOL_GPL(xfrm_state_mtu
);
2443 int __xfrm_init_state(struct xfrm_state
*x
, bool init_replay
, bool offload
)
2445 const struct xfrm_mode
*inner_mode
;
2446 const struct xfrm_mode
*outer_mode
;
2447 int family
= x
->props
.family
;
2450 if (family
== AF_INET
&&
2451 xs_net(x
)->ipv4
.sysctl_ip_no_pmtu_disc
)
2452 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
2454 err
= -EPROTONOSUPPORT
;
2456 if (x
->sel
.family
!= AF_UNSPEC
) {
2457 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
2458 if (inner_mode
== NULL
)
2461 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2462 family
!= x
->sel
.family
)
2465 x
->inner_mode
= *inner_mode
;
2467 const struct xfrm_mode
*inner_mode_iaf
;
2468 int iafamily
= AF_INET
;
2470 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->props
.family
);
2471 if (inner_mode
== NULL
)
2474 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
))
2477 x
->inner_mode
= *inner_mode
;
2479 if (x
->props
.family
== AF_INET
)
2480 iafamily
= AF_INET6
;
2482 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, iafamily
);
2483 if (inner_mode_iaf
) {
2484 if (inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)
2485 x
->inner_mode_iaf
= *inner_mode_iaf
;
2489 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2490 if (x
->type
== NULL
)
2493 x
->type_offload
= xfrm_get_type_offload(x
->id
.proto
, family
, offload
);
2495 err
= x
->type
->init_state(x
);
2499 outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2501 err
= -EPROTONOSUPPORT
;
2505 x
->outer_mode
= *outer_mode
;
2507 err
= xfrm_init_replay(x
);
2516 EXPORT_SYMBOL(__xfrm_init_state
);
2518 int xfrm_init_state(struct xfrm_state
*x
)
2522 err
= __xfrm_init_state(x
, true, false);
2524 x
->km
.state
= XFRM_STATE_VALID
;
2529 EXPORT_SYMBOL(xfrm_init_state
);
2531 int __net_init
xfrm_state_init(struct net
*net
)
2535 if (net_eq(net
, &init_net
))
2536 xfrm_state_cache
= KMEM_CACHE(xfrm_state
,
2537 SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
2539 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2541 sz
= sizeof(struct hlist_head
) * 8;
2543 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2544 if (!net
->xfrm
.state_bydst
)
2546 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2547 if (!net
->xfrm
.state_bysrc
)
2549 net
->xfrm
.state_byspi
= xfrm_hash_alloc(sz
);
2550 if (!net
->xfrm
.state_byspi
)
2552 net
->xfrm
.state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2554 net
->xfrm
.state_num
= 0;
2555 INIT_WORK(&net
->xfrm
.state_hash_work
, xfrm_hash_resize
);
2556 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
2560 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2562 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2567 void xfrm_state_fini(struct net
*net
)
2571 flush_work(&net
->xfrm
.state_hash_work
);
2572 flush_work(&xfrm_state_gc_work
);
2573 xfrm_state_flush(net
, 0, false, true);
2575 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2577 sz
= (net
->xfrm
.state_hmask
+ 1) * sizeof(struct hlist_head
);
2578 WARN_ON(!hlist_empty(net
->xfrm
.state_byspi
));
2579 xfrm_hash_free(net
->xfrm
.state_byspi
, sz
);
2580 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2581 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2582 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2583 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2586 #ifdef CONFIG_AUDITSYSCALL
2587 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2588 struct audit_buffer
*audit_buf
)
2590 struct xfrm_sec_ctx
*ctx
= x
->security
;
2591 u32 spi
= ntohl(x
->id
.spi
);
2594 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2595 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2597 switch (x
->props
.family
) {
2599 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2600 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2603 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2604 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2608 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2611 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2612 struct audit_buffer
*audit_buf
)
2614 const struct iphdr
*iph4
;
2615 const struct ipv6hdr
*iph6
;
2620 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2621 &iph4
->saddr
, &iph4
->daddr
);
2624 iph6
= ipv6_hdr(skb
);
2625 audit_log_format(audit_buf
,
2626 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2627 &iph6
->saddr
, &iph6
->daddr
,
2628 iph6
->flow_lbl
[0] & 0x0f,
2635 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
, bool task_valid
)
2637 struct audit_buffer
*audit_buf
;
2639 audit_buf
= xfrm_audit_start("SAD-add");
2640 if (audit_buf
== NULL
)
2642 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2643 xfrm_audit_helper_sainfo(x
, audit_buf
);
2644 audit_log_format(audit_buf
, " res=%u", result
);
2645 audit_log_end(audit_buf
);
2647 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2649 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
, bool task_valid
)
2651 struct audit_buffer
*audit_buf
;
2653 audit_buf
= xfrm_audit_start("SAD-delete");
2654 if (audit_buf
== NULL
)
2656 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2657 xfrm_audit_helper_sainfo(x
, audit_buf
);
2658 audit_log_format(audit_buf
, " res=%u", result
);
2659 audit_log_end(audit_buf
);
2661 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2663 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2664 struct sk_buff
*skb
)
2666 struct audit_buffer
*audit_buf
;
2669 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2670 if (audit_buf
== NULL
)
2672 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2673 /* don't record the sequence number because it's inherent in this kind
2674 * of audit message */
2675 spi
= ntohl(x
->id
.spi
);
2676 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2677 audit_log_end(audit_buf
);
2679 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2681 void xfrm_audit_state_replay(struct xfrm_state
*x
,
2682 struct sk_buff
*skb
, __be32 net_seq
)
2684 struct audit_buffer
*audit_buf
;
2687 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2688 if (audit_buf
== NULL
)
2690 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2691 spi
= ntohl(x
->id
.spi
);
2692 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2693 spi
, spi
, ntohl(net_seq
));
2694 audit_log_end(audit_buf
);
2696 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay
);
2698 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2700 struct audit_buffer
*audit_buf
;
2702 audit_buf
= xfrm_audit_start("SA-notfound");
2703 if (audit_buf
== NULL
)
2705 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2706 audit_log_end(audit_buf
);
2708 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2710 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2711 __be32 net_spi
, __be32 net_seq
)
2713 struct audit_buffer
*audit_buf
;
2716 audit_buf
= xfrm_audit_start("SA-notfound");
2717 if (audit_buf
== NULL
)
2719 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2720 spi
= ntohl(net_spi
);
2721 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2722 spi
, spi
, ntohl(net_seq
));
2723 audit_log_end(audit_buf
);
2725 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2727 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2728 struct sk_buff
*skb
, u8 proto
)
2730 struct audit_buffer
*audit_buf
;
2734 audit_buf
= xfrm_audit_start("SA-icv-failure");
2735 if (audit_buf
== NULL
)
2737 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2738 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2739 u32 spi
= ntohl(net_spi
);
2740 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2741 spi
, spi
, ntohl(net_seq
));
2743 audit_log_end(audit_buf
);
2745 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2746 #endif /* CONFIG_AUDITSYSCALL */