An IPv4 route records the rt_genid of the netns it was created in, and rt_is_expired() compares it with the rt_genid of the netns its device belongs to when the route is checked. Every netns starts its rt_genid at 0 and increments it on each cache flush, so different netns often hold the same value. When a device moves to another netns, routes cached through it remain usable until dst_dev_put() points them to blackhole_netdev. For a route using a nexthop object this happens in nexthop_free_rcu(), which free_fib_info_rcu() queues from inside an RCU callback, so the rcu_barrier() in __dev_change_net_namespace() does not wait for it. If the rt_genid of the new netns matches the cached one in that window, ipv4_dst_check() accepts the route and a socket in the old netns keeps sending through a device that now belongs to another netns. Take every rt_genid bump from one sequence shared by all netns, which never hands out 0, the value every netns starts with. Inserting a route bumps rt_genid, so the routes a netns caches carry values drawn from the sequence, which no other netns holds until it wraps after 2^32 bumps. Concurrent bumps of one netns can leave the older of two values, so rt_genid is no longer monotonic per netns. It is only compared for equality, so this costs at most an extra route lookup. A connected UDP socket in netns A caches a route through a nexthop object on a veth. The veth is moved to netns B and brought up with one RTM_NEWLINK, then the socket sends 200000 datagrams. B's rt_genid is bumped 0 to 20 times before the move, 63 trials: before after trials sending after move 3 0 datagrams sent after move 3132 0 Fixes: 4c7e8084fd46 ("ipv4: Plumb support for nexthop object in a fib_info") Signed-off-by: Qiliang Yuan Reviewed-by: Kuniyuki Iwashima --- include/net/net_namespace.h | 5 +---- net/ipv4/route.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index c29a22ca178fa..140858a2fe6d4 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -557,10 +557,7 @@ static inline int rt_genid_ipv6(const struct net *net) } #endif -static inline void rt_genid_bump_ipv4(struct net *net) -{ - atomic_inc(&net->ipv4.rt_genid); -} +void rt_genid_bump_ipv4(struct net *net); extern void (*__fib6_flush_trees)(struct net *net); static inline void rt_genid_bump_ipv6(struct net *net) diff --git a/net/ipv4/route.c b/net/ipv4/route.c index d0a5f6d77094c..16d470ab50142 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -393,6 +393,27 @@ static inline int ip_rt_proc_init(void) } #endif /* CONFIG_PROC_FS */ +/* + * Draw rt_genid bumps from one sequence shared by all netns. A route keeps + * the genid of the netns it was created in, and is checked against the netns + * its device is in now, so distinct values make that check fail once the + * device has moved. Every netns starts at 0, which the sequence never hands + * out, and inserting a route bumps the genid, so cached routes carry values + * drawn from the sequence. + */ +static atomic_t rt_genid_seq; + +void rt_genid_bump_ipv4(struct net *net) +{ + int genid; + + do { + genid = atomic_inc_return(&rt_genid_seq); + } while (!genid); + atomic_set(&net->ipv4.rt_genid, genid); +} +EXPORT_SYMBOL_GPL(rt_genid_bump_ipv4); + static inline bool rt_is_expired(const struct rtable *rth) { bool res; -- 2.43.0