🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26094

[PATCH RFC net v10 5/6] net: virtio: compute the outer checksum of non-GSO tunnel packets

#26094
From: Showta Ishizaki
Date: Thu, 03 Dec 2026 01:53
186 lines
14113 bytes
A packet may be tagged as UDP tunnel GSO and still be too small to
segment. Then nothing consumes the pseudo-header seed the sender left in
the outer UDP checksum. The GSO layer is not run, and transmit checksum
offload completes only the inner checksum, so the seed goes out as the
outer checksum.

Zeroing it is not an option either: IPv6 receivers drop a zero UDP
checksum unless the tunnel opted in (RFC 6935, RFC 6936).

Compute the outer checksum when VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM asks
for one: with local checksum offload while the inner checksum is still
partial, or by a full sum once the inner one was completed in software
because its metadata did not match the packet. Such packets no longer
need to be dropped.

The addresses come from the outer header in the frame and the length
from the UDP header. udp_set_csum() is not used: it is built with
CONFIG_INET, which virtio_net does not depend on.

Fixes: a2fb4bc4e2a6 ("net: implement virtio helpers to handle UDP GSO tunneling.")
Suggested-by: Willem de Bruijn <willemb@google.com>
Suggested-by: Chris Arges <carges@cloudflare.com>
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 include/linux/virtio_net.h | 113 ++++++++++++++++++++++++++++++++++---
 1 file changed, 106 insertions(+), 7 deletions(-)

diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index 714b20acd38f..6fe0c5921717 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -8,8 +8,10 @@
 #include <linux/netdevice.h>
 #include <linux/udp.h>
 #include <net/ip.h>
+#include <net/ip6_checksum.h>
 #include <net/ipv6.h>
 #include <net/tcp.h>
+#include <net/udp.h>
 #include <uapi/linux/tcp.h>
 #include <uapi/linux/virtio_net.h>
 
@@ -553,6 +555,72 @@ virtio_net_tnl_offsets_ok(struct sk_buff *skb, bool has_eth, bool outer_isv6,
 	return ip_proto == IPPROTO_UDP && outer_th == thoff;
 }
 
+/* Fill in the outer UDP checksum of a tunnel packet that is not GSO.
+ * While the inner checksum is still CHECKSUM_PARTIAL this is local
+ * checksum offload, as udp_set_csum() does for udp_tunnel_xmit_skb(),
+ * and needs the transport header at @outer_th.  Once the inner
+ * checksum has been completed the datagram is summed in full.
+ *
+ * The pseudo-header comes from the frame, not from the seed the sender
+ * put in uh->check: the addresses of the outer IP header and the
+ * length of the UDP header, which skb->len exceeds when the frame is
+ * padded.
+ *
+ * udp_set_csum() itself is not used: it is built with CONFIG_INET,
+ * which virtio_net does not depend on.
+ */
+static inline int
+virtio_net_tnl_set_outer_csum(struct sk_buff *skb, bool has_eth,
+			      bool outer_isv6, unsigned int outer_th)
+{
+	unsigned int thoff;
+	struct udphdr *uh;
+	int ip_proto, nhoff;
+	__be16 proto;
+	__wsum csum;
+	int len, err;
+
+	/* virtio_net_tnl_offsets_ok() has passed; this finds nhoff. */
+	if (!virtio_net_l3_start(skb, has_eth, &nhoff, &proto) ||
+	    !virtio_net_l4_start(skb, nhoff, proto, &thoff, &ip_proto))
+		return -EINVAL;
+
+	err = skb_ensure_writable(skb, outer_th + sizeof(*uh));
+	if (err)
+		return err;
+
+	uh = (struct udphdr *)(skb->data + outer_th);
+	len = ntohs(uh->len);
+	uh->check = 0;
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		csum = lco_csum(skb);
+	} else {
+		/* Sum the datagram alone: uh->len, not skb->len - outer_th,
+		 * which also counts non-zero Ethernet padding and so
+		 * miscomputes the outer checksum.  len is from the frame,
+		 * so bound it before reading that many bytes.
+		 */
+		if (len < sizeof(*uh) || len > skb->len - outer_th)
+			return -EINVAL;
+		csum = skb_checksum(skb, outer_th, len, 0);
+	}
+
+	if (outer_isv6) {
+		const struct ipv6hdr *ip6h = (void *)(skb->data + nhoff);
+
+		uh->check = udp_v6_check(len, &ip6h->saddr, &ip6h->daddr,
+					 csum);
+	} else {
+		const struct iphdr *iph = (void *)(skb->data + nhoff);
+
+		uh->check = udp_v4_check(len, iph->saddr, iph->daddr, csum);
+	}
+	if (uh->check == 0)
+		uh->check = CSUM_MANGLED_0;
+
+	return 0;
+}
+
 /*
  * Return value:
  * 0 - success
@@ -568,6 +636,7 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 			  bool little_endian)
 {
 	const struct virtio_net_hdr *hdr = (const struct virtio_net_hdr *)vhdr;
+	typeof(skb->transport_header) old_transport = skb->transport_header;
 	unsigned int inner_nh, outer_th, inner_th;
 	unsigned int inner_l3min, outer_l3min;
 	u8 gso_inner_type, gso_tunnel_type;
@@ -639,15 +708,26 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 
 	inner_proto = inner_isv6 ? htons(ETH_P_IPV6) : htons(ETH_P_IP);
 
-	/* NEEDS_CSUM is set, so this is CHECKSUM_PARTIAL.  A packet whose
-	 * inner metadata does not match cannot be segmented, and completing
-	 * the inner checksum alone would leave the outer one to the seed
-	 * the sender put there.
-	 */
+	/* NEEDS_CSUM is set, so this is CHECKSUM_PARTIAL. */
 	if (unlikely(!virtio_net_csum_partial_ok_at_offset(skb, gso_inner_type,
 							   inner_nh,
-							   inner_proto)))
-		return -EINVAL;
+							   inner_proto))) {
+		/* __virtio_net_hdr_to_skb set the transport header to
+		 * csum_start, restore it to a known good value.
+		 */
+		skb->transport_header = old_transport;
+
+		ret = __virtio_net_resolve_csum_partial_mismatch(skb);
+		if (ret > 0 && hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
+			int err = virtio_net_tnl_set_outer_csum(skb, has_eth,
+								outer_isv6,
+								outer_th);
+
+			if (err)
+				return err;
+		}
+		return ret;
+	}
 
 	skb_set_inner_protocol(skb, inner_proto);
 	if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
@@ -661,6 +741,25 @@ virtio_net_hdr_tnl_to_skb(struct sk_buff *skb,
 	skb->inner_mac_header = inner_nh + skb_headroom(skb);
 	skb->transport_header = outer_th + skb_headroom(skb);
 	skb->encapsulation = 1;
+
+	if (!skb_is_gso(skb)) {
+		/* Too small to segment.  Senders do tag small packets with
+		 * GSO headers, e.g. packetdrill sets gso_type and gso_size
+		 * with --mss, whatever the packet size.  Nothing consumes
+		 * the seed the sender left in the outer uh->check without
+		 * GSO, so compute the outer checksum if one was asked for.
+		 */
+		skb_shinfo(skb)->gso_type &= ~(SKB_GSO_UDP_TUNNEL_CSUM |
+					       SKB_GSO_UDP_TUNNEL);
+		if (hdr->flags & VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM) {
+			ret = virtio_net_tnl_set_outer_csum(skb, has_eth,
+							    outer_isv6,
+							    outer_th);
+			if (ret)
+				return ret;
+		}
+	}
+
 	return 0;
 }
 
-- 
2.54.0 (Apple Git-157)
Technical Details
Message-ID: <20261203.rfcv10.52108.5.zakinko@snowrabbit.org>
Path: mbox-mo1.pugleaf.net!news-ams.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376203-linux-kernel-netdev=dewy.ru
References: <20261203.rfcv10.52108.0.zakinko@snowrabbit.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-ams.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376203-linux-kernel-netdev=dewy.ru
From: zakinko@snowrabbit.org (Showta Ishizaki)
Newsgroups: linux.kernel.netdev
Subject: [PATCH RFC net v10 5/6] net: virtio: compute the outer checksum of
 non-GSO tunnel packets
Date: Thu, 03 Dec 2026 10:53:19 +0900
Organization: Usenet Network
Message-ID: <20261203.rfcv10.52108.5.zakinko@snowrabbit.org>
References: <20261203.rfcv10.52108.0.zakinko@snowrabbit.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2109048"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:jzopnNofJ1tPX1XUE77f4dcX318= sha256:GM94Pfk9MxrhKXO3stJz/Xa6sjAyzd9eGOdTPP+vhAM=
	sha1:COoNnt9juhsHqvqauL3iB4wJf0o= sha256:0iLkQrMqWxoo7M7TgSlPeCXGI5vygeJCEdH4biuQGLk=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id CC2901177DF; Thu,  3 Dec 2026 04:55:00 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.951
X-Spam-Status: No, score=-2.951 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id TwL4OUSmPHv3;
	Thu,  3 Dec 2026 04:54:56 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-376203-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=snowrabbit.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 096531177E0; Thu,  3 Dec 2026 04:54:51 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 96AC32A975; Thu,  3 Dec 2026 01:54:39 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796262879;
	b=JLrA7bYVp5W7NWmf/pndUItVC0lpzcPSc8EPgdq1mrG2Y5IbL9WRlUxQsslq5T6lL/Oc
	 l+8eLGkOJNhmSxVUDPBnqlm7+l88saU6LaaVmxxioteWRyHuTvbyo9mJ36muUtwcOCn9N
	 5D6MI/zBhISKpj88M29C4157TiCou785jVCKRMQ3sJI8fj81MmXNdDpJS53979TuHpbec
	 oUCoeCDj/asHcrxZlrEfg4+qlr+nvraxVuYS7Bw1qxVIkf92kHKsmAzsd0aikckQAUL/5
	 TTTvL1XVlQKe6Yx2XvVUP87aD545KsnuXqN8ASt/4DvV/N9SWHhMnFdhL67OPuenabw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796262879;
	h=X-Original-To:Received:DMARC-Filter:Received:From:To:Cc:Subject:Date:
	 Message-ID:In-Reply-To:References:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Type:Content-Transfer-Encoding;
	bh=o7/dWUz8nSMIrqHMTo7ZilNIlInVZrJhtb/9JXY0ubk=;
	b=YfUgXzXL5XDKubIVY++QVooBmM7DrMG4hQl4WVWmTA7lVoHThEjCUzXVrWojEmmK9gg6
	 HIDl3Ur6YTg17beWGSagYAPRaoM9plxFwhpeqz3LptYWnDeUrKTCsjpARRdtWyDAYbOuo
	 9n7zaKvgP5ZspEdjWad72XGuEiSLGN3VZkTsrPLkGOhcsQqlfis0iibc0JODxgiuyQAG7
	 U+JAloK88DQn+qZGRIVyoGnbflQKZ28I8X7U8K/1wHRuYioyxSIuvHCc2uXi/JkarbTf9
	 yyuHesA4w0Ur0mBla1plT0QTSZClerKtgWeLtV7U/iQDkAKvW6vnb8TYdXJVArXrMNw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dmarc=none header.from=snowrabbit.org;
	spf=pass smtp.mailfrom=snowrabbit.org;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 76C1E606A332;
	Thu,  3 Dec 2026 01:54:39 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=182.48.54.220
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796262878;
	b=0IuY1iqbr8tMnmD4KKhq9pAkAOFHxIlLtsAb3q6WmmR4zd5d4lyWNoVE7udwvPOYW/06
	 7WWxUUEQ/E2M71Kdag9OiVlauHa1d9MIL9QSp7WEDbOgkFcS9jnxoLXxwqr3u8hNFyWwZ
	 X63uKdpWhOIWwGBTPTAD7UWjGxoGIMivByw5JT8vqNOGBSzVNmmj8FGpgx6bcuzYD/X8P
	 lw+riY1jz+AvlH46SlPainw70J9WBFVKUgza13IwmbCrBP7OQ/lcJwmCyg0judqP7cFdZ
	 PivqErIpBFy6SgUr5dvQzZFjJrs4ZD16Ni/tJV5ryb9/yJoI6+1ZtQ0yqhrXM6SSMWA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796262878;
	h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:In-Reply-To:
	 References:MIME-Version:Content-Type:Content-Transfer-Encoding;
	bh=o7/dWUz8nSMIrqHMTo7ZilNIlInVZrJhtb/9JXY0ubk=;
	b=ETzZw3OOFyglNNC+8OnFHZ/BVkzrNoORDCvIUNS1P04wIotua8B3xtB9IwxOkmDH2Q9q
	 wQ20bkFCwOf6hY4Vy+fblQjmNNimUzaUpbo1LOpoUoG0nUyf2RmorlyTPYmurjZvEJJsI
	 nji06fUjqBhHoN08sPLkrlcYi0IqOYYVWnHxxqaCTfcOxilr+YoJhaQCCfZJmmx83rw8A
	 W35N4dhEo1JrK0Vu4AHkqrydMLQrrDGfNlVuZRUfUdtF+iNjmmOxVR+uGiYZyMYFc/LS4
	 zuBEA0wqrCWwjbqfmPFx0fgTJo/FQ4HAxNbz7gw14aWdittSqVmFVpNTWkEqY5Q1ozw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=snowrabbit.org;
	spf=pass smtp.mailfrom=snowrabbit.org;
	arc=none smtp.remote-ip=182.48.54.220
X-Received: from bambi.snowrabbit.org (bambi.snowrabbit.org [182.48.54.220])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 2520B606A32C;
	Thu,  3 Dec 2026 01:54:38 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2520B606A32C
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=snowrabbit.org
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=snowrabbit.org
X-Received: by bambi.snowrabbit.org (Postfix, from userid 3001)
	id 9E321C92FC; Thu,  3 Dec 2026 10:54:37 +0900 (JST)
X-Cc: Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	David S. Miller <davem@davemloft.net>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	Simon Horman <horms@kernel.org>,
	Eugenio Pérez <eperezma@redhat.com>,
	Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
	Richard Weinberger <richard@nod.at>,
	Anton Ivanov <anton.ivanov@cambridgegreys.com>,
	Johannes Berg <johannes@sipsolutions.net>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Chris Arges <carges@cloudflare.com>,
	Paulos Yibelo <habte.yibelo@gmail.com>,
	Björn Töpel <bjorn@kernel.org>,
	virtualization@lists.linux.dev,
	linux-um@lists.infradead.org,
	linux-doc@vger.kernel.org,
	Showta Ishizaki <zakinko@snowrabbit.org>
X-In-Reply-To: <20261203.rfcv10.52108.0.zakinko@snowrabbit.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list