Lines Matching +full:x +full:-

1 // SPDX-License-Identifier: GPL-2.0-only
11 * Split up af-specific functions
36 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
54 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x) in xfrm_state_hold_rcu() argument
56 return refcount_inc_not_zero(&x->refcnt); in xfrm_state_hold_rcu()
65 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask); in xfrm_dst_hash()
73 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask); in xfrm_src_hash()
80 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask); in xfrm_spi_hash()
85 return __xfrm_seq_hash(seq, net->xfrm.state_hmask); in xfrm_seq_hash()
94 if (_x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
100 if (!_x || _x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
104 hlist_add_before_rcu(_n, &_x->by); \
115 struct xfrm_state *x; in xfrm_hash_transfer() local
117 hlist_for_each_entry_safe(x, tmp, list, bydst) { in xfrm_hash_transfer()
120 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr, in xfrm_hash_transfer()
121 x->props.reqid, x->props.family, in xfrm_hash_transfer()
123 XFRM_STATE_INSERT(bydst, &x->bydst, ndsttable + h, x->xso.type); in xfrm_hash_transfer()
125 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr, in xfrm_hash_transfer()
126 x->props.family, in xfrm_hash_transfer()
128 XFRM_STATE_INSERT(bysrc, &x->bysrc, nsrctable + h, x->xso.type); in xfrm_hash_transfer()
130 if (x->id.spi) { in xfrm_hash_transfer()
131 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi, in xfrm_hash_transfer()
132 x->id.proto, x->props.family, in xfrm_hash_transfer()
134 XFRM_STATE_INSERT(byspi, &x->byspi, nspitable + h, in xfrm_hash_transfer()
135 x->xso.type); in xfrm_hash_transfer()
138 if (x->km.seq) { in xfrm_hash_transfer()
139 h = __xfrm_seq_hash(x->km.seq, nhashmask); in xfrm_hash_transfer()
140 XFRM_STATE_INSERT(byseq, &x->byseq, nseqtable + h, in xfrm_hash_transfer()
141 x->xso.type); in xfrm_hash_transfer()
159 nsize = xfrm_hash_new_size(net->xfrm.state_hmask); in xfrm_hash_resize()
182 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_hash_resize()
183 write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation); in xfrm_hash_resize()
185 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; in xfrm_hash_resize()
186 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net); in xfrm_hash_resize()
187 for (i = net->xfrm.state_hmask; i >= 0; i--) in xfrm_hash_resize()
190 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net); in xfrm_hash_resize()
191 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net); in xfrm_hash_resize()
192 oseq = xfrm_state_deref_prot(net->xfrm.state_byseq, net); in xfrm_hash_resize()
193 ohashmask = net->xfrm.state_hmask; in xfrm_hash_resize()
195 rcu_assign_pointer(net->xfrm.state_bydst, ndst); in xfrm_hash_resize()
196 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc); in xfrm_hash_resize()
197 rcu_assign_pointer(net->xfrm.state_byspi, nspi); in xfrm_hash_resize()
198 rcu_assign_pointer(net->xfrm.state_byseq, nseq); in xfrm_hash_resize()
199 net->xfrm.state_hmask = nhashmask; in xfrm_hash_resize()
201 write_seqcount_end(&net->xfrm.xfrm_state_hash_generation); in xfrm_hash_resize()
202 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_hash_resize()
220 int __xfrm_state_delete(struct xfrm_state *x);
222 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
224 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
232 return -EAFNOSUPPORT; in xfrm_register_type()
234 #define X(afi, T, name) do { \ in xfrm_register_type() macro
235 WARN_ON((afi)->type_ ## name); \ in xfrm_register_type()
236 (afi)->type_ ## name = (T); \ in xfrm_register_type()
239 switch (type->proto) { in xfrm_register_type()
241 X(afinfo, type, comp); in xfrm_register_type()
244 X(afinfo, type, ah); in xfrm_register_type()
247 X(afinfo, type, esp); in xfrm_register_type()
250 X(afinfo, type, ipip); in xfrm_register_type()
253 X(afinfo, type, dstopts); in xfrm_register_type()
256 X(afinfo, type, routing); in xfrm_register_type()
259 X(afinfo, type, ipip6); in xfrm_register_type()
263 err = -EPROTONOSUPPORT; in xfrm_register_type()
266 #undef X in xfrm_register_type()
279 #define X(afi, T, name) do { \ in xfrm_unregister_type() macro
280 WARN_ON((afi)->type_ ## name != (T)); \ in xfrm_unregister_type()
281 (afi)->type_ ## name = NULL; \ in xfrm_unregister_type()
284 switch (type->proto) { in xfrm_unregister_type()
286 X(afinfo, type, comp); in xfrm_unregister_type()
289 X(afinfo, type, ah); in xfrm_unregister_type()
292 X(afinfo, type, esp); in xfrm_unregister_type()
295 X(afinfo, type, ipip); in xfrm_unregister_type()
298 X(afinfo, type, dstopts); in xfrm_unregister_type()
301 X(afinfo, type, routing); in xfrm_unregister_type()
304 X(afinfo, type, ipip6); in xfrm_unregister_type()
310 #undef X in xfrm_unregister_type()
328 type = afinfo->type_comp; in xfrm_get_type()
331 type = afinfo->type_ah; in xfrm_get_type()
334 type = afinfo->type_esp; in xfrm_get_type()
337 type = afinfo->type_ipip; in xfrm_get_type()
340 type = afinfo->type_dstopts; in xfrm_get_type()
343 type = afinfo->type_routing; in xfrm_get_type()
346 type = afinfo->type_ipip6; in xfrm_get_type()
352 if (unlikely(type && !try_module_get(type->owner))) in xfrm_get_type()
358 request_module("xfrm-type-%d-%d", family, proto); in xfrm_get_type()
368 module_put(type->owner); in xfrm_put_type()
378 return -EAFNOSUPPORT; in xfrm_register_type_offload()
380 switch (type->proto) { in xfrm_register_type_offload()
382 WARN_ON(afinfo->type_offload_esp); in xfrm_register_type_offload()
383 afinfo->type_offload_esp = type; in xfrm_register_type_offload()
387 err = -EPROTONOSUPPORT; in xfrm_register_type_offload()
404 switch (type->proto) { in xfrm_unregister_type_offload()
406 WARN_ON(afinfo->type_offload_esp != type); in xfrm_unregister_type_offload()
407 afinfo->type_offload_esp = NULL; in xfrm_unregister_type_offload()
430 type = afinfo->type_offload_esp; in xfrm_get_type_offload()
436 if ((type && !try_module_get(type->owner))) in xfrm_get_type_offload()
442 request_module("xfrm-offload-%d-%d", family, proto); in xfrm_get_type_offload()
452 module_put(type->owner); in xfrm_put_type_offload()
503 if (mode->family == family) in xfrm_get_mode()
508 if (mode->family == family) in xfrm_get_mode()
518 void xfrm_state_free(struct xfrm_state *x) in xfrm_state_free() argument
520 kmem_cache_free(xfrm_state_cache, x); in xfrm_state_free()
524 static void ___xfrm_state_destroy(struct xfrm_state *x) in ___xfrm_state_destroy() argument
526 hrtimer_cancel(&x->mtimer); in ___xfrm_state_destroy()
527 del_timer_sync(&x->rtimer); in ___xfrm_state_destroy()
528 kfree(x->aead); in ___xfrm_state_destroy()
529 kfree(x->aalg); in ___xfrm_state_destroy()
530 kfree(x->ealg); in ___xfrm_state_destroy()
531 kfree(x->calg); in ___xfrm_state_destroy()
532 kfree(x->encap); in ___xfrm_state_destroy()
533 kfree(x->coaddr); in ___xfrm_state_destroy()
534 kfree(x->replay_esn); in ___xfrm_state_destroy()
535 kfree(x->preplay_esn); in ___xfrm_state_destroy()
536 if (x->type_offload) in ___xfrm_state_destroy()
537 xfrm_put_type_offload(x->type_offload); in ___xfrm_state_destroy()
538 if (x->type) { in ___xfrm_state_destroy()
539 x->type->destructor(x); in ___xfrm_state_destroy()
540 xfrm_put_type(x->type); in ___xfrm_state_destroy()
542 if (x->xfrag.page) in ___xfrm_state_destroy()
543 put_page(x->xfrag.page); in ___xfrm_state_destroy()
544 xfrm_dev_state_free(x); in ___xfrm_state_destroy()
545 security_xfrm_state_free(x); in ___xfrm_state_destroy()
546 xfrm_state_free(x); in ___xfrm_state_destroy()
551 struct xfrm_state *x; in xfrm_state_gc_task() local
561 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist) in xfrm_state_gc_task()
562 ___xfrm_state_destroy(x); in xfrm_state_gc_task()
567 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer); in xfrm_timer_handler() local
574 spin_lock(&x->lock); in xfrm_timer_handler()
575 xfrm_dev_state_update_stats(x); in xfrm_timer_handler()
577 if (x->km.state == XFRM_STATE_DEAD) in xfrm_timer_handler()
579 if (x->km.state == XFRM_STATE_EXPIRED) in xfrm_timer_handler()
581 if (x->lft.hard_add_expires_seconds) { in xfrm_timer_handler()
582 time64_t tmo = x->lft.hard_add_expires_seconds + in xfrm_timer_handler()
583 x->curlft.add_time - now; in xfrm_timer_handler()
585 if (x->xflags & XFRM_SOFT_EXPIRE) { in xfrm_timer_handler()
588 * workaround: fix x->curflt.add_time by below: in xfrm_timer_handler()
590 x->curlft.add_time = now - x->saved_tmo - 1; in xfrm_timer_handler()
591 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo; in xfrm_timer_handler()
598 if (x->lft.hard_use_expires_seconds) { in xfrm_timer_handler()
599 time64_t tmo = x->lft.hard_use_expires_seconds + in xfrm_timer_handler()
600 (READ_ONCE(x->curlft.use_time) ? : now) - now; in xfrm_timer_handler()
606 if (x->km.dying) in xfrm_timer_handler()
608 if (x->lft.soft_add_expires_seconds) { in xfrm_timer_handler()
609 time64_t tmo = x->lft.soft_add_expires_seconds + in xfrm_timer_handler()
610 x->curlft.add_time - now; in xfrm_timer_handler()
613 x->xflags &= ~XFRM_SOFT_EXPIRE; in xfrm_timer_handler()
616 x->xflags |= XFRM_SOFT_EXPIRE; in xfrm_timer_handler()
617 x->saved_tmo = tmo; in xfrm_timer_handler()
620 if (x->lft.soft_use_expires_seconds) { in xfrm_timer_handler()
621 time64_t tmo = x->lft.soft_use_expires_seconds + in xfrm_timer_handler()
622 (READ_ONCE(x->curlft.use_time) ? : now) - now; in xfrm_timer_handler()
629 x->km.dying = warn; in xfrm_timer_handler()
631 km_state_expired(x, 0, 0); in xfrm_timer_handler()
634 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0)); in xfrm_timer_handler()
641 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) in xfrm_timer_handler()
642 x->km.state = XFRM_STATE_EXPIRED; in xfrm_timer_handler()
644 err = __xfrm_state_delete(x); in xfrm_timer_handler()
646 km_state_expired(x, 1, 0); in xfrm_timer_handler()
648 xfrm_audit_state_delete(x, err ? 0 : 1, true); in xfrm_timer_handler()
651 spin_unlock(&x->lock); in xfrm_timer_handler()
659 struct xfrm_state *x; in xfrm_state_alloc() local
661 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC); in xfrm_state_alloc()
663 if (x) { in xfrm_state_alloc()
664 write_pnet(&x->xs_net, net); in xfrm_state_alloc()
665 refcount_set(&x->refcnt, 1); in xfrm_state_alloc()
666 atomic_set(&x->tunnel_users, 0); in xfrm_state_alloc()
667 INIT_LIST_HEAD(&x->km.all); in xfrm_state_alloc()
668 INIT_HLIST_NODE(&x->bydst); in xfrm_state_alloc()
669 INIT_HLIST_NODE(&x->bysrc); in xfrm_state_alloc()
670 INIT_HLIST_NODE(&x->byspi); in xfrm_state_alloc()
671 INIT_HLIST_NODE(&x->byseq); in xfrm_state_alloc()
672 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT); in xfrm_state_alloc()
673 x->mtimer.function = xfrm_timer_handler; in xfrm_state_alloc()
674 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0); in xfrm_state_alloc()
675 x->curlft.add_time = ktime_get_real_seconds(); in xfrm_state_alloc()
676 x->lft.soft_byte_limit = XFRM_INF; in xfrm_state_alloc()
677 x->lft.soft_packet_limit = XFRM_INF; in xfrm_state_alloc()
678 x->lft.hard_byte_limit = XFRM_INF; in xfrm_state_alloc()
679 x->lft.hard_packet_limit = XFRM_INF; in xfrm_state_alloc()
680 x->replay_maxage = 0; in xfrm_state_alloc()
681 x->replay_maxdiff = 0; in xfrm_state_alloc()
682 spin_lock_init(&x->lock); in xfrm_state_alloc()
684 return x; in xfrm_state_alloc()
689 void xfrm_dev_state_delete(struct xfrm_state *x) in xfrm_dev_state_delete() argument
691 struct xfrm_dev_offload *xso = &x->xso; in xfrm_dev_state_delete()
692 struct net_device *dev = READ_ONCE(xso->dev); in xfrm_dev_state_delete()
695 dev->xfrmdev_ops->xdo_dev_state_delete(x); in xfrm_dev_state_delete()
697 hlist_add_head(&x->dev_gclist, &xfrm_state_dev_gc_list); in xfrm_dev_state_delete()
703 void xfrm_dev_state_free(struct xfrm_state *x) in xfrm_dev_state_free() argument
705 struct xfrm_dev_offload *xso = &x->xso; in xfrm_dev_state_free()
706 struct net_device *dev = READ_ONCE(xso->dev); in xfrm_dev_state_free()
708 if (dev && dev->xfrmdev_ops) { in xfrm_dev_state_free()
710 if (!hlist_unhashed(&x->dev_gclist)) in xfrm_dev_state_free()
711 hlist_del(&x->dev_gclist); in xfrm_dev_state_free()
714 if (dev->xfrmdev_ops->xdo_dev_state_free) in xfrm_dev_state_free()
715 dev->xfrmdev_ops->xdo_dev_state_free(x); in xfrm_dev_state_free()
716 WRITE_ONCE(xso->dev, NULL); in xfrm_dev_state_free()
717 xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED; in xfrm_dev_state_free()
718 netdev_put(dev, &xso->dev_tracker); in xfrm_dev_state_free()
723 void __xfrm_state_destroy(struct xfrm_state *x, bool sync) in __xfrm_state_destroy() argument
725 WARN_ON(x->km.state != XFRM_STATE_DEAD); in __xfrm_state_destroy()
729 ___xfrm_state_destroy(x); in __xfrm_state_destroy()
732 hlist_add_head(&x->gclist, &xfrm_state_gc_list); in __xfrm_state_destroy()
739 int __xfrm_state_delete(struct xfrm_state *x) in __xfrm_state_delete() argument
741 struct net *net = xs_net(x); in __xfrm_state_delete()
742 int err = -ESRCH; in __xfrm_state_delete()
744 if (x->km.state != XFRM_STATE_DEAD) { in __xfrm_state_delete()
745 x->km.state = XFRM_STATE_DEAD; in __xfrm_state_delete()
746 spin_lock(&net->xfrm.xfrm_state_lock); in __xfrm_state_delete()
747 list_del(&x->km.all); in __xfrm_state_delete()
748 hlist_del_rcu(&x->bydst); in __xfrm_state_delete()
749 hlist_del_rcu(&x->bysrc); in __xfrm_state_delete()
750 if (x->km.seq) in __xfrm_state_delete()
751 hlist_del_rcu(&x->byseq); in __xfrm_state_delete()
752 if (x->id.spi) in __xfrm_state_delete()
753 hlist_del_rcu(&x->byspi); in __xfrm_state_delete()
754 net->xfrm.state_num--; in __xfrm_state_delete()
755 xfrm_nat_keepalive_state_updated(x); in __xfrm_state_delete()
756 spin_unlock(&net->xfrm.xfrm_state_lock); in __xfrm_state_delete()
758 if (x->encap_sk) in __xfrm_state_delete()
759 sock_put(rcu_dereference_raw(x->encap_sk)); in __xfrm_state_delete()
761 xfrm_dev_state_delete(x); in __xfrm_state_delete()
767 xfrm_state_put(x); in __xfrm_state_delete()
775 int xfrm_state_delete(struct xfrm_state *x) in xfrm_state_delete() argument
779 spin_lock_bh(&x->lock); in xfrm_state_delete()
780 err = __xfrm_state_delete(x); in xfrm_state_delete()
781 spin_unlock_bh(&x->lock); in xfrm_state_delete()
793 for (i = 0; i <= net->xfrm.state_hmask; i++) { in xfrm_state_flush_secctx_check()
794 struct xfrm_state *x; in xfrm_state_flush_secctx_check() local
796 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { in xfrm_state_flush_secctx_check()
797 if (xfrm_id_proto_match(x->id.proto, proto) && in xfrm_state_flush_secctx_check()
798 (err = security_xfrm_state_delete(x)) != 0) { in xfrm_state_flush_secctx_check()
799 xfrm_audit_state_delete(x, 0, task_valid); in xfrm_state_flush_secctx_check()
813 for (i = 0; i <= net->xfrm.state_hmask; i++) { in xfrm_dev_state_flush_secctx_check()
814 struct xfrm_state *x; in xfrm_dev_state_flush_secctx_check() local
817 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { in xfrm_dev_state_flush_secctx_check()
818 xso = &x->xso; in xfrm_dev_state_flush_secctx_check()
820 if (xso->dev == dev && in xfrm_dev_state_flush_secctx_check()
821 (err = security_xfrm_state_delete(x)) != 0) { in xfrm_dev_state_flush_secctx_check()
822 xfrm_audit_state_delete(x, 0, task_valid); in xfrm_dev_state_flush_secctx_check()
848 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_flush()
853 err = -ESRCH; in xfrm_state_flush()
854 for (i = 0; i <= net->xfrm.state_hmask; i++) { in xfrm_state_flush()
855 struct xfrm_state *x; in xfrm_state_flush() local
857 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { in xfrm_state_flush()
858 if (!xfrm_state_kern(x) && in xfrm_state_flush()
859 xfrm_id_proto_match(x->id.proto, proto)) { in xfrm_state_flush()
860 xfrm_state_hold(x); in xfrm_state_flush()
861 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_flush()
863 err = xfrm_state_delete(x); in xfrm_state_flush()
864 xfrm_audit_state_delete(x, err ? 0 : 1, in xfrm_state_flush()
867 xfrm_state_put_sync(x); in xfrm_state_flush()
869 xfrm_state_put(x); in xfrm_state_flush()
873 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_flush()
879 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_flush()
889 struct xfrm_state *x; in xfrm_dev_state_flush() local
894 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_dev_state_flush()
899 err = -ESRCH; in xfrm_dev_state_flush()
900 for (i = 0; i <= net->xfrm.state_hmask; i++) { in xfrm_dev_state_flush()
902 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { in xfrm_dev_state_flush()
903 xso = &x->xso; in xfrm_dev_state_flush()
905 if (!xfrm_state_kern(x) && xso->dev == dev) { in xfrm_dev_state_flush()
906 xfrm_state_hold(x); in xfrm_dev_state_flush()
907 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_dev_state_flush()
909 err = xfrm_state_delete(x); in xfrm_dev_state_flush()
910 xfrm_dev_state_free(x); in xfrm_dev_state_flush()
912 xfrm_audit_state_delete(x, err ? 0 : 1, in xfrm_dev_state_flush()
914 xfrm_state_put(x); in xfrm_dev_state_flush()
918 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_dev_state_flush()
927 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_dev_state_flush()
931 hlist_for_each_entry_safe(x, tmp, &xfrm_state_dev_gc_list, dev_gclist) { in xfrm_dev_state_flush()
932 xso = &x->xso; in xfrm_dev_state_flush()
934 if (xso->dev == dev) { in xfrm_dev_state_flush()
936 xfrm_dev_state_free(x); in xfrm_dev_state_flush()
952 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_sad_getinfo()
953 si->sadcnt = net->xfrm.state_num; in xfrm_sad_getinfo()
954 si->sadhcnt = net->xfrm.state_hmask + 1; in xfrm_sad_getinfo()
955 si->sadhmcnt = xfrm_state_hashmax; in xfrm_sad_getinfo()
956 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_sad_getinfo()
963 const struct flowi4 *fl4 = &fl->u.ip4; in __xfrm4_init_tempsel()
965 sel->daddr.a4 = fl4->daddr; in __xfrm4_init_tempsel()
966 sel->saddr.a4 = fl4->saddr; in __xfrm4_init_tempsel()
967 sel->dport = xfrm_flowi_dport(fl, &fl4->uli); in __xfrm4_init_tempsel()
968 sel->dport_mask = htons(0xffff); in __xfrm4_init_tempsel()
969 sel->sport = xfrm_flowi_sport(fl, &fl4->uli); in __xfrm4_init_tempsel()
970 sel->sport_mask = htons(0xffff); in __xfrm4_init_tempsel()
971 sel->family = AF_INET; in __xfrm4_init_tempsel()
972 sel->prefixlen_d = 32; in __xfrm4_init_tempsel()
973 sel->prefixlen_s = 32; in __xfrm4_init_tempsel()
974 sel->proto = fl4->flowi4_proto; in __xfrm4_init_tempsel()
975 sel->ifindex = fl4->flowi4_oif; in __xfrm4_init_tempsel()
981 const struct flowi6 *fl6 = &fl->u.ip6; in __xfrm6_init_tempsel()
984 *(struct in6_addr *)&sel->daddr = fl6->daddr; in __xfrm6_init_tempsel()
985 *(struct in6_addr *)&sel->saddr = fl6->saddr; in __xfrm6_init_tempsel()
986 sel->dport = xfrm_flowi_dport(fl, &fl6->uli); in __xfrm6_init_tempsel()
987 sel->dport_mask = htons(0xffff); in __xfrm6_init_tempsel()
988 sel->sport = xfrm_flowi_sport(fl, &fl6->uli); in __xfrm6_init_tempsel()
989 sel->sport_mask = htons(0xffff); in __xfrm6_init_tempsel()
990 sel->family = AF_INET6; in __xfrm6_init_tempsel()
991 sel->prefixlen_d = 128; in __xfrm6_init_tempsel()
992 sel->prefixlen_s = 128; in __xfrm6_init_tempsel()
993 sel->proto = fl6->flowi6_proto; in __xfrm6_init_tempsel()
994 sel->ifindex = fl6->flowi6_oif; in __xfrm6_init_tempsel()
998 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, in xfrm_init_tempstate() argument
1005 __xfrm4_init_tempsel(&x->sel, fl); in xfrm_init_tempstate()
1008 __xfrm6_init_tempsel(&x->sel, fl); in xfrm_init_tempstate()
1012 x->id = tmpl->id; in xfrm_init_tempstate()
1014 switch (tmpl->encap_family) { in xfrm_init_tempstate()
1016 if (x->id.daddr.a4 == 0) in xfrm_init_tempstate()
1017 x->id.daddr.a4 = daddr->a4; in xfrm_init_tempstate()
1018 x->props.saddr = tmpl->saddr; in xfrm_init_tempstate()
1019 if (x->props.saddr.a4 == 0) in xfrm_init_tempstate()
1020 x->props.saddr.a4 = saddr->a4; in xfrm_init_tempstate()
1023 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr)) in xfrm_init_tempstate()
1024 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr)); in xfrm_init_tempstate()
1025 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr)); in xfrm_init_tempstate()
1026 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr)) in xfrm_init_tempstate()
1027 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr)); in xfrm_init_tempstate()
1031 x->props.mode = tmpl->mode; in xfrm_init_tempstate()
1032 x->props.reqid = tmpl->reqid; in xfrm_init_tempstate()
1033 x->props.family = tmpl->encap_family; in xfrm_init_tempstate()
1043 struct xfrm_state *x; in __xfrm_state_lookup_all() local
1045 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) { in __xfrm_state_lookup_all()
1047 if (xdo->type == XFRM_DEV_OFFLOAD_PACKET) { in __xfrm_state_lookup_all()
1048 if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET) in __xfrm_state_lookup_all()
1057 if (xdo->dev != x->xso.dev) in __xfrm_state_lookup_all()
1059 } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET) in __xfrm_state_lookup_all()
1063 if (x->props.family != family || in __xfrm_state_lookup_all()
1064 x->id.spi != spi || in __xfrm_state_lookup_all()
1065 x->id.proto != proto || in __xfrm_state_lookup_all()
1066 !xfrm_addr_equal(&x->id.daddr, daddr, family)) in __xfrm_state_lookup_all()
1069 if ((mark & x->mark.m) != x->mark.v) in __xfrm_state_lookup_all()
1071 if (!xfrm_state_hold_rcu(x)) in __xfrm_state_lookup_all()
1073 return x; in __xfrm_state_lookup_all()
1085 struct xfrm_state *x; in __xfrm_state_lookup() local
1087 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) { in __xfrm_state_lookup()
1088 if (x->props.family != family || in __xfrm_state_lookup()
1089 x->id.spi != spi || in __xfrm_state_lookup()
1090 x->id.proto != proto || in __xfrm_state_lookup()
1091 !xfrm_addr_equal(&x->id.daddr, daddr, family)) in __xfrm_state_lookup()
1094 if ((mark & x->mark.m) != x->mark.v) in __xfrm_state_lookup()
1096 if (!xfrm_state_hold_rcu(x)) in __xfrm_state_lookup()
1098 return x; in __xfrm_state_lookup()
1110 struct xfrm_state *x; in __xfrm_state_lookup_byaddr() local
1112 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) { in __xfrm_state_lookup_byaddr()
1113 if (x->props.family != family || in __xfrm_state_lookup_byaddr()
1114 x->id.proto != proto || in __xfrm_state_lookup_byaddr()
1115 !xfrm_addr_equal(&x->id.daddr, daddr, family) || in __xfrm_state_lookup_byaddr()
1116 !xfrm_addr_equal(&x->props.saddr, saddr, family)) in __xfrm_state_lookup_byaddr()
1119 if ((mark & x->mark.m) != x->mark.v) in __xfrm_state_lookup_byaddr()
1121 if (!xfrm_state_hold_rcu(x)) in __xfrm_state_lookup_byaddr()
1123 return x; in __xfrm_state_lookup_byaddr()
1130 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) in __xfrm_state_locate() argument
1132 struct net *net = xs_net(x); in __xfrm_state_locate()
1133 u32 mark = x->mark.v & x->mark.m; in __xfrm_state_locate()
1136 return __xfrm_state_lookup(net, mark, &x->id.daddr, in __xfrm_state_locate()
1137 x->id.spi, x->id.proto, family); in __xfrm_state_locate()
1140 &x->id.daddr, in __xfrm_state_locate()
1141 &x->props.saddr, in __xfrm_state_locate()
1142 x->id.proto, family); in __xfrm_state_locate()
1148 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax && in xfrm_hash_grow_check()
1149 net->xfrm.state_num > net->xfrm.state_hmask) in xfrm_hash_grow_check()
1150 schedule_work(&net->xfrm.state_hash_work); in xfrm_hash_grow_check()
1153 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x, in xfrm_state_look_at() argument
1169 if (x->km.state == XFRM_STATE_VALID) { in xfrm_state_look_at()
1170 if ((x->sel.family && in xfrm_state_look_at()
1171 (x->sel.family != family || in xfrm_state_look_at()
1172 !xfrm_selector_match(&x->sel, fl, family))) || in xfrm_state_look_at()
1173 !security_xfrm_state_pol_flow_match(x, pol, in xfrm_state_look_at()
1174 &fl->u.__fl_common)) in xfrm_state_look_at()
1178 (*best)->km.dying > x->km.dying || in xfrm_state_look_at()
1179 ((*best)->km.dying == x->km.dying && in xfrm_state_look_at()
1180 (*best)->curlft.add_time < x->curlft.add_time)) in xfrm_state_look_at()
1181 *best = x; in xfrm_state_look_at()
1182 } else if (x->km.state == XFRM_STATE_ACQ) { in xfrm_state_look_at()
1184 } else if (x->km.state == XFRM_STATE_ERROR || in xfrm_state_look_at()
1185 x->km.state == XFRM_STATE_EXPIRED) { in xfrm_state_look_at()
1186 if ((!x->sel.family || in xfrm_state_look_at()
1187 (x->sel.family == family && in xfrm_state_look_at()
1188 xfrm_selector_match(&x->sel, fl, family))) && in xfrm_state_look_at()
1189 security_xfrm_state_pol_flow_match(x, pol, in xfrm_state_look_at()
1190 &fl->u.__fl_common)) in xfrm_state_look_at()
1191 *error = -ESRCH; in xfrm_state_look_at()
1204 struct xfrm_state *x, *x0, *to_put; in xfrm_state_find() local
1208 u32 mark = pol->mark.v & pol->mark.m; in xfrm_state_find()
1209 unsigned short encap_family = tmpl->encap_family; in xfrm_state_find()
1215 sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation); in xfrm_state_find()
1218 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family); in xfrm_state_find()
1219 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) { in xfrm_state_find()
1221 if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) { in xfrm_state_find()
1222 if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET) in xfrm_state_find()
1231 if (pol->xdo.dev != x->xso.dev) in xfrm_state_find()
1233 } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET) in xfrm_state_find()
1237 if (x->props.family == encap_family && in xfrm_state_find()
1238 x->props.reqid == tmpl->reqid && in xfrm_state_find()
1239 (mark & x->mark.m) == x->mark.v && in xfrm_state_find()
1240 x->if_id == if_id && in xfrm_state_find()
1241 !(x->props.flags & XFRM_STATE_WILDRECV) && in xfrm_state_find()
1242 xfrm_state_addr_check(x, daddr, saddr, encap_family) && in xfrm_state_find()
1243 tmpl->mode == x->props.mode && in xfrm_state_find()
1244 tmpl->id.proto == x->id.proto && in xfrm_state_find()
1245 (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) in xfrm_state_find()
1246 xfrm_state_look_at(pol, x, fl, family, in xfrm_state_find()
1252 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); in xfrm_state_find()
1253 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) { in xfrm_state_find()
1255 if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) { in xfrm_state_find()
1256 if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET) in xfrm_state_find()
1265 if (pol->xdo.dev != x->xso.dev) in xfrm_state_find()
1267 } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET) in xfrm_state_find()
1271 if (x->props.family == encap_family && in xfrm_state_find()
1272 x->props.reqid == tmpl->reqid && in xfrm_state_find()
1273 (mark & x->mark.m) == x->mark.v && in xfrm_state_find()
1274 x->if_id == if_id && in xfrm_state_find()
1275 !(x->props.flags & XFRM_STATE_WILDRECV) && in xfrm_state_find()
1276 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) && in xfrm_state_find()
1277 tmpl->mode == x->props.mode && in xfrm_state_find()
1278 tmpl->id.proto == x->id.proto && in xfrm_state_find()
1279 (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) in xfrm_state_find()
1280 xfrm_state_look_at(pol, x, fl, family, in xfrm_state_find()
1285 x = best; in xfrm_state_find()
1286 if (!x && !error && !acquire_in_progress) { in xfrm_state_find()
1287 if (tmpl->id.spi && in xfrm_state_find()
1289 tmpl->id.spi, tmpl->id.proto, in xfrm_state_find()
1291 &pol->xdo)) != NULL) { in xfrm_state_find()
1293 error = -EEXIST; in xfrm_state_find()
1299 * for each and every packet - garbage collection might not in xfrm_state_find()
1303 error = -ESRCH; in xfrm_state_find()
1307 x = xfrm_state_alloc(net); in xfrm_state_find()
1308 if (x == NULL) { in xfrm_state_find()
1309 error = -ENOMEM; in xfrm_state_find()
1314 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family); in xfrm_state_find()
1315 memcpy(&x->mark, &pol->mark, sizeof(x->mark)); in xfrm_state_find()
1316 x->if_id = if_id; in xfrm_state_find()
1318 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid); in xfrm_state_find()
1320 x->km.state = XFRM_STATE_DEAD; in xfrm_state_find()
1321 to_put = x; in xfrm_state_find()
1322 x = NULL; in xfrm_state_find()
1326 if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) { in xfrm_state_find()
1327 struct xfrm_dev_offload *xdo = &pol->xdo; in xfrm_state_find()
1328 struct xfrm_dev_offload *xso = &x->xso; in xfrm_state_find()
1330 xso->type = XFRM_DEV_OFFLOAD_PACKET; in xfrm_state_find()
1331 xso->dir = xdo->dir; in xfrm_state_find()
1332 xso->dev = xdo->dev; in xfrm_state_find()
1333 xso->real_dev = xdo->real_dev; in xfrm_state_find()
1334 xso->flags = XFRM_DEV_OFFLOAD_FLAG_ACQ; in xfrm_state_find()
1335 netdev_hold(xso->dev, &xso->dev_tracker, GFP_ATOMIC); in xfrm_state_find()
1336 error = xso->dev->xfrmdev_ops->xdo_dev_state_add(x, NULL); in xfrm_state_find()
1338 xso->dir = 0; in xfrm_state_find()
1339 netdev_put(xso->dev, &xso->dev_tracker); in xfrm_state_find()
1340 xso->dev = NULL; in xfrm_state_find()
1341 xso->real_dev = NULL; in xfrm_state_find()
1342 xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED; in xfrm_state_find()
1343 x->km.state = XFRM_STATE_DEAD; in xfrm_state_find()
1344 to_put = x; in xfrm_state_find()
1345 x = NULL; in xfrm_state_find()
1350 if (km_query(x, tmpl, pol) == 0) { in xfrm_state_find()
1351 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_find()
1352 x->km.state = XFRM_STATE_ACQ; in xfrm_state_find()
1353 x->dir = XFRM_SA_DIR_OUT; in xfrm_state_find()
1354 list_add(&x->km.all, &net->xfrm.state_all); in xfrm_state_find()
1355 XFRM_STATE_INSERT(bydst, &x->bydst, in xfrm_state_find()
1356 net->xfrm.state_bydst + h, in xfrm_state_find()
1357 x->xso.type); in xfrm_state_find()
1359 XFRM_STATE_INSERT(bysrc, &x->bysrc, in xfrm_state_find()
1360 net->xfrm.state_bysrc + h, in xfrm_state_find()
1361 x->xso.type); in xfrm_state_find()
1362 if (x->id.spi) { in xfrm_state_find()
1363 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family); in xfrm_state_find()
1364 XFRM_STATE_INSERT(byspi, &x->byspi, in xfrm_state_find()
1365 net->xfrm.state_byspi + h, in xfrm_state_find()
1366 x->xso.type); in xfrm_state_find()
1368 if (x->km.seq) { in xfrm_state_find()
1369 h = xfrm_seq_hash(net, x->km.seq); in xfrm_state_find()
1370 XFRM_STATE_INSERT(byseq, &x->byseq, in xfrm_state_find()
1371 net->xfrm.state_byseq + h, in xfrm_state_find()
1372 x->xso.type); in xfrm_state_find()
1374 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; in xfrm_state_find()
1375 hrtimer_start(&x->mtimer, in xfrm_state_find()
1376 ktime_set(net->xfrm.sysctl_acq_expires, 0), in xfrm_state_find()
1378 net->xfrm.state_num++; in xfrm_state_find()
1379 xfrm_hash_grow_check(net, x->bydst.next != NULL); in xfrm_state_find()
1380 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_find()
1383 struct xfrm_dev_offload *xso = &x->xso; in xfrm_state_find()
1385 if (xso->type == XFRM_DEV_OFFLOAD_PACKET) { in xfrm_state_find()
1386 xfrm_dev_state_delete(x); in xfrm_state_find()
1387 xfrm_dev_state_free(x); in xfrm_state_find()
1390 x->km.state = XFRM_STATE_DEAD; in xfrm_state_find()
1391 to_put = x; in xfrm_state_find()
1392 x = NULL; in xfrm_state_find()
1393 error = -ESRCH; in xfrm_state_find()
1397 if (x) { in xfrm_state_find()
1398 if (!xfrm_state_hold_rcu(x)) { in xfrm_state_find()
1399 *err = -EAGAIN; in xfrm_state_find()
1400 x = NULL; in xfrm_state_find()
1403 *err = acquire_in_progress ? -EAGAIN : error; in xfrm_state_find()
1409 if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) { in xfrm_state_find()
1410 *err = -EAGAIN; in xfrm_state_find()
1411 if (x) { in xfrm_state_find()
1412 xfrm_state_put(x); in xfrm_state_find()
1413 x = NULL; in xfrm_state_find()
1417 return x; in xfrm_state_find()
1426 struct xfrm_state *rx = NULL, *x = NULL; in xfrm_stateonly_find() local
1428 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_stateonly_find()
1430 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { in xfrm_stateonly_find()
1431 if (x->props.family == family && in xfrm_stateonly_find()
1432 x->props.reqid == reqid && in xfrm_stateonly_find()
1433 (mark & x->mark.m) == x->mark.v && in xfrm_stateonly_find()
1434 x->if_id == if_id && in xfrm_stateonly_find()
1435 !(x->props.flags & XFRM_STATE_WILDRECV) && in xfrm_stateonly_find()
1436 xfrm_state_addr_check(x, daddr, saddr, family) && in xfrm_stateonly_find()
1437 mode == x->props.mode && in xfrm_stateonly_find()
1438 proto == x->id.proto && in xfrm_stateonly_find()
1439 x->km.state == XFRM_STATE_VALID) { in xfrm_stateonly_find()
1440 rx = x; in xfrm_stateonly_find()
1447 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_stateonly_find()
1457 struct xfrm_state *x; in xfrm_state_lookup_byspi() local
1460 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_lookup_byspi()
1461 list_for_each_entry(w, &net->xfrm.state_all, all) { in xfrm_state_lookup_byspi()
1462 x = container_of(w, struct xfrm_state, km); in xfrm_state_lookup_byspi()
1463 if (x->props.family != family || in xfrm_state_lookup_byspi()
1464 x->id.spi != spi) in xfrm_state_lookup_byspi()
1467 xfrm_state_hold(x); in xfrm_state_lookup_byspi()
1468 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_lookup_byspi()
1469 return x; in xfrm_state_lookup_byspi()
1471 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_lookup_byspi()
1476 static void __xfrm_state_insert(struct xfrm_state *x) in __xfrm_state_insert() argument
1478 struct net *net = xs_net(x); in __xfrm_state_insert()
1481 list_add(&x->km.all, &net->xfrm.state_all); in __xfrm_state_insert()
1483 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr, in __xfrm_state_insert()
1484 x->props.reqid, x->props.family); in __xfrm_state_insert()
1485 XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h, in __xfrm_state_insert()
1486 x->xso.type); in __xfrm_state_insert()
1488 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family); in __xfrm_state_insert()
1489 XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h, in __xfrm_state_insert()
1490 x->xso.type); in __xfrm_state_insert()
1492 if (x->id.spi) { in __xfrm_state_insert()
1493 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, in __xfrm_state_insert()
1494 x->props.family); in __xfrm_state_insert()
1496 XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h, in __xfrm_state_insert()
1497 x->xso.type); in __xfrm_state_insert()
1500 if (x->km.seq) { in __xfrm_state_insert()
1501 h = xfrm_seq_hash(net, x->km.seq); in __xfrm_state_insert()
1503 XFRM_STATE_INSERT(byseq, &x->byseq, net->xfrm.state_byseq + h, in __xfrm_state_insert()
1504 x->xso.type); in __xfrm_state_insert()
1507 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT); in __xfrm_state_insert()
1508 if (x->replay_maxage) in __xfrm_state_insert()
1509 mod_timer(&x->rtimer, jiffies + x->replay_maxage); in __xfrm_state_insert()
1511 net->xfrm.state_num++; in __xfrm_state_insert()
1513 xfrm_hash_grow_check(net, x->bydst.next != NULL); in __xfrm_state_insert()
1514 xfrm_nat_keepalive_state_updated(x); in __xfrm_state_insert()
1517 /* net->xfrm.xfrm_state_lock is held */
1521 unsigned short family = xnew->props.family; in __xfrm_state_bump_genids()
1522 u32 reqid = xnew->props.reqid; in __xfrm_state_bump_genids()
1523 struct xfrm_state *x; in __xfrm_state_bump_genids() local
1525 u32 mark = xnew->mark.v & xnew->mark.m; in __xfrm_state_bump_genids()
1526 u32 if_id = xnew->if_id; in __xfrm_state_bump_genids()
1528 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family); in __xfrm_state_bump_genids()
1529 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { in __xfrm_state_bump_genids()
1530 if (x->props.family == family && in __xfrm_state_bump_genids()
1531 x->props.reqid == reqid && in __xfrm_state_bump_genids()
1532 x->if_id == if_id && in __xfrm_state_bump_genids()
1533 (mark & x->mark.m) == x->mark.v && in __xfrm_state_bump_genids()
1534 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) && in __xfrm_state_bump_genids()
1535 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family)) in __xfrm_state_bump_genids()
1536 x->genid++; in __xfrm_state_bump_genids()
1540 void xfrm_state_insert(struct xfrm_state *x) in xfrm_state_insert() argument
1542 struct net *net = xs_net(x); in xfrm_state_insert()
1544 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_insert()
1545 __xfrm_state_bump_genids(x); in xfrm_state_insert()
1546 __xfrm_state_insert(x); in xfrm_state_insert()
1547 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_insert()
1551 /* net->xfrm.xfrm_state_lock is held */
1561 struct xfrm_state *x; in __find_acq_core() local
1562 u32 mark = m->v & m->m; in __find_acq_core()
1564 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { in __find_acq_core()
1565 if (x->props.reqid != reqid || in __find_acq_core()
1566 x->props.mode != mode || in __find_acq_core()
1567 x->props.family != family || in __find_acq_core()
1568 x->km.state != XFRM_STATE_ACQ || in __find_acq_core()
1569 x->id.spi != 0 || in __find_acq_core()
1570 x->id.proto != proto || in __find_acq_core()
1571 (mark & x->mark.m) != x->mark.v || in __find_acq_core()
1572 !xfrm_addr_equal(&x->id.daddr, daddr, family) || in __find_acq_core()
1573 !xfrm_addr_equal(&x->props.saddr, saddr, family)) in __find_acq_core()
1576 xfrm_state_hold(x); in __find_acq_core()
1577 return x; in __find_acq_core()
1583 x = xfrm_state_alloc(net); in __find_acq_core()
1584 if (likely(x)) { in __find_acq_core()
1587 x->sel.daddr.a4 = daddr->a4; in __find_acq_core()
1588 x->sel.saddr.a4 = saddr->a4; in __find_acq_core()
1589 x->sel.prefixlen_d = 32; in __find_acq_core()
1590 x->sel.prefixlen_s = 32; in __find_acq_core()
1591 x->props.saddr.a4 = saddr->a4; in __find_acq_core()
1592 x->id.daddr.a4 = daddr->a4; in __find_acq_core()
1596 x->sel.daddr.in6 = daddr->in6; in __find_acq_core()
1597 x->sel.saddr.in6 = saddr->in6; in __find_acq_core()
1598 x->sel.prefixlen_d = 128; in __find_acq_core()
1599 x->sel.prefixlen_s = 128; in __find_acq_core()
1600 x->props.saddr.in6 = saddr->in6; in __find_acq_core()
1601 x->id.daddr.in6 = daddr->in6; in __find_acq_core()
1605 x->km.state = XFRM_STATE_ACQ; in __find_acq_core()
1606 x->id.proto = proto; in __find_acq_core()
1607 x->props.family = family; in __find_acq_core()
1608 x->props.mode = mode; in __find_acq_core()
1609 x->props.reqid = reqid; in __find_acq_core()
1610 x->if_id = if_id; in __find_acq_core()
1611 x->mark.v = m->v; in __find_acq_core()
1612 x->mark.m = m->m; in __find_acq_core()
1613 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; in __find_acq_core()
1614 xfrm_state_hold(x); in __find_acq_core()
1615 hrtimer_start(&x->mtimer, in __find_acq_core()
1616 ktime_set(net->xfrm.sysctl_acq_expires, 0), in __find_acq_core()
1618 list_add(&x->km.all, &net->xfrm.state_all); in __find_acq_core()
1619 XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h, in __find_acq_core()
1620 x->xso.type); in __find_acq_core()
1622 XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h, in __find_acq_core()
1623 x->xso.type); in __find_acq_core()
1625 net->xfrm.state_num++; in __find_acq_core()
1627 xfrm_hash_grow_check(net, x->bydst.next != NULL); in __find_acq_core()
1630 return x; in __find_acq_core()
1635 int xfrm_state_add(struct xfrm_state *x) in xfrm_state_add() argument
1637 struct net *net = xs_net(x); in xfrm_state_add()
1641 u32 mark = x->mark.v & x->mark.m; in xfrm_state_add()
1642 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); in xfrm_state_add()
1644 family = x->props.family; in xfrm_state_add()
1648 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_add()
1650 x1 = __xfrm_state_locate(x, use_spi, family); in xfrm_state_add()
1654 err = -EEXIST; in xfrm_state_add()
1658 if (use_spi && x->km.seq) { in xfrm_state_add()
1659 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq); in xfrm_state_add()
1660 if (x1 && ((x1->id.proto != x->id.proto) || in xfrm_state_add()
1661 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) { in xfrm_state_add()
1668 x1 = __find_acq_core(net, &x->mark, family, x->props.mode, in xfrm_state_add()
1669 x->props.reqid, x->if_id, x->id.proto, in xfrm_state_add()
1670 &x->id.daddr, &x->props.saddr, 0); in xfrm_state_add()
1672 __xfrm_state_bump_genids(x); in xfrm_state_add()
1673 __xfrm_state_insert(x); in xfrm_state_add()
1677 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_add()
1692 static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security) in clone_security() argument
1695 int size = sizeof(*uctx) + security->ctx_len; in clone_security()
1700 return -ENOMEM; in clone_security()
1702 uctx->exttype = XFRMA_SEC_CTX; in clone_security()
1703 uctx->len = size; in clone_security()
1704 uctx->ctx_doi = security->ctx_doi; in clone_security()
1705 uctx->ctx_alg = security->ctx_alg; in clone_security()
1706 uctx->ctx_len = security->ctx_len; in clone_security()
1707 memcpy(uctx + 1, security->ctx_str, security->ctx_len); in clone_security()
1708 err = security_xfrm_state_alloc(x, uctx); in clone_security()
1720 struct xfrm_state *x = xfrm_state_alloc(net); in xfrm_state_clone() local
1721 if (!x) in xfrm_state_clone()
1724 memcpy(&x->id, &orig->id, sizeof(x->id)); in xfrm_state_clone()
1725 memcpy(&x->sel, &orig->sel, sizeof(x->sel)); in xfrm_state_clone()
1726 memcpy(&x->lft, &orig->lft, sizeof(x->lft)); in xfrm_state_clone()
1727 x->props.mode = orig->props.mode; in xfrm_state_clone()
1728 x->props.replay_window = orig->props.replay_window; in xfrm_state_clone()
1729 x->props.reqid = orig->props.reqid; in xfrm_state_clone()
1730 x->props.family = orig->props.family; in xfrm_state_clone()
1731 x->props.saddr = orig->props.saddr; in xfrm_state_clone()
1733 if (orig->aalg) { in xfrm_state_clone()
1734 x->aalg = xfrm_algo_auth_clone(orig->aalg); in xfrm_state_clone()
1735 if (!x->aalg) in xfrm_state_clone()
1738 x->props.aalgo = orig->props.aalgo; in xfrm_state_clone()
1740 if (orig->aead) { in xfrm_state_clone()
1741 x->aead = xfrm_algo_aead_clone(orig->aead); in xfrm_state_clone()
1742 x->geniv = orig->geniv; in xfrm_state_clone()
1743 if (!x->aead) in xfrm_state_clone()
1746 if (orig->ealg) { in xfrm_state_clone()
1747 x->ealg = xfrm_algo_clone(orig->ealg); in xfrm_state_clone()
1748 if (!x->ealg) in xfrm_state_clone()
1751 x->props.ealgo = orig->props.ealgo; in xfrm_state_clone()
1753 if (orig->calg) { in xfrm_state_clone()
1754 x->calg = xfrm_algo_clone(orig->calg); in xfrm_state_clone()
1755 if (!x->calg) in xfrm_state_clone()
1758 x->props.calgo = orig->props.calgo; in xfrm_state_clone()
1760 if (encap || orig->encap) { in xfrm_state_clone()
1762 x->encap = kmemdup(encap, sizeof(*x->encap), in xfrm_state_clone()
1765 x->encap = kmemdup(orig->encap, sizeof(*x->encap), in xfrm_state_clone()
1768 if (!x->encap) in xfrm_state_clone()
1772 if (orig->security) in xfrm_state_clone()
1773 if (clone_security(x, orig->security)) in xfrm_state_clone()
1776 if (orig->coaddr) { in xfrm_state_clone()
1777 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr), in xfrm_state_clone()
1779 if (!x->coaddr) in xfrm_state_clone()
1783 if (orig->replay_esn) { in xfrm_state_clone()
1784 if (xfrm_replay_clone(x, orig)) in xfrm_state_clone()
1788 memcpy(&x->mark, &orig->mark, sizeof(x->mark)); in xfrm_state_clone()
1789 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark)); in xfrm_state_clone()
1791 x->props.flags = orig->props.flags; in xfrm_state_clone()
1792 x->props.extra_flags = orig->props.extra_flags; in xfrm_state_clone()
1794 x->if_id = orig->if_id; in xfrm_state_clone()
1795 x->tfcpad = orig->tfcpad; in xfrm_state_clone()
1796 x->replay_maxdiff = orig->replay_maxdiff; in xfrm_state_clone()
1797 x->replay_maxage = orig->replay_maxage; in xfrm_state_clone()
1798 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft)); in xfrm_state_clone()
1799 x->km.state = orig->km.state; in xfrm_state_clone()
1800 x->km.seq = orig->km.seq; in xfrm_state_clone()
1801 x->replay = orig->replay; in xfrm_state_clone()
1802 x->preplay = orig->preplay; in xfrm_state_clone()
1803 x->mapping_maxage = orig->mapping_maxage; in xfrm_state_clone()
1804 x->lastused = orig->lastused; in xfrm_state_clone()
1805 x->new_mapping = 0; in xfrm_state_clone()
1806 x->new_mapping_sport = 0; in xfrm_state_clone()
1807 x->dir = orig->dir; in xfrm_state_clone()
1809 return x; in xfrm_state_clone()
1812 xfrm_state_put(x); in xfrm_state_clone()
1821 struct xfrm_state *x = NULL; in xfrm_migrate_state_find() local
1823 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_migrate_state_find()
1825 if (m->reqid) { in xfrm_migrate_state_find()
1826 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr, in xfrm_migrate_state_find()
1827 m->reqid, m->old_family); in xfrm_migrate_state_find()
1828 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { in xfrm_migrate_state_find()
1829 if (x->props.mode != m->mode || in xfrm_migrate_state_find()
1830 x->id.proto != m->proto) in xfrm_migrate_state_find()
1832 if (m->reqid && x->props.reqid != m->reqid) in xfrm_migrate_state_find()
1834 if (if_id != 0 && x->if_id != if_id) in xfrm_migrate_state_find()
1836 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, in xfrm_migrate_state_find()
1837 m->old_family) || in xfrm_migrate_state_find()
1838 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, in xfrm_migrate_state_find()
1839 m->old_family)) in xfrm_migrate_state_find()
1841 xfrm_state_hold(x); in xfrm_migrate_state_find()
1845 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr, in xfrm_migrate_state_find()
1846 m->old_family); in xfrm_migrate_state_find()
1847 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { in xfrm_migrate_state_find()
1848 if (x->props.mode != m->mode || in xfrm_migrate_state_find()
1849 x->id.proto != m->proto) in xfrm_migrate_state_find()
1851 if (if_id != 0 && x->if_id != if_id) in xfrm_migrate_state_find()
1853 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, in xfrm_migrate_state_find()
1854 m->old_family) || in xfrm_migrate_state_find()
1855 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, in xfrm_migrate_state_find()
1856 m->old_family)) in xfrm_migrate_state_find()
1858 xfrm_state_hold(x); in xfrm_migrate_state_find()
1863 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_migrate_state_find()
1865 return x; in xfrm_migrate_state_find()
1869 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x, in xfrm_state_migrate() argument
1875 xc = xfrm_state_clone(x, encap); in xfrm_state_migrate()
1879 xc->props.family = m->new_family; in xfrm_state_migrate()
1884 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr)); in xfrm_state_migrate()
1885 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr)); in xfrm_state_migrate()
1888 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) { in xfrm_state_migrate()
1905 int xfrm_state_update(struct xfrm_state *x) in xfrm_state_update() argument
1909 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); in xfrm_state_update()
1910 struct net *net = xs_net(x); in xfrm_state_update()
1914 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_update()
1915 x1 = __xfrm_state_locate(x, use_spi, x->props.family); in xfrm_state_update()
1917 err = -ESRCH; in xfrm_state_update()
1923 err = -EEXIST; in xfrm_state_update()
1927 if (x1->km.state == XFRM_STATE_ACQ) { in xfrm_state_update()
1928 if (x->dir && x1->dir != x->dir) in xfrm_state_update()
1931 __xfrm_state_insert(x); in xfrm_state_update()
1932 x = NULL; in xfrm_state_update()
1934 if (x1->dir != x->dir) in xfrm_state_update()
1940 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_update()
1948 if (!x) { in xfrm_state_update()
1954 err = -EINVAL; in xfrm_state_update()
1955 spin_lock_bh(&x1->lock); in xfrm_state_update()
1956 if (likely(x1->km.state == XFRM_STATE_VALID)) { in xfrm_state_update()
1957 if (x->encap && x1->encap && in xfrm_state_update()
1958 x->encap->encap_type == x1->encap->encap_type) in xfrm_state_update()
1959 memcpy(x1->encap, x->encap, sizeof(*x1->encap)); in xfrm_state_update()
1960 else if (x->encap || x1->encap) in xfrm_state_update()
1963 if (x->coaddr && x1->coaddr) { in xfrm_state_update()
1964 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); in xfrm_state_update()
1966 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) in xfrm_state_update()
1967 memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); in xfrm_state_update()
1968 memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); in xfrm_state_update()
1969 x1->km.dying = 0; in xfrm_state_update()
1971 hrtimer_start(&x1->mtimer, ktime_set(1, 0), in xfrm_state_update()
1973 if (READ_ONCE(x1->curlft.use_time)) in xfrm_state_update()
1976 if (x->props.smark.m || x->props.smark.v || x->if_id) { in xfrm_state_update()
1977 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_update()
1979 if (x->props.smark.m || x->props.smark.v) in xfrm_state_update()
1980 x1->props.smark = x->props.smark; in xfrm_state_update()
1982 if (x->if_id) in xfrm_state_update()
1983 x1->if_id = x->if_id; in xfrm_state_update()
1986 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_update()
1990 x->km.state = XFRM_STATE_DEAD; in xfrm_state_update()
1991 __xfrm_state_put(x); in xfrm_state_update()
1995 spin_unlock_bh(&x1->lock); in xfrm_state_update()
2003 int xfrm_state_check_expire(struct xfrm_state *x) in xfrm_state_check_expire() argument
2005 xfrm_dev_state_update_stats(x); in xfrm_state_check_expire()
2007 if (!READ_ONCE(x->curlft.use_time)) in xfrm_state_check_expire()
2008 WRITE_ONCE(x->curlft.use_time, ktime_get_real_seconds()); in xfrm_state_check_expire()
2010 if (x->curlft.bytes >= x->lft.hard_byte_limit || in xfrm_state_check_expire()
2011 x->curlft.packets >= x->lft.hard_packet_limit) { in xfrm_state_check_expire()
2012 x->km.state = XFRM_STATE_EXPIRED; in xfrm_state_check_expire()
2013 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT); in xfrm_state_check_expire()
2014 return -EINVAL; in xfrm_state_check_expire()
2017 if (!x->km.dying && in xfrm_state_check_expire()
2018 (x->curlft.bytes >= x->lft.soft_byte_limit || in xfrm_state_check_expire()
2019 x->curlft.packets >= x->lft.soft_packet_limit)) { in xfrm_state_check_expire()
2020 x->km.dying = 1; in xfrm_state_check_expire()
2021 km_state_expired(x, 0, 0); in xfrm_state_check_expire()
2029 struct xfrm_state *x; in xfrm_state_update_stats() local
2032 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_update_stats()
2033 for (i = 0; i <= net->xfrm.state_hmask; i++) { in xfrm_state_update_stats()
2034 hlist_for_each_entry(x, net->xfrm.state_bydst + i, bydst) in xfrm_state_update_stats()
2035 xfrm_dev_state_update_stats(x); in xfrm_state_update_stats()
2037 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_update_stats()
2044 struct xfrm_state *x; in xfrm_state_lookup() local
2047 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family); in xfrm_state_lookup()
2049 return x; in xfrm_state_lookup()
2058 struct xfrm_state *x; in xfrm_state_lookup_byaddr() local
2060 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_lookup_byaddr()
2061 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family); in xfrm_state_lookup_byaddr()
2062 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_lookup_byaddr()
2063 return x; in xfrm_state_lookup_byaddr()
2072 struct xfrm_state *x; in xfrm_find_acq() local
2074 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_find_acq()
2075 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create); in xfrm_find_acq()
2076 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_find_acq()
2078 return x; in xfrm_find_acq()
2101 count[i] += count[i - 1]; in __xfrm6_sort()
2104 dst[count[class[i] - 1]++] = src[i]; in __xfrm6_sort()
2121 switch (v->props.mode) { in __xfrm6_state_sort_cmp()
2123 if (v->id.proto != IPPROTO_AH) in __xfrm6_state_sort_cmp()
2150 switch (v->mode) { in __xfrm6_tmpl_sort_cmp()
2213 struct xfrm_state *x; in __xfrm_find_acq_byseq() local
2215 hlist_for_each_entry_rcu(x, net->xfrm.state_byseq + h, byseq) { in __xfrm_find_acq_byseq()
2216 if (x->km.seq == seq && in __xfrm_find_acq_byseq()
2217 (mark & x->mark.m) == x->mark.v && in __xfrm_find_acq_byseq()
2218 x->km.state == XFRM_STATE_ACQ) { in __xfrm_find_acq_byseq()
2219 xfrm_state_hold(x); in __xfrm_find_acq_byseq()
2220 return x; in __xfrm_find_acq_byseq()
2229 struct xfrm_state *x; in xfrm_find_acq_byseq() local
2231 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_find_acq_byseq()
2232 x = __xfrm_find_acq_byseq(net, mark, seq); in xfrm_find_acq_byseq()
2233 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_find_acq_byseq()
2234 return x; in xfrm_find_acq_byseq()
2259 /* IPCOMP spi is 16-bits. */ in verify_spi_info()
2262 return -EINVAL; in verify_spi_info()
2268 return -EINVAL; in verify_spi_info()
2273 return -EINVAL; in verify_spi_info()
2280 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high, in xfrm_alloc_spi() argument
2283 struct net *net = xs_net(x); in xfrm_alloc_spi()
2286 int err = -ENOENT; in xfrm_alloc_spi()
2290 u32 mark = x->mark.v & x->mark.m; in xfrm_alloc_spi()
2292 spin_lock_bh(&x->lock); in xfrm_alloc_spi()
2293 if (x->km.state == XFRM_STATE_DEAD) { in xfrm_alloc_spi()
2299 if (x->id.spi) in xfrm_alloc_spi()
2302 err = -ENOENT; in xfrm_alloc_spi()
2305 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); in xfrm_alloc_spi()
2314 for (h = 0; h < high-low+1; h++) { in xfrm_alloc_spi()
2316 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); in xfrm_alloc_spi()
2325 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_alloc_spi()
2326 x->id.spi = newspi; in xfrm_alloc_spi()
2327 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); in xfrm_alloc_spi()
2328 XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h, in xfrm_alloc_spi()
2329 x->xso.type); in xfrm_alloc_spi()
2330 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_alloc_spi()
2338 spin_unlock_bh(&x->lock); in xfrm_alloc_spi()
2344 static bool __xfrm_state_filter_match(struct xfrm_state *x, in __xfrm_state_filter_match() argument
2348 if ((filter->family == AF_INET || in __xfrm_state_filter_match()
2349 filter->family == AF_INET6) && in __xfrm_state_filter_match()
2350 x->props.family != filter->family) in __xfrm_state_filter_match()
2353 return addr_match(&x->props.saddr, &filter->saddr, in __xfrm_state_filter_match()
2354 filter->splen) && in __xfrm_state_filter_match()
2355 addr_match(&x->id.daddr, &filter->daddr, in __xfrm_state_filter_match()
2356 filter->dplen); in __xfrm_state_filter_match()
2366 struct xfrm_state_walk *x; in xfrm_state_walk() local
2369 if (walk->seq != 0 && list_empty(&walk->all)) in xfrm_state_walk()
2372 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_walk()
2373 if (list_empty(&walk->all)) in xfrm_state_walk()
2374 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all); in xfrm_state_walk()
2376 x = list_first_entry(&walk->all, struct xfrm_state_walk, all); in xfrm_state_walk()
2377 list_for_each_entry_from(x, &net->xfrm.state_all, all) { in xfrm_state_walk()
2378 if (x->state == XFRM_STATE_DEAD) in xfrm_state_walk()
2380 state = container_of(x, struct xfrm_state, km); in xfrm_state_walk()
2381 if (!xfrm_id_proto_match(state->id.proto, walk->proto)) in xfrm_state_walk()
2383 if (!__xfrm_state_filter_match(state, walk->filter)) in xfrm_state_walk()
2385 err = func(state, walk->seq, data); in xfrm_state_walk()
2387 list_move_tail(&walk->all, &x->all); in xfrm_state_walk()
2390 walk->seq++; in xfrm_state_walk()
2392 if (walk->seq == 0) { in xfrm_state_walk()
2393 err = -ENOENT; in xfrm_state_walk()
2396 list_del_init(&walk->all); in xfrm_state_walk()
2398 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_walk()
2406 INIT_LIST_HEAD(&walk->all); in xfrm_state_walk_init()
2407 walk->proto = proto; in xfrm_state_walk_init()
2408 walk->state = XFRM_STATE_DEAD; in xfrm_state_walk_init()
2409 walk->seq = 0; in xfrm_state_walk_init()
2410 walk->filter = filter; in xfrm_state_walk_init()
2416 kfree(walk->filter); in xfrm_state_walk_done()
2418 if (list_empty(&walk->all)) in xfrm_state_walk_done()
2421 spin_lock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_walk_done()
2422 list_del(&walk->all); in xfrm_state_walk_done()
2423 spin_unlock_bh(&net->xfrm.xfrm_state_lock); in xfrm_state_walk_done()
2429 struct xfrm_state *x = from_timer(x, t, rtimer); in xfrm_replay_timer_handler() local
2431 spin_lock(&x->lock); in xfrm_replay_timer_handler()
2433 if (x->km.state == XFRM_STATE_VALID) { in xfrm_replay_timer_handler()
2434 if (xfrm_aevent_is_on(xs_net(x))) in xfrm_replay_timer_handler()
2435 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT); in xfrm_replay_timer_handler()
2437 x->xflags |= XFRM_TIME_DEFER; in xfrm_replay_timer_handler()
2440 spin_unlock(&x->lock); in xfrm_replay_timer_handler()
2451 if (km->notify_policy) in km_policy_notify()
2452 km->notify_policy(xp, dir, c); in km_policy_notify()
2456 void km_state_notify(struct xfrm_state *x, const struct km_event *c) in km_state_notify() argument
2461 if (km->notify) in km_state_notify()
2462 km->notify(x, c); in km_state_notify()
2469 void km_state_expired(struct xfrm_state *x, int hard, u32 portid) in km_state_expired() argument
2476 km_state_notify(x, &c); in km_state_expired()
2484 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) in km_query() argument
2486 int err = -EINVAL, acqret; in km_query()
2491 acqret = km->acquire(x, t, pol); in km_query()
2500 static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) in __km_new_mapping() argument
2502 int err = -EINVAL; in __km_new_mapping()
2507 if (km->new_mapping) in __km_new_mapping()
2508 err = km->new_mapping(x, ipaddr, sport); in __km_new_mapping()
2516 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) in km_new_mapping() argument
2520 if (x->mapping_maxage) { in km_new_mapping()
2521 if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage || in km_new_mapping()
2522 x->new_mapping_sport != sport) { in km_new_mapping()
2523 x->new_mapping_sport = sport; in km_new_mapping()
2524 x->new_mapping = jiffies / HZ; in km_new_mapping()
2525 ret = __km_new_mapping(x, ipaddr, sport); in km_new_mapping()
2528 ret = __km_new_mapping(x, ipaddr, sport); in km_new_mapping()
2552 int err = -EINVAL; in km_migrate()
2558 if (km->migrate) { in km_migrate()
2559 ret = km->migrate(sel, dir, type, m, num_migrate, k, in km_migrate()
2573 int err = -EINVAL; in km_report()
2579 if (km->report) { in km_report()
2580 ret = km->report(net, proto, sel, addr); in km_report()
2597 if (km->is_alive && km->is_alive(c)) { in km_is_alive()
2619 if (!try_module_get(xtr->owner)) in xfrm_get_translator()
2629 module_put(xtr->owner); in xfrm_put_translator()
2639 err = -EEXIST; in xfrm_register_translator()
2655 err = -EINVAL; in xfrm_unregister_translator()
2682 return -EMSGSIZE; in xfrm_user_policy()
2693 return -EOPNOTSUPP; in xfrm_user_policy()
2696 err = xtr->xlate_user_policy_sockptr(&data, optlen); in xfrm_user_policy()
2704 err = -EINVAL; in xfrm_user_policy()
2707 pol = km->compile_policy(sk, optname, data, in xfrm_user_policy()
2731 list_add_tail_rcu(&km->list, &xfrm_km_list); in xfrm_register_km()
2739 list_del_rcu(&km->list); in xfrm_unregister_km()
2749 if (WARN_ON(afinfo->family >= NPROTO)) in xfrm_state_register_afinfo()
2750 return -EAFNOSUPPORT; in xfrm_state_register_afinfo()
2753 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) in xfrm_state_register_afinfo()
2754 err = -EEXIST; in xfrm_state_register_afinfo()
2756 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo); in xfrm_state_register_afinfo()
2764 int err = 0, family = afinfo->family; in xfrm_state_unregister_afinfo()
2767 return -EAFNOSUPPORT; in xfrm_state_unregister_afinfo()
2770 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { in xfrm_state_unregister_afinfo()
2772 err = -EINVAL; in xfrm_state_unregister_afinfo()
2774 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL); in xfrm_state_unregister_afinfo()
2810 void xfrm_state_delete_tunnel(struct xfrm_state *x) in xfrm_state_delete_tunnel() argument
2812 if (x->tunnel) { in xfrm_state_delete_tunnel()
2813 struct xfrm_state *t = x->tunnel; in xfrm_state_delete_tunnel()
2815 if (atomic_read(&t->tunnel_users) == 2) in xfrm_state_delete_tunnel()
2817 atomic_dec(&t->tunnel_users); in xfrm_state_delete_tunnel()
2819 x->tunnel = NULL; in xfrm_state_delete_tunnel()
2824 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu) in xfrm_state_mtu() argument
2826 const struct xfrm_type *type = READ_ONCE(x->type); in xfrm_state_mtu()
2830 if (x->km.state != XFRM_STATE_VALID || in xfrm_state_mtu()
2831 !type || type->proto != IPPROTO_ESP) in xfrm_state_mtu()
2832 return mtu - x->props.header_len; in xfrm_state_mtu()
2834 aead = x->data; in xfrm_state_mtu()
2837 switch (x->props.mode) { in xfrm_state_mtu()
2840 if (x->props.family == AF_INET) in xfrm_state_mtu()
2842 else if (x->props.family == AF_INET6) in xfrm_state_mtu()
2852 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) - in xfrm_state_mtu()
2853 net_adj) & ~(blksize - 1)) + net_adj - 2; in xfrm_state_mtu()
2857 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload, in __xfrm_init_state() argument
2862 int family = x->props.family; in __xfrm_init_state()
2866 READ_ONCE(xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)) in __xfrm_init_state()
2867 x->props.flags |= XFRM_STATE_NOPMTUDISC; in __xfrm_init_state()
2869 err = -EPROTONOSUPPORT; in __xfrm_init_state()
2871 if (x->sel.family != AF_UNSPEC) { in __xfrm_init_state()
2872 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family); in __xfrm_init_state()
2878 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) && in __xfrm_init_state()
2879 family != x->sel.family) { in __xfrm_init_state()
2884 x->inner_mode = *inner_mode; in __xfrm_init_state()
2889 inner_mode = xfrm_get_mode(x->props.mode, x->props.family); in __xfrm_init_state()
2895 x->inner_mode = *inner_mode; in __xfrm_init_state()
2897 if (x->props.family == AF_INET) in __xfrm_init_state()
2900 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily); in __xfrm_init_state()
2902 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL) in __xfrm_init_state()
2903 x->inner_mode_iaf = *inner_mode_iaf; in __xfrm_init_state()
2907 x->type = xfrm_get_type(x->id.proto, family); in __xfrm_init_state()
2908 if (x->type == NULL) { in __xfrm_init_state()
2913 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload); in __xfrm_init_state()
2915 err = x->type->init_state(x, extack); in __xfrm_init_state()
2919 outer_mode = xfrm_get_mode(x->props.mode, family); in __xfrm_init_state()
2922 err = -EPROTONOSUPPORT; in __xfrm_init_state()
2926 x->outer_mode = *outer_mode; in __xfrm_init_state()
2928 err = xfrm_init_replay(x, extack); in __xfrm_init_state()
2933 if (x->nat_keepalive_interval) { in __xfrm_init_state()
2934 if (x->dir != XFRM_SA_DIR_OUT) { in __xfrm_init_state()
2936 err = -EINVAL; in __xfrm_init_state()
2940 if (!x->encap || x->encap->encap_type != UDP_ENCAP_ESPINUDP) { in __xfrm_init_state()
2943 err = -EINVAL; in __xfrm_init_state()
2954 int xfrm_init_state(struct xfrm_state *x) in xfrm_init_state() argument
2958 err = __xfrm_init_state(x, true, false, NULL); in xfrm_init_state()
2960 x->km.state = XFRM_STATE_VALID; in xfrm_init_state()
2975 INIT_LIST_HEAD(&net->xfrm.state_all); in xfrm_state_init()
2979 net->xfrm.state_bydst = xfrm_hash_alloc(sz); in xfrm_state_init()
2980 if (!net->xfrm.state_bydst) in xfrm_state_init()
2982 net->xfrm.state_bysrc = xfrm_hash_alloc(sz); in xfrm_state_init()
2983 if (!net->xfrm.state_bysrc) in xfrm_state_init()
2985 net->xfrm.state_byspi = xfrm_hash_alloc(sz); in xfrm_state_init()
2986 if (!net->xfrm.state_byspi) in xfrm_state_init()
2988 net->xfrm.state_byseq = xfrm_hash_alloc(sz); in xfrm_state_init()
2989 if (!net->xfrm.state_byseq) in xfrm_state_init()
2991 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1); in xfrm_state_init()
2993 net->xfrm.state_num = 0; in xfrm_state_init()
2994 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize); in xfrm_state_init()
2995 spin_lock_init(&net->xfrm.xfrm_state_lock); in xfrm_state_init()
2996 seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation, in xfrm_state_init()
2997 &net->xfrm.xfrm_state_lock); in xfrm_state_init()
3001 xfrm_hash_free(net->xfrm.state_byspi, sz); in xfrm_state_init()
3003 xfrm_hash_free(net->xfrm.state_bysrc, sz); in xfrm_state_init()
3005 xfrm_hash_free(net->xfrm.state_bydst, sz); in xfrm_state_init()
3007 return -ENOMEM; in xfrm_state_init()
3014 flush_work(&net->xfrm.state_hash_work); in xfrm_state_fini()
3018 WARN_ON(!list_empty(&net->xfrm.state_all)); in xfrm_state_fini()
3020 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head); in xfrm_state_fini()
3021 WARN_ON(!hlist_empty(net->xfrm.state_byseq)); in xfrm_state_fini()
3022 xfrm_hash_free(net->xfrm.state_byseq, sz); in xfrm_state_fini()
3023 WARN_ON(!hlist_empty(net->xfrm.state_byspi)); in xfrm_state_fini()
3024 xfrm_hash_free(net->xfrm.state_byspi, sz); in xfrm_state_fini()
3025 WARN_ON(!hlist_empty(net->xfrm.state_bysrc)); in xfrm_state_fini()
3026 xfrm_hash_free(net->xfrm.state_bysrc, sz); in xfrm_state_fini()
3027 WARN_ON(!hlist_empty(net->xfrm.state_bydst)); in xfrm_state_fini()
3028 xfrm_hash_free(net->xfrm.state_bydst, sz); in xfrm_state_fini()
3032 static void xfrm_audit_helper_sainfo(struct xfrm_state *x, in xfrm_audit_helper_sainfo() argument
3035 struct xfrm_sec_ctx *ctx = x->security; in xfrm_audit_helper_sainfo()
3036 u32 spi = ntohl(x->id.spi); in xfrm_audit_helper_sainfo()
3040 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); in xfrm_audit_helper_sainfo()
3042 switch (x->props.family) { in xfrm_audit_helper_sainfo()
3045 &x->props.saddr.a4, &x->id.daddr.a4); in xfrm_audit_helper_sainfo()
3049 x->props.saddr.a6, x->id.daddr.a6); in xfrm_audit_helper_sainfo()
3053 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); in xfrm_audit_helper_sainfo()
3066 &iph4->saddr, &iph4->daddr); in xfrm_audit_helper_pktinfo()
3071 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x", in xfrm_audit_helper_pktinfo()
3072 &iph6->saddr, &iph6->daddr, in xfrm_audit_helper_pktinfo()
3073 iph6->flow_lbl[0] & 0x0f, in xfrm_audit_helper_pktinfo()
3074 iph6->flow_lbl[1], in xfrm_audit_helper_pktinfo()
3075 iph6->flow_lbl[2]); in xfrm_audit_helper_pktinfo()
3080 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid) in xfrm_audit_state_add() argument
3084 audit_buf = xfrm_audit_start("SAD-add"); in xfrm_audit_state_add()
3088 xfrm_audit_helper_sainfo(x, audit_buf); in xfrm_audit_state_add()
3094 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid) in xfrm_audit_state_delete() argument
3098 audit_buf = xfrm_audit_start("SAD-delete"); in xfrm_audit_state_delete()
3102 xfrm_audit_helper_sainfo(x, audit_buf); in xfrm_audit_state_delete()
3108 void xfrm_audit_state_replay_overflow(struct xfrm_state *x, in xfrm_audit_state_replay_overflow() argument
3114 audit_buf = xfrm_audit_start("SA-replay-overflow"); in xfrm_audit_state_replay_overflow()
3117 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); in xfrm_audit_state_replay_overflow()
3120 spi = ntohl(x->id.spi); in xfrm_audit_state_replay_overflow()
3121 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); in xfrm_audit_state_replay_overflow()
3126 void xfrm_audit_state_replay(struct xfrm_state *x, in xfrm_audit_state_replay() argument
3132 audit_buf = xfrm_audit_start("SA-replayed-pkt"); in xfrm_audit_state_replay()
3135 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); in xfrm_audit_state_replay()
3136 spi = ntohl(x->id.spi); in xfrm_audit_state_replay()
3137 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", in xfrm_audit_state_replay()
3147 audit_buf = xfrm_audit_start("SA-notfound"); in xfrm_audit_state_notfound_simple()
3161 audit_buf = xfrm_audit_start("SA-notfound"); in xfrm_audit_state_notfound()
3166 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", in xfrm_audit_state_notfound()
3172 void xfrm_audit_state_icvfail(struct xfrm_state *x, in xfrm_audit_state_icvfail() argument
3179 audit_buf = xfrm_audit_start("SA-icv-failure"); in xfrm_audit_state_icvfail()
3182 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); in xfrm_audit_state_icvfail()
3185 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", in xfrm_audit_state_icvfail()