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
connect_to_server(int srv_fd)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
test_bpf_nf_ct(int mode)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_reserved_new, -EINVAL, "Test EINVAL for reserved in new struct not set to 0");
108 ASSERT_EQ(skel->bss->test_einval_netns_id, -EINVAL, "Test EINVAL for netns_id < -1");
109 ASSERT_EQ(skel->bss->test_einval_len_opts, -EINVAL, "Test EINVAL for len__opts != NF_BPF_CT_OPTS_SZ");
110 ASSERT_EQ(skel->bss->test_eproto_l4proto, -EPROTO, "Test EPROTO for l4proto != TCP or UDP");
111 ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id");
112 ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup");
113 ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple");
114 ASSERT_EQ(skel->data->test_alloc_entry, 0, "Test for alloc new entry");
115 ASSERT_EQ(skel->data->test_insert_entry, 0, "Test for insert new entry");
116 ASSERT_EQ(skel->data->test_succ_lookup, 0, "Test for successful lookup");
117 /* allow some tolerance for test_delta_timeout value to avoid races. */
118 ASSERT_GT(skel->bss->test_delta_timeout, 8, "Test for min ct timeout update");
119 ASSERT_LE(skel->bss->test_delta_timeout, 10, "Test for max ct timeout update");
120 ASSERT_EQ(skel->bss->test_insert_lookup_mark, 77, "Test for insert and lookup mark value");
121 ASSERT_EQ(skel->bss->test_status, IPS_STATUS_MASK, "Test for ct status update ");
122 ASSERT_EQ(skel->data->test_exist_lookup, 0, "Test existing connection lookup");
123 ASSERT_EQ(skel->bss->test_exist_lookup_mark, 43, "Test existing connection lookup ctmark");
124 ASSERT_EQ(skel->data->test_snat_addr, 0, "Test for source natting");
125 ASSERT_EQ(skel->data->test_dnat_addr, 0, "Test for destination natting");
126 ASSERT_EQ(skel->data->test_ct_zone_id_alloc_entry, 0, "Test for alloc new entry in specified ct zone");
127 ASSERT_EQ(skel->data->test_ct_zone_id_insert_entry, 0, "Test for insert new entry in specified ct zone");
128 ASSERT_EQ(skel->data->test_ct_zone_id_succ_lookup, 0, "Test for successful lookup in specified ct_zone");
129 ASSERT_EQ(skel->bss->test_ct_zone_dir_enoent_lookup, -ENOENT, "Test ENOENT for lookup with wrong ct zone dir");
130 ASSERT_EQ(skel->bss->test_ct_zone_id_enoent_lookup, -ENOENT, "Test ENOENT for lookup in wrong ct zone");
131
132 end:
133 if (client_fd != -1)
134 close(client_fd);
135 if (srv_client_fd != -1)
136 close(srv_client_fd);
137 if (srv_fd != -1)
138 close(srv_fd);
139
140 snprintf(cmd, sizeof(cmd), iptables, "-D");
141 system(cmd);
142 test_bpf_nf__destroy(skel);
143 }
144
test_bpf_nf_ct_fail(const char * prog_name,const char * err_msg)145 static void test_bpf_nf_ct_fail(const char *prog_name, const char *err_msg)
146 {
147 LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf,
148 .kernel_log_size = sizeof(log_buf),
149 .kernel_log_level = 1);
150 struct test_bpf_nf_fail *skel;
151 struct bpf_program *prog;
152 int ret;
153
154 skel = test_bpf_nf_fail__open_opts(&opts);
155 if (!ASSERT_OK_PTR(skel, "test_bpf_nf_fail__open"))
156 return;
157
158 prog = bpf_object__find_program_by_name(skel->obj, prog_name);
159 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
160 goto end;
161
162 bpf_program__set_autoload(prog, true);
163
164 ret = test_bpf_nf_fail__load(skel);
165 if (!ASSERT_ERR(ret, "test_bpf_nf_fail__load must fail"))
166 goto end;
167
168 if (!ASSERT_OK_PTR(strstr(log_buf, err_msg), "expected error message")) {
169 fprintf(stderr, "Expected: %s\n", err_msg);
170 fprintf(stderr, "Verifier: %s\n", log_buf);
171 }
172
173 end:
174 test_bpf_nf_fail__destroy(skel);
175 }
176
test_bpf_nf(void)177 void test_bpf_nf(void)
178 {
179 int i;
180 if (test__start_subtest("xdp-ct"))
181 test_bpf_nf_ct(TEST_XDP);
182 if (test__start_subtest("tc-bpf-ct"))
183 test_bpf_nf_ct(TEST_TC_BPF);
184 for (i = 0; i < ARRAY_SIZE(test_bpf_nf_fail_tests); i++) {
185 if (test__start_subtest(test_bpf_nf_fail_tests[i].prog_name))
186 test_bpf_nf_ct_fail(test_bpf_nf_fail_tests[i].prog_name,
187 test_bpf_nf_fail_tests[i].err_msg);
188 }
189 }
190