]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Avoid subtraction after htons() in ipip tests
authorAsbjørn Sloth Tønnesen <ast@fiberby.net>
Thu, 8 Aug 2024 07:59:02 +0000 (07:59 +0000)
committerMartin KaFai Lau <martin.lau@kernel.org>
Tue, 13 Aug 2024 23:43:15 +0000 (16:43 -0700)
On little-endian systems, doing subtraction after htons()
leads to interesting results:

Given:
  MAGIC_BYTES = 123 = 0x007B aka. in big endian: 0x7B00 = 31488
  sizeof(struct iphdr) = 20

Before this patch:
__bpf_constant_htons(MAGIC_BYTES) - sizeof(struct iphdr) = 0x7AEC
0x7AEC = htons(0xEC7A) = htons(60538)

So these were outer IP packets with a total length of 123 bytes,
containing an inner IP packet with a total length of 60538 bytes.

After this patch:
__bpf_constant_htons(MAGIC_BYTES - sizeof(struct iphdr)) = htons(103)

Now these packets are outer IP packets with a total length of 123 bytes,
containing an inner IP packet with a total length of 103 bytes.

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
Reviewed-by: Toke Høiland-Jørgensen <toke@kernel.org>
Link: https://lore.kernel.org/r/20240808075906.1849564-1-ast@fiberby.net
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/prog_tests/flow_dissector.c

index 9e5f38739104bf540ae143fd4e663d44a8ffe4d8..6b3078dd56455f762221ff2d5392345fb9c2c110 100644 (file)
@@ -378,8 +378,8 @@ struct test tests[] = {
                        .iph_inner.ihl = 5,
                        .iph_inner.protocol = IPPROTO_TCP,
                        .iph_inner.tot_len =
-                               __bpf_constant_htons(MAGIC_BYTES) -
-                               sizeof(struct iphdr),
+                               __bpf_constant_htons(MAGIC_BYTES -
+                               sizeof(struct iphdr)),
                        .tcp.doff = 5,
                        .tcp.source = 80,
                        .tcp.dest = 8080,
@@ -407,8 +407,8 @@ struct test tests[] = {
                        .iph_inner.ihl = 5,
                        .iph_inner.protocol = IPPROTO_TCP,
                        .iph_inner.tot_len =
-                               __bpf_constant_htons(MAGIC_BYTES) -
-                               sizeof(struct iphdr),
+                               __bpf_constant_htons(MAGIC_BYTES -
+                               sizeof(struct iphdr)),
                        .tcp.doff = 5,
                        .tcp.source = 80,
                        .tcp.dest = 8080,
@@ -436,8 +436,8 @@ struct test tests[] = {
                        .iph_inner.ihl = 5,
                        .iph_inner.protocol = IPPROTO_TCP,
                        .iph_inner.tot_len =
-                               __bpf_constant_htons(MAGIC_BYTES) -
-                               sizeof(struct iphdr),
+                               __bpf_constant_htons(MAGIC_BYTES -
+                               sizeof(struct iphdr)),
                        .tcp.doff = 5,
                        .tcp.source = 99,
                        .tcp.dest = 9090,