[RFC PATCH ath-next 1/3] wifi: mac80211: count the segments of a received GSO frame

From: Julius Bairaktaris

Date: Mon Nov 23 2026 - 08:50:19 EST


A driver with rx decap offload may merge consecutive in-order TCP frames
of one station into a GSO frame before it hands it over, as GRO would
on the netdev. ieee80211_rx_8023() counts every frame it takes as one:
one packet, fragment and MSDU of the station, one packet of the netdev,
and the bytes of the merged frame, which carries the headers of its
first segment only. The counters then report fewer frames and bytes
than the station sent.

Count a TCP GSO frame as its gso_segs frames, and its bytes as those of
the frames it was merged from: its length plus the headers, up to the
end of the TCP header, of every segment after the first. Any other
frame counts as before.

A merged frame to another station of the same interface takes the
internal forward of ieee80211_rx_8023(), which sets ETH_P_802_3 as the
protocol and the network header on the MAC header. The segmentation of
the frame on the way out then fails: skb_mac_gso_segment() finds no
handler for the protocol, and a driver that segments in hardware reads
the IP header at offset 0. Give a GSO frame the protocol of its
Ethernet header and its network header behind it; any other frame is
forwarded as before.

Signed-off-by: Julius Bairaktaris <julius@xxxxxxxxxxxxxx>
---
net/mac80211/rx.c | 61 +++++++++++++++++++++++++++++++++++++++++------
1 file changed, 54 insertions(+), 7 deletions(-)

diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
index 905fd70491ce..fd7dbd4b0611 100644
--- a/net/mac80211/rx.c
+++ b/net/mac80211/rx.c
@@ -19,6 +19,7 @@
#include <linux/export.h>
#include <linux/kcov.h>
#include <linux/bitops.h>
+#include <linux/tcp.h>
#include <kunit/visibility.h>
#include <net/mac80211.h>
#include <net/ieee80211_radiotap.h>
@@ -4915,6 +4916,33 @@ void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
__ieee80211_check_fast_rx_iface(sdata);
}

+/*
+ * The frames a received 802.3 frame stands for: one, or the segments of a TCP
+ * GSO frame that the driver merged from consecutive frames of a stream, each
+ * of which carried the headers the merged one has. @extra is set to the bytes
+ * of those headers the merged frame no longer carries.
+ */
+static unsigned int ieee80211_rx_8023_segs(struct sk_buff *skb,
+ unsigned int *extra)
+{
+ unsigned int segs, hdrlen;
+
+ *extra = 0;
+ if (!skb_is_gso(skb) || !skb_is_gso_tcp(skb) ||
+ !skb_transport_header_was_set(skb))
+ return 1;
+
+ hdrlen = skb_transport_offset(skb) + sizeof(struct tcphdr);
+ if (skb_headlen(skb) < hdrlen)
+ return 1;
+
+ segs = max_t(unsigned int, skb_shinfo(skb)->gso_segs, 1);
+ hdrlen += tcp_hdrlen(skb) - sizeof(struct tcphdr);
+ *extra = (segs - 1) * hdrlen;
+
+ return segs;
+}
+
static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
struct ieee80211_fast_rx *fast_rx,
int orig_len)
@@ -4925,6 +4953,8 @@ static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
struct sk_buff *skb = rx->skb;
void *sa = skb->data + ETH_ALEN;
void *da = skb->data;
+ struct pcpu_sw_netstats *tstats;
+ unsigned int segs, extra;

if (WARN_ON_ONCE(!link_sta)) {
dev_kfree_skb(rx->skb);
@@ -4964,12 +4994,17 @@ static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
stats->last_rx = jiffies;
stats->last_rate = sta_stats_encode_rate(status);

- stats->fragments++;
- stats->packets++;
+ segs = ieee80211_rx_8023_segs(skb, &extra);
+ stats->fragments += segs;
+ stats->packets += segs;

skb->dev = fast_rx->dev;

- dev_sw_netstats_rx_add(fast_rx->dev, skb->len);
+ tstats = this_cpu_ptr(fast_rx->dev->tstats);
+ u64_stats_update_begin(&tstats->syncp);
+ u64_stats_add(&tstats->rx_packets, segs);
+ u64_stats_add(&tstats->rx_bytes, skb->len + extra);
+ u64_stats_update_end(&tstats->syncp);

/* The seqno index has the same property as needed
* for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
@@ -4977,8 +5012,8 @@ static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
* frame, so count MSDUs.
*/
u64_stats_update_begin(&stats->syncp);
- u64_stats_inc(&stats->msdu[rx->seqno_idx]);
- u64_stats_add(&stats->bytes, orig_len);
+ u64_stats_add(&stats->msdu[rx->seqno_idx], segs);
+ u64_stats_add(&stats->bytes, orig_len + extra);
u64_stats_update_end(&stats->syncp);

if (fast_rx->internal_forward) {
@@ -4998,9 +5033,21 @@ static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
* reclassifying the frame (see cfg80211_classify8021d).
*/
xmit_skb->priority += 256;
- xmit_skb->protocol = htons(ETH_P_802_3);
- skb_reset_network_header(xmit_skb);
skb_reset_mac_header(xmit_skb);
+ /*
+ * A GSO frame is cut into its segments on the way
+ * out, which needs the protocol and network header
+ * it was merged with: with ETH_P_802_3 and the
+ * network header on the MAC header, the segmentation
+ * finds no IP header.
+ */
+ if (skb_is_gso(xmit_skb)) {
+ xmit_skb->protocol = eth_hdr(xmit_skb)->h_proto;
+ skb_set_network_header(xmit_skb, ETH_HLEN);
+ } else {
+ xmit_skb->protocol = htons(ETH_P_802_3);
+ skb_reset_network_header(xmit_skb);
+ }
dev_queue_xmit(xmit_skb);
}

--
2.53.0