Thread View: linux.kernel.netdev
1 messages
1 total messages
Started by David Ahern
Tue, 01 Dec 2026 17:40
[PATCH net-next v3 10/13] ipv6: stamp dsts with the generation sampled before the FIB lookup
Author: David Ahern
Date: Tue, 01 Dec 2026 17:40
Date: Tue, 01 Dec 2026 17:40
275 lines
19077 bytes
19077 bytes
From: David Ahern <dahern@nvidia.com>
A dst is stamped with the netns route generation in ip6_dst_alloc(),
which runs after the FIB has been read. A route change that lands
between the lookup and the allocation is folded into the stamp: the
dst was built from the old tree but carries the new generation, so
ip6_dst_check() accepts it and it can keep bypassing the new route
indefinitely.
Sample the generation before the FIB is walked, carry it in
struct fib6_result and stamp every dst built from that result with it.
Writers bump the generation after the route change is visible, so a
lookup that read the old tree cannot have sampled the new generation;
the resulting dst fails ip6_dst_check() as soon as the change is
published. Use an acquire read for the sample so the FIB reads are not
reordered before it.
The sample is taken in fib6_table_lookup(), ip6_pol_route_lookup() and
__ip6_route_redirect(). Exceptions created from an existing dst in
__ip6_rt_update_pmtu() and rt6_do_redirect() inherit that dst's stamp
instead of the current generation, taken with rt6_dst_sernum() so that
a dst already marked stale still passes on the generation it was built
under. The copy that replaces a stale exception in
rt6_refresh_exception() is stamped from the lookup result too; every
caller samples, and a debug warning catches one that does not. Dsts
allocated without a lookup result keep the stamp taken in
ip6_dst_alloc().
A lookup can also return a cached dst that a later lookup built:
between the sample and reading the pcpu slot, a lookup on the same CPU
(softirq, preemption, migration) can run after a FIB change and
install a pcpu dst stamped with the newer generation for the route
this lookup selected, and an exception can be refreshed by another CPU
in the same way. Such a dst passes the validity checks, but the
decision it is returned for predates the change. ip6_pol_route() makes
a dst whose stamp differs from its sample fail ip6_dst_check(), so its
users look the route up again. This also covers callers that use
ip6_pol_route() directly, such as seg6, which caches the result in a
dst_cache.
The sample is taken for one table. A lookup that reads several, the
local table and then the main one, or those the rules select, decides
from all of them: a route that is added to a table that was already
read, and missed, while a later one is read is not part of the
decision, but the dst of the later table is stamped after the change
and would look current. fib6_rule_lookup() therefore samples before
the first table, and if the dst it returns carries a newer stamp, a
change landed in between and the dst is made to fail ip6_dst_check()
so that its users look the route up again. That only happens when the
generation moved during the lookup.
Reported-by: Ralf Lici <ralf@mandelbit.com>
Signed-off-by: David Ahern <dahern@nvidia.com>
---
include/net/ip6_fib.h | 11 +++++++++++
include/net/net_namespace.h | 10 ++++++++++
net/ipv6/fib6_rules.c | 33 ++++++++++++++++++++++++++++++---
net/ipv6/route.c | 30 +++++++++++++++++++++++++++++-
4 files changed, 80 insertions(+), 4 deletions(-)
diff --git a/include/net/ip6_fib.h b/include/net/ip6_fib.h
index ef5138ae7075..1c70176751d5 100644
--- a/include/net/ip6_fib.h
+++ b/include/net/ip6_fib.h
@@ -231,6 +231,11 @@ struct fib6_result {
u32 fib6_flags;
u8 fib6_type;
struct rt6_info *rt6;
+ /* Route generation sampled before the FIB was read. Dsts built
+ * from this result are stamped with it, not with the generation
+ * at allocation time. 0 means not sampled.
+ */
+ int sernum;
};
#define for_each_fib6_node_rt_rcu(fn) \
@@ -305,6 +310,12 @@ static inline void rt6_dst_invalidate(struct rt6_info *rt)
WRITE_ONCE(rt->sernum, sernum | RT6_SERNUM_STALE);
}
+/* The generation @rt was built under, whether or not it is still valid */
+static inline int rt6_dst_sernum(const struct rt6_info *rt)
+{
+ return READ_ONCE(rt->sernum) & ~RT6_SERNUM_STALE;
+}
+
static inline void ip6_rt_put(struct rt6_info *rt)
{
/* dst_release() accepts a NULL parameter.
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index f39baf51bcc5..31ffac7baf73 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -557,6 +557,16 @@ static inline int rt_genid_ipv6(const struct net *net)
return atomic_read(&net->ipv6.fib6_sernum);
}
+/* Sample the generation before walking the FIB. Writers bump it after the
+ * route change is visible, so a lookup that sees the old tree cannot sample
+ * the new generation: whatever dst it builds carries a generation that
+ * fails ip6_dst_check() once the change lands.
+ */
+static inline int rt_genid_ipv6_sample(const struct net *net)
+{
+ return atomic_read_acquire(&net->ipv6.fib6_sernum);
+}
+
/* Invalidate every cached IPv6 dst in @net. Returns the new generation,
* which is never 0.
*/
diff --git a/net/ipv6/fib6_rules.c b/net/ipv6/fib6_rules.c
index 7efb0515b5c8..8b64b5d44e18 100644
--- a/net/ipv6/fib6_rules.c
+++ b/net/ipv6/fib6_rules.c
@@ -96,10 +96,37 @@ int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
EXPORT_SYMBOL_GPL(fib6_lookup);
#endif
+/* A dst that a table lookup returns is stamped with the generation sampled
+ * before that table was read, so a change that lands after the read cannot
+ * be folded into the stamp. A lookup that reads several tables, the local
+ * one and then the main one, or those the rules select, makes its decision
+ * from all of them. A route that is added to a table that was already read,
+ * and missed, while a later one is being read is not part of that decision,
+ * but the dst of the later table is stamped after the change and would look
+ * current. Sample before the first table, and if the dst carries a newer
+ * stamp a change landed in between: make it fail ip6_dst_check(), so that
+ * its users look the route up again.
+ */
+static struct dst_entry *fib6_rule_lookup_done(struct dst_entry *dst,
+ int sernum)
+{
+ struct rt6_info *rt = dst_rt6_info(dst);
+
+ /* The static null, prohibit and blackhole entries are not made from a
+ * route and not stamped
+ */
+ if (rcu_access_pointer(rt->from) && rt6_dst_sernum(rt) != sernum)
+ rt6_dst_invalidate(rt);
+
+ return dst;
+}
+
struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
const struct sk_buff *skb,
int flags, pol_lookup_t lookup)
{
+ int sernum = rt_genid_ipv6_sample(net);
+
if (net->ipv6.fib6_has_custom_rules) {
struct fib6_result res = {};
struct fib_lookup_arg arg = {
@@ -116,19 +143,19 @@ struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
flowi6_to_flowi(fl6), flags, &arg);
if (res.rt6)
- return &res.rt6->dst;
+ return fib6_rule_lookup_done(&res.rt6->dst, sernum);
} else {
struct rt6_info *rt;
rt = pol_lookup_func(lookup,
net, net->ipv6.fib6_local_tbl, fl6, skb, flags);
if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
- return &rt->dst;
+ return fib6_rule_lookup_done(&rt->dst, sernum);
ip6_rt_put_flags(rt, flags);
rt = pol_lookup_func(lookup,
net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
if (rt->dst.error != -EAGAIN)
- return &rt->dst;
+ return fib6_rule_lookup_done(&rt->dst, sernum);
ip6_rt_put_flags(rt, flags);
}
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 959157896f98..8df141363e12 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1214,6 +1214,12 @@ static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
ip6_rt_init_dst(rt, res);
+ /* Pair the dst with the generation the FIB was read under, so a
+ * change between the lookup and this allocation is not folded in.
+ */
+ if (res->sernum)
+ rt->sernum = res->sernum;
+
rt->rt6i_dst = f6i->fib6_dst;
rt->rt6i_flags = res->fib6_flags;
if (nh->fib_nh_gw_family) {
@@ -1299,6 +1305,7 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
bool retry = false;
rcu_read_lock();
+ res.sernum = rt_genid_ipv6_sample(net);
fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
restart:
res.f6i = rcu_dereference(fn->leaf);
@@ -2230,7 +2237,10 @@ static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
* Dropping it instead would make every FIB change in the netns discard all
* learned PMTUs and redirects.
*
- * The copy is stamped like any dst built from this result. Caller holds
+ * The copy is stamped with the generation sampled before the FIB was read,
+ * like any dst built from this result, so @res must carry that sample:
+ * without it the copy would be stamped when it is allocated and could look
+ * current although the FIB changed after the lookup. Caller holds
* rcu_read_lock().
*/
static struct rt6_info *rt6_refresh_exception(const struct fib6_result *res,
@@ -2241,6 +2251,8 @@ static struct rt6_info *rt6_refresh_exception(const struct fib6_result *res,
struct rt6_info *nrt;
int err;
+ DEBUG_NET_WARN_ON_ONCE(!res->sernum);
+
#ifdef CONFIG_IPV6_SUBTREES
if (old->rt6i_src.plen)
saddr = &old->rt6i_src.addr;
@@ -2685,6 +2697,7 @@ int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
{
struct fib6_node *fn, *saved_fn;
+ res->sernum = rt_genid_ipv6_sample(net);
fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
saved_fn = fn;
@@ -2800,6 +2813,17 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
local_bh_enable();
}
out:
+ /* A cached dst, pcpu or exception, can have been built or refreshed
+ * by a lookup that ran after a FIB change landed behind our sample:
+ * it is stamped with the newer generation and passes
+ * rt6_get_pcpu_route() and the exception lookup, but our decision
+ * predates that change. Make it fail ip6_dst_check() so that its
+ * users look the route up again, as fib6_rule_lookup_done() does
+ * across tables; this also covers callers that bypass that wrapper.
+ */
+ if (rt && rt6_dst_sernum(rt) != res.sernum)
+ rt6_dst_invalidate(rt);
+
if (!rt)
rt = net->ipv6.ip6_null_entry;
if (!(flags & RT6_LOOKUP_F_DST_NOREF))
@@ -3521,6 +3545,8 @@ static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
res.fib6_flags = res.f6i->fib6_flags;
res.fib6_type = res.f6i->fib6_type;
+ /* derived from @rt6: it is only as current as that dst */
+ res.sernum = rt6_dst_sernum(rt6);
if (res.f6i->nh) {
struct fib6_nh_match_arg arg = {
@@ -3700,6 +3726,7 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
*/
rcu_read_lock();
+ res.sernum = rt_genid_ipv6_sample(net);
fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
restart:
for_each_fib6_node_rt_rcu(fn) {
@@ -4925,6 +4952,7 @@ static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_bu
res.fib6_flags = res.f6i->fib6_flags;
res.fib6_type = res.f6i->fib6_type;
+ res.sernum = rt6_dst_sernum(rt);
nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
if (!nrt)
goto out;
--
2.54.0 (Apple Git-157)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads