npf_ruleset.c revision 1.46.4.1 1 1.1 rmind /*-
2 1.41 rmind * Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * This material is based upon work partially supported by The
6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.1 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.1 rmind /*
31 1.1 rmind * NPF ruleset module.
32 1.1 rmind */
33 1.1 rmind
34 1.43 christos #ifdef _KERNEL
35 1.1 rmind #include <sys/cdefs.h>
36 1.46.4.1 christos __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.46.4.1 2019/06/10 22:09:46 christos Exp $");
37 1.1 rmind
38 1.1 rmind #include <sys/param.h>
39 1.11 rmind #include <sys/types.h>
40 1.1 rmind
41 1.20 rmind #include <sys/atomic.h>
42 1.1 rmind #include <sys/kmem.h>
43 1.1 rmind #include <sys/queue.h>
44 1.17 rmind #include <sys/mbuf.h>
45 1.1 rmind #include <sys/types.h>
46 1.1 rmind
47 1.17 rmind #include <net/bpf.h>
48 1.20 rmind #include <net/bpfjit.h>
49 1.3 rmind #include <net/pfil.h>
50 1.1 rmind #include <net/if.h>
51 1.43 christos #endif
52 1.1 rmind
53 1.1 rmind #include "npf_impl.h"
54 1.1 rmind
55 1.4 rmind struct npf_ruleset {
56 1.18 rmind /*
57 1.18 rmind * - List of all rules.
58 1.18 rmind * - Dynamic (i.e. named) rules.
59 1.18 rmind * - G/C list for convenience.
60 1.18 rmind */
61 1.17 rmind LIST_HEAD(, npf_rule) rs_all;
62 1.17 rmind LIST_HEAD(, npf_rule) rs_dynamic;
63 1.18 rmind LIST_HEAD(, npf_rule) rs_gc;
64 1.17 rmind
65 1.19 rmind /* Unique ID counter. */
66 1.19 rmind uint64_t rs_idcnt;
67 1.19 rmind
68 1.17 rmind /* Number of array slots and active rules. */
69 1.17 rmind u_int rs_slots;
70 1.17 rmind u_int rs_nitems;
71 1.17 rmind
72 1.17 rmind /* Array of ordered rules. */
73 1.17 rmind npf_rule_t * rs_rules[];
74 1.4 rmind };
75 1.4 rmind
76 1.1 rmind struct npf_rule {
77 1.17 rmind /* Attributes, interface and skip slot. */
78 1.4 rmind uint32_t r_attr;
79 1.4 rmind u_int r_ifid;
80 1.17 rmind u_int r_skip_to;
81 1.17 rmind
82 1.17 rmind /* Code to process, if any. */
83 1.17 rmind int r_type;
84 1.27 rmind bpfjit_func_t r_jcode;
85 1.17 rmind void * r_code;
86 1.36 rmind u_int r_clen;
87 1.17 rmind
88 1.17 rmind /* NAT policy (optional), rule procedure and subset. */
89 1.17 rmind npf_natpolicy_t * r_natp;
90 1.4 rmind npf_rproc_t * r_rproc;
91 1.17 rmind
92 1.42 rmind union {
93 1.42 rmind /*
94 1.42 rmind * Dynamic group: rule subset and a group list entry.
95 1.42 rmind */
96 1.42 rmind struct {
97 1.42 rmind npf_rule_t * r_subset;
98 1.42 rmind LIST_ENTRY(npf_rule) r_dentry;
99 1.42 rmind };
100 1.17 rmind
101 1.42 rmind /*
102 1.42 rmind * Dynamic rule: priority, parent group and next rule.
103 1.42 rmind */
104 1.42 rmind struct {
105 1.42 rmind int r_priority;
106 1.42 rmind npf_rule_t * r_parent;
107 1.42 rmind npf_rule_t * r_next;
108 1.42 rmind };
109 1.42 rmind };
110 1.17 rmind
111 1.36 rmind /* Rule ID, name and the optional key. */
112 1.19 rmind uint64_t r_id;
113 1.36 rmind char r_name[NPF_RULE_MAXNAMELEN];
114 1.36 rmind uint8_t r_key[NPF_RULE_MAXKEYLEN];
115 1.18 rmind
116 1.36 rmind /* All-list entry and the auxiliary info. */
117 1.17 rmind LIST_ENTRY(npf_rule) r_aentry;
118 1.46.4.1 christos nvlist_t * r_info;
119 1.46.4.1 christos size_t r_info_len;
120 1.36 rmind };
121 1.17 rmind
122 1.37 rmind #define SKIPTO_ADJ_FLAG (1U << 31)
123 1.37 rmind #define SKIPTO_MASK (SKIPTO_ADJ_FLAG - 1)
124 1.37 rmind
125 1.46.4.1 christos static nvlist_t * npf_rule_export(npf_t *, const npf_rule_t *);
126 1.1 rmind
127 1.31 rmind /*
128 1.31 rmind * Private attributes - must be in the NPF_RULE_PRIVMASK range.
129 1.31 rmind */
130 1.31 rmind #define NPF_RULE_KEEPNAT (0x01000000 & NPF_RULE_PRIVMASK)
131 1.31 rmind
132 1.17 rmind #define NPF_DYNAMIC_GROUP_P(attr) \
133 1.17 rmind (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
134 1.17 rmind
135 1.19 rmind #define NPF_DYNAMIC_RULE_P(attr) \
136 1.19 rmind (((attr) & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC)
137 1.19 rmind
138 1.1 rmind npf_ruleset_t *
139 1.17 rmind npf_ruleset_create(size_t slots)
140 1.1 rmind {
141 1.17 rmind size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
142 1.1 rmind npf_ruleset_t *rlset;
143 1.1 rmind
144 1.17 rmind rlset = kmem_zalloc(len, KM_SLEEP);
145 1.17 rmind LIST_INIT(&rlset->rs_dynamic);
146 1.17 rmind LIST_INIT(&rlset->rs_all);
147 1.19 rmind LIST_INIT(&rlset->rs_gc);
148 1.19 rmind rlset->rs_slots = slots;
149 1.19 rmind
150 1.1 rmind return rlset;
151 1.1 rmind }
152 1.1 rmind
153 1.1 rmind void
154 1.1 rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
155 1.1 rmind {
156 1.17 rmind size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
157 1.1 rmind npf_rule_t *rl;
158 1.1 rmind
159 1.17 rmind while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
160 1.42 rmind if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
161 1.42 rmind /*
162 1.42 rmind * Note: r_subset may point to the rules which
163 1.42 rmind * were inherited by a new ruleset.
164 1.42 rmind */
165 1.42 rmind rl->r_subset = NULL;
166 1.42 rmind LIST_REMOVE(rl, r_dentry);
167 1.42 rmind }
168 1.42 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
169 1.42 rmind /* Not removing from r_subset, see above. */
170 1.42 rmind KASSERT(rl->r_parent != NULL);
171 1.42 rmind }
172 1.42 rmind LIST_REMOVE(rl, r_aentry);
173 1.1 rmind npf_rule_free(rl);
174 1.1 rmind }
175 1.17 rmind KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
176 1.43 christos
177 1.43 christos npf_ruleset_gc(rlset);
178 1.18 rmind KASSERT(LIST_EMPTY(&rlset->rs_gc));
179 1.17 rmind kmem_free(rlset, len);
180 1.1 rmind }
181 1.1 rmind
182 1.1 rmind /*
183 1.1 rmind * npf_ruleset_insert: insert the rule into the specified ruleset.
184 1.1 rmind */
185 1.1 rmind void
186 1.1 rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
187 1.1 rmind {
188 1.17 rmind u_int n = rlset->rs_nitems;
189 1.17 rmind
190 1.17 rmind KASSERT(n < rlset->rs_slots);
191 1.17 rmind
192 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
193 1.17 rmind if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
194 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
195 1.24 rmind } else {
196 1.24 rmind KASSERTMSG(rl->r_parent == NULL, "cannot be dynamic rule");
197 1.24 rmind rl->r_attr &= ~NPF_RULE_DYNAMIC;
198 1.17 rmind }
199 1.17 rmind
200 1.17 rmind rlset->rs_rules[n] = rl;
201 1.17 rmind rlset->rs_nitems++;
202 1.45 christos rl->r_id = ++rlset->rs_idcnt;
203 1.17 rmind
204 1.17 rmind if (rl->r_skip_to < ++n) {
205 1.37 rmind rl->r_skip_to = SKIPTO_ADJ_FLAG | n;
206 1.17 rmind }
207 1.17 rmind }
208 1.17 rmind
209 1.46 rmind npf_rule_t *
210 1.17 rmind npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
211 1.17 rmind {
212 1.17 rmind npf_rule_t *rl;
213 1.17 rmind
214 1.17 rmind LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
215 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
216 1.17 rmind if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
217 1.17 rmind break;
218 1.17 rmind }
219 1.17 rmind return rl;
220 1.17 rmind }
221 1.17 rmind
222 1.39 rmind /*
223 1.39 rmind * npf_ruleset_add: insert dynamic rule into the (active) ruleset.
224 1.39 rmind */
225 1.17 rmind int
226 1.17 rmind npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
227 1.17 rmind {
228 1.42 rmind npf_rule_t *rg, *it, *target;
229 1.42 rmind int priocmd;
230 1.17 rmind
231 1.42 rmind if (!NPF_DYNAMIC_RULE_P(rl->r_attr)) {
232 1.42 rmind return EINVAL;
233 1.42 rmind }
234 1.17 rmind rg = npf_ruleset_lookup(rlset, rname);
235 1.17 rmind if (rg == NULL) {
236 1.19 rmind return ESRCH;
237 1.19 rmind }
238 1.17 rmind
239 1.19 rmind /* Dynamic rule - assign a unique ID and save the parent. */
240 1.19 rmind rl->r_id = ++rlset->rs_idcnt;
241 1.17 rmind rl->r_parent = rg;
242 1.17 rmind
243 1.17 rmind /*
244 1.17 rmind * Rule priority: (highest) 1, 2 ... n (lowest).
245 1.17 rmind * Negative priority indicates an operation and is reset to zero.
246 1.17 rmind */
247 1.17 rmind if ((priocmd = rl->r_priority) < 0) {
248 1.17 rmind rl->r_priority = 0;
249 1.17 rmind }
250 1.17 rmind
251 1.42 rmind /*
252 1.42 rmind * WARNING: once rg->subset or target->r_next of an *active*
253 1.42 rmind * rule is set, then our rule becomes globally visible and active.
254 1.42 rmind * Must issue a load fence to ensure rl->r_next visibility first.
255 1.42 rmind */
256 1.17 rmind switch (priocmd) {
257 1.17 rmind case NPF_PRI_LAST:
258 1.17 rmind default:
259 1.42 rmind target = NULL;
260 1.42 rmind it = rg->r_subset;
261 1.42 rmind while (it && it->r_priority <= rl->r_priority) {
262 1.42 rmind target = it;
263 1.42 rmind it = it->r_next;
264 1.42 rmind }
265 1.42 rmind if (target) {
266 1.42 rmind rl->r_next = target->r_next;
267 1.42 rmind membar_producer();
268 1.42 rmind target->r_next = rl;
269 1.42 rmind break;
270 1.17 rmind }
271 1.42 rmind /* FALLTHROUGH */
272 1.42 rmind
273 1.42 rmind case NPF_PRI_FIRST:
274 1.42 rmind rl->r_next = rg->r_subset;
275 1.42 rmind membar_producer();
276 1.42 rmind rg->r_subset = rl;
277 1.17 rmind break;
278 1.17 rmind }
279 1.17 rmind
280 1.17 rmind /* Finally, add into the all-list. */
281 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
282 1.17 rmind return 0;
283 1.17 rmind }
284 1.17 rmind
285 1.42 rmind static void
286 1.42 rmind npf_ruleset_unlink(npf_rule_t *rl, npf_rule_t *prev)
287 1.42 rmind {
288 1.42 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
289 1.42 rmind if (prev) {
290 1.42 rmind prev->r_next = rl->r_next;
291 1.42 rmind } else {
292 1.42 rmind npf_rule_t *rg = rl->r_parent;
293 1.42 rmind rg->r_subset = rl->r_next;
294 1.42 rmind }
295 1.42 rmind LIST_REMOVE(rl, r_aentry);
296 1.42 rmind }
297 1.42 rmind
298 1.39 rmind /*
299 1.39 rmind * npf_ruleset_remove: remove the dynamic rule given the rule ID.
300 1.39 rmind */
301 1.18 rmind int
302 1.19 rmind npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uint64_t id)
303 1.17 rmind {
304 1.42 rmind npf_rule_t *rg, *prev = NULL;
305 1.17 rmind
306 1.17 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
307 1.19 rmind return ESRCH;
308 1.17 rmind }
309 1.42 rmind for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
310 1.24 rmind KASSERT(rl->r_parent == rg);
311 1.42 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
312 1.24 rmind
313 1.17 rmind /* Compare ID. On match, remove and return. */
314 1.19 rmind if (rl->r_id == id) {
315 1.42 rmind npf_ruleset_unlink(rl, prev);
316 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
317 1.19 rmind return 0;
318 1.17 rmind }
319 1.42 rmind prev = rl;
320 1.17 rmind }
321 1.19 rmind return ENOENT;
322 1.17 rmind }
323 1.17 rmind
324 1.39 rmind /*
325 1.39 rmind * npf_ruleset_remkey: remove the dynamic rule given the rule key.
326 1.39 rmind */
327 1.18 rmind int
328 1.17 rmind npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
329 1.17 rmind const void *key, size_t len)
330 1.17 rmind {
331 1.42 rmind npf_rule_t *rg, *rlast = NULL, *prev = NULL, *lastprev = NULL;
332 1.1 rmind
333 1.17 rmind KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
334 1.17 rmind
335 1.17 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
336 1.19 rmind return ESRCH;
337 1.17 rmind }
338 1.18 rmind
339 1.42 rmind /* Compare the key and find the last in the list. */
340 1.42 rmind for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
341 1.24 rmind KASSERT(rl->r_parent == rg);
342 1.42 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
343 1.17 rmind if (memcmp(rl->r_key, key, len) == 0) {
344 1.42 rmind lastprev = prev;
345 1.42 rmind rlast = rl;
346 1.17 rmind }
347 1.42 rmind prev = rl;
348 1.42 rmind }
349 1.42 rmind if (!rlast) {
350 1.42 rmind return ENOENT;
351 1.1 rmind }
352 1.42 rmind npf_ruleset_unlink(rlast, lastprev);
353 1.42 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rlast, r_aentry);
354 1.42 rmind return 0;
355 1.18 rmind }
356 1.18 rmind
357 1.39 rmind /*
358 1.39 rmind * npf_ruleset_list: serialise and return the dynamic rules.
359 1.39 rmind */
360 1.46.4.1 christos nvlist_t *
361 1.43 christos npf_ruleset_list(npf_t *npf, npf_ruleset_t *rlset, const char *rname)
362 1.18 rmind {
363 1.46.4.1 christos nvlist_t *rgroup;
364 1.42 rmind npf_rule_t *rg;
365 1.18 rmind
366 1.43 christos KASSERT(npf_config_locked_p(npf));
367 1.36 rmind
368 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
369 1.18 rmind return NULL;
370 1.18 rmind }
371 1.46.4.1 christos if ((rgroup = nvlist_create(0)) == NULL) {
372 1.18 rmind return NULL;
373 1.18 rmind }
374 1.42 rmind for (npf_rule_t *rl = rg->r_subset; rl; rl = rl->r_next) {
375 1.46.4.1 christos nvlist_t *rule;
376 1.36 rmind
377 1.24 rmind KASSERT(rl->r_parent == rg);
378 1.42 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
379 1.36 rmind
380 1.46.4.1 christos rule = npf_rule_export(npf, rl);
381 1.46.4.1 christos if (!rule) {
382 1.46.4.1 christos nvlist_destroy(rgroup);
383 1.18 rmind return NULL;
384 1.18 rmind }
385 1.46.4.1 christos nvlist_append_nvlist_array(rgroup, "rules", rule);
386 1.46.4.1 christos nvlist_destroy(rule);
387 1.18 rmind }
388 1.46.4.1 christos return rgroup;
389 1.18 rmind }
390 1.18 rmind
391 1.39 rmind /*
392 1.39 rmind * npf_ruleset_flush: flush the dynamic rules in the ruleset by inserting
393 1.39 rmind * them into the G/C list.
394 1.39 rmind */
395 1.18 rmind int
396 1.18 rmind npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
397 1.18 rmind {
398 1.18 rmind npf_rule_t *rg, *rl;
399 1.18 rmind
400 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
401 1.19 rmind return ESRCH;
402 1.18 rmind }
403 1.42 rmind
404 1.42 rmind rl = atomic_swap_ptr(&rg->r_subset, NULL);
405 1.42 rmind membar_producer();
406 1.42 rmind
407 1.42 rmind while (rl) {
408 1.42 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
409 1.24 rmind KASSERT(rl->r_parent == rg);
410 1.42 rmind
411 1.42 rmind LIST_REMOVE(rl, r_aentry);
412 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
413 1.42 rmind rl = rl->r_next;
414 1.18 rmind }
415 1.45 christos rlset->rs_idcnt = 0;
416 1.18 rmind return 0;
417 1.18 rmind }
418 1.18 rmind
419 1.39 rmind /*
420 1.39 rmind * npf_ruleset_gc: destroy the rules in G/C list.
421 1.39 rmind */
422 1.39 rmind void
423 1.39 rmind npf_ruleset_gc(npf_ruleset_t *rlset)
424 1.39 rmind {
425 1.39 rmind npf_rule_t *rl;
426 1.39 rmind
427 1.39 rmind while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
428 1.39 rmind LIST_REMOVE(rl, r_aentry);
429 1.39 rmind npf_rule_free(rl);
430 1.39 rmind }
431 1.39 rmind }
432 1.39 rmind
433 1.39 rmind /*
434 1.39 rmind * npf_ruleset_export: serialise and return the static rules.
435 1.39 rmind */
436 1.36 rmind int
437 1.46.4.1 christos npf_ruleset_export(npf_t *npf, const npf_ruleset_t *rlset,
438 1.46.4.1 christos const char *key, nvlist_t *npf_dict)
439 1.36 rmind {
440 1.46.4.1 christos const unsigned nitems = rlset->rs_nitems;
441 1.46.4.1 christos unsigned n = 0;
442 1.36 rmind int error = 0;
443 1.36 rmind
444 1.43 christos KASSERT(npf_config_locked_p(npf));
445 1.36 rmind
446 1.37 rmind while (n < nitems) {
447 1.37 rmind const npf_rule_t *rl = rlset->rs_rules[n];
448 1.36 rmind const npf_natpolicy_t *natp = rl->r_natp;
449 1.46.4.1 christos nvlist_t *rule;
450 1.36 rmind
451 1.46.4.1 christos rule = npf_rule_export(npf, rl);
452 1.46.4.1 christos if (!rule) {
453 1.46.4.1 christos error = ENOMEM;
454 1.36 rmind break;
455 1.36 rmind }
456 1.46.4.1 christos if (natp && (error = npf_nat_policyexport(natp, rule)) != 0) {
457 1.46.4.1 christos nvlist_destroy(rule);
458 1.36 rmind break;
459 1.36 rmind }
460 1.46.4.1 christos nvlist_append_nvlist_array(npf_dict, key, rule);
461 1.46.4.1 christos nvlist_destroy(rule);
462 1.37 rmind n++;
463 1.36 rmind }
464 1.36 rmind return error;
465 1.36 rmind }
466 1.36 rmind
467 1.17 rmind /*
468 1.31 rmind * npf_ruleset_reload: prepare the new ruleset by scanning the active
469 1.39 rmind * ruleset and: 1) sharing the dynamic rules 2) sharing NAT policies.
470 1.17 rmind *
471 1.31 rmind * => The active (old) ruleset should be exclusively locked.
472 1.17 rmind */
473 1.17 rmind void
474 1.43 christos npf_ruleset_reload(npf_t *npf, npf_ruleset_t *newset,
475 1.43 christos npf_ruleset_t *oldset, bool load)
476 1.17 rmind {
477 1.31 rmind npf_rule_t *rg, *rl;
478 1.35 rmind uint64_t nid = 0;
479 1.17 rmind
480 1.43 christos KASSERT(npf_config_locked_p(npf));
481 1.17 rmind
482 1.31 rmind /*
483 1.31 rmind * Scan the dynamic rules and share (migrate) if needed.
484 1.31 rmind */
485 1.31 rmind LIST_FOREACH(rg, &newset->rs_dynamic, r_dentry) {
486 1.42 rmind npf_rule_t *active_rgroup;
487 1.18 rmind
488 1.31 rmind /* Look for a dynamic ruleset group with such name. */
489 1.42 rmind active_rgroup = npf_ruleset_lookup(oldset, rg->r_name);
490 1.42 rmind if (active_rgroup == NULL) {
491 1.17 rmind continue;
492 1.17 rmind }
493 1.18 rmind
494 1.18 rmind /*
495 1.42 rmind * ATOMICITY: Copy the head pointer of the linked-list,
496 1.42 rmind * but do not remove the rules from the active r_subset.
497 1.42 rmind * This is necessary because the rules are still active
498 1.42 rmind * and therefore are accessible for inspection via the
499 1.42 rmind * old ruleset.
500 1.18 rmind */
501 1.42 rmind rg->r_subset = active_rgroup->r_subset;
502 1.42 rmind
503 1.42 rmind /*
504 1.42 rmind * We can safely migrate to the new all-rule list and
505 1.42 rmind * reset the parent rule, though.
506 1.42 rmind */
507 1.42 rmind for (rl = rg->r_subset; rl; rl = rl->r_next) {
508 1.42 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
509 1.18 rmind LIST_REMOVE(rl, r_aentry);
510 1.31 rmind LIST_INSERT_HEAD(&newset->rs_all, rl, r_aentry);
511 1.42 rmind
512 1.42 rmind KASSERT(rl->r_parent == active_rgroup);
513 1.19 rmind rl->r_parent = rg;
514 1.18 rmind }
515 1.1 rmind }
516 1.19 rmind
517 1.31 rmind /*
518 1.40 rmind * If performing the load of connections then NAT policies may
519 1.40 rmind * already have translated connections associated with them and
520 1.40 rmind * we should not share or inherit anything.
521 1.40 rmind */
522 1.40 rmind if (load)
523 1.40 rmind return;
524 1.40 rmind
525 1.40 rmind /*
526 1.31 rmind * Scan all rules in the new ruleset and share NAT policies.
527 1.35 rmind * Also, assign a unique ID for each policy here.
528 1.31 rmind */
529 1.31 rmind LIST_FOREACH(rl, &newset->rs_all, r_aentry) {
530 1.31 rmind npf_natpolicy_t *np;
531 1.31 rmind npf_rule_t *actrl;
532 1.31 rmind
533 1.31 rmind /* Does the rule have a NAT policy associated? */
534 1.31 rmind if ((np = rl->r_natp) == NULL) {
535 1.31 rmind continue;
536 1.31 rmind }
537 1.35 rmind
538 1.38 rmind /*
539 1.38 rmind * First, try to share the active port map. If this
540 1.38 rmind * policy will be unused, npf_nat_freepolicy() will
541 1.38 rmind * drop the reference.
542 1.38 rmind */
543 1.38 rmind npf_ruleset_sharepm(oldset, np);
544 1.38 rmind
545 1.31 rmind /* Does it match with any policy in the active ruleset? */
546 1.38 rmind LIST_FOREACH(actrl, &oldset->rs_all, r_aentry) {
547 1.38 rmind if (!actrl->r_natp)
548 1.38 rmind continue;
549 1.38 rmind if ((actrl->r_attr & NPF_RULE_KEEPNAT) != 0)
550 1.38 rmind continue;
551 1.38 rmind if (npf_nat_cmppolicy(actrl->r_natp, np))
552 1.38 rmind break;
553 1.38 rmind }
554 1.38 rmind if (!actrl) {
555 1.38 rmind /* No: just set the ID and continue. */
556 1.35 rmind npf_nat_setid(np, ++nid);
557 1.31 rmind continue;
558 1.31 rmind }
559 1.31 rmind
560 1.38 rmind /* Yes: inherit the matching NAT policy. */
561 1.31 rmind rl->r_natp = actrl->r_natp;
562 1.35 rmind npf_nat_setid(rl->r_natp, ++nid);
563 1.31 rmind
564 1.31 rmind /*
565 1.31 rmind * Finally, mark the active rule to not destroy its NAT
566 1.31 rmind * policy later as we inherited it (but the rule must be
567 1.31 rmind * kept active for now). Destroy the new/unused policy.
568 1.31 rmind */
569 1.31 rmind actrl->r_attr |= NPF_RULE_KEEPNAT;
570 1.31 rmind npf_nat_freepolicy(np);
571 1.31 rmind }
572 1.31 rmind
573 1.19 rmind /* Inherit the ID counter. */
574 1.31 rmind newset->rs_idcnt = oldset->rs_idcnt;
575 1.1 rmind }
576 1.1 rmind
577 1.39 rmind /*
578 1.39 rmind * npf_ruleset_sharepm: attempt to share the active NAT portmap.
579 1.39 rmind */
580 1.6 rmind npf_rule_t *
581 1.6 rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
582 1.6 rmind {
583 1.6 rmind npf_natpolicy_t *np;
584 1.6 rmind npf_rule_t *rl;
585 1.6 rmind
586 1.39 rmind /*
587 1.39 rmind * Scan the NAT policies in the ruleset and match with the
588 1.39 rmind * given policy based on the translation IP address. If they
589 1.39 rmind * match - adjust the given NAT policy to use the active NAT
590 1.39 rmind * portmap. In such case the reference on the old portmap is
591 1.39 rmind * dropped and acquired on the active one.
592 1.39 rmind */
593 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
594 1.6 rmind np = rl->r_natp;
595 1.6 rmind if (np == NULL || np == mnp)
596 1.6 rmind continue;
597 1.6 rmind if (npf_nat_sharepm(np, mnp))
598 1.6 rmind break;
599 1.6 rmind }
600 1.6 rmind return rl;
601 1.6 rmind }
602 1.6 rmind
603 1.35 rmind npf_natpolicy_t *
604 1.35 rmind npf_ruleset_findnat(npf_ruleset_t *rlset, uint64_t id)
605 1.35 rmind {
606 1.35 rmind npf_rule_t *rl;
607 1.35 rmind
608 1.35 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
609 1.35 rmind npf_natpolicy_t *np = rl->r_natp;
610 1.35 rmind if (np && npf_nat_getid(np) == id) {
611 1.35 rmind return np;
612 1.35 rmind }
613 1.35 rmind }
614 1.35 rmind return NULL;
615 1.35 rmind }
616 1.35 rmind
617 1.1 rmind /*
618 1.13 rmind * npf_ruleset_freealg: inspect the ruleset and disassociate specified
619 1.13 rmind * ALG from all NAT entries using it.
620 1.13 rmind */
621 1.13 rmind void
622 1.13 rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
623 1.13 rmind {
624 1.13 rmind npf_rule_t *rl;
625 1.17 rmind npf_natpolicy_t *np;
626 1.13 rmind
627 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
628 1.17 rmind if ((np = rl->r_natp) != NULL) {
629 1.13 rmind npf_nat_freealg(np, alg);
630 1.13 rmind }
631 1.13 rmind }
632 1.13 rmind }
633 1.13 rmind
634 1.13 rmind /*
635 1.25 rmind * npf_rule_alloc: allocate a rule and initialise it.
636 1.1 rmind */
637 1.4 rmind npf_rule_t *
638 1.46.4.1 christos npf_rule_alloc(npf_t *npf, const nvlist_t *rule)
639 1.1 rmind {
640 1.4 rmind npf_rule_t *rl;
641 1.7 rmind const char *rname;
642 1.46.4.1 christos const void *key, *info;
643 1.46.4.1 christos size_t len;
644 1.1 rmind
645 1.46.4.1 christos /* Allocate a rule structure and keep the information. */
646 1.11 rmind rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
647 1.46.4.1 christos info = dnvlist_get_binary(rule, "info", &rl->r_info_len, NULL, 0);
648 1.46.4.1 christos if (info) {
649 1.46.4.1 christos rl->r_info = kmem_alloc(rl->r_info_len, KM_SLEEP);
650 1.46.4.1 christos memcpy(rl->r_info, info, rl->r_info_len);
651 1.46.4.1 christos }
652 1.4 rmind rl->r_natp = NULL;
653 1.4 rmind
654 1.11 rmind /* Name (optional) */
655 1.46.4.1 christos if ((rname = dnvlist_get_string(rule, "name", NULL)) != NULL) {
656 1.17 rmind strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
657 1.7 rmind } else {
658 1.7 rmind rl->r_name[0] = '\0';
659 1.7 rmind }
660 1.7 rmind
661 1.11 rmind /* Attributes, priority and interface ID (optional). */
662 1.46.4.1 christos rl->r_attr = dnvlist_get_number(rule, "attr", 0);
663 1.31 rmind rl->r_attr &= ~NPF_RULE_PRIVMASK;
664 1.26 rmind
665 1.42 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
666 1.42 rmind /* Priority of the dynamic rule. */
667 1.46.4.1 christos rl->r_priority = dnvlist_get_number(rule, "prio", 0);
668 1.42 rmind } else {
669 1.42 rmind /* The skip-to index. No need to validate it. */
670 1.46.4.1 christos rl->r_skip_to = dnvlist_get_number(rule, "skip-to", 0);
671 1.42 rmind }
672 1.42 rmind
673 1.42 rmind /* Interface name; register and get the npf-if-id. */
674 1.46.4.1 christos if ((rname = dnvlist_get_string(rule, "ifname", NULL)) != NULL) {
675 1.43 christos if ((rl->r_ifid = npf_ifmap_register(npf, rname)) == 0) {
676 1.26 rmind kmem_free(rl, sizeof(npf_rule_t));
677 1.26 rmind return NULL;
678 1.26 rmind }
679 1.26 rmind } else {
680 1.26 rmind rl->r_ifid = 0;
681 1.26 rmind }
682 1.4 rmind
683 1.17 rmind /* Key (optional). */
684 1.46.4.1 christos if ((key = dnvlist_get_binary(rule, "key", &len, NULL, 0)) != NULL) {
685 1.17 rmind if (len > NPF_RULE_MAXKEYLEN) {
686 1.17 rmind kmem_free(rl, sizeof(npf_rule_t));
687 1.17 rmind return NULL;
688 1.17 rmind }
689 1.17 rmind memcpy(rl->r_key, key, len);
690 1.4 rmind }
691 1.36 rmind return rl;
692 1.36 rmind }
693 1.36 rmind
694 1.46.4.1 christos static nvlist_t *
695 1.46.4.1 christos npf_rule_export(npf_t *npf, const npf_rule_t *rl)
696 1.36 rmind {
697 1.46.4.1 christos nvlist_t *rule = nvlist_create(0);
698 1.46.4.1 christos unsigned skip_to = 0;
699 1.46.4.1 christos npf_rproc_t *rp;
700 1.36 rmind
701 1.46.4.1 christos nvlist_add_number(rule, "attr", rl->r_attr);
702 1.46.4.1 christos nvlist_add_number(rule, "prio", rl->r_priority);
703 1.37 rmind if ((rl->r_skip_to & SKIPTO_ADJ_FLAG) == 0) {
704 1.37 rmind skip_to = rl->r_skip_to & SKIPTO_MASK;
705 1.37 rmind }
706 1.46.4.1 christos nvlist_add_number(rule, "skip-to", skip_to);
707 1.46.4.1 christos nvlist_add_number(rule, "code-type", rl->r_type);
708 1.36 rmind if (rl->r_code) {
709 1.46.4.1 christos nvlist_add_binary(rule, "code", rl->r_code, rl->r_clen);
710 1.36 rmind }
711 1.36 rmind if (rl->r_ifid) {
712 1.43 christos const char *ifname = npf_ifmap_getname(npf, rl->r_ifid);
713 1.46.4.1 christos nvlist_add_string(rule, "ifname", ifname);
714 1.36 rmind }
715 1.46.4.1 christos nvlist_add_number(rule, "id", rl->r_id);
716 1.36 rmind
717 1.36 rmind if (rl->r_name[0]) {
718 1.46.4.1 christos nvlist_add_string(rule, "name", rl->r_name);
719 1.36 rmind }
720 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
721 1.46.4.1 christos nvlist_add_binary(rule, "key", rl->r_key, NPF_RULE_MAXKEYLEN);
722 1.18 rmind }
723 1.37 rmind if (rl->r_info) {
724 1.46.4.1 christos nvlist_add_binary(rule, "info", rl->r_info, rl->r_info_len);
725 1.37 rmind }
726 1.46.4.1 christos if ((rp = npf_rule_getrproc(rl)) != NULL) {
727 1.46.4.1 christos const char *rname = npf_rproc_getname(rp);
728 1.46.4.1 christos nvlist_add_string(rule, "rproc", rname);
729 1.44 christos npf_rproc_release(rp);
730 1.44 christos }
731 1.46.4.1 christos return rule;
732 1.17 rmind }
733 1.17 rmind
734 1.17 rmind /*
735 1.17 rmind * npf_rule_setcode: assign filter code to the rule.
736 1.17 rmind *
737 1.20 rmind * => The code must be validated by the caller.
738 1.20 rmind * => JIT compilation may be performed here.
739 1.17 rmind */
740 1.17 rmind void
741 1.17 rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
742 1.17 rmind {
743 1.25 rmind KASSERT(type == NPF_CODE_BPF);
744 1.28 rmind
745 1.28 rmind rl->r_type = type;
746 1.36 rmind rl->r_code = code;
747 1.36 rmind rl->r_clen = size;
748 1.36 rmind rl->r_jcode = npf_bpf_compile(code, size);
749 1.17 rmind }
750 1.17 rmind
751 1.17 rmind /*
752 1.17 rmind * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
753 1.17 rmind */
754 1.17 rmind void
755 1.17 rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
756 1.17 rmind {
757 1.17 rmind npf_rproc_acquire(rp);
758 1.6 rmind rl->r_rproc = rp;
759 1.1 rmind }
760 1.1 rmind
761 1.1 rmind /*
762 1.1 rmind * npf_rule_free: free the specified rule.
763 1.1 rmind */
764 1.1 rmind void
765 1.1 rmind npf_rule_free(npf_rule_t *rl)
766 1.1 rmind {
767 1.4 rmind npf_natpolicy_t *np = rl->r_natp;
768 1.4 rmind npf_rproc_t *rp = rl->r_rproc;
769 1.1 rmind
770 1.31 rmind if (np && (rl->r_attr & NPF_RULE_KEEPNAT) == 0) {
771 1.4 rmind /* Free NAT policy. */
772 1.4 rmind npf_nat_freepolicy(np);
773 1.4 rmind }
774 1.4 rmind if (rp) {
775 1.6 rmind /* Release rule procedure. */
776 1.4 rmind npf_rproc_release(rp);
777 1.4 rmind }
778 1.17 rmind if (rl->r_code) {
779 1.20 rmind /* Free byte-code. */
780 1.17 rmind kmem_free(rl->r_code, rl->r_clen);
781 1.1 rmind }
782 1.20 rmind if (rl->r_jcode) {
783 1.20 rmind /* Free JIT code. */
784 1.28 rmind bpf_jit_freecode(rl->r_jcode);
785 1.20 rmind }
786 1.36 rmind if (rl->r_info) {
787 1.46.4.1 christos kmem_free(rl->r_info, rl->r_info_len);
788 1.18 rmind }
789 1.4 rmind kmem_free(rl, sizeof(npf_rule_t));
790 1.1 rmind }
791 1.1 rmind
792 1.1 rmind /*
793 1.19 rmind * npf_rule_getid: return the unique ID of a rule.
794 1.10 rmind * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
795 1.1 rmind * npf_rule_getnat: get NAT policy assigned to the rule.
796 1.1 rmind */
797 1.1 rmind
798 1.19 rmind uint64_t
799 1.19 rmind npf_rule_getid(const npf_rule_t *rl)
800 1.19 rmind {
801 1.19 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
802 1.19 rmind return rl->r_id;
803 1.19 rmind }
804 1.19 rmind
805 1.10 rmind npf_rproc_t *
806 1.30 rmind npf_rule_getrproc(const npf_rule_t *rl)
807 1.10 rmind {
808 1.10 rmind npf_rproc_t *rp = rl->r_rproc;
809 1.10 rmind
810 1.10 rmind if (rp) {
811 1.10 rmind npf_rproc_acquire(rp);
812 1.10 rmind }
813 1.10 rmind return rp;
814 1.10 rmind }
815 1.10 rmind
816 1.1 rmind npf_natpolicy_t *
817 1.1 rmind npf_rule_getnat(const npf_rule_t *rl)
818 1.1 rmind {
819 1.4 rmind return rl->r_natp;
820 1.1 rmind }
821 1.1 rmind
822 1.4 rmind /*
823 1.4 rmind * npf_rule_setnat: assign NAT policy to the rule and insert into the
824 1.4 rmind * NAT policy list in the ruleset.
825 1.4 rmind */
826 1.1 rmind void
827 1.1 rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
828 1.1 rmind {
829 1.4 rmind KASSERT(rl->r_natp == NULL);
830 1.4 rmind rl->r_natp = np;
831 1.1 rmind }
832 1.1 rmind
833 1.17 rmind /*
834 1.17 rmind * npf_rule_inspect: match the interface, direction and run the filter code.
835 1.29 rmind * Returns true if rule matches and false otherwise.
836 1.17 rmind */
837 1.17 rmind static inline bool
838 1.29 rmind npf_rule_inspect(const npf_rule_t *rl, bpf_args_t *bc_args,
839 1.29 rmind const int di_mask, const u_int ifid)
840 1.17 rmind {
841 1.17 rmind /* Match the interface. */
842 1.29 rmind if (rl->r_ifid && rl->r_ifid != ifid) {
843 1.17 rmind return false;
844 1.17 rmind }
845 1.17 rmind
846 1.17 rmind /* Match the direction. */
847 1.17 rmind if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
848 1.17 rmind if ((rl->r_attr & di_mask) == 0)
849 1.17 rmind return false;
850 1.17 rmind }
851 1.17 rmind
852 1.24 rmind /* Any code? */
853 1.36 rmind if (!rl->r_code) {
854 1.24 rmind KASSERT(rl->r_jcode == NULL);
855 1.17 rmind return true;
856 1.17 rmind }
857 1.25 rmind KASSERT(rl->r_type == NPF_CODE_BPF);
858 1.29 rmind return npf_bpf_filter(bc_args, rl->r_code, rl->r_jcode) != 0;
859 1.17 rmind }
860 1.17 rmind
861 1.17 rmind /*
862 1.17 rmind * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
863 1.17 rmind * This is only for the dynamic rules. Subrules cannot have nested rules.
864 1.17 rmind */
865 1.42 rmind static inline npf_rule_t *
866 1.42 rmind npf_rule_reinspect(const npf_rule_t *rg, bpf_args_t *bc_args,
867 1.29 rmind const int di_mask, const u_int ifid)
868 1.7 rmind {
869 1.17 rmind npf_rule_t *final_rl = NULL, *rl;
870 1.17 rmind
871 1.42 rmind KASSERT(NPF_DYNAMIC_GROUP_P(rg->r_attr));
872 1.7 rmind
873 1.42 rmind for (rl = rg->r_subset; rl; rl = rl->r_next) {
874 1.42 rmind KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
875 1.29 rmind if (!npf_rule_inspect(rl, bc_args, di_mask, ifid)) {
876 1.7 rmind continue;
877 1.17 rmind }
878 1.17 rmind if (rl->r_attr & NPF_RULE_FINAL) {
879 1.17 rmind return rl;
880 1.17 rmind }
881 1.17 rmind final_rl = rl;
882 1.7 rmind }
883 1.17 rmind return final_rl;
884 1.7 rmind }
885 1.1 rmind
886 1.1 rmind /*
887 1.7 rmind * npf_ruleset_inspect: inspect the packet against the given ruleset.
888 1.1 rmind *
889 1.25 rmind * Loop through the rules in the set and run the byte-code of each rule
890 1.7 rmind * against the packet (nbuf chain). If sub-ruleset is found, inspect it.
891 1.1 rmind */
892 1.1 rmind npf_rule_t *
893 1.34 rmind npf_ruleset_inspect(npf_cache_t *npc, const npf_ruleset_t *rlset,
894 1.34 rmind const int di, const int layer)
895 1.1 rmind {
896 1.34 rmind nbuf_t *nbuf = npc->npc_nbuf;
897 1.7 rmind const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
898 1.17 rmind const u_int nitems = rlset->rs_nitems;
899 1.29 rmind const u_int ifid = nbuf->nb_ifid;
900 1.17 rmind npf_rule_t *final_rl = NULL;
901 1.29 rmind bpf_args_t bc_args;
902 1.17 rmind u_int n = 0;
903 1.1 rmind
904 1.33 rmind KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
905 1.29 rmind
906 1.33 rmind /*
907 1.33 rmind * Prepare the external memory store and the arguments for
908 1.43 christos * the BPF programs to be executed. Reset mbuf before taking
909 1.43 christos * any pointers for the BPF.
910 1.33 rmind */
911 1.33 rmind uint32_t bc_words[NPF_BPF_NWORDS];
912 1.43 christos
913 1.43 christos nbuf_reset(nbuf);
914 1.34 rmind npf_bpf_prepare(npc, &bc_args, bc_words);
915 1.17 rmind
916 1.17 rmind while (n < nitems) {
917 1.17 rmind npf_rule_t *rl = rlset->rs_rules[n];
918 1.37 rmind const u_int skip_to = rl->r_skip_to & SKIPTO_MASK;
919 1.17 rmind const uint32_t attr = rl->r_attr;
920 1.17 rmind
921 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
922 1.17 rmind KASSERT(n < skip_to);
923 1.1 rmind
924 1.17 rmind /* Group is a barrier: return a matching if found any. */
925 1.17 rmind if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
926 1.17 rmind break;
927 1.17 rmind }
928 1.17 rmind
929 1.17 rmind /* Main inspection of the rule. */
930 1.29 rmind if (!npf_rule_inspect(rl, &bc_args, di_mask, ifid)) {
931 1.17 rmind n = skip_to;
932 1.1 rmind continue;
933 1.1 rmind }
934 1.17 rmind
935 1.17 rmind if (NPF_DYNAMIC_GROUP_P(attr)) {
936 1.17 rmind /*
937 1.17 rmind * If this is a dynamic rule, re-inspect the subrules.
938 1.17 rmind * If it has any matching rule, then it is final.
939 1.17 rmind */
940 1.29 rmind rl = npf_rule_reinspect(rl, &bc_args, di_mask, ifid);
941 1.17 rmind if (rl != NULL) {
942 1.17 rmind final_rl = rl;
943 1.17 rmind break;
944 1.17 rmind }
945 1.17 rmind } else if ((attr & NPF_RULE_GROUP) == 0) {
946 1.17 rmind /*
947 1.17 rmind * Groups themselves are not matching.
948 1.17 rmind */
949 1.17 rmind final_rl = rl;
950 1.1 rmind }
951 1.17 rmind
952 1.1 rmind /* Set the matching rule and check for "final". */
953 1.17 rmind if (attr & NPF_RULE_FINAL) {
954 1.2 rmind break;
955 1.1 rmind }
956 1.17 rmind n++;
957 1.2 rmind }
958 1.16 rmind
959 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
960 1.7 rmind return final_rl;
961 1.1 rmind }
962 1.1 rmind
963 1.1 rmind /*
964 1.17 rmind * npf_rule_conclude: return decision and the flags for conclusion.
965 1.1 rmind *
966 1.1 rmind * => Returns ENETUNREACH if "block" and 0 if "pass".
967 1.1 rmind */
968 1.1 rmind int
969 1.45 christos npf_rule_conclude(const npf_rule_t *rl, npf_match_info_t *mi)
970 1.1 rmind {
971 1.1 rmind /* If not passing - drop the packet. */
972 1.45 christos mi->mi_retfl = rl->r_attr;
973 1.45 christos mi->mi_rid = rl->r_id;
974 1.17 rmind return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
975 1.1 rmind }
976 1.41 rmind
977 1.41 rmind
978 1.41 rmind #if defined(DDB) || defined(_NPF_TESTING)
979 1.41 rmind
980 1.41 rmind void
981 1.43 christos npf_ruleset_dump(npf_t *npf, const char *name)
982 1.41 rmind {
983 1.43 christos npf_ruleset_t *rlset = npf_config_ruleset(npf);
984 1.41 rmind npf_rule_t *rg, *rl;
985 1.41 rmind
986 1.41 rmind LIST_FOREACH(rg, &rlset->rs_dynamic, r_dentry) {
987 1.41 rmind printf("ruleset '%s':\n", rg->r_name);
988 1.42 rmind for (rl = rg->r_subset; rl; rl = rl->r_next) {
989 1.41 rmind printf("\tid %"PRIu64", key: ", rl->r_id);
990 1.41 rmind for (u_int i = 0; i < NPF_RULE_MAXKEYLEN; i++)
991 1.41 rmind printf("%x", rl->r_key[i]);
992 1.41 rmind printf("\n");
993 1.41 rmind }
994 1.41 rmind }
995 1.41 rmind }
996 1.41 rmind
997 1.41 rmind #endif
998