Subsystems (3)
David Ahern
maintainerDavid Ahern
maintainercover ipv6: validate dsts against the netns route generation
From: David Ahern <[email protected]>
An IPv6 dst has never carried the fib6 serial number it was created under. rt6_get_cookie() reads fn->fn_sernum through rt->from when a socket, dst_cache or xfrm bundle stores its cookie, so a FIB change that lands between the route lookup and the cookie capture is folded into the cookie and a stale dst looks valid. The only dsts stamped at creation are pcpu dsts of nexthop-object routes, which store the netns genid in rt->sernum.
Looking at this I found that rt_genid_ipv6() and the fib6 serial number are already the same counter: net->ipv6.fib6_sernum, which fib6_new_sernum() advances on every FIB change. fn_sernum is a per-node copy of it. So this series moves dst validation onto that counter, the way IPv4 uses rt_genid, instead of adding another one:
1. Stamp every dst with the counter in ip6_dst_alloc() and validate by comparison in ip6_dst_check(). Long-lived dsts (pcpu dsts of routes with an embedded fib6_nh, and exception dsts) are re-stamped when a lookup finds them again, otherwise they would stay invalid.
2. ip6_link_failure() forced a new lookup by writing -1 to fn_sernum. That no longer invalidates anything, so clear the failing dst's sernum instead.
3-4. Nexthop objects: factor out nh_has_f6i(), then bump the counter
when a nexthop used through a group is replaced, and when a member is removed from a group.
5. A new PMTU or redirect exception used to bump the counter, which with (1) would invalidate every dst in the namespace. Do what IPv4 does in update_or_create_fnhe() and invalidate only the pcpu dsts of the affected fib6_nh. Route dumps still need to see that an exception was added to the root node, so add a per-table sequence for that rather than inventing fn_sernum values the counter could later issue again.
6. Since nothing validates against fn_sernum any more, rt_genid_bump_ipv6() no longer needs to walk every node of every table. Make it an atomic increment like rt_genid_bump_ipv4(), and drop the __fib6_flush_trees pointer. fn_sernum remains only for route dump consistency, so nexthop, address and rule changes no longer restart in-progress dumps.
David Ahern (6):
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
include/net/ip6_fib.h | 45 +++------------- include/net/net_namespace.h | 26 ++++++--- net/ipv4/nexthop.c | 42 ++++++++++++--- net/ipv6/addrconf_core.c | 7 --- net/ipv6/ip6_fib.c | 76 +++++++++++--------------- net/ipv6/route.c | 104 ++++++++++++++++++++++++------------ 6 files changed, 162 insertions(+), 138 deletions(-)
Signature · 1 line
2.43.0
David Ahern
maintainerRFC 1/6 ipv6: validate dsts against the netns route generation
In reply to David AhernFrom: 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.
Because validity now lives in the dst, long-lived dsts must be re-stamped when a lookup finds them again, or they would stay invalid forever:
- pcpu dsts of routes with an embedded fib6_nh are re-stamped in rt6_get_pcpu_route(); nexthop-object pcpu dsts are still dropped and rebuilt.
- exception dsts are re-stamped 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.
--- include/net/ip6_fib.h | 42 +++------------------------ net/ipv6/ip6_fib.c | 2 -- net/ipv6/route.c | 66 +++++++++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 67 deletions(-)
include/net/ip6_fib.h +4 -38
@@ -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
@@ -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 +39 -27
131 lines not on this page · raw message
Signature · 1 line
2.43.0
David Ahern
maintainerRFC 2/6 ipv6: invalidate the failing dst in ip6_link_failure()
In reply to David AhernFrom: 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.
--- net/ipv6/route.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
net/ipv6/route.c +5 -6
@@ -2885,14 +2885,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
David Ahern
maintainerRFC 3/6 nexthop: factor out nh_has_f6i()
In reply to David AhernFrom: 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.
--- net/ipv4/nexthop.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
net/ipv4/nexthop.c +12 -5
@@ -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
David Ahern
maintainerRFC 4/6 nexthop: bump the IPv6 route generation on group changes
In reply to David AhernFrom: 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.
--- net/ipv4/nexthop.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
net/ipv4/nexthop.c +25 -2
@@ -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
David Ahern
maintainerRFC 5/6 ipv6: scope exception invalidation to the affected nexthop
In reply to David AhernFrom: 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.
--- 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
@@ -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
@@ -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
@@ -1732,6 +1732,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) {
@@ -1806,10 +1828,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
David Ahern
maintainerRFC 6/6 ipv6: make rt_genid_bump_ipv6() a plain counter increment
In reply to David AhernFrom: 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.
--- 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
@@ -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
@@ -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
David Ahern
maintainerRe: [RFC net-next 0/6] ipv6: validate dsts against the netns route generation
In reply to David AhernQuoted 37 lines · David Ahern
From: David Ahern <[email protected]>
An IPv6 dst has never carried the fib6 serial number it was created under.
rt6_get_cookie()readsfn->fn_sernumthroughrt->fromwhen a socket,dst_cacheor xfrm bundle stores its cookie, so a FIB change that lands between the route lookup and the cookie capture is folded into the cookie and a stale dst looks valid. The only dsts stamped at creation are pcpu dsts of nexthop-object routes, which store the netns genid inrt->sernum.Looking at this I found that
rt_genid_ipv6()and the fib6 serial number are already the same counter:net->ipv6.fib6_sernum, whichfib6_new_sernum()advances on every FIB change.fn_sernumis a per-node copy of it. So this series moves dst validation onto that counter, the way IPv4 usesrt_genid, instead of adding another one:1. Stamp every dst with the counter in
ip6_dst_alloc()and validate by comparison inip6_dst_check(). Long-lived dsts (pcpu dsts of routes with an embeddedfib6_nh, and exception dsts) are re-stamped when a lookup finds them again, otherwise they would stay invalid.2.
ip6_link_failure()forced a new lookup by writing -1 tofn_sernum. That no longer invalidates anything, so clear the failing dst's sernum instead.3-4. Nexthop objects: factor out
nh_has_f6i(), then bump the counter
when a nexthop used through a group is replaced, and when a member is removed from a group.5. A new PMTU or redirect exception used to bump the counter, which with (1) would invalidate every dst in the namespace. Do what IPv4 does in
update_or_create_fnhe()and invalidate only the pcpu dsts of the affectedfib6_nh. Route dumps still need to see that an exception was added to the root node, so add a per-table sequence for that rather than inventingfn_sernumvalues the counter could later issue again.
Quoted 7 lines
6. Since nothing validates against
fn_sernumany more,rt_genid_bump_ipv6()no longer needs to walk every node of every table. Make it an atomic increment likert_genid_bump_ipv4(), and drop the__fib6_flush_treespointer.fn_sernumremains only for route dump consistency, so nexthop, address and rule changes no longer restart in-progress dumps.
Ralf: This aligns with the direction I meant in my reply on October 13.
Re: [RFC net-next 0/6] ipv6: validate dsts against the netns route generation
In reply to David AhernHi David,
I had a look at the series and tested it. I see two problems with the approach.
Quoted 8 lines · David Ahern
copy of it. So this series moves dst validation onto that counter, the way IPv4 uses
rt_genid, instead of adding another one:1. Stamp every dst with the counter in
ip6_dst_alloc()and validate by comparison inip6_dst_check(). Long-lived dsts (pcpu dsts of routes with an embeddedfib6_nh, and exception dsts) are re-stamped when a lookup finds them again, otherwise they would stay invalid.
pcpu and exception dsts are shared by every socket and dst_cache that holds them, and with this series validity lives in the shared dst instead of in each holder's cookie. When one lookup re-stamps the dst, the stale dst becomes valid again for every holder. IPv4 does not
re-stamp: a stale cached route fails rt_cache_valid and __mkroute_output allocates a new one, so routes already held by sockets keep their old genid.
The script below shows this. A connected UDP socket on CPU0 caches its route to fd01::2 through fd01::/64, then a blackhole for fd01::2/128 is added. If an unrelated socket on CPU0 then connects to fd01::3 through the same /64, the first socket keeps using the old route: all 10 datagrams it sends after the blackhole reach the wire, in 3 out of 3 runs. Without the unrelated socket they are dropped, and current net-next and my series [1] drop them in both cases. With an xfrm policy for the socket's port instead of the blackhole, the same sequence makes the socket keep sending its 10 datagrams in plaintext, where net-next and [1] encrypt them. This is not a narrow race but the deterministic outcome with this series.
The stamp is also read after route selection, in ip6_dst_alloc and in rt6_refresh_sernum. A FIB change that lands in between, e.g. the same blackhole added while a lookup for fd01::2 has already selected the /64, yields a dst selected from the old state that carries the new generation. That is the window ip6_dst_lookup_flow_cookie in [1] closes by reading the generation before the lookup.
For generation-based validation to hold, the value has to live with the holder rather than in the shared dst, and it has to be read before route selection. dst_check already takes a per-holder cookie for this: with the generation read before the lookup as the cookie, compared with the current one, dsts need no stamp or re-stamping and pcpu dsts stay reusable across changes. That is what ip6_dst_lookup_flow_cookie returns, with fn_sernum in place of the netns generation, so the two approaches differ in invalidation granularity (per node vs netns-wide) rather than in where the snapshot lives. I'd rather keep the per-node granularity, but either one needs the holders converted to a cookie read before the lookup.
[1] https://lore.kernel.org/netdev/[email protected]/
Reproducer, run as root in a VM, "restamp.sh route|xfrm ctrl|other":
#!/bin/bash
# usage: restamp.sh route|xfrm ctrl|other
#
# A connected UDP socket on CPU0 caches its route to fd01::2, then that
# destination gets a blackhole (route) or an xfrm policy (xfrm). With
# "other", an unrelated socket on CPU0 then connects through the same /64.
# Counts what the first socket sends to fd01::2 after the change.
case=$1 mode=$2
log=/tmp/restamp-$case-$mode.txt
on_cpu0() { taskset -c 0 ip netns exec cl "$@"; }ip netns del cl 2>/dev/null
ip netns add cl
ip -n cl link add c0 type veth peer name p0
for dev in c0 p0; do
# no link-local address: its DAD would add a route and bump the generation ip -n cl link set $dev addrgenmode none ip -n cl link set $dev up done ip -n cl addr add fd00::1/64 dev c0 nodad ip -n cl neigh add fd00::2 lladdr 02:00:00:00:00:02 dev c0 nud permanent ip -n cl route add fd01::/64 via fd00::2 dev c0 ip -n cl xfrm state add src fd00::1 dst fd01::2 proto esp spi 0x1001 reqid 1 \ mode transport aead 'rfc4106(gcm(aes))' \ 0x0102030405060708090a0b0c0d0e0f1011121314 128
on_cpu0 bash -c 'exec 3>/dev/udp/fd01::2/5002; echo before >&3; sleep 3
for i in {1..10}; do echo after >&3; sleep 0.1; done' 2>/dev/null &sock=$!
sleep 0.5
if [ $case = route ]; then
ip -n cl route add blackhole fd01::2/128
else
ip -n cl xfrm policy add src fd00::1 dst fd01::2 proto udp dport 5002 \ dir out tmpl src fd00::1 dst fd01::2 proto esp reqid 1 mode transport
fi
ip netns exec cl tcpdump -nn -l -i c0 dst fd01::2 >$log 2>/dev/null & cap=$!
if [ $mode = other ]; then
sleep 0.2 on_cpu0 bash -c 'echo x >/dev/udp/fd01::3/9'
fi
wait $sock
sleep 0.5; kill $cap; wait $cap
if [ $case = route ]; then
echo "route $mode: $(grep -c UDP $log) datagrams sent past the blackhole" else echo "xfrm $mode: $(grep -c UDP $log) plaintext, $(grep -c ESP $log) ESP"
fi
ip netns del cl
Signature · 2 lines
Ralf Lici Mandelbit Srl
Re: [RFC net-next 0/6] ipv6: validate dsts against the netns route generation
In reply to David AhernQuoted 2 lines · David Ahern
Ralf: This aligns with the direction I meant in my reply on October 13.
I see, but the concerns from my October 20 reply [1] still hold. There I noted that a stamp stored in the dst would have to stay immutable, and that reusable per-CPU and exception dsts would have to be replaced when the generation changes, rather than updated. That is the problem I describe in my reply to the cover letter.
For anyone following this, the analysis of the race and the series addressing it, now at v4, are in [2] and [3]. I would have welcomed an answer to my October 20 question before a second series for the same problem, but now that both are on the list, how would you like to proceed?
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
[3] https://lore.kernel.org/netdev/[email protected]/
Signature · 2 lines
Ralf Lici Mandelbit Srl
David Ahern
maintainerRe: [RFC net-next 0/6] ipv6: validate dsts against the netns route generation
Quoted 10 lines · Ralf Lici
Quoted 2 lines · David Ahern
Ralf: This aligns with the direction I meant in my reply on October 13.
I see, but the concerns from my October 20 reply [1] still hold. There I noted that a stamp stored in the dst would have to stay immutable, and that reusable per-CPU and exception dsts would have to be replaced when the generation changes, rather than updated. That is the problem I describe in my reply to the cover letter.
Quoted 5 lines
For anyone following this, the analysis of the race and the series addressing it, now at v4, are in [2] and [3]. I would have welcomed an answer to my October 20 question before a second series for the same problem, but now that both are on the list, how would you like to proceed?
My goal here is to better align ipv6 dst handling with ipv4 and not grow apis for the validation in some parts of the stack or expose internal details outside of the routing layer.
That said, I have very limited time to work on a proposal. If this set cannot converge on an acceptable solution in a few days, then I will have to move on.