Skip to contentSkip to conversation

ipv6: validate dsts against the netns route generation at lookup

RFC New David Ahern 1 participant 8 messages Nov 23

Subsystems (3)
lore
Share & tools
lore

David Ahern

author maintainer
#0 lore raw

v2 cover ipv6: validate dsts against the netns route generation at lookup

From: David Ahern <[email protected]>

IPv6 dsts are validated in ip6_dst_check() against a cookie that the holder (socket, dst_cache, xfrm bundle, ...) samples from rt->from's fib6_node (fn_sernum) when it stores the dst. That is later than the lookup that produced the dst, which allows this sequence:

CPU0 CPU1
---- ----
lookup returns Dold
add a more-specific route (e.g. a blackhole /128), bump fn_sernum
rt6_get_cookie(Dold) reads the new
fn_sernum
publish (Dold, new cookie)

The next dst_check() compares the cookie with the same node's current fn_sernum and accepts Dold, which keeps bypassing the more-specific route indefinitely. The same late sampling exists in every persistent cache (socket TX/RX, xfrm, dst_cache users, SCTP, flowtable, IPVS, RXE), so fixing each user would leave future users exposed. This is the problem described in the thread where ovpn hit it.

This series moves the validity into the dst, as IPv4 does with rt_genid:

1. every dst carries the netns route generation it was built under and ip6_dst_check() compares it with the current one; holders no longer contribute a token.
2-5. keep invalidation targeted where it was before: ip6_link_failure()
clears the failing dst's sernum, nexthop group changes bump the generation, and PMTU/redirect exceptions invalidate only the pcpu dsts of the affected nexthop, as IPv4 does for fnhe.
6. rt_genid_bump_ipv6() becomes a counter increment like its IPv4 counterpart instead of walking every tree to rewrite fn_sernum.
7. the generation is sampled before the FIB is read and carried in struct fib6_result, so a dst and the FIB lookup that produced it refer to the same point in time. Without this a change landing between the lookup and the allocation is folded into the stamp.

Long-lived dsts are never re-stamped: pcpu and exception dsts are shared by all holders, so re-stamping one would make it valid again for every socket and dst_cache that already saw it as stale. A stale pcpu dst is dropped and rebuilt; a stale exception is treated as a miss. This is the same behaviour as IPv4, where a stale cached route fails rt_cache_valid() and a new one is allocated.

Known trade-off: any FIB change in a netns now invalidates every cached IPv6 dst in it, not just those under the affected node; stale dsts are rebuilt lazily on their next use.

Changes since v1:
- Do not re-stamp pcpu and exception dsts when a lookup finds them again (patch 1); that made stale dsts valid again for other holders.
- New patch 7: sample the generation before the FIB lookup.
- Updated the patch 1 commit message to match.

AI assistance: Claude (Sonnet 5.5) helped develop these patches. I have reviewed them and take responsibility for them.

Ralf: Patch 7 is alternative to your patch that adds the cookie to the APIs. Instead the result struct contains the genid before the lookup is done. Adding it to the fib6_result seems simpler and accomplishes the end goal everywhere.

David Ahern (7):
ipv6: validate dsts against the netns route generation
ipv6: invalidate the failing dst in ip6_link_failure()
nexthop: factor out nh_has_f6i()
nexthop: bump the IPv6 route generation on group changes
ipv6: scope exception invalidation to the affected nexthop
ipv6: make rt_genid_bump_ipv6() a plain counter increment
ipv6: stamp dsts with the generation sampled before the FIB lookup

 include/net/ip6_fib.h       |  50 +++++------------
 include/net/net_namespace.h |  36 ++++++++++---
 net/ipv4/nexthop.c          |  42 ++++++++++++---
 net/ipv6/addrconf_core.c    |   7 ---
 net/ipv6/ip6_fib.c          |  76 +++++++++++---------------
 net/ipv6/route.c            | 105 ++++++++++++++++++++++++------------
 6 files changed, 178 insertions(+), 138 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

RFC v2 1/7 ipv6: validate dsts against the netns route generation

From: David Ahern <[email protected]>

An rt6_info never carried the fib6 serial number it was created under. rt6_get_cookie() read fn->fn_sernum through rt->from at the time a socket, dst_cache or xfrm bundle stored its cookie, so a change that landed between route lookup and cookie capture was folded into the cookie and a stale dst looked valid. Only nexthop-object pcpu routes were stamped at creation, with the same counter.

net->ipv6.fib6_sernum is already the counter that rt_genid_ipv6() reads and that every FIB change bumps. Stamp every dst with it in ip6_dst_alloc() and validate with a plain comparison in ip6_dst_check(); the remaining checks are that the fib6_info is still in the tree and not expired. rt6_get_cookie() now returns rt->sernum and fib6_get_cookie_safe() goes away. fn_sernum stays for route dump consistency.

Validity lives in the dst, so a long-lived dst is never re-stamped by a later lookup: pcpu and exception dsts are shared by every socket and dst_cache that holds them, and re-stamping would make a stale dst valid again for all of them. IPv4 does the same: a stale cached route fails rt_cache_valid() and a new one is allocated, while holders of the old one keep seeing it as stale. Likewise here:

- a pcpu dst that fails the check is dropped from the pcpu slot and rebuilt by rt6_make_pcpu_route().
- an exception dst that fails the check is treated as a miss by rt6_find_cached_rt().

The smp_wmb() in __fib6_update_sernum_upto_root() paired with the smp_rmb() in fib6_get_cookie_safe(), so that a lockless reader that saw a new fn_sernum also saw the route inserted before it. That reader is gone. The remaining fn_sernum readers, the route dumps, take tb6_lock after reading it and the writers hold it, so drop the barrier.

ip6_link_failure() still writes fn_sernum, which no longer invalidates anything; that is addressed in the next patch.

---
Changes since v1:
- Do not re-stamp pcpu and exception dsts when a lookup finds them again (drop rt6_refresh_sernum()); a stale pcpu dst is dropped and rebuilt, a stale exception is treated as a miss.
- Commit message updated to match.

 include/net/ip6_fib.h | 42 ++++-----------------------------
 net/ipv6/ip6_fib.c    |  2 --
 net/ipv6/route.c      | 55 ++++++++++++++++++++++---------------------
 3 files changed, 32 insertions(+), 67 deletions(-)
include/net/ip6_fib.h +4 -38
index 232289b439c3..ecce7ab007a5 100644
@@ -273,46 +273,12 @@ static inline bool fib6_check_expired(const struct fib6_info *f6i)
 	return false;
 }
 
-/* Function to safely get fn->fn_sernum for passed in rt
- * and store result in passed in cookie.
- * Return true if we can get cookie safely
- * Return false if not
- */
-static inline bool fib6_get_cookie_safe(const struct fib6_info *f6i,
-					u32 *cookie)
-{
-	struct fib6_node *fn;
-	bool status = false;
-
-	fn = rcu_dereference(f6i->fib6_node);
-
-	if (fn) {
-		*cookie = READ_ONCE(fn->fn_sernum);
-		/* pairs with smp_wmb() in __fib6_update_sernum_upto_root() */
-		smp_rmb();
-		status = true;
-	}
-
-	return status;
-}
-
 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
 {
-	struct fib6_info *from;
-	u32 cookie = 0;
-
-	if (rt->sernum)
-		return rt->sernum;
-
-	rcu_read_lock();
-
-	from = rcu_dereference(rt->from);
-	if (from)
-		fib6_get_cookie_safe(from, &cookie);
-
-	rcu_read_unlock();
-
-	return cookie;
+	/* sernum is the netns route generation the dst was created or last
+	 * revalidated under; ip6_dst_check() compares it to the current one.
+	 */
+	return READ_ONCE(rt->sernum);
 }
 
 static inline void ip6_rt_put(struct rt6_info *rt)
net/ipv6/ip6_fib.c +0 -2
index e1d49897d6ab..d465ac6ffbc6 100644
@@ -1432,8 +1432,6 @@ static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
 	struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
 				lockdep_is_held(&rt->fib6_table->tb6_lock));
 
-	/* paired with smp_rmb() in fib6_get_cookie_safe() */
-	smp_wmb();
 	while (fn) {
 		WRITE_ONCE(fn->fn_sernum, sernum);
 		fn = rcu_dereference_protected(fn->parent,
net/ipv6/route.c +28 -27

120 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#2 lore raw

RFC v2 2/7 ipv6: invalidate the failing dst in ip6_link_failure()

In reply to David Ahern

From: David Ahern <[email protected]>

When a default route's neighbour fails, ip6_link_failure() wrote -1 to the fn_sernum of the route's node so that users of dsts under that node would look the route up again and let router selection pick another default router.

dsts are now validated against the netns route generation, so that write no longer invalidates anything. Clear the sernum of the failing dst instead; 0 never matches the generation, so its users do the lookup again. Only that dst is invalidated, which for a pcpu dst is one CPU's copy, rather than every dst under the node. Bumping the netns generation here would be much costlier, since a failing router can trigger this for every packet sent through it.

---
Changes since v1:
- None.

 net/ipv6/route.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
net/ipv6/route.c +5 -6
index 6fa76c95b1d2..a132d33259a2 100644
@@ -2874,14 +2874,13 @@ static void ip6_link_failure(struct sk_buff *skb)
 			rt6_remove_exception_rt(rt);
 		} else {
 			struct fib6_info *from;
-			struct fib6_node *fn;
 
+			/* Force sockets using this dst to look the route up
+			 * again; 0 never matches the netns generation.
+			 */
 			from = rcu_dereference(rt->from);
-			if (from) {
-				fn = rcu_dereference(from->fib6_node);
-				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
-					WRITE_ONCE(fn->fn_sernum, -1);
-			}
+			if (from && (rt->rt6i_flags & RTF_DEFAULT))
+				WRITE_ONCE(rt->sernum, 0);
 		}
 		rcu_read_unlock();
 	}
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

RFC v2 3/7 nexthop: factor out nh_has_f6i()

In reply to David Ahern

From: David Ahern <[email protected]>

nh_rt_cache_flush() open codes a locked check of whether any IPv6 routes use a nexthop. Move it into a helper, placed ahead of remove_nexthop_from_groups(), so the next patch can use it there as well.

No functional change.

---
Changes since v1:
- None.

 net/ipv4/nexthop.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
net/ipv4/nexthop.c +12 -5
index 42e55b5a755e..53431fe7d328 100644
@@ -2109,6 +2109,17 @@ remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
 	return false;
 }
 
+static bool nh_has_f6i(struct nexthop *nh)
+{
+	bool have_f6i;
+
+	spin_lock_bh(&nh->lock);
+	have_f6i = !list_empty(&nh->f6i_list);
+	spin_unlock_bh(&nh->lock);
+
+	return have_f6i;
+}
+
 static bool __must_check
 remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
 			   struct nl_info *nlinfo)
@@ -2248,16 +2259,12 @@ static void nh_rt_cache_flush(struct net *net, struct nexthop *nh,
 			      struct nexthop *replaced_nh)
 {
 	struct nh_group *nhg;
-	bool have_f6i;
 	int i;
 
 	if (!list_empty(&nh->fi_list))
 		rt_cache_flush(net);
 
-	spin_lock_bh(&nh->lock);
-	have_f6i = !list_empty(&nh->f6i_list);
-	spin_unlock_bh(&nh->lock);
-	if (have_f6i)
+	if (nh_has_f6i(nh))
 		rt_genid_bump_ipv6(net);
 
 	/* if an IPv6 group was replaced, we have to release all old
Signature · 1 line
2.43.0
author maintainer
#4 lore raw

RFC v2 4/7 nexthop: bump the IPv6 route generation on group changes

In reply to David Ahern

From: David Ahern <[email protected]>

IPv6 dsts are invalidated by bumping the netns route generation, but two nexthop object changes did not do so:

- Replacing a nexthop that is a member of a group only bumped if the member itself had IPv6 routes, not when routes used a group containing it.
- Removing a member from a group republishes the group array without any bump, so dsts built from the old group stayed valid.

Bump in both cases when IPv6 routes use the nexthop, directly or through a group.

---
Changes since v1:
- None.

 net/ipv4/nexthop.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
net/ipv4/nexthop.c +25 -2
index 53431fe7d328..7cf496589f4f 100644
@@ -2120,6 +2120,21 @@ static bool nh_has_f6i(struct nexthop *nh)
 	return have_f6i;
 }
 
+/* true if IPv6 routes use @nh directly or through a group containing it */
+static bool nh_used_by_f6i(struct nexthop *nh)
+{
+	struct nh_grp_entry *nhge;
+
+	if (nh_has_f6i(nh))
+		return true;
+
+	list_for_each_entry(nhge, &nh->grp_list, nh_list)
+		if (nh_has_f6i(nhge->nh_parent))
+			return true;
+
+	return false;
+}
+
 static bool __must_check
 remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
 			   struct nl_info *nlinfo)
@@ -2127,6 +2142,7 @@ remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
 	struct nh_grp_entry *nhge, *tmp;
 	LIST_HEAD(deferred_free);
 	bool need_flush = false;
+	bool have_f6i = false;
 
 	/* If there is nothing to do, let's avoid the costly call to
 	 * synchronize_net()
@@ -2134,13 +2150,19 @@ remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
 	if (list_empty(&nh->grp_list))
 		return false;
 
-	list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
+	list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list) {
+		have_f6i |= nh_has_f6i(nhge->nh_parent);
 		need_flush |= remove_nh_grp_entry(net, nhge, nlinfo,
 						  &deferred_free);
+	}
 
 	/* make sure all see the newly published array before releasing rtnl */
 	synchronize_net();
 
+	/* a group lost a member: dsts built from the old group are stale */
+	if (have_f6i)
+		rt_genid_bump_ipv6(net);
+
 	/* Now safe to free percpu stats — all RCU readers have finished */
 	list_for_each_entry_safe(nhge, tmp, &deferred_free, nh_list) {
 		list_del(&nhge->nh_list);
@@ -2264,7 +2286,8 @@ static void nh_rt_cache_flush(struct net *net, struct nexthop *nh,
 	if (!list_empty(&nh->fi_list))
 		rt_cache_flush(net);
 
-	if (nh_has_f6i(nh))
+	/* dsts are validated against the netns route generation */
+	if (nh_used_by_f6i(nh))
 		rt_genid_bump_ipv6(net);
 
 	/* if an IPv6 group was replaced, we have to release all old
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

RFC v2 5/7 ipv6: scope exception invalidation to the affected nexthop

In reply to David Ahern

From: David Ahern <[email protected]>

rt6_insert_exception() bumped the netns route generation, which with generation-based dst validation invalidates every dst in the namespace each time a PMTU or redirect exception is created. A burst of packet-too-big messages would make all sockets and dst_caches look their routes up again.

IPv4 limits this to the nexthop: update_or_create_fnhe() marks only that nexthop's cached dsts stale. Do the same here by clearing the sernum of the fib6_nh's pcpu dsts, so their users look the route up once and find the exception.

Route dumps notice a changed tree through the root node's fn_sernum, and the exception dump relies on that changing when an exception is inserted on the root. A fresh fn_sernum can only come from the netns counter, which must not be bumped here, and a locally made-up value could later be issued by the counter to the same node and go unnoticed. Add a per-table tb6_excp_seq, bumped for exceptions on the root node, and have fib6_dump_table() restart when either it or the root's fn_sernum has changed.

---
Changes since v1:
- None.

 include/net/ip6_fib.h |  3 +++
 net/ipv6/ip6_fib.c    | 25 ++++++++++++++++++++++++-
 net/ipv6/route.c      | 27 +++++++++++++++++++++++++--
 3 files changed, 52 insertions(+), 3 deletions(-)
include/net/ip6_fib.h +3 -0
index ecce7ab007a5..92fed30a7418 100644
@@ -329,6 +329,7 @@ struct fib6_walker {
 	unsigned int skip;
 	unsigned int count;
 	unsigned int skip_in_node;
+	unsigned int excp_seq;	/* tb6_excp_seq seen by a partial dump */
 	int (*func)(struct fib6_walker *);
 	void *args;
 };
@@ -362,6 +363,7 @@ struct fib6_table {
 	struct inet_peer_base	tb6_peers;
 	unsigned int		flags;
 	unsigned int		fib_seq; /* writes protected by rtnl_mutex */
+	unsigned int		tb6_excp_seq;	/* writes under tb6_lock */
 	struct hlist_head       tb6_gc_hlist;	/* GC candidates */
 #define RT6_TABLE_HAS_DFLT_ROUTER	BIT(0)
 };
@@ -583,6 +585,7 @@ int fib6_tables_dump(struct net *net, struct notifier_block *nb,
 		     struct netlink_ext_ack *extack);
 
 void fib6_update_sernum(struct net *net, struct fib6_info *rt);
+void fib6_update_excp_seq(struct fib6_info *rt);
 #if IS_ENABLED(CONFIG_IPV6)
 void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt);
 #else
net/ipv6/ip6_fib.c +24 -1
index d465ac6ffbc6..266370e15774 100644
@@ -148,6 +148,25 @@ void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
 		WRITE_ONCE(fn->fn_sernum, fib6_new_sernum(net));
 }
 
+/* Tell route dumps that an exception was added under @f6i without
+ * invalidating cached dsts: those follow the netns generation, which this
+ * leaves alone. A dump notices a changed tree through the root node, so
+ * only an exception on the root needs to be visible. Fresh fn_sernum values
+ * can only come from the netns counter, so use a table sequence rather than
+ * a made-up fn_sernum that the counter could later issue again.
+ * Caller holds tb6_lock.
+ */
+void fib6_update_excp_seq(struct fib6_info *f6i)
+{
+	struct fib6_table *table = f6i->fib6_table;
+	struct fib6_node *fn;
+
+	fn = rcu_dereference_protected(f6i->fib6_node,
+				       lockdep_is_held(&table->tb6_lock));
+	if (fn == &table->tb6_root)
+		WRITE_ONCE(table->tb6_excp_seq, table->tb6_excp_seq + 1);
+}
+
 /*
  *	Auxiliary address test functions for the radix tree.
  *
@@ -637,12 +656,16 @@ static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
 		if (res > 0) {
 			cb->args[4] = 1;
 			cb->args[5] = READ_ONCE(w->root->fn_sernum);
+			w->excp_seq = READ_ONCE(table->tb6_excp_seq);
 		}
 	} else {
 		int sernum = READ_ONCE(w->root->fn_sernum);
-		if (cb->args[5] != sernum) {
+		unsigned int excp_seq = READ_ONCE(table->tb6_excp_seq);
+
+		if (cb->args[5] != sernum || w->excp_seq != excp_seq) {
 			/* Begin at the root if the tree changed */
 			cb->args[5] = sernum;
+			w->excp_seq = excp_seq;
 			w->state = FWS_INIT;
 			w->node = w->root;
 			w->skip = w->count;
net/ipv6/route.c +25 -2
index a132d33259a2..38d5f3bc85bb 100644
@@ -1717,6 +1717,28 @@ struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
 	return bucket;
 }
 
+/* A new exception may apply to dsts already cached on @nh. Make them fail
+ * ip6_dst_check() once so their users look the route up again and find it;
+ * dsts of other routes are left alone.
+ */
+static void fib6_nh_invalidate_pcpu(const struct fib6_nh *nh)
+{
+	int cpu;
+
+	if (!nh->rt6i_pcpu)
+		return;
+
+	rcu_read_lock();
+	for_each_possible_cpu(cpu) {
+		struct rt6_info *pcpu_rt;
+
+		pcpu_rt = READ_ONCE(*per_cpu_ptr(nh->rt6i_pcpu, cpu));
+		if (pcpu_rt)
+			WRITE_ONCE(pcpu_rt->sernum, 0);
+	}
+	rcu_read_unlock();
+}
+
 static int rt6_insert_exception(struct rt6_info *nrt,
 				const struct fib6_result *res)
 {
@@ -1791,10 +1813,11 @@ static int rt6_insert_exception(struct rt6_info *nrt,
 out:
 	spin_unlock_bh(&rt6_exception_lock);
 
-	/* Update fn->fn_sernum to invalidate all cached dst */
 	if (!err) {
+		fib6_nh_invalidate_pcpu(nh);
+
 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
-		fib6_update_sernum(net, f6i);
+		fib6_update_excp_seq(f6i);
 		fib6_add_gc_list(f6i);
 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
 		fib6_force_start_gc(net);
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

RFC v2 6/7 ipv6: make rt_genid_bump_ipv6() a plain counter increment

In reply to David Ahern

From: David Ahern <[email protected]>

rt_genid_bump_ipv6() called fib6_flush_trees() through a function pointer. That walked every node of every table to write a new fn_sernum, so that cookies derived from fn_sernum would mismatch.

Cached dsts are now validated against net->ipv6.fib6_sernum itself, so the walk no longer invalidates anything. Do what rt_genid_bump_ipv4() does and increment the counter, keeping fib6_new_sernum()'s wrap to 1 so the value is never 0.

This makes nexthop, address and FIB rule changes cheap, and stops them from touching fn_sernum, which route dumps use to detect that a tree changed: a dump is no longer restarted by events that do not change the tree.

With the walk gone, drop the __fib6_flush_trees pointer and its export, fib6_new_sernum() in favour of the new helper, and the sernum handling in fib6_cleaner that only the walk used.

---
Changes since v1:
- None.

 include/net/net_namespace.h | 26 ++++++++++++++-----
 net/ipv6/addrconf_core.c    |  7 -----
 net/ipv6/ip6_fib.c          | 51 ++++++-------------------------------
 3 files changed, 27 insertions(+), 57 deletions(-)
include/net/net_namespace.h +19 -7
index 4a4d72080acd..f39baf51bcc5 100644
@@ -556,18 +556,30 @@ static inline int rt_genid_ipv6(const struct net *net)
 {
 	return atomic_read(&net->ipv6.fib6_sernum);
 }
-#endif
 
-static inline void rt_genid_bump_ipv4(struct net *net)
+/* Invalidate every cached IPv6 dst in @net. Returns the new generation,
+ * which is never 0.
+ */
+static inline int rt_genid_bump_ipv6(struct net *net)
 {
-	atomic_inc(&net->ipv4.rt_genid);
+	int new, old = atomic_read(&net->ipv6.fib6_sernum);
+
+	do {
+		new = old < INT_MAX ? old + 1 : 1;
+	} while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new));
+
+	return new;
 }
+#else
+static inline int rt_genid_bump_ipv6(struct net *net)
+{
+	return 0;
+}
+#endif
 
-extern void (*__fib6_flush_trees)(struct net *net);
-static inline void rt_genid_bump_ipv6(struct net *net)
+static inline void rt_genid_bump_ipv4(struct net *net)
 {
-	if (__fib6_flush_trees)
-		__fib6_flush_trees(net);
+	atomic_inc(&net->ipv4.rt_genid);
 }
 
 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
net/ipv6/addrconf_core.c +0 -7
index 29095c90aa16..e7ee5f98327d 100644
@@ -9,13 +9,6 @@
 #include <net/addrconf.h>
 #include <net/ip.h>
 
-/* if ipv6 module registers this function is used by xfrm to force all
- * sockets to relookup their nodes - this is fairly expensive, be
- * careful
- */
-void (*__fib6_flush_trees)(struct net *);
-EXPORT_SYMBOL(__fib6_flush_trees);
-
 #define IPV6_ADDR_SCOPE_TYPE(scope)	((scope) << 16)
 
 static inline unsigned int ipv6_addr_scope2type(unsigned int scope)
net/ipv6/ip6_fib.c +8 -43

129 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#7 lore raw

RFC v2 7/7 ipv6: stamp dsts with the generation sampled before the FIB lookup

In reply to David Ahern

From: David Ahern <[email protected]>

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. Dsts allocated without a lookup result keep the stamp taken in ip6_dst_alloc().

---
Changes since v1:
- New patch.

 include/net/ip6_fib.h       |  5 +++++
 include/net/net_namespace.h | 10 ++++++++++
 net/ipv6/route.c            | 12 ++++++++++++
 3 files changed, 27 insertions(+)
include/net/ip6_fib.h +5 -0
index 92fed30a7418..a11c4a556b5d 100644
@@ -225,6 +225,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)					\
include/net/net_namespace.h +10 -0
index f39baf51bcc5..31ffac7baf73 100644
@@ -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.
  */
net/ipv6/route.c +12 -0
index 38d5f3bc85bb..deedb5fb62ca 100644
@@ -1210,6 +1210,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) {
@@ -1294,6 +1300,7 @@ INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
 	struct rt6_info *rt;
 
 	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);
@@ -2277,6 +2284,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;
 
@@ -2987,6 +2995,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 = READ_ONCE(rt6->sernum);
 
 		if (res.f6i->nh) {
 			struct fib6_nh_match_arg arg = {
@@ -3166,6 +3176,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) {
@@ -4383,6 +4394,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 = READ_ONCE(rt->sernum);
 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
 	if (!nrt)
 		goto out;
Signature · 1 line
2.43.0