🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25678

[PATCH ipsec v3 1/2] xfrm: advance the route generation when policies are removed

#25678
From: Ralf Lici
Date: Wed, 02 Dec 2026 08:52
173 lines
15481 bytes
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 <ralf@mandelbit.com>
---
 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
Technical Details
Message-ID: <c8dd9a89e7a293b30f69b12a8b8888e7bb47c241.1796200887.git.ralf@mandelbit.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375652-linux-kernel-netdev=dewy.ru
References: <cover.1796200887.git.ralf@mandelbit.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375652-linux-kernel-netdev=dewy.ru
From: ralf@mandelbit.com (Ralf Lici)
Newsgroups: linux.kernel.netdev
Subject: [PATCH ipsec v3 1/2] xfrm: advance the route generation when policies are removed
Date: Wed,  2 Dec 2026 09:52:02 +0100
Organization: Usenet Network
Message-ID: <c8dd9a89e7a293b30f69b12a8b8888e7bb47c241.1796200887.git.ralf@mandelbit.com>
References: <cover.1796200887.git.ralf@mandelbit.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1927723"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:mP8DMnleHjgOSChSfvm/dihk3eQ= sha256:9AZ7H/FQsnYA1FRGEaQlf/PpimvrNyJCer8EERfwkms=
	sha1:ZSLcTjrOgSpnP/K9L09Z5XIhfAc= sha256:/FXv/BeTvfmt37XYSJguAnuAzCA+1TjZjsIreceaQDk=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id C86C51175B9; Wed,  2 Dec 2026 11:52:49 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=mandelbit.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id iU2txkNocON2;
	Wed,  2 Dec 2026 11:52:46 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375652-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 384AD1175B7; Wed,  2 Dec 2026 11:52:38 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 1C4DC2D400; Wed,  2 Dec 2026 08:52:31 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796201550;
	b=psaO0mSRsVkw7N8d0Ksx15jLICpzLBMO7zzlkMxSaz65zL+A8NgQObS2MXsqfTS6NCYK
	 VdFOpxKJNaK76EGKymvcY/dccSI6yiDkHDZYN5ci7LcHXpdUXWTfYJVOBA4ojZKp1xBjj
	 cER9EY8GSfLpektw0gdDJqI+lpj2tCV2meusG9z4nRIip1cTyijz/biayYK5tMmCUjcuH
	 ZizsCz7JKbxOPkGf71WcVRPdp0I8KAuB2xEuW5M+E3A0i5Lwit82SkOQYWmeXpVU47QUn
	 OfY0YmiS5+v4yzWA1tjJgeqp8U8rVv5u+V/RugsXHtBVjU27QC7WbYB4jxnBbUKEzbw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796201550;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To:
	 Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding:X-Rspamd-Queue-Id;
	bh=yZTnWL6yHdffVPs0xWQEdCbb5zoWZTJE+52z25SZyrk=;
	b=zqizY7LTQNWZWvWnQ5dSHtLVwqMOUNXf0QIr0zEuOq37VazhPMz6mNnoSPyAb4JCqx2p
	 QJiO4LWHsS4riAWdqb2tDK9CiOQld610UjZ0UwgbX4As2W+85EoLyYztWPkWitoR3XhCc
	 XeS2RaedVux/bGw3BSpgf3qWlSAMSlB3K2sY916PXIXC1sbiWQ2Fm2U3d2LHdaOZoiPua
	 muTdUlEd77WAdsGG8/TbnKNczDx/zXgHsqwLZC0RDvLCGVfvV2z/7USpkAIPtwVgURmaK
	 VVw1FLSItiNf8nF/TZi/kvSFFel3BBkDAsdMnwYlfUNtvxjYZ1eJv+iQwspmBZjazkQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=nNkauEq8;
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id CC689606A69C;
	Wed,  2 Dec 2026 08:52:30 +0000 (UTC)
X-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
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.62
X-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==
X-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==
X-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
X-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)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4D4DA606A692
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com
X-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)
X-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==
X-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
X-Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Shuah Khan <shuah@kernel.org>,
	Nicolas Dichtel <nicolas.dichtel@6wind.com>,
	Sabrina Dubroca <sd@queasysnail.net>,
	netdev@vger.kernel.org,
	ipsec@lists.linux.dev,
	linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
X-Mailer: git-send-email 2.56.1
X-In-Reply-To: <cover.1796200887.git.ralf@mandelbit.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-Rspamd-Queue-Id: 4jfWjD6qddzKnhk