1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include <network_helpers.h> 4 #include <linux/netfilter/nf_conntrack_common.h> 5 #include "test_bpf_nf.skel.h" 6 #include "test_bpf_nf_fail.skel.h" 7 8 static char log_buf[1024 * 1024]; 9 10 struct { 11 const char *prog_name; 12 const char *err_msg; 13 } test_bpf_nf_fail_tests[] = { 14 { "alloc_release", "kernel function bpf_ct_release args#0 expected pointer to STRUCT nf_conn but" }, 15 { "insert_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" }, 16 { "lookup_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" }, 17 { "set_timeout_after_insert", "kernel function bpf_ct_set_timeout args#0 expected pointer to STRUCT nf_conn___init but" }, 18 { "set_status_after_insert", "kernel function bpf_ct_set_status args#0 expected pointer to STRUCT nf_conn___init but" }, 19 { "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout args#0 expected pointer to STRUCT nf_conn but" }, 20 { "change_status_after_alloc", "kernel function bpf_ct_change_status args#0 expected pointer to STRUCT nf_conn but" }, 21 { "write_not_allowlisted_field", "no write support to nf_conn at off" }, 22 }; 23 24 enum { 25 TEST_XDP, 26 TEST_TC_BPF, 27 }; 28 29 #define TIMEOUT_MS 3000 30 #define IPS_STATUS_MASK (IPS_CONFIRMED | IPS_SEEN_REPLY | \ 31 IPS_SRC_NAT_DONE | IPS_DST_NAT_DONE | \ 32 IPS_SRC_NAT | IPS_DST_NAT) 33 34 static int connect_to_server(int srv_fd) 35 { 36 int fd = -1; 37 38 fd = socket(AF_INET, SOCK_STREAM, 0); 39 if (!ASSERT_GE(fd, 0, "socket")) 40 goto out; 41 42 if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, TIMEOUT_MS), 0, "connect_fd_to_fd")) { 43 close(fd); 44 fd = -1; 45 } 46 out: 47 return fd; 48 } 49 50 static void test_bpf_nf_ct(int mode) 51 { 52 const char *iptables = "iptables-legacy -t raw %s PREROUTING -j CONNMARK --set-mark 42/0"; 53 int srv_fd = -1, client_fd = -1, srv_client_fd = -1; 54 struct sockaddr_in peer_addr = {}; 55 struct test_bpf_nf *skel; 56 int prog_fd, err; 57 socklen_t len; 58 u16 srv_port; 59 char cmd[128]; 60 LIBBPF_OPTS(bpf_test_run_opts, topts, 61 .data_in = &pkt_v4, 62 .data_size_in = sizeof(pkt_v4), 63 .repeat = 1, 64 ); 65 66 skel = test_bpf_nf__open_and_load(); 67 if (!ASSERT_OK_PTR(skel, "test_bpf_nf__open_and_load")) 68 return; 69 70 /* Enable connection tracking */ 71 snprintf(cmd, sizeof(cmd), iptables, "-A"); 72 if (!ASSERT_OK(system(cmd), cmd)) 73 goto end; 74 75 srv_port = (mode == TEST_XDP) ? 5005 : 5006; 76 srv_fd = start_server(AF_INET, SOCK_STREAM, "127.0.0.1", srv_port, TIMEOUT_MS); 77 if (!ASSERT_GE(srv_fd, 0, "start_server")) 78 goto end; 79 80 client_fd = connect_to_server(srv_fd); 81 if (!ASSERT_GE(client_fd, 0, "connect_to_server")) 82 goto end; 83 84 len = sizeof(peer_addr); 85 srv_client_fd = accept(srv_fd, (struct sockaddr *)&peer_addr, &len); 86 if (!ASSERT_GE(srv_client_fd, 0, "accept")) 87 goto end; 88 if (!ASSERT_EQ(len, sizeof(struct sockaddr_in), "sockaddr len")) 89 goto end; 90 91 skel->bss->saddr = peer_addr.sin_addr.s_addr; 92 skel->bss->sport = peer_addr.sin_port; 93 skel->bss->daddr = peer_addr.sin_addr.s_addr; 94 skel->bss->dport = htons(srv_port); 95 96 if (mode == TEST_XDP) 97 prog_fd = bpf_program__fd(skel->progs.nf_xdp_ct_test); 98 else 99 prog_fd = bpf_program__fd(skel->progs.nf_skb_ct_test); 100 101 err = bpf_prog_test_run_opts(prog_fd, &topts); 102 if (!ASSERT_OK(err, "bpf_prog_test_run")) 103 goto end; 104 105 ASSERT_EQ(skel->bss->test_einval_bpf_tuple, -EINVAL, "Test EINVAL for NULL bpf_tuple"); 106 ASSERT_EQ(skel->bss->test_einval_reserved, -EINVAL, "Test EINVAL for reserved not set to 0"); 107 ASSERT_EQ(skel->bss->test_einval_netns_id, -EINVAL, "Test EINVAL for netns_id < -1"); 108 ASSERT_EQ(skel->bss->test_einval_len_opts, -EINVAL, "Test EINVAL for len__opts != NF_BPF_CT_OPTS_SZ"); 109 ASSERT_EQ(skel->bss->test_eproto_l4proto, -EPROTO, "Test EPROTO for l4proto != TCP or UDP"); 110 ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id"); 111 ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup"); 112 ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple"); 113 ASSERT_EQ(skel->data->test_alloc_entry, 0, "Test for alloc new entry"); 114 ASSERT_EQ(skel->data->test_insert_entry, 0, "Test for insert new entry"); 115 ASSERT_EQ(skel->data->test_succ_lookup, 0, "Test for successful lookup"); 116 /* allow some tolerance for test_delta_timeout value to avoid races. */ 117 ASSERT_GT(skel->bss->test_delta_timeout, 8, "Test for min ct timeout update"); 118 ASSERT_LE(skel->bss->test_delta_timeout, 10, "Test for max ct timeout update"); 119 ASSERT_EQ(skel->bss->test_insert_lookup_mark, 77, "Test for insert and lookup mark value"); 120 ASSERT_EQ(skel->bss->test_status, IPS_STATUS_MASK, "Test for ct status update "); 121 ASSERT_EQ(skel->data->test_exist_lookup, 0, "Test existing connection lookup"); 122 ASSERT_EQ(skel->bss->test_exist_lookup_mark, 43, "Test existing connection lookup ctmark"); 123 ASSERT_EQ(skel->data->test_snat_addr, 0, "Test for source natting"); 124 ASSERT_EQ(skel->data->test_dnat_addr, 0, "Test for destination natting"); 125 end: 126 if (srv_client_fd != -1) 127 close(srv_client_fd); 128 if (client_fd != -1) 129 close(client_fd); 130 if (srv_fd != -1) 131 close(srv_fd); 132 snprintf(cmd, sizeof(cmd), iptables, "-D"); 133 system(cmd); 134 test_bpf_nf__destroy(skel); 135 } 136 137 static void test_bpf_nf_ct_fail(const char *prog_name, const char *err_msg) 138 { 139 LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf, 140 .kernel_log_size = sizeof(log_buf), 141 .kernel_log_level = 1); 142 struct test_bpf_nf_fail *skel; 143 struct bpf_program *prog; 144 int ret; 145 146 skel = test_bpf_nf_fail__open_opts(&opts); 147 if (!ASSERT_OK_PTR(skel, "test_bpf_nf_fail__open")) 148 return; 149 150 prog = bpf_object__find_program_by_name(skel->obj, prog_name); 151 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 152 goto end; 153 154 bpf_program__set_autoload(prog, true); 155 156 ret = test_bpf_nf_fail__load(skel); 157 if (!ASSERT_ERR(ret, "test_bpf_nf_fail__load must fail")) 158 goto end; 159 160 if (!ASSERT_OK_PTR(strstr(log_buf, err_msg), "expected error message")) { 161 fprintf(stderr, "Expected: %s\n", err_msg); 162 fprintf(stderr, "Verifier: %s\n", log_buf); 163 } 164 165 end: 166 test_bpf_nf_fail__destroy(skel); 167 } 168 169 void test_bpf_nf(void) 170 { 171 int i; 172 if (test__start_subtest("xdp-ct")) 173 test_bpf_nf_ct(TEST_XDP); 174 if (test__start_subtest("tc-bpf-ct")) 175 test_bpf_nf_ct(TEST_TC_BPF); 176 for (i = 0; i < ARRAY_SIZE(test_bpf_nf_fail_tests); i++) { 177 if (test__start_subtest(test_bpf_nf_fail_tests[i].prog_name)) 178 test_bpf_nf_ct_fail(test_bpf_nf_fail_tests[i].prog_name, 179 test_bpf_nf_fail_tests[i].err_msg); 180 } 181 } 182