From: Ralf Lici <ralf@mandelbit.com>
To: netdev@vger.kernel.org
Cc: Ralf Lici <ralf@mandelbit.com>, David Ahern <dsahern@kernel.org>,
Ido Schimmel <idosch@nvidia.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Steffen Klassert <steffen.klassert@secunet.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
Florian Westphal <fw@strlen.de>,
Xia Yan <yanxia@cmss.chinamobile.com>,
Sabrina Dubroca <sd@queasysnail.net>,
Antonio Quartulli <antonio@mandelbit.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH net-next v5 3/4] ipv6: sample the route's serial during the lookup
Date: Tue, 24 Nov 2026 17:27:09 +0100 [thread overview]
Message-ID: <773ec5346a43c3b6d9cd03e01569caea7043c577.1795537128.git.ralf@mandelbit.com> (raw)
In-Reply-To: <cover.1795537128.git.ralf@mandelbit.com>
ip6_dst_check validates a dst against a cookie that its holder reads
with rt6_get_cookie after the lookup has returned. For routes without
their own sernum, this is the current fn_sernum of the route's node, so
a FIB change or xfrm policy insertion between the lookup and the read
pairs an old dst with a new serial, and the stale dst keeps passing
dst_check until another relevant change.
Sample that state during the lookup instead. ip6_pol_route and
ip6_pol_route_lookup read the IPv6 generation before walking the FIB
and, once the route and path are selected, the fn_sernum of the route's
node followed by the generation again. If the generation changed in
between, or the route was unlinked meanwhile, the selection may predate
the change and the sample is 0. Store the sample in struct fib6_result
and stamp every dst built from the result with it.
The sample covers the lookup in one table. With fib rules, the table is
selected before the first generation read, so a rule inserted while the
rules are evaluated can leave the lookup on the previous table with a
stamp that still validates. Closing that needs the generation read
before rule selection and passed down to the table lookups, which is
left for a follow-up.
Writers that change the selection without advancing the serial are not
covered either: deleting one route of an ECMP group makes fib6_del_route
rebalance the remaining ones without a new serial, and changing the
forwarding sysctl changes how gateways are scored without one, so a
lookup racing with either can keep its previous path. Both cases
predate this series.
Nothing uses the stamp yet, the following patch does.
Suggested-by: David Ahern <dsahern@kernel.org>
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
New in v5
- Replaces the lookup cookie API and its users from v4. (David)
include/net/ip6_fib.h | 10 +++++++++
net/ipv6/ip6_fib.c | 4 ++++
net/ipv6/route.c | 49 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+)
diff --git a/include/net/ip6_fib.h b/include/net/ip6_fib.h
index 232289b439c3..75da95b2349b 100644
--- a/include/net/ip6_fib.h
+++ b/include/net/ip6_fib.h
@@ -208,6 +208,10 @@ struct rt6_info {
struct dst_entry dst;
struct fib6_info __rcu *from;
int sernum;
+ /* fn_sernum of the route's node, sampled by the lookup that built
+ * this dst, 0 if the lookup raced with a generation change
+ */
+ u32 cookie;
struct rt6key rt6i_dst;
struct rt6key rt6i_src;
@@ -225,6 +229,12 @@ struct fib6_result {
u32 fib6_flags;
u8 fib6_type;
struct rt6_info *rt6;
+ /* sampled by rt6_lookup_stamp: the IPv6 generation and the route
+ * node's fn_sernum, both 0 if the generation changed during the
+ * lookup
+ */
+ int gen;
+ u32 cookie;
};
#define for_each_fib6_node_rt_rcu(fn) \
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index e1d49897d6ab..c94968a8fcbd 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -123,6 +123,10 @@ static void fib6_walkers_skip_rt(struct net *net, struct fib6_info *rt)
fib6_walkers_replace_leaf(net, rt, next);
}
+/* The successful atomic_try_cmpxchg is fully ordered: it publishes the
+ * routing change before the new generation, and the new generation before
+ * the nodes stamped with it. rt6_lookup_stamp relies on both.
+ */
static int fib6_new_sernum(struct net *net)
{
int new, old = atomic_read(&net->ipv6.fib6_sernum);
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 98e3ca0644ab..8b98ee945c58 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1216,11 +1216,48 @@ static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
rt->rt6i_flags |= RTF_GATEWAY;
}
rt6_set_from(rt, f6i);
+ rt->cookie = res->cookie;
#ifdef CONFIG_IPV6_SUBTREES
rt->rt6i_src = f6i->fib6_src;
#endif
}
+static int rt6_lookup_gen(const struct net *net)
+{
+ /* the acquire pairs with the full barrier in fib6_new_sernum */
+ return atomic_read_acquire(&net->ipv6.fib6_sernum);
+}
+
+/* Record in @res the state the selected route was found under: the
+ * generation read by rt6_lookup_gen and the fn_sernum of the route's
+ * node. If a FIB change advanced the generation during the lookup, the
+ * selection may predate it, so record 0, which never validates.
+ */
+static void rt6_lookup_stamp(const struct net *net, struct fib6_result *res,
+ int gen)
+{
+ bool linked;
+ u32 cookie;
+
+ /* a route unlinked by a concurrent deletion has no node left, so no
+ * dst built from it should validate: treat it like a generation change
+ */
+ linked = fib6_get_cookie_safe(res->f6i, &cookie);
+
+ /* keep the FIB and fn_sernum reads before the final generation read,
+ * pairs with the full barrier in fib6_new_sernum
+ */
+ smp_rmb();
+ if (unlikely(!linked || rt_genid_ipv6(net) != gen)) {
+ res->gen = 0;
+ res->cookie = 0;
+ return;
+ }
+
+ res->gen = gen;
+ res->cookie = cookie;
+}
+
static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
struct in6_addr *saddr)
{
@@ -1291,8 +1328,11 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
struct fib6_result res = {};
struct fib6_node *fn;
struct rt6_info *rt;
+ int gen;
rcu_read_lock();
+ /* read before walking the FIB, rt6_lookup_stamp checks it again */
+ gen = rt6_lookup_gen(net);
fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
restart:
res.f6i = rcu_dereference(fn->leaf);
@@ -1311,11 +1351,13 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
dst_hold(&rt->dst);
goto out;
} else if (res.fib6_flags & RTF_REJECT) {
+ rt6_lookup_stamp(net, &res, gen);
goto do_create;
}
fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
fl6->flowi6_oif != 0, skb, flags);
+ rt6_lookup_stamp(net, &res, gen);
/* Search through exception table */
rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
@@ -2278,10 +2320,16 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
struct rt6_info *rt = NULL;
bool have_oif_match;
int strict = 0;
+ int gen;
WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
!rcu_read_lock_held());
+ /* read before the forwarding mode and the FIB, rt6_lookup_stamp
+ * checks it again
+ */
+ gen = rt6_lookup_gen(net);
+
strict |= flags & RT6_LOOKUP_F_IFACE;
strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
@@ -2296,6 +2344,7 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
have_oif_match = fl6->flowi6_iif == LOOPBACK_IFINDEX &&
oif == res.nh->fib_nh_dev->ifindex;
fib6_select_path(net, &res, fl6, oif, have_oif_match, skb, strict);
+ rt6_lookup_stamp(net, &res, gen);
/*Search through exception table */
rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
--
2.56.0
next prev parent reply other threads:[~2026-11-24 16:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 16:27 [PATCH net-next v5 0/4] ipv6: tie dst validation to the lookup that returned the dst Ralf Lici
2026-11-24 16:27 ` [PATCH net-next v5 1/4] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
2026-11-24 16:27 ` [PATCH net-next v5 2/4] ipv6: advance the serial after rebalancing in fib6_ifup and fib6_ifdown Ralf Lici
2026-11-24 16:27 ` Ralf Lici [this message]
2026-11-24 16:27 ` [PATCH net-next v5 4/4] ipv6: validate dsts against the state their lookup found 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=773ec5346a43c3b6d9cd03e01569caea7043c577.1795537128.git.ralf@mandelbit.com \
--to=ralf@mandelbit.com \
--cc=antonio@mandelbit.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=fw@strlen.de \
--cc=herbert@gondor.apana.org.au \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sd@queasysnail.net \
--cc=steffen.klassert@secunet.com \
--cc=yanxia@cmss.chinamobile.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®