Linux 5.7.7
[linux/fpc-iii.git] / net / xfrm / xfrm_state.c
blob8be2d926acc21d01d7bff7cb3c295b69f5153825
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * xfrm_state.c
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific functions
12 * Derek Atkins <derek@ihtfp.com>
13 * Add UDP Encapsulation
17 #include <linux/workqueue.h>
18 #include <net/xfrm.h>
19 #include <linux/pfkeyv2.h>
20 #include <linux/ipsec.h>
21 #include <linux/module.h>
22 #include <linux/cache.h>
23 #include <linux/audit.h>
24 #include <linux/uaccess.h>
25 #include <linux/ktime.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
30 #include <crypto/aead.h>
32 #include "xfrm_hash.h"
34 #define xfrm_state_deref_prot(table, net) \
35 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
37 static void xfrm_state_gc_task(struct work_struct *work);
39 /* Each xfrm_state may be linked to two tables:
41 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
42 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
43 destination/tunnel endpoint. (output)
46 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
47 static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
48 static struct kmem_cache *xfrm_state_cache __ro_after_init;
50 static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
51 static HLIST_HEAD(xfrm_state_gc_list);
53 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
55 return refcount_inc_not_zero(&x->refcnt);
58 static inline unsigned int xfrm_dst_hash(struct net *net,
59 const xfrm_address_t *daddr,
60 const xfrm_address_t *saddr,
61 u32 reqid,
62 unsigned short family)
64 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
67 static inline unsigned int xfrm_src_hash(struct net *net,
68 const xfrm_address_t *daddr,
69 const xfrm_address_t *saddr,
70 unsigned short family)
72 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
75 static inline unsigned int
76 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
77 __be32 spi, u8 proto, unsigned short family)
79 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
82 static void xfrm_hash_transfer(struct hlist_head *list,
83 struct hlist_head *ndsttable,
84 struct hlist_head *nsrctable,
85 struct hlist_head *nspitable,
86 unsigned int nhashmask)
88 struct hlist_node *tmp;
89 struct xfrm_state *x;
91 hlist_for_each_entry_safe(x, tmp, list, bydst) {
92 unsigned int h;
94 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
95 x->props.reqid, x->props.family,
96 nhashmask);
97 hlist_add_head_rcu(&x->bydst, ndsttable + h);
99 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
100 x->props.family,
101 nhashmask);
102 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
104 if (x->id.spi) {
105 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
106 x->id.proto, x->props.family,
107 nhashmask);
108 hlist_add_head_rcu(&x->byspi, nspitable + h);
113 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
115 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
118 static void xfrm_hash_resize(struct work_struct *work)
120 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
121 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
122 unsigned long nsize, osize;
123 unsigned int nhashmask, ohashmask;
124 int i;
126 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
127 ndst = xfrm_hash_alloc(nsize);
128 if (!ndst)
129 return;
130 nsrc = xfrm_hash_alloc(nsize);
131 if (!nsrc) {
132 xfrm_hash_free(ndst, nsize);
133 return;
135 nspi = xfrm_hash_alloc(nsize);
136 if (!nspi) {
137 xfrm_hash_free(ndst, nsize);
138 xfrm_hash_free(nsrc, nsize);
139 return;
142 spin_lock_bh(&net->xfrm.xfrm_state_lock);
143 write_seqcount_begin(&xfrm_state_hash_generation);
145 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
146 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
147 for (i = net->xfrm.state_hmask; i >= 0; i--)
148 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
150 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
151 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
152 ohashmask = net->xfrm.state_hmask;
154 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
155 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
156 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
157 net->xfrm.state_hmask = nhashmask;
159 write_seqcount_end(&xfrm_state_hash_generation);
160 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
162 osize = (ohashmask + 1) * sizeof(struct hlist_head);
164 synchronize_rcu();
166 xfrm_hash_free(odst, osize);
167 xfrm_hash_free(osrc, osize);
168 xfrm_hash_free(ospi, osize);
171 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
172 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
174 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
176 int __xfrm_state_delete(struct xfrm_state *x);
178 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
179 static bool km_is_alive(const struct km_event *c);
180 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
182 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
184 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
185 int err = 0;
187 if (!afinfo)
188 return -EAFNOSUPPORT;
190 #define X(afi, T, name) do { \
191 WARN_ON((afi)->type_ ## name); \
192 (afi)->type_ ## name = (T); \
193 } while (0)
195 switch (type->proto) {
196 case IPPROTO_COMP:
197 X(afinfo, type, comp);
198 break;
199 case IPPROTO_AH:
200 X(afinfo, type, ah);
201 break;
202 case IPPROTO_ESP:
203 X(afinfo, type, esp);
204 break;
205 case IPPROTO_IPIP:
206 X(afinfo, type, ipip);
207 break;
208 case IPPROTO_DSTOPTS:
209 X(afinfo, type, dstopts);
210 break;
211 case IPPROTO_ROUTING:
212 X(afinfo, type, routing);
213 break;
214 case IPPROTO_IPV6:
215 X(afinfo, type, ipip6);
216 break;
217 default:
218 WARN_ON(1);
219 err = -EPROTONOSUPPORT;
220 break;
222 #undef X
223 rcu_read_unlock();
224 return err;
226 EXPORT_SYMBOL(xfrm_register_type);
228 void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
230 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
232 if (unlikely(afinfo == NULL))
233 return;
235 #define X(afi, T, name) do { \
236 WARN_ON((afi)->type_ ## name != (T)); \
237 (afi)->type_ ## name = NULL; \
238 } while (0)
240 switch (type->proto) {
241 case IPPROTO_COMP:
242 X(afinfo, type, comp);
243 break;
244 case IPPROTO_AH:
245 X(afinfo, type, ah);
246 break;
247 case IPPROTO_ESP:
248 X(afinfo, type, esp);
249 break;
250 case IPPROTO_IPIP:
251 X(afinfo, type, ipip);
252 break;
253 case IPPROTO_DSTOPTS:
254 X(afinfo, type, dstopts);
255 break;
256 case IPPROTO_ROUTING:
257 X(afinfo, type, routing);
258 break;
259 case IPPROTO_IPV6:
260 X(afinfo, type, ipip6);
261 break;
262 default:
263 WARN_ON(1);
264 break;
266 #undef X
267 rcu_read_unlock();
269 EXPORT_SYMBOL(xfrm_unregister_type);
271 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
273 const struct xfrm_type *type = NULL;
274 struct xfrm_state_afinfo *afinfo;
275 int modload_attempted = 0;
277 retry:
278 afinfo = xfrm_state_get_afinfo(family);
279 if (unlikely(afinfo == NULL))
280 return NULL;
282 switch (proto) {
283 case IPPROTO_COMP:
284 type = afinfo->type_comp;
285 break;
286 case IPPROTO_AH:
287 type = afinfo->type_ah;
288 break;
289 case IPPROTO_ESP:
290 type = afinfo->type_esp;
291 break;
292 case IPPROTO_IPIP:
293 type = afinfo->type_ipip;
294 break;
295 case IPPROTO_DSTOPTS:
296 type = afinfo->type_dstopts;
297 break;
298 case IPPROTO_ROUTING:
299 type = afinfo->type_routing;
300 break;
301 case IPPROTO_IPV6:
302 type = afinfo->type_ipip6;
303 break;
304 default:
305 break;
308 if (unlikely(type && !try_module_get(type->owner)))
309 type = NULL;
311 rcu_read_unlock();
313 if (!type && !modload_attempted) {
314 request_module("xfrm-type-%d-%d", family, proto);
315 modload_attempted = 1;
316 goto retry;
319 return type;
322 static void xfrm_put_type(const struct xfrm_type *type)
324 module_put(type->owner);
327 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
328 unsigned short family)
330 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
331 int err = 0;
333 if (unlikely(afinfo == NULL))
334 return -EAFNOSUPPORT;
336 switch (type->proto) {
337 case IPPROTO_ESP:
338 WARN_ON(afinfo->type_offload_esp);
339 afinfo->type_offload_esp = type;
340 break;
341 default:
342 WARN_ON(1);
343 err = -EPROTONOSUPPORT;
344 break;
347 rcu_read_unlock();
348 return err;
350 EXPORT_SYMBOL(xfrm_register_type_offload);
352 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
353 unsigned short family)
355 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
357 if (unlikely(afinfo == NULL))
358 return;
360 switch (type->proto) {
361 case IPPROTO_ESP:
362 WARN_ON(afinfo->type_offload_esp != type);
363 afinfo->type_offload_esp = NULL;
364 break;
365 default:
366 WARN_ON(1);
367 break;
369 rcu_read_unlock();
371 EXPORT_SYMBOL(xfrm_unregister_type_offload);
373 static const struct xfrm_type_offload *
374 xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
376 const struct xfrm_type_offload *type = NULL;
377 struct xfrm_state_afinfo *afinfo;
379 retry:
380 afinfo = xfrm_state_get_afinfo(family);
381 if (unlikely(afinfo == NULL))
382 return NULL;
384 switch (proto) {
385 case IPPROTO_ESP:
386 type = afinfo->type_offload_esp;
387 break;
388 default:
389 break;
392 if ((type && !try_module_get(type->owner)))
393 type = NULL;
395 rcu_read_unlock();
397 if (!type && try_load) {
398 request_module("xfrm-offload-%d-%d", family, proto);
399 try_load = false;
400 goto retry;
403 return type;
406 static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
408 module_put(type->owner);
411 static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
412 [XFRM_MODE_BEET] = {
413 .encap = XFRM_MODE_BEET,
414 .flags = XFRM_MODE_FLAG_TUNNEL,
415 .family = AF_INET,
417 [XFRM_MODE_TRANSPORT] = {
418 .encap = XFRM_MODE_TRANSPORT,
419 .family = AF_INET,
421 [XFRM_MODE_TUNNEL] = {
422 .encap = XFRM_MODE_TUNNEL,
423 .flags = XFRM_MODE_FLAG_TUNNEL,
424 .family = AF_INET,
428 static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
429 [XFRM_MODE_BEET] = {
430 .encap = XFRM_MODE_BEET,
431 .flags = XFRM_MODE_FLAG_TUNNEL,
432 .family = AF_INET6,
434 [XFRM_MODE_ROUTEOPTIMIZATION] = {
435 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
436 .family = AF_INET6,
438 [XFRM_MODE_TRANSPORT] = {
439 .encap = XFRM_MODE_TRANSPORT,
440 .family = AF_INET6,
442 [XFRM_MODE_TUNNEL] = {
443 .encap = XFRM_MODE_TUNNEL,
444 .flags = XFRM_MODE_FLAG_TUNNEL,
445 .family = AF_INET6,
449 static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
451 const struct xfrm_mode *mode;
453 if (unlikely(encap >= XFRM_MODE_MAX))
454 return NULL;
456 switch (family) {
457 case AF_INET:
458 mode = &xfrm4_mode_map[encap];
459 if (mode->family == family)
460 return mode;
461 break;
462 case AF_INET6:
463 mode = &xfrm6_mode_map[encap];
464 if (mode->family == family)
465 return mode;
466 break;
467 default:
468 break;
471 return NULL;
474 void xfrm_state_free(struct xfrm_state *x)
476 kmem_cache_free(xfrm_state_cache, x);
478 EXPORT_SYMBOL(xfrm_state_free);
480 static void ___xfrm_state_destroy(struct xfrm_state *x)
482 hrtimer_cancel(&x->mtimer);
483 del_timer_sync(&x->rtimer);
484 kfree(x->aead);
485 kfree(x->aalg);
486 kfree(x->ealg);
487 kfree(x->calg);
488 kfree(x->encap);
489 kfree(x->coaddr);
490 kfree(x->replay_esn);
491 kfree(x->preplay_esn);
492 if (x->type_offload)
493 xfrm_put_type_offload(x->type_offload);
494 if (x->type) {
495 x->type->destructor(x);
496 xfrm_put_type(x->type);
498 if (x->xfrag.page)
499 put_page(x->xfrag.page);
500 xfrm_dev_state_free(x);
501 security_xfrm_state_free(x);
502 xfrm_state_free(x);
505 static void xfrm_state_gc_task(struct work_struct *work)
507 struct xfrm_state *x;
508 struct hlist_node *tmp;
509 struct hlist_head gc_list;
511 spin_lock_bh(&xfrm_state_gc_lock);
512 hlist_move_list(&xfrm_state_gc_list, &gc_list);
513 spin_unlock_bh(&xfrm_state_gc_lock);
515 synchronize_rcu();
517 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
518 ___xfrm_state_destroy(x);
521 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
523 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
524 enum hrtimer_restart ret = HRTIMER_NORESTART;
525 time64_t now = ktime_get_real_seconds();
526 time64_t next = TIME64_MAX;
527 int warn = 0;
528 int err = 0;
530 spin_lock(&x->lock);
531 if (x->km.state == XFRM_STATE_DEAD)
532 goto out;
533 if (x->km.state == XFRM_STATE_EXPIRED)
534 goto expired;
535 if (x->lft.hard_add_expires_seconds) {
536 long tmo = x->lft.hard_add_expires_seconds +
537 x->curlft.add_time - now;
538 if (tmo <= 0) {
539 if (x->xflags & XFRM_SOFT_EXPIRE) {
540 /* enter hard expire without soft expire first?!
541 * setting a new date could trigger this.
542 * workaround: fix x->curflt.add_time by below:
544 x->curlft.add_time = now - x->saved_tmo - 1;
545 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
546 } else
547 goto expired;
549 if (tmo < next)
550 next = tmo;
552 if (x->lft.hard_use_expires_seconds) {
553 long tmo = x->lft.hard_use_expires_seconds +
554 (x->curlft.use_time ? : now) - now;
555 if (tmo <= 0)
556 goto expired;
557 if (tmo < next)
558 next = tmo;
560 if (x->km.dying)
561 goto resched;
562 if (x->lft.soft_add_expires_seconds) {
563 long tmo = x->lft.soft_add_expires_seconds +
564 x->curlft.add_time - now;
565 if (tmo <= 0) {
566 warn = 1;
567 x->xflags &= ~XFRM_SOFT_EXPIRE;
568 } else if (tmo < next) {
569 next = tmo;
570 x->xflags |= XFRM_SOFT_EXPIRE;
571 x->saved_tmo = tmo;
574 if (x->lft.soft_use_expires_seconds) {
575 long tmo = x->lft.soft_use_expires_seconds +
576 (x->curlft.use_time ? : now) - now;
577 if (tmo <= 0)
578 warn = 1;
579 else if (tmo < next)
580 next = tmo;
583 x->km.dying = warn;
584 if (warn)
585 km_state_expired(x, 0, 0);
586 resched:
587 if (next != TIME64_MAX) {
588 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
589 ret = HRTIMER_RESTART;
592 goto out;
594 expired:
595 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
596 x->km.state = XFRM_STATE_EXPIRED;
598 err = __xfrm_state_delete(x);
599 if (!err)
600 km_state_expired(x, 1, 0);
602 xfrm_audit_state_delete(x, err ? 0 : 1, true);
604 out:
605 spin_unlock(&x->lock);
606 return ret;
609 static void xfrm_replay_timer_handler(struct timer_list *t);
611 struct xfrm_state *xfrm_state_alloc(struct net *net)
613 struct xfrm_state *x;
615 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
617 if (x) {
618 write_pnet(&x->xs_net, net);
619 refcount_set(&x->refcnt, 1);
620 atomic_set(&x->tunnel_users, 0);
621 INIT_LIST_HEAD(&x->km.all);
622 INIT_HLIST_NODE(&x->bydst);
623 INIT_HLIST_NODE(&x->bysrc);
624 INIT_HLIST_NODE(&x->byspi);
625 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
626 x->mtimer.function = xfrm_timer_handler;
627 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
628 x->curlft.add_time = ktime_get_real_seconds();
629 x->lft.soft_byte_limit = XFRM_INF;
630 x->lft.soft_packet_limit = XFRM_INF;
631 x->lft.hard_byte_limit = XFRM_INF;
632 x->lft.hard_packet_limit = XFRM_INF;
633 x->replay_maxage = 0;
634 x->replay_maxdiff = 0;
635 spin_lock_init(&x->lock);
637 return x;
639 EXPORT_SYMBOL(xfrm_state_alloc);
641 void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
643 WARN_ON(x->km.state != XFRM_STATE_DEAD);
645 if (sync) {
646 synchronize_rcu();
647 ___xfrm_state_destroy(x);
648 } else {
649 spin_lock_bh(&xfrm_state_gc_lock);
650 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
651 spin_unlock_bh(&xfrm_state_gc_lock);
652 schedule_work(&xfrm_state_gc_work);
655 EXPORT_SYMBOL(__xfrm_state_destroy);
657 int __xfrm_state_delete(struct xfrm_state *x)
659 struct net *net = xs_net(x);
660 int err = -ESRCH;
662 if (x->km.state != XFRM_STATE_DEAD) {
663 x->km.state = XFRM_STATE_DEAD;
664 spin_lock(&net->xfrm.xfrm_state_lock);
665 list_del(&x->km.all);
666 hlist_del_rcu(&x->bydst);
667 hlist_del_rcu(&x->bysrc);
668 if (x->id.spi)
669 hlist_del_rcu(&x->byspi);
670 net->xfrm.state_num--;
671 spin_unlock(&net->xfrm.xfrm_state_lock);
673 if (x->encap_sk)
674 sock_put(rcu_dereference_raw(x->encap_sk));
676 xfrm_dev_state_delete(x);
678 /* All xfrm_state objects are created by xfrm_state_alloc.
679 * The xfrm_state_alloc call gives a reference, and that
680 * is what we are dropping here.
682 xfrm_state_put(x);
683 err = 0;
686 return err;
688 EXPORT_SYMBOL(__xfrm_state_delete);
690 int xfrm_state_delete(struct xfrm_state *x)
692 int err;
694 spin_lock_bh(&x->lock);
695 err = __xfrm_state_delete(x);
696 spin_unlock_bh(&x->lock);
698 return err;
700 EXPORT_SYMBOL(xfrm_state_delete);
702 #ifdef CONFIG_SECURITY_NETWORK_XFRM
703 static inline int
704 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
706 int i, err = 0;
708 for (i = 0; i <= net->xfrm.state_hmask; i++) {
709 struct xfrm_state *x;
711 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
712 if (xfrm_id_proto_match(x->id.proto, proto) &&
713 (err = security_xfrm_state_delete(x)) != 0) {
714 xfrm_audit_state_delete(x, 0, task_valid);
715 return err;
720 return err;
723 static inline int
724 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
726 int i, err = 0;
728 for (i = 0; i <= net->xfrm.state_hmask; i++) {
729 struct xfrm_state *x;
730 struct xfrm_state_offload *xso;
732 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
733 xso = &x->xso;
735 if (xso->dev == dev &&
736 (err = security_xfrm_state_delete(x)) != 0) {
737 xfrm_audit_state_delete(x, 0, task_valid);
738 return err;
743 return err;
745 #else
746 static inline int
747 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
749 return 0;
752 static inline int
753 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
755 return 0;
757 #endif
759 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
761 int i, err = 0, cnt = 0;
763 spin_lock_bh(&net->xfrm.xfrm_state_lock);
764 err = xfrm_state_flush_secctx_check(net, proto, 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 restart:
772 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
773 if (!xfrm_state_kern(x) &&
774 xfrm_id_proto_match(x->id.proto, proto)) {
775 xfrm_state_hold(x);
776 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
778 err = xfrm_state_delete(x);
779 xfrm_audit_state_delete(x, err ? 0 : 1,
780 task_valid);
781 if (sync)
782 xfrm_state_put_sync(x);
783 else
784 xfrm_state_put(x);
785 if (!err)
786 cnt++;
788 spin_lock_bh(&net->xfrm.xfrm_state_lock);
789 goto restart;
793 out:
794 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
795 if (cnt)
796 err = 0;
798 return err;
800 EXPORT_SYMBOL(xfrm_state_flush);
802 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
804 int i, err = 0, cnt = 0;
806 spin_lock_bh(&net->xfrm.xfrm_state_lock);
807 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
808 if (err)
809 goto out;
811 err = -ESRCH;
812 for (i = 0; i <= net->xfrm.state_hmask; i++) {
813 struct xfrm_state *x;
814 struct xfrm_state_offload *xso;
815 restart:
816 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
817 xso = &x->xso;
819 if (!xfrm_state_kern(x) && xso->dev == dev) {
820 xfrm_state_hold(x);
821 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
823 err = xfrm_state_delete(x);
824 xfrm_audit_state_delete(x, err ? 0 : 1,
825 task_valid);
826 xfrm_state_put(x);
827 if (!err)
828 cnt++;
830 spin_lock_bh(&net->xfrm.xfrm_state_lock);
831 goto restart;
835 if (cnt)
836 err = 0;
838 out:
839 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
840 return err;
842 EXPORT_SYMBOL(xfrm_dev_state_flush);
844 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
846 spin_lock_bh(&net->xfrm.xfrm_state_lock);
847 si->sadcnt = net->xfrm.state_num;
848 si->sadhcnt = net->xfrm.state_hmask + 1;
849 si->sadhmcnt = xfrm_state_hashmax;
850 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
852 EXPORT_SYMBOL(xfrm_sad_getinfo);
854 static void
855 __xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
857 const struct flowi4 *fl4 = &fl->u.ip4;
859 sel->daddr.a4 = fl4->daddr;
860 sel->saddr.a4 = fl4->saddr;
861 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
862 sel->dport_mask = htons(0xffff);
863 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
864 sel->sport_mask = htons(0xffff);
865 sel->family = AF_INET;
866 sel->prefixlen_d = 32;
867 sel->prefixlen_s = 32;
868 sel->proto = fl4->flowi4_proto;
869 sel->ifindex = fl4->flowi4_oif;
872 static void
873 __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
875 const struct flowi6 *fl6 = &fl->u.ip6;
877 /* Initialize temporary selector matching only to current session. */
878 *(struct in6_addr *)&sel->daddr = fl6->daddr;
879 *(struct in6_addr *)&sel->saddr = fl6->saddr;
880 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
881 sel->dport_mask = htons(0xffff);
882 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
883 sel->sport_mask = htons(0xffff);
884 sel->family = AF_INET6;
885 sel->prefixlen_d = 128;
886 sel->prefixlen_s = 128;
887 sel->proto = fl6->flowi6_proto;
888 sel->ifindex = fl6->flowi6_oif;
891 static void
892 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
893 const struct xfrm_tmpl *tmpl,
894 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
895 unsigned short family)
897 switch (family) {
898 case AF_INET:
899 __xfrm4_init_tempsel(&x->sel, fl);
900 break;
901 case AF_INET6:
902 __xfrm6_init_tempsel(&x->sel, fl);
903 break;
906 x->id = tmpl->id;
908 switch (tmpl->encap_family) {
909 case AF_INET:
910 if (x->id.daddr.a4 == 0)
911 x->id.daddr.a4 = daddr->a4;
912 x->props.saddr = tmpl->saddr;
913 if (x->props.saddr.a4 == 0)
914 x->props.saddr.a4 = saddr->a4;
915 break;
916 case AF_INET6:
917 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
918 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
919 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
920 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
921 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
922 break;
925 x->props.mode = tmpl->mode;
926 x->props.reqid = tmpl->reqid;
927 x->props.family = tmpl->encap_family;
930 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
931 const xfrm_address_t *daddr,
932 __be32 spi, u8 proto,
933 unsigned short family)
935 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
936 struct xfrm_state *x;
938 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
939 if (x->props.family != family ||
940 x->id.spi != spi ||
941 x->id.proto != proto ||
942 !xfrm_addr_equal(&x->id.daddr, daddr, family))
943 continue;
945 if ((mark & x->mark.m) != x->mark.v)
946 continue;
947 if (!xfrm_state_hold_rcu(x))
948 continue;
949 return x;
952 return NULL;
955 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
956 const xfrm_address_t *daddr,
957 const xfrm_address_t *saddr,
958 u8 proto, unsigned short family)
960 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
961 struct xfrm_state *x;
963 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
964 if (x->props.family != family ||
965 x->id.proto != proto ||
966 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
967 !xfrm_addr_equal(&x->props.saddr, saddr, family))
968 continue;
970 if ((mark & x->mark.m) != x->mark.v)
971 continue;
972 if (!xfrm_state_hold_rcu(x))
973 continue;
974 return x;
977 return NULL;
980 static inline struct xfrm_state *
981 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
983 struct net *net = xs_net(x);
984 u32 mark = x->mark.v & x->mark.m;
986 if (use_spi)
987 return __xfrm_state_lookup(net, mark, &x->id.daddr,
988 x->id.spi, x->id.proto, family);
989 else
990 return __xfrm_state_lookup_byaddr(net, mark,
991 &x->id.daddr,
992 &x->props.saddr,
993 x->id.proto, family);
996 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
998 if (have_hash_collision &&
999 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
1000 net->xfrm.state_num > net->xfrm.state_hmask)
1001 schedule_work(&net->xfrm.state_hash_work);
1004 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
1005 const struct flowi *fl, unsigned short family,
1006 struct xfrm_state **best, int *acq_in_progress,
1007 int *error)
1009 /* Resolution logic:
1010 * 1. There is a valid state with matching selector. Done.
1011 * 2. Valid state with inappropriate selector. Skip.
1013 * Entering area of "sysdeps".
1015 * 3. If state is not valid, selector is temporary, it selects
1016 * only session which triggered previous resolution. Key
1017 * manager will do something to install a state with proper
1018 * selector.
1020 if (x->km.state == XFRM_STATE_VALID) {
1021 if ((x->sel.family &&
1022 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
1023 !security_xfrm_state_pol_flow_match(x, pol, fl))
1024 return;
1026 if (!*best ||
1027 (*best)->km.dying > x->km.dying ||
1028 ((*best)->km.dying == x->km.dying &&
1029 (*best)->curlft.add_time < x->curlft.add_time))
1030 *best = x;
1031 } else if (x->km.state == XFRM_STATE_ACQ) {
1032 *acq_in_progress = 1;
1033 } else if (x->km.state == XFRM_STATE_ERROR ||
1034 x->km.state == XFRM_STATE_EXPIRED) {
1035 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
1036 security_xfrm_state_pol_flow_match(x, pol, fl))
1037 *error = -ESRCH;
1041 struct xfrm_state *
1042 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1043 const struct flowi *fl, struct xfrm_tmpl *tmpl,
1044 struct xfrm_policy *pol, int *err,
1045 unsigned short family, u32 if_id)
1047 static xfrm_address_t saddr_wildcard = { };
1048 struct net *net = xp_net(pol);
1049 unsigned int h, h_wildcard;
1050 struct xfrm_state *x, *x0, *to_put;
1051 int acquire_in_progress = 0;
1052 int error = 0;
1053 struct xfrm_state *best = NULL;
1054 u32 mark = pol->mark.v & pol->mark.m;
1055 unsigned short encap_family = tmpl->encap_family;
1056 unsigned int sequence;
1057 struct km_event c;
1059 to_put = NULL;
1061 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
1063 rcu_read_lock();
1064 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
1065 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
1066 if (x->props.family == encap_family &&
1067 x->props.reqid == tmpl->reqid &&
1068 (mark & x->mark.m) == x->mark.v &&
1069 x->if_id == if_id &&
1070 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1071 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1072 tmpl->mode == x->props.mode &&
1073 tmpl->id.proto == x->id.proto &&
1074 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1075 xfrm_state_look_at(pol, x, fl, encap_family,
1076 &best, &acquire_in_progress, &error);
1078 if (best || acquire_in_progress)
1079 goto found;
1081 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
1082 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
1083 if (x->props.family == encap_family &&
1084 x->props.reqid == tmpl->reqid &&
1085 (mark & x->mark.m) == x->mark.v &&
1086 x->if_id == if_id &&
1087 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1088 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1089 tmpl->mode == x->props.mode &&
1090 tmpl->id.proto == x->id.proto &&
1091 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1092 xfrm_state_look_at(pol, x, fl, encap_family,
1093 &best, &acquire_in_progress, &error);
1096 found:
1097 x = best;
1098 if (!x && !error && !acquire_in_progress) {
1099 if (tmpl->id.spi &&
1100 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
1101 tmpl->id.proto, encap_family)) != NULL) {
1102 to_put = x0;
1103 error = -EEXIST;
1104 goto out;
1107 c.net = net;
1108 /* If the KMs have no listeners (yet...), avoid allocating an SA
1109 * for each and every packet - garbage collection might not
1110 * handle the flood.
1112 if (!km_is_alive(&c)) {
1113 error = -ESRCH;
1114 goto out;
1117 x = xfrm_state_alloc(net);
1118 if (x == NULL) {
1119 error = -ENOMEM;
1120 goto out;
1122 /* Initialize temporary state matching only
1123 * to current session. */
1124 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1125 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1126 x->if_id = if_id;
1128 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1129 if (error) {
1130 x->km.state = XFRM_STATE_DEAD;
1131 to_put = x;
1132 x = NULL;
1133 goto out;
1136 if (km_query(x, tmpl, pol) == 0) {
1137 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1138 x->km.state = XFRM_STATE_ACQ;
1139 list_add(&x->km.all, &net->xfrm.state_all);
1140 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1141 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1142 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1143 if (x->id.spi) {
1144 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1145 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1147 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1148 hrtimer_start(&x->mtimer,
1149 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1150 HRTIMER_MODE_REL_SOFT);
1151 net->xfrm.state_num++;
1152 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1153 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1154 } else {
1155 x->km.state = XFRM_STATE_DEAD;
1156 to_put = x;
1157 x = NULL;
1158 error = -ESRCH;
1161 out:
1162 if (x) {
1163 if (!xfrm_state_hold_rcu(x)) {
1164 *err = -EAGAIN;
1165 x = NULL;
1167 } else {
1168 *err = acquire_in_progress ? -EAGAIN : error;
1170 rcu_read_unlock();
1171 if (to_put)
1172 xfrm_state_put(to_put);
1174 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1175 *err = -EAGAIN;
1176 if (x) {
1177 xfrm_state_put(x);
1178 x = NULL;
1182 return x;
1185 struct xfrm_state *
1186 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1187 xfrm_address_t *daddr, xfrm_address_t *saddr,
1188 unsigned short family, u8 mode, u8 proto, u32 reqid)
1190 unsigned int h;
1191 struct xfrm_state *rx = NULL, *x = NULL;
1193 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1194 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1195 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1196 if (x->props.family == family &&
1197 x->props.reqid == reqid &&
1198 (mark & x->mark.m) == x->mark.v &&
1199 x->if_id == if_id &&
1200 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1201 xfrm_state_addr_check(x, daddr, saddr, family) &&
1202 mode == x->props.mode &&
1203 proto == x->id.proto &&
1204 x->km.state == XFRM_STATE_VALID) {
1205 rx = x;
1206 break;
1210 if (rx)
1211 xfrm_state_hold(rx);
1212 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1215 return rx;
1217 EXPORT_SYMBOL(xfrm_stateonly_find);
1219 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1220 unsigned short family)
1222 struct xfrm_state *x;
1223 struct xfrm_state_walk *w;
1225 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1226 list_for_each_entry(w, &net->xfrm.state_all, all) {
1227 x = container_of(w, struct xfrm_state, km);
1228 if (x->props.family != family ||
1229 x->id.spi != spi)
1230 continue;
1232 xfrm_state_hold(x);
1233 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1234 return x;
1236 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1237 return NULL;
1239 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1241 static void __xfrm_state_insert(struct xfrm_state *x)
1243 struct net *net = xs_net(x);
1244 unsigned int h;
1246 list_add(&x->km.all, &net->xfrm.state_all);
1248 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1249 x->props.reqid, x->props.family);
1250 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1252 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1253 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1255 if (x->id.spi) {
1256 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1257 x->props.family);
1259 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1262 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
1263 if (x->replay_maxage)
1264 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1266 net->xfrm.state_num++;
1268 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1271 /* net->xfrm.xfrm_state_lock is held */
1272 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1274 struct net *net = xs_net(xnew);
1275 unsigned short family = xnew->props.family;
1276 u32 reqid = xnew->props.reqid;
1277 struct xfrm_state *x;
1278 unsigned int h;
1279 u32 mark = xnew->mark.v & xnew->mark.m;
1280 u32 if_id = xnew->if_id;
1282 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1283 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1284 if (x->props.family == family &&
1285 x->props.reqid == reqid &&
1286 x->if_id == if_id &&
1287 (mark & x->mark.m) == x->mark.v &&
1288 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1289 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1290 x->genid++;
1294 void xfrm_state_insert(struct xfrm_state *x)
1296 struct net *net = xs_net(x);
1298 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1299 __xfrm_state_bump_genids(x);
1300 __xfrm_state_insert(x);
1301 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1303 EXPORT_SYMBOL(xfrm_state_insert);
1305 /* net->xfrm.xfrm_state_lock is held */
1306 static struct xfrm_state *__find_acq_core(struct net *net,
1307 const struct xfrm_mark *m,
1308 unsigned short family, u8 mode,
1309 u32 reqid, u32 if_id, u8 proto,
1310 const xfrm_address_t *daddr,
1311 const xfrm_address_t *saddr,
1312 int create)
1314 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1315 struct xfrm_state *x;
1316 u32 mark = m->v & m->m;
1318 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1319 if (x->props.reqid != reqid ||
1320 x->props.mode != mode ||
1321 x->props.family != family ||
1322 x->km.state != XFRM_STATE_ACQ ||
1323 x->id.spi != 0 ||
1324 x->id.proto != proto ||
1325 (mark & x->mark.m) != x->mark.v ||
1326 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1327 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1328 continue;
1330 xfrm_state_hold(x);
1331 return x;
1334 if (!create)
1335 return NULL;
1337 x = xfrm_state_alloc(net);
1338 if (likely(x)) {
1339 switch (family) {
1340 case AF_INET:
1341 x->sel.daddr.a4 = daddr->a4;
1342 x->sel.saddr.a4 = saddr->a4;
1343 x->sel.prefixlen_d = 32;
1344 x->sel.prefixlen_s = 32;
1345 x->props.saddr.a4 = saddr->a4;
1346 x->id.daddr.a4 = daddr->a4;
1347 break;
1349 case AF_INET6:
1350 x->sel.daddr.in6 = daddr->in6;
1351 x->sel.saddr.in6 = saddr->in6;
1352 x->sel.prefixlen_d = 128;
1353 x->sel.prefixlen_s = 128;
1354 x->props.saddr.in6 = saddr->in6;
1355 x->id.daddr.in6 = daddr->in6;
1356 break;
1359 x->km.state = XFRM_STATE_ACQ;
1360 x->id.proto = proto;
1361 x->props.family = family;
1362 x->props.mode = mode;
1363 x->props.reqid = reqid;
1364 x->if_id = if_id;
1365 x->mark.v = m->v;
1366 x->mark.m = m->m;
1367 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1368 xfrm_state_hold(x);
1369 hrtimer_start(&x->mtimer,
1370 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1371 HRTIMER_MODE_REL_SOFT);
1372 list_add(&x->km.all, &net->xfrm.state_all);
1373 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1374 h = xfrm_src_hash(net, daddr, saddr, family);
1375 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1377 net->xfrm.state_num++;
1379 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1382 return x;
1385 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1387 int xfrm_state_add(struct xfrm_state *x)
1389 struct net *net = xs_net(x);
1390 struct xfrm_state *x1, *to_put;
1391 int family;
1392 int err;
1393 u32 mark = x->mark.v & x->mark.m;
1394 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1396 family = x->props.family;
1398 to_put = NULL;
1400 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1402 x1 = __xfrm_state_locate(x, use_spi, family);
1403 if (x1) {
1404 to_put = x1;
1405 x1 = NULL;
1406 err = -EEXIST;
1407 goto out;
1410 if (use_spi && x->km.seq) {
1411 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1412 if (x1 && ((x1->id.proto != x->id.proto) ||
1413 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1414 to_put = x1;
1415 x1 = NULL;
1419 if (use_spi && !x1)
1420 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1421 x->props.reqid, x->if_id, x->id.proto,
1422 &x->id.daddr, &x->props.saddr, 0);
1424 __xfrm_state_bump_genids(x);
1425 __xfrm_state_insert(x);
1426 err = 0;
1428 out:
1429 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1431 if (x1) {
1432 xfrm_state_delete(x1);
1433 xfrm_state_put(x1);
1436 if (to_put)
1437 xfrm_state_put(to_put);
1439 return err;
1441 EXPORT_SYMBOL(xfrm_state_add);
1443 #ifdef CONFIG_XFRM_MIGRATE
1444 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1445 struct xfrm_encap_tmpl *encap)
1447 struct net *net = xs_net(orig);
1448 struct xfrm_state *x = xfrm_state_alloc(net);
1449 if (!x)
1450 goto out;
1452 memcpy(&x->id, &orig->id, sizeof(x->id));
1453 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1454 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1455 x->props.mode = orig->props.mode;
1456 x->props.replay_window = orig->props.replay_window;
1457 x->props.reqid = orig->props.reqid;
1458 x->props.family = orig->props.family;
1459 x->props.saddr = orig->props.saddr;
1461 if (orig->aalg) {
1462 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1463 if (!x->aalg)
1464 goto error;
1466 x->props.aalgo = orig->props.aalgo;
1468 if (orig->aead) {
1469 x->aead = xfrm_algo_aead_clone(orig->aead);
1470 x->geniv = orig->geniv;
1471 if (!x->aead)
1472 goto error;
1474 if (orig->ealg) {
1475 x->ealg = xfrm_algo_clone(orig->ealg);
1476 if (!x->ealg)
1477 goto error;
1479 x->props.ealgo = orig->props.ealgo;
1481 if (orig->calg) {
1482 x->calg = xfrm_algo_clone(orig->calg);
1483 if (!x->calg)
1484 goto error;
1486 x->props.calgo = orig->props.calgo;
1488 if (encap || orig->encap) {
1489 if (encap)
1490 x->encap = kmemdup(encap, sizeof(*x->encap),
1491 GFP_KERNEL);
1492 else
1493 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1494 GFP_KERNEL);
1496 if (!x->encap)
1497 goto error;
1500 if (orig->coaddr) {
1501 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1502 GFP_KERNEL);
1503 if (!x->coaddr)
1504 goto error;
1507 if (orig->replay_esn) {
1508 if (xfrm_replay_clone(x, orig))
1509 goto error;
1512 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1514 if (xfrm_init_state(x) < 0)
1515 goto error;
1517 x->props.flags = orig->props.flags;
1518 x->props.extra_flags = orig->props.extra_flags;
1520 x->if_id = orig->if_id;
1521 x->tfcpad = orig->tfcpad;
1522 x->replay_maxdiff = orig->replay_maxdiff;
1523 x->replay_maxage = orig->replay_maxage;
1524 x->curlft.add_time = orig->curlft.add_time;
1525 x->km.state = orig->km.state;
1526 x->km.seq = orig->km.seq;
1527 x->replay = orig->replay;
1528 x->preplay = orig->preplay;
1530 return x;
1532 error:
1533 xfrm_state_put(x);
1534 out:
1535 return NULL;
1538 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1540 unsigned int h;
1541 struct xfrm_state *x = NULL;
1543 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1545 if (m->reqid) {
1546 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1547 m->reqid, m->old_family);
1548 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1549 if (x->props.mode != m->mode ||
1550 x->id.proto != m->proto)
1551 continue;
1552 if (m->reqid && x->props.reqid != m->reqid)
1553 continue;
1554 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1555 m->old_family) ||
1556 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1557 m->old_family))
1558 continue;
1559 xfrm_state_hold(x);
1560 break;
1562 } else {
1563 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1564 m->old_family);
1565 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1566 if (x->props.mode != m->mode ||
1567 x->id.proto != m->proto)
1568 continue;
1569 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1570 m->old_family) ||
1571 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1572 m->old_family))
1573 continue;
1574 xfrm_state_hold(x);
1575 break;
1579 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1581 return x;
1583 EXPORT_SYMBOL(xfrm_migrate_state_find);
1585 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1586 struct xfrm_migrate *m,
1587 struct xfrm_encap_tmpl *encap)
1589 struct xfrm_state *xc;
1591 xc = xfrm_state_clone(x, encap);
1592 if (!xc)
1593 return NULL;
1595 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1596 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1598 /* add state */
1599 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1600 /* a care is needed when the destination address of the
1601 state is to be updated as it is a part of triplet */
1602 xfrm_state_insert(xc);
1603 } else {
1604 if (xfrm_state_add(xc) < 0)
1605 goto error;
1608 return xc;
1609 error:
1610 xfrm_state_put(xc);
1611 return NULL;
1613 EXPORT_SYMBOL(xfrm_state_migrate);
1614 #endif
1616 int xfrm_state_update(struct xfrm_state *x)
1618 struct xfrm_state *x1, *to_put;
1619 int err;
1620 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1621 struct net *net = xs_net(x);
1623 to_put = NULL;
1625 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1626 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1628 err = -ESRCH;
1629 if (!x1)
1630 goto out;
1632 if (xfrm_state_kern(x1)) {
1633 to_put = x1;
1634 err = -EEXIST;
1635 goto out;
1638 if (x1->km.state == XFRM_STATE_ACQ) {
1639 __xfrm_state_insert(x);
1640 x = NULL;
1642 err = 0;
1644 out:
1645 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1647 if (to_put)
1648 xfrm_state_put(to_put);
1650 if (err)
1651 return err;
1653 if (!x) {
1654 xfrm_state_delete(x1);
1655 xfrm_state_put(x1);
1656 return 0;
1659 err = -EINVAL;
1660 spin_lock_bh(&x1->lock);
1661 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1662 if (x->encap && x1->encap &&
1663 x->encap->encap_type == x1->encap->encap_type)
1664 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1665 else if (x->encap || x1->encap)
1666 goto fail;
1668 if (x->coaddr && x1->coaddr) {
1669 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1671 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1672 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1673 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1674 x1->km.dying = 0;
1676 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1677 HRTIMER_MODE_REL_SOFT);
1678 if (x1->curlft.use_time)
1679 xfrm_state_check_expire(x1);
1681 if (x->props.smark.m || x->props.smark.v || x->if_id) {
1682 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1684 if (x->props.smark.m || x->props.smark.v)
1685 x1->props.smark = x->props.smark;
1687 if (x->if_id)
1688 x1->if_id = x->if_id;
1690 __xfrm_state_bump_genids(x1);
1691 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1694 err = 0;
1695 x->km.state = XFRM_STATE_DEAD;
1696 __xfrm_state_put(x);
1699 fail:
1700 spin_unlock_bh(&x1->lock);
1702 xfrm_state_put(x1);
1704 return err;
1706 EXPORT_SYMBOL(xfrm_state_update);
1708 int xfrm_state_check_expire(struct xfrm_state *x)
1710 if (!x->curlft.use_time)
1711 x->curlft.use_time = ktime_get_real_seconds();
1713 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1714 x->curlft.packets >= x->lft.hard_packet_limit) {
1715 x->km.state = XFRM_STATE_EXPIRED;
1716 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
1717 return -EINVAL;
1720 if (!x->km.dying &&
1721 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1722 x->curlft.packets >= x->lft.soft_packet_limit)) {
1723 x->km.dying = 1;
1724 km_state_expired(x, 0, 0);
1726 return 0;
1728 EXPORT_SYMBOL(xfrm_state_check_expire);
1730 struct xfrm_state *
1731 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1732 u8 proto, unsigned short family)
1734 struct xfrm_state *x;
1736 rcu_read_lock();
1737 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1738 rcu_read_unlock();
1739 return x;
1741 EXPORT_SYMBOL(xfrm_state_lookup);
1743 struct xfrm_state *
1744 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1745 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1746 u8 proto, unsigned short family)
1748 struct xfrm_state *x;
1750 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1751 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1752 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1753 return x;
1755 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1757 struct xfrm_state *
1758 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1759 u32 if_id, u8 proto, const xfrm_address_t *daddr,
1760 const xfrm_address_t *saddr, int create, unsigned short family)
1762 struct xfrm_state *x;
1764 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1765 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
1766 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1768 return x;
1770 EXPORT_SYMBOL(xfrm_find_acq);
1772 #ifdef CONFIG_XFRM_SUB_POLICY
1773 #if IS_ENABLED(CONFIG_IPV6)
1774 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
1775 static void
1776 __xfrm6_sort(void **dst, void **src, int n,
1777 int (*cmp)(const void *p), int maxclass)
1779 int count[XFRM_MAX_DEPTH] = { };
1780 int class[XFRM_MAX_DEPTH];
1781 int i;
1783 for (i = 0; i < n; i++) {
1784 int c = cmp(src[i]);
1786 class[i] = c;
1787 count[c]++;
1790 for (i = 2; i < maxclass; i++)
1791 count[i] += count[i - 1];
1793 for (i = 0; i < n; i++) {
1794 dst[count[class[i] - 1]++] = src[i];
1795 src[i] = NULL;
1799 /* Rule for xfrm_state:
1801 * rule 1: select IPsec transport except AH
1802 * rule 2: select MIPv6 RO or inbound trigger
1803 * rule 3: select IPsec transport AH
1804 * rule 4: select IPsec tunnel
1805 * rule 5: others
1807 static int __xfrm6_state_sort_cmp(const void *p)
1809 const struct xfrm_state *v = p;
1811 switch (v->props.mode) {
1812 case XFRM_MODE_TRANSPORT:
1813 if (v->id.proto != IPPROTO_AH)
1814 return 1;
1815 else
1816 return 3;
1817 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1818 case XFRM_MODE_ROUTEOPTIMIZATION:
1819 case XFRM_MODE_IN_TRIGGER:
1820 return 2;
1821 #endif
1822 case XFRM_MODE_TUNNEL:
1823 case XFRM_MODE_BEET:
1824 return 4;
1826 return 5;
1829 /* Rule for xfrm_tmpl:
1831 * rule 1: select IPsec transport
1832 * rule 2: select MIPv6 RO or inbound trigger
1833 * rule 3: select IPsec tunnel
1834 * rule 4: others
1836 static int __xfrm6_tmpl_sort_cmp(const void *p)
1838 const struct xfrm_tmpl *v = p;
1840 switch (v->mode) {
1841 case XFRM_MODE_TRANSPORT:
1842 return 1;
1843 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1844 case XFRM_MODE_ROUTEOPTIMIZATION:
1845 case XFRM_MODE_IN_TRIGGER:
1846 return 2;
1847 #endif
1848 case XFRM_MODE_TUNNEL:
1849 case XFRM_MODE_BEET:
1850 return 3;
1852 return 4;
1854 #else
1855 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1856 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1858 static inline void
1859 __xfrm6_sort(void **dst, void **src, int n,
1860 int (*cmp)(const void *p), int maxclass)
1862 int i;
1864 for (i = 0; i < n; i++)
1865 dst[i] = src[i];
1867 #endif /* CONFIG_IPV6 */
1869 void
1870 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1871 unsigned short family)
1873 int i;
1875 if (family == AF_INET6)
1876 __xfrm6_sort((void **)dst, (void **)src, n,
1877 __xfrm6_tmpl_sort_cmp, 5);
1878 else
1879 for (i = 0; i < n; i++)
1880 dst[i] = src[i];
1883 void
1884 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1885 unsigned short family)
1887 int i;
1889 if (family == AF_INET6)
1890 __xfrm6_sort((void **)dst, (void **)src, n,
1891 __xfrm6_state_sort_cmp, 6);
1892 else
1893 for (i = 0; i < n; i++)
1894 dst[i] = src[i];
1896 #endif
1898 /* Silly enough, but I'm lazy to build resolution list */
1900 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1902 int i;
1904 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1905 struct xfrm_state *x;
1907 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1908 if (x->km.seq == seq &&
1909 (mark & x->mark.m) == x->mark.v &&
1910 x->km.state == XFRM_STATE_ACQ) {
1911 xfrm_state_hold(x);
1912 return x;
1916 return NULL;
1919 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1921 struct xfrm_state *x;
1923 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1924 x = __xfrm_find_acq_byseq(net, mark, seq);
1925 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1926 return x;
1928 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1930 u32 xfrm_get_acqseq(void)
1932 u32 res;
1933 static atomic_t acqseq;
1935 do {
1936 res = atomic_inc_return(&acqseq);
1937 } while (!res);
1939 return res;
1941 EXPORT_SYMBOL(xfrm_get_acqseq);
1943 int verify_spi_info(u8 proto, u32 min, u32 max)
1945 switch (proto) {
1946 case IPPROTO_AH:
1947 case IPPROTO_ESP:
1948 break;
1950 case IPPROTO_COMP:
1951 /* IPCOMP spi is 16-bits. */
1952 if (max >= 0x10000)
1953 return -EINVAL;
1954 break;
1956 default:
1957 return -EINVAL;
1960 if (min > max)
1961 return -EINVAL;
1963 return 0;
1965 EXPORT_SYMBOL(verify_spi_info);
1967 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1969 struct net *net = xs_net(x);
1970 unsigned int h;
1971 struct xfrm_state *x0;
1972 int err = -ENOENT;
1973 __be32 minspi = htonl(low);
1974 __be32 maxspi = htonl(high);
1975 u32 mark = x->mark.v & x->mark.m;
1977 spin_lock_bh(&x->lock);
1978 if (x->km.state == XFRM_STATE_DEAD)
1979 goto unlock;
1981 err = 0;
1982 if (x->id.spi)
1983 goto unlock;
1985 err = -ENOENT;
1987 if (minspi == maxspi) {
1988 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1989 if (x0) {
1990 xfrm_state_put(x0);
1991 goto unlock;
1993 x->id.spi = minspi;
1994 } else {
1995 u32 spi = 0;
1996 for (h = 0; h < high-low+1; h++) {
1997 spi = low + prandom_u32()%(high-low+1);
1998 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1999 if (x0 == NULL) {
2000 x->id.spi = htonl(spi);
2001 break;
2003 xfrm_state_put(x0);
2006 if (x->id.spi) {
2007 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2008 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2009 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
2010 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2012 err = 0;
2015 unlock:
2016 spin_unlock_bh(&x->lock);
2018 return err;
2020 EXPORT_SYMBOL(xfrm_alloc_spi);
2022 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2023 struct xfrm_address_filter *filter)
2025 if (filter) {
2026 if ((filter->family == AF_INET ||
2027 filter->family == AF_INET6) &&
2028 x->props.family != filter->family)
2029 return false;
2031 return addr_match(&x->props.saddr, &filter->saddr,
2032 filter->splen) &&
2033 addr_match(&x->id.daddr, &filter->daddr,
2034 filter->dplen);
2036 return true;
2039 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2040 int (*func)(struct xfrm_state *, int, void*),
2041 void *data)
2043 struct xfrm_state *state;
2044 struct xfrm_state_walk *x;
2045 int err = 0;
2047 if (walk->seq != 0 && list_empty(&walk->all))
2048 return 0;
2050 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2051 if (list_empty(&walk->all))
2052 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2053 else
2054 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2055 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2056 if (x->state == XFRM_STATE_DEAD)
2057 continue;
2058 state = container_of(x, struct xfrm_state, km);
2059 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2060 continue;
2061 if (!__xfrm_state_filter_match(state, walk->filter))
2062 continue;
2063 err = func(state, walk->seq, data);
2064 if (err) {
2065 list_move_tail(&walk->all, &x->all);
2066 goto out;
2068 walk->seq++;
2070 if (walk->seq == 0) {
2071 err = -ENOENT;
2072 goto out;
2074 list_del_init(&walk->all);
2075 out:
2076 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2077 return err;
2079 EXPORT_SYMBOL(xfrm_state_walk);
2081 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2082 struct xfrm_address_filter *filter)
2084 INIT_LIST_HEAD(&walk->all);
2085 walk->proto = proto;
2086 walk->state = XFRM_STATE_DEAD;
2087 walk->seq = 0;
2088 walk->filter = filter;
2090 EXPORT_SYMBOL(xfrm_state_walk_init);
2092 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2094 kfree(walk->filter);
2096 if (list_empty(&walk->all))
2097 return;
2099 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2100 list_del(&walk->all);
2101 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2103 EXPORT_SYMBOL(xfrm_state_walk_done);
2105 static void xfrm_replay_timer_handler(struct timer_list *t)
2107 struct xfrm_state *x = from_timer(x, t, rtimer);
2109 spin_lock(&x->lock);
2111 if (x->km.state == XFRM_STATE_VALID) {
2112 if (xfrm_aevent_is_on(xs_net(x)))
2113 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
2114 else
2115 x->xflags |= XFRM_TIME_DEFER;
2118 spin_unlock(&x->lock);
2121 static LIST_HEAD(xfrm_km_list);
2123 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2125 struct xfrm_mgr *km;
2127 rcu_read_lock();
2128 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2129 if (km->notify_policy)
2130 km->notify_policy(xp, dir, c);
2131 rcu_read_unlock();
2134 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2136 struct xfrm_mgr *km;
2137 rcu_read_lock();
2138 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2139 if (km->notify)
2140 km->notify(x, c);
2141 rcu_read_unlock();
2144 EXPORT_SYMBOL(km_policy_notify);
2145 EXPORT_SYMBOL(km_state_notify);
2147 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2149 struct km_event c;
2151 c.data.hard = hard;
2152 c.portid = portid;
2153 c.event = XFRM_MSG_EXPIRE;
2154 km_state_notify(x, &c);
2157 EXPORT_SYMBOL(km_state_expired);
2159 * We send to all registered managers regardless of failure
2160 * We are happy with one success
2162 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2164 int err = -EINVAL, acqret;
2165 struct xfrm_mgr *km;
2167 rcu_read_lock();
2168 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2169 acqret = km->acquire(x, t, pol);
2170 if (!acqret)
2171 err = acqret;
2173 rcu_read_unlock();
2174 return err;
2176 EXPORT_SYMBOL(km_query);
2178 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2180 int err = -EINVAL;
2181 struct xfrm_mgr *km;
2183 rcu_read_lock();
2184 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2185 if (km->new_mapping)
2186 err = km->new_mapping(x, ipaddr, sport);
2187 if (!err)
2188 break;
2190 rcu_read_unlock();
2191 return err;
2193 EXPORT_SYMBOL(km_new_mapping);
2195 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2197 struct km_event c;
2199 c.data.hard = hard;
2200 c.portid = portid;
2201 c.event = XFRM_MSG_POLEXPIRE;
2202 km_policy_notify(pol, dir, &c);
2204 EXPORT_SYMBOL(km_policy_expired);
2206 #ifdef CONFIG_XFRM_MIGRATE
2207 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2208 const struct xfrm_migrate *m, int num_migrate,
2209 const struct xfrm_kmaddress *k,
2210 const struct xfrm_encap_tmpl *encap)
2212 int err = -EINVAL;
2213 int ret;
2214 struct xfrm_mgr *km;
2216 rcu_read_lock();
2217 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2218 if (km->migrate) {
2219 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2220 encap);
2221 if (!ret)
2222 err = ret;
2225 rcu_read_unlock();
2226 return err;
2228 EXPORT_SYMBOL(km_migrate);
2229 #endif
2231 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2233 int err = -EINVAL;
2234 int ret;
2235 struct xfrm_mgr *km;
2237 rcu_read_lock();
2238 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2239 if (km->report) {
2240 ret = km->report(net, proto, sel, addr);
2241 if (!ret)
2242 err = ret;
2245 rcu_read_unlock();
2246 return err;
2248 EXPORT_SYMBOL(km_report);
2250 static bool km_is_alive(const struct km_event *c)
2252 struct xfrm_mgr *km;
2253 bool is_alive = false;
2255 rcu_read_lock();
2256 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2257 if (km->is_alive && km->is_alive(c)) {
2258 is_alive = true;
2259 break;
2262 rcu_read_unlock();
2264 return is_alive;
2267 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2269 int err;
2270 u8 *data;
2271 struct xfrm_mgr *km;
2272 struct xfrm_policy *pol = NULL;
2274 if (in_compat_syscall())
2275 return -EOPNOTSUPP;
2277 if (!optval && !optlen) {
2278 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2279 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2280 __sk_dst_reset(sk);
2281 return 0;
2284 if (optlen <= 0 || optlen > PAGE_SIZE)
2285 return -EMSGSIZE;
2287 data = memdup_user(optval, optlen);
2288 if (IS_ERR(data))
2289 return PTR_ERR(data);
2291 err = -EINVAL;
2292 rcu_read_lock();
2293 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2294 pol = km->compile_policy(sk, optname, data,
2295 optlen, &err);
2296 if (err >= 0)
2297 break;
2299 rcu_read_unlock();
2301 if (err >= 0) {
2302 xfrm_sk_policy_insert(sk, err, pol);
2303 xfrm_pol_put(pol);
2304 __sk_dst_reset(sk);
2305 err = 0;
2308 kfree(data);
2309 return err;
2311 EXPORT_SYMBOL(xfrm_user_policy);
2313 static DEFINE_SPINLOCK(xfrm_km_lock);
2315 int xfrm_register_km(struct xfrm_mgr *km)
2317 spin_lock_bh(&xfrm_km_lock);
2318 list_add_tail_rcu(&km->list, &xfrm_km_list);
2319 spin_unlock_bh(&xfrm_km_lock);
2320 return 0;
2322 EXPORT_SYMBOL(xfrm_register_km);
2324 int xfrm_unregister_km(struct xfrm_mgr *km)
2326 spin_lock_bh(&xfrm_km_lock);
2327 list_del_rcu(&km->list);
2328 spin_unlock_bh(&xfrm_km_lock);
2329 synchronize_rcu();
2330 return 0;
2332 EXPORT_SYMBOL(xfrm_unregister_km);
2334 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2336 int err = 0;
2338 if (WARN_ON(afinfo->family >= NPROTO))
2339 return -EAFNOSUPPORT;
2341 spin_lock_bh(&xfrm_state_afinfo_lock);
2342 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2343 err = -EEXIST;
2344 else
2345 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2346 spin_unlock_bh(&xfrm_state_afinfo_lock);
2347 return err;
2349 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2351 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2353 int err = 0, family = afinfo->family;
2355 if (WARN_ON(family >= NPROTO))
2356 return -EAFNOSUPPORT;
2358 spin_lock_bh(&xfrm_state_afinfo_lock);
2359 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2360 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2361 err = -EINVAL;
2362 else
2363 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2365 spin_unlock_bh(&xfrm_state_afinfo_lock);
2366 synchronize_rcu();
2367 return err;
2369 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2371 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2373 if (unlikely(family >= NPROTO))
2374 return NULL;
2376 return rcu_dereference(xfrm_state_afinfo[family]);
2378 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
2380 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2382 struct xfrm_state_afinfo *afinfo;
2383 if (unlikely(family >= NPROTO))
2384 return NULL;
2385 rcu_read_lock();
2386 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2387 if (unlikely(!afinfo))
2388 rcu_read_unlock();
2389 return afinfo;
2392 void xfrm_flush_gc(void)
2394 flush_work(&xfrm_state_gc_work);
2396 EXPORT_SYMBOL(xfrm_flush_gc);
2398 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2399 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2401 if (x->tunnel) {
2402 struct xfrm_state *t = x->tunnel;
2404 if (atomic_read(&t->tunnel_users) == 2)
2405 xfrm_state_delete(t);
2406 atomic_dec(&t->tunnel_users);
2407 xfrm_state_put_sync(t);
2408 x->tunnel = NULL;
2411 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2413 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
2415 const struct xfrm_type *type = READ_ONCE(x->type);
2416 struct crypto_aead *aead;
2417 u32 blksize, net_adj = 0;
2419 if (x->km.state != XFRM_STATE_VALID ||
2420 !type || type->proto != IPPROTO_ESP)
2421 return mtu - x->props.header_len;
2423 aead = x->data;
2424 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2426 switch (x->props.mode) {
2427 case XFRM_MODE_TRANSPORT:
2428 case XFRM_MODE_BEET:
2429 if (x->props.family == AF_INET)
2430 net_adj = sizeof(struct iphdr);
2431 else if (x->props.family == AF_INET6)
2432 net_adj = sizeof(struct ipv6hdr);
2433 break;
2434 case XFRM_MODE_TUNNEL:
2435 break;
2436 default:
2437 WARN_ON_ONCE(1);
2438 break;
2441 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2442 net_adj) & ~(blksize - 1)) + net_adj - 2;
2444 EXPORT_SYMBOL_GPL(xfrm_state_mtu);
2446 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2448 const struct xfrm_mode *inner_mode;
2449 const struct xfrm_mode *outer_mode;
2450 int family = x->props.family;
2451 int err;
2453 if (family == AF_INET &&
2454 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2455 x->props.flags |= XFRM_STATE_NOPMTUDISC;
2457 err = -EPROTONOSUPPORT;
2459 if (x->sel.family != AF_UNSPEC) {
2460 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2461 if (inner_mode == NULL)
2462 goto error;
2464 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2465 family != x->sel.family)
2466 goto error;
2468 x->inner_mode = *inner_mode;
2469 } else {
2470 const struct xfrm_mode *inner_mode_iaf;
2471 int iafamily = AF_INET;
2473 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2474 if (inner_mode == NULL)
2475 goto error;
2477 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
2478 goto error;
2480 x->inner_mode = *inner_mode;
2482 if (x->props.family == AF_INET)
2483 iafamily = AF_INET6;
2485 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2486 if (inner_mode_iaf) {
2487 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2488 x->inner_mode_iaf = *inner_mode_iaf;
2492 x->type = xfrm_get_type(x->id.proto, family);
2493 if (x->type == NULL)
2494 goto error;
2496 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2498 err = x->type->init_state(x);
2499 if (err)
2500 goto error;
2502 outer_mode = xfrm_get_mode(x->props.mode, family);
2503 if (!outer_mode) {
2504 err = -EPROTONOSUPPORT;
2505 goto error;
2508 x->outer_mode = *outer_mode;
2509 if (init_replay) {
2510 err = xfrm_init_replay(x);
2511 if (err)
2512 goto error;
2515 error:
2516 return err;
2519 EXPORT_SYMBOL(__xfrm_init_state);
2521 int xfrm_init_state(struct xfrm_state *x)
2523 int err;
2525 err = __xfrm_init_state(x, true, false);
2526 if (!err)
2527 x->km.state = XFRM_STATE_VALID;
2529 return err;
2532 EXPORT_SYMBOL(xfrm_init_state);
2534 int __net_init xfrm_state_init(struct net *net)
2536 unsigned int sz;
2538 if (net_eq(net, &init_net))
2539 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2540 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2542 INIT_LIST_HEAD(&net->xfrm.state_all);
2544 sz = sizeof(struct hlist_head) * 8;
2546 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2547 if (!net->xfrm.state_bydst)
2548 goto out_bydst;
2549 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2550 if (!net->xfrm.state_bysrc)
2551 goto out_bysrc;
2552 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2553 if (!net->xfrm.state_byspi)
2554 goto out_byspi;
2555 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2557 net->xfrm.state_num = 0;
2558 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2559 spin_lock_init(&net->xfrm.xfrm_state_lock);
2560 return 0;
2562 out_byspi:
2563 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2564 out_bysrc:
2565 xfrm_hash_free(net->xfrm.state_bydst, sz);
2566 out_bydst:
2567 return -ENOMEM;
2570 void xfrm_state_fini(struct net *net)
2572 unsigned int sz;
2574 flush_work(&net->xfrm.state_hash_work);
2575 flush_work(&xfrm_state_gc_work);
2576 xfrm_state_flush(net, 0, false, true);
2578 WARN_ON(!list_empty(&net->xfrm.state_all));
2580 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2581 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2582 xfrm_hash_free(net->xfrm.state_byspi, sz);
2583 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2584 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2585 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2586 xfrm_hash_free(net->xfrm.state_bydst, sz);
2589 #ifdef CONFIG_AUDITSYSCALL
2590 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2591 struct audit_buffer *audit_buf)
2593 struct xfrm_sec_ctx *ctx = x->security;
2594 u32 spi = ntohl(x->id.spi);
2596 if (ctx)
2597 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2598 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2600 switch (x->props.family) {
2601 case AF_INET:
2602 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2603 &x->props.saddr.a4, &x->id.daddr.a4);
2604 break;
2605 case AF_INET6:
2606 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2607 x->props.saddr.a6, x->id.daddr.a6);
2608 break;
2611 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2614 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2615 struct audit_buffer *audit_buf)
2617 const struct iphdr *iph4;
2618 const struct ipv6hdr *iph6;
2620 switch (family) {
2621 case AF_INET:
2622 iph4 = ip_hdr(skb);
2623 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2624 &iph4->saddr, &iph4->daddr);
2625 break;
2626 case AF_INET6:
2627 iph6 = ipv6_hdr(skb);
2628 audit_log_format(audit_buf,
2629 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2630 &iph6->saddr, &iph6->daddr,
2631 iph6->flow_lbl[0] & 0x0f,
2632 iph6->flow_lbl[1],
2633 iph6->flow_lbl[2]);
2634 break;
2638 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2640 struct audit_buffer *audit_buf;
2642 audit_buf = xfrm_audit_start("SAD-add");
2643 if (audit_buf == NULL)
2644 return;
2645 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2646 xfrm_audit_helper_sainfo(x, audit_buf);
2647 audit_log_format(audit_buf, " res=%u", result);
2648 audit_log_end(audit_buf);
2650 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2652 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2654 struct audit_buffer *audit_buf;
2656 audit_buf = xfrm_audit_start("SAD-delete");
2657 if (audit_buf == NULL)
2658 return;
2659 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2660 xfrm_audit_helper_sainfo(x, audit_buf);
2661 audit_log_format(audit_buf, " res=%u", result);
2662 audit_log_end(audit_buf);
2664 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2666 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2667 struct sk_buff *skb)
2669 struct audit_buffer *audit_buf;
2670 u32 spi;
2672 audit_buf = xfrm_audit_start("SA-replay-overflow");
2673 if (audit_buf == NULL)
2674 return;
2675 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2676 /* don't record the sequence number because it's inherent in this kind
2677 * of audit message */
2678 spi = ntohl(x->id.spi);
2679 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2680 audit_log_end(audit_buf);
2682 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2684 void xfrm_audit_state_replay(struct xfrm_state *x,
2685 struct sk_buff *skb, __be32 net_seq)
2687 struct audit_buffer *audit_buf;
2688 u32 spi;
2690 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2691 if (audit_buf == NULL)
2692 return;
2693 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2694 spi = ntohl(x->id.spi);
2695 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2696 spi, spi, ntohl(net_seq));
2697 audit_log_end(audit_buf);
2699 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2701 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2703 struct audit_buffer *audit_buf;
2705 audit_buf = xfrm_audit_start("SA-notfound");
2706 if (audit_buf == NULL)
2707 return;
2708 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2709 audit_log_end(audit_buf);
2711 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2713 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2714 __be32 net_spi, __be32 net_seq)
2716 struct audit_buffer *audit_buf;
2717 u32 spi;
2719 audit_buf = xfrm_audit_start("SA-notfound");
2720 if (audit_buf == NULL)
2721 return;
2722 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2723 spi = ntohl(net_spi);
2724 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2725 spi, spi, ntohl(net_seq));
2726 audit_log_end(audit_buf);
2728 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2730 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2731 struct sk_buff *skb, u8 proto)
2733 struct audit_buffer *audit_buf;
2734 __be32 net_spi;
2735 __be32 net_seq;
2737 audit_buf = xfrm_audit_start("SA-icv-failure");
2738 if (audit_buf == NULL)
2739 return;
2740 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2741 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2742 u32 spi = ntohl(net_spi);
2743 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2744 spi, spi, ntohl(net_seq));
2746 audit_log_end(audit_buf);
2748 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2749 #endif /* CONFIG_AUDITSYSCALL */