From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.62 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796201545; b=TI45D3x2tDVABeatAhWN6FvQ1ZpmZ7MLsV46BTjNJq1RQFyschV3cahv/SJBigTA19r2 iacd2T8v0NLWkpMDxkEUfp1Zyj1MuS5xNypi10fz4Biiwuia7SdRfO8EYFw3xvjP2dQ9i Us+PZTpVe6Ec6BBVymBLKVog5eYAyqR3wYJeXezg+VBe85gjfsZDd/dFzuIsUeKQqL4eO QEJVygTp3Ziu7a3BMqup26fTMVu3CZideu2zFh0ntNCjJDPBLNGzgGj9Qg5tylaOpcChb rOleqW6yw7YrR5o5PSfECSexY1DtxYN1Rph9Nb7NX8dc1DCtv1RNpb6Ybm+7KpYwKmQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796201545; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=yZTnWL6yHdffVPs0xWQEdCbb5zoWZTJE+52z25SZyrk=; b=ks5NTYheQnkAITjf2Bh4UUhRCEGD2uwDDBUuAz68CSbVHX42apY6ydjyD5EhAyz+vY+F V9G+tXofvV3SuGxgs3NTlk9aW9sU5bpLhqjQ4ss/Y0OrniWwHC+yWrL7mS0Rf2UviGYL9 FXCiCo1l/BcUO9hBK7zcscRblQgRExXHYgeAkjddVWwHBLpBla9eOWETza0Z16G85ps/E QHoOnMjHWOr+bKB58U/7jMGUittiR2yIc1Q2ngOw8sPmL5oXhd3ZE+7E6l5xnsZ6LtAQt yTeH/KqgtznzzQra2gqWgj3ZZQ58T6SsOyGQidF3JoAJ7qG9i9/YFRCcHfo6zxVXKsg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=nNkauEq8; arc=none smtp.remote-ip=195.10.208.62 Received: from mout-b-202.mailbox.org (mout-b-202.mailbox.org [195.10.208.62]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D4DA606A692; Wed, 2 Dec 2026 08:52:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4D4DA606A692 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=nNkauEq8 Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-b-202.mailbox.org (Postfix) with ESMTPS id 4jfWjD6qddzKnhk; Wed, 02 Dec 2026 09:52:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1796201537; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yZTnWL6yHdffVPs0xWQEdCbb5zoWZTJE+52z25SZyrk=; b=nNkauEq8d7V4tUXLDMDj1BYnW4DU2TOnZCg+K0IFelLFCwK915bZKFlvIKXlsS1u/ddnnb C6X+U8k3YTQlkk1f/t75dcBKd6R70JXfimmUSa/U6VACXeg2vFwbyeoOrUOsyI8mG4eVKc ARryf8lJUBivBC52+1PYT4uF9i1D64/53tfvGU2nPf+hKDfG5hUxbjNGhKsNQQN58ZcJ5Z e9kYcPly62lxrcS1e4RuU8fMnc4QQofVupM+nDK+T43Z0WcWkropb/unMwR7RZ6ctzu4PX 6Jab6BAkEtbb7LXshIluOKbHm4Fd4sw452vcCorsOwTefm8XAKxXSvTM7/rRDA== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of ralf@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=ralf@mandelbit.com From: Ralf Lici To: Steffen Klassert , Herbert Xu Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Shuah Khan , Nicolas Dichtel , Sabrina Dubroca , netdev@vger.kernel.org, ipsec@lists.linux.dev, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH ipsec v3 1/2] xfrm: advance the route generation when policies are removed Date: Wed, 2 Dec 2026 09:52:02 +0100 Message-ID: X-Mailer: git-send-email 2.56.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4jfWjD6qddzKnhk Only xfrm_policy_insert and xfrm_set_default advance the IPv4 and IPv6 route generations. Deleting, flushing or expiring a policy does not, so a route that a socket cached while an allow policy without templates matched its flow keeps validating after that policy is removed, and the socket keeps sending in plaintext although a policy requiring IPsec now matches. xfrm_policy_insert also advances the generation before unlinking the policy that the new one replaces, which can still be matched until then, since the new policy can be linked behind it. Advance the generation once a policy is unlinked by a delete, once after all policies are unlinked by a flush, and after the replaced policy is unlinked by an insert. xfrm_policy_delete also runs from the policy timer, in softirq context, where the IPv6 bump, a walk of the FIB, would run once for each expiring policy, so leave it to a work item, which coalesces the expiries into one walk in process context. The IPv4 bump is a plain atomic_inc, so order the unlink before it. Socket policies only apply to their own socket and are left out. Fixes: ee8372dd1989 ("xfrm: invalidate dst on policy insertion/deletion") Signed-off-by: Ralf Lici --- include/net/netns/xfrm.h | 1 + net/xfrm/xfrm_policy.c | 58 +++++++++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/include/net/netns/xfrm.h b/include/net/netns/xfrm.h index b73983a17e08..56dc62c0c148 100644 --- a/include/net/netns/xfrm.h +++ b/include/net/netns/xfrm.h @@ -55,6 +55,7 @@ struct netns_xfrm { struct xfrm_policy_hash policy_bydst[XFRM_POLICY_MAX]; unsigned int policy_count[XFRM_POLICY_MAX * 2]; struct work_struct policy_hash_work; + struct work_struct policy_genid_ipv6_work; struct xfrm_policy_hthresh policy_hthresh; struct list_head inexact_bins; diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index d2e938d4db9a..82d24a9e7425 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -1569,6 +1569,25 @@ static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain, return delpol; } +static void xfrm_policy_bump_rt_genid(struct net *net, unsigned short family) +{ + if (family == AF_INET) { + /* Order the policy change before the new generation */ + smp_mb__before_atomic(); + rt_genid_bump_ipv4(net); + } else { + rt_genid_bump_ipv6(net); + } +} + +static void xfrm_policy_genid_ipv6_work(struct work_struct *work) +{ + struct net *net = container_of(work, struct net, + xfrm.policy_genid_ipv6_work); + + xfrm_policy_bump_rt_genid(net, AF_INET6); +} + int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) { struct net *net = xp_net(policy); @@ -1589,16 +1608,13 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) __xfrm_policy_link(policy, dir); - /* After previous checking, family can either be AF_INET or AF_INET6 */ - if (policy->family == AF_INET) - rt_genid_bump_ipv4(net); - else - rt_genid_bump_ipv6(net); - if (delpol) { xfrm_policy_requeue(delpol, policy); __xfrm_policy_unlink(delpol, dir); } + + /* Advance the route generation only once the replaced policy is gone */ + xfrm_policy_bump_rt_genid(net, policy->family); policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index); hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index)); policy->curlft.add_time = ktime_get_real_seconds(); @@ -1695,6 +1711,7 @@ xfrm_policy_bysel_ctx(struct net *net, const struct xfrm_mark *mark, u32 if_id, return pol; } __xfrm_policy_unlink(pol, dir); + xfrm_policy_bump_rt_genid(net, pol->family); } ret = pol; } @@ -1735,6 +1752,7 @@ xfrm_policy_byid(struct net *net, const struct xfrm_mark *mark, u32 if_id, return pol; } __xfrm_policy_unlink(pol, dir); + xfrm_policy_bump_rt_genid(net, pol->family); } ret = pol; break; @@ -1835,10 +1853,12 @@ int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) spin_lock_bh(&net->xfrm.xfrm_policy_lock); goto again; } - if (cnt) + if (cnt) { + rt_genid_bump_all(net); __xfrm_policy_inexact_flush(net); - else + } else { err = -ESRCH; + } out: spin_unlock_bh(&net->xfrm.xfrm_policy_lock); return err; @@ -1875,10 +1895,12 @@ int xfrm_dev_policy_flush(struct net *net, struct net_device *dev, spin_lock_bh(&net->xfrm.xfrm_policy_lock); goto again; } - if (cnt) + if (cnt) { + rt_genid_bump_all(net); __xfrm_policy_inexact_flush(net); - else + } else { err = -ESRCH; + } out: spin_unlock_bh(&net->xfrm.xfrm_policy_lock); return err; @@ -2365,6 +2387,16 @@ int xfrm_policy_delete(struct xfrm_policy *pol, int dir) spin_lock_bh(&net->xfrm.xfrm_policy_lock); pol = __xfrm_policy_unlink(pol, dir); + /* The policy timer calls this in softirq context, so leave the IPv6 + * FIB walk to process context, once for all the policies that expire + * together. Socket policies only apply to their own socket. + */ + if (pol && dir < XFRM_POLICY_MAX) { + if (pol->family == AF_INET) + xfrm_policy_bump_rt_genid(net, AF_INET); + else + schedule_work(&net->xfrm.policy_genid_ipv6_work); + } spin_unlock_bh(&net->xfrm.xfrm_policy_lock); if (pol) { xfrm_policy_kill(pol); @@ -4296,6 +4328,8 @@ static int __net_init xfrm_policy_init(struct net *net) INIT_LIST_HEAD(&net->xfrm.inexact_bins); INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize); INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild); + INIT_WORK(&net->xfrm.policy_genid_ipv6_work, + xfrm_policy_genid_ipv6_work); return 0; out_bydst: @@ -4318,6 +4352,10 @@ static void __net_exit xfrm_net_pre_exit(struct net *net) xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false); #endif xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false); + /* Before IPv6 goes away. A policy deleted by its timer before the + * flush queued the work under the policy lock. + */ + disable_work_sync(&net->xfrm.policy_genid_ipv6_work); } static void xfrm_policy_fini(struct net *net) -- 2.56.1