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/compat.h>
18 #include <linux/workqueue.h>
20 #include <linux/pfkeyv2.h>
21 #include <linux/ipsec.h>
22 #include <linux/module.h>
23 #include <linux/cache.h>
24 #include <linux/audit.h>
25 #include <linux/uaccess.h>
26 #include <linux/ktime.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
31 #include <crypto/aead.h>
33 #include "xfrm_hash.h"
35 #define xfrm_state_deref_prot(table, net) \
36 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
38 static void xfrm_state_gc_task(struct work_struct *work);
40 /* Each xfrm_state may be linked to two tables:
42 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
43 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
44 destination/tunnel endpoint. (output)
47 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
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 unsigned int xfrm_seq_hash(struct net *net, u32 seq)
84 return __xfrm_seq_hash(seq, net->xfrm.state_hmask);
87 static void xfrm_hash_transfer(struct hlist_head *list,
88 struct hlist_head *ndsttable,
89 struct hlist_head *nsrctable,
90 struct hlist_head *nspitable,
91 struct hlist_head *nseqtable,
92 unsigned int nhashmask)
94 struct hlist_node *tmp;
97 hlist_for_each_entry_safe(x, tmp, list, bydst) {
100 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
101 x->props.reqid, x->props.family,
103 hlist_add_head_rcu(&x->bydst, ndsttable + h);
105 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
108 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
111 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
112 x->id.proto, x->props.family,
114 hlist_add_head_rcu(&x->byspi, nspitable + h);
118 h = __xfrm_seq_hash(x->km.seq, nhashmask);
119 hlist_add_head_rcu(&x->byseq, nseqtable + h);
124 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
126 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
129 static void xfrm_hash_resize(struct work_struct *work)
131 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
132 struct hlist_head *ndst, *nsrc, *nspi, *nseq, *odst, *osrc, *ospi, *oseq;
133 unsigned long nsize, osize;
134 unsigned int nhashmask, ohashmask;
137 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
138 ndst = xfrm_hash_alloc(nsize);
141 nsrc = xfrm_hash_alloc(nsize);
143 xfrm_hash_free(ndst, nsize);
146 nspi = xfrm_hash_alloc(nsize);
148 xfrm_hash_free(ndst, nsize);
149 xfrm_hash_free(nsrc, nsize);
152 nseq = xfrm_hash_alloc(nsize);
154 xfrm_hash_free(ndst, nsize);
155 xfrm_hash_free(nsrc, nsize);
156 xfrm_hash_free(nspi, nsize);
160 spin_lock_bh(&net->xfrm.xfrm_state_lock);
161 write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
163 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
164 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
165 for (i = net->xfrm.state_hmask; i >= 0; i--)
166 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nseq, nhashmask);
168 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
169 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
170 oseq = xfrm_state_deref_prot(net->xfrm.state_byseq, net);
171 ohashmask = net->xfrm.state_hmask;
173 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
174 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
175 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
176 rcu_assign_pointer(net->xfrm.state_byseq, nseq);
177 net->xfrm.state_hmask = nhashmask;
179 write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
180 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
182 osize = (ohashmask + 1) * sizeof(struct hlist_head);
186 xfrm_hash_free(odst, osize);
187 xfrm_hash_free(osrc, osize);
188 xfrm_hash_free(ospi, osize);
189 xfrm_hash_free(oseq, osize);
192 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
193 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
195 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
197 int __xfrm_state_delete(struct xfrm_state *x);
199 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
200 static bool km_is_alive(const struct km_event *c);
201 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
203 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
205 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
209 return -EAFNOSUPPORT;
211 #define X(afi, T, name) do { \
212 WARN_ON((afi)->type_ ## name); \
213 (afi)->type_ ## name = (T); \
216 switch (type->proto) {
218 X(afinfo, type, comp);
224 X(afinfo, type, esp);
227 X(afinfo, type, ipip);
229 case IPPROTO_DSTOPTS:
230 X(afinfo, type, dstopts);
232 case IPPROTO_ROUTING:
233 X(afinfo, type, routing);
236 X(afinfo, type, ipip6);
240 err = -EPROTONOSUPPORT;
247 EXPORT_SYMBOL(xfrm_register_type);
249 void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
251 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
253 if (unlikely(afinfo == NULL))
256 #define X(afi, T, name) do { \
257 WARN_ON((afi)->type_ ## name != (T)); \
258 (afi)->type_ ## name = NULL; \
261 switch (type->proto) {
263 X(afinfo, type, comp);
269 X(afinfo, type, esp);
272 X(afinfo, type, ipip);
274 case IPPROTO_DSTOPTS:
275 X(afinfo, type, dstopts);
277 case IPPROTO_ROUTING:
278 X(afinfo, type, routing);
281 X(afinfo, type, ipip6);
290 EXPORT_SYMBOL(xfrm_unregister_type);
292 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
294 const struct xfrm_type *type = NULL;
295 struct xfrm_state_afinfo *afinfo;
296 int modload_attempted = 0;
299 afinfo = xfrm_state_get_afinfo(family);
300 if (unlikely(afinfo == NULL))
305 type = afinfo->type_comp;
308 type = afinfo->type_ah;
311 type = afinfo->type_esp;
314 type = afinfo->type_ipip;
316 case IPPROTO_DSTOPTS:
317 type = afinfo->type_dstopts;
319 case IPPROTO_ROUTING:
320 type = afinfo->type_routing;
323 type = afinfo->type_ipip6;
329 if (unlikely(type && !try_module_get(type->owner)))
334 if (!type && !modload_attempted) {
335 request_module("xfrm-type-%d-%d", family, proto);
336 modload_attempted = 1;
343 static void xfrm_put_type(const struct xfrm_type *type)
345 module_put(type->owner);
348 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
349 unsigned short family)
351 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
354 if (unlikely(afinfo == NULL))
355 return -EAFNOSUPPORT;
357 switch (type->proto) {
359 WARN_ON(afinfo->type_offload_esp);
360 afinfo->type_offload_esp = type;
364 err = -EPROTONOSUPPORT;
371 EXPORT_SYMBOL(xfrm_register_type_offload);
373 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
374 unsigned short family)
376 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
378 if (unlikely(afinfo == NULL))
381 switch (type->proto) {
383 WARN_ON(afinfo->type_offload_esp != type);
384 afinfo->type_offload_esp = NULL;
392 EXPORT_SYMBOL(xfrm_unregister_type_offload);
394 static const struct xfrm_type_offload *
395 xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
397 const struct xfrm_type_offload *type = NULL;
398 struct xfrm_state_afinfo *afinfo;
401 afinfo = xfrm_state_get_afinfo(family);
402 if (unlikely(afinfo == NULL))
407 type = afinfo->type_offload_esp;
413 if ((type && !try_module_get(type->owner)))
418 if (!type && try_load) {
419 request_module("xfrm-offload-%d-%d", family, proto);
427 static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
429 module_put(type->owner);
432 static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
434 .encap = XFRM_MODE_BEET,
435 .flags = XFRM_MODE_FLAG_TUNNEL,
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 xfrm6_mode_map[XFRM_MODE_MAX] = {
451 .encap = XFRM_MODE_BEET,
452 .flags = XFRM_MODE_FLAG_TUNNEL,
455 [XFRM_MODE_ROUTEOPTIMIZATION] = {
456 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
459 [XFRM_MODE_TRANSPORT] = {
460 .encap = XFRM_MODE_TRANSPORT,
463 [XFRM_MODE_TUNNEL] = {
464 .encap = XFRM_MODE_TUNNEL,
465 .flags = XFRM_MODE_FLAG_TUNNEL,
470 static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
472 const struct xfrm_mode *mode;
474 if (unlikely(encap >= XFRM_MODE_MAX))
479 mode = &xfrm4_mode_map[encap];
480 if (mode->family == family)
484 mode = &xfrm6_mode_map[encap];
485 if (mode->family == family)
495 void xfrm_state_free(struct xfrm_state *x)
497 kmem_cache_free(xfrm_state_cache, x);
499 EXPORT_SYMBOL(xfrm_state_free);
501 static void ___xfrm_state_destroy(struct xfrm_state *x)
503 hrtimer_cancel(&x->mtimer);
504 del_timer_sync(&x->rtimer);
511 kfree(x->replay_esn);
512 kfree(x->preplay_esn);
514 xfrm_put_type_offload(x->type_offload);
516 x->type->destructor(x);
517 xfrm_put_type(x->type);
520 put_page(x->xfrag.page);
521 xfrm_dev_state_free(x);
522 security_xfrm_state_free(x);
526 static void xfrm_state_gc_task(struct work_struct *work)
528 struct xfrm_state *x;
529 struct hlist_node *tmp;
530 struct hlist_head gc_list;
532 spin_lock_bh(&xfrm_state_gc_lock);
533 hlist_move_list(&xfrm_state_gc_list, &gc_list);
534 spin_unlock_bh(&xfrm_state_gc_lock);
538 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
539 ___xfrm_state_destroy(x);
542 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
544 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
545 enum hrtimer_restart ret = HRTIMER_NORESTART;
546 time64_t now = ktime_get_real_seconds();
547 time64_t next = TIME64_MAX;
552 if (x->km.state == XFRM_STATE_DEAD)
554 if (x->km.state == XFRM_STATE_EXPIRED)
556 if (x->lft.hard_add_expires_seconds) {
557 long tmo = x->lft.hard_add_expires_seconds +
558 x->curlft.add_time - now;
560 if (x->xflags & XFRM_SOFT_EXPIRE) {
561 /* enter hard expire without soft expire first?!
562 * setting a new date could trigger this.
563 * workaround: fix x->curflt.add_time by below:
565 x->curlft.add_time = now - x->saved_tmo - 1;
566 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
573 if (x->lft.hard_use_expires_seconds) {
574 long tmo = x->lft.hard_use_expires_seconds +
575 (x->curlft.use_time ? : now) - now;
583 if (x->lft.soft_add_expires_seconds) {
584 long tmo = x->lft.soft_add_expires_seconds +
585 x->curlft.add_time - now;
588 x->xflags &= ~XFRM_SOFT_EXPIRE;
589 } else if (tmo < next) {
591 x->xflags |= XFRM_SOFT_EXPIRE;
595 if (x->lft.soft_use_expires_seconds) {
596 long tmo = x->lft.soft_use_expires_seconds +
597 (x->curlft.use_time ? : now) - now;
606 km_state_expired(x, 0, 0);
608 if (next != TIME64_MAX) {
609 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
610 ret = HRTIMER_RESTART;
616 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
617 x->km.state = XFRM_STATE_EXPIRED;
619 err = __xfrm_state_delete(x);
621 km_state_expired(x, 1, 0);
623 xfrm_audit_state_delete(x, err ? 0 : 1, true);
626 spin_unlock(&x->lock);
630 static void xfrm_replay_timer_handler(struct timer_list *t);
632 struct xfrm_state *xfrm_state_alloc(struct net *net)
634 struct xfrm_state *x;
636 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
639 write_pnet(&x->xs_net, net);
640 refcount_set(&x->refcnt, 1);
641 atomic_set(&x->tunnel_users, 0);
642 INIT_LIST_HEAD(&x->km.all);
643 INIT_HLIST_NODE(&x->bydst);
644 INIT_HLIST_NODE(&x->bysrc);
645 INIT_HLIST_NODE(&x->byspi);
646 INIT_HLIST_NODE(&x->byseq);
647 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
648 x->mtimer.function = xfrm_timer_handler;
649 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
650 x->curlft.add_time = ktime_get_real_seconds();
651 x->lft.soft_byte_limit = XFRM_INF;
652 x->lft.soft_packet_limit = XFRM_INF;
653 x->lft.hard_byte_limit = XFRM_INF;
654 x->lft.hard_packet_limit = XFRM_INF;
655 x->replay_maxage = 0;
656 x->replay_maxdiff = 0;
657 spin_lock_init(&x->lock);
661 EXPORT_SYMBOL(xfrm_state_alloc);
663 void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
665 WARN_ON(x->km.state != XFRM_STATE_DEAD);
669 ___xfrm_state_destroy(x);
671 spin_lock_bh(&xfrm_state_gc_lock);
672 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
673 spin_unlock_bh(&xfrm_state_gc_lock);
674 schedule_work(&xfrm_state_gc_work);
677 EXPORT_SYMBOL(__xfrm_state_destroy);
679 int __xfrm_state_delete(struct xfrm_state *x)
681 struct net *net = xs_net(x);
684 if (x->km.state != XFRM_STATE_DEAD) {
685 x->km.state = XFRM_STATE_DEAD;
686 spin_lock(&net->xfrm.xfrm_state_lock);
687 list_del(&x->km.all);
688 hlist_del_rcu(&x->bydst);
689 hlist_del_rcu(&x->bysrc);
691 hlist_del_rcu(&x->byseq);
693 hlist_del_rcu(&x->byspi);
694 net->xfrm.state_num--;
695 spin_unlock(&net->xfrm.xfrm_state_lock);
698 sock_put(rcu_dereference_raw(x->encap_sk));
700 xfrm_dev_state_delete(x);
702 /* All xfrm_state objects are created by xfrm_state_alloc.
703 * The xfrm_state_alloc call gives a reference, and that
704 * is what we are dropping here.
712 EXPORT_SYMBOL(__xfrm_state_delete);
714 int xfrm_state_delete(struct xfrm_state *x)
718 spin_lock_bh(&x->lock);
719 err = __xfrm_state_delete(x);
720 spin_unlock_bh(&x->lock);
724 EXPORT_SYMBOL(xfrm_state_delete);
726 #ifdef CONFIG_SECURITY_NETWORK_XFRM
728 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
732 for (i = 0; i <= net->xfrm.state_hmask; i++) {
733 struct xfrm_state *x;
735 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
736 if (xfrm_id_proto_match(x->id.proto, proto) &&
737 (err = security_xfrm_state_delete(x)) != 0) {
738 xfrm_audit_state_delete(x, 0, task_valid);
748 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
752 for (i = 0; i <= net->xfrm.state_hmask; i++) {
753 struct xfrm_state *x;
754 struct xfrm_state_offload *xso;
756 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
759 if (xso->dev == dev &&
760 (err = security_xfrm_state_delete(x)) != 0) {
761 xfrm_audit_state_delete(x, 0, task_valid);
771 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
777 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
783 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
785 int i, err = 0, cnt = 0;
787 spin_lock_bh(&net->xfrm.xfrm_state_lock);
788 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
793 for (i = 0; i <= net->xfrm.state_hmask; i++) {
794 struct xfrm_state *x;
796 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
797 if (!xfrm_state_kern(x) &&
798 xfrm_id_proto_match(x->id.proto, proto)) {
800 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
802 err = xfrm_state_delete(x);
803 xfrm_audit_state_delete(x, err ? 0 : 1,
806 xfrm_state_put_sync(x);
812 spin_lock_bh(&net->xfrm.xfrm_state_lock);
818 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
824 EXPORT_SYMBOL(xfrm_state_flush);
826 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
828 int i, err = 0, cnt = 0;
830 spin_lock_bh(&net->xfrm.xfrm_state_lock);
831 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
836 for (i = 0; i <= net->xfrm.state_hmask; i++) {
837 struct xfrm_state *x;
838 struct xfrm_state_offload *xso;
840 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
843 if (!xfrm_state_kern(x) && xso->dev == dev) {
845 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
847 err = xfrm_state_delete(x);
848 xfrm_audit_state_delete(x, err ? 0 : 1,
854 spin_lock_bh(&net->xfrm.xfrm_state_lock);
863 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
866 EXPORT_SYMBOL(xfrm_dev_state_flush);
868 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
870 spin_lock_bh(&net->xfrm.xfrm_state_lock);
871 si->sadcnt = net->xfrm.state_num;
872 si->sadhcnt = net->xfrm.state_hmask + 1;
873 si->sadhmcnt = xfrm_state_hashmax;
874 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
876 EXPORT_SYMBOL(xfrm_sad_getinfo);
879 __xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
881 const struct flowi4 *fl4 = &fl->u.ip4;
883 sel->daddr.a4 = fl4->daddr;
884 sel->saddr.a4 = fl4->saddr;
885 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
886 sel->dport_mask = htons(0xffff);
887 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
888 sel->sport_mask = htons(0xffff);
889 sel->family = AF_INET;
890 sel->prefixlen_d = 32;
891 sel->prefixlen_s = 32;
892 sel->proto = fl4->flowi4_proto;
893 sel->ifindex = fl4->flowi4_oif;
897 __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
899 const struct flowi6 *fl6 = &fl->u.ip6;
901 /* Initialize temporary selector matching only to current session. */
902 *(struct in6_addr *)&sel->daddr = fl6->daddr;
903 *(struct in6_addr *)&sel->saddr = fl6->saddr;
904 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
905 sel->dport_mask = htons(0xffff);
906 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
907 sel->sport_mask = htons(0xffff);
908 sel->family = AF_INET6;
909 sel->prefixlen_d = 128;
910 sel->prefixlen_s = 128;
911 sel->proto = fl6->flowi6_proto;
912 sel->ifindex = fl6->flowi6_oif;
916 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
917 const struct xfrm_tmpl *tmpl,
918 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
919 unsigned short family)
923 __xfrm4_init_tempsel(&x->sel, fl);
926 __xfrm6_init_tempsel(&x->sel, fl);
932 switch (tmpl->encap_family) {
934 if (x->id.daddr.a4 == 0)
935 x->id.daddr.a4 = daddr->a4;
936 x->props.saddr = tmpl->saddr;
937 if (x->props.saddr.a4 == 0)
938 x->props.saddr.a4 = saddr->a4;
941 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
942 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
943 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
944 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
945 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
949 x->props.mode = tmpl->mode;
950 x->props.reqid = tmpl->reqid;
951 x->props.family = tmpl->encap_family;
954 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
955 const xfrm_address_t *daddr,
956 __be32 spi, u8 proto,
957 unsigned short family)
959 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
960 struct xfrm_state *x;
962 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
963 if (x->props.family != family ||
965 x->id.proto != proto ||
966 !xfrm_addr_equal(&x->id.daddr, daddr, family))
969 if ((mark & x->mark.m) != x->mark.v)
971 if (!xfrm_state_hold_rcu(x))
979 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
980 const xfrm_address_t *daddr,
981 const xfrm_address_t *saddr,
982 u8 proto, unsigned short family)
984 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
985 struct xfrm_state *x;
987 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
988 if (x->props.family != family ||
989 x->id.proto != proto ||
990 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
991 !xfrm_addr_equal(&x->props.saddr, saddr, family))
994 if ((mark & x->mark.m) != x->mark.v)
996 if (!xfrm_state_hold_rcu(x))
1004 static inline struct xfrm_state *
1005 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
1007 struct net *net = xs_net(x);
1008 u32 mark = x->mark.v & x->mark.m;
1011 return __xfrm_state_lookup(net, mark, &x->id.daddr,
1012 x->id.spi, x->id.proto, family);
1014 return __xfrm_state_lookup_byaddr(net, mark,
1017 x->id.proto, family);
1020 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
1022 if (have_hash_collision &&
1023 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
1024 net->xfrm.state_num > net->xfrm.state_hmask)
1025 schedule_work(&net->xfrm.state_hash_work);
1028 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
1029 const struct flowi *fl, unsigned short family,
1030 struct xfrm_state **best, int *acq_in_progress,
1033 /* Resolution logic:
1034 * 1. There is a valid state with matching selector. Done.
1035 * 2. Valid state with inappropriate selector. Skip.
1037 * Entering area of "sysdeps".
1039 * 3. If state is not valid, selector is temporary, it selects
1040 * only session which triggered previous resolution. Key
1041 * manager will do something to install a state with proper
1044 if (x->km.state == XFRM_STATE_VALID) {
1045 if ((x->sel.family &&
1046 (x->sel.family != family ||
1047 !xfrm_selector_match(&x->sel, fl, family))) ||
1048 !security_xfrm_state_pol_flow_match(x, pol,
1049 &fl->u.__fl_common))
1053 (*best)->km.dying > x->km.dying ||
1054 ((*best)->km.dying == x->km.dying &&
1055 (*best)->curlft.add_time < x->curlft.add_time))
1057 } else if (x->km.state == XFRM_STATE_ACQ) {
1058 *acq_in_progress = 1;
1059 } else if (x->km.state == XFRM_STATE_ERROR ||
1060 x->km.state == XFRM_STATE_EXPIRED) {
1061 if ((!x->sel.family ||
1062 (x->sel.family == family &&
1063 xfrm_selector_match(&x->sel, fl, family))) &&
1064 security_xfrm_state_pol_flow_match(x, pol,
1065 &fl->u.__fl_common))
1071 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1072 const struct flowi *fl, struct xfrm_tmpl *tmpl,
1073 struct xfrm_policy *pol, int *err,
1074 unsigned short family, u32 if_id)
1076 static xfrm_address_t saddr_wildcard = { };
1077 struct net *net = xp_net(pol);
1078 unsigned int h, h_wildcard;
1079 struct xfrm_state *x, *x0, *to_put;
1080 int acquire_in_progress = 0;
1082 struct xfrm_state *best = NULL;
1083 u32 mark = pol->mark.v & pol->mark.m;
1084 unsigned short encap_family = tmpl->encap_family;
1085 unsigned int sequence;
1090 sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
1093 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
1094 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
1095 if (x->props.family == encap_family &&
1096 x->props.reqid == tmpl->reqid &&
1097 (mark & x->mark.m) == x->mark.v &&
1098 x->if_id == if_id &&
1099 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1100 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1101 tmpl->mode == x->props.mode &&
1102 tmpl->id.proto == x->id.proto &&
1103 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1104 xfrm_state_look_at(pol, x, fl, family,
1105 &best, &acquire_in_progress, &error);
1107 if (best || acquire_in_progress)
1110 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
1111 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
1112 if (x->props.family == encap_family &&
1113 x->props.reqid == tmpl->reqid &&
1114 (mark & x->mark.m) == x->mark.v &&
1115 x->if_id == if_id &&
1116 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1117 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1118 tmpl->mode == x->props.mode &&
1119 tmpl->id.proto == x->id.proto &&
1120 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1121 xfrm_state_look_at(pol, x, fl, family,
1122 &best, &acquire_in_progress, &error);
1127 if (!x && !error && !acquire_in_progress) {
1129 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
1130 tmpl->id.proto, encap_family)) != NULL) {
1137 /* If the KMs have no listeners (yet...), avoid allocating an SA
1138 * for each and every packet - garbage collection might not
1141 if (!km_is_alive(&c)) {
1146 x = xfrm_state_alloc(net);
1151 /* Initialize temporary state matching only
1152 * to current session. */
1153 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1154 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1157 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1159 x->km.state = XFRM_STATE_DEAD;
1165 if (km_query(x, tmpl, pol) == 0) {
1166 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1167 x->km.state = XFRM_STATE_ACQ;
1168 list_add(&x->km.all, &net->xfrm.state_all);
1169 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1170 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1171 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1173 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1174 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1177 h = xfrm_seq_hash(net, x->km.seq);
1178 hlist_add_head_rcu(&x->byseq, net->xfrm.state_byseq + h);
1180 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1181 hrtimer_start(&x->mtimer,
1182 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1183 HRTIMER_MODE_REL_SOFT);
1184 net->xfrm.state_num++;
1185 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1186 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1188 x->km.state = XFRM_STATE_DEAD;
1196 if (!xfrm_state_hold_rcu(x)) {
1201 *err = acquire_in_progress ? -EAGAIN : error;
1205 xfrm_state_put(to_put);
1207 if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
1219 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1220 xfrm_address_t *daddr, xfrm_address_t *saddr,
1221 unsigned short family, u8 mode, u8 proto, u32 reqid)
1224 struct xfrm_state *rx = NULL, *x = NULL;
1226 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1227 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1228 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1229 if (x->props.family == family &&
1230 x->props.reqid == reqid &&
1231 (mark & x->mark.m) == x->mark.v &&
1232 x->if_id == if_id &&
1233 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1234 xfrm_state_addr_check(x, daddr, saddr, family) &&
1235 mode == x->props.mode &&
1236 proto == x->id.proto &&
1237 x->km.state == XFRM_STATE_VALID) {
1244 xfrm_state_hold(rx);
1245 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1250 EXPORT_SYMBOL(xfrm_stateonly_find);
1252 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1253 unsigned short family)
1255 struct xfrm_state *x;
1256 struct xfrm_state_walk *w;
1258 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1259 list_for_each_entry(w, &net->xfrm.state_all, all) {
1260 x = container_of(w, struct xfrm_state, km);
1261 if (x->props.family != family ||
1266 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1269 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1272 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1274 static void __xfrm_state_insert(struct xfrm_state *x)
1276 struct net *net = xs_net(x);
1279 list_add(&x->km.all, &net->xfrm.state_all);
1281 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1282 x->props.reqid, x->props.family);
1283 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1285 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1286 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1289 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1292 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1296 h = xfrm_seq_hash(net, x->km.seq);
1298 hlist_add_head_rcu(&x->byseq, net->xfrm.state_byseq + h);
1301 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
1302 if (x->replay_maxage)
1303 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1305 net->xfrm.state_num++;
1307 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1310 /* net->xfrm.xfrm_state_lock is held */
1311 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1313 struct net *net = xs_net(xnew);
1314 unsigned short family = xnew->props.family;
1315 u32 reqid = xnew->props.reqid;
1316 struct xfrm_state *x;
1318 u32 mark = xnew->mark.v & xnew->mark.m;
1319 u32 if_id = xnew->if_id;
1321 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1322 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1323 if (x->props.family == family &&
1324 x->props.reqid == reqid &&
1325 x->if_id == if_id &&
1326 (mark & x->mark.m) == x->mark.v &&
1327 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1328 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1333 void xfrm_state_insert(struct xfrm_state *x)
1335 struct net *net = xs_net(x);
1337 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1338 __xfrm_state_bump_genids(x);
1339 __xfrm_state_insert(x);
1340 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1342 EXPORT_SYMBOL(xfrm_state_insert);
1344 /* net->xfrm.xfrm_state_lock is held */
1345 static struct xfrm_state *__find_acq_core(struct net *net,
1346 const struct xfrm_mark *m,
1347 unsigned short family, u8 mode,
1348 u32 reqid, u32 if_id, u8 proto,
1349 const xfrm_address_t *daddr,
1350 const xfrm_address_t *saddr,
1353 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1354 struct xfrm_state *x;
1355 u32 mark = m->v & m->m;
1357 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1358 if (x->props.reqid != reqid ||
1359 x->props.mode != mode ||
1360 x->props.family != family ||
1361 x->km.state != XFRM_STATE_ACQ ||
1363 x->id.proto != proto ||
1364 (mark & x->mark.m) != x->mark.v ||
1365 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1366 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1376 x = xfrm_state_alloc(net);
1380 x->sel.daddr.a4 = daddr->a4;
1381 x->sel.saddr.a4 = saddr->a4;
1382 x->sel.prefixlen_d = 32;
1383 x->sel.prefixlen_s = 32;
1384 x->props.saddr.a4 = saddr->a4;
1385 x->id.daddr.a4 = daddr->a4;
1389 x->sel.daddr.in6 = daddr->in6;
1390 x->sel.saddr.in6 = saddr->in6;
1391 x->sel.prefixlen_d = 128;
1392 x->sel.prefixlen_s = 128;
1393 x->props.saddr.in6 = saddr->in6;
1394 x->id.daddr.in6 = daddr->in6;
1398 x->km.state = XFRM_STATE_ACQ;
1399 x->id.proto = proto;
1400 x->props.family = family;
1401 x->props.mode = mode;
1402 x->props.reqid = reqid;
1406 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1408 hrtimer_start(&x->mtimer,
1409 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1410 HRTIMER_MODE_REL_SOFT);
1411 list_add(&x->km.all, &net->xfrm.state_all);
1412 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1413 h = xfrm_src_hash(net, daddr, saddr, family);
1414 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1416 net->xfrm.state_num++;
1418 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1424 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1426 int xfrm_state_add(struct xfrm_state *x)
1428 struct net *net = xs_net(x);
1429 struct xfrm_state *x1, *to_put;
1432 u32 mark = x->mark.v & x->mark.m;
1433 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1435 family = x->props.family;
1439 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1441 x1 = __xfrm_state_locate(x, use_spi, family);
1449 if (use_spi && x->km.seq) {
1450 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1451 if (x1 && ((x1->id.proto != x->id.proto) ||
1452 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1459 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1460 x->props.reqid, x->if_id, x->id.proto,
1461 &x->id.daddr, &x->props.saddr, 0);
1463 __xfrm_state_bump_genids(x);
1464 __xfrm_state_insert(x);
1468 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1471 xfrm_state_delete(x1);
1476 xfrm_state_put(to_put);
1480 EXPORT_SYMBOL(xfrm_state_add);
1482 #ifdef CONFIG_XFRM_MIGRATE
1483 static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1485 struct xfrm_user_sec_ctx *uctx;
1486 int size = sizeof(*uctx) + security->ctx_len;
1489 uctx = kmalloc(size, GFP_KERNEL);
1493 uctx->exttype = XFRMA_SEC_CTX;
1495 uctx->ctx_doi = security->ctx_doi;
1496 uctx->ctx_alg = security->ctx_alg;
1497 uctx->ctx_len = security->ctx_len;
1498 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1499 err = security_xfrm_state_alloc(x, uctx);
1507 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1508 struct xfrm_encap_tmpl *encap)
1510 struct net *net = xs_net(orig);
1511 struct xfrm_state *x = xfrm_state_alloc(net);
1515 memcpy(&x->id, &orig->id, sizeof(x->id));
1516 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1517 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1518 x->props.mode = orig->props.mode;
1519 x->props.replay_window = orig->props.replay_window;
1520 x->props.reqid = orig->props.reqid;
1521 x->props.family = orig->props.family;
1522 x->props.saddr = orig->props.saddr;
1525 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1529 x->props.aalgo = orig->props.aalgo;
1532 x->aead = xfrm_algo_aead_clone(orig->aead);
1533 x->geniv = orig->geniv;
1538 x->ealg = xfrm_algo_clone(orig->ealg);
1542 x->props.ealgo = orig->props.ealgo;
1545 x->calg = xfrm_algo_clone(orig->calg);
1549 x->props.calgo = orig->props.calgo;
1551 if (encap || orig->encap) {
1553 x->encap = kmemdup(encap, sizeof(*x->encap),
1556 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1564 if (clone_security(x, orig->security))
1568 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1574 if (orig->replay_esn) {
1575 if (xfrm_replay_clone(x, orig))
1579 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1580 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
1582 if (xfrm_init_state(x) < 0)
1585 x->props.flags = orig->props.flags;
1586 x->props.extra_flags = orig->props.extra_flags;
1588 x->if_id = orig->if_id;
1589 x->tfcpad = orig->tfcpad;
1590 x->replay_maxdiff = orig->replay_maxdiff;
1591 x->replay_maxage = orig->replay_maxage;
1592 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
1593 x->km.state = orig->km.state;
1594 x->km.seq = orig->km.seq;
1595 x->replay = orig->replay;
1596 x->preplay = orig->preplay;
1597 x->mapping_maxage = orig->mapping_maxage;
1599 x->new_mapping_sport = 0;
1609 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
1613 struct xfrm_state *x = NULL;
1615 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1618 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1619 m->reqid, m->old_family);
1620 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1621 if (x->props.mode != m->mode ||
1622 x->id.proto != m->proto)
1624 if (m->reqid && x->props.reqid != m->reqid)
1626 if (if_id != 0 && x->if_id != if_id)
1628 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1630 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1637 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1639 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1640 if (x->props.mode != m->mode ||
1641 x->id.proto != m->proto)
1643 if (if_id != 0 && x->if_id != if_id)
1645 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1647 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1655 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1659 EXPORT_SYMBOL(xfrm_migrate_state_find);
1661 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1662 struct xfrm_migrate *m,
1663 struct xfrm_encap_tmpl *encap)
1665 struct xfrm_state *xc;
1667 xc = xfrm_state_clone(x, encap);
1671 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1672 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1675 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1676 /* a care is needed when the destination address of the
1677 state is to be updated as it is a part of triplet */
1678 xfrm_state_insert(xc);
1680 if (xfrm_state_add(xc) < 0)
1689 EXPORT_SYMBOL(xfrm_state_migrate);
1692 int xfrm_state_update(struct xfrm_state *x)
1694 struct xfrm_state *x1, *to_put;
1696 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1697 struct net *net = xs_net(x);
1701 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1702 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1708 if (xfrm_state_kern(x1)) {
1714 if (x1->km.state == XFRM_STATE_ACQ) {
1715 __xfrm_state_insert(x);
1721 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1724 xfrm_state_put(to_put);
1730 xfrm_state_delete(x1);
1736 spin_lock_bh(&x1->lock);
1737 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1738 if (x->encap && x1->encap &&
1739 x->encap->encap_type == x1->encap->encap_type)
1740 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1741 else if (x->encap || x1->encap)
1744 if (x->coaddr && x1->coaddr) {
1745 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1747 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1748 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1749 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1752 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1753 HRTIMER_MODE_REL_SOFT);
1754 if (x1->curlft.use_time)
1755 xfrm_state_check_expire(x1);
1757 if (x->props.smark.m || x->props.smark.v || x->if_id) {
1758 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1760 if (x->props.smark.m || x->props.smark.v)
1761 x1->props.smark = x->props.smark;
1764 x1->if_id = x->if_id;
1766 __xfrm_state_bump_genids(x1);
1767 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1771 x->km.state = XFRM_STATE_DEAD;
1772 __xfrm_state_put(x);
1776 spin_unlock_bh(&x1->lock);
1782 EXPORT_SYMBOL(xfrm_state_update);
1784 int xfrm_state_check_expire(struct xfrm_state *x)
1786 if (!x->curlft.use_time)
1787 x->curlft.use_time = ktime_get_real_seconds();
1789 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1790 x->curlft.packets >= x->lft.hard_packet_limit) {
1791 x->km.state = XFRM_STATE_EXPIRED;
1792 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
1797 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1798 x->curlft.packets >= x->lft.soft_packet_limit)) {
1800 km_state_expired(x, 0, 0);
1804 EXPORT_SYMBOL(xfrm_state_check_expire);
1807 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1808 u8 proto, unsigned short family)
1810 struct xfrm_state *x;
1813 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1817 EXPORT_SYMBOL(xfrm_state_lookup);
1820 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1821 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1822 u8 proto, unsigned short family)
1824 struct xfrm_state *x;
1826 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1827 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1828 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1831 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1834 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1835 u32 if_id, u8 proto, const xfrm_address_t *daddr,
1836 const xfrm_address_t *saddr, int create, unsigned short family)
1838 struct xfrm_state *x;
1840 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1841 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
1842 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1846 EXPORT_SYMBOL(xfrm_find_acq);
1848 #ifdef CONFIG_XFRM_SUB_POLICY
1849 #if IS_ENABLED(CONFIG_IPV6)
1850 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
1852 __xfrm6_sort(void **dst, void **src, int n,
1853 int (*cmp)(const void *p), int maxclass)
1855 int count[XFRM_MAX_DEPTH] = { };
1856 int class[XFRM_MAX_DEPTH];
1859 for (i = 0; i < n; i++) {
1860 int c = cmp(src[i]);
1866 for (i = 2; i < maxclass; i++)
1867 count[i] += count[i - 1];
1869 for (i = 0; i < n; i++) {
1870 dst[count[class[i] - 1]++] = src[i];
1875 /* Rule for xfrm_state:
1877 * rule 1: select IPsec transport except AH
1878 * rule 2: select MIPv6 RO or inbound trigger
1879 * rule 3: select IPsec transport AH
1880 * rule 4: select IPsec tunnel
1883 static int __xfrm6_state_sort_cmp(const void *p)
1885 const struct xfrm_state *v = p;
1887 switch (v->props.mode) {
1888 case XFRM_MODE_TRANSPORT:
1889 if (v->id.proto != IPPROTO_AH)
1893 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1894 case XFRM_MODE_ROUTEOPTIMIZATION:
1895 case XFRM_MODE_IN_TRIGGER:
1898 case XFRM_MODE_TUNNEL:
1899 case XFRM_MODE_BEET:
1905 /* Rule for xfrm_tmpl:
1907 * rule 1: select IPsec transport
1908 * rule 2: select MIPv6 RO or inbound trigger
1909 * rule 3: select IPsec tunnel
1912 static int __xfrm6_tmpl_sort_cmp(const void *p)
1914 const struct xfrm_tmpl *v = p;
1917 case XFRM_MODE_TRANSPORT:
1919 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1920 case XFRM_MODE_ROUTEOPTIMIZATION:
1921 case XFRM_MODE_IN_TRIGGER:
1924 case XFRM_MODE_TUNNEL:
1925 case XFRM_MODE_BEET:
1931 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1932 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1935 __xfrm6_sort(void **dst, void **src, int n,
1936 int (*cmp)(const void *p), int maxclass)
1940 for (i = 0; i < n; i++)
1943 #endif /* CONFIG_IPV6 */
1946 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1947 unsigned short family)
1951 if (family == AF_INET6)
1952 __xfrm6_sort((void **)dst, (void **)src, n,
1953 __xfrm6_tmpl_sort_cmp, 5);
1955 for (i = 0; i < n; i++)
1960 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1961 unsigned short family)
1965 if (family == AF_INET6)
1966 __xfrm6_sort((void **)dst, (void **)src, n,
1967 __xfrm6_state_sort_cmp, 6);
1969 for (i = 0; i < n; i++)
1974 /* Silly enough, but I'm lazy to build resolution list */
1976 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1978 unsigned int h = xfrm_seq_hash(net, seq);
1979 struct xfrm_state *x;
1981 hlist_for_each_entry_rcu(x, net->xfrm.state_byseq + h, byseq) {
1982 if (x->km.seq == seq &&
1983 (mark & x->mark.m) == x->mark.v &&
1984 x->km.state == XFRM_STATE_ACQ) {
1993 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1995 struct xfrm_state *x;
1997 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1998 x = __xfrm_find_acq_byseq(net, mark, seq);
1999 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2002 EXPORT_SYMBOL(xfrm_find_acq_byseq);
2004 u32 xfrm_get_acqseq(void)
2007 static atomic_t acqseq;
2010 res = atomic_inc_return(&acqseq);
2015 EXPORT_SYMBOL(xfrm_get_acqseq);
2017 int verify_spi_info(u8 proto, u32 min, u32 max)
2025 /* IPCOMP spi is 16-bits. */
2039 EXPORT_SYMBOL(verify_spi_info);
2041 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
2043 struct net *net = xs_net(x);
2045 struct xfrm_state *x0;
2047 __be32 minspi = htonl(low);
2048 __be32 maxspi = htonl(high);
2050 u32 mark = x->mark.v & x->mark.m;
2052 spin_lock_bh(&x->lock);
2053 if (x->km.state == XFRM_STATE_DEAD)
2062 if (minspi == maxspi) {
2063 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
2071 for (h = 0; h < high-low+1; h++) {
2072 spi = low + prandom_u32()%(high-low+1);
2073 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
2075 newspi = htonl(spi);
2082 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2084 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2085 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
2086 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2092 spin_unlock_bh(&x->lock);
2096 EXPORT_SYMBOL(xfrm_alloc_spi);
2098 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2099 struct xfrm_address_filter *filter)
2102 if ((filter->family == AF_INET ||
2103 filter->family == AF_INET6) &&
2104 x->props.family != filter->family)
2107 return addr_match(&x->props.saddr, &filter->saddr,
2109 addr_match(&x->id.daddr, &filter->daddr,
2115 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2116 int (*func)(struct xfrm_state *, int, void*),
2119 struct xfrm_state *state;
2120 struct xfrm_state_walk *x;
2123 if (walk->seq != 0 && list_empty(&walk->all))
2126 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2127 if (list_empty(&walk->all))
2128 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2130 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2131 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2132 if (x->state == XFRM_STATE_DEAD)
2134 state = container_of(x, struct xfrm_state, km);
2135 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2137 if (!__xfrm_state_filter_match(state, walk->filter))
2139 err = func(state, walk->seq, data);
2141 list_move_tail(&walk->all, &x->all);
2146 if (walk->seq == 0) {
2150 list_del_init(&walk->all);
2152 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2155 EXPORT_SYMBOL(xfrm_state_walk);
2157 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2158 struct xfrm_address_filter *filter)
2160 INIT_LIST_HEAD(&walk->all);
2161 walk->proto = proto;
2162 walk->state = XFRM_STATE_DEAD;
2164 walk->filter = filter;
2166 EXPORT_SYMBOL(xfrm_state_walk_init);
2168 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2170 kfree(walk->filter);
2172 if (list_empty(&walk->all))
2175 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2176 list_del(&walk->all);
2177 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2179 EXPORT_SYMBOL(xfrm_state_walk_done);
2181 static void xfrm_replay_timer_handler(struct timer_list *t)
2183 struct xfrm_state *x = from_timer(x, t, rtimer);
2185 spin_lock(&x->lock);
2187 if (x->km.state == XFRM_STATE_VALID) {
2188 if (xfrm_aevent_is_on(xs_net(x)))
2189 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
2191 x->xflags |= XFRM_TIME_DEFER;
2194 spin_unlock(&x->lock);
2197 static LIST_HEAD(xfrm_km_list);
2199 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2201 struct xfrm_mgr *km;
2204 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2205 if (km->notify_policy)
2206 km->notify_policy(xp, dir, c);
2210 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2212 struct xfrm_mgr *km;
2214 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2220 EXPORT_SYMBOL(km_policy_notify);
2221 EXPORT_SYMBOL(km_state_notify);
2223 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2229 c.event = XFRM_MSG_EXPIRE;
2230 km_state_notify(x, &c);
2233 EXPORT_SYMBOL(km_state_expired);
2235 * We send to all registered managers regardless of failure
2236 * We are happy with one success
2238 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2240 int err = -EINVAL, acqret;
2241 struct xfrm_mgr *km;
2244 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2245 acqret = km->acquire(x, t, pol);
2252 EXPORT_SYMBOL(km_query);
2254 static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2257 struct xfrm_mgr *km;
2260 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2261 if (km->new_mapping)
2262 err = km->new_mapping(x, ipaddr, sport);
2270 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2274 if (x->mapping_maxage) {
2275 if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
2276 x->new_mapping_sport != sport) {
2277 x->new_mapping_sport = sport;
2278 x->new_mapping = jiffies / HZ;
2279 ret = __km_new_mapping(x, ipaddr, sport);
2282 ret = __km_new_mapping(x, ipaddr, sport);
2287 EXPORT_SYMBOL(km_new_mapping);
2289 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2295 c.event = XFRM_MSG_POLEXPIRE;
2296 km_policy_notify(pol, dir, &c);
2298 EXPORT_SYMBOL(km_policy_expired);
2300 #ifdef CONFIG_XFRM_MIGRATE
2301 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2302 const struct xfrm_migrate *m, int num_migrate,
2303 const struct xfrm_kmaddress *k,
2304 const struct xfrm_encap_tmpl *encap)
2308 struct xfrm_mgr *km;
2311 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2313 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2322 EXPORT_SYMBOL(km_migrate);
2325 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2329 struct xfrm_mgr *km;
2332 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2334 ret = km->report(net, proto, sel, addr);
2342 EXPORT_SYMBOL(km_report);
2344 static bool km_is_alive(const struct km_event *c)
2346 struct xfrm_mgr *km;
2347 bool is_alive = false;
2350 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2351 if (km->is_alive && km->is_alive(c)) {
2361 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2362 static DEFINE_SPINLOCK(xfrm_translator_lock);
2363 static struct xfrm_translator __rcu *xfrm_translator;
2365 struct xfrm_translator *xfrm_get_translator(void)
2367 struct xfrm_translator *xtr;
2370 xtr = rcu_dereference(xfrm_translator);
2373 if (!try_module_get(xtr->owner))
2379 EXPORT_SYMBOL_GPL(xfrm_get_translator);
2381 void xfrm_put_translator(struct xfrm_translator *xtr)
2383 module_put(xtr->owner);
2385 EXPORT_SYMBOL_GPL(xfrm_put_translator);
2387 int xfrm_register_translator(struct xfrm_translator *xtr)
2391 spin_lock_bh(&xfrm_translator_lock);
2392 if (unlikely(xfrm_translator != NULL))
2395 rcu_assign_pointer(xfrm_translator, xtr);
2396 spin_unlock_bh(&xfrm_translator_lock);
2400 EXPORT_SYMBOL_GPL(xfrm_register_translator);
2402 int xfrm_unregister_translator(struct xfrm_translator *xtr)
2406 spin_lock_bh(&xfrm_translator_lock);
2407 if (likely(xfrm_translator != NULL)) {
2408 if (rcu_access_pointer(xfrm_translator) != xtr)
2411 RCU_INIT_POINTER(xfrm_translator, NULL);
2413 spin_unlock_bh(&xfrm_translator_lock);
2418 EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2421 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
2425 struct xfrm_mgr *km;
2426 struct xfrm_policy *pol = NULL;
2428 if (sockptr_is_null(optval) && !optlen) {
2429 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2430 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2435 if (optlen <= 0 || optlen > PAGE_SIZE)
2438 data = memdup_sockptr(optval, optlen);
2440 return PTR_ERR(data);
2442 if (in_compat_syscall()) {
2443 struct xfrm_translator *xtr = xfrm_get_translator();
2450 err = xtr->xlate_user_policy_sockptr(&data, optlen);
2451 xfrm_put_translator(xtr);
2460 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2461 pol = km->compile_policy(sk, optname, data,
2469 xfrm_sk_policy_insert(sk, err, pol);
2478 EXPORT_SYMBOL(xfrm_user_policy);
2480 static DEFINE_SPINLOCK(xfrm_km_lock);
2482 int xfrm_register_km(struct xfrm_mgr *km)
2484 spin_lock_bh(&xfrm_km_lock);
2485 list_add_tail_rcu(&km->list, &xfrm_km_list);
2486 spin_unlock_bh(&xfrm_km_lock);
2489 EXPORT_SYMBOL(xfrm_register_km);
2491 int xfrm_unregister_km(struct xfrm_mgr *km)
2493 spin_lock_bh(&xfrm_km_lock);
2494 list_del_rcu(&km->list);
2495 spin_unlock_bh(&xfrm_km_lock);
2499 EXPORT_SYMBOL(xfrm_unregister_km);
2501 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2505 if (WARN_ON(afinfo->family >= NPROTO))
2506 return -EAFNOSUPPORT;
2508 spin_lock_bh(&xfrm_state_afinfo_lock);
2509 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2512 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2513 spin_unlock_bh(&xfrm_state_afinfo_lock);
2516 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2518 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2520 int err = 0, family = afinfo->family;
2522 if (WARN_ON(family >= NPROTO))
2523 return -EAFNOSUPPORT;
2525 spin_lock_bh(&xfrm_state_afinfo_lock);
2526 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2527 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2530 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2532 spin_unlock_bh(&xfrm_state_afinfo_lock);
2536 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2538 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2540 if (unlikely(family >= NPROTO))
2543 return rcu_dereference(xfrm_state_afinfo[family]);
2545 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
2547 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2549 struct xfrm_state_afinfo *afinfo;
2550 if (unlikely(family >= NPROTO))
2553 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2554 if (unlikely(!afinfo))
2559 void xfrm_flush_gc(void)
2561 flush_work(&xfrm_state_gc_work);
2563 EXPORT_SYMBOL(xfrm_flush_gc);
2565 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2566 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2569 struct xfrm_state *t = x->tunnel;
2571 if (atomic_read(&t->tunnel_users) == 2)
2572 xfrm_state_delete(t);
2573 atomic_dec(&t->tunnel_users);
2574 xfrm_state_put_sync(t);
2578 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2580 u32 __xfrm_state_mtu(struct xfrm_state *x, int mtu)
2582 const struct xfrm_type *type = READ_ONCE(x->type);
2583 struct crypto_aead *aead;
2584 u32 blksize, net_adj = 0;
2586 if (x->km.state != XFRM_STATE_VALID ||
2587 !type || type->proto != IPPROTO_ESP)
2588 return mtu - x->props.header_len;
2591 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2593 switch (x->props.mode) {
2594 case XFRM_MODE_TRANSPORT:
2595 case XFRM_MODE_BEET:
2596 if (x->props.family == AF_INET)
2597 net_adj = sizeof(struct iphdr);
2598 else if (x->props.family == AF_INET6)
2599 net_adj = sizeof(struct ipv6hdr);
2601 case XFRM_MODE_TUNNEL:
2608 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2609 net_adj) & ~(blksize - 1)) + net_adj - 2;
2611 EXPORT_SYMBOL_GPL(__xfrm_state_mtu);
2613 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
2615 mtu = __xfrm_state_mtu(x, mtu);
2617 if (x->props.family == AF_INET6 && mtu < IPV6_MIN_MTU)
2618 return IPV6_MIN_MTU;
2623 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2625 const struct xfrm_mode *inner_mode;
2626 const struct xfrm_mode *outer_mode;
2627 int family = x->props.family;
2630 if (family == AF_INET &&
2631 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2632 x->props.flags |= XFRM_STATE_NOPMTUDISC;
2634 err = -EPROTONOSUPPORT;
2636 if (x->sel.family != AF_UNSPEC) {
2637 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2638 if (inner_mode == NULL)
2641 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2642 family != x->sel.family)
2645 x->inner_mode = *inner_mode;
2647 const struct xfrm_mode *inner_mode_iaf;
2648 int iafamily = AF_INET;
2650 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2651 if (inner_mode == NULL)
2654 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
2657 x->inner_mode = *inner_mode;
2659 if (x->props.family == AF_INET)
2660 iafamily = AF_INET6;
2662 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2663 if (inner_mode_iaf) {
2664 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2665 x->inner_mode_iaf = *inner_mode_iaf;
2669 x->type = xfrm_get_type(x->id.proto, family);
2670 if (x->type == NULL)
2673 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2675 err = x->type->init_state(x);
2679 outer_mode = xfrm_get_mode(x->props.mode, family);
2681 err = -EPROTONOSUPPORT;
2685 x->outer_mode = *outer_mode;
2687 err = xfrm_init_replay(x);
2696 EXPORT_SYMBOL(__xfrm_init_state);
2698 int xfrm_init_state(struct xfrm_state *x)
2702 err = __xfrm_init_state(x, true, false);
2704 x->km.state = XFRM_STATE_VALID;
2709 EXPORT_SYMBOL(xfrm_init_state);
2711 int __net_init xfrm_state_init(struct net *net)
2715 if (net_eq(net, &init_net))
2716 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2717 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2719 INIT_LIST_HEAD(&net->xfrm.state_all);
2721 sz = sizeof(struct hlist_head) * 8;
2723 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2724 if (!net->xfrm.state_bydst)
2726 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2727 if (!net->xfrm.state_bysrc)
2729 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2730 if (!net->xfrm.state_byspi)
2732 net->xfrm.state_byseq = xfrm_hash_alloc(sz);
2733 if (!net->xfrm.state_byseq)
2735 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2737 net->xfrm.state_num = 0;
2738 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2739 spin_lock_init(&net->xfrm.xfrm_state_lock);
2740 seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation,
2741 &net->xfrm.xfrm_state_lock);
2745 xfrm_hash_free(net->xfrm.state_byspi, sz);
2747 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2749 xfrm_hash_free(net->xfrm.state_bydst, sz);
2754 void xfrm_state_fini(struct net *net)
2758 flush_work(&net->xfrm.state_hash_work);
2759 flush_work(&xfrm_state_gc_work);
2760 xfrm_state_flush(net, 0, false, true);
2762 WARN_ON(!list_empty(&net->xfrm.state_all));
2764 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2765 WARN_ON(!hlist_empty(net->xfrm.state_byseq));
2766 xfrm_hash_free(net->xfrm.state_byseq, sz);
2767 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2768 xfrm_hash_free(net->xfrm.state_byspi, sz);
2769 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2770 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2771 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2772 xfrm_hash_free(net->xfrm.state_bydst, sz);
2775 #ifdef CONFIG_AUDITSYSCALL
2776 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2777 struct audit_buffer *audit_buf)
2779 struct xfrm_sec_ctx *ctx = x->security;
2780 u32 spi = ntohl(x->id.spi);
2783 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2784 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2786 switch (x->props.family) {
2788 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2789 &x->props.saddr.a4, &x->id.daddr.a4);
2792 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2793 x->props.saddr.a6, x->id.daddr.a6);
2797 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2800 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2801 struct audit_buffer *audit_buf)
2803 const struct iphdr *iph4;
2804 const struct ipv6hdr *iph6;
2809 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2810 &iph4->saddr, &iph4->daddr);
2813 iph6 = ipv6_hdr(skb);
2814 audit_log_format(audit_buf,
2815 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2816 &iph6->saddr, &iph6->daddr,
2817 iph6->flow_lbl[0] & 0x0f,
2824 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2826 struct audit_buffer *audit_buf;
2828 audit_buf = xfrm_audit_start("SAD-add");
2829 if (audit_buf == NULL)
2831 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2832 xfrm_audit_helper_sainfo(x, audit_buf);
2833 audit_log_format(audit_buf, " res=%u", result);
2834 audit_log_end(audit_buf);
2836 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2838 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2840 struct audit_buffer *audit_buf;
2842 audit_buf = xfrm_audit_start("SAD-delete");
2843 if (audit_buf == NULL)
2845 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2846 xfrm_audit_helper_sainfo(x, audit_buf);
2847 audit_log_format(audit_buf, " res=%u", result);
2848 audit_log_end(audit_buf);
2850 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2852 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2853 struct sk_buff *skb)
2855 struct audit_buffer *audit_buf;
2858 audit_buf = xfrm_audit_start("SA-replay-overflow");
2859 if (audit_buf == NULL)
2861 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2862 /* don't record the sequence number because it's inherent in this kind
2863 * of audit message */
2864 spi = ntohl(x->id.spi);
2865 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2866 audit_log_end(audit_buf);
2868 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2870 void xfrm_audit_state_replay(struct xfrm_state *x,
2871 struct sk_buff *skb, __be32 net_seq)
2873 struct audit_buffer *audit_buf;
2876 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2877 if (audit_buf == NULL)
2879 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2880 spi = ntohl(x->id.spi);
2881 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2882 spi, spi, ntohl(net_seq));
2883 audit_log_end(audit_buf);
2885 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2887 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2889 struct audit_buffer *audit_buf;
2891 audit_buf = xfrm_audit_start("SA-notfound");
2892 if (audit_buf == NULL)
2894 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2895 audit_log_end(audit_buf);
2897 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2899 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2900 __be32 net_spi, __be32 net_seq)
2902 struct audit_buffer *audit_buf;
2905 audit_buf = xfrm_audit_start("SA-notfound");
2906 if (audit_buf == NULL)
2908 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2909 spi = ntohl(net_spi);
2910 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2911 spi, spi, ntohl(net_seq));
2912 audit_log_end(audit_buf);
2914 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2916 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2917 struct sk_buff *skb, u8 proto)
2919 struct audit_buffer *audit_buf;
2923 audit_buf = xfrm_audit_start("SA-icv-failure");
2924 if (audit_buf == NULL)
2926 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2927 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2928 u32 spi = ntohl(net_spi);
2929 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2930 spi, spi, ntohl(net_seq));
2932 audit_log_end(audit_buf);
2934 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2935 #endif /* CONFIG_AUDITSYSCALL */