Skip to contentSkip to conversation

net: gro: skip the checksum of fraglist GRO packets

PATCH NAK Rosen Penev 4 participants 4 messages Nov 23 – Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: gro: skip the checksum of fraglist GRO packets

TCP fraglist GRO is used for packets that are not for a local socket. It chains the original packets instead of merging them, and the segmentation on the way out restores them as they were, each with its own checksum. GRO still verifies the checksum of every packet before merging it, which on a NIC without RX checksum offload means computing it in software over all of the payload. On a router this is the only time the payload is
read: forwarding a packet otherwise only touches its headers.

Skip the check for a fraglist skb when it would have to be done in software and leave the merged skb CHECKSUM_NONE instead of marking it CHECKSUM_UNNECESSARY. A corrupted packet is then forwarded unchanged and dropped by the receiver, as it would be without GRO. NICs with RX checksum offload keep verifying it, which costs them nothing.

The checksum in the head of such an skb does not cover the packets on its frag_list, so a plain __skb_checksum_complete() of it would fail. Make __skb_checksum_complete() verify each packet against its own headers instead, and mark the skb CHECKSUM_UNNECESSARY when they all match. This covers whatever asks for the checksum later: conntrack with nf_conntrack_checksum enabled, and local delivery, which happens for example when a REDIRECT rule sends a connection without a local socket to one. Only fraglist skbs of plain TCP over IPv4 or IPv6 without extension headers are left unverified, which is what that function can check.

tcp_add_backlog() condenses an skb before verifying its checksum, which can pull the packets on the frag_list into the head and leave nothing to verify them against, dropping valid packets for a busy socket. Verify the checksum first.

With nf_conntrack_checksum enabled, the default outside of OpenWrt, conntrack now computes the checksum that GRO used to, so the work moves rather than disappears; with RPS it can move to a busier CPU.

A fraglist skb whose geometry was changed after GRO, by NAT or bpf_skb_pull_data(), is segmented by skb_segment(), which computes new checksums. Such a packet is now forwarded with a valid checksum even if it was corrupted before it arrived; the packet boundaries needed to check it are gone by then.

Cisco Meraki MX65 (BCM58625, 2x Cortex-A9 at 1.2 GHz, bgmac without RX checksum offload), TCP forwarded through the router, CPU bound with an MSS of 536, median of four runs in Mbit/s, in one boot:

checked skipped

  WAN->LAN     496      621
  LAN->WAN     682      802

At the full MSS the router already forwards at line rate in each direction, and csum_partial(), which took 27% of the cycles, drops out of the profile.

---
 include/net/gro.h        | 13 ++++++++
 net/core/skbuff.c        | 67 ++++++++++++++++++++++++++++++++++++++++
 net/ipv4/tcp_ipv4.c      | 19 +++++++-----
 net/ipv4/tcp_offload.c   | 29 ++++++++++++-----
 net/ipv6/tcpv6_offload.c | 29 ++++++++++++-----
 5 files changed, 134 insertions(+), 23 deletions(-)
include/net/gro.h +13 -0
index 7a19d4486456..3cbf6d11be16 100644
@@ -292,6 +292,19 @@ static inline void skb_gro_incr_csum_unnecessary(struct sk_buff *skb)
 #define skb_gro_checksum_simple_validate(skb)				\
 	__skb_gro_checksum_validate(skb, 0, false, 0, null_compute_pseudo)
 
+/* A TCP fraglist GRO skb is only built for packets that are not for a local
+ * socket, and is segmented back into the original packets, each with its own
+ * checksum. Leave the checksum of such a packet unverified when that would
+ * mean computing it in software: the merged skb stays CHECKSUM_NONE, and
+ * __skb_checksum_complete() verifies the packets one by one if anything on the
+ * way asks for it.
+ */
+static inline bool skb_gro_flist_skip_csum(const struct sk_buff *skb)
+{
+	return NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark &&
+	       skb->ip_summed == CHECKSUM_NONE;
+}
+
 static inline bool __skb_gro_checksum_convert_check(struct sk_buff *skb)
 {
 	return (NAPI_GRO_CB(skb)->csum_cnt == 0 &&
net/core/skbuff.c +67 -0

79 lines not on this page · raw message

net/ipv4/tcp_ipv4.c +11 -8
index f2a5c8f3bf9d..67897435291a 100644
@@ -1919,14 +1919,6 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 	int delta;
 	int err;
 
-	/* In case all data was pulled from skb frags (in __pskb_pull_tail()),
-	 * we can fix skb->truesize to its real value to avoid future drops.
-	 * This is valid because skb is not yet charged to the socket.
-	 * It has been noticed pure SACK packets were sometimes dropped
-	 * (if cooked by drivers without copybreak feature).
-	 */
-	skb_condense(skb);
-
 	tcp_cleanup_skb(skb);
 
 	if (unlikely(tcp_checksum_complete(skb))) {
@@ -1937,6 +1929,17 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 		return SKB_DROP_REASON_TCP_CSUM;
 	}
 
+	/* Verify the checksum first: a TCP fraglist GRO skb left unverified
+	 * can only be checked while its packets are still on its frag_list.
+	 *
+	 * In case all data was pulled from skb frags (in __pskb_pull_tail()),
+	 * we can fix skb->truesize to its real value to avoid future drops.
+	 * This is valid because skb is not yet charged to the socket.
+	 * It has been noticed pure SACK packets were sometimes dropped
+	 * (if cooked by drivers without copybreak feature).
+	 */
+	skb_condense(skb);
+
 	/* Attempt coalescing to last skb in backlog, even if we are
 	 * above the limits.
 	 * This is okay because skb capacity is limited to MAX_SKB_FRAGS.
net/ipv4/tcp_offload.c +21 -8
index b84fcd07c723..d38eaf24c3c5 100644
@@ -410,8 +410,11 @@ static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 			return;
 		}
 
-		/* Fall back to the regular GRO path */
-		if (NAPI_GRO_CB(p)->count == 1)
+		/* Fall back to the regular GRO path, unless the checksum of p
+		 * was left unverified: regular GRO would compute a new one on
+		 * the way out. p is flushed instead.
+		 */
+		if (NAPI_GRO_CB(p)->count == 1 && p->ip_summed != CHECKSUM_NONE)
 			NAPI_GRO_CB(p)->is_flist = 0;
 
 		NAPI_GRO_CB(skb)->is_flist = 0;
@@ -433,10 +436,21 @@ static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 INDIRECT_CALLABLE_SCOPE
 struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
 {
+	unsigned int off = skb_gro_offset(skb);
 	struct tcphdr *th;
 
-	/* Don't bother verifying checksum if we're going to flush anyway. */
-	if (!NAPI_GRO_CB(skb)->flush &&
+	th = skb_gro_header(skb, off + sizeof(*th), off);
+	if (unlikely(!th))
+		goto flush;
+
+	if (unlikely(skb->dev->features & NETIF_F_GRO_FRAGLIST))
+		tcp4_check_fraglist_gro(head, skb, th);
+
+	/* Don't bother verifying checksum if we're going to flush anyway.
+	 * A fraglist skb is verified one packet at a time if anything asks
+	 * for it, see skb_gro_flist_skip_csum().
+	 */
+	if (!NAPI_GRO_CB(skb)->flush && !skb_gro_flist_skip_csum(skb) &&
 	    skb_gro_checksum_validate(skb, IPPROTO_TCP,
 				      inet_gro_compute_pseudo))
 		goto flush;
@@ -445,9 +459,6 @@ struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
 	if (!th)
 		goto flush;
 
-	if (unlikely(skb->dev->features & NETIF_F_GRO_FRAGLIST))
-		tcp4_check_fraglist_gro(head, skb, th);
-
 	return tcp_gro_receive(head, skb, th);
 
 flush:
@@ -465,7 +476,9 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
 		skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV4;
 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
 
-		__skb_incr_checksum_unnecessary(skb);
+		/* not verified, see skb_gro_flist_skip_csum() */
+		if (skb->ip_summed != CHECKSUM_NONE)
+			__skb_incr_checksum_unnecessary(skb);
 
 		return 0;
 	}
net/ipv6/tcpv6_offload.c +22 -7
index eec3778855eb..9c5709b2bf72 100644
@@ -32,8 +32,11 @@ static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 			return;
 		}
 
-		/* Fall back to the regular GRO path */
-		if (NAPI_GRO_CB(p)->count == 1)
+		/* Fall back to the regular GRO path, unless the checksum of p
+		 * was left unverified: regular GRO would compute a new one on
+		 * the way out. p is flushed instead.
+		 */
+		if (NAPI_GRO_CB(p)->count == 1 && p->ip_summed != CHECKSUM_NONE)
 			NAPI_GRO_CB(p)->is_flist = 0;
 
 		NAPI_GRO_CB(skb)->is_flist = 0;
@@ -56,10 +59,23 @@ static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 static __always_inline struct sk_buff *tcp6_gro_receive(struct list_head *head,
 							struct sk_buff *skb)
 {
+	const struct ipv6hdr *iph = skb_gro_network_header(skb);
+	unsigned int off = skb_gro_offset(skb);
 	struct tcphdr *th;
 
-	/* Don't bother verifying checksum if we're going to flush anyway. */
+	th = skb_gro_header(skb, off + sizeof(*th), off);
+	if (unlikely(!th))
+		goto flush;
+
+	if (unlikely(skb->dev->features & NETIF_F_GRO_FRAGLIST))
+		tcp6_check_fraglist_gro(head, skb, th);
+
+	/* Don't bother verifying checksum if we're going to flush anyway.
+	 * __skb_checksum_complete() can only verify the packets of a fraglist
+	 * skb without extension headers.
+	 */
 	if (!NAPI_GRO_CB(skb)->flush &&
+	    !(skb_gro_flist_skip_csum(skb) && iph->nexthdr == IPPROTO_TCP) &&
 	    skb_gro_checksum_validate(skb, IPPROTO_TCP,
 				      ip6_gro_compute_pseudo))
 		goto flush;
@@ -68,9 +84,6 @@ static __always_inline struct sk_buff *tcp6_gro_receive(struct list_head *head,
 	if (!th)
 		goto flush;
 
-	if (unlikely(skb->dev->features & NETIF_F_GRO_FRAGLIST))
-		tcp6_check_fraglist_gro(head, skb, th);
-
 	return tcp_gro_receive(head, skb, th);
 
 flush:
@@ -88,7 +101,9 @@ static __always_inline int tcp6_gro_complete(struct sk_buff *skb, int thoff)
 		skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV6;
 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
 
-		__skb_incr_checksum_unnecessary(skb);
+		/* not verified, see skb_gro_flist_skip_csum() */
+		if (skb->ip_summed != CHECKSUM_NONE)
+			__skb_incr_checksum_unnecessary(skb);
 
 		return 0;
 	}
Signature · 1 line
2.56.1
#1 lore raw
Quoted 4 lines · Rosen Penev

Make __skb_checksum_complete() verify each packet against its own headers instead, and mark the skb CHECKSUM_UNNECESSARY when they all match. This covers whatever asks for the checksum later: conntrack with nf_conntrack_checksum enabled, and local delivery, which happens for

It doesn't. This breaks tc progs.
Today the skb is CHECKSUM_UNNECESSARY when a tc ingress prog runs. With the patch valid packets fail skb_flist_csum_ok() after the prog:

  helper                  what skb_flist_tcp_csum_ok() gets
  ----------------------  -------------------------------------------
  bpf_skb_pull_data()     2 packets, mss 536, thlen 20, all pulled:
                          len = 1092 bytes, th->check covers 556
  bpf_skb_change_proto()  6 to 4: ipv6 = false, frag_list has IPv6
                          headers, iph->ihl = 0

TCP drops the skb and conntrack marks it invalid. CLAT has no IPv6 socket, so GRO always picks fraglist for it.

Quoted 5 lines

A fraglist skb whose geometry was changed after GRO, by NAT or bpf_skb_pull_data(), is segmented by skb_segment(), which computes new checksums. Such a packet is now forwarded with a valid checksum even if it was corrupted before it arrived; the packet boundaries needed to check it are gone by then.

That's silent data corruption. It's not an option. tcp4_gso_segment() also takes that path on SKB_GSO_DODGY. bpf_skb_adjust_room() and bpf_skb_change_proto() set it on every GSO skb. Willem's series adds skb->encapsulation, like seg6 encap or ESP offload:
https://lore.kernel.org/bpf/[email protected]/

Quoted 3 lines

At the full MSS the router already forwards at line rate in each direction, and csum_partial(), which took 27% of the cycles, drops out of the profile.

So there is no throughput win at full MSS.
With the default nf_conntrack_checksum the work only moves to conntrack. Paolo suggests to remove fraglist GSO. This adds another state to it:
https://lore.kernel.org/bpf/[email protected]/

Nack.

pw-bot: cr

#2 lore raw
In reply to Rosen Penev
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ipv6: gro: stale ipv6hdr pointer dereference in tcp6_gro_receive() - [High] net: gro: CHECKSUM_NONE…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ipv6: gro: stale ipv6hdr pointer dereference in tcp6_gro_receive()
- [High] net: gro: CHECKSUM_NONE fraglist GRO skbs break GSO on NETIF_F_GSO_FRAGLIST devs
It also noticed 2 related pre-existing issues this patch does not introduce:
- [High] tcp: missing protocol check in tcp_add_backlog() allows cross-family coalesce [1]
- [High] tcp: gro: stale CHECKSUM_COMPLETE on fraglist in tcp4/6_gro_complete() [2]
--

commit fa341d1e23410a4c1023b2a324454cb1f2e9afbb
Author: Rosen Penev <[email protected]>

net: gro: skip the checksum of fraglist GRO packets

This commit skips software TCP checksum validation during fraglist GRO and leaves the merged skb with CHECKSUM_NONE, teaching __skb_checksum_complete() to verify each frag_list segment individually on demand and moving tcp_add_backlog()'s checksum check ahead of skb_condense().

Quoted 27 lines
net/ipv4/tcp_ipv4.c +4 -8
index f2a5c8f3bf9d7..67897435291ae 100644
@@ -1919,14 +1919,6 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 	int delta;
 	int err;
 
-	/* In case all data was pulled from skb frags (in __pskb_pull_tail()),
-	 * we can fix skb->truesize to its real value to avoid future drops.
-	 * This is valid because skb is not yet charged to the socket.
-	 * It has been noticed pure SACK packets were sometimes dropped
-	 * (if cooked by drivers without copybreak feature).
-	 */
-	skb_condense(skb);
-
 	tcp_cleanup_skb(skb);
 
 	if (unlikely(tcp_checksum_complete(skb))) {
@@ -1937,6 +1929,17 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 		return SKB_DROP_REASON_TCP_CSUM;
 	}
 
+	/* Verify the checksum first: a TCP fraglist GRO skb left unverified
+	 * can only be checked while its packets are still on its frag_list.
+	 *
+	 * In case all data was pulled from skb frags (in __pskb_pull_tail()),

net/ipv4/tcp_ipv4.c enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb) · line 1945

Quoted 10 lines
net/ipv4/tcp_ipv4.c +7 -0
+	 * we can fix skb->truesize to its real value to avoid future drops.
+	 * This is valid because skb is not yet charged to the socket.
+	 * It has been noticed pure SACK packets were sometimes dropped
+	 * (if cooked by drivers without copybreak feature).
+	 */
+	skb_condense(skb);
+
 	/* Attempt coalescing to last skb in backlog, even if we are
 	 * above the limits.
 	 * This is okay because skb capacity is limited to MAX_SKB_FRAGS.

[Severity: High]
This problem wasn't introduced by this patch, but can tcp_add_backlog() coalesce ETH_P_IPV6 and ETH_P_IP packets together when an AF_INET6 socket disconnects and reconnects to an IPv4-mapped peer [1]?

If tcp_v6_rcv() looks up the socket before userspace disconnects and reconnects it to an IPv4-mapped address while holding the socket lock, both the in-flight IPv6 packet and subsequent IPv4 packets are queued via tcp_add_backlog(). Neither the checks right below nor tcp_skb_can_collapse_rx() verify tail->protocol == skb->protocol before skb_try_coalesce(). When __release_sock() drains the backlog into tcp_v4_do_rcv(), a stale IPv6 packet coalesced onto an IPv4 tail bypasses the skb->protocol check and injects stale payload, sequence numbers, or FIN flags into the IPv4 stream, while an IPv4 packet coalesced onto a stale IPv6 tail is dropped with it.

Quoted 4 lines
net/ipv4/tcp_offload.c +0 -0
index b84fcd07c7230..d38eaf24c3c58 100644

[ ... ]

net/ipv4/tcp_offload.c INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff) · line 484

Quoted 11 lines
net/ipv4/tcp_offload.c +3 -1
@@ -465,7 +476,9 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
 		skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV4;
 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
 
-		__skb_incr_checksum_unnecessary(skb);
+		/* not verified, see skb_gro_flist_skip_csum() */
+		if (skb->ip_summed != CHECKSUM_NONE)
+			__skb_incr_checksum_unnecessary(skb);
 
 		return 0;
 	}

[Severity: High]
Does leaving SKB_GSO_FRAGLIST packets as CHECKSUM_NONE break GSO handling on egress devices with NETIF_F_GSO_FRAGLIST (and similarly in tcp6_gro_complete())?

include/linux/netdevice.h:netif_needs_gso() {
	return skb_is_gso(skb) && (!skb_gso_ok(skb, features) ||
		unlikely((skb->ip_summed != CHECKSUM_PARTIAL) &&
			 (skb->ip_summed != CHECKSUM_UNNECESSARY)));
}

Because netif_needs_gso() returns true for CHECKSUM_NONE even when skb_gso_ok() is true, validate_xmit_skb() forces software GSO on virtual devices advertising NETIF_F_GSO_FRAGLIST | NETIF_F_FRAGLIST (such as bridges, veth, VLANs, or VRFs). In tcp4_gso_segment() (and tcp6_gso_segment()):

net/ipv4/tcp_offload.c:tcp4_gso_segment() {
	...
	if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) {
		struct tcphdr *th = tcp_hdr(skb);

		if ((skb_pagelen(skb) - th->doff * 4 == skb_shinfo(skb)->gso_size) &&
		    !(skb_shinfo(skb)->gso_type & SKB_GSO_DODGY))
			return __tcp4_gso_segment_list(skb, features);

		skb->ip_summed = CHECKSUM_NONE;
	}

	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
		...
		th->check = 0;
		skb->ip_summed = CHECKSUM_PARTIAL;
		__tcp_v4_send_check(skb, iph->saddr, iph->daddr);
	}

	return tcp_gso_segment(skb, features);
}

Unmodified fraglist GRO skbs are immediately split by __tcp4_gso_segment_list() at the first virtual device instead of passing through as a GSO aggregate. Worse, if the geometry was modified after GRO (for example by bpf_skb_pull_data()), tcp4_gso_segment() overwrites th->check with a pseudo-header checksum, sets CHECKSUM_PARTIAL, and calls tcp_gso_segment(), which sees skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST) == true and returns NULL without segmenting the skb, transmitting the broken fraglist skb unsegmented with an invalid th->check.

[Severity: High]
This problem wasn't introduced by this patch, but when packets arrive with CHECKSUM_COMPLETE, does __skb_incr_checksum_unnecessary() leave a stale single-segment skb->csum on the coalesced fraglist skb (and similarly in tcp6_gro_complete()) [2]?

Each segment was already validated by skb_gro_checksum_validate() in tcp4_gro_receive(), which converted CHECKSUM_NONE to CHECKSUM_UNNECESSARY but left CHECKSUM_COMPLETE intact. Because __skb_incr_checksum_unnecessary() only acts on CHECKSUM_NONE and CHECKSUM_UNNECESSARY, a CHECKSUM_COMPLETE fraglist skb keeps skb->csum from the first segment while skb->len spans the entire aggregate, causing later checksum checks in conntrack or local delivery to fail and drop the packet.

Quoted 4 lines
net/ipv6/tcpv6_offload.c +0 -0
index eec3778855eb8..9c5709b2bf721 100644

[ ... ]

Quoted 15 lines
net/ipv6/tcpv6_offload.c +8 -1
@@ -56,10 +59,23 @@ static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 static __always_inline struct sk_buff *tcp6_gro_receive(struct list_head *head,
 							struct sk_buff *skb)
 {
+	const struct ipv6hdr *iph = skb_gro_network_header(skb);
+	unsigned int off = skb_gro_offset(skb);
 	struct tcphdr *th;
 
-	/* Don't bother verifying checksum if we're going to flush anyway. */
+	th = skb_gro_header(skb, off + sizeof(*th), off);
+	if (unlikely(!th))
+		goto flush;
+
+	if (unlikely(skb->dev->features & NETIF_F_GRO_FRAGLIST))
+		tcp6_check_fraglist_gro(head, skb, th);

net/ipv6/tcpv6_offload.c static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb, · line 81

Quoted 10 lines
net/ipv6/tcpv6_offload.c +6 -0
+
+	/* Don't bother verifying checksum if we're going to flush anyway.
+	 * __skb_checksum_complete() can only verify the packets of a fraglist
+	 * skb without extension headers.
+	 */
 	if (!NAPI_GRO_CB(skb)->flush &&
+	    !(skb_gro_flist_skip_csum(skb) && iph->nexthdr == IPPROTO_TCP) &&
 	    skb_gro_checksum_validate(skb, IPPROTO_TCP,
 				      ip6_gro_compute_pseudo))
 		goto flush;

[Severity: High]
Can iph->nexthdr dereference freed memory after skb_gro_header()?

iph is loaded via skb_gro_network_header(skb) before skb_gro_header() pulls the TCP header. If off + sizeof(*th) exceeds frag0_len, skb_gro_header() falls back to skb_gro_header_slow() -> pskb_may_pull(), which can release the frag0 page or reallocate skb->head, leaving iph dangling when iph->nexthdr is read. Fetching skb_gro_network_header(skb) after skb_gro_header() avoids the stale pointer.

[1] https://sashiko.dev/#/bug/linux-54f30cb5-2d86-46d9-b2c9-c25a0adaef88
[2] https://sashiko.dev/#/bug/linux-b1b0f506-a36b-4c96-bc55-812755610e6c

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Rosen Penev
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider.

Critical: 0 · High: 3 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] tcp6_gro_receive() in net/ipv6/tcpv6_offload.c saves iph = skb_gro_network_header(skb) at line 62.
- [High] The new fraglist branch in __skb_checksum_complete() (net/core/skbuff.c:4167-4170) checks only ip_summed == CHECKSUM_NONE…
- [High] net: gro: CHECKSUM_NONE fraglist GRO skbs break GSO on NETIF_F_GSO_FRAGLIST devs
- [Medium] When tcp4_gro_receive()/tcp6_gro_receive() skip skb_gro_checksum_validate() for fraglist candidates…
- [Low] The comment above __skb_checksum_complete() (net/core/skbuff.c) and the kernel-doc of skb_checksum_complete()…

--

net: gro: skip the checksum of fraglist GRO packets

On NICs without RX checksum offload, this patch skips the software TCP checksum check in GRO for fraglist candidates and leaves the merged skb CHECKSUM_NONE. It adds a path to __skb_checksum_complete() that checks each packet of such an skb against its own headers. tcp_add_backlog() now verifies the checksum before calling skb_condense().

Quoted 15 lines
net/core/skbuff.c +7 -0
index e5b0209aec1c9..fc26b230497de 100644
@@ -4087,6 +4087,68 @@ __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
 }
 EXPORT_SYMBOL(__skb_checksum_complete_head);
 
+/* Verify the TCP checksum of one packet of a TCP fraglist GRO skb: the
+ * head, whose data starts at or before its TCP header, or one of the skbs on
+ * its frag_list, whose data starts at the payload with the headers in front
+ * of it.
+ */
+static bool skb_flist_tcp_csum_ok(const struct sk_buff *skb, bool ipv6)
+{

[ ... ]

net/core/skbuff.c __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) · line 4098

Quoted 8 lines
net/core/skbuff.c +8 -0
+	off = th - skb->data;
+	if (off > (int)skb_headlen(skb) || -off > (int)skb_headroom(skb))
+		return false;
+
+	len = skb_pagelen(skb) - off;
+	csum = 0;
+	if (skb_checksum_full(skb, off, len, &csum))
+		return false;

[Severity: High]
Can valid packets fail this check once the head and frag_list no longer match the packets GRO built?

__skb_checksum_complete() picks the fraglist branch using only ip_summed and the SKB_GSO_FRAGLIST and SKB_GSO_TCPV4/TCPV6 bits. The expression skb_pagelen(skb) - off assumes the head still holds only its own packet. tcp4_gso_segment() checks the geometry before it trusts the frag_list:

net/ipv4/tcp_offload.c:tcp4_gso_segment() {
    ...
		if ((skb_pagelen(skb) - th->doff * 4 == skb_shinfo(skb)->gso_size) &&
		    !(skb_shinfo(skb)->gso_type & SKB_GSO_DODGY))
			return __tcp4_gso_segment_list(skb, features);
    ...
}

There is no similar check here.

For example, a tc ingress BPF program can call bpf_skb_pull_data() with a length that goes past the first packet:

bpf_skb_pull_data()
bpf_try_make_writable()
skb_ensure_writable()
pskb_may_pull()
__pskb_pull_tail() <- frag_list payload moved into the head

gso_type still has SKB_GSO_FRAGLIST. With nf_conntrack_checksum=1, the default, PRE_ROUTING then runs:

tcp_error()
nf_checksum()
nf_ip_checksum()
__skb_checksum_complete()
skb_flist_csum_ok() <- head checksum now covers extra bytes

conntrack logs "bad checksum" and attaches no ct, so the aggregate is INVALID. If the aggregate is delivered locally through REDIRECT or TPROXY, tcp_checksum_complete() drops it with SKB_DROP_REASON_TCP_CSUM.

bpf_skb_proto_4_to_6() and bpf_skb_proto_6_to_4() seem to cause a similar mismatch. They swap SKB_GSO_TCPV4 and SKB_GSO_TCPV6 in gso_type, but the frag_list members keep their old-family headers.

Before this patch these skbs were CHECKSUM_UNNECESSARY and nothing checked them again.

The commit message says:

This covers whatever asks for the checksum later: conntrack with nf_conntrack_checksum enabled, and local delivery

It also says that skbs changed by bpf_skb_pull_data() are segmented by skb_segment() on the way out. That doesn't cover any check made before egress. The only pull this patch moves after the check is the skb_condense() call in tcp_add_backlog().

Does this need a geometry check like the one in tcp4_gso_segment(), plus a defined fallback when the geometry no longer matches?

[ ... ]

net/core/skbuff.c __sum16 __skb_checksum_complete(struct sk_buff *skb) · line 4170

Quoted 8 lines
net/core/skbuff.c +4 -0
@@ -4102,6 +4164,11 @@ __sum16 __skb_checksum_complete(struct sk_buff *skb)
 	__wsum csum = 0;
 	__sum16 sum;
 
+	if (unlikely(skb->ip_summed == CHECKSUM_NONE && skb_is_gso(skb) &&
+		     (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+		     (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))))
+		return skb_flist_csum_ok(skb) ? 0 : (__force __sum16)1;

[Severity: Low]
This isn't a bug, but should the comment above __skb_checksum_complete() and the kernel-doc of skb_checksum_complete() in include/linux/skbuff.h be updated for this branch?

Both describe the same behaviour. The function computes the checksum over the whole packet plus skb->csum and stores the full checksum back in skb->csum. It leaves the skb CHECKSUM_COMPLETE and returns the checksum.

This early return ignores skb->csum and returns a made-up 0 or 1. On success it sets the skb to CHECKSUM_UNNECESSARY. On failure it leaves the skb CHECKSUM_NONE without calling skb_checksum_complete_failed() or setting csum_valid.

The current callers (nf_ip_checksum(), nf_ip6_checksum() and tcp_checksum_complete()) only test for zero. So this is a documentation mismatch, not a functional problem.

Quoted 4 lines
net/ipv4/tcp_offload.c +0 -0
index b84fcd07c7230..d38eaf24c3c58 100644

[ ... ]

Quoted 4 lines
net/ipv4/tcp_offload.c +0 -0
@@ -433,10 +436,21 @@ static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 INDIRECT_CALLABLE_SCOPE
 struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
 {

[ ... ]

net/ipv4/tcp_offload.c static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb, · line 456

Quoted 8 lines
net/ipv4/tcp_offload.c +5 -0
+	/* Don't bother verifying checksum if we're going to flush anyway.
+	 * A fraglist skb is verified one packet at a time if anything asks
+	 * for it, see skb_gro_flist_skip_csum().
+	 */
+	if (!NAPI_GRO_CB(skb)->flush && !skb_gro_flist_skip_csum(skb) &&
 	    skb_gro_checksum_validate(skb, IPPROTO_TCP,
 				      inet_gro_compute_pseudo))
 		goto flush;

[Severity: Medium]
What happens to valid packets that get chained to a corrupted one here?

Before this patch, a packet with a bad TCP checksum failed skb_gro_checksum_validate() and went to flush, so it was handled on its own.

With the check skipped, the next non-GSO packet inherits is_flist from p in tcp4_check_fraglist_gro(). Both packets are CHECKSUM_NONE, so this test in tcp_gro_receive() doesn't stop the merge:

		flush |= skb->ip_summed != p->ip_summed;

skb_gro_receive_list() can then chain the bad packet with up to 63 valid ones. skb_flist_csum_ok() gives one result for the whole aggregate. With nf_conntrack_checksum=1, tcp_error() at PRE_ROUTING marks every packet in it "bad checksum" and attaches no ct.

A "ct state invalid drop" rule then drops all of them. Without such a rule they are forwarded without NAT, for example SNAT or masquerade traffic leaving with its private source address. On local delivery, tcp_checksum_complete() drops all of them.

tcp6_gro_receive() skips the check in the same way.

The commit message says:

A corrupted packet is then forwarded unchanged and dropped by the receiver, as it would be without GRO.

Is that still true when conntrack checksum validation is enabled?

Quoted 4 lines
net/ipv6/tcpv6_offload.c +0 -0
index eec3778855eb8..9c5709b2bf721 100644

[ ... ]

Quoted 15 lines
net/ipv6/tcpv6_offload.c +8 -1
@@ -56,10 +59,23 @@ static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
 static __always_inline struct sk_buff *tcp6_gro_receive(struct list_head *head,
 							struct sk_buff *skb)
 {
+	const struct ipv6hdr *iph = skb_gro_network_header(skb);
+	unsigned int off = skb_gro_offset(skb);
 	struct tcphdr *th;
 
-	/* Don't bother verifying checksum if we're going to flush anyway. */
+	th = skb_gro_header(skb, off + sizeof(*th), off);
+	if (unlikely(!th))
+		goto flush;
+
+	if (unlikely(skb->dev->features & NETIF_F_GRO_FRAGLIST))
+		tcp6_check_fraglist_gro(head, skb, th);

net/ipv6/tcpv6_offload.c static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb, · line 81

Quoted 10 lines
net/ipv6/tcpv6_offload.c +6 -0
+
+	/* Don't bother verifying checksum if we're going to flush anyway.
+	 * __skb_checksum_complete() can only verify the packets of a fraglist
+	 * skb without extension headers.
+	 */
 	if (!NAPI_GRO_CB(skb)->flush &&
+	    !(skb_gro_flist_skip_csum(skb) && iph->nexthdr == IPPROTO_TCP) &&
 	    skb_gro_checksum_validate(skb, IPPROTO_TCP,
 				      ip6_gro_compute_pseudo))
 		goto flush;

[Severity: High]
Can iph point to freed memory by the time iph->nexthdr is read?

iph comes from skb_gro_network_header() before skb_gro_header() runs. If frag0_len is shorter than off + sizeof(*th), skb_gro_header() calls skb_gro_header_slow() -> pskb_may_pull() -> __pskb_pull_tail(). include/net/gro.h documents that this can invalidate frag0:

/* A pull can release the page fragment frag0 points to, or reallocate
* skb->head, even if it fails. Call this before the pull, to make sure later
* skb_gro_header() and skb_gro_network_header() calls no longer use frag0. */

If headlen is non-zero, iph points into skb->head, and pskb_expand_head() frees that buffer when tailroom is short. If headlen is zero, iph points into frags[0], which skb_frag_unref() releases when the whole fragment is pulled.

The read only happens in the case this patch targets: NETIF_F_GRO_FRAGLIST set, no local socket, and CHECKSUM_NONE. The value it reads decides whether the checksum is skipped.

Before this patch, tcp6_check_fraglist_gro() and ip6_gro_compute_pseudo() both fetched the network header after the pull. The IPv4 side still does. Commit 98d949a15ff9d3 ("net: gro: invalidate frag0 after a pull") fixed the same kind of problem for UDP and ESP.

Could iph be fetched with skb_gro_network_header() after skb_gro_header() succeeds instead?

[ ... ]

net/ipv6/tcpv6_offload.c INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff) · line 484

Quoted 11 lines
net/ipv6/tcpv6_offload.c +3 -1
@@ -465,7 +476,9 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
 		skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV4;
 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
 
-		__skb_incr_checksum_unnecessary(skb);
+		/* not verified, see skb_gro_flist_skip_csum() */
+		if (skb->ip_summed != CHECKSUM_NONE)
+			__skb_incr_checksum_unnecessary(skb);
 
 		return 0;
 	}

[Severity: High]
Does leaving the aggregate CHECKSUM_NONE defeat fraglist GSO passthrough on the way out?

netif_needs_gso() asks for software segmentation of any GSO skb that is neither CHECKSUM_PARTIAL nor CHECKSUM_UNNECESSARY, even when skb_gso_ok() says the device can take it as is:

include/linux/netdevice.h:netif_needs_gso() {
	return skb_is_gso(skb) && (!skb_gso_ok(skb, features) ||
		unlikely((skb->ip_summed != CHECKSUM_PARTIAL) &&
			 (skb->ip_summed != CHECKSUM_UNNECESSARY)));
}

Before this patch, tcp4_gro_complete() and tcp6_gro_complete() marked the fraglist skb CHECKSUM_UNNECESSARY, so it passed through bridge, VLAN, veth, VRF, bond and tunnel devices, which advertise NETIF_F_GSO_FRAGLIST and NETIF_F_FRAGLIST, and was only split at the real egress device. Now validate_xmit_skb() on the first of those devices calls skb_gso_segment(), and tcp4_gso_segment() or tcp6_gso_segment() hands it to __tcp4_gso_segment_list() or __tcp6_gso_segment_list(). skb_segment_list() doesn't look at the device features, so the aggregate is split into up to 64 packets right there, and everything after that point (bridge forwarding, VLAN tagging, tunnel encapsulation) runs once per packet again.

On OpenWrt, forwarded traffic normally crosses br-lan, so this hits the setup the patch is aimed at. Was there a bridge on the path for the MX65 numbers?

It gets worse when the geometry was changed after GRO, for example by bpf_skb_pull_data(). tcp4_gso_segment() then skips the list path, sets CHECKSUM_PARTIAL, writes a pseudo-header checksum into th->check with __tcp_v4_send_check() and calls tcp_gso_segment(). With NETIF_F_GSO_FRAGLIST, NETIF_F_TSO and NETIF_F_FRAGLIST in features, the skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST) test there is true, so it returns NULL without calling skb_segment(). validate_xmit_skb() then sends the original skb on unsegmented, but now CHECKSUM_PARTIAL. If that device is a veth, the peer sees CHECKSUM_PARTIAL, skb_csum_unnecessary() returns true, and a local socket in that namespace accepts payload that nothing verified. The new skb_flist_csum_ok() path can't catch it either, since the skb is no longer CHECKSUM_NONE. tcp6_gso_segment() does the same through __tcp_v6_send_check().

Should netif_needs_gso() handle the SKB_GSO_FRAGLIST case, or does the unverified state need a marker other than CHECKSUM_NONE, so the transmit path keeps treating these skbs as it did before?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123042735.139550-1-rosenp%40gmail.com