1 // SPDX-License-Identifier: GPL-2.0-only
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific functions
12 * Derek Atkins <derek@ihtfp.com>
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 #define XFRM_STATE_INSERT(by, _n, _h, _type) \
89 struct xfrm_state *_x = NULL; \
91 if (_type != XFRM_DEV_OFFLOAD_PACKET) { \
92 hlist_for_each_entry_rcu(_x, _h, by) { \
93 if (_x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
99 if (!_x || _x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
100 /* SAD is empty or consist from HW SAs only */ \
101 hlist_add_head_rcu(_n, _h); \
103 hlist_add_before_rcu(_n, &_x->by); \
106 static void xfrm_hash_transfer(struct hlist_head *list,
107 struct hlist_head *ndsttable,
108 struct hlist_head *nsrctable,
109 struct hlist_head *nspitable,
110 struct hlist_head *nseqtable,
111 unsigned int nhashmask)
113 struct hlist_node *tmp;
114 struct xfrm_state *x;
116 hlist_for_each_entry_safe(x, tmp, list, bydst) {
119 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
120 x->props.reqid, x->props.family,
122 XFRM_STATE_INSERT(bydst, &x->bydst, ndsttable + h, x->xso.type);
124 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
127 XFRM_STATE_INSERT(bysrc, &x->bysrc, nsrctable + h, x->xso.type);
130 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
131 x->id.proto, x->props.family,
133 XFRM_STATE_INSERT(byspi, &x->byspi, nspitable + h,
138 h = __xfrm_seq_hash(x->km.seq, nhashmask);
139 XFRM_STATE_INSERT(byseq, &x->byseq, nseqtable + h,
145 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
147 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
150 static void xfrm_hash_resize(struct work_struct *work)
152 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
153 struct hlist_head *ndst, *nsrc, *nspi, *nseq, *odst, *osrc, *ospi, *oseq;
154 unsigned long nsize, osize;
155 unsigned int nhashmask, ohashmask;
158 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
159 ndst = xfrm_hash_alloc(nsize);
162 nsrc = xfrm_hash_alloc(nsize);
164 xfrm_hash_free(ndst, nsize);
167 nspi = xfrm_hash_alloc(nsize);
169 xfrm_hash_free(ndst, nsize);
170 xfrm_hash_free(nsrc, nsize);
173 nseq = xfrm_hash_alloc(nsize);
175 xfrm_hash_free(ndst, nsize);
176 xfrm_hash_free(nsrc, nsize);
177 xfrm_hash_free(nspi, nsize);
181 spin_lock_bh(&net->xfrm.xfrm_state_lock);
182 write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
184 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
185 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
186 for (i = net->xfrm.state_hmask; i >= 0; i--)
187 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nseq, nhashmask);
189 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
190 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
191 oseq = xfrm_state_deref_prot(net->xfrm.state_byseq, net);
192 ohashmask = net->xfrm.state_hmask;
194 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
195 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
196 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
197 rcu_assign_pointer(net->xfrm.state_byseq, nseq);
198 net->xfrm.state_hmask = nhashmask;
200 write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
201 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
203 osize = (ohashmask + 1) * sizeof(struct hlist_head);
207 xfrm_hash_free(odst, osize);
208 xfrm_hash_free(osrc, osize);
209 xfrm_hash_free(ospi, osize);
210 xfrm_hash_free(oseq, osize);
213 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
214 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
216 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
218 int __xfrm_state_delete(struct xfrm_state *x);
220 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
221 static bool km_is_alive(const struct km_event *c);
222 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
224 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
226 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
230 return -EAFNOSUPPORT;
232 #define X(afi, T, name) do { \
233 WARN_ON((afi)->type_ ## name); \
234 (afi)->type_ ## name = (T); \
237 switch (type->proto) {
239 X(afinfo, type, comp);
245 X(afinfo, type, esp);
248 X(afinfo, type, ipip);
250 case IPPROTO_DSTOPTS:
251 X(afinfo, type, dstopts);
253 case IPPROTO_ROUTING:
254 X(afinfo, type, routing);
257 X(afinfo, type, ipip6);
261 err = -EPROTONOSUPPORT;
268 EXPORT_SYMBOL(xfrm_register_type);
270 void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
272 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
274 if (unlikely(afinfo == NULL))
277 #define X(afi, T, name) do { \
278 WARN_ON((afi)->type_ ## name != (T)); \
279 (afi)->type_ ## name = NULL; \
282 switch (type->proto) {
284 X(afinfo, type, comp);
290 X(afinfo, type, esp);
293 X(afinfo, type, ipip);
295 case IPPROTO_DSTOPTS:
296 X(afinfo, type, dstopts);
298 case IPPROTO_ROUTING:
299 X(afinfo, type, routing);
302 X(afinfo, type, ipip6);
311 EXPORT_SYMBOL(xfrm_unregister_type);
313 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
315 const struct xfrm_type *type = NULL;
316 struct xfrm_state_afinfo *afinfo;
317 int modload_attempted = 0;
320 afinfo = xfrm_state_get_afinfo(family);
321 if (unlikely(afinfo == NULL))
326 type = afinfo->type_comp;
329 type = afinfo->type_ah;
332 type = afinfo->type_esp;
335 type = afinfo->type_ipip;
337 case IPPROTO_DSTOPTS:
338 type = afinfo->type_dstopts;
340 case IPPROTO_ROUTING:
341 type = afinfo->type_routing;
344 type = afinfo->type_ipip6;
350 if (unlikely(type && !try_module_get(type->owner)))
355 if (!type && !modload_attempted) {
356 request_module("xfrm-type-%d-%d", family, proto);
357 modload_attempted = 1;
364 static void xfrm_put_type(const struct xfrm_type *type)
366 module_put(type->owner);
369 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
370 unsigned short family)
372 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
375 if (unlikely(afinfo == NULL))
376 return -EAFNOSUPPORT;
378 switch (type->proto) {
380 WARN_ON(afinfo->type_offload_esp);
381 afinfo->type_offload_esp = type;
385 err = -EPROTONOSUPPORT;
392 EXPORT_SYMBOL(xfrm_register_type_offload);
394 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
395 unsigned short family)
397 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
399 if (unlikely(afinfo == NULL))
402 switch (type->proto) {
404 WARN_ON(afinfo->type_offload_esp != type);
405 afinfo->type_offload_esp = NULL;
413 EXPORT_SYMBOL(xfrm_unregister_type_offload);
415 static const struct xfrm_type_offload *
416 xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
418 const struct xfrm_type_offload *type = NULL;
419 struct xfrm_state_afinfo *afinfo;
422 afinfo = xfrm_state_get_afinfo(family);
423 if (unlikely(afinfo == NULL))
428 type = afinfo->type_offload_esp;
434 if ((type && !try_module_get(type->owner)))
439 if (!type && try_load) {
440 request_module("xfrm-offload-%d-%d", family, proto);
448 static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
450 module_put(type->owner);
453 static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
455 .encap = XFRM_MODE_BEET,
456 .flags = XFRM_MODE_FLAG_TUNNEL,
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 xfrm6_mode_map[XFRM_MODE_MAX] = {
472 .encap = XFRM_MODE_BEET,
473 .flags = XFRM_MODE_FLAG_TUNNEL,
476 [XFRM_MODE_ROUTEOPTIMIZATION] = {
477 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
480 [XFRM_MODE_TRANSPORT] = {
481 .encap = XFRM_MODE_TRANSPORT,
484 [XFRM_MODE_TUNNEL] = {
485 .encap = XFRM_MODE_TUNNEL,
486 .flags = XFRM_MODE_FLAG_TUNNEL,
491 static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
493 const struct xfrm_mode *mode;
495 if (unlikely(encap >= XFRM_MODE_MAX))
500 mode = &xfrm4_mode_map[encap];
501 if (mode->family == family)
505 mode = &xfrm6_mode_map[encap];
506 if (mode->family == family)
516 void xfrm_state_free(struct xfrm_state *x)
518 kmem_cache_free(xfrm_state_cache, x);
520 EXPORT_SYMBOL(xfrm_state_free);
522 static void ___xfrm_state_destroy(struct xfrm_state *x)
524 hrtimer_cancel(&x->mtimer);
525 del_timer_sync(&x->rtimer);
532 kfree(x->replay_esn);
533 kfree(x->preplay_esn);
535 xfrm_put_type_offload(x->type_offload);
537 x->type->destructor(x);
538 xfrm_put_type(x->type);
541 put_page(x->xfrag.page);
542 xfrm_dev_state_free(x);
543 security_xfrm_state_free(x);
547 static void xfrm_state_gc_task(struct work_struct *work)
549 struct xfrm_state *x;
550 struct hlist_node *tmp;
551 struct hlist_head gc_list;
553 spin_lock_bh(&xfrm_state_gc_lock);
554 hlist_move_list(&xfrm_state_gc_list, &gc_list);
555 spin_unlock_bh(&xfrm_state_gc_lock);
559 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
560 ___xfrm_state_destroy(x);
563 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
565 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
566 enum hrtimer_restart ret = HRTIMER_NORESTART;
567 time64_t now = ktime_get_real_seconds();
568 time64_t next = TIME64_MAX;
573 xfrm_dev_state_update_curlft(x);
575 if (x->km.state == XFRM_STATE_DEAD)
577 if (x->km.state == XFRM_STATE_EXPIRED)
579 if (x->lft.hard_add_expires_seconds) {
580 time64_t tmo = x->lft.hard_add_expires_seconds +
581 x->curlft.add_time - now;
583 if (x->xflags & XFRM_SOFT_EXPIRE) {
584 /* enter hard expire without soft expire first?!
585 * setting a new date could trigger this.
586 * workaround: fix x->curflt.add_time by below:
588 x->curlft.add_time = now - x->saved_tmo - 1;
589 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
596 if (x->lft.hard_use_expires_seconds) {
597 time64_t tmo = x->lft.hard_use_expires_seconds +
598 (READ_ONCE(x->curlft.use_time) ? : now) - now;
606 if (x->lft.soft_add_expires_seconds) {
607 time64_t tmo = x->lft.soft_add_expires_seconds +
608 x->curlft.add_time - now;
611 x->xflags &= ~XFRM_SOFT_EXPIRE;
612 } else if (tmo < next) {
614 x->xflags |= XFRM_SOFT_EXPIRE;
618 if (x->lft.soft_use_expires_seconds) {
619 time64_t tmo = x->lft.soft_use_expires_seconds +
620 (READ_ONCE(x->curlft.use_time) ? : now) - now;
629 km_state_expired(x, 0, 0);
631 if (next != TIME64_MAX) {
632 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
633 ret = HRTIMER_RESTART;
639 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
640 x->km.state = XFRM_STATE_EXPIRED;
642 err = __xfrm_state_delete(x);
644 km_state_expired(x, 1, 0);
646 xfrm_audit_state_delete(x, err ? 0 : 1, true);
649 spin_unlock(&x->lock);
653 static void xfrm_replay_timer_handler(struct timer_list *t);
655 struct xfrm_state *xfrm_state_alloc(struct net *net)
657 struct xfrm_state *x;
659 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
662 write_pnet(&x->xs_net, net);
663 refcount_set(&x->refcnt, 1);
664 atomic_set(&x->tunnel_users, 0);
665 INIT_LIST_HEAD(&x->km.all);
666 INIT_HLIST_NODE(&x->bydst);
667 INIT_HLIST_NODE(&x->bysrc);
668 INIT_HLIST_NODE(&x->byspi);
669 INIT_HLIST_NODE(&x->byseq);
670 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
671 x->mtimer.function = xfrm_timer_handler;
672 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
673 x->curlft.add_time = ktime_get_real_seconds();
674 x->lft.soft_byte_limit = XFRM_INF;
675 x->lft.soft_packet_limit = XFRM_INF;
676 x->lft.hard_byte_limit = XFRM_INF;
677 x->lft.hard_packet_limit = XFRM_INF;
678 x->replay_maxage = 0;
679 x->replay_maxdiff = 0;
680 spin_lock_init(&x->lock);
684 EXPORT_SYMBOL(xfrm_state_alloc);
686 void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
688 WARN_ON(x->km.state != XFRM_STATE_DEAD);
692 ___xfrm_state_destroy(x);
694 spin_lock_bh(&xfrm_state_gc_lock);
695 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
696 spin_unlock_bh(&xfrm_state_gc_lock);
697 schedule_work(&xfrm_state_gc_work);
700 EXPORT_SYMBOL(__xfrm_state_destroy);
702 int __xfrm_state_delete(struct xfrm_state *x)
704 struct net *net = xs_net(x);
707 if (x->km.state != XFRM_STATE_DEAD) {
708 x->km.state = XFRM_STATE_DEAD;
709 spin_lock(&net->xfrm.xfrm_state_lock);
710 list_del(&x->km.all);
711 hlist_del_rcu(&x->bydst);
712 hlist_del_rcu(&x->bysrc);
714 hlist_del_rcu(&x->byseq);
716 hlist_del_rcu(&x->byspi);
717 net->xfrm.state_num--;
718 spin_unlock(&net->xfrm.xfrm_state_lock);
721 sock_put(rcu_dereference_raw(x->encap_sk));
723 xfrm_dev_state_delete(x);
725 /* All xfrm_state objects are created by xfrm_state_alloc.
726 * The xfrm_state_alloc call gives a reference, and that
727 * is what we are dropping here.
735 EXPORT_SYMBOL(__xfrm_state_delete);
737 int xfrm_state_delete(struct xfrm_state *x)
741 spin_lock_bh(&x->lock);
742 err = __xfrm_state_delete(x);
743 spin_unlock_bh(&x->lock);
747 EXPORT_SYMBOL(xfrm_state_delete);
749 #ifdef CONFIG_SECURITY_NETWORK_XFRM
751 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
755 for (i = 0; i <= net->xfrm.state_hmask; i++) {
756 struct xfrm_state *x;
758 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
759 if (xfrm_id_proto_match(x->id.proto, proto) &&
760 (err = security_xfrm_state_delete(x)) != 0) {
761 xfrm_audit_state_delete(x, 0, task_valid);
771 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
775 for (i = 0; i <= net->xfrm.state_hmask; i++) {
776 struct xfrm_state *x;
777 struct xfrm_dev_offload *xso;
779 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
782 if (xso->dev == dev &&
783 (err = security_xfrm_state_delete(x)) != 0) {
784 xfrm_audit_state_delete(x, 0, task_valid);
794 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
800 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
806 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
808 int i, err = 0, cnt = 0;
810 spin_lock_bh(&net->xfrm.xfrm_state_lock);
811 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
816 for (i = 0; i <= net->xfrm.state_hmask; i++) {
817 struct xfrm_state *x;
819 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
820 if (!xfrm_state_kern(x) &&
821 xfrm_id_proto_match(x->id.proto, proto)) {
823 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
825 err = xfrm_state_delete(x);
826 xfrm_audit_state_delete(x, err ? 0 : 1,
829 xfrm_state_put_sync(x);
835 spin_lock_bh(&net->xfrm.xfrm_state_lock);
841 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
847 EXPORT_SYMBOL(xfrm_state_flush);
849 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
851 int i, err = 0, cnt = 0;
853 spin_lock_bh(&net->xfrm.xfrm_state_lock);
854 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
859 for (i = 0; i <= net->xfrm.state_hmask; i++) {
860 struct xfrm_state *x;
861 struct xfrm_dev_offload *xso;
863 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
866 if (!xfrm_state_kern(x) && xso->dev == dev) {
868 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
870 err = xfrm_state_delete(x);
871 xfrm_audit_state_delete(x, err ? 0 : 1,
877 spin_lock_bh(&net->xfrm.xfrm_state_lock);
886 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
889 EXPORT_SYMBOL(xfrm_dev_state_flush);
891 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
893 spin_lock_bh(&net->xfrm.xfrm_state_lock);
894 si->sadcnt = net->xfrm.state_num;
895 si->sadhcnt = net->xfrm.state_hmask + 1;
896 si->sadhmcnt = xfrm_state_hashmax;
897 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
899 EXPORT_SYMBOL(xfrm_sad_getinfo);
902 __xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
904 const struct flowi4 *fl4 = &fl->u.ip4;
906 sel->daddr.a4 = fl4->daddr;
907 sel->saddr.a4 = fl4->saddr;
908 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
909 sel->dport_mask = htons(0xffff);
910 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
911 sel->sport_mask = htons(0xffff);
912 sel->family = AF_INET;
913 sel->prefixlen_d = 32;
914 sel->prefixlen_s = 32;
915 sel->proto = fl4->flowi4_proto;
916 sel->ifindex = fl4->flowi4_oif;
920 __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
922 const struct flowi6 *fl6 = &fl->u.ip6;
924 /* Initialize temporary selector matching only to current session. */
925 *(struct in6_addr *)&sel->daddr = fl6->daddr;
926 *(struct in6_addr *)&sel->saddr = fl6->saddr;
927 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
928 sel->dport_mask = htons(0xffff);
929 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
930 sel->sport_mask = htons(0xffff);
931 sel->family = AF_INET6;
932 sel->prefixlen_d = 128;
933 sel->prefixlen_s = 128;
934 sel->proto = fl6->flowi6_proto;
935 sel->ifindex = fl6->flowi6_oif;
939 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
940 const struct xfrm_tmpl *tmpl,
941 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
942 unsigned short family)
946 __xfrm4_init_tempsel(&x->sel, fl);
949 __xfrm6_init_tempsel(&x->sel, fl);
955 switch (tmpl->encap_family) {
957 if (x->id.daddr.a4 == 0)
958 x->id.daddr.a4 = daddr->a4;
959 x->props.saddr = tmpl->saddr;
960 if (x->props.saddr.a4 == 0)
961 x->props.saddr.a4 = saddr->a4;
964 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
965 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
966 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
967 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
968 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
972 x->props.mode = tmpl->mode;
973 x->props.reqid = tmpl->reqid;
974 x->props.family = tmpl->encap_family;
977 static struct xfrm_state *__xfrm_state_lookup_all(struct net *net, u32 mark,
978 const xfrm_address_t *daddr,
979 __be32 spi, u8 proto,
980 unsigned short family,
981 struct xfrm_dev_offload *xdo)
983 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
984 struct xfrm_state *x;
986 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
987 #ifdef CONFIG_XFRM_OFFLOAD
988 if (xdo->type == XFRM_DEV_OFFLOAD_PACKET) {
989 if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
990 /* HW states are in the head of list, there is
991 * no need to iterate further.
995 /* Packet offload: both policy and SA should
998 if (xdo->dev != x->xso.dev)
1000 } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
1001 /* Skip HW policy for SW lookups */
1004 if (x->props.family != family ||
1006 x->id.proto != proto ||
1007 !xfrm_addr_equal(&x->id.daddr, daddr, family))
1010 if ((mark & x->mark.m) != x->mark.v)
1012 if (!xfrm_state_hold_rcu(x))
1020 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
1021 const xfrm_address_t *daddr,
1022 __be32 spi, u8 proto,
1023 unsigned short family)
1025 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
1026 struct xfrm_state *x;
1028 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
1029 if (x->props.family != family ||
1031 x->id.proto != proto ||
1032 !xfrm_addr_equal(&x->id.daddr, daddr, family))
1035 if ((mark & x->mark.m) != x->mark.v)
1037 if (!xfrm_state_hold_rcu(x))
1045 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1046 const xfrm_address_t *daddr,
1047 const xfrm_address_t *saddr,
1048 u8 proto, unsigned short family)
1050 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
1051 struct xfrm_state *x;
1053 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
1054 if (x->props.family != family ||
1055 x->id.proto != proto ||
1056 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1057 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1060 if ((mark & x->mark.m) != x->mark.v)
1062 if (!xfrm_state_hold_rcu(x))
1070 static inline struct xfrm_state *
1071 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
1073 struct net *net = xs_net(x);
1074 u32 mark = x->mark.v & x->mark.m;
1077 return __xfrm_state_lookup(net, mark, &x->id.daddr,
1078 x->id.spi, x->id.proto, family);
1080 return __xfrm_state_lookup_byaddr(net, mark,
1083 x->id.proto, family);
1086 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
1088 if (have_hash_collision &&
1089 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
1090 net->xfrm.state_num > net->xfrm.state_hmask)
1091 schedule_work(&net->xfrm.state_hash_work);
1094 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
1095 const struct flowi *fl, unsigned short family,
1096 struct xfrm_state **best, int *acq_in_progress,
1099 /* Resolution logic:
1100 * 1. There is a valid state with matching selector. Done.
1101 * 2. Valid state with inappropriate selector. Skip.
1103 * Entering area of "sysdeps".
1105 * 3. If state is not valid, selector is temporary, it selects
1106 * only session which triggered previous resolution. Key
1107 * manager will do something to install a state with proper
1110 if (x->km.state == XFRM_STATE_VALID) {
1111 if ((x->sel.family &&
1112 (x->sel.family != family ||
1113 !xfrm_selector_match(&x->sel, fl, family))) ||
1114 !security_xfrm_state_pol_flow_match(x, pol,
1115 &fl->u.__fl_common))
1119 (*best)->km.dying > x->km.dying ||
1120 ((*best)->km.dying == x->km.dying &&
1121 (*best)->curlft.add_time < x->curlft.add_time))
1123 } else if (x->km.state == XFRM_STATE_ACQ) {
1124 *acq_in_progress = 1;
1125 } else if (x->km.state == XFRM_STATE_ERROR ||
1126 x->km.state == XFRM_STATE_EXPIRED) {
1127 if ((!x->sel.family ||
1128 (x->sel.family == family &&
1129 xfrm_selector_match(&x->sel, fl, family))) &&
1130 security_xfrm_state_pol_flow_match(x, pol,
1131 &fl->u.__fl_common))
1137 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1138 const struct flowi *fl, struct xfrm_tmpl *tmpl,
1139 struct xfrm_policy *pol, int *err,
1140 unsigned short family, u32 if_id)
1142 static xfrm_address_t saddr_wildcard = { };
1143 struct net *net = xp_net(pol);
1144 unsigned int h, h_wildcard;
1145 struct xfrm_state *x, *x0, *to_put;
1146 int acquire_in_progress = 0;
1148 struct xfrm_state *best = NULL;
1149 u32 mark = pol->mark.v & pol->mark.m;
1150 unsigned short encap_family = tmpl->encap_family;
1151 unsigned int sequence;
1156 sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
1159 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
1160 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
1161 #ifdef CONFIG_XFRM_OFFLOAD
1162 if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
1163 if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
1164 /* HW states are in the head of list, there is
1165 * no need to iterate further.
1169 /* Packet offload: both policy and SA should
1172 if (pol->xdo.dev != x->xso.dev)
1174 } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
1175 /* Skip HW policy for SW lookups */
1178 if (x->props.family == encap_family &&
1179 x->props.reqid == tmpl->reqid &&
1180 (mark & x->mark.m) == x->mark.v &&
1181 x->if_id == if_id &&
1182 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1183 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1184 tmpl->mode == x->props.mode &&
1185 tmpl->id.proto == x->id.proto &&
1186 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1187 xfrm_state_look_at(pol, x, fl, family,
1188 &best, &acquire_in_progress, &error);
1190 if (best || acquire_in_progress)
1193 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
1194 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
1195 #ifdef CONFIG_XFRM_OFFLOAD
1196 if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
1197 if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
1198 /* HW states are in the head of list, there is
1199 * no need to iterate further.
1203 /* Packet offload: both policy and SA should
1206 if (pol->xdo.dev != x->xso.dev)
1208 } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
1209 /* Skip HW policy for SW lookups */
1212 if (x->props.family == encap_family &&
1213 x->props.reqid == tmpl->reqid &&
1214 (mark & x->mark.m) == x->mark.v &&
1215 x->if_id == if_id &&
1216 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1217 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1218 tmpl->mode == x->props.mode &&
1219 tmpl->id.proto == x->id.proto &&
1220 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1221 xfrm_state_look_at(pol, x, fl, family,
1222 &best, &acquire_in_progress, &error);
1227 if (!x && !error && !acquire_in_progress) {
1229 (x0 = __xfrm_state_lookup_all(net, mark, daddr,
1230 tmpl->id.spi, tmpl->id.proto,
1232 &pol->xdo)) != NULL) {
1239 /* If the KMs have no listeners (yet...), avoid allocating an SA
1240 * for each and every packet - garbage collection might not
1243 if (!km_is_alive(&c)) {
1248 x = xfrm_state_alloc(net);
1253 /* Initialize temporary state matching only
1254 * to current session. */
1255 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1256 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1259 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1261 x->km.state = XFRM_STATE_DEAD;
1266 #ifdef CONFIG_XFRM_OFFLOAD
1267 if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
1268 struct xfrm_dev_offload *xdo = &pol->xdo;
1269 struct xfrm_dev_offload *xso = &x->xso;
1271 xso->type = XFRM_DEV_OFFLOAD_PACKET;
1272 xso->dir = xdo->dir;
1273 xso->dev = xdo->dev;
1274 xso->real_dev = xdo->real_dev;
1275 xso->flags = XFRM_DEV_OFFLOAD_FLAG_ACQ;
1276 netdev_tracker_alloc(xso->dev, &xso->dev_tracker,
1278 error = xso->dev->xfrmdev_ops->xdo_dev_state_add(x, NULL);
1281 netdev_put(xso->dev, &xso->dev_tracker);
1283 xso->real_dev = NULL;
1284 xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
1285 x->km.state = XFRM_STATE_DEAD;
1292 if (km_query(x, tmpl, pol) == 0) {
1293 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1294 x->km.state = XFRM_STATE_ACQ;
1295 list_add(&x->km.all, &net->xfrm.state_all);
1296 XFRM_STATE_INSERT(bydst, &x->bydst,
1297 net->xfrm.state_bydst + h,
1299 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1300 XFRM_STATE_INSERT(bysrc, &x->bysrc,
1301 net->xfrm.state_bysrc + h,
1304 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1305 XFRM_STATE_INSERT(byspi, &x->byspi,
1306 net->xfrm.state_byspi + h,
1310 h = xfrm_seq_hash(net, x->km.seq);
1311 XFRM_STATE_INSERT(byseq, &x->byseq,
1312 net->xfrm.state_byseq + h,
1315 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1316 hrtimer_start(&x->mtimer,
1317 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1318 HRTIMER_MODE_REL_SOFT);
1319 net->xfrm.state_num++;
1320 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1321 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1323 #ifdef CONFIG_XFRM_OFFLOAD
1324 struct xfrm_dev_offload *xso = &x->xso;
1326 if (xso->type == XFRM_DEV_OFFLOAD_PACKET) {
1327 xso->dev->xfrmdev_ops->xdo_dev_state_delete(x);
1329 netdev_put(xso->dev, &xso->dev_tracker);
1331 xso->real_dev = NULL;
1332 xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
1335 x->km.state = XFRM_STATE_DEAD;
1343 if (!xfrm_state_hold_rcu(x)) {
1348 *err = acquire_in_progress ? -EAGAIN : error;
1352 xfrm_state_put(to_put);
1354 if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
1366 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1367 xfrm_address_t *daddr, xfrm_address_t *saddr,
1368 unsigned short family, u8 mode, u8 proto, u32 reqid)
1371 struct xfrm_state *rx = NULL, *x = NULL;
1373 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1374 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1375 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1376 if (x->props.family == family &&
1377 x->props.reqid == reqid &&
1378 (mark & x->mark.m) == x->mark.v &&
1379 x->if_id == if_id &&
1380 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1381 xfrm_state_addr_check(x, daddr, saddr, family) &&
1382 mode == x->props.mode &&
1383 proto == x->id.proto &&
1384 x->km.state == XFRM_STATE_VALID) {
1391 xfrm_state_hold(rx);
1392 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1397 EXPORT_SYMBOL(xfrm_stateonly_find);
1399 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1400 unsigned short family)
1402 struct xfrm_state *x;
1403 struct xfrm_state_walk *w;
1405 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1406 list_for_each_entry(w, &net->xfrm.state_all, all) {
1407 x = container_of(w, struct xfrm_state, km);
1408 if (x->props.family != family ||
1413 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1416 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1419 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1421 static void __xfrm_state_insert(struct xfrm_state *x)
1423 struct net *net = xs_net(x);
1426 list_add(&x->km.all, &net->xfrm.state_all);
1428 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1429 x->props.reqid, x->props.family);
1430 XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
1433 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1434 XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
1438 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1441 XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
1446 h = xfrm_seq_hash(net, x->km.seq);
1448 XFRM_STATE_INSERT(byseq, &x->byseq, net->xfrm.state_byseq + h,
1452 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
1453 if (x->replay_maxage)
1454 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1456 net->xfrm.state_num++;
1458 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1461 /* net->xfrm.xfrm_state_lock is held */
1462 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1464 struct net *net = xs_net(xnew);
1465 unsigned short family = xnew->props.family;
1466 u32 reqid = xnew->props.reqid;
1467 struct xfrm_state *x;
1469 u32 mark = xnew->mark.v & xnew->mark.m;
1470 u32 if_id = xnew->if_id;
1472 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1473 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1474 if (x->props.family == family &&
1475 x->props.reqid == reqid &&
1476 x->if_id == if_id &&
1477 (mark & x->mark.m) == x->mark.v &&
1478 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1479 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1484 void xfrm_state_insert(struct xfrm_state *x)
1486 struct net *net = xs_net(x);
1488 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1489 __xfrm_state_bump_genids(x);
1490 __xfrm_state_insert(x);
1491 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1493 EXPORT_SYMBOL(xfrm_state_insert);
1495 /* net->xfrm.xfrm_state_lock is held */
1496 static struct xfrm_state *__find_acq_core(struct net *net,
1497 const struct xfrm_mark *m,
1498 unsigned short family, u8 mode,
1499 u32 reqid, u32 if_id, u8 proto,
1500 const xfrm_address_t *daddr,
1501 const xfrm_address_t *saddr,
1504 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1505 struct xfrm_state *x;
1506 u32 mark = m->v & m->m;
1508 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1509 if (x->props.reqid != reqid ||
1510 x->props.mode != mode ||
1511 x->props.family != family ||
1512 x->km.state != XFRM_STATE_ACQ ||
1514 x->id.proto != proto ||
1515 (mark & x->mark.m) != x->mark.v ||
1516 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1517 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1527 x = xfrm_state_alloc(net);
1531 x->sel.daddr.a4 = daddr->a4;
1532 x->sel.saddr.a4 = saddr->a4;
1533 x->sel.prefixlen_d = 32;
1534 x->sel.prefixlen_s = 32;
1535 x->props.saddr.a4 = saddr->a4;
1536 x->id.daddr.a4 = daddr->a4;
1540 x->sel.daddr.in6 = daddr->in6;
1541 x->sel.saddr.in6 = saddr->in6;
1542 x->sel.prefixlen_d = 128;
1543 x->sel.prefixlen_s = 128;
1544 x->props.saddr.in6 = saddr->in6;
1545 x->id.daddr.in6 = daddr->in6;
1549 x->km.state = XFRM_STATE_ACQ;
1550 x->id.proto = proto;
1551 x->props.family = family;
1552 x->props.mode = mode;
1553 x->props.reqid = reqid;
1557 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1559 hrtimer_start(&x->mtimer,
1560 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1561 HRTIMER_MODE_REL_SOFT);
1562 list_add(&x->km.all, &net->xfrm.state_all);
1563 XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
1565 h = xfrm_src_hash(net, daddr, saddr, family);
1566 XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
1569 net->xfrm.state_num++;
1571 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1577 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1579 int xfrm_state_add(struct xfrm_state *x)
1581 struct net *net = xs_net(x);
1582 struct xfrm_state *x1, *to_put;
1585 u32 mark = x->mark.v & x->mark.m;
1586 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1588 family = x->props.family;
1592 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1594 x1 = __xfrm_state_locate(x, use_spi, family);
1602 if (use_spi && x->km.seq) {
1603 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1604 if (x1 && ((x1->id.proto != x->id.proto) ||
1605 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1612 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1613 x->props.reqid, x->if_id, x->id.proto,
1614 &x->id.daddr, &x->props.saddr, 0);
1616 __xfrm_state_bump_genids(x);
1617 __xfrm_state_insert(x);
1621 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1624 xfrm_state_delete(x1);
1629 xfrm_state_put(to_put);
1633 EXPORT_SYMBOL(xfrm_state_add);
1635 #ifdef CONFIG_XFRM_MIGRATE
1636 static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1638 struct xfrm_user_sec_ctx *uctx;
1639 int size = sizeof(*uctx) + security->ctx_len;
1642 uctx = kmalloc(size, GFP_KERNEL);
1646 uctx->exttype = XFRMA_SEC_CTX;
1648 uctx->ctx_doi = security->ctx_doi;
1649 uctx->ctx_alg = security->ctx_alg;
1650 uctx->ctx_len = security->ctx_len;
1651 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1652 err = security_xfrm_state_alloc(x, uctx);
1660 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1661 struct xfrm_encap_tmpl *encap)
1663 struct net *net = xs_net(orig);
1664 struct xfrm_state *x = xfrm_state_alloc(net);
1668 memcpy(&x->id, &orig->id, sizeof(x->id));
1669 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1670 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1671 x->props.mode = orig->props.mode;
1672 x->props.replay_window = orig->props.replay_window;
1673 x->props.reqid = orig->props.reqid;
1674 x->props.family = orig->props.family;
1675 x->props.saddr = orig->props.saddr;
1678 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1682 x->props.aalgo = orig->props.aalgo;
1685 x->aead = xfrm_algo_aead_clone(orig->aead);
1686 x->geniv = orig->geniv;
1691 x->ealg = xfrm_algo_clone(orig->ealg);
1695 x->props.ealgo = orig->props.ealgo;
1698 x->calg = xfrm_algo_clone(orig->calg);
1702 x->props.calgo = orig->props.calgo;
1704 if (encap || orig->encap) {
1706 x->encap = kmemdup(encap, sizeof(*x->encap),
1709 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1717 if (clone_security(x, orig->security))
1721 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1727 if (orig->replay_esn) {
1728 if (xfrm_replay_clone(x, orig))
1732 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1733 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
1735 x->props.flags = orig->props.flags;
1736 x->props.extra_flags = orig->props.extra_flags;
1738 x->if_id = orig->if_id;
1739 x->tfcpad = orig->tfcpad;
1740 x->replay_maxdiff = orig->replay_maxdiff;
1741 x->replay_maxage = orig->replay_maxage;
1742 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
1743 x->km.state = orig->km.state;
1744 x->km.seq = orig->km.seq;
1745 x->replay = orig->replay;
1746 x->preplay = orig->preplay;
1747 x->mapping_maxage = orig->mapping_maxage;
1748 x->lastused = orig->lastused;
1750 x->new_mapping_sport = 0;
1760 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
1764 struct xfrm_state *x = NULL;
1766 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1769 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1770 m->reqid, m->old_family);
1771 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1772 if (x->props.mode != m->mode ||
1773 x->id.proto != m->proto)
1775 if (m->reqid && x->props.reqid != m->reqid)
1777 if (if_id != 0 && x->if_id != if_id)
1779 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1781 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1788 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1790 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1791 if (x->props.mode != m->mode ||
1792 x->id.proto != m->proto)
1794 if (if_id != 0 && x->if_id != if_id)
1796 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1798 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1806 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1810 EXPORT_SYMBOL(xfrm_migrate_state_find);
1812 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1813 struct xfrm_migrate *m,
1814 struct xfrm_encap_tmpl *encap)
1816 struct xfrm_state *xc;
1818 xc = xfrm_state_clone(x, encap);
1822 xc->props.family = m->new_family;
1824 if (xfrm_init_state(xc) < 0)
1827 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1828 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1831 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1832 /* a care is needed when the destination address of the
1833 state is to be updated as it is a part of triplet */
1834 xfrm_state_insert(xc);
1836 if (xfrm_state_add(xc) < 0)
1845 EXPORT_SYMBOL(xfrm_state_migrate);
1848 int xfrm_state_update(struct xfrm_state *x)
1850 struct xfrm_state *x1, *to_put;
1852 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1853 struct net *net = xs_net(x);
1857 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1858 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1864 if (xfrm_state_kern(x1)) {
1870 if (x1->km.state == XFRM_STATE_ACQ) {
1871 __xfrm_state_insert(x);
1877 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1880 xfrm_state_put(to_put);
1886 xfrm_state_delete(x1);
1892 spin_lock_bh(&x1->lock);
1893 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1894 if (x->encap && x1->encap &&
1895 x->encap->encap_type == x1->encap->encap_type)
1896 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1897 else if (x->encap || x1->encap)
1900 if (x->coaddr && x1->coaddr) {
1901 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1903 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1904 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1905 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1908 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1909 HRTIMER_MODE_REL_SOFT);
1910 if (READ_ONCE(x1->curlft.use_time))
1911 xfrm_state_check_expire(x1);
1913 if (x->props.smark.m || x->props.smark.v || x->if_id) {
1914 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1916 if (x->props.smark.m || x->props.smark.v)
1917 x1->props.smark = x->props.smark;
1920 x1->if_id = x->if_id;
1922 __xfrm_state_bump_genids(x1);
1923 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1927 x->km.state = XFRM_STATE_DEAD;
1928 __xfrm_state_put(x);
1932 spin_unlock_bh(&x1->lock);
1938 EXPORT_SYMBOL(xfrm_state_update);
1940 int xfrm_state_check_expire(struct xfrm_state *x)
1942 xfrm_dev_state_update_curlft(x);
1944 if (!READ_ONCE(x->curlft.use_time))
1945 WRITE_ONCE(x->curlft.use_time, ktime_get_real_seconds());
1947 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1948 x->curlft.packets >= x->lft.hard_packet_limit) {
1949 x->km.state = XFRM_STATE_EXPIRED;
1950 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
1955 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1956 x->curlft.packets >= x->lft.soft_packet_limit)) {
1958 km_state_expired(x, 0, 0);
1962 EXPORT_SYMBOL(xfrm_state_check_expire);
1965 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1966 u8 proto, unsigned short family)
1968 struct xfrm_state *x;
1971 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1975 EXPORT_SYMBOL(xfrm_state_lookup);
1978 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1979 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1980 u8 proto, unsigned short family)
1982 struct xfrm_state *x;
1984 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1985 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1986 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1989 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1992 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1993 u32 if_id, u8 proto, const xfrm_address_t *daddr,
1994 const xfrm_address_t *saddr, int create, unsigned short family)
1996 struct xfrm_state *x;
1998 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1999 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
2000 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2004 EXPORT_SYMBOL(xfrm_find_acq);
2006 #ifdef CONFIG_XFRM_SUB_POLICY
2007 #if IS_ENABLED(CONFIG_IPV6)
2008 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
2010 __xfrm6_sort(void **dst, void **src, int n,
2011 int (*cmp)(const void *p), int maxclass)
2013 int count[XFRM_MAX_DEPTH] = { };
2014 int class[XFRM_MAX_DEPTH];
2017 for (i = 0; i < n; i++) {
2018 int c = cmp(src[i]);
2024 for (i = 2; i < maxclass; i++)
2025 count[i] += count[i - 1];
2027 for (i = 0; i < n; i++) {
2028 dst[count[class[i] - 1]++] = src[i];
2033 /* Rule for xfrm_state:
2035 * rule 1: select IPsec transport except AH
2036 * rule 2: select MIPv6 RO or inbound trigger
2037 * rule 3: select IPsec transport AH
2038 * rule 4: select IPsec tunnel
2041 static int __xfrm6_state_sort_cmp(const void *p)
2043 const struct xfrm_state *v = p;
2045 switch (v->props.mode) {
2046 case XFRM_MODE_TRANSPORT:
2047 if (v->id.proto != IPPROTO_AH)
2051 #if IS_ENABLED(CONFIG_IPV6_MIP6)
2052 case XFRM_MODE_ROUTEOPTIMIZATION:
2053 case XFRM_MODE_IN_TRIGGER:
2056 case XFRM_MODE_TUNNEL:
2057 case XFRM_MODE_BEET:
2063 /* Rule for xfrm_tmpl:
2065 * rule 1: select IPsec transport
2066 * rule 2: select MIPv6 RO or inbound trigger
2067 * rule 3: select IPsec tunnel
2070 static int __xfrm6_tmpl_sort_cmp(const void *p)
2072 const struct xfrm_tmpl *v = p;
2075 case XFRM_MODE_TRANSPORT:
2077 #if IS_ENABLED(CONFIG_IPV6_MIP6)
2078 case XFRM_MODE_ROUTEOPTIMIZATION:
2079 case XFRM_MODE_IN_TRIGGER:
2082 case XFRM_MODE_TUNNEL:
2083 case XFRM_MODE_BEET:
2089 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
2090 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
2093 __xfrm6_sort(void **dst, void **src, int n,
2094 int (*cmp)(const void *p), int maxclass)
2098 for (i = 0; i < n; i++)
2101 #endif /* CONFIG_IPV6 */
2104 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
2105 unsigned short family)
2109 if (family == AF_INET6)
2110 __xfrm6_sort((void **)dst, (void **)src, n,
2111 __xfrm6_tmpl_sort_cmp, 5);
2113 for (i = 0; i < n; i++)
2118 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
2119 unsigned short family)
2123 if (family == AF_INET6)
2124 __xfrm6_sort((void **)dst, (void **)src, n,
2125 __xfrm6_state_sort_cmp, 6);
2127 for (i = 0; i < n; i++)
2132 /* Silly enough, but I'm lazy to build resolution list */
2134 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
2136 unsigned int h = xfrm_seq_hash(net, seq);
2137 struct xfrm_state *x;
2139 hlist_for_each_entry_rcu(x, net->xfrm.state_byseq + h, byseq) {
2140 if (x->km.seq == seq &&
2141 (mark & x->mark.m) == x->mark.v &&
2142 x->km.state == XFRM_STATE_ACQ) {
2151 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
2153 struct xfrm_state *x;
2155 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2156 x = __xfrm_find_acq_byseq(net, mark, seq);
2157 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2160 EXPORT_SYMBOL(xfrm_find_acq_byseq);
2162 u32 xfrm_get_acqseq(void)
2165 static atomic_t acqseq;
2168 res = atomic_inc_return(&acqseq);
2173 EXPORT_SYMBOL(xfrm_get_acqseq);
2175 int verify_spi_info(u8 proto, u32 min, u32 max, struct netlink_ext_ack *extack)
2183 /* IPCOMP spi is 16-bits. */
2184 if (max >= 0x10000) {
2185 NL_SET_ERR_MSG(extack, "IPCOMP SPI must be <= 65535");
2191 NL_SET_ERR_MSG(extack, "Invalid protocol, must be one of AH, ESP, IPCOMP");
2196 NL_SET_ERR_MSG(extack, "Invalid SPI range: min > max");
2202 EXPORT_SYMBOL(verify_spi_info);
2204 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high,
2205 struct netlink_ext_ack *extack)
2207 struct net *net = xs_net(x);
2209 struct xfrm_state *x0;
2211 __be32 minspi = htonl(low);
2212 __be32 maxspi = htonl(high);
2214 u32 mark = x->mark.v & x->mark.m;
2216 spin_lock_bh(&x->lock);
2217 if (x->km.state == XFRM_STATE_DEAD) {
2218 NL_SET_ERR_MSG(extack, "Target ACQUIRE is in DEAD state");
2228 if (minspi == maxspi) {
2229 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
2231 NL_SET_ERR_MSG(extack, "Requested SPI is already in use");
2238 for (h = 0; h < high-low+1; h++) {
2239 spi = get_random_u32_inclusive(low, high);
2240 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
2242 newspi = htonl(spi);
2249 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2251 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2252 XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
2254 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2258 NL_SET_ERR_MSG(extack, "No SPI available in the requested range");
2262 spin_unlock_bh(&x->lock);
2266 EXPORT_SYMBOL(xfrm_alloc_spi);
2268 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2269 struct xfrm_address_filter *filter)
2272 if ((filter->family == AF_INET ||
2273 filter->family == AF_INET6) &&
2274 x->props.family != filter->family)
2277 return addr_match(&x->props.saddr, &filter->saddr,
2279 addr_match(&x->id.daddr, &filter->daddr,
2285 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2286 int (*func)(struct xfrm_state *, int, void*),
2289 struct xfrm_state *state;
2290 struct xfrm_state_walk *x;
2293 if (walk->seq != 0 && list_empty(&walk->all))
2296 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2297 if (list_empty(&walk->all))
2298 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2300 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2301 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2302 if (x->state == XFRM_STATE_DEAD)
2304 state = container_of(x, struct xfrm_state, km);
2305 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2307 if (!__xfrm_state_filter_match(state, walk->filter))
2309 err = func(state, walk->seq, data);
2311 list_move_tail(&walk->all, &x->all);
2316 if (walk->seq == 0) {
2320 list_del_init(&walk->all);
2322 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2325 EXPORT_SYMBOL(xfrm_state_walk);
2327 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2328 struct xfrm_address_filter *filter)
2330 INIT_LIST_HEAD(&walk->all);
2331 walk->proto = proto;
2332 walk->state = XFRM_STATE_DEAD;
2334 walk->filter = filter;
2336 EXPORT_SYMBOL(xfrm_state_walk_init);
2338 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2340 kfree(walk->filter);
2342 if (list_empty(&walk->all))
2345 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2346 list_del(&walk->all);
2347 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2349 EXPORT_SYMBOL(xfrm_state_walk_done);
2351 static void xfrm_replay_timer_handler(struct timer_list *t)
2353 struct xfrm_state *x = from_timer(x, t, rtimer);
2355 spin_lock(&x->lock);
2357 if (x->km.state == XFRM_STATE_VALID) {
2358 if (xfrm_aevent_is_on(xs_net(x)))
2359 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
2361 x->xflags |= XFRM_TIME_DEFER;
2364 spin_unlock(&x->lock);
2367 static LIST_HEAD(xfrm_km_list);
2369 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2371 struct xfrm_mgr *km;
2374 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2375 if (km->notify_policy)
2376 km->notify_policy(xp, dir, c);
2380 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2382 struct xfrm_mgr *km;
2384 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2390 EXPORT_SYMBOL(km_policy_notify);
2391 EXPORT_SYMBOL(km_state_notify);
2393 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2399 c.event = XFRM_MSG_EXPIRE;
2400 km_state_notify(x, &c);
2403 EXPORT_SYMBOL(km_state_expired);
2405 * We send to all registered managers regardless of failure
2406 * We are happy with one success
2408 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2410 int err = -EINVAL, acqret;
2411 struct xfrm_mgr *km;
2414 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2415 acqret = km->acquire(x, t, pol);
2422 EXPORT_SYMBOL(km_query);
2424 static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2427 struct xfrm_mgr *km;
2430 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2431 if (km->new_mapping)
2432 err = km->new_mapping(x, ipaddr, sport);
2440 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2444 if (x->mapping_maxage) {
2445 if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
2446 x->new_mapping_sport != sport) {
2447 x->new_mapping_sport = sport;
2448 x->new_mapping = jiffies / HZ;
2449 ret = __km_new_mapping(x, ipaddr, sport);
2452 ret = __km_new_mapping(x, ipaddr, sport);
2457 EXPORT_SYMBOL(km_new_mapping);
2459 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2465 c.event = XFRM_MSG_POLEXPIRE;
2466 km_policy_notify(pol, dir, &c);
2468 EXPORT_SYMBOL(km_policy_expired);
2470 #ifdef CONFIG_XFRM_MIGRATE
2471 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2472 const struct xfrm_migrate *m, int num_migrate,
2473 const struct xfrm_kmaddress *k,
2474 const struct xfrm_encap_tmpl *encap)
2478 struct xfrm_mgr *km;
2481 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2483 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2492 EXPORT_SYMBOL(km_migrate);
2495 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2499 struct xfrm_mgr *km;
2502 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2504 ret = km->report(net, proto, sel, addr);
2512 EXPORT_SYMBOL(km_report);
2514 static bool km_is_alive(const struct km_event *c)
2516 struct xfrm_mgr *km;
2517 bool is_alive = false;
2520 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2521 if (km->is_alive && km->is_alive(c)) {
2531 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2532 static DEFINE_SPINLOCK(xfrm_translator_lock);
2533 static struct xfrm_translator __rcu *xfrm_translator;
2535 struct xfrm_translator *xfrm_get_translator(void)
2537 struct xfrm_translator *xtr;
2540 xtr = rcu_dereference(xfrm_translator);
2543 if (!try_module_get(xtr->owner))
2549 EXPORT_SYMBOL_GPL(xfrm_get_translator);
2551 void xfrm_put_translator(struct xfrm_translator *xtr)
2553 module_put(xtr->owner);
2555 EXPORT_SYMBOL_GPL(xfrm_put_translator);
2557 int xfrm_register_translator(struct xfrm_translator *xtr)
2561 spin_lock_bh(&xfrm_translator_lock);
2562 if (unlikely(xfrm_translator != NULL))
2565 rcu_assign_pointer(xfrm_translator, xtr);
2566 spin_unlock_bh(&xfrm_translator_lock);
2570 EXPORT_SYMBOL_GPL(xfrm_register_translator);
2572 int xfrm_unregister_translator(struct xfrm_translator *xtr)
2576 spin_lock_bh(&xfrm_translator_lock);
2577 if (likely(xfrm_translator != NULL)) {
2578 if (rcu_access_pointer(xfrm_translator) != xtr)
2581 RCU_INIT_POINTER(xfrm_translator, NULL);
2583 spin_unlock_bh(&xfrm_translator_lock);
2588 EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2591 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
2595 struct xfrm_mgr *km;
2596 struct xfrm_policy *pol = NULL;
2598 if (sockptr_is_null(optval) && !optlen) {
2599 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2600 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2605 if (optlen <= 0 || optlen > PAGE_SIZE)
2608 data = memdup_sockptr(optval, optlen);
2610 return PTR_ERR(data);
2612 if (in_compat_syscall()) {
2613 struct xfrm_translator *xtr = xfrm_get_translator();
2620 err = xtr->xlate_user_policy_sockptr(&data, optlen);
2621 xfrm_put_translator(xtr);
2630 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2631 pol = km->compile_policy(sk, optname, data,
2639 xfrm_sk_policy_insert(sk, err, pol);
2648 EXPORT_SYMBOL(xfrm_user_policy);
2650 static DEFINE_SPINLOCK(xfrm_km_lock);
2652 void xfrm_register_km(struct xfrm_mgr *km)
2654 spin_lock_bh(&xfrm_km_lock);
2655 list_add_tail_rcu(&km->list, &xfrm_km_list);
2656 spin_unlock_bh(&xfrm_km_lock);
2658 EXPORT_SYMBOL(xfrm_register_km);
2660 void xfrm_unregister_km(struct xfrm_mgr *km)
2662 spin_lock_bh(&xfrm_km_lock);
2663 list_del_rcu(&km->list);
2664 spin_unlock_bh(&xfrm_km_lock);
2667 EXPORT_SYMBOL(xfrm_unregister_km);
2669 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2673 if (WARN_ON(afinfo->family >= NPROTO))
2674 return -EAFNOSUPPORT;
2676 spin_lock_bh(&xfrm_state_afinfo_lock);
2677 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2680 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2681 spin_unlock_bh(&xfrm_state_afinfo_lock);
2684 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2686 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2688 int err = 0, family = afinfo->family;
2690 if (WARN_ON(family >= NPROTO))
2691 return -EAFNOSUPPORT;
2693 spin_lock_bh(&xfrm_state_afinfo_lock);
2694 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2695 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2698 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2700 spin_unlock_bh(&xfrm_state_afinfo_lock);
2704 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2706 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2708 if (unlikely(family >= NPROTO))
2711 return rcu_dereference(xfrm_state_afinfo[family]);
2713 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
2715 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2717 struct xfrm_state_afinfo *afinfo;
2718 if (unlikely(family >= NPROTO))
2721 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2722 if (unlikely(!afinfo))
2727 void xfrm_flush_gc(void)
2729 flush_work(&xfrm_state_gc_work);
2731 EXPORT_SYMBOL(xfrm_flush_gc);
2733 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2734 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2737 struct xfrm_state *t = x->tunnel;
2739 if (atomic_read(&t->tunnel_users) == 2)
2740 xfrm_state_delete(t);
2741 atomic_dec(&t->tunnel_users);
2742 xfrm_state_put_sync(t);
2746 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2748 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
2750 const struct xfrm_type *type = READ_ONCE(x->type);
2751 struct crypto_aead *aead;
2752 u32 blksize, net_adj = 0;
2754 if (x->km.state != XFRM_STATE_VALID ||
2755 !type || type->proto != IPPROTO_ESP)
2756 return mtu - x->props.header_len;
2759 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2761 switch (x->props.mode) {
2762 case XFRM_MODE_TRANSPORT:
2763 case XFRM_MODE_BEET:
2764 if (x->props.family == AF_INET)
2765 net_adj = sizeof(struct iphdr);
2766 else if (x->props.family == AF_INET6)
2767 net_adj = sizeof(struct ipv6hdr);
2769 case XFRM_MODE_TUNNEL:
2776 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2777 net_adj) & ~(blksize - 1)) + net_adj - 2;
2779 EXPORT_SYMBOL_GPL(xfrm_state_mtu);
2781 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
2782 struct netlink_ext_ack *extack)
2784 const struct xfrm_mode *inner_mode;
2785 const struct xfrm_mode *outer_mode;
2786 int family = x->props.family;
2789 if (family == AF_INET &&
2790 READ_ONCE(xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc))
2791 x->props.flags |= XFRM_STATE_NOPMTUDISC;
2793 err = -EPROTONOSUPPORT;
2795 if (x->sel.family != AF_UNSPEC) {
2796 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2797 if (inner_mode == NULL) {
2798 NL_SET_ERR_MSG(extack, "Requested mode not found");
2802 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2803 family != x->sel.family) {
2804 NL_SET_ERR_MSG(extack, "Only tunnel modes can accommodate a change of family");
2808 x->inner_mode = *inner_mode;
2810 const struct xfrm_mode *inner_mode_iaf;
2811 int iafamily = AF_INET;
2813 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2814 if (inner_mode == NULL) {
2815 NL_SET_ERR_MSG(extack, "Requested mode not found");
2819 x->inner_mode = *inner_mode;
2821 if (x->props.family == AF_INET)
2822 iafamily = AF_INET6;
2824 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2825 if (inner_mode_iaf) {
2826 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2827 x->inner_mode_iaf = *inner_mode_iaf;
2831 x->type = xfrm_get_type(x->id.proto, family);
2832 if (x->type == NULL) {
2833 NL_SET_ERR_MSG(extack, "Requested type not found");
2837 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2839 err = x->type->init_state(x, extack);
2843 outer_mode = xfrm_get_mode(x->props.mode, family);
2845 NL_SET_ERR_MSG(extack, "Requested mode not found");
2846 err = -EPROTONOSUPPORT;
2850 x->outer_mode = *outer_mode;
2852 err = xfrm_init_replay(x, extack);
2861 EXPORT_SYMBOL(__xfrm_init_state);
2863 int xfrm_init_state(struct xfrm_state *x)
2867 err = __xfrm_init_state(x, true, false, NULL);
2869 x->km.state = XFRM_STATE_VALID;
2874 EXPORT_SYMBOL(xfrm_init_state);
2876 int __net_init xfrm_state_init(struct net *net)
2880 if (net_eq(net, &init_net))
2881 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2882 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2884 INIT_LIST_HEAD(&net->xfrm.state_all);
2886 sz = sizeof(struct hlist_head) * 8;
2888 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2889 if (!net->xfrm.state_bydst)
2891 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2892 if (!net->xfrm.state_bysrc)
2894 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2895 if (!net->xfrm.state_byspi)
2897 net->xfrm.state_byseq = xfrm_hash_alloc(sz);
2898 if (!net->xfrm.state_byseq)
2900 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2902 net->xfrm.state_num = 0;
2903 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2904 spin_lock_init(&net->xfrm.xfrm_state_lock);
2905 seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation,
2906 &net->xfrm.xfrm_state_lock);
2910 xfrm_hash_free(net->xfrm.state_byspi, sz);
2912 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2914 xfrm_hash_free(net->xfrm.state_bydst, sz);
2919 void xfrm_state_fini(struct net *net)
2923 flush_work(&net->xfrm.state_hash_work);
2924 flush_work(&xfrm_state_gc_work);
2925 xfrm_state_flush(net, 0, false, true);
2927 WARN_ON(!list_empty(&net->xfrm.state_all));
2929 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2930 WARN_ON(!hlist_empty(net->xfrm.state_byseq));
2931 xfrm_hash_free(net->xfrm.state_byseq, sz);
2932 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2933 xfrm_hash_free(net->xfrm.state_byspi, sz);
2934 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2935 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2936 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2937 xfrm_hash_free(net->xfrm.state_bydst, sz);
2940 #ifdef CONFIG_AUDITSYSCALL
2941 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2942 struct audit_buffer *audit_buf)
2944 struct xfrm_sec_ctx *ctx = x->security;
2945 u32 spi = ntohl(x->id.spi);
2948 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2949 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2951 switch (x->props.family) {
2953 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2954 &x->props.saddr.a4, &x->id.daddr.a4);
2957 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2958 x->props.saddr.a6, x->id.daddr.a6);
2962 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2965 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2966 struct audit_buffer *audit_buf)
2968 const struct iphdr *iph4;
2969 const struct ipv6hdr *iph6;
2974 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2975 &iph4->saddr, &iph4->daddr);
2978 iph6 = ipv6_hdr(skb);
2979 audit_log_format(audit_buf,
2980 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2981 &iph6->saddr, &iph6->daddr,
2982 iph6->flow_lbl[0] & 0x0f,
2989 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2991 struct audit_buffer *audit_buf;
2993 audit_buf = xfrm_audit_start("SAD-add");
2994 if (audit_buf == NULL)
2996 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2997 xfrm_audit_helper_sainfo(x, audit_buf);
2998 audit_log_format(audit_buf, " res=%u", result);
2999 audit_log_end(audit_buf);
3001 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
3003 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
3005 struct audit_buffer *audit_buf;
3007 audit_buf = xfrm_audit_start("SAD-delete");
3008 if (audit_buf == NULL)
3010 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3011 xfrm_audit_helper_sainfo(x, audit_buf);
3012 audit_log_format(audit_buf, " res=%u", result);
3013 audit_log_end(audit_buf);
3015 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
3017 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
3018 struct sk_buff *skb)
3020 struct audit_buffer *audit_buf;
3023 audit_buf = xfrm_audit_start("SA-replay-overflow");
3024 if (audit_buf == NULL)
3026 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3027 /* don't record the sequence number because it's inherent in this kind
3028 * of audit message */
3029 spi = ntohl(x->id.spi);
3030 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
3031 audit_log_end(audit_buf);
3033 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
3035 void xfrm_audit_state_replay(struct xfrm_state *x,
3036 struct sk_buff *skb, __be32 net_seq)
3038 struct audit_buffer *audit_buf;
3041 audit_buf = xfrm_audit_start("SA-replayed-pkt");
3042 if (audit_buf == NULL)
3044 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3045 spi = ntohl(x->id.spi);
3046 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3047 spi, spi, ntohl(net_seq));
3048 audit_log_end(audit_buf);
3050 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
3052 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
3054 struct audit_buffer *audit_buf;
3056 audit_buf = xfrm_audit_start("SA-notfound");
3057 if (audit_buf == NULL)
3059 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
3060 audit_log_end(audit_buf);
3062 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
3064 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
3065 __be32 net_spi, __be32 net_seq)
3067 struct audit_buffer *audit_buf;
3070 audit_buf = xfrm_audit_start("SA-notfound");
3071 if (audit_buf == NULL)
3073 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
3074 spi = ntohl(net_spi);
3075 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3076 spi, spi, ntohl(net_seq));
3077 audit_log_end(audit_buf);
3079 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
3081 void xfrm_audit_state_icvfail(struct xfrm_state *x,
3082 struct sk_buff *skb, u8 proto)
3084 struct audit_buffer *audit_buf;
3088 audit_buf = xfrm_audit_start("SA-icv-failure");
3089 if (audit_buf == NULL)
3091 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3092 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
3093 u32 spi = ntohl(net_spi);
3094 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3095 spi, spi, ntohl(net_seq));
3097 audit_log_end(audit_buf);
3099 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
3100 #endif /* CONFIG_AUDITSYSCALL */