mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anton Danilov <littlesmilingcloud@gmail.com>
To: netdev@vger.kernel.org
Cc: Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	Simon Horman <horms@kernel.org>, David Ahern <dsahern@kernel.org>,
	Willem de Bruijn <willemb@google.com>,
	Ido Schimmel <idosch@nvidia.com>, Florian Westphal <fw@strlen.de>,
	William Tu <u9012063@gmail.com>,
	Meenakshi Vohra <mvohra@vmware.com>,
	Matthias May <matthias.may@westermo.com>,
	Mazin Al Haddad <mazin@getstate.dev>,
	Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net-next v7 3/5] ip6_gre: classify the inner packet once on transmit
Date: Tue,  1 Dec 2026 03:59:39 +0300	[thread overview]
Message-ID: <20261201005941.1536429-4-littlesmilingcloud@gmail.com> (raw)
In-Reply-To: <20261201005941.1536429-1-littlesmilingcloud@gmail.com>

ip6gre_tunnel_xmit() and ip6erspan_tunnel_xmit() work out what the
encapsulated packet carries in three places, each by its own rule:

 - the length check, pskb_inet_may_pull(), goes by skb->protocol and
   makes sure of an IPv4 or IPv6 header at the network header;
 - the dispatch in ip6gre_tunnel_xmit() uses skb_protocol(skb, true),
   which looks through VLAN tags from skb->data, at a depth taken from
   skb->mac_len;
 - ip6_tnl_xmit() calls skb_protocol(skb, true) again for the hop limit
   to inherit, after the GRE and ERSPAN headers have been pushed, so
   this walk starts inside those headers.

For a VLAN-tagged frame the three disagree:

 - the check sees a VLAN type and pulls nothing, while the dispatch
   finds IPv4 or IPv6 behind the tag and reads the inner header anyway.
   A tagged frame too short for that header has it read from past its
   end and is sent, while an untagged one is dropped (the syzbot report
   below);
 - the walk in ip6_tnl_xmit() lines up with the inner EtherType only on
   ip6gretap with a 4 byte GRE header and skb->mac_len 0 or 18. With a
   key, a checksum, a sequence number, another mac_len, or on
   ip6erspan, it reads MAC address or ERSPAN bytes instead: the hop
   limit is then not inherited, or, when those bytes read as ETH_P_IP
   or ETH_P_IPV6, taken from wherever the network header points,
   whatever the frame carries;
 - skb->mac_len is no guide here: on transmit it is whatever the device
   the skb was received on left, 14 forwarded, 18 from a VLAN device
   with reorder_hdr off, 24 from an NBMA gre device, 0 for a local one.

Classify the packet once, on entry, and pass the result down:

 - a non-VLAN skb->protocol is taken as it is, with the network header
   where the sender left it and the same length check as before;
 - a VLAN skb->protocol on ip6gretap and ip6erspan is resolved from the
   frame, starting with the EtherType at offset 12 from skb->data,
   where __vlan_get_protocol() starts once skb->mac_len is ETH_HLEN.
   When the tags lead to IPv4 or IPv6, that header is pulled and the
   network header is set on it, and a frame too short for it is dropped
   as an untagged one is. Any other frame gets the same check as before
   and is sent untouched, with no field taken from it;
 - ip6gre has no Ethernet header at skb->data, so no tags are looked
   for there and a VLAN skb->protocol is a protocol like any other.

ip6_tnl_xmit() takes the result as a new argument and no longer parses
the packet. ip6_tunnel's own devices pass skb->protocol: they send only
IPv4, IPv6 and MPLS, for which skb_protocol() returned just that.

What changes for traffic that was not broken before:

 - the hop limit of a tagged frame is inherited on every ip6gretap and
   ip6erspan device, not only on ip6gretap with a 4 byte GRE header and
   mac_len 0 or 18;
 - a frame that arrives with its network header on a VLAN tag (after tc
   act_ife decode, a generic XDP redirect, or tc act_vlan push on an
   already tagged frame) gets its traffic class from the IP header, not
   from the TCI;
 - a VLAN-typed skb on ip6gre, such as an Ethernet frame forwarded to
   it by nft fwd, no longer has its traffic class taken through the tag;
   its GRE protocol stays the VLAN type, with an Ethernet or mangled IP
   header behind it, before and after.

The length check mismatch was reported by syzbot, the walk after the
push by the Sashiko review of v5, and the role of skb->mac_len by an AI
code review agent on an earlier version. All were confirmed in a VM on
an unmodified kernel:

 - with KMSAN, an 18 byte tagged ARP frame written by a packet socket
   to a keyed ip6gretap or an ip6erspan device, with bytes that read as
   ETH_P_IP where the second walk lands, goes out with a hop limit from
   past its end; with the patch it goes out with the route's hop limit
   and the reports are gone;
 - a 20 byte tagged IPv4 frame on a keyed ip6gretap is sent before the
   patch and dropped with it;
 - IPv4 packets with TTL 33 and hoplimit inherit were sent to ip6gretap
   with and without a key and to ip6erspan v1 and v2, in 35 ways: local
   stack, VLAN devices with reorder_hdr on and off, a bridge, tc mirred
   from ingress and egress, bpf and generic XDP redirect, nft fwd,
   packet sockets, act_vlan, act_pedit, act_ife, and a tun device.
   Before the patch the tagged frames that reach the device with a VLAN
   skb->protocol go out with hop limit 33 only on ip6gretap without a
   key and only with skb->mac_len 0, 64 otherwise; with the patch all
   go out with 33 on all four devices. Frames that reach with an IP
   skb->protocol are unchanged.

gre_gso.sh, l2_tos_ttl_inherit.sh, ip6_gre_headroom.sh, ip6_tunnel.sh
and the mirror_gre and ip6gre forwarding selftests give the same
results before and after.

Fixes: 3f8a8447fd0b ("ip6_gre: use actual protocol to select xmit")
Fixes: b09ab9c92e50 ("ip6_tunnel: allow to inherit from VLAN encapsulated IP")
Reported-by: syzbot+6023ea32e206eef7920a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=6023ea32e206eef7920a
Suggested-by: Eric Dumazet <edumazet@kernel.org>
Signed-off-by: Anton Danilov <littlesmilingcloud@gmail.com>
---
 include/net/ip6_tunnel.h |  3 +-
 net/ipv6/ip6_gre.c       | 77 ++++++++++++++++++++++++++++++++++------
 net/ipv6/ip6_tunnel.c    |  9 +++--
 3 files changed, 72 insertions(+), 17 deletions(-)

diff --git a/include/net/ip6_tunnel.h b/include/net/ip6_tunnel.h
index d1f0a427e9c8..50caf745a61b 100644
--- a/include/net/ip6_tunnel.h
+++ b/include/net/ip6_tunnel.h
@@ -144,7 +144,8 @@ int ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
 int ip6_tnl_xmit_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
 		     const struct in6_addr *raddr);
 int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
-		 struct flowi6 *fl6, int encap_limit, __u32 *pmtu, __u8 proto);
+		 struct flowi6 *fl6, int encap_limit, __u32 *pmtu, __u8 proto,
+		 __be16 payload_protocol);
 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw);
 __u32 ip6_tnl_get_cap(struct ip6_tnl *t, const struct in6_addr *laddr,
 			     const struct in6_addr *raddr);
diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c
index d8e239a96841..d7d7fd478200 100644
--- a/net/ipv6/ip6_gre.c
+++ b/net/ipv6/ip6_gre.c
@@ -730,7 +730,8 @@ static struct ip_tunnel_info *skb_tunnel_info_txcheck(struct sk_buff *skb)
 static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
 			       struct net_device *dev, __u8 dsfield,
 			       struct flowi6 *fl6, int encap_limit,
-			       __u32 *pmtu, __be16 proto)
+			       __u32 *pmtu, __be16 proto,
+			       __be16 payload_protocol)
 {
 	struct ip6_tnl *tunnel = netdev_priv(dev);
 	IP_TUNNEL_DECLARE_FLAGS(flags);
@@ -797,7 +798,7 @@ static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
 	}
 
 	return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
-			    NEXTHDR_GRE);
+			    NEXTHDR_GRE, payload_protocol);
 }
 
 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
@@ -821,7 +822,7 @@ static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
 		return -1;
 
 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
-			  skb->protocol);
+			  skb->protocol, htons(ETH_P_IP));
 	if (err != 0) {
 		/* XXX: send ICMP error even if DF is not set. */
 		if (err == -EMSGSIZE)
@@ -860,7 +861,7 @@ static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
 		return -1;
 
 	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
-			  &mtu, skb->protocol);
+			  &mtu, skb->protocol, htons(ETH_P_IPV6));
 	if (err != 0) {
 		if (err == -EMSGSIZE)
 			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
@@ -870,7 +871,8 @@ static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
 	return 0;
 }
 
-static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
+static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev,
+			     __be16 payload_protocol)
 {
 	struct ip6_tnl *t = netdev_priv(dev);
 	int encap_limit = -1;
@@ -887,11 +889,64 @@ static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
 						t->parms.o_flags));
 	if (err)
 		return err;
-	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, skb->protocol);
+	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
+			  skb->protocol, payload_protocol);
 
 	return err;
 }
 
+/* Classify the payload once, on entry to ndo_start_xmit, and make sure
+ * that the header the answer stands for is in the linear area.  Nothing
+ * below parses the frame again: the answer is passed down, also to
+ * ip6_tnl_xmit().  It is ETH_P_IP or ETH_P_IPV6 only when that header is
+ * at the network header.
+ *
+ * If skb->protocol is not a VLAN type, it and the network header are
+ * taken as the sender left them.
+ *
+ * If it is a VLAN type on an Ethernet device, the payload type is in the
+ * frame, behind the tags, and is read from there, starting with the
+ * EtherType at offset 12 from skb->data: that is where
+ * __vlan_get_protocol() starts when skb->mac_len is ETH_HLEN.  The
+ * skb->mac_len the skb comes with is no guide, it is what the device it
+ * was received on left there, or 0.  When the tags lead to IPv4 or IPv6,
+ * that header is pulled and the network header is set on it.  Any other
+ * frame gets the same check as before and is sent as it is, with the
+ * network header left alone, and no field is taken from it.
+ *
+ * ip6gre has no Ethernet header at skb->data to find tags in, so a VLAN
+ * skb->protocol there is a protocol like any other.
+ */
+static enum skb_drop_reason
+ip6gre_xmit_classify(struct sk_buff *skb, const struct net_device *dev,
+		     __be16 *payload_protocol)
+{
+	enum skb_drop_reason reason;
+	int depth, nhlen;
+	__be16 type;
+
+	*payload_protocol = skb->protocol;
+	if (dev->type != ARPHRD_ETHER || !eth_type_vlan(skb->protocol))
+		return pskb_inet_may_pull_reason(skb);
+
+	skb->mac_len = ETH_HLEN;
+	type = __vlan_get_protocol(skb, skb->protocol, &depth);
+	if (type == htons(ETH_P_IP))
+		nhlen = sizeof(struct iphdr);
+	else if (type == htons(ETH_P_IPV6))
+		nhlen = sizeof(struct ipv6hdr);
+	else
+		return pskb_inet_may_pull_reason(skb);
+
+	reason = pskb_may_pull_reason(skb, depth + nhlen);
+	if (reason)
+		return reason;
+
+	skb_set_network_header(skb, depth);
+	*payload_protocol = type;
+	return SKB_NOT_DROPPED_YET;
+}
+
 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
 	struct net_device *dev)
 {
@@ -900,7 +955,7 @@ static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
 	__be16 payload_protocol;
 	int ret;
 
-	if (!pskb_inet_may_pull(skb))
+	if (ip6gre_xmit_classify(skb, dev, &payload_protocol))
 		goto tx_err;
 
 	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
@@ -909,7 +964,6 @@ static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
 	if (t->parms.collect_md)
 		tun_info = skb_tunnel_info_txcheck(skb);
 
-	payload_protocol = skb_protocol(skb, true);
 	switch (payload_protocol) {
 	case htons(ETH_P_IP):
 		ret = ip6gre_xmit_ipv4(skb, dev);
@@ -918,7 +972,7 @@ static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
 		ret = ip6gre_xmit_ipv6(skb, dev);
 		break;
 	default:
-		ret = ip6gre_xmit_other(skb, dev);
+		ret = ip6gre_xmit_other(skb, dev, payload_protocol);
 		break;
 	}
 
@@ -941,6 +995,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
 	struct ip_tunnel_info *tun_info = NULL;
 	struct ip6_tnl *t = netdev_priv(dev);
 	IP_TUNNEL_DECLARE_FLAGS(flags) = { };
+	__be16 payload_protocol;
 	bool truncate = false;
 	int encap_limit = -1;
 	__u8 dsfield = false;
@@ -950,7 +1005,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
 	__u32 mtu;
 	int nhoff;
 
-	if (!pskb_inet_may_pull(skb))
+	if (ip6gre_xmit_classify(skb, dev, &payload_protocol))
 		goto tx_err;
 
 	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
@@ -1073,7 +1128,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
 			 htonl(atomic_fetch_inc(&t->o_seqno)));
 
 	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
-			   NEXTHDR_GRE);
+			   NEXTHDR_GRE, payload_protocol);
 	if (err != 0) {
 		/* XXX: send ICMP error even if DF is not set. */
 		if (err == -EMSGSIZE) {
diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c
index 4912b08228a1..868c49b97a8f 100644
--- a/net/ipv6/ip6_tunnel.c
+++ b/net/ipv6/ip6_tunnel.c
@@ -1107,6 +1107,8 @@ EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  *   @encap_limit: encapsulation limit
  *   @pmtu: Path MTU is stored if packet is too big
  *   @proto: next header value
+ *   @payload_protocol: protocol of the inner packet; ETH_P_IP or ETH_P_IPV6
+ *	only if that header is at the network header, in the linear area
  *
  * Description:
  *   Build new header and do some sanity checks on the packet before sending
@@ -1120,7 +1122,7 @@ EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
 
 int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
 		 struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
-		 __u8 proto)
+		 __u8 proto, __be16 payload_protocol)
 {
 	struct ip6_tnl *t = netdev_priv(dev);
 	struct net *net = t->net;
@@ -1132,13 +1134,10 @@ int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
 	unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
 	unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
 	unsigned int max_headroom = psh_hlen;
-	__be16 payload_protocol;
 	bool use_cache = false;
 	u8 hop_limit;
 	int err = -1;
 
-	payload_protocol = skb_protocol(skb, true);
-
 	if (t->parms.collect_md) {
 		hop_limit = skb_tunnel_info(skb)->key.ttl;
 		goto route_lookup;
@@ -1424,7 +1423,7 @@ ipxip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev,
 	skb_set_inner_ipproto(skb, protocol);
 
 	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
-			   protocol);
+			   protocol, skb->protocol);
 	if (err != 0) {
 		/* XXX: send ICMP error even if DF is not set. */
 		if (err == -EMSGSIZE)
-- 
2.47.3


  parent reply	other threads:[~2026-12-01  0:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  0:59 [PATCH net-next v7 0/5] ip6_gre, erspan: fix inner header handling for VLAN-tagged and short frames Anton Danilov
2026-12-01  0:59 ` [PATCH net-next v7 1/5] erspan: bound the VLAN tag read in erspan_build_header{,_v2}() Anton Danilov
2026-12-01  0:59 ` [PATCH net-next v7 2/5] erspan: take the COS from the mirrored frame, not from the tunnel family Anton Danilov
2026-12-01  0:59 ` Anton Danilov [this message]
2026-12-01  0:59 ` [PATCH net-next v7 4/5] ip6_gre: go by the inner protocol behind VLAN tags in ip6erspan Anton Danilov
2026-12-01  0:59 ` [PATCH net-next v7 5/5] selftests: net: add a test for ip6gretap and ip6erspan with VLAN tags Anton Danilov

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=20261201005941.1536429-4-littlesmilingcloud@gmail.com \
    --to=littlesmilingcloud@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=matthias.may@westermo.com \
    --cc=mazin@getstate.dev \
    --cc=mvohra@vmware.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=u9012063@gmail.com \
    --cc=willemb@google.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

all inboxes | Powered by JetHome®