1 // SPDX-License-Identifier: GPL-2.0-only
7 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific functions
13 * Add UDP Encapsulation
17 #include <linux/workqueue.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,
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;
91 hlist_for_each_entry_safe(x, tmp, list, bydst) {
94 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
95 x->props.reqid, x->props.family,
97 hlist_add_head_rcu(&x->bydst, ndsttable + h);
99 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
102 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
105 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
106 x->id.proto, x->props.family,
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;
126 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
127 ndst = xfrm_hash_alloc(nsize);
130 nsrc = xfrm_hash_alloc(nsize);
132 xfrm_hash_free(ndst, nsize);
135 nspi = xfrm_hash_alloc(nsize);
137 xfrm_hash_free(ndst, nsize);
138 xfrm_hash_free(nsrc, nsize);
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);
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);
188 return -EAFNOSUPPORT;
190 #define X(afi, T, name) do { \
191 WARN_ON((afi)->type_ ## name); \
192 (afi)->type_ ## name = (T); \
195 switch (type->proto) {
197 X(afinfo, type, comp);
203 X(afinfo, type, esp);
206 X(afinfo, type, ipip);
208 case IPPROTO_DSTOPTS:
209 X(afinfo, type, dstopts);
211 case IPPROTO_ROUTING:
212 X(afinfo, type, routing);
215 X(afinfo, type, ipip6);
219 err = -EPROTONOSUPPORT;
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))
235 #define X(afi, T, name) do { \
236 WARN_ON((afi)->type_ ## name != (T)); \
237 (afi)->type_ ## name = NULL; \
240 switch (type->proto) {
242 X(afinfo, type, comp);
248 X(afinfo, type, esp);
251 X(afinfo, type, ipip);
253 case IPPROTO_DSTOPTS:
254 X(afinfo, type, dstopts);
256 case IPPROTO_ROUTING:
257 X(afinfo, type, routing);
260 X(afinfo, type, ipip6);
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;
278 afinfo = xfrm_state_get_afinfo(family);
279 if (unlikely(afinfo == NULL))
284 type = afinfo->type_comp;
287 type = afinfo->type_ah;
290 type = afinfo->type_esp;
293 type = afinfo->type_ipip;
295 case IPPROTO_DSTOPTS:
296 type = afinfo->type_dstopts;
298 case IPPROTO_ROUTING:
299 type = afinfo->type_routing;
302 type = afinfo->type_ipip6;
308 if (unlikely(type && !try_module_get(type->owner)))
313 if (!type && !modload_attempted) {
314 request_module("xfrm-type-%d-%d", family, proto);
315 modload_attempted = 1;
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);
333 if (unlikely(afinfo == NULL))
334 return -EAFNOSUPPORT;
336 switch (type->proto) {
338 WARN_ON(afinfo->type_offload_esp);
339 afinfo->type_offload_esp = type;
343 err = -EPROTONOSUPPORT;
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))
360 switch (type->proto) {
362 WARN_ON(afinfo->type_offload_esp != type);
363 afinfo->type_offload_esp = NULL;
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;
380 afinfo = xfrm_state_get_afinfo(family);
381 if (unlikely(afinfo == NULL))
386 type = afinfo->type_offload_esp;
392 if ((type && !try_module_get(type->owner)))
397 if (!type && try_load) {
398 request_module("xfrm-offload-%d-%d", family, proto);
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] = {
413 .encap = XFRM_MODE_BEET,
414 .flags = XFRM_MODE_FLAG_TUNNEL,
417 [XFRM_MODE_TRANSPORT] = {
418 .encap = XFRM_MODE_TRANSPORT,
421 [XFRM_MODE_TUNNEL] = {
422 .encap = XFRM_MODE_TUNNEL,
423 .flags = XFRM_MODE_FLAG_TUNNEL,
428 static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
430 .encap = XFRM_MODE_BEET,
431 .flags = XFRM_MODE_FLAG_TUNNEL,
434 [XFRM_MODE_ROUTEOPTIMIZATION] = {
435 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
438 [XFRM_MODE_TRANSPORT] = {
439 .encap = XFRM_MODE_TRANSPORT,
442 [XFRM_MODE_TUNNEL] = {
443 .encap = XFRM_MODE_TUNNEL,
444 .flags = XFRM_MODE_FLAG_TUNNEL,
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))
458 mode = &xfrm4_mode_map[encap];
459 if (mode->family == family)
463 mode = &xfrm6_mode_map[encap];
464 if (mode->family == family)
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);
490 kfree(x->replay_esn);
491 kfree(x->preplay_esn);
493 xfrm_put_type_offload(x->type_offload);
495 x->type->destructor(x);
496 xfrm_put_type(x->type);
499 put_page(x->xfrag.page);
500 xfrm_dev_state_free(x);
501 security_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);
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;
531 if (x->km.state == XFRM_STATE_DEAD)
533 if (x->km.state == XFRM_STATE_EXPIRED)
535 if (x->lft.hard_add_expires_seconds) {
536 long tmo = x->lft.hard_add_expires_seconds +
537 x->curlft.add_time - now;
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;
552 if (x->lft.hard_use_expires_seconds) {
553 long tmo = x->lft.hard_use_expires_seconds +
554 (x->curlft.use_time ? : now) - now;
562 if (x->lft.soft_add_expires_seconds) {
563 long tmo = x->lft.soft_add_expires_seconds +
564 x->curlft.add_time - now;
567 x->xflags &= ~XFRM_SOFT_EXPIRE;
568 } else if (tmo < next) {
570 x->xflags |= XFRM_SOFT_EXPIRE;
574 if (x->lft.soft_use_expires_seconds) {
575 long tmo = x->lft.soft_use_expires_seconds +
576 (x->curlft.use_time ? : now) - now;
585 km_state_expired(x, 0, 0);
587 if (next != TIME64_MAX) {
588 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
589 ret = HRTIMER_RESTART;
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);
600 km_state_expired(x, 1, 0);
602 xfrm_audit_state_delete(x, err ? 0 : 1, true);
605 spin_unlock(&x->lock);
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);
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);
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);
647 ___xfrm_state_destroy(x);
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);
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);
669 hlist_del_rcu(&x->byspi);
670 net->xfrm.state_num--;
671 spin_unlock(&net->xfrm.xfrm_state_lock);
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.
688 EXPORT_SYMBOL(__xfrm_state_delete);
690 int xfrm_state_delete(struct xfrm_state *x)
694 spin_lock_bh(&x->lock);
695 err = __xfrm_state_delete(x);
696 spin_unlock_bh(&x->lock);
700 EXPORT_SYMBOL(xfrm_state_delete);
702 #ifdef CONFIG_SECURITY_NETWORK_XFRM
704 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
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);
724 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
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) {
735 if (xso->dev == dev &&
736 (err = security_xfrm_state_delete(x)) != 0) {
737 xfrm_audit_state_delete(x, 0, task_valid);
747 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
753 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
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);
769 for (i = 0; i <= net->xfrm.state_hmask; i++) {
770 struct xfrm_state *x;
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)) {
776 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
778 err = xfrm_state_delete(x);
779 xfrm_audit_state_delete(x, err ? 0 : 1,
782 xfrm_state_put_sync(x);
788 spin_lock_bh(&net->xfrm.xfrm_state_lock);
794 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
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);
812 for (i = 0; i <= net->xfrm.state_hmask; i++) {
813 struct xfrm_state *x;
814 struct xfrm_state_offload *xso;
816 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
819 if (!xfrm_state_kern(x) && xso->dev == dev) {
821 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
823 err = xfrm_state_delete(x);
824 xfrm_audit_state_delete(x, err ? 0 : 1,
830 spin_lock_bh(&net->xfrm.xfrm_state_lock);
839 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
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);
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;
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;
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)
899 __xfrm4_init_tempsel(&x->sel, fl);
902 __xfrm6_init_tempsel(&x->sel, fl);
908 switch (tmpl->encap_family) {
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;
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));
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 ||
941 x->id.proto != proto ||
942 !xfrm_addr_equal(&x->id.daddr, daddr, family))
945 if ((mark & x->mark.m) != x->mark.v)
947 if (!xfrm_state_hold_rcu(x))
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))
970 if ((mark & x->mark.m) != x->mark.v)
972 if (!xfrm_state_hold_rcu(x))
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;
987 return __xfrm_state_lookup(net, mark, &x->id.daddr,
988 x->id.spi, x->id.proto, family);
990 return __xfrm_state_lookup_byaddr(net, mark,
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,
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
1020 if (x->km.state == XFRM_STATE_VALID) {
1021 if ((x->sel.family &&
1022 (x->sel.family != family ||
1023 !xfrm_selector_match(&x->sel, fl, family))) ||
1024 !security_xfrm_state_pol_flow_match(x, pol, fl))
1028 (*best)->km.dying > x->km.dying ||
1029 ((*best)->km.dying == x->km.dying &&
1030 (*best)->curlft.add_time < x->curlft.add_time))
1032 } else if (x->km.state == XFRM_STATE_ACQ) {
1033 *acq_in_progress = 1;
1034 } else if (x->km.state == XFRM_STATE_ERROR ||
1035 x->km.state == XFRM_STATE_EXPIRED) {
1036 if ((!x->sel.family ||
1037 (x->sel.family == family &&
1038 xfrm_selector_match(&x->sel, fl, family))) &&
1039 security_xfrm_state_pol_flow_match(x, pol, fl))
1045 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1046 const struct flowi *fl, struct xfrm_tmpl *tmpl,
1047 struct xfrm_policy *pol, int *err,
1048 unsigned short family, u32 if_id)
1050 static xfrm_address_t saddr_wildcard = { };
1051 struct net *net = xp_net(pol);
1052 unsigned int h, h_wildcard;
1053 struct xfrm_state *x, *x0, *to_put;
1054 int acquire_in_progress = 0;
1056 struct xfrm_state *best = NULL;
1057 u32 mark = pol->mark.v & pol->mark.m;
1058 unsigned short encap_family = tmpl->encap_family;
1059 unsigned int sequence;
1064 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
1067 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
1068 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
1069 if (x->props.family == encap_family &&
1070 x->props.reqid == tmpl->reqid &&
1071 (mark & x->mark.m) == x->mark.v &&
1072 x->if_id == if_id &&
1073 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1074 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1075 tmpl->mode == x->props.mode &&
1076 tmpl->id.proto == x->id.proto &&
1077 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1078 xfrm_state_look_at(pol, x, fl, family,
1079 &best, &acquire_in_progress, &error);
1081 if (best || acquire_in_progress)
1084 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
1085 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
1086 if (x->props.family == encap_family &&
1087 x->props.reqid == tmpl->reqid &&
1088 (mark & x->mark.m) == x->mark.v &&
1089 x->if_id == if_id &&
1090 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1091 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1092 tmpl->mode == x->props.mode &&
1093 tmpl->id.proto == x->id.proto &&
1094 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1095 xfrm_state_look_at(pol, x, fl, family,
1096 &best, &acquire_in_progress, &error);
1101 if (!x && !error && !acquire_in_progress) {
1103 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
1104 tmpl->id.proto, encap_family)) != NULL) {
1111 /* If the KMs have no listeners (yet...), avoid allocating an SA
1112 * for each and every packet - garbage collection might not
1115 if (!km_is_alive(&c)) {
1120 x = xfrm_state_alloc(net);
1125 /* Initialize temporary state matching only
1126 * to current session. */
1127 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1128 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1131 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1133 x->km.state = XFRM_STATE_DEAD;
1139 if (km_query(x, tmpl, pol) == 0) {
1140 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1141 x->km.state = XFRM_STATE_ACQ;
1142 list_add(&x->km.all, &net->xfrm.state_all);
1143 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1144 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1145 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1147 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1148 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1150 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1151 hrtimer_start(&x->mtimer,
1152 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1153 HRTIMER_MODE_REL_SOFT);
1154 net->xfrm.state_num++;
1155 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1158 x->km.state = XFRM_STATE_DEAD;
1166 if (!xfrm_state_hold_rcu(x)) {
1171 *err = acquire_in_progress ? -EAGAIN : error;
1175 xfrm_state_put(to_put);
1177 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1189 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1190 xfrm_address_t *daddr, xfrm_address_t *saddr,
1191 unsigned short family, u8 mode, u8 proto, u32 reqid)
1194 struct xfrm_state *rx = NULL, *x = NULL;
1196 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1197 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1198 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1199 if (x->props.family == family &&
1200 x->props.reqid == reqid &&
1201 (mark & x->mark.m) == x->mark.v &&
1202 x->if_id == if_id &&
1203 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1204 xfrm_state_addr_check(x, daddr, saddr, family) &&
1205 mode == x->props.mode &&
1206 proto == x->id.proto &&
1207 x->km.state == XFRM_STATE_VALID) {
1214 xfrm_state_hold(rx);
1215 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1220 EXPORT_SYMBOL(xfrm_stateonly_find);
1222 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1223 unsigned short family)
1225 struct xfrm_state *x;
1226 struct xfrm_state_walk *w;
1228 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1229 list_for_each_entry(w, &net->xfrm.state_all, all) {
1230 x = container_of(w, struct xfrm_state, km);
1231 if (x->props.family != family ||
1236 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1239 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1242 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1244 static void __xfrm_state_insert(struct xfrm_state *x)
1246 struct net *net = xs_net(x);
1249 list_add(&x->km.all, &net->xfrm.state_all);
1251 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1252 x->props.reqid, x->props.family);
1253 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1255 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1256 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1259 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1262 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1265 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
1266 if (x->replay_maxage)
1267 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1269 net->xfrm.state_num++;
1271 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1274 /* net->xfrm.xfrm_state_lock is held */
1275 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1277 struct net *net = xs_net(xnew);
1278 unsigned short family = xnew->props.family;
1279 u32 reqid = xnew->props.reqid;
1280 struct xfrm_state *x;
1282 u32 mark = xnew->mark.v & xnew->mark.m;
1283 u32 if_id = xnew->if_id;
1285 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1286 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1287 if (x->props.family == family &&
1288 x->props.reqid == reqid &&
1289 x->if_id == if_id &&
1290 (mark & x->mark.m) == x->mark.v &&
1291 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1292 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1297 void xfrm_state_insert(struct xfrm_state *x)
1299 struct net *net = xs_net(x);
1301 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1302 __xfrm_state_bump_genids(x);
1303 __xfrm_state_insert(x);
1304 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1306 EXPORT_SYMBOL(xfrm_state_insert);
1308 /* net->xfrm.xfrm_state_lock is held */
1309 static struct xfrm_state *__find_acq_core(struct net *net,
1310 const struct xfrm_mark *m,
1311 unsigned short family, u8 mode,
1312 u32 reqid, u32 if_id, u8 proto,
1313 const xfrm_address_t *daddr,
1314 const xfrm_address_t *saddr,
1317 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1318 struct xfrm_state *x;
1319 u32 mark = m->v & m->m;
1321 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1322 if (x->props.reqid != reqid ||
1323 x->props.mode != mode ||
1324 x->props.family != family ||
1325 x->km.state != XFRM_STATE_ACQ ||
1327 x->id.proto != proto ||
1328 (mark & x->mark.m) != x->mark.v ||
1329 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1330 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1340 x = xfrm_state_alloc(net);
1344 x->sel.daddr.a4 = daddr->a4;
1345 x->sel.saddr.a4 = saddr->a4;
1346 x->sel.prefixlen_d = 32;
1347 x->sel.prefixlen_s = 32;
1348 x->props.saddr.a4 = saddr->a4;
1349 x->id.daddr.a4 = daddr->a4;
1353 x->sel.daddr.in6 = daddr->in6;
1354 x->sel.saddr.in6 = saddr->in6;
1355 x->sel.prefixlen_d = 128;
1356 x->sel.prefixlen_s = 128;
1357 x->props.saddr.in6 = saddr->in6;
1358 x->id.daddr.in6 = daddr->in6;
1362 x->km.state = XFRM_STATE_ACQ;
1363 x->id.proto = proto;
1364 x->props.family = family;
1365 x->props.mode = mode;
1366 x->props.reqid = reqid;
1370 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1372 hrtimer_start(&x->mtimer,
1373 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1374 HRTIMER_MODE_REL_SOFT);
1375 list_add(&x->km.all, &net->xfrm.state_all);
1376 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1377 h = xfrm_src_hash(net, daddr, saddr, family);
1378 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1380 net->xfrm.state_num++;
1382 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1388 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1390 int xfrm_state_add(struct xfrm_state *x)
1392 struct net *net = xs_net(x);
1393 struct xfrm_state *x1, *to_put;
1396 u32 mark = x->mark.v & x->mark.m;
1397 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1399 family = x->props.family;
1403 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1405 x1 = __xfrm_state_locate(x, use_spi, family);
1413 if (use_spi && x->km.seq) {
1414 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1415 if (x1 && ((x1->id.proto != x->id.proto) ||
1416 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1423 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1424 x->props.reqid, x->if_id, x->id.proto,
1425 &x->id.daddr, &x->props.saddr, 0);
1427 __xfrm_state_bump_genids(x);
1428 __xfrm_state_insert(x);
1432 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1435 xfrm_state_delete(x1);
1440 xfrm_state_put(to_put);
1444 EXPORT_SYMBOL(xfrm_state_add);
1446 #ifdef CONFIG_XFRM_MIGRATE
1447 static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1449 struct xfrm_user_sec_ctx *uctx;
1450 int size = sizeof(*uctx) + security->ctx_len;
1453 uctx = kmalloc(size, GFP_KERNEL);
1457 uctx->exttype = XFRMA_SEC_CTX;
1459 uctx->ctx_doi = security->ctx_doi;
1460 uctx->ctx_alg = security->ctx_alg;
1461 uctx->ctx_len = security->ctx_len;
1462 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1463 err = security_xfrm_state_alloc(x, uctx);
1471 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1472 struct xfrm_encap_tmpl *encap)
1474 struct net *net = xs_net(orig);
1475 struct xfrm_state *x = xfrm_state_alloc(net);
1479 memcpy(&x->id, &orig->id, sizeof(x->id));
1480 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1481 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1482 x->props.mode = orig->props.mode;
1483 x->props.replay_window = orig->props.replay_window;
1484 x->props.reqid = orig->props.reqid;
1485 x->props.family = orig->props.family;
1486 x->props.saddr = orig->props.saddr;
1489 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1493 x->props.aalgo = orig->props.aalgo;
1496 x->aead = xfrm_algo_aead_clone(orig->aead);
1497 x->geniv = orig->geniv;
1502 x->ealg = xfrm_algo_clone(orig->ealg);
1506 x->props.ealgo = orig->props.ealgo;
1509 x->calg = xfrm_algo_clone(orig->calg);
1513 x->props.calgo = orig->props.calgo;
1515 if (encap || orig->encap) {
1517 x->encap = kmemdup(encap, sizeof(*x->encap),
1520 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1528 if (clone_security(x, orig->security))
1532 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1538 if (orig->replay_esn) {
1539 if (xfrm_replay_clone(x, orig))
1543 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1544 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
1546 if (xfrm_init_state(x) < 0)
1549 x->props.flags = orig->props.flags;
1550 x->props.extra_flags = orig->props.extra_flags;
1552 x->if_id = orig->if_id;
1553 x->tfcpad = orig->tfcpad;
1554 x->replay_maxdiff = orig->replay_maxdiff;
1555 x->replay_maxage = orig->replay_maxage;
1556 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
1557 x->km.state = orig->km.state;
1558 x->km.seq = orig->km.seq;
1559 x->replay = orig->replay;
1560 x->preplay = orig->preplay;
1570 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1573 struct xfrm_state *x = NULL;
1575 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1578 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1579 m->reqid, m->old_family);
1580 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1581 if (x->props.mode != m->mode ||
1582 x->id.proto != m->proto)
1584 if (m->reqid && x->props.reqid != m->reqid)
1586 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1588 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1595 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1597 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1598 if (x->props.mode != m->mode ||
1599 x->id.proto != m->proto)
1601 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1603 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1611 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1615 EXPORT_SYMBOL(xfrm_migrate_state_find);
1617 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1618 struct xfrm_migrate *m,
1619 struct xfrm_encap_tmpl *encap)
1621 struct xfrm_state *xc;
1623 xc = xfrm_state_clone(x, encap);
1627 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1628 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1631 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1632 /* a care is needed when the destination address of the
1633 state is to be updated as it is a part of triplet */
1634 xfrm_state_insert(xc);
1636 if (xfrm_state_add(xc) < 0)
1645 EXPORT_SYMBOL(xfrm_state_migrate);
1648 int xfrm_state_update(struct xfrm_state *x)
1650 struct xfrm_state *x1, *to_put;
1652 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1653 struct net *net = xs_net(x);
1657 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1658 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1664 if (xfrm_state_kern(x1)) {
1670 if (x1->km.state == XFRM_STATE_ACQ) {
1671 __xfrm_state_insert(x);
1677 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1680 xfrm_state_put(to_put);
1686 xfrm_state_delete(x1);
1692 spin_lock_bh(&x1->lock);
1693 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1694 if (x->encap && x1->encap &&
1695 x->encap->encap_type == x1->encap->encap_type)
1696 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1697 else if (x->encap || x1->encap)
1700 if (x->coaddr && x1->coaddr) {
1701 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1703 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1704 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1705 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1708 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1709 HRTIMER_MODE_REL_SOFT);
1710 if (x1->curlft.use_time)
1711 xfrm_state_check_expire(x1);
1713 if (x->props.smark.m || x->props.smark.v || x->if_id) {
1714 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1716 if (x->props.smark.m || x->props.smark.v)
1717 x1->props.smark = x->props.smark;
1720 x1->if_id = x->if_id;
1722 __xfrm_state_bump_genids(x1);
1723 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1727 x->km.state = XFRM_STATE_DEAD;
1728 __xfrm_state_put(x);
1732 spin_unlock_bh(&x1->lock);
1738 EXPORT_SYMBOL(xfrm_state_update);
1740 int xfrm_state_check_expire(struct xfrm_state *x)
1742 if (!x->curlft.use_time)
1743 x->curlft.use_time = ktime_get_real_seconds();
1745 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1746 x->curlft.packets >= x->lft.hard_packet_limit) {
1747 x->km.state = XFRM_STATE_EXPIRED;
1748 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
1753 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1754 x->curlft.packets >= x->lft.soft_packet_limit)) {
1756 km_state_expired(x, 0, 0);
1760 EXPORT_SYMBOL(xfrm_state_check_expire);
1763 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1764 u8 proto, unsigned short family)
1766 struct xfrm_state *x;
1769 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1773 EXPORT_SYMBOL(xfrm_state_lookup);
1776 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1777 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1778 u8 proto, unsigned short family)
1780 struct xfrm_state *x;
1782 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1783 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1784 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1787 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1790 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1791 u32 if_id, u8 proto, const xfrm_address_t *daddr,
1792 const xfrm_address_t *saddr, int create, unsigned short family)
1794 struct xfrm_state *x;
1796 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1797 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
1798 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1802 EXPORT_SYMBOL(xfrm_find_acq);
1804 #ifdef CONFIG_XFRM_SUB_POLICY
1805 #if IS_ENABLED(CONFIG_IPV6)
1806 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
1808 __xfrm6_sort(void **dst, void **src, int n,
1809 int (*cmp)(const void *p), int maxclass)
1811 int count[XFRM_MAX_DEPTH] = { };
1812 int class[XFRM_MAX_DEPTH];
1815 for (i = 0; i < n; i++) {
1816 int c = cmp(src[i]);
1822 for (i = 2; i < maxclass; i++)
1823 count[i] += count[i - 1];
1825 for (i = 0; i < n; i++) {
1826 dst[count[class[i] - 1]++] = src[i];
1831 /* Rule for xfrm_state:
1833 * rule 1: select IPsec transport except AH
1834 * rule 2: select MIPv6 RO or inbound trigger
1835 * rule 3: select IPsec transport AH
1836 * rule 4: select IPsec tunnel
1839 static int __xfrm6_state_sort_cmp(const void *p)
1841 const struct xfrm_state *v = p;
1843 switch (v->props.mode) {
1844 case XFRM_MODE_TRANSPORT:
1845 if (v->id.proto != IPPROTO_AH)
1849 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1850 case XFRM_MODE_ROUTEOPTIMIZATION:
1851 case XFRM_MODE_IN_TRIGGER:
1854 case XFRM_MODE_TUNNEL:
1855 case XFRM_MODE_BEET:
1861 /* Rule for xfrm_tmpl:
1863 * rule 1: select IPsec transport
1864 * rule 2: select MIPv6 RO or inbound trigger
1865 * rule 3: select IPsec tunnel
1868 static int __xfrm6_tmpl_sort_cmp(const void *p)
1870 const struct xfrm_tmpl *v = p;
1873 case XFRM_MODE_TRANSPORT:
1875 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1876 case XFRM_MODE_ROUTEOPTIMIZATION:
1877 case XFRM_MODE_IN_TRIGGER:
1880 case XFRM_MODE_TUNNEL:
1881 case XFRM_MODE_BEET:
1887 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1888 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1891 __xfrm6_sort(void **dst, void **src, int n,
1892 int (*cmp)(const void *p), int maxclass)
1896 for (i = 0; i < n; i++)
1899 #endif /* CONFIG_IPV6 */
1902 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1903 unsigned short family)
1907 if (family == AF_INET6)
1908 __xfrm6_sort((void **)dst, (void **)src, n,
1909 __xfrm6_tmpl_sort_cmp, 5);
1911 for (i = 0; i < n; i++)
1916 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1917 unsigned short family)
1921 if (family == AF_INET6)
1922 __xfrm6_sort((void **)dst, (void **)src, n,
1923 __xfrm6_state_sort_cmp, 6);
1925 for (i = 0; i < n; i++)
1930 /* Silly enough, but I'm lazy to build resolution list */
1932 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1936 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1937 struct xfrm_state *x;
1939 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1940 if (x->km.seq == seq &&
1941 (mark & x->mark.m) == x->mark.v &&
1942 x->km.state == XFRM_STATE_ACQ) {
1951 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1953 struct xfrm_state *x;
1955 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1956 x = __xfrm_find_acq_byseq(net, mark, seq);
1957 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1960 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1962 u32 xfrm_get_acqseq(void)
1965 static atomic_t acqseq;
1968 res = atomic_inc_return(&acqseq);
1973 EXPORT_SYMBOL(xfrm_get_acqseq);
1975 int verify_spi_info(u8 proto, u32 min, u32 max)
1983 /* IPCOMP spi is 16-bits. */
1997 EXPORT_SYMBOL(verify_spi_info);
1999 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
2001 struct net *net = xs_net(x);
2003 struct xfrm_state *x0;
2005 __be32 minspi = htonl(low);
2006 __be32 maxspi = htonl(high);
2007 u32 mark = x->mark.v & x->mark.m;
2009 spin_lock_bh(&x->lock);
2010 if (x->km.state == XFRM_STATE_DEAD)
2019 if (minspi == maxspi) {
2020 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
2028 for (h = 0; h < high-low+1; h++) {
2029 spi = low + prandom_u32()%(high-low+1);
2030 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
2032 x->id.spi = htonl(spi);
2039 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2040 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2041 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
2042 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2048 spin_unlock_bh(&x->lock);
2052 EXPORT_SYMBOL(xfrm_alloc_spi);
2054 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2055 struct xfrm_address_filter *filter)
2058 if ((filter->family == AF_INET ||
2059 filter->family == AF_INET6) &&
2060 x->props.family != filter->family)
2063 return addr_match(&x->props.saddr, &filter->saddr,
2065 addr_match(&x->id.daddr, &filter->daddr,
2071 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2072 int (*func)(struct xfrm_state *, int, void*),
2075 struct xfrm_state *state;
2076 struct xfrm_state_walk *x;
2079 if (walk->seq != 0 && list_empty(&walk->all))
2082 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2083 if (list_empty(&walk->all))
2084 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2086 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2087 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2088 if (x->state == XFRM_STATE_DEAD)
2090 state = container_of(x, struct xfrm_state, km);
2091 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2093 if (!__xfrm_state_filter_match(state, walk->filter))
2095 err = func(state, walk->seq, data);
2097 list_move_tail(&walk->all, &x->all);
2102 if (walk->seq == 0) {
2106 list_del_init(&walk->all);
2108 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2111 EXPORT_SYMBOL(xfrm_state_walk);
2113 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2114 struct xfrm_address_filter *filter)
2116 INIT_LIST_HEAD(&walk->all);
2117 walk->proto = proto;
2118 walk->state = XFRM_STATE_DEAD;
2120 walk->filter = filter;
2122 EXPORT_SYMBOL(xfrm_state_walk_init);
2124 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2126 kfree(walk->filter);
2128 if (list_empty(&walk->all))
2131 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2132 list_del(&walk->all);
2133 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2135 EXPORT_SYMBOL(xfrm_state_walk_done);
2137 static void xfrm_replay_timer_handler(struct timer_list *t)
2139 struct xfrm_state *x = from_timer(x, t, rtimer);
2141 spin_lock(&x->lock);
2143 if (x->km.state == XFRM_STATE_VALID) {
2144 if (xfrm_aevent_is_on(xs_net(x)))
2145 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
2147 x->xflags |= XFRM_TIME_DEFER;
2150 spin_unlock(&x->lock);
2153 static LIST_HEAD(xfrm_km_list);
2155 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2157 struct xfrm_mgr *km;
2160 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2161 if (km->notify_policy)
2162 km->notify_policy(xp, dir, c);
2166 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2168 struct xfrm_mgr *km;
2170 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2176 EXPORT_SYMBOL(km_policy_notify);
2177 EXPORT_SYMBOL(km_state_notify);
2179 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2185 c.event = XFRM_MSG_EXPIRE;
2186 km_state_notify(x, &c);
2189 EXPORT_SYMBOL(km_state_expired);
2191 * We send to all registered managers regardless of failure
2192 * We are happy with one success
2194 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2196 int err = -EINVAL, acqret;
2197 struct xfrm_mgr *km;
2200 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2201 acqret = km->acquire(x, t, pol);
2208 EXPORT_SYMBOL(km_query);
2210 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2213 struct xfrm_mgr *km;
2216 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2217 if (km->new_mapping)
2218 err = km->new_mapping(x, ipaddr, sport);
2225 EXPORT_SYMBOL(km_new_mapping);
2227 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2233 c.event = XFRM_MSG_POLEXPIRE;
2234 km_policy_notify(pol, dir, &c);
2236 EXPORT_SYMBOL(km_policy_expired);
2238 #ifdef CONFIG_XFRM_MIGRATE
2239 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2240 const struct xfrm_migrate *m, int num_migrate,
2241 const struct xfrm_kmaddress *k,
2242 const struct xfrm_encap_tmpl *encap)
2246 struct xfrm_mgr *km;
2249 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2251 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2260 EXPORT_SYMBOL(km_migrate);
2263 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2267 struct xfrm_mgr *km;
2270 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2272 ret = km->report(net, proto, sel, addr);
2280 EXPORT_SYMBOL(km_report);
2282 static bool km_is_alive(const struct km_event *c)
2284 struct xfrm_mgr *km;
2285 bool is_alive = false;
2288 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2289 if (km->is_alive && km->is_alive(c)) {
2299 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2300 static DEFINE_SPINLOCK(xfrm_translator_lock);
2301 static struct xfrm_translator __rcu *xfrm_translator;
2303 struct xfrm_translator *xfrm_get_translator(void)
2305 struct xfrm_translator *xtr;
2308 xtr = rcu_dereference(xfrm_translator);
2311 if (!try_module_get(xtr->owner))
2317 EXPORT_SYMBOL_GPL(xfrm_get_translator);
2319 void xfrm_put_translator(struct xfrm_translator *xtr)
2321 module_put(xtr->owner);
2323 EXPORT_SYMBOL_GPL(xfrm_put_translator);
2325 int xfrm_register_translator(struct xfrm_translator *xtr)
2329 spin_lock_bh(&xfrm_translator_lock);
2330 if (unlikely(xfrm_translator != NULL))
2333 rcu_assign_pointer(xfrm_translator, xtr);
2334 spin_unlock_bh(&xfrm_translator_lock);
2338 EXPORT_SYMBOL_GPL(xfrm_register_translator);
2340 int xfrm_unregister_translator(struct xfrm_translator *xtr)
2344 spin_lock_bh(&xfrm_translator_lock);
2345 if (likely(xfrm_translator != NULL)) {
2346 if (rcu_access_pointer(xfrm_translator) != xtr)
2349 RCU_INIT_POINTER(xfrm_translator, NULL);
2351 spin_unlock_bh(&xfrm_translator_lock);
2356 EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2359 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
2363 struct xfrm_mgr *km;
2364 struct xfrm_policy *pol = NULL;
2366 if (sockptr_is_null(optval) && !optlen) {
2367 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2368 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2373 if (optlen <= 0 || optlen > PAGE_SIZE)
2376 data = memdup_sockptr(optval, optlen);
2378 return PTR_ERR(data);
2380 if (in_compat_syscall()) {
2381 struct xfrm_translator *xtr = xfrm_get_translator();
2386 err = xtr->xlate_user_policy_sockptr(&data, optlen);
2387 xfrm_put_translator(xtr);
2396 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2397 pol = km->compile_policy(sk, optname, data,
2405 xfrm_sk_policy_insert(sk, err, pol);
2414 EXPORT_SYMBOL(xfrm_user_policy);
2416 static DEFINE_SPINLOCK(xfrm_km_lock);
2418 int xfrm_register_km(struct xfrm_mgr *km)
2420 spin_lock_bh(&xfrm_km_lock);
2421 list_add_tail_rcu(&km->list, &xfrm_km_list);
2422 spin_unlock_bh(&xfrm_km_lock);
2425 EXPORT_SYMBOL(xfrm_register_km);
2427 int xfrm_unregister_km(struct xfrm_mgr *km)
2429 spin_lock_bh(&xfrm_km_lock);
2430 list_del_rcu(&km->list);
2431 spin_unlock_bh(&xfrm_km_lock);
2435 EXPORT_SYMBOL(xfrm_unregister_km);
2437 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2441 if (WARN_ON(afinfo->family >= NPROTO))
2442 return -EAFNOSUPPORT;
2444 spin_lock_bh(&xfrm_state_afinfo_lock);
2445 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2448 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2449 spin_unlock_bh(&xfrm_state_afinfo_lock);
2452 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2454 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2456 int err = 0, family = afinfo->family;
2458 if (WARN_ON(family >= NPROTO))
2459 return -EAFNOSUPPORT;
2461 spin_lock_bh(&xfrm_state_afinfo_lock);
2462 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2463 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2466 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2468 spin_unlock_bh(&xfrm_state_afinfo_lock);
2472 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2474 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2476 if (unlikely(family >= NPROTO))
2479 return rcu_dereference(xfrm_state_afinfo[family]);
2481 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
2483 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2485 struct xfrm_state_afinfo *afinfo;
2486 if (unlikely(family >= NPROTO))
2489 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2490 if (unlikely(!afinfo))
2495 void xfrm_flush_gc(void)
2497 flush_work(&xfrm_state_gc_work);
2499 EXPORT_SYMBOL(xfrm_flush_gc);
2501 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2502 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2505 struct xfrm_state *t = x->tunnel;
2507 if (atomic_read(&t->tunnel_users) == 2)
2508 xfrm_state_delete(t);
2509 atomic_dec(&t->tunnel_users);
2510 xfrm_state_put_sync(t);
2514 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2516 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
2518 const struct xfrm_type *type = READ_ONCE(x->type);
2519 struct crypto_aead *aead;
2520 u32 blksize, net_adj = 0;
2522 if (x->km.state != XFRM_STATE_VALID ||
2523 !type || type->proto != IPPROTO_ESP)
2524 return mtu - x->props.header_len;
2527 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2529 switch (x->props.mode) {
2530 case XFRM_MODE_TRANSPORT:
2531 case XFRM_MODE_BEET:
2532 if (x->props.family == AF_INET)
2533 net_adj = sizeof(struct iphdr);
2534 else if (x->props.family == AF_INET6)
2535 net_adj = sizeof(struct ipv6hdr);
2537 case XFRM_MODE_TUNNEL:
2544 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2545 net_adj) & ~(blksize - 1)) + net_adj - 2;
2547 EXPORT_SYMBOL_GPL(xfrm_state_mtu);
2549 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2551 const struct xfrm_mode *inner_mode;
2552 const struct xfrm_mode *outer_mode;
2553 int family = x->props.family;
2556 if (family == AF_INET &&
2557 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2558 x->props.flags |= XFRM_STATE_NOPMTUDISC;
2560 err = -EPROTONOSUPPORT;
2562 if (x->sel.family != AF_UNSPEC) {
2563 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2564 if (inner_mode == NULL)
2567 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2568 family != x->sel.family)
2571 x->inner_mode = *inner_mode;
2573 const struct xfrm_mode *inner_mode_iaf;
2574 int iafamily = AF_INET;
2576 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2577 if (inner_mode == NULL)
2580 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
2583 x->inner_mode = *inner_mode;
2585 if (x->props.family == AF_INET)
2586 iafamily = AF_INET6;
2588 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2589 if (inner_mode_iaf) {
2590 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2591 x->inner_mode_iaf = *inner_mode_iaf;
2595 x->type = xfrm_get_type(x->id.proto, family);
2596 if (x->type == NULL)
2599 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2601 err = x->type->init_state(x);
2605 outer_mode = xfrm_get_mode(x->props.mode, family);
2607 err = -EPROTONOSUPPORT;
2611 x->outer_mode = *outer_mode;
2613 err = xfrm_init_replay(x);
2622 EXPORT_SYMBOL(__xfrm_init_state);
2624 int xfrm_init_state(struct xfrm_state *x)
2628 err = __xfrm_init_state(x, true, false);
2630 x->km.state = XFRM_STATE_VALID;
2635 EXPORT_SYMBOL(xfrm_init_state);
2637 int __net_init xfrm_state_init(struct net *net)
2641 if (net_eq(net, &init_net))
2642 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2643 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2645 INIT_LIST_HEAD(&net->xfrm.state_all);
2647 sz = sizeof(struct hlist_head) * 8;
2649 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2650 if (!net->xfrm.state_bydst)
2652 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2653 if (!net->xfrm.state_bysrc)
2655 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2656 if (!net->xfrm.state_byspi)
2658 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2660 net->xfrm.state_num = 0;
2661 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2662 spin_lock_init(&net->xfrm.xfrm_state_lock);
2666 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2668 xfrm_hash_free(net->xfrm.state_bydst, sz);
2673 void xfrm_state_fini(struct net *net)
2677 flush_work(&net->xfrm.state_hash_work);
2678 flush_work(&xfrm_state_gc_work);
2679 xfrm_state_flush(net, 0, false, true);
2681 WARN_ON(!list_empty(&net->xfrm.state_all));
2683 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2684 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2685 xfrm_hash_free(net->xfrm.state_byspi, sz);
2686 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2687 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2688 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2689 xfrm_hash_free(net->xfrm.state_bydst, sz);
2692 #ifdef CONFIG_AUDITSYSCALL
2693 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2694 struct audit_buffer *audit_buf)
2696 struct xfrm_sec_ctx *ctx = x->security;
2697 u32 spi = ntohl(x->id.spi);
2700 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2701 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2703 switch (x->props.family) {
2705 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2706 &x->props.saddr.a4, &x->id.daddr.a4);
2709 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2710 x->props.saddr.a6, x->id.daddr.a6);
2714 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2717 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2718 struct audit_buffer *audit_buf)
2720 const struct iphdr *iph4;
2721 const struct ipv6hdr *iph6;
2726 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2727 &iph4->saddr, &iph4->daddr);
2730 iph6 = ipv6_hdr(skb);
2731 audit_log_format(audit_buf,
2732 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2733 &iph6->saddr, &iph6->daddr,
2734 iph6->flow_lbl[0] & 0x0f,
2741 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2743 struct audit_buffer *audit_buf;
2745 audit_buf = xfrm_audit_start("SAD-add");
2746 if (audit_buf == NULL)
2748 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2749 xfrm_audit_helper_sainfo(x, audit_buf);
2750 audit_log_format(audit_buf, " res=%u", result);
2751 audit_log_end(audit_buf);
2753 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2755 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2757 struct audit_buffer *audit_buf;
2759 audit_buf = xfrm_audit_start("SAD-delete");
2760 if (audit_buf == NULL)
2762 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2763 xfrm_audit_helper_sainfo(x, audit_buf);
2764 audit_log_format(audit_buf, " res=%u", result);
2765 audit_log_end(audit_buf);
2767 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2769 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2770 struct sk_buff *skb)
2772 struct audit_buffer *audit_buf;
2775 audit_buf = xfrm_audit_start("SA-replay-overflow");
2776 if (audit_buf == NULL)
2778 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2779 /* don't record the sequence number because it's inherent in this kind
2780 * of audit message */
2781 spi = ntohl(x->id.spi);
2782 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2783 audit_log_end(audit_buf);
2785 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2787 void xfrm_audit_state_replay(struct xfrm_state *x,
2788 struct sk_buff *skb, __be32 net_seq)
2790 struct audit_buffer *audit_buf;
2793 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2794 if (audit_buf == NULL)
2796 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2797 spi = ntohl(x->id.spi);
2798 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2799 spi, spi, ntohl(net_seq));
2800 audit_log_end(audit_buf);
2802 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2804 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2806 struct audit_buffer *audit_buf;
2808 audit_buf = xfrm_audit_start("SA-notfound");
2809 if (audit_buf == NULL)
2811 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2812 audit_log_end(audit_buf);
2814 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2816 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2817 __be32 net_spi, __be32 net_seq)
2819 struct audit_buffer *audit_buf;
2822 audit_buf = xfrm_audit_start("SA-notfound");
2823 if (audit_buf == NULL)
2825 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2826 spi = ntohl(net_spi);
2827 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2828 spi, spi, ntohl(net_seq));
2829 audit_log_end(audit_buf);
2831 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2833 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2834 struct sk_buff *skb, u8 proto)
2836 struct audit_buffer *audit_buf;
2840 audit_buf = xfrm_audit_start("SA-icv-failure");
2841 if (audit_buf == NULL)
2843 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2844 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2845 u32 spi = ntohl(net_spi);
2846 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2847 spi, spi, ntohl(net_seq));
2849 audit_log_end(audit_buf);
2851 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2852 #endif /* CONFIG_AUDITSYSCALL */