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 /* Hash table to find appropriate SA towards given target (endpoint
48 * of tunnel or destination of transport mode) allowed by selector.
50 * Main use is finding SA after policy selected tunnel or transport mode.
51 * Also, it can be used by ah/esp icmp error handler to find offending SA.
53 static struct hlist_head
*xfrm_state_bydst __read_mostly
;
54 static struct hlist_head
*xfrm_state_bysrc __read_mostly
;
55 static struct hlist_head
*xfrm_state_byspi __read_mostly
;
56 static unsigned int xfrm_state_hmask __read_mostly
;
57 static unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
58 static unsigned int xfrm_state_num
;
59 static unsigned int xfrm_state_genid
;
61 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
);
62 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
);
64 #ifdef CONFIG_AUDITSYSCALL
65 static void xfrm_audit_state_replay(struct xfrm_state
*x
,
66 struct sk_buff
*skb
, __be32 net_seq
);
68 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
69 #endif /* CONFIG_AUDITSYSCALL */
71 static inline unsigned int xfrm_dst_hash(xfrm_address_t
*daddr
,
72 xfrm_address_t
*saddr
,
74 unsigned short family
)
76 return __xfrm_dst_hash(daddr
, saddr
, reqid
, family
, xfrm_state_hmask
);
79 static inline unsigned int xfrm_src_hash(xfrm_address_t
*daddr
,
80 xfrm_address_t
*saddr
,
81 unsigned short family
)
83 return __xfrm_src_hash(daddr
, saddr
, family
, xfrm_state_hmask
);
86 static inline unsigned int
87 xfrm_spi_hash(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
89 return __xfrm_spi_hash(daddr
, spi
, proto
, family
, xfrm_state_hmask
);
92 static void xfrm_hash_transfer(struct hlist_head
*list
,
93 struct hlist_head
*ndsttable
,
94 struct hlist_head
*nsrctable
,
95 struct hlist_head
*nspitable
,
96 unsigned int nhashmask
)
98 struct hlist_node
*entry
, *tmp
;
101 hlist_for_each_entry_safe(x
, entry
, tmp
, list
, bydst
) {
104 h
= __xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
105 x
->props
.reqid
, x
->props
.family
,
107 hlist_add_head(&x
->bydst
, ndsttable
+h
);
109 h
= __xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
,
112 hlist_add_head(&x
->bysrc
, nsrctable
+h
);
115 h
= __xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
,
116 x
->id
.proto
, x
->props
.family
,
118 hlist_add_head(&x
->byspi
, nspitable
+h
);
123 static unsigned long xfrm_hash_new_size(void)
125 return ((xfrm_state_hmask
+ 1) << 1) *
126 sizeof(struct hlist_head
);
129 static DEFINE_MUTEX(hash_resize_mutex
);
131 static void xfrm_hash_resize(struct work_struct
*__unused
)
133 struct hlist_head
*ndst
, *nsrc
, *nspi
, *odst
, *osrc
, *ospi
;
134 unsigned long nsize
, osize
;
135 unsigned int nhashmask
, ohashmask
;
138 mutex_lock(&hash_resize_mutex
);
140 nsize
= xfrm_hash_new_size();
141 ndst
= xfrm_hash_alloc(nsize
);
144 nsrc
= xfrm_hash_alloc(nsize
);
146 xfrm_hash_free(ndst
, nsize
);
149 nspi
= xfrm_hash_alloc(nsize
);
151 xfrm_hash_free(ndst
, nsize
);
152 xfrm_hash_free(nsrc
, nsize
);
156 spin_lock_bh(&xfrm_state_lock
);
158 nhashmask
= (nsize
/ sizeof(struct hlist_head
)) - 1U;
159 for (i
= xfrm_state_hmask
; i
>= 0; i
--)
160 xfrm_hash_transfer(xfrm_state_bydst
+i
, ndst
, nsrc
, nspi
,
163 odst
= xfrm_state_bydst
;
164 osrc
= xfrm_state_bysrc
;
165 ospi
= xfrm_state_byspi
;
166 ohashmask
= xfrm_state_hmask
;
168 xfrm_state_bydst
= ndst
;
169 xfrm_state_bysrc
= nsrc
;
170 xfrm_state_byspi
= nspi
;
171 xfrm_state_hmask
= nhashmask
;
173 spin_unlock_bh(&xfrm_state_lock
);
175 osize
= (ohashmask
+ 1) * sizeof(struct hlist_head
);
176 xfrm_hash_free(odst
, osize
);
177 xfrm_hash_free(osrc
, osize
);
178 xfrm_hash_free(ospi
, osize
);
181 mutex_unlock(&hash_resize_mutex
);
184 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
186 DECLARE_WAIT_QUEUE_HEAD(km_waitq
);
187 EXPORT_SYMBOL(km_waitq
);
189 static DEFINE_RWLOCK(xfrm_state_afinfo_lock
);
190 static struct xfrm_state_afinfo
*xfrm_state_afinfo
[NPROTO
];
192 static struct work_struct xfrm_state_gc_work
;
193 static HLIST_HEAD(xfrm_state_gc_list
);
194 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
196 int __xfrm_state_delete(struct xfrm_state
*x
);
198 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
199 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
);
201 static struct xfrm_state_afinfo
*xfrm_state_lock_afinfo(unsigned int family
)
203 struct xfrm_state_afinfo
*afinfo
;
204 if (unlikely(family
>= NPROTO
))
206 write_lock_bh(&xfrm_state_afinfo_lock
);
207 afinfo
= xfrm_state_afinfo
[family
];
208 if (unlikely(!afinfo
))
209 write_unlock_bh(&xfrm_state_afinfo_lock
);
213 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo
*afinfo
)
214 __releases(xfrm_state_afinfo_lock
)
216 write_unlock_bh(&xfrm_state_afinfo_lock
);
219 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
221 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
222 const struct xfrm_type
**typemap
;
225 if (unlikely(afinfo
== NULL
))
226 return -EAFNOSUPPORT
;
227 typemap
= afinfo
->type_map
;
229 if (likely(typemap
[type
->proto
] == NULL
))
230 typemap
[type
->proto
] = type
;
233 xfrm_state_unlock_afinfo(afinfo
);
236 EXPORT_SYMBOL(xfrm_register_type
);
238 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
240 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
241 const struct xfrm_type
**typemap
;
244 if (unlikely(afinfo
== NULL
))
245 return -EAFNOSUPPORT
;
246 typemap
= afinfo
->type_map
;
248 if (unlikely(typemap
[type
->proto
] != type
))
251 typemap
[type
->proto
] = NULL
;
252 xfrm_state_unlock_afinfo(afinfo
);
255 EXPORT_SYMBOL(xfrm_unregister_type
);
257 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
259 struct xfrm_state_afinfo
*afinfo
;
260 const struct xfrm_type
**typemap
;
261 const struct xfrm_type
*type
;
262 int modload_attempted
= 0;
265 afinfo
= xfrm_state_get_afinfo(family
);
266 if (unlikely(afinfo
== NULL
))
268 typemap
= afinfo
->type_map
;
270 type
= typemap
[proto
];
271 if (unlikely(type
&& !try_module_get(type
->owner
)))
273 if (!type
&& !modload_attempted
) {
274 xfrm_state_put_afinfo(afinfo
);
275 request_module("xfrm-type-%d-%d", family
, proto
);
276 modload_attempted
= 1;
280 xfrm_state_put_afinfo(afinfo
);
284 static void xfrm_put_type(const struct xfrm_type
*type
)
286 module_put(type
->owner
);
289 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
291 struct xfrm_state_afinfo
*afinfo
;
292 struct xfrm_mode
**modemap
;
295 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
298 afinfo
= xfrm_state_lock_afinfo(family
);
299 if (unlikely(afinfo
== NULL
))
300 return -EAFNOSUPPORT
;
303 modemap
= afinfo
->mode_map
;
304 if (modemap
[mode
->encap
])
308 if (!try_module_get(afinfo
->owner
))
311 mode
->afinfo
= afinfo
;
312 modemap
[mode
->encap
] = mode
;
316 xfrm_state_unlock_afinfo(afinfo
);
319 EXPORT_SYMBOL(xfrm_register_mode
);
321 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
323 struct xfrm_state_afinfo
*afinfo
;
324 struct xfrm_mode
**modemap
;
327 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
330 afinfo
= xfrm_state_lock_afinfo(family
);
331 if (unlikely(afinfo
== NULL
))
332 return -EAFNOSUPPORT
;
335 modemap
= afinfo
->mode_map
;
336 if (likely(modemap
[mode
->encap
] == mode
)) {
337 modemap
[mode
->encap
] = NULL
;
338 module_put(mode
->afinfo
->owner
);
342 xfrm_state_unlock_afinfo(afinfo
);
345 EXPORT_SYMBOL(xfrm_unregister_mode
);
347 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
349 struct xfrm_state_afinfo
*afinfo
;
350 struct xfrm_mode
*mode
;
351 int modload_attempted
= 0;
353 if (unlikely(encap
>= XFRM_MODE_MAX
))
357 afinfo
= xfrm_state_get_afinfo(family
);
358 if (unlikely(afinfo
== NULL
))
361 mode
= afinfo
->mode_map
[encap
];
362 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
364 if (!mode
&& !modload_attempted
) {
365 xfrm_state_put_afinfo(afinfo
);
366 request_module("xfrm-mode-%d-%d", family
, encap
);
367 modload_attempted
= 1;
371 xfrm_state_put_afinfo(afinfo
);
375 static void xfrm_put_mode(struct xfrm_mode
*mode
)
377 module_put(mode
->owner
);
380 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
382 del_timer_sync(&x
->timer
);
383 del_timer_sync(&x
->rtimer
);
390 xfrm_put_mode(x
->inner_mode
);
391 if (x
->inner_mode_iaf
)
392 xfrm_put_mode(x
->inner_mode_iaf
);
394 xfrm_put_mode(x
->outer_mode
);
396 x
->type
->destructor(x
);
397 xfrm_put_type(x
->type
);
399 security_xfrm_state_free(x
);
403 static void xfrm_state_gc_task(struct work_struct
*data
)
405 struct xfrm_state
*x
;
406 struct hlist_node
*entry
, *tmp
;
407 struct hlist_head gc_list
;
409 spin_lock_bh(&xfrm_state_gc_lock
);
410 gc_list
.first
= xfrm_state_gc_list
.first
;
411 INIT_HLIST_HEAD(&xfrm_state_gc_list
);
412 spin_unlock_bh(&xfrm_state_gc_lock
);
414 hlist_for_each_entry_safe(x
, entry
, tmp
, &gc_list
, bydst
)
415 xfrm_state_gc_destroy(x
);
420 static inline unsigned long make_jiffies(long secs
)
422 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
423 return MAX_SCHEDULE_TIMEOUT
-1;
428 static void xfrm_timer_handler(unsigned long data
)
430 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
431 unsigned long now
= get_seconds();
432 long next
= LONG_MAX
;
437 if (x
->km
.state
== XFRM_STATE_DEAD
)
439 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
441 if (x
->lft
.hard_add_expires_seconds
) {
442 long tmo
= x
->lft
.hard_add_expires_seconds
+
443 x
->curlft
.add_time
- now
;
449 if (x
->lft
.hard_use_expires_seconds
) {
450 long tmo
= x
->lft
.hard_use_expires_seconds
+
451 (x
->curlft
.use_time
? : now
) - now
;
459 if (x
->lft
.soft_add_expires_seconds
) {
460 long tmo
= x
->lft
.soft_add_expires_seconds
+
461 x
->curlft
.add_time
- now
;
467 if (x
->lft
.soft_use_expires_seconds
) {
468 long tmo
= x
->lft
.soft_use_expires_seconds
+
469 (x
->curlft
.use_time
? : now
) - now
;
478 km_state_expired(x
, 0, 0);
480 if (next
!= LONG_MAX
)
481 mod_timer(&x
->timer
, jiffies
+ make_jiffies(next
));
486 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0) {
487 x
->km
.state
= XFRM_STATE_EXPIRED
;
493 err
= __xfrm_state_delete(x
);
494 if (!err
&& x
->id
.spi
)
495 km_state_expired(x
, 1, 0);
497 xfrm_audit_state_delete(x
, err
? 0 : 1,
498 audit_get_loginuid(current
), 0);
501 spin_unlock(&x
->lock
);
504 static void xfrm_replay_timer_handler(unsigned long data
);
506 struct xfrm_state
*xfrm_state_alloc(void)
508 struct xfrm_state
*x
;
510 x
= kzalloc(sizeof(struct xfrm_state
), GFP_ATOMIC
);
513 atomic_set(&x
->refcnt
, 1);
514 atomic_set(&x
->tunnel_users
, 0);
515 INIT_HLIST_NODE(&x
->bydst
);
516 INIT_HLIST_NODE(&x
->bysrc
);
517 INIT_HLIST_NODE(&x
->byspi
);
518 setup_timer(&x
->timer
, xfrm_timer_handler
, (unsigned long)x
);
519 setup_timer(&x
->rtimer
, xfrm_replay_timer_handler
,
521 x
->curlft
.add_time
= get_seconds();
522 x
->lft
.soft_byte_limit
= XFRM_INF
;
523 x
->lft
.soft_packet_limit
= XFRM_INF
;
524 x
->lft
.hard_byte_limit
= XFRM_INF
;
525 x
->lft
.hard_packet_limit
= XFRM_INF
;
526 x
->replay_maxage
= 0;
527 x
->replay_maxdiff
= 0;
528 x
->inner_mode
= NULL
;
529 x
->inner_mode_iaf
= NULL
;
530 spin_lock_init(&x
->lock
);
534 EXPORT_SYMBOL(xfrm_state_alloc
);
536 void __xfrm_state_destroy(struct xfrm_state
*x
)
538 BUG_TRAP(x
->km
.state
== XFRM_STATE_DEAD
);
540 spin_lock_bh(&xfrm_state_gc_lock
);
541 hlist_add_head(&x
->bydst
, &xfrm_state_gc_list
);
542 spin_unlock_bh(&xfrm_state_gc_lock
);
543 schedule_work(&xfrm_state_gc_work
);
545 EXPORT_SYMBOL(__xfrm_state_destroy
);
547 int __xfrm_state_delete(struct xfrm_state
*x
)
551 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
552 x
->km
.state
= XFRM_STATE_DEAD
;
553 spin_lock(&xfrm_state_lock
);
554 hlist_del(&x
->bydst
);
555 hlist_del(&x
->bysrc
);
557 hlist_del(&x
->byspi
);
559 spin_unlock(&xfrm_state_lock
);
561 /* All xfrm_state objects are created by xfrm_state_alloc.
562 * The xfrm_state_alloc call gives a reference, and that
563 * is what we are dropping here.
571 EXPORT_SYMBOL(__xfrm_state_delete
);
573 int xfrm_state_delete(struct xfrm_state
*x
)
577 spin_lock_bh(&x
->lock
);
578 err
= __xfrm_state_delete(x
);
579 spin_unlock_bh(&x
->lock
);
583 EXPORT_SYMBOL(xfrm_state_delete
);
585 #ifdef CONFIG_SECURITY_NETWORK_XFRM
587 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
591 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
592 struct hlist_node
*entry
;
593 struct xfrm_state
*x
;
595 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
596 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
597 (err
= security_xfrm_state_delete(x
)) != 0) {
598 xfrm_audit_state_delete(x
, 0,
599 audit_info
->loginuid
,
610 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
616 int xfrm_state_flush(u8 proto
, struct xfrm_audit
*audit_info
)
620 spin_lock_bh(&xfrm_state_lock
);
621 err
= xfrm_state_flush_secctx_check(proto
, audit_info
);
625 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
626 struct hlist_node
*entry
;
627 struct xfrm_state
*x
;
629 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
630 if (!xfrm_state_kern(x
) &&
631 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
633 spin_unlock_bh(&xfrm_state_lock
);
635 err
= xfrm_state_delete(x
);
636 xfrm_audit_state_delete(x
, err
? 0 : 1,
637 audit_info
->loginuid
,
641 spin_lock_bh(&xfrm_state_lock
);
649 spin_unlock_bh(&xfrm_state_lock
);
653 EXPORT_SYMBOL(xfrm_state_flush
);
655 void xfrm_sad_getinfo(struct xfrmk_sadinfo
*si
)
657 spin_lock_bh(&xfrm_state_lock
);
658 si
->sadcnt
= xfrm_state_num
;
659 si
->sadhcnt
= xfrm_state_hmask
;
660 si
->sadhmcnt
= xfrm_state_hashmax
;
661 spin_unlock_bh(&xfrm_state_lock
);
663 EXPORT_SYMBOL(xfrm_sad_getinfo
);
666 xfrm_init_tempsel(struct xfrm_state
*x
, struct flowi
*fl
,
667 struct xfrm_tmpl
*tmpl
,
668 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
669 unsigned short family
)
671 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
674 afinfo
->init_tempsel(x
, fl
, tmpl
, daddr
, saddr
);
675 xfrm_state_put_afinfo(afinfo
);
679 static struct xfrm_state
*__xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
681 unsigned int h
= xfrm_spi_hash(daddr
, spi
, proto
, family
);
682 struct xfrm_state
*x
;
683 struct hlist_node
*entry
;
685 hlist_for_each_entry(x
, entry
, xfrm_state_byspi
+h
, byspi
) {
686 if (x
->props
.family
!= family
||
688 x
->id
.proto
!= proto
)
693 if (x
->id
.daddr
.a4
!= daddr
->a4
)
697 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
711 static struct xfrm_state
*__xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, u8 proto
, unsigned short family
)
713 unsigned int h
= xfrm_src_hash(daddr
, saddr
, family
);
714 struct xfrm_state
*x
;
715 struct hlist_node
*entry
;
717 hlist_for_each_entry(x
, entry
, xfrm_state_bysrc
+h
, bysrc
) {
718 if (x
->props
.family
!= family
||
719 x
->id
.proto
!= proto
)
724 if (x
->id
.daddr
.a4
!= daddr
->a4
||
725 x
->props
.saddr
.a4
!= saddr
->a4
)
729 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
732 !ipv6_addr_equal((struct in6_addr
*)saddr
,
746 static inline struct xfrm_state
*
747 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
750 return __xfrm_state_lookup(&x
->id
.daddr
, x
->id
.spi
,
751 x
->id
.proto
, family
);
753 return __xfrm_state_lookup_byaddr(&x
->id
.daddr
,
755 x
->id
.proto
, family
);
758 static void xfrm_hash_grow_check(int have_hash_collision
)
760 if (have_hash_collision
&&
761 (xfrm_state_hmask
+ 1) < xfrm_state_hashmax
&&
762 xfrm_state_num
> xfrm_state_hmask
)
763 schedule_work(&xfrm_hash_work
);
767 xfrm_state_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
768 struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
769 struct xfrm_policy
*pol
, int *err
,
770 unsigned short family
)
773 struct hlist_node
*entry
;
774 struct xfrm_state
*x
, *x0
;
775 int acquire_in_progress
= 0;
777 struct xfrm_state
*best
= NULL
;
779 spin_lock_bh(&xfrm_state_lock
);
780 h
= xfrm_dst_hash(daddr
, saddr
, tmpl
->reqid
, family
);
781 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
782 if (x
->props
.family
== family
&&
783 x
->props
.reqid
== tmpl
->reqid
&&
784 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
785 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
786 tmpl
->mode
== x
->props
.mode
&&
787 tmpl
->id
.proto
== x
->id
.proto
&&
788 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
)) {
790 1. There is a valid state with matching selector.
792 2. Valid state with inappropriate selector. Skip.
794 Entering area of "sysdeps".
796 3. If state is not valid, selector is temporary,
797 it selects only session which triggered
798 previous resolution. Key manager will do
799 something to install a state with proper
802 if (x
->km
.state
== XFRM_STATE_VALID
) {
803 if ((x
->sel
.family
&& !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
804 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
807 best
->km
.dying
> x
->km
.dying
||
808 (best
->km
.dying
== x
->km
.dying
&&
809 best
->curlft
.add_time
< x
->curlft
.add_time
))
811 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
812 acquire_in_progress
= 1;
813 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
814 x
->km
.state
== XFRM_STATE_EXPIRED
) {
815 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
816 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
823 if (!x
&& !error
&& !acquire_in_progress
) {
825 (x0
= __xfrm_state_lookup(daddr
, tmpl
->id
.spi
,
826 tmpl
->id
.proto
, family
)) != NULL
) {
831 x
= xfrm_state_alloc();
836 /* Initialize temporary selector matching only
837 * to current session. */
838 xfrm_init_tempsel(x
, fl
, tmpl
, daddr
, saddr
, family
);
840 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->secid
);
842 x
->km
.state
= XFRM_STATE_DEAD
;
848 if (km_query(x
, tmpl
, pol
) == 0) {
849 x
->km
.state
= XFRM_STATE_ACQ
;
850 hlist_add_head(&x
->bydst
, xfrm_state_bydst
+h
);
851 h
= xfrm_src_hash(daddr
, saddr
, family
);
852 hlist_add_head(&x
->bysrc
, 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
);
879 xfrm_stateonly_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
880 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
883 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
884 struct hlist_node
*entry
;
886 spin_lock(&xfrm_state_lock
);
887 h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
888 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
889 if (x
->props
.family
== family
&&
890 x
->props
.reqid
== reqid
&&
891 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
892 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
893 mode
== x
->props
.mode
&&
894 proto
== x
->id
.proto
&&
895 x
->km
.state
== XFRM_STATE_VALID
) {
903 spin_unlock(&xfrm_state_lock
);
908 EXPORT_SYMBOL(xfrm_stateonly_find
);
910 static void __xfrm_state_insert(struct xfrm_state
*x
)
914 x
->genid
= ++xfrm_state_genid
;
916 h
= xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
917 x
->props
.reqid
, x
->props
.family
);
918 hlist_add_head(&x
->bydst
, xfrm_state_bydst
+h
);
920 h
= xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
921 hlist_add_head(&x
->bysrc
, xfrm_state_bysrc
+h
);
924 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
927 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
930 mod_timer(&x
->timer
, jiffies
+ HZ
);
931 if (x
->replay_maxage
)
932 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
938 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
941 /* xfrm_state_lock is held */
942 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
944 unsigned short family
= xnew
->props
.family
;
945 u32 reqid
= xnew
->props
.reqid
;
946 struct xfrm_state
*x
;
947 struct hlist_node
*entry
;
950 h
= xfrm_dst_hash(&xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
951 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
952 if (x
->props
.family
== family
&&
953 x
->props
.reqid
== reqid
&&
954 !xfrm_addr_cmp(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
955 !xfrm_addr_cmp(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
956 x
->genid
= xfrm_state_genid
;
960 void xfrm_state_insert(struct xfrm_state
*x
)
962 spin_lock_bh(&xfrm_state_lock
);
963 __xfrm_state_bump_genids(x
);
964 __xfrm_state_insert(x
);
965 spin_unlock_bh(&xfrm_state_lock
);
967 EXPORT_SYMBOL(xfrm_state_insert
);
969 /* xfrm_state_lock is held */
970 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
)
972 unsigned int h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
973 struct hlist_node
*entry
;
974 struct xfrm_state
*x
;
976 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
977 if (x
->props
.reqid
!= reqid
||
978 x
->props
.mode
!= mode
||
979 x
->props
.family
!= family
||
980 x
->km
.state
!= XFRM_STATE_ACQ
||
982 x
->id
.proto
!= proto
)
987 if (x
->id
.daddr
.a4
!= daddr
->a4
||
988 x
->props
.saddr
.a4
!= saddr
->a4
)
992 if (!ipv6_addr_equal((struct in6_addr
*)x
->id
.daddr
.a6
,
993 (struct in6_addr
*)daddr
) ||
994 !ipv6_addr_equal((struct in6_addr
*)
996 (struct in6_addr
*)saddr
))
1008 x
= xfrm_state_alloc();
1012 x
->sel
.daddr
.a4
= daddr
->a4
;
1013 x
->sel
.saddr
.a4
= saddr
->a4
;
1014 x
->sel
.prefixlen_d
= 32;
1015 x
->sel
.prefixlen_s
= 32;
1016 x
->props
.saddr
.a4
= saddr
->a4
;
1017 x
->id
.daddr
.a4
= daddr
->a4
;
1021 ipv6_addr_copy((struct in6_addr
*)x
->sel
.daddr
.a6
,
1022 (struct in6_addr
*)daddr
);
1023 ipv6_addr_copy((struct in6_addr
*)x
->sel
.saddr
.a6
,
1024 (struct in6_addr
*)saddr
);
1025 x
->sel
.prefixlen_d
= 128;
1026 x
->sel
.prefixlen_s
= 128;
1027 ipv6_addr_copy((struct in6_addr
*)x
->props
.saddr
.a6
,
1028 (struct in6_addr
*)saddr
);
1029 ipv6_addr_copy((struct in6_addr
*)x
->id
.daddr
.a6
,
1030 (struct in6_addr
*)daddr
);
1034 x
->km
.state
= XFRM_STATE_ACQ
;
1035 x
->id
.proto
= proto
;
1036 x
->props
.family
= family
;
1037 x
->props
.mode
= mode
;
1038 x
->props
.reqid
= reqid
;
1039 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
1041 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
1042 add_timer(&x
->timer
);
1043 hlist_add_head(&x
->bydst
, xfrm_state_bydst
+h
);
1044 h
= xfrm_src_hash(daddr
, saddr
, family
);
1045 hlist_add_head(&x
->bysrc
, xfrm_state_bysrc
+h
);
1049 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
1055 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
);
1057 int xfrm_state_add(struct xfrm_state
*x
)
1059 struct xfrm_state
*x1
;
1062 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1064 family
= x
->props
.family
;
1066 spin_lock_bh(&xfrm_state_lock
);
1068 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1076 if (use_spi
&& x
->km
.seq
) {
1077 x1
= __xfrm_find_acq_byseq(x
->km
.seq
);
1078 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1079 xfrm_addr_cmp(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1086 x1
= __find_acq_core(family
, x
->props
.mode
, x
->props
.reqid
,
1088 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1090 __xfrm_state_bump_genids(x
);
1091 __xfrm_state_insert(x
);
1095 spin_unlock_bh(&xfrm_state_lock
);
1098 xfrm_state_delete(x1
);
1104 EXPORT_SYMBOL(xfrm_state_add
);
1106 #ifdef CONFIG_XFRM_MIGRATE
1107 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
, int *errp
)
1110 struct xfrm_state
*x
= xfrm_state_alloc();
1114 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1115 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1116 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1117 x
->props
.mode
= orig
->props
.mode
;
1118 x
->props
.replay_window
= orig
->props
.replay_window
;
1119 x
->props
.reqid
= orig
->props
.reqid
;
1120 x
->props
.family
= orig
->props
.family
;
1121 x
->props
.saddr
= orig
->props
.saddr
;
1124 x
->aalg
= xfrm_algo_clone(orig
->aalg
);
1128 x
->props
.aalgo
= orig
->props
.aalgo
;
1131 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1135 x
->props
.ealgo
= orig
->props
.ealgo
;
1138 x
->calg
= xfrm_algo_clone(orig
->calg
);
1142 x
->props
.calgo
= orig
->props
.calgo
;
1145 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
), GFP_KERNEL
);
1151 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1157 err
= xfrm_init_state(x
);
1161 x
->props
.flags
= orig
->props
.flags
;
1163 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1164 x
->km
.state
= orig
->km
.state
;
1165 x
->km
.seq
= orig
->km
.seq
;
1183 /* xfrm_state_lock is held */
1184 struct xfrm_state
* xfrm_migrate_state_find(struct xfrm_migrate
*m
)
1187 struct xfrm_state
*x
;
1188 struct hlist_node
*entry
;
1191 h
= xfrm_dst_hash(&m
->old_daddr
, &m
->old_saddr
,
1192 m
->reqid
, m
->old_family
);
1193 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+h
, bydst
) {
1194 if (x
->props
.mode
!= m
->mode
||
1195 x
->id
.proto
!= m
->proto
)
1197 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1199 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1201 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1208 h
= xfrm_src_hash(&m
->old_daddr
, &m
->old_saddr
,
1210 hlist_for_each_entry(x
, entry
, xfrm_state_bysrc
+h
, bysrc
) {
1211 if (x
->props
.mode
!= m
->mode
||
1212 x
->id
.proto
!= m
->proto
)
1214 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1216 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1226 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1228 struct xfrm_state
* xfrm_state_migrate(struct xfrm_state
*x
,
1229 struct xfrm_migrate
*m
)
1231 struct xfrm_state
*xc
;
1234 xc
= xfrm_state_clone(x
, &err
);
1238 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1239 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1242 if (!xfrm_addr_cmp(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1243 /* a care is needed when the destination address of the
1244 state is to be updated as it is a part of triplet */
1245 xfrm_state_insert(xc
);
1247 if ((err
= xfrm_state_add(xc
)) < 0)
1256 EXPORT_SYMBOL(xfrm_state_migrate
);
1259 int xfrm_state_update(struct xfrm_state
*x
)
1261 struct xfrm_state
*x1
;
1263 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1265 spin_lock_bh(&xfrm_state_lock
);
1266 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1272 if (xfrm_state_kern(x1
)) {
1278 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1279 __xfrm_state_insert(x
);
1285 spin_unlock_bh(&xfrm_state_lock
);
1291 xfrm_state_delete(x1
);
1297 spin_lock_bh(&x1
->lock
);
1298 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1299 if (x
->encap
&& x1
->encap
)
1300 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1301 if (x
->coaddr
&& x1
->coaddr
) {
1302 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1304 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1305 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1306 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1309 mod_timer(&x1
->timer
, jiffies
+ HZ
);
1310 if (x1
->curlft
.use_time
)
1311 xfrm_state_check_expire(x1
);
1315 spin_unlock_bh(&x1
->lock
);
1321 EXPORT_SYMBOL(xfrm_state_update
);
1323 int xfrm_state_check_expire(struct xfrm_state
*x
)
1325 if (!x
->curlft
.use_time
)
1326 x
->curlft
.use_time
= get_seconds();
1328 if (x
->km
.state
!= XFRM_STATE_VALID
)
1331 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1332 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1333 x
->km
.state
= XFRM_STATE_EXPIRED
;
1334 mod_timer(&x
->timer
, jiffies
);
1339 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1340 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1342 km_state_expired(x
, 0, 0);
1346 EXPORT_SYMBOL(xfrm_state_check_expire
);
1349 xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
,
1350 unsigned short family
)
1352 struct xfrm_state
*x
;
1354 spin_lock_bh(&xfrm_state_lock
);
1355 x
= __xfrm_state_lookup(daddr
, spi
, proto
, family
);
1356 spin_unlock_bh(&xfrm_state_lock
);
1359 EXPORT_SYMBOL(xfrm_state_lookup
);
1362 xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1363 u8 proto
, unsigned short family
)
1365 struct xfrm_state
*x
;
1367 spin_lock_bh(&xfrm_state_lock
);
1368 x
= __xfrm_state_lookup_byaddr(daddr
, saddr
, proto
, family
);
1369 spin_unlock_bh(&xfrm_state_lock
);
1372 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1375 xfrm_find_acq(u8 mode
, u32 reqid
, u8 proto
,
1376 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1377 int create
, unsigned short family
)
1379 struct xfrm_state
*x
;
1381 spin_lock_bh(&xfrm_state_lock
);
1382 x
= __find_acq_core(family
, mode
, reqid
, proto
, daddr
, saddr
, create
);
1383 spin_unlock_bh(&xfrm_state_lock
);
1387 EXPORT_SYMBOL(xfrm_find_acq
);
1389 #ifdef CONFIG_XFRM_SUB_POLICY
1391 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1392 unsigned short family
)
1395 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1397 return -EAFNOSUPPORT
;
1399 spin_lock_bh(&xfrm_state_lock
);
1400 if (afinfo
->tmpl_sort
)
1401 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1402 spin_unlock_bh(&xfrm_state_lock
);
1403 xfrm_state_put_afinfo(afinfo
);
1406 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1409 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1410 unsigned short family
)
1413 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1415 return -EAFNOSUPPORT
;
1417 spin_lock_bh(&xfrm_state_lock
);
1418 if (afinfo
->state_sort
)
1419 err
= afinfo
->state_sort(dst
, src
, n
);
1420 spin_unlock_bh(&xfrm_state_lock
);
1421 xfrm_state_put_afinfo(afinfo
);
1424 EXPORT_SYMBOL(xfrm_state_sort
);
1427 /* Silly enough, but I'm lazy to build resolution list */
1429 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
)
1433 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
1434 struct hlist_node
*entry
;
1435 struct xfrm_state
*x
;
1437 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
1438 if (x
->km
.seq
== seq
&&
1439 x
->km
.state
== XFRM_STATE_ACQ
) {
1448 struct xfrm_state
*xfrm_find_acq_byseq(u32 seq
)
1450 struct xfrm_state
*x
;
1452 spin_lock_bh(&xfrm_state_lock
);
1453 x
= __xfrm_find_acq_byseq(seq
);
1454 spin_unlock_bh(&xfrm_state_lock
);
1457 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1459 u32
xfrm_get_acqseq(void)
1463 static DEFINE_SPINLOCK(acqseq_lock
);
1465 spin_lock_bh(&acqseq_lock
);
1466 res
= (++acqseq
? : ++acqseq
);
1467 spin_unlock_bh(&acqseq_lock
);
1470 EXPORT_SYMBOL(xfrm_get_acqseq
);
1472 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1475 struct xfrm_state
*x0
;
1477 __be32 minspi
= htonl(low
);
1478 __be32 maxspi
= htonl(high
);
1480 spin_lock_bh(&x
->lock
);
1481 if (x
->km
.state
== XFRM_STATE_DEAD
)
1490 if (minspi
== maxspi
) {
1491 x0
= xfrm_state_lookup(&x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1499 for (h
=0; h
<high
-low
+1; h
++) {
1500 spi
= low
+ net_random()%(high
-low
+1);
1501 x0
= xfrm_state_lookup(&x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1503 x
->id
.spi
= htonl(spi
);
1510 spin_lock_bh(&xfrm_state_lock
);
1511 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1512 hlist_add_head(&x
->byspi
, xfrm_state_byspi
+h
);
1513 spin_unlock_bh(&xfrm_state_lock
);
1519 spin_unlock_bh(&x
->lock
);
1523 EXPORT_SYMBOL(xfrm_alloc_spi
);
1525 int xfrm_state_walk(u8 proto
, int (*func
)(struct xfrm_state
*, int, void*),
1529 struct xfrm_state
*x
, *last
= NULL
;
1530 struct hlist_node
*entry
;
1534 spin_lock_bh(&xfrm_state_lock
);
1535 for (i
= 0; i
<= xfrm_state_hmask
; i
++) {
1536 hlist_for_each_entry(x
, entry
, xfrm_state_bydst
+i
, bydst
) {
1537 if (!xfrm_id_proto_match(x
->id
.proto
, proto
))
1540 err
= func(last
, count
, data
);
1552 err
= func(last
, 0, data
);
1554 spin_unlock_bh(&xfrm_state_lock
);
1557 EXPORT_SYMBOL(xfrm_state_walk
);
1560 void xfrm_replay_notify(struct xfrm_state
*x
, int event
)
1563 /* we send notify messages in case
1564 * 1. we updated on of the sequence numbers, and the seqno difference
1565 * is at least x->replay_maxdiff, in this case we also update the
1566 * timeout of our timer function
1567 * 2. if x->replay_maxage has elapsed since last update,
1568 * and there were changes
1570 * The state structure must be locked!
1574 case XFRM_REPLAY_UPDATE
:
1575 if (x
->replay_maxdiff
&&
1576 (x
->replay
.seq
- x
->preplay
.seq
< x
->replay_maxdiff
) &&
1577 (x
->replay
.oseq
- x
->preplay
.oseq
< x
->replay_maxdiff
)) {
1578 if (x
->xflags
& XFRM_TIME_DEFER
)
1579 event
= XFRM_REPLAY_TIMEOUT
;
1586 case XFRM_REPLAY_TIMEOUT
:
1587 if ((x
->replay
.seq
== x
->preplay
.seq
) &&
1588 (x
->replay
.bitmap
== x
->preplay
.bitmap
) &&
1589 (x
->replay
.oseq
== x
->preplay
.oseq
)) {
1590 x
->xflags
|= XFRM_TIME_DEFER
;
1597 memcpy(&x
->preplay
, &x
->replay
, sizeof(struct xfrm_replay_state
));
1598 c
.event
= XFRM_MSG_NEWAE
;
1599 c
.data
.aevent
= event
;
1600 km_state_notify(x
, &c
);
1602 if (x
->replay_maxage
&&
1603 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
1604 x
->xflags
&= ~XFRM_TIME_DEFER
;
1607 static void xfrm_replay_timer_handler(unsigned long data
)
1609 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
1611 spin_lock(&x
->lock
);
1613 if (x
->km
.state
== XFRM_STATE_VALID
) {
1614 if (xfrm_aevent_is_on())
1615 xfrm_replay_notify(x
, XFRM_REPLAY_TIMEOUT
);
1617 x
->xflags
|= XFRM_TIME_DEFER
;
1620 spin_unlock(&x
->lock
);
1623 int xfrm_replay_check(struct xfrm_state
*x
,
1624 struct sk_buff
*skb
, __be32 net_seq
)
1627 u32 seq
= ntohl(net_seq
);
1629 if (unlikely(seq
== 0))
1632 if (likely(seq
> x
->replay
.seq
))
1635 diff
= x
->replay
.seq
- seq
;
1636 if (diff
>= min_t(unsigned int, x
->props
.replay_window
,
1637 sizeof(x
->replay
.bitmap
) * 8)) {
1638 x
->stats
.replay_window
++;
1642 if (x
->replay
.bitmap
& (1U << diff
)) {
1649 xfrm_audit_state_replay(x
, skb
, net_seq
);
1653 void xfrm_replay_advance(struct xfrm_state
*x
, __be32 net_seq
)
1656 u32 seq
= ntohl(net_seq
);
1658 if (seq
> x
->replay
.seq
) {
1659 diff
= seq
- x
->replay
.seq
;
1660 if (diff
< x
->props
.replay_window
)
1661 x
->replay
.bitmap
= ((x
->replay
.bitmap
) << diff
) | 1;
1663 x
->replay
.bitmap
= 1;
1664 x
->replay
.seq
= seq
;
1666 diff
= x
->replay
.seq
- seq
;
1667 x
->replay
.bitmap
|= (1U << diff
);
1670 if (xfrm_aevent_is_on())
1671 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
1674 static LIST_HEAD(xfrm_km_list
);
1675 static DEFINE_RWLOCK(xfrm_km_lock
);
1677 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1679 struct xfrm_mgr
*km
;
1681 read_lock(&xfrm_km_lock
);
1682 list_for_each_entry(km
, &xfrm_km_list
, list
)
1683 if (km
->notify_policy
)
1684 km
->notify_policy(xp
, dir
, c
);
1685 read_unlock(&xfrm_km_lock
);
1688 void km_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1690 struct xfrm_mgr
*km
;
1691 read_lock(&xfrm_km_lock
);
1692 list_for_each_entry(km
, &xfrm_km_list
, list
)
1695 read_unlock(&xfrm_km_lock
);
1698 EXPORT_SYMBOL(km_policy_notify
);
1699 EXPORT_SYMBOL(km_state_notify
);
1701 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
)
1707 c
.event
= XFRM_MSG_EXPIRE
;
1708 km_state_notify(x
, &c
);
1714 EXPORT_SYMBOL(km_state_expired
);
1716 * We send to all registered managers regardless of failure
1717 * We are happy with one success
1719 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1721 int err
= -EINVAL
, acqret
;
1722 struct xfrm_mgr
*km
;
1724 read_lock(&xfrm_km_lock
);
1725 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1726 acqret
= km
->acquire(x
, t
, pol
, XFRM_POLICY_OUT
);
1730 read_unlock(&xfrm_km_lock
);
1733 EXPORT_SYMBOL(km_query
);
1735 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1738 struct xfrm_mgr
*km
;
1740 read_lock(&xfrm_km_lock
);
1741 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1742 if (km
->new_mapping
)
1743 err
= km
->new_mapping(x
, ipaddr
, sport
);
1747 read_unlock(&xfrm_km_lock
);
1750 EXPORT_SYMBOL(km_new_mapping
);
1752 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 pid
)
1758 c
.event
= XFRM_MSG_POLEXPIRE
;
1759 km_policy_notify(pol
, dir
, &c
);
1764 EXPORT_SYMBOL(km_policy_expired
);
1766 #ifdef CONFIG_XFRM_MIGRATE
1767 int km_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1768 struct xfrm_migrate
*m
, int num_migrate
)
1772 struct xfrm_mgr
*km
;
1774 read_lock(&xfrm_km_lock
);
1775 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1777 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
);
1782 read_unlock(&xfrm_km_lock
);
1785 EXPORT_SYMBOL(km_migrate
);
1788 int km_report(u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
1792 struct xfrm_mgr
*km
;
1794 read_lock(&xfrm_km_lock
);
1795 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1797 ret
= km
->report(proto
, sel
, addr
);
1802 read_unlock(&xfrm_km_lock
);
1805 EXPORT_SYMBOL(km_report
);
1807 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
1811 struct xfrm_mgr
*km
;
1812 struct xfrm_policy
*pol
= NULL
;
1814 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
1817 data
= kmalloc(optlen
, GFP_KERNEL
);
1822 if (copy_from_user(data
, optval
, optlen
))
1826 read_lock(&xfrm_km_lock
);
1827 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1828 pol
= km
->compile_policy(sk
, optname
, data
,
1833 read_unlock(&xfrm_km_lock
);
1836 xfrm_sk_policy_insert(sk
, err
, pol
);
1845 EXPORT_SYMBOL(xfrm_user_policy
);
1847 int xfrm_register_km(struct xfrm_mgr
*km
)
1849 write_lock_bh(&xfrm_km_lock
);
1850 list_add_tail(&km
->list
, &xfrm_km_list
);
1851 write_unlock_bh(&xfrm_km_lock
);
1854 EXPORT_SYMBOL(xfrm_register_km
);
1856 int xfrm_unregister_km(struct xfrm_mgr
*km
)
1858 write_lock_bh(&xfrm_km_lock
);
1859 list_del(&km
->list
);
1860 write_unlock_bh(&xfrm_km_lock
);
1863 EXPORT_SYMBOL(xfrm_unregister_km
);
1865 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
1868 if (unlikely(afinfo
== NULL
))
1870 if (unlikely(afinfo
->family
>= NPROTO
))
1871 return -EAFNOSUPPORT
;
1872 write_lock_bh(&xfrm_state_afinfo_lock
);
1873 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
1876 xfrm_state_afinfo
[afinfo
->family
] = afinfo
;
1877 write_unlock_bh(&xfrm_state_afinfo_lock
);
1880 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
1882 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
1885 if (unlikely(afinfo
== NULL
))
1887 if (unlikely(afinfo
->family
>= NPROTO
))
1888 return -EAFNOSUPPORT
;
1889 write_lock_bh(&xfrm_state_afinfo_lock
);
1890 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
1891 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != afinfo
))
1894 xfrm_state_afinfo
[afinfo
->family
] = NULL
;
1896 write_unlock_bh(&xfrm_state_afinfo_lock
);
1899 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
1901 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
1903 struct xfrm_state_afinfo
*afinfo
;
1904 if (unlikely(family
>= NPROTO
))
1906 read_lock(&xfrm_state_afinfo_lock
);
1907 afinfo
= xfrm_state_afinfo
[family
];
1908 if (unlikely(!afinfo
))
1909 read_unlock(&xfrm_state_afinfo_lock
);
1913 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
)
1914 __releases(xfrm_state_afinfo_lock
)
1916 read_unlock(&xfrm_state_afinfo_lock
);
1919 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1920 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
1923 struct xfrm_state
*t
= x
->tunnel
;
1925 if (atomic_read(&t
->tunnel_users
) == 2)
1926 xfrm_state_delete(t
);
1927 atomic_dec(&t
->tunnel_users
);
1932 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
1934 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
1938 spin_lock_bh(&x
->lock
);
1939 if (x
->km
.state
== XFRM_STATE_VALID
&&
1940 x
->type
&& x
->type
->get_mtu
)
1941 res
= x
->type
->get_mtu(x
, mtu
);
1943 res
= mtu
- x
->props
.header_len
;
1944 spin_unlock_bh(&x
->lock
);
1948 int xfrm_init_state(struct xfrm_state
*x
)
1950 struct xfrm_state_afinfo
*afinfo
;
1951 struct xfrm_mode
*inner_mode
;
1952 int family
= x
->props
.family
;
1955 err
= -EAFNOSUPPORT
;
1956 afinfo
= xfrm_state_get_afinfo(family
);
1961 if (afinfo
->init_flags
)
1962 err
= afinfo
->init_flags(x
);
1964 xfrm_state_put_afinfo(afinfo
);
1969 err
= -EPROTONOSUPPORT
;
1971 if (x
->sel
.family
!= AF_UNSPEC
) {
1972 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
1973 if (inner_mode
== NULL
)
1976 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
1977 family
!= x
->sel
.family
) {
1978 xfrm_put_mode(inner_mode
);
1982 x
->inner_mode
= inner_mode
;
1984 struct xfrm_mode
*inner_mode_iaf
;
1986 inner_mode
= xfrm_get_mode(x
->props
.mode
, AF_INET
);
1987 if (inner_mode
== NULL
)
1990 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
1991 xfrm_put_mode(inner_mode
);
1995 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, AF_INET6
);
1996 if (inner_mode_iaf
== NULL
)
1999 if (!(inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2000 xfrm_put_mode(inner_mode_iaf
);
2004 if (x
->props
.family
== AF_INET
) {
2005 x
->inner_mode
= inner_mode
;
2006 x
->inner_mode_iaf
= inner_mode_iaf
;
2008 x
->inner_mode
= inner_mode_iaf
;
2009 x
->inner_mode_iaf
= inner_mode
;
2013 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2014 if (x
->type
== NULL
)
2017 err
= x
->type
->init_state(x
);
2021 x
->outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2022 if (x
->outer_mode
== NULL
)
2025 x
->km
.state
= XFRM_STATE_VALID
;
2031 EXPORT_SYMBOL(xfrm_init_state
);
2033 void __init
xfrm_state_init(void)
2037 sz
= sizeof(struct hlist_head
) * 8;
2039 xfrm_state_bydst
= xfrm_hash_alloc(sz
);
2040 xfrm_state_bysrc
= xfrm_hash_alloc(sz
);
2041 xfrm_state_byspi
= xfrm_hash_alloc(sz
);
2042 if (!xfrm_state_bydst
|| !xfrm_state_bysrc
|| !xfrm_state_byspi
)
2043 panic("XFRM: Cannot allocate bydst/bysrc/byspi hashes.");
2044 xfrm_state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2046 INIT_WORK(&xfrm_state_gc_work
, xfrm_state_gc_task
);
2049 #ifdef CONFIG_AUDITSYSCALL
2050 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2051 struct audit_buffer
*audit_buf
)
2053 struct xfrm_sec_ctx
*ctx
= x
->security
;
2054 u32 spi
= ntohl(x
->id
.spi
);
2057 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2058 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2060 switch(x
->props
.family
) {
2062 audit_log_format(audit_buf
,
2063 " src=" NIPQUAD_FMT
" dst=" NIPQUAD_FMT
,
2064 NIPQUAD(x
->props
.saddr
.a4
),
2065 NIPQUAD(x
->id
.daddr
.a4
));
2068 audit_log_format(audit_buf
,
2069 " src=" NIP6_FMT
" dst=" NIP6_FMT
,
2070 NIP6(*(struct in6_addr
*)x
->props
.saddr
.a6
),
2071 NIP6(*(struct in6_addr
*)x
->id
.daddr
.a6
));
2075 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2078 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2079 struct audit_buffer
*audit_buf
)
2082 struct ipv6hdr
*iph6
;
2087 audit_log_format(audit_buf
,
2088 " src=" NIPQUAD_FMT
" dst=" NIPQUAD_FMT
,
2089 NIPQUAD(iph4
->saddr
),
2090 NIPQUAD(iph4
->daddr
));
2093 iph6
= ipv6_hdr(skb
);
2094 audit_log_format(audit_buf
,
2095 " src=" NIP6_FMT
" dst=" NIP6_FMT
2096 " flowlbl=0x%x%x%x",
2099 iph6
->flow_lbl
[0] & 0x0f,
2106 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
,
2107 u32 auid
, u32 secid
)
2109 struct audit_buffer
*audit_buf
;
2111 audit_buf
= xfrm_audit_start("SAD-add");
2112 if (audit_buf
== NULL
)
2114 xfrm_audit_helper_usrinfo(auid
, secid
, audit_buf
);
2115 xfrm_audit_helper_sainfo(x
, audit_buf
);
2116 audit_log_format(audit_buf
, " res=%u", result
);
2117 audit_log_end(audit_buf
);
2119 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2121 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
,
2122 u32 auid
, u32 secid
)
2124 struct audit_buffer
*audit_buf
;
2126 audit_buf
= xfrm_audit_start("SAD-delete");
2127 if (audit_buf
== NULL
)
2129 xfrm_audit_helper_usrinfo(auid
, secid
, audit_buf
);
2130 xfrm_audit_helper_sainfo(x
, audit_buf
);
2131 audit_log_format(audit_buf
, " res=%u", result
);
2132 audit_log_end(audit_buf
);
2134 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2136 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2137 struct sk_buff
*skb
)
2139 struct audit_buffer
*audit_buf
;
2142 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2143 if (audit_buf
== NULL
)
2145 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2146 /* don't record the sequence number because it's inherent in this kind
2147 * of audit message */
2148 spi
= ntohl(x
->id
.spi
);
2149 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2150 audit_log_end(audit_buf
);
2152 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2154 static void xfrm_audit_state_replay(struct xfrm_state
*x
,
2155 struct sk_buff
*skb
, __be32 net_seq
)
2157 struct audit_buffer
*audit_buf
;
2160 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2161 if (audit_buf
== NULL
)
2163 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2164 spi
= ntohl(x
->id
.spi
);
2165 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2166 spi
, spi
, ntohl(net_seq
));
2167 audit_log_end(audit_buf
);
2170 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2172 struct audit_buffer
*audit_buf
;
2174 audit_buf
= xfrm_audit_start("SA-notfound");
2175 if (audit_buf
== NULL
)
2177 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2178 audit_log_end(audit_buf
);
2180 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2182 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2183 __be32 net_spi
, __be32 net_seq
)
2185 struct audit_buffer
*audit_buf
;
2188 audit_buf
= xfrm_audit_start("SA-notfound");
2189 if (audit_buf
== NULL
)
2191 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2192 spi
= ntohl(net_spi
);
2193 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2194 spi
, spi
, ntohl(net_seq
));
2195 audit_log_end(audit_buf
);
2197 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2199 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2200 struct sk_buff
*skb
, u8 proto
)
2202 struct audit_buffer
*audit_buf
;
2206 audit_buf
= xfrm_audit_start("SA-icv-failure");
2207 if (audit_buf
== NULL
)
2209 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2210 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2211 u32 spi
= ntohl(net_spi
);
2212 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2213 spi
, spi
, ntohl(net_seq
));
2215 audit_log_end(audit_buf
);
2217 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2218 #endif /* CONFIG_AUDITSYSCALL */