瀏覽代碼

bpf: Add BPF_SOCK_OPS_TCP_LISTEN_CB

Add new TCP-BPF callback that is called on listen(2) right after socket
transition to TCP_LISTEN state.

It fills the gap for listening sockets in TCP-BPF. For example BPF
program can set BPF_SOCK_OPS_STATE_CB_FLAG when socket becomes listening
and track later transition from TCP_LISTEN to TCP_CLOSE with
BPF_SOCK_OPS_STATE_CB callback.

Before there was no way to do it with TCP-BPF and other options were
much harder to work with. E.g. socket state tracking can be done with
tracepoints (either raw or regular) but they can't be attached to cgroup
and their lifetime has to be managed separately.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Andrey Ignatov 7 年之前
父節點
當前提交
f333ee0cdb
共有 2 個文件被更改,包括 4 次插入0 次删除
  1. 3 0
      include/uapi/linux/bpf.h
  2. 1 0
      net/ipv4/af_inet.c

+ 3 - 0
include/uapi/linux/bpf.h

@@ -2555,6 +2555,9 @@ enum {
 					 * Arg1: old_state
 					 * Arg1: old_state
 					 * Arg2: new_state
 					 * Arg2: new_state
 					 */
 					 */
+	BPF_SOCK_OPS_TCP_LISTEN_CB,	/* Called on listen(2), right after
+					 * socket transition to LISTEN state.
+					 */
 };
 };
 
 
 /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
 /* List of TCP states. There is a build check in net/ipv4/tcp.c to detect

+ 1 - 0
net/ipv4/af_inet.c

@@ -229,6 +229,7 @@ int inet_listen(struct socket *sock, int backlog)
 		err = inet_csk_listen_start(sk, backlog);
 		err = inet_csk_listen_start(sk, backlog);
 		if (err)
 		if (err)
 			goto out;
 			goto out;
+		tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);
 	}
 	}
 	sk->sk_max_ack_backlog = backlog;
 	sk->sk_max_ack_backlog = backlog;
 	err = 0;
 	err = 0;