Netdev List
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp
@ 2018-11-07 21:36 Andrey Ignatov
  2018-11-07 21:51 ` Joe Stringer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrey Ignatov @ 2018-11-07 21:36 UTC (permalink / raw)
  To: netdev; +Cc: Andrey Ignatov, ast, daniel, joe, kernel-team

Lookup functions in sk_lookup have different expectations about byte
order of provided arguments.

Specifically __inet_lookup, __udp4_lib_lookup and __udp6_lib_lookup
expect dport to be in network byte order and do ntohs(dport) internally.

At the same time __inet6_lookup expects dport to be in host byte order
and correspondingly name the argument hnum.

sk_lookup works correctly with __inet_lookup, __udp4_lib_lookup and
__inet6_lookup with regard to dport. But in __udp6_lib_lookup case it
uses host instead of expected network byte order. It makes result
returned by bpf_sk_lookup_udp for IPv6 incorrect.

The patch fixes byte order of dport passed to __udp6_lib_lookup.

Originally sk_lookup properly handled UDPv6, but not TCPv6. 5ef0ae84f02a
fixes TCPv6 but breaks UDPv6.

Fixes: 5ef0ae84f02a ("bpf: Fix IPv6 dport byte-order in bpf_sk_lookup")
Signed-off-by: Andrey Ignatov <rdna@fb.com>
---
 net/core/filter.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index e521c5ebc7d1..9a1327eb25fa 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4852,18 +4852,17 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
 	} else {
 		struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
 		struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
-		u16 hnum = ntohs(tuple->ipv6.dport);
 		int sdif = inet6_sdif(skb);
 
 		if (proto == IPPROTO_TCP)
 			sk = __inet6_lookup(net, &tcp_hashinfo, skb, 0,
 					    src6, tuple->ipv6.sport,
-					    dst6, hnum,
+					    dst6, ntohs(tuple->ipv6.dport),
 					    dif, sdif, &refcounted);
 		else if (likely(ipv6_bpf_stub))
 			sk = ipv6_bpf_stub->udp6_lib_lookup(net,
 							    src6, tuple->ipv6.sport,
-							    dst6, hnum,
+							    dst6, tuple->ipv6.dport,
 							    dif, sdif,
 							    &udp_table, skb);
 #endif
-- 
2.17.1

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

* Re: [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp
  2018-11-07 21:36 [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp Andrey Ignatov
@ 2018-11-07 21:51 ` Joe Stringer
  2018-11-07 21:55 ` Martin Lau
  2018-11-09  7:40 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Joe Stringer @ 2018-11-07 21:51 UTC (permalink / raw)
  To: rdna; +Cc: netdev, ast, daniel, Joe Stringer, kernel-team

On Wed, 7 Nov 2018 at 13:37, Andrey Ignatov <rdna@fb.com> wrote:
>
> Lookup functions in sk_lookup have different expectations about byte
> order of provided arguments.
>
> Specifically __inet_lookup, __udp4_lib_lookup and __udp6_lib_lookup
> expect dport to be in network byte order and do ntohs(dport) internally.
>
> At the same time __inet6_lookup expects dport to be in host byte order
> and correspondingly name the argument hnum.
>
> sk_lookup works correctly with __inet_lookup, __udp4_lib_lookup and
> __inet6_lookup with regard to dport. But in __udp6_lib_lookup case it
> uses host instead of expected network byte order. It makes result
> returned by bpf_sk_lookup_udp for IPv6 incorrect.
>
> The patch fixes byte order of dport passed to __udp6_lib_lookup.
>
> Originally sk_lookup properly handled UDPv6, but not TCPv6. 5ef0ae84f02a
> fixes TCPv6 but breaks UDPv6.
>
> Fixes: 5ef0ae84f02a ("bpf: Fix IPv6 dport byte-order in bpf_sk_lookup")
> Signed-off-by: Andrey Ignatov <rdna@fb.com>

Thanks for the fix, makes sense.

Acked-by: Joe Stringer <joe@wand.net.nz>

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

* Re: [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp
  2018-11-07 21:36 [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp Andrey Ignatov
  2018-11-07 21:51 ` Joe Stringer
@ 2018-11-07 21:55 ` Martin Lau
  2018-11-09  7:40 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Martin Lau @ 2018-11-07 21:55 UTC (permalink / raw)
  To: Andrey Ignatov
  Cc: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	joe@wand.net.nz, Kernel Team

On Wed, Nov 07, 2018 at 01:36:07PM -0800, Andrey Ignatov wrote:
> Lookup functions in sk_lookup have different expectations about byte
> order of provided arguments.
> 
> Specifically __inet_lookup, __udp4_lib_lookup and __udp6_lib_lookup
> expect dport to be in network byte order and do ntohs(dport) internally.
> 
> At the same time __inet6_lookup expects dport to be in host byte order
> and correspondingly name the argument hnum.
> 
> sk_lookup works correctly with __inet_lookup, __udp4_lib_lookup and
> __inet6_lookup with regard to dport. But in __udp6_lib_lookup case it
> uses host instead of expected network byte order. It makes result
> returned by bpf_sk_lookup_udp for IPv6 incorrect.
> 
> The patch fixes byte order of dport passed to __udp6_lib_lookup.
> 
> Originally sk_lookup properly handled UDPv6, but not TCPv6. 5ef0ae84f02a
> fixes TCPv6 but breaks UDPv6.
> 
> Fixes: 5ef0ae84f02a ("bpf: Fix IPv6 dport byte-order in bpf_sk_lookup")
> Signed-off-by: Andrey Ignatov <rdna@fb.com>
Good catch.

Acked-by: Martin KaFai Lau <kafai@fb.com>

> ---
>  net/core/filter.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/net/core/filter.c b/net/core/filter.c
> index e521c5ebc7d1..9a1327eb25fa 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -4852,18 +4852,17 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
>  	} else {
>  		struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
>  		struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
> -		u16 hnum = ntohs(tuple->ipv6.dport);
>  		int sdif = inet6_sdif(skb);
>  
>  		if (proto == IPPROTO_TCP)
>  			sk = __inet6_lookup(net, &tcp_hashinfo, skb, 0,
>  					    src6, tuple->ipv6.sport,
> -					    dst6, hnum,
> +					    dst6, ntohs(tuple->ipv6.dport),
>  					    dif, sdif, &refcounted);
>  		else if (likely(ipv6_bpf_stub))
>  			sk = ipv6_bpf_stub->udp6_lib_lookup(net,
>  							    src6, tuple->ipv6.sport,
> -							    dst6, hnum,
> +							    dst6, tuple->ipv6.dport,
>  							    dif, sdif,
>  							    &udp_table, skb);
>  #endif
> -- 
> 2.17.1
> 

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

* Re: [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp
  2018-11-07 21:36 [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp Andrey Ignatov
  2018-11-07 21:51 ` Joe Stringer
  2018-11-07 21:55 ` Martin Lau
@ 2018-11-09  7:40 ` Daniel Borkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2018-11-09  7:40 UTC (permalink / raw)
  To: Andrey Ignatov, netdev; +Cc: ast, joe, kernel-team

On 11/07/2018 10:36 PM, Andrey Ignatov wrote:
> Lookup functions in sk_lookup have different expectations about byte
> order of provided arguments.
> 
> Specifically __inet_lookup, __udp4_lib_lookup and __udp6_lib_lookup
> expect dport to be in network byte order and do ntohs(dport) internally.
> 
> At the same time __inet6_lookup expects dport to be in host byte order
> and correspondingly name the argument hnum.
> 
> sk_lookup works correctly with __inet_lookup, __udp4_lib_lookup and
> __inet6_lookup with regard to dport. But in __udp6_lib_lookup case it
> uses host instead of expected network byte order. It makes result
> returned by bpf_sk_lookup_udp for IPv6 incorrect.
> 
> The patch fixes byte order of dport passed to __udp6_lib_lookup.
> 
> Originally sk_lookup properly handled UDPv6, but not TCPv6. 5ef0ae84f02a
> fixes TCPv6 but breaks UDPv6.
> 
> Fixes: 5ef0ae84f02a ("bpf: Fix IPv6 dport byte-order in bpf_sk_lookup")
> Signed-off-by: Andrey Ignatov <rdna@fb.com>

Applied to bpf, thanks Andrey!

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

end of thread, other threads:[~2018-11-09 17:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-11-07 21:36 [PATCH bpf] bpf: Fix IPv6 dport byte order in bpf_sk_lookup_udp Andrey Ignatov
2018-11-07 21:51 ` Joe Stringer
2018-11-07 21:55 ` Martin Lau
2018-11-09  7:40 ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox