All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willem de Bruijn <willemb@google.com>
To: netdev@vger.kernel.org
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	 kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	elder@kernel.org,  Willem de Bruijn <willemb@google.com>
Subject: [PATCH net-next v2 4/5] packet: use Ethernet header length in vlan_get_tci()
Date: Sun, 29 Nov 2026 11:30:39 -0500	[thread overview]
Message-ID: <20261129163327.893403-5-willemb@google.com> (raw)
In-Reply-To: <20261129163327.893403-1-willemb@google.com>

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.

Have vlan_get_tci() return whether a VLAN header was extracted, so
callers only set TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID when
the TCI is valid. This avoids setting false VLAN flags on non-Ethernet
devices, truncated frames, and TP_STATUS_COPY skbs queued by
tpacket_rcv() whose sockaddr_ll (including sll_hatype) is zeroed.

This changes the reported VLAN metadata 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 actual TCI.
- Non-Ethernet devices and frames without an extractable VLAN header
  previously still set TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID:
  headerless devices (e.g., tun, rmnet) matched 0 == 0 and read payload
  bytes at offset 0 as TCI, while other non-Ethernet devices and
  truncated frames reported TCI 0. They now omit VLAN metadata and flags.
- 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.

Signed-off-by: Willem de Bruijn <willemb@google.com>
---
 net/packet/af_packet.c | 65 ++++++++++++++++++------------------------
 1 file changed, 28 insertions(+), 37 deletions(-)

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 4ff7a2cc11e5..7b02fd6b63eb 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -547,34 +547,25 @@ 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 bool vlan_get_tci(const struct sk_buff *skb, unsigned short type,
+			 u16 *tci)
 {
 	struct vlan_hdr vhdr, *vh;
-	unsigned int header_len;
 
-	if (!dev)
-		return 0;
+	if (type != ARPHRD_ETHER && type != ARPHRD_LOOPBACK)
+		return false;
 
 	/* 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;
+		return false;
 
-	return ntohs(vh->h_vlan_TCI);
+	*tci = ntohs(vh->h_vlan_TCI);
+	return true;
 }
 
 static __be16 vlan_get_protocol_dgram(const struct sk_buff *skb)
@@ -1025,13 +1016,17 @@ static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
 			struct tpacket3_hdr *ppd)
 {
 	struct packet_sock *po = container_of(pkc, struct packet_sock, rx_ring.prb_bdqc);
+	u16 tci;
 
 	if (skb_vlan_tag_present(pkc->skb)) {
 		ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
 		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);
+	} else if (unlikely(po->sk.sk_type == SOCK_DGRAM &&
+			    eth_type_vlan(pkc->skb->protocol) &&
+			    vlan_get_tci(pkc->skb, pkc->skb->dev->type,
+					 &tci))) {
+		ppd->hv1.tp_vlan_tci = tci;
 		ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->protocol);
 		ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 	} else {
@@ -2294,6 +2289,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	__u32 ts_status;
 	unsigned int slot_id = 0;
 	int vnet_hdr_sz = 0;
+	u16 tci;
 
 	/* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
 	 * We may add members to them until current aligned size without forcing
@@ -2474,8 +2470,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 			h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
 			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);
+		} else if (unlikely(sk->sk_type == SOCK_DGRAM &&
+				    eth_type_vlan(skb->protocol) &&
+				    vlan_get_tci(skb, dev->type, &tci))) {
+			h.h2->tp_vlan_tci = tci;
 			h.h2->tp_vlan_tpid = ntohs(skb->protocol);
 			status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
 		} else {
@@ -3622,7 +3620,9 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 	}
 
 	if (packet_sock_flag(pkt_sk(sk), PACKET_SOCK_AUXDATA)) {
+		const struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
 		struct tpacket_auxdata aux;
+		u16 tci;
 
 		aux.tp_status = TP_STATUS_USER;
 		if (skb->ip_summed == CHECKSUM_PARTIAL)
@@ -3641,21 +3641,12 @@ static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 			aux.tp_vlan_tci = skb_vlan_tag_get(skb);
 			aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
 			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();
+		} else if (unlikely(sock->type == SOCK_DGRAM &&
+				    eth_type_vlan(skb->protocol) &&
+				    vlan_get_tci(skb, sll->sll_hatype, &tci))) {
+			aux.tp_vlan_tci = tci;
+			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;
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-29 16:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 16:30 [PATCH net-next v2 0/5] net: remove dev_validate_header() and net_device::min_header_len Willem de Bruijn
2026-11-29 16:30 ` [PATCH net-next v2 1/5] net: ipa: remove ethernet-specific artifacts from modem netdev setup Willem de Bruijn
2026-11-29 16:30 ` [PATCH net-next v2 2/5] net: remove header_ops->validate and dev_validate_header() Willem de Bruijn
2026-11-30 17:36   ` netdev-bot+sashiko
2026-11-29 16:30 ` [PATCH net-next v2 3/5] packet: reset network header on all headerless devices Willem de Bruijn
2026-11-30 17:36   ` netdev-bot+sashiko
2026-11-29 16:30 ` Willem de Bruijn [this message]
2026-11-30 17:36   ` [PATCH net-next v2 4/5] packet: use Ethernet header length in vlan_get_tci() netdev-bot+sashiko
2026-11-29 16:30 ` [PATCH net-next v2 5/5] net: remove unused net_device::min_header_len Willem de Bruijn
2026-12-02  1:46 ` [PATCH net-next v2 0/5] net: remove dev_validate_header() and net_device::min_header_len willemb

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=20261129163327.893403-5-willemb@google.com \
    --to=willemb@google.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=elder@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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.