Skip to contentSkip to conversation

net: keep the TCP fixed-ID flags on the header they describe

PATCH New Julius Bairaktaris 2 participants 5 messages Nov 28 – Nov 29

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

v4 cover net: keep the TCP fixed-ID flags on the header they describe

GRO sets SKB_GSO_TCP_FIXEDID for a TCP packet whose IPv4 IDs do not change. Since commit 3271f19bf7b9 ("net: gso: restore ids of outer ip headers correctly") the flag describes the outermost header and SKB_GSO_TCP_FIXEDID_INNER the inner one, so every path that adds or removes a header would have to move the flags. None does, and a TCP stream that GRO merged and that crosses a tunnel boundary is segmented with the fixed-ID state of the wrong header.

Patch 1 lets SKB_GSO_TCP_FIXEDID describe the innermost header again and replaces the inner flag with SKB_GSO_TCP_FIXEDID_OUTER, as Alexei suggested in review of v1. A header added in front of a packet that is already encapsulated clears the outer flag, and ESP transport and BEET mode with crypto offload, which keep the IPv4 header, copy the inner flag to the outer one. GRO of an IPv6 stream in an IPv4 tunnel sets the outer flag as it does for an IPv4 stream, and xfrm_output() no longer segments a decapsulated packet for a tunnel mode SA as an encapsulated one. Two flags cannot describe the middle header of a packet with three IPv4
headers: software segmentation uses SKB_GSO_TCP_FIXEDID for it, as it used SKB_GSO_TCP_FIXEDID_INNER before. Patch 2 adds a selftest.

Trigger: a host with GRO on its ingress device forwards a TCP stream into or out of an IPIP, GRE, VXLAN or crypto-offloaded ESP tunnel; any sender on the network can produce the stream. Impact: IPv4 IDs that repeat across the segments of a tunnel's outer header, or that change on a header whose IDs the sender kept fixed; no crash or memory corruption. CVSS 3.1 3.7 (AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:N). Until the fix: "ethtool -K <dev> gro off" on the ingress device.

The hosted Sashiko AI review of v2 2/3 of my "ipmr: forward GSO multicast packets" series reported the issue [1], its review of v3 the IPv6 and the xfrm_output() cases, and the selftest in patch 2 reproduces each.

Tested in QEMU (KVM) with the selftest, 22 cases, 400 segments each:
- net (d5b89d55a322), the new cases, 1 run: 6in4 with fixed outer IDs fails (40 distinct outer IDs for 400 segments, want 1), and so do both ESP tunnel mode cases (390 of 400 segments carry an inner ID the sender did not use).
- v3 of this series, the new cases, 1 run: 6in4 with fixed outer IDs and ESP tunnel mode after VXLAN decapsulation fail, as above.
- this series on net-next (f89726ffabef), 3 of 3 runs: 22 of 22 pass, and with 2 busy loops on the 4 vCPUs, 3 of 3 runs: 22 of 22 pass.
- the v3 receiver on the v3 kernel with 2 busy loops on the 4 vCPUs, the 12 encap cases, 5 runs: 60 of 60 cases lose 18 to 62 segments, each exactly the frames the socket dropped, as on NIPA; the v4
receiver: 0 of 60.

Changes in v4:
- Target net-next, as Paolo asked; rebased onto net-next f89726ffabef, and the Fixes: tag dropped.
- Patch 1: set SKB_GSO_TCP_FIXEDID_OUTER in tcp6_gro_complete() for an IPv4 outer header with fixed IDs in front of an IPv6 TCP stream; segmentation numbered those IDs from the first one (Sashiko).
- Patch 1: in xfrm_output(), segment a GSO packet for a tunnel mode SA before ESP only if it is still encapsulated; a decapsulated packet kept skb->inner_protocol, and its only IPv4 header got the outer flag (Sashiko).
- Patch 2: read the captured frames from a PACKET_RX_RING with room for every frame of a case and fail a case on a socket drop; on NIPA the socket buffer overflowed while the receiver was not scheduled (Paolo).
- Patch 2: add 6in4 cases with fixed and incrementing outer IDs and an ESP tunnel mode case after VXLAN decapsulation.
- v3: https://lore.kernel.org/netdev/[email protected]/
- v2: https://lore.kernel.org/netdev/[email protected]/
- v1: https://lore.kernel.org/netdev/[email protected]/

[1] https://lore.kernel.org/netdev/[email protected]/

I used Claude (Anthropic) to help write these patches.

Julius Bairaktaris (2):
net: keep the TCP fixed-ID flags on the header they describe
selftests: net: check TCP IP IDs across tunnels after GRO and GSO

 .../networking/segmentation-offloads.rst      |   8 +-
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |   8 +-
 drivers/net/ethernet/sfc/ef100_tx.c           |  12 +-
 include/linux/netdevice.h                     |   2 +-
 include/linux/skbuff.h                        |  12 +-
 include/net/ip_tunnels.h                      |   1 +
 include/uapi/linux/bpf.h                      |  16 +-
 net/core/filter.c                             |   1 +
 net/core/lwt_bpf.c                            |   2 +
 net/ipv4/af_inet.c                            |   9 +-
 net/ipv4/ip_tunnel_core.c                     |   2 +
 net/ipv4/tcp_offload.c                        |   7 +-
 net/ipv6/tcpv6_offload.c                      |   5 +-
 net/xfrm/xfrm_output.c                        |  18 +-
 tools/include/uapi/linux/bpf.h                |  16 +-
 tools/testing/selftests/net/.gitignore        |   1 +
 tools/testing/selftests/net/Makefile          |   2 +
 tools/testing/selftests/net/tcp_fixedid.c     | 538 ++++++++++++++++++
 tools/testing/selftests/net/tcp_fixedid.sh    | 472 +++++++++++++++
 19 files changed, 1091 insertions(+), 41 deletions(-)
 create mode 100644 tools/testing/selftests/net/tcp_fixedid.c
 create mode 100755 tools/testing/selftests/net/tcp_fixedid.sh

base-commit: f89726ffabef1b08c52e6163b7ab94b0d5f15503

Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/2 net: keep the TCP fixed-ID flags on the header they describe

Commit 3271f19bf7b9 ("net: gso: restore ids of outer ip headers correctly") made SKB_GSO_TCP_FIXEDID describe the outermost IPv4 header and added SKB_GSO_TCP_FIXEDID_INNER for the inner header of an encapsulated packet. Every path that adds or removes a header then has to move the flags, and none does. A TCP stream that GRO merged with fixed IDs and that is forwarded into an IPIP tunnel, or into an ESP tunnel mode SA with crypto offload, is segmented with one outer ID for all segments and with incrementing inner IDs. A tunnel that decapsulates such a packet in iptunnel_pull_offloads() leaves the state of the removed outer header on the new outer one, and the inner header of a GRE or UDP tunnel, which segmentation reads at encap_level 0, gets the state of the outer header.

Let SKB_GSO_TCP_FIXEDID describe the innermost IPv4 header again, as it did before that commit, and replace SKB_GSO_TCP_FIXEDID_INNER with SKB_GSO_TCP_FIXEDID_OUTER for the outermost header of an encapsulated packet. Where a header is added in front of a GSO packet, in iptunnel_handle_offloads() and in the BPF LWT encapsulation, clear the outer flag, so that the new header gets incrementing IDs as it does on a packet that was not encapsulated before. Clear it as well where iptunnel_pull_offloads() and bpf_skb_net_shrink() remove the outer header. ESP transport and BEET mode with crypto offload set skb->encapsulation but keep the IPv4 header and its ID, so xfrm_output() copies the inner flag to the outer one for them. Convert mlx5 and sfc, the two drivers that use the inner flag.

GRO of an IPv6 TCP stream in an IPv4 tunnel (SIT, or VXLAN over IPv4) records fixed outer IDs as it does for an IPv4 stream, but tcp6_gro_complete() never turned that into a flag: set SKB_GSO_TCP_FIXEDID_OUTER there as tcp4_gro_complete() does. As for an IPv4 stream, a device without NETIF_F_TSO_MANGLEID then leaves such a packet to software segmentation.

xfrm_output() segments a GSO packet for a tunnel mode SA with crypto offload before ESP when skb->inner_protocol is set, as an encapsulated packet, but a tunnel that decapsulated the packet leaves inner_protocol set, and segmentation then reads the outer flag for its only IPv4 header. Take that path only for a packet that is still encapsulated.

Two flags cannot describe a header between the outermost and the innermost one of a packet with three IPv4 headers: software segmentation uses SKB_GSO_TCP_FIXEDID for it, as it used SKB_GSO_TCP_FIXEDID_INNER before.

---
v4:
- Set the outer flag in tcp6_gro_complete() for an IPv4 outer header
with fixed IDs in front of an IPv6 TCP stream (Sashiko).
- Take xfrm_output_gso() for a tunnel mode SA only for a packet that is
still encapsulated; a decapsulated one kept inner_protocol (Sashiko).
- Target net-next (Paolo), Fixes: tag dropped.
v3:
- Clear SKB_GSO_TCP_FIXEDID_OUTER in iptunnel_handle_offloads() and in
the BPF LWT encapsulation, where a header goes in front of an
encapsulated packet (Sashiko).
- Copy the inner flag to the outer one in xfrm_output() for ESP transport
and BEET mode with crypto offload (Sashiko).
- Name the paths that change the outer flag and the middle-header limit
in skbuff.h, segmentation-offloads.rst and the message; list the outer
flag in the bpf_skb_adjust_room() decap flags of the uAPI header
(Sashiko).
- Wrap lines at 80 columns (NIPA).
v2: https://lore.kernel.org/netdev/f7d096c63c6a0b7a23a4dcea9a3de0915a48ac56.1794139176.git.julius@bairaktaris.de/
v3: https://lore.kernel.org/netdev/7c74b2076dc7dee1d481d39a135e0946d5e04145.1795284858.git.julius@bairaktaris.de/

 .../networking/segmentation-offloads.rst       |  8 +++++---
 .../net/ethernet/mellanox/mlx5/core/en_rx.c    |  8 ++------
 drivers/net/ethernet/sfc/ef100_tx.c            | 12 +++---------
 include/linux/netdevice.h                      |  2 +-
 include/linux/skbuff.h                         | 12 +++++++++++-
 include/net/ip_tunnels.h                       |  1 +
 include/uapi/linux/bpf.h                       | 16 +++++++++-------
 net/core/filter.c                              |  1 +
 net/core/lwt_bpf.c                             |  2 ++
 net/ipv4/af_inet.c                             |  9 ++++++---
 net/ipv4/ip_tunnel_core.c                      |  2 ++
 net/ipv4/tcp_offload.c                         |  7 +++++--
 net/ipv6/tcpv6_offload.c                       |  5 ++++-
 net/xfrm/xfrm_output.c                         | 18 +++++++++++++++++-
 tools/include/uapi/linux/bpf.h                 | 16 +++++++++-------
 15 files changed, 78 insertions(+), 41 deletions(-)
Documentation/networking/segmentation-offloads.rst +5 -3
index 25a8b7eca847..45c1703d68b8 100644
@@ -49,9 +49,11 @@ The default behavior is to increment the IP ID with every segment.  If the
 GSO type SKB_GSO_TCP_FIXEDID is specified then we will not increment the IP
 ID and all segments will use the same IP ID.
 
-For encapsulated packets, SKB_GSO_TCP_FIXEDID refers only to the outer header.
-SKB_GSO_TCP_FIXEDID_INNER can be used to specify the same for the inner header.
-Any combination of these two GSO types is allowed.
+For encapsulated packets, SKB_GSO_TCP_FIXEDID describes the innermost header
+and SKB_GSO_TCP_FIXEDID_OUTER the outermost header. Any combination of these
+two GSO types is allowed. A header added in front of a packet gets incrementing
+IDs. A header between the outermost and the innermost one has no flag of its
+own; software segmentation uses SKB_GSO_TCP_FIXEDID for it.
 
 If a device has NETIF_F_TSO_MANGLEID set then the IP ID can be ignored when
 performing TSO and we will either increment the IP ID for all frames, or leave
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +2 -6
index 1586cb2404c0..e47b1232130c 100644
@@ -1175,12 +1175,8 @@ static void mlx5e_shampo_update_ipv4_tcp_hdr(struct mlx5e_rq *rq, struct iphdr *
 	tcp->check = ~tcp_v4_check(skb->len - tcp_off, ipv4->saddr,
 				   ipv4->daddr, 0);
 	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
-	if (ntohs(ipv4->id) == rq->hw_gro_data->second_ip_id) {
-		bool encap = rq->hw_gro_data->fk.control.flags & FLOW_DIS_ENCAPSULATION;
-
-		skb_shinfo(skb)->gso_type |= encap ? SKB_GSO_TCP_FIXEDID_INNER :
-						     SKB_GSO_TCP_FIXEDID;
-	}
+	if (ntohs(ipv4->id) == rq->hw_gro_data->second_ip_id)
+		skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
 
 	skb->csum_start = (unsigned char *)tcp - skb->head;
 	skb->csum_offset = offsetof(struct tcphdr, check);
drivers/net/ethernet/sfc/ef100_tx.c +3 -9
index 03005757c060..162cfa54f2d0 100644
@@ -201,16 +201,10 @@ static void ef100_make_tso_desc(struct efx_nic *efx,
 	bool outer_csum;
 	u32 paylen;
 
-	if (encap) {
-		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID_INNER)
-			mangleid = ESE_GZ_TX_DESC_IP4_ID_NO_OP;
-		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID)
-			mangleid_outer = ESE_GZ_TX_DESC_IP4_ID_NO_OP;
-	} else {
-		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID)
-			mangleid = ESE_GZ_TX_DESC_IP4_ID_NO_OP;
+	if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID)
+		mangleid = ESE_GZ_TX_DESC_IP4_ID_NO_OP;
+	if (!encap || (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID_OUTER))
 		mangleid_outer = ESE_GZ_TX_DESC_IP4_ID_NO_OP;
-	}
 
 	if (efx->net_dev->features & NETIF_F_HW_VLAN_CTAG_TX)
 		vlan_enable = skb_vlan_tag_present(skb);
include/linux/netdevice.h +1 -1
index 694b6d29f680..9e0992afbbed 100644
@@ -5545,7 +5545,7 @@ static inline bool net_gso_ok(netdev_features_t features, int gso_type)
 {
 	netdev_features_t feature;
 
-	if (gso_type & (SKB_GSO_TCP_FIXEDID | SKB_GSO_TCP_FIXEDID_INNER))
+	if (gso_type & (SKB_GSO_TCP_FIXEDID | SKB_GSO_TCP_FIXEDID_OUTER))
 		gso_type |= __SKB_GSO_TCP_FIXEDID;
 
 	feature = ((netdev_features_t)gso_type << NETIF_F_GSO_SHIFT) & NETIF_F_GSO_MASK;
include/linux/skbuff.h +11 -1
index c609cafc7a14..caf374b452ca 100644
@@ -724,9 +724,19 @@ enum {
 
 	/* These indirectly map onto the same netdev feature.
 	 * If NETIF_F_TSO_MANGLEID is set it may mangle both inner and outer IDs.
+	 * SKB_GSO_TCP_FIXEDID describes the innermost IPv4 header and
+	 * SKB_GSO_TCP_FIXEDID_OUTER the outermost one. Adding a header in
+	 * iptunnel_handle_offloads() or with BPF LWT clears the outer flag, and
+	 * so does removing one in iptunnel_pull_offloads() or in
+	 * bpf_skb_adjust_room() with a BPF_F_ADJ_ROOM_DECAP_L4_* or
+	 * BPF_F_ADJ_ROOM_DECAP_IPXIP* flag. xfrm_output() sets it from the
+	 * inner flag for ESP transport and BEET mode offload, which keep the
+	 * header.
+	 * Software segmentation uses SKB_GSO_TCP_FIXEDID for any header in
+	 * between.
 	 */
 	SKB_GSO_TCP_FIXEDID = 1 << 30,
-	SKB_GSO_TCP_FIXEDID_INNER = 1 << 31,
+	SKB_GSO_TCP_FIXEDID_OUTER = 1 << 31,
 };
 
 #if BITS_PER_LONG > 32
include/net/ip_tunnels.h +1 -0
index 5cccf4c0e691..a8e1cd7021fc 100644
@@ -667,6 +667,7 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
 			return err;
 		skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
 					       NETIF_F_GSO_SHIFT);
+		skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCP_FIXEDID_OUTER;
 	}
 
 	skb->encapsulation = 0;
include/uapi/linux/bpf.h +9 -7
index 8d7228834305..e5aae5320af9 100644
@@ -3097,20 +3097,22 @@ union bpf_attr {
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_L4_GRE**:
  *		  Clear GRE tunnel GSO flags (SKB_GSO_GRE and SKB_GSO_GRE_CSUM)
- *		  when decapsulating a GRE tunnel.
+ *		  and SKB_GSO_TCP_FIXEDID_OUTER when decapsulating a GRE tunnel.
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_L4_UDP**:
  *		  Clear UDP tunnel GSO flags (SKB_GSO_UDP_TUNNEL and
- *		  SKB_GSO_UDP_TUNNEL_CSUM) when decapsulating a UDP tunnel.
+ *		  SKB_GSO_UDP_TUNNEL_CSUM) and SKB_GSO_TCP_FIXEDID_OUTER when
+ *		  decapsulating a UDP tunnel.
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_IPXIP4**:
- *		  Clear IPIP/SIT tunnel GSO flag (SKB_GSO_IPXIP4) when decapsulating
- *		  a tunnel with an outer IPv4 header (IPv4-in-IPv4 or IPv6-in-IPv4).
+ *		  Clear IPIP/SIT tunnel GSO flag (SKB_GSO_IPXIP4) and
+ *		  SKB_GSO_TCP_FIXEDID_OUTER when decapsulating a tunnel with an
+ *		  outer IPv4 header (IPv4-in-IPv4 or IPv6-in-IPv4).
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_IPXIP6**:
- *		  Clear IPv6 encapsulation tunnel GSO flag (SKB_GSO_IPXIP6) when
- *		  decapsulating a tunnel with an outer IPv6 header (IPv6-in-IPv6
- *		  or IPv4-in-IPv6).
+ *		  Clear IPv6 encapsulation tunnel GSO flag (SKB_GSO_IPXIP6) and
+ *		  SKB_GSO_TCP_FIXEDID_OUTER when decapsulating a tunnel with an
+ *		  outer IPv6 header (IPv6-in-IPv6 or IPv4-in-IPv6).
  *
  *		When using the decapsulation flags above, the skb->encapsulation
  *		flag is automatically cleared if all tunnel-specific GSO flags
net/core/filter.c +1 -0
index d6a6dd9c9547..5a7957c21d17 100644
@@ -3856,6 +3856,7 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
 		/* Clear encapsulation flag only when no tunnel GSO flags remain */
 		if (flags & (BPF_F_ADJ_ROOM_DECAP_L4_MASK |
 			     BPF_F_ADJ_ROOM_DECAP_IPXIP_MASK)) {
+			shinfo->gso_type &= ~SKB_GSO_TCP_FIXEDID_OUTER;
 			if (!(shinfo->gso_type & (SKB_GSO_UDP_TUNNEL |
 						  SKB_GSO_UDP_TUNNEL_CSUM |
 						  SKB_GSO_GRE |
net/core/lwt_bpf.c +2 -0
index 484cd3b53dea..a7d6fda69f60 100644
@@ -585,6 +585,8 @@ static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
 	struct skb_shared_info *shinfo = skb_shinfo(skb);
 
 	gso_type |= SKB_GSO_DODGY;
+	/* The outer flag described the header below the new one. */
+	shinfo->gso_type &= ~SKB_GSO_TCP_FIXEDID_OUTER;
 	shinfo->gso_type |= gso_type;
 	skb_decrease_gso_size(shinfo, encap_len);
 	shinfo->gso_segs = 0;
net/ipv4/af_inet.c +6 -3
index 5d03a9149b40..c4541c924aaf 100644
@@ -1404,10 +1404,13 @@ struct sk_buff *inet_gso_segment(struct sk_buff *skb,
 
 	segs = ERR_PTR(-EPROTONOSUPPORT);
 
-	fixedid = !!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCP_FIXEDID << encap));
-
-	if (!skb->encapsulation || encap)
+	if (!skb->encapsulation || encap) {
 		udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
+		fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID);
+	} else {
+		fixedid = !!(skb_shinfo(skb)->gso_type &
+			     SKB_GSO_TCP_FIXEDID_OUTER);
+	}
 
 	ops = rcu_dereference(inet_offloads[proto]);
 	if (likely(ops && ops->callbacks.gso_segment)) {
net/ipv4/ip_tunnel_core.c +2 -0
index 6c2855adff28..dbf6bbf25114 100644
@@ -193,6 +193,8 @@ int iptunnel_handle_offloads(struct sk_buff *skb,
 		err = skb_header_unclone(skb, GFP_ATOMIC);
 		if (unlikely(err))
 			return err;
+		/* The outer flag described the header below the new one. */
+		skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCP_FIXEDID_OUTER;
 		skb_shinfo(skb)->gso_type |= gso_type_mask;
 		return 0;
 	}
net/ipv4/tcp_offload.c +5 -2
index 7691be01da4d..93ea20b90550 100644
@@ -463,6 +463,8 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
 {
 	const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
 	const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
+	const u8 fixedid = NAPI_GRO_CB(skb)->ip_fixedid;
+	const u8 encap = skb->encapsulation;
 	struct tcphdr *th = tcp_hdr(skb);
 
 	if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
@@ -477,9 +479,10 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
 	th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
 				  iph->daddr, 0);
 
-	BUILD_BUG_ON(SKB_GSO_TCP_FIXEDID << 1 != SKB_GSO_TCP_FIXEDID_INNER);
+	/* Bit 0 of ip_fixedid is the outermost header, bit 1 the inner one. */
 	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
-			(NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
+		(((fixedid >> encap) & 1) * SKB_GSO_TCP_FIXEDID) |
+		((fixedid & encap) * SKB_GSO_TCP_FIXEDID_OUTER);
 
 	tcp_gro_complete(skb);
 	return 0;
net/ipv6/tcpv6_offload.c +4 -1
index 71feb92aeac2..f232d5b38bbf 100644
@@ -96,7 +96,10 @@ static __always_inline int tcp6_gro_complete(struct sk_buff *skb, int thoff)
 
 	th->check = ~tcp_v6_check(skb->len - thoff, &iph->saddr,
 				  &iph->daddr, 0);
-	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
+	/* Only an IPv4 outer header sets bit 0 of ip_fixedid. */
+	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6 |
+		((NAPI_GRO_CB(skb)->ip_fixedid & skb->encapsulation) *
+		 SKB_GSO_TCP_FIXEDID_OUTER);
 
 	tcp_gro_complete(skb);
 	return 0;
net/xfrm/xfrm_output.c +17 -1
index f1f612cd7b43..e5bcb845e9ac 100644
@@ -791,6 +791,7 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 	secpath_reset(skb);
 
 	if (xfrm_dev_offload_ok(skb, x)) {
+		bool encap = skb->encapsulation;
 		struct sec_path *sp;
 
 		sp = secpath_set(skb);
@@ -805,10 +806,25 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 		xfrm_state_hold(x);
 
 		xfrm_get_inner_ipproto(skb, x);
+		/*
+		 * Transport and BEET mode keep the IPv4 header and its ID, so
+		 * the header that SKB_GSO_TCP_FIXEDID describes is the outer
+		 * one as well.
+		 */
+		if (!encap &&
+		    (x->props.mode == XFRM_MODE_TRANSPORT ||
+		     x->props.mode == XFRM_MODE_BEET) &&
+		    (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID))
+			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID_OUTER;
 		skb->encapsulation = 1;
 
 		if (skb_is_gso(skb)) {
-			if (skb->inner_protocol && x->props.mode == XFRM_MODE_TUNNEL)
+			/*
+			 * inner_protocol is only valid on an encapsulated skb:
+			 * a tunnel that decapsulated the packet leaves it set.
+			 */
+			if (encap && skb->inner_protocol &&
+			    x->props.mode == XFRM_MODE_TUNNEL)
 				return xfrm_output_gso(net, sk, skb);
 
 			skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
tools/include/uapi/linux/bpf.h +9 -7
index 8d7228834305..e5aae5320af9 100644
@@ -3097,20 +3097,22 @@ union bpf_attr {
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_L4_GRE**:
  *		  Clear GRE tunnel GSO flags (SKB_GSO_GRE and SKB_GSO_GRE_CSUM)
- *		  when decapsulating a GRE tunnel.
+ *		  and SKB_GSO_TCP_FIXEDID_OUTER when decapsulating a GRE tunnel.
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_L4_UDP**:
  *		  Clear UDP tunnel GSO flags (SKB_GSO_UDP_TUNNEL and
- *		  SKB_GSO_UDP_TUNNEL_CSUM) when decapsulating a UDP tunnel.
+ *		  SKB_GSO_UDP_TUNNEL_CSUM) and SKB_GSO_TCP_FIXEDID_OUTER when
+ *		  decapsulating a UDP tunnel.
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_IPXIP4**:
- *		  Clear IPIP/SIT tunnel GSO flag (SKB_GSO_IPXIP4) when decapsulating
- *		  a tunnel with an outer IPv4 header (IPv4-in-IPv4 or IPv6-in-IPv4).
+ *		  Clear IPIP/SIT tunnel GSO flag (SKB_GSO_IPXIP4) and
+ *		  SKB_GSO_TCP_FIXEDID_OUTER when decapsulating a tunnel with an
+ *		  outer IPv4 header (IPv4-in-IPv4 or IPv6-in-IPv4).
  *
  *		* **BPF_F_ADJ_ROOM_DECAP_IPXIP6**:
- *		  Clear IPv6 encapsulation tunnel GSO flag (SKB_GSO_IPXIP6) when
- *		  decapsulating a tunnel with an outer IPv6 header (IPv6-in-IPv6
- *		  or IPv4-in-IPv6).
+ *		  Clear IPv6 encapsulation tunnel GSO flag (SKB_GSO_IPXIP6) and
+ *		  SKB_GSO_TCP_FIXEDID_OUTER when decapsulating a tunnel with an
+ *		  outer IPv6 header (IPv6-in-IPv6 or IPv4-in-IPv6).
  *
  *		When using the decapsulation flags above, the skb->encapsulation
  *		flag is automatically cleared if all tunnel-specific GSO flags
Signature · 1 line
2.53.0
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] xfrm: stale inner_protocol in xfrm_output() skips saving inner header offsets --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] xfrm: stale inner_protocol in xfrm_output() skips saving inner header offsets
--

commit 6f304adf33d568f2ad16eea15056eaded001170a
Author: Julius Bairaktaris <[email protected]>

net: keep the TCP fixed-ID flags on the header they describe

This commit makes SKB_GSO_TCP_FIXEDID describe the innermost IPv4 header again, replaces SKB_GSO_TCP_FIXEDID_INNER with SKB_GSO_TCP_FIXEDID_OUTER for the outermost header of an encapsulated packet, and updates tunnel and XFRM encapsulation/decapsulation paths accordingly.

Quoted 29 lines
net/xfrm/xfrm_output.c +11 -0
index f1f612cd7b438..e5bcb845e9acb 100644
@@ -791,6 +791,7 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 	secpath_reset(skb);
 
 	if (xfrm_dev_offload_ok(skb, x)) {
+		bool encap = skb->encapsulation;
 		struct sec_path *sp;
 
 		sp = secpath_set(skb);
@@ -805,10 +806,25 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
 		xfrm_state_hold(x);
 
 		xfrm_get_inner_ipproto(skb, x);
+		/*
+		 * Transport and BEET mode keep the IPv4 header and its ID, so
+		 * the header that SKB_GSO_TCP_FIXEDID describes is the outer
+		 * one as well.
+		 */
+		if (!encap &&
+		    (x->props.mode == XFRM_MODE_TRANSPORT ||
+		     x->props.mode == XFRM_MODE_BEET) &&
+		    (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID))
+			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID_OUTER;
 		skb->encapsulation = 1;
 
 		if (skb_is_gso(skb)) {

net/xfrm/xfrm_output.c int xfrm_output(struct sock *sk, struct sk_buff *skb) · line 830

Quoted 10 lines
net/xfrm/xfrm_output.c +6 -1
-			if (skb->inner_protocol && x->props.mode == XFRM_MODE_TUNNEL)
+			/*
+			 * inner_protocol is only valid on an encapsulated skb:
+			 * a tunnel that decapsulated the packet leaves it set.
+			 */
+			if (encap && skb->inner_protocol &&
+			    x->props.mode == XFRM_MODE_TUNNEL)
 				return xfrm_output_gso(net, sk, skb);
 
 			skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;

[Severity: Medium]
When !encap and a stale skb->inner_protocol remains from an earlier tunnel decapsulation, does skipping xfrm_output_gso() without clearing skb->inner_protocol cause xfrm4_tunnel_encap_add() and xfrm6_tunnel_encap_add() to skip saving the inner header offsets?

net/xfrm/xfrm_output.c:xfrm4_tunnel_encap_add() {
	...
	if (!skb->inner_protocol) {
		skb_set_inner_network_header(skb, skb_network_offset(skb));
		skb_set_inner_transport_header(skb, skb_transport_offset(skb));
	}
	...
}

For a decapsulated SKB_GSO_FRAGLIST GRO packet (where inet_gro_complete() set skb->inner_protocol, while tcp4_gro_complete() returned early on is_flist without calling tcp_gro_complete() to set inner_transport_header), inner_transport_header is left unset when xfrm4_tunnel_encap_add() pushes the outer IP header. qdisc_pkt_len_segs_init() then sees a negative or invalid skb_inner_transport_offset(skb) on the offloaded SKB_GSO_ESP packet and drops it with SKB_DROP_REASON_SKB_BAD_GSO.

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

PATCH v4 2/2 selftests: net: check TCP IP IDs across tunnels after GRO and GSO

In reply to Julius Bairaktaris

Send TCP segments with fixed or incrementing IPv4 IDs from a packet socket, let GRO on a veth merge them, forward the merged packets into IPIP, GRE and VXLAN tunnels over IPv4 and IPv6 and into ESP tunnel and transport mode SAs offloaded to netdevsim, with segmentation done in software, and check that the IDs of the IPv4 header in front of TCP come out as they went in and the IDs of a new outer IPv4 header are unique, or for ESP tunnel mode increment within each packet. Also forward a VXLAN stream with one header's IDs fixed after decapsulation into IPIP and into the ESP tunnel mode SA, forward IPv6 TCP in IPv4 (6in4) with fixed and incrementing outer IDs, which must come out as they went in, and send the merged packets through a tunnel that the far end decapsulates without segmenting them before it forwards them.

The receiver reads a packet ring with room for five times the segments of a test, so that it loses none while it is not scheduled, and a test fails if the socket dropped a frame. A test where GRO merged nothing is skipped.

---
v4:
- Read the frames from a PACKET_RX_RING with room for every frame of a
case, and fail a case on a socket drop; the socket buffer overflowed
on NIPA (Paolo).
- Add 6in4 cases with fixed and incrementing outer IDs, and an ESP
tunnel mode case after VXLAN decapsulation.
v3:
- Add an ESP transport mode case.
- Skip a run without GRO merging before the outer IDs are checked; check
that ESP tunnel mode outer IDs increment within each packet (Sashiko).
- Delete only the netdevsim devices the test created, also from the EXIT
trap, before the namespaces (Sashiko).
- Make the receiver's -c optional (Sashiko).
- Use slowwait and check_gen_prog from lib.sh; create the netdevsim
devices inside their namespace with the new_device write checked
(Sashiko).
- Report a receiver that is not bound within 10 s as such.
- Fix shellcheck SC2016, wrap lines at 80 columns (NIPA).
v2: https://lore.kernel.org/netdev/c4f9c977725ee51341ca84dedf2dddb9fcb4e8ea.1794139176.git.julius@bairaktaris.de/
v3: https://lore.kernel.org/netdev/2f75527eb49d7d4ffec32a61fd7dd080673dc57f.1795284858.git.julius@bairaktaris.de/

 tools/testing/selftests/net/.gitignore     |   1 +
 tools/testing/selftests/net/Makefile       |   2 +
 tools/testing/selftests/net/tcp_fixedid.c  | 538 +++++++++++++++++++++
 tools/testing/selftests/net/tcp_fixedid.sh | 472 ++++++++++++++++++
 4 files changed, 1013 insertions(+)
 create mode 100644 tools/testing/selftests/net/tcp_fixedid.c
 create mode 100755 tools/testing/selftests/net/tcp_fixedid.sh
tools/testing/selftests/net/.gitignore +1 -0
index 83d68e2dd4ac..79a9eb065fbd 100644
@@ -48,6 +48,7 @@ so_rcv_listener
 stress_reuseport_listen
 tap
 tcp_fastopen_backup_key
+tcp_fixedid
 tcp_inq
 tcp_mmap
 tcp_port_share
tools/testing/selftests/net/Makefile +2 -0
index fb0a64662053..d1ebe4e47351 100644
@@ -110,6 +110,7 @@ TEST_PROGS := \
 	stress_reuseport_listen.sh \
 	tcp_ecmp_failover.sh \
 	tcp_fastopen_backup_key.sh \
+	tcp_fixedid.sh \
 	test_bridge_backup_port.sh \
 	test_bridge_neigh_suppress.sh \
 	test_ingress_egress_chaining.sh \
@@ -175,6 +176,7 @@ TEST_GEN_FILES := \
 	socket \
 	stress_reuseport_listen \
 	tcp_fastopen_backup_key \
+	tcp_fixedid \
 	tcp_inq \
 	tcp_mmap \
 	tfo \
tools/testing/selftests/net/tcp_fixedid.c new file +538 -0

538 lines not on this page · raw message

tools/testing/selftests/net/tcp_fixedid.sh new file +472 -0

472 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw
In reply to Julius Bairaktaris
Sashiko has finished reviewing this 2-patch series:

Sashiko has finished reviewing this 2-patch series:

- Patch 2: no issues found.
- Patch 1 ("net: keep the TCP fixed-ID flags on the header they..."): 1 potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 1.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]