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