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 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
14 extern unsigned long CONFIG_HZ __kconfig;
16 const volatile char veth[IFNAMSIZ];
17 const volatile int veth_ifindex;
24 int nr_socket_post_create;
36 static const char not_exist_cc[] = "not_exist";
37 static const char cubic_cc[] = "cubic";
38 static const char reno_cc[] = "reno";
40 static const struct sockopt_test sol_socket_tests[] = {
41 { .opt = SO_REUSEADDR, .flip = 1, },
42 { .opt = SO_SNDBUF, .new = 8123, .expected = 8123 * 2, },
43 { .opt = SO_RCVBUF, .new = 8123, .expected = 8123 * 2, },
44 { .opt = SO_KEEPALIVE, .flip = 1, },
45 { .opt = SO_PRIORITY, .new = 0xeb9f, .expected = 0xeb9f, },
46 { .opt = SO_REUSEPORT, .flip = 1, },
47 { .opt = SO_RCVLOWAT, .new = 8123, .expected = 8123, },
48 { .opt = SO_MARK, .new = 0xeb9f, .expected = 0xeb9f, },
49 { .opt = SO_MAX_PACING_RATE, .new = 0xeb9f, .expected = 0xeb9f, },
50 { .opt = SO_TXREHASH, .flip = 1, },
54 static const struct sockopt_test sol_tcp_tests[] = {
55 { .opt = TCP_NODELAY, .flip = 1, },
56 { .opt = TCP_KEEPIDLE, .new = 123, .expected = 123, .restore = 321, },
57 { .opt = TCP_KEEPINTVL, .new = 123, .expected = 123, .restore = 321, },
58 { .opt = TCP_KEEPCNT, .new = 123, .expected = 123, .restore = 124, },
59 { .opt = TCP_SYNCNT, .new = 123, .expected = 123, .restore = 124, },
60 { .opt = TCP_WINDOW_CLAMP, .new = 8123, .expected = 8123, .restore = 8124, },
61 { .opt = TCP_CONGESTION, },
62 { .opt = TCP_THIN_LINEAR_TIMEOUTS, .flip = 1, },
63 { .opt = TCP_USER_TIMEOUT, .new = 123400, .expected = 123400, },
64 { .opt = TCP_NOTSENT_LOWAT, .new = 1314, .expected = 1314, },
68 static const struct sockopt_test sol_ip_tests[] = {
69 { .opt = IP_TOS, .new = 0xe1, .expected = 0xe1, .tcp_expected = 0xe0, },
73 static const struct sockopt_test sol_ipv6_tests[] = {
74 { .opt = IPV6_TCLASS, .new = 0xe1, .expected = 0xe1, .tcp_expected = 0xe0, },
75 { .opt = IPV6_AUTOFLOWLABEL, .flip = 1, },
84 static int bpf_test_sockopt_flip(void *ctx, struct sock *sk,
85 const struct sockopt_test *t,
88 int old, tmp, new, opt = t->opt;
92 if (bpf_getsockopt(ctx, level, opt, &old, sizeof(old)))
94 /* kernel initialized txrehash to 255 */
95 if (level == SOL_SOCKET && opt == SO_TXREHASH && old != 0 && old != 1)
99 if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new)))
101 if (bpf_getsockopt(ctx, level, opt, &tmp, sizeof(tmp)) ||
105 if (bpf_setsockopt(ctx, level, opt, &old, sizeof(old)))
111 static int bpf_test_sockopt_int(void *ctx, struct sock *sk,
112 const struct sockopt_test *t,
115 int old, tmp, new, expected, opt;
119 if (sk->sk_type == SOCK_STREAM && t->tcp_expected)
120 expected = t->tcp_expected;
122 expected = t->expected;
124 if (bpf_getsockopt(ctx, level, opt, &old, sizeof(old)) ||
128 if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new)))
130 if (bpf_getsockopt(ctx, level, opt, &tmp, sizeof(tmp)) ||
136 if (bpf_setsockopt(ctx, level, opt, &old, sizeof(old)))
142 static int bpf_test_socket_sockopt(__u32 i, struct loop_ctx *lc)
144 const struct sockopt_test *t;
146 if (i >= ARRAY_SIZE(sol_socket_tests))
149 t = &sol_socket_tests[i];
154 return bpf_test_sockopt_flip(lc->ctx, lc->sk, t, SOL_SOCKET);
156 return bpf_test_sockopt_int(lc->ctx, lc->sk, t, SOL_SOCKET);
159 static int bpf_test_ip_sockopt(__u32 i, struct loop_ctx *lc)
161 const struct sockopt_test *t;
163 if (i >= ARRAY_SIZE(sol_ip_tests))
166 t = &sol_ip_tests[i];
171 return bpf_test_sockopt_flip(lc->ctx, lc->sk, t, IPPROTO_IP);
173 return bpf_test_sockopt_int(lc->ctx, lc->sk, t, IPPROTO_IP);
176 static int bpf_test_ipv6_sockopt(__u32 i, struct loop_ctx *lc)
178 const struct sockopt_test *t;
180 if (i >= ARRAY_SIZE(sol_ipv6_tests))
183 t = &sol_ipv6_tests[i];
188 return bpf_test_sockopt_flip(lc->ctx, lc->sk, t, IPPROTO_IPV6);
190 return bpf_test_sockopt_int(lc->ctx, lc->sk, t, IPPROTO_IPV6);
193 static int bpf_test_tcp_sockopt(__u32 i, struct loop_ctx *lc)
195 const struct sockopt_test *t;
199 if (i >= ARRAY_SIZE(sol_tcp_tests))
202 t = &sol_tcp_tests[i];
209 if (t->opt == TCP_CONGESTION) {
210 char old_cc[16], tmp_cc[16];
214 if (!bpf_setsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION,
215 (void *)not_exist_cc, sizeof(not_exist_cc)))
217 if (bpf_getsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, old_cc, sizeof(old_cc)))
219 if (!bpf_strncmp(old_cc, sizeof(old_cc), cubic_cc)) {
221 new_cc_len = sizeof(reno_cc);
224 new_cc_len = sizeof(cubic_cc);
226 if (bpf_setsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, (void *)new_cc,
229 if (bpf_getsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, tmp_cc, sizeof(tmp_cc)))
231 if (bpf_strncmp(tmp_cc, sizeof(tmp_cc), new_cc))
233 if (bpf_setsockopt(ctx, IPPROTO_TCP, TCP_CONGESTION, old_cc, sizeof(old_cc)))
239 return bpf_test_sockopt_flip(ctx, sk, t, IPPROTO_TCP);
241 return bpf_test_sockopt_int(ctx, sk, t, IPPROTO_TCP);
244 static int bpf_test_sockopt(void *ctx, struct sock *sk)
246 struct loop_ctx lc = { .ctx = ctx, .sk = sk, };
250 family = sk->sk_family;
251 proto = sk->sk_protocol;
253 n = bpf_loop(ARRAY_SIZE(sol_socket_tests), bpf_test_socket_sockopt, &lc, 0);
254 if (n != ARRAY_SIZE(sol_socket_tests))
257 if (proto == IPPROTO_TCP) {
258 n = bpf_loop(ARRAY_SIZE(sol_tcp_tests), bpf_test_tcp_sockopt, &lc, 0);
259 if (n != ARRAY_SIZE(sol_tcp_tests))
263 if (family == AF_INET) {
264 n = bpf_loop(ARRAY_SIZE(sol_ip_tests), bpf_test_ip_sockopt, &lc, 0);
265 if (n != ARRAY_SIZE(sol_ip_tests))
268 n = bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0);
269 if (n != ARRAY_SIZE(sol_ipv6_tests))
276 static int binddev_test(void *ctx)
278 const char empty_ifname[] = "";
279 int ifindex, zero = 0;
281 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
282 (void *)veth, sizeof(veth)))
284 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
285 &ifindex, sizeof(int)) ||
286 ifindex != veth_ifindex)
289 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
290 (void *)empty_ifname, sizeof(empty_ifname)))
292 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
293 &ifindex, sizeof(int)) ||
297 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
298 (void *)&veth_ifindex, sizeof(int)))
300 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
301 &ifindex, sizeof(int)) ||
302 ifindex != veth_ifindex)
305 if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
308 if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
309 &ifindex, sizeof(int)) ||
316 static int test_tcp_maxseg(void *ctx, struct sock *sk)
320 if (sk->sk_state != TCP_ESTABLISHED)
321 return bpf_setsockopt(ctx, IPPROTO_TCP, TCP_MAXSEG,
324 if (bpf_getsockopt(ctx, IPPROTO_TCP, TCP_MAXSEG, &tmp, sizeof(tmp)) ||
331 static int test_tcp_saved_syn(void *ctx, struct sock *sk)
336 if (sk->sk_state == TCP_LISTEN)
337 return bpf_setsockopt(ctx, IPPROTO_TCP, TCP_SAVE_SYN,
340 return bpf_getsockopt(ctx, IPPROTO_TCP, TCP_SAVED_SYN,
341 saved_syn, sizeof(saved_syn));
344 SEC("lsm_cgroup/socket_post_create")
345 int BPF_PROG(socket_post_create, struct socket *sock, int family,
346 int type, int protocol, int kern)
348 struct sock *sk = sock->sk;
353 nr_socket_post_create += !bpf_test_sockopt(sk, sk);
354 nr_binddev += !binddev_test(sk);
360 int skops_sockopt(struct bpf_sock_ops *skops)
362 struct bpf_sock *bpf_sk = skops->sk;
368 sk = (struct sock *)bpf_skc_to_tcp_sock(bpf_sk);
373 case BPF_SOCK_OPS_TCP_LISTEN_CB:
374 nr_listen += !(bpf_test_sockopt(skops, sk) ||
375 test_tcp_maxseg(skops, sk) ||
376 test_tcp_saved_syn(skops, sk));
378 case BPF_SOCK_OPS_TCP_CONNECT_CB:
379 nr_connect += !(bpf_test_sockopt(skops, sk) ||
380 test_tcp_maxseg(skops, sk));
382 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
383 nr_active += !(bpf_test_sockopt(skops, sk) ||
384 test_tcp_maxseg(skops, sk));
386 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
387 nr_passive += !(bpf_test_sockopt(skops, sk) ||
388 test_tcp_maxseg(skops, sk) ||
389 test_tcp_saved_syn(skops, sk));
390 bpf_sock_ops_cb_flags_set(skops,
391 skops->bpf_sock_ops_cb_flags |
392 BPF_SOCK_OPS_STATE_CB_FLAG);
394 case BPF_SOCK_OPS_STATE_CB:
395 if (skops->args[1] == BPF_TCP_CLOSE_WAIT)
396 nr_fin_wait1 += !bpf_test_sockopt(skops, sk);
403 char _license[] SEC("license") = "GPL";