From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214982; b=iDKaKaB2Wqf6GOeeIlCIUngUZp/Qgkvth1ahrnwrgdWW1ypTBPcg3xOJbrZn7WAdtQFU c9DrEnr+St9mR6zDKqyKGzBAAOWkI4+tb5vC+nUlx6xaC58YTprZ8mmIivTsyBQlLHlrr LDXrZsoeXKn+EQS45iL4VIC7IgKwgZ/z8zv37abYhgGxJES86qzpNul+gaxgrNfe44X/C iIfhp/CIezQlMjTPid2ht9kWkcAgvktuZymQAfuEqg3JcuwI5sJMtECIDzLIALZK++yG7 Dxyh7K+qK7PnwnisciUwf45VsQCQf+4ItrR7KeOLew4U87MlbZkoUdnAmovMRM8YSzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214982; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=0vXYkk8VAu0Gd/5tzo/waLTRq/YD6CMMhfT5Gmn7veA=; b=pGIoRQHx00b3rwMClQt2LVnP3JkD51KoSX3OUCgTnSEssUU/LzhQJLrbpp+dpo6OY4Sz /qHjKunjDdSbCszuvXdlDfNCM9a60Vaj5LAuHye6gjnPeO4YKMLNv/W5A09cpmFTTcZFE cx5LV3z4MPTm2GlXk/rBDYpOVCOISU8s2WKnzTDij/EUfsgLEO4tG0yptuMEQKrvg/2rV mBg4LioSxLC7f4qUCKHENG9zpUD/wXMqX2lmuAYS9E8UBle8pP0v7tr1xNzc+5lJF8tCo DGxJxTmONdBfRE+odcmVfBgOI+FhiCeylwaiPPRo+cOyZYdzmgMNZc6tL/92Y6C2RWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="Zxr3//Zu"; arc=none smtp.remote-ip=209.85.167.50 Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB818606A68B for ; Wed, 2 Dec 2026 12:36:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AB818606A68B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Zxr3//Zu Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-5c31369ae36so5567556e87.0 for ; Wed, 02 Dec 2026 04:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796214980; x=1796819780; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=0vXYkk8VAu0Gd/5tzo/waLTRq/YD6CMMhfT5Gmn7veA=; b=Zxr3//ZuPzgTNIqF3lCnRKCr2r/UoZYWerxHmFdTe/LnE+HesEP/S7X5QnmDWt8e4H 5mcJNxoVqO4raobcI23aFaRNGnqu1cYwrf5Sa0/FVlaAyZl7U9rof6eY8eROJMTXr7NB GMei6wFAX+LEcHe94NoQmxQOXaLe9o6XiXzjJRApyU/UXplZMB23N5zJ9l/N9V7dM9qn FgZauWYuMG3Kb/8lfRqZh94shYB7jOtweU8WbCyqeniaYsFXO5sZyL+XW/BcjK9GCEmV Pnx1lk6JEOpdWpQP/MQjqyu7rSYPrOgNU9/dMZQ59DwfNYtvMSC4pM5WCvVu/GSwDgba C7dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796214980; x=1796819780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=0vXYkk8VAu0Gd/5tzo/waLTRq/YD6CMMhfT5Gmn7veA=; b=ldj9B5IXAbsEwvDP6fhhVBdduIJDrjC7Mu9n3xO3Ge1OnKKf99EjaJgXsPEMRTWa0m g56ZkAjqwKWohGu2IsyUroqHFi2ew4hJH2CKyGDN5TRBNv8A8kAEQvlUSz50n3wzUZjT LUlaGTcCcqhhDwTnnkxvx53n1vdQlpSpC9p3oZgrixlOGR8UGn+UVaxIqFvt6aC6b0QE RyZUFVmW86OMk/h/H+c7go11LoTb3GXpcVTEqIT67G79bJHEz3LySp7dOhhjIEllHs0N 5kbmMrBufSGjZTf//AjFVM8X9xVQEtqYY5ALm+i+ymrl9tJ+nlk26ZYeeZBfuN+KzmSd Ee8g== X-Forwarded-Encrypted: i=1; AL+hErFZGsl4G5q1vi0a2ntea4344fZS+7V00goLVXTkPMl7EGtrZF7XoQICpBCLHN88jrKn27Ksfx/0Q2vV/7g=@vger.kernel.org X-Gm-Message-State: APuU4Z8UKnvZgYQqi1yymF4jBeNzyy8WpkeslBJLCt5ETDrGF0NFyZ2W soEsXXr9IUOzG9WaHjKr3yF6CfpC/o+KOvRfDgLr5M8dMdRx2keRIPdj X-Gm-Gg: AZFdTZc4vFNADgAN5qmj5chGBnll49IG5IFgmDIebTSp1hKsdy47U31HXEDg4dMCNsY R7sQPcc8HP2RluBuYz5E8RSAtBwld8y7lkhsOzQeGnTVaT9EjUBriEyKCs8AmYihkQDtHoKfxQJ dbEbD/kBtb98s9hnACAjSsnQt3TAK2N1CsNd7/pmG4cnlk+FWdvYYZa9Py1CfM6vyu2cbcB05IU 4QzsTeRnwzpYOkhxNaUQMUpNw3vpHP4fgv+HVcDxQr7rvApeMwb+EA1mbqm8LljQ4dS7Kvmy9e1 QIQRYNschKnNuqWvszRAyla4GocX5PfzNWKXBLLC+B03esCjmQzMrXBYfgIZ2iZ0oJ5E/jF/AcC mMnjEZmKJE/gbhRK2uN31+PDksmpUxWHnGRGBgyR/LtMhzrCja71FKL1Zhj2sd0c6/PxNLy11DW 7arDS7dUTm7sL2fS+QBHa9m1xYuXy6/SLyDBhWwycrL+Qjhuu2z7ZxqfFWok9aKZDn8oFcdbz1v /fuBgvMXTxZ9Q3AybIB3baZHIqQVuVDqQbl55y5zA2bwlc9tZ/WfFJUOn1hWDbyxpL85utVyeBE 1DpeIbl8nX0y1aQp5U/gx13szTuM/qwbG1dp/67BaHrYavQBjNP69XiBNFYTuE5PFx8cwWyWze6 TKEAFQ/F9/2O14Kk1 X-Received: by 2002:a05:6512:1282:b0:5c3:3e01:dad0 with SMTP id 2adb3069b0e04-5c33e01dc9bmr5754897e87.51.1796214979809; Wed, 02 Dec 2026 04:36:19 -0800 (PST) Received: from dau-home-pc.megasoftware.org ([94.28.149.75]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5c1bd0171c7sm9529553e87.31.2026.12.02.04.36.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 04:36:19 -0800 (PST) From: Anton Danilov To: netdev@vger.kernel.org Cc: Eric Dumazet , Jakub Kicinski , Paolo Abeni , "David S. Miller" , Simon Horman , David Ahern , Willem de Bruijn , Ido Schimmel , Florian Westphal , William Tu , Meenakshi Vohra , Matthias May , Mazin Al Haddad , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v8 2/5] erspan: take the COS from the mirrored frame, not from the tunnel family Date: Wed, 2 Dec 2026 15:36:10 +0300 Message-ID: <20261202123613.1708463-3-littlesmilingcloud@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261202123613.1708463-1-littlesmilingcloud@gmail.com> References: <20261202123613.1708463-1-littlesmilingcloud@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit erspan_build_header() and erspan_build_header_v2() take the TOS for the COS field from ip_hdr(skb)->tos when the tunnel is IPv4 and from the IPv6 traffic class when the tunnel is IPv6. But is_ipv4 describes the tunnel, not the mirrored frame, so - an IPv4 frame mirrored to ip6erspan gets its COS from the IPv4 version/IHL byte and the high TOS nibble: always 2, whatever the DSCP; - an IPv6 frame mirrored to erspan gets its COS from the low nibble of the traffic class: 4 instead of 5 for EF; - a frame that is neither gets its COS from whatever lies at the network header offset, which for a short frame is past its end. Pick the IP version by the frame instead: by skb->protocol or, when that is a VLAN type, by the version field at the network header. Read the two bytes that hold the TOS or traffic class with skb_header_pointer(). Return COS 0 if those bytes are unavailable or the protocol is not recognized as IPv4 or IPv6 by this rule. Keep the existing DSCP-to-COS mapping. This change bounds the reads and removes the tunnel family from the interpretation of the inner IP header; it does not introduce a new QoS policy based on VLAN PCP, MPLS TC or skb->priority. The latter is a queueing priority, not necessarily a three-bit class of service, and would need an explicit mapping. For skb->protocol carrying a VLAN type, the version field is only a heuristic, not validation of the encapsulated protocol. A non-IP payload such as an MPLS label can begin with the same nibble as IPv4 or IPv6 and still produce a nonzero COS. Its COS can also differ from the old tunnel-family-based result. Classifying arbitrary non-IP VLAN payloads is outside this change; no COS 0 guarantee is made for them. The two-byte read is bounded regardless of that classification. The TOS is still read at the network header. That is where the stack, tc mirred and packet sockets leave the IP header, also for frames that do not start with an Ethernet header and its tags, such as a packet mirrored from an L3 device or a frame behind a switch tag mirrored from a DSA conduit. Where the network header is not on the IP header the COS stays wrong, as do the other fields read there; one such case is a tagged frame written by a packet socket under an IP sll_protocol, whose network header is on the tag. Drop the is_ipv4 argument, and with it the stale forward declaration of erspan_build_header() in ip_gre.c, which would have had to follow. The wrong COS and the read past the end were found by an AI code review agent, in a review of another series that touches these functions, and confirmed in a VM on an unmodified kernel: - EF (DSCP 46) frames mirrored with tc from a veth give COS 2 for IPv4 over ip6erspan v1 and v2, COS 4 for IPv6 over erspan v1 and v2, and COS 5 in the other four combinations. With the patch all eight give COS 5. So do the same frames with the EtherType at offset 12 overwritten by tc pedit as a switch tag would, sent through a VLAN device, and IP packets mirrored from a tun device: 48 cases in all; - a 14 byte non-IP frame gives a KMSAN report when the packet is received on the other side; with the patch it does not. Signed-off-by: Anton Danilov --- include/net/erspan.h | 53 ++++++++++++++++++++++++++++++++++++-------- net/ipv4/ip_gre.c | 13 ++++------- net/ipv6/ip6_gre.c | 10 ++++----- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/include/net/erspan.h b/include/net/erspan.h index 6bda757b6da2..c272b3fb9bfd 100644 --- a/include/net/erspan.h +++ b/include/net/erspan.h @@ -58,9 +58,11 @@ * GRE proto ERSPAN type I/II = 0x88BE, type III = 0x22EB */ +#include #include #include #include +#include #include #define ERSPAN_VERSION 0x1 /* ERSPAN type II */ @@ -170,9 +172,46 @@ static inline u8 tos_to_cos(u8 tos) return cos; } +/* TOS of the mirrored frame, for the COS field, read at the network + * header. The IP version comes from skb->protocol or, for a VLAN type, + * from the version field there, and not from the tunnel family. The + * bytes are read with skb_header_pointer(), so a frame that does not + * have them gets 0. + */ +static inline u8 erspan_frame_tos(const struct sk_buff *skb) +{ + /* Align the two-byte buffer as struct iphdr, since + * skb_header_pointer() may return its address as iph. + */ + __be16 _prefix __aligned(__alignof__(struct iphdr)); + int offset = skb_network_offset(skb); + __be16 type = skb->protocol; + const struct iphdr *iph; + + if (offset < 0) + return 0; + + /* Version and DS field fit in the first 16 bits of either IP header. */ + iph = skb_header_pointer(skb, offset, sizeof(_prefix), &_prefix); + if (!iph) + return 0; + + if (eth_type_vlan(type)) { + if (iph->version == 4) + type = htons(ETH_P_IP); + else if (iph->version == 6) + type = htons(ETH_P_IPV6); + } + + if (type == htons(ETH_P_IP)) + return iph->tos; + if (type == htons(ETH_P_IPV6)) + return ipv6_get_dsfield((const struct ipv6hdr *)iph); + return 0; +} + static inline void erspan_build_header(struct sk_buff *skb, - u32 id, u32 index, - bool truncate, bool is_ipv4) + u32 id, u32 index, bool truncate) { enum erspan_encap_type enc_type; struct erspan_base_hdr *ershdr; @@ -184,9 +223,7 @@ static inline void erspan_build_header(struct sk_buff *skb, u8 tos; __be32 *idx; - tos = is_ipv4 ? ip_hdr(skb)->tos : - (ipv6_hdr(skb)->priority << 4) + - (ipv6_hdr(skb)->flow_lbl[0] >> 4); + tos = erspan_frame_tos(skb); enc_type = ERSPAN_ENCAP_NOVLAN; @@ -265,7 +302,7 @@ static inline u8 erspan_detect_bso(struct sk_buff *skb) static inline void erspan_build_header_v2(struct sk_buff *skb, u32 id, u8 direction, u16 hwid, - bool truncate, bool is_ipv4) + bool truncate) { struct erspan_base_hdr *ershdr; struct erspan_md2 *md2; @@ -279,9 +316,7 @@ static inline void erspan_build_header_v2(struct sk_buff *skb, u8 sgt = 0; u8 tos; - tos = is_ipv4 ? ip_hdr(skb)->tos : - (ipv6_hdr(skb)->priority << 4) + - (ipv6_hdr(skb)->flow_lbl[0] >> 4); + tos = erspan_frame_tos(skb); /* Unlike v1, v2 does not have En field, * so only extract vlan tci field. diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index 745bd12dcd68..7e8606074979 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -111,9 +111,6 @@ static struct rtnl_link_ops ipgre_link_ops __read_mostly; static const struct header_ops ipgre_header_ops; static int ipgre_tunnel_init(struct net_device *dev); -static void erspan_build_header(struct sk_buff *skb, - u32 id, u32 index, - bool truncate, bool is_ipv4); static unsigned int ipgre_net_id __read_mostly; static unsigned int gre_tap_net_id __read_mostly; @@ -630,14 +627,14 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev) if (version == 1) { erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)), - ntohl(md->u.index), truncate, true); + ntohl(md->u.index), truncate); proto = htons(ETH_P_ERSPAN); } else if (version == 2) { erspan_build_header_v2(skb, ntohl(tunnel_id_to_key32(key->tun_id)), md->u.md2.dir, get_hwid(&md->u.md2), - truncate, true); + truncate); proto = htons(ETH_P_ERSPAN2); } else { reason = SKB_DROP_REASON_UNHANDLED_PROTO; @@ -790,13 +787,11 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb, __clear_bit(IP_TUNNEL_SEQ_BIT, flags); } else if (tunnel->erspan_ver == 1) { erspan_build_header(skb, ntohl(tunnel->parms.o_key), - tunnel->index, - truncate, true); + tunnel->index, truncate); proto = htons(ETH_P_ERSPAN); } else if (tunnel->erspan_ver == 2) { erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key), - tunnel->dir, tunnel->hwid, - truncate, true); + tunnel->dir, tunnel->hwid, truncate); proto = htons(ETH_P_ERSPAN2); } else { reason = SKB_DROP_REASON_UNHANDLED_PROTO; diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c index 38c5c519986d..51f4311bebfa 100644 --- a/net/ipv6/ip6_gre.c +++ b/net/ipv6/ip6_gre.c @@ -1019,15 +1019,14 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, if (md->version == 1) { erspan_build_header(skb, ntohl(tun_id), - ntohl(md->u.index), truncate, - false); + ntohl(md->u.index), truncate); proto = htons(ETH_P_ERSPAN); } else if (md->version == 2) { erspan_build_header_v2(skb, ntohl(tun_id), md->u.md2.dir, get_hwid(&md->u.md2), - truncate, false); + truncate); proto = htons(ETH_P_ERSPAN2); } else { goto tx_err; @@ -1053,14 +1052,13 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, if (t->parms.erspan_ver == 1) { erspan_build_header(skb, ntohl(t->parms.o_key), - t->parms.index, - truncate, false); + t->parms.index, truncate); proto = htons(ETH_P_ERSPAN); } else if (t->parms.erspan_ver == 2) { erspan_build_header_v2(skb, ntohl(t->parms.o_key), t->parms.dir, t->parms.hwid, - truncate, false); + truncate); proto = htons(ETH_P_ERSPAN2); } else { goto tx_err; -- 2.47.3