bpf: Check sk_fullsock() before returning from bpf_sk_lookup()
Subsystems (4)
PATCH 1/1 bpf: Check sk_fullsock() before returning from bpf_sk_lookup()
The BPF_FUNC_sk_lookup_xxx helpers return RET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields in bpf_sock can be accessed, e.g. type, protocol, mark and priority.
Some new helper, like bpf_sk_storage_get(), also expects ARG_PTR_TO_SOCKET is a fullsock.
bpf_sk_lookup() currently calls sk_to_full_sk() before returning. However, the ptr returned from sk_to_full_sk() is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is in TCP_TIME_WAIT.
This patch checks for sk_fullsock() before returning. If it is not a fullsock, sock_gen_put() is called if needed and then returns NULL.
--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
net/core/filter.c +14 -2
@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,struct sock *sk =__bpf_skc_lookup (skb, tuple, len, caller_net, ifindex, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk); +return NULL ; + } + }return sk; }
@@ -5369,8 +5375,14 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,struct sock *sk =bpf_skc_lookup (skb, tuple, len, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk); +return NULL ; + } + }return sk; }
Signature · 1 line
2.17.1
Quoted 26 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)net/core/filter.c +0
-0
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · line 5344
Quoted 9 lines
net/core/filter.c +4
-1@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,struct sock *sk =__bpf_skc_lookup (skb, tuple, len, caller_net, ifindex, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);
This looks a bit convoluted/weird.
What about telling/asking __bpf_skc_lookup() to not return a non fullsock instead ?
Quoted 23 lines
net/core/filter.c +10
-1+return NULL ; + } + }return sk; }@@ -5369,8 +5375,14 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,struct sock *sk =bpf_skc_lookup (skb, tuple, len, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk); +return NULL ; + } + }return sk; }
Quoted 41 lines · Eric Dumazet
Quoted 26 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)net/core/filter.c +0
-0net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · line 5344
Quoted 9 lines
net/core/filter.c +4
-1@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,struct sock *sk =__bpf_skc_lookup (skb, tuple, len, caller_net, ifindex, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);This looks a bit convoluted/weird.
Quoted 1 line
What about telling/asking
__bpf_skc_lookup()to not return a non fullsock instead ?
It is becausee some other helpers, like BPF_FUNC_skc_lookup_tcp, can return non fullsock.
Quoted 24 lines
Quoted 24 lines
net/core/filter.c +10
-1+return NULL ; + } + }return sk; }@@ -5369,8 +5375,14 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,struct sock *sk =bpf_skc_lookup (skb, tuple, len, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk); +return NULL ; + } + }return sk; }
A message that is not in the archive (sent off-list or not yet received): CADa=RyxisbcVeXL7yq6o02XOgWd87QCzq-6zDXRnm9RoD2WM=A@mail.gmail.com
Quoted 46 lines · Joe Stringer
Quoted 44 lines · On Fri, May 17, 2019, 12:02 Martin Lau <[email protected]> wrote:
Quoted 24 lines · Eric Dumazet
Quoted 21 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)Quoted 5 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct
Quoted 5 lines
net/core/filter.c +0
-0@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct
bpf_sock_tuple*tuple, u32 len,Quoted 2 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5338
Quoted 2 lines
net/core/filter.c +0
-0struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, netns_id,flags);
Quoted 10 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · line 5344
Quoted 7 lines
net/core/filter.c +4
-1-if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);This looks a bit convoluted/weird.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
What about telling/asking
__bpf_skc_lookup()to not return a nonfullsock instead ?
It is becausee some other helpers, likeBPF_FUNC_skc_lookup_tcp, can return non fullsockFYI this is necessary for finding a transparently proxied socket for a non-local connection (tproxy use case).
You meant it is necessary to return a non fullsock from the BPF_FUNC_sk_lookup_xxx helpers?
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines
net/core/filter.c bpf_sk_lookup(struct sk_buff *skb, struct
Quoted 10 lines
net/core/filter.c +3
-0+return NULL ; + } + }return sk; }@@ -5369,8 +5375,14 @@ bpf_sk_lookup(struct sk_buff *skb, struct
bpf_sock_tuple*tuple, u32 len,Quoted 16 lines
Quoted 16 lines
net/core/filter.c +7
-1struct sock *sk =bpf_skc_lookup (skb, tuple, len, proto, netns_id, flags); -if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk); +return NULL ; + } + }return sk; }
Quoted 54 lines · On Sat, May 18, 2019, 09:05 Martin Lau <[email protected]> wrote:
Quoted 52 lines · Joe Stringer
Quoted 49 lines · On Fri, May 17, 2019, 12:02 Martin Lau <[email protected]> wrote:
Quoted 24 lines · Eric Dumazet
Quoted 21 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)Quoted 5 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct
Quoted 5 lines
net/core/filter.c +0
-0@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct
bpf_sock_tuple*tuple, u32 len,Quoted 2 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5338
Quoted 2 lines
net/core/filter.c +0
-0struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, netns_id,flags);
Quoted 11 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · line 5344
Quoted 7 lines
net/core/filter.c +4
-1-if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);This looks a bit convoluted/weird.
What about telling/asking
__bpf_skc_lookup()to not return a nonfullsock instead ?
It is becausee some other helpers, likeBPF_FUNC_skc_lookup_tcp, can return non fullsock
Quoted 4 lines
Quoted 2 lines
FYI this is necessary for finding a transparently proxied socket for a non-local connection (tproxy use case).
You meant it is necessary to return a non fullsock from the
BPF_FUNC_sk_lookup_xxxhelpers?
Yes, that's what I want to associate with the skb so that the delivery to the SO_TRANSPARENT is received properly.
For the first packet of a connection, we look up the socket using the tproxy socket port as the destination, and deliver the packet there. The SO_TRANSPARENT logic then kicks in and sends back the ack and creates the non-full sock for the connection tuple, which can be entirely unrelated to local addresses or ports.
For the second forward-direction packet, (ie ACK in 3-way handshake) then we must deliver the packet to this non-full sock as that's what is negotiating the proxied connection. If you look up using the packet tuple then get the full sock from it, it will go back to the SO_TRANSPARENT parent socket. Delivering the ACK there will result in a RST being sent back, because the SO_TRANSPARENT socket is just there to accept new connections for connections to be proxied. So this is the case where I need the non-full sock.
(In practice, the lookup logic attempts the packet tuple first then if that fails, uses the tproxy port for lookup to achieve the above).
Quoted 78 lines · Joe Stringer
Quoted 54 lines · On Sat, May 18, 2019, 09:05 Martin Lau <[email protected]> wrote:
Quoted 52 lines · Joe Stringer
Quoted 49 lines · On Fri, May 17, 2019, 12:02 Martin Lau <[email protected]> wrote:
Quoted 24 lines · Eric Dumazet
Quoted 21 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)Quoted 5 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct
Quoted 5 lines
net/core/filter.c +0
-0@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct
bpf_sock_tuple*tuple, u32 len,Quoted 2 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5338
Quoted 2 lines
net/core/filter.c +0
-0struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, netns_id,flags);
Quoted 11 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · line 5344
Quoted 7 lines
net/core/filter.c +4
-1-if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);This looks a bit convoluted/weird.
What about telling/asking
__bpf_skc_lookup()to not return a nonfullsock instead ?
It is becausee some other helpers, likeBPF_FUNC_skc_lookup_tcp, can return non fullsockQuoted 4 lines
Quoted 2 lines
FYI this is necessary for finding a transparently proxied socket for a non-local connection (tproxy use case).
You meant it is necessary to return a non fullsock from the
BPF_FUNC_sk_lookup_xxxhelpers?Yes, that's what I want to associate with the skb so that the delivery to the
SO_TRANSPARENTis received properly.For the first packet of a connection, we look up the socket using the tproxy socket port as the destination, and deliver the packet there. The
SO_TRANSPARENTlogic then kicks in and sends back the ack and creates the non-full sock for the connection tuple, which can be entirely unrelated to local addresses or ports.For the second forward-direction packet, (ie ACK in 3-way handshake) then we must deliver the packet to this non-full sock as that's what is negotiating the proxied connection. If you look up using the packet tuple then get the full sock from it, it will go back to the
SO_TRANSPARENTparent socket. Delivering the ACK there will result in a RST being sent back, because theSO_TRANSPARENTsocket is just there to accept new connections for connections to be proxied. So this is the case where I need the non-full sock.
Quoted 2 lines
(In practice, the lookup logic attempts the packet tuple first then if that fails, uses the tproxy port for lookup to achieve the above).
hmm...I am likely missing something.
1) The above can be done by the "BPF_FUNC_skC_lookup_tcp" which returns a non fullsock (RET_PTR_TO_SOCK_COMMON_OR_NULL), no?
2) The bpf_func_proto of "BPF_FUNC_sk_lookup_tcp" returns fullsock (RET_PTR_TO_SOCKET_OR_NULL) and the bpf_prog (and the verifier) is expecting that. How to address the bug here?
Quoted 81 lines · Martin Lau
Quoted 78 lines · Joe Stringer
Quoted 54 lines · On Sat, May 18, 2019, 09:05 Martin Lau <[email protected]> wrote:
Quoted 52 lines · Joe Stringer
Quoted 49 lines · On Fri, May 17, 2019, 12:02 Martin Lau <[email protected]> wrote:
Quoted 24 lines · Eric Dumazet
Quoted 21 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)Quoted 5 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct
Quoted 5 lines
net/core/filter.c +0
-0@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct
bpf_sock_tuple*tuple, u32 len,Quoted 2 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5338
Quoted 2 lines
net/core/filter.c +0
-0struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, netns_id,flags);
Quoted 11 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5343
Quoted 7 lines
net/core/filter.c +4
-1-if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);This looks a bit convoluted/weird.
What about telling/asking
__bpf_skc_lookup()to not return a nonfullsock instead ?
It is becausee some other helpers, likeBPF_FUNC_skc_lookup_tcp, can return non fullsockQuoted 4 lines
Quoted 2 lines
FYI this is necessary for finding a transparently proxied socket for a non-local connection (tproxy use case).
You meant it is necessary to return a non fullsock from the
BPF_FUNC_sk_lookup_xxxhelpers?Yes, that's what I want to associate with the skb so that the delivery to the
SO_TRANSPARENTis received properly.For the first packet of a connection, we look up the socket using the tproxy socket port as the destination, and deliver the packet there. The
SO_TRANSPARENTlogic then kicks in and sends back the ack and creates the non-full sock for the connection tuple, which can be entirely unrelated to local addresses or ports.For the second forward-direction packet, (ie ACK in 3-way handshake) then we must deliver the packet to this non-full sock as that's what is negotiating the proxied connection. If you look up using the packet tuple then get the full sock from it, it will go back to the
SO_TRANSPARENTparent socket. Delivering the ACK there will result in a RST being sent back, because theSO_TRANSPARENTsocket is just there to accept new connections for connections to be proxied. So this is the case where I need the non-full sock.Quoted 2 lines
(In practice, the lookup logic attempts the packet tuple first then if that fails, uses the tproxy port for lookup to achieve the above).
Quoted 8 lines
hmm...I am likely missing something.
1) The above can be done by the "
BPF_FUNC_skC_lookup_tcp" which returns a non fullsock (RET_PTR_TO_SOCK_COMMON_OR_NULL), no?2) The
bpf_func_protoof "BPF_FUNC_sk_lookup_tcp" returns fullsock (RET_PTR_TO_SOCKET_OR_NULL) and thebpf_prog(and the verifier) is expecting that. How to address the bug here?
Joe, do you have other concerns on this bug fix?
Quoted 80 lines · Martin Lau
Quoted 78 lines · Joe Stringer
Quoted 54 lines · On Sat, May 18, 2019, 09:05 Martin Lau <[email protected]> wrote:
Quoted 52 lines · Joe Stringer
Quoted 49 lines · On Fri, May 17, 2019, 12:02 Martin Lau <[email protected]> wrote:
Quoted 24 lines · Eric Dumazet
Quoted 21 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>--- net/core/filter.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)Quoted 5 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct
Quoted 5 lines
net/core/filter.c +0
-0@@ -5337,8 +5337,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct
bpf_sock_tuple*tuple, u32 len,Quoted 2 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5338
Quoted 2 lines
net/core/filter.c +0
-0struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, netns_id,flags);
Quoted 11 lines
net/core/filter.c __bpf_sk_lookup(struct sk_buff *skb, struct · line 5343
Quoted 7 lines
net/core/filter.c +4
-1-if (sk) +if (sk) { sk =sk_to_full_sk (sk); +if (!sk_fullsock (sk)) { +if (!sock_flag (sk,SOCK_RCU_FREE )) +sock_gen_put (sk);This looks a bit convoluted/weird.
What about telling/asking
__bpf_skc_lookup()to not return a nonfullsock instead ?
It is becausee some other helpers, likeBPF_FUNC_skc_lookup_tcp, can return non fullsockQuoted 4 lines
Quoted 2 lines
FYI this is necessary for finding a transparently proxied socket for a non-local connection (tproxy use case).
You meant it is necessary to return a non fullsock from the
BPF_FUNC_sk_lookup_xxxhelpers?Yes, that's what I want to associate with the skb so that the delivery to the
SO_TRANSPARENTis received properly.For the first packet of a connection, we look up the socket using the tproxy socket port as the destination, and deliver the packet there. The
SO_TRANSPARENTlogic then kicks in and sends back the ack and creates the non-full sock for the connection tuple, which can be entirely unrelated to local addresses or ports.For the second forward-direction packet, (ie ACK in 3-way handshake) then we must deliver the packet to this non-full sock as that's what is negotiating the proxied connection. If you look up using the packet tuple then get the full sock from it, it will go back to the
SO_TRANSPARENTparent socket. Delivering the ACK there will result in a RST being sent back, because theSO_TRANSPARENTsocket is just there to accept new connections for connections to be proxied. So this is the case where I need the non-full sock.
Quoted 6 lines
Quoted 2 lines
(In practice, the lookup logic attempts the packet tuple first then if that fails, uses the tproxy port for lookup to achieve the above).
hmm...I am likely missing something.
1) The above can be done by the "
BPF_FUNC_skC_lookup_tcp" which returns a non fullsock (RET_PTR_TO_SOCK_COMMON_OR_NULL), no?
Correct, I meant to send as response to Eric as to use cases for __bpf_skc_lookup() returning non fullsock.
Quoted 3 lines
2) The
bpf_func_protoof "BPF_FUNC_sk_lookup_tcp" returns fullsock (RET_PTR_TO_SOCKET_OR_NULL) and thebpf_prog(and the verifier) is expecting that. How to address the bug here?
Your proposal seems fine to me.
Quoted 12 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.
Quoted 7 lines
This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>---
Quoted 11 lines · Martin KaFai Lau
The
BPF_FUNC_sk_lookup_xxxhelpers returnRET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields inbpf_sockcan be accessed, e.g. type, protocol, mark and priority.
Some new helper, likebpf_sk_storage_get(), also expectsARG_PTR_TO_SOCKETis a fullsock.
bpf_sk_lookup()currently callssk_to_full_sk()before returning. However, the ptr returned fromsk_to_full_sk()is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is inTCP_TIME_WAIT.
Quoted 6 lines
This patch checks for
sk_fullsock()before returning. If it is not a fullsock,sock_gen_put()is called if needed and then returns NULL.Fixes 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF")Cc Joe Stringer <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>
Applied, thanks!