History

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

Martin KaFai Lau committed 7 years ago

Commit dbafd7d1 parent 1b986582 files · +31 −1Browse files

Showing the changes to include/uapi/linux (1 of 2 files). Show all

Unified · Split

include/uapi/linux/bpf.h+10−1
@@ -2366,6 +2366,14 @@ union bpf_attr { · 2365 unmodified lines
23662366 * current value is ect (ECN capable). Works with IPv6 and IPv4.
23672367 * Return
23682368 * 1 if set, 0 if not set.
2369+ *
2370+ * struct bpf_sock *bpf_get_listener_sock(struct bpf_sock *sk)
2371+ * Description
2372+ * Return a **struct bpf_sock** pointer in TCP_LISTEN state.
2373+ * bpf_sk_release() is unnecessary and not allowed.
2374+ * Return
2375+ * A **struct bpf_sock** pointer on success, or NULL in
2376+ * case of failure.
23692377 */
23702378 #define __BPF_FUNC_MAPPER(FN) \
23712379 FN(unspec), \
@@ -2465,7 +2473,8 @@ union bpf_attr { · 93 unmodified lines
24652473 FN(spin_unlock), \
24662474 FN(sk_fullsock), \
24672475 FN(tcp_sock), \
2468− FN(skb_ecn_set_ce),
2476+ FN(skb_ecn_set_ce), \
2477+ FN(get_listener_sock),
24692478
24702479 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
24712480 * function eBPF program intends to call