OSDN Git Service

bpf: Add bpf_get_listener_sock(struct bpf_sock *sk) helper
authorMartin KaFai Lau <kafai@fb.com>
Tue, 12 Mar 2019 17:23:04 +0000 (10:23 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 13 Mar 2019 19:04:35 +0000 (12:04 -0700)
commitdbafd7ddd62369b2f3926ab847cbf8fc40e800b7
tree35ce32dfc258798b98bb4d7e4253f34bb4cf43e1
parent1b986589680a2a5b6fc1ac196ea69925a93d9dd9
bpf: Add bpf_get_listener_sock(struct bpf_sock *sk) helper

Add a new helper "struct bpf_sock *bpf_get_listener_sock(struct bpf_sock *sk)"
which returns a bpf_sock in TCP_LISTEN state.  It will trace back to
the listener sk from a request_sock if possible.  It returns NULL
for all other cases.

No reference is taken because the helper ensures the sk is
in SOCK_RCU_FREE (where the TCP_LISTEN sock should be in).
Hence, bpf_sk_release() is unnecessary and the verifier does not
allow bpf_sk_release(listen_sk) to be called either.

The following is also allowed because the bpf_prog is run under
rcu_read_lock():

sk = bpf_sk_lookup_tcp();
/* if (!sk) { ... } */
listen_sk = bpf_get_listener_sock(sk);
/* if (!listen_sk) { ... } */
bpf_sk_release(sk);
src_port = listen_sk->src_port; /* Allowed */

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/uapi/linux/bpf.h
net/core/filter.c