1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <test_progs.h> 4 #include "netns_cookie_prog.skel.h" 5 #include "network_helpers.h" 6 7 #ifndef SO_NETNS_COOKIE 8 #define SO_NETNS_COOKIE 71 9 #endif 10 11 #define loopback 1 12 13 static int duration; 14 15 void test_netns_cookie(void) 16 { 17 LIBBPF_OPTS(bpf_prog_attach_opts, opta); 18 LIBBPF_OPTS(bpf_prog_detach_opts, optd); 19 int server_fd = -1, client_fd = -1, cgroup_fd = -1; 20 int err, val, ret, map, verdict, tc_fd; 21 struct netns_cookie_prog *skel; 22 uint64_t cookie_expected_value; 23 socklen_t vallen = sizeof(cookie_expected_value); 24 static const char send_msg[] = "message"; 25 26 skel = netns_cookie_prog__open_and_load(); 27 if (!ASSERT_OK_PTR(skel, "skel_open")) 28 return; 29 30 cgroup_fd = test__join_cgroup("/netns_cookie"); 31 if (CHECK(cgroup_fd < 0, "join_cgroup", "cgroup creation failed\n")) 32 goto done; 33 34 skel->links.get_netns_cookie_sockops = bpf_program__attach_cgroup( 35 skel->progs.get_netns_cookie_sockops, cgroup_fd); 36 if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_sockops, "prog_attach")) 37 goto done; 38 39 verdict = bpf_program__fd(skel->progs.get_netns_cookie_sk_msg); 40 map = bpf_map__fd(skel->maps.sock_map); 41 err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0); 42 if (!ASSERT_OK(err, "prog_attach")) 43 goto done; 44 45 tc_fd = bpf_program__fd(skel->progs.get_netns_cookie_tcx); 46 err = bpf_prog_attach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &opta); 47 if (!ASSERT_OK(err, "prog_attach")) 48 goto done; 49 50 server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0); 51 if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno)) 52 goto cleanup_tc; 53 54 client_fd = connect_to_fd(server_fd, 0); 55 if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno)) 56 goto cleanup_tc; 57 58 ret = send(client_fd, send_msg, sizeof(send_msg), 0); 59 if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", ret)) 60 goto cleanup_tc; 61 62 err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sockops_netns_cookies), 63 &client_fd, &val); 64 if (!ASSERT_OK(err, "map_lookup(sockops_netns_cookies)")) 65 goto cleanup_tc; 66 67 err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE, 68 &cookie_expected_value, &vallen); 69 if (!ASSERT_OK(err, "getsockopt")) 70 goto cleanup_tc; 71 72 ASSERT_EQ(val, cookie_expected_value, "cookie_value"); 73 74 err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies), 75 &client_fd, &val); 76 if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)")) 77 goto cleanup_tc; 78 79 ASSERT_EQ(val, cookie_expected_value, "cookie_value"); 80 ASSERT_EQ(skel->bss->tcx_init_netns_cookie, cookie_expected_value, "cookie_value"); 81 ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value"); 82 83 cleanup_tc: 84 err = bpf_prog_detach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &optd); 85 ASSERT_OK(err, "prog_detach"); 86 87 done: 88 if (server_fd != -1) 89 close(server_fd); 90 if (client_fd != -1) 91 close(client_fd); 92 if (cgroup_fd != -1) 93 close(cgroup_fd); 94 netns_cookie_prog__destroy(skel); 95 } 96