Skip to contentSkip to conversation

selftests/bpf: Avoid subtraction after htons() in ipip tests

PATCH Applied Asbjørn Sloth Tønnesen 3 participants 3 messages Aug 8, 2024 – Aug 14, 2024

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

PATCH 1/1 selftests/bpf: Avoid subtraction after htons() in ipip tests

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.

---
I didn't target bpf and add a Fixes: e853ae776a58 ("selftests/bpf:
support BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP"), since it only breaks when I change the BPF flow dissector to interact with tot_len.

 .../selftests/bpf/prog_tests/flow_dissector.c        | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/flow_dissector.c +6 -6
index 9e5f38739104..6b3078dd5645 100644
@@ -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,
Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Asbjørn Sloth Tønnesen

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

Quoted 6 lines

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.

It's just using bag of holding technology!

Quoted 7 lines

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.

reviewer Applied
#2 lore raw
Quoted 21 lines · Toke Høiland-Jørgensen
Quoted 7 lines · Asbjørn Sloth Tønnesen

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

Quoted 6 lines

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.

It's just using bag of holding technology!

Quoted 3 lines

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

Quoted 6 lines
Quoted 4 lines

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.

Applied to bpf-next/net. Thanks.