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 <asm/uaccess.h>
24 #include "xfrm_hash.h"
27 EXPORT_SYMBOL(xfrm_nl
);
29 u32 sysctl_xfrm_aevent_etime __read_mostly
= XFRM_AE_ETIME
;
30 EXPORT_SYMBOL(sysctl_xfrm_aevent_etime
);
32 u32 sysctl_xfrm_aevent_rseqth __read_mostly
= XFRM_AE_SEQT_SIZE
;
33 EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth
);
35 u32 sysctl_xfrm_acq_expires __read_mostly
= 30;
37 /* Each xfrm_state may be linked to two tables:
39 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
40 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
41 destination/tunnel endpoint. (output)
44 static DEFINE_SPINLOCK(xfrm_state_lock
);
46 /* Hash table to find appropriate SA towards given target (endpoint
47 * of tunnel or destination of transport mode) allowed by selector.
49 * Main use is finding SA after policy selected tunnel or transport mode.
50 * Also, it can be used by ah/esp icmp error handler to find offending SA.
52 static struct hlist_head
*xfrm_state_bydst __read_mostly
;
53 static struct hlist_head
*xfrm_state_bysrc __read_mostly
;
54 static struct hlist_head
*xfrm_state_byspi __read_mostly
;
55 static unsigned int xfrm_state_hmask __read_mostly
;
56 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
57 static unsigned int xfrm_state_num
;
58 static unsigned int xfrm_state_genid
;
60 static inline unsigned int xfrm_dst_hash(xfrm_address_t
*daddr
,
61 xfrm_address_t
*saddr
,
63 unsigned short family
)
65 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, xfrm_state_hmask
);
68 static inline unsigned int xfrm_src_hash(xfrm_address_t
*daddr
,
69 xfrm_address_t
*saddr
,
70 unsigned short family
)
72 return __xfrm_src_hash(daddr
, saddr
, family
, xfrm_state_hmask
);
75 static inline unsigned int
76 xfrm_spi_hash(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
78 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, xfrm_state_hmask
);
81 static void xfrm_hash_transfer(struct hlist_head
*list
,
82 struct hlist_head
*ndsttable
,
83 struct hlist_head
*nsrctable
,
84 struct hlist_head
*nspitable
,
85 unsigned int nhashmask
)
87 struct hlist_node
*entry
, *tmp
;
90 hlist_for_each_entry_safe(x
, entry
, tmp
, list
, bydst
) {
93 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
94 x
->props
.reqid
, x
->props
.family
,
96 hlist_add_head(&x
->bydst
, ndsttable
+h
);
98 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
101 hlist_add_head(&x
->bysrc
, nsrctable
+h
);
104 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
105 x
->id
.proto
, x
->props
.family
,
107 hlist_add_head(&x
->byspi
, nspitable
+h
);
112 static unsigned long xfrm_hash_new_size(void)
114 return ((xfrm_state_hmask
+ 1) << 1) *
115 sizeof(struct hlist_head
);
118 static DEFINE_MUTEX(hash_resize_mutex
);
120 static void xfrm_hash_resize(struct work_struct
*__unused
)
122 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
123 unsigned long nsize
, osize
;
124 unsigned int nhashmask
, ohashmask
;
127 mutex_lock(&hash_resize_mutex
);
129 nsize
= xfrm_hash_new_size();
130 ndst
= xfrm_hash_alloc(nsize
);
133 nsrc
= xfrm_hash_alloc(nsize
);
135 xfrm_hash_free(ndst
, nsize
);
138 nspi
= xfrm_hash_alloc(nsize
);
140 xfrm_hash_free(ndst
, nsize
);
141 xfrm_hash_free(nsrc
, nsize
);
145 spin_lock_bh(&xfrm_state_lock
);
147 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
148 for (i
= xfrm_state_hmask
; i
>= 0; i
--)
149 xfrm_hash_transfer(xfrm_state_bydst
+i
, ndst
, nsrc
, nspi
,
152 odst
= xfrm_state_bydst
;
153 osrc
= xfrm_state_bysrc
;
154 ospi
= xfrm_state_byspi
;
155 ohashmask
= xfrm_state_hmask
;
157 xfrm_state_bydst
= ndst
;
158 xfrm_state_bysrc
= nsrc
;
159 xfrm_state_byspi
= nspi
;
160 xfrm_state_hmask
= nhashmask
;
162 spin_unlock_bh(&xfrm_state_lock
);
164 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
165 xfrm_hash_free(odst
, osize
);
166 xfrm_hash_free(osrc
, osize
);
167 xfrm_hash_free(ospi
, osize
);
170 mutex_unlock(&hash_resize_mutex
);
173 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
175 DECLARE_WAIT_QUEUE_HEAD(km_waitq
);
176 EXPORT_SYMBOL(km_waitq
);
178 static DEFINE_RWLOCK(xfrm_state_afinfo_lock
);
179 static struct xfrm_state_afinfo
*xfrm_state_afinfo
[NPROTO
];
181 static struct work_struct xfrm_state_gc_work
;
182 static HLIST_HEAD(xfrm_state_gc_list
);
183 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
185 int __xfrm_state_delete(struct xfrm_state
*x
);
187 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
188 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
);
190 static struct xfrm_state_afinfo
*xfrm_state_lock_afinfo(unsigned int family
)
192 struct xfrm_state_afinfo
*afinfo
;
193 if (unlikely(family
>= NPROTO
))
195 write_lock_bh(&xfrm_state_afinfo_lock
);
196 afinfo
= xfrm_state_afinfo
[family
];
197 if (unlikely(!afinfo
))
198 write_unlock_bh(&xfrm_state_afinfo_lock
);
202 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo
*afinfo
)
204 write_unlock_bh(&xfrm_state_afinfo_lock
);
207 int xfrm_register_type(struct xfrm_type
*type
, unsigned short family
)
209 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
210 struct xfrm_type
**typemap
;
213 if (unlikely(afinfo
== NULL
))
214 return -EAFNOSUPPORT
;
215 typemap
= afinfo
->type_map
;
217 if (likely(typemap
[type
->proto
] == NULL
))
218 typemap
[type
->proto
] = type
;
221 xfrm_state_unlock_afinfo(afinfo
);
224 EXPORT_SYMBOL(xfrm_register_type
);
226 int xfrm_unregister_type(struct xfrm_type
*type
, unsigned short family
)
228 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
229 struct xfrm_type
**typemap
;
232 if (unlikely(afinfo
== NULL
))
233 return -EAFNOSUPPORT
;
234 typemap
= afinfo
->type_map
;
236 if (unlikely(typemap
[type
->proto
] != type
))
239 typemap
[type
->proto
] = NULL
;
240 xfrm_state_unlock_afinfo(afinfo
);
243 EXPORT_SYMBOL(xfrm_unregister_type
);
245 static struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
247 struct xfrm_state_afinfo
*afinfo
;
248 struct xfrm_type
**typemap
;
249 struct xfrm_type
*type
;
250 int modload_attempted
= 0;
253 afinfo
= xfrm_state_get_afinfo(family
);
254 if (unlikely(afinfo
== NULL
))
256 typemap
= afinfo
->type_map
;
258 type
= typemap
[proto
];
259 if (unlikely(type
&& !try_module_get(type
->owner
)))
261 if (!type
&& !modload_attempted
) {
262 xfrm_state_put_afinfo(afinfo
);
263 request_module("xfrm-type-%d-%d", family
, proto
);
264 modload_attempted
= 1;
268 xfrm_state_put_afinfo(afinfo
);
272 static void xfrm_put_type(struct xfrm_type
*type
)
274 module_put(type
->owner
);
277 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
279 struct xfrm_state_afinfo
*afinfo
;
280 struct xfrm_mode
**modemap
;
283 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
286 afinfo
= xfrm_state_lock_afinfo(family
);
287 if (unlikely(afinfo
== NULL
))
288 return -EAFNOSUPPORT
;
291 modemap
= afinfo
->mode_map
;
292 if (likely(modemap
[mode
->encap
] == NULL
)) {
293 modemap
[mode
->encap
] = mode
;
297 xfrm_state_unlock_afinfo(afinfo
);
300 EXPORT_SYMBOL(xfrm_register_mode
);
302 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
304 struct xfrm_state_afinfo
*afinfo
;
305 struct xfrm_mode
**modemap
;
308 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
311 afinfo
= xfrm_state_lock_afinfo(family
);
312 if (unlikely(afinfo
== NULL
))
313 return -EAFNOSUPPORT
;
316 modemap
= afinfo
->mode_map
;
317 if (likely(modemap
[mode
->encap
] == mode
)) {
318 modemap
[mode
->encap
] = NULL
;
322 xfrm_state_unlock_afinfo(afinfo
);
325 EXPORT_SYMBOL(xfrm_unregister_mode
);
327 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
329 struct xfrm_state_afinfo
*afinfo
;
330 struct xfrm_mode
*mode
;
331 int modload_attempted
= 0;
333 if (unlikely(encap
>= XFRM_MODE_MAX
))
337 afinfo
= xfrm_state_get_afinfo(family
);
338 if (unlikely(afinfo
== NULL
))
341 mode
= afinfo
->mode_map
[encap
];
342 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
344 if (!mode
&& !modload_attempted
) {
345 xfrm_state_put_afinfo(afinfo
);
346 request_module("xfrm-mode-%d-%d", family
, encap
);
347 modload_attempted
= 1;
351 xfrm_state_put_afinfo(afinfo
);
355 static void xfrm_put_mode(struct xfrm_mode
*mode
)
357 module_put(mode
->owner
);
360 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
362 del_timer_sync(&x
->timer
);
363 del_timer_sync(&x
->rtimer
);
370 xfrm_put_mode(x
->mode
);
372 x
->type
->destructor(x
);
373 xfrm_put_type(x
->type
);
375 security_xfrm_state_free(x
);
379 static void xfrm_state_gc_task(struct work_struct
*data
)
381 struct xfrm_state
*x
;
382 struct hlist_node
*entry
, *tmp
;
383 struct hlist_head gc_list
;
385 spin_lock_bh(&xfrm_state_gc_lock
);
386 gc_list
.first
= xfrm_state_gc_list
.first
;
387 INIT_HLIST_HEAD(&xfrm_state_gc_list
);
388 spin_unlock_bh(&xfrm_state_gc_lock
);
390 hlist_for_each_entry_safe(x
, entry
, tmp
, &gc_list
, bydst
)
391 xfrm_state_gc_destroy(x
);
396 static inline unsigned long make_jiffies(long secs
)
398 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
399 return MAX_SCHEDULE_TIMEOUT
-1;
404 static void xfrm_timer_handler(unsigned long data
)
406 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
407 unsigned long now
= get_seconds();
408 long next
= LONG_MAX
;
413 if (x
->km
.state
== XFRM_STATE_DEAD
)
415 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
417 if (x
->lft
.hard_add_expires_seconds
) {
418 long tmo
= x
->lft
.hard_add_expires_seconds
+
419 x
->curlft
.add_time
- now
;
425 if (x
->lft
.hard_use_expires_seconds
) {
426 long tmo
= x
->lft
.hard_use_expires_seconds
+
427 (x
->curlft
.use_time
? : now
) - now
;
435 if (x
->lft
.soft_add_expires_seconds
) {
436 long tmo
= x
->lft
.soft_add_expires_seconds
+
437 x
->curlft
.add_time
- now
;
443 if (x
->lft
.soft_use_expires_seconds
) {
444 long tmo
= x
->lft
.soft_use_expires_seconds
+
445 (x
->curlft
.use_time
? : now
) - now
;
454 km_state_expired(x
, 0, 0);
456 if (next
!= LONG_MAX
)
457 mod_timer(&x
->timer
, jiffies
+ make_jiffies(next
));
462 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0) {
463 x
->km
.state
= XFRM_STATE_EXPIRED
;
469 err
= __xfrm_state_delete(x
);
470 if (!err
&& x
->id
.spi
)
471 km_state_expired(x
, 1, 0);
473 xfrm_audit_state_delete(x
, err
? 0 : 1,
474 audit_get_loginuid(current
->audit_context
), 0);
477 spin_unlock(&x
->lock
);
480 static void xfrm_replay_timer_handler(unsigned long data
);
482 struct xfrm_state
*xfrm_state_alloc(void)
484 struct xfrm_state
*x
;
486 x
= kzalloc(sizeof(struct xfrm_state
), GFP_ATOMIC
);
489 atomic_set(&x
->refcnt
, 1);
490 atomic_set(&x
->tunnel_users
, 0);
491 INIT_HLIST_NODE(&x
->bydst
);
492 INIT_HLIST_NODE(&x
->bysrc
);
493 INIT_HLIST_NODE(&x
->byspi
);
494 init_timer(&x
->timer
);
495 x
->timer
.function
= xfrm_timer_handler
;
496 x
->timer
.data
= (unsigned long)x
;
497 init_timer(&x
->rtimer
);
498 x
->rtimer
.function
= xfrm_replay_timer_handler
;
499 x
->rtimer
.data
= (unsigned long)x
;
500 x
->curlft
.add_time
= get_seconds();
501 x
->lft
.soft_byte_limit
= XFRM_INF
;
502 x
->lft
.soft_packet_limit
= XFRM_INF
;
503 x
->lft
.hard_byte_limit
= XFRM_INF
;
504 x
->lft
.hard_packet_limit
= XFRM_INF
;
505 x
->replay_maxage
= 0;
506 x
->replay_maxdiff
= 0;
507 spin_lock_init(&x
->lock
);
511 EXPORT_SYMBOL(xfrm_state_alloc
);
513 void __xfrm_state_destroy(struct xfrm_state
*x
)
515 BUG_TRAP(x
->km
.state
== XFRM_STATE_DEAD
);
517 spin_lock_bh(&xfrm_state_gc_lock
);
518 hlist_add_head(&x
->bydst
, &xfrm_state_gc_list
);
519 spin_unlock_bh(&xfrm_state_gc_lock
);
520 schedule_work(&xfrm_state_gc_work
);
522 EXPORT_SYMBOL(__xfrm_state_destroy
);
524 int __xfrm_state_delete(struct xfrm_state
*x
)
528 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
529 x
->km
.state
= XFRM_STATE_DEAD
;
530 spin_lock(&xfrm_state_lock
);
531 hlist_del(&x
->bydst
);
532 hlist_del(&x
->bysrc
);
534 hlist_del(&x
->byspi
);
536 spin_unlock(&xfrm_state_lock
);
538 /* All xfrm_state objects are created by xfrm_state_alloc.
539 * The xfrm_state_alloc call gives a reference, and that
540 * is what we are dropping here.
548 EXPORT_SYMBOL(__xfrm_state_delete
);
550 int xfrm_state_delete(struct xfrm_state
*x
)
554 spin_lock_bh(&x
->lock
);
555 err
= __xfrm_state_delete(x
);
556 spin_unlock_bh(&x
->lock
);
560 EXPORT_SYMBOL(xfrm_state_delete
);
562 #ifdef CONFIG_SECURITY_NETWORK_XFRM
564 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
568 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
569 struct hlist_node
*entry
;
570 struct xfrm_state
*x
;
572 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
573 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
574 (err
= security_xfrm_state_delete(x
)) != 0) {
575 xfrm_audit_state_delete(x
, 0,
576 audit_info
->loginuid
,
587 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
593 int xfrm_state_flush(u8 proto
, struct xfrm_audit
*audit_info
)
597 spin_lock_bh(&xfrm_state_lock
);
598 err
= xfrm_state_flush_secctx_check(proto
, audit_info
);
602 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
603 struct hlist_node
*entry
;
604 struct xfrm_state
*x
;
606 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
607 if (!xfrm_state_kern(x
) &&
608 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
610 spin_unlock_bh(&xfrm_state_lock
);
612 err
= xfrm_state_delete(x
);
613 xfrm_audit_state_delete(x
, err
? 0 : 1,
614 audit_info
->loginuid
,
618 spin_lock_bh(&xfrm_state_lock
);
626 spin_unlock_bh(&xfrm_state_lock
);
630 EXPORT_SYMBOL(xfrm_state_flush
);
632 void xfrm_sad_getinfo(struct xfrmk_sadinfo
*si
)
634 spin_lock_bh(&xfrm_state_lock
);
635 si
->sadcnt
= xfrm_state_num
;
636 si
->sadhcnt
= xfrm_state_hmask
;
637 si
->sadhmcnt
= xfrm_state_hashmax
;
638 spin_unlock_bh(&xfrm_state_lock
);
640 EXPORT_SYMBOL(xfrm_sad_getinfo
);
643 xfrm_init_tempsel(struct xfrm_state
*x
, struct flowi
*fl
,
644 struct xfrm_tmpl
*tmpl
,
645 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
646 unsigned short family
)
648 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
651 afinfo
->init_tempsel(x
, fl
, tmpl
, daddr
, saddr
);
652 xfrm_state_put_afinfo(afinfo
);
656 static struct xfrm_state
*__xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
658 unsigned int h
= xfrm_spi_hash(daddr
, spi
, proto
, family
);
659 struct xfrm_state
*x
;
660 struct hlist_node
*entry
;
662 hlist_for_each_entry(x
, entry
, xfrm_state_byspi
+h
, byspi
) {
663 if (x
->props
.family
!= family
||
665 x
->id
.proto
!= proto
)
670 if (x
->id
.daddr
.a4
!= daddr
->a4
)
674 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
688 static struct xfrm_state
*__xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, u8 proto
, unsigned short family
)
690 unsigned int h
= xfrm_src_hash(daddr
, saddr
, family
);
691 struct xfrm_state
*x
;
692 struct hlist_node
*entry
;
694 hlist_for_each_entry(x
, entry
, xfrm_state_bysrc
+h
, bysrc
) {
695 if (x
->props
.family
!= family
||
696 x
->id
.proto
!= proto
)
701 if (x
->id
.daddr
.a4
!= daddr
->a4
||
702 x
->props
.saddr
.a4
!= saddr
->a4
)
706 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
709 !ipv6_addr_equal((struct in6_addr
*)saddr
,
723 static inline struct xfrm_state
*
724 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
727 return __xfrm_state_lookup(&x
->id
.daddr
, x
->id
.spi
,
728 x
->id
.proto
, family
);
730 return __xfrm_state_lookup_byaddr(&x
->id
.daddr
,
732 x
->id
.proto
, family
);
735 static void xfrm_hash_grow_check(int have_hash_collision
)
737 if (have_hash_collision
&&
738 (xfrm_state_hmask
+ 1) < xfrm_state_hashmax
&&
739 xfrm_state_num
> xfrm_state_hmask
)
740 schedule_work(&xfrm_hash_work
);
744 xfrm_state_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
745 struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
746 struct xfrm_policy
*pol
, int *err
,
747 unsigned short family
)
749 unsigned int h
= xfrm_dst_hash(daddr
, saddr
, tmpl
->reqid
, family
);
750 struct hlist_node
*entry
;
751 struct xfrm_state
*x
, *x0
;
752 int acquire_in_progress
= 0;
754 struct xfrm_state
*best
= NULL
;
756 spin_lock_bh(&xfrm_state_lock
);
757 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
758 if (x
->props
.family
== family
&&
759 x
->props
.reqid
== tmpl
->reqid
&&
760 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
761 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
762 tmpl
->mode
== x
->props
.mode
&&
763 tmpl
->id
.proto
== x
->id
.proto
&&
764 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
)) {
766 1. There is a valid state with matching selector.
768 2. Valid state with inappropriate selector. Skip.
770 Entering area of "sysdeps".
772 3. If state is not valid, selector is temporary,
773 it selects only session which triggered
774 previous resolution. Key manager will do
775 something to install a state with proper
778 if (x
->km
.state
== XFRM_STATE_VALID
) {
779 if (!xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) ||
780 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
783 best
->km
.dying
> x
->km
.dying
||
784 (best
->km
.dying
== x
->km
.dying
&&
785 best
->curlft
.add_time
< x
->curlft
.add_time
))
787 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
788 acquire_in_progress
= 1;
789 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
790 x
->km
.state
== XFRM_STATE_EXPIRED
) {
791 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
792 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
799 if (!x
&& !error
&& !acquire_in_progress
) {
801 (x0
= __xfrm_state_lookup(daddr
, tmpl
->id
.spi
,
802 tmpl
->id
.proto
, family
)) != NULL
) {
807 x
= xfrm_state_alloc();
812 /* Initialize temporary selector matching only
813 * to current session. */
814 xfrm_init_tempsel(x
, fl
, tmpl
, daddr
, saddr
, family
);
816 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->secid
);
818 x
->km
.state
= XFRM_STATE_DEAD
;
824 if (km_query(x
, tmpl
, pol
) == 0) {
825 x
->km
.state
= XFRM_STATE_ACQ
;
826 hlist_add_head(&x
->bydst
, xfrm_state_bydst
+h
);
827 h
= xfrm_src_hash(daddr
, saddr
, family
);
828 hlist_add_head(&x
->bysrc
, xfrm_state_bysrc
+h
);
830 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, family
);
831 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
833 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
834 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
835 add_timer(&x
->timer
);
837 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
839 x
->km
.state
= XFRM_STATE_DEAD
;
849 *err
= acquire_in_progress
? -EAGAIN
: error
;
850 spin_unlock_bh(&xfrm_state_lock
);
855 xfrm_stateonly_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
856 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
858 unsigned int h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
859 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
860 struct hlist_node
*entry
;
862 spin_lock(&xfrm_state_lock
);
863 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
864 if (x
->props
.family
== family
&&
865 x
->props
.reqid
== reqid
&&
866 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
867 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
868 mode
== x
->props
.mode
&&
869 proto
== x
->id
.proto
&&
870 x
->km
.state
== XFRM_STATE_VALID
) {
878 spin_unlock(&xfrm_state_lock
);
883 EXPORT_SYMBOL(xfrm_stateonly_find
);
885 static void __xfrm_state_insert(struct xfrm_state
*x
)
889 x
->genid
= ++xfrm_state_genid
;
891 h
= xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
892 x
->props
.reqid
, x
->props
.family
);
893 hlist_add_head(&x
->bydst
, xfrm_state_bydst
+h
);
895 h
= xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
896 hlist_add_head(&x
->bysrc
, xfrm_state_bysrc
+h
);
899 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
902 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
905 mod_timer(&x
->timer
, jiffies
+ HZ
);
906 if (x
->replay_maxage
)
907 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
913 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
916 /* xfrm_state_lock is held */
917 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
919 unsigned short family
= xnew
->props
.family
;
920 u32 reqid
= xnew
->props
.reqid
;
921 struct xfrm_state
*x
;
922 struct hlist_node
*entry
;
925 h
= xfrm_dst_hash(&xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
926 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
927 if (x
->props
.family
== family
&&
928 x
->props
.reqid
== reqid
&&
929 !xfrm_addr_cmp(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
930 !xfrm_addr_cmp(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
931 x
->genid
= xfrm_state_genid
;
935 void xfrm_state_insert(struct xfrm_state
*x
)
937 spin_lock_bh(&xfrm_state_lock
);
938 __xfrm_state_bump_genids(x
);
939 __xfrm_state_insert(x
);
940 spin_unlock_bh(&xfrm_state_lock
);
942 EXPORT_SYMBOL(xfrm_state_insert
);
944 /* xfrm_state_lock is held */
945 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
)
947 unsigned int h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
948 struct hlist_node
*entry
;
949 struct xfrm_state
*x
;
951 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
952 if (x
->props
.reqid
!= reqid
||
953 x
->props
.mode
!= mode
||
954 x
->props
.family
!= family
||
955 x
->km
.state
!= XFRM_STATE_ACQ
||
957 x
->id
.proto
!= proto
)
962 if (x
->id
.daddr
.a4
!= daddr
->a4
||
963 x
->props
.saddr
.a4
!= saddr
->a4
)
967 if (!ipv6_addr_equal((struct in6_addr
*)x
->id
.daddr
.a6
,
968 (struct in6_addr
*)daddr
) ||
969 !ipv6_addr_equal((struct in6_addr
*)
971 (struct in6_addr
*)saddr
))
983 x
= xfrm_state_alloc();
987 x
->sel
.daddr
.a4
= daddr
->a4
;
988 x
->sel
.saddr
.a4
= saddr
->a4
;
989 x
->sel
.prefixlen_d
= 32;
990 x
->sel
.prefixlen_s
= 32;
991 x
->props
.saddr
.a4
= saddr
->a4
;
992 x
->id
.daddr
.a4
= daddr
->a4
;
996 ipv6_addr_copy((struct in6_addr
*)x
->sel
.daddr
.a6
,
997 (struct in6_addr
*)daddr
);
998 ipv6_addr_copy((struct in6_addr
*)x
->sel
.saddr
.a6
,
999 (struct in6_addr
*)saddr
);
1000 x
->sel
.prefixlen_d
= 128;
1001 x
->sel
.prefixlen_s
= 128;
1002 ipv6_addr_copy((struct in6_addr
*)x
->props
.saddr
.a6
,
1003 (struct in6_addr
*)saddr
);
1004 ipv6_addr_copy((struct in6_addr
*)x
->id
.daddr
.a6
,
1005 (struct in6_addr
*)daddr
);
1009 x
->km
.state
= XFRM_STATE_ACQ
;
1010 x
->id
.proto
= proto
;
1011 x
->props
.family
= family
;
1012 x
->props
.mode
= mode
;
1013 x
->props
.reqid
= reqid
;
1014 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
1016 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
1017 add_timer(&x
->timer
);
1018 hlist_add_head(&x
->bydst
, xfrm_state_bydst
+h
);
1019 h
= xfrm_src_hash(daddr
, saddr
, family
);
1020 hlist_add_head(&x
->bysrc
, xfrm_state_bysrc
+h
);
1024 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
1030 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
);
1032 int xfrm_state_add(struct xfrm_state
*x
)
1034 struct xfrm_state
*x1
;
1037 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1039 family
= x
->props
.family
;
1041 spin_lock_bh(&xfrm_state_lock
);
1043 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1051 if (use_spi
&& x
->km
.seq
) {
1052 x1
= __xfrm_find_acq_byseq(x
->km
.seq
);
1053 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1054 xfrm_addr_cmp(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1061 x1
= __find_acq_core(family
, x
->props
.mode
, x
->props
.reqid
,
1063 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1065 __xfrm_state_bump_genids(x
);
1066 __xfrm_state_insert(x
);
1070 spin_unlock_bh(&xfrm_state_lock
);
1073 xfrm_state_delete(x1
);
1079 EXPORT_SYMBOL(xfrm_state_add
);
1081 #ifdef CONFIG_XFRM_MIGRATE
1082 struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
, int *errp
)
1085 struct xfrm_state
*x
= xfrm_state_alloc();
1089 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1090 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1091 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1092 x
->props
.mode
= orig
->props
.mode
;
1093 x
->props
.replay_window
= orig
->props
.replay_window
;
1094 x
->props
.reqid
= orig
->props
.reqid
;
1095 x
->props
.family
= orig
->props
.family
;
1096 x
->props
.saddr
= orig
->props
.saddr
;
1099 x
->aalg
= xfrm_algo_clone(orig
->aalg
);
1103 x
->props
.aalgo
= orig
->props
.aalgo
;
1106 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1110 x
->props
.ealgo
= orig
->props
.ealgo
;
1113 x
->calg
= xfrm_algo_clone(orig
->calg
);
1117 x
->props
.calgo
= orig
->props
.calgo
;
1120 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
), GFP_KERNEL
);
1126 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1132 err
= xfrm_init_state(x
);
1136 x
->props
.flags
= orig
->props
.flags
;
1138 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1139 x
->km
.state
= orig
->km
.state
;
1140 x
->km
.seq
= orig
->km
.seq
;
1157 EXPORT_SYMBOL(xfrm_state_clone
);
1159 /* xfrm_state_lock is held */
1160 struct xfrm_state
* xfrm_migrate_state_find(struct xfrm_migrate
*m
)
1163 struct xfrm_state
*x
;
1164 struct hlist_node
*entry
;
1167 h
= xfrm_dst_hash(&m
->old_daddr
, &m
->old_saddr
,
1168 m
->reqid
, m
->old_family
);
1169 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
1170 if (x
->props
.mode
!= m
->mode
||
1171 x
->id
.proto
!= m
->proto
)
1173 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1175 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1177 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1184 h
= xfrm_src_hash(&m
->old_daddr
, &m
->old_saddr
,
1186 hlist_for_each_entry(x
, entry
, xfrm_state_bysrc
+h
, bysrc
) {
1187 if (x
->props
.mode
!= m
->mode
||
1188 x
->id
.proto
!= m
->proto
)
1190 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1192 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1202 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1204 struct xfrm_state
* xfrm_state_migrate(struct xfrm_state
*x
,
1205 struct xfrm_migrate
*m
)
1207 struct xfrm_state
*xc
;
1210 xc
= xfrm_state_clone(x
, &err
);
1214 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1215 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1218 if (!xfrm_addr_cmp(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1219 /* a care is needed when the destination address of the
1220 state is to be updated as it is a part of triplet */
1221 xfrm_state_insert(xc
);
1223 if ((err
= xfrm_state_add(xc
)) < 0)
1232 EXPORT_SYMBOL(xfrm_state_migrate
);
1235 int xfrm_state_update(struct xfrm_state
*x
)
1237 struct xfrm_state
*x1
;
1239 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1241 spin_lock_bh(&xfrm_state_lock
);
1242 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1248 if (xfrm_state_kern(x1
)) {
1254 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1255 __xfrm_state_insert(x
);
1261 spin_unlock_bh(&xfrm_state_lock
);
1267 xfrm_state_delete(x1
);
1273 spin_lock_bh(&x1
->lock
);
1274 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1275 if (x
->encap
&& x1
->encap
)
1276 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1277 if (x
->coaddr
&& x1
->coaddr
) {
1278 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1280 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1281 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1282 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1285 mod_timer(&x1
->timer
, jiffies
+ HZ
);
1286 if (x1
->curlft
.use_time
)
1287 xfrm_state_check_expire(x1
);
1291 spin_unlock_bh(&x1
->lock
);
1297 EXPORT_SYMBOL(xfrm_state_update
);
1299 int xfrm_state_check_expire(struct xfrm_state
*x
)
1301 if (!x
->curlft
.use_time
)
1302 x
->curlft
.use_time
= get_seconds();
1304 if (x
->km
.state
!= XFRM_STATE_VALID
)
1307 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1308 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1309 x
->km
.state
= XFRM_STATE_EXPIRED
;
1310 mod_timer(&x
->timer
, jiffies
);
1315 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1316 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1318 km_state_expired(x
, 0, 0);
1322 EXPORT_SYMBOL(xfrm_state_check_expire
);
1325 xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
,
1326 unsigned short family
)
1328 struct xfrm_state
*x
;
1330 spin_lock_bh(&xfrm_state_lock
);
1331 x
= __xfrm_state_lookup(daddr
, spi
, proto
, family
);
1332 spin_unlock_bh(&xfrm_state_lock
);
1335 EXPORT_SYMBOL(xfrm_state_lookup
);
1338 xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1339 u8 proto
, unsigned short family
)
1341 struct xfrm_state
*x
;
1343 spin_lock_bh(&xfrm_state_lock
);
1344 x
= __xfrm_state_lookup_byaddr(daddr
, saddr
, proto
, family
);
1345 spin_unlock_bh(&xfrm_state_lock
);
1348 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1351 xfrm_find_acq(u8 mode
, u32 reqid
, u8 proto
,
1352 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1353 int create
, unsigned short family
)
1355 struct xfrm_state
*x
;
1357 spin_lock_bh(&xfrm_state_lock
);
1358 x
= __find_acq_core(family
, mode
, reqid
, proto
, daddr
, saddr
, create
);
1359 spin_unlock_bh(&xfrm_state_lock
);
1363 EXPORT_SYMBOL(xfrm_find_acq
);
1365 #ifdef CONFIG_XFRM_SUB_POLICY
1367 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1368 unsigned short family
)
1371 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1373 return -EAFNOSUPPORT
;
1375 spin_lock_bh(&xfrm_state_lock
);
1376 if (afinfo
->tmpl_sort
)
1377 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1378 spin_unlock_bh(&xfrm_state_lock
);
1379 xfrm_state_put_afinfo(afinfo
);
1382 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1385 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1386 unsigned short family
)
1389 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1391 return -EAFNOSUPPORT
;
1393 spin_lock_bh(&xfrm_state_lock
);
1394 if (afinfo
->state_sort
)
1395 err
= afinfo
->state_sort(dst
, src
, n
);
1396 spin_unlock_bh(&xfrm_state_lock
);
1397 xfrm_state_put_afinfo(afinfo
);
1400 EXPORT_SYMBOL(xfrm_state_sort
);
1403 /* Silly enough, but I'm lazy to build resolution list */
1405 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
)
1409 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
1410 struct hlist_node
*entry
;
1411 struct xfrm_state
*x
;
1413 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
1414 if (x
->km
.seq
== seq
&&
1415 x
->km
.state
== XFRM_STATE_ACQ
) {
1424 struct xfrm_state
*xfrm_find_acq_byseq(u32 seq
)
1426 struct xfrm_state
*x
;
1428 spin_lock_bh(&xfrm_state_lock
);
1429 x
= __xfrm_find_acq_byseq(seq
);
1430 spin_unlock_bh(&xfrm_state_lock
);
1433 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1435 u32
xfrm_get_acqseq(void)
1439 static DEFINE_SPINLOCK(acqseq_lock
);
1441 spin_lock_bh(&acqseq_lock
);
1442 res
= (++acqseq
? : ++acqseq
);
1443 spin_unlock_bh(&acqseq_lock
);
1446 EXPORT_SYMBOL(xfrm_get_acqseq
);
1448 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1451 struct xfrm_state
*x0
;
1453 __be32 minspi
= htonl(low
);
1454 __be32 maxspi
= htonl(high
);
1456 spin_lock_bh(&x
->lock
);
1457 if (x
->km
.state
== XFRM_STATE_DEAD
)
1466 if (minspi
== maxspi
) {
1467 x0
= xfrm_state_lookup(&x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1475 for (h
=0; h
<high
-low
+1; h
++) {
1476 spi
= low
+ net_random()%(high
-low
+1);
1477 x0
= xfrm_state_lookup(&x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1479 x
->id
.spi
= htonl(spi
);
1486 spin_lock_bh(&xfrm_state_lock
);
1487 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1488 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
1489 spin_unlock_bh(&xfrm_state_lock
);
1495 spin_unlock_bh(&x
->lock
);
1499 EXPORT_SYMBOL(xfrm_alloc_spi
);
1501 int xfrm_state_walk(u8 proto
, int (*func
)(struct xfrm_state
*, int, void*),
1505 struct xfrm_state
*x
, *last
= NULL
;
1506 struct hlist_node
*entry
;
1510 spin_lock_bh(&xfrm_state_lock
);
1511 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
1512 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
1513 if (!xfrm_id_proto_match(x
->id
.proto
, proto
))
1516 err
= func(last
, count
, data
);
1528 err
= func(last
, 0, data
);
1530 spin_unlock_bh(&xfrm_state_lock
);
1533 EXPORT_SYMBOL(xfrm_state_walk
);
1536 void xfrm_replay_notify(struct xfrm_state
*x
, int event
)
1539 /* we send notify messages in case
1540 * 1. we updated on of the sequence numbers, and the seqno difference
1541 * is at least x->replay_maxdiff, in this case we also update the
1542 * timeout of our timer function
1543 * 2. if x->replay_maxage has elapsed since last update,
1544 * and there were changes
1546 * The state structure must be locked!
1550 case XFRM_REPLAY_UPDATE
:
1551 if (x
->replay_maxdiff
&&
1552 (x
->replay
.seq
- x
->preplay
.seq
< x
->replay_maxdiff
) &&
1553 (x
->replay
.oseq
- x
->preplay
.oseq
< x
->replay_maxdiff
)) {
1554 if (x
->xflags
& XFRM_TIME_DEFER
)
1555 event
= XFRM_REPLAY_TIMEOUT
;
1562 case XFRM_REPLAY_TIMEOUT
:
1563 if ((x
->replay
.seq
== x
->preplay
.seq
) &&
1564 (x
->replay
.bitmap
== x
->preplay
.bitmap
) &&
1565 (x
->replay
.oseq
== x
->preplay
.oseq
)) {
1566 x
->xflags
|= XFRM_TIME_DEFER
;
1573 memcpy(&x
->preplay
, &x
->replay
, sizeof(struct xfrm_replay_state
));
1574 c
.event
= XFRM_MSG_NEWAE
;
1575 c
.data
.aevent
= event
;
1576 km_state_notify(x
, &c
);
1578 if (x
->replay_maxage
&&
1579 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
1580 x
->xflags
&= ~XFRM_TIME_DEFER
;
1583 static void xfrm_replay_timer_handler(unsigned long data
)
1585 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
1587 spin_lock(&x
->lock
);
1589 if (x
->km
.state
== XFRM_STATE_VALID
) {
1590 if (xfrm_aevent_is_on())
1591 xfrm_replay_notify(x
, XFRM_REPLAY_TIMEOUT
);
1593 x
->xflags
|= XFRM_TIME_DEFER
;
1596 spin_unlock(&x
->lock
);
1599 int xfrm_replay_check(struct xfrm_state
*x
, __be32 net_seq
)
1602 u32 seq
= ntohl(net_seq
);
1604 if (unlikely(seq
== 0))
1607 if (likely(seq
> x
->replay
.seq
))
1610 diff
= x
->replay
.seq
- seq
;
1611 if (diff
>= min_t(unsigned int, x
->props
.replay_window
,
1612 sizeof(x
->replay
.bitmap
) * 8)) {
1613 x
->stats
.replay_window
++;
1617 if (x
->replay
.bitmap
& (1U << diff
)) {
1623 EXPORT_SYMBOL(xfrm_replay_check
);
1625 void xfrm_replay_advance(struct xfrm_state
*x
, __be32 net_seq
)
1628 u32 seq
= ntohl(net_seq
);
1630 if (seq
> x
->replay
.seq
) {
1631 diff
= seq
- x
->replay
.seq
;
1632 if (diff
< x
->props
.replay_window
)
1633 x
->replay
.bitmap
= ((x
->replay
.bitmap
) << diff
) | 1;
1635 x
->replay
.bitmap
= 1;
1636 x
->replay
.seq
= seq
;
1638 diff
= x
->replay
.seq
- seq
;
1639 x
->replay
.bitmap
|= (1U << diff
);
1642 if (xfrm_aevent_is_on())
1643 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
1645 EXPORT_SYMBOL(xfrm_replay_advance
);
1647 static struct list_head xfrm_km_list
= LIST_HEAD_INIT(xfrm_km_list
);
1648 static DEFINE_RWLOCK(xfrm_km_lock
);
1650 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1652 struct xfrm_mgr
*km
;
1654 read_lock(&xfrm_km_lock
);
1655 list_for_each_entry(km
, &xfrm_km_list
, list
)
1656 if (km
->notify_policy
)
1657 km
->notify_policy(xp
, dir
, c
);
1658 read_unlock(&xfrm_km_lock
);
1661 void km_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1663 struct xfrm_mgr
*km
;
1664 read_lock(&xfrm_km_lock
);
1665 list_for_each_entry(km
, &xfrm_km_list
, list
)
1668 read_unlock(&xfrm_km_lock
);
1671 EXPORT_SYMBOL(km_policy_notify
);
1672 EXPORT_SYMBOL(km_state_notify
);
1674 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
)
1680 c
.event
= XFRM_MSG_EXPIRE
;
1681 km_state_notify(x
, &c
);
1687 EXPORT_SYMBOL(km_state_expired
);
1689 * We send to all registered managers regardless of failure
1690 * We are happy with one success
1692 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1694 int err
= -EINVAL
, acqret
;
1695 struct xfrm_mgr
*km
;
1697 read_lock(&xfrm_km_lock
);
1698 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1699 acqret
= km
->acquire(x
, t
, pol
, XFRM_POLICY_OUT
);
1703 read_unlock(&xfrm_km_lock
);
1706 EXPORT_SYMBOL(km_query
);
1708 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1711 struct xfrm_mgr
*km
;
1713 read_lock(&xfrm_km_lock
);
1714 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1715 if (km
->new_mapping
)
1716 err
= km
->new_mapping(x
, ipaddr
, sport
);
1720 read_unlock(&xfrm_km_lock
);
1723 EXPORT_SYMBOL(km_new_mapping
);
1725 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 pid
)
1731 c
.event
= XFRM_MSG_POLEXPIRE
;
1732 km_policy_notify(pol
, dir
, &c
);
1737 EXPORT_SYMBOL(km_policy_expired
);
1739 int km_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1740 struct xfrm_migrate
*m
, int num_migrate
)
1744 struct xfrm_mgr
*km
;
1746 read_lock(&xfrm_km_lock
);
1747 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1749 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
);
1754 read_unlock(&xfrm_km_lock
);
1757 EXPORT_SYMBOL(km_migrate
);
1759 int km_report(u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
1763 struct xfrm_mgr
*km
;
1765 read_lock(&xfrm_km_lock
);
1766 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1768 ret
= km
->report(proto
, sel
, addr
);
1773 read_unlock(&xfrm_km_lock
);
1776 EXPORT_SYMBOL(km_report
);
1778 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
1782 struct xfrm_mgr
*km
;
1783 struct xfrm_policy
*pol
= NULL
;
1785 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
1788 data
= kmalloc(optlen
, GFP_KERNEL
);
1793 if (copy_from_user(data
, optval
, optlen
))
1797 read_lock(&xfrm_km_lock
);
1798 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1799 pol
= km
->compile_policy(sk
, optname
, data
,
1804 read_unlock(&xfrm_km_lock
);
1807 xfrm_sk_policy_insert(sk
, err
, pol
);
1816 EXPORT_SYMBOL(xfrm_user_policy
);
1818 int xfrm_register_km(struct xfrm_mgr
*km
)
1820 write_lock_bh(&xfrm_km_lock
);
1821 list_add_tail(&km
->list
, &xfrm_km_list
);
1822 write_unlock_bh(&xfrm_km_lock
);
1825 EXPORT_SYMBOL(xfrm_register_km
);
1827 int xfrm_unregister_km(struct xfrm_mgr
*km
)
1829 write_lock_bh(&xfrm_km_lock
);
1830 list_del(&km
->list
);
1831 write_unlock_bh(&xfrm_km_lock
);
1834 EXPORT_SYMBOL(xfrm_unregister_km
);
1836 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
1839 if (unlikely(afinfo
== NULL
))
1841 if (unlikely(afinfo
->family
>= NPROTO
))
1842 return -EAFNOSUPPORT
;
1843 write_lock_bh(&xfrm_state_afinfo_lock
);
1844 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
1847 xfrm_state_afinfo
[afinfo
->family
] = afinfo
;
1848 write_unlock_bh(&xfrm_state_afinfo_lock
);
1851 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
1853 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
1856 if (unlikely(afinfo
== NULL
))
1858 if (unlikely(afinfo
->family
>= NPROTO
))
1859 return -EAFNOSUPPORT
;
1860 write_lock_bh(&xfrm_state_afinfo_lock
);
1861 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
1862 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != afinfo
))
1865 xfrm_state_afinfo
[afinfo
->family
] = NULL
;
1867 write_unlock_bh(&xfrm_state_afinfo_lock
);
1870 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
1872 struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned short family
)
1874 struct xfrm_state_afinfo
*afinfo
;
1875 if (unlikely(family
>= NPROTO
))
1877 read_lock(&xfrm_state_afinfo_lock
);
1878 afinfo
= xfrm_state_afinfo
[family
];
1879 if (unlikely(!afinfo
))
1880 read_unlock(&xfrm_state_afinfo_lock
);
1884 void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
)
1886 read_unlock(&xfrm_state_afinfo_lock
);
1889 EXPORT_SYMBOL(xfrm_state_get_afinfo
);
1890 EXPORT_SYMBOL(xfrm_state_put_afinfo
);
1892 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1893 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
1896 struct xfrm_state
*t
= x
->tunnel
;
1898 if (atomic_read(&t
->tunnel_users
) == 2)
1899 xfrm_state_delete(t
);
1900 atomic_dec(&t
->tunnel_users
);
1905 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
1907 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
1911 spin_lock_bh(&x
->lock
);
1912 if (x
->km
.state
== XFRM_STATE_VALID
&&
1913 x
->type
&& x
->type
->get_mtu
)
1914 res
= x
->type
->get_mtu(x
, mtu
);
1916 res
= mtu
- x
->props
.header_len
;
1917 spin_unlock_bh(&x
->lock
);
1921 int xfrm_init_state(struct xfrm_state
*x
)
1923 struct xfrm_state_afinfo
*afinfo
;
1924 int family
= x
->props
.family
;
1927 err
= -EAFNOSUPPORT
;
1928 afinfo
= xfrm_state_get_afinfo(family
);
1933 if (afinfo
->init_flags
)
1934 err
= afinfo
->init_flags(x
);
1936 xfrm_state_put_afinfo(afinfo
);
1941 err
= -EPROTONOSUPPORT
;
1942 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
1943 if (x
->type
== NULL
)
1946 err
= x
->type
->init_state(x
);
1950 x
->mode
= xfrm_get_mode(x
->props
.mode
, family
);
1951 if (x
->mode
== NULL
)
1954 x
->km
.state
= XFRM_STATE_VALID
;
1960 EXPORT_SYMBOL(xfrm_init_state
);
1962 void __init
xfrm_state_init(void)
1966 sz
= sizeof(struct hlist_head
) * 8;
1968 xfrm_state_bydst
= xfrm_hash_alloc(sz
);
1969 xfrm_state_bysrc
= xfrm_hash_alloc(sz
);
1970 xfrm_state_byspi
= xfrm_hash_alloc(sz
);
1971 if (!xfrm_state_bydst
|| !xfrm_state_bysrc
|| !xfrm_state_byspi
)
1972 panic("XFRM: Cannot allocate bydst/bysrc/byspi hashes.");
1973 xfrm_state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
1975 INIT_WORK(&xfrm_state_gc_work
, xfrm_state_gc_task
);
1978 #ifdef CONFIG_AUDITSYSCALL
1979 static inline void xfrm_audit_common_stateinfo(struct xfrm_state
*x
,
1980 struct audit_buffer
*audit_buf
)
1983 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
1984 x
->security
->ctx_alg
, x
->security
->ctx_doi
,
1985 x
->security
->ctx_str
);
1987 switch(x
->props
.family
) {
1989 audit_log_format(audit_buf
, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
1990 NIPQUAD(x
->props
.saddr
.a4
),
1991 NIPQUAD(x
->id
.daddr
.a4
));
1995 struct in6_addr saddr6
, daddr6
;
1997 memcpy(&saddr6
, x
->props
.saddr
.a6
,
1998 sizeof(struct in6_addr
));
1999 memcpy(&daddr6
, x
->id
.daddr
.a6
,
2000 sizeof(struct in6_addr
));
2001 audit_log_format(audit_buf
,
2002 " src=" NIP6_FMT
" dst=" NIP6_FMT
,
2003 NIP6(saddr6
), NIP6(daddr6
));
2010 xfrm_audit_state_add(struct xfrm_state
*x
, int result
, u32 auid
, u32 sid
)
2012 struct audit_buffer
*audit_buf
;
2013 extern int audit_enabled
;
2015 if (audit_enabled
== 0)
2017 audit_buf
= xfrm_audit_start(sid
, auid
);
2018 if (audit_buf
== NULL
)
2020 audit_log_format(audit_buf
, " op=SAD-add res=%u",result
);
2021 xfrm_audit_common_stateinfo(x
, audit_buf
);
2022 audit_log_format(audit_buf
, " spi=%lu(0x%lx)",
2023 (unsigned long)x
->id
.spi
, (unsigned long)x
->id
.spi
);
2024 audit_log_end(audit_buf
);
2026 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2029 xfrm_audit_state_delete(struct xfrm_state
*x
, int result
, u32 auid
, u32 sid
)
2031 struct audit_buffer
*audit_buf
;
2032 extern int audit_enabled
;
2034 if (audit_enabled
== 0)
2036 audit_buf
= xfrm_audit_start(sid
, auid
);
2037 if (audit_buf
== NULL
)
2039 audit_log_format(audit_buf
, " op=SAD-delete res=%u",result
);
2040 xfrm_audit_common_stateinfo(x
, audit_buf
);
2041 audit_log_format(audit_buf
, " spi=%lu(0x%lx)",
2042 (unsigned long)x
->id
.spi
, (unsigned long)x
->id
.spi
);
2043 audit_log_end(audit_buf
);
2045 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2046 #endif /* CONFIG_AUDITSYSCALL */