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 unsigned int xfrm_state_hashmax __read_mostly
= 1 * 1024 * 1024;
48 static unsigned int xfrm_state_genid
;
50 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
);
51 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
);
53 #ifdef CONFIG_AUDITSYSCALL
54 static void xfrm_audit_state_replay(struct xfrm_state
*x
,
55 struct sk_buff
*skb
, __be32 net_seq
);
57 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
58 #endif /* CONFIG_AUDITSYSCALL */
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
, init_net
.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
, init_net
.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
, init_net
.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(unsigned int state_hmask
)
114 return ((state_hmask
+ 1) << 1) * sizeof(struct hlist_head
);
117 static DEFINE_MUTEX(hash_resize_mutex
);
119 static void xfrm_hash_resize(struct work_struct
*work
)
121 struct net
*net
= container_of(work
, struct net
, xfrm
.state_hash_work
);
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(net
->xfrm
.state_hmask
);
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
= net
->xfrm
.state_hmask
; i
>= 0; i
--)
149 xfrm_hash_transfer(net
->xfrm
.state_bydst
+i
, ndst
, nsrc
, nspi
,
152 odst
= net
->xfrm
.state_bydst
;
153 osrc
= net
->xfrm
.state_bysrc
;
154 ospi
= net
->xfrm
.state_byspi
;
155 ohashmask
= net
->xfrm
.state_hmask
;
157 net
->xfrm
.state_bydst
= ndst
;
158 net
->xfrm
.state_bysrc
= nsrc
;
159 net
->xfrm
.state_byspi
= nspi
;
160 net
->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 DEFINE_RWLOCK(xfrm_state_afinfo_lock
);
174 static struct xfrm_state_afinfo
*xfrm_state_afinfo
[NPROTO
];
176 static DEFINE_SPINLOCK(xfrm_state_gc_lock
);
178 int __xfrm_state_delete(struct xfrm_state
*x
);
180 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
);
181 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
);
183 static struct xfrm_state_afinfo
*xfrm_state_lock_afinfo(unsigned int family
)
185 struct xfrm_state_afinfo
*afinfo
;
186 if (unlikely(family
>= NPROTO
))
188 write_lock_bh(&xfrm_state_afinfo_lock
);
189 afinfo
= xfrm_state_afinfo
[family
];
190 if (unlikely(!afinfo
))
191 write_unlock_bh(&xfrm_state_afinfo_lock
);
195 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo
*afinfo
)
196 __releases(xfrm_state_afinfo_lock
)
198 write_unlock_bh(&xfrm_state_afinfo_lock
);
201 int xfrm_register_type(const struct xfrm_type
*type
, unsigned short family
)
203 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
204 const struct xfrm_type
**typemap
;
207 if (unlikely(afinfo
== NULL
))
208 return -EAFNOSUPPORT
;
209 typemap
= afinfo
->type_map
;
211 if (likely(typemap
[type
->proto
] == NULL
))
212 typemap
[type
->proto
] = type
;
215 xfrm_state_unlock_afinfo(afinfo
);
218 EXPORT_SYMBOL(xfrm_register_type
);
220 int xfrm_unregister_type(const struct xfrm_type
*type
, unsigned short family
)
222 struct xfrm_state_afinfo
*afinfo
= xfrm_state_lock_afinfo(family
);
223 const struct xfrm_type
**typemap
;
226 if (unlikely(afinfo
== NULL
))
227 return -EAFNOSUPPORT
;
228 typemap
= afinfo
->type_map
;
230 if (unlikely(typemap
[type
->proto
] != type
))
233 typemap
[type
->proto
] = NULL
;
234 xfrm_state_unlock_afinfo(afinfo
);
237 EXPORT_SYMBOL(xfrm_unregister_type
);
239 static const struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
241 struct xfrm_state_afinfo
*afinfo
;
242 const struct xfrm_type
**typemap
;
243 const struct xfrm_type
*type
;
244 int modload_attempted
= 0;
247 afinfo
= xfrm_state_get_afinfo(family
);
248 if (unlikely(afinfo
== NULL
))
250 typemap
= afinfo
->type_map
;
252 type
= typemap
[proto
];
253 if (unlikely(type
&& !try_module_get(type
->owner
)))
255 if (!type
&& !modload_attempted
) {
256 xfrm_state_put_afinfo(afinfo
);
257 request_module("xfrm-type-%d-%d", family
, proto
);
258 modload_attempted
= 1;
262 xfrm_state_put_afinfo(afinfo
);
266 static void xfrm_put_type(const struct xfrm_type
*type
)
268 module_put(type
->owner
);
271 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
273 struct xfrm_state_afinfo
*afinfo
;
274 struct xfrm_mode
**modemap
;
277 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
280 afinfo
= xfrm_state_lock_afinfo(family
);
281 if (unlikely(afinfo
== NULL
))
282 return -EAFNOSUPPORT
;
285 modemap
= afinfo
->mode_map
;
286 if (modemap
[mode
->encap
])
290 if (!try_module_get(afinfo
->owner
))
293 mode
->afinfo
= afinfo
;
294 modemap
[mode
->encap
] = mode
;
298 xfrm_state_unlock_afinfo(afinfo
);
301 EXPORT_SYMBOL(xfrm_register_mode
);
303 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
305 struct xfrm_state_afinfo
*afinfo
;
306 struct xfrm_mode
**modemap
;
309 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
312 afinfo
= xfrm_state_lock_afinfo(family
);
313 if (unlikely(afinfo
== NULL
))
314 return -EAFNOSUPPORT
;
317 modemap
= afinfo
->mode_map
;
318 if (likely(modemap
[mode
->encap
] == mode
)) {
319 modemap
[mode
->encap
] = NULL
;
320 module_put(mode
->afinfo
->owner
);
324 xfrm_state_unlock_afinfo(afinfo
);
327 EXPORT_SYMBOL(xfrm_unregister_mode
);
329 static struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
331 struct xfrm_state_afinfo
*afinfo
;
332 struct xfrm_mode
*mode
;
333 int modload_attempted
= 0;
335 if (unlikely(encap
>= XFRM_MODE_MAX
))
339 afinfo
= xfrm_state_get_afinfo(family
);
340 if (unlikely(afinfo
== NULL
))
343 mode
= afinfo
->mode_map
[encap
];
344 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
346 if (!mode
&& !modload_attempted
) {
347 xfrm_state_put_afinfo(afinfo
);
348 request_module("xfrm-mode-%d-%d", family
, encap
);
349 modload_attempted
= 1;
353 xfrm_state_put_afinfo(afinfo
);
357 static void xfrm_put_mode(struct xfrm_mode
*mode
)
359 module_put(mode
->owner
);
362 static void xfrm_state_gc_destroy(struct xfrm_state
*x
)
364 del_timer_sync(&x
->timer
);
365 del_timer_sync(&x
->rtimer
);
372 xfrm_put_mode(x
->inner_mode
);
373 if (x
->inner_mode_iaf
)
374 xfrm_put_mode(x
->inner_mode_iaf
);
376 xfrm_put_mode(x
->outer_mode
);
378 x
->type
->destructor(x
);
379 xfrm_put_type(x
->type
);
381 security_xfrm_state_free(x
);
385 static void xfrm_state_gc_task(struct work_struct
*work
)
387 struct net
*net
= container_of(work
, struct net
, xfrm
.state_gc_work
);
388 struct xfrm_state
*x
;
389 struct hlist_node
*entry
, *tmp
;
390 struct hlist_head gc_list
;
392 spin_lock_bh(&xfrm_state_gc_lock
);
393 hlist_move_list(&net
->xfrm
.state_gc_list
, &gc_list
);
394 spin_unlock_bh(&xfrm_state_gc_lock
);
396 hlist_for_each_entry_safe(x
, entry
, tmp
, &gc_list
, gclist
)
397 xfrm_state_gc_destroy(x
);
399 wake_up(&net
->xfrm
.km_waitq
);
402 static inline unsigned long make_jiffies(long secs
)
404 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
405 return MAX_SCHEDULE_TIMEOUT
-1;
410 static void xfrm_timer_handler(unsigned long data
)
412 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
413 unsigned long now
= get_seconds();
414 long next
= LONG_MAX
;
419 if (x
->km
.state
== XFRM_STATE_DEAD
)
421 if (x
->km
.state
== XFRM_STATE_EXPIRED
)
423 if (x
->lft
.hard_add_expires_seconds
) {
424 long tmo
= x
->lft
.hard_add_expires_seconds
+
425 x
->curlft
.add_time
- now
;
431 if (x
->lft
.hard_use_expires_seconds
) {
432 long tmo
= x
->lft
.hard_use_expires_seconds
+
433 (x
->curlft
.use_time
? : now
) - now
;
441 if (x
->lft
.soft_add_expires_seconds
) {
442 long tmo
= x
->lft
.soft_add_expires_seconds
+
443 x
->curlft
.add_time
- now
;
449 if (x
->lft
.soft_use_expires_seconds
) {
450 long tmo
= x
->lft
.soft_use_expires_seconds
+
451 (x
->curlft
.use_time
? : now
) - now
;
460 km_state_expired(x
, 0, 0);
462 if (next
!= LONG_MAX
)
463 mod_timer(&x
->timer
, jiffies
+ make_jiffies(next
));
468 if (x
->km
.state
== XFRM_STATE_ACQ
&& x
->id
.spi
== 0) {
469 x
->km
.state
= XFRM_STATE_EXPIRED
;
470 wake_up(&init_net
.xfrm
.km_waitq
);
475 err
= __xfrm_state_delete(x
);
476 if (!err
&& x
->id
.spi
)
477 km_state_expired(x
, 1, 0);
479 xfrm_audit_state_delete(x
, err
? 0 : 1,
480 audit_get_loginuid(current
),
481 audit_get_sessionid(current
), 0);
484 spin_unlock(&x
->lock
);
487 static void xfrm_replay_timer_handler(unsigned long data
);
489 struct xfrm_state
*xfrm_state_alloc(struct net
*net
)
491 struct xfrm_state
*x
;
493 x
= kzalloc(sizeof(struct xfrm_state
), GFP_ATOMIC
);
496 write_pnet(&x
->xs_net
, net
);
497 atomic_set(&x
->refcnt
, 1);
498 atomic_set(&x
->tunnel_users
, 0);
499 INIT_LIST_HEAD(&x
->km
.all
);
500 INIT_HLIST_NODE(&x
->bydst
);
501 INIT_HLIST_NODE(&x
->bysrc
);
502 INIT_HLIST_NODE(&x
->byspi
);
503 setup_timer(&x
->timer
, xfrm_timer_handler
, (unsigned long)x
);
504 setup_timer(&x
->rtimer
, xfrm_replay_timer_handler
,
506 x
->curlft
.add_time
= get_seconds();
507 x
->lft
.soft_byte_limit
= XFRM_INF
;
508 x
->lft
.soft_packet_limit
= XFRM_INF
;
509 x
->lft
.hard_byte_limit
= XFRM_INF
;
510 x
->lft
.hard_packet_limit
= XFRM_INF
;
511 x
->replay_maxage
= 0;
512 x
->replay_maxdiff
= 0;
513 x
->inner_mode
= NULL
;
514 x
->inner_mode_iaf
= NULL
;
515 spin_lock_init(&x
->lock
);
519 EXPORT_SYMBOL(xfrm_state_alloc
);
521 void __xfrm_state_destroy(struct xfrm_state
*x
)
523 WARN_ON(x
->km
.state
!= XFRM_STATE_DEAD
);
525 spin_lock_bh(&xfrm_state_gc_lock
);
526 hlist_add_head(&x
->gclist
, &init_net
.xfrm
.state_gc_list
);
527 spin_unlock_bh(&xfrm_state_gc_lock
);
528 schedule_work(&init_net
.xfrm
.state_gc_work
);
530 EXPORT_SYMBOL(__xfrm_state_destroy
);
532 int __xfrm_state_delete(struct xfrm_state
*x
)
536 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
537 x
->km
.state
= XFRM_STATE_DEAD
;
538 spin_lock(&xfrm_state_lock
);
539 list_del(&x
->km
.all
);
540 hlist_del(&x
->bydst
);
541 hlist_del(&x
->bysrc
);
543 hlist_del(&x
->byspi
);
544 init_net
.xfrm
.state_num
--;
545 spin_unlock(&xfrm_state_lock
);
547 /* All xfrm_state objects are created by xfrm_state_alloc.
548 * The xfrm_state_alloc call gives a reference, and that
549 * is what we are dropping here.
557 EXPORT_SYMBOL(__xfrm_state_delete
);
559 int xfrm_state_delete(struct xfrm_state
*x
)
563 spin_lock_bh(&x
->lock
);
564 err
= __xfrm_state_delete(x
);
565 spin_unlock_bh(&x
->lock
);
569 EXPORT_SYMBOL(xfrm_state_delete
);
571 #ifdef CONFIG_SECURITY_NETWORK_XFRM
573 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
577 for (i
= 0; i
<= init_net
.xfrm
.state_hmask
; i
++) {
578 struct hlist_node
*entry
;
579 struct xfrm_state
*x
;
581 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+i
, bydst
) {
582 if (xfrm_id_proto_match(x
->id
.proto
, proto
) &&
583 (err
= security_xfrm_state_delete(x
)) != 0) {
584 xfrm_audit_state_delete(x
, 0,
585 audit_info
->loginuid
,
586 audit_info
->sessionid
,
597 xfrm_state_flush_secctx_check(u8 proto
, struct xfrm_audit
*audit_info
)
603 int xfrm_state_flush(u8 proto
, struct xfrm_audit
*audit_info
)
607 spin_lock_bh(&xfrm_state_lock
);
608 err
= xfrm_state_flush_secctx_check(proto
, audit_info
);
612 for (i
= 0; i
<= init_net
.xfrm
.state_hmask
; i
++) {
613 struct hlist_node
*entry
;
614 struct xfrm_state
*x
;
616 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+i
, bydst
) {
617 if (!xfrm_state_kern(x
) &&
618 xfrm_id_proto_match(x
->id
.proto
, proto
)) {
620 spin_unlock_bh(&xfrm_state_lock
);
622 err
= xfrm_state_delete(x
);
623 xfrm_audit_state_delete(x
, err
? 0 : 1,
624 audit_info
->loginuid
,
625 audit_info
->sessionid
,
629 spin_lock_bh(&xfrm_state_lock
);
637 spin_unlock_bh(&xfrm_state_lock
);
638 wake_up(&init_net
.xfrm
.km_waitq
);
641 EXPORT_SYMBOL(xfrm_state_flush
);
643 void xfrm_sad_getinfo(struct xfrmk_sadinfo
*si
)
645 spin_lock_bh(&xfrm_state_lock
);
646 si
->sadcnt
= init_net
.xfrm
.state_num
;
647 si
->sadhcnt
= init_net
.xfrm
.state_hmask
;
648 si
->sadhmcnt
= xfrm_state_hashmax
;
649 spin_unlock_bh(&xfrm_state_lock
);
651 EXPORT_SYMBOL(xfrm_sad_getinfo
);
654 xfrm_init_tempsel(struct xfrm_state
*x
, struct flowi
*fl
,
655 struct xfrm_tmpl
*tmpl
,
656 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
657 unsigned short family
)
659 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
662 afinfo
->init_tempsel(x
, fl
, tmpl
, daddr
, saddr
);
663 xfrm_state_put_afinfo(afinfo
);
667 static struct xfrm_state
*__xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
, unsigned short family
)
669 unsigned int h
= xfrm_spi_hash(daddr
, spi
, proto
, family
);
670 struct xfrm_state
*x
;
671 struct hlist_node
*entry
;
673 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_byspi
+h
, byspi
) {
674 if (x
->props
.family
!= family
||
676 x
->id
.proto
!= proto
)
681 if (x
->id
.daddr
.a4
!= daddr
->a4
)
685 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
699 static struct xfrm_state
*__xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, u8 proto
, unsigned short family
)
701 unsigned int h
= xfrm_src_hash(daddr
, saddr
, family
);
702 struct xfrm_state
*x
;
703 struct hlist_node
*entry
;
705 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bysrc
+h
, bysrc
) {
706 if (x
->props
.family
!= family
||
707 x
->id
.proto
!= proto
)
712 if (x
->id
.daddr
.a4
!= daddr
->a4
||
713 x
->props
.saddr
.a4
!= saddr
->a4
)
717 if (!ipv6_addr_equal((struct in6_addr
*)daddr
,
720 !ipv6_addr_equal((struct in6_addr
*)saddr
,
734 static inline struct xfrm_state
*
735 __xfrm_state_locate(struct xfrm_state
*x
, int use_spi
, int family
)
738 return __xfrm_state_lookup(&x
->id
.daddr
, x
->id
.spi
,
739 x
->id
.proto
, family
);
741 return __xfrm_state_lookup_byaddr(&x
->id
.daddr
,
743 x
->id
.proto
, family
);
746 static void xfrm_hash_grow_check(int have_hash_collision
)
748 if (have_hash_collision
&&
749 (init_net
.xfrm
.state_hmask
+ 1) < xfrm_state_hashmax
&&
750 init_net
.xfrm
.state_num
> init_net
.xfrm
.state_hmask
)
751 schedule_work(&init_net
.xfrm
.state_hash_work
);
755 xfrm_state_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
756 struct flowi
*fl
, struct xfrm_tmpl
*tmpl
,
757 struct xfrm_policy
*pol
, int *err
,
758 unsigned short family
)
761 struct hlist_node
*entry
;
762 struct xfrm_state
*x
, *x0
, *to_put
;
763 int acquire_in_progress
= 0;
765 struct xfrm_state
*best
= NULL
;
769 spin_lock_bh(&xfrm_state_lock
);
770 h
= xfrm_dst_hash(daddr
, saddr
, tmpl
->reqid
, family
);
771 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
772 if (x
->props
.family
== family
&&
773 x
->props
.reqid
== tmpl
->reqid
&&
774 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
775 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
776 tmpl
->mode
== x
->props
.mode
&&
777 tmpl
->id
.proto
== x
->id
.proto
&&
778 (tmpl
->id
.spi
== x
->id
.spi
|| !tmpl
->id
.spi
)) {
780 1. There is a valid state with matching selector.
782 2. Valid state with inappropriate selector. Skip.
784 Entering area of "sysdeps".
786 3. If state is not valid, selector is temporary,
787 it selects only session which triggered
788 previous resolution. Key manager will do
789 something to install a state with proper
792 if (x
->km
.state
== XFRM_STATE_VALID
) {
793 if ((x
->sel
.family
&& !xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
)) ||
794 !security_xfrm_state_pol_flow_match(x
, pol
, fl
))
797 best
->km
.dying
> x
->km
.dying
||
798 (best
->km
.dying
== x
->km
.dying
&&
799 best
->curlft
.add_time
< x
->curlft
.add_time
))
801 } else if (x
->km
.state
== XFRM_STATE_ACQ
) {
802 acquire_in_progress
= 1;
803 } else if (x
->km
.state
== XFRM_STATE_ERROR
||
804 x
->km
.state
== XFRM_STATE_EXPIRED
) {
805 if (xfrm_selector_match(&x
->sel
, fl
, x
->sel
.family
) &&
806 security_xfrm_state_pol_flow_match(x
, pol
, fl
))
813 if (!x
&& !error
&& !acquire_in_progress
) {
815 (x0
= __xfrm_state_lookup(daddr
, tmpl
->id
.spi
,
816 tmpl
->id
.proto
, family
)) != NULL
) {
821 x
= xfrm_state_alloc(&init_net
);
826 /* Initialize temporary selector matching only
827 * to current session. */
828 xfrm_init_tempsel(x
, fl
, tmpl
, daddr
, saddr
, family
);
830 error
= security_xfrm_state_alloc_acquire(x
, pol
->security
, fl
->secid
);
832 x
->km
.state
= XFRM_STATE_DEAD
;
838 if (km_query(x
, tmpl
, pol
) == 0) {
839 x
->km
.state
= XFRM_STATE_ACQ
;
840 list_add(&x
->km
.all
, &init_net
.xfrm
.state_all
);
841 hlist_add_head(&x
->bydst
, init_net
.xfrm
.state_bydst
+h
);
842 h
= xfrm_src_hash(daddr
, saddr
, family
);
843 hlist_add_head(&x
->bysrc
, init_net
.xfrm
.state_bysrc
+h
);
845 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, family
);
846 hlist_add_head(&x
->byspi
, init_net
.xfrm
.state_byspi
+h
);
848 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
849 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
850 add_timer(&x
->timer
);
851 init_net
.xfrm
.state_num
++;
852 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
854 x
->km
.state
= XFRM_STATE_DEAD
;
864 *err
= acquire_in_progress
? -EAGAIN
: error
;
865 spin_unlock_bh(&xfrm_state_lock
);
867 xfrm_state_put(to_put
);
872 xfrm_stateonly_find(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
873 unsigned short family
, u8 mode
, u8 proto
, u32 reqid
)
876 struct xfrm_state
*rx
= NULL
, *x
= NULL
;
877 struct hlist_node
*entry
;
879 spin_lock(&xfrm_state_lock
);
880 h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
881 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
882 if (x
->props
.family
== family
&&
883 x
->props
.reqid
== reqid
&&
884 !(x
->props
.flags
& XFRM_STATE_WILDRECV
) &&
885 xfrm_state_addr_check(x
, daddr
, saddr
, family
) &&
886 mode
== x
->props
.mode
&&
887 proto
== x
->id
.proto
&&
888 x
->km
.state
== XFRM_STATE_VALID
) {
896 spin_unlock(&xfrm_state_lock
);
901 EXPORT_SYMBOL(xfrm_stateonly_find
);
903 static void __xfrm_state_insert(struct xfrm_state
*x
)
907 x
->genid
= ++xfrm_state_genid
;
909 list_add(&x
->km
.all
, &init_net
.xfrm
.state_all
);
911 h
= xfrm_dst_hash(&x
->id
.daddr
, &x
->props
.saddr
,
912 x
->props
.reqid
, x
->props
.family
);
913 hlist_add_head(&x
->bydst
, init_net
.xfrm
.state_bydst
+h
);
915 h
= xfrm_src_hash(&x
->id
.daddr
, &x
->props
.saddr
, x
->props
.family
);
916 hlist_add_head(&x
->bysrc
, init_net
.xfrm
.state_bysrc
+h
);
919 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
,
922 hlist_add_head(&x
->byspi
, init_net
.xfrm
.state_byspi
+h
);
925 mod_timer(&x
->timer
, jiffies
+ HZ
);
926 if (x
->replay_maxage
)
927 mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
);
929 wake_up(&init_net
.xfrm
.km_waitq
);
931 init_net
.xfrm
.state_num
++;
933 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
936 /* xfrm_state_lock is held */
937 static void __xfrm_state_bump_genids(struct xfrm_state
*xnew
)
939 unsigned short family
= xnew
->props
.family
;
940 u32 reqid
= xnew
->props
.reqid
;
941 struct xfrm_state
*x
;
942 struct hlist_node
*entry
;
945 h
= xfrm_dst_hash(&xnew
->id
.daddr
, &xnew
->props
.saddr
, reqid
, family
);
946 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
947 if (x
->props
.family
== family
&&
948 x
->props
.reqid
== reqid
&&
949 !xfrm_addr_cmp(&x
->id
.daddr
, &xnew
->id
.daddr
, family
) &&
950 !xfrm_addr_cmp(&x
->props
.saddr
, &xnew
->props
.saddr
, family
))
951 x
->genid
= xfrm_state_genid
;
955 void xfrm_state_insert(struct xfrm_state
*x
)
957 spin_lock_bh(&xfrm_state_lock
);
958 __xfrm_state_bump_genids(x
);
959 __xfrm_state_insert(x
);
960 spin_unlock_bh(&xfrm_state_lock
);
962 EXPORT_SYMBOL(xfrm_state_insert
);
964 /* xfrm_state_lock is held */
965 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
)
967 unsigned int h
= xfrm_dst_hash(daddr
, saddr
, reqid
, family
);
968 struct hlist_node
*entry
;
969 struct xfrm_state
*x
;
971 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
972 if (x
->props
.reqid
!= reqid
||
973 x
->props
.mode
!= mode
||
974 x
->props
.family
!= family
||
975 x
->km
.state
!= XFRM_STATE_ACQ
||
977 x
->id
.proto
!= proto
)
982 if (x
->id
.daddr
.a4
!= daddr
->a4
||
983 x
->props
.saddr
.a4
!= saddr
->a4
)
987 if (!ipv6_addr_equal((struct in6_addr
*)x
->id
.daddr
.a6
,
988 (struct in6_addr
*)daddr
) ||
989 !ipv6_addr_equal((struct in6_addr
*)
991 (struct in6_addr
*)saddr
))
1003 x
= xfrm_state_alloc(&init_net
);
1007 x
->sel
.daddr
.a4
= daddr
->a4
;
1008 x
->sel
.saddr
.a4
= saddr
->a4
;
1009 x
->sel
.prefixlen_d
= 32;
1010 x
->sel
.prefixlen_s
= 32;
1011 x
->props
.saddr
.a4
= saddr
->a4
;
1012 x
->id
.daddr
.a4
= daddr
->a4
;
1016 ipv6_addr_copy((struct in6_addr
*)x
->sel
.daddr
.a6
,
1017 (struct in6_addr
*)daddr
);
1018 ipv6_addr_copy((struct in6_addr
*)x
->sel
.saddr
.a6
,
1019 (struct in6_addr
*)saddr
);
1020 x
->sel
.prefixlen_d
= 128;
1021 x
->sel
.prefixlen_s
= 128;
1022 ipv6_addr_copy((struct in6_addr
*)x
->props
.saddr
.a6
,
1023 (struct in6_addr
*)saddr
);
1024 ipv6_addr_copy((struct in6_addr
*)x
->id
.daddr
.a6
,
1025 (struct in6_addr
*)daddr
);
1029 x
->km
.state
= XFRM_STATE_ACQ
;
1030 x
->id
.proto
= proto
;
1031 x
->props
.family
= family
;
1032 x
->props
.mode
= mode
;
1033 x
->props
.reqid
= reqid
;
1034 x
->lft
.hard_add_expires_seconds
= sysctl_xfrm_acq_expires
;
1036 x
->timer
.expires
= jiffies
+ sysctl_xfrm_acq_expires
*HZ
;
1037 add_timer(&x
->timer
);
1038 list_add(&x
->km
.all
, &init_net
.xfrm
.state_all
);
1039 hlist_add_head(&x
->bydst
, init_net
.xfrm
.state_bydst
+h
);
1040 h
= xfrm_src_hash(daddr
, saddr
, family
);
1041 hlist_add_head(&x
->bysrc
, init_net
.xfrm
.state_bysrc
+h
);
1043 init_net
.xfrm
.state_num
++;
1045 xfrm_hash_grow_check(x
->bydst
.next
!= NULL
);
1051 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
);
1053 int xfrm_state_add(struct xfrm_state
*x
)
1055 struct xfrm_state
*x1
, *to_put
;
1058 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1060 family
= x
->props
.family
;
1064 spin_lock_bh(&xfrm_state_lock
);
1066 x1
= __xfrm_state_locate(x
, use_spi
, family
);
1074 if (use_spi
&& x
->km
.seq
) {
1075 x1
= __xfrm_find_acq_byseq(x
->km
.seq
);
1076 if (x1
&& ((x1
->id
.proto
!= x
->id
.proto
) ||
1077 xfrm_addr_cmp(&x1
->id
.daddr
, &x
->id
.daddr
, family
))) {
1084 x1
= __find_acq_core(family
, x
->props
.mode
, x
->props
.reqid
,
1086 &x
->id
.daddr
, &x
->props
.saddr
, 0);
1088 __xfrm_state_bump_genids(x
);
1089 __xfrm_state_insert(x
);
1093 spin_unlock_bh(&xfrm_state_lock
);
1096 xfrm_state_delete(x1
);
1101 xfrm_state_put(to_put
);
1105 EXPORT_SYMBOL(xfrm_state_add
);
1107 #ifdef CONFIG_XFRM_MIGRATE
1108 static struct xfrm_state
*xfrm_state_clone(struct xfrm_state
*orig
, int *errp
)
1111 struct xfrm_state
*x
= xfrm_state_alloc(&init_net
);
1115 memcpy(&x
->id
, &orig
->id
, sizeof(x
->id
));
1116 memcpy(&x
->sel
, &orig
->sel
, sizeof(x
->sel
));
1117 memcpy(&x
->lft
, &orig
->lft
, sizeof(x
->lft
));
1118 x
->props
.mode
= orig
->props
.mode
;
1119 x
->props
.replay_window
= orig
->props
.replay_window
;
1120 x
->props
.reqid
= orig
->props
.reqid
;
1121 x
->props
.family
= orig
->props
.family
;
1122 x
->props
.saddr
= orig
->props
.saddr
;
1125 x
->aalg
= xfrm_algo_clone(orig
->aalg
);
1129 x
->props
.aalgo
= orig
->props
.aalgo
;
1132 x
->ealg
= xfrm_algo_clone(orig
->ealg
);
1136 x
->props
.ealgo
= orig
->props
.ealgo
;
1139 x
->calg
= xfrm_algo_clone(orig
->calg
);
1143 x
->props
.calgo
= orig
->props
.calgo
;
1146 x
->encap
= kmemdup(orig
->encap
, sizeof(*x
->encap
), GFP_KERNEL
);
1152 x
->coaddr
= kmemdup(orig
->coaddr
, sizeof(*x
->coaddr
),
1158 err
= xfrm_init_state(x
);
1162 x
->props
.flags
= orig
->props
.flags
;
1164 x
->curlft
.add_time
= orig
->curlft
.add_time
;
1165 x
->km
.state
= orig
->km
.state
;
1166 x
->km
.seq
= orig
->km
.seq
;
1184 /* xfrm_state_lock is held */
1185 struct xfrm_state
* xfrm_migrate_state_find(struct xfrm_migrate
*m
)
1188 struct xfrm_state
*x
;
1189 struct hlist_node
*entry
;
1192 h
= xfrm_dst_hash(&m
->old_daddr
, &m
->old_saddr
,
1193 m
->reqid
, m
->old_family
);
1194 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+h
, bydst
) {
1195 if (x
->props
.mode
!= m
->mode
||
1196 x
->id
.proto
!= m
->proto
)
1198 if (m
->reqid
&& x
->props
.reqid
!= m
->reqid
)
1200 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1202 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1209 h
= xfrm_src_hash(&m
->old_daddr
, &m
->old_saddr
,
1211 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bysrc
+h
, bysrc
) {
1212 if (x
->props
.mode
!= m
->mode
||
1213 x
->id
.proto
!= m
->proto
)
1215 if (xfrm_addr_cmp(&x
->id
.daddr
, &m
->old_daddr
,
1217 xfrm_addr_cmp(&x
->props
.saddr
, &m
->old_saddr
,
1227 EXPORT_SYMBOL(xfrm_migrate_state_find
);
1229 struct xfrm_state
* xfrm_state_migrate(struct xfrm_state
*x
,
1230 struct xfrm_migrate
*m
)
1232 struct xfrm_state
*xc
;
1235 xc
= xfrm_state_clone(x
, &err
);
1239 memcpy(&xc
->id
.daddr
, &m
->new_daddr
, sizeof(xc
->id
.daddr
));
1240 memcpy(&xc
->props
.saddr
, &m
->new_saddr
, sizeof(xc
->props
.saddr
));
1243 if (!xfrm_addr_cmp(&x
->id
.daddr
, &m
->new_daddr
, m
->new_family
)) {
1244 /* a care is needed when the destination address of the
1245 state is to be updated as it is a part of triplet */
1246 xfrm_state_insert(xc
);
1248 if ((err
= xfrm_state_add(xc
)) < 0)
1257 EXPORT_SYMBOL(xfrm_state_migrate
);
1260 int xfrm_state_update(struct xfrm_state
*x
)
1262 struct xfrm_state
*x1
, *to_put
;
1264 int use_spi
= xfrm_id_proto_match(x
->id
.proto
, IPSEC_PROTO_ANY
);
1268 spin_lock_bh(&xfrm_state_lock
);
1269 x1
= __xfrm_state_locate(x
, use_spi
, x
->props
.family
);
1275 if (xfrm_state_kern(x1
)) {
1281 if (x1
->km
.state
== XFRM_STATE_ACQ
) {
1282 __xfrm_state_insert(x
);
1288 spin_unlock_bh(&xfrm_state_lock
);
1291 xfrm_state_put(to_put
);
1297 xfrm_state_delete(x1
);
1303 spin_lock_bh(&x1
->lock
);
1304 if (likely(x1
->km
.state
== XFRM_STATE_VALID
)) {
1305 if (x
->encap
&& x1
->encap
)
1306 memcpy(x1
->encap
, x
->encap
, sizeof(*x1
->encap
));
1307 if (x
->coaddr
&& x1
->coaddr
) {
1308 memcpy(x1
->coaddr
, x
->coaddr
, sizeof(*x1
->coaddr
));
1310 if (!use_spi
&& memcmp(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
)))
1311 memcpy(&x1
->sel
, &x
->sel
, sizeof(x1
->sel
));
1312 memcpy(&x1
->lft
, &x
->lft
, sizeof(x1
->lft
));
1315 mod_timer(&x1
->timer
, jiffies
+ HZ
);
1316 if (x1
->curlft
.use_time
)
1317 xfrm_state_check_expire(x1
);
1321 spin_unlock_bh(&x1
->lock
);
1327 EXPORT_SYMBOL(xfrm_state_update
);
1329 int xfrm_state_check_expire(struct xfrm_state
*x
)
1331 if (!x
->curlft
.use_time
)
1332 x
->curlft
.use_time
= get_seconds();
1334 if (x
->km
.state
!= XFRM_STATE_VALID
)
1337 if (x
->curlft
.bytes
>= x
->lft
.hard_byte_limit
||
1338 x
->curlft
.packets
>= x
->lft
.hard_packet_limit
) {
1339 x
->km
.state
= XFRM_STATE_EXPIRED
;
1340 mod_timer(&x
->timer
, jiffies
);
1345 (x
->curlft
.bytes
>= x
->lft
.soft_byte_limit
||
1346 x
->curlft
.packets
>= x
->lft
.soft_packet_limit
)) {
1348 km_state_expired(x
, 0, 0);
1352 EXPORT_SYMBOL(xfrm_state_check_expire
);
1355 xfrm_state_lookup(xfrm_address_t
*daddr
, __be32 spi
, u8 proto
,
1356 unsigned short family
)
1358 struct xfrm_state
*x
;
1360 spin_lock_bh(&xfrm_state_lock
);
1361 x
= __xfrm_state_lookup(daddr
, spi
, proto
, family
);
1362 spin_unlock_bh(&xfrm_state_lock
);
1365 EXPORT_SYMBOL(xfrm_state_lookup
);
1368 xfrm_state_lookup_byaddr(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1369 u8 proto
, unsigned short family
)
1371 struct xfrm_state
*x
;
1373 spin_lock_bh(&xfrm_state_lock
);
1374 x
= __xfrm_state_lookup_byaddr(daddr
, saddr
, proto
, family
);
1375 spin_unlock_bh(&xfrm_state_lock
);
1378 EXPORT_SYMBOL(xfrm_state_lookup_byaddr
);
1381 xfrm_find_acq(u8 mode
, u32 reqid
, u8 proto
,
1382 xfrm_address_t
*daddr
, xfrm_address_t
*saddr
,
1383 int create
, unsigned short family
)
1385 struct xfrm_state
*x
;
1387 spin_lock_bh(&xfrm_state_lock
);
1388 x
= __find_acq_core(family
, mode
, reqid
, proto
, daddr
, saddr
, create
);
1389 spin_unlock_bh(&xfrm_state_lock
);
1393 EXPORT_SYMBOL(xfrm_find_acq
);
1395 #ifdef CONFIG_XFRM_SUB_POLICY
1397 xfrm_tmpl_sort(struct xfrm_tmpl
**dst
, struct xfrm_tmpl
**src
, int n
,
1398 unsigned short family
)
1401 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1403 return -EAFNOSUPPORT
;
1405 spin_lock_bh(&xfrm_state_lock
);
1406 if (afinfo
->tmpl_sort
)
1407 err
= afinfo
->tmpl_sort(dst
, src
, n
);
1408 spin_unlock_bh(&xfrm_state_lock
);
1409 xfrm_state_put_afinfo(afinfo
);
1412 EXPORT_SYMBOL(xfrm_tmpl_sort
);
1415 xfrm_state_sort(struct xfrm_state
**dst
, struct xfrm_state
**src
, int n
,
1416 unsigned short family
)
1419 struct xfrm_state_afinfo
*afinfo
= xfrm_state_get_afinfo(family
);
1421 return -EAFNOSUPPORT
;
1423 spin_lock_bh(&xfrm_state_lock
);
1424 if (afinfo
->state_sort
)
1425 err
= afinfo
->state_sort(dst
, src
, n
);
1426 spin_unlock_bh(&xfrm_state_lock
);
1427 xfrm_state_put_afinfo(afinfo
);
1430 EXPORT_SYMBOL(xfrm_state_sort
);
1433 /* Silly enough, but I'm lazy to build resolution list */
1435 static struct xfrm_state
*__xfrm_find_acq_byseq(u32 seq
)
1439 for (i
= 0; i
<= init_net
.xfrm
.state_hmask
; i
++) {
1440 struct hlist_node
*entry
;
1441 struct xfrm_state
*x
;
1443 hlist_for_each_entry(x
, entry
, init_net
.xfrm
.state_bydst
+i
, bydst
) {
1444 if (x
->km
.seq
== seq
&&
1445 x
->km
.state
== XFRM_STATE_ACQ
) {
1454 struct xfrm_state
*xfrm_find_acq_byseq(u32 seq
)
1456 struct xfrm_state
*x
;
1458 spin_lock_bh(&xfrm_state_lock
);
1459 x
= __xfrm_find_acq_byseq(seq
);
1460 spin_unlock_bh(&xfrm_state_lock
);
1463 EXPORT_SYMBOL(xfrm_find_acq_byseq
);
1465 u32
xfrm_get_acqseq(void)
1469 static DEFINE_SPINLOCK(acqseq_lock
);
1471 spin_lock_bh(&acqseq_lock
);
1472 res
= (++acqseq
? : ++acqseq
);
1473 spin_unlock_bh(&acqseq_lock
);
1476 EXPORT_SYMBOL(xfrm_get_acqseq
);
1478 int xfrm_alloc_spi(struct xfrm_state
*x
, u32 low
, u32 high
)
1481 struct xfrm_state
*x0
;
1483 __be32 minspi
= htonl(low
);
1484 __be32 maxspi
= htonl(high
);
1486 spin_lock_bh(&x
->lock
);
1487 if (x
->km
.state
== XFRM_STATE_DEAD
)
1496 if (minspi
== maxspi
) {
1497 x0
= xfrm_state_lookup(&x
->id
.daddr
, minspi
, x
->id
.proto
, x
->props
.family
);
1505 for (h
=0; h
<high
-low
+1; h
++) {
1506 spi
= low
+ net_random()%(high
-low
+1);
1507 x0
= xfrm_state_lookup(&x
->id
.daddr
, htonl(spi
), x
->id
.proto
, x
->props
.family
);
1509 x
->id
.spi
= htonl(spi
);
1516 spin_lock_bh(&xfrm_state_lock
);
1517 h
= xfrm_spi_hash(&x
->id
.daddr
, x
->id
.spi
, x
->id
.proto
, x
->props
.family
);
1518 hlist_add_head(&x
->byspi
, init_net
.xfrm
.state_byspi
+h
);
1519 spin_unlock_bh(&xfrm_state_lock
);
1525 spin_unlock_bh(&x
->lock
);
1529 EXPORT_SYMBOL(xfrm_alloc_spi
);
1531 int xfrm_state_walk(struct xfrm_state_walk
*walk
,
1532 int (*func
)(struct xfrm_state
*, int, void*),
1535 struct xfrm_state
*state
;
1536 struct xfrm_state_walk
*x
;
1539 if (walk
->seq
!= 0 && list_empty(&walk
->all
))
1542 spin_lock_bh(&xfrm_state_lock
);
1543 if (list_empty(&walk
->all
))
1544 x
= list_first_entry(&init_net
.xfrm
.state_all
, struct xfrm_state_walk
, all
);
1546 x
= list_entry(&walk
->all
, struct xfrm_state_walk
, all
);
1547 list_for_each_entry_from(x
, &init_net
.xfrm
.state_all
, all
) {
1548 if (x
->state
== XFRM_STATE_DEAD
)
1550 state
= container_of(x
, struct xfrm_state
, km
);
1551 if (!xfrm_id_proto_match(state
->id
.proto
, walk
->proto
))
1553 err
= func(state
, walk
->seq
, data
);
1555 list_move_tail(&walk
->all
, &x
->all
);
1560 if (walk
->seq
== 0) {
1564 list_del_init(&walk
->all
);
1566 spin_unlock_bh(&xfrm_state_lock
);
1569 EXPORT_SYMBOL(xfrm_state_walk
);
1571 void xfrm_state_walk_init(struct xfrm_state_walk
*walk
, u8 proto
)
1573 INIT_LIST_HEAD(&walk
->all
);
1574 walk
->proto
= proto
;
1575 walk
->state
= XFRM_STATE_DEAD
;
1578 EXPORT_SYMBOL(xfrm_state_walk_init
);
1580 void xfrm_state_walk_done(struct xfrm_state_walk
*walk
)
1582 if (list_empty(&walk
->all
))
1585 spin_lock_bh(&xfrm_state_lock
);
1586 list_del(&walk
->all
);
1587 spin_lock_bh(&xfrm_state_lock
);
1589 EXPORT_SYMBOL(xfrm_state_walk_done
);
1592 void xfrm_replay_notify(struct xfrm_state
*x
, int event
)
1595 /* we send notify messages in case
1596 * 1. we updated on of the sequence numbers, and the seqno difference
1597 * is at least x->replay_maxdiff, in this case we also update the
1598 * timeout of our timer function
1599 * 2. if x->replay_maxage has elapsed since last update,
1600 * and there were changes
1602 * The state structure must be locked!
1606 case XFRM_REPLAY_UPDATE
:
1607 if (x
->replay_maxdiff
&&
1608 (x
->replay
.seq
- x
->preplay
.seq
< x
->replay_maxdiff
) &&
1609 (x
->replay
.oseq
- x
->preplay
.oseq
< x
->replay_maxdiff
)) {
1610 if (x
->xflags
& XFRM_TIME_DEFER
)
1611 event
= XFRM_REPLAY_TIMEOUT
;
1618 case XFRM_REPLAY_TIMEOUT
:
1619 if ((x
->replay
.seq
== x
->preplay
.seq
) &&
1620 (x
->replay
.bitmap
== x
->preplay
.bitmap
) &&
1621 (x
->replay
.oseq
== x
->preplay
.oseq
)) {
1622 x
->xflags
|= XFRM_TIME_DEFER
;
1629 memcpy(&x
->preplay
, &x
->replay
, sizeof(struct xfrm_replay_state
));
1630 c
.event
= XFRM_MSG_NEWAE
;
1631 c
.data
.aevent
= event
;
1632 km_state_notify(x
, &c
);
1634 if (x
->replay_maxage
&&
1635 !mod_timer(&x
->rtimer
, jiffies
+ x
->replay_maxage
))
1636 x
->xflags
&= ~XFRM_TIME_DEFER
;
1639 static void xfrm_replay_timer_handler(unsigned long data
)
1641 struct xfrm_state
*x
= (struct xfrm_state
*)data
;
1643 spin_lock(&x
->lock
);
1645 if (x
->km
.state
== XFRM_STATE_VALID
) {
1646 if (xfrm_aevent_is_on())
1647 xfrm_replay_notify(x
, XFRM_REPLAY_TIMEOUT
);
1649 x
->xflags
|= XFRM_TIME_DEFER
;
1652 spin_unlock(&x
->lock
);
1655 int xfrm_replay_check(struct xfrm_state
*x
,
1656 struct sk_buff
*skb
, __be32 net_seq
)
1659 u32 seq
= ntohl(net_seq
);
1661 if (unlikely(seq
== 0))
1664 if (likely(seq
> x
->replay
.seq
))
1667 diff
= x
->replay
.seq
- seq
;
1668 if (diff
>= min_t(unsigned int, x
->props
.replay_window
,
1669 sizeof(x
->replay
.bitmap
) * 8)) {
1670 x
->stats
.replay_window
++;
1674 if (x
->replay
.bitmap
& (1U << diff
)) {
1681 xfrm_audit_state_replay(x
, skb
, net_seq
);
1685 void xfrm_replay_advance(struct xfrm_state
*x
, __be32 net_seq
)
1688 u32 seq
= ntohl(net_seq
);
1690 if (seq
> x
->replay
.seq
) {
1691 diff
= seq
- x
->replay
.seq
;
1692 if (diff
< x
->props
.replay_window
)
1693 x
->replay
.bitmap
= ((x
->replay
.bitmap
) << diff
) | 1;
1695 x
->replay
.bitmap
= 1;
1696 x
->replay
.seq
= seq
;
1698 diff
= x
->replay
.seq
- seq
;
1699 x
->replay
.bitmap
|= (1U << diff
);
1702 if (xfrm_aevent_is_on())
1703 xfrm_replay_notify(x
, XFRM_REPLAY_UPDATE
);
1706 static LIST_HEAD(xfrm_km_list
);
1707 static DEFINE_RWLOCK(xfrm_km_lock
);
1709 void km_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1711 struct xfrm_mgr
*km
;
1713 read_lock(&xfrm_km_lock
);
1714 list_for_each_entry(km
, &xfrm_km_list
, list
)
1715 if (km
->notify_policy
)
1716 km
->notify_policy(xp
, dir
, c
);
1717 read_unlock(&xfrm_km_lock
);
1720 void km_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1722 struct xfrm_mgr
*km
;
1723 read_lock(&xfrm_km_lock
);
1724 list_for_each_entry(km
, &xfrm_km_list
, list
)
1727 read_unlock(&xfrm_km_lock
);
1730 EXPORT_SYMBOL(km_policy_notify
);
1731 EXPORT_SYMBOL(km_state_notify
);
1733 void km_state_expired(struct xfrm_state
*x
, int hard
, u32 pid
)
1739 c
.event
= XFRM_MSG_EXPIRE
;
1740 km_state_notify(x
, &c
);
1743 wake_up(&init_net
.xfrm
.km_waitq
);
1746 EXPORT_SYMBOL(km_state_expired
);
1748 * We send to all registered managers regardless of failure
1749 * We are happy with one success
1751 int km_query(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*pol
)
1753 int err
= -EINVAL
, acqret
;
1754 struct xfrm_mgr
*km
;
1756 read_lock(&xfrm_km_lock
);
1757 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1758 acqret
= km
->acquire(x
, t
, pol
, XFRM_POLICY_OUT
);
1762 read_unlock(&xfrm_km_lock
);
1765 EXPORT_SYMBOL(km_query
);
1767 int km_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
1770 struct xfrm_mgr
*km
;
1772 read_lock(&xfrm_km_lock
);
1773 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1774 if (km
->new_mapping
)
1775 err
= km
->new_mapping(x
, ipaddr
, sport
);
1779 read_unlock(&xfrm_km_lock
);
1782 EXPORT_SYMBOL(km_new_mapping
);
1784 void km_policy_expired(struct xfrm_policy
*pol
, int dir
, int hard
, u32 pid
)
1790 c
.event
= XFRM_MSG_POLEXPIRE
;
1791 km_policy_notify(pol
, dir
, &c
);
1794 wake_up(&init_net
.xfrm
.km_waitq
);
1796 EXPORT_SYMBOL(km_policy_expired
);
1798 #ifdef CONFIG_XFRM_MIGRATE
1799 int km_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1800 struct xfrm_migrate
*m
, int num_migrate
,
1801 struct xfrm_kmaddress
*k
)
1805 struct xfrm_mgr
*km
;
1807 read_lock(&xfrm_km_lock
);
1808 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1810 ret
= km
->migrate(sel
, dir
, type
, m
, num_migrate
, k
);
1815 read_unlock(&xfrm_km_lock
);
1818 EXPORT_SYMBOL(km_migrate
);
1821 int km_report(u8 proto
, struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
1825 struct xfrm_mgr
*km
;
1827 read_lock(&xfrm_km_lock
);
1828 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1830 ret
= km
->report(proto
, sel
, addr
);
1835 read_unlock(&xfrm_km_lock
);
1838 EXPORT_SYMBOL(km_report
);
1840 int xfrm_user_policy(struct sock
*sk
, int optname
, u8 __user
*optval
, int optlen
)
1844 struct xfrm_mgr
*km
;
1845 struct xfrm_policy
*pol
= NULL
;
1847 if (optlen
<= 0 || optlen
> PAGE_SIZE
)
1850 data
= kmalloc(optlen
, GFP_KERNEL
);
1855 if (copy_from_user(data
, optval
, optlen
))
1859 read_lock(&xfrm_km_lock
);
1860 list_for_each_entry(km
, &xfrm_km_list
, list
) {
1861 pol
= km
->compile_policy(sk
, optname
, data
,
1866 read_unlock(&xfrm_km_lock
);
1869 xfrm_sk_policy_insert(sk
, err
, pol
);
1878 EXPORT_SYMBOL(xfrm_user_policy
);
1880 int xfrm_register_km(struct xfrm_mgr
*km
)
1882 write_lock_bh(&xfrm_km_lock
);
1883 list_add_tail(&km
->list
, &xfrm_km_list
);
1884 write_unlock_bh(&xfrm_km_lock
);
1887 EXPORT_SYMBOL(xfrm_register_km
);
1889 int xfrm_unregister_km(struct xfrm_mgr
*km
)
1891 write_lock_bh(&xfrm_km_lock
);
1892 list_del(&km
->list
);
1893 write_unlock_bh(&xfrm_km_lock
);
1896 EXPORT_SYMBOL(xfrm_unregister_km
);
1898 int xfrm_state_register_afinfo(struct xfrm_state_afinfo
*afinfo
)
1901 if (unlikely(afinfo
== NULL
))
1903 if (unlikely(afinfo
->family
>= NPROTO
))
1904 return -EAFNOSUPPORT
;
1905 write_lock_bh(&xfrm_state_afinfo_lock
);
1906 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != NULL
))
1909 xfrm_state_afinfo
[afinfo
->family
] = afinfo
;
1910 write_unlock_bh(&xfrm_state_afinfo_lock
);
1913 EXPORT_SYMBOL(xfrm_state_register_afinfo
);
1915 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo
*afinfo
)
1918 if (unlikely(afinfo
== NULL
))
1920 if (unlikely(afinfo
->family
>= NPROTO
))
1921 return -EAFNOSUPPORT
;
1922 write_lock_bh(&xfrm_state_afinfo_lock
);
1923 if (likely(xfrm_state_afinfo
[afinfo
->family
] != NULL
)) {
1924 if (unlikely(xfrm_state_afinfo
[afinfo
->family
] != afinfo
))
1927 xfrm_state_afinfo
[afinfo
->family
] = NULL
;
1929 write_unlock_bh(&xfrm_state_afinfo_lock
);
1932 EXPORT_SYMBOL(xfrm_state_unregister_afinfo
);
1934 static struct xfrm_state_afinfo
*xfrm_state_get_afinfo(unsigned int family
)
1936 struct xfrm_state_afinfo
*afinfo
;
1937 if (unlikely(family
>= NPROTO
))
1939 read_lock(&xfrm_state_afinfo_lock
);
1940 afinfo
= xfrm_state_afinfo
[family
];
1941 if (unlikely(!afinfo
))
1942 read_unlock(&xfrm_state_afinfo_lock
);
1946 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo
*afinfo
)
1947 __releases(xfrm_state_afinfo_lock
)
1949 read_unlock(&xfrm_state_afinfo_lock
);
1952 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1953 void xfrm_state_delete_tunnel(struct xfrm_state
*x
)
1956 struct xfrm_state
*t
= x
->tunnel
;
1958 if (atomic_read(&t
->tunnel_users
) == 2)
1959 xfrm_state_delete(t
);
1960 atomic_dec(&t
->tunnel_users
);
1965 EXPORT_SYMBOL(xfrm_state_delete_tunnel
);
1967 int xfrm_state_mtu(struct xfrm_state
*x
, int mtu
)
1971 spin_lock_bh(&x
->lock
);
1972 if (x
->km
.state
== XFRM_STATE_VALID
&&
1973 x
->type
&& x
->type
->get_mtu
)
1974 res
= x
->type
->get_mtu(x
, mtu
);
1976 res
= mtu
- x
->props
.header_len
;
1977 spin_unlock_bh(&x
->lock
);
1981 int xfrm_init_state(struct xfrm_state
*x
)
1983 struct xfrm_state_afinfo
*afinfo
;
1984 struct xfrm_mode
*inner_mode
;
1985 int family
= x
->props
.family
;
1988 err
= -EAFNOSUPPORT
;
1989 afinfo
= xfrm_state_get_afinfo(family
);
1994 if (afinfo
->init_flags
)
1995 err
= afinfo
->init_flags(x
);
1997 xfrm_state_put_afinfo(afinfo
);
2002 err
= -EPROTONOSUPPORT
;
2004 if (x
->sel
.family
!= AF_UNSPEC
) {
2005 inner_mode
= xfrm_get_mode(x
->props
.mode
, x
->sel
.family
);
2006 if (inner_mode
== NULL
)
2009 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2010 family
!= x
->sel
.family
) {
2011 xfrm_put_mode(inner_mode
);
2015 x
->inner_mode
= inner_mode
;
2017 struct xfrm_mode
*inner_mode_iaf
;
2019 inner_mode
= xfrm_get_mode(x
->props
.mode
, AF_INET
);
2020 if (inner_mode
== NULL
)
2023 if (!(inner_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2024 xfrm_put_mode(inner_mode
);
2028 inner_mode_iaf
= xfrm_get_mode(x
->props
.mode
, AF_INET6
);
2029 if (inner_mode_iaf
== NULL
)
2032 if (!(inner_mode_iaf
->flags
& XFRM_MODE_FLAG_TUNNEL
)) {
2033 xfrm_put_mode(inner_mode_iaf
);
2037 if (x
->props
.family
== AF_INET
) {
2038 x
->inner_mode
= inner_mode
;
2039 x
->inner_mode_iaf
= inner_mode_iaf
;
2041 x
->inner_mode
= inner_mode_iaf
;
2042 x
->inner_mode_iaf
= inner_mode
;
2046 x
->type
= xfrm_get_type(x
->id
.proto
, family
);
2047 if (x
->type
== NULL
)
2050 err
= x
->type
->init_state(x
);
2054 x
->outer_mode
= xfrm_get_mode(x
->props
.mode
, family
);
2055 if (x
->outer_mode
== NULL
)
2058 x
->km
.state
= XFRM_STATE_VALID
;
2064 EXPORT_SYMBOL(xfrm_init_state
);
2066 int __net_init
xfrm_state_init(struct net
*net
)
2070 INIT_LIST_HEAD(&net
->xfrm
.state_all
);
2072 sz
= sizeof(struct hlist_head
) * 8;
2074 net
->xfrm
.state_bydst
= xfrm_hash_alloc(sz
);
2075 if (!net
->xfrm
.state_bydst
)
2077 net
->xfrm
.state_bysrc
= xfrm_hash_alloc(sz
);
2078 if (!net
->xfrm
.state_bysrc
)
2080 net
->xfrm
.state_byspi
= xfrm_hash_alloc(sz
);
2081 if (!net
->xfrm
.state_byspi
)
2083 net
->xfrm
.state_hmask
= ((sz
/ sizeof(struct hlist_head
)) - 1);
2085 net
->xfrm
.state_num
= 0;
2086 INIT_WORK(&net
->xfrm
.state_hash_work
, xfrm_hash_resize
);
2087 INIT_HLIST_HEAD(&net
->xfrm
.state_gc_list
);
2088 INIT_WORK(&net
->xfrm
.state_gc_work
, xfrm_state_gc_task
);
2089 init_waitqueue_head(&net
->xfrm
.km_waitq
);
2093 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2095 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2100 void xfrm_state_fini(struct net
*net
)
2104 WARN_ON(!list_empty(&net
->xfrm
.state_all
));
2106 sz
= (net
->xfrm
.state_hmask
+ 1) * sizeof(struct hlist_head
);
2107 WARN_ON(!hlist_empty(net
->xfrm
.state_byspi
));
2108 xfrm_hash_free(net
->xfrm
.state_byspi
, sz
);
2109 WARN_ON(!hlist_empty(net
->xfrm
.state_bysrc
));
2110 xfrm_hash_free(net
->xfrm
.state_bysrc
, sz
);
2111 WARN_ON(!hlist_empty(net
->xfrm
.state_bydst
));
2112 xfrm_hash_free(net
->xfrm
.state_bydst
, sz
);
2115 #ifdef CONFIG_AUDITSYSCALL
2116 static void xfrm_audit_helper_sainfo(struct xfrm_state
*x
,
2117 struct audit_buffer
*audit_buf
)
2119 struct xfrm_sec_ctx
*ctx
= x
->security
;
2120 u32 spi
= ntohl(x
->id
.spi
);
2123 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2124 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2126 switch(x
->props
.family
) {
2128 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2129 &x
->props
.saddr
.a4
, &x
->id
.daddr
.a4
);
2132 audit_log_format(audit_buf
, " src=%pI6 dst=%pI6",
2133 x
->props
.saddr
.a6
, x
->id
.daddr
.a6
);
2137 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2140 static void xfrm_audit_helper_pktinfo(struct sk_buff
*skb
, u16 family
,
2141 struct audit_buffer
*audit_buf
)
2144 struct ipv6hdr
*iph6
;
2149 audit_log_format(audit_buf
, " src=%pI4 dst=%pI4",
2150 &iph4
->saddr
, &iph4
->daddr
);
2153 iph6
= ipv6_hdr(skb
);
2154 audit_log_format(audit_buf
,
2155 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2156 &iph6
->saddr
,&iph6
->daddr
,
2157 iph6
->flow_lbl
[0] & 0x0f,
2164 void xfrm_audit_state_add(struct xfrm_state
*x
, int result
,
2165 uid_t auid
, u32 sessionid
, u32 secid
)
2167 struct audit_buffer
*audit_buf
;
2169 audit_buf
= xfrm_audit_start("SAD-add");
2170 if (audit_buf
== NULL
)
2172 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2173 xfrm_audit_helper_sainfo(x
, audit_buf
);
2174 audit_log_format(audit_buf
, " res=%u", result
);
2175 audit_log_end(audit_buf
);
2177 EXPORT_SYMBOL_GPL(xfrm_audit_state_add
);
2179 void xfrm_audit_state_delete(struct xfrm_state
*x
, int result
,
2180 uid_t auid
, u32 sessionid
, u32 secid
)
2182 struct audit_buffer
*audit_buf
;
2184 audit_buf
= xfrm_audit_start("SAD-delete");
2185 if (audit_buf
== NULL
)
2187 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2188 xfrm_audit_helper_sainfo(x
, audit_buf
);
2189 audit_log_format(audit_buf
, " res=%u", result
);
2190 audit_log_end(audit_buf
);
2192 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete
);
2194 void xfrm_audit_state_replay_overflow(struct xfrm_state
*x
,
2195 struct sk_buff
*skb
)
2197 struct audit_buffer
*audit_buf
;
2200 audit_buf
= xfrm_audit_start("SA-replay-overflow");
2201 if (audit_buf
== NULL
)
2203 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2204 /* don't record the sequence number because it's inherent in this kind
2205 * of audit message */
2206 spi
= ntohl(x
->id
.spi
);
2207 audit_log_format(audit_buf
, " spi=%u(0x%x)", spi
, spi
);
2208 audit_log_end(audit_buf
);
2210 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow
);
2212 static void xfrm_audit_state_replay(struct xfrm_state
*x
,
2213 struct sk_buff
*skb
, __be32 net_seq
)
2215 struct audit_buffer
*audit_buf
;
2218 audit_buf
= xfrm_audit_start("SA-replayed-pkt");
2219 if (audit_buf
== NULL
)
2221 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2222 spi
= ntohl(x
->id
.spi
);
2223 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2224 spi
, spi
, ntohl(net_seq
));
2225 audit_log_end(audit_buf
);
2228 void xfrm_audit_state_notfound_simple(struct sk_buff
*skb
, u16 family
)
2230 struct audit_buffer
*audit_buf
;
2232 audit_buf
= xfrm_audit_start("SA-notfound");
2233 if (audit_buf
== NULL
)
2235 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2236 audit_log_end(audit_buf
);
2238 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple
);
2240 void xfrm_audit_state_notfound(struct sk_buff
*skb
, u16 family
,
2241 __be32 net_spi
, __be32 net_seq
)
2243 struct audit_buffer
*audit_buf
;
2246 audit_buf
= xfrm_audit_start("SA-notfound");
2247 if (audit_buf
== NULL
)
2249 xfrm_audit_helper_pktinfo(skb
, family
, audit_buf
);
2250 spi
= ntohl(net_spi
);
2251 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2252 spi
, spi
, ntohl(net_seq
));
2253 audit_log_end(audit_buf
);
2255 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound
);
2257 void xfrm_audit_state_icvfail(struct xfrm_state
*x
,
2258 struct sk_buff
*skb
, u8 proto
)
2260 struct audit_buffer
*audit_buf
;
2264 audit_buf
= xfrm_audit_start("SA-icv-failure");
2265 if (audit_buf
== NULL
)
2267 xfrm_audit_helper_pktinfo(skb
, x
->props
.family
, audit_buf
);
2268 if (xfrm_parse_spi(skb
, proto
, &net_spi
, &net_seq
) == 0) {
2269 u32 spi
= ntohl(net_spi
);
2270 audit_log_format(audit_buf
, " spi=%u(0x%x) seqno=%u",
2271 spi
, spi
, ntohl(net_seq
));
2273 audit_log_end(audit_buf
);
2275 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail
);
2276 #endif /* CONFIG_AUDITSYSCALL */