From: Jamal Hadi Salim <jhs@mojatatu.com>
To: netdev@vger.kernel.org
Cc: Jamal Hadi Salim <jhs@mojatatu.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
bpf@vger.kernel.org, Jiri Pirko <jiri@resnulli.us>,
Victor Nogueira <victor@mojatatu.com>
Subject: [PATCH net-next v3 2/4] net: dst: keep the LWT metadata of a dropped route
Date: Sun, 15 Nov 2026 05:19:09 -0500 [thread overview]
Message-ID: <CORE-9NOG.v3.r0.20261115050625-2@mojatatu.com> (raw)
In-Reply-To: <CORE-9NOG.v3.r0.20261115050625@mojatatu.com>
Found while working on the two producers that steer a packet to another
device's neighbour, fixed later in this series. Both release the route
dst on the way, and a route installed with an encapsulation carries the
parameters a device downstream needs: an ip or ip6 encapsulation holds an
ip_tunnel_info, where skb_tunnel_info() reads it, and an xfrm encapsulation
holds an xfrm_md_info, where skb_xfrm_md_info() reads it. The release
discards them, so a tunnel or an xfrm interface downstream has nothing left
to build the outer header with, or to select its if_id.
The Fix:
Add skb_dst_drop_keep_metadata(), which moves those parameters onto a
metadata dst of its own before the route is released. The ip and ip6
payload is copied field by field; the route's dst cache belongs to its
lwtstate and is shared by every packet that uses the route, so it is not
duplicated: the copy starts with an empty cache, which costs the tunnel one
route lookup. The xfrm payload keeps the route itself, because the xfrm
lookup reads its family; the counted reference the skb already holds moves
into the metadata dst, which releases it once it has been consumed.
The helper owns the whole operation: a dst that is not a route is not
dropped, because a metadata dst is the encapsulation itself. Its callers
must not transmit with the stale route, so a failed copy is reported and
the skb is left untouched for its owner to drop.
Conditions to recreate the bug:
Install a route with an encapsulation (ip route add ... encap ip id <id>
dst <remote>, or encap xfrm if_id <id>), steer a packet to another
device's neighbour with a netdev egress fwd rule or
bpf_redirect_neigh(), and release the route dst before neigh_xmit() hands
the packet on. A tunnel or xfrm device in the path then finds no
parameters in skb_tunnel_info() or skb_xfrm_md_info() and drops the packet
instead of using them.
Reviewed-by: Victor Nogueira <victor@mojatatu.com>
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
v2 -> v3:
- Document the `Return:` value of skb_dst_drop_keep_metadata() in its
kernel-doc comment. Caught by NIPA CI.
include/net/dst_metadata.h | 1 +
net/core/dst.c | 76 ++++++++++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)
diff --git a/include/net/dst_metadata.h b/include/net/dst_metadata.h
index 02fffbb85099..a7808d42f779 100644
--- a/include/net/dst_metadata.h
+++ b/include/net/dst_metadata.h
@@ -138,6 +138,7 @@ struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
struct metadata_dst __percpu *
metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
+bool skb_dst_drop_keep_metadata(struct sk_buff *skb);
static inline struct metadata_dst *tun_rx_dst(int md_size)
{
diff --git a/net/core/dst.c b/net/core/dst.c
index 092861133023..9c80c225cbfd 100644
--- a/net/core/dst.c
+++ b/net/core/dst.c
@@ -353,3 +353,79 @@ void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
free_percpu(md_dst);
}
EXPORT_SYMBOL_GPL(metadata_dst_free_percpu);
+
+/**
+ * skb_dst_drop_keep_metadata - release a route dst, keeping its LWT metadata
+ * @skb: buffer whose dst is to be released
+ *
+ * A route installed with an encapsulation carries the parameters a downstream
+ * device needs in dst->lwtstate, where skb_tunnel_info() and skb_xfrm_md_info()
+ * find them. Releasing such a route would discard them, so move them onto a
+ * metadata dst of their own first. The route is not duplicated: the copy
+ * starts with an empty cache, and for xfrm the route is kept because the
+ * lookup needs its family.
+ *
+ * A dst that is metadata is left in place.
+ *
+ * Return: true if the skb is left free to be sent, either because its dst is
+ * metadata already or because the LWT metadata was moved onto a new dst;
+ * false if no copy could be allocated, in which case the caller must drop the
+ * skb.
+ */
+bool skb_dst_drop_keep_metadata(struct sk_buff *skb)
+{
+ struct dst_entry *old = skb_dst(skb);
+ struct metadata_dst *md_dst = NULL;
+
+ if (!skb_valid_dst(skb))
+ return true;
+
+ if (old->lwtstate && old->lwtstate->type == LWTUNNEL_ENCAP_XFRM) {
+ struct xfrm_md_info *src = lwt_xfrm_info(old->lwtstate);
+
+ md_dst = metadata_dst_alloc(0, METADATA_XFRM, GFP_ATOMIC);
+ if (!md_dst)
+ return false;
+
+ /* Count the route so it outlives the skb, then move the
+ * reference into the metadata dst.
+ */
+ if (!skb_dst_force(skb)) {
+ metadata_dst_free(md_dst);
+ return false;
+ }
+
+ md_dst->u.xfrm_info.if_id = src->if_id;
+ md_dst->u.xfrm_info.link = src->link;
+ md_dst->u.xfrm_info.dst_orig = old;
+ skb_dstref_steal(skb);
+ skb_dst_set(skb, &md_dst->dst);
+ return true;
+ }
+
+ if (old->lwtstate &&
+ (old->lwtstate->type == LWTUNNEL_ENCAP_IP ||
+ old->lwtstate->type == LWTUNNEL_ENCAP_IP6)) {
+ struct ip_tunnel_info *src = lwt_tun_info(old->lwtstate);
+ struct ip_tunnel_info *dst;
+
+ md_dst = metadata_dst_alloc(src->options_len,
+ METADATA_IP_TUNNEL, GFP_ATOMIC);
+ if (!md_dst)
+ return false;
+
+ dst = &md_dst->u.tun_info;
+ dst->key = src->key;
+ dst->encap = src->encap;
+ dst->options_len = src->options_len;
+ dst->mode = src->mode;
+ memcpy(ip_tunnel_info_opts(dst), ip_tunnel_info_opts(src),
+ src->options_len);
+ }
+
+ skb_dst_drop(skb);
+ if (md_dst)
+ skb_dst_set(skb, &md_dst->dst);
+ return true;
+}
+EXPORT_SYMBOL_GPL(skb_dst_drop_keep_metadata);
--
2.43.0
next prev parent reply other threads:[~2026-11-15 10:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-15 10:19 [PATCH net-next v3 0/4] net: drop the stale dst when steering to another device Jamal Hadi Salim
2026-11-15 10:19 ` [PATCH net-next v3 1/4] net: xfrm: interface: restore dst_orig on every xfrm interface Jamal Hadi Salim
2026-11-16 10:48 ` netdev-bot+sashiko
2026-11-15 10:19 ` Jamal Hadi Salim [this message]
2026-11-16 10:48 ` [PATCH net-next v3 2/4] net: dst: keep the LWT metadata of a dropped route netdev-bot+sashiko
2026-11-15 10:19 ` [PATCH net-next v3 3/4] netfilter: nft_fwd_netdev: drop the stale dst when steering to a device Jamal Hadi Salim
2026-11-16 10:48 ` netdev-bot+sashiko
2026-11-15 10:19 ` [PATCH net-next v3 4/4] bpf: drop the stale dst when redirecting to an explicit nexthop Jamal Hadi Salim
2026-11-16 10:48 ` netdev-bot+sashiko
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=CORE-9NOG.v3.r0.20261115050625-2@mojatatu.com \
--to=jhs@mojatatu.com \
--cc=bpf@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=jiri@resnulli.us \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=victor@mojatatu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox