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