🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Showta Ishizaki Wed, 25 Nov 2026 16:06
[PATCH RFC net v8 0/6] more virtio-net csum hardening
#22655
Author: Showta Ishizaki
Date: Wed, 25 Nov 2026 16:06
77 lines
10748 bytes
Michael wrote in v7 that he would be on other things for a while and
posted it in case anyone wants to take it up, so here it is, rebased and
with the review comments addressed. Michael, if you already have a newer
version, I am happy to drop this one in favor of it. Patches 1-4 and 6
are yours; what I changed is noted above my sign-off.

Changes since v7:
- Rebased. Commit 73e08d50eb13 ("net: virtio: validate outer protocol of
  UDP tunnel GSO packets") has the header parsing helpers and the outer
  header checks, so those are gone from patch 4.
- Patch 4: has_eth is passed to virtio_net_hdr_to_skb() as well, instead
  of looking at the device type. I dropped Chris' Tested-by, as this is
  no longer what he tested.
- Patch 4: the tnl_csum_negotiated check is ahead of the validation
  again, as Willem pointed out.
- Patch 5 is new: the outer checksum of tunnel packets that are too small
  to segment. Willem asked for it as a separate patch.
- Patch 6: the sysctl does not turn off the outer header checks, which
  are in the tree unconditionally now. I turned the Reviewed-by tags into
  Cc, as that changes what was reviewed.

The two issues Michael listed as open in v7 are still open, hence the
RFC: the inner packet of a tunnel can have a valid header at
inner_nh_offset that overlaps the real transport header, and completing
the checksum at input breaks nf/BPF programs that edit the payload and
rely on CHECKSUM_PARTIAL.

I compared no patches, 1-4 and 1-6 on arm64 (qemu, TCG) with frames
written to a tap:

- Non-GSO UDP tunnel packets that ask for the outer checksum, over IPv4
  and IPv6, go out with a wrong one without the series and with 1-4, and
  with a correct one with 1-6.
- When the inner packet is a fragment, 1-4 reject them with EINVAL, and
  1-6 send them with a correct checksum.
- GSO behind an IPv6 AH is accepted without the series, and rejected
  with 1-4 and with 1-6.
- A plain UDP packet with csum_offset at the destination port is
  completed at input and counted in rx_csum_sw. One with good metadata is
  not counted.

The objects touched build on x86_64 with W=1 after each patch. At the end
of the series I also built the um objects, and linked an arm64 kernel
with CONFIG_INET=n and VIRTIO_NET=y.

Based on net e6474ead54b8, where the tests were run as well.

Michael S. Tsirkin (5):
  net: account for ingress-resolved virtio net hdr state
  um: vector: trim skbs before verifying the header
  virtio_net: parse tunnel csum_start little-endian
  net: resolve uncommon partial checksums at ingress
  net: add a sysctl to opt out of virtio net hdr checksum validation

Showta Ishizaki (1):
  net: virtio: compute the outer checksum of non-GSO tunnel packets

 Documentation/admin-guide/sysctl/net.rst |  19 ++
 arch/um/drivers/vector_kern.c            |   6 +-
 arch/um/drivers/vector_transports.c      |   4 +-
 drivers/net/tap.c                        |   4 +-
 drivers/net/tun.c                        |  44 +++-
 drivers/net/tun_vnet.h                   |   4 +-
 drivers/net/virtio_net.c                 |   2 +-
 include/linux/virtio_net.h               | 247 ++++++++++++++++++++++-
 include/net/netns/core.h                 |   1 +
 net/core/dev.c                           |   7 +
 net/core/net_namespace.c                 |   1 +
 net/core/sysctl_net_core.c               |  50 +++++
 net/packet/af_packet.c                   |  16 +-
 13 files changed, 381 insertions(+), 24 deletions(-)


base-commit: e6474ead54b8acecb8b0462dde2d77f46d2b7dc1
-- 
2.54.0
[PATCH RFC net v8 2/6] um: vector: trim skbs before verifying the header
#22657
Author: Showta Ishizaki
Date: Wed, 25 Nov 2026 16:06
60 lines
9629 bytes
From: "Michael S. Tsirkin" <mst@redhat.com>

The vector receive path over-allocates the skb and only trims it
to the received length after the transport header is verified.

A follow up patch will complete the packet checksum in software within
virtio_net_hdr_to_skb so we need the packet trimmed (otherwise the
checksum would be computed incorrectly over the unreceived tail of the
buffer).

Trim first, so the import sees the packet as it was received.

Reviewed-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 arch/um/drivers/vector_kern.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/um/drivers/vector_kern.c b/arch/um/drivers/vector_kern.c
index e902e78c5..828bd6199 100644
--- a/arch/um/drivers/vector_kern.c
+++ b/arch/um/drivers/vector_kern.c
@@ -873,6 +873,7 @@ static int vector_legacy_rx(struct vector_private *vp)
 
 	if (skb != NULL) {
 		if (pkt_len > vp->header_size) {
+			pskb_trim(skb, pkt_len - vp->rx_header_size);
 			if (vp->header_size > 0) {
 				header_check = vp->verify_header(
 					vp->header_rxbuffer, skb, vp);
@@ -887,7 +888,6 @@ static int vector_legacy_rx(struct vector_private *vp)
 					skb->ip_summed = CHECKSUM_UNNECESSARY;
 				}
 			}
-			pskb_trim(skb, pkt_len - vp->rx_header_size);
 			skb->protocol = eth_type_trans(skb, skb->dev);
 			vp->dev->stats.rx_bytes += skb->len;
 			vp->dev->stats.rx_packets++;
@@ -989,6 +989,8 @@ static int vector_mmsg_rx(struct vector_private *vp, int budget)
 	for (i = 0; i < packet_count; i++) {
 		skb = (*skbuff_vector);
 		if (mmsg_vector->msg_len > vp->header_size) {
+			pskb_trim(skb,
+				mmsg_vector->msg_len - vp->rx_header_size);
 			if (vp->header_size > 0) {
 				header_check = vp->verify_header(
 					mmsg_vector->msg_hdr.msg_iov->iov_base,
@@ -1013,8 +1015,6 @@ static int vector_mmsg_rx(struct vector_private *vp, int budget)
 					skb->ip_summed = CHECKSUM_UNNECESSARY;
 				}
 			}
-			pskb_trim(skb,
-				mmsg_vector->msg_len - vp->rx_header_size);
 			skb->protocol = eth_type_trans(skb, skb->dev);
 			/*
 			 * We do not need to lock on updating stats here
-- 
2.54.0 (Apple Git-157)
[PATCH RFC net v8 5/6] net: virtio: compute the outer checksum of non-GSO tunnel packets
#22654
Author: Showta Ishizaki
Date: Wed, 25 Nov 2026 16:06
177 lines
13655 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>
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 include/linux/virtio_net.h | 105 ++++++++++++++++++++++++++++++++++---
 1 file changed, 98 insertions(+), 7 deletions(-)

diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index e9057767b..5d5049418 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>
 
@@ -545,6 +547,64 @@ 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
+		csum = skb_checksum(skb, outer_th, skb->len - outer_th, 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
@@ -560,6 +620,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;
@@ -631,15 +692,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) {
@@ -653,6 +725,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)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads