npf_rule_test.c revision 1.23 1 1.1 rmind /*
2 1.16 rmind * NPF ruleset tests.
3 1.1 rmind *
4 1.1 rmind * Public Domain.
5 1.1 rmind */
6 1.1 rmind
7 1.13 christos #ifdef _KERNEL
8 1.1 rmind #include <sys/types.h>
9 1.13 christos #endif
10 1.1 rmind
11 1.1 rmind #include "npf_impl.h"
12 1.1 rmind #include "npf_test.h"
13 1.1 rmind
14 1.1 rmind #define RESULT_PASS 0
15 1.1 rmind #define RESULT_BLOCK ENETUNREACH
16 1.1 rmind
17 1.1 rmind static const struct test_case {
18 1.20 riastrad int af;
19 1.1 rmind const char * src;
20 1.1 rmind const char * dst;
21 1.1 rmind const char * ifname;
22 1.1 rmind int di;
23 1.1 rmind int stateful_ret;
24 1.1 rmind int ret;
25 1.1 rmind } test_cases[] = {
26 1.1 rmind
27 1.1 rmind /* Stateful pass. */
28 1.1 rmind {
29 1.20 riastrad .af = AF_INET,
30 1.1 rmind .src = "10.1.1.1", .dst = "10.1.1.2",
31 1.1 rmind .ifname = IFNAME_INT, .di = PFIL_OUT,
32 1.1 rmind .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
33 1.1 rmind },
34 1.1 rmind {
35 1.20 riastrad .af = AF_INET,
36 1.1 rmind .src = "10.1.1.2", .dst = "10.1.1.1",
37 1.1 rmind .ifname = IFNAME_INT, .di = PFIL_IN,
38 1.1 rmind .stateful_ret = RESULT_PASS, .ret = RESULT_BLOCK
39 1.1 rmind },
40 1.1 rmind
41 1.1 rmind /* Pass forwards stream only. */
42 1.1 rmind {
43 1.20 riastrad .af = AF_INET,
44 1.1 rmind .src = "10.1.1.1", .dst = "10.1.1.3",
45 1.1 rmind .ifname = IFNAME_INT, .di = PFIL_OUT,
46 1.1 rmind .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
47 1.1 rmind },
48 1.1 rmind {
49 1.20 riastrad .af = AF_INET,
50 1.1 rmind .src = "10.1.1.3", .dst = "10.1.1.1",
51 1.1 rmind .ifname = IFNAME_INT, .di = PFIL_IN,
52 1.1 rmind .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
53 1.1 rmind },
54 1.1 rmind
55 1.21 riastrad /*
56 1.23 riastrad * Pass in from any of the { fe80::1, fe80:1000:0:0/95,
57 1.23 riastrad * fe80::2, fe80::2000:0:0/96, fe80::3, fe80::3000:0:0/97 }
58 1.23 riastrad * group.
59 1.21 riastrad */
60 1.23 riastrad { /* fe80::1 */
61 1.21 riastrad .af = AF_INET6,
62 1.21 riastrad .src = "fe80::1", .dst = "fe80::adec:c91c:d116:7592",
63 1.22 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
64 1.21 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
65 1.21 riastrad },
66 1.23 riastrad { /* fe80::1000:0:0/95 */
67 1.23 riastrad .af = AF_INET6,
68 1.23 riastrad .src = "fe80::1001:0:0", .dst = "fe80::adec:c91c:d116:7592",
69 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
70 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
71 1.23 riastrad },
72 1.23 riastrad { /* fe80::1000:0:0/95, one bit off */
73 1.23 riastrad .af = AF_INET6,
74 1.23 riastrad .src = "fe80::1003:0:0", .dst = "fe80::adec:c91c:d116:7592",
75 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
76 1.23 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
77 1.23 riastrad },
78 1.23 riastrad { /* fe80::2 */
79 1.21 riastrad .af = AF_INET6,
80 1.21 riastrad .src = "fe80::2", .dst = "fe80::adec:c91c:d116:7592",
81 1.22 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
82 1.21 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
83 1.21 riastrad },
84 1.23 riastrad { /* fe80::2000:0:0/96 */
85 1.23 riastrad .af = AF_INET6,
86 1.23 riastrad .src = "fe80::2000:8000:0", .dst = "fe80::adec:c91c:d116:7592",
87 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
88 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
89 1.23 riastrad },
90 1.23 riastrad { /* fe80::2000:0:0/96, one bit off */
91 1.23 riastrad .af = AF_INET6,
92 1.23 riastrad .src = "fe80::2001:8000:0", .dst = "fe80::adec:c91c:d116:7592",
93 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
94 1.23 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
95 1.23 riastrad },
96 1.23 riastrad { /* fe80::3 */
97 1.23 riastrad .af = AF_INET6,
98 1.23 riastrad .src = "fe80::3", .dst = "fe80::adec:c91c:d116:7592",
99 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
100 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
101 1.23 riastrad },
102 1.23 riastrad { /* fe80::3000:0:0/97 */
103 1.23 riastrad .af = AF_INET6,
104 1.23 riastrad .src = "fe80::3000:7fff:0", .dst = "fe80::adec:c91c:d116:7592",
105 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
106 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
107 1.23 riastrad },
108 1.23 riastrad { /* fe80::3000:0:0/97, one bit off */
109 1.23 riastrad .af = AF_INET6,
110 1.23 riastrad .src = "fe80::3000:ffff:0", .dst = "fe80::adec:c91c:d116:7592",
111 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
112 1.23 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
113 1.23 riastrad },
114 1.21 riastrad {
115 1.21 riastrad .af = AF_INET6,
116 1.23 riastrad .src = "fe80::4", .dst = "fe80::adec:c91c:d116:7592",
117 1.22 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
118 1.22 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
119 1.22 riastrad },
120 1.22 riastrad
121 1.22 riastrad /*
122 1.23 riastrad * Pass in from anywhere _not_ in that group, as long as it is
123 1.23 riastrad * to that group.
124 1.22 riastrad */
125 1.23 riastrad { /* fe80::1 */
126 1.22 riastrad .af = AF_INET6,
127 1.22 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::1",
128 1.22 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
129 1.22 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
130 1.22 riastrad },
131 1.23 riastrad { /* fe80::1000:0:0/95 */
132 1.23 riastrad .af = AF_INET6,
133 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::1001:0:0",
134 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
135 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
136 1.23 riastrad },
137 1.23 riastrad { /* fe80::1000:0:0/95, one bit off */
138 1.23 riastrad .af = AF_INET6,
139 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::1003:0:0",
140 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
141 1.23 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
142 1.23 riastrad },
143 1.23 riastrad { /* fe80::2 */
144 1.22 riastrad .af = AF_INET6,
145 1.22 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::2",
146 1.22 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
147 1.22 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
148 1.22 riastrad },
149 1.23 riastrad { /* fe80::2000:0:0/96 */
150 1.23 riastrad .af = AF_INET6,
151 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::2000:8000:0",
152 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
153 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
154 1.23 riastrad },
155 1.23 riastrad { /* fe80::2000:0:0/96, one bit off */
156 1.23 riastrad .af = AF_INET6,
157 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::2001:8000:0",
158 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
159 1.23 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
160 1.23 riastrad },
161 1.23 riastrad { /* fe80::3 */
162 1.23 riastrad .af = AF_INET6,
163 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::3",
164 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
165 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
166 1.23 riastrad },
167 1.23 riastrad { /* fe80::3000:0:0/97 */
168 1.23 riastrad .af = AF_INET6,
169 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::3000:7fff:0",
170 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
171 1.23 riastrad .stateful_ret = RESULT_PASS, .ret = RESULT_PASS
172 1.23 riastrad },
173 1.23 riastrad { /* fe80::3000:0:0/97, one bit off */
174 1.23 riastrad .af = AF_INET6,
175 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::3000:ffff:0",
176 1.23 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
177 1.23 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
178 1.23 riastrad },
179 1.22 riastrad {
180 1.22 riastrad .af = AF_INET6,
181 1.23 riastrad .src = "fe80::adec:c91c:d116:7592", .dst = "fe80::4",
182 1.22 riastrad .ifname = IFNAME_INT, .di = PFIL_IN,
183 1.21 riastrad .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
184 1.21 riastrad },
185 1.21 riastrad
186 1.1 rmind /* Block. */
187 1.20 riastrad {
188 1.20 riastrad .af = AF_INET,
189 1.20 riastrad .src = "10.1.1.1", .dst = "10.1.1.4",
190 1.1 rmind .ifname = IFNAME_INT, .di = PFIL_OUT,
191 1.1 rmind .stateful_ret = RESULT_BLOCK, .ret = RESULT_BLOCK
192 1.1 rmind },
193 1.1 rmind
194 1.1 rmind };
195 1.1 rmind
196 1.1 rmind static int
197 1.17 rmind run_raw_testcase(unsigned i)
198 1.1 rmind {
199 1.17 rmind const struct test_case *t = &test_cases[i];
200 1.13 christos npf_t *npf = npf_getkernctx();
201 1.17 rmind npf_cache_t *npc;
202 1.17 rmind struct mbuf *m;
203 1.1 rmind npf_rule_t *rl;
204 1.17 rmind int slock, error;
205 1.17 rmind
206 1.20 riastrad m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, 9000, 9000);
207 1.17 rmind npc = get_cached_pkt(m, t->ifname);
208 1.1 rmind
209 1.19 rmind slock = npf_config_read_enter(npf);
210 1.17 rmind rl = npf_ruleset_inspect(npc, npf_config_ruleset(npf), t->di, NPF_LAYER_3);
211 1.1 rmind if (rl) {
212 1.17 rmind npf_match_info_t mi;
213 1.14 christos error = npf_rule_conclude(rl, &mi);
214 1.1 rmind } else {
215 1.1 rmind error = ENOENT;
216 1.1 rmind }
217 1.19 rmind npf_config_read_exit(npf, slock);
218 1.17 rmind
219 1.17 rmind put_cached_pkt(npc);
220 1.1 rmind return error;
221 1.1 rmind }
222 1.1 rmind
223 1.4 rmind static int
224 1.17 rmind run_handler_testcase(unsigned i)
225 1.4 rmind {
226 1.8 rmind const struct test_case *t = &test_cases[i];
227 1.13 christos ifnet_t *ifp = npf_test_getif(t->ifname);
228 1.17 rmind npf_t *npf = npf_getkernctx();
229 1.17 rmind struct mbuf *m;
230 1.4 rmind int error;
231 1.4 rmind
232 1.20 riastrad m = mbuf_get_pkt(t->af, IPPROTO_UDP, t->src, t->dst, 9000, 9000);
233 1.18 rmind error = npfk_packet_handler(npf, &m, ifp, t->di);
234 1.17 rmind if (m) {
235 1.17 rmind m_freem(m);
236 1.17 rmind }
237 1.4 rmind return error;
238 1.4 rmind }
239 1.4 rmind
240 1.4 rmind static npf_rule_t *
241 1.4 rmind npf_blockall_rule(void)
242 1.4 rmind {
243 1.13 christos npf_t *npf = npf_getkernctx();
244 1.15 rmind nvlist_t *rule = nvlist_create(0);
245 1.17 rmind npf_rule_t *rl;
246 1.4 rmind
247 1.15 rmind nvlist_add_number(rule, "attr",
248 1.7 rmind NPF_RULE_IN | NPF_RULE_OUT | NPF_RULE_DYNAMIC);
249 1.17 rmind rl = npf_rule_alloc(npf, rule);
250 1.17 rmind nvlist_destroy(rule);
251 1.17 rmind return rl;
252 1.4 rmind }
253 1.4 rmind
254 1.17 rmind static bool
255 1.17 rmind test_static(bool verbose)
256 1.1 rmind {
257 1.1 rmind for (unsigned i = 0; i < __arraycount(test_cases); i++) {
258 1.1 rmind const struct test_case *t = &test_cases[i];
259 1.17 rmind int error, serror;
260 1.1 rmind
261 1.17 rmind if (npf_test_getif(t->ifname) == NULL) {
262 1.1 rmind printf("Interface %s is not configured.\n", t->ifname);
263 1.1 rmind return false;
264 1.1 rmind }
265 1.1 rmind
266 1.17 rmind error = run_raw_testcase(i);
267 1.17 rmind serror = run_handler_testcase(i);
268 1.1 rmind
269 1.1 rmind if (verbose) {
270 1.16 rmind printf("rule test %d:\texpected %d (stateful) and %d\n"
271 1.16 rmind "\t\t-> returned %d and %d\n",
272 1.1 rmind i + 1, t->stateful_ret, t->ret, serror, error);
273 1.1 rmind }
274 1.17 rmind CHECK_TRUE(error == t->ret);
275 1.17 rmind CHECK_TRUE(serror == t->stateful_ret)
276 1.1 rmind }
277 1.17 rmind return true;
278 1.17 rmind }
279 1.17 rmind
280 1.17 rmind static bool
281 1.17 rmind test_dynamic(void)
282 1.17 rmind {
283 1.17 rmind npf_t *npf = npf_getkernctx();
284 1.17 rmind npf_ruleset_t *rlset;
285 1.17 rmind npf_rule_t *rl;
286 1.17 rmind uint64_t id;
287 1.17 rmind int error;
288 1.4 rmind
289 1.8 rmind /*
290 1.8 rmind * Test dynamic NPF rules.
291 1.8 rmind */
292 1.8 rmind
293 1.17 rmind error = run_raw_testcase(0);
294 1.16 rmind CHECK_TRUE(error == RESULT_PASS);
295 1.4 rmind
296 1.13 christos npf_config_enter(npf);
297 1.13 christos rlset = npf_config_ruleset(npf);
298 1.4 rmind
299 1.4 rmind rl = npf_blockall_rule();
300 1.4 rmind error = npf_ruleset_add(rlset, "test-rules", rl);
301 1.16 rmind CHECK_TRUE(error == 0);
302 1.4 rmind
303 1.17 rmind error = run_raw_testcase(0);
304 1.16 rmind CHECK_TRUE(error == RESULT_BLOCK);
305 1.4 rmind
306 1.6 rmind id = npf_rule_getid(rl);
307 1.6 rmind error = npf_ruleset_remove(rlset, "test-rules", id);
308 1.16 rmind CHECK_TRUE(error == 0);
309 1.4 rmind
310 1.13 christos npf_config_exit(npf);
311 1.4 rmind
312 1.17 rmind error = run_raw_testcase(0);
313 1.16 rmind CHECK_TRUE(error == RESULT_PASS);
314 1.4 rmind
315 1.16 rmind return true;
316 1.1 rmind }
317 1.17 rmind
318 1.17 rmind bool
319 1.17 rmind npf_rule_test(bool verbose)
320 1.17 rmind {
321 1.17 rmind bool ok;
322 1.17 rmind
323 1.17 rmind ok = test_static(verbose);
324 1.17 rmind CHECK_TRUE(ok);
325 1.17 rmind
326 1.17 rmind ok = test_dynamic();
327 1.17 rmind CHECK_TRUE(ok);
328 1.17 rmind
329 1.17 rmind return true;
330 1.17 rmind }
331