Skip to contentSkip to conversation

bpf tcp save syn set/get sockoptions

PATCH Applied Nikita V. Shirokov 2 participants 4 messages Aug 30, 2018 – Aug 31, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover bpf tcp save syn set/get sockoptions

adding supprot for two new bpf's tcp sockopts:
TCP_SAVE_SYN (set) and TCP_SAVED_SYN (get)
this would allow for tcp-bpf program to build some logic based on fields from ingress syn packet (e.g. doing tcp's tos/tclass reflection (see sample prog)) and do it transparently from userspace program point of view

v2->v3:
- make patch series public
v1->v2:
- adding proper SPDX license

Nikita V. Shirokov (2):
new options for bpf_(set|get)sockopt
new sample bpf prog

 net/core/filter.c                  | 25 +++++++--
 samples/bpf/Makefile               |  1 +
 samples/bpf/tcp_tos_reflect_kern.c | 87 ++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+), 4 deletions(-)
 create mode 100644 samples/bpf/tcp_tos_reflect_kern.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/2 new options for bpf_(set|get)sockopt

adding support for two new bpf's get/set sockopts: TCP_SAVE_SYN (set) and TCP_SAVED_SYN (get). this would allow for bpf program to build logic based on data from ingress SYN packet

---
 net/core/filter.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
net/core/filter.c +21 -4
index c25eb36f1320..feb578506009 100644
@@ -4007,6 +4007,12 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
 					tp->snd_ssthresh = val;
 				}
 				break;
+			case TCP_SAVE_SYN:
+				if (val < 0 || val > 1)
+					ret = -EINVAL;
+				else
+					tp->save_syn = val;
+				break;
 			default:
 				ret = -EINVAL;
 			}
@@ -4032,21 +4038,32 @@ static const struct bpf_func_proto bpf_setsockopt_proto = {
 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
 	   int, level, int, optname, char *, optval, int, optlen)
 {
+	struct inet_connection_sock *icsk;
 	struct sock *sk = bpf_sock->sk;
+	struct tcp_sock *tp;
 
 	if (!sk_fullsock(sk))
 		goto err_clear;
-
 #ifdef CONFIG_INET
 	if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
-		if (optname == TCP_CONGESTION) {
-			struct inet_connection_sock *icsk = inet_csk(sk);
+		switch (optname) {
+		case TCP_CONGESTION:
+			icsk = inet_csk(sk);
 
 			if (!icsk->icsk_ca_ops || optlen <= 1)
 				goto err_clear;
 			strncpy(optval, icsk->icsk_ca_ops->name, optlen);
 			optval[optlen - 1] = 0;
-		} else {
+			break;
+		case TCP_SAVED_SYN:
+			tp = tcp_sk(sk);
+
+			if (optlen <= 0 || !tp->saved_syn ||
+			    optlen > tp->saved_syn[0])
+				goto err_clear;
+			memcpy(optval, tp->saved_syn + 1, optlen);
+			break;
+		default:
 			goto err_clear;
 		}
 	} else if (level == SOL_IP) {
Signature · 1 line
2.17.1
#2 lore raw

PATCH v3 2/2 new sample bpf prog

In reply to Nikita V. Shirokov

sample program which shows TCP_SAVE_SYN/TCP_SAVED_SYN usage example:
bpf's program which is doing TOS/TCLASS reflection (server would reply with a same TOS/TCLASS as client)

---
 samples/bpf/Makefile               |  1 +
 samples/bpf/tcp_tos_reflect_kern.c | 87 ++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
 create mode 100644 samples/bpf/tcp_tos_reflect_kern.c
samples/bpf/Makefile +1 -0
index 36f9f41d094b..be0a961450bc 100644
@@ -153,6 +153,7 @@ always += tcp_cong_kern.o
 always += tcp_iw_kern.o
 always += tcp_clamp_kern.o
 always += tcp_basertt_kern.o
+always += tcp_tos_reflect_kern.o
 always += xdp_redirect_kern.o
 always += xdp_redirect_map_kern.o
 always += xdp_redirect_cpu_kern.o
samples/bpf/tcp_tos_reflect_kern.c new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw
In reply to Nikita V. Shirokov
Quoted 6 lines · Nikita V. Shirokov

adding supprot for two new bpf's tcp sockopts:
TCP_SAVE_SYN (set) and TCP_SAVED_SYN (get)
this would allow for tcp-bpf program to build some logic based on fields from ingress syn packet (e.g. doing tcp's tos/tclass reflection (see sample prog)) and do it transparently from userspace program point of view

Applied, Thanks
but please convert the sample code into selftest.