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. Like ip6_dst_lookup_flow, it performs the route lookup followed by the xfrm policy lookup. Sample the cookie between two reads of the IPv6 generation and return cookie 0 if the generation changed. Both FIB changes and xfrm policy insertion advance the generation. This lets the route dst serve the current packet while preventing its subsequent reuse through cache validation. For dsts of routes using nexthop objects, return the generation read before the lookup, which ip6_dst_check compares with rt->sernum. xfrm bundles retain their existing validation through their own route and path cookies. The generation checks rely on writers publishing routing changes before advancing the generation, and advancing it before stamping the nodes. Both follow from the fully ordered atomic_try_cmpxchg in fib6_new_sernum. Document this there and name it as the pairing side of the read barriers in the helper. The following patch converts selected socket cache users, while other persistent caches are left for follow-up conversions. Signed-off-by: Ralf Lici Reviewed-by: Xia Yan --- Changes since v3 https://lore.kernel.org/netdev/7af19082325ce83699c5fd82de4925e52ed7dff2.1794906743.git.ralf@mandelbit.com/ - Move the writer ordering and the ip6_dst_check change to separate patches. (David) - Say in the kdoc that the helper performs the xfrm policy lookup. (David) - Narrow the kdoc to changes that advance the IPv6 generation. (Sashiko) - Document the full ordering of fib6_new_sernum and name it as the pairing side of the read barriers. (Xia) 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_fib.c | 4 +++ net/ipv6/ip6_output.c | 57 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/include/net/ipv6.h b/include/net/ipv6.h index b77dc5c3ea26..ddeca746c991 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_fib.c b/net/ipv6/ip6_fib.c index e1d49897d6ab..1915d31136d6 100644 --- a/net/ipv6/ip6_fib.c +++ b/net/ipv6/ip6_fib.c @@ -123,6 +123,10 @@ static void fib6_walkers_skip_rt(struct net *net, struct fib6_info *rt) fib6_walkers_replace_leaf(net, rt, next); } +/* The successful atomic_try_cmpxchg is fully ordered: it publishes the + * routing change before the new generation, and the new generation before + * the nodes stamped with it. ip6_dst_lookup_flow_cookie relies on both. + */ static int fib6_new_sernum(struct net *net) { int new, old = atomic_read(&net->ipv6.fib6_sernum); diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 3cc911f6618e..083ffdc8f404 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,60 @@ 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 and xfrm policy lookup on + * flow 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 the xfrm policy 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 a FIB + * change or an xfrm policy insertion advanced the IPv6 generation during + * the lookup, a route dst no longer validates with it. Route deletion + * does not advance the generation. 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, + * pairs with the full barrier in fib6_new_sernum + */ + 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, + * pairs with the full barrier in fib6_new_sernum + */ + 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 -- 2.56.0