6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
16 #include <linux/workqueue.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <linux/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
29 #include "xfrm_hash.h"
31 #define xfrm_state_deref_prot(table, net) \
32 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
34 static void xfrm_state_gc_task(struct work_struct
*work
);
36 /* Each xfrm_state may be linked to two tables:
38 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
39 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
40 destination/tunnel endpoint. (output)
43 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
44 static __read_mostly seqcount_t xfrm_state_hash_generation
= SEQCNT_ZERO(xfrm_state_hash_generation
);
45 static struct kmem_cache
*xfrm_state_cache __ro_after_init
;
47 static DECLARE_WORK(xfrm_state_gc_work
, xfrm_state_gc_task
);
48 static HLIST_HEAD(xfrm_state_gc_list
);
50 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu
*x
)
52 return refcount_inc_not_zero(&x
->refcnt
);
55 static inline unsigned int xfrm_dst_hash(struct net
*net
,
56 const xfrm_address_t
*daddr
,
57 const xfrm_address_t
*saddr
,
59 unsigned short family
)
61 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, net
->xfrm
.state_hmask
);
64 static inline unsigned int xfrm_src_hash(struct net
*net
,
65 const xfrm_address_t
*daddr
,
66 const xfrm_address_t
*saddr
,
67 unsigned short family
)
69 return __xfrm_src_hash(daddr
, saddr
, family
, net
->xfrm
.state_hmask
);
72 static inline unsigned int
73 xfrm_spi_hash(struct net
*net
, const xfrm_address_t
*daddr
,
74 __be32 spi
, u8 proto
, unsigned short family
)
76 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, net
->xfrm
.state_hmask
);
79 static void xfrm_hash_transfer(struct hlist_head
*list
,
80 struct hlist_head
*ndsttable
,
81 struct hlist_head
*nsrctable
,
82 struct hlist_head
*nspitable
,
83 unsigned int nhashmask
)
85 struct hlist_node
*tmp
;
88 hlist_for_each_entry_safe(x
, tmp
, list
, bydst
) {
91 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
92 x
->props
.reqid
, x
->props
.family
,
94 hlist_add_head_rcu(&x
->bydst
, ndsttable
+ h
);
96 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
99 hlist_add_head_rcu(&x
->bysrc
, nsrctable
+ h
);
102 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
103 x
->id
.proto
, x
->props
.family
,
105 hlist_add_head_rcu(&x
->byspi
, nspitable
+ h
);
110 static unsigned long xfrm_hash_new_size(unsigned int state_hmask
)
112 return ((state_hmask
+ 1) << 1) * sizeof(struct hlist_head
);
115 static void xfrm_hash_resize(struct work_struct
*work
)
117 struct net
*net
= container_of(work
, struct net
, xfrm
.state_hash_work
);
118 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
119 unsigned long nsize
, osize
;
120 unsigned int nhashmask
, ohashmask
;
123 nsize
= xfrm_hash_new_size(net
->xfrm
.state_hmask
);
124 ndst
= xfrm_hash_alloc(nsize
);
127 nsrc
= xfrm_hash_alloc(nsize
);
129 xfrm_hash_free(ndst
, nsize
);
132 nspi
= xfrm_hash_alloc(nsize
);
134 xfrm_hash_free(ndst
, nsize
);
135 xfrm_hash_free(nsrc
, nsize
);
139 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
140 write_seqcount_begin(&xfrm_state_hash_generation
);
142 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
143 odst
= xfrm_state_deref_prot(net
->xfrm
.state_bydst
, net
);
144 for (i
= net
->xfrm
.state_hmask
; i
>= 0; i
--)
145 xfrm_hash_transfer(odst
+ i
, ndst
, nsrc
, nspi
, nhashmask
);
147 osrc
= xfrm_state_deref_prot(net
->xfrm
.state_bysrc
, net
);
148 ospi
= xfrm_state_deref_prot(net
->xfrm
.state_byspi
, net
);
149 ohashmask
= net
->xfrm
.state_hmask
;
151 rcu_assign_pointer(net
->xfrm
.state_bydst
, ndst
);
152 rcu_assign_pointer(net
->xfrm
.state_bysrc
, nsrc
);
153 rcu_assign_pointer(net
->xfrm
.state_byspi
, nspi
);
154 net
->xfrm
.state_hmask
= nhashmask
;
156 write_seqcount_end(&xfrm_state_hash_generation
);
157 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
159 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
163 xfrm_hash_free(odst
, osize
);
164 xfrm_hash_free(osrc
, osize
);
165 xfrm_hash_free(ospi
, osize
);
168 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock
);
169 static struct xfrm_state_afinfo __rcu
*xfrm_state_afinfo
[NPROTO
];
171 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
173 int __xfrm_state_delete(struct xfrm_state
*x
);
175 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
176 bool km_is_alive(const struct km_event
*c
);
177 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
);
179 static DEFINE_SPINLOCK(xfrm_type_lock
);
180 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
182 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
183 const struct xfrm_type
**typemap
;
186 if (unlikely(afinfo
== NULL
))
187 return -EAFNOSUPPORT
;
188 typemap
= afinfo
->type_map
;
189 spin_lock_bh(&xfrm_type_lock
);
191 if (likely(typemap
[type
->proto
] == NULL
))
192 typemap
[type
->proto
] = type
;
195 spin_unlock_bh(&xfrm_type_lock
);
199 EXPORT_SYMBOL(xfrm_register_type
);
201 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
203 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
204 const struct xfrm_type
**typemap
;
207 if (unlikely(afinfo
== NULL
))
208 return -EAFNOSUPPORT
;
209 typemap
= afinfo
->type_map
;
210 spin_lock_bh(&xfrm_type_lock
);
212 if (unlikely(typemap
[type
->proto
] != type
))
215 typemap
[type
->proto
] = NULL
;
216 spin_unlock_bh(&xfrm_type_lock
);
220 EXPORT_SYMBOL(xfrm_unregister_type
);
222 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
224 struct xfrm_state_afinfo
*afinfo
;
225 const struct xfrm_type
**typemap
;
226 const struct xfrm_type
*type
;
227 int modload_attempted
= 0;
230 afinfo
= xfrm_state_get_afinfo(family
);
231 if (unlikely(afinfo
== NULL
))
233 typemap
= afinfo
->type_map
;
235 type
= READ_ONCE(typemap
[proto
]);
236 if (unlikely(type
&& !try_module_get(type
->owner
)))
241 if (!type
&& !modload_attempted
) {
242 request_module("xfrm-type-%d-%d", family
, proto
);
243 modload_attempted
= 1;
250 static void xfrm_put_type(const struct xfrm_type
*type
)
252 module_put(type
->owner
);
255 static DEFINE_SPINLOCK(xfrm_type_offload_lock
);
256 int xfrm_register_type_offload(const struct xfrm_type_offload
*type
,
257 unsigned short family
)
259 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
260 const struct xfrm_type_offload
**typemap
;
263 if (unlikely(afinfo
== NULL
))
264 return -EAFNOSUPPORT
;
265 typemap
= afinfo
->type_offload_map
;
266 spin_lock_bh(&xfrm_type_offload_lock
);
268 if (likely(typemap
[type
->proto
] == NULL
))
269 typemap
[type
->proto
] = type
;
272 spin_unlock_bh(&xfrm_type_offload_lock
);
276 EXPORT_SYMBOL(xfrm_register_type_offload
);
278 int xfrm_unregister_type_offload(const struct xfrm_type_offload
*type
,
279 unsigned short family
)
281 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
282 const struct xfrm_type_offload
**typemap
;
285 if (unlikely(afinfo
== NULL
))
286 return -EAFNOSUPPORT
;
287 typemap
= afinfo
->type_offload_map
;
288 spin_lock_bh(&xfrm_type_offload_lock
);
290 if (unlikely(typemap
[type
->proto
] != type
))
293 typemap
[type
->proto
] = NULL
;
294 spin_unlock_bh(&xfrm_type_offload_lock
);
298 EXPORT_SYMBOL(xfrm_unregister_type_offload
);
300 static const struct xfrm_type_offload
*
301 xfrm_get_type_offload(u8 proto
, unsigned short family
, bool try_load
)
303 struct xfrm_state_afinfo
*afinfo
;
304 const struct xfrm_type_offload
**typemap
;
305 const struct xfrm_type_offload
*type
;
308 afinfo
= xfrm_state_get_afinfo(family
);
309 if (unlikely(afinfo
== NULL
))
311 typemap
= afinfo
->type_offload_map
;
313 type
= typemap
[proto
];
314 if ((type
&& !try_module_get(type
->owner
)))
319 if (!type
&& try_load
) {
320 request_module("xfrm-offload-%d-%d", family
, proto
);
328 static void xfrm_put_type_offload(const struct xfrm_type_offload
*type
)
330 module_put(type
->owner
);
333 static DEFINE_SPINLOCK(xfrm_mode_lock
);
334 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
336 struct xfrm_state_afinfo
*afinfo
;
337 struct xfrm_mode
**modemap
;
340 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
343 afinfo
= xfrm_state_get_afinfo(family
);
344 if (unlikely(afinfo
== NULL
))
345 return -EAFNOSUPPORT
;
348 modemap
= afinfo
->mode_map
;
349 spin_lock_bh(&xfrm_mode_lock
);
350 if (modemap
[mode
->encap
])
354 if (!try_module_get(afinfo
->owner
))
357 mode
->afinfo
= afinfo
;
358 modemap
[mode
->encap
] = mode
;
362 spin_unlock_bh(&xfrm_mode_lock
);
366 EXPORT_SYMBOL(xfrm_register_mode
);
368 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
370 struct xfrm_state_afinfo
*afinfo
;
371 struct xfrm_mode
**modemap
;
374 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
377 afinfo
= xfrm_state_get_afinfo(family
);
378 if (unlikely(afinfo
== NULL
))
379 return -EAFNOSUPPORT
;
382 modemap
= afinfo
->mode_map
;
383 spin_lock_bh(&xfrm_mode_lock
);
384 if (likely(modemap
[mode
->encap
] == mode
)) {
385 modemap
[mode
->encap
] = NULL
;
386 module_put(mode
->afinfo
->owner
);
390 spin_unlock_bh(&xfrm_mode_lock
);
394 EXPORT_SYMBOL(xfrm_unregister_mode
);
396 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
398 struct xfrm_state_afinfo
*afinfo
;
399 struct xfrm_mode
*mode
;
400 int modload_attempted
= 0;
402 if (unlikely(encap
>= XFRM_MODE_MAX
))
406 afinfo
= xfrm_state_get_afinfo(family
);
407 if (unlikely(afinfo
== NULL
))
410 mode
= READ_ONCE(afinfo
->mode_map
[encap
]);
411 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
415 if (!mode
&& !modload_attempted
) {
416 request_module("xfrm-mode-%d-%d", family
, encap
);
417 modload_attempted
= 1;
424 static void xfrm_put_mode(struct xfrm_mode
*mode
)
426 module_put(mode
->owner
);
429 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
431 tasklet_hrtimer_cancel(&x
->mtimer
);
432 del_timer_sync(&x
->rtimer
);
439 kfree(x
->replay_esn
);
440 kfree(x
->preplay_esn
);
442 xfrm_put_mode(x
->inner_mode
);
443 if (x
->inner_mode_iaf
)
444 xfrm_put_mode(x
->inner_mode_iaf
);
446 xfrm_put_mode(x
->outer_mode
);
448 xfrm_put_type_offload(x
->type_offload
);
450 x
->type
->destructor(x
);
451 xfrm_put_type(x
->type
);
453 xfrm_dev_state_free(x
);
454 security_xfrm_state_free(x
);
455 kmem_cache_free(xfrm_state_cache
, x
);
458 static void xfrm_state_gc_task(struct work_struct
*work
)
460 struct xfrm_state
*x
;
461 struct hlist_node
*tmp
;
462 struct hlist_head gc_list
;
464 spin_lock_bh(&xfrm_state_gc_lock
);
465 hlist_move_list(&xfrm_state_gc_list
, &gc_list
);
466 spin_unlock_bh(&xfrm_state_gc_lock
);
470 hlist_for_each_entry_safe(x
, tmp
, &gc_list
, gclist
)
471 xfrm_state_gc_destroy(x
);
474 static enum hrtimer_restart
xfrm_timer_handler(struct hrtimer
*me
)
476 struct tasklet_hrtimer
*thr
= container_of(me
, struct tasklet_hrtimer
, timer
);
477 struct xfrm_state
*x
= container_of(thr
, struct xfrm_state
, mtimer
);
478 time64_t now
= ktime_get_real_seconds();
479 time64_t next
= TIME64_MAX
;
484 if (x
->km
.state
== XFRM_STATE_DEAD
)
486 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
488 if (x
->lft
.hard_add_expires_seconds
) {
489 long tmo
= x
->lft
.hard_add_expires_seconds
+
490 x
->curlft
.add_time
- now
;
492 if (x
->xflags
& XFRM_SOFT_EXPIRE
) {
493 /* enter hard expire without soft expire first?!
494 * setting a new date could trigger this.
495 * workaround: fix x->curflt.add_time by below:
497 x
->curlft
.add_time
= now
- x
->saved_tmo
- 1;
498 tmo
= x
->lft
.hard_add_expires_seconds
- x
->saved_tmo
;
505 if (x
->lft
.hard_use_expires_seconds
) {
506 long tmo
= x
->lft
.hard_use_expires_seconds
+
507 (x
->curlft
.use_time
? : now
) - now
;
515 if (x
->lft
.soft_add_expires_seconds
) {
516 long tmo
= x
->lft
.soft_add_expires_seconds
+
517 x
->curlft
.add_time
- now
;
520 x
->xflags
&= ~XFRM_SOFT_EXPIRE
;
521 } else if (tmo
< next
) {
523 x
->xflags
|= XFRM_SOFT_EXPIRE
;
527 if (x
->lft
.soft_use_expires_seconds
) {
528 long tmo
= x
->lft
.soft_use_expires_seconds
+
529 (x
->curlft
.use_time
? : now
) - now
;
538 km_state_expired(x
, 0, 0);
540 if (next
!= TIME64_MAX
) {
541 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(next
, 0), HRTIMER_MODE_REL
);
547 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0)
548 x
->km
.state
= XFRM_STATE_EXPIRED
;
550 err
= __xfrm_state_delete(x
);
552 km_state_expired(x
, 1, 0);
554 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
557 spin_unlock(&x
->lock
);
558 return HRTIMER_NORESTART
;
561 static void xfrm_replay_timer_handler(struct timer_list
*t
);
563 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
565 struct xfrm_state
*x
;
567 x
= kmem_cache_alloc(xfrm_state_cache
, GFP_ATOMIC
| __GFP_ZERO
);
570 write_pnet(&x
->xs_net
, net
);
571 refcount_set(&x
->refcnt
, 1);
572 atomic_set(&x
->tunnel_users
, 0);
573 INIT_LIST_HEAD(&x
->km
.all
);
574 INIT_HLIST_NODE(&x
->bydst
);
575 INIT_HLIST_NODE(&x
->bysrc
);
576 INIT_HLIST_NODE(&x
->byspi
);
577 tasklet_hrtimer_init(&x
->mtimer
, xfrm_timer_handler
,
578 CLOCK_BOOTTIME
, HRTIMER_MODE_ABS
);
579 timer_setup(&x
->rtimer
, xfrm_replay_timer_handler
, 0);
580 x
->curlft
.add_time
= ktime_get_real_seconds();
581 x
->lft
.soft_byte_limit
= XFRM_INF
;
582 x
->lft
.soft_packet_limit
= XFRM_INF
;
583 x
->lft
.hard_byte_limit
= XFRM_INF
;
584 x
->lft
.hard_packet_limit
= XFRM_INF
;
585 x
->replay_maxage
= 0;
586 x
->replay_maxdiff
= 0;
587 x
->inner_mode
= NULL
;
588 x
->inner_mode_iaf
= NULL
;
589 spin_lock_init(&x
->lock
);
593 EXPORT_SYMBOL(xfrm_state_alloc
);
595 void __xfrm_state_destroy(struct xfrm_state
*x
)
597 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
599 spin_lock_bh(&xfrm_state_gc_lock
);
600 hlist_add_head(&x
->gclist
, &xfrm_state_gc_list
);
601 spin_unlock_bh(&xfrm_state_gc_lock
);
602 schedule_work(&xfrm_state_gc_work
);
604 EXPORT_SYMBOL(__xfrm_state_destroy
);
606 int __xfrm_state_delete(struct xfrm_state
*x
)
608 struct net
*net
= xs_net(x
);
611 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
612 x
->km
.state
= XFRM_STATE_DEAD
;
613 spin_lock(&net
->xfrm
.xfrm_state_lock
);
614 list_del(&x
->km
.all
);
615 hlist_del_rcu(&x
->bydst
);
616 hlist_del_rcu(&x
->bysrc
);
618 hlist_del_rcu(&x
->byspi
);
619 net
->xfrm
.state_num
--;
620 spin_unlock(&net
->xfrm
.xfrm_state_lock
);
622 xfrm_dev_state_delete(x
);
624 /* All xfrm_state objects are created by xfrm_state_alloc.
625 * The xfrm_state_alloc call gives a reference, and that
626 * is what we are dropping here.
634 EXPORT_SYMBOL(__xfrm_state_delete
);
636 int xfrm_state_delete(struct xfrm_state
*x
)
640 spin_lock_bh(&x
->lock
);
641 err
= __xfrm_state_delete(x
);
642 spin_unlock_bh(&x
->lock
);
646 EXPORT_SYMBOL(xfrm_state_delete
);
648 #ifdef CONFIG_SECURITY_NETWORK_XFRM
650 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
654 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
655 struct xfrm_state
*x
;
657 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
658 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
659 (err
= security_xfrm_state_delete(x
)) != 0) {
660 xfrm_audit_state_delete(x
, 0, task_valid
);
670 xfrm_dev_state_flush_secctx_check(struct net
*net
, struct net_device
*dev
, bool task_valid
)
674 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
675 struct xfrm_state
*x
;
676 struct xfrm_state_offload
*xso
;
678 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
681 if (xso
->dev
== dev
&&
682 (err
= security_xfrm_state_delete(x
)) != 0) {
683 xfrm_audit_state_delete(x
, 0, task_valid
);
693 xfrm_state_flush_secctx_check(struct net
*net
, u8 proto
, bool task_valid
)
699 xfrm_dev_state_flush_secctx_check(struct net
*net
, struct net_device
*dev
, bool task_valid
)
705 int xfrm_state_flush(struct net
*net
, u8 proto
, bool task_valid
)
707 int i
, err
= 0, cnt
= 0;
709 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
710 err
= xfrm_state_flush_secctx_check(net
, proto
, task_valid
);
715 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
716 struct xfrm_state
*x
;
718 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
719 if (!xfrm_state_kern(x
) &&
720 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
722 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
724 err
= xfrm_state_delete(x
);
725 xfrm_audit_state_delete(x
, err
? 0 : 1,
731 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
737 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
743 EXPORT_SYMBOL(xfrm_state_flush
);
745 int xfrm_dev_state_flush(struct net
*net
, struct net_device
*dev
, bool task_valid
)
747 int i
, err
= 0, cnt
= 0;
749 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
750 err
= xfrm_dev_state_flush_secctx_check(net
, dev
, task_valid
);
755 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
756 struct xfrm_state
*x
;
757 struct xfrm_state_offload
*xso
;
759 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
762 if (!xfrm_state_kern(x
) && xso
->dev
== dev
) {
764 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
766 err
= xfrm_state_delete(x
);
767 xfrm_audit_state_delete(x
, err
? 0 : 1,
773 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
782 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
785 EXPORT_SYMBOL(xfrm_dev_state_flush
);
787 void xfrm_sad_getinfo(struct net
*net
, struct xfrmk_sadinfo
*si
)
789 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
790 si
->sadcnt
= net
->xfrm
.state_num
;
791 si
->sadhcnt
= net
->xfrm
.state_hmask
;
792 si
->sadhmcnt
= xfrm_state_hashmax
;
793 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
795 EXPORT_SYMBOL(xfrm_sad_getinfo
);
798 xfrm_init_tempstate(struct xfrm_state
*x
, const struct flowi
*fl
,
799 const struct xfrm_tmpl
*tmpl
,
800 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
801 unsigned short family
)
803 struct xfrm_state_afinfo
*afinfo
= xfrm_state_afinfo_get_rcu(family
);
808 afinfo
->init_tempsel(&x
->sel
, fl
);
810 if (family
!= tmpl
->encap_family
) {
811 afinfo
= xfrm_state_afinfo_get_rcu(tmpl
->encap_family
);
815 afinfo
->init_temprop(x
, tmpl
, daddr
, saddr
);
818 static struct xfrm_state
*__xfrm_state_lookup(struct net
*net
, u32 mark
,
819 const xfrm_address_t
*daddr
,
820 __be32 spi
, u8 proto
,
821 unsigned short family
)
823 unsigned int h
= xfrm_spi_hash(net
, daddr
, spi
, proto
, family
);
824 struct xfrm_state
*x
;
826 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_byspi
+ h
, byspi
) {
827 if (x
->props
.family
!= family
||
829 x
->id
.proto
!= proto
||
830 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
))
833 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
835 if (!xfrm_state_hold_rcu(x
))
843 static struct xfrm_state
*__xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
844 const xfrm_address_t
*daddr
,
845 const xfrm_address_t
*saddr
,
846 u8 proto
, unsigned short family
)
848 unsigned int h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
849 struct xfrm_state
*x
;
851 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bysrc
+ h
, bysrc
) {
852 if (x
->props
.family
!= family
||
853 x
->id
.proto
!= proto
||
854 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
855 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
858 if ((mark
& x
->mark
.m
) != x
->mark
.v
)
860 if (!xfrm_state_hold_rcu(x
))
868 static inline struct xfrm_state
*
869 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
871 struct net
*net
= xs_net(x
);
872 u32 mark
= x
->mark
.v
& x
->mark
.m
;
875 return __xfrm_state_lookup(net
, mark
, &x
->id
.daddr
,
876 x
->id
.spi
, x
->id
.proto
, family
);
878 return __xfrm_state_lookup_byaddr(net
, mark
,
881 x
->id
.proto
, family
);
884 static void xfrm_hash_grow_check(struct net
*net
, int have_hash_collision
)
886 if (have_hash_collision
&&
887 (net
->xfrm
.state_hmask
+ 1) < xfrm_state_hashmax
&&
888 net
->xfrm
.state_num
> net
->xfrm
.state_hmask
)
889 schedule_work(&net
->xfrm
.state_hash_work
);
892 static void xfrm_state_look_at(struct xfrm_policy
*pol
, struct xfrm_state
*x
,
893 const struct flowi
*fl
, unsigned short family
,
894 struct xfrm_state
**best
, int *acq_in_progress
,
898 * 1. There is a valid state with matching selector. Done.
899 * 2. Valid state with inappropriate selector. Skip.
901 * Entering area of "sysdeps".
903 * 3. If state is not valid, selector is temporary, it selects
904 * only session which triggered previous resolution. Key
905 * manager will do something to install a state with proper
908 if (x
->km
.state
== XFRM_STATE_VALID
) {
909 if ((x
->sel
.family
&&
910 !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
911 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
915 (*best
)->km
.dying
> x
->km
.dying
||
916 ((*best
)->km
.dying
== x
->km
.dying
&&
917 (*best
)->curlft
.add_time
< x
->curlft
.add_time
))
919 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
920 *acq_in_progress
= 1;
921 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
922 x
->km
.state
== XFRM_STATE_EXPIRED
) {
923 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
924 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
930 xfrm_state_find(const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
931 const struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
932 struct xfrm_policy
*pol
, int *err
,
933 unsigned short family
, u32 if_id
)
935 static xfrm_address_t saddr_wildcard
= { };
936 struct net
*net
= xp_net(pol
);
937 unsigned int h
, h_wildcard
;
938 struct xfrm_state
*x
, *x0
, *to_put
;
939 int acquire_in_progress
= 0;
941 struct xfrm_state
*best
= NULL
;
942 u32 mark
= pol
->mark
.v
& pol
->mark
.m
;
943 unsigned short encap_family
= tmpl
->encap_family
;
944 unsigned int sequence
;
949 sequence
= read_seqcount_begin(&xfrm_state_hash_generation
);
952 h
= xfrm_dst_hash(net
, daddr
, saddr
, tmpl
->reqid
, encap_family
);
953 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bydst
+ h
, bydst
) {
954 if (x
->props
.family
== encap_family
&&
955 x
->props
.reqid
== tmpl
->reqid
&&
956 (mark
& x
->mark
.m
) == x
->mark
.v
&&
958 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
959 xfrm_state_addr_check(x
, daddr
, saddr
, encap_family
) &&
960 tmpl
->mode
== x
->props
.mode
&&
961 tmpl
->id
.proto
== x
->id
.proto
&&
962 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
963 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
964 &best
, &acquire_in_progress
, &error
);
966 if (best
|| acquire_in_progress
)
969 h_wildcard
= xfrm_dst_hash(net
, daddr
, &saddr_wildcard
, tmpl
->reqid
, encap_family
);
970 hlist_for_each_entry_rcu(x
, net
->xfrm
.state_bydst
+ h_wildcard
, bydst
) {
971 if (x
->props
.family
== encap_family
&&
972 x
->props
.reqid
== tmpl
->reqid
&&
973 (mark
& x
->mark
.m
) == x
->mark
.v
&&
975 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
976 xfrm_addr_equal(&x
->id
.daddr
, daddr
, encap_family
) &&
977 tmpl
->mode
== x
->props
.mode
&&
978 tmpl
->id
.proto
== x
->id
.proto
&&
979 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
))
980 xfrm_state_look_at(pol
, x
, fl
, encap_family
,
981 &best
, &acquire_in_progress
, &error
);
986 if (!x
&& !error
&& !acquire_in_progress
) {
988 (x0
= __xfrm_state_lookup(net
, mark
, daddr
, tmpl
->id
.spi
,
989 tmpl
->id
.proto
, encap_family
)) != NULL
) {
996 /* If the KMs have no listeners (yet...), avoid allocating an SA
997 * for each and every packet - garbage collection might not
1000 if (!km_is_alive(&c
)) {
1005 x
= xfrm_state_alloc(net
);
1010 /* Initialize temporary state matching only
1011 * to current session. */
1012 xfrm_init_tempstate(x
, fl
, tmpl
, daddr
, saddr
, family
);
1013 memcpy(&x
->mark
, &pol
->mark
, sizeof(x
->mark
));
1016 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->flowi_secid
);
1018 x
->km
.state
= XFRM_STATE_DEAD
;
1024 if (km_query(x
, tmpl
, pol
) == 0) {
1025 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1026 x
->km
.state
= XFRM_STATE_ACQ
;
1027 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1028 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1029 h
= xfrm_src_hash(net
, daddr
, saddr
, encap_family
);
1030 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1032 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, encap_family
);
1033 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1035 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1036 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(net
->xfrm
.sysctl_acq_expires
, 0), HRTIMER_MODE_REL
);
1037 net
->xfrm
.state_num
++;
1038 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1039 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1041 x
->km
.state
= XFRM_STATE_DEAD
;
1049 if (!xfrm_state_hold_rcu(x
)) {
1054 *err
= acquire_in_progress
? -EAGAIN
: error
;
1058 xfrm_state_put(to_put
);
1060 if (read_seqcount_retry(&xfrm_state_hash_generation
, sequence
)) {
1072 xfrm_stateonly_find(struct net
*net
, u32 mark
, u32 if_id
,
1073 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1074 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
1077 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
1079 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1080 h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1081 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1082 if (x
->props
.family
== family
&&
1083 x
->props
.reqid
== reqid
&&
1084 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1085 x
->if_id
== if_id
&&
1086 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
1087 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
1088 mode
== x
->props
.mode
&&
1089 proto
== x
->id
.proto
&&
1090 x
->km
.state
== XFRM_STATE_VALID
) {
1097 xfrm_state_hold(rx
);
1098 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1103 EXPORT_SYMBOL(xfrm_stateonly_find
);
1105 struct xfrm_state
*xfrm_state_lookup_byspi(struct net
*net
, __be32 spi
,
1106 unsigned short family
)
1108 struct xfrm_state
*x
;
1109 struct xfrm_state_walk
*w
;
1111 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1112 list_for_each_entry(w
, &net
->xfrm
.state_all
, all
) {
1113 x
= container_of(w
, struct xfrm_state
, km
);
1114 if (x
->props
.family
!= family
||
1119 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1122 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1125 EXPORT_SYMBOL(xfrm_state_lookup_byspi
);
1127 static void __xfrm_state_insert(struct xfrm_state
*x
)
1129 struct net
*net
= xs_net(x
);
1132 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1134 h
= xfrm_dst_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
,
1135 x
->props
.reqid
, x
->props
.family
);
1136 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1138 h
= xfrm_src_hash(net
, &x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
1139 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1142 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
1145 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1148 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL
);
1149 if (x
->replay_maxage
)
1150 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
1152 net
->xfrm
.state_num
++;
1154 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1157 /* net->xfrm.xfrm_state_lock is held */
1158 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
1160 struct net
*net
= xs_net(xnew
);
1161 unsigned short family
= xnew
->props
.family
;
1162 u32 reqid
= xnew
->props
.reqid
;
1163 struct xfrm_state
*x
;
1165 u32 mark
= xnew
->mark
.v
& xnew
->mark
.m
;
1166 u32 if_id
= xnew
->if_id
;
1168 h
= xfrm_dst_hash(net
, &xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
1169 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1170 if (x
->props
.family
== family
&&
1171 x
->props
.reqid
== reqid
&&
1172 x
->if_id
== if_id
&&
1173 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1174 xfrm_addr_equal(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
1175 xfrm_addr_equal(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
1180 void xfrm_state_insert(struct xfrm_state
*x
)
1182 struct net
*net
= xs_net(x
);
1184 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1185 __xfrm_state_bump_genids(x
);
1186 __xfrm_state_insert(x
);
1187 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1189 EXPORT_SYMBOL(xfrm_state_insert
);
1191 /* net->xfrm.xfrm_state_lock is held */
1192 static struct xfrm_state
*__find_acq_core(struct net
*net
,
1193 const struct xfrm_mark
*m
,
1194 unsigned short family
, u8 mode
,
1195 u32 reqid
, u32 if_id
, u8 proto
,
1196 const xfrm_address_t
*daddr
,
1197 const xfrm_address_t
*saddr
,
1200 unsigned int h
= xfrm_dst_hash(net
, daddr
, saddr
, reqid
, family
);
1201 struct xfrm_state
*x
;
1202 u32 mark
= m
->v
& m
->m
;
1204 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1205 if (x
->props
.reqid
!= reqid
||
1206 x
->props
.mode
!= mode
||
1207 x
->props
.family
!= family
||
1208 x
->km
.state
!= XFRM_STATE_ACQ
||
1210 x
->id
.proto
!= proto
||
1211 (mark
& x
->mark
.m
) != x
->mark
.v
||
1212 !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
) ||
1213 !xfrm_addr_equal(&x
->props
.saddr
, saddr
, family
))
1223 x
= xfrm_state_alloc(net
);
1227 x
->sel
.daddr
.a4
= daddr
->a4
;
1228 x
->sel
.saddr
.a4
= saddr
->a4
;
1229 x
->sel
.prefixlen_d
= 32;
1230 x
->sel
.prefixlen_s
= 32;
1231 x
->props
.saddr
.a4
= saddr
->a4
;
1232 x
->id
.daddr
.a4
= daddr
->a4
;
1236 x
->sel
.daddr
.in6
= daddr
->in6
;
1237 x
->sel
.saddr
.in6
= saddr
->in6
;
1238 x
->sel
.prefixlen_d
= 128;
1239 x
->sel
.prefixlen_s
= 128;
1240 x
->props
.saddr
.in6
= saddr
->in6
;
1241 x
->id
.daddr
.in6
= daddr
->in6
;
1245 x
->km
.state
= XFRM_STATE_ACQ
;
1246 x
->id
.proto
= proto
;
1247 x
->props
.family
= family
;
1248 x
->props
.mode
= mode
;
1249 x
->props
.reqid
= reqid
;
1253 x
->lft
.hard_add_expires_seconds
= net
->xfrm
.sysctl_acq_expires
;
1255 tasklet_hrtimer_start(&x
->mtimer
, ktime_set(net
->xfrm
.sysctl_acq_expires
, 0), HRTIMER_MODE_REL
);
1256 list_add(&x
->km
.all
, &net
->xfrm
.state_all
);
1257 hlist_add_head_rcu(&x
->bydst
, net
->xfrm
.state_bydst
+ h
);
1258 h
= xfrm_src_hash(net
, daddr
, saddr
, family
);
1259 hlist_add_head_rcu(&x
->bysrc
, net
->xfrm
.state_bysrc
+ h
);
1261 net
->xfrm
.state_num
++;
1263 xfrm_hash_grow_check(net
, x
->bydst
.next
!= NULL
);
1269 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
);
1271 int xfrm_state_add(struct xfrm_state
*x
)
1273 struct net
*net
= xs_net(x
);
1274 struct xfrm_state
*x1
, *to_put
;
1277 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1278 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1280 family
= x
->props
.family
;
1284 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1286 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1294 if (use_spi
&& x
->km
.seq
) {
1295 x1
= __xfrm_find_acq_byseq(net
, mark
, x
->km
.seq
);
1296 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1297 !xfrm_addr_equal(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1304 x1
= __find_acq_core(net
, &x
->mark
, family
, x
->props
.mode
,
1305 x
->props
.reqid
, x
->if_id
, x
->id
.proto
,
1306 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1308 __xfrm_state_bump_genids(x
);
1309 __xfrm_state_insert(x
);
1313 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1316 xfrm_state_delete(x1
);
1321 xfrm_state_put(to_put
);
1325 EXPORT_SYMBOL(xfrm_state_add
);
1327 #ifdef CONFIG_XFRM_MIGRATE
1328 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
,
1329 struct xfrm_encap_tmpl
*encap
)
1331 struct net
*net
= xs_net(orig
);
1332 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1336 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1337 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1338 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1339 x
->props
.mode
= orig
->props
.mode
;
1340 x
->props
.replay_window
= orig
->props
.replay_window
;
1341 x
->props
.reqid
= orig
->props
.reqid
;
1342 x
->props
.family
= orig
->props
.family
;
1343 x
->props
.saddr
= orig
->props
.saddr
;
1346 x
->aalg
= xfrm_algo_auth_clone(orig
->aalg
);
1350 x
->props
.aalgo
= orig
->props
.aalgo
;
1353 x
->aead
= xfrm_algo_aead_clone(orig
->aead
);
1354 x
->geniv
= orig
->geniv
;
1359 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1363 x
->props
.ealgo
= orig
->props
.ealgo
;
1366 x
->calg
= xfrm_algo_clone(orig
->calg
);
1370 x
->props
.calgo
= orig
->props
.calgo
;
1372 if (encap
|| orig
->encap
) {
1374 x
->encap
= kmemdup(encap
, sizeof(*x
->encap
),
1377 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
),
1385 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1391 if (orig
->replay_esn
) {
1392 if (xfrm_replay_clone(x
, orig
))
1396 memcpy(&x
->mark
, &orig
->mark
, sizeof(x
->mark
));
1398 if (xfrm_init_state(x
) < 0)
1401 x
->props
.flags
= orig
->props
.flags
;
1402 x
->props
.extra_flags
= orig
->props
.extra_flags
;
1404 x
->if_id
= orig
->if_id
;
1405 x
->tfcpad
= orig
->tfcpad
;
1406 x
->replay_maxdiff
= orig
->replay_maxdiff
;
1407 x
->replay_maxage
= orig
->replay_maxage
;
1408 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1409 x
->km
.state
= orig
->km
.state
;
1410 x
->km
.seq
= orig
->km
.seq
;
1411 x
->replay
= orig
->replay
;
1412 x
->preplay
= orig
->preplay
;
1422 struct xfrm_state
*xfrm_migrate_state_find(struct xfrm_migrate
*m
, struct net
*net
)
1425 struct xfrm_state
*x
= NULL
;
1427 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1430 h
= xfrm_dst_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1431 m
->reqid
, m
->old_family
);
1432 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+h
, bydst
) {
1433 if (x
->props
.mode
!= m
->mode
||
1434 x
->id
.proto
!= m
->proto
)
1436 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1438 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1440 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1447 h
= xfrm_src_hash(net
, &m
->old_daddr
, &m
->old_saddr
,
1449 hlist_for_each_entry(x
, net
->xfrm
.state_bysrc
+h
, bysrc
) {
1450 if (x
->props
.mode
!= m
->mode
||
1451 x
->id
.proto
!= m
->proto
)
1453 if (!xfrm_addr_equal(&x
->id
.daddr
, &m
->old_daddr
,
1455 !xfrm_addr_equal(&x
->props
.saddr
, &m
->old_saddr
,
1463 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1467 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1469 struct xfrm_state
*xfrm_state_migrate(struct xfrm_state
*x
,
1470 struct xfrm_migrate
*m
,
1471 struct xfrm_encap_tmpl
*encap
)
1473 struct xfrm_state
*xc
;
1475 xc
= xfrm_state_clone(x
, encap
);
1479 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1480 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1483 if (xfrm_addr_equal(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1484 /* a care is needed when the destination address of the
1485 state is to be updated as it is a part of triplet */
1486 xfrm_state_insert(xc
);
1488 if (xfrm_state_add(xc
) < 0)
1497 EXPORT_SYMBOL(xfrm_state_migrate
);
1500 int xfrm_state_update(struct xfrm_state
*x
)
1502 struct xfrm_state
*x1
, *to_put
;
1504 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1505 struct net
*net
= xs_net(x
);
1509 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1510 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1516 if (xfrm_state_kern(x1
)) {
1522 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1523 __xfrm_state_insert(x
);
1529 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1532 xfrm_state_put(to_put
);
1538 xfrm_state_delete(x1
);
1544 spin_lock_bh(&x1
->lock
);
1545 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1546 if (x
->encap
&& x1
->encap
&&
1547 x
->encap
->encap_type
== x1
->encap
->encap_type
)
1548 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1549 else if (x
->encap
|| x1
->encap
)
1552 if (x
->coaddr
&& x1
->coaddr
) {
1553 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1555 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1556 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1557 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1560 tasklet_hrtimer_start(&x1
->mtimer
, ktime_set(1, 0), HRTIMER_MODE_REL
);
1561 if (x1
->curlft
.use_time
)
1562 xfrm_state_check_expire(x1
);
1564 if (x
->props
.smark
.m
|| x
->props
.smark
.v
|| x
->if_id
) {
1565 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1567 if (x
->props
.smark
.m
|| x
->props
.smark
.v
)
1568 x1
->props
.smark
= x
->props
.smark
;
1571 x1
->if_id
= x
->if_id
;
1573 __xfrm_state_bump_genids(x1
);
1574 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1578 x
->km
.state
= XFRM_STATE_DEAD
;
1579 __xfrm_state_put(x
);
1583 spin_unlock_bh(&x1
->lock
);
1589 EXPORT_SYMBOL(xfrm_state_update
);
1591 int xfrm_state_check_expire(struct xfrm_state
*x
)
1593 if (!x
->curlft
.use_time
)
1594 x
->curlft
.use_time
= ktime_get_real_seconds();
1596 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1597 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1598 x
->km
.state
= XFRM_STATE_EXPIRED
;
1599 tasklet_hrtimer_start(&x
->mtimer
, 0, HRTIMER_MODE_REL
);
1604 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1605 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1607 km_state_expired(x
, 0, 0);
1611 EXPORT_SYMBOL(xfrm_state_check_expire
);
1614 xfrm_state_lookup(struct net
*net
, u32 mark
, const xfrm_address_t
*daddr
, __be32 spi
,
1615 u8 proto
, unsigned short family
)
1617 struct xfrm_state
*x
;
1620 x
= __xfrm_state_lookup(net
, mark
, daddr
, spi
, proto
, family
);
1624 EXPORT_SYMBOL(xfrm_state_lookup
);
1627 xfrm_state_lookup_byaddr(struct net
*net
, u32 mark
,
1628 const xfrm_address_t
*daddr
, const xfrm_address_t
*saddr
,
1629 u8 proto
, unsigned short family
)
1631 struct xfrm_state
*x
;
1633 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1634 x
= __xfrm_state_lookup_byaddr(net
, mark
, daddr
, saddr
, proto
, family
);
1635 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1638 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1641 xfrm_find_acq(struct net
*net
, const struct xfrm_mark
*mark
, u8 mode
, u32 reqid
,
1642 u32 if_id
, u8 proto
, const xfrm_address_t
*daddr
,
1643 const xfrm_address_t
*saddr
, int create
, unsigned short family
)
1645 struct xfrm_state
*x
;
1647 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1648 x
= __find_acq_core(net
, mark
, family
, mode
, reqid
, if_id
, proto
, daddr
, saddr
, create
);
1649 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1653 EXPORT_SYMBOL(xfrm_find_acq
);
1655 #ifdef CONFIG_XFRM_SUB_POLICY
1657 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1658 unsigned short family
, struct net
*net
)
1662 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1664 return -EAFNOSUPPORT
;
1666 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
); /*FIXME*/
1667 if (afinfo
->tmpl_sort
)
1668 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1670 for (i
= 0; i
< n
; i
++)
1672 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1676 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1679 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1680 unsigned short family
)
1684 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1685 struct net
*net
= xs_net(*src
);
1688 return -EAFNOSUPPORT
;
1690 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1691 if (afinfo
->state_sort
)
1692 err
= afinfo
->state_sort(dst
, src
, n
);
1694 for (i
= 0; i
< n
; i
++)
1696 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1700 EXPORT_SYMBOL(xfrm_state_sort
);
1703 /* Silly enough, but I'm lazy to build resolution list */
1705 static struct xfrm_state
*__xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1709 for (i
= 0; i
<= net
->xfrm
.state_hmask
; i
++) {
1710 struct xfrm_state
*x
;
1712 hlist_for_each_entry(x
, net
->xfrm
.state_bydst
+i
, bydst
) {
1713 if (x
->km
.seq
== seq
&&
1714 (mark
& x
->mark
.m
) == x
->mark
.v
&&
1715 x
->km
.state
== XFRM_STATE_ACQ
) {
1724 struct xfrm_state
*xfrm_find_acq_byseq(struct net
*net
, u32 mark
, u32 seq
)
1726 struct xfrm_state
*x
;
1728 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1729 x
= __xfrm_find_acq_byseq(net
, mark
, seq
);
1730 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1733 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1735 u32
xfrm_get_acqseq(void)
1738 static atomic_t acqseq
;
1741 res
= atomic_inc_return(&acqseq
);
1746 EXPORT_SYMBOL(xfrm_get_acqseq
);
1748 int verify_spi_info(u8 proto
, u32 min
, u32 max
)
1756 /* IPCOMP spi is 16-bits. */
1770 EXPORT_SYMBOL(verify_spi_info
);
1772 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1774 struct net
*net
= xs_net(x
);
1776 struct xfrm_state
*x0
;
1778 __be32 minspi
= htonl(low
);
1779 __be32 maxspi
= htonl(high
);
1780 u32 mark
= x
->mark
.v
& x
->mark
.m
;
1782 spin_lock_bh(&x
->lock
);
1783 if (x
->km
.state
== XFRM_STATE_DEAD
)
1792 if (minspi
== maxspi
) {
1793 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1801 for (h
= 0; h
< high
-low
+1; h
++) {
1802 spi
= low
+ prandom_u32()%(high
-low
+1);
1803 x0
= xfrm_state_lookup(net
, mark
, &x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1805 x
->id
.spi
= htonl(spi
);
1812 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1813 h
= xfrm_spi_hash(net
, &x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1814 hlist_add_head_rcu(&x
->byspi
, net
->xfrm
.state_byspi
+ h
);
1815 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1821 spin_unlock_bh(&x
->lock
);
1825 EXPORT_SYMBOL(xfrm_alloc_spi
);
1827 static bool __xfrm_state_filter_match(struct xfrm_state
*x
,
1828 struct xfrm_address_filter
*filter
)
1831 if ((filter
->family
== AF_INET
||
1832 filter
->family
== AF_INET6
) &&
1833 x
->props
.family
!= filter
->family
)
1836 return addr_match(&x
->props
.saddr
, &filter
->saddr
,
1838 addr_match(&x
->id
.daddr
, &filter
->daddr
,
1844 int xfrm_state_walk(struct net
*net
, struct xfrm_state_walk
*walk
,
1845 int (*func
)(struct xfrm_state
*, int, void*),
1848 struct xfrm_state
*state
;
1849 struct xfrm_state_walk
*x
;
1852 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
1855 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1856 if (list_empty(&walk
->all
))
1857 x
= list_first_entry(&net
->xfrm
.state_all
, struct xfrm_state_walk
, all
);
1859 x
= list_first_entry(&walk
->all
, struct xfrm_state_walk
, all
);
1860 list_for_each_entry_from(x
, &net
->xfrm
.state_all
, all
) {
1861 if (x
->state
== XFRM_STATE_DEAD
)
1863 state
= container_of(x
, struct xfrm_state
, km
);
1864 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
1866 if (!__xfrm_state_filter_match(state
, walk
->filter
))
1868 err
= func(state
, walk
->seq
, data
);
1870 list_move_tail(&walk
->all
, &x
->all
);
1875 if (walk
->seq
== 0) {
1879 list_del_init(&walk
->all
);
1881 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1884 EXPORT_SYMBOL(xfrm_state_walk
);
1886 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
,
1887 struct xfrm_address_filter
*filter
)
1889 INIT_LIST_HEAD(&walk
->all
);
1890 walk
->proto
= proto
;
1891 walk
->state
= XFRM_STATE_DEAD
;
1893 walk
->filter
= filter
;
1895 EXPORT_SYMBOL(xfrm_state_walk_init
);
1897 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
, struct net
*net
)
1899 kfree(walk
->filter
);
1901 if (list_empty(&walk
->all
))
1904 spin_lock_bh(&net
->xfrm
.xfrm_state_lock
);
1905 list_del(&walk
->all
);
1906 spin_unlock_bh(&net
->xfrm
.xfrm_state_lock
);
1908 EXPORT_SYMBOL(xfrm_state_walk_done
);
1910 static void xfrm_replay_timer_handler(struct timer_list
*t
)
1912 struct xfrm_state
*x
= from_timer(x
, t
, rtimer
);
1914 spin_lock(&x
->lock
);
1916 if (x
->km
.state
== XFRM_STATE_VALID
) {
1917 if (xfrm_aevent_is_on(xs_net(x
)))
1918 x
->repl
->notify(x
, XFRM_REPLAY_TIMEOUT
);
1920 x
->xflags
|= XFRM_TIME_DEFER
;
1923 spin_unlock(&x
->lock
);
1926 static LIST_HEAD(xfrm_km_list
);
1928 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
1930 struct xfrm_mgr
*km
;
1933 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1934 if (km
->notify_policy
)
1935 km
->notify_policy(xp
, dir
, c
);
1939 void km_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
1941 struct xfrm_mgr
*km
;
1943 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
)
1949 EXPORT_SYMBOL(km_policy_notify
);
1950 EXPORT_SYMBOL(km_state_notify
);
1952 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 portid
)
1958 c
.event
= XFRM_MSG_EXPIRE
;
1959 km_state_notify(x
, &c
);
1962 EXPORT_SYMBOL(km_state_expired
);
1964 * We send to all registered managers regardless of failure
1965 * We are happy with one success
1967 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1969 int err
= -EINVAL
, acqret
;
1970 struct xfrm_mgr
*km
;
1973 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1974 acqret
= km
->acquire(x
, t
, pol
);
1981 EXPORT_SYMBOL(km_query
);
1983 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1986 struct xfrm_mgr
*km
;
1989 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
1990 if (km
->new_mapping
)
1991 err
= km
->new_mapping(x
, ipaddr
, sport
);
1998 EXPORT_SYMBOL(km_new_mapping
);
2000 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 portid
)
2006 c
.event
= XFRM_MSG_POLEXPIRE
;
2007 km_policy_notify(pol
, dir
, &c
);
2009 EXPORT_SYMBOL(km_policy_expired
);
2011 #ifdef CONFIG_XFRM_MIGRATE
2012 int km_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2013 const struct xfrm_migrate
*m
, int num_migrate
,
2014 const struct xfrm_kmaddress
*k
,
2015 const struct xfrm_encap_tmpl
*encap
)
2019 struct xfrm_mgr
*km
;
2022 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2024 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
,
2033 EXPORT_SYMBOL(km_migrate
);
2036 int km_report(struct net
*net
, u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2040 struct xfrm_mgr
*km
;
2043 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2045 ret
= km
->report(net
, proto
, sel
, addr
);
2053 EXPORT_SYMBOL(km_report
);
2055 bool km_is_alive(const struct km_event
*c
)
2057 struct xfrm_mgr
*km
;
2058 bool is_alive
= false;
2061 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2062 if (km
->is_alive
&& km
->is_alive(c
)) {
2071 EXPORT_SYMBOL(km_is_alive
);
2073 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
2077 struct xfrm_mgr
*km
;
2078 struct xfrm_policy
*pol
= NULL
;
2080 #ifdef CONFIG_COMPAT
2081 if (in_compat_syscall())
2085 if (!optval
&& !optlen
) {
2086 xfrm_sk_policy_insert(sk
, XFRM_POLICY_IN
, NULL
);
2087 xfrm_sk_policy_insert(sk
, XFRM_POLICY_OUT
, NULL
);
2092 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
2095 data
= memdup_user(optval
, optlen
);
2097 return PTR_ERR(data
);
2101 list_for_each_entry_rcu(km
, &xfrm_km_list
, list
) {
2102 pol
= km
->compile_policy(sk
, optname
, data
,
2110 xfrm_sk_policy_insert(sk
, err
, pol
);
2119 EXPORT_SYMBOL(xfrm_user_policy
);
2121 static DEFINE_SPINLOCK(xfrm_km_lock
);
2123 int xfrm_register_km(struct xfrm_mgr
*km
)
2125 spin_lock_bh(&xfrm_km_lock
);
2126 list_add_tail_rcu(&km
->list
, &xfrm_km_list
);
2127 spin_unlock_bh(&xfrm_km_lock
);
2130 EXPORT_SYMBOL(xfrm_register_km
);
2132 int xfrm_unregister_km(struct xfrm_mgr
*km
)
2134 spin_lock_bh(&xfrm_km_lock
);
2135 list_del_rcu(&km
->list
);
2136 spin_unlock_bh(&xfrm_km_lock
);
2140 EXPORT_SYMBOL(xfrm_unregister_km
);
2142 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
2146 if (WARN_ON(afinfo
->family
>= NPROTO
))
2147 return -EAFNOSUPPORT
;
2149 spin_lock_bh(&xfrm_state_afinfo_lock
);
2150 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
2153 rcu_assign_pointer(xfrm_state_afinfo
[afinfo
->family
], afinfo
);
2154 spin_unlock_bh(&xfrm_state_afinfo_lock
);
2157 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
2159 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
2161 int err
= 0, family
= afinfo
->family
;
2163 if (WARN_ON(family
>= NPROTO
))
2164 return -EAFNOSUPPORT
;
2166 spin_lock_bh(&xfrm_state_afinfo_lock
);
2167 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
2168 if (rcu_access_pointer(xfrm_state_afinfo
[family
]) != afinfo
)
2171 RCU_INIT_POINTER(xfrm_state_afinfo
[afinfo
->family
], NULL
);
2173 spin_unlock_bh(&xfrm_state_afinfo_lock
);
2177 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
2179 struct xfrm_state_afinfo
*xfrm_state_afinfo_get_rcu(unsigned int family
)
2181 if (unlikely(family
>= NPROTO
))
2184 return rcu_dereference(xfrm_state_afinfo
[family
]);
2187 struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
2189 struct xfrm_state_afinfo
*afinfo
;
2190 if (unlikely(family
>= NPROTO
))
2193 afinfo
= rcu_dereference(xfrm_state_afinfo
[family
]);
2194 if (unlikely(!afinfo
))
2199 void xfrm_flush_gc(void)
2201 flush_work(&xfrm_state_gc_work
);
2203 EXPORT_SYMBOL(xfrm_flush_gc
);
2205 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2206 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
2209 struct xfrm_state
*t
= x
->tunnel
;
2211 if (atomic_read(&t
->tunnel_users
) == 2)
2212 xfrm_state_delete(t
);
2213 atomic_dec(&t
->tunnel_users
);
2218 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
2220 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
2222 const struct xfrm_type
*type
= READ_ONCE(x
->type
);
2224 if (x
->km
.state
== XFRM_STATE_VALID
&&
2225 type
&& type
->get_mtu
)
2226 return type
->get_mtu(x
, mtu
);
2228 return mtu
- x
->props
.header_len
;
2231 int __xfrm_init_state(struct xfrm_state
*x
, bool init_replay
, bool offload
)
2233 struct xfrm_state_afinfo
*afinfo
;
2234 struct xfrm_mode
*inner_mode
;
2235 int family
= x
->props
.family
;
2238 err
= -EAFNOSUPPORT
;
2239 afinfo
= xfrm_state_get_afinfo(family
);
2244 if (afinfo
->init_flags
)
2245 err
= afinfo
->init_flags(x
);
2252 err
= -EPROTONOSUPPORT
;
2254 if (x
->sel
.family
!= AF_UNSPEC
) {
2255 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
2256 if (inner_mode
== NULL
)
2259 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2260 family
!= x
->sel
.family
) {
2261 xfrm_put_mode(inner_mode
);
2265 x
->inner_mode
= inner_mode
;
2267 struct xfrm_mode
*inner_mode_iaf
;
2268 int iafamily
= AF_INET
;
2270 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->props
.family
);
2271 if (inner_mode
== NULL
)
2274 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2275 xfrm_put_mode(inner_mode
);
2278 x
->inner_mode
= inner_mode
;
2280 if (x
->props
.family
== AF_INET
)
2281 iafamily
= AF_INET6
;
2283 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, iafamily
);
2284 if (inner_mode_iaf
) {
2285 if (inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)
2286 x
->inner_mode_iaf
= inner_mode_iaf
;
2288 xfrm_put_mode(inner_mode_iaf
);
2292 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2293 if (x
->type
== NULL
)
2296 x
->type_offload
= xfrm_get_type_offload(x
->id
.proto
, family
, offload
);
2298 err
= x
->type
->init_state(x
);
2302 x
->outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2303 if (x
->outer_mode
== NULL
) {
2304 err
= -EPROTONOSUPPORT
;
2309 err
= xfrm_init_replay(x
);
2318 EXPORT_SYMBOL(__xfrm_init_state
);
2320 int xfrm_init_state(struct xfrm_state
*x
)
2324 err
= __xfrm_init_state(x
, true, false);
2326 x
->km
.state
= XFRM_STATE_VALID
;
2331 EXPORT_SYMBOL(xfrm_init_state
);
2333 int __net_init
xfrm_state_init(struct net
*net
)
2337 if (net_eq(net
, &init_net
))
2338 xfrm_state_cache
= KMEM_CACHE(xfrm_state
,
2339 SLAB_HWCACHE_ALIGN
| SLAB_PANIC
);
2341 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2343 sz
= sizeof(struct hlist_head
) * 8;
2345 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2346 if (!net
->xfrm
.state_bydst
)
2348 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2349 if (!net
->xfrm
.state_bysrc
)
2351 net
->xfrm
.state_byspi
= xfrm_hash_alloc(sz
);
2352 if (!net
->xfrm
.state_byspi
)
2354 net
->xfrm
.state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2356 net
->xfrm
.state_num
= 0;
2357 INIT_WORK(&net
->xfrm
.state_hash_work
, xfrm_hash_resize
);
2358 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
2362 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2364 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2369 void xfrm_state_fini(struct net
*net
)
2373 flush_work(&net
->xfrm
.state_hash_work
);
2374 xfrm_state_flush(net
, IPSEC_PROTO_ANY
, false);
2375 flush_work(&xfrm_state_gc_work
);
2377 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2379 sz
= (net
->xfrm
.state_hmask
+ 1) * sizeof(struct hlist_head
);
2380 WARN_ON(!hlist_empty(net
->xfrm
.state_byspi
));
2381 xfrm_hash_free(net
->xfrm
.state_byspi
, sz
);
2382 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2383 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2384 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2385 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2388 #ifdef CONFIG_AUDITSYSCALL
2389 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2390 struct audit_buffer
*audit_buf
)
2392 struct xfrm_sec_ctx
*ctx
= x
->security
;
2393 u32 spi
= ntohl(x
->id
.spi
);
2396 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2397 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2399 switch (x
->props
.family
) {
2401 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2402 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2405 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2406 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2410 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2413 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2414 struct audit_buffer
*audit_buf
)
2416 const struct iphdr
*iph4
;
2417 const struct ipv6hdr
*iph6
;
2422 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2423 &iph4
->saddr
, &iph4
->daddr
);
2426 iph6
= ipv6_hdr(skb
);
2427 audit_log_format(audit_buf
,
2428 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2429 &iph6
->saddr
, &iph6
->daddr
,
2430 iph6
->flow_lbl
[0] & 0x0f,
2437 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
, bool task_valid
)
2439 struct audit_buffer
*audit_buf
;
2441 audit_buf
= xfrm_audit_start("SAD-add");
2442 if (audit_buf
== NULL
)
2444 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2445 xfrm_audit_helper_sainfo(x
, audit_buf
);
2446 audit_log_format(audit_buf
, " res=%u", result
);
2447 audit_log_end(audit_buf
);
2449 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2451 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
, bool task_valid
)
2453 struct audit_buffer
*audit_buf
;
2455 audit_buf
= xfrm_audit_start("SAD-delete");
2456 if (audit_buf
== NULL
)
2458 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2459 xfrm_audit_helper_sainfo(x
, audit_buf
);
2460 audit_log_format(audit_buf
, " res=%u", result
);
2461 audit_log_end(audit_buf
);
2463 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2465 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2466 struct sk_buff
*skb
)
2468 struct audit_buffer
*audit_buf
;
2471 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2472 if (audit_buf
== NULL
)
2474 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2475 /* don't record the sequence number because it's inherent in this kind
2476 * of audit message */
2477 spi
= ntohl(x
->id
.spi
);
2478 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2479 audit_log_end(audit_buf
);
2481 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2483 void xfrm_audit_state_replay(struct xfrm_state
*x
,
2484 struct sk_buff
*skb
, __be32 net_seq
)
2486 struct audit_buffer
*audit_buf
;
2489 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2490 if (audit_buf
== NULL
)
2492 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2493 spi
= ntohl(x
->id
.spi
);
2494 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2495 spi
, spi
, ntohl(net_seq
));
2496 audit_log_end(audit_buf
);
2498 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay
);
2500 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2502 struct audit_buffer
*audit_buf
;
2504 audit_buf
= xfrm_audit_start("SA-notfound");
2505 if (audit_buf
== NULL
)
2507 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2508 audit_log_end(audit_buf
);
2510 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2512 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2513 __be32 net_spi
, __be32 net_seq
)
2515 struct audit_buffer
*audit_buf
;
2518 audit_buf
= xfrm_audit_start("SA-notfound");
2519 if (audit_buf
== NULL
)
2521 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2522 spi
= ntohl(net_spi
);
2523 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2524 spi
, spi
, ntohl(net_seq
));
2525 audit_log_end(audit_buf
);
2527 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2529 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2530 struct sk_buff
*skb
, u8 proto
)
2532 struct audit_buffer
*audit_buf
;
2536 audit_buf
= xfrm_audit_start("SA-icv-failure");
2537 if (audit_buf
== NULL
)
2539 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2540 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2541 u32 spi
= ntohl(net_spi
);
2542 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2543 spi
, spi
, ntohl(net_seq
));
2545 audit_log_end(audit_buf
);
2547 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2548 #endif /* CONFIG_AUDITSYSCALL */