ALSA: hda/realtek - Change front mic location for Lenovo M710q
[linux/fpc-iii.git] / net / xfrm / xfrm_state.c
blob11e09eb138d60cad4fea9ef815799221fe63294c
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);
45 static struct kmem_cache *xfrm_state_cache __ro_after_init;
47 static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
48 static HLIST_HEAD(xfrm_state_gc_list);
50 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
52 return refcount_inc_not_zero(&x->refcnt);
55 static inline unsigned int xfrm_dst_hash(struct net *net,
56 const xfrm_address_t *daddr,
57 const xfrm_address_t *saddr,
58 u32 reqid,
59 unsigned short family)
61 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
64 static inline unsigned int xfrm_src_hash(struct net *net,
65 const xfrm_address_t *daddr,
66 const xfrm_address_t *saddr,
67 unsigned short family)
69 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
72 static inline unsigned int
73 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
74 __be32 spi, u8 proto, unsigned short family)
76 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
79 static void xfrm_hash_transfer(struct hlist_head *list,
80 struct hlist_head *ndsttable,
81 struct hlist_head *nsrctable,
82 struct hlist_head *nspitable,
83 unsigned int nhashmask)
85 struct hlist_node *tmp;
86 struct xfrm_state *x;
88 hlist_for_each_entry_safe(x, tmp, list, bydst) {
89 unsigned int h;
91 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
92 x->props.reqid, x->props.family,
93 nhashmask);
94 hlist_add_head_rcu(&x->bydst, ndsttable + h);
96 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
97 x->props.family,
98 nhashmask);
99 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
101 if (x->id.spi) {
102 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
103 x->id.proto, x->props.family,
104 nhashmask);
105 hlist_add_head_rcu(&x->byspi, nspitable + h);
110 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
112 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
115 static void xfrm_hash_resize(struct work_struct *work)
117 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
118 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
119 unsigned long nsize, osize;
120 unsigned int nhashmask, ohashmask;
121 int i;
123 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
124 ndst = xfrm_hash_alloc(nsize);
125 if (!ndst)
126 return;
127 nsrc = xfrm_hash_alloc(nsize);
128 if (!nsrc) {
129 xfrm_hash_free(ndst, nsize);
130 return;
132 nspi = xfrm_hash_alloc(nsize);
133 if (!nspi) {
134 xfrm_hash_free(ndst, nsize);
135 xfrm_hash_free(nsrc, nsize);
136 return;
139 spin_lock_bh(&net->xfrm.xfrm_state_lock);
140 write_seqcount_begin(&xfrm_state_hash_generation);
142 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
143 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
144 for (i = net->xfrm.state_hmask; i >= 0; i--)
145 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
147 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
148 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
149 ohashmask = net->xfrm.state_hmask;
151 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
152 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
153 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
154 net->xfrm.state_hmask = nhashmask;
156 write_seqcount_end(&xfrm_state_hash_generation);
157 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
159 osize = (ohashmask + 1) * sizeof(struct hlist_head);
161 synchronize_rcu();
163 xfrm_hash_free(odst, osize);
164 xfrm_hash_free(osrc, osize);
165 xfrm_hash_free(ospi, osize);
168 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
169 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
171 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
173 int __xfrm_state_delete(struct xfrm_state *x);
175 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
176 bool km_is_alive(const struct km_event *c);
177 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
179 static DEFINE_SPINLOCK(xfrm_type_lock);
180 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
182 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
183 const struct xfrm_type **typemap;
184 int err = 0;
186 if (unlikely(afinfo == NULL))
187 return -EAFNOSUPPORT;
188 typemap = afinfo->type_map;
189 spin_lock_bh(&xfrm_type_lock);
191 if (likely(typemap[type->proto] == NULL))
192 typemap[type->proto] = type;
193 else
194 err = -EEXIST;
195 spin_unlock_bh(&xfrm_type_lock);
196 rcu_read_unlock();
197 return err;
199 EXPORT_SYMBOL(xfrm_register_type);
201 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
203 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
204 const struct xfrm_type **typemap;
205 int err = 0;
207 if (unlikely(afinfo == NULL))
208 return -EAFNOSUPPORT;
209 typemap = afinfo->type_map;
210 spin_lock_bh(&xfrm_type_lock);
212 if (unlikely(typemap[type->proto] != type))
213 err = -ENOENT;
214 else
215 typemap[type->proto] = NULL;
216 spin_unlock_bh(&xfrm_type_lock);
217 rcu_read_unlock();
218 return err;
220 EXPORT_SYMBOL(xfrm_unregister_type);
222 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
224 struct xfrm_state_afinfo *afinfo;
225 const struct xfrm_type **typemap;
226 const struct xfrm_type *type;
227 int modload_attempted = 0;
229 retry:
230 afinfo = xfrm_state_get_afinfo(family);
231 if (unlikely(afinfo == NULL))
232 return NULL;
233 typemap = afinfo->type_map;
235 type = READ_ONCE(typemap[proto]);
236 if (unlikely(type && !try_module_get(type->owner)))
237 type = NULL;
239 rcu_read_unlock();
241 if (!type && !modload_attempted) {
242 request_module("xfrm-type-%d-%d", family, proto);
243 modload_attempted = 1;
244 goto retry;
247 return type;
250 static void xfrm_put_type(const struct xfrm_type *type)
252 module_put(type->owner);
255 static DEFINE_SPINLOCK(xfrm_type_offload_lock);
256 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
257 unsigned short family)
259 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
260 const struct xfrm_type_offload **typemap;
261 int err = 0;
263 if (unlikely(afinfo == NULL))
264 return -EAFNOSUPPORT;
265 typemap = afinfo->type_offload_map;
266 spin_lock_bh(&xfrm_type_offload_lock);
268 if (likely(typemap[type->proto] == NULL))
269 typemap[type->proto] = type;
270 else
271 err = -EEXIST;
272 spin_unlock_bh(&xfrm_type_offload_lock);
273 rcu_read_unlock();
274 return err;
276 EXPORT_SYMBOL(xfrm_register_type_offload);
278 int xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
279 unsigned short family)
281 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
282 const struct xfrm_type_offload **typemap;
283 int err = 0;
285 if (unlikely(afinfo == NULL))
286 return -EAFNOSUPPORT;
287 typemap = afinfo->type_offload_map;
288 spin_lock_bh(&xfrm_type_offload_lock);
290 if (unlikely(typemap[type->proto] != type))
291 err = -ENOENT;
292 else
293 typemap[type->proto] = NULL;
294 spin_unlock_bh(&xfrm_type_offload_lock);
295 rcu_read_unlock();
296 return err;
298 EXPORT_SYMBOL(xfrm_unregister_type_offload);
300 static const struct xfrm_type_offload *
301 xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
303 struct xfrm_state_afinfo *afinfo;
304 const struct xfrm_type_offload **typemap;
305 const struct xfrm_type_offload *type;
307 retry:
308 afinfo = xfrm_state_get_afinfo(family);
309 if (unlikely(afinfo == NULL))
310 return NULL;
311 typemap = afinfo->type_offload_map;
313 type = typemap[proto];
314 if ((type && !try_module_get(type->owner)))
315 type = NULL;
317 rcu_read_unlock();
319 if (!type && try_load) {
320 request_module("xfrm-offload-%d-%d", family, proto);
321 try_load = false;
322 goto retry;
325 return type;
328 static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
330 module_put(type->owner);
333 static DEFINE_SPINLOCK(xfrm_mode_lock);
334 int xfrm_register_mode(struct xfrm_mode *mode, int family)
336 struct xfrm_state_afinfo *afinfo;
337 struct xfrm_mode **modemap;
338 int err;
340 if (unlikely(mode->encap >= XFRM_MODE_MAX))
341 return -EINVAL;
343 afinfo = xfrm_state_get_afinfo(family);
344 if (unlikely(afinfo == NULL))
345 return -EAFNOSUPPORT;
347 err = -EEXIST;
348 modemap = afinfo->mode_map;
349 spin_lock_bh(&xfrm_mode_lock);
350 if (modemap[mode->encap])
351 goto out;
353 err = -ENOENT;
354 if (!try_module_get(afinfo->owner))
355 goto out;
357 mode->afinfo = afinfo;
358 modemap[mode->encap] = mode;
359 err = 0;
361 out:
362 spin_unlock_bh(&xfrm_mode_lock);
363 rcu_read_unlock();
364 return err;
366 EXPORT_SYMBOL(xfrm_register_mode);
368 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
370 struct xfrm_state_afinfo *afinfo;
371 struct xfrm_mode **modemap;
372 int err;
374 if (unlikely(mode->encap >= XFRM_MODE_MAX))
375 return -EINVAL;
377 afinfo = xfrm_state_get_afinfo(family);
378 if (unlikely(afinfo == NULL))
379 return -EAFNOSUPPORT;
381 err = -ENOENT;
382 modemap = afinfo->mode_map;
383 spin_lock_bh(&xfrm_mode_lock);
384 if (likely(modemap[mode->encap] == mode)) {
385 modemap[mode->encap] = NULL;
386 module_put(mode->afinfo->owner);
387 err = 0;
390 spin_unlock_bh(&xfrm_mode_lock);
391 rcu_read_unlock();
392 return err;
394 EXPORT_SYMBOL(xfrm_unregister_mode);
396 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
398 struct xfrm_state_afinfo *afinfo;
399 struct xfrm_mode *mode;
400 int modload_attempted = 0;
402 if (unlikely(encap >= XFRM_MODE_MAX))
403 return NULL;
405 retry:
406 afinfo = xfrm_state_get_afinfo(family);
407 if (unlikely(afinfo == NULL))
408 return NULL;
410 mode = READ_ONCE(afinfo->mode_map[encap]);
411 if (unlikely(mode && !try_module_get(mode->owner)))
412 mode = NULL;
414 rcu_read_unlock();
415 if (!mode && !modload_attempted) {
416 request_module("xfrm-mode-%d-%d", family, encap);
417 modload_attempted = 1;
418 goto retry;
421 return mode;
424 static void xfrm_put_mode(struct xfrm_mode *mode)
426 module_put(mode->owner);
429 void xfrm_state_free(struct xfrm_state *x)
431 kmem_cache_free(xfrm_state_cache, x);
433 EXPORT_SYMBOL(xfrm_state_free);
435 static void ___xfrm_state_destroy(struct xfrm_state *x)
437 tasklet_hrtimer_cancel(&x->mtimer);
438 del_timer_sync(&x->rtimer);
439 kfree(x->aead);
440 kfree(x->aalg);
441 kfree(x->ealg);
442 kfree(x->calg);
443 kfree(x->encap);
444 kfree(x->coaddr);
445 kfree(x->replay_esn);
446 kfree(x->preplay_esn);
447 if (x->inner_mode)
448 xfrm_put_mode(x->inner_mode);
449 if (x->inner_mode_iaf)
450 xfrm_put_mode(x->inner_mode_iaf);
451 if (x->outer_mode)
452 xfrm_put_mode(x->outer_mode);
453 if (x->type_offload)
454 xfrm_put_type_offload(x->type_offload);
455 if (x->type) {
456 x->type->destructor(x);
457 xfrm_put_type(x->type);
459 xfrm_dev_state_free(x);
460 security_xfrm_state_free(x);
461 xfrm_state_free(x);
464 static void xfrm_state_gc_task(struct work_struct *work)
466 struct xfrm_state *x;
467 struct hlist_node *tmp;
468 struct hlist_head gc_list;
470 spin_lock_bh(&xfrm_state_gc_lock);
471 hlist_move_list(&xfrm_state_gc_list, &gc_list);
472 spin_unlock_bh(&xfrm_state_gc_lock);
474 synchronize_rcu();
476 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
477 ___xfrm_state_destroy(x);
480 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
482 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
483 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
484 time64_t now = ktime_get_real_seconds();
485 time64_t next = TIME64_MAX;
486 int warn = 0;
487 int err = 0;
489 spin_lock(&x->lock);
490 if (x->km.state == XFRM_STATE_DEAD)
491 goto out;
492 if (x->km.state == XFRM_STATE_EXPIRED)
493 goto expired;
494 if (x->lft.hard_add_expires_seconds) {
495 long tmo = x->lft.hard_add_expires_seconds +
496 x->curlft.add_time - now;
497 if (tmo <= 0) {
498 if (x->xflags & XFRM_SOFT_EXPIRE) {
499 /* enter hard expire without soft expire first?!
500 * setting a new date could trigger this.
501 * workaround: fix x->curflt.add_time by below:
503 x->curlft.add_time = now - x->saved_tmo - 1;
504 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
505 } else
506 goto expired;
508 if (tmo < next)
509 next = tmo;
511 if (x->lft.hard_use_expires_seconds) {
512 long tmo = x->lft.hard_use_expires_seconds +
513 (x->curlft.use_time ? : now) - now;
514 if (tmo <= 0)
515 goto expired;
516 if (tmo < next)
517 next = tmo;
519 if (x->km.dying)
520 goto resched;
521 if (x->lft.soft_add_expires_seconds) {
522 long tmo = x->lft.soft_add_expires_seconds +
523 x->curlft.add_time - now;
524 if (tmo <= 0) {
525 warn = 1;
526 x->xflags &= ~XFRM_SOFT_EXPIRE;
527 } else if (tmo < next) {
528 next = tmo;
529 x->xflags |= XFRM_SOFT_EXPIRE;
530 x->saved_tmo = tmo;
533 if (x->lft.soft_use_expires_seconds) {
534 long tmo = x->lft.soft_use_expires_seconds +
535 (x->curlft.use_time ? : now) - now;
536 if (tmo <= 0)
537 warn = 1;
538 else if (tmo < next)
539 next = tmo;
542 x->km.dying = warn;
543 if (warn)
544 km_state_expired(x, 0, 0);
545 resched:
546 if (next != TIME64_MAX) {
547 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
550 goto out;
552 expired:
553 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
554 x->km.state = XFRM_STATE_EXPIRED;
556 err = __xfrm_state_delete(x);
557 if (!err)
558 km_state_expired(x, 1, 0);
560 xfrm_audit_state_delete(x, err ? 0 : 1, true);
562 out:
563 spin_unlock(&x->lock);
564 return HRTIMER_NORESTART;
567 static void xfrm_replay_timer_handler(struct timer_list *t);
569 struct xfrm_state *xfrm_state_alloc(struct net *net)
571 struct xfrm_state *x;
573 x = kmem_cache_alloc(xfrm_state_cache, GFP_ATOMIC | __GFP_ZERO);
575 if (x) {
576 write_pnet(&x->xs_net, net);
577 refcount_set(&x->refcnt, 1);
578 atomic_set(&x->tunnel_users, 0);
579 INIT_LIST_HEAD(&x->km.all);
580 INIT_HLIST_NODE(&x->bydst);
581 INIT_HLIST_NODE(&x->bysrc);
582 INIT_HLIST_NODE(&x->byspi);
583 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
584 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
585 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
586 x->curlft.add_time = ktime_get_real_seconds();
587 x->lft.soft_byte_limit = XFRM_INF;
588 x->lft.soft_packet_limit = XFRM_INF;
589 x->lft.hard_byte_limit = XFRM_INF;
590 x->lft.hard_packet_limit = XFRM_INF;
591 x->replay_maxage = 0;
592 x->replay_maxdiff = 0;
593 x->inner_mode = NULL;
594 x->inner_mode_iaf = NULL;
595 spin_lock_init(&x->lock);
597 return x;
599 EXPORT_SYMBOL(xfrm_state_alloc);
601 void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
603 WARN_ON(x->km.state != XFRM_STATE_DEAD);
605 if (sync) {
606 synchronize_rcu();
607 ___xfrm_state_destroy(x);
608 } else {
609 spin_lock_bh(&xfrm_state_gc_lock);
610 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
611 spin_unlock_bh(&xfrm_state_gc_lock);
612 schedule_work(&xfrm_state_gc_work);
615 EXPORT_SYMBOL(__xfrm_state_destroy);
617 int __xfrm_state_delete(struct xfrm_state *x)
619 struct net *net = xs_net(x);
620 int err = -ESRCH;
622 if (x->km.state != XFRM_STATE_DEAD) {
623 x->km.state = XFRM_STATE_DEAD;
624 spin_lock(&net->xfrm.xfrm_state_lock);
625 list_del(&x->km.all);
626 hlist_del_rcu(&x->bydst);
627 hlist_del_rcu(&x->bysrc);
628 if (x->id.spi)
629 hlist_del_rcu(&x->byspi);
630 net->xfrm.state_num--;
631 spin_unlock(&net->xfrm.xfrm_state_lock);
633 xfrm_dev_state_delete(x);
635 /* All xfrm_state objects are created by xfrm_state_alloc.
636 * The xfrm_state_alloc call gives a reference, and that
637 * is what we are dropping here.
639 xfrm_state_put(x);
640 err = 0;
643 return err;
645 EXPORT_SYMBOL(__xfrm_state_delete);
647 int xfrm_state_delete(struct xfrm_state *x)
649 int err;
651 spin_lock_bh(&x->lock);
652 err = __xfrm_state_delete(x);
653 spin_unlock_bh(&x->lock);
655 return err;
657 EXPORT_SYMBOL(xfrm_state_delete);
659 #ifdef CONFIG_SECURITY_NETWORK_XFRM
660 static inline int
661 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
663 int i, err = 0;
665 for (i = 0; i <= net->xfrm.state_hmask; i++) {
666 struct xfrm_state *x;
668 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
669 if (xfrm_id_proto_match(x->id.proto, proto) &&
670 (err = security_xfrm_state_delete(x)) != 0) {
671 xfrm_audit_state_delete(x, 0, task_valid);
672 return err;
677 return err;
680 static inline int
681 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
683 int i, err = 0;
685 for (i = 0; i <= net->xfrm.state_hmask; i++) {
686 struct xfrm_state *x;
687 struct xfrm_state_offload *xso;
689 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
690 xso = &x->xso;
692 if (xso->dev == dev &&
693 (err = security_xfrm_state_delete(x)) != 0) {
694 xfrm_audit_state_delete(x, 0, task_valid);
695 return err;
700 return err;
702 #else
703 static inline int
704 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
706 return 0;
709 static inline int
710 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
712 return 0;
714 #endif
716 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
718 int i, err = 0, cnt = 0;
720 spin_lock_bh(&net->xfrm.xfrm_state_lock);
721 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
722 if (err)
723 goto out;
725 err = -ESRCH;
726 for (i = 0; i <= net->xfrm.state_hmask; i++) {
727 struct xfrm_state *x;
728 restart:
729 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
730 if (!xfrm_state_kern(x) &&
731 xfrm_id_proto_match(x->id.proto, proto)) {
732 xfrm_state_hold(x);
733 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
735 err = xfrm_state_delete(x);
736 xfrm_audit_state_delete(x, err ? 0 : 1,
737 task_valid);
738 if (sync)
739 xfrm_state_put_sync(x);
740 else
741 xfrm_state_put(x);
742 if (!err)
743 cnt++;
745 spin_lock_bh(&net->xfrm.xfrm_state_lock);
746 goto restart;
750 out:
751 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
752 if (cnt)
753 err = 0;
755 return err;
757 EXPORT_SYMBOL(xfrm_state_flush);
759 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
761 int i, err = 0, cnt = 0;
763 spin_lock_bh(&net->xfrm.xfrm_state_lock);
764 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
765 if (err)
766 goto out;
768 err = -ESRCH;
769 for (i = 0; i <= net->xfrm.state_hmask; i++) {
770 struct xfrm_state *x;
771 struct xfrm_state_offload *xso;
772 restart:
773 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
774 xso = &x->xso;
776 if (!xfrm_state_kern(x) && xso->dev == dev) {
777 xfrm_state_hold(x);
778 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
780 err = xfrm_state_delete(x);
781 xfrm_audit_state_delete(x, err ? 0 : 1,
782 task_valid);
783 xfrm_state_put(x);
784 if (!err)
785 cnt++;
787 spin_lock_bh(&net->xfrm.xfrm_state_lock);
788 goto restart;
792 if (cnt)
793 err = 0;
795 out:
796 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
797 return err;
799 EXPORT_SYMBOL(xfrm_dev_state_flush);
801 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
803 spin_lock_bh(&net->xfrm.xfrm_state_lock);
804 si->sadcnt = net->xfrm.state_num;
805 si->sadhcnt = net->xfrm.state_hmask + 1;
806 si->sadhmcnt = xfrm_state_hashmax;
807 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
809 EXPORT_SYMBOL(xfrm_sad_getinfo);
811 static void
812 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
813 const struct xfrm_tmpl *tmpl,
814 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
815 unsigned short family)
817 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
819 if (!afinfo)
820 return;
822 afinfo->init_tempsel(&x->sel, fl);
824 if (family != tmpl->encap_family) {
825 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
826 if (!afinfo)
827 return;
829 afinfo->init_temprop(x, tmpl, daddr, saddr);
832 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
833 const xfrm_address_t *daddr,
834 __be32 spi, u8 proto,
835 unsigned short family)
837 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
838 struct xfrm_state *x;
840 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
841 if (x->props.family != family ||
842 x->id.spi != spi ||
843 x->id.proto != proto ||
844 !xfrm_addr_equal(&x->id.daddr, daddr, family))
845 continue;
847 if ((mark & x->mark.m) != x->mark.v)
848 continue;
849 if (!xfrm_state_hold_rcu(x))
850 continue;
851 return x;
854 return NULL;
857 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
858 const xfrm_address_t *daddr,
859 const xfrm_address_t *saddr,
860 u8 proto, unsigned short family)
862 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
863 struct xfrm_state *x;
865 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
866 if (x->props.family != family ||
867 x->id.proto != proto ||
868 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
869 !xfrm_addr_equal(&x->props.saddr, saddr, family))
870 continue;
872 if ((mark & x->mark.m) != x->mark.v)
873 continue;
874 if (!xfrm_state_hold_rcu(x))
875 continue;
876 return x;
879 return NULL;
882 static inline struct xfrm_state *
883 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
885 struct net *net = xs_net(x);
886 u32 mark = x->mark.v & x->mark.m;
888 if (use_spi)
889 return __xfrm_state_lookup(net, mark, &x->id.daddr,
890 x->id.spi, x->id.proto, family);
891 else
892 return __xfrm_state_lookup_byaddr(net, mark,
893 &x->id.daddr,
894 &x->props.saddr,
895 x->id.proto, family);
898 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
900 if (have_hash_collision &&
901 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
902 net->xfrm.state_num > net->xfrm.state_hmask)
903 schedule_work(&net->xfrm.state_hash_work);
906 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
907 const struct flowi *fl, unsigned short family,
908 struct xfrm_state **best, int *acq_in_progress,
909 int *error)
911 /* Resolution logic:
912 * 1. There is a valid state with matching selector. Done.
913 * 2. Valid state with inappropriate selector. Skip.
915 * Entering area of "sysdeps".
917 * 3. If state is not valid, selector is temporary, it selects
918 * only session which triggered previous resolution. Key
919 * manager will do something to install a state with proper
920 * selector.
922 if (x->km.state == XFRM_STATE_VALID) {
923 if ((x->sel.family &&
924 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
925 !security_xfrm_state_pol_flow_match(x, pol, fl))
926 return;
928 if (!*best ||
929 (*best)->km.dying > x->km.dying ||
930 ((*best)->km.dying == x->km.dying &&
931 (*best)->curlft.add_time < x->curlft.add_time))
932 *best = x;
933 } else if (x->km.state == XFRM_STATE_ACQ) {
934 *acq_in_progress = 1;
935 } else if (x->km.state == XFRM_STATE_ERROR ||
936 x->km.state == XFRM_STATE_EXPIRED) {
937 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
938 security_xfrm_state_pol_flow_match(x, pol, fl))
939 *error = -ESRCH;
943 struct xfrm_state *
944 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
945 const struct flowi *fl, struct xfrm_tmpl *tmpl,
946 struct xfrm_policy *pol, int *err,
947 unsigned short family, u32 if_id)
949 static xfrm_address_t saddr_wildcard = { };
950 struct net *net = xp_net(pol);
951 unsigned int h, h_wildcard;
952 struct xfrm_state *x, *x0, *to_put;
953 int acquire_in_progress = 0;
954 int error = 0;
955 struct xfrm_state *best = NULL;
956 u32 mark = pol->mark.v & pol->mark.m;
957 unsigned short encap_family = tmpl->encap_family;
958 unsigned int sequence;
959 struct km_event c;
961 to_put = NULL;
963 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
965 rcu_read_lock();
966 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
967 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
968 if (x->props.family == encap_family &&
969 x->props.reqid == tmpl->reqid &&
970 (mark & x->mark.m) == x->mark.v &&
971 x->if_id == if_id &&
972 !(x->props.flags & XFRM_STATE_WILDRECV) &&
973 xfrm_state_addr_check(x, daddr, saddr, 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);
980 if (best || acquire_in_progress)
981 goto found;
983 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
984 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
985 if (x->props.family == encap_family &&
986 x->props.reqid == tmpl->reqid &&
987 (mark & x->mark.m) == x->mark.v &&
988 x->if_id == if_id &&
989 !(x->props.flags & XFRM_STATE_WILDRECV) &&
990 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
991 tmpl->mode == x->props.mode &&
992 tmpl->id.proto == x->id.proto &&
993 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
994 xfrm_state_look_at(pol, x, fl, encap_family,
995 &best, &acquire_in_progress, &error);
998 found:
999 x = best;
1000 if (!x && !error && !acquire_in_progress) {
1001 if (tmpl->id.spi &&
1002 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
1003 tmpl->id.proto, encap_family)) != NULL) {
1004 to_put = x0;
1005 error = -EEXIST;
1006 goto out;
1009 c.net = net;
1010 /* If the KMs have no listeners (yet...), avoid allocating an SA
1011 * for each and every packet - garbage collection might not
1012 * handle the flood.
1014 if (!km_is_alive(&c)) {
1015 error = -ESRCH;
1016 goto out;
1019 x = xfrm_state_alloc(net);
1020 if (x == NULL) {
1021 error = -ENOMEM;
1022 goto out;
1024 /* Initialize temporary state matching only
1025 * to current session. */
1026 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1027 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1028 x->if_id = if_id;
1030 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1031 if (error) {
1032 x->km.state = XFRM_STATE_DEAD;
1033 to_put = x;
1034 x = NULL;
1035 goto out;
1038 if (km_query(x, tmpl, pol) == 0) {
1039 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1040 x->km.state = XFRM_STATE_ACQ;
1041 list_add(&x->km.all, &net->xfrm.state_all);
1042 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1043 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1044 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1045 if (x->id.spi) {
1046 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1047 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1049 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1050 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1051 net->xfrm.state_num++;
1052 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1053 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1054 } else {
1055 x->km.state = XFRM_STATE_DEAD;
1056 to_put = x;
1057 x = NULL;
1058 error = -ESRCH;
1061 out:
1062 if (x) {
1063 if (!xfrm_state_hold_rcu(x)) {
1064 *err = -EAGAIN;
1065 x = NULL;
1067 } else {
1068 *err = acquire_in_progress ? -EAGAIN : error;
1070 rcu_read_unlock();
1071 if (to_put)
1072 xfrm_state_put(to_put);
1074 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1075 *err = -EAGAIN;
1076 if (x) {
1077 xfrm_state_put(x);
1078 x = NULL;
1082 return x;
1085 struct xfrm_state *
1086 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1087 xfrm_address_t *daddr, xfrm_address_t *saddr,
1088 unsigned short family, u8 mode, u8 proto, u32 reqid)
1090 unsigned int h;
1091 struct xfrm_state *rx = NULL, *x = NULL;
1093 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1094 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1095 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1096 if (x->props.family == family &&
1097 x->props.reqid == reqid &&
1098 (mark & x->mark.m) == x->mark.v &&
1099 x->if_id == if_id &&
1100 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1101 xfrm_state_addr_check(x, daddr, saddr, family) &&
1102 mode == x->props.mode &&
1103 proto == x->id.proto &&
1104 x->km.state == XFRM_STATE_VALID) {
1105 rx = x;
1106 break;
1110 if (rx)
1111 xfrm_state_hold(rx);
1112 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1115 return rx;
1117 EXPORT_SYMBOL(xfrm_stateonly_find);
1119 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1120 unsigned short family)
1122 struct xfrm_state *x;
1123 struct xfrm_state_walk *w;
1125 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1126 list_for_each_entry(w, &net->xfrm.state_all, all) {
1127 x = container_of(w, struct xfrm_state, km);
1128 if (x->props.family != family ||
1129 x->id.spi != spi)
1130 continue;
1132 xfrm_state_hold(x);
1133 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1134 return x;
1136 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1137 return NULL;
1139 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1141 static void __xfrm_state_insert(struct xfrm_state *x)
1143 struct net *net = xs_net(x);
1144 unsigned int h;
1146 list_add(&x->km.all, &net->xfrm.state_all);
1148 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1149 x->props.reqid, x->props.family);
1150 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1152 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1153 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1155 if (x->id.spi) {
1156 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1157 x->props.family);
1159 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1162 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1163 if (x->replay_maxage)
1164 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1166 net->xfrm.state_num++;
1168 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1171 /* net->xfrm.xfrm_state_lock is held */
1172 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1174 struct net *net = xs_net(xnew);
1175 unsigned short family = xnew->props.family;
1176 u32 reqid = xnew->props.reqid;
1177 struct xfrm_state *x;
1178 unsigned int h;
1179 u32 mark = xnew->mark.v & xnew->mark.m;
1180 u32 if_id = xnew->if_id;
1182 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1183 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1184 if (x->props.family == family &&
1185 x->props.reqid == reqid &&
1186 x->if_id == if_id &&
1187 (mark & x->mark.m) == x->mark.v &&
1188 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1189 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1190 x->genid++;
1194 void xfrm_state_insert(struct xfrm_state *x)
1196 struct net *net = xs_net(x);
1198 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1199 __xfrm_state_bump_genids(x);
1200 __xfrm_state_insert(x);
1201 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1203 EXPORT_SYMBOL(xfrm_state_insert);
1205 /* net->xfrm.xfrm_state_lock is held */
1206 static struct xfrm_state *__find_acq_core(struct net *net,
1207 const struct xfrm_mark *m,
1208 unsigned short family, u8 mode,
1209 u32 reqid, u32 if_id, u8 proto,
1210 const xfrm_address_t *daddr,
1211 const xfrm_address_t *saddr,
1212 int create)
1214 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1215 struct xfrm_state *x;
1216 u32 mark = m->v & m->m;
1218 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1219 if (x->props.reqid != reqid ||
1220 x->props.mode != mode ||
1221 x->props.family != family ||
1222 x->km.state != XFRM_STATE_ACQ ||
1223 x->id.spi != 0 ||
1224 x->id.proto != proto ||
1225 (mark & x->mark.m) != x->mark.v ||
1226 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1227 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1228 continue;
1230 xfrm_state_hold(x);
1231 return x;
1234 if (!create)
1235 return NULL;
1237 x = xfrm_state_alloc(net);
1238 if (likely(x)) {
1239 switch (family) {
1240 case AF_INET:
1241 x->sel.daddr.a4 = daddr->a4;
1242 x->sel.saddr.a4 = saddr->a4;
1243 x->sel.prefixlen_d = 32;
1244 x->sel.prefixlen_s = 32;
1245 x->props.saddr.a4 = saddr->a4;
1246 x->id.daddr.a4 = daddr->a4;
1247 break;
1249 case AF_INET6:
1250 x->sel.daddr.in6 = daddr->in6;
1251 x->sel.saddr.in6 = saddr->in6;
1252 x->sel.prefixlen_d = 128;
1253 x->sel.prefixlen_s = 128;
1254 x->props.saddr.in6 = saddr->in6;
1255 x->id.daddr.in6 = daddr->in6;
1256 break;
1259 x->km.state = XFRM_STATE_ACQ;
1260 x->id.proto = proto;
1261 x->props.family = family;
1262 x->props.mode = mode;
1263 x->props.reqid = reqid;
1264 x->if_id = if_id;
1265 x->mark.v = m->v;
1266 x->mark.m = m->m;
1267 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1268 xfrm_state_hold(x);
1269 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1270 list_add(&x->km.all, &net->xfrm.state_all);
1271 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1272 h = xfrm_src_hash(net, daddr, saddr, family);
1273 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1275 net->xfrm.state_num++;
1277 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1280 return x;
1283 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1285 int xfrm_state_add(struct xfrm_state *x)
1287 struct net *net = xs_net(x);
1288 struct xfrm_state *x1, *to_put;
1289 int family;
1290 int err;
1291 u32 mark = x->mark.v & x->mark.m;
1292 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1294 family = x->props.family;
1296 to_put = NULL;
1298 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1300 x1 = __xfrm_state_locate(x, use_spi, family);
1301 if (x1) {
1302 to_put = x1;
1303 x1 = NULL;
1304 err = -EEXIST;
1305 goto out;
1308 if (use_spi && x->km.seq) {
1309 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1310 if (x1 && ((x1->id.proto != x->id.proto) ||
1311 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1312 to_put = x1;
1313 x1 = NULL;
1317 if (use_spi && !x1)
1318 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1319 x->props.reqid, x->if_id, x->id.proto,
1320 &x->id.daddr, &x->props.saddr, 0);
1322 __xfrm_state_bump_genids(x);
1323 __xfrm_state_insert(x);
1324 err = 0;
1326 out:
1327 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1329 if (x1) {
1330 xfrm_state_delete(x1);
1331 xfrm_state_put(x1);
1334 if (to_put)
1335 xfrm_state_put(to_put);
1337 return err;
1339 EXPORT_SYMBOL(xfrm_state_add);
1341 #ifdef CONFIG_XFRM_MIGRATE
1342 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1343 struct xfrm_encap_tmpl *encap)
1345 struct net *net = xs_net(orig);
1346 struct xfrm_state *x = xfrm_state_alloc(net);
1347 if (!x)
1348 goto out;
1350 memcpy(&x->id, &orig->id, sizeof(x->id));
1351 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1352 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1353 x->props.mode = orig->props.mode;
1354 x->props.replay_window = orig->props.replay_window;
1355 x->props.reqid = orig->props.reqid;
1356 x->props.family = orig->props.family;
1357 x->props.saddr = orig->props.saddr;
1359 if (orig->aalg) {
1360 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1361 if (!x->aalg)
1362 goto error;
1364 x->props.aalgo = orig->props.aalgo;
1366 if (orig->aead) {
1367 x->aead = xfrm_algo_aead_clone(orig->aead);
1368 x->geniv = orig->geniv;
1369 if (!x->aead)
1370 goto error;
1372 if (orig->ealg) {
1373 x->ealg = xfrm_algo_clone(orig->ealg);
1374 if (!x->ealg)
1375 goto error;
1377 x->props.ealgo = orig->props.ealgo;
1379 if (orig->calg) {
1380 x->calg = xfrm_algo_clone(orig->calg);
1381 if (!x->calg)
1382 goto error;
1384 x->props.calgo = orig->props.calgo;
1386 if (encap || orig->encap) {
1387 if (encap)
1388 x->encap = kmemdup(encap, sizeof(*x->encap),
1389 GFP_KERNEL);
1390 else
1391 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1392 GFP_KERNEL);
1394 if (!x->encap)
1395 goto error;
1398 if (orig->coaddr) {
1399 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1400 GFP_KERNEL);
1401 if (!x->coaddr)
1402 goto error;
1405 if (orig->replay_esn) {
1406 if (xfrm_replay_clone(x, orig))
1407 goto error;
1410 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1412 if (xfrm_init_state(x) < 0)
1413 goto error;
1415 x->props.flags = orig->props.flags;
1416 x->props.extra_flags = orig->props.extra_flags;
1418 x->if_id = orig->if_id;
1419 x->tfcpad = orig->tfcpad;
1420 x->replay_maxdiff = orig->replay_maxdiff;
1421 x->replay_maxage = orig->replay_maxage;
1422 x->curlft.add_time = orig->curlft.add_time;
1423 x->km.state = orig->km.state;
1424 x->km.seq = orig->km.seq;
1425 x->replay = orig->replay;
1426 x->preplay = orig->preplay;
1428 return x;
1430 error:
1431 xfrm_state_put(x);
1432 out:
1433 return NULL;
1436 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1438 unsigned int h;
1439 struct xfrm_state *x = NULL;
1441 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1443 if (m->reqid) {
1444 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1445 m->reqid, m->old_family);
1446 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1447 if (x->props.mode != m->mode ||
1448 x->id.proto != m->proto)
1449 continue;
1450 if (m->reqid && x->props.reqid != m->reqid)
1451 continue;
1452 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1453 m->old_family) ||
1454 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1455 m->old_family))
1456 continue;
1457 xfrm_state_hold(x);
1458 break;
1460 } else {
1461 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1462 m->old_family);
1463 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1464 if (x->props.mode != m->mode ||
1465 x->id.proto != m->proto)
1466 continue;
1467 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1468 m->old_family) ||
1469 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1470 m->old_family))
1471 continue;
1472 xfrm_state_hold(x);
1473 break;
1477 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1479 return x;
1481 EXPORT_SYMBOL(xfrm_migrate_state_find);
1483 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1484 struct xfrm_migrate *m,
1485 struct xfrm_encap_tmpl *encap)
1487 struct xfrm_state *xc;
1489 xc = xfrm_state_clone(x, encap);
1490 if (!xc)
1491 return NULL;
1493 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1494 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1496 /* add state */
1497 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1498 /* a care is needed when the destination address of the
1499 state is to be updated as it is a part of triplet */
1500 xfrm_state_insert(xc);
1501 } else {
1502 if (xfrm_state_add(xc) < 0)
1503 goto error;
1506 return xc;
1507 error:
1508 xfrm_state_put(xc);
1509 return NULL;
1511 EXPORT_SYMBOL(xfrm_state_migrate);
1512 #endif
1514 int xfrm_state_update(struct xfrm_state *x)
1516 struct xfrm_state *x1, *to_put;
1517 int err;
1518 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1519 struct net *net = xs_net(x);
1521 to_put = NULL;
1523 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1524 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1526 err = -ESRCH;
1527 if (!x1)
1528 goto out;
1530 if (xfrm_state_kern(x1)) {
1531 to_put = x1;
1532 err = -EEXIST;
1533 goto out;
1536 if (x1->km.state == XFRM_STATE_ACQ) {
1537 __xfrm_state_insert(x);
1538 x = NULL;
1540 err = 0;
1542 out:
1543 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1545 if (to_put)
1546 xfrm_state_put(to_put);
1548 if (err)
1549 return err;
1551 if (!x) {
1552 xfrm_state_delete(x1);
1553 xfrm_state_put(x1);
1554 return 0;
1557 err = -EINVAL;
1558 spin_lock_bh(&x1->lock);
1559 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1560 if (x->encap && x1->encap &&
1561 x->encap->encap_type == x1->encap->encap_type)
1562 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1563 else if (x->encap || x1->encap)
1564 goto fail;
1566 if (x->coaddr && x1->coaddr) {
1567 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1569 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1570 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1571 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1572 x1->km.dying = 0;
1574 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1575 if (x1->curlft.use_time)
1576 xfrm_state_check_expire(x1);
1578 if (x->props.smark.m || x->props.smark.v || x->if_id) {
1579 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1581 if (x->props.smark.m || x->props.smark.v)
1582 x1->props.smark = x->props.smark;
1584 if (x->if_id)
1585 x1->if_id = x->if_id;
1587 __xfrm_state_bump_genids(x1);
1588 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1591 err = 0;
1592 x->km.state = XFRM_STATE_DEAD;
1593 __xfrm_state_put(x);
1596 fail:
1597 spin_unlock_bh(&x1->lock);
1599 xfrm_state_put(x1);
1601 return err;
1603 EXPORT_SYMBOL(xfrm_state_update);
1605 int xfrm_state_check_expire(struct xfrm_state *x)
1607 if (!x->curlft.use_time)
1608 x->curlft.use_time = ktime_get_real_seconds();
1610 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1611 x->curlft.packets >= x->lft.hard_packet_limit) {
1612 x->km.state = XFRM_STATE_EXPIRED;
1613 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
1614 return -EINVAL;
1617 if (!x->km.dying &&
1618 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1619 x->curlft.packets >= x->lft.soft_packet_limit)) {
1620 x->km.dying = 1;
1621 km_state_expired(x, 0, 0);
1623 return 0;
1625 EXPORT_SYMBOL(xfrm_state_check_expire);
1627 struct xfrm_state *
1628 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1629 u8 proto, unsigned short family)
1631 struct xfrm_state *x;
1633 rcu_read_lock();
1634 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1635 rcu_read_unlock();
1636 return x;
1638 EXPORT_SYMBOL(xfrm_state_lookup);
1640 struct xfrm_state *
1641 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1642 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1643 u8 proto, unsigned short family)
1645 struct xfrm_state *x;
1647 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1648 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1649 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1650 return x;
1652 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1654 struct xfrm_state *
1655 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1656 u32 if_id, u8 proto, const xfrm_address_t *daddr,
1657 const xfrm_address_t *saddr, int create, unsigned short family)
1659 struct xfrm_state *x;
1661 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1662 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
1663 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1665 return x;
1667 EXPORT_SYMBOL(xfrm_find_acq);
1669 #ifdef CONFIG_XFRM_SUB_POLICY
1671 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1672 unsigned short family, struct net *net)
1674 int i;
1675 int err = 0;
1676 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1677 if (!afinfo)
1678 return -EAFNOSUPPORT;
1680 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
1681 if (afinfo->tmpl_sort)
1682 err = afinfo->tmpl_sort(dst, src, n);
1683 else
1684 for (i = 0; i < n; i++)
1685 dst[i] = src[i];
1686 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1687 rcu_read_unlock();
1688 return err;
1690 EXPORT_SYMBOL(xfrm_tmpl_sort);
1693 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1694 unsigned short family)
1696 int i;
1697 int err = 0;
1698 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1699 struct net *net = xs_net(*src);
1701 if (!afinfo)
1702 return -EAFNOSUPPORT;
1704 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1705 if (afinfo->state_sort)
1706 err = afinfo->state_sort(dst, src, n);
1707 else
1708 for (i = 0; i < n; i++)
1709 dst[i] = src[i];
1710 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1711 rcu_read_unlock();
1712 return err;
1714 EXPORT_SYMBOL(xfrm_state_sort);
1715 #endif
1717 /* Silly enough, but I'm lazy to build resolution list */
1719 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1721 int i;
1723 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1724 struct xfrm_state *x;
1726 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1727 if (x->km.seq == seq &&
1728 (mark & x->mark.m) == x->mark.v &&
1729 x->km.state == XFRM_STATE_ACQ) {
1730 xfrm_state_hold(x);
1731 return x;
1735 return NULL;
1738 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1740 struct xfrm_state *x;
1742 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1743 x = __xfrm_find_acq_byseq(net, mark, seq);
1744 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1745 return x;
1747 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1749 u32 xfrm_get_acqseq(void)
1751 u32 res;
1752 static atomic_t acqseq;
1754 do {
1755 res = atomic_inc_return(&acqseq);
1756 } while (!res);
1758 return res;
1760 EXPORT_SYMBOL(xfrm_get_acqseq);
1762 int verify_spi_info(u8 proto, u32 min, u32 max)
1764 switch (proto) {
1765 case IPPROTO_AH:
1766 case IPPROTO_ESP:
1767 break;
1769 case IPPROTO_COMP:
1770 /* IPCOMP spi is 16-bits. */
1771 if (max >= 0x10000)
1772 return -EINVAL;
1773 break;
1775 default:
1776 return -EINVAL;
1779 if (min > max)
1780 return -EINVAL;
1782 return 0;
1784 EXPORT_SYMBOL(verify_spi_info);
1786 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1788 struct net *net = xs_net(x);
1789 unsigned int h;
1790 struct xfrm_state *x0;
1791 int err = -ENOENT;
1792 __be32 minspi = htonl(low);
1793 __be32 maxspi = htonl(high);
1794 u32 mark = x->mark.v & x->mark.m;
1796 spin_lock_bh(&x->lock);
1797 if (x->km.state == XFRM_STATE_DEAD)
1798 goto unlock;
1800 err = 0;
1801 if (x->id.spi)
1802 goto unlock;
1804 err = -ENOENT;
1806 if (minspi == maxspi) {
1807 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1808 if (x0) {
1809 xfrm_state_put(x0);
1810 goto unlock;
1812 x->id.spi = minspi;
1813 } else {
1814 u32 spi = 0;
1815 for (h = 0; h < high-low+1; h++) {
1816 spi = low + prandom_u32()%(high-low+1);
1817 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1818 if (x0 == NULL) {
1819 x->id.spi = htonl(spi);
1820 break;
1822 xfrm_state_put(x0);
1825 if (x->id.spi) {
1826 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1827 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1828 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1829 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1831 err = 0;
1834 unlock:
1835 spin_unlock_bh(&x->lock);
1837 return err;
1839 EXPORT_SYMBOL(xfrm_alloc_spi);
1841 static bool __xfrm_state_filter_match(struct xfrm_state *x,
1842 struct xfrm_address_filter *filter)
1844 if (filter) {
1845 if ((filter->family == AF_INET ||
1846 filter->family == AF_INET6) &&
1847 x->props.family != filter->family)
1848 return false;
1850 return addr_match(&x->props.saddr, &filter->saddr,
1851 filter->splen) &&
1852 addr_match(&x->id.daddr, &filter->daddr,
1853 filter->dplen);
1855 return true;
1858 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1859 int (*func)(struct xfrm_state *, int, void*),
1860 void *data)
1862 struct xfrm_state *state;
1863 struct xfrm_state_walk *x;
1864 int err = 0;
1866 if (walk->seq != 0 && list_empty(&walk->all))
1867 return 0;
1869 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1870 if (list_empty(&walk->all))
1871 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1872 else
1873 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
1874 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1875 if (x->state == XFRM_STATE_DEAD)
1876 continue;
1877 state = container_of(x, struct xfrm_state, km);
1878 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1879 continue;
1880 if (!__xfrm_state_filter_match(state, walk->filter))
1881 continue;
1882 err = func(state, walk->seq, data);
1883 if (err) {
1884 list_move_tail(&walk->all, &x->all);
1885 goto out;
1887 walk->seq++;
1889 if (walk->seq == 0) {
1890 err = -ENOENT;
1891 goto out;
1893 list_del_init(&walk->all);
1894 out:
1895 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1896 return err;
1898 EXPORT_SYMBOL(xfrm_state_walk);
1900 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1901 struct xfrm_address_filter *filter)
1903 INIT_LIST_HEAD(&walk->all);
1904 walk->proto = proto;
1905 walk->state = XFRM_STATE_DEAD;
1906 walk->seq = 0;
1907 walk->filter = filter;
1909 EXPORT_SYMBOL(xfrm_state_walk_init);
1911 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1913 kfree(walk->filter);
1915 if (list_empty(&walk->all))
1916 return;
1918 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1919 list_del(&walk->all);
1920 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1922 EXPORT_SYMBOL(xfrm_state_walk_done);
1924 static void xfrm_replay_timer_handler(struct timer_list *t)
1926 struct xfrm_state *x = from_timer(x, t, rtimer);
1928 spin_lock(&x->lock);
1930 if (x->km.state == XFRM_STATE_VALID) {
1931 if (xfrm_aevent_is_on(xs_net(x)))
1932 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1933 else
1934 x->xflags |= XFRM_TIME_DEFER;
1937 spin_unlock(&x->lock);
1940 static LIST_HEAD(xfrm_km_list);
1942 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1944 struct xfrm_mgr *km;
1946 rcu_read_lock();
1947 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1948 if (km->notify_policy)
1949 km->notify_policy(xp, dir, c);
1950 rcu_read_unlock();
1953 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1955 struct xfrm_mgr *km;
1956 rcu_read_lock();
1957 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1958 if (km->notify)
1959 km->notify(x, c);
1960 rcu_read_unlock();
1963 EXPORT_SYMBOL(km_policy_notify);
1964 EXPORT_SYMBOL(km_state_notify);
1966 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1968 struct km_event c;
1970 c.data.hard = hard;
1971 c.portid = portid;
1972 c.event = XFRM_MSG_EXPIRE;
1973 km_state_notify(x, &c);
1976 EXPORT_SYMBOL(km_state_expired);
1978 * We send to all registered managers regardless of failure
1979 * We are happy with one success
1981 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1983 int err = -EINVAL, acqret;
1984 struct xfrm_mgr *km;
1986 rcu_read_lock();
1987 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1988 acqret = km->acquire(x, t, pol);
1989 if (!acqret)
1990 err = acqret;
1992 rcu_read_unlock();
1993 return err;
1995 EXPORT_SYMBOL(km_query);
1997 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1999 int err = -EINVAL;
2000 struct xfrm_mgr *km;
2002 rcu_read_lock();
2003 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2004 if (km->new_mapping)
2005 err = km->new_mapping(x, ipaddr, sport);
2006 if (!err)
2007 break;
2009 rcu_read_unlock();
2010 return err;
2012 EXPORT_SYMBOL(km_new_mapping);
2014 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2016 struct km_event c;
2018 c.data.hard = hard;
2019 c.portid = portid;
2020 c.event = XFRM_MSG_POLEXPIRE;
2021 km_policy_notify(pol, dir, &c);
2023 EXPORT_SYMBOL(km_policy_expired);
2025 #ifdef CONFIG_XFRM_MIGRATE
2026 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2027 const struct xfrm_migrate *m, int num_migrate,
2028 const struct xfrm_kmaddress *k,
2029 const struct xfrm_encap_tmpl *encap)
2031 int err = -EINVAL;
2032 int ret;
2033 struct xfrm_mgr *km;
2035 rcu_read_lock();
2036 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2037 if (km->migrate) {
2038 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2039 encap);
2040 if (!ret)
2041 err = ret;
2044 rcu_read_unlock();
2045 return err;
2047 EXPORT_SYMBOL(km_migrate);
2048 #endif
2050 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2052 int err = -EINVAL;
2053 int ret;
2054 struct xfrm_mgr *km;
2056 rcu_read_lock();
2057 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2058 if (km->report) {
2059 ret = km->report(net, proto, sel, addr);
2060 if (!ret)
2061 err = ret;
2064 rcu_read_unlock();
2065 return err;
2067 EXPORT_SYMBOL(km_report);
2069 bool km_is_alive(const struct km_event *c)
2071 struct xfrm_mgr *km;
2072 bool is_alive = false;
2074 rcu_read_lock();
2075 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2076 if (km->is_alive && km->is_alive(c)) {
2077 is_alive = true;
2078 break;
2081 rcu_read_unlock();
2083 return is_alive;
2085 EXPORT_SYMBOL(km_is_alive);
2087 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2089 int err;
2090 u8 *data;
2091 struct xfrm_mgr *km;
2092 struct xfrm_policy *pol = NULL;
2094 #ifdef CONFIG_COMPAT
2095 if (in_compat_syscall())
2096 return -EOPNOTSUPP;
2097 #endif
2099 if (!optval && !optlen) {
2100 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2101 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2102 __sk_dst_reset(sk);
2103 return 0;
2106 if (optlen <= 0 || optlen > PAGE_SIZE)
2107 return -EMSGSIZE;
2109 data = memdup_user(optval, optlen);
2110 if (IS_ERR(data))
2111 return PTR_ERR(data);
2113 err = -EINVAL;
2114 rcu_read_lock();
2115 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2116 pol = km->compile_policy(sk, optname, data,
2117 optlen, &err);
2118 if (err >= 0)
2119 break;
2121 rcu_read_unlock();
2123 if (err >= 0) {
2124 xfrm_sk_policy_insert(sk, err, pol);
2125 xfrm_pol_put(pol);
2126 __sk_dst_reset(sk);
2127 err = 0;
2130 kfree(data);
2131 return err;
2133 EXPORT_SYMBOL(xfrm_user_policy);
2135 static DEFINE_SPINLOCK(xfrm_km_lock);
2137 int xfrm_register_km(struct xfrm_mgr *km)
2139 spin_lock_bh(&xfrm_km_lock);
2140 list_add_tail_rcu(&km->list, &xfrm_km_list);
2141 spin_unlock_bh(&xfrm_km_lock);
2142 return 0;
2144 EXPORT_SYMBOL(xfrm_register_km);
2146 int xfrm_unregister_km(struct xfrm_mgr *km)
2148 spin_lock_bh(&xfrm_km_lock);
2149 list_del_rcu(&km->list);
2150 spin_unlock_bh(&xfrm_km_lock);
2151 synchronize_rcu();
2152 return 0;
2154 EXPORT_SYMBOL(xfrm_unregister_km);
2156 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2158 int err = 0;
2160 if (WARN_ON(afinfo->family >= NPROTO))
2161 return -EAFNOSUPPORT;
2163 spin_lock_bh(&xfrm_state_afinfo_lock);
2164 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2165 err = -EEXIST;
2166 else
2167 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2168 spin_unlock_bh(&xfrm_state_afinfo_lock);
2169 return err;
2171 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2173 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2175 int err = 0, family = afinfo->family;
2177 if (WARN_ON(family >= NPROTO))
2178 return -EAFNOSUPPORT;
2180 spin_lock_bh(&xfrm_state_afinfo_lock);
2181 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2182 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2183 err = -EINVAL;
2184 else
2185 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2187 spin_unlock_bh(&xfrm_state_afinfo_lock);
2188 synchronize_rcu();
2189 return err;
2191 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2193 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2195 if (unlikely(family >= NPROTO))
2196 return NULL;
2198 return rcu_dereference(xfrm_state_afinfo[family]);
2201 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2203 struct xfrm_state_afinfo *afinfo;
2204 if (unlikely(family >= NPROTO))
2205 return NULL;
2206 rcu_read_lock();
2207 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2208 if (unlikely(!afinfo))
2209 rcu_read_unlock();
2210 return afinfo;
2213 void xfrm_flush_gc(void)
2215 flush_work(&xfrm_state_gc_work);
2217 EXPORT_SYMBOL(xfrm_flush_gc);
2219 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2220 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2222 if (x->tunnel) {
2223 struct xfrm_state *t = x->tunnel;
2225 if (atomic_read(&t->tunnel_users) == 2)
2226 xfrm_state_delete(t);
2227 atomic_dec(&t->tunnel_users);
2228 xfrm_state_put_sync(t);
2229 x->tunnel = NULL;
2232 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2234 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2236 const struct xfrm_type *type = READ_ONCE(x->type);
2238 if (x->km.state == XFRM_STATE_VALID &&
2239 type && type->get_mtu)
2240 return type->get_mtu(x, mtu);
2242 return mtu - x->props.header_len;
2245 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2247 struct xfrm_state_afinfo *afinfo;
2248 struct xfrm_mode *inner_mode;
2249 int family = x->props.family;
2250 int err;
2252 err = -EAFNOSUPPORT;
2253 afinfo = xfrm_state_get_afinfo(family);
2254 if (!afinfo)
2255 goto error;
2257 err = 0;
2258 if (afinfo->init_flags)
2259 err = afinfo->init_flags(x);
2261 rcu_read_unlock();
2263 if (err)
2264 goto error;
2266 err = -EPROTONOSUPPORT;
2268 if (x->sel.family != AF_UNSPEC) {
2269 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2270 if (inner_mode == NULL)
2271 goto error;
2273 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2274 family != x->sel.family) {
2275 xfrm_put_mode(inner_mode);
2276 goto error;
2279 x->inner_mode = inner_mode;
2280 } else {
2281 struct xfrm_mode *inner_mode_iaf;
2282 int iafamily = AF_INET;
2284 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2285 if (inner_mode == NULL)
2286 goto error;
2288 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2289 xfrm_put_mode(inner_mode);
2290 goto error;
2292 x->inner_mode = inner_mode;
2294 if (x->props.family == AF_INET)
2295 iafamily = AF_INET6;
2297 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2298 if (inner_mode_iaf) {
2299 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2300 x->inner_mode_iaf = inner_mode_iaf;
2301 else
2302 xfrm_put_mode(inner_mode_iaf);
2306 x->type = xfrm_get_type(x->id.proto, family);
2307 if (x->type == NULL)
2308 goto error;
2310 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2312 err = x->type->init_state(x);
2313 if (err)
2314 goto error;
2316 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2317 if (x->outer_mode == NULL) {
2318 err = -EPROTONOSUPPORT;
2319 goto error;
2322 if (init_replay) {
2323 err = xfrm_init_replay(x);
2324 if (err)
2325 goto error;
2328 error:
2329 return err;
2332 EXPORT_SYMBOL(__xfrm_init_state);
2334 int xfrm_init_state(struct xfrm_state *x)
2336 int err;
2338 err = __xfrm_init_state(x, true, false);
2339 if (!err)
2340 x->km.state = XFRM_STATE_VALID;
2342 return err;
2345 EXPORT_SYMBOL(xfrm_init_state);
2347 int __net_init xfrm_state_init(struct net *net)
2349 unsigned int sz;
2351 if (net_eq(net, &init_net))
2352 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2353 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2355 INIT_LIST_HEAD(&net->xfrm.state_all);
2357 sz = sizeof(struct hlist_head) * 8;
2359 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2360 if (!net->xfrm.state_bydst)
2361 goto out_bydst;
2362 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2363 if (!net->xfrm.state_bysrc)
2364 goto out_bysrc;
2365 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2366 if (!net->xfrm.state_byspi)
2367 goto out_byspi;
2368 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2370 net->xfrm.state_num = 0;
2371 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2372 spin_lock_init(&net->xfrm.xfrm_state_lock);
2373 return 0;
2375 out_byspi:
2376 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2377 out_bysrc:
2378 xfrm_hash_free(net->xfrm.state_bydst, sz);
2379 out_bydst:
2380 return -ENOMEM;
2383 void xfrm_state_fini(struct net *net)
2385 unsigned int sz;
2387 flush_work(&net->xfrm.state_hash_work);
2388 flush_work(&xfrm_state_gc_work);
2389 xfrm_state_flush(net, 0, false, true);
2391 WARN_ON(!list_empty(&net->xfrm.state_all));
2393 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2394 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2395 xfrm_hash_free(net->xfrm.state_byspi, sz);
2396 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2397 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2398 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2399 xfrm_hash_free(net->xfrm.state_bydst, sz);
2402 #ifdef CONFIG_AUDITSYSCALL
2403 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2404 struct audit_buffer *audit_buf)
2406 struct xfrm_sec_ctx *ctx = x->security;
2407 u32 spi = ntohl(x->id.spi);
2409 if (ctx)
2410 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2411 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2413 switch (x->props.family) {
2414 case AF_INET:
2415 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2416 &x->props.saddr.a4, &x->id.daddr.a4);
2417 break;
2418 case AF_INET6:
2419 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2420 x->props.saddr.a6, x->id.daddr.a6);
2421 break;
2424 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2427 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2428 struct audit_buffer *audit_buf)
2430 const struct iphdr *iph4;
2431 const struct ipv6hdr *iph6;
2433 switch (family) {
2434 case AF_INET:
2435 iph4 = ip_hdr(skb);
2436 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2437 &iph4->saddr, &iph4->daddr);
2438 break;
2439 case AF_INET6:
2440 iph6 = ipv6_hdr(skb);
2441 audit_log_format(audit_buf,
2442 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2443 &iph6->saddr, &iph6->daddr,
2444 iph6->flow_lbl[0] & 0x0f,
2445 iph6->flow_lbl[1],
2446 iph6->flow_lbl[2]);
2447 break;
2451 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2453 struct audit_buffer *audit_buf;
2455 audit_buf = xfrm_audit_start("SAD-add");
2456 if (audit_buf == NULL)
2457 return;
2458 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2459 xfrm_audit_helper_sainfo(x, audit_buf);
2460 audit_log_format(audit_buf, " res=%u", result);
2461 audit_log_end(audit_buf);
2463 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2465 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2467 struct audit_buffer *audit_buf;
2469 audit_buf = xfrm_audit_start("SAD-delete");
2470 if (audit_buf == NULL)
2471 return;
2472 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2473 xfrm_audit_helper_sainfo(x, audit_buf);
2474 audit_log_format(audit_buf, " res=%u", result);
2475 audit_log_end(audit_buf);
2477 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2479 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2480 struct sk_buff *skb)
2482 struct audit_buffer *audit_buf;
2483 u32 spi;
2485 audit_buf = xfrm_audit_start("SA-replay-overflow");
2486 if (audit_buf == NULL)
2487 return;
2488 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2489 /* don't record the sequence number because it's inherent in this kind
2490 * of audit message */
2491 spi = ntohl(x->id.spi);
2492 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2493 audit_log_end(audit_buf);
2495 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2497 void xfrm_audit_state_replay(struct xfrm_state *x,
2498 struct sk_buff *skb, __be32 net_seq)
2500 struct audit_buffer *audit_buf;
2501 u32 spi;
2503 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2504 if (audit_buf == NULL)
2505 return;
2506 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2507 spi = ntohl(x->id.spi);
2508 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2509 spi, spi, ntohl(net_seq));
2510 audit_log_end(audit_buf);
2512 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2514 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2516 struct audit_buffer *audit_buf;
2518 audit_buf = xfrm_audit_start("SA-notfound");
2519 if (audit_buf == NULL)
2520 return;
2521 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2522 audit_log_end(audit_buf);
2524 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2526 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2527 __be32 net_spi, __be32 net_seq)
2529 struct audit_buffer *audit_buf;
2530 u32 spi;
2532 audit_buf = xfrm_audit_start("SA-notfound");
2533 if (audit_buf == NULL)
2534 return;
2535 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2536 spi = ntohl(net_spi);
2537 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2538 spi, spi, ntohl(net_seq));
2539 audit_log_end(audit_buf);
2541 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2543 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2544 struct sk_buff *skb, u8 proto)
2546 struct audit_buffer *audit_buf;
2547 __be32 net_spi;
2548 __be32 net_seq;
2550 audit_buf = xfrm_audit_start("SA-icv-failure");
2551 if (audit_buf == NULL)
2552 return;
2553 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2554 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2555 u32 spi = ntohl(net_spi);
2556 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2557 spi, spi, ntohl(net_seq));
2559 audit_log_end(audit_buf);
2561 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2562 #endif /* CONFIG_AUDITSYSCALL */