Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux/fpc-iii.git] / net / xfrm / xfrm_state.c
blob065d89606888ec1bf053577d3949746bcea6f099
1 /*
2 * xfrm_state.c
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
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>
17 #include <net/xfrm.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <linux/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
29 #include "xfrm_hash.h"
31 #define xfrm_state_deref_prot(table, net) \
32 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
34 static void xfrm_state_gc_task(struct work_struct *work);
36 /* Each xfrm_state may be linked to two tables:
38 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
39 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
40 destination/tunnel endpoint. (output)
43 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
44 static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
46 static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
47 static HLIST_HEAD(xfrm_state_gc_list);
49 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
51 return refcount_inc_not_zero(&x->refcnt);
54 static inline unsigned int xfrm_dst_hash(struct net *net,
55 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
57 u32 reqid,
58 unsigned short family)
60 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
63 static inline unsigned int xfrm_src_hash(struct net *net,
64 const xfrm_address_t *daddr,
65 const xfrm_address_t *saddr,
66 unsigned short family)
68 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
71 static inline unsigned int
72 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
73 __be32 spi, u8 proto, unsigned short family)
75 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
78 static void xfrm_hash_transfer(struct hlist_head *list,
79 struct hlist_head *ndsttable,
80 struct hlist_head *nsrctable,
81 struct hlist_head *nspitable,
82 unsigned int nhashmask)
84 struct hlist_node *tmp;
85 struct xfrm_state *x;
87 hlist_for_each_entry_safe(x, tmp, list, bydst) {
88 unsigned int h;
90 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
91 x->props.reqid, x->props.family,
92 nhashmask);
93 hlist_add_head_rcu(&x->bydst, ndsttable + h);
95 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
96 x->props.family,
97 nhashmask);
98 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
100 if (x->id.spi) {
101 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
102 x->id.proto, x->props.family,
103 nhashmask);
104 hlist_add_head_rcu(&x->byspi, nspitable + h);
109 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
111 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
114 static void xfrm_hash_resize(struct work_struct *work)
116 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
117 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
118 unsigned long nsize, osize;
119 unsigned int nhashmask, ohashmask;
120 int i;
122 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
123 ndst = xfrm_hash_alloc(nsize);
124 if (!ndst)
125 return;
126 nsrc = xfrm_hash_alloc(nsize);
127 if (!nsrc) {
128 xfrm_hash_free(ndst, nsize);
129 return;
131 nspi = xfrm_hash_alloc(nsize);
132 if (!nspi) {
133 xfrm_hash_free(ndst, nsize);
134 xfrm_hash_free(nsrc, nsize);
135 return;
138 spin_lock_bh(&net->xfrm.xfrm_state_lock);
139 write_seqcount_begin(&xfrm_state_hash_generation);
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
142 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
143 for (i = net->xfrm.state_hmask; i >= 0; i--)
144 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
146 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
147 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
148 ohashmask = net->xfrm.state_hmask;
150 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
151 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
152 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
153 net->xfrm.state_hmask = nhashmask;
155 write_seqcount_end(&xfrm_state_hash_generation);
156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
160 synchronize_rcu();
162 xfrm_hash_free(odst, osize);
163 xfrm_hash_free(osrc, osize);
164 xfrm_hash_free(ospi, osize);
167 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
168 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
170 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
172 int __xfrm_state_delete(struct xfrm_state *x);
174 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
175 bool km_is_alive(const struct km_event *c);
176 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
178 static DEFINE_SPINLOCK(xfrm_type_lock);
179 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
181 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
182 const struct xfrm_type **typemap;
183 int err = 0;
185 if (unlikely(afinfo == NULL))
186 return -EAFNOSUPPORT;
187 typemap = afinfo->type_map;
188 spin_lock_bh(&xfrm_type_lock);
190 if (likely(typemap[type->proto] == NULL))
191 typemap[type->proto] = type;
192 else
193 err = -EEXIST;
194 spin_unlock_bh(&xfrm_type_lock);
195 rcu_read_unlock();
196 return err;
198 EXPORT_SYMBOL(xfrm_register_type);
200 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
202 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
203 const struct xfrm_type **typemap;
204 int err = 0;
206 if (unlikely(afinfo == NULL))
207 return -EAFNOSUPPORT;
208 typemap = afinfo->type_map;
209 spin_lock_bh(&xfrm_type_lock);
211 if (unlikely(typemap[type->proto] != type))
212 err = -ENOENT;
213 else
214 typemap[type->proto] = NULL;
215 spin_unlock_bh(&xfrm_type_lock);
216 rcu_read_unlock();
217 return err;
219 EXPORT_SYMBOL(xfrm_unregister_type);
221 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
223 struct xfrm_state_afinfo *afinfo;
224 const struct xfrm_type **typemap;
225 const struct xfrm_type *type;
226 int modload_attempted = 0;
228 retry:
229 afinfo = xfrm_state_get_afinfo(family);
230 if (unlikely(afinfo == NULL))
231 return NULL;
232 typemap = afinfo->type_map;
234 type = READ_ONCE(typemap[proto]);
235 if (unlikely(type && !try_module_get(type->owner)))
236 type = NULL;
238 rcu_read_unlock();
240 if (!type && !modload_attempted) {
241 request_module("xfrm-type-%d-%d", family, proto);
242 modload_attempted = 1;
243 goto retry;
246 return type;
249 static void xfrm_put_type(const struct xfrm_type *type)
251 module_put(type->owner);
254 static DEFINE_SPINLOCK(xfrm_type_offload_lock);
255 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
256 unsigned short family)
258 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
259 const struct xfrm_type_offload **typemap;
260 int err = 0;
262 if (unlikely(afinfo == NULL))
263 return -EAFNOSUPPORT;
264 typemap = afinfo->type_offload_map;
265 spin_lock_bh(&xfrm_type_offload_lock);
267 if (likely(typemap[type->proto] == NULL))
268 typemap[type->proto] = type;
269 else
270 err = -EEXIST;
271 spin_unlock_bh(&xfrm_type_offload_lock);
272 rcu_read_unlock();
273 return err;
275 EXPORT_SYMBOL(xfrm_register_type_offload);
277 int xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
278 unsigned short family)
280 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
281 const struct xfrm_type_offload **typemap;
282 int err = 0;
284 if (unlikely(afinfo == NULL))
285 return -EAFNOSUPPORT;
286 typemap = afinfo->type_offload_map;
287 spin_lock_bh(&xfrm_type_offload_lock);
289 if (unlikely(typemap[type->proto] != type))
290 err = -ENOENT;
291 else
292 typemap[type->proto] = NULL;
293 spin_unlock_bh(&xfrm_type_offload_lock);
294 rcu_read_unlock();
295 return err;
297 EXPORT_SYMBOL(xfrm_unregister_type_offload);
299 static const struct xfrm_type_offload *
300 xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
302 struct xfrm_state_afinfo *afinfo;
303 const struct xfrm_type_offload **typemap;
304 const struct xfrm_type_offload *type;
306 retry:
307 afinfo = xfrm_state_get_afinfo(family);
308 if (unlikely(afinfo == NULL))
309 return NULL;
310 typemap = afinfo->type_offload_map;
312 type = typemap[proto];
313 if ((type && !try_module_get(type->owner)))
314 type = NULL;
316 if (!type && try_load) {
317 request_module("xfrm-offload-%d-%d", family, proto);
318 try_load = 0;
319 goto retry;
322 rcu_read_unlock();
323 return type;
326 static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
328 module_put(type->owner);
331 static DEFINE_SPINLOCK(xfrm_mode_lock);
332 int xfrm_register_mode(struct xfrm_mode *mode, int family)
334 struct xfrm_state_afinfo *afinfo;
335 struct xfrm_mode **modemap;
336 int err;
338 if (unlikely(mode->encap >= XFRM_MODE_MAX))
339 return -EINVAL;
341 afinfo = xfrm_state_get_afinfo(family);
342 if (unlikely(afinfo == NULL))
343 return -EAFNOSUPPORT;
345 err = -EEXIST;
346 modemap = afinfo->mode_map;
347 spin_lock_bh(&xfrm_mode_lock);
348 if (modemap[mode->encap])
349 goto out;
351 err = -ENOENT;
352 if (!try_module_get(afinfo->owner))
353 goto out;
355 mode->afinfo = afinfo;
356 modemap[mode->encap] = mode;
357 err = 0;
359 out:
360 spin_unlock_bh(&xfrm_mode_lock);
361 rcu_read_unlock();
362 return err;
364 EXPORT_SYMBOL(xfrm_register_mode);
366 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
368 struct xfrm_state_afinfo *afinfo;
369 struct xfrm_mode **modemap;
370 int err;
372 if (unlikely(mode->encap >= XFRM_MODE_MAX))
373 return -EINVAL;
375 afinfo = xfrm_state_get_afinfo(family);
376 if (unlikely(afinfo == NULL))
377 return -EAFNOSUPPORT;
379 err = -ENOENT;
380 modemap = afinfo->mode_map;
381 spin_lock_bh(&xfrm_mode_lock);
382 if (likely(modemap[mode->encap] == mode)) {
383 modemap[mode->encap] = NULL;
384 module_put(mode->afinfo->owner);
385 err = 0;
388 spin_unlock_bh(&xfrm_mode_lock);
389 rcu_read_unlock();
390 return err;
392 EXPORT_SYMBOL(xfrm_unregister_mode);
394 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
396 struct xfrm_state_afinfo *afinfo;
397 struct xfrm_mode *mode;
398 int modload_attempted = 0;
400 if (unlikely(encap >= XFRM_MODE_MAX))
401 return NULL;
403 retry:
404 afinfo = xfrm_state_get_afinfo(family);
405 if (unlikely(afinfo == NULL))
406 return NULL;
408 mode = READ_ONCE(afinfo->mode_map[encap]);
409 if (unlikely(mode && !try_module_get(mode->owner)))
410 mode = NULL;
412 rcu_read_unlock();
413 if (!mode && !modload_attempted) {
414 request_module("xfrm-mode-%d-%d", family, encap);
415 modload_attempted = 1;
416 goto retry;
419 return mode;
422 static void xfrm_put_mode(struct xfrm_mode *mode)
424 module_put(mode->owner);
427 static void xfrm_state_gc_destroy(struct xfrm_state *x)
429 tasklet_hrtimer_cancel(&x->mtimer);
430 del_timer_sync(&x->rtimer);
431 kfree(x->aead);
432 kfree(x->aalg);
433 kfree(x->ealg);
434 kfree(x->calg);
435 kfree(x->encap);
436 kfree(x->coaddr);
437 kfree(x->replay_esn);
438 kfree(x->preplay_esn);
439 if (x->inner_mode)
440 xfrm_put_mode(x->inner_mode);
441 if (x->inner_mode_iaf)
442 xfrm_put_mode(x->inner_mode_iaf);
443 if (x->outer_mode)
444 xfrm_put_mode(x->outer_mode);
445 if (x->type_offload)
446 xfrm_put_type_offload(x->type_offload);
447 if (x->type) {
448 x->type->destructor(x);
449 xfrm_put_type(x->type);
451 xfrm_dev_state_free(x);
452 security_xfrm_state_free(x);
453 kfree(x);
456 static void xfrm_state_gc_task(struct work_struct *work)
458 struct xfrm_state *x;
459 struct hlist_node *tmp;
460 struct hlist_head gc_list;
462 spin_lock_bh(&xfrm_state_gc_lock);
463 hlist_move_list(&xfrm_state_gc_list, &gc_list);
464 spin_unlock_bh(&xfrm_state_gc_lock);
466 synchronize_rcu();
468 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
469 xfrm_state_gc_destroy(x);
472 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
474 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
475 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
476 unsigned long now = get_seconds();
477 long next = LONG_MAX;
478 int warn = 0;
479 int err = 0;
481 spin_lock(&x->lock);
482 if (x->km.state == XFRM_STATE_DEAD)
483 goto out;
484 if (x->km.state == XFRM_STATE_EXPIRED)
485 goto expired;
486 if (x->lft.hard_add_expires_seconds) {
487 long tmo = x->lft.hard_add_expires_seconds +
488 x->curlft.add_time - now;
489 if (tmo <= 0) {
490 if (x->xflags & XFRM_SOFT_EXPIRE) {
491 /* enter hard expire without soft expire first?!
492 * setting a new date could trigger this.
493 * workaround: fix x->curflt.add_time by below:
495 x->curlft.add_time = now - x->saved_tmo - 1;
496 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
497 } else
498 goto expired;
500 if (tmo < next)
501 next = tmo;
503 if (x->lft.hard_use_expires_seconds) {
504 long tmo = x->lft.hard_use_expires_seconds +
505 (x->curlft.use_time ? : now) - now;
506 if (tmo <= 0)
507 goto expired;
508 if (tmo < next)
509 next = tmo;
511 if (x->km.dying)
512 goto resched;
513 if (x->lft.soft_add_expires_seconds) {
514 long tmo = x->lft.soft_add_expires_seconds +
515 x->curlft.add_time - now;
516 if (tmo <= 0) {
517 warn = 1;
518 x->xflags &= ~XFRM_SOFT_EXPIRE;
519 } else if (tmo < next) {
520 next = tmo;
521 x->xflags |= XFRM_SOFT_EXPIRE;
522 x->saved_tmo = tmo;
525 if (x->lft.soft_use_expires_seconds) {
526 long tmo = x->lft.soft_use_expires_seconds +
527 (x->curlft.use_time ? : now) - now;
528 if (tmo <= 0)
529 warn = 1;
530 else if (tmo < next)
531 next = tmo;
534 x->km.dying = warn;
535 if (warn)
536 km_state_expired(x, 0, 0);
537 resched:
538 if (next != LONG_MAX) {
539 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
542 goto out;
544 expired:
545 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
546 x->km.state = XFRM_STATE_EXPIRED;
548 err = __xfrm_state_delete(x);
549 if (!err)
550 km_state_expired(x, 1, 0);
552 xfrm_audit_state_delete(x, err ? 0 : 1, true);
554 out:
555 spin_unlock(&x->lock);
556 return HRTIMER_NORESTART;
559 static void xfrm_replay_timer_handler(struct timer_list *t);
561 struct xfrm_state *xfrm_state_alloc(struct net *net)
563 struct xfrm_state *x;
565 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
567 if (x) {
568 write_pnet(&x->xs_net, net);
569 refcount_set(&x->refcnt, 1);
570 atomic_set(&x->tunnel_users, 0);
571 INIT_LIST_HEAD(&x->km.all);
572 INIT_HLIST_NODE(&x->bydst);
573 INIT_HLIST_NODE(&x->bysrc);
574 INIT_HLIST_NODE(&x->byspi);
575 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
576 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
577 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
578 x->curlft.add_time = get_seconds();
579 x->lft.soft_byte_limit = XFRM_INF;
580 x->lft.soft_packet_limit = XFRM_INF;
581 x->lft.hard_byte_limit = XFRM_INF;
582 x->lft.hard_packet_limit = XFRM_INF;
583 x->replay_maxage = 0;
584 x->replay_maxdiff = 0;
585 x->inner_mode = NULL;
586 x->inner_mode_iaf = NULL;
587 spin_lock_init(&x->lock);
589 return x;
591 EXPORT_SYMBOL(xfrm_state_alloc);
593 void __xfrm_state_destroy(struct xfrm_state *x)
595 WARN_ON(x->km.state != XFRM_STATE_DEAD);
597 spin_lock_bh(&xfrm_state_gc_lock);
598 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
599 spin_unlock_bh(&xfrm_state_gc_lock);
600 schedule_work(&xfrm_state_gc_work);
602 EXPORT_SYMBOL(__xfrm_state_destroy);
604 int __xfrm_state_delete(struct xfrm_state *x)
606 struct net *net = xs_net(x);
607 int err = -ESRCH;
609 if (x->km.state != XFRM_STATE_DEAD) {
610 x->km.state = XFRM_STATE_DEAD;
611 spin_lock(&net->xfrm.xfrm_state_lock);
612 list_del(&x->km.all);
613 hlist_del_rcu(&x->bydst);
614 hlist_del_rcu(&x->bysrc);
615 if (x->id.spi)
616 hlist_del_rcu(&x->byspi);
617 net->xfrm.state_num--;
618 spin_unlock(&net->xfrm.xfrm_state_lock);
620 xfrm_dev_state_delete(x);
622 /* All xfrm_state objects are created by xfrm_state_alloc.
623 * The xfrm_state_alloc call gives a reference, and that
624 * is what we are dropping here.
626 xfrm_state_put(x);
627 err = 0;
630 return err;
632 EXPORT_SYMBOL(__xfrm_state_delete);
634 int xfrm_state_delete(struct xfrm_state *x)
636 int err;
638 spin_lock_bh(&x->lock);
639 err = __xfrm_state_delete(x);
640 spin_unlock_bh(&x->lock);
642 return err;
644 EXPORT_SYMBOL(xfrm_state_delete);
646 #ifdef CONFIG_SECURITY_NETWORK_XFRM
647 static inline int
648 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
650 int i, err = 0;
652 for (i = 0; i <= net->xfrm.state_hmask; i++) {
653 struct xfrm_state *x;
655 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
656 if (xfrm_id_proto_match(x->id.proto, proto) &&
657 (err = security_xfrm_state_delete(x)) != 0) {
658 xfrm_audit_state_delete(x, 0, task_valid);
659 return err;
664 return err;
667 static inline int
668 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
670 int i, err = 0;
672 for (i = 0; i <= net->xfrm.state_hmask; i++) {
673 struct xfrm_state *x;
674 struct xfrm_state_offload *xso;
676 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
677 xso = &x->xso;
679 if (xso->dev == dev &&
680 (err = security_xfrm_state_delete(x)) != 0) {
681 xfrm_audit_state_delete(x, 0, task_valid);
682 return err;
687 return err;
689 #else
690 static inline int
691 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
693 return 0;
696 static inline int
697 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
699 return 0;
701 #endif
703 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
705 int i, err = 0, cnt = 0;
707 spin_lock_bh(&net->xfrm.xfrm_state_lock);
708 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
709 if (err)
710 goto out;
712 err = -ESRCH;
713 for (i = 0; i <= net->xfrm.state_hmask; i++) {
714 struct xfrm_state *x;
715 restart:
716 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
717 if (!xfrm_state_kern(x) &&
718 xfrm_id_proto_match(x->id.proto, proto)) {
719 xfrm_state_hold(x);
720 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
722 err = xfrm_state_delete(x);
723 xfrm_audit_state_delete(x, err ? 0 : 1,
724 task_valid);
725 xfrm_state_put(x);
726 if (!err)
727 cnt++;
729 spin_lock_bh(&net->xfrm.xfrm_state_lock);
730 goto restart;
734 out:
735 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
736 if (cnt) {
737 err = 0;
738 xfrm_policy_cache_flush();
740 return err;
742 EXPORT_SYMBOL(xfrm_state_flush);
744 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
746 int i, err = 0, cnt = 0;
748 spin_lock_bh(&net->xfrm.xfrm_state_lock);
749 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
750 if (err)
751 goto out;
753 err = -ESRCH;
754 for (i = 0; i <= net->xfrm.state_hmask; i++) {
755 struct xfrm_state *x;
756 struct xfrm_state_offload *xso;
757 restart:
758 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
759 xso = &x->xso;
761 if (!xfrm_state_kern(x) && xso->dev == dev) {
762 xfrm_state_hold(x);
763 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
765 err = xfrm_state_delete(x);
766 xfrm_audit_state_delete(x, err ? 0 : 1,
767 task_valid);
768 xfrm_state_put(x);
769 if (!err)
770 cnt++;
772 spin_lock_bh(&net->xfrm.xfrm_state_lock);
773 goto restart;
777 if (cnt)
778 err = 0;
780 out:
781 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
782 return err;
784 EXPORT_SYMBOL(xfrm_dev_state_flush);
786 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
788 spin_lock_bh(&net->xfrm.xfrm_state_lock);
789 si->sadcnt = net->xfrm.state_num;
790 si->sadhcnt = net->xfrm.state_hmask;
791 si->sadhmcnt = xfrm_state_hashmax;
792 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
794 EXPORT_SYMBOL(xfrm_sad_getinfo);
796 static void
797 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
798 const struct xfrm_tmpl *tmpl,
799 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
800 unsigned short family)
802 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
804 if (!afinfo)
805 return;
807 afinfo->init_tempsel(&x->sel, fl);
809 if (family != tmpl->encap_family) {
810 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
811 if (!afinfo)
812 return;
814 afinfo->init_temprop(x, tmpl, daddr, saddr);
817 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
818 const xfrm_address_t *daddr,
819 __be32 spi, u8 proto,
820 unsigned short family)
822 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
823 struct xfrm_state *x;
825 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
826 if (x->props.family != family ||
827 x->id.spi != spi ||
828 x->id.proto != proto ||
829 !xfrm_addr_equal(&x->id.daddr, daddr, family))
830 continue;
832 if ((mark & x->mark.m) != x->mark.v)
833 continue;
834 if (!xfrm_state_hold_rcu(x))
835 continue;
836 return x;
839 return NULL;
842 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
843 const xfrm_address_t *daddr,
844 const xfrm_address_t *saddr,
845 u8 proto, unsigned short family)
847 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
848 struct xfrm_state *x;
850 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
851 if (x->props.family != family ||
852 x->id.proto != proto ||
853 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
854 !xfrm_addr_equal(&x->props.saddr, saddr, family))
855 continue;
857 if ((mark & x->mark.m) != x->mark.v)
858 continue;
859 if (!xfrm_state_hold_rcu(x))
860 continue;
861 return x;
864 return NULL;
867 static inline struct xfrm_state *
868 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
870 struct net *net = xs_net(x);
871 u32 mark = x->mark.v & x->mark.m;
873 if (use_spi)
874 return __xfrm_state_lookup(net, mark, &x->id.daddr,
875 x->id.spi, x->id.proto, family);
876 else
877 return __xfrm_state_lookup_byaddr(net, mark,
878 &x->id.daddr,
879 &x->props.saddr,
880 x->id.proto, family);
883 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
885 if (have_hash_collision &&
886 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
887 net->xfrm.state_num > net->xfrm.state_hmask)
888 schedule_work(&net->xfrm.state_hash_work);
891 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
892 const struct flowi *fl, unsigned short family,
893 struct xfrm_state **best, int *acq_in_progress,
894 int *error)
896 /* Resolution logic:
897 * 1. There is a valid state with matching selector. Done.
898 * 2. Valid state with inappropriate selector. Skip.
900 * Entering area of "sysdeps".
902 * 3. If state is not valid, selector is temporary, it selects
903 * only session which triggered previous resolution. Key
904 * manager will do something to install a state with proper
905 * selector.
907 if (x->km.state == XFRM_STATE_VALID) {
908 if ((x->sel.family &&
909 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
910 !security_xfrm_state_pol_flow_match(x, pol, fl))
911 return;
913 if (!*best ||
914 (*best)->km.dying > x->km.dying ||
915 ((*best)->km.dying == x->km.dying &&
916 (*best)->curlft.add_time < x->curlft.add_time))
917 *best = x;
918 } else if (x->km.state == XFRM_STATE_ACQ) {
919 *acq_in_progress = 1;
920 } else if (x->km.state == XFRM_STATE_ERROR ||
921 x->km.state == XFRM_STATE_EXPIRED) {
922 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
923 security_xfrm_state_pol_flow_match(x, pol, fl))
924 *error = -ESRCH;
928 struct xfrm_state *
929 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
930 const struct flowi *fl, struct xfrm_tmpl *tmpl,
931 struct xfrm_policy *pol, int *err,
932 unsigned short family)
934 static xfrm_address_t saddr_wildcard = { };
935 struct net *net = xp_net(pol);
936 unsigned int h, h_wildcard;
937 struct xfrm_state *x, *x0, *to_put;
938 int acquire_in_progress = 0;
939 int error = 0;
940 struct xfrm_state *best = NULL;
941 u32 mark = pol->mark.v & pol->mark.m;
942 unsigned short encap_family = tmpl->encap_family;
943 unsigned int sequence;
944 struct km_event c;
946 to_put = NULL;
948 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
950 rcu_read_lock();
951 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
952 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
953 if (x->props.family == encap_family &&
954 x->props.reqid == tmpl->reqid &&
955 (mark & x->mark.m) == x->mark.v &&
956 !(x->props.flags & XFRM_STATE_WILDRECV) &&
957 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
958 tmpl->mode == x->props.mode &&
959 tmpl->id.proto == x->id.proto &&
960 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
961 xfrm_state_look_at(pol, x, fl, encap_family,
962 &best, &acquire_in_progress, &error);
964 if (best || acquire_in_progress)
965 goto found;
967 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
968 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
969 if (x->props.family == encap_family &&
970 x->props.reqid == tmpl->reqid &&
971 (mark & x->mark.m) == x->mark.v &&
972 !(x->props.flags & XFRM_STATE_WILDRECV) &&
973 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
974 tmpl->mode == x->props.mode &&
975 tmpl->id.proto == x->id.proto &&
976 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
977 xfrm_state_look_at(pol, x, fl, encap_family,
978 &best, &acquire_in_progress, &error);
981 found:
982 x = best;
983 if (!x && !error && !acquire_in_progress) {
984 if (tmpl->id.spi &&
985 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
986 tmpl->id.proto, encap_family)) != NULL) {
987 to_put = x0;
988 error = -EEXIST;
989 goto out;
992 c.net = net;
993 /* If the KMs have no listeners (yet...), avoid allocating an SA
994 * for each and every packet - garbage collection might not
995 * handle the flood.
997 if (!km_is_alive(&c)) {
998 error = -ESRCH;
999 goto out;
1002 x = xfrm_state_alloc(net);
1003 if (x == NULL) {
1004 error = -ENOMEM;
1005 goto out;
1007 /* Initialize temporary state matching only
1008 * to current session. */
1009 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1010 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1012 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1013 if (error) {
1014 x->km.state = XFRM_STATE_DEAD;
1015 to_put = x;
1016 x = NULL;
1017 goto out;
1020 if (km_query(x, tmpl, pol) == 0) {
1021 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1022 x->km.state = XFRM_STATE_ACQ;
1023 list_add(&x->km.all, &net->xfrm.state_all);
1024 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1025 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1026 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1027 if (x->id.spi) {
1028 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1029 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1031 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1032 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1033 net->xfrm.state_num++;
1034 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1035 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1036 } else {
1037 x->km.state = XFRM_STATE_DEAD;
1038 to_put = x;
1039 x = NULL;
1040 error = -ESRCH;
1043 out:
1044 if (x) {
1045 if (!xfrm_state_hold_rcu(x)) {
1046 *err = -EAGAIN;
1047 x = NULL;
1049 } else {
1050 *err = acquire_in_progress ? -EAGAIN : error;
1052 rcu_read_unlock();
1053 if (to_put)
1054 xfrm_state_put(to_put);
1056 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1057 *err = -EAGAIN;
1058 if (x) {
1059 xfrm_state_put(x);
1060 x = NULL;
1064 return x;
1067 struct xfrm_state *
1068 xfrm_stateonly_find(struct net *net, u32 mark,
1069 xfrm_address_t *daddr, xfrm_address_t *saddr,
1070 unsigned short family, u8 mode, u8 proto, u32 reqid)
1072 unsigned int h;
1073 struct xfrm_state *rx = NULL, *x = NULL;
1075 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1076 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1077 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1078 if (x->props.family == family &&
1079 x->props.reqid == reqid &&
1080 (mark & x->mark.m) == x->mark.v &&
1081 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1082 xfrm_state_addr_check(x, daddr, saddr, family) &&
1083 mode == x->props.mode &&
1084 proto == x->id.proto &&
1085 x->km.state == XFRM_STATE_VALID) {
1086 rx = x;
1087 break;
1091 if (rx)
1092 xfrm_state_hold(rx);
1093 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1096 return rx;
1098 EXPORT_SYMBOL(xfrm_stateonly_find);
1100 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1101 unsigned short family)
1103 struct xfrm_state *x;
1104 struct xfrm_state_walk *w;
1106 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1107 list_for_each_entry(w, &net->xfrm.state_all, all) {
1108 x = container_of(w, struct xfrm_state, km);
1109 if (x->props.family != family ||
1110 x->id.spi != spi)
1111 continue;
1113 xfrm_state_hold(x);
1114 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1115 return x;
1117 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1118 return NULL;
1120 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1122 static void __xfrm_state_insert(struct xfrm_state *x)
1124 struct net *net = xs_net(x);
1125 unsigned int h;
1127 list_add(&x->km.all, &net->xfrm.state_all);
1129 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1130 x->props.reqid, x->props.family);
1131 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1133 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1134 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1136 if (x->id.spi) {
1137 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1138 x->props.family);
1140 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1143 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1144 if (x->replay_maxage)
1145 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1147 net->xfrm.state_num++;
1149 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1152 /* net->xfrm.xfrm_state_lock is held */
1153 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1155 struct net *net = xs_net(xnew);
1156 unsigned short family = xnew->props.family;
1157 u32 reqid = xnew->props.reqid;
1158 struct xfrm_state *x;
1159 unsigned int h;
1160 u32 mark = xnew->mark.v & xnew->mark.m;
1162 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1163 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1164 if (x->props.family == family &&
1165 x->props.reqid == reqid &&
1166 (mark & x->mark.m) == x->mark.v &&
1167 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1168 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1169 x->genid++;
1173 void xfrm_state_insert(struct xfrm_state *x)
1175 struct net *net = xs_net(x);
1177 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1178 __xfrm_state_bump_genids(x);
1179 __xfrm_state_insert(x);
1180 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1182 EXPORT_SYMBOL(xfrm_state_insert);
1184 /* net->xfrm.xfrm_state_lock is held */
1185 static struct xfrm_state *__find_acq_core(struct net *net,
1186 const struct xfrm_mark *m,
1187 unsigned short family, u8 mode,
1188 u32 reqid, u8 proto,
1189 const xfrm_address_t *daddr,
1190 const xfrm_address_t *saddr,
1191 int create)
1193 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1194 struct xfrm_state *x;
1195 u32 mark = m->v & m->m;
1197 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1198 if (x->props.reqid != reqid ||
1199 x->props.mode != mode ||
1200 x->props.family != family ||
1201 x->km.state != XFRM_STATE_ACQ ||
1202 x->id.spi != 0 ||
1203 x->id.proto != proto ||
1204 (mark & x->mark.m) != x->mark.v ||
1205 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1206 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1207 continue;
1209 xfrm_state_hold(x);
1210 return x;
1213 if (!create)
1214 return NULL;
1216 x = xfrm_state_alloc(net);
1217 if (likely(x)) {
1218 switch (family) {
1219 case AF_INET:
1220 x->sel.daddr.a4 = daddr->a4;
1221 x->sel.saddr.a4 = saddr->a4;
1222 x->sel.prefixlen_d = 32;
1223 x->sel.prefixlen_s = 32;
1224 x->props.saddr.a4 = saddr->a4;
1225 x->id.daddr.a4 = daddr->a4;
1226 break;
1228 case AF_INET6:
1229 x->sel.daddr.in6 = daddr->in6;
1230 x->sel.saddr.in6 = saddr->in6;
1231 x->sel.prefixlen_d = 128;
1232 x->sel.prefixlen_s = 128;
1233 x->props.saddr.in6 = saddr->in6;
1234 x->id.daddr.in6 = daddr->in6;
1235 break;
1238 x->km.state = XFRM_STATE_ACQ;
1239 x->id.proto = proto;
1240 x->props.family = family;
1241 x->props.mode = mode;
1242 x->props.reqid = reqid;
1243 x->mark.v = m->v;
1244 x->mark.m = m->m;
1245 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1246 xfrm_state_hold(x);
1247 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1248 list_add(&x->km.all, &net->xfrm.state_all);
1249 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1250 h = xfrm_src_hash(net, daddr, saddr, family);
1251 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1253 net->xfrm.state_num++;
1255 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1258 return x;
1261 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1263 int xfrm_state_add(struct xfrm_state *x)
1265 struct net *net = xs_net(x);
1266 struct xfrm_state *x1, *to_put;
1267 int family;
1268 int err;
1269 u32 mark = x->mark.v & x->mark.m;
1270 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1272 family = x->props.family;
1274 to_put = NULL;
1276 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1278 x1 = __xfrm_state_locate(x, use_spi, family);
1279 if (x1) {
1280 to_put = x1;
1281 x1 = NULL;
1282 err = -EEXIST;
1283 goto out;
1286 if (use_spi && x->km.seq) {
1287 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1288 if (x1 && ((x1->id.proto != x->id.proto) ||
1289 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1290 to_put = x1;
1291 x1 = NULL;
1295 if (use_spi && !x1)
1296 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1297 x->props.reqid, x->id.proto,
1298 &x->id.daddr, &x->props.saddr, 0);
1300 __xfrm_state_bump_genids(x);
1301 __xfrm_state_insert(x);
1302 err = 0;
1304 out:
1305 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1307 if (x1) {
1308 xfrm_state_delete(x1);
1309 xfrm_state_put(x1);
1312 if (to_put)
1313 xfrm_state_put(to_put);
1315 return err;
1317 EXPORT_SYMBOL(xfrm_state_add);
1319 #ifdef CONFIG_XFRM_MIGRATE
1320 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1321 struct xfrm_encap_tmpl *encap)
1323 struct net *net = xs_net(orig);
1324 struct xfrm_state *x = xfrm_state_alloc(net);
1325 if (!x)
1326 goto out;
1328 memcpy(&x->id, &orig->id, sizeof(x->id));
1329 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1330 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1331 x->props.mode = orig->props.mode;
1332 x->props.replay_window = orig->props.replay_window;
1333 x->props.reqid = orig->props.reqid;
1334 x->props.family = orig->props.family;
1335 x->props.saddr = orig->props.saddr;
1337 if (orig->aalg) {
1338 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1339 if (!x->aalg)
1340 goto error;
1342 x->props.aalgo = orig->props.aalgo;
1344 if (orig->aead) {
1345 x->aead = xfrm_algo_aead_clone(orig->aead);
1346 if (!x->aead)
1347 goto error;
1349 if (orig->ealg) {
1350 x->ealg = xfrm_algo_clone(orig->ealg);
1351 if (!x->ealg)
1352 goto error;
1354 x->props.ealgo = orig->props.ealgo;
1356 if (orig->calg) {
1357 x->calg = xfrm_algo_clone(orig->calg);
1358 if (!x->calg)
1359 goto error;
1361 x->props.calgo = orig->props.calgo;
1363 if (encap || orig->encap) {
1364 if (encap)
1365 x->encap = kmemdup(encap, sizeof(*x->encap),
1366 GFP_KERNEL);
1367 else
1368 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1369 GFP_KERNEL);
1371 if (!x->encap)
1372 goto error;
1375 if (orig->coaddr) {
1376 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1377 GFP_KERNEL);
1378 if (!x->coaddr)
1379 goto error;
1382 if (orig->replay_esn) {
1383 if (xfrm_replay_clone(x, orig))
1384 goto error;
1387 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1389 if (xfrm_init_state(x) < 0)
1390 goto error;
1392 x->props.flags = orig->props.flags;
1393 x->props.extra_flags = orig->props.extra_flags;
1395 x->tfcpad = orig->tfcpad;
1396 x->replay_maxdiff = orig->replay_maxdiff;
1397 x->replay_maxage = orig->replay_maxage;
1398 x->curlft.add_time = orig->curlft.add_time;
1399 x->km.state = orig->km.state;
1400 x->km.seq = orig->km.seq;
1401 x->replay = orig->replay;
1402 x->preplay = orig->preplay;
1404 return x;
1406 error:
1407 xfrm_state_put(x);
1408 out:
1409 return NULL;
1412 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1414 unsigned int h;
1415 struct xfrm_state *x = NULL;
1417 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1419 if (m->reqid) {
1420 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1421 m->reqid, m->old_family);
1422 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1423 if (x->props.mode != m->mode ||
1424 x->id.proto != m->proto)
1425 continue;
1426 if (m->reqid && x->props.reqid != m->reqid)
1427 continue;
1428 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1429 m->old_family) ||
1430 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1431 m->old_family))
1432 continue;
1433 xfrm_state_hold(x);
1434 break;
1436 } else {
1437 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1438 m->old_family);
1439 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1440 if (x->props.mode != m->mode ||
1441 x->id.proto != m->proto)
1442 continue;
1443 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1444 m->old_family) ||
1445 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1446 m->old_family))
1447 continue;
1448 xfrm_state_hold(x);
1449 break;
1453 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1455 return x;
1457 EXPORT_SYMBOL(xfrm_migrate_state_find);
1459 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1460 struct xfrm_migrate *m,
1461 struct xfrm_encap_tmpl *encap)
1463 struct xfrm_state *xc;
1465 xc = xfrm_state_clone(x, encap);
1466 if (!xc)
1467 return NULL;
1469 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1470 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1472 /* add state */
1473 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1474 /* a care is needed when the destination address of the
1475 state is to be updated as it is a part of triplet */
1476 xfrm_state_insert(xc);
1477 } else {
1478 if (xfrm_state_add(xc) < 0)
1479 goto error;
1482 return xc;
1483 error:
1484 xfrm_state_put(xc);
1485 return NULL;
1487 EXPORT_SYMBOL(xfrm_state_migrate);
1488 #endif
1490 int xfrm_state_update(struct xfrm_state *x)
1492 struct xfrm_state *x1, *to_put;
1493 int err;
1494 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1495 struct net *net = xs_net(x);
1497 to_put = NULL;
1499 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1500 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1502 err = -ESRCH;
1503 if (!x1)
1504 goto out;
1506 if (xfrm_state_kern(x1)) {
1507 to_put = x1;
1508 err = -EEXIST;
1509 goto out;
1512 if (x1->km.state == XFRM_STATE_ACQ) {
1513 __xfrm_state_insert(x);
1514 x = NULL;
1516 err = 0;
1518 out:
1519 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1521 if (to_put)
1522 xfrm_state_put(to_put);
1524 if (err)
1525 return err;
1527 if (!x) {
1528 xfrm_state_delete(x1);
1529 xfrm_state_put(x1);
1530 return 0;
1533 err = -EINVAL;
1534 spin_lock_bh(&x1->lock);
1535 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1536 if (x->encap && x1->encap)
1537 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1538 if (x->coaddr && x1->coaddr) {
1539 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1541 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1542 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1543 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1544 x1->km.dying = 0;
1546 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1547 if (x1->curlft.use_time)
1548 xfrm_state_check_expire(x1);
1550 err = 0;
1551 x->km.state = XFRM_STATE_DEAD;
1552 __xfrm_state_put(x);
1554 spin_unlock_bh(&x1->lock);
1556 xfrm_state_put(x1);
1558 return err;
1560 EXPORT_SYMBOL(xfrm_state_update);
1562 int xfrm_state_check_expire(struct xfrm_state *x)
1564 if (!x->curlft.use_time)
1565 x->curlft.use_time = get_seconds();
1567 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1568 x->curlft.packets >= x->lft.hard_packet_limit) {
1569 x->km.state = XFRM_STATE_EXPIRED;
1570 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
1571 return -EINVAL;
1574 if (!x->km.dying &&
1575 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1576 x->curlft.packets >= x->lft.soft_packet_limit)) {
1577 x->km.dying = 1;
1578 km_state_expired(x, 0, 0);
1580 return 0;
1582 EXPORT_SYMBOL(xfrm_state_check_expire);
1584 struct xfrm_state *
1585 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1586 u8 proto, unsigned short family)
1588 struct xfrm_state *x;
1590 rcu_read_lock();
1591 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1592 rcu_read_unlock();
1593 return x;
1595 EXPORT_SYMBOL(xfrm_state_lookup);
1597 struct xfrm_state *
1598 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1599 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1600 u8 proto, unsigned short family)
1602 struct xfrm_state *x;
1604 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1605 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1606 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1607 return x;
1609 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1611 struct xfrm_state *
1612 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1613 u8 proto, const xfrm_address_t *daddr,
1614 const xfrm_address_t *saddr, int create, unsigned short family)
1616 struct xfrm_state *x;
1618 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1619 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1620 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1622 return x;
1624 EXPORT_SYMBOL(xfrm_find_acq);
1626 #ifdef CONFIG_XFRM_SUB_POLICY
1628 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1629 unsigned short family, struct net *net)
1631 int i;
1632 int err = 0;
1633 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1634 if (!afinfo)
1635 return -EAFNOSUPPORT;
1637 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
1638 if (afinfo->tmpl_sort)
1639 err = afinfo->tmpl_sort(dst, src, n);
1640 else
1641 for (i = 0; i < n; i++)
1642 dst[i] = src[i];
1643 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1644 rcu_read_unlock();
1645 return err;
1647 EXPORT_SYMBOL(xfrm_tmpl_sort);
1650 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1651 unsigned short family)
1653 int i;
1654 int err = 0;
1655 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1656 struct net *net = xs_net(*src);
1658 if (!afinfo)
1659 return -EAFNOSUPPORT;
1661 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1662 if (afinfo->state_sort)
1663 err = afinfo->state_sort(dst, src, n);
1664 else
1665 for (i = 0; i < n; i++)
1666 dst[i] = src[i];
1667 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1668 rcu_read_unlock();
1669 return err;
1671 EXPORT_SYMBOL(xfrm_state_sort);
1672 #endif
1674 /* Silly enough, but I'm lazy to build resolution list */
1676 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1678 int i;
1680 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1681 struct xfrm_state *x;
1683 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1684 if (x->km.seq == seq &&
1685 (mark & x->mark.m) == x->mark.v &&
1686 x->km.state == XFRM_STATE_ACQ) {
1687 xfrm_state_hold(x);
1688 return x;
1692 return NULL;
1695 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1697 struct xfrm_state *x;
1699 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1700 x = __xfrm_find_acq_byseq(net, mark, seq);
1701 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1702 return x;
1704 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1706 u32 xfrm_get_acqseq(void)
1708 u32 res;
1709 static atomic_t acqseq;
1711 do {
1712 res = atomic_inc_return(&acqseq);
1713 } while (!res);
1715 return res;
1717 EXPORT_SYMBOL(xfrm_get_acqseq);
1719 int verify_spi_info(u8 proto, u32 min, u32 max)
1721 switch (proto) {
1722 case IPPROTO_AH:
1723 case IPPROTO_ESP:
1724 break;
1726 case IPPROTO_COMP:
1727 /* IPCOMP spi is 16-bits. */
1728 if (max >= 0x10000)
1729 return -EINVAL;
1730 break;
1732 default:
1733 return -EINVAL;
1736 if (min > max)
1737 return -EINVAL;
1739 return 0;
1741 EXPORT_SYMBOL(verify_spi_info);
1743 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1745 struct net *net = xs_net(x);
1746 unsigned int h;
1747 struct xfrm_state *x0;
1748 int err = -ENOENT;
1749 __be32 minspi = htonl(low);
1750 __be32 maxspi = htonl(high);
1751 u32 mark = x->mark.v & x->mark.m;
1753 spin_lock_bh(&x->lock);
1754 if (x->km.state == XFRM_STATE_DEAD)
1755 goto unlock;
1757 err = 0;
1758 if (x->id.spi)
1759 goto unlock;
1761 err = -ENOENT;
1763 if (minspi == maxspi) {
1764 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1765 if (x0) {
1766 xfrm_state_put(x0);
1767 goto unlock;
1769 x->id.spi = minspi;
1770 } else {
1771 u32 spi = 0;
1772 for (h = 0; h < high-low+1; h++) {
1773 spi = low + prandom_u32()%(high-low+1);
1774 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1775 if (x0 == NULL) {
1776 x->id.spi = htonl(spi);
1777 break;
1779 xfrm_state_put(x0);
1782 if (x->id.spi) {
1783 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1784 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1785 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1786 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1788 err = 0;
1791 unlock:
1792 spin_unlock_bh(&x->lock);
1794 return err;
1796 EXPORT_SYMBOL(xfrm_alloc_spi);
1798 static bool __xfrm_state_filter_match(struct xfrm_state *x,
1799 struct xfrm_address_filter *filter)
1801 if (filter) {
1802 if ((filter->family == AF_INET ||
1803 filter->family == AF_INET6) &&
1804 x->props.family != filter->family)
1805 return false;
1807 return addr_match(&x->props.saddr, &filter->saddr,
1808 filter->splen) &&
1809 addr_match(&x->id.daddr, &filter->daddr,
1810 filter->dplen);
1812 return true;
1815 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1816 int (*func)(struct xfrm_state *, int, void*),
1817 void *data)
1819 struct xfrm_state *state;
1820 struct xfrm_state_walk *x;
1821 int err = 0;
1823 if (walk->seq != 0 && list_empty(&walk->all))
1824 return 0;
1826 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1827 if (list_empty(&walk->all))
1828 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1829 else
1830 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
1831 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1832 if (x->state == XFRM_STATE_DEAD)
1833 continue;
1834 state = container_of(x, struct xfrm_state, km);
1835 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1836 continue;
1837 if (!__xfrm_state_filter_match(state, walk->filter))
1838 continue;
1839 err = func(state, walk->seq, data);
1840 if (err) {
1841 list_move_tail(&walk->all, &x->all);
1842 goto out;
1844 walk->seq++;
1846 if (walk->seq == 0) {
1847 err = -ENOENT;
1848 goto out;
1850 list_del_init(&walk->all);
1851 out:
1852 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1853 return err;
1855 EXPORT_SYMBOL(xfrm_state_walk);
1857 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1858 struct xfrm_address_filter *filter)
1860 INIT_LIST_HEAD(&walk->all);
1861 walk->proto = proto;
1862 walk->state = XFRM_STATE_DEAD;
1863 walk->seq = 0;
1864 walk->filter = filter;
1866 EXPORT_SYMBOL(xfrm_state_walk_init);
1868 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1870 kfree(walk->filter);
1872 if (list_empty(&walk->all))
1873 return;
1875 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1876 list_del(&walk->all);
1877 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1879 EXPORT_SYMBOL(xfrm_state_walk_done);
1881 static void xfrm_replay_timer_handler(struct timer_list *t)
1883 struct xfrm_state *x = from_timer(x, t, rtimer);
1885 spin_lock(&x->lock);
1887 if (x->km.state == XFRM_STATE_VALID) {
1888 if (xfrm_aevent_is_on(xs_net(x)))
1889 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1890 else
1891 x->xflags |= XFRM_TIME_DEFER;
1894 spin_unlock(&x->lock);
1897 static LIST_HEAD(xfrm_km_list);
1899 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1901 struct xfrm_mgr *km;
1903 rcu_read_lock();
1904 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1905 if (km->notify_policy)
1906 km->notify_policy(xp, dir, c);
1907 rcu_read_unlock();
1910 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1912 struct xfrm_mgr *km;
1913 rcu_read_lock();
1914 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1915 if (km->notify)
1916 km->notify(x, c);
1917 rcu_read_unlock();
1920 EXPORT_SYMBOL(km_policy_notify);
1921 EXPORT_SYMBOL(km_state_notify);
1923 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1925 struct km_event c;
1927 c.data.hard = hard;
1928 c.portid = portid;
1929 c.event = XFRM_MSG_EXPIRE;
1930 km_state_notify(x, &c);
1933 EXPORT_SYMBOL(km_state_expired);
1935 * We send to all registered managers regardless of failure
1936 * We are happy with one success
1938 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1940 int err = -EINVAL, acqret;
1941 struct xfrm_mgr *km;
1943 rcu_read_lock();
1944 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1945 acqret = km->acquire(x, t, pol);
1946 if (!acqret)
1947 err = acqret;
1949 rcu_read_unlock();
1950 return err;
1952 EXPORT_SYMBOL(km_query);
1954 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1956 int err = -EINVAL;
1957 struct xfrm_mgr *km;
1959 rcu_read_lock();
1960 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1961 if (km->new_mapping)
1962 err = km->new_mapping(x, ipaddr, sport);
1963 if (!err)
1964 break;
1966 rcu_read_unlock();
1967 return err;
1969 EXPORT_SYMBOL(km_new_mapping);
1971 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
1973 struct km_event c;
1975 c.data.hard = hard;
1976 c.portid = portid;
1977 c.event = XFRM_MSG_POLEXPIRE;
1978 km_policy_notify(pol, dir, &c);
1980 EXPORT_SYMBOL(km_policy_expired);
1982 #ifdef CONFIG_XFRM_MIGRATE
1983 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1984 const struct xfrm_migrate *m, int num_migrate,
1985 const struct xfrm_kmaddress *k,
1986 const struct xfrm_encap_tmpl *encap)
1988 int err = -EINVAL;
1989 int ret;
1990 struct xfrm_mgr *km;
1992 rcu_read_lock();
1993 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1994 if (km->migrate) {
1995 ret = km->migrate(sel, dir, type, m, num_migrate, k,
1996 encap);
1997 if (!ret)
1998 err = ret;
2001 rcu_read_unlock();
2002 return err;
2004 EXPORT_SYMBOL(km_migrate);
2005 #endif
2007 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2009 int err = -EINVAL;
2010 int ret;
2011 struct xfrm_mgr *km;
2013 rcu_read_lock();
2014 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2015 if (km->report) {
2016 ret = km->report(net, proto, sel, addr);
2017 if (!ret)
2018 err = ret;
2021 rcu_read_unlock();
2022 return err;
2024 EXPORT_SYMBOL(km_report);
2026 bool km_is_alive(const struct km_event *c)
2028 struct xfrm_mgr *km;
2029 bool is_alive = false;
2031 rcu_read_lock();
2032 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2033 if (km->is_alive && km->is_alive(c)) {
2034 is_alive = true;
2035 break;
2038 rcu_read_unlock();
2040 return is_alive;
2042 EXPORT_SYMBOL(km_is_alive);
2044 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2046 int err;
2047 u8 *data;
2048 struct xfrm_mgr *km;
2049 struct xfrm_policy *pol = NULL;
2051 if (optlen <= 0 || optlen > PAGE_SIZE)
2052 return -EMSGSIZE;
2054 data = memdup_user(optval, optlen);
2055 if (IS_ERR(data))
2056 return PTR_ERR(data);
2058 err = -EINVAL;
2059 rcu_read_lock();
2060 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2061 pol = km->compile_policy(sk, optname, data,
2062 optlen, &err);
2063 if (err >= 0)
2064 break;
2066 rcu_read_unlock();
2068 if (err >= 0) {
2069 xfrm_sk_policy_insert(sk, err, pol);
2070 xfrm_pol_put(pol);
2071 __sk_dst_reset(sk);
2072 err = 0;
2075 kfree(data);
2076 return err;
2078 EXPORT_SYMBOL(xfrm_user_policy);
2080 static DEFINE_SPINLOCK(xfrm_km_lock);
2082 int xfrm_register_km(struct xfrm_mgr *km)
2084 spin_lock_bh(&xfrm_km_lock);
2085 list_add_tail_rcu(&km->list, &xfrm_km_list);
2086 spin_unlock_bh(&xfrm_km_lock);
2087 return 0;
2089 EXPORT_SYMBOL(xfrm_register_km);
2091 int xfrm_unregister_km(struct xfrm_mgr *km)
2093 spin_lock_bh(&xfrm_km_lock);
2094 list_del_rcu(&km->list);
2095 spin_unlock_bh(&xfrm_km_lock);
2096 synchronize_rcu();
2097 return 0;
2099 EXPORT_SYMBOL(xfrm_unregister_km);
2101 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2103 int err = 0;
2105 if (WARN_ON(afinfo->family >= NPROTO))
2106 return -EAFNOSUPPORT;
2108 spin_lock_bh(&xfrm_state_afinfo_lock);
2109 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2110 err = -EEXIST;
2111 else
2112 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2113 spin_unlock_bh(&xfrm_state_afinfo_lock);
2114 return err;
2116 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2118 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2120 int err = 0, family = afinfo->family;
2122 if (WARN_ON(family >= NPROTO))
2123 return -EAFNOSUPPORT;
2125 spin_lock_bh(&xfrm_state_afinfo_lock);
2126 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2127 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2128 err = -EINVAL;
2129 else
2130 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2132 spin_unlock_bh(&xfrm_state_afinfo_lock);
2133 synchronize_rcu();
2134 return err;
2136 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2138 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2140 if (unlikely(family >= NPROTO))
2141 return NULL;
2143 return rcu_dereference(xfrm_state_afinfo[family]);
2146 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2148 struct xfrm_state_afinfo *afinfo;
2149 if (unlikely(family >= NPROTO))
2150 return NULL;
2151 rcu_read_lock();
2152 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2153 if (unlikely(!afinfo))
2154 rcu_read_unlock();
2155 return afinfo;
2158 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2159 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2161 if (x->tunnel) {
2162 struct xfrm_state *t = x->tunnel;
2164 if (atomic_read(&t->tunnel_users) == 2)
2165 xfrm_state_delete(t);
2166 atomic_dec(&t->tunnel_users);
2167 xfrm_state_put(t);
2168 x->tunnel = NULL;
2171 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2173 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2175 const struct xfrm_type *type = READ_ONCE(x->type);
2177 if (x->km.state == XFRM_STATE_VALID &&
2178 type && type->get_mtu)
2179 return type->get_mtu(x, mtu);
2181 return mtu - x->props.header_len;
2184 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2186 struct xfrm_state_afinfo *afinfo;
2187 struct xfrm_mode *inner_mode;
2188 int family = x->props.family;
2189 int err;
2191 err = -EAFNOSUPPORT;
2192 afinfo = xfrm_state_get_afinfo(family);
2193 if (!afinfo)
2194 goto error;
2196 err = 0;
2197 if (afinfo->init_flags)
2198 err = afinfo->init_flags(x);
2200 rcu_read_unlock();
2202 if (err)
2203 goto error;
2205 err = -EPROTONOSUPPORT;
2207 if (x->sel.family != AF_UNSPEC) {
2208 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2209 if (inner_mode == NULL)
2210 goto error;
2212 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2213 family != x->sel.family) {
2214 xfrm_put_mode(inner_mode);
2215 goto error;
2218 x->inner_mode = inner_mode;
2219 } else {
2220 struct xfrm_mode *inner_mode_iaf;
2221 int iafamily = AF_INET;
2223 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2224 if (inner_mode == NULL)
2225 goto error;
2227 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2228 xfrm_put_mode(inner_mode);
2229 goto error;
2231 x->inner_mode = inner_mode;
2233 if (x->props.family == AF_INET)
2234 iafamily = AF_INET6;
2236 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2237 if (inner_mode_iaf) {
2238 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2239 x->inner_mode_iaf = inner_mode_iaf;
2240 else
2241 xfrm_put_mode(inner_mode_iaf);
2245 x->type = xfrm_get_type(x->id.proto, family);
2246 if (x->type == NULL)
2247 goto error;
2249 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2251 err = x->type->init_state(x);
2252 if (err)
2253 goto error;
2255 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2256 if (x->outer_mode == NULL) {
2257 err = -EPROTONOSUPPORT;
2258 goto error;
2261 if (init_replay) {
2262 err = xfrm_init_replay(x);
2263 if (err)
2264 goto error;
2267 x->km.state = XFRM_STATE_VALID;
2269 error:
2270 return err;
2273 EXPORT_SYMBOL(__xfrm_init_state);
2275 int xfrm_init_state(struct xfrm_state *x)
2277 return __xfrm_init_state(x, true, false);
2280 EXPORT_SYMBOL(xfrm_init_state);
2282 int __net_init xfrm_state_init(struct net *net)
2284 unsigned int sz;
2286 INIT_LIST_HEAD(&net->xfrm.state_all);
2288 sz = sizeof(struct hlist_head) * 8;
2290 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2291 if (!net->xfrm.state_bydst)
2292 goto out_bydst;
2293 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2294 if (!net->xfrm.state_bysrc)
2295 goto out_bysrc;
2296 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2297 if (!net->xfrm.state_byspi)
2298 goto out_byspi;
2299 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2301 net->xfrm.state_num = 0;
2302 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2303 spin_lock_init(&net->xfrm.xfrm_state_lock);
2304 return 0;
2306 out_byspi:
2307 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2308 out_bysrc:
2309 xfrm_hash_free(net->xfrm.state_bydst, sz);
2310 out_bydst:
2311 return -ENOMEM;
2314 void xfrm_state_fini(struct net *net)
2316 unsigned int sz;
2318 flush_work(&net->xfrm.state_hash_work);
2319 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
2320 flush_work(&xfrm_state_gc_work);
2322 WARN_ON(!list_empty(&net->xfrm.state_all));
2324 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2325 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2326 xfrm_hash_free(net->xfrm.state_byspi, sz);
2327 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2328 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2329 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2330 xfrm_hash_free(net->xfrm.state_bydst, sz);
2333 #ifdef CONFIG_AUDITSYSCALL
2334 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2335 struct audit_buffer *audit_buf)
2337 struct xfrm_sec_ctx *ctx = x->security;
2338 u32 spi = ntohl(x->id.spi);
2340 if (ctx)
2341 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2342 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2344 switch (x->props.family) {
2345 case AF_INET:
2346 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2347 &x->props.saddr.a4, &x->id.daddr.a4);
2348 break;
2349 case AF_INET6:
2350 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2351 x->props.saddr.a6, x->id.daddr.a6);
2352 break;
2355 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2358 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2359 struct audit_buffer *audit_buf)
2361 const struct iphdr *iph4;
2362 const struct ipv6hdr *iph6;
2364 switch (family) {
2365 case AF_INET:
2366 iph4 = ip_hdr(skb);
2367 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2368 &iph4->saddr, &iph4->daddr);
2369 break;
2370 case AF_INET6:
2371 iph6 = ipv6_hdr(skb);
2372 audit_log_format(audit_buf,
2373 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2374 &iph6->saddr, &iph6->daddr,
2375 iph6->flow_lbl[0] & 0x0f,
2376 iph6->flow_lbl[1],
2377 iph6->flow_lbl[2]);
2378 break;
2382 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2384 struct audit_buffer *audit_buf;
2386 audit_buf = xfrm_audit_start("SAD-add");
2387 if (audit_buf == NULL)
2388 return;
2389 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2390 xfrm_audit_helper_sainfo(x, audit_buf);
2391 audit_log_format(audit_buf, " res=%u", result);
2392 audit_log_end(audit_buf);
2394 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2396 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2398 struct audit_buffer *audit_buf;
2400 audit_buf = xfrm_audit_start("SAD-delete");
2401 if (audit_buf == NULL)
2402 return;
2403 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2404 xfrm_audit_helper_sainfo(x, audit_buf);
2405 audit_log_format(audit_buf, " res=%u", result);
2406 audit_log_end(audit_buf);
2408 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2410 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2411 struct sk_buff *skb)
2413 struct audit_buffer *audit_buf;
2414 u32 spi;
2416 audit_buf = xfrm_audit_start("SA-replay-overflow");
2417 if (audit_buf == NULL)
2418 return;
2419 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2420 /* don't record the sequence number because it's inherent in this kind
2421 * of audit message */
2422 spi = ntohl(x->id.spi);
2423 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2424 audit_log_end(audit_buf);
2426 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2428 void xfrm_audit_state_replay(struct xfrm_state *x,
2429 struct sk_buff *skb, __be32 net_seq)
2431 struct audit_buffer *audit_buf;
2432 u32 spi;
2434 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2435 if (audit_buf == NULL)
2436 return;
2437 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2438 spi = ntohl(x->id.spi);
2439 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2440 spi, spi, ntohl(net_seq));
2441 audit_log_end(audit_buf);
2443 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2445 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2447 struct audit_buffer *audit_buf;
2449 audit_buf = xfrm_audit_start("SA-notfound");
2450 if (audit_buf == NULL)
2451 return;
2452 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2453 audit_log_end(audit_buf);
2455 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2457 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2458 __be32 net_spi, __be32 net_seq)
2460 struct audit_buffer *audit_buf;
2461 u32 spi;
2463 audit_buf = xfrm_audit_start("SA-notfound");
2464 if (audit_buf == NULL)
2465 return;
2466 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2467 spi = ntohl(net_spi);
2468 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2469 spi, spi, ntohl(net_seq));
2470 audit_log_end(audit_buf);
2472 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2474 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2475 struct sk_buff *skb, u8 proto)
2477 struct audit_buffer *audit_buf;
2478 __be32 net_spi;
2479 __be32 net_seq;
2481 audit_buf = xfrm_audit_start("SA-icv-failure");
2482 if (audit_buf == NULL)
2483 return;
2484 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2485 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2486 u32 spi = ntohl(net_spi);
2487 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2488 spi, spi, ntohl(net_seq));
2490 audit_log_end(audit_buf);
2492 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2493 #endif /* CONFIG_AUDITSYSCALL */