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 <[email protected]>
Closes: 
https://lore.kernel.org/netdev/[email protected]/
Signed-off-by: Eric Dumazet <[email protected]>
---
 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


Reply via email to