Skip to contentSkip to conversation

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

PATCH Superseded Julius Bairaktaris 3 participants 6 messages Nov 1 – Nov 2

Subsystems (11)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

GRO sets SKB_GSO_TCP_FIXEDID for the outer IPv4 header of a TCP packet whose IDs do not change, and SKB_GSO_TCP_FIXEDID_INNER for the inner header of an encapsulated one, since commit 3271f19bf7b9 ("net: gso:
restore ids of outer ip headers correctly"). Encapsulation, decapsulation and the GRE and UDP tunnel segmentation paths do not keep the flags on the header they describe, so a TCP stream that GRO merged and that crosses a tunnel boundary is segmented with the fixed-ID state of the wrong header. Sashiko found this in review of an ipmr series:
https://lore.kernel.org/netdev/[email protected]/

Patch 1 moves the flag on encapsulation and decapsulation and keeps the encapsulation level below GRE and UDP tunnels. The three changes are one patch because each alone loses fixed IDs in a case that works today. Patch 2 adds a selftest.

Tested in QEMU (KASAN) with the selftest, 17 cases, 400 segments each:
- net (7076f684d513): 5 cases fail: fixed inner IDs incremented after IPIP over IPv4 and IPv6 (40 distinct inner IDs), outer IDs repeated after GRE and VXLAN over IPv4 (10 distinct outer IDs for 400 segments), and a VXLAN stream with fixed outer IDs whose outer state survives decapsulation into IPIP. The same 5 failed in 3 runs of an earlier version of the test.
- this series: 17 of 17 pass, 3 of 3 runs.
- with only the decapsulation part, or only the encapsulation and segmentation parts: 8 and 5 cases fail, among them every encapsulation followed by decapsulation without segmentation, which passes on net.
- fou_gre_gro.sh and bareudp.sh pass on net and with the series; gre_gso.sh and udpgro_fwd.sh (3 paired runs) show the same failures on both, which come from the slow emulated machine.

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

 include/linux/skbuff.h                     |  30 ++
 include/net/ip_tunnels.h                   |   2 +
 net/core/filter.c                          |  11 +-
 net/core/lwt_bpf.c                         |   1 +
 net/ipv4/gre_offload.c                     |   1 -
 net/ipv4/ip_tunnel_core.c                  |   5 +-
 net/ipv4/udp_offload.c                     |   1 -
 net/ipv6/exthdrs.c                         |  11 +-
 tools/testing/selftests/net/.gitignore     |   1 +
 tools/testing/selftests/net/Makefile       |   2 +
 tools/testing/selftests/net/tcp_fixedid.c  | 396 +++++++++++++++++++++
 tools/testing/selftests/net/tcp_fixedid.sh | 291 +++++++++++++++
 12 files changed, 744 insertions(+), 8 deletions(-)
 create mode 100644 tools/testing/selftests/net/tcp_fixedid.c
 create mode 100755 tools/testing/selftests/net/tcp_fixedid.sh

base-commit: cb284914e749388eef402938bdb3115fe1f6d337

Signature · 1 line
2.53.0
#1 lore raw

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

GRO sets SKB_GSO_TCP_FIXEDID for the outer IPv4 header of a TCP packet and SKB_GSO_TCP_FIXEDID_INNER for the inner header of an encapsulated one, and segmentation reads them the same way. Three paths do not keep the flags on the header they describe:

- Encapsulation: iptunnel_handle_offloads(), bpf_skb_net_grow() and bpf_lwt_push_ip_encap() make the IPv4 header of a GSO packet the inner one but leave SKB_GSO_TCP_FIXEDID. A TCP stream that GRO merged with fixed IDs and that is forwarded into a tunnel gets one outer ID for all segments, also with DF clear, and incrementing inner IDs.
- Decapsulation: iptunnel_pull_offloads(), bpf_skb_net_shrink() and the segment routing decapsulation in ipv6_srh_rcv() and ipv6_rpl_srh_rcv() leave both flags, so the new outer header is segmented with the state of the removed one: inner IDs that incremented come out fixed when the outer IDs were fixed, and the reverse.
- Segmentation below GRE and UDP tunnels: inet_gso_segment() tells the inner header from the outer one by SKB_GSO_CB(skb)->encap_level, which gre_gso_segment() and __skb_udp_tunnel_segment() reset before they segment the inner packet. The inner IPv4 header of a GRE, VXLAN, GENEVE, FOU, GUE or bareudp packet is segmented with the outer flag.

Add skb_gso_fixedid_encap() and skb_gso_fixedid_decap(), which move the flag to the inner or the outer bit, and call them where a GSO packet becomes encapsulated or loses its outer header. The segment routing paths use iptunnel_pull_offloads() for this, which also clears the tunnel GSO types of the removed header. Keep encap_level below GRE and UDP tunnels, as ipip_gso_segment() and the IPv6 tunnel handlers do; the inner pass then also masks the features with hw_enc_features, which the tunnel handlers have done already, and resets the inner headers of each segment.

The three changes go together: with any one of them alone, fixed IDs are lost in a case that works today, such as a packet encapsulated and decapsulated again without segmentation, or one forwarded into an IPv6 GRE tunnel.

Fixes 3271f19bf7b9 ("net: gso: restore ids of outer ip headers correctly")
---
 include/linux/skbuff.h    | 30 ++++++++++++++++++++++++++++++
 include/net/ip_tunnels.h  |  2 ++
 net/core/filter.c         | 11 ++++++++---
 net/core/lwt_bpf.c        |  1 +
 net/ipv4/gre_offload.c    |  1 -
 net/ipv4/ip_tunnel_core.c |  5 ++++-
 net/ipv4/udp_offload.c    |  1 -
 net/ipv6/exthdrs.c        | 11 +++++++++--
 8 files changed, 54 insertions(+), 8 deletions(-)
include/linux/skbuff.h +30 -0
index 6787fcf6ac3e4..00df57cbc05c2 100644
@@ -5337,6 +5337,36 @@ static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo,
 	shinfo->gso_size -= decrement;
 }
 
+/**
+ * skb_gso_fixedid_encap - move the TCP fixed-ID flag to the inner header
+ * @shinfo: shared info of a GSO packet that becomes encapsulated
+ *
+ * The IPv4 header that SKB_GSO_TCP_FIXEDID describes becomes the inner one,
+ * and the new outer header gets an incrementing ID.
+ */
+static inline void skb_gso_fixedid_encap(struct skb_shared_info *shinfo)
+{
+	unsigned int fixedid = shinfo->gso_type & SKB_GSO_TCP_FIXEDID;
+
+	shinfo->gso_type &= ~(SKB_GSO_TCP_FIXEDID | SKB_GSO_TCP_FIXEDID_INNER);
+	shinfo->gso_type |= fixedid << 1;
+}
+
+/**
+ * skb_gso_fixedid_decap - move the TCP fixed-ID flag to the new outer header
+ * @shinfo: shared info of an encapsulated GSO packet losing its outer header
+ *
+ * The inner IPv4 header, which SKB_GSO_TCP_FIXEDID_INNER describes, becomes
+ * the outer one, and the state of the removed outer header is dropped.
+ */
+static inline void skb_gso_fixedid_decap(struct skb_shared_info *shinfo)
+{
+	unsigned int fixedid = shinfo->gso_type & SKB_GSO_TCP_FIXEDID_INNER;
+
+	shinfo->gso_type &= ~(SKB_GSO_TCP_FIXEDID | SKB_GSO_TCP_FIXEDID_INNER);
+	shinfo->gso_type |= fixedid >> 1;
+}
+
 void __skb_warn_lro_forwarding(const struct sk_buff *skb);
 
 static __always_inline bool skb_warn_if_lro(const struct sk_buff *skb)
include/net/ip_tunnels.h +2 -0
index 5cccf4c0e6913..c2ffda6029a06 100644
@@ -665,6 +665,8 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
 		err = skb_unclone(skb, GFP_ATOMIC);
 		if (unlikely(err))
 			return err;
+		if (skb->encapsulation)
+			skb_gso_fixedid_decap(skb_shinfo(skb));
 		skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
 					       NETIF_F_GSO_SHIFT);
 	}
net/core/filter.c +8 -3
index 70dc621672f2e..70f41e7597bf3 100644
@@ -3691,6 +3691,9 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
 	if (skb_is_gso(skb)) {
 		struct skb_shared_info *shinfo = skb_shinfo(skb);
 
+		if (encap)
+			skb_gso_fixedid_encap(shinfo);
+
 		/* Header must be checked, and gso_segs recomputed. */
 		shinfo->gso_type |= gso_type;
 		shinfo->gso_segs = 0;
@@ -3782,9 +3785,11 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
 						  SKB_GSO_GRE_CSUM |
 						  SKB_GSO_IPXIP4 |
 						  SKB_GSO_IPXIP6 |
-						  SKB_GSO_ESP)))
-				if (skb->encapsulation)
-					skb->encapsulation = 0;
+						  SKB_GSO_ESP)) &&
+			    skb->encapsulation) {
+				skb_gso_fixedid_decap(shinfo);
+				skb->encapsulation = 0;
+			}
 		}
 
 		/* Header must be checked, and gso_segs recomputed. */
net/core/lwt_bpf.c +1 -0
index da49364ec63de..4d9faf2cc5de5 100644
@@ -539,6 +539,7 @@ static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
 {
 	struct skb_shared_info *shinfo = skb_shinfo(skb);
 
+	skb_gso_fixedid_encap(shinfo);
 	gso_type |= SKB_GSO_DODGY;
 	shinfo->gso_type |= gso_type;
 	skb_decrease_gso_size(shinfo, encap_len);
net/ipv4/gre_offload.c +0 -1
index c8129782d39de..a8f9a0a0b06bd 100644
@@ -35,7 +35,6 @@ static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
 
 	/* setup inner skb. */
 	skb->encapsulation = 0;
-	SKB_GSO_CB(skb)->encap_level = 0;
 	__skb_pull(skb, tnl_hlen);
 	skb_reset_mac_header(skb);
 	skb_set_network_header(skb, skb_inner_network_offset(skb));
net/ipv4/ip_tunnel_core.c +4 -1
index 6c2855adff289..59050229e3499 100644
@@ -182,9 +182,10 @@ EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
 int iptunnel_handle_offloads(struct sk_buff *skb,
 			     int gso_type_mask)
 {
+	bool encap = !skb->encapsulation;
 	int err;
 
-	if (likely(!skb->encapsulation)) {
+	if (likely(encap)) {
 		skb_reset_inner_headers(skb);
 		skb->encapsulation = 1;
 	}
@@ -193,6 +194,8 @@ int iptunnel_handle_offloads(struct sk_buff *skb,
 		err = skb_header_unclone(skb, GFP_ATOMIC);
 		if (unlikely(err))
 			return err;
+		if (encap)
+			skb_gso_fixedid_encap(skb_shinfo(skb));
 		skb_shinfo(skb)->gso_type |= gso_type_mask;
 		return 0;
 	}
net/ipv4/udp_offload.c +0 -1
index cf07c3c6611a2..edcabe1115a60 100644
@@ -205,7 +205,6 @@ static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
 
 	/* setup inner skb. */
 	skb->encapsulation = 0;
-	SKB_GSO_CB(skb)->encap_level = 0;
 	__skb_pull(skb, tnl_hlen);
 	skb_reset_mac_header(skb);
 	skb_set_network_header(skb, skb_inner_network_offset(skb));
net/ipv6/exthdrs.c +9 -2
index 96c8402a27a54..126058772bc5a 100644
@@ -53,6 +53,7 @@
 #include <linux/ioam6_genl.h>
 #include <net/ioam6.h>
 #include <net/dst_metadata.h>
+#include <net/ip_tunnels.h>
 
 #include <linux/uaccess.h>
 
@@ -406,7 +407,10 @@ static int ipv6_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev)
 
 			skb_reset_network_header(skb);
 			skb_reset_transport_header(skb);
-			skb->encapsulation = 0;
+			if (iptunnel_pull_offloads(skb)) {
+				kfree_skb(skb);
+				return -1;
+			}
 			if (hdr->nexthdr == NEXTHDR_IPV4)
 				skb->protocol = htons(ETH_P_IP);
 			__skb_tunnel_rx(skb, skb->dev, net);
@@ -513,7 +517,10 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev)
 
 			skb_reset_network_header(skb);
 			skb_reset_transport_header(skb);
-			skb->encapsulation = 0;
+			if (iptunnel_pull_offloads(skb)) {
+				kfree_skb(skb);
+				return -1;
+			}
 
 			__skb_tunnel_rx(skb, skb->dev, net);
 
Signature · 1 line
2.53.0
#2 lore raw
Quoted 4 lines · Julius Bairaktaris

GRO sets SKB_GSO_TCP_FIXEDID for the outer IPv4 header of a TCP packet and SKB_GSO_TCP_FIXEDID_INNER for the inner header of an encapsulated one, and segmentation reads them the same way. Three paths do not keep the flags on the header they describe:

When the same mistake is made in N places the interface is the problem.

Before commit 3271f19bf7b9 SKB_GSO_TCP_FIXEDID described the innermost header. Encapsulation didn't have to know about it.
That commit made it describe the outermost header.
Now every site that adds or removes a header has to move the flag.

This patch changes 9 sites and it's not enough. xfrm_output() sets skb->encapsulation and SKB_GSO_ESP. xfrm4_tunnel_encap_add() adds the outer IPv4 header. SKB_GSO_TCP_FIXEDID stays, so ESP tunnel mode has the same bug as IPIP.

Quoted 5 lines

tunnel GSO types of the removed header. Keep encap_level below GRE and UDP tunnels, as ipip_gso_segment() and the IPv6 tunnel handlers do; the inner pass then also masks the features with hw_enc_features, which the tunnel handlers have done already, and resets the inner headers of each segment.

This undoes
commit 5197f3499c47 ("net: Reset encap_level to avoid resetting features on inner IP headers"). With remcsum __skb_udp_tunnel_segment() sets NETIF_F_HW_CSUM after its
mask. The mask in inet_gso_segment() clears it again on bnxt and mlx4.
Their hw_enc_features has NETIF_F_IP_CSUM and no NETIF_F_HW_CSUM.
Then skb_segment() checksums every segment in software.

Quoted 4 lines

The three changes go together: with any one of them alone, fixed IDs are lost in a case that works today, such as a packet encapsulated and decapsulated again without segmentation, or one forwarded into an IPv6 GRE tunnel.

Lets keep SKB_GSO_TCP_FIXEDID on the innermost header.
The second flag describes the outer header of an encapsulated packet. Willem's intuition in the review of 3271f19bf7b9 was right:
https://lore.kernel.org/netdev/[email protected]/

this patch flag on the innermost
header

  encapsulation                move the flag       nothing to do
  ESP tunnel mode              missed              nothing to do
  decapsulation                move the flag       clear the outer flag
  SRH decapsulation            move the flag       nothing, outer is IPv6
  FOU pulls offloads twice     needs a check of    clears it twice
                               skb->encapsulation
  GRE, UDP tunnel GSO          keep encap_level    as-is
  sfc, mlx5                    as-is               less code

Then no need for skb_gso_fixedid_encap(), skb_gso_fixedid_decap(), the bool in iptunnel_handle_offloads(), etc.

Something like this (compile tested only, the mlx5 hunk was not built):

Documentation/networking/segmentation-offloads.rst +2 -2
index 25a8b7e..0f20bb6 100644
@@ -49,8 +49,8 @@ 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.
+For encapsulated packets, SKB_GSO_TCP_FIXEDID refers only to the inner header.
+SKB_GSO_TCP_FIXEDID_OUTER can be used to specify the same for the outer header.
 Any combination of these two GSO types is allowed.
 
 If a device has NETIF_F_TSO_MANGLEID set then the IP ID can be ignored when
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +2 -6
index 22fea47..b804a75 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 0300575..162cfa5 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 6f7fec2..ac3f664 100644
@@ -5535,7 +5535,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 +3 -1
index 6787fcf..6eb913e 100644
@@ -716,9 +716,11 @@ 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 stays with the innermost IPv4 header, so there is
+	 * nothing to do on encapsulation. Decapsulation clears the outer flag.
 	 */
 	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 5cccf4c..a8e1cd7 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;
net/core/filter.c +1 -0
index 09c60a5..0b8d363 100644
@@ -3803,6 +3803,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/ipv4/af_inet.c +5 -3
index 7677b03..d5769a5 100644
@@ -1402,10 +1402,12 @@ 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/tcp_offload.c +4 -2
index b84fcd0..7919004 100644
@@ -459,6 +459,7 @@ 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;
 	struct tcphdr *th = tcp_hdr(skb);
 
 	if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
@@ -473,9 +474,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 is the inner one. */
 	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
-			(NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
+		(((fixedid >> skb->encapsulation) & 1) * SKB_GSO_TCP_FIXEDID) |
+		((fixedid & skb->encapsulation) * SKB_GSO_TCP_FIXEDID_OUTER);
 
 	tcp_gro_complete(skb);
 	return 0;
#3 lore raw
In reply to Julius Bairaktaris
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] ipv6: use-after-free of hdr in ipv6_srh_rcv() after iptunnel_pull_offloads() - [Medium] net…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] ipv6: use-after-free of hdr in ipv6_srh_rcv() after iptunnel_pull_offloads()
- [Medium] net: __skb_udp_tunnel_segment() loses remcsum NETIF_F_HW_CSUM when encap_level > 0
- [Medium] net: geneve_post_decap_hint() leaves fixed-ID flag on outer header after decap --

commit f6a9b837efd7885691511746c504ec36f4d4eb89
Author: Julius Bairaktaris <[email protected]>

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

This commit introduces skb_gso_fixedid_encap() and skb_gso_fixedid_decap() to move the SKB_GSO_TCP_FIXEDID and SKB_GSO_TCP_FIXEDID_INNER flags between headers during tunnel encapsulation and decapsulation. It also preserves SKB_GSO_CB(skb)->encap_level in gre_gso_segment() and __skb_udp_tunnel_segment() so inner IPv4 headers are segmented using the inner fixed-ID flag.

[ ... ]

include/net/ip_tunnels.h static inline int iptunnel_pull_offloads(struct sk_buff *skb) · line 672

Quoted 13 lines
include/net/ip_tunnels.h +2 -0
index 5cccf4c0e6913..c2ffda6029a06 100644
@@ -665,6 +665,8 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
 		err = skb_unclone(skb, GFP_ATOMIC);
 		if (unlikely(err))
 			return err;
+		if (skb->encapsulation)
+			skb_gso_fixedid_decap(skb_shinfo(skb));
 		skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
 					       NETIF_F_GSO_SHIFT);
 	}

[Severity: Medium]
When a double-encapsulated Geneve GSO packet carrying a GRO hint option is decapsulated in geneve_udp_encap_recv(), iptunnel_pull_header() calls iptunnel_pull_offloads(), which shifts SKB_GSO_TCP_FIXEDID_INNER into SKB_GSO_TCP_FIXEDID and clears skb->encapsulation.

Immediately afterwards, geneve_udp_encap_recv() calls geneve_post_decap_hint() to restore encapsulation state for the nested tunnel:

drivers/net/geneve.c:geneve_udp_encap_recv() {
    ...
	if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len, inner_proto,
				 !net_eq(geneve->net, dev_net(geneve->dev)))) {
		dev_dstats_rx_dropped(geneve->dev);
		goto drop;
	}
    ...
	if (geneve_post_decap_hint(sk, skb, sizeof(struct genevehdr) +
				   opts_len, &geneveh)) {
    ...
}
drivers/net/geneve.c:geneve_post_decap_hint() {
    ...
	skb->encapsulation = 1;
	skb_set_transport_header(skb, gro_hint->nested_tp_offset);
    ...
	if (uh->check) {
		...
		skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
		...
	} else {
		skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
	}
    ...
}

Doesn't this leave the fixed-ID flag for the innermost TCP/IPv4 header in SKB_GSO_TCP_FIXEDID instead of SKB_GSO_TCP_FIXEDID_INNER while skb->encapsulation is 1, causing subsequent segmentation or decapsulation of the nested tunnel to apply the inner TCP fixed-ID state to the nested tunnel's outer header and lose the fixed-ID state for the innermost IPv4 header?

[ ... ]

net/ipv4/udp_offload.c static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb, · line 210

Quoted 12 lines
net/ipv4/udp_offload.c +0 -1
index cf07c3c6611a2..edcabe1115a60 100644
@@ -205,7 +205,6 @@ static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
 
 	/* setup inner skb. */
 	skb->encapsulation = 0;
-	SKB_GSO_CB(skb)->encap_level = 0;
 	__skb_pull(skb, tnl_hlen);
 	skb_reset_mac_header(skb);
 	skb_set_network_header(skb, skb_inner_network_offset(skb));

[Severity: Medium]
In __skb_udp_tunnel_segment(), when SKB_GSO_TUNNEL_REMCSUM is active, features is first masked with skb->dev->hw_enc_features and then updated to strip NETIF_F_CSUM_MASK and set NETIF_F_HW_CSUM before calling gso_inner_segment():

net/ipv4/udp_offload.c:__skb_udp_tunnel_segment() {
    ...
	features &= skb->dev->hw_enc_features;
	if (need_csum)
		features &= ~NETIF_F_SCTP_CRC;

	if (remcsum) {
		features &= ~NETIF_F_CSUM_MASK;
		if (!need_csum || offload_csum)
			features |= NETIF_F_HW_CSUM;
	}

	/* segment inner packet. */
	segs = gso_inner_segment(skb, features);
    ...
}

With SKB_GSO_CB(skb)->encap_level remaining non-zero, both inet_gso_segment() and ipv6_gso_segment() see encap as true and mask features with skb->dev->hw_enc_features a second time:

net/ipv4/af_inet.c:inet_gso_segment() {
    ...
	encap = SKB_GSO_CB(skb)->encap_level > 0;
	if (encap)
		features &= skb->dev->hw_enc_features;
    ...
}

Won't this second masking clear NETIF_F_HW_CSUM on any device where hw_enc_features does not include NETIF_F_HW_CSUM (such as devices without hardware tunnel checksum offload or NICs that only advertise NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM in hw_enc_features), forcing software segmentation to compute unnecessary inner payload checksums and disabling GSO_PARTIAL and frag_list segmentation for remcsum packets?

Quoted 14 lines
net/ipv6/exthdrs.c +1 -0
index 96c8402a27a54..126058772bc5a 100644
@@ -53,6 +53,7 @@
 #include <linux/ioam6_genl.h>
 #include <net/ioam6.h>
 #include <net/dst_metadata.h>
+#include <net/ip_tunnels.h>
 
 #include <linux/uaccess.h>
 
@@ -406,7 +407,10 @@ static int ipv6_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev)
 

net/ipv6/exthdrs.c static int ipv6_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) · line 416

Quoted 10 lines
net/ipv6/exthdrs.c +4 -1
 			skb_reset_network_header(skb);
 			skb_reset_transport_header(skb);
-			skb->encapsulation = 0;
+			if (iptunnel_pull_offloads(skb)) {
+				kfree_skb(skb);
+				return -1;
+			}
 			if (hdr->nexthdr == NEXTHDR_IPV4)
 				skb->protocol = htons(ETH_P_IP);
 			__skb_tunnel_rx(skb, skb->dev, net);

[Severity: Critical]
In ipv6_srh_rcv(), hdr points into skb->head via skb_transport_header(skb) before entering the segments_left == 0 decapsulation block:

net/ipv6/exthdrs.c:ipv6_srh_rcv() {
	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
    ...
looped_back:
	if (hdr->segments_left == 0) {
		if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) {
			...
			if (iptunnel_pull_offloads(skb)) {
				kfree_skb(skb);
				return -1;
			}
			if (hdr->nexthdr == NEXTHDR_IPV4)
				skb->protocol = htons(ETH_P_IP);
    ...
}

When skb_is_gso(skb) && skb_cloned(skb) is true, iptunnel_pull_offloads() calls skb_unclone(skb, GFP_ATOMIC) -> pskb_expand_head(), which reallocates skb->head and frees the old buffer.

Doesn't reading hdr->nexthdr after iptunnel_pull_offloads(skb) cause a use-after-free read from the old skb->head buffer?

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

PATCH 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 with segmentation done in software, and check that the inner IDs come out as they went in and the IDs of a new outer IPv4 header are unique. Also forward a VXLAN stream with one header's IDs fixed after decapsulation into IPIP, and send the merged packets through a tunnel that the far end decapsulates without segmenting them before it forwards them.

A test where GRO merged nothing is skipped.

---
 tools/testing/selftests/net/.gitignore     |   1 +
 tools/testing/selftests/net/Makefile       |   2 +
 tools/testing/selftests/net/tcp_fixedid.c  | 396 +++++++++++++++++++++
 tools/testing/selftests/net/tcp_fixedid.sh | 291 +++++++++++++++
 4 files changed, 690 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 21412510ac26b..96263d5978258 100644
@@ -46,6 +46,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 9180b9bec5155..dd402814bc748 100644
@@ -106,6 +106,7 @@ TEST_PROGS := \
 	srv6_iptunnel_cache.sh \
 	stress_reuseport_listen.sh \
 	tcp_ecmp_failover.sh \
+	tcp_fixedid.sh \
 	tcp_fastopen_backup_key.sh \
 	test_bridge_backup_port.sh \
 	test_bridge_neigh_suppress.sh \
@@ -169,6 +170,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 +396 -0

396 lines not on this page · raw message

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

291 lines not on this page · raw message

Signature · 1 line
2.53.0
#5 lore raw

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

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] ipv6: seg6: use-after-free of hdr in ipv6_srh_rcv() after iptunnel_pull_offloads() - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] ipv6: seg6: use-after-free of hdr in ipv6_srh_rcv() after iptunnel_pull_offloads()
- [Medium] selftests: net: unchecked tunnel() return status in run_decap()
- [Medium] udp: gso: inner hw_enc_features re-masking clobbers remcsum NETIF_F_HW_CSUM --

commit e0a5534c2eabd4b1e53b2a69aaee94090aff3fec
Author: Julius Bairaktaris <[email protected]>

selftests: net: check TCP IP IDs across tunnels after GRO and GSO

This series updates GSO fixed-ID flag handling across tunnel encapsulation, decapsulation, and segmentation paths, and adds a network selftest verifying inner and outer IPv4 IDs across IPIP, GRE, and VXLAN tunnels.

net/ipv4/udp_offload.c static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb, · line 210

Quoted 12 lines
net/ipv4/udp_offload.c +0 -1
index cf07c3c6611a2..edcabe1115a60 100644
@@ -205,7 +205,6 @@ static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
 
 	/* setup inner skb. */
 	skb->encapsulation = 0;
-	SKB_GSO_CB(skb)->encap_level = 0;
 	__skb_pull(skb, tnl_hlen);
 	skb_reset_mac_header(skb);
 	skb_set_network_header(skb, skb_inner_network_offset(skb));

[Severity: Medium]
When __skb_udp_tunnel_segment() processes a packet with remote checksum offload enabled, it masks features with skb->dev->hw_enc_features, strips all checksum bits, and sets only the hardware checksum bit:

net/ipv4/udp_offload.c:__skb_udp_tunnel_segment() {
	...
	features &= skb->dev->hw_enc_features;
	if (need_csum)
		features &= ~NETIF_F_SCTP_CRC;

	if (remcsum) {
		features &= ~NETIF_F_CSUM_MASK;
		if (!need_csum || offload_csum)
			features |= NETIF_F_HW_CSUM;
	}

	/* segment inner packet. */
	segs = gso_inner_segment(skb, features);
	...
}

With SKB_GSO_CB(skb)->encap_level = 0 removed here, encap_level remains greater than zero when gso_inner_segment() invokes inet_gso_segment() in net/ipv4/af_inet.c or ipv6_gso_segment() in net/ipv6/ip6_offload.c:

	encap = SKB_GSO_CB(skb)->encap_level > 0;
	if (encap)
		features &= skb->dev->hw_enc_features;

Won't this second masking with skb->dev->hw_enc_features clear the NETIF_F_HW_CSUM bit that __skb_udp_tunnel_segment() just set for remcsum on any device where hw_enc_features does not include NETIF_F_HW_CSUM (such as devices with hw_enc_features == 0 or devices only advertising protocol- specific checksum flags), forcing skb_segment() into the software checksum path?

Quoted 4 lines
net/ipv6/exthdrs.c +0 -0
index 96c8402a27a54..126058772bc5a 100644

[ ... ]

net/ipv6/exthdrs.c static int ipv6_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) · line 416

Quoted 12 lines
net/ipv6/exthdrs.c +4 -1
@@ -406,7 +407,10 @@ static int ipv6_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev)
 
 			skb_reset_network_header(skb);
 			skb_reset_transport_header(skb);
-			skb->encapsulation = 0;
+			if (iptunnel_pull_offloads(skb)) {
+				kfree_skb(skb);
+				return -1;
+			}
 			if (hdr->nexthdr == NEXTHDR_IPV4)
 				skb->protocol = htons(ETH_P_IP);
 			__skb_tunnel_rx(skb, skb->dev, net);

[Severity: Critical]
In ipv6_srh_rcv(), hdr is initialized from skb_transport_header(skb) at the start of the function:

net/ipv6/exthdrs.c:ipv6_srh_rcv() {
	...
	hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);

When hdr->segments_left == 0 on entry, the skb_cloned(skb) check later in ipv6_srh_rcv() has not run. Calling iptunnel_pull_offloads(skb) on a cloned GSO packet calls skb_unclone(skb, GFP_ATOMIC) -> pskb_expand_head(), which allocates a new skb->head buffer and frees the old one:

include/net/ip_tunnels.h:iptunnel_pull_offloads() {
	if (skb_is_gso(skb)) {
		int err;

		err = skb_unclone(skb, GFP_ATOMIC);
		if (unlikely(err))
			return err;
	...
}

Can checking hdr->nexthdr after iptunnel_pull_offloads(skb) returns dereference a dangling hdr pointer into the freed skb->head buffer?

Quoted 6 lines
tools/testing/selftests/net/tcp_fixedid.sh new file +0 -0
new file mode 100755
index 0000000000000..88f9298174077
@@ -0,0 +1,291 @@

[ ... ]

Quoted 21 lines
tools/testing/selftests/net/tcp_fixedid.sh new file +21 -0
+# run_decap <fixed: inner|outer>: VXLAN from NSRC, decapsulated in NRTR
+run_decap() {
+	local fixed=$1 want=incr flags=-F
+	local r0mac t0
+
+	setup || { log_test_skip "decap"; return; }
+	ip -n "$NRTR" link add vx0 address "$VX_MAC" type vxlan id 42 \
+		dstport 4789 local 10.1.0.2 nolearning ||
+		{ log_test_skip "decap"; cleanup_all_ns; return; }
+	ip -n "$NRTR" addr add 10.3.0.2/24 dev vx0
+	ip -n "$NRTR" link set vx0 up
+	tunnel "$NRTR" tun0 ipip 4 10.2.0.1 10.2.0.2
+	route_into "$NRTR" tun0
+	noseg "$NRTR" r1
+
+	if [ "$fixed" = inner ]; then
+		want=fixed
+		flags=-f
+	fi
+	r0mac=$(ip -n "$NRTR" -br link show r0 | awk '{ print $3 }')
+	capture "$NMID" m1

tools/testing/selftests/net/tcp_fixedid.sh line 231

Quoted 10 lines
tools/testing/selftests/net/tcp_fixedid.sh new file +10 -0
+	t0=$(tx_packets "$NRTR" tun0)
+	ip netns exec "$NSRC" "$TX" -T -i s0 -M "$r0mac" -s 10.3.0.1 \
+		-d "$INNER_DST" -n "$SEGS" -b "$BURSTS" \
+		-V 10.1.0.1,10.1.0.2 -m "$VX_MAC" $flags
+	wait "$CAP_PID"
+	check "decap VXLAN with fixed $fixed IDs, into IPIP" \
+		"$(tail -n 1 "$RESULT")" "$want" 1 \
+		$(($(tx_packets "$NRTR" tun0) - t0))
+	cleanup_all_ns
+}

[Severity: Medium]
Unlike run_encap() and run_encap_decap(), run_decap() does not check the return status of the tunnel helper when creating the ipip device tun0:

	tunnel "$NRTR" tun0 ipip 4 10.2.0.1 10.2.0.2

If the kernel supports vxlan but does not have ipip support enabled, won't tun0 creation fail silently here, causing capture to wait for its full poll timeout and tx_packets to output an empty string that triggers an arithmetic syntax error in:

	$(($(tx_packets "$NRTR" tun0) - t0))

and fails the test instead of skipping it?

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