From: Eric Dumazet <edumazet@kernel.org>
To: netdev@vger.kernel.org, "David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: "Eric Dumazet" <edumazet@kernel.org>,
"Simon Horman" <horms@kernel.org>,
"Willem de Bruijn" <willemb@google.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Jason Wang" <jasowangio@gmail.com>,
"Eugenio Pérez" <eperezma@redhat.com>,
"Hassan Sultan" <hassansultan@google.com>,
virtualization@lists.linux.dev
Subject: [PATCH net v3 8/8] selftests: net: tuntap: write UDP tunnel offsets in little endian
Date: Thu, 5 Nov 2026 12:57:15 +0100 [thread overview]
Message-ID: <20261105115715.1466924-9-edumazet@kernel.org> (raw)
In-Reply-To: <20261105115715.1466924-1-edumazet@kernel.org>
outer_th_offset and inner_nh_offset of struct
virtio_net_hdr_v1_hash_tunnel are __le16, and the kernel always reads
them with le16_to_cpu(). build_virtio_net_hdr_v1_hash_tunnel() writes
them in host order, and tun.c adjusts them with native arithmetic, so
all UDP tunnel GSO tests fail on big-endian hosts: the kernel sees
offsets such as 0x2200 and rejects the frames before the checks the
tests target.
Write the offsets with htole16(), and add vnet_tnl_offset_add() to
adjust them. csum_start stays in host order: it follows the tun vnet
header endianness, which the tests do not change.
Fixes: a942fcd72e97 ("selftest: tun: Introduce tuntap_helpers.h header for TUN/TAP testing")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/netdev/179387332145.488856.1149073246990672028@kernel.org/
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
tools/testing/selftests/net/tun.c | 10 +++++-----
tools/testing/selftests/net/tuntap_helpers.h | 19 +++++++++++++++----
2 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index a135d3aa10d1..5f797a1bc604 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -738,8 +738,8 @@ static int build_udptnl_frame(uint8_t *buf, bool vlan, uint16_t l3proto,
cur += build_virtio_net_hdr_v1_hash_tunnel(cur, true, hlen, 1400,
AF_INET, AF_INET);
- vh->outer_th_offset += vlan_len;
- vh->inner_nh_offset += vlan_len;
+ vnet_tnl_offset_add(&vh->outer_th_offset, vlan_len);
+ vnet_tnl_offset_add(&vh->inner_nh_offset, vlan_len);
vh->hash_hdr.hdr.csum_start += vlan_len;
if (vlan) {
@@ -944,7 +944,7 @@ TEST_F(tun_vnet_udptnl_proto, outer_proto_bad_vlan_offset)
int len, ret;
len = build_udptnl_frame(self->pkt, true, ETH_P_IP, 2800);
- vh->outer_th_offset -= 4;
+ vnet_tnl_offset_add(&vh->outer_th_offset, -4);
ret = write(self->fd, self->pkt, len);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, EINVAL);
@@ -1031,8 +1031,8 @@ static int build_udptnl_l3_frame(uint8_t *buf, uint16_t pi_proto,
cur = vnet + build_virtio_net_hdr_v1_hash_tunnel(vnet, false, hlen,
1400, AF_INET,
AF_INET);
- vh->outer_th_offset += IPV4_OPT_LEN;
- vh->inner_nh_offset += IPV4_OPT_LEN;
+ vnet_tnl_offset_add(&vh->outer_th_offset, IPV4_OPT_LEN);
+ vnet_tnl_offset_add(&vh->inner_nh_offset, IPV4_OPT_LEN);
vh->hash_hdr.hdr.csum_start += IPV4_OPT_LEN;
/* Build the frame without options first: build_udp_packet_csum()
diff --git a/tools/testing/selftests/net/tuntap_helpers.h b/tools/testing/selftests/net/tuntap_helpers.h
index ecad83fae158..dc85cd9bc0ae 100644
--- a/tools/testing/selftests/net/tuntap_helpers.h
+++ b/tools/testing/selftests/net/tuntap_helpers.h
@@ -3,6 +3,7 @@
#ifndef _TUNTAP_HELPERS_H
#define _TUNTAP_HELPERS_H
+#include <endian.h>
#include <errno.h>
#include <linux/if_packet.h>
#include <linux/ipv6.h>
@@ -368,6 +369,7 @@ static inline int build_virtio_net_hdr_v1_hash_tunnel(uint8_t *buf, bool is_tap,
struct virtio_net_hdr_v1_hash_tunnel *vh_tunnel = (void *)buf;
struct virtio_net_hdr_v1 *vh = &vh_tunnel->hash_hdr.hdr;
int outer_iphlen, inner_iphlen, eth_hlen, gso_type;
+ int outer_th, inner_nh;
eth_hlen = is_tap ? ETH_HLEN : 0;
outer_iphlen = (outer_family == AF_INET) ? sizeof(struct iphdr) :
@@ -375,11 +377,14 @@ static inline int build_virtio_net_hdr_v1_hash_tunnel(uint8_t *buf, bool is_tap,
inner_iphlen = (inner_family == AF_INET) ? sizeof(struct iphdr) :
sizeof(struct ipv6hdr);
- vh_tunnel->outer_th_offset = eth_hlen + outer_iphlen;
- vh_tunnel->inner_nh_offset = vh_tunnel->outer_th_offset + ETH_HLEN +
- GENEVE_HLEN + sizeof(struct udphdr);
+ outer_th = eth_hlen + outer_iphlen;
+ inner_nh = outer_th + ETH_HLEN + GENEVE_HLEN + sizeof(struct udphdr);
- vh->csum_start = vh_tunnel->inner_nh_offset + inner_iphlen;
+ /* The tunnel offsets are always little endian. */
+ vh_tunnel->outer_th_offset = htole16(outer_th);
+ vh_tunnel->inner_nh_offset = htole16(inner_nh);
+
+ vh->csum_start = inner_nh + inner_iphlen;
vh->csum_offset = __builtin_offsetof(struct udphdr, check);
vh->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
vh->hdr_len = hdr_len;
@@ -395,4 +400,10 @@ static inline int build_virtio_net_hdr_v1_hash_tunnel(uint8_t *buf, bool is_tap,
return sizeof(struct virtio_net_hdr_v1_hash_tunnel);
}
+/* Add @delta to a little endian tunnel offset of the virtio header. */
+static inline void vnet_tnl_offset_add(__le16 *off, int delta)
+{
+ *off = htole16(le16toh(*off) + delta);
+}
+
#endif /* _TUNTAP_HELPERS_H */
--
2.53.0
prev parent reply other threads:[~2026-11-05 11:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 11:57 [PATCH net v3 0/8] net: virtio: validate outer protocol of UDP tunnel GSO packets Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 1/8] " Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 2/8] net: virtio: do not use dev->type to find the outer L3 header Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 3/8] tun: clear stale skb->protocol of NAPI frags skbs Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 4/8] selftests: net: tuntap: fix UDP checksum length in build_udp_packet_csum() Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 5/8] selftests: net: tun: test outer protocol of UDP tunnel GSO packets Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 6/8] selftests: net: tun: test UDP tunnel GSO on retyped devices Eric Dumazet
2026-11-05 11:57 ` [PATCH net v3 7/8] selftests: net: tun: test more outer header mismatches of UDP tunnel GSO Eric Dumazet
2026-11-05 11:57 ` Eric Dumazet [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261105115715.1466924-9-edumazet@kernel.org \
--to=edumazet@kernel.org \
--cc=davem@davemloft.net \
--cc=eperezma@redhat.com \
--cc=hassansultan@google.com \
--cc=horms@kernel.org \
--cc=jasowangio@gmail.com \
--cc=kuba@kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox