6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
16 #include <linux/workqueue.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
25 #include "xfrm_hash.h"
28 EXPORT_SYMBOL(xfrm_nl
);
30 u32 sysctl_xfrm_aevent_etime __read_mostly
= XFRM_AE_ETIME
;
31 EXPORT_SYMBOL(sysctl_xfrm_aevent_etime
);
33 u32 sysctl_xfrm_aevent_rseqth __read_mostly
= XFRM_AE_SEQT_SIZE
;
34 EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth
);
36 u32 sysctl_xfrm_acq_expires __read_mostly
= 30;
38 /* Each xfrm_state may be linked to two tables:
40 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
41 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
42 destination/tunnel endpoint. (output)
45 static DEFINE_SPINLOCK(xfrm_state_lock
);
47 static struct hlist_head
*xfrm_state_byspi __read_mostly
;
48 static unsigned int xfrm_state_hmask __read_mostly
;
49 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
50 static unsigned int xfrm_state_num
;
51 static unsigned int xfrm_state_genid
;
53 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
);
54 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
);
56 #ifdef CONFIG_AUDITSYSCALL
57 static void xfrm_audit_state_replay(struct xfrm_state
*x
,
58 struct sk_buff
*skb
, __be32 net_seq
);
60 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
61 #endif /* CONFIG_AUDITSYSCALL */
63 static inline unsigned int xfrm_dst_hash(xfrm_address_t
*daddr
,
64 xfrm_address_t
*saddr
,
66 unsigned short family
)
68 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, xfrm_state_hmask
);
71 static inline unsigned int xfrm_src_hash(xfrm_address_t
*daddr
,
72 xfrm_address_t
*saddr
,
73 unsigned short family
)
75 return __xfrm_src_hash(daddr
, saddr
, family
, xfrm_state_hmask
);
78 static inline unsigned int
79 xfrm_spi_hash(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
81 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, xfrm_state_hmask
);
84 static void xfrm_hash_transfer(struct hlist_head
*list
,
85 struct hlist_head
*ndsttable
,
86 struct hlist_head
*nsrctable
,
87 struct hlist_head
*nspitable
,
88 unsigned int nhashmask
)
90 struct hlist_node
*entry
, *tmp
;
93 hlist_for_each_entry_safe(x
, entry
, tmp
, list
, bydst
) {
96 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
97 x
->props
.reqid
, x
->props
.family
,
99 hlist_add_head(&x
->bydst
, ndsttable
+h
);
101 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
104 hlist_add_head(&x
->bysrc
, nsrctable
+h
);
107 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
108 x
->id
.proto
, x
->props
.family
,
110 hlist_add_head(&x
->byspi
, nspitable
+h
);
115 static unsigned long xfrm_hash_new_size(void)
117 return ((xfrm_state_hmask
+ 1) << 1) *
118 sizeof(struct hlist_head
);
121 static DEFINE_MUTEX(hash_resize_mutex
);
123 static void xfrm_hash_resize(struct work_struct
*__unused
)
125 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
126 unsigned long nsize
, osize
;
127 unsigned int nhashmask
, ohashmask
;
130 mutex_lock(&hash_resize_mutex
);
132 nsize
= xfrm_hash_new_size();
133 ndst
= xfrm_hash_alloc(nsize
);
136 nsrc
= xfrm_hash_alloc(nsize
);
138 xfrm_hash_free(ndst
, nsize
);
141 nspi
= xfrm_hash_alloc(nsize
);
143 xfrm_hash_free(ndst
, nsize
);
144 xfrm_hash_free(nsrc
, nsize
);
148 spin_lock_bh(&xfrm_state_lock
);
150 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
151 for (i
= xfrm_state_hmask
; i
>= 0; i
--)
152 xfrm_hash_transfer(init_net
.xfrm
.state_bydst
+i
, ndst
, nsrc
, nspi
,
155 odst
= init_net
.xfrm
.state_bydst
;
156 osrc
= init_net
.xfrm
.state_bysrc
;
157 ospi
= xfrm_state_byspi
;
158 ohashmask
= xfrm_state_hmask
;
160 init_net
.xfrm
.state_bydst
= ndst
;
161 init_net
.xfrm
.state_bysrc
= nsrc
;
162 xfrm_state_byspi
= nspi
;
163 xfrm_state_hmask
= nhashmask
;
165 spin_unlock_bh(&xfrm_state_lock
);
167 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
168 xfrm_hash_free(odst
, osize
);
169 xfrm_hash_free(osrc
, osize
);
170 xfrm_hash_free(ospi
, osize
);
173 mutex_unlock(&hash_resize_mutex
);
176 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
178 DECLARE_WAIT_QUEUE_HEAD(km_waitq
);
179 EXPORT_SYMBOL(km_waitq
);
181 static DEFINE_RWLOCK(xfrm_state_afinfo_lock
);
182 static struct xfrm_state_afinfo
*xfrm_state_afinfo
[NPROTO
];
184 static struct work_struct xfrm_state_gc_work
;
185 static HLIST_HEAD(xfrm_state_gc_list
);
186 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
188 int __xfrm_state_delete(struct xfrm_state
*x
);
190 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
191 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
);
193 static struct xfrm_state_afinfo
*xfrm_state_lock_afinfo(unsigned int family
)
195 struct xfrm_state_afinfo
*afinfo
;
196 if (unlikely(family
>= NPROTO
))
198 write_lock_bh(&xfrm_state_afinfo_lock
);
199 afinfo
= xfrm_state_afinfo
[family
];
200 if (unlikely(!afinfo
))
201 write_unlock_bh(&xfrm_state_afinfo_lock
);
205 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo
*afinfo
)
206 __releases(xfrm_state_afinfo_lock
)
208 write_unlock_bh(&xfrm_state_afinfo_lock
);
211 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
213 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
214 const struct xfrm_type
**typemap
;
217 if (unlikely(afinfo
== NULL
))
218 return -EAFNOSUPPORT
;
219 typemap
= afinfo
->type_map
;
221 if (likely(typemap
[type
->proto
] == NULL
))
222 typemap
[type
->proto
] = type
;
225 xfrm_state_unlock_afinfo(afinfo
);
228 EXPORT_SYMBOL(xfrm_register_type
);
230 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
232 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
233 const struct xfrm_type
**typemap
;
236 if (unlikely(afinfo
== NULL
))
237 return -EAFNOSUPPORT
;
238 typemap
= afinfo
->type_map
;
240 if (unlikely(typemap
[type
->proto
] != type
))
243 typemap
[type
->proto
] = NULL
;
244 xfrm_state_unlock_afinfo(afinfo
);
247 EXPORT_SYMBOL(xfrm_unregister_type
);
249 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
251 struct xfrm_state_afinfo
*afinfo
;
252 const struct xfrm_type
**typemap
;
253 const struct xfrm_type
*type
;
254 int modload_attempted
= 0;
257 afinfo
= xfrm_state_get_afinfo(family
);
258 if (unlikely(afinfo
== NULL
))
260 typemap
= afinfo
->type_map
;
262 type
= typemap
[proto
];
263 if (unlikely(type
&& !try_module_get(type
->owner
)))
265 if (!type
&& !modload_attempted
) {
266 xfrm_state_put_afinfo(afinfo
);
267 request_module("xfrm-type-%d-%d", family
, proto
);
268 modload_attempted
= 1;
272 xfrm_state_put_afinfo(afinfo
);
276 static void xfrm_put_type(const struct xfrm_type
*type
)
278 module_put(type
->owner
);
281 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
283 struct xfrm_state_afinfo
*afinfo
;
284 struct xfrm_mode
**modemap
;
287 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
290 afinfo
= xfrm_state_lock_afinfo(family
);
291 if (unlikely(afinfo
== NULL
))
292 return -EAFNOSUPPORT
;
295 modemap
= afinfo
->mode_map
;
296 if (modemap
[mode
->encap
])
300 if (!try_module_get(afinfo
->owner
))
303 mode
->afinfo
= afinfo
;
304 modemap
[mode
->encap
] = mode
;
308 xfrm_state_unlock_afinfo(afinfo
);
311 EXPORT_SYMBOL(xfrm_register_mode
);
313 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
315 struct xfrm_state_afinfo
*afinfo
;
316 struct xfrm_mode
**modemap
;
319 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
322 afinfo
= xfrm_state_lock_afinfo(family
);
323 if (unlikely(afinfo
== NULL
))
324 return -EAFNOSUPPORT
;
327 modemap
= afinfo
->mode_map
;
328 if (likely(modemap
[mode
->encap
] == mode
)) {
329 modemap
[mode
->encap
] = NULL
;
330 module_put(mode
->afinfo
->owner
);
334 xfrm_state_unlock_afinfo(afinfo
);
337 EXPORT_SYMBOL(xfrm_unregister_mode
);
339 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
341 struct xfrm_state_afinfo
*afinfo
;
342 struct xfrm_mode
*mode
;
343 int modload_attempted
= 0;
345 if (unlikely(encap
>= XFRM_MODE_MAX
))
349 afinfo
= xfrm_state_get_afinfo(family
);
350 if (unlikely(afinfo
== NULL
))
353 mode
= afinfo
->mode_map
[encap
];
354 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
356 if (!mode
&& !modload_attempted
) {
357 xfrm_state_put_afinfo(afinfo
);
358 request_module("xfrm-mode-%d-%d", family
, encap
);
359 modload_attempted
= 1;
363 xfrm_state_put_afinfo(afinfo
);
367 static void xfrm_put_mode(struct xfrm_mode
*mode
)
369 module_put(mode
->owner
);
372 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
374 del_timer_sync(&x
->timer
);
375 del_timer_sync(&x
->rtimer
);
382 xfrm_put_mode(x
->inner_mode
);
383 if (x
->inner_mode_iaf
)
384 xfrm_put_mode(x
->inner_mode_iaf
);
386 xfrm_put_mode(x
->outer_mode
);
388 x
->type
->destructor(x
);
389 xfrm_put_type(x
->type
);
391 security_xfrm_state_free(x
);
395 static void xfrm_state_gc_task(struct work_struct
*data
)
397 struct xfrm_state
*x
;
398 struct hlist_node
*entry
, *tmp
;
399 struct hlist_head gc_list
;
401 spin_lock_bh(&xfrm_state_gc_lock
);
402 hlist_move_list(&xfrm_state_gc_list
, &gc_list
);
403 spin_unlock_bh(&xfrm_state_gc_lock
);
405 hlist_for_each_entry_safe(x
, entry
, tmp
, &gc_list
, gclist
)
406 xfrm_state_gc_destroy(x
);
411 static inline unsigned long make_jiffies(long secs
)
413 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
414 return MAX_SCHEDULE_TIMEOUT
-1;
419 static void xfrm_timer_handler(unsigned long data
)
421 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
422 unsigned long now
= get_seconds();
423 long next
= LONG_MAX
;
428 if (x
->km
.state
== XFRM_STATE_DEAD
)
430 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
432 if (x
->lft
.hard_add_expires_seconds
) {
433 long tmo
= x
->lft
.hard_add_expires_seconds
+
434 x
->curlft
.add_time
- now
;
440 if (x
->lft
.hard_use_expires_seconds
) {
441 long tmo
= x
->lft
.hard_use_expires_seconds
+
442 (x
->curlft
.use_time
? : now
) - now
;
450 if (x
->lft
.soft_add_expires_seconds
) {
451 long tmo
= x
->lft
.soft_add_expires_seconds
+
452 x
->curlft
.add_time
- now
;
458 if (x
->lft
.soft_use_expires_seconds
) {
459 long tmo
= x
->lft
.soft_use_expires_seconds
+
460 (x
->curlft
.use_time
? : now
) - now
;
469 km_state_expired(x
, 0, 0);
471 if (next
!= LONG_MAX
)
472 mod_timer(&x
->timer
, jiffies
+ make_jiffies(next
));
477 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0) {
478 x
->km
.state
= XFRM_STATE_EXPIRED
;
484 err
= __xfrm_state_delete(x
);
485 if (!err
&& x
->id
.spi
)
486 km_state_expired(x
, 1, 0);
488 xfrm_audit_state_delete(x
, err
? 0 : 1,
489 audit_get_loginuid(current
),
490 audit_get_sessionid(current
), 0);
493 spin_unlock(&x
->lock
);
496 static void xfrm_replay_timer_handler(unsigned long data
);
498 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
500 struct xfrm_state
*x
;
502 x
= kzalloc(sizeof(struct xfrm_state
), GFP_ATOMIC
);
505 write_pnet(&x
->xs_net
, net
);
506 atomic_set(&x
->refcnt
, 1);
507 atomic_set(&x
->tunnel_users
, 0);
508 INIT_LIST_HEAD(&x
->km
.all
);
509 INIT_HLIST_NODE(&x
->bydst
);
510 INIT_HLIST_NODE(&x
->bysrc
);
511 INIT_HLIST_NODE(&x
->byspi
);
512 setup_timer(&x
->timer
, xfrm_timer_handler
, (unsigned long)x
);
513 setup_timer(&x
->rtimer
, xfrm_replay_timer_handler
,
515 x
->curlft
.add_time
= get_seconds();
516 x
->lft
.soft_byte_limit
= XFRM_INF
;
517 x
->lft
.soft_packet_limit
= XFRM_INF
;
518 x
->lft
.hard_byte_limit
= XFRM_INF
;
519 x
->lft
.hard_packet_limit
= XFRM_INF
;
520 x
->replay_maxage
= 0;
521 x
->replay_maxdiff
= 0;
522 x
->inner_mode
= NULL
;
523 x
->inner_mode_iaf
= NULL
;
524 spin_lock_init(&x
->lock
);
528 EXPORT_SYMBOL(xfrm_state_alloc
);
530 void __xfrm_state_destroy(struct xfrm_state
*x
)
532 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
534 spin_lock_bh(&xfrm_state_gc_lock
);
535 hlist_add_head(&x
->gclist
, &xfrm_state_gc_list
);
536 spin_unlock_bh(&xfrm_state_gc_lock
);
537 schedule_work(&xfrm_state_gc_work
);
539 EXPORT_SYMBOL(__xfrm_state_destroy
);
541 int __xfrm_state_delete(struct xfrm_state
*x
)
545 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
546 x
->km
.state
= XFRM_STATE_DEAD
;
547 spin_lock(&xfrm_state_lock
);
548 list_del(&x
->km
.all
);
549 hlist_del(&x
->bydst
);
550 hlist_del(&x
->bysrc
);
552 hlist_del(&x
->byspi
);
554 spin_unlock(&xfrm_state_lock
);
556 /* All xfrm_state objects are created by xfrm_state_alloc.
557 * The xfrm_state_alloc call gives a reference, and that
558 * is what we are dropping here.
566 EXPORT_SYMBOL(__xfrm_state_delete
);
568 int xfrm_state_delete(struct xfrm_state
*x
)
572 spin_lock_bh(&x
->lock
);
573 err
= __xfrm_state_delete(x
);
574 spin_unlock_bh(&x
->lock
);
578 EXPORT_SYMBOL(xfrm_state_delete
);
580 #ifdef CONFIG_SECURITY_NETWORK_XFRM
582 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
586 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
587 struct hlist_node
*entry
;
588 struct xfrm_state
*x
;
590 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+i
, bydst
) {
591 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
592 (err
= security_xfrm_state_delete(x
)) != 0) {
593 xfrm_audit_state_delete(x
, 0,
594 audit_info
->loginuid
,
595 audit_info
->sessionid
,
606 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
612 int xfrm_state_flush(u8 proto
, struct xfrm_audit
*audit_info
)
616 spin_lock_bh(&xfrm_state_lock
);
617 err
= xfrm_state_flush_secctx_check(proto
, audit_info
);
621 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
622 struct hlist_node
*entry
;
623 struct xfrm_state
*x
;
625 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+i
, bydst
) {
626 if (!xfrm_state_kern(x
) &&
627 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
629 spin_unlock_bh(&xfrm_state_lock
);
631 err
= xfrm_state_delete(x
);
632 xfrm_audit_state_delete(x
, err
? 0 : 1,
633 audit_info
->loginuid
,
634 audit_info
->sessionid
,
638 spin_lock_bh(&xfrm_state_lock
);
646 spin_unlock_bh(&xfrm_state_lock
);
650 EXPORT_SYMBOL(xfrm_state_flush
);
652 void xfrm_sad_getinfo(struct xfrmk_sadinfo
*si
)
654 spin_lock_bh(&xfrm_state_lock
);
655 si
->sadcnt
= xfrm_state_num
;
656 si
->sadhcnt
= xfrm_state_hmask
;
657 si
->sadhmcnt
= xfrm_state_hashmax
;
658 spin_unlock_bh(&xfrm_state_lock
);
660 EXPORT_SYMBOL(xfrm_sad_getinfo
);
663 xfrm_init_tempsel(struct xfrm_state
*x
, struct flowi
*fl
,
664 struct xfrm_tmpl
*tmpl
,
665 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
666 unsigned short family
)
668 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
671 afinfo
->init_tempsel(x
, fl
, tmpl
, daddr
, saddr
);
672 xfrm_state_put_afinfo(afinfo
);
676 static struct xfrm_state
*__xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
678 unsigned int h
= xfrm_spi_hash(daddr
, spi
, proto
, family
);
679 struct xfrm_state
*x
;
680 struct hlist_node
*entry
;
682 hlist_for_each_entry(x
, entry
, xfrm_state_byspi
+h
, byspi
) {
683 if (x
->props
.family
!= family
||
685 x
->id
.proto
!= proto
)
690 if (x
->id
.daddr
.a4
!= daddr
->a4
)
694 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
708 static struct xfrm_state
*__xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, u8 proto
, unsigned short family
)
710 unsigned int h
= xfrm_src_hash(daddr
, saddr
, family
);
711 struct xfrm_state
*x
;
712 struct hlist_node
*entry
;
714 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bysrc
+h
, bysrc
) {
715 if (x
->props
.family
!= family
||
716 x
->id
.proto
!= proto
)
721 if (x
->id
.daddr
.a4
!= daddr
->a4
||
722 x
->props
.saddr
.a4
!= saddr
->a4
)
726 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
729 !ipv6_addr_equal((struct in6_addr
*)saddr
,
743 static inline struct xfrm_state
*
744 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
747 return __xfrm_state_lookup(&x
->id
.daddr
, x
->id
.spi
,
748 x
->id
.proto
, family
);
750 return __xfrm_state_lookup_byaddr(&x
->id
.daddr
,
752 x
->id
.proto
, family
);
755 static void xfrm_hash_grow_check(int have_hash_collision
)
757 if (have_hash_collision
&&
758 (xfrm_state_hmask
+ 1) < xfrm_state_hashmax
&&
759 xfrm_state_num
> xfrm_state_hmask
)
760 schedule_work(&xfrm_hash_work
);
764 xfrm_state_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
765 struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
766 struct xfrm_policy
*pol
, int *err
,
767 unsigned short family
)
770 struct hlist_node
*entry
;
771 struct xfrm_state
*x
, *x0
, *to_put
;
772 int acquire_in_progress
= 0;
774 struct xfrm_state
*best
= NULL
;
778 spin_lock_bh(&xfrm_state_lock
);
779 h
= xfrm_dst_hash(daddr
, saddr
, tmpl
->reqid
, family
);
780 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
781 if (x
->props
.family
== family
&&
782 x
->props
.reqid
== tmpl
->reqid
&&
783 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
784 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
785 tmpl
->mode
== x
->props
.mode
&&
786 tmpl
->id
.proto
== x
->id
.proto
&&
787 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
)) {
789 1. There is a valid state with matching selector.
791 2. Valid state with inappropriate selector. Skip.
793 Entering area of "sysdeps".
795 3. If state is not valid, selector is temporary,
796 it selects only session which triggered
797 previous resolution. Key manager will do
798 something to install a state with proper
801 if (x
->km
.state
== XFRM_STATE_VALID
) {
802 if ((x
->sel
.family
&& !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
803 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
806 best
->km
.dying
> x
->km
.dying
||
807 (best
->km
.dying
== x
->km
.dying
&&
808 best
->curlft
.add_time
< x
->curlft
.add_time
))
810 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
811 acquire_in_progress
= 1;
812 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
813 x
->km
.state
== XFRM_STATE_EXPIRED
) {
814 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
815 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
822 if (!x
&& !error
&& !acquire_in_progress
) {
824 (x0
= __xfrm_state_lookup(daddr
, tmpl
->id
.spi
,
825 tmpl
->id
.proto
, family
)) != NULL
) {
830 x
= xfrm_state_alloc(&init_net
);
835 /* Initialize temporary selector matching only
836 * to current session. */
837 xfrm_init_tempsel(x
, fl
, tmpl
, daddr
, saddr
, family
);
839 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->secid
);
841 x
->km
.state
= XFRM_STATE_DEAD
;
847 if (km_query(x
, tmpl
, pol
) == 0) {
848 x
->km
.state
= XFRM_STATE_ACQ
;
849 list_add(&x
->km
.all
, &init_net
.xfrm
.state_all
);
850 hlist_add_head(&x
->bydst
, init_net
.xfrm
.state_bydst
+h
);
851 h
= xfrm_src_hash(daddr
, saddr
, family
);
852 hlist_add_head(&x
->bysrc
, init_net
.xfrm
.state_bysrc
+h
);
854 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, family
);
855 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
857 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
858 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
859 add_timer(&x
->timer
);
861 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
863 x
->km
.state
= XFRM_STATE_DEAD
;
873 *err
= acquire_in_progress
? -EAGAIN
: error
;
874 spin_unlock_bh(&xfrm_state_lock
);
876 xfrm_state_put(to_put
);
881 xfrm_stateonly_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
882 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
885 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
886 struct hlist_node
*entry
;
888 spin_lock(&xfrm_state_lock
);
889 h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
890 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
891 if (x
->props
.family
== family
&&
892 x
->props
.reqid
== reqid
&&
893 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
894 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
895 mode
== x
->props
.mode
&&
896 proto
== x
->id
.proto
&&
897 x
->km
.state
== XFRM_STATE_VALID
) {
905 spin_unlock(&xfrm_state_lock
);
910 EXPORT_SYMBOL(xfrm_stateonly_find
);
912 static void __xfrm_state_insert(struct xfrm_state
*x
)
916 x
->genid
= ++xfrm_state_genid
;
918 list_add(&x
->km
.all
, &init_net
.xfrm
.state_all
);
920 h
= xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
921 x
->props
.reqid
, x
->props
.family
);
922 hlist_add_head(&x
->bydst
, init_net
.xfrm
.state_bydst
+h
);
924 h
= xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
925 hlist_add_head(&x
->bysrc
, init_net
.xfrm
.state_bysrc
+h
);
928 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
931 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
934 mod_timer(&x
->timer
, jiffies
+ HZ
);
935 if (x
->replay_maxage
)
936 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
942 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
945 /* xfrm_state_lock is held */
946 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
948 unsigned short family
= xnew
->props
.family
;
949 u32 reqid
= xnew
->props
.reqid
;
950 struct xfrm_state
*x
;
951 struct hlist_node
*entry
;
954 h
= xfrm_dst_hash(&xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
955 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
956 if (x
->props
.family
== family
&&
957 x
->props
.reqid
== reqid
&&
958 !xfrm_addr_cmp(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
959 !xfrm_addr_cmp(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
960 x
->genid
= xfrm_state_genid
;
964 void xfrm_state_insert(struct xfrm_state
*x
)
966 spin_lock_bh(&xfrm_state_lock
);
967 __xfrm_state_bump_genids(x
);
968 __xfrm_state_insert(x
);
969 spin_unlock_bh(&xfrm_state_lock
);
971 EXPORT_SYMBOL(xfrm_state_insert
);
973 /* xfrm_state_lock is held */
974 static struct xfrm_state
*__find_acq_core(unsigned short family
, u8 mode
, u32 reqid
, u8 proto
, xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, int create
)
976 unsigned int h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
977 struct hlist_node
*entry
;
978 struct xfrm_state
*x
;
980 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
981 if (x
->props
.reqid
!= reqid
||
982 x
->props
.mode
!= mode
||
983 x
->props
.family
!= family
||
984 x
->km
.state
!= XFRM_STATE_ACQ
||
986 x
->id
.proto
!= proto
)
991 if (x
->id
.daddr
.a4
!= daddr
->a4
||
992 x
->props
.saddr
.a4
!= saddr
->a4
)
996 if (!ipv6_addr_equal((struct in6_addr
*)x
->id
.daddr
.a6
,
997 (struct in6_addr
*)daddr
) ||
998 !ipv6_addr_equal((struct in6_addr
*)
1000 (struct in6_addr
*)saddr
))
1012 x
= xfrm_state_alloc(&init_net
);
1016 x
->sel
.daddr
.a4
= daddr
->a4
;
1017 x
->sel
.saddr
.a4
= saddr
->a4
;
1018 x
->sel
.prefixlen_d
= 32;
1019 x
->sel
.prefixlen_s
= 32;
1020 x
->props
.saddr
.a4
= saddr
->a4
;
1021 x
->id
.daddr
.a4
= daddr
->a4
;
1025 ipv6_addr_copy((struct in6_addr
*)x
->sel
.daddr
.a6
,
1026 (struct in6_addr
*)daddr
);
1027 ipv6_addr_copy((struct in6_addr
*)x
->sel
.saddr
.a6
,
1028 (struct in6_addr
*)saddr
);
1029 x
->sel
.prefixlen_d
= 128;
1030 x
->sel
.prefixlen_s
= 128;
1031 ipv6_addr_copy((struct in6_addr
*)x
->props
.saddr
.a6
,
1032 (struct in6_addr
*)saddr
);
1033 ipv6_addr_copy((struct in6_addr
*)x
->id
.daddr
.a6
,
1034 (struct in6_addr
*)daddr
);
1038 x
->km
.state
= XFRM_STATE_ACQ
;
1039 x
->id
.proto
= proto
;
1040 x
->props
.family
= family
;
1041 x
->props
.mode
= mode
;
1042 x
->props
.reqid
= reqid
;
1043 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
1045 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
1046 add_timer(&x
->timer
);
1047 list_add(&x
->km
.all
, &init_net
.xfrm
.state_all
);
1048 hlist_add_head(&x
->bydst
, init_net
.xfrm
.state_bydst
+h
);
1049 h
= xfrm_src_hash(daddr
, saddr
, family
);
1050 hlist_add_head(&x
->bysrc
, init_net
.xfrm
.state_bysrc
+h
);
1054 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
1060 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
);
1062 int xfrm_state_add(struct xfrm_state
*x
)
1064 struct xfrm_state
*x1
, *to_put
;
1067 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1069 family
= x
->props
.family
;
1073 spin_lock_bh(&xfrm_state_lock
);
1075 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1083 if (use_spi
&& x
->km
.seq
) {
1084 x1
= __xfrm_find_acq_byseq(x
->km
.seq
);
1085 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1086 xfrm_addr_cmp(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1093 x1
= __find_acq_core(family
, x
->props
.mode
, x
->props
.reqid
,
1095 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1097 __xfrm_state_bump_genids(x
);
1098 __xfrm_state_insert(x
);
1102 spin_unlock_bh(&xfrm_state_lock
);
1105 xfrm_state_delete(x1
);
1110 xfrm_state_put(to_put
);
1114 EXPORT_SYMBOL(xfrm_state_add
);
1116 #ifdef CONFIG_XFRM_MIGRATE
1117 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
, int *errp
)
1120 struct xfrm_state
*x
= xfrm_state_alloc(&init_net
);
1124 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1125 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1126 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1127 x
->props
.mode
= orig
->props
.mode
;
1128 x
->props
.replay_window
= orig
->props
.replay_window
;
1129 x
->props
.reqid
= orig
->props
.reqid
;
1130 x
->props
.family
= orig
->props
.family
;
1131 x
->props
.saddr
= orig
->props
.saddr
;
1134 x
->aalg
= xfrm_algo_clone(orig
->aalg
);
1138 x
->props
.aalgo
= orig
->props
.aalgo
;
1141 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1145 x
->props
.ealgo
= orig
->props
.ealgo
;
1148 x
->calg
= xfrm_algo_clone(orig
->calg
);
1152 x
->props
.calgo
= orig
->props
.calgo
;
1155 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
), GFP_KERNEL
);
1161 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1167 err
= xfrm_init_state(x
);
1171 x
->props
.flags
= orig
->props
.flags
;
1173 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1174 x
->km
.state
= orig
->km
.state
;
1175 x
->km
.seq
= orig
->km
.seq
;
1193 /* xfrm_state_lock is held */
1194 struct xfrm_state
* xfrm_migrate_state_find(struct xfrm_migrate
*m
)
1197 struct xfrm_state
*x
;
1198 struct hlist_node
*entry
;
1201 h
= xfrm_dst_hash(&m
->old_daddr
, &m
->old_saddr
,
1202 m
->reqid
, m
->old_family
);
1203 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
1204 if (x
->props
.mode
!= m
->mode
||
1205 x
->id
.proto
!= m
->proto
)
1207 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1209 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1211 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1218 h
= xfrm_src_hash(&m
->old_daddr
, &m
->old_saddr
,
1220 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bysrc
+h
, bysrc
) {
1221 if (x
->props
.mode
!= m
->mode
||
1222 x
->id
.proto
!= m
->proto
)
1224 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1226 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1236 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1238 struct xfrm_state
* xfrm_state_migrate(struct xfrm_state
*x
,
1239 struct xfrm_migrate
*m
)
1241 struct xfrm_state
*xc
;
1244 xc
= xfrm_state_clone(x
, &err
);
1248 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1249 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1252 if (!xfrm_addr_cmp(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1253 /* a care is needed when the destination address of the
1254 state is to be updated as it is a part of triplet */
1255 xfrm_state_insert(xc
);
1257 if ((err
= xfrm_state_add(xc
)) < 0)
1266 EXPORT_SYMBOL(xfrm_state_migrate
);
1269 int xfrm_state_update(struct xfrm_state
*x
)
1271 struct xfrm_state
*x1
, *to_put
;
1273 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1277 spin_lock_bh(&xfrm_state_lock
);
1278 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1284 if (xfrm_state_kern(x1
)) {
1290 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1291 __xfrm_state_insert(x
);
1297 spin_unlock_bh(&xfrm_state_lock
);
1300 xfrm_state_put(to_put
);
1306 xfrm_state_delete(x1
);
1312 spin_lock_bh(&x1
->lock
);
1313 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1314 if (x
->encap
&& x1
->encap
)
1315 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1316 if (x
->coaddr
&& x1
->coaddr
) {
1317 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1319 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1320 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1321 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1324 mod_timer(&x1
->timer
, jiffies
+ HZ
);
1325 if (x1
->curlft
.use_time
)
1326 xfrm_state_check_expire(x1
);
1330 spin_unlock_bh(&x1
->lock
);
1336 EXPORT_SYMBOL(xfrm_state_update
);
1338 int xfrm_state_check_expire(struct xfrm_state
*x
)
1340 if (!x
->curlft
.use_time
)
1341 x
->curlft
.use_time
= get_seconds();
1343 if (x
->km
.state
!= XFRM_STATE_VALID
)
1346 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1347 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1348 x
->km
.state
= XFRM_STATE_EXPIRED
;
1349 mod_timer(&x
->timer
, jiffies
);
1354 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1355 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1357 km_state_expired(x
, 0, 0);
1361 EXPORT_SYMBOL(xfrm_state_check_expire
);
1364 xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
,
1365 unsigned short family
)
1367 struct xfrm_state
*x
;
1369 spin_lock_bh(&xfrm_state_lock
);
1370 x
= __xfrm_state_lookup(daddr
, spi
, proto
, family
);
1371 spin_unlock_bh(&xfrm_state_lock
);
1374 EXPORT_SYMBOL(xfrm_state_lookup
);
1377 xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1378 u8 proto
, unsigned short family
)
1380 struct xfrm_state
*x
;
1382 spin_lock_bh(&xfrm_state_lock
);
1383 x
= __xfrm_state_lookup_byaddr(daddr
, saddr
, proto
, family
);
1384 spin_unlock_bh(&xfrm_state_lock
);
1387 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1390 xfrm_find_acq(u8 mode
, u32 reqid
, u8 proto
,
1391 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1392 int create
, unsigned short family
)
1394 struct xfrm_state
*x
;
1396 spin_lock_bh(&xfrm_state_lock
);
1397 x
= __find_acq_core(family
, mode
, reqid
, proto
, daddr
, saddr
, create
);
1398 spin_unlock_bh(&xfrm_state_lock
);
1402 EXPORT_SYMBOL(xfrm_find_acq
);
1404 #ifdef CONFIG_XFRM_SUB_POLICY
1406 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1407 unsigned short family
)
1410 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1412 return -EAFNOSUPPORT
;
1414 spin_lock_bh(&xfrm_state_lock
);
1415 if (afinfo
->tmpl_sort
)
1416 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1417 spin_unlock_bh(&xfrm_state_lock
);
1418 xfrm_state_put_afinfo(afinfo
);
1421 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1424 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1425 unsigned short family
)
1428 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1430 return -EAFNOSUPPORT
;
1432 spin_lock_bh(&xfrm_state_lock
);
1433 if (afinfo
->state_sort
)
1434 err
= afinfo
->state_sort(dst
, src
, n
);
1435 spin_unlock_bh(&xfrm_state_lock
);
1436 xfrm_state_put_afinfo(afinfo
);
1439 EXPORT_SYMBOL(xfrm_state_sort
);
1442 /* Silly enough, but I'm lazy to build resolution list */
1444 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
)
1448 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
1449 struct hlist_node
*entry
;
1450 struct xfrm_state
*x
;
1452 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+i
, bydst
) {
1453 if (x
->km
.seq
== seq
&&
1454 x
->km
.state
== XFRM_STATE_ACQ
) {
1463 struct xfrm_state
*xfrm_find_acq_byseq(u32 seq
)
1465 struct xfrm_state
*x
;
1467 spin_lock_bh(&xfrm_state_lock
);
1468 x
= __xfrm_find_acq_byseq(seq
);
1469 spin_unlock_bh(&xfrm_state_lock
);
1472 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1474 u32
xfrm_get_acqseq(void)
1478 static DEFINE_SPINLOCK(acqseq_lock
);
1480 spin_lock_bh(&acqseq_lock
);
1481 res
= (++acqseq
? : ++acqseq
);
1482 spin_unlock_bh(&acqseq_lock
);
1485 EXPORT_SYMBOL(xfrm_get_acqseq
);
1487 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1490 struct xfrm_state
*x0
;
1492 __be32 minspi
= htonl(low
);
1493 __be32 maxspi
= htonl(high
);
1495 spin_lock_bh(&x
->lock
);
1496 if (x
->km
.state
== XFRM_STATE_DEAD
)
1505 if (minspi
== maxspi
) {
1506 x0
= xfrm_state_lookup(&x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1514 for (h
=0; h
<high
-low
+1; h
++) {
1515 spi
= low
+ net_random()%(high
-low
+1);
1516 x0
= xfrm_state_lookup(&x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1518 x
->id
.spi
= htonl(spi
);
1525 spin_lock_bh(&xfrm_state_lock
);
1526 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1527 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
1528 spin_unlock_bh(&xfrm_state_lock
);
1534 spin_unlock_bh(&x
->lock
);
1538 EXPORT_SYMBOL(xfrm_alloc_spi
);
1540 int xfrm_state_walk(struct xfrm_state_walk
*walk
,
1541 int (*func
)(struct xfrm_state
*, int, void*),
1544 struct xfrm_state
*state
;
1545 struct xfrm_state_walk
*x
;
1548 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
1551 spin_lock_bh(&xfrm_state_lock
);
1552 if (list_empty(&walk
->all
))
1553 x
= list_first_entry(&init_net
.xfrm
.state_all
, struct xfrm_state_walk
, all
);
1555 x
= list_entry(&walk
->all
, struct xfrm_state_walk
, all
);
1556 list_for_each_entry_from(x
, &init_net
.xfrm
.state_all
, all
) {
1557 if (x
->state
== XFRM_STATE_DEAD
)
1559 state
= container_of(x
, struct xfrm_state
, km
);
1560 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
1562 err
= func(state
, walk
->seq
, data
);
1564 list_move_tail(&walk
->all
, &x
->all
);
1569 if (walk
->seq
== 0) {
1573 list_del_init(&walk
->all
);
1575 spin_unlock_bh(&xfrm_state_lock
);
1578 EXPORT_SYMBOL(xfrm_state_walk
);
1580 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
)
1582 INIT_LIST_HEAD(&walk
->all
);
1583 walk
->proto
= proto
;
1584 walk
->state
= XFRM_STATE_DEAD
;
1587 EXPORT_SYMBOL(xfrm_state_walk_init
);
1589 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
)
1591 if (list_empty(&walk
->all
))
1594 spin_lock_bh(&xfrm_state_lock
);
1595 list_del(&walk
->all
);
1596 spin_lock_bh(&xfrm_state_lock
);
1598 EXPORT_SYMBOL(xfrm_state_walk_done
);
1601 void xfrm_replay_notify(struct xfrm_state
*x
, int event
)
1604 /* we send notify messages in case
1605 * 1. we updated on of the sequence numbers, and the seqno difference
1606 * is at least x->replay_maxdiff, in this case we also update the
1607 * timeout of our timer function
1608 * 2. if x->replay_maxage has elapsed since last update,
1609 * and there were changes
1611 * The state structure must be locked!
1615 case XFRM_REPLAY_UPDATE
:
1616 if (x
->replay_maxdiff
&&
1617 (x
->replay
.seq
- x
->preplay
.seq
< x
->replay_maxdiff
) &&
1618 (x
->replay
.oseq
- x
->preplay
.oseq
< x
->replay_maxdiff
)) {
1619 if (x
->xflags
& XFRM_TIME_DEFER
)
1620 event
= XFRM_REPLAY_TIMEOUT
;
1627 case XFRM_REPLAY_TIMEOUT
:
1628 if ((x
->replay
.seq
== x
->preplay
.seq
) &&
1629 (x
->replay
.bitmap
== x
->preplay
.bitmap
) &&
1630 (x
->replay
.oseq
== x
->preplay
.oseq
)) {
1631 x
->xflags
|= XFRM_TIME_DEFER
;
1638 memcpy(&x
->preplay
, &x
->replay
, sizeof(struct xfrm_replay_state
));
1639 c
.event
= XFRM_MSG_NEWAE
;
1640 c
.data
.aevent
= event
;
1641 km_state_notify(x
, &c
);
1643 if (x
->replay_maxage
&&
1644 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
1645 x
->xflags
&= ~XFRM_TIME_DEFER
;
1648 static void xfrm_replay_timer_handler(unsigned long data
)
1650 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
1652 spin_lock(&x
->lock
);
1654 if (x
->km
.state
== XFRM_STATE_VALID
) {
1655 if (xfrm_aevent_is_on())
1656 xfrm_replay_notify(x
, XFRM_REPLAY_TIMEOUT
);
1658 x
->xflags
|= XFRM_TIME_DEFER
;
1661 spin_unlock(&x
->lock
);
1664 int xfrm_replay_check(struct xfrm_state
*x
,
1665 struct sk_buff
*skb
, __be32 net_seq
)
1668 u32 seq
= ntohl(net_seq
);
1670 if (unlikely(seq
== 0))
1673 if (likely(seq
> x
->replay
.seq
))
1676 diff
= x
->replay
.seq
- seq
;
1677 if (diff
>= min_t(unsigned int, x
->props
.replay_window
,
1678 sizeof(x
->replay
.bitmap
) * 8)) {
1679 x
->stats
.replay_window
++;
1683 if (x
->replay
.bitmap
& (1U << diff
)) {
1690 xfrm_audit_state_replay(x
, skb
, net_seq
);
1694 void xfrm_replay_advance(struct xfrm_state
*x
, __be32 net_seq
)
1697 u32 seq
= ntohl(net_seq
);
1699 if (seq
> x
->replay
.seq
) {
1700 diff
= seq
- x
->replay
.seq
;
1701 if (diff
< x
->props
.replay_window
)
1702 x
->replay
.bitmap
= ((x
->replay
.bitmap
) << diff
) | 1;
1704 x
->replay
.bitmap
= 1;
1705 x
->replay
.seq
= seq
;
1707 diff
= x
->replay
.seq
- seq
;
1708 x
->replay
.bitmap
|= (1U << diff
);
1711 if (xfrm_aevent_is_on())
1712 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
1715 static LIST_HEAD(xfrm_km_list
);
1716 static DEFINE_RWLOCK(xfrm_km_lock
);
1718 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1720 struct xfrm_mgr
*km
;
1722 read_lock(&xfrm_km_lock
);
1723 list_for_each_entry(km
, &xfrm_km_list
, list
)
1724 if (km
->notify_policy
)
1725 km
->notify_policy(xp
, dir
, c
);
1726 read_unlock(&xfrm_km_lock
);
1729 void km_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1731 struct xfrm_mgr
*km
;
1732 read_lock(&xfrm_km_lock
);
1733 list_for_each_entry(km
, &xfrm_km_list
, list
)
1736 read_unlock(&xfrm_km_lock
);
1739 EXPORT_SYMBOL(km_policy_notify
);
1740 EXPORT_SYMBOL(km_state_notify
);
1742 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
)
1748 c
.event
= XFRM_MSG_EXPIRE
;
1749 km_state_notify(x
, &c
);
1755 EXPORT_SYMBOL(km_state_expired
);
1757 * We send to all registered managers regardless of failure
1758 * We are happy with one success
1760 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1762 int err
= -EINVAL
, acqret
;
1763 struct xfrm_mgr
*km
;
1765 read_lock(&xfrm_km_lock
);
1766 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1767 acqret
= km
->acquire(x
, t
, pol
, XFRM_POLICY_OUT
);
1771 read_unlock(&xfrm_km_lock
);
1774 EXPORT_SYMBOL(km_query
);
1776 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1779 struct xfrm_mgr
*km
;
1781 read_lock(&xfrm_km_lock
);
1782 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1783 if (km
->new_mapping
)
1784 err
= km
->new_mapping(x
, ipaddr
, sport
);
1788 read_unlock(&xfrm_km_lock
);
1791 EXPORT_SYMBOL(km_new_mapping
);
1793 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 pid
)
1799 c
.event
= XFRM_MSG_POLEXPIRE
;
1800 km_policy_notify(pol
, dir
, &c
);
1805 EXPORT_SYMBOL(km_policy_expired
);
1807 #ifdef CONFIG_XFRM_MIGRATE
1808 int km_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1809 struct xfrm_migrate
*m
, int num_migrate
,
1810 struct xfrm_kmaddress
*k
)
1814 struct xfrm_mgr
*km
;
1816 read_lock(&xfrm_km_lock
);
1817 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1819 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
);
1824 read_unlock(&xfrm_km_lock
);
1827 EXPORT_SYMBOL(km_migrate
);
1830 int km_report(u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
1834 struct xfrm_mgr
*km
;
1836 read_lock(&xfrm_km_lock
);
1837 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1839 ret
= km
->report(proto
, sel
, addr
);
1844 read_unlock(&xfrm_km_lock
);
1847 EXPORT_SYMBOL(km_report
);
1849 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
1853 struct xfrm_mgr
*km
;
1854 struct xfrm_policy
*pol
= NULL
;
1856 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
1859 data
= kmalloc(optlen
, GFP_KERNEL
);
1864 if (copy_from_user(data
, optval
, optlen
))
1868 read_lock(&xfrm_km_lock
);
1869 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1870 pol
= km
->compile_policy(sk
, optname
, data
,
1875 read_unlock(&xfrm_km_lock
);
1878 xfrm_sk_policy_insert(sk
, err
, pol
);
1887 EXPORT_SYMBOL(xfrm_user_policy
);
1889 int xfrm_register_km(struct xfrm_mgr
*km
)
1891 write_lock_bh(&xfrm_km_lock
);
1892 list_add_tail(&km
->list
, &xfrm_km_list
);
1893 write_unlock_bh(&xfrm_km_lock
);
1896 EXPORT_SYMBOL(xfrm_register_km
);
1898 int xfrm_unregister_km(struct xfrm_mgr
*km
)
1900 write_lock_bh(&xfrm_km_lock
);
1901 list_del(&km
->list
);
1902 write_unlock_bh(&xfrm_km_lock
);
1905 EXPORT_SYMBOL(xfrm_unregister_km
);
1907 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
1910 if (unlikely(afinfo
== NULL
))
1912 if (unlikely(afinfo
->family
>= NPROTO
))
1913 return -EAFNOSUPPORT
;
1914 write_lock_bh(&xfrm_state_afinfo_lock
);
1915 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
1918 xfrm_state_afinfo
[afinfo
->family
] = afinfo
;
1919 write_unlock_bh(&xfrm_state_afinfo_lock
);
1922 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
1924 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
1927 if (unlikely(afinfo
== NULL
))
1929 if (unlikely(afinfo
->family
>= NPROTO
))
1930 return -EAFNOSUPPORT
;
1931 write_lock_bh(&xfrm_state_afinfo_lock
);
1932 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
1933 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != afinfo
))
1936 xfrm_state_afinfo
[afinfo
->family
] = NULL
;
1938 write_unlock_bh(&xfrm_state_afinfo_lock
);
1941 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
1943 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
1945 struct xfrm_state_afinfo
*afinfo
;
1946 if (unlikely(family
>= NPROTO
))
1948 read_lock(&xfrm_state_afinfo_lock
);
1949 afinfo
= xfrm_state_afinfo
[family
];
1950 if (unlikely(!afinfo
))
1951 read_unlock(&xfrm_state_afinfo_lock
);
1955 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
)
1956 __releases(xfrm_state_afinfo_lock
)
1958 read_unlock(&xfrm_state_afinfo_lock
);
1961 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1962 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
1965 struct xfrm_state
*t
= x
->tunnel
;
1967 if (atomic_read(&t
->tunnel_users
) == 2)
1968 xfrm_state_delete(t
);
1969 atomic_dec(&t
->tunnel_users
);
1974 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
1976 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
1980 spin_lock_bh(&x
->lock
);
1981 if (x
->km
.state
== XFRM_STATE_VALID
&&
1982 x
->type
&& x
->type
->get_mtu
)
1983 res
= x
->type
->get_mtu(x
, mtu
);
1985 res
= mtu
- x
->props
.header_len
;
1986 spin_unlock_bh(&x
->lock
);
1990 int xfrm_init_state(struct xfrm_state
*x
)
1992 struct xfrm_state_afinfo
*afinfo
;
1993 struct xfrm_mode
*inner_mode
;
1994 int family
= x
->props
.family
;
1997 err
= -EAFNOSUPPORT
;
1998 afinfo
= xfrm_state_get_afinfo(family
);
2003 if (afinfo
->init_flags
)
2004 err
= afinfo
->init_flags(x
);
2006 xfrm_state_put_afinfo(afinfo
);
2011 err
= -EPROTONOSUPPORT
;
2013 if (x
->sel
.family
!= AF_UNSPEC
) {
2014 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
2015 if (inner_mode
== NULL
)
2018 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2019 family
!= x
->sel
.family
) {
2020 xfrm_put_mode(inner_mode
);
2024 x
->inner_mode
= inner_mode
;
2026 struct xfrm_mode
*inner_mode_iaf
;
2028 inner_mode
= xfrm_get_mode(x
->props
.mode
, AF_INET
);
2029 if (inner_mode
== NULL
)
2032 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2033 xfrm_put_mode(inner_mode
);
2037 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, AF_INET6
);
2038 if (inner_mode_iaf
== NULL
)
2041 if (!(inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2042 xfrm_put_mode(inner_mode_iaf
);
2046 if (x
->props
.family
== AF_INET
) {
2047 x
->inner_mode
= inner_mode
;
2048 x
->inner_mode_iaf
= inner_mode_iaf
;
2050 x
->inner_mode
= inner_mode_iaf
;
2051 x
->inner_mode_iaf
= inner_mode
;
2055 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2056 if (x
->type
== NULL
)
2059 err
= x
->type
->init_state(x
);
2063 x
->outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2064 if (x
->outer_mode
== NULL
)
2067 x
->km
.state
= XFRM_STATE_VALID
;
2073 EXPORT_SYMBOL(xfrm_init_state
);
2075 int __net_init
xfrm_state_init(struct net
*net
)
2079 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2081 sz
= sizeof(struct hlist_head
) * 8;
2083 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2084 if (!net
->xfrm
.state_bydst
)
2086 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2087 if (!net
->xfrm
.state_bysrc
)
2089 xfrm_state_byspi
= xfrm_hash_alloc(sz
);
2090 xfrm_state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2092 INIT_WORK(&xfrm_state_gc_work
, xfrm_state_gc_task
);
2096 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2101 void xfrm_state_fini(struct net
*net
)
2105 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2107 sz
= (xfrm_state_hmask
+ 1) * sizeof(struct hlist_head
);
2108 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2109 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2110 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2111 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2114 #ifdef CONFIG_AUDITSYSCALL
2115 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2116 struct audit_buffer
*audit_buf
)
2118 struct xfrm_sec_ctx
*ctx
= x
->security
;
2119 u32 spi
= ntohl(x
->id
.spi
);
2122 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2123 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2125 switch(x
->props
.family
) {
2127 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2128 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2131 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2132 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2136 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2139 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2140 struct audit_buffer
*audit_buf
)
2143 struct ipv6hdr
*iph6
;
2148 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2149 &iph4
->saddr
, &iph4
->daddr
);
2152 iph6
= ipv6_hdr(skb
);
2153 audit_log_format(audit_buf
,
2154 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2155 &iph6
->saddr
,&iph6
->daddr
,
2156 iph6
->flow_lbl
[0] & 0x0f,
2163 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
,
2164 uid_t auid
, u32 sessionid
, u32 secid
)
2166 struct audit_buffer
*audit_buf
;
2168 audit_buf
= xfrm_audit_start("SAD-add");
2169 if (audit_buf
== NULL
)
2171 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2172 xfrm_audit_helper_sainfo(x
, audit_buf
);
2173 audit_log_format(audit_buf
, " res=%u", result
);
2174 audit_log_end(audit_buf
);
2176 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2178 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
,
2179 uid_t auid
, u32 sessionid
, u32 secid
)
2181 struct audit_buffer
*audit_buf
;
2183 audit_buf
= xfrm_audit_start("SAD-delete");
2184 if (audit_buf
== NULL
)
2186 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2187 xfrm_audit_helper_sainfo(x
, audit_buf
);
2188 audit_log_format(audit_buf
, " res=%u", result
);
2189 audit_log_end(audit_buf
);
2191 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2193 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2194 struct sk_buff
*skb
)
2196 struct audit_buffer
*audit_buf
;
2199 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2200 if (audit_buf
== NULL
)
2202 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2203 /* don't record the sequence number because it's inherent in this kind
2204 * of audit message */
2205 spi
= ntohl(x
->id
.spi
);
2206 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2207 audit_log_end(audit_buf
);
2209 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2211 static void xfrm_audit_state_replay(struct xfrm_state
*x
,
2212 struct sk_buff
*skb
, __be32 net_seq
)
2214 struct audit_buffer
*audit_buf
;
2217 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2218 if (audit_buf
== NULL
)
2220 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2221 spi
= ntohl(x
->id
.spi
);
2222 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2223 spi
, spi
, ntohl(net_seq
));
2224 audit_log_end(audit_buf
);
2227 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2229 struct audit_buffer
*audit_buf
;
2231 audit_buf
= xfrm_audit_start("SA-notfound");
2232 if (audit_buf
== NULL
)
2234 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2235 audit_log_end(audit_buf
);
2237 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2239 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2240 __be32 net_spi
, __be32 net_seq
)
2242 struct audit_buffer
*audit_buf
;
2245 audit_buf
= xfrm_audit_start("SA-notfound");
2246 if (audit_buf
== NULL
)
2248 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2249 spi
= ntohl(net_spi
);
2250 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2251 spi
, spi
, ntohl(net_seq
));
2252 audit_log_end(audit_buf
);
2254 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2256 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2257 struct sk_buff
*skb
, u8 proto
)
2259 struct audit_buffer
*audit_buf
;
2263 audit_buf
= xfrm_audit_start("SA-icv-failure");
2264 if (audit_buf
== NULL
)
2266 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2267 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2268 u32 spi
= ntohl(net_spi
);
2269 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2270 spi
, spi
, ntohl(net_seq
));
2272 audit_log_end(audit_buf
);
2274 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2275 #endif /* CONFIG_AUDITSYSCALL */