Skip to contentSkip to conversation

net: remove dev_validate_header() and net_device::min_header_len

PATCH In review Willem de Bruijn 4 participants 13 messages Nov 24 – Nov 26

Subsystems (5)
lore
Share & tools
lore

Willem de Bruijn

author maintainer
#0 lore raw

cover net: remove dev_validate_header() and net_device::min_header_len

From: Willem de Bruijn <[email protected]>

Remove unused link-layer header validation plumbing, clean up the remaining af_packet users, and drop net_device::min_header_len.

Mostly cleanup, with two user-visible changes:

- Patch 4: on devices that set hard_header_len but have no link layer header (e.g., PPP), SOCK_RAW and TX_RING sends now always have skb->network_header at the start of the packet, not only for short packets.
- Patch 5: on SOCK_DGRAM, the VLAN TCI of non-offloaded tags is now reported on all Ethernet devices, including those with hard_header_len > ETH_HLEN, and is 0 on non-Ethernet devices.

1. Drop stale Ethernet fields (including min_header_len) in IPA modem.
2. Remove header_ops->validate and dev_validate_header().
3. Move af_packet TX header length checks ahead of skb allocation.
4. Reset network header on all headerless devices via dev_has_header().
5. Use ARPHRD_ETHER / ARPHRD_LOOPBACK and ETH_HLEN in vlan_get_tci().
6. Remove unused net_device::min_header_len.

Testing (under vng):
- tools/testing/selftests/net: psock_snd.sh, psock_tpacket, and txring_overwrite.
- Custom tests for short/zero header validation (SOCK_RAW, SOCK_DGRAM, SOCK_PACKET, TX_RING), non-offloaded outgoing VLAN TCI capture (AUXDATA, TPACKET_V2, TPACKET_V3), and 48B/100B IPv6 TX over sit (SOCK_RAW, SOCK_PACKET, TX_RING).

Willem de Bruijn (6):
net: ipa: remove ethernet-specific artifacts from modem netdev setup
net: remove header_ops->validate and dev_validate_header()
packet: validate TX header length before skb allocation
packet: reset network header on all headerless devices
packet: use Ethernet header length in vlan_get_tci()
net: remove unused net_device::min_header_len

 .../networking/net_cachelines/net_device.rst  |  1 -
 drivers/net/ipa/ipa_modem.c                   |  4 -
 drivers/net/loopback.c                        |  1 -
 include/linux/netdevice.h                     | 18 -----
 net/ethernet/eth.c                            |  1 -
 net/packet/af_packet.c                        | 80 ++++++++-----------
 6 files changed, 32 insertions(+), 73 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author maintainer
#1 lore raw

PATCH 1/6 net: ipa: remove ethernet-specific artifacts from modem netdev setup

From: Willem de Bruijn <[email protected]>

Commit 75944b040bbc ("net: ipa: open-code ether_setup()") open-coded ether_setup(), including fields that do not apply to this headerless ARPHRD_RAWIP device:

- min_header_len = ETH_HLEN, while hard_header_len is 0
- an Ethernet broadcast address, while addr_len is 0
- clearing IFF_BROADCAST | IFF_MULTICAST, which are never set

Remove these and the now unused etherdevice.h include.

With min_header_len left at 0, the device behaves like other headerless devices (such as tun and rmnet) in af_packet's vlan_get_tci(), where min_header_len == hard_header_len instead of returning early on the mismatch. Normal IPA traffic uses ETH_P_MAP rather than a VLAN ethertype, so it is unaffected.

---
 drivers/net/ipa/ipa_modem.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/ipa/ipa_modem.c +0 -4
index d84c1dbd3b1a..e8392d9d3a4f 100644
@@ -5,7 +5,6 @@
  */
 
 #include <linux/errno.h>
-#include <linux/etherdevice.h>
 #include <linux/if_arp.h>
 #include <linux/if_rmnet.h>
 #include <linux/netdevice.h>
@@ -223,15 +222,12 @@ static void ipa_modem_netdev_setup(struct net_device *netdev)
 	netdev->header_ops = NULL;
 	netdev->type = ARPHRD_RAWIP;
 	netdev->hard_header_len = 0;
-	netdev->min_header_len = ETH_HLEN;
 	netdev->min_mtu = ETH_MIN_MTU;
 	netdev->max_mtu = IPA_MTU;
 	netdev->mtu = netdev->max_mtu;
 	netdev->addr_len = 0;
 	netdev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
-	netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
 	netdev->priv_flags |= IFF_TX_SKB_SHARING;
-	eth_broadcast_addr(netdev->broadcast);
 
 	/* The endpoint is configured for QMAP */
 	netdev->needed_headroom = sizeof(struct rmnet_map_header);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
Acked-by on v1 1/6
#2 lore raw

Re: [PATCH net-next 1/6] net: ipa: remove ethernet-specific artifacts from modem netdev setup

Quoted 12 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Commit 75944b040bbc ("net: ipa: open-code ether_setup()") open-coded ether_setup(), including fields that do not apply to this headerless ARPHRD_RAWIP device:

- min_header_len = ETH_HLEN, while hard_header_len is 0
- an Ethernet broadcast address, while addr_len is 0
- clearing IFF_BROADCAST | IFF_MULTICAST, which are never set

Remove these and the now unused etherdevice.h include.

Quoted 8 lines

With min_header_len left at 0, the device behaves like other headerless devices (such as tun and rmnet) in af_packet's vlan_get_tci(), where min_header_len == hard_header_len instead of returning early on the mismatch. Normal IPA traffic uses ETH_P_MAP rather than a VLAN ethertype, so it is unaffected.

This looks pretty reasonable to me. The "don't open-code" change was meant to avoid duplicate code. All the assignments were preserved, but I didn't fully understand how these fields were used.

For now I'll just ack this. If I manage to get my system up to test this, I'll do that and provide a Reviewed-by, but I'm not sure I'll get to that soon.

Quoted 32 lines
---
  drivers/net/ipa/ipa_modem.c | 4 ----
  1 file changed, 4 deletions(-)
drivers/net/ipa/ipa_modem.c +0 -4
index d84c1dbd3b1a..e8392d9d3a4f 100644
@@ -5,7 +5,6 @@
   */
 
  #include <linux/errno.h>
-#include <linux/etherdevice.h>
  #include <linux/if_arp.h>
  #include <linux/if_rmnet.h>
  #include <linux/netdevice.h>
@@ -223,15 +222,12 @@ static void ipa_modem_netdev_setup(struct net_device *netdev)
  	netdev->header_ops = NULL;
  	netdev->type = ARPHRD_RAWIP;
  	netdev->hard_header_len = 0;
-	netdev->min_header_len = ETH_HLEN;
  	netdev->min_mtu = ETH_MIN_MTU;
  	netdev->max_mtu = IPA_MTU;
  	netdev->mtu = netdev->max_mtu;
  	netdev->addr_len = 0;
  	netdev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
-	netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
  	netdev->priv_flags |= IFF_TX_SKB_SHARING;
-	eth_broadcast_addr(netdev->broadcast);
 
  	/* The endpoint is configured for QMAP */
  	netdev->needed_headroom = sizeof(struct rmnet_map_header);
#3 lore raw

Re: [PATCH net-next 1/6] net: ipa: remove ethernet-specific artifacts from modem netdev setup

Quoted 27 lines · Alex Elder
Quoted 12 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Commit 75944b040bbc ("net: ipa: open-code ether_setup()") open-coded ether_setup(), including fields that do not apply to this headerless ARPHRD_RAWIP device:

- min_header_len = ETH_HLEN, while hard_header_len is 0
- an Ethernet broadcast address, while addr_len is 0
- clearing IFF_BROADCAST | IFF_MULTICAST, which are never set

Remove these and the now unused etherdevice.h include.

Quoted 8 lines

With min_header_len left at 0, the device behaves like other headerless devices (such as tun and rmnet) in af_packet's vlan_get_tci(), where min_header_len == hard_header_len instead of returning early on the mismatch. Normal IPA traffic uses ETH_P_MAP rather than a VLAN ethertype, so it is unaffected.

This looks pretty reasonable to me. The "don't open-code" change was meant to avoid duplicate code. All the assignments were preserved, but I didn't fully understand how these fields were used.

Quoted 5 lines

For now I'll just ack this. If I manage to get my system up to test this, I'll do that and provide a Reviewed-by, but I'm not sure I'll get to that soon.

Thanks Alex!

author maintainer
#4 lore raw

PATCH 2/6 net: remove header_ops->validate and dev_validate_header()

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

header_ops->validate was added in commit 2793a23aacbd ("net: validate variable length ll headers") for AX.25, its only implementation. AX.25 was removed in commit dd8d4bc28ad7 ("net: remove ax25 and amateur radio (hamradio) subsystem"), and the CAP_SYS_RAWIO bypass in dev_validate_header() was removed in commit 3b9a324e646d ("net: remove CAP_SYS_RAWIO zero-padding in dev_validate_header").

With both gone, dev_validate_header() only checks
len >= dev->hard_header_len. Remove header_ops->validate and dev_validate_header(), and replace the only callers in af_packet with direct checks against the already snapshotted hard_header_len.

---
 include/linux/netdevice.h | 16 ----------------
 net/packet/af_packet.c    |  7 +++----
 2 files changed, 3 insertions(+), 20 deletions(-)
include/linux/netdevice.h +0 -16
index 694b6d29f680..930899a7a577 100644
@@ -326,7 +326,6 @@ struct header_ops {
 	void	(*cache_update)(struct hh_cache *hh,
 				const struct net_device *dev,
 				const unsigned char *haddr);
-	bool	(*validate)(const char *ll_header, unsigned int len);
 	__be16	(*parse_protocol)(const struct sk_buff *skb);
 };
 
@@ -3587,21 +3586,6 @@ static inline __be16 dev_parse_header_protocol(const struct sk_buff *skb)
 	return dev->header_ops->parse_protocol(skb);
 }
 
-/* ll_header must have at least hard_header_len allocated */
-static inline bool dev_validate_header(const struct net_device *dev,
-				       char *ll_header, int len)
-{
-	if (likely(len >= dev->hard_header_len))
-		return true;
-	if (len < dev->min_header_len)
-		return false;
-
-	if (dev->header_ops && dev->header_ops->validate)
-		return dev->header_ops->validate(ll_header, len);
-
-	return false;
-}
-
 static inline bool dev_has_header(const struct net_device *dev)
 {
 	return dev->header_ops && dev->header_ops->create;
net/packet/af_packet.c +3 -4
index 96c5216a9d6b..230b16742346 100644
@@ -2060,7 +2060,7 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 		goto retry;
 	}
 
-	if (!dev_validate_header(dev, skb->data, len) || !skb->len) {
+	if (len < hard_header_len || !skb->len) {
 		err = -EINVAL;
 		goto out_unlock;
 	}
@@ -2644,7 +2644,7 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 		err = skb_store_bits(skb, 0, data, hdrlen);
 		if (unlikely(err))
 			return err;
-		if (!dev_validate_header(dev, skb->data, hdrlen))
+		if (hdrlen < hard_header_len)
 			return -EINVAL;
 
 		data += hdrlen;
@@ -3115,8 +3115,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 	if (err)
 		goto out_free;
 
-	if ((sock->type == SOCK_RAW &&
-	     !dev_validate_header(dev, skb->data, len)) || !skb->len) {
+	if ((sock->type == SOCK_RAW && len < hard_header_len) || !skb->len) {
 		err = -EINVAL;
 		goto out_free;
 	}
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#5 lore raw

Re: [PATCH net-next 2/6] net: remove header_ops->validate and dev_validate_header()

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

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0 (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] In packet_sendmsg_spkt() (net/packet/af_packet.c), the SOCK_PACKET send path now checks the packet length against the wrong…

Pre-existing issues:
- [Low] Baseline decision: false (kept only because requires_validation=true).

--

net: remove header_ops->validate and dev_validate_header()

This patch removes header_ops->validate and dev_validate_header(). Their only user, AX.25, has been removed, and so has the CAP_SYS_RAWIO bypass. The three af_packet callers now compare directly against the snapshotted hard_header_len.

net/packet/af_packet.c static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, · line 2063

Quoted 10 lines
net/packet/af_packet.c +1 -1
index 96c5216a9d6b4..230b16742346c 100644
@@ -2060,7 +2060,7 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 		goto retry;
 	}
 
-	if (!dev_validate_header(dev, skb->data, len) || !skb->len) {
+	if (len < hard_header_len || !skb->len) {

[Severity: Medium]
Is this check still the same as the old one when the lookup after the retry returns a different net_device?

packet_sendmsg_spkt() looks up the device by name twice: once before the skb is allocated, and again after goto retry. hard_header_len is only read on the first pass:

	/* Keep the allocation-time header length across retry. */
	if (!skb)
		hard_header_len = READ_ONCE(dev->hard_header_len);

The old dev_validate_header(dev, skb->data, len) read dev->hard_header_len from the device found on the retry. That is the device passed to dev_queue_xmit(). The new check uses the value from the first device.

Between the two lookups the RCU lock is dropped, and the code sleeps in sock_wmalloc() and memcpy_from_msg(). User space can stall that copy with a userfaultfd or FUSE backed buffer. Meanwhile the name can move to another device: netif_change_name() allows renames while the device is IFF_UP, the device can be deleted and recreated, or the name can become an altname on another device.

Take a name that first resolves to a tun device (hard_header_len 0) and then to a veth device (hard_header_len 14). A 1 byte frame now passes len < hard_header_len, where the old code returned -EINVAL. Can that frame then reach dev_queue_xmit() on the veth? If so, eth_type_trans() on the peer would read eth->h_proto from skb->data past skb->len.

CAP_NET_RAW in a network namespace the user owns is enough to reach this.

packet_snd() and tpacket_fill_skb() have a weaker version of this. They no longer see hard_header_len grow on the same device (for example through a bonding type change) during the sleeping allocation and copy. tpacket_fill_skb() also uses a single value for the whole tpacket_snd() batch. In those two paths the old live check could also go stale right after it ran.

The commit message describes these as "direct checks against the already snapshotted hard_header_len". For packet_sendmsg_spkt(), though, the snapshot comes from a device that may not be the one sent on.

Later in the series, "packet: validate TX header length before skb allocation" moves this check into the first-pass if (!skb) block. If I read it right, the device found on the retry is still never checked at the end of the series.

[Severity: Low]
This is a pre-existing issue, but with this check the short-header branch in the allocation path of packet_sendmsg_spkt() looks like dead code:

		if (hhlen) {
			skb->data -= hhlen;
			skb->tail -= hhlen;
			if (len < hhlen)
				skb_reset_network_header(skb);
		}

hhlen is either 0 or hard_header_len. Any skb that takes the len < hhlen branch is therefore rejected by the len < hard_header_len check after the retry. The branch already had no effect on sent packets once AX.25 was removed.

"packet: validate TX header length before skb allocation" removes it later in the series.

Quoted 3 lines
net/packet/af_packet.c +0 -0
 		err = -EINVAL;
 		goto out_unlock;
 	}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124170551.1874487-1-willemdebruijn.kernel%40gmail.com
author maintainer
#6 lore raw

PATCH 3/6 packet: validate TX header length before skb allocation

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Now that dev_validate_header() is gone, TX header validation only compares length against hard_header_len and no longer needs the copied link layer header.

Simplify packet_sendmsg_spkt(), tpacket_snd(), and packet_snd() by validating against the snapshotted hard_header_len before skb allocation and copy. This removes the now unreachable len < hhlen network header reset in packet_sendmsg_spkt() and, since __packet_snd_vnet_parse() already enforces vnet_hdr.hdr_len <= tp_len, the redundant hdrlen clamp in tpacket_fill_skb().

A too short packet now fails with -EINVAL before, rather than after, allocation and copy errors (e.g., -ENOBUFS, -EAGAIN, -EFAULT).

---
 net/packet/af_packet.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
net/packet/af_packet.c +19 -15
index 230b16742346..e8326cade94a 100644
@@ -2024,8 +2024,13 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 	}
 
 	/* Keep the allocation-time header length across retry. */
-	if (!skb)
+	if (!skb) {
 		hard_header_len = READ_ONCE(dev->hard_header_len);
+		if (len < hard_header_len || !len) {
+			err = -EINVAL;
+			goto out_unlock;
+		}
+	}
 
 	err = -EMSGSIZE;
 	if (len > dev->mtu + hard_header_len + VLAN_HLEN + extra_len)
@@ -2051,8 +2056,6 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 		if (hhlen) {
 			skb->data -= hhlen;
 			skb->tail -= hhlen;
-			if (len < hhlen)
-				skb_reset_network_header(skb);
 		}
 		err = memcpy_from_msg(skb_put(skb, len), msg, len);
 		if (err)
@@ -2060,10 +2063,6 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 		goto retry;
 	}
 
-	if (len < hard_header_len || !skb->len) {
-		err = -EINVAL;
-		goto out_unlock;
-	}
 	if (len > (dev->mtu + hard_header_len + extra_len) &&
 	    !packet_extra_vlan_len_allowed(dev, skb)) {
 		err = -EMSGSIZE;
@@ -2637,18 +2636,14 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 		if (unlikely(err < 0))
 			return -EINVAL;
 	} else if (copylen) {
-		int hdrlen = min_t(int, copylen, tp_len);
-
 		skb_push(skb, hard_header_len);
 		skb_put(skb, copylen - hard_header_len);
-		err = skb_store_bits(skb, 0, data, hdrlen);
+		err = skb_store_bits(skb, 0, data, copylen);
 		if (unlikely(err))
 			return err;
-		if (hdrlen < hard_header_len)
-			return -EINVAL;
 
-		data += hdrlen;
-		to_write -= hdrlen;
+		data += copylen;
+		to_write -= copylen;
 	}
 
 	offset = offset_in_page(data);
@@ -2878,6 +2873,10 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
 						    vnet_hdr.hdr_len);
 			has_vnet_hdr = true;
 		}
+		if (unlikely(tp_len < reserve)) {
+			tp_len = -EINVAL;
+			goto tpacket_error;
+		}
 		/* Prevent the byte count from wrapping into a negative errno */
 		if (unlikely(tp_len > INT_MAX - len_sum))
 			break;
@@ -3086,6 +3085,11 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 	    (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
 		goto out_unlock;
 
+	if (unlikely(len < reserve)) {
+		err = -EINVAL;
+		goto out_unlock;
+	}
+
 	err = -ENOBUFS;
 	hlen = LL_RESERVED_SPACE_EX(dev, hard_header_len);
 	tlen = dev->needed_tailroom;
@@ -3115,7 +3119,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 	if (err)
 		goto out_free;
 
-	if ((sock->type == SOCK_RAW && len < hard_header_len) || !skb->len) {
+	if (unlikely(!skb->len)) {
 		err = -EINVAL;
 		goto out_free;
 	}
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#7 lore raw

Re: [PATCH net-next 3/6] packet: validate TX header length before skb allocation

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

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] In packet_sendmsg_spkt() (net/packet/af_packet.c), the header- length check (len < hard_header_len || !len) now runs only on the…

--

packet: validate TX header length before skb allocation

This patch moves the TX header length checks in packet_sendmsg_spkt(), tpacket_snd() and packet_snd() ahead of skb allocation. The checks now compare against the snapshotted hard_header_len. It also drops the network header reset in packet_sendmsg_spkt(), which can no longer be reached, and the hdrlen clamp in tpacket_fill_skb().

Quoted 19 lines
net/packet/af_packet.c +6 -1
index 230b16742346c..e8326cade94af 100644
@@ -2024,8 +2024,13 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 	}
 
 	/* Keep the allocation-time header length across retry. */
-	if (!skb)
+	if (!skb) {
 		hard_header_len = READ_ONCE(dev->hard_header_len);
+		if (len < hard_header_len || !len) {
+			err = -EINVAL;
+			goto out_unlock;
+		}
+	}
 
 	err = -EMSGSIZE;
 	if (len > dev->mtu + hard_header_len + VLAN_HLEN + extra_len)

[ ... ]

net/packet/af_packet.c static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, · line 2068

Quoted 11 lines
net/packet/af_packet.c +0 -4
@@ -2060,10 +2063,6 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 		goto retry;
 	}
 
-	if (len < hard_header_len || !skb->len) {
-		err = -EINVAL;
-		goto out_unlock;
-	}
 	if (len > (dev->mtu + hard_header_len + extra_len) &&
 	    !packet_extra_vlan_len_allowed(dev, skb)) {
 		err = -EMSGSIZE;

[Severity: Medium]
With this check removed, is len ever validated against the device that actually transmits the skb?

The new check in the first hunk only runs while skb is NULL. It uses the device returned by the first dev_get_by_name_rcu() lookup. After that, packet_sendmsg_spkt() drops the RCU read lock and calls sock_wmalloc() with GFP_KERNEL and then memcpy_from_msg(). Both can sleep. It then jumps back and looks the device up again by name:

retry:

	rcu_read_lock();
	dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);

Suppose spkt_device now names a different netdev (renamed, or deleted and recreated) with a larger hard_header_len. Only the MTU checks remain before dev_queue_xmit().

For example, a 1 byte frame sized for a tun device (hard_header_len 0) could be sent on a veth device (hard_header_len 14). This needs CAP_NET_RAW and CAP_NET_ADMIN. Both are available inside an unprivileged user plus network namespace.

On veth the call path is:

veth_xmit()->veth_forward_skb()->__dev_forward_skb()->eth_type_trans() ->eth_skb_pull_mac()

eth_skb_pull_mac() reads the ethhdr at skb->data even when skb->len is less than ETH_HLEN:

include/linux/etherdevice.h:eth_skb_pull_mac() {
	struct ethhdr *eth = (struct ethhdr *)skb->data;

	skb_pull_inline(skb, ETH_HLEN);
	return eth;
}

Would eth->h_proto and the pkt_type classification then come from uninitialized bytes past skb->len?

Before the series, dev_validate_header(dev, skb->data, len) ran after the retry. It checked the live transmit device, inside the same RCU section as dev_queue_xmit().

The earlier patch "net: remove header_ops->validate and dev_validate_header()" switched that check to the snapshotted value. This patch then removes the post-retry check. The gap is still there at the end of the series ("net: remove unused net_device::min_header_len").

Could len be checked here again against READ_ONCE(dev->hard_header_len) of the retry device? Another option is to bail out if the device changed between the two lookups.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124170551.1874487-1-willemdebruijn.kernel%40gmail.com
author maintainer
#8 lore raw

PATCH 4/6 packet: reset network header on all headerless devices

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Some devices without a userspace-visible link layer header (!dev_has_header()) still set hard_header_len > 0 instead of needed_headroom to reserve space for headers they prepend internally in ndo_start_xmit() (e.g., PPP, mctp-usb, routed br2684, and previously sit). On SOCK_RAW sends, af_packet reserves hard_header_len and leaves skb->network_header pointing that many bytes into the L3 header.

Commit 88a8121dc1d3 ("af_packet: fix raw sockets over 6in4 tunnel") added a workaround in packet_snd(), explicitly noting in its commit message that it was temporary and left to be cleaned up in net-next. That heuristic misses longer packets and misfires on non-Ethernet devices with a link layer header where min_header_len is 0.

Replace it with !dev_has_header(dev) in packet_snd() and apply the same in tpacket_fill_skb().

For consistency, also switch packet_sendmsg_spkt() from dev->header_ops to dev_has_header(dev). This has no effect today: the only header_ops without ->create is ip_tunnel_header_ops, and all devices using it have hard_header_len 0.

---
 net/packet/af_packet.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
net/packet/af_packet.c +4 -3
index e8326cade94a..bf0ad6d5c1e3 100644
@@ -2039,7 +2039,7 @@ static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
 	if (!skb) {
 		size_t reserved = LL_RESERVED_SPACE_EX(dev, hard_header_len);
 		int tlen = dev->needed_tailroom;
-		unsigned int hhlen = dev->header_ops ? hard_header_len : 0;
+		unsigned int hhlen = dev_has_header(dev) ? hard_header_len : 0;
 
 		rcu_read_unlock();
 		skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
@@ -2638,6 +2638,8 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 	} else if (copylen) {
 		skb_push(skb, hard_header_len);
 		skb_put(skb, copylen - hard_header_len);
+		if (!dev_has_header(dev))
+			skb_reset_network_header(skb);
 		err = skb_store_bits(skb, 0, data, copylen);
 		if (unlikely(err))
 			return err;
@@ -3109,8 +3111,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
 			goto out_free;
 	} else if (reserve) {
 		skb_reserve(skb, -reserve);
-		if (len < reserve + sizeof(struct ipv6hdr) &&
-		    dev->min_header_len != hard_header_len)
+		if (!dev_has_header(dev))
 			skb_reset_network_header(skb);
 	}
 
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author maintainer
#9 lore raw

PATCH 5/6 packet: use Ethernet header length in vlan_get_tci()

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

vlan_get_tci() uses dev->min_header_len to locate the outer 802.1Q/AD header on SOCK_DGRAM captures, with an is_vlan_dev() fallback that has been obsolete since commit 447cbe95ebb9 ("vlan: fix skb_under_panic and races when toggling HW VLAN offload") stopped adding VLAN_HLEN to vlandev->hard_header_len.

Only Ethernet and loopback devices set min_header_len (to ETH_HLEN). Check ARPHRD_ETHER / ARPHRD_LOOPBACK and read at ETH_HLEN directly instead, removing the last min_header_len reader and letting packet_recvmsg() use sll->sll_hatype without an RCU device lookup.

This changes the reported TCI in three cases:

- Ethernet devices with hard_header_len > ETH_HLEN (e.g., smsc95xx, rndis_host) previously failed the min_header_len == hard_header_len test and reported TCI 0. They now report the TCI.
- Non-Ethernet devices with hard_header_len 0 (e.g., tun, rmnet) previously read a VLAN header at the MAC offset. They now report TCI 0.
- packet_recvmsg() previously omitted the VLAN fields if the device had been unregistered since the packet was queued. It now fills them from the queued skb.

---
 net/packet/af_packet.c | 38 +++++++++-----------------------------
 1 file changed, 9 insertions(+), 29 deletions(-)
net/packet/af_packet.c +9 -29
index bf0ad6d5c1e3..b169d3e11b17 100644
@@ -547,29 +547,18 @@ static void *packet_current_frame(struct packet_sock *po,
 	return packet_lookup_frame(po, rb, rb->head, status);
 }
 
-static u16 vlan_get_tci(const struct sk_buff *skb, struct net_device *dev)
+static u16 vlan_get_tci(const struct sk_buff *skb, unsigned short type)
 {
 	struct vlan_hdr vhdr, *vh;
-	unsigned int header_len;
 
-	if (!dev)
+	if (type != ARPHRD_ETHER && type != ARPHRD_LOOPBACK)
 		return 0;
 
 	/* In the SOCK_DGRAM scenario, skb data starts at the network
 	 * protocol, which is after the VLAN headers. The outer VLAN
-	 * header is at the hard_header_len offset in non-variable
-	 * length link layer headers. If it's a VLAN device, the
-	 * min_header_len should be used to exclude the VLAN header
-	 * size.
+	 * header is at the ETH_HLEN offset from the MAC header.
 	 */
-	if (dev->min_header_len == dev->hard_header_len)
-		header_len = dev->hard_header_len;
-	else if (is_vlan_dev(dev))
-		header_len = dev->min_header_len;
-	else
-		return 0;
-
-	vh = skb_header_pointer(skb, skb_mac_offset(skb) + header_len,
+	vh = skb_header_pointer(skb, skb_mac_offset(skb) + ETH_HLEN,
 				sizeof(vhdr), &vhdr);
 	if (unlikely(!vh))
 		return 0;
@@ -1012,7 +1001,7 @@ static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
 		ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
 		ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 	} else if (unlikely(po->sk.sk_type == SOCK_DGRAM && eth_type_vlan(pkc->skb->protocol))) {
-		ppd->hv1.tp_vlan_tci = vlan_get_tci(pkc->skb, pkc->skb->dev);
+		ppd->hv1.tp_vlan_tci = vlan_get_tci(pkc->skb, pkc->skb->dev->type);
 		ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->protocol);
 		ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 	} else {
@@ -2454,7 +2443,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 			h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
 			status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 		} else if (unlikely(sk->sk_type == SOCK_DGRAM && eth_type_vlan(skb->protocol))) {
-			h.h2->tp_vlan_tci = vlan_get_tci(skb, skb->dev);
+			h.h2->tp_vlan_tci = vlan_get_tci(skb, dev->type);
 			h.h2->tp_vlan_tpid = ntohs(skb->protocol);
 			status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 		} else {
@@ -3626,19 +3615,10 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 			aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 		} else if (unlikely(sock->type == SOCK_DGRAM && eth_type_vlan(skb->protocol))) {
 			struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
-			struct net_device *dev;
 
-			rcu_read_lock();
-			dev = dev_get_by_index_rcu(sock_net(sk), sll->sll_ifindex);
-			if (dev) {
-				aux.tp_vlan_tci = vlan_get_tci(skb, dev);
-				aux.tp_vlan_tpid = ntohs(skb->protocol);
-				aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
-			} else {
-				aux.tp_vlan_tci = 0;
-				aux.tp_vlan_tpid = 0;
-			}
-			rcu_read_unlock();
+			aux.tp_vlan_tci = vlan_get_tci(skb, sll->sll_hatype);
+			aux.tp_vlan_tpid = ntohs(skb->protocol);
+			aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 		} else {
 			aux.tp_vlan_tci = 0;
 			aux.tp_vlan_tpid = 0;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#10 lore raw

Re: [PATCH net-next 5/6] packet: use Ethernet header length in vlan_get_tci()

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

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] packet_recvmsg() (net/packet/af_packet.c:3616-3621) now passes PACKET_SKB_CB(skb)->sa.ll.sll_hatype to vlan_get_tci() and sets…

--

packet: use Ethernet header length in vlan_get_tci()

This changes vlan_get_tci() to take a device type instead of a device. For ARPHRD_ETHER and ARPHRD_LOOPBACK devices it now reads the outer VLAN header at ETH_HLEN, instead of using dev->min_header_len. packet_recvmsg() now passes the queued sll_hatype instead of looking up the device by ifindex under RCU.

Quoted 4 lines
net/packet/af_packet.c +0 -0
index bf0ad6d5c1e38..b169d3e11b174 100644

[ ... ]

Quoted 10 lines
net/packet/af_packet.c +0 -5
@@ -3626,19 +3615,10 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 			aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 		} else if (unlikely(sock->type == SOCK_DGRAM && eth_type_vlan(skb->protocol))) {
 			struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
-			struct net_device *dev;
 
-			rcu_read_lock();
-			dev = dev_get_by_index_rcu(sock_net(sk), sll->sll_ifindex);
-			if (dev) {
-				aux.tp_vlan_tci = vlan_get_tci(skb, dev);

net/packet/af_packet.c static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, · line 3621

Quoted 10 lines
net/packet/af_packet.c +3 -7
-				aux.tp_vlan_tpid = ntohs(skb->protocol);
-				aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
-			} else {
-				aux.tp_vlan_tci = 0;
-				aux.tp_vlan_tpid = 0;
-			}
-			rcu_read_unlock();
+			aux.tp_vlan_tci = vlan_get_tci(skb, sll->sll_hatype);
+			aux.tp_vlan_tpid = ntohs(skb->protocol);
+			aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;

[Severity: Medium]
Is sll_hatype always filled in for skbs on sk_receive_queue?

For TPACKET_V1/V2 rings, tpacket_rcv() queues a TP_STATUS_COPY skb. Its sockaddr_ll is zeroed, and sll_hatype and sll_ifindex are never set afterwards:

net/packet/af_packet.c:tpacket_rcv() {
    ...
				if (copy_skb) {
					memset(&PACKET_SKB_CB(copy_skb)->sa.ll, 0,
					       sizeof(PACKET_SKB_CB(copy_skb)->sa.ll));
					skb_set_owner_r(copy_skb, sk);
				}
    ...
		__skb_queue_tail(&sk->sk_receive_queue, copy_skb);
    ...
}

As far as I can see, only packet_rcv() sets sll_hatype in the cb.

For such a copy_skb, sll_hatype is 0, so vlan_get_tci() hits the new check and returns 0:

	if (type != ARPHRD_ETHER && type != ARPHRD_LOOPBACK)
		return 0;

The auxdata cmsg would then report tp_vlan_tci 0 with TP_STATUS_VLAN_VALID set. That happens even when the frame came from an Ethernet device and carries a real in-band 802.1Q/AD tag.

Before this patch, dev_get_by_index_rcu() failed for sll_ifindex 0, so the VLAN fields and flags were left clear.

The TPACKET_V2 ring frame for the same packet still calls vlan_get_tci(skb, dev->type) in tpacket_rcv() and reports the real TCI. The ring header and the recvmsg() auxdata would then disagree.

One way to reach this is a SOCK_DGRAM socket with a V1/V2 RX ring and both PACKET_COPY_THRESH and PACKET_AUXDATA set. It then has to receive an in-band VLAN frame larger than tp_frame_size.

The commit message says:

- packet_recvmsg() previously omitted the VLAN fields if the device had been unregistered since the packet was queued. It now fills them from the queued skb.

This assumes the cb always holds the real hatype.

Would it make sense to set PACKET_SKB_CB(copy_skb)->sa.ll.sll_hatype to dev->type in tpacket_rcv()? Another option is to leave TP_STATUS_VLAN_VALID clear when the hatype is not Ethernet or loopback.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124170551.1874487-1-willemdebruijn.kernel%40gmail.com
author maintainer
#11 lore raw

PATCH 6/6 net: remove unused net_device::min_header_len

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

dev->min_header_len was introduced in commit 217e6fa24ce2 ("net:
introduce device min_header_len") for dev_validate_header() when header_ops->validate and the CAP_SYS_RAWIO bypass could accept frames shorter than hard_header_len.

Now that dev_validate_header() and the remaining workarounds in af_packet have been removed, dev->min_header_len has no readers left. Remove the field from struct net_device along with its remaining assignments in ether_setup() and loopback setup.

---
 Documentation/networking/net_cachelines/net_device.rst | 1 -
 drivers/net/loopback.c                                 | 1 -
 include/linux/netdevice.h                              | 2 --
 net/ethernet/eth.c                                     | 1 -
 4 files changed, 5 deletions(-)
Documentation/networking/net_cachelines/net_device.rst +0 -1
index 7620d3fcb8dc..f73ea0069432 100644
@@ -46,7 +46,6 @@ netdev_features_t                   gso_partial_features        read_mostly
 unsigned_int                        min_mtu
 unsigned_int                        max_mtu
 unsigned_short                      type
-unsigned_char                       min_header_len
 unsigned_char                       name_assign_type
 int                                 group
 struct net_device_stats             stats
drivers/net/loopback.c +0 -1
index 1e6d1b7d71ba..6fdf00c223b9 100644
@@ -173,7 +173,6 @@ static void gen_lo_setup(struct net_device *dev,
 {
 	dev->mtu		= mtu;
 	dev->hard_header_len	= ETH_HLEN;	/* 14	*/
-	dev->min_header_len	= ETH_HLEN;	/* 14	*/
 	dev->addr_len		= ETH_ALEN;	/* 6	*/
 	dev->type		= ARPHRD_LOOPBACK;	/* 0x0001*/
 	dev->flags		= IFF_LOOPBACK;
include/linux/netdevice.h +0 -2
index 930899a7a577..2a1b3f48bed5 100644
@@ -1929,7 +1929,6 @@ enum netdev_reg_state {
  *	@max_mtu:	Interface Maximum MTU value
  *	@type:		Interface hardware type
  *	@hard_header_len: Maximum hardware header length.
- *	@min_header_len:  Minimum hardware header length
  *
  *	@needed_headroom: Extra headroom the hardware may need, but not in all
  *			  cases can this be guaranteed
@@ -2292,7 +2291,6 @@ struct net_device {
 	unsigned int		min_mtu;
 	unsigned int		max_mtu;
 	unsigned short		type;
-	unsigned char		min_header_len;
 	unsigned char		name_assign_type;
 
 	int			group;
net/ethernet/eth.c +0 -1
index 39aab0f8035a..db39527d4704 100644
@@ -330,7 +330,6 @@ void ether_setup(struct net_device *dev)
 	dev->header_ops		= &eth_header_ops;
 	dev->type		= ARPHRD_ETHER;
 	dev->hard_header_len 	= ETH_HLEN;
-	dev->min_header_len	= ETH_HLEN;
 	dev->mtu		= ETH_DATA_LEN;
 	dev->min_mtu		= ETH_MIN_MTU;
 	dev->max_mtu		= ETH_DATA_LEN;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#12 lore raw
In reply to Willem de Bruijn
Quoted 38 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Remove unused link-layer header validation plumbing, clean up the remaining af_packet users, and drop net_device::min_header_len.

Mostly cleanup, with two user-visible changes:

- Patch 4: on devices that set hard_header_len but have no link layer header (e.g., PPP), SOCK_RAW and TX_RING sends now always have skb->network_header at the start of the packet, not only for short packets.
- Patch 5: on SOCK_DGRAM, the VLAN TCI of non-offloaded tags is now reported on all Ethernet devices, including those with hard_header_len > ETH_HLEN, and is 0 on non-Ethernet devices.

1. Drop stale Ethernet fields (including min_header_len) in IPA modem.
2. Remove header_ops->validate and dev_validate_header().
3. Move af_packet TX header length checks ahead of skb allocation.
4. Reset network header on all headerless devices via dev_has_header().
5. Use ARPHRD_ETHER / ARPHRD_LOOPBACK and ETH_HLEN in vlan_get_tci().
6. Remove unused net_device::min_header_len.

Testing (under vng):
- tools/testing/selftests/net: psock_snd.sh, psock_tpacket, and txring_overwrite.
- Custom tests for short/zero header validation (SOCK_RAW, SOCK_DGRAM, SOCK_PACKET, TX_RING), non-offloaded outgoing VLAN TCI capture (AUXDATA, TPACKET_V2, TPACKET_V3), and 48B/100B IPv6 TX over sit (SOCK_RAW, SOCK_PACKET, TX_RING).

Willem de Bruijn (6):
net: ipa: remove ethernet-specific artifacts from modem netdev setup
net: remove header_ops->validate and dev_validate_header()
packet: validate TX header length before skb allocation
packet: reset network header on all headerless devices
packet: use Ethernet header length in vlan_get_tci()
net: remove unused net_device::min_header_len

Quoted 7 lines
  .../networking/net_cachelines/net_device.rst  |  1 -
  drivers/net/ipa/ipa_modem.c                   |  4 -
  drivers/net/loopback.c                        |  1 -
  include/linux/netdevice.h                     | 18 -----
  net/ethernet/eth.c                            |  1 -
  net/packet/af_packet.c                        | 80 ++++++++-----------
  6 files changed, 32 insertions(+), 73 deletions(-)

Sashiko reports are valid. I will respin

--

pw-bot: cr