Netdev List
 help / color / mirror / Atom feed
* [PATCH net v3] net: xdp: fix generic XDP on skbs without an Ethernet header
@ 2026-11-24 14:25 Jiayuan Chen
  2026-11-25 14:27 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayuan Chen @ 2026-11-24 14:25 UTC (permalink / raw)
  To: netdev
  Cc: Jiayuan Chen, Kenneth Lee, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, Alexei Starovoitov,
	Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev, Kuniyuki Iwashima, Wang Zhan, Martin Karsten,
	bpf

Kenneth Lee reported a bug [1]:

  skb_vlan_push got skb with skb->data not at mac header (offset 14)
  WARNING: net/core/skbuff.c:6474 at skb_vlan_push+0x5ec/0x8a0
   ____bpf_skb_vlan_push net/core/filter.c:3239 [inline]
   bpf_skb_vlan_push+0x216/0x8b0 net/core/filter.c:3229
   bpf_prog_run_data_pointers+0x17c/0x240 include/linux/filter.h:917
   tcf_bpf_act+0x31c/0x940 net/sched/act_bpf.c:50
   tcf_action_exec+0x2c1/0x720 net/sched/act_api.c:1147
   tcf_classify+0x6e4/0x1080 net/sched/cls_api.c:1860
   tc_run+0x31c/0x5d0 net/core/dev.c:4411
   sch_handle_ingress net/core/dev.c:4486 [inline]
   __netif_receive_skb_core+0x141b/0x2ec0 net/core/dev.c:6054

bpf_prog_run_generic_xdp() reads xdp->data as an Ethernet header. On an
L3 device like sit0 there is none, so the "did the program change the
header" check compares garbage, and the fixup:

	__skb_push(skb, ETH_HLEN);
	skb->pkt_type = PACKET_HOST;
	skb->protocol = eth_type_trans(skb, skb->dev);

leaves mac_len == 0 with skb_mac_header(skb) == skb->data - ETH_HLEN.
tc ingress then can't get skb->data back onto the mac header.

XDP is Ethernet only, so refuse to attach a generic XDP program to a
device that is not ARPHRD_ETHER.

The one exception is lo. Its type is ARPHRD_LOOPBACK, but it uses
eth_header_ops and eth_type_trans() like any Ethernet device, so its
skbs do carry an Ethernet header. Generic XDP on lo works today and the
BPF selftests rely on it (xdp_attach, xdp_devmap_attach, check_mtu,
...), so keep allowing it.

Rejecting the attach is not enough on its own, dev->type can change
after the program is attached. An empty bond or team takes the type of
its first port, and TUNSETLINK lets a tun device change its type.
dev->hard_header_len is no help either, it is what the driver wants
reserved on TX and many Ethernet drivers make it larger than ETH_HLEN.
So also skip the program for skbs that have no ETH_HLEN sized mac
header.

[1]: https://lore.kernel.org/bpf/20260812043643.808295-1-kennethbwlee@snu.ac.kr/

Fixes: 297249569932 ("net: fix generic XDP to handle if eth header was mangled")
Reported-by: Kenneth Lee <kennethbwlee@snu.ac.kr>
Closes: https://lore.kernel.org/bpf/20260812043643.808295-1-kennethbwlee@snu.ac.kr/
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
v2 -> v3:
 - Only handle skbs with an ETH_HLEN mac header in generic XDP. This
   also skips VLAN devices with reorder_hdr off.

v1 -> v2:
 - Generic XDP does not support L3 devices, as discussed on v1. Reject
   the attach and skip skbs without an Ethernet header, instead of
   running the program on them.
 - Target net instead of net-next.

v2: https://lore.kernel.org/netdev/20261123121540.275576-1-jiayuan.chen@linux.dev/
v1: https://lore.kernel.org/netdev/20260813113229.381136-1-jiayuan.chen@linux.dev/
---
 net/core/dev.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/net/core/dev.c b/net/core/dev.c
index 1a63f6b939f7..4a4e030d93b6 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -5844,6 +5844,10 @@ static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
 	if (skb_is_redirected(skb))
 		return XDP_PASS;
 
+	/* XDP is Ethernet only, skip skbs without an Ethernet header. */
+	if (skb->data - skb_mac_header(skb) != ETH_HLEN)
+		return XDP_PASS;
+
 	/* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM
 	 * bytes. This is the guarantee that also native XDP provides,
 	 * thus we need to do it here as well.
@@ -10869,6 +10873,12 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 			NL_SET_ERR_MSG(extack, "XDP program already attached");
 			return -EBUSY;
 		}
+		/* lo is ARPHRD_LOOPBACK but uses Ethernet framing. */
+		if (mode == XDP_MODE_SKB && dev->type != ARPHRD_ETHER &&
+		    dev->type != ARPHRD_LOOPBACK) {
+			NL_SET_ERR_MSG(extack, "Generic XDP only supports Ethernet devices");
+			return -EOPNOTSUPP;
+		}
 	}
 
 	/* don't call drivers if the effective program didn't change */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net v3] net: xdp: fix generic XDP on skbs without an Ethernet header
  2026-11-24 14:25 [PATCH net v3] net: xdp: fix generic XDP on skbs without an Ethernet header Jiayuan Chen
@ 2026-11-25 14:27 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25 14:27 UTC (permalink / raw)
  To: jiayuan.chen
  Cc: netdev, kennethbwlee, davem, edumazet, kuba, pabeni, horms, ast,
	daniel, hawk, john.fastabend, sdf, kuniyu, wang.zhan, mkarsten,
	bpf

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

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

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).

- [Medium] dev_xdp_attach() (net/core/dev.c) now rejects every
  XDP_MODE_SKB attach with -EOPNOTSUPP when dev->type is neither…
- [Medium] The new early return in netif_receive_generic_xdp() (`if
  (skb->data - skb_mac_header(skb) != ETH_HLEN) return XDP_PASS;`) skips…
- [Low] Documentation/networking/af_xdp.rst (around line 86) says XDP_SKB
  mode, which is used when the driver has no XDP support or XDP_SKB is…

--

net: xdp: fix generic XDP on skbs without an Ethernet header

The patch refuses to attach a generic XDP program to a device that is
neither ARPHRD_ETHER nor ARPHRD_LOOPBACK. It also skips the generic XDP
program at runtime for any skb whose mac header is not exactly ETH_HLEN
bytes. The goal is to fix a WARN in skb_vlan_push() that comes from the
Ethernet header fixup in bpf_prog_run_generic_xdp() on L3 devices such as
sit0.

Closes: https://lore.kernel.org/bpf/20260812043643.808295-1-kennethbwlee@snu.ac.kr/

> diff --git a/net/core/dev.c b/net/core/dev.c
> index cd0796a1dbfdf..a9ef193967bab 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -5825,6 +5825,10 @@ static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
>  	if (skb_is_redirected(skb))
>  		return XDP_PASS;
>  
> +	/* XDP is Ethernet only, skip skbs without an Ethernet header. */
> +	if (skb->data - skb_mac_header(skb) != ETH_HLEN)
> +		return XDP_PASS;

[Severity: Medium]
Can this silently skip a program that was attached successfully to an
ARPHRD_ETHER device?

Take a VLAN device with reorder_hdr off. Its type is ARPHRD_ETHER and it
has no ndo_bpf, so dev_xdp_attach() defaults to XDP_MODE_SKB and accepts
the program. On receive, vlan_do_receive() does this:

net/8021q/vlan_core.c:vlan_do_receive() {
    ...
	if (!(READ_ONCE(vlan_dev_priv(vlan_dev)->flags) & VLAN_FLAG_REORDER_HDR) &&
	    ...
		skb_push(skb, offset);
		skb = *skbp = vlan_insert_inner_tag(skb, skb->vlan_proto,
						    skb->vlan_tci, skb->mac_len);
		...
		skb_pull(skb, offset + VLAN_HLEN);
		skb_reset_mac_len(skb);
	}
    ...
}

That leaves skb->data - skb_mac_header(skb) == 18. Then
__netif_receive_skb_core() jumps to another_round and calls
do_xdp_generic() with the VLAN device's program. The new check returns
XDP_PASS for every packet.

Before this patch the program ran on the full Ethernet+VLAN frame, so
read-only filters and XDP_DROP filters worked. reorder_hdr can be toggled
at runtime, so a filter that is already attached could stop being
enforced. Nothing reports this: no warning, no extack and no tracepoint.

The commit message justifies the runtime check only by dev->type changing
after attach. The VLAN effect appears only in the v2->v3 note below the
--- line, and that note won't be kept when the patch is applied.

Would it make sense to warn once here, or to reject the attach, and to
describe this in the commit message?

A separate point: bpf_xdp_link_update() calls dev_xdp_install() directly:

net/core/dev.c:bpf_xdp_link_update() {
    ...
	mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
	bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
	err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
			      xdp_link->flags, new_prog);
    ...
}

This path never reaches the new dev->type check in dev_xdp_attach(). The
device type can change after the link is created, for example through
bond/team port enslavement or TUNSETLINK. In that case the link update
still succeeds, and the new program never runs either.

Is this inconsistency intended?

[ ... ]

> @@ -10775,6 +10779,12 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
>  			NL_SET_ERR_MSG(extack, "XDP program already attached");
>  			return -EBUSY;
>  		}
> +		/* lo is ARPHRD_LOOPBACK but uses Ethernet framing. */
> +		if (mode == XDP_MODE_SKB && dev->type != ARPHRD_ETHER &&
> +		    dev->type != ARPHRD_LOOPBACK) {
> +			NL_SET_ERR_MSG(extack, "Generic XDP only supports Ethernet devices");
> +			return -EOPNOTSUPP;
> +		}

[Severity: Medium]
Does this break existing generic XDP users on L3 devices?

Today an XDP_MODE_SKB attach succeeds on tun in IFF_TUN mode, wireguard
(ARPHRD_NONE), sit/ipip/gre/ip6tnl, PPP, RAWIP WWAN and IPoIB.

Some programs never modify the first 14 bytes, for example read-only
filters or programs that return XDP_DROP or XDP_PASS. These programs run
correctly, because the fixup in bpf_prog_run_generic_xdp() only fires
when those bytes change:

	if ((orig_eth_type != eth->h_proto) ||
	    (orig_host != ether_addr_equal_64bits(eth->h_dest,
						  skb->dev->dev_addr)) ||
	    (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
		__skb_push(skb, ETH_HLEN);

A plain "ip link set dev X xdp obj ..." with no mode flag is affected as
well. dev_xdp_mode() falls back to XDP_MODE_SKB when ndo_bpf is missing:

	return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;

tun_netdev_ops (TUN mode) has no ndo_bpf, so XDP on an IFF_TUN device
always uses generic mode and is now always rejected.

The commit message says these attaches will be refused, but it describes
generic XDP on L3 devices only as comparing garbage.

Could it say that generic XDP setups that work today on these devices will
now get -EOPNOTSUPP? The Fixes: tag points at a 2017 commit, so this uAPI
restriction would also reach every stable tree.

[Severity: Low]
Should Documentation/networking/af_xdp.rst be updated in the same patch?
It describes XDP_SKB mode as:

  A fallback mode that works for any network device.

After this patch, dev_xdp_attach() returns -EOPNOTSUPP for XDP_MODE_SKB on
any device that is neither ARPHRD_ETHER nor ARPHRD_LOOPBACK. That covers
both cases the document describes: an explicit XDP_SKB request, and the
fallback for drivers without XDP support.

netif_receive_generic_xdp() also skips skbs whose mac header is not
ETH_HLEN bytes, so generic XDP runs in fewer cases than the document
describes.

>  	}
>  
>  	/* don't call drivers if the effective program didn't change */

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124142545.221584-1-jiayuan.chen%40linux.dev

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25 14:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 14:25 [PATCH net v3] net: xdp: fix generic XDP on skbs without an Ethernet header Jiayuan Chen
2026-11-25 14:27 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox