Skip to contentSkip to conversation

xsk: handle two header issues

PATCH Superseded Jason Xing 4 participants 9 messages Nov 27 – Nov 29

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jason Xing

author
#0 lore raw

v2 cover xsk: handle two header issues

Cover more potential mac_headers cases in __dev_direct_xmit, including xsk.

Initialize the metadata of headers in xsk.

---
V2

1. address issues reported by sashiko
2. introduce 0001 patch to prevent the changes from 0002 casuing ICE unintended buggy behavior because of using mac_header in ICE.

Jason Xing (2):
net: reset the mac header in __dev_direct_xmit()
xsk: fill in header offsets for TX checksum offload

 net/core/dev.c |  2 ++
 net/xdp/xsk.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 47 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH v2 1/2 net: reset the mac header in __dev_direct_xmit()

Compared to __dev_queue_xmit(), __dev_direct_xmit() is the only open gate for skbs whose mac_header might not be initialized. A few months ago commit c2707480cfbf ("net/packet: reset the MAC header on the packet-socket transmit path") solves the case where AF_PACKET skb can miss resetting mac header if a few conditions in packet_parse_headers() are not satisified and it will cause out-of-bounds access.

Similarly, Petr reported an issue about AF_XDP path where copy mode can also trigger the uninitialized mac header problem.

In reply to Petr's patch, Eric suggested to add the reset logic in a general layer to stop future issues like this happening instead of dealing with scattered cases one by one[1]. So anchor the mac header in __dev_direct_xmit() exactly as __dev_queue_xmit() does, before validate_xmit_skb_list() so that ndo_features_check() and the vlan helpers see it too.

For callers that already reset the header the operation is idempotent.

[1]: https://lore.kernel.org/all/CANn89i+aacS2PfaMXnimzGgbCLz-JSnqo6QyoS122Gf+kxya3w@mail.gmail.com/

---
1. this patch will be backported to stable branches to fix the xsk problem and will also be merged in net-next. In the -next branch, I'm going to follow Eric's thoughts to handle the mac_header 'issues' from the driver side, and then finally remove the guarantee (reseting mac header in the core stack layer) like this patch and that af_packet case.
2. this patch is also a prep for the second patch in the series.

---
 net/core/dev.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/dev.c +2 -0
index 3c58db732761..6ea2fd3506f7 100644
@@ -5149,6 +5149,8 @@ int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
 		     !netif_carrier_ok(dev)))
 		goto drop;
 
+	skb_reset_mac_header(skb);
+
 	skb = validate_xmit_skb_list(skb, dev, &again);
 	if (skb != orig_skb)
 		goto drop;
Signature · 1 line
2.43.7
author
#3 lore raw

PATCH v2 2/2 xsk: fill in header offsets for TX checksum offload

In reply to Jason Xing

An XDP_TXMD_FLAGS_CHECKSUM request is turned into an skb carrying CHECKSUM_PARTIAL by xsk_skb_metadata() where csum_start/csum_offset/ ip_summed are set.

However skb->protocol stays 0, the offload never actually reaches the hardware and checksum ends up being computed in software every time (please see skb_checksum_help() in xsk path):
1. harmonize_features() strips the checksum features on devices.
2. ice_features_check() rejects the frame on NETIF_F_HW_CSUM capable ice.

The frame layout comes from user space, and request.csum_start is already the offset of the L4 header from the start of the frame, so the transport header is known exactly.

Only the L2 length has to be found, by parsing the Ethernet header and, when present, the VLAN tag stack. skb_header_pointer() is used for that, so it works for frames whose data is not in the linear part of the skb.

skb->protocol is set to the outer ethertype, as the rest of the stack does for in-band VLAN tags, so skb_vlan_tagged() and vlan_features_check() keep seeing the tags. can_checksum_protocol() and the drivers get the L3 type through vlan_get_protocol(). The mac header is anchored by __dev_direct_xmit() in the previous patch.

A frame whose L2 header cannot be parsed, whose L3 type is not IPv4 or IPv6, or whose csum_start does not leave room for an L3 header cannot be described to the hardware, so it keeps being checksummed in software exactly as before.

The patch is based on Maciej suggestion: 1) do not rely on mac header that probe in AF_PACKET uses, 2) use skb_header_pointer and walk vlan tags to inspect the accurate location of each field that existing drivers possibly use.

[1]: https://lore.kernel.org/all/arKm5gCaERk6MaGA@boxer/

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)
net/xdp/xsk.c +45 -2
index 52d538160e0a..8f566996a6dd 100644
@@ -11,8 +11,12 @@
 
 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
 
+#include <linux/if_arp.h>
+#include <linux/if_vlan.h>
 #include <linux/if_xdp.h>
 #include <linux/init.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/task.h>
@@ -1027,9 +1031,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 
 static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
 {
-	if (unlikely(pool->tx_sw_csum &&
-		     skb->ip_summed == CHECKSUM_PARTIAL))
+	struct ethhdr eth_buf, *eth;
+	int nhoff = ETH_HLEN;
+	unsigned int l3_min;
+	__be16 proto;
+
+	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
+		return 0;
+
+	if (unlikely(pool->tx_sw_csum))
+		return skb_checksum_help(skb);
+
+	if (unlikely(skb->dev->type != ARPHRD_ETHER))
+		return 0;
+
+	eth = skb_header_pointer(skb, 0, sizeof(*eth), &eth_buf);
+	if (unlikely(!eth))
+		return skb_checksum_help(skb);
+
+	proto = eth->h_proto;
+	if (eth_type_vlan(proto)) {
+		proto = __vlan_get_protocol(skb, proto, &nhoff);
+		if (unlikely(!proto))
+			return skb_checksum_help(skb);
+	}
+
+	switch (proto) {
+	case htons(ETH_P_IP):
+		l3_min = sizeof(struct iphdr);
+		break;
+	case htons(ETH_P_IPV6):
+		l3_min = sizeof(struct ipv6hdr);
+		break;
+	default:
 		return skb_checksum_help(skb);
+	}
+
+	if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
+		return skb_checksum_help(skb);
+
+	skb->protocol = eth->h_proto;
+	skb_set_network_header(skb, nhoff);
+	skb_set_transport_header(skb, skb_checksum_start_offset(skb));
 
 	return 0;
 }
Signature · 1 line
2.43.7
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH net v2 1/2] net: reset the mac header in __dev_direct_xmit()

In reply to Jason Xing
Quoted 19 lines · Jason Xing

Compared to __dev_queue_xmit(), __dev_direct_xmit() is the only open gate for skbs whose mac_header might not be initialized. A few months ago commit c2707480cfbf ("net/packet: reset the MAC header on the packet-socket transmit path") solves the case where AF_PACKET skb can miss resetting mac header if a few conditions in packet_parse_headers() are not satisified and it will cause out-of-bounds access.

Similarly, Petr reported an issue about AF_XDP path where copy mode can also trigger the uninitialized mac header problem.

In reply to Petr's patch, Eric suggested to add the reset logic in a general layer to stop future issues like this happening instead of dealing with scattered cases one by one[1]. So anchor the mac header in __dev_direct_xmit() exactly as __dev_queue_xmit() does, before validate_xmit_skb_list() so that ndo_features_check() and the vlan helpers see it too.

For callers that already reset the header the operation is idempotent.

Quoted 8 lines

[1]: https://lore.kernel.org/all/CANn89i+aacS2PfaMXnimzGgbCLz-JSnqo6QyoS122Gf+kxya3w@mail.gmail.com/

#4 lore raw

Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload

In reply to Jason Xing
Quoted 62 lines · Jason Xing

An XDP_TXMD_FLAGS_CHECKSUM request is turned into an skb carrying CHECKSUM_PARTIAL by xsk_skb_metadata() where csum_start/csum_offset/ ip_summed are set.

However skb->protocol stays 0, the offload never actually reaches the hardware and checksum ends up being computed in software every time (please see skb_checksum_help() in xsk path):
1. harmonize_features() strips the checksum features on devices.
2. ice_features_check() rejects the frame on NETIF_F_HW_CSUM capable ice.

The frame layout comes from user space, and request.csum_start is already the offset of the L4 header from the start of the frame, so the transport header is known exactly.

Only the L2 length has to be found, by parsing the Ethernet header and, when present, the VLAN tag stack. skb_header_pointer() is used for that, so it works for frames whose data is not in the linear part of the skb.

skb->protocol is set to the outer ethertype, as the rest of the stack does for in-band VLAN tags, so skb_vlan_tagged() and vlan_features_check() keep seeing the tags. can_checksum_protocol() and the drivers get the L3 type through vlan_get_protocol(). The mac header is anchored by __dev_direct_xmit() in the previous patch.

A frame whose L2 header cannot be parsed, whose L3 type is not IPv4 or IPv6, or whose csum_start does not leave room for an L3 header cannot be described to the hardware, so it keeps being checksummed in software exactly as before.

The patch is based on Maciej suggestion: 1) do not rely on mac header that probe in AF_PACKET uses, 2) use skb_header_pointer and walk vlan tags to inspect the accurate location of each field that existing drivers possibly use.

[1]: https://lore.kernel.org/all/arKm5gCaERk6MaGA@boxer/

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)
net/xdp/xsk.c +4 -0
index 52d538160e0a..8f566996a6dd 100644
@@ -11,8 +11,12 @@
 
 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
 
+#include <linux/if_arp.h>
+#include <linux/if_vlan.h>
 #include <linux/if_xdp.h>
 #include <linux/init.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/task.h>

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1032

Quoted 2 lines
net/xdp/xsk.c +0 -0
@@ -1027,9 +1031,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 
 static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)

nit: Name! Now this helper has a second job; parse the Ethernet/VLAN headers and set up the skb for hardware offload, even though the application didn't ask to force software.

Quoted 32 lines
net/xdp/xsk.c +29 -2
 {
-	if (unlikely(pool->tx_sw_csum &&
-		     skb->ip_summed == CHECKSUM_PARTIAL))
+	struct ethhdr eth_buf, *eth;
+	int nhoff = ETH_HLEN;
+	unsigned int l3_min;
+	__be16 proto;
+
+	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
+		return 0;
+
+	if (unlikely(pool->tx_sw_csum))
+		return skb_checksum_help(skb);
+
+	if (unlikely(skb->dev->type != ARPHRD_ETHER))
+		return 0;
+
+	eth = skb_header_pointer(skb, 0, sizeof(*eth), &eth_buf);
+	if (unlikely(!eth))
+		return skb_checksum_help(skb);
+
+	proto = eth->h_proto;
+	if (eth_type_vlan(proto)) {
+		proto = __vlan_get_protocol(skb, proto, &nhoff);
+		if (unlikely(!proto))
+			return skb_checksum_help(skb);
+	}
+
+	switch (proto) {
+	case htons(ETH_P_IP):
+		l3_min = sizeof(struct iphdr);
+		break;

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1072

Quoted 10 lines
net/xdp/xsk.c +9 -0
+	case htons(ETH_P_IPV6):
+		l3_min = sizeof(struct ipv6hdr);
+		break;
+	default:
 		return skb_checksum_help(skb);
+	}
+
+	if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
+		return skb_checksum_help(skb);
+

Forcing software csum when parsing fails seems wrong. Can we return 0 and leave the skb unchanged instead?

Take Ethernet/MPLS/IPv4/UDP with one MPLS label. UDP starts at byte 38 and its checksum field is at byte 44. The parser stops at MPLS, but with TX checksum offload enabled, virtio can still pass csum_start=38 and csum_offset=6 to the backend.

skb_checksum_help() can sum bytes in fragments, but it requires the checksum start and the result field to be in the linear head. Virtio sets IFF_TX_SKB_NO_LINEAR, so AF_XDP puts all packet bytes in fragments. The helper returns -EINVAL and AF_XDP drops the packet.

Returning 0 leaves CHECKSUM_PARTIAL and both offsets intact, so the normal TX path can handle the request as before. The old XDP_UMEM_TX_SW_CSUM should case is ok!

Björn

#6 lore raw

Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload

In reply to Jason Xing
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] xsk: skb_checksum_help() warning in xsk_skb_sw_csum() on nonlinear skb - [Medium] xsk: missing…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] xsk: skb_checksum_help() warning in xsk_skb_sw_csum() on nonlinear skb
- [Medium] xsk: missing skb_checksum_help() fallback in xsk_skb_sw_csum() for non-Ethernet devs
--

commit 56dbb0a2d6a12181bfa80e8eb8b24e6cbe5b0a5c
Author: Jason Xing <[email protected]>

xsk: fill in header offsets for TX checksum offload

This commit parses the Ethernet and VLAN headers in xsk_skb_sw_csum() to populate skb->protocol, the network header offset, and the transport header offset for AF_XDP TX checksum offload, falling back to skb_checksum_help() when header validation fails.

Quoted 4 lines
net/xdp/xsk.c +0 -0
index 18cbd50047d80..3827d8e4b642a 100644

[ ... ]

Quoted 9 lines
net/xdp/xsk.c +3 -2
@@ -1028,9 +1032,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 
 static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
 {
-	if (unlikely(pool->tx_sw_csum &&
-		     skb->ip_summed == CHECKSUM_PARTIAL))
+	struct ethhdr eth_buf, *eth;
+	int nhoff = ETH_HLEN;
+	unsigned int l3_min;

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1047

Quoted 10 lines
net/xdp/xsk.c +10 -0
+	__be16 proto;
+
+	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
+		return 0;
+
+	if (unlikely(pool->tx_sw_csum))
+		return skb_checksum_help(skb);
+
+	if (unlikely(skb->dev->type != ARPHRD_ETHER))
+		return 0;

[Severity: Medium]
Should non-ARPHRD_ETHER devices fall back to software checksumming instead of returning 0 here?

Returning 0 leaves skb->ip_summed as CHECKSUM_PARTIAL with skb->protocol still 0 and csum_start unvalidated. On non-ARPHRD_ETHER devices that advertise NETIF_F_HW_CSUM (such as loopback with ARPHRD_LOOPBACK or tunnel devices), can_checksum_protocol() returns true even for protocol 0, so validate_xmit_skb() does not call skb_checksum_help() either. On loopback, if csum_start < ETH_HLEN, loopback_xmit() pulls ETH_HLEN bytes and delivers a CHECKSUM_PARTIAL skb with a negative skb_checksum_start_offset(), triggering a WARN_ONCE() in skb_checksum_help() on the receive path.

Quoted 15 lines
net/xdp/xsk.c +15 -0
+
+	eth = skb_header_pointer(skb, 0, sizeof(*eth), &eth_buf);
+	if (unlikely(!eth))
+		return skb_checksum_help(skb);
+
+	proto = eth->h_proto;
+	if (eth_type_vlan(proto)) {
+		proto = __vlan_get_protocol(skb, proto, &nhoff);
+		if (unlikely(!proto))
+			return skb_checksum_help(skb);
+	}
+
+	switch (proto) {
+	case htons(ETH_P_IP):
+		l3_min = sizeof(struct iphdr);

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1071

Quoted 10 lines
net/xdp/xsk.c +9 -0
+		break;
+	case htons(ETH_P_IPV6):
+		l3_min = sizeof(struct ipv6hdr);
+		break;
+	default:
 		return skb_checksum_help(skb);
+	}
+
+	if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
+		return skb_checksum_help(skb);

[Severity: Medium]
Can calling skb_checksum_help() on these fallback paths trigger a WARN_ONCE() when the socket is bound to an IFF_TX_SKB_NO_LINEAR device such as virtio_net?

d73a9a63f9f7 ("xsk: reject sw-csum UMEM binding to IFF_TX_SKB_NO_LINEAR devices") rejected binding in xp_assign_dev() only when pool->tx_sw_csum is set. When pool->tx_sw_csum is false, xsk_build_skb_zerocopy() still builds skbs with skb_headlen(skb) == 0 on IFF_TX_SKB_NO_LINEAR devices. If a frame with XDP_TXMD_FLAGS_CHECKSUM hits any of these fallback branches (for example, a non-IP ethertype or a csum_start smaller than nhoff + l3_min), skb_checksum_help() sees offset >= skb_headlen(skb), fires WARN_ONCE(), and returns -EINVAL.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload

In reply to Jason Xing
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

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

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

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] This patch adds skb_checksum_help() calls in xsk_skb_sw_csum() (net/xdp/xsk.c) that run even when…
- [Medium] When pool->tx_sw_csum is false, xsk_skb_sw_csum() now checksums on the CPU every Ethernet CHECKSUM_PARTIAL frame it cannot…
- [Medium] xsk_skb_sw_csum() now sets skb->protocol, network_header and transport_header from a frame fully controlled by user space.
- [Medium] xsk_skb_sw_csum() now passes AF_XDP copy-mode CHECKSUM_PARTIAL frames to protocol-aware or unit-constrained hardware…

--

xsk: fill in header offsets for TX checksum offload

This patch makes xsk_skb_sw_csum() parse the Ethernet and VLAN headers of CHECKSUM_PARTIAL AF_XDP copy-mode frames. It then sets skb->protocol and the network and transport headers so the checksum request can be offloaded to hardware. Frames whose L2 header can't be parsed, that aren't IPv4/IPv6, or whose csum_start leaves no room for an L3 header fall back to skb_checksum_help().

Quoted 4 lines
net/xdp/xsk.c +0 -0
index 18cbd50047d80..3827d8e4b642a 100644

[ ... ]

Quoted 13 lines
net/xdp/xsk.c +7 -2
@@ -1028,9 +1032,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 
 static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
 {
-	if (unlikely(pool->tx_sw_csum &&
-		     skb->ip_summed == CHECKSUM_PARTIAL))
+	struct ethhdr eth_buf, *eth;
+	int nhoff = ETH_HLEN;
+	unsigned int l3_min;
+	__be16 proto;
+
+	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
+		return 0;

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1051

Quoted 10 lines
net/xdp/xsk.c +10 -0
+
+	if (unlikely(pool->tx_sw_csum))
+		return skb_checksum_help(skb);
+
+	if (unlikely(skb->dev->type != ARPHRD_ETHER))
+		return 0;
+
+	eth = skb_header_pointer(skb, 0, sizeof(*eth), &eth_buf);
+	if (unlikely(!eth))
+		return skb_checksum_help(skb);

[Severity: High]
Can this fallback, and the other new skb_checksum_help() fallbacks below, break copy-mode transmit on IFF_TX_SKB_NO_LINEAR devices such as virtio_net?

On those devices xsk_build_skb() goes through xsk_build_skb_zerocopy(), which only reserves headroom in the linear area:

net/xdp/xsk.c:xsk_build_skb_zerocopy() {
    ...
		skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
		...
		skb_reserve(skb, hr);
    ...
}

That leaves skb_headlen() at 0. All of the payload sits in page frags attached with skb_fill_page_desc(), which doesn't set SKBFL_SHARED_FRAG. So skb_checksum_help() doesn't linearize, and it hits this check:

net/core/dev.c:skb_checksum_help() {
    ...
	if (unlikely(offset >= skb_headlen(skb))) {
		DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
		WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n",
    ...
}

It then returns -EINVAL, so __xsk_generic_xmit() drops the frame and sendmsg() fails:

		err = xsk_skb_sw_csum(skb, xs->pool);
		if (unlikely(err)) {
			xsk_drop_skb(skb);
			goto out;
		}

User space can reach every one of these fallbacks, because xsk_skb_metadata() only checks csum_start + csum_offset + 2 <= desc->len. Any of these frames will do:

- a frame shorter than ETH_HLEN
- an unparsable VLAN stack
- a non-IPv4/IPv6 ethertype
- a csum_start below nhoff + 20 (IPv4) or nhoff + 40 (IPv6)

Before this patch, these frames returned 0 here when tx_sw_csum was off. xp_assign_dev() also refuses tx_sw_csum on these devices, precisely so that skb_checksum_help() is never reached for them:

	if (pool->tx_sw_csum && (netdev->priv_flags & IFF_TX_SKB_NO_LINEAR))
		return -EOPNOTSUPP;

virtio_net advertises NETIF_F_HW_CSUM, so the device computed the checksum. With this patch the frame is dropped instead, and the user can trigger a WARN_ONCE plus skb_dump. On panic_on_warn systems that is a panic.

The commit message says these frames "keep being checksummed in software exactly as before". Would returning 0 in the fallback cases work better? That would leave the decision to validate_xmit_skb() as it was before.

Quoted 7 lines
net/xdp/xsk.c +7 -0
+
+	proto = eth->h_proto;
+	if (eth_type_vlan(proto)) {
+		proto = __vlan_get_protocol(skb, proto, &nhoff);
+		if (unlikely(!proto))
+			return skb_checksum_help(skb);
+	}

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1067

Quoted 10 lines
net/xdp/xsk.c +9 -0
+
+	switch (proto) {
+	case htons(ETH_P_IP):
+		l3_min = sizeof(struct iphdr);
+		break;
+	case htons(ETH_P_IPV6):
+		l3_min = sizeof(struct ipv6hdr);
+		break;
+	default:
 		return skb_checksum_help(skb);

[Severity: Medium]
Leaving the non-linear case aside, does this take checksum offload away from frames that were offloaded before this patch?

Even with skb->protocol == 0, a NETIF_F_HW_CSUM device still offloaded the frame:

include/linux/netdevice.h:can_checksum_protocol() {
    ...
	if (features & NETIF_F_HW_CSUM) {
		/* Can checksum everything */
		return true;
	}
    ...
}

skb_csum_hwoffload_help() also returns 0 for NETIF_F_HW_CSUM. On virtio_net, veth and other HW_CSUM devices, non-IP or unparsable frames are now checksummed on the CPU instead.

The policy also looks inconsistent. Non-ARPHRD_ETHER devices return 0 and keep offload, but Ethernet devices with an unknown L3 type are forced into software.

Documentation/networking/xsk-tx-metadata.rst says XDP_TXMD_FLAGS_CHECKSUM asks the device to compute the checksum. It describes CPU checksumming as the XDP_UMEM_TX_SW_CSUM option.

For HW_CSUM devices the commit message doesn't match this behaviour either:

However skb->protocol stays 0, the offload never actually reaches the hardware and checksum ends up being computed in software every time

and:

... cannot be described to the hardware, so it keeps being checksummed in software exactly as before.

Since this is tagged for stable, could the commit message describe the behaviour change accurately? Alternatively, could these fallbacks return 0 so the core path keeps making the decision?

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1071

Quoted 4 lines
net/xdp/xsk.c +4 -0
+	}
+
+	if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
+		return skb_checksum_help(skb);

[Severity: Medium]
Is checking only the minimal L3 header size enough before handing the frame to protocol-aware driver checksum paths?

xsk_skb_metadata() only guarantees csum_start + csum_offset + 2 <= desc->len. Nothing guarantees that a full L4 header exists at csum_start.

Before this patch, harmonize_features() or ice_features_check() kept these frames away from the driver offload path. Now take ice E810, which has IP_CSUM|IPV6_CSUM but no HW_CSUM. ice_tx_csum() takes the L4 protocol from the user-controlled ip.v4->protocol, and for TCP it reads:

drivers/net/ethernet/intel/ice/ice_txrx.c:ice_tx_csum() {
    ...
	case IPPROTO_TCP:
		/* enable checksum offloads */
		cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
		l4_len = l4.tcp->doff;
    ...
}

For example, take eth(14) + IPv4 with protocol=TCP, csum_start=34, csum_offset=6 and desc->len=42:

- skb_csum_hwoffload_help() accepts it, since csum_offset 6 equals offsetof(struct udphdr, check)
- ice_features_check() accepts MACLEN 14 and IPLEN 20

doff is then read at transport_header + 12, up to 5 bytes past skb->tail. The read stays inside the skb head allocation, but that memory is uninitialized and KMSAN would flag it.

The value read is programmed into the descriptor's L4LEN field. Whether a bogus L4LEN can trigger NIC malicious driver detection or TX hangs hasn't been verified.

AF_PACKET with vnet_hdr has a similar weakness, but this patch newly exposes the AF_XDP path to it.

Should this require a full TCP/UDP header past csum_start, consistent with csum_offset, before setting the transport header and skipping the software checksum?

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1075

Quoted 4 lines
net/xdp/xsk.c +4 -0
+
+	skb->protocol = eth->h_proto;
+	skb_set_network_header(skb, nhoff);
+	skb_set_transport_header(skb, skb_checksum_start_offset(skb));

[Severity: Medium]
Can the hardware end up writing the checksum somewhere other than the requested csum_start/csum_offset?

Only the ethertype and csum_start >= nhoff + l3_min are checked here. Drivers, though, program these offsets in coarser units or take the L4 type from the packet itself. Three cases on ice show this.

First, the legacy path (E810) programs IPLEN in dwords:

	l3_len = l4.hdr - ip.hdr;
	offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;

ice_features_check() only rejects odd or oversized lengths:

	len = skb_network_header_len(skb);
	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
		goto out_rm_features;

An L3 length of 22 therefore becomes 5 dwords, and the L4 checksum starts 2 bytes before csum_start.

Second, the L4 type comes from ip.v4->protocol or the IPv6 nexthdr, not from csum_offset. skb_csum_hwoffload_help() only requires csum_offset to be 16 or 6.

With protocol=TCP and csum_offset=6, the device writes the TCP checksum at L4+16 rather than L4+6. With protocol=SCTP (csum_not_inet is 0), it writes a CRC32c at L4+8 rather than a 16-bit Internet checksum.

Third, the GCS path (E830, HW_CSUM) uses:

		u16 csum_offset = skb->csum_offset / 2;

That rounds an odd csum_offset down, so the result lands one byte early.

Before this patch, skb_checksum_help() computed all of these at exactly csum_start/csum_offset. With this patch, other bytes of the transmitted frame get overwritten. So the commit message's "exactly as before" claim doesn't hold for these frames either.

Would it make sense to add these checks, or else leave the decision to the core path?

- IPv4 ihl * 4 (or the IPv6 header plus extension headers) equals csum_start - nhoff
- the L4 protocol is TCP or UDP with a matching csum_offset (16 or 6)
- the offsets are even

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127105347.72997-1-kerneljasonxing%40gmail.com
author
#5 lore raw

Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload

In reply to Björn Töpel
Quoted 66 lines · Björn Töpel
Quoted 63 lines · Jason Xing

An XDP_TXMD_FLAGS_CHECKSUM request is turned into an skb carrying CHECKSUM_PARTIAL by xsk_skb_metadata() where csum_start/csum_offset/ ip_summed are set.

However skb->protocol stays 0, the offload never actually reaches the hardware and checksum ends up being computed in software every time (please see skb_checksum_help() in xsk path):
1. harmonize_features() strips the checksum features on devices.
2. ice_features_check() rejects the frame on NETIF_F_HW_CSUM capable ice.

The frame layout comes from user space, and request.csum_start is already the offset of the L4 header from the start of the frame, so the transport header is known exactly.

Only the L2 length has to be found, by parsing the Ethernet header and, when present, the VLAN tag stack. skb_header_pointer() is used for that, so it works for frames whose data is not in the linear part of the skb.

skb->protocol is set to the outer ethertype, as the rest of the stack does for in-band VLAN tags, so skb_vlan_tagged() and vlan_features_check() keep seeing the tags. can_checksum_protocol() and the drivers get the L3 type through vlan_get_protocol(). The mac header is anchored by __dev_direct_xmit() in the previous patch.

A frame whose L2 header cannot be parsed, whose L3 type is not IPv4 or IPv6, or whose csum_start does not leave room for an L3 header cannot be described to the hardware, so it keeps being checksummed in software exactly as before.

The patch is based on Maciej suggestion: 1) do not rely on mac header that probe in AF_PACKET uses, 2) use skb_header_pointer and walk vlan tags to inspect the accurate location of each field that existing drivers possibly use.

[1]: https://lore.kernel.org/all/arKm5gCaERk6MaGA@boxer/

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)
net/xdp/xsk.c +4 -0
index 52d538160e0a..8f566996a6dd 100644
@@ -11,8 +11,12 @@
 
 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
 
+#include <linux/if_arp.h>
+#include <linux/if_vlan.h>
 #include <linux/if_xdp.h>
 #include <linux/init.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/task.h>
@@ -1027,9 +1031,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 
Quoted 5 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1032

Quoted 1 line
net/xdp/xsk.c +0 -0
 static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)

nit: Name! Now this helper has a second job; parse the Ethernet/VLAN headers and set up the skb for hardware offload, even though the application didn't ask to force software.

Thanks for the review.

Right, I will accordingly rename it.

Quoted 52 lines
Quoted 33 lines
net/xdp/xsk.c +29 -2
 {
-     if (unlikely(pool->tx_sw_csum &&
-                  skb->ip_summed == CHECKSUM_PARTIAL))
+     struct ethhdr eth_buf, *eth;
+     int nhoff = ETH_HLEN;
+     unsigned int l3_min;
+     __be16 proto;
+
+     if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
+             return 0;
+
+     if (unlikely(pool->tx_sw_csum))
+             return skb_checksum_help(skb);
+
+     if (unlikely(skb->dev->type != ARPHRD_ETHER))
+             return 0;
+
+     eth = skb_header_pointer(skb, 0, sizeof(*eth), &eth_buf);
+     if (unlikely(!eth))
+             return skb_checksum_help(skb);
+
+     proto = eth->h_proto;
+     if (eth_type_vlan(proto)) {
+             proto = __vlan_get_protocol(skb, proto, &nhoff);
+             if (unlikely(!proto))
+                     return skb_checksum_help(skb);
+     }
+
+     switch (proto) {
+     case htons(ETH_P_IP):
+             l3_min = sizeof(struct iphdr);
+             break;

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1072

Quoted 10 lines
net/xdp/xsk.c +9 -0
+     case htons(ETH_P_IPV6):
+             l3_min = sizeof(struct ipv6hdr);
+             break;
+     default:
              return skb_checksum_help(skb);
+     }
+
+     if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
+             return skb_checksum_help(skb);
+

Forcing software csum when parsing fails seems wrong. Can we return 0 and leave the skb unchanged instead?

Take Ethernet/MPLS/IPv4/UDP with one MPLS label. UDP starts at byte 38 and its checksum field is at byte 44. The parser stops at MPLS, but with TX checksum offload enabled, virtio can still pass csum_start=38 and csum_offset=6 to the backend.

Quoted 8 lines

skb_checksum_help() can sum bytes in fragments, but it requires the checksum start and the result field to be in the linear head. Virtio sets IFF_TX_SKB_NO_LINEAR, so AF_XDP puts all packet bytes in fragments. The helper returns -EINVAL and AF_XDP drops the packet.

Returning 0 leaves CHECKSUM_PARTIAL and both offsets intact, so the normal TX path can handle the request as before. The old XDP_UMEM_TX_SW_CSUM should case is ok!

Point taken. Thanks.

I also noticed there are three places that break the previous behavior as Sashiko pointed out.

Thanks,
Jason

author
#8 lore raw

Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload

In reply to [email protected]
Quoted 116 lines · On Sat, Nov 28, 2026 at 7:47 PM <[email protected]> wrote:

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

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

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] This patch adds skb_checksum_help() calls in xsk_skb_sw_csum() (net/xdp/xsk.c) that run even when…
- [Medium] When pool->tx_sw_csum is false, xsk_skb_sw_csum() now checksums on the CPU every Ethernet CHECKSUM_PARTIAL frame it cannot…
- [Medium] xsk_skb_sw_csum() now sets skb->protocol, network_header and transport_header from a frame fully controlled by user space.
- [Medium] xsk_skb_sw_csum() now passes AF_XDP copy-mode CHECKSUM_PARTIAL frames to protocol-aware or unit-constrained hardware…

--

xsk: fill in header offsets for TX checksum offload

This patch makes xsk_skb_sw_csum() parse the Ethernet and VLAN headers of CHECKSUM_PARTIAL AF_XDP copy-mode frames. It then sets skb->protocol and the network and transport headers so the checksum request can be offloaded to hardware. Frames whose L2 header can't be parsed, that aren't IPv4/IPv6, or whose csum_start leaves no room for an L3 header fall back to skb_checksum_help().

Quoted 4 lines
net/xdp/xsk.c +0 -0
index 18cbd50047d80..3827d8e4b642a 100644

[ ... ]

Quoted 13 lines
net/xdp/xsk.c +7 -2
@@ -1028,9 +1032,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 
 static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
 {
-     if (unlikely(pool->tx_sw_csum &&
-                  skb->ip_summed == CHECKSUM_PARTIAL))
+     struct ethhdr eth_buf, *eth;
+     int nhoff = ETH_HLEN;
+     unsigned int l3_min;
+     __be16 proto;
+
+     if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
+             return 0;

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1051

Quoted 10 lines
net/xdp/xsk.c +10 -0
+
+     if (unlikely(pool->tx_sw_csum))
+             return skb_checksum_help(skb);
+
+     if (unlikely(skb->dev->type != ARPHRD_ETHER))
+             return 0;
+
+     eth = skb_header_pointer(skb, 0, sizeof(*eth), &eth_buf);
+     if (unlikely(!eth))
+             return skb_checksum_help(skb);

[Severity: High]
Can this fallback, and the other new skb_checksum_help() fallbacks below, break copy-mode transmit on IFF_TX_SKB_NO_LINEAR devices such as virtio_net?

On those devices xsk_build_skb() goes through xsk_build_skb_zerocopy(), which only reserves headroom in the linear area:

net/xdp/xsk.c:xsk_build_skb_zerocopy() {
    ...
                skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
                ...
                skb_reserve(skb, hr);
    ...
}

That leaves skb_headlen() at 0. All of the payload sits in page frags attached with skb_fill_page_desc(), which doesn't set SKBFL_SHARED_FRAG. So skb_checksum_help() doesn't linearize, and it hits this check:

net/core/dev.c:skb_checksum_help() {
    ...
        if (unlikely(offset >= skb_headlen(skb))) {
                DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
                WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n",
    ...
}

It then returns -EINVAL, so __xsk_generic_xmit() drops the frame and sendmsg() fails:

                err = xsk_skb_sw_csum(skb, xs->pool);
                if (unlikely(err)) {
                        xsk_drop_skb(skb);
                        goto out;
                }

User space can reach every one of these fallbacks, because xsk_skb_metadata() only checks csum_start + csum_offset + 2 <= desc->len. Any of these frames will do:

- a frame shorter than ETH_HLEN
- an unparsable VLAN stack
- a non-IPv4/IPv6 ethertype
- a csum_start below nhoff + 20 (IPv4) or nhoff + 40 (IPv6)

Before this patch, these frames returned 0 here when tx_sw_csum was off. xp_assign_dev() also refuses tx_sw_csum on these devices, precisely so that skb_checksum_help() is never reached for them:

        if (pool->tx_sw_csum && (netdev->priv_flags & IFF_TX_SKB_NO_LINEAR))
                return -EOPNOTSUPP;
Quoted 8 lines

virtio_net advertises NETIF_F_HW_CSUM, so the device computed the checksum. With this patch the frame is dropped instead, and the user can trigger a WARN_ONCE plus skb_dump. On panic_on_warn systems that is a panic.

The commit message says these frames "keep being checksummed in software exactly as before". Would returning 0 in the fallback cases work better? That would leave the decision to validate_xmit_skb() as it was before.

Right, Bjorn also flagged this.

Quoted 53 lines
Quoted 8 lines
net/xdp/xsk.c +7 -0
+
+     proto = eth->h_proto;
+     if (eth_type_vlan(proto)) {
+             proto = __vlan_get_protocol(skb, proto, &nhoff);
+             if (unlikely(!proto))
+                     return skb_checksum_help(skb);
+     }

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1067

Quoted 10 lines
net/xdp/xsk.c +9 -0
+
+     switch (proto) {
+     case htons(ETH_P_IP):
+             l3_min = sizeof(struct iphdr);
+             break;
+     case htons(ETH_P_IPV6):
+             l3_min = sizeof(struct ipv6hdr);
+             break;
+     default:
              return skb_checksum_help(skb);

[Severity: Medium]
Leaving the non-linear case aside, does this take checksum offload away from frames that were offloaded before this patch?

Even with skb->protocol == 0, a NETIF_F_HW_CSUM device still offloaded the frame:

include/linux/netdevice.h:can_checksum_protocol() {
    ...
        if (features & NETIF_F_HW_CSUM) {
                /* Can checksum everything */
                return true;
        }
    ...
}

skb_csum_hwoffload_help() also returns 0 for NETIF_F_HW_CSUM. On virtio_net, veth and other HW_CSUM devices, non-IP or unparsable frames are now checksummed on the CPU instead.

The policy also looks inconsistent. Non-ARPHRD_ETHER devices return 0 and keep offload, but Ethernet devices with an unknown L3 type are forced into software.

Documentation/networking/xsk-tx-metadata.rst says XDP_TXMD_FLAGS_CHECKSUM asks the device to compute the checksum. It describes CPU checksumming as the XDP_UMEM_TX_SW_CSUM option.

For HW_CSUM devices the commit message doesn't match this behaviour either:

However skb->protocol stays 0, the offload never actually reaches the hardware and checksum ends up being computed in software every time

Quoted 8 lines

and:

... cannot be described to the hardware, so it keeps being checksummed in software exactly as before.

Since this is tagged for stable, could the commit message describe the behaviour change accurately? Alternatively, could these fallbacks return 0 so the core path keeps making the decision?

Will return 0 here.

Quoted 39 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1071

Quoted 5 lines
net/xdp/xsk.c +4 -0
+     }
+
+     if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
+             return skb_checksum_help(skb);

[Severity: Medium]
Is checking only the minimal L3 header size enough before handing the frame to protocol-aware driver checksum paths?

xsk_skb_metadata() only guarantees csum_start + csum_offset + 2 <= desc->len. Nothing guarantees that a full L4 header exists at csum_start.

Before this patch, harmonize_features() or ice_features_check() kept these frames away from the driver offload path. Now take ice E810, which has IP_CSUM|IPV6_CSUM but no HW_CSUM. ice_tx_csum() takes the L4 protocol from the user-controlled ip.v4->protocol, and for TCP it reads:

drivers/net/ethernet/intel/ice/ice_txrx.c:ice_tx_csum() {
    ...
        case IPPROTO_TCP:
                /* enable checksum offloads */
                cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
                l4_len = l4.tcp->doff;
    ...
}

For example, take eth(14) + IPv4 with protocol=TCP, csum_start=34, csum_offset=6 and desc->len=42:

- skb_csum_hwoffload_help() accepts it, since csum_offset 6 equals offsetof(struct udphdr, check)
- ice_features_check() accepts MACLEN 14 and IPLEN 20

doff is then read at transport_header + 12, up to 5 bytes past skb->tail. The read stays inside the skb head allocation, but that memory is uninitialized and KMSAN would flag it.

Quoted 5 lines

The value read is programmed into the descriptor's L4LEN field. Whether a bogus L4LEN can trigger NIC malicious driver detection or TX hangs hasn't been verified.

AF_PACKET with vnet_hdr has a similar weakness, but this patch newly

yes

Quoted 5 lines

exposes the AF_XDP path to it.

Should this require a full TCP/UDP header past csum_start, consistent with csum_offset, before setting the transport header and skipping the software checksum?

Same here.

Quoted 42 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 1075

Quoted 5 lines
net/xdp/xsk.c +4 -0
+
+     skb->protocol = eth->h_proto;
+     skb_set_network_header(skb, nhoff);
+     skb_set_transport_header(skb, skb_checksum_start_offset(skb));

[Severity: Medium]
Can the hardware end up writing the checksum somewhere other than the requested csum_start/csum_offset?

Only the ethertype and csum_start >= nhoff + l3_min are checked here. Drivers, though, program these offsets in coarser units or take the L4 type from the packet itself. Three cases on ice show this.

First, the legacy path (E810) programs IPLEN in dwords:

        l3_len = l4.hdr - ip.hdr;
        offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;

ice_features_check() only rejects odd or oversized lengths:

        len = skb_network_header_len(skb);
        if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
                goto out_rm_features;

An L3 length of 22 therefore becomes 5 dwords, and the L4 checksum starts 2 bytes before csum_start.

Second, the L4 type comes from ip.v4->protocol or the IPv6 nexthdr, not from csum_offset. skb_csum_hwoffload_help() only requires csum_offset to be 16 or 6.

With protocol=TCP and csum_offset=6, the device writes the TCP checksum at L4+16 rather than L4+6. With protocol=SCTP (csum_not_inet is 0), it writes a CRC32c at L4+8 rather than a 16-bit Internet checksum.

Third, the GCS path (E830, HW_CSUM) uses:

                u16 csum_offset = skb->csum_offset / 2;

That rounds an odd csum_offset down, so the result lands one byte early.

Quoted 7 lines

Before this patch, skb_checksum_help() computed all of these at exactly csum_start/csum_offset. With this patch, other bytes of the transmitted frame get overwritten. So the commit message's "exactly as before" claim doesn't hold for these frames either.

Would it make sense to add these checks, or else leave the decision to the core path?

I don't think we need to parse each section in the header to do the strong check here.

Thanks,
Jason

Quoted 10 lines

- IPv4 ihl * 4 (or the IPv6 header plus extension headers) equals csum_start - nhoff
- the L4 protocol is TCP or UDP with a matching csum_offset (16 or 6)
- the offsets are even

[ ... ]

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127105347.72997-1-kerneljasonxing%40gmail.com