netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: Avoid subtraction after htons() in ipip tests
@ 2024-08-08  7:59 Asbjørn Sloth Tønnesen
  2024-08-08  9:24 ` Toke Høiland-Jørgensen
  0 siblings, 1 reply; 3+ messages in thread
From: Asbjørn Sloth Tønnesen @ 2024-08-08  7:59 UTC (permalink / raw)
  To: bpf
  Cc: Asbjørn Sloth Tønnesen, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan, Tushar Vyavahare, Magnus Karlsson, Willem de Bruijn,
	netdev, linux-kselftest, linux-kernel

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>
---
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(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c
index 9e5f38739104..6b3078dd5645 100644
--- a/tools/testing/selftests/bpf/prog_tests/flow_dissector.c
+++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector.c
@@ -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,
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: Avoid subtraction after htons() in ipip tests
  2024-08-08  7:59 [PATCH bpf-next] selftests/bpf: Avoid subtraction after htons() in ipip tests Asbjørn Sloth Tønnesen
@ 2024-08-08  9:24 ` Toke Høiland-Jørgensen
  2024-08-14  1:15   ` Martin KaFai Lau
  0 siblings, 1 reply; 3+ messages in thread
From: Toke Høiland-Jørgensen @ 2024-08-08  9:24 UTC (permalink / raw)
  To: Asbjørn Sloth Tønnesen, bpf
  Cc: Asbjørn Sloth Tønnesen, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan, Tushar Vyavahare, Magnus Karlsson, Willem de Bruijn,
	netdev, linux-kselftest, linux-kernel

Asbjørn Sloth Tønnesen <ast@fiberby.net> writes:

> 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.

It's just using bag of holding technology!

> 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>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: Avoid subtraction after htons() in ipip tests
  2024-08-08  9:24 ` Toke Høiland-Jørgensen
@ 2024-08-14  1:15   ` Martin KaFai Lau
  0 siblings, 0 replies; 3+ messages in thread
From: Martin KaFai Lau @ 2024-08-14  1:15 UTC (permalink / raw)
  To: Asbjørn Sloth Tønnesen,
	Toke Høiland-Jørgensen
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan, Tushar Vyavahare, Magnus Karlsson, Willem de Bruijn,
	netdev, linux-kselftest, linux-kernel

On 8/8/24 2:24 AM, Toke Høiland-Jørgensen wrote:
> Asbjørn Sloth Tønnesen <ast@fiberby.net> writes:
> 
>> 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.
> 
> It's just using bag of holding technology!
> 
>> 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>

Applied to bpf-next/net. Thanks.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-08-14  1:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-08  7:59 [PATCH bpf-next] selftests/bpf: Avoid subtraction after htons() in ipip tests Asbjørn Sloth Tønnesen
2024-08-08  9:24 ` Toke Høiland-Jørgensen
2024-08-14  1:15   ` Martin KaFai Lau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).