Persistent dst caches read the validation cookie with rt6_get_cookie after route lookup has returned. For routes without their own sernum, this reads the current fn_sernum of the associated node. A concurrent FIB change or xfrm policy insertion can therefore pair an old dst with a new serial, allowing subsequent dst_check calls to accept the stale result until another relevant change. Add ip6_dst_lookup_flow_cookie to return the dst together with its validation cookie. Sample the cookie between two reads of the IPv6 generation and return cookie 0 if the generation changed. This lets the route dst serve the current packet while preventing its subsequent reuse through cache validation. xfrm bundles retain their existing validation through their own route and path cookies. For nexthop-object dsts, return the pre-lookup generation and require the supplied cookie to match rt->sernum in ip6_dst_check. This also rejects a dst allocated with a newer serial after route selection. Existing callers passing rt6_get_cookie retain their validation behavior. The generation checks require writers to publish routing changes before advancing the serial. Move the serial updates in fib6_ifup and fib6_ifdown after rt6_multipath_rebalance, preserving their existing invalidation scope. Otherwise, a lookup can observe the new serial while selecting a nexthop using the old ECMP bounds, without detecting a generation change. This establishes the API and writer ordering needed to address late cookie sampling. The following patch converts selected socket cache users while other persistent caches are left for follow-up conversions. Signed-off-by: Ralf Lici --- Changes since v2 https://lore.kernel.org/all/e48cac0cdf9f3adcedecbbfff4a314a98bc22d01.1794384146.git.ralf@mandelbit.com/ - Move the serial update after rt6_multipath_rebalance in fib6_ifup and fib6_ifdown. (Sashiko) - Clarify that this introduces the new API while the following patch converts *some* users to it while leaving the rest as follow-up. Changes since v1 https://lore.kernel.org/netdev/e40f48e925a0950eac99a3f084e485e82cabe88f.1793806941.git.ralf@mandelbit.com/ - Fix kdoc return statement in ip6_dst_lookup_flow and ip6_dst_lookup_flow_cookie. (NIPA) include/net/ipv6.h | 13 +++++++++++ net/ipv6/ip6_output.c | 51 +++++++++++++++++++++++++++++++++++++++++-- net/ipv6/route.c | 8 +++---- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/include/net/ipv6.h b/include/net/ipv6.h index 579df1516d25..5a6c66504056 100644 --- a/include/net/ipv6.h +++ b/include/net/ipv6.h @@ -1062,6 +1062,11 @@ int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst, #if IS_ENABLED(CONFIG_IPV6) struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6, const struct in6_addr *final_dst); +struct dst_entry *ip6_dst_lookup_flow_cookie(struct net *net, + const struct sock *sk, + struct flowi6 *fl6, + const struct in6_addr *final_dst, + u32 *cookie); #else static inline struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6, @@ -1069,6 +1074,14 @@ static inline struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struc { return ERR_PTR(-EAFNOSUPPORT); } + +static inline struct dst_entry * +ip6_dst_lookup_flow_cookie(struct net *net, const struct sock *sk, + struct flowi6 *fl6, const struct in6_addr *final_dst, + u32 *cookie) +{ + return ERR_PTR(-EAFNOSUPPORT); +} #endif struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 3cc911f6618e..b87ccb5586cf 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1279,8 +1279,7 @@ EXPORT_SYMBOL_GPL(ip6_dst_lookup); * * This function performs a route lookup on the given flow. * - * It returns a valid dst pointer on success, or a pointer encoded - * error code. + * Return: a valid dst pointer on success or a pointer encoded error code. */ struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6, const struct in6_addr *final_dst) @@ -1300,6 +1299,54 @@ struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, st } EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow); +/** + * ip6_dst_lookup_flow_cookie - perform route lookup on flow with ipsec + * and return the matching dst cookie + * @net: Network namespace to perform lookup in + * @sk: socket which provides route info + * @fl6: flow to lookup + * @final_dst: final destination address for ipsec lookup + * @cookie: where to store the cookie for the returned dst + * + * Same as ip6_dst_lookup_flow, but also returns the cookie describing + * the routing state the dst was obtained from. A caller caching the dst + * should use this cookie when validating it with dst_check. If the + * IPv6 FIB changed during the lookup, a route dst no longer validates + * with it. An xfrm bundle is validated by xfrm and ignores the cookie. + * + * Return: a valid dst pointer on success or a pointer encoded error code. + */ +struct dst_entry *ip6_dst_lookup_flow_cookie(struct net *net, + const struct sock *sk, + struct flowi6 *fl6, + const struct in6_addr *final_dst, + u32 *cookie) +{ + const struct rt6_info *rt; + struct dst_entry *dst; + int gen0, gen1; + + gen0 = rt_genid_ipv6(net); + /* read the generation before looking at the FIB or xfrm policies */ + smp_rmb(); + + dst = ip6_dst_lookup_flow(net, sk, fl6, final_dst); + if (IS_ERR(dst)) + return dst; + + rt = dst_rt6_info(dst); + *cookie = rt->sernum ? gen0 : rt6_get_cookie(rt); + + /* keep the FIB and cookie reads before the final generation read */ + smp_rmb(); + gen1 = rt_genid_ipv6(net); + if (unlikely(gen0 != gen1)) + *cookie = 0; + + return dst; +} +EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow_cookie); + /** * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow * @sk: socket which provides the dst cache and route info diff --git a/net/ipv6/route.c b/net/ipv6/route.c index cc505817399b..c54844b83828 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -2817,7 +2817,7 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst, rt = dst_rt6_info(dst); if (rt->sernum) - return rt6_is_valid(rt) ? dst : NULL; + return (rt6_is_valid(rt) && cookie == rt->sernum) ? dst : NULL; rcu_read_lock(); @@ -4911,8 +4911,8 @@ static int fib6_ifup(struct fib6_info *rt, void *p_arg) if (rt != net->ipv6.fib6_null_entry && !rt->nh && rt->fib6_nh->fib_nh_dev == arg->dev) { rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; - fib6_update_sernum_upto_root(net, rt); rt6_multipath_rebalance(rt); + fib6_update_sernum_upto_root(net, rt); } return 0; @@ -5015,8 +5015,8 @@ static int fib6_ifdown(struct fib6_info *rt, void *p_arg) } rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | RTNH_F_LINKDOWN); - fib6_update_sernum(net, rt); rt6_multipath_rebalance(rt); + fib6_update_sernum(net, rt); } return -2; case NETDEV_CHANGE: @@ -5024,8 +5024,8 @@ static int fib6_ifdown(struct fib6_info *rt, void *p_arg) rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) break; rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; - fib6_update_sernum(net, rt); rt6_multipath_rebalance(rt); + fib6_update_sernum(net, rt); break; } -- 2.56.0