1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
5 #include "bpf_tracing_net.h"
6 #include <bpf/bpf_core_read.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_tracing.h>
11 extern unsigned long CONFIG_HZ __kconfig;
13 const volatile char veth[IFNAMSIZ];
14 const volatile int veth_ifindex;
21 int nr_socket_post_create;
33 static const char not_exist_cc[] = "not_exist";
34 static const char cubic_cc[] = "cubic";
35 static const char reno_cc[] = "reno";
37 static const struct sockopt_test sol_socket_tests[] = {
38 { .opt = SO_REUSEADDR, .flip = 1, },
39 { .opt = SO_SNDBUF, .new = 8123, .expected = 8123 * 2, },
40 { .opt = SO_RCVBUF, .new = 8123, .expected = 8123 * 2, },
41 { .opt = SO_KEEPALIVE, .flip = 1, },
42 { .opt = SO_PRIORITY, .new = 0xeb9f, .expected = 0xeb9f, },
43 { .opt = SO_REUSEPORT, .flip = 1, },
44 { .opt = SO_RCVLOWAT, .new = 8123, .expected = 8123, },
45 { .opt = SO_MARK, .new = 0xeb9f, .expected = 0xeb9f, },
46 { .opt = SO_MAX_PACING_RATE, .new = 0xeb9f, .expected = 0xeb9f, },
47 { .opt = SO_TXREHASH, .flip = 1, },
51 static const struct sockopt_test sol_tcp_tests[] = {
52 { .opt = TCP_NODELAY, .flip = 1, },
53 { .opt = TCP_KEEPIDLE, .new = 123, .expected = 123, .restore = 321, },
54 { .opt = TCP_KEEPINTVL, .new = 123, .expected = 123, .restore = 321, },
55 { .opt = TCP_KEEPCNT, .new = 123, .expected = 123, .restore = 124, },
56 { .opt = TCP_SYNCNT, .new = 123, .expected = 123, .restore = 124, },
57 { .opt = TCP_WINDOW_CLAMP, .new = 8123, .expected = 8123, .restore = 8124, },
58 { .opt = TCP_CONGESTION, },
59 { .opt = TCP_THIN_LINEAR_TIMEOUTS, .flip = 1, },
60 { .opt = TCP_USER_TIMEOUT, .new = 123400, .expected = 123400, },
61 { .opt = TCP_NOTSENT_LOWAT, .new = 1314, .expected = 1314, },
62 { .opt = TCP_BPF_SOCK_OPS_CB_FLAGS, .new = BPF_SOCK_OPS_ALL_CB_FLAGS,
63 .expected = BPF_SOCK_OPS_ALL_CB_FLAGS, },
67 static const struct sockopt_test sol_ip_tests[] = {
68 { .opt = IP_TOS, .new = 0xe1, .expected = 0xe1, .tcp_expected = 0xe0, },
72 static const struct sockopt_test sol_ipv6_tests[] = {
73 { .opt = IPV6_TCLASS, .new = 0xe1, .expected = 0xe1, .tcp_expected = 0xe0, },
74 { .opt = IPV6_AUTOFLOWLABEL, .flip = 1, },
83 static int bpf_test_sockopt_flip(void *ctx, struct sock *sk,
84 const struct sockopt_test *t,
87 int old, tmp, new, opt = t->opt;
91 if (bpf_getsockopt(ctx, level, opt, &old, sizeof(old)))
93 /* kernel initialized txrehash to 255 */
94 if (level == SOL_SOCKET && opt == SO_TXREHASH && old != 0 && old != 1)
98 if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new)))
100 if (bpf_getsockopt(ctx, level, opt, &tmp, sizeof(tmp)) ||
104 if (bpf_setsockopt(ctx, level, opt, &old, sizeof(old)))
110 static int bpf_test_sockopt_int(void *ctx, struct sock *sk,
111 const struct sockopt_test *t,
114 int old, tmp, new, expected, opt;
118 if (sk->sk_type == SOCK_STREAM && t->tcp_expected)
119 expected = t->tcp_expected;
121 expected = t->expected;
123 if (bpf_getsockopt(ctx, level, opt, &old, sizeof(old)) ||
127 if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new)))
129 if (bpf_getsockopt(ctx, level, opt, &tmp, sizeof(tmp)) ||
135 if (bpf_setsockopt(ctx, level, opt, &old, sizeof(old)))
141 static int bpf_test_socket_sockopt(__u32 i, struct loop_ctx *lc)
143 const struct sockopt_test *t;
145 if (i >= ARRAY_SIZE(sol_socket_tests))
148 t = &sol_socket_tests[i];
153 return bpf_test_sockopt_flip(lc->ctx, lc->sk, t, SOL_SOCKET);
155 return bpf_test_sockopt_int(lc->ctx, lc->sk, t, SOL_SOCKET);
158 static int bpf_test_ip_sockopt(__u32 i, struct loop_ctx *lc)
160 const struct sockopt_test *t;
162 if (i >= ARRAY_SIZE(sol_ip_tests))
165 t = &sol_ip_tests[i];
170 return bpf_test_sockopt_flip(lc->ctx, lc->sk, t, IPPROTO_IP);
172 return bpf_test_sockopt_int(lc->ctx, lc->sk, t, IPPROTO_IP);
175 static int bpf_test_ipv6_sockopt(__u32 i, struct loop_ctx *lc)
177 const struct sockopt_test *t;
179 if (i >= ARRAY_SIZE(sol_ipv6_tests))
182 t = &sol_ipv6_tests[i];
187 return bpf_test_sockopt_flip(lc->ctx, lc->sk, t, IPPROTO_IPV6);
189 return bpf_test_sockopt_int(lc->ctx, lc->sk, t, IPPROTO_IPV6);
192 static int bpf_test_tcp_sockopt(__u32 i, struct loop_ctx *lc)
194 const struct sockopt_test *t;
198 if (i >= ARRAY_SIZE(sol_tcp_tests))
201 t = &sol_tcp_tests[i];
208 if (t->opt == TCP_CONGESTION) {
209 char old_cc[16], tmp_cc[16];
213 if (!bpf_setsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION,
214 (void *)not_exist_cc, sizeof(not_exist_cc)))
216 if (bpf_getsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, old_cc, sizeof(old_cc)))
218 if (!bpf_strncmp(old_cc, sizeof(old_cc), cubic_cc)) {
220 new_cc_len = sizeof(reno_cc);
223 new_cc_len = sizeof(cubic_cc);
225 if (bpf_setsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, (void *)new_cc,
228 if (bpf_getsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, tmp_cc, sizeof(tmp_cc)))
230 if (bpf_strncmp(tmp_cc, sizeof(tmp_cc), new_cc))
232 if (bpf_setsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, old_cc, sizeof(old_cc)))
238 return bpf_test_sockopt_flip(ctx, sk, t, IPPROTO_TCP);
240 return bpf_test_sockopt_int(ctx, sk, t, IPPROTO_TCP);
243 static int bpf_test_sockopt(void *ctx, struct sock *sk)
245 struct loop_ctx lc = { .ctx = ctx, .sk = sk, };
249 family = sk->sk_family;
250 proto = sk->sk_protocol;
252 n = bpf_loop(ARRAY_SIZE(sol_socket_tests), bpf_test_socket_sockopt, &lc, 0);
253 if (n != ARRAY_SIZE(sol_socket_tests))
256 if (proto == IPPROTO_TCP) {
257 n = bpf_loop(ARRAY_SIZE(sol_tcp_tests), bpf_test_tcp_sockopt, &lc, 0);
258 if (n != ARRAY_SIZE(sol_tcp_tests))
262 if (family == AF_INET) {
263 n = bpf_loop(ARRAY_SIZE(sol_ip_tests), bpf_test_ip_sockopt, &lc, 0);
264 if (n != ARRAY_SIZE(sol_ip_tests))
267 n = bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0);
268 if (n != ARRAY_SIZE(sol_ipv6_tests))
275 static int binddev_test(void *ctx)
277 const char empty_ifname[] = "";
278 int ifindex, zero = 0;
280 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
281 (void *)veth, sizeof(veth)))
283 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
284 &ifindex, sizeof(int)) ||
285 ifindex != veth_ifindex)
288 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
289 (void *)empty_ifname, sizeof(empty_ifname)))
291 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
292 &ifindex, sizeof(int)) ||
296 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
297 (void *)&veth_ifindex, sizeof(int)))
299 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
300 &ifindex, sizeof(int)) ||
301 ifindex != veth_ifindex)
304 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
307 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
308 &ifindex, sizeof(int)) ||
315 static int test_tcp_maxseg(void *ctx, struct sock *sk)
319 if (sk->sk_state != TCP_ESTABLISHED)
320 return bpf_setsockopt(ctx, IPPROTO_TCP, TCP_MAXSEG,
323 if (bpf_getsockopt(ctx, IPPROTO_TCP, TCP_MAXSEG, &tmp, sizeof(tmp)) ||
330 static int test_tcp_saved_syn(void *ctx, struct sock *sk)
335 if (sk->sk_state == TCP_LISTEN)
336 return bpf_setsockopt(ctx, IPPROTO_TCP, TCP_SAVE_SYN,
339 return bpf_getsockopt(ctx, IPPROTO_TCP, TCP_SAVED_SYN,
340 saved_syn, sizeof(saved_syn));
343 SEC("lsm_cgroup/socket_post_create")
344 int BPF_PROG(socket_post_create, struct socket *sock, int family,
345 int type, int protocol, int kern)
347 struct sock *sk = sock->sk;
352 nr_socket_post_create += !bpf_test_sockopt(sk, sk);
353 nr_binddev += !binddev_test(sk);
358 SEC("cgroup/getsockopt")
359 int _getsockopt(struct bpf_sockopt *ctx)
361 struct bpf_sock *sk = ctx->sk;
362 int *optval = ctx->optval;
365 if (!sk || ctx->level != SOL_TCP || ctx->optname != TCP_BPF_SOCK_OPS_CB_FLAGS)
368 tp = bpf_core_cast(sk, struct tcp_sock);
369 if (ctx->optval + sizeof(int) <= ctx->optval_end) {
370 *optval = tp->bpf_sock_ops_cb_flags;
377 int skops_sockopt(struct bpf_sock_ops *skops)
379 struct bpf_sock *bpf_sk = skops->sk;
386 sk = (struct sock *)bpf_skc_to_tcp_sock(bpf_sk);
391 case BPF_SOCK_OPS_TCP_LISTEN_CB:
392 nr_listen += !(bpf_test_sockopt(skops, sk) ||
393 test_tcp_maxseg(skops, sk) ||
394 test_tcp_saved_syn(skops, sk));
396 case BPF_SOCK_OPS_TCP_CONNECT_CB:
397 nr_connect += !(bpf_test_sockopt(skops, sk) ||
398 test_tcp_maxseg(skops, sk));
400 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
401 nr_active += !(bpf_test_sockopt(skops, sk) ||
402 test_tcp_maxseg(skops, sk));
404 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
405 nr_passive += !(bpf_test_sockopt(skops, sk) ||
406 test_tcp_maxseg(skops, sk) ||
407 test_tcp_saved_syn(skops, sk));
408 flags = skops->bpf_sock_ops_cb_flags | BPF_SOCK_OPS_STATE_CB_FLAG;
409 bpf_setsockopt(skops, SOL_TCP, TCP_BPF_SOCK_OPS_CB_FLAGS, &flags, sizeof(flags));
411 case BPF_SOCK_OPS_STATE_CB:
412 if (skops->args[1] == BPF_TCP_CLOSE_WAIT)
413 nr_fin_wait1 += !bpf_test_sockopt(skops, sk);
420 char _license[] SEC("license") = "GPL";