From: Ralf Lici <ralf@mandelbit.com>
To: Steffen Klassert <steffen.klassert@secunet.com>,
Herbert Xu <herbert@gondor.apana.org.au>
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
Subject: [PATCH ipsec v3 1/2] xfrm: advance the route generation when policies are removed
Date: Wed, 2 Dec 2026 09:52:02 +0100 [thread overview]
Message-ID: <c8dd9a89e7a293b30f69b12a8b8888e7bb47c241.1796200887.git.ralf@mandelbit.com> (raw)
In-Reply-To: <cover.1796200887.git.ralf@mandelbit.com>
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
next prev parent reply other threads:[~2026-12-02 8:52 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 8:52 [PATCH ipsec v3 0/2] " Ralf Lici
2026-12-02 8:52 ` Ralf Lici [this message]
2026-12-02 8:52 ` [PATCH ipsec v3 2/2] selftests: net: add a test for xfrm policy removal Ralf Lici
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c8dd9a89e7a293b30f69b12a8b8888e7bb47c241.1796200887.git.ralf@mandelbit.com \
--to=ralf@mandelbit.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=horms@kernel.org \
--cc=ipsec@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nicolas.dichtel@6wind.com \
--cc=pabeni@redhat.com \
--cc=sd@queasysnail.net \
--cc=shuah@kernel.org \
--cc=steffen.klassert@secunet.com \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®