🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Ralf Lici Fri, 20 Nov 2026 09:36
[PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie
#19721
Author: Ralf Lici
Date: Fri, 20 Nov 2026 09:36
179 lines
16834 bytes
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 <ralf@mandelbit.com>
Reviewed-by: Xia Yan <yanxia@cmss.chinamobile.com>
---
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
Re: [PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie
#21702
Author: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 10:26
211 lines
15519 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 2 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] ip6_dst_lookup_flow_cookie() (net/ipv6/ip6_output.c) only closes
  the lookup/cookie race when the returned dst is a plain route.
- [High] The generation check in ip6_dst_lookup_flow_cookie() is only
  sound if every writer publishes its full routing/policy change before…

--

ipv6: add ip6_dst_lookup_flow_cookie

This adds ip6_dst_lookup_flow_cookie(). Like ip6_dst_lookup_flow(), it
does the route and xfrm policy lookup, and it also returns the dst
validation cookie. The cookie is sampled between two reads of the IPv6
generation and forced to 0 if the generation changed. A comment is also
added to fib6_new_sernum() describing the writer side ordering that the
helper relies on.

> diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
> index e1d49897d6ab0..1915d31136d65 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.
> + */

[Severity: High]
Does xfrm_policy_insert() follow this ordering when it replaces an
existing policy (excl == 0)?

In xfrm_policy_insert(), the IPv6 generation is advanced before the
replaced policy is unlinked:

net/xfrm/xfrm_policy.c:xfrm_policy_insert() {
    ...
	__xfrm_policy_link(policy, dir);
    ...
	else
		rt_genid_bump_ipv6(net);

	if (delpol) {
		xfrm_policy_requeue(delpol, policy);
		__xfrm_policy_unlink(delpol, dir);
	}
    ...
}

Nothing advances the generation after the unlink. xfrm_policy_kill(delpol)
only does atomic_inc(&policy->genid), and that only affects bundles.

Suppose the new policy has a larger priority value than the old one, and
a later entry in the chain has priority <= the new priority. Then
xfrm_policy_insert_list() links the new policy behind that entry, which
puts it after the old policy. During this window, xfrm_policy_lookup_bytype()
takes the first match, so it keeps selecting the old policy.

Can this sequence happen?

CPU2 xfrm_policy_insert()        CPU1 ip6_dst_lookup_flow_cookie()
  __xfrm_policy_link(P_new)
  rt_genid_bump_ipv6()  /* G+1 */
                                   gen0 = G+1
                                   xfrm_lookup() matches P_old
                                   (allow, no templates), returns
                                   dst_orig untransformed
                                   *cookie = rt6_get_cookie(rt) = G+1
                                   gen1 = G+1, cookie is kept
  __xfrm_policy_unlink(P_old)

The socket's cached plain route would then keep passing ip6_dst_check().
Traffic would go out in plaintext even though P_new requires IPsec or
blocks the flow. This lasts until an unrelated IPv6 generation change.

The following patch, "ipv6: use lookup cookies in selected socket dst
cache paths", relies on lookup cookies to prevent this kind of plaintext
bypass. No later patch in the series changes xfrm_policy_insert().

> diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
> index 3cc911f6618e7..083ffdc8f4046 100644
> --- a/net/ipv6/ip6_output.c
> +++ b/net/ipv6/ip6_output.c

[ ... ]

> @@ -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);
>  

[ ... ]

> +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;

[Severity: High]
If ip6_dst_lookup_flow() returns an xfrm bundle, does zeroing the cookie
here change anything?

xfrm_dst_check() never reads the cookie:

net/xfrm/xfrm_policy.c:xfrm_dst_check() {
    ...
	if (READ_ONCE(dst->obsolete) < 0 && !stale_bundle(dst))
		return dst;
    ...
}

xfrm_bundle_ok() checks only dst_check(path, path_cookie) and
dst_check(xdst->route, route_cookie), plus the policy and state genids.

Those two cookies are sampled with rt6_get_cookie() after the route and
policy lookups, which is inside the gen0..gen1 window. That is the same
late sampling that the commit message describes as the bug:

net/ipv6/xfrm6_policy.c:xfrm6_fill_dst() {
    ...
	xdst->route_cookie = rt6_get_cookie(rt);
    ...
}

net/xfrm/xfrm_policy.c:xfrm_init_path() {
    ...
		path->path_cookie = rt6_get_cookie(dst_rt6_info(dst));
    ...
}

Could this interleaving occur?

1. A reader (tcp_v6_connect(), ip6_datagram_dst_update() or
   ip6_sk_dst_lookup_flow() after the following patch) reads gen0 = G,
   looks up route R, and xfrm_lookup() selects the existing policy P_old.

2. A higher precedence policy P_new with a different selector is
   inserted, so P_old is not killed and its genid does not change.
   xfrm_policy_insert() calls rt_genid_bump_ipv6() -> fib6_flush_trees(),
   which sets fib6_sernum to G+1 and stamps every node with G+1.

3. The reader's xfrm_bundle_create() then reads route_cookie and
   path_cookie as G+1.

4. Since gen1 != gen0, *cookie is set to 0, but the bundle ignores it.
   The socket caches the bundle. Later dst_check() calls pass because
   fn_sernum is G+1 and P_old->genid has not changed.

The flow would keep using P_old until some unrelated generation change.
That means the old tunnel or SA stays in use, even if P_new is a block
policy. A concurrent fib6_add() of a more specific route could pair the
old xdst->route with a new serial in the same way.

The commit message says:

    xfrm bundles retain their existing validation through their own route
    and path cookies.

That validation depends on the same racy sampling this patch is meant to
fix. The cookie value alone cannot invalidate a bundle, so does the
gen0 != gen1 case need something more? For example, a result telling the
caller not to cache the dst, or marking the bundle obsolete.

> +
> +	return dst;
> +}
> +EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow_cookie);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795166508.git.ralf%40mandelbit.com
Re: [PATCH net-next v4 4/5] ipv6: add ip6_dst_lookup_flow_cookie
#21940
Author: Ralf Lici
Date: Tue, 24 Nov 2026 16:22
33 lines
10470 bytes
On Tue, 24 Nov 2026 10:26:54 +0000, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
>
> Critical: 0 · High: 2 · Medium: 0 · Low: 0
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
> - [High] ip6_dst_lookup_flow_cookie() (net/ipv6/ip6_output.c) only closes
>   the lookup/cookie race when the returned dst is a plain route.
> - [High] The generation check in ip6_dst_lookup_flow_cookie() is only
>   sound if every writer publishes its full routing/policy change before…
>

Both points are addressed in the next revision, which drops
ip6_dst_lookup_flow_cookie and moves the validation into the dst:

- The route and path dsts of xfrm bundles are now validated against
  their own lookup stamp, so the late route_cookie and path_cookie no
  longer matter.
- The xfrm_policy_insert ordering will be fixed separately in the ipsec
  tree.

pw-bot: cr

-- 
Ralf Lici
Mandelbit Srl
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads