All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ralf Lici <ralf@mandelbit.com>
To: netdev-bot+sashiko@kernel.org
Cc: netdev@vger.kernel.org, dsahern@kernel.org, idosch@nvidia.com,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, ncardwell@google.com,
	kuniyu@google.com, steffen.klassert@secunet.com,
	herbert@gondor.apana.org.au, fw@strlen.de, sd@queasysnail.net,
	antonio@mandelbit.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v3 2/3] ipv6: add ip6_dst_lookup_flow_cookie
Date: Thu, 19 Nov 2026 13:31:36 +0100	[thread overview]
Message-ID: <20261119123138.416106-1-ralf@mandelbit.com> (raw)
In-Reply-To: <179508017446.3136.16309442875415057261@kernel.org>

On Thu, 19 Nov 2026 09:22:54 +0000, netdev-bot+sashiko@kernel.org wrote:
> > @@ -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
>
> [ ... ]
>
> > + *	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.
>
> [Severity: Low]
> Does this guarantee hold for nexthop-object dsts (rt->sernum != 0) when
> the FIB change is a route deletion?
>
> For those dsts the helper returns gen0, and ip6_dst_check() only checks
> this:
>
> 	if (rt->sernum)
> 		return (rt6_is_valid(rt) && cookie == rt->sernum) ? dst : NULL;
>
> It never looks at rt->from.
>
> Route deletion goes through
> fib6_del()->fib6_del_route()->fib6_purge_rt()->__fib6_drop_pcpu_from().
> None of these call fib6_new_sernum(), so net->ipv6.fib6_sernum does not
> change. Deletion only clears pcpu_rt->from:
>
> 	from = unrcu_pointer(xchg(&pcpu_rt->from, NULL));
> 	fib6_info_release(from);
>
> Say the selected nexthop-object route is deleted between the gen0 and
> gen1 reads. Then gen0 == gen1, and the cookie still matches
> pcpu_rt->sernum. The cached dst keeps validating until some unrelated
> generation bump, such as any route insertion in the netns.
>
> The stale-dst behavior itself predates this patch, since callers of
> rt6_get_cookie() also get rt->sernum. What is new is this kdoc claim on
> a new exported API.
>
> Could the comment be narrowed so future callers don't assume deletions
> are covered?
>

Right, deletion does not advance the generation, and the rt->sernum
branch of ip6_dst_check never looks at rt->from. This predates the
series, but the kdoc should not promise more than the helper does. I'll
narrow it to FIB changes that advance the IPv6 generation in the next
revision.

> > +	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;
>
> [Severity: High]
> Does setting the cookie to 0 here have any effect when
> ip6_dst_lookup_flow() returned an xfrm bundle?
>

No, it doesn't. Bundles are validated by xfrm_bundle_ok against their
own route and path cookies, which are still sampled late in
xfrm_bundle_create. The cover letter lists the xfrm bundle cookies among
the cache users this series does not convert.

> The commit message says:
>
>     xfrm bundles retain their existing validation
>     through their own route and path cookies.
>
> However, those cookies are sampled late too, which is the pattern the
> commit calls buggy. Both are set inside xfrm_bundle_create(). That runs
> after ip6_dst_lookup_tail() picked the route, and after policy lookup and
> state resolution:
>
> net/xfrm/xfrm_policy.c:xfrm_init_path() {
>     ...
> 		path->path_cookie = rt6_get_cookie(dst_rt6_info(dst));
>     ...
> }
>
> net/ipv6/xfrm6_policy.c:xfrm6_fill_dst() {
>     ...
> 	xdst->route_cookie = rt6_get_cookie(rt);
>     ...
> }
>
> xfrm_bundle_ok() then validates only against those two cookies:
>
> 	if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
>     ...
> 		if (!dst_check(xdst->route, xdst->route_cookie))
> 			return 0;
>
> xfrm_dst_check() ignores the cookie the socket stored:
>
> 	if (READ_ONCE(dst->obsolete) < 0 && !stale_bundle(dst))
> 		return dst;
>
> So when a change lands while the bundle is being built, the helper sees
> gen0 != gen1, but nothing acts on that result.
>
> Here is one sequence. A socket is covered by IPsec policy P1. During
> tcp_v6_connect(), someone inserts a higher-priority policy P2 that does
> not replace P1, for example a block policy or a different tunnel:
>
> xfrm_policy_insert()
>     rt_genid_bump_ipv6(net)   -> fib6_flush_trees(), new node sernums
>     (xfrm_policy_kill() only runs for a replaced delpol, so the genid
>      of P1 is untouched)
>
> If path_cookie and route_cookie are read after the flush, they already
> hold the new fn_sernum.
>
> Would the cached P1 bundle then pass xfrm_bundle_ok() on every later
> send, and bypass P2 until some other relevant FIB change?
>
> The next patch in the series ("ipv6: use lookup cookies in selected
> socket dst cache paths") fixes this policy bypass for plain routes. That
> patch only routes tcp_v6_connect(), ip6_datagram_dst_update() and
> ip6_sk_dst_lookup_flow() through this helper, so the bundle case is still
> open at the end of the series.
>
> Could the bundle route and path cookies be sampled differently, or could
> the detected generation change be passed into the bundle?
>

Passing the cookie from this helper into the bundle would only cover
dst_orig: in tunnel mode the outer routes are looked up inside bundle
creation. The fix I have in mind applies the same bracketing within
xfrm, reading the generation before the policy lookup and storing 0 as
route and path cookie if it changed by the time they are sampled. That
also covers bundles created through xfrm_lookup outside of this helper,
so I'd rather do it as a separate change on top of this API.

-- 
Ralf Lici
Mandelbit Srl

  reply	other threads:[~2026-11-19 12:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  9:21 [PATCH net-next v3 0/3] ipv6: tie the dst cookie to the lookup that returned the dst Ralf Lici
2026-11-17  9:21 ` [PATCH net-next v3 1/3] ipv6: stamp the default route node with a fresh serial on link failure Ralf Lici
2026-11-19  9:22   ` netdev-bot+sashiko
2026-11-17  9:21 ` [PATCH net-next v3 2/3] ipv6: add ip6_dst_lookup_flow_cookie Ralf Lici
2026-11-18  8:48   ` Xia Yan
2026-11-18 14:39     ` Ralf Lici
2026-11-18 15:07       ` Xia Yan
2026-11-19  9:22   ` netdev-bot+sashiko
2026-11-19 12:31     ` Ralf Lici [this message]
2026-11-20  2:13   ` David Ahern
2026-11-20  8:16     ` Ralf Lici
2026-11-17  9:21 ` [PATCH net-next v3 3/3] ipv6: use lookup cookies in selected socket dst cache paths Ralf Lici
2026-11-19  9:22   ` netdev-bot+sashiko
2026-11-19 12:34     ` Ralf Lici

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119123138.416106-1-ralf@mandelbit.com \
    --to=ralf@mandelbit.com \
    --cc=antonio@mandelbit.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=steffen.klassert@secunet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.