1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
8 #include <sys/socket.h>
9 #include <linux/compiler.h>
11 #include "test_progs.h"
12 #include "cgroup_helpers.h"
13 #include "network_helpers.h"
14 #include "test_tcp_hdr_options.h"
15 #include "test_tcp_hdr_options.skel.h"
16 #include "test_misc_tcp_hdr_options.skel.h"
18 #define LO_ADDR6 "::1"
19 #define CG_NAME "/tcpbpf-hdr-opt-test"
21 static struct bpf_test_option exp_passive_estab_in;
22 static struct bpf_test_option exp_active_estab_in;
23 static struct bpf_test_option exp_passive_fin_in;
24 static struct bpf_test_option exp_active_fin_in;
25 static struct hdr_stg exp_passive_hdr_stg;
26 static struct hdr_stg exp_active_hdr_stg = { .active = true, };
28 static struct test_misc_tcp_hdr_options *misc_skel;
29 static struct test_tcp_hdr_options *skel;
30 static int lport_linum_map_fd;
31 static int hdr_stg_map_fd;
32 static __u32 duration;
43 static int create_netns(void)
45 if (CHECK(unshare(CLONE_NEWNET), "create netns",
46 "unshare(CLONE_NEWNET): %s (%d)",
47 strerror(errno), errno))
50 if (CHECK(system("ip link set dev lo up"), "run ip cmd",
51 "failed to bring lo link up\n"))
57 static int write_sysctl(const char *sysctl, const char *value)
61 fd = open(sysctl, O_WRONLY);
62 if (CHECK(fd == -1, "open sysctl", "open(%s): %s (%d)\n",
63 sysctl, strerror(errno), errno))
67 err = write(fd, value, len);
69 if (CHECK(err != len, "write sysctl",
70 "write(%s, %s): err:%d %s (%d)\n",
71 sysctl, value, err, strerror(errno), errno))
77 static void print_hdr_stg(const struct hdr_stg *hdr_stg, const char *prefix)
79 fprintf(stderr, "%s{active:%u, resend_syn:%u, syncookie:%u, fastopen:%u}\n",
80 prefix ? : "", hdr_stg->active, hdr_stg->resend_syn,
81 hdr_stg->syncookie, hdr_stg->fastopen);
84 static void print_option(const struct bpf_test_option *opt, const char *prefix)
86 fprintf(stderr, "%s{flags:0x%x, max_delack_ms:%u, rand:0x%x}\n",
87 prefix ? : "", opt->flags, opt->max_delack_ms, opt->rand);
90 static void sk_fds_close(struct sk_fds *sk_fds)
92 close(sk_fds->srv_fd);
93 close(sk_fds->passive_fd);
94 close(sk_fds->active_fd);
97 static int sk_fds_shutdown(struct sk_fds *sk_fds)
101 shutdown(sk_fds->active_fd, SHUT_WR);
102 ret = read(sk_fds->passive_fd, &abyte, sizeof(abyte));
103 if (CHECK(ret != 0, "read-after-shutdown(passive_fd):",
105 ret, strerror(errno), errno))
108 shutdown(sk_fds->passive_fd, SHUT_WR);
109 ret = read(sk_fds->active_fd, &abyte, sizeof(abyte));
110 if (CHECK(ret != 0, "read-after-shutdown(active_fd):",
112 ret, strerror(errno), errno))
118 static int sk_fds_connect(struct sk_fds *sk_fds, bool fast_open)
120 const char fast[] = "FAST!!!";
121 struct sockaddr_in6 addr6;
124 sk_fds->srv_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
125 if (CHECK(sk_fds->srv_fd == -1, "start_server", "%s (%d)\n",
126 strerror(errno), errno))
130 sk_fds->active_fd = fastopen_connect(sk_fds->srv_fd, fast,
133 sk_fds->active_fd = connect_to_fd(sk_fds->srv_fd, 0);
135 if (CHECK_FAIL(sk_fds->active_fd == -1)) {
136 close(sk_fds->srv_fd);
141 if (CHECK(getsockname(sk_fds->srv_fd, (struct sockaddr *)&addr6,
142 &len), "getsockname(srv_fd)", "%s (%d)\n",
143 strerror(errno), errno))
145 sk_fds->passive_lport = ntohs(addr6.sin6_port);
148 if (CHECK(getsockname(sk_fds->active_fd, (struct sockaddr *)&addr6,
149 &len), "getsockname(active_fd)", "%s (%d)\n",
150 strerror(errno), errno))
152 sk_fds->active_lport = ntohs(addr6.sin6_port);
154 sk_fds->passive_fd = accept(sk_fds->srv_fd, NULL, 0);
155 if (CHECK(sk_fds->passive_fd == -1, "accept(srv_fd)", "%s (%d)\n",
156 strerror(errno), errno))
160 char bytes_in[sizeof(fast)];
163 ret = read(sk_fds->passive_fd, bytes_in, sizeof(bytes_in));
164 if (CHECK(ret != sizeof(fast), "read fastopen syn data",
165 "expected=%lu actual=%d\n", sizeof(fast), ret)) {
166 close(sk_fds->passive_fd);
174 close(sk_fds->active_fd);
175 close(sk_fds->srv_fd);
178 memset(sk_fds, -1, sizeof(*sk_fds));
182 static int check_hdr_opt(const struct bpf_test_option *exp,
183 const struct bpf_test_option *act,
184 const char *hdr_desc)
186 if (CHECK(memcmp(exp, act, sizeof(*exp)),
187 "expected-vs-actual", "unexpected %s\n", hdr_desc)) {
188 print_option(exp, "expected: ");
189 print_option(act, " actual: ");
196 static int check_hdr_stg(const struct hdr_stg *exp, int fd,
197 const char *stg_desc)
201 if (CHECK(bpf_map_lookup_elem(hdr_stg_map_fd, &fd, &act),
202 "map_lookup(hdr_stg_map_fd)", "%s %s (%d)\n",
203 stg_desc, strerror(errno), errno))
206 if (CHECK(memcmp(exp, &act, sizeof(*exp)),
207 "expected-vs-actual", "unexpected %s\n", stg_desc)) {
208 print_hdr_stg(exp, "expected: ");
209 print_hdr_stg(&act, " actual: ");
216 static int check_error_linum(const struct sk_fds *sk_fds)
218 unsigned int nr_errors = 0;
219 struct linum_err linum_err;
222 lport = sk_fds->passive_lport;
223 if (!bpf_map_lookup_elem(lport_linum_map_fd, &lport, &linum_err)) {
225 "bpf prog error out at lport:passive(%d), linum:%u err:%d\n",
226 lport, linum_err.linum, linum_err.err);
230 lport = sk_fds->active_lport;
231 if (!bpf_map_lookup_elem(lport_linum_map_fd, &lport, &linum_err)) {
233 "bpf prog error out at lport:active(%d), linum:%u err:%d\n",
234 lport, linum_err.linum, linum_err.err);
241 static void check_hdr_and_close_fds(struct sk_fds *sk_fds)
243 const __u32 expected_inherit_cb_flags =
244 BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG |
245 BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG |
246 BPF_SOCK_OPS_STATE_CB_FLAG;
248 if (sk_fds_shutdown(sk_fds))
251 if (CHECK(expected_inherit_cb_flags != skel->bss->inherit_cb_flags,
252 "Unexpected inherit_cb_flags", "0x%x != 0x%x\n",
253 skel->bss->inherit_cb_flags, expected_inherit_cb_flags))
256 if (check_hdr_stg(&exp_passive_hdr_stg, sk_fds->passive_fd,
260 if (check_hdr_stg(&exp_active_hdr_stg, sk_fds->active_fd,
264 if (check_hdr_opt(&exp_passive_estab_in, &skel->bss->passive_estab_in,
268 if (check_hdr_opt(&exp_active_estab_in, &skel->bss->active_estab_in,
272 if (check_hdr_opt(&exp_passive_fin_in, &skel->bss->passive_fin_in,
276 check_hdr_opt(&exp_active_fin_in, &skel->bss->active_fin_in,
280 CHECK_FAIL(check_error_linum(sk_fds));
281 sk_fds_close(sk_fds);
284 static void prepare_out(void)
286 skel->bss->active_syn_out = exp_passive_estab_in;
287 skel->bss->passive_synack_out = exp_active_estab_in;
289 skel->bss->active_fin_out = exp_passive_fin_in;
290 skel->bss->passive_fin_out = exp_active_fin_in;
293 static void reset_test(void)
295 size_t optsize = sizeof(struct bpf_test_option);
298 memset(&skel->bss->passive_synack_out, 0, optsize);
299 memset(&skel->bss->passive_fin_out, 0, optsize);
301 memset(&skel->bss->passive_estab_in, 0, optsize);
302 memset(&skel->bss->passive_fin_in, 0, optsize);
304 memset(&skel->bss->active_syn_out, 0, optsize);
305 memset(&skel->bss->active_fin_out, 0, optsize);
307 memset(&skel->bss->active_estab_in, 0, optsize);
308 memset(&skel->bss->active_fin_in, 0, optsize);
310 skel->bss->inherit_cb_flags = 0;
312 skel->data->test_kind = TCPOPT_EXP;
313 skel->data->test_magic = 0xeB9F;
315 memset(&exp_passive_estab_in, 0, optsize);
316 memset(&exp_active_estab_in, 0, optsize);
317 memset(&exp_passive_fin_in, 0, optsize);
318 memset(&exp_active_fin_in, 0, optsize);
320 memset(&exp_passive_hdr_stg, 0, sizeof(exp_passive_hdr_stg));
321 memset(&exp_active_hdr_stg, 0, sizeof(exp_active_hdr_stg));
322 exp_active_hdr_stg.active = true;
324 err = bpf_map_get_next_key(lport_linum_map_fd, NULL, &lport);
326 bpf_map_delete_elem(lport_linum_map_fd, &lport);
327 err = bpf_map_get_next_key(lport_linum_map_fd, &lport, &lport);
331 static void fastopen_estab(void)
333 struct bpf_link *link;
334 struct sk_fds sk_fds;
336 hdr_stg_map_fd = bpf_map__fd(skel->maps.hdr_stg_map);
337 lport_linum_map_fd = bpf_map__fd(skel->maps.lport_linum_map);
339 exp_passive_estab_in.flags = OPTION_F_RAND | OPTION_F_MAX_DELACK_MS;
340 exp_passive_estab_in.rand = 0xfa;
341 exp_passive_estab_in.max_delack_ms = 11;
343 exp_active_estab_in.flags = OPTION_F_RAND | OPTION_F_MAX_DELACK_MS;
344 exp_active_estab_in.rand = 0xce;
345 exp_active_estab_in.max_delack_ms = 22;
347 exp_passive_hdr_stg.fastopen = true;
351 /* Allow fastopen without fastopen cookie */
352 if (write_sysctl("/proc/sys/net/ipv4/tcp_fastopen", "1543"))
355 link = bpf_program__attach_cgroup(skel->progs.estab, cg_fd);
356 if (!ASSERT_OK_PTR(link, "attach_cgroup(estab)"))
359 if (sk_fds_connect(&sk_fds, true)) {
360 bpf_link__destroy(link);
364 check_hdr_and_close_fds(&sk_fds);
365 bpf_link__destroy(link);
368 static void syncookie_estab(void)
370 struct bpf_link *link;
371 struct sk_fds sk_fds;
373 hdr_stg_map_fd = bpf_map__fd(skel->maps.hdr_stg_map);
374 lport_linum_map_fd = bpf_map__fd(skel->maps.lport_linum_map);
376 exp_passive_estab_in.flags = OPTION_F_RAND | OPTION_F_MAX_DELACK_MS;
377 exp_passive_estab_in.rand = 0xfa;
378 exp_passive_estab_in.max_delack_ms = 11;
380 exp_active_estab_in.flags = OPTION_F_RAND | OPTION_F_MAX_DELACK_MS |
382 exp_active_estab_in.rand = 0xce;
383 exp_active_estab_in.max_delack_ms = 22;
385 exp_passive_hdr_stg.syncookie = true;
386 exp_active_hdr_stg.resend_syn = true,
390 /* Clear the RESEND to ensure the bpf prog can learn
391 * want_cookie and set the RESEND by itself.
393 skel->bss->passive_synack_out.flags &= ~OPTION_F_RESEND;
395 /* Enforce syncookie mode */
396 if (write_sysctl("/proc/sys/net/ipv4/tcp_syncookies", "2"))
399 link = bpf_program__attach_cgroup(skel->progs.estab, cg_fd);
400 if (!ASSERT_OK_PTR(link, "attach_cgroup(estab)"))
403 if (sk_fds_connect(&sk_fds, false)) {
404 bpf_link__destroy(link);
408 check_hdr_and_close_fds(&sk_fds);
409 bpf_link__destroy(link);
412 static void fin(void)
414 struct bpf_link *link;
415 struct sk_fds sk_fds;
417 hdr_stg_map_fd = bpf_map__fd(skel->maps.hdr_stg_map);
418 lport_linum_map_fd = bpf_map__fd(skel->maps.lport_linum_map);
420 exp_passive_fin_in.flags = OPTION_F_RAND;
421 exp_passive_fin_in.rand = 0xfa;
423 exp_active_fin_in.flags = OPTION_F_RAND;
424 exp_active_fin_in.rand = 0xce;
428 if (write_sysctl("/proc/sys/net/ipv4/tcp_syncookies", "1"))
431 link = bpf_program__attach_cgroup(skel->progs.estab, cg_fd);
432 if (!ASSERT_OK_PTR(link, "attach_cgroup(estab)"))
435 if (sk_fds_connect(&sk_fds, false)) {
436 bpf_link__destroy(link);
440 check_hdr_and_close_fds(&sk_fds);
441 bpf_link__destroy(link);
444 static void __simple_estab(bool exprm)
446 struct bpf_link *link;
447 struct sk_fds sk_fds;
449 hdr_stg_map_fd = bpf_map__fd(skel->maps.hdr_stg_map);
450 lport_linum_map_fd = bpf_map__fd(skel->maps.lport_linum_map);
452 exp_passive_estab_in.flags = OPTION_F_RAND | OPTION_F_MAX_DELACK_MS;
453 exp_passive_estab_in.rand = 0xfa;
454 exp_passive_estab_in.max_delack_ms = 11;
456 exp_active_estab_in.flags = OPTION_F_RAND | OPTION_F_MAX_DELACK_MS;
457 exp_active_estab_in.rand = 0xce;
458 exp_active_estab_in.max_delack_ms = 22;
463 skel->data->test_kind = 0xB9;
464 skel->data->test_magic = 0;
467 if (write_sysctl("/proc/sys/net/ipv4/tcp_syncookies", "1"))
470 link = bpf_program__attach_cgroup(skel->progs.estab, cg_fd);
471 if (!ASSERT_OK_PTR(link, "attach_cgroup(estab)"))
474 if (sk_fds_connect(&sk_fds, false)) {
475 bpf_link__destroy(link);
479 check_hdr_and_close_fds(&sk_fds);
480 bpf_link__destroy(link);
483 static void no_exprm_estab(void)
485 __simple_estab(false);
488 static void simple_estab(void)
490 __simple_estab(true);
493 static void misc(void)
495 const char send_msg[] = "MISC!!!";
496 char recv_msg[sizeof(send_msg)];
497 const unsigned int nr_data = 2;
498 struct bpf_link *link;
499 struct sk_fds sk_fds;
502 lport_linum_map_fd = bpf_map__fd(misc_skel->maps.lport_linum_map);
504 if (write_sysctl("/proc/sys/net/ipv4/tcp_syncookies", "1"))
507 link = bpf_program__attach_cgroup(misc_skel->progs.misc_estab, cg_fd);
508 if (!ASSERT_OK_PTR(link, "attach_cgroup(misc_estab)"))
511 if (sk_fds_connect(&sk_fds, false)) {
512 bpf_link__destroy(link);
516 for (i = 0; i < nr_data; i++) {
517 /* MSG_EOR to ensure skb will not be combined */
518 ret = send(sk_fds.active_fd, send_msg, sizeof(send_msg),
520 if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n",
524 ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg));
525 if (CHECK(ret != sizeof(send_msg), "read(msg)", "ret:%d\n",
530 if (sk_fds_shutdown(&sk_fds))
533 CHECK(misc_skel->bss->nr_syn != 1, "unexpected nr_syn",
534 "expected (1) != actual (%u)\n",
535 misc_skel->bss->nr_syn);
537 CHECK(misc_skel->bss->nr_data != nr_data, "unexpected nr_data",
538 "expected (%u) != actual (%u)\n",
539 nr_data, misc_skel->bss->nr_data);
541 /* The last ACK may have been delayed, so it is either 1 or 2. */
542 CHECK(misc_skel->bss->nr_pure_ack != 1 &&
543 misc_skel->bss->nr_pure_ack != 2,
544 "unexpected nr_pure_ack",
545 "expected (1 or 2) != actual (%u)\n",
546 misc_skel->bss->nr_pure_ack);
548 CHECK(misc_skel->bss->nr_fin != 1, "unexpected nr_fin",
549 "expected (1) != actual (%u)\n",
550 misc_skel->bss->nr_fin);
553 CHECK_FAIL(check_error_linum(&sk_fds));
554 sk_fds_close(&sk_fds);
555 bpf_link__destroy(link);
563 #define DEF_TEST(name) { #name, name }
564 static struct test tests[] = {
565 DEF_TEST(simple_estab),
566 DEF_TEST(no_exprm_estab),
567 DEF_TEST(syncookie_estab),
568 DEF_TEST(fastopen_estab),
573 void test_tcp_hdr_options(void)
577 skel = test_tcp_hdr_options__open_and_load();
578 if (CHECK(!skel, "open and load skel", "failed"))
581 misc_skel = test_misc_tcp_hdr_options__open_and_load();
582 if (CHECK(!misc_skel, "open and load misc test skel", "failed"))
585 cg_fd = test__join_cgroup(CG_NAME);
586 if (CHECK_FAIL(cg_fd < 0))
589 for (i = 0; i < ARRAY_SIZE(tests); i++) {
590 if (!test__start_subtest(tests[i].desc))
603 test_misc_tcp_hdr_options__destroy(misc_skel);
604 test_tcp_hdr_options__destroy(skel);