npf_ruleset.c revision 1.38 1 1.38 rmind /* $NetBSD: npf_ruleset.c,v 1.38 2014/11/26 21:25:35 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.17 rmind * Copyright (c) 2009-2013 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.1 rmind #include <sys/cdefs.h>
37 1.38 rmind __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.38 2014/11/26 21:25:35 rmind Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.11 rmind #include <sys/types.h>
41 1.1 rmind
42 1.20 rmind #include <sys/atomic.h>
43 1.1 rmind #include <sys/kmem.h>
44 1.1 rmind #include <sys/queue.h>
45 1.17 rmind #include <sys/mbuf.h>
46 1.1 rmind #include <sys/types.h>
47 1.1 rmind
48 1.17 rmind #include <net/bpf.h>
49 1.20 rmind #include <net/bpfjit.h>
50 1.3 rmind #include <net/pfil.h>
51 1.1 rmind #include <net/if.h>
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.17 rmind /* Rule priority: (highest) 1, 2 ... n (lowest). */
93 1.17 rmind pri_t r_priority;
94 1.17 rmind
95 1.17 rmind /*
96 1.17 rmind * Dynamic group: subset queue and a dynamic group list entry.
97 1.17 rmind * Dynamic rule: entry and the parent rule (the group).
98 1.17 rmind */
99 1.17 rmind union {
100 1.17 rmind TAILQ_HEAD(npf_ruleq, npf_rule) r_subset;
101 1.17 rmind TAILQ_ENTRY(npf_rule) r_entry;
102 1.17 rmind } /* C11 */;
103 1.17 rmind union {
104 1.17 rmind LIST_ENTRY(npf_rule) r_dentry;
105 1.17 rmind npf_rule_t * r_parent;
106 1.17 rmind } /* C11 */;
107 1.17 rmind
108 1.36 rmind /* Rule ID, name and the optional key. */
109 1.19 rmind uint64_t r_id;
110 1.36 rmind char r_name[NPF_RULE_MAXNAMELEN];
111 1.36 rmind uint8_t r_key[NPF_RULE_MAXKEYLEN];
112 1.18 rmind
113 1.36 rmind /* All-list entry and the auxiliary info. */
114 1.17 rmind LIST_ENTRY(npf_rule) r_aentry;
115 1.36 rmind prop_data_t r_info;
116 1.36 rmind };
117 1.17 rmind
118 1.37 rmind #define SKIPTO_ADJ_FLAG (1U << 31)
119 1.37 rmind #define SKIPTO_MASK (SKIPTO_ADJ_FLAG - 1)
120 1.37 rmind
121 1.37 rmind static int npf_rule_export(const npf_ruleset_t *,
122 1.37 rmind const npf_rule_t *, prop_dictionary_t);
123 1.1 rmind
124 1.31 rmind /*
125 1.31 rmind * Private attributes - must be in the NPF_RULE_PRIVMASK range.
126 1.31 rmind */
127 1.31 rmind #define NPF_RULE_KEEPNAT (0x01000000 & NPF_RULE_PRIVMASK)
128 1.31 rmind
129 1.17 rmind #define NPF_DYNAMIC_GROUP_P(attr) \
130 1.17 rmind (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
131 1.17 rmind
132 1.19 rmind #define NPF_DYNAMIC_RULE_P(attr) \
133 1.19 rmind (((attr) & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC)
134 1.19 rmind
135 1.1 rmind npf_ruleset_t *
136 1.17 rmind npf_ruleset_create(size_t slots)
137 1.1 rmind {
138 1.17 rmind size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
139 1.1 rmind npf_ruleset_t *rlset;
140 1.1 rmind
141 1.17 rmind rlset = kmem_zalloc(len, KM_SLEEP);
142 1.17 rmind LIST_INIT(&rlset->rs_dynamic);
143 1.17 rmind LIST_INIT(&rlset->rs_all);
144 1.19 rmind LIST_INIT(&rlset->rs_gc);
145 1.19 rmind rlset->rs_slots = slots;
146 1.19 rmind
147 1.1 rmind return rlset;
148 1.1 rmind }
149 1.1 rmind
150 1.17 rmind static void
151 1.17 rmind npf_ruleset_unlink(npf_ruleset_t *rlset, npf_rule_t *rl)
152 1.17 rmind {
153 1.17 rmind if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
154 1.17 rmind LIST_REMOVE(rl, r_dentry);
155 1.17 rmind }
156 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
157 1.17 rmind npf_rule_t *rg = rl->r_parent;
158 1.17 rmind TAILQ_REMOVE(&rg->r_subset, rl, r_entry);
159 1.17 rmind }
160 1.17 rmind LIST_REMOVE(rl, r_aentry);
161 1.17 rmind }
162 1.17 rmind
163 1.1 rmind void
164 1.1 rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
165 1.1 rmind {
166 1.17 rmind size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
167 1.1 rmind npf_rule_t *rl;
168 1.1 rmind
169 1.17 rmind while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
170 1.17 rmind npf_ruleset_unlink(rlset, rl);
171 1.1 rmind npf_rule_free(rl);
172 1.1 rmind }
173 1.17 rmind KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
174 1.18 rmind KASSERT(LIST_EMPTY(&rlset->rs_gc));
175 1.17 rmind kmem_free(rlset, len);
176 1.1 rmind }
177 1.1 rmind
178 1.1 rmind /*
179 1.1 rmind * npf_ruleset_insert: insert the rule into the specified ruleset.
180 1.1 rmind */
181 1.1 rmind void
182 1.1 rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
183 1.1 rmind {
184 1.17 rmind u_int n = rlset->rs_nitems;
185 1.17 rmind
186 1.17 rmind KASSERT(n < rlset->rs_slots);
187 1.17 rmind
188 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
189 1.17 rmind if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
190 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
191 1.24 rmind } else {
192 1.24 rmind KASSERTMSG(rl->r_parent == NULL, "cannot be dynamic rule");
193 1.24 rmind rl->r_attr &= ~NPF_RULE_DYNAMIC;
194 1.17 rmind }
195 1.17 rmind
196 1.17 rmind rlset->rs_rules[n] = rl;
197 1.17 rmind rlset->rs_nitems++;
198 1.17 rmind
199 1.17 rmind if (rl->r_skip_to < ++n) {
200 1.37 rmind rl->r_skip_to = SKIPTO_ADJ_FLAG | n;
201 1.17 rmind }
202 1.17 rmind }
203 1.17 rmind
204 1.17 rmind static npf_rule_t *
205 1.17 rmind npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
206 1.17 rmind {
207 1.17 rmind npf_rule_t *rl;
208 1.17 rmind
209 1.17 rmind KASSERT(npf_config_locked_p());
210 1.17 rmind
211 1.17 rmind LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
212 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
213 1.17 rmind if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
214 1.17 rmind break;
215 1.17 rmind }
216 1.17 rmind return rl;
217 1.17 rmind }
218 1.17 rmind
219 1.17 rmind int
220 1.17 rmind npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
221 1.17 rmind {
222 1.17 rmind npf_rule_t *rg, *it;
223 1.17 rmind pri_t priocmd;
224 1.17 rmind
225 1.17 rmind rg = npf_ruleset_lookup(rlset, rname);
226 1.17 rmind if (rg == NULL) {
227 1.19 rmind return ESRCH;
228 1.19 rmind }
229 1.19 rmind if (!NPF_DYNAMIC_RULE_P(rl->r_attr)) {
230 1.19 rmind return EINVAL;
231 1.17 rmind }
232 1.17 rmind
233 1.19 rmind /* Dynamic rule - assign a unique ID and save the parent. */
234 1.19 rmind rl->r_id = ++rlset->rs_idcnt;
235 1.17 rmind rl->r_parent = rg;
236 1.17 rmind
237 1.17 rmind /*
238 1.17 rmind * Rule priority: (highest) 1, 2 ... n (lowest).
239 1.17 rmind * Negative priority indicates an operation and is reset to zero.
240 1.17 rmind */
241 1.17 rmind if ((priocmd = rl->r_priority) < 0) {
242 1.17 rmind rl->r_priority = 0;
243 1.17 rmind }
244 1.17 rmind
245 1.17 rmind switch (priocmd) {
246 1.17 rmind case NPF_PRI_FIRST:
247 1.17 rmind TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
248 1.17 rmind if (rl->r_priority <= it->r_priority)
249 1.17 rmind break;
250 1.17 rmind }
251 1.17 rmind if (it) {
252 1.17 rmind TAILQ_INSERT_BEFORE(it, rl, r_entry);
253 1.17 rmind } else {
254 1.17 rmind TAILQ_INSERT_HEAD(&rg->r_subset, rl, r_entry);
255 1.17 rmind }
256 1.17 rmind break;
257 1.17 rmind case NPF_PRI_LAST:
258 1.17 rmind default:
259 1.17 rmind TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
260 1.17 rmind if (rl->r_priority < it->r_priority)
261 1.17 rmind break;
262 1.17 rmind }
263 1.17 rmind if (it) {
264 1.17 rmind TAILQ_INSERT_BEFORE(it, rl, r_entry);
265 1.17 rmind } else {
266 1.17 rmind TAILQ_INSERT_TAIL(&rg->r_subset, rl, r_entry);
267 1.17 rmind }
268 1.17 rmind break;
269 1.17 rmind }
270 1.17 rmind
271 1.17 rmind /* Finally, add into the all-list. */
272 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
273 1.17 rmind return 0;
274 1.17 rmind }
275 1.17 rmind
276 1.18 rmind int
277 1.19 rmind npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uint64_t id)
278 1.17 rmind {
279 1.17 rmind npf_rule_t *rg, *rl;
280 1.17 rmind
281 1.17 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
282 1.19 rmind return ESRCH;
283 1.17 rmind }
284 1.17 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
285 1.24 rmind KASSERT(rl->r_parent == rg);
286 1.24 rmind
287 1.17 rmind /* Compare ID. On match, remove and return. */
288 1.19 rmind if (rl->r_id == id) {
289 1.17 rmind npf_ruleset_unlink(rlset, rl);
290 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
291 1.19 rmind return 0;
292 1.17 rmind }
293 1.17 rmind }
294 1.19 rmind return ENOENT;
295 1.17 rmind }
296 1.17 rmind
297 1.18 rmind int
298 1.17 rmind npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
299 1.17 rmind const void *key, size_t len)
300 1.17 rmind {
301 1.17 rmind npf_rule_t *rg, *rl;
302 1.1 rmind
303 1.17 rmind KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
304 1.17 rmind
305 1.17 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
306 1.19 rmind return ESRCH;
307 1.17 rmind }
308 1.18 rmind
309 1.17 rmind /* Find the last in the list. */
310 1.17 rmind TAILQ_FOREACH_REVERSE(rl, &rg->r_subset, npf_ruleq, r_entry) {
311 1.24 rmind KASSERT(rl->r_parent == rg);
312 1.24 rmind
313 1.17 rmind /* Compare the key. On match, remove and return. */
314 1.17 rmind if (memcmp(rl->r_key, key, len) == 0) {
315 1.17 rmind npf_ruleset_unlink(rlset, rl);
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.1 rmind }
320 1.19 rmind return ENOENT;
321 1.18 rmind }
322 1.18 rmind
323 1.18 rmind prop_dictionary_t
324 1.18 rmind npf_ruleset_list(npf_ruleset_t *rlset, const char *rname)
325 1.18 rmind {
326 1.36 rmind prop_dictionary_t rgdict;
327 1.18 rmind prop_array_t rules;
328 1.18 rmind npf_rule_t *rg, *rl;
329 1.18 rmind
330 1.36 rmind KASSERT(npf_config_locked_p());
331 1.36 rmind
332 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
333 1.18 rmind return NULL;
334 1.18 rmind }
335 1.36 rmind if ((rgdict = prop_dictionary_create()) == NULL) {
336 1.18 rmind return NULL;
337 1.18 rmind }
338 1.18 rmind if ((rules = prop_array_create()) == NULL) {
339 1.36 rmind prop_object_release(rgdict);
340 1.18 rmind return NULL;
341 1.18 rmind }
342 1.18 rmind
343 1.18 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
344 1.36 rmind prop_dictionary_t rldict;
345 1.36 rmind
346 1.36 rmind rldict = prop_dictionary_create();
347 1.24 rmind KASSERT(rl->r_parent == rg);
348 1.36 rmind
349 1.37 rmind if (npf_rule_export(rlset, rl, rldict)) {
350 1.18 rmind prop_object_release(rldict);
351 1.19 rmind prop_object_release(rules);
352 1.18 rmind return NULL;
353 1.18 rmind }
354 1.37 rmind prop_array_add(rules, rldict);
355 1.37 rmind prop_object_release(rldict);
356 1.18 rmind }
357 1.19 rmind
358 1.36 rmind if (!prop_dictionary_set(rgdict, "rules", rules)) {
359 1.36 rmind prop_object_release(rgdict);
360 1.36 rmind rgdict = NULL;
361 1.18 rmind }
362 1.18 rmind prop_object_release(rules);
363 1.36 rmind return rgdict;
364 1.18 rmind }
365 1.18 rmind
366 1.18 rmind int
367 1.18 rmind npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
368 1.18 rmind {
369 1.18 rmind npf_rule_t *rg, *rl;
370 1.18 rmind
371 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
372 1.19 rmind return ESRCH;
373 1.18 rmind }
374 1.18 rmind while ((rl = TAILQ_FIRST(&rg->r_subset)) != NULL) {
375 1.24 rmind KASSERT(rl->r_parent == rg);
376 1.18 rmind npf_ruleset_unlink(rlset, rl);
377 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
378 1.18 rmind }
379 1.18 rmind return 0;
380 1.18 rmind }
381 1.18 rmind
382 1.36 rmind int
383 1.36 rmind npf_ruleset_export(const npf_ruleset_t *rlset, prop_array_t rules)
384 1.36 rmind {
385 1.37 rmind const u_int nitems = rlset->rs_nitems;
386 1.36 rmind int error = 0;
387 1.37 rmind u_int n = 0;
388 1.36 rmind
389 1.36 rmind KASSERT(npf_config_locked_p());
390 1.36 rmind
391 1.37 rmind while (n < nitems) {
392 1.37 rmind const npf_rule_t *rl = rlset->rs_rules[n];
393 1.36 rmind const npf_natpolicy_t *natp = rl->r_natp;
394 1.36 rmind prop_dictionary_t rldict;
395 1.36 rmind
396 1.36 rmind rldict = prop_dictionary_create();
397 1.37 rmind if ((error = npf_rule_export(rlset, rl, rldict)) != 0) {
398 1.36 rmind prop_object_release(rldict);
399 1.36 rmind break;
400 1.36 rmind }
401 1.36 rmind if (natp && (error = npf_nat_policyexport(natp, rldict)) != 0) {
402 1.36 rmind prop_object_release(rldict);
403 1.36 rmind break;
404 1.36 rmind }
405 1.37 rmind prop_array_add(rules, rldict);
406 1.37 rmind prop_object_release(rldict);
407 1.37 rmind n++;
408 1.36 rmind }
409 1.36 rmind return error;
410 1.36 rmind }
411 1.36 rmind
412 1.18 rmind void
413 1.18 rmind npf_ruleset_gc(npf_ruleset_t *rlset)
414 1.18 rmind {
415 1.18 rmind npf_rule_t *rl;
416 1.18 rmind
417 1.18 rmind while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
418 1.18 rmind LIST_REMOVE(rl, r_aentry);
419 1.18 rmind npf_rule_free(rl);
420 1.18 rmind }
421 1.17 rmind }
422 1.17 rmind
423 1.17 rmind /*
424 1.31 rmind * npf_ruleset_reload: prepare the new ruleset by scanning the active
425 1.31 rmind * ruleset and 1) sharing the dynamic rules 2) sharing NAT policies.
426 1.17 rmind *
427 1.31 rmind * => The active (old) ruleset should be exclusively locked.
428 1.17 rmind */
429 1.17 rmind void
430 1.31 rmind npf_ruleset_reload(npf_ruleset_t *newset, npf_ruleset_t *oldset)
431 1.17 rmind {
432 1.31 rmind npf_rule_t *rg, *rl;
433 1.35 rmind uint64_t nid = 0;
434 1.17 rmind
435 1.17 rmind KASSERT(npf_config_locked_p());
436 1.17 rmind
437 1.31 rmind /*
438 1.31 rmind * Scan the dynamic rules and share (migrate) if needed.
439 1.31 rmind */
440 1.31 rmind LIST_FOREACH(rg, &newset->rs_dynamic, r_dentry) {
441 1.31 rmind npf_rule_t *actrg;
442 1.18 rmind
443 1.31 rmind /* Look for a dynamic ruleset group with such name. */
444 1.31 rmind actrg = npf_ruleset_lookup(oldset, rg->r_name);
445 1.31 rmind if (actrg == NULL) {
446 1.17 rmind continue;
447 1.17 rmind }
448 1.18 rmind
449 1.18 rmind /*
450 1.24 rmind * Copy the list-head structure. This is necessary because
451 1.24 rmind * the rules are still active and therefore accessible for
452 1.24 rmind * inspection via the old ruleset.
453 1.18 rmind */
454 1.31 rmind memcpy(&rg->r_subset, &actrg->r_subset, sizeof(rg->r_subset));
455 1.19 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
456 1.24 rmind /*
457 1.24 rmind * We can safely migrate to the new all-rule list
458 1.24 rmind * and re-set the parent rule, though.
459 1.24 rmind */
460 1.18 rmind LIST_REMOVE(rl, r_aentry);
461 1.31 rmind LIST_INSERT_HEAD(&newset->rs_all, rl, r_aentry);
462 1.19 rmind rl->r_parent = rg;
463 1.18 rmind }
464 1.1 rmind }
465 1.19 rmind
466 1.31 rmind /*
467 1.31 rmind * Scan all rules in the new ruleset and share NAT policies.
468 1.35 rmind * Also, assign a unique ID for each policy here.
469 1.31 rmind */
470 1.31 rmind LIST_FOREACH(rl, &newset->rs_all, r_aentry) {
471 1.31 rmind npf_natpolicy_t *np;
472 1.31 rmind npf_rule_t *actrl;
473 1.31 rmind
474 1.31 rmind /* Does the rule have a NAT policy associated? */
475 1.31 rmind if ((np = rl->r_natp) == NULL) {
476 1.31 rmind continue;
477 1.31 rmind }
478 1.35 rmind
479 1.38 rmind /*
480 1.38 rmind * First, try to share the active port map. If this
481 1.38 rmind * policy will be unused, npf_nat_freepolicy() will
482 1.38 rmind * drop the reference.
483 1.38 rmind */
484 1.38 rmind npf_ruleset_sharepm(oldset, np);
485 1.38 rmind
486 1.31 rmind /* Does it match with any policy in the active ruleset? */
487 1.38 rmind LIST_FOREACH(actrl, &oldset->rs_all, r_aentry) {
488 1.38 rmind if (!actrl->r_natp)
489 1.38 rmind continue;
490 1.38 rmind if ((actrl->r_attr & NPF_RULE_KEEPNAT) != 0)
491 1.38 rmind continue;
492 1.38 rmind if (npf_nat_cmppolicy(actrl->r_natp, np))
493 1.38 rmind break;
494 1.38 rmind }
495 1.38 rmind if (!actrl) {
496 1.38 rmind /* No: just set the ID and continue. */
497 1.35 rmind npf_nat_setid(np, ++nid);
498 1.31 rmind continue;
499 1.31 rmind }
500 1.31 rmind
501 1.38 rmind /* Yes: inherit the matching NAT policy. */
502 1.31 rmind rl->r_natp = actrl->r_natp;
503 1.35 rmind npf_nat_setid(rl->r_natp, ++nid);
504 1.31 rmind
505 1.31 rmind /*
506 1.31 rmind * Finally, mark the active rule to not destroy its NAT
507 1.31 rmind * policy later as we inherited it (but the rule must be
508 1.31 rmind * kept active for now). Destroy the new/unused policy.
509 1.31 rmind */
510 1.31 rmind actrl->r_attr |= NPF_RULE_KEEPNAT;
511 1.31 rmind npf_nat_freepolicy(np);
512 1.31 rmind }
513 1.31 rmind
514 1.19 rmind /* Inherit the ID counter. */
515 1.31 rmind newset->rs_idcnt = oldset->rs_idcnt;
516 1.1 rmind }
517 1.1 rmind
518 1.6 rmind npf_rule_t *
519 1.6 rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
520 1.6 rmind {
521 1.6 rmind npf_natpolicy_t *np;
522 1.6 rmind npf_rule_t *rl;
523 1.6 rmind
524 1.38 rmind /* Find a matching NAT policy in the old ruleset; skip the self. */
525 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
526 1.6 rmind np = rl->r_natp;
527 1.6 rmind if (np == NULL || np == mnp)
528 1.6 rmind continue;
529 1.6 rmind if (npf_nat_sharepm(np, mnp))
530 1.6 rmind break;
531 1.6 rmind }
532 1.6 rmind return rl;
533 1.6 rmind }
534 1.6 rmind
535 1.35 rmind npf_natpolicy_t *
536 1.35 rmind npf_ruleset_findnat(npf_ruleset_t *rlset, uint64_t id)
537 1.35 rmind {
538 1.35 rmind npf_rule_t *rl;
539 1.35 rmind
540 1.35 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
541 1.35 rmind npf_natpolicy_t *np = rl->r_natp;
542 1.35 rmind if (np && npf_nat_getid(np) == id) {
543 1.35 rmind return np;
544 1.35 rmind }
545 1.35 rmind }
546 1.35 rmind return NULL;
547 1.35 rmind }
548 1.35 rmind
549 1.1 rmind /*
550 1.13 rmind * npf_ruleset_freealg: inspect the ruleset and disassociate specified
551 1.13 rmind * ALG from all NAT entries using it.
552 1.13 rmind */
553 1.13 rmind void
554 1.13 rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
555 1.13 rmind {
556 1.13 rmind npf_rule_t *rl;
557 1.17 rmind npf_natpolicy_t *np;
558 1.13 rmind
559 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
560 1.17 rmind if ((np = rl->r_natp) != NULL) {
561 1.13 rmind npf_nat_freealg(np, alg);
562 1.13 rmind }
563 1.13 rmind }
564 1.13 rmind }
565 1.13 rmind
566 1.13 rmind /*
567 1.25 rmind * npf_rule_alloc: allocate a rule and initialise it.
568 1.1 rmind */
569 1.4 rmind npf_rule_t *
570 1.17 rmind npf_rule_alloc(prop_dictionary_t rldict)
571 1.1 rmind {
572 1.4 rmind npf_rule_t *rl;
573 1.7 rmind const char *rname;
574 1.36 rmind prop_data_t d;
575 1.1 rmind
576 1.4 rmind /* Allocate a rule structure. */
577 1.11 rmind rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
578 1.17 rmind TAILQ_INIT(&rl->r_subset);
579 1.4 rmind rl->r_natp = NULL;
580 1.4 rmind
581 1.11 rmind /* Name (optional) */
582 1.7 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
583 1.17 rmind strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
584 1.7 rmind } else {
585 1.7 rmind rl->r_name[0] = '\0';
586 1.7 rmind }
587 1.7 rmind
588 1.11 rmind /* Attributes, priority and interface ID (optional). */
589 1.36 rmind prop_dictionary_get_uint32(rldict, "attr", &rl->r_attr);
590 1.36 rmind prop_dictionary_get_int32(rldict, "prio", &rl->r_priority);
591 1.31 rmind rl->r_attr &= ~NPF_RULE_PRIVMASK;
592 1.26 rmind
593 1.36 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "ifname", &rname)) {
594 1.26 rmind if ((rl->r_ifid = npf_ifmap_register(rname)) == 0) {
595 1.26 rmind kmem_free(rl, sizeof(npf_rule_t));
596 1.26 rmind return NULL;
597 1.26 rmind }
598 1.26 rmind } else {
599 1.26 rmind rl->r_ifid = 0;
600 1.26 rmind }
601 1.4 rmind
602 1.17 rmind /* Get the skip-to index. No need to validate it. */
603 1.17 rmind prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
604 1.17 rmind
605 1.17 rmind /* Key (optional). */
606 1.17 rmind prop_object_t obj = prop_dictionary_get(rldict, "key");
607 1.17 rmind const void *key = prop_data_data_nocopy(obj);
608 1.17 rmind
609 1.17 rmind if (key) {
610 1.17 rmind size_t len = prop_data_size(obj);
611 1.17 rmind if (len > NPF_RULE_MAXKEYLEN) {
612 1.17 rmind kmem_free(rl, sizeof(npf_rule_t));
613 1.17 rmind return NULL;
614 1.17 rmind }
615 1.17 rmind memcpy(rl->r_key, key, len);
616 1.4 rmind }
617 1.18 rmind
618 1.36 rmind if ((d = prop_dictionary_get(rldict, "info")) != NULL) {
619 1.36 rmind rl->r_info = prop_data_copy(d);
620 1.36 rmind }
621 1.36 rmind return rl;
622 1.36 rmind }
623 1.36 rmind
624 1.36 rmind static int
625 1.37 rmind npf_rule_export(const npf_ruleset_t *rlset, const npf_rule_t *rl,
626 1.37 rmind prop_dictionary_t rldict)
627 1.36 rmind {
628 1.37 rmind u_int skip_to = 0;
629 1.36 rmind prop_data_t d;
630 1.36 rmind
631 1.36 rmind prop_dictionary_set_uint32(rldict, "attr", rl->r_attr);
632 1.36 rmind prop_dictionary_set_int32(rldict, "prio", rl->r_priority);
633 1.37 rmind if ((rl->r_skip_to & SKIPTO_ADJ_FLAG) == 0) {
634 1.37 rmind skip_to = rl->r_skip_to & SKIPTO_MASK;
635 1.37 rmind }
636 1.37 rmind prop_dictionary_set_uint32(rldict, "skip-to", skip_to);
637 1.36 rmind prop_dictionary_set_int32(rldict, "code-type", rl->r_type);
638 1.36 rmind if (rl->r_code) {
639 1.36 rmind d = prop_data_create_data(rl->r_code, rl->r_clen);
640 1.36 rmind prop_dictionary_set_and_rel(rldict, "code", d);
641 1.36 rmind }
642 1.36 rmind
643 1.36 rmind if (rl->r_ifid) {
644 1.36 rmind const char *ifname = npf_ifmap_getname(rl->r_ifid);
645 1.36 rmind prop_dictionary_set_cstring(rldict, "ifname", ifname);
646 1.36 rmind }
647 1.36 rmind prop_dictionary_set_uint64(rldict, "id", rl->r_id);
648 1.36 rmind
649 1.36 rmind if (rl->r_name[0]) {
650 1.36 rmind prop_dictionary_set_cstring(rldict, "name", rl->r_name);
651 1.36 rmind }
652 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
653 1.36 rmind d = prop_data_create_data(rl->r_key, NPF_RULE_MAXKEYLEN);
654 1.36 rmind prop_dictionary_set_and_rel(rldict, "key", d);
655 1.18 rmind }
656 1.37 rmind if (rl->r_info) {
657 1.37 rmind prop_dictionary_set(rldict, "info", rl->r_info);
658 1.37 rmind }
659 1.36 rmind return 0;
660 1.17 rmind }
661 1.17 rmind
662 1.17 rmind /*
663 1.17 rmind * npf_rule_setcode: assign filter code to the rule.
664 1.17 rmind *
665 1.20 rmind * => The code must be validated by the caller.
666 1.20 rmind * => JIT compilation may be performed here.
667 1.17 rmind */
668 1.17 rmind void
669 1.17 rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
670 1.17 rmind {
671 1.25 rmind KASSERT(type == NPF_CODE_BPF);
672 1.28 rmind
673 1.28 rmind rl->r_type = type;
674 1.36 rmind rl->r_code = code;
675 1.36 rmind rl->r_clen = size;
676 1.36 rmind rl->r_jcode = npf_bpf_compile(code, size);
677 1.17 rmind }
678 1.17 rmind
679 1.17 rmind /*
680 1.17 rmind * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
681 1.17 rmind */
682 1.17 rmind void
683 1.17 rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
684 1.17 rmind {
685 1.17 rmind npf_rproc_acquire(rp);
686 1.6 rmind rl->r_rproc = rp;
687 1.1 rmind }
688 1.1 rmind
689 1.1 rmind /*
690 1.1 rmind * npf_rule_free: free the specified rule.
691 1.1 rmind */
692 1.1 rmind void
693 1.1 rmind npf_rule_free(npf_rule_t *rl)
694 1.1 rmind {
695 1.4 rmind npf_natpolicy_t *np = rl->r_natp;
696 1.4 rmind npf_rproc_t *rp = rl->r_rproc;
697 1.1 rmind
698 1.31 rmind if (np && (rl->r_attr & NPF_RULE_KEEPNAT) == 0) {
699 1.4 rmind /* Free NAT policy. */
700 1.4 rmind npf_nat_freepolicy(np);
701 1.4 rmind }
702 1.4 rmind if (rp) {
703 1.6 rmind /* Release rule procedure. */
704 1.4 rmind npf_rproc_release(rp);
705 1.4 rmind }
706 1.17 rmind if (rl->r_code) {
707 1.20 rmind /* Free byte-code. */
708 1.17 rmind kmem_free(rl->r_code, rl->r_clen);
709 1.1 rmind }
710 1.20 rmind if (rl->r_jcode) {
711 1.20 rmind /* Free JIT code. */
712 1.28 rmind bpf_jit_freecode(rl->r_jcode);
713 1.20 rmind }
714 1.36 rmind if (rl->r_info) {
715 1.36 rmind prop_object_release(rl->r_info);
716 1.18 rmind }
717 1.4 rmind kmem_free(rl, sizeof(npf_rule_t));
718 1.1 rmind }
719 1.1 rmind
720 1.1 rmind /*
721 1.19 rmind * npf_rule_getid: return the unique ID of a rule.
722 1.10 rmind * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
723 1.1 rmind * npf_rule_getnat: get NAT policy assigned to the rule.
724 1.1 rmind */
725 1.1 rmind
726 1.19 rmind uint64_t
727 1.19 rmind npf_rule_getid(const npf_rule_t *rl)
728 1.19 rmind {
729 1.19 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
730 1.19 rmind return rl->r_id;
731 1.19 rmind }
732 1.19 rmind
733 1.10 rmind npf_rproc_t *
734 1.30 rmind npf_rule_getrproc(const npf_rule_t *rl)
735 1.10 rmind {
736 1.10 rmind npf_rproc_t *rp = rl->r_rproc;
737 1.10 rmind
738 1.10 rmind if (rp) {
739 1.10 rmind npf_rproc_acquire(rp);
740 1.10 rmind }
741 1.10 rmind return rp;
742 1.10 rmind }
743 1.10 rmind
744 1.1 rmind npf_natpolicy_t *
745 1.1 rmind npf_rule_getnat(const npf_rule_t *rl)
746 1.1 rmind {
747 1.4 rmind return rl->r_natp;
748 1.1 rmind }
749 1.1 rmind
750 1.4 rmind /*
751 1.4 rmind * npf_rule_setnat: assign NAT policy to the rule and insert into the
752 1.4 rmind * NAT policy list in the ruleset.
753 1.4 rmind */
754 1.1 rmind void
755 1.1 rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
756 1.1 rmind {
757 1.4 rmind KASSERT(rl->r_natp == NULL);
758 1.4 rmind rl->r_natp = np;
759 1.1 rmind }
760 1.1 rmind
761 1.17 rmind /*
762 1.17 rmind * npf_rule_inspect: match the interface, direction and run the filter code.
763 1.29 rmind * Returns true if rule matches and false otherwise.
764 1.17 rmind */
765 1.17 rmind static inline bool
766 1.29 rmind npf_rule_inspect(const npf_rule_t *rl, bpf_args_t *bc_args,
767 1.29 rmind const int di_mask, const u_int ifid)
768 1.17 rmind {
769 1.17 rmind /* Match the interface. */
770 1.29 rmind if (rl->r_ifid && rl->r_ifid != ifid) {
771 1.17 rmind return false;
772 1.17 rmind }
773 1.17 rmind
774 1.17 rmind /* Match the direction. */
775 1.17 rmind if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
776 1.17 rmind if ((rl->r_attr & di_mask) == 0)
777 1.17 rmind return false;
778 1.17 rmind }
779 1.17 rmind
780 1.24 rmind /* Any code? */
781 1.36 rmind if (!rl->r_code) {
782 1.24 rmind KASSERT(rl->r_jcode == NULL);
783 1.17 rmind return true;
784 1.17 rmind }
785 1.25 rmind KASSERT(rl->r_type == NPF_CODE_BPF);
786 1.29 rmind return npf_bpf_filter(bc_args, rl->r_code, rl->r_jcode) != 0;
787 1.17 rmind }
788 1.17 rmind
789 1.17 rmind /*
790 1.17 rmind * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
791 1.17 rmind * This is only for the dynamic rules. Subrules cannot have nested rules.
792 1.17 rmind */
793 1.17 rmind static npf_rule_t *
794 1.29 rmind npf_rule_reinspect(const npf_rule_t *drl, bpf_args_t *bc_args,
795 1.29 rmind const int di_mask, const u_int ifid)
796 1.7 rmind {
797 1.17 rmind npf_rule_t *final_rl = NULL, *rl;
798 1.17 rmind
799 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(drl->r_attr));
800 1.7 rmind
801 1.17 rmind TAILQ_FOREACH(rl, &drl->r_subset, r_entry) {
802 1.29 rmind if (!npf_rule_inspect(rl, bc_args, di_mask, ifid)) {
803 1.7 rmind continue;
804 1.17 rmind }
805 1.17 rmind if (rl->r_attr & NPF_RULE_FINAL) {
806 1.17 rmind return rl;
807 1.17 rmind }
808 1.17 rmind final_rl = rl;
809 1.7 rmind }
810 1.17 rmind return final_rl;
811 1.7 rmind }
812 1.1 rmind
813 1.1 rmind /*
814 1.7 rmind * npf_ruleset_inspect: inspect the packet against the given ruleset.
815 1.1 rmind *
816 1.25 rmind * Loop through the rules in the set and run the byte-code of each rule
817 1.7 rmind * against the packet (nbuf chain). If sub-ruleset is found, inspect it.
818 1.1 rmind */
819 1.1 rmind npf_rule_t *
820 1.34 rmind npf_ruleset_inspect(npf_cache_t *npc, const npf_ruleset_t *rlset,
821 1.34 rmind const int di, const int layer)
822 1.1 rmind {
823 1.34 rmind nbuf_t *nbuf = npc->npc_nbuf;
824 1.7 rmind const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
825 1.17 rmind const u_int nitems = rlset->rs_nitems;
826 1.29 rmind const u_int ifid = nbuf->nb_ifid;
827 1.17 rmind npf_rule_t *final_rl = NULL;
828 1.29 rmind bpf_args_t bc_args;
829 1.17 rmind u_int n = 0;
830 1.1 rmind
831 1.33 rmind KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
832 1.29 rmind
833 1.33 rmind /*
834 1.33 rmind * Prepare the external memory store and the arguments for
835 1.33 rmind * the BPF programs to be executed.
836 1.33 rmind */
837 1.33 rmind uint32_t bc_words[NPF_BPF_NWORDS];
838 1.34 rmind npf_bpf_prepare(npc, &bc_args, bc_words);
839 1.17 rmind
840 1.17 rmind while (n < nitems) {
841 1.17 rmind npf_rule_t *rl = rlset->rs_rules[n];
842 1.37 rmind const u_int skip_to = rl->r_skip_to & SKIPTO_MASK;
843 1.17 rmind const uint32_t attr = rl->r_attr;
844 1.17 rmind
845 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
846 1.1 rmind KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
847 1.17 rmind KASSERT(n < skip_to);
848 1.1 rmind
849 1.17 rmind /* Group is a barrier: return a matching if found any. */
850 1.17 rmind if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
851 1.17 rmind break;
852 1.17 rmind }
853 1.17 rmind
854 1.17 rmind /* Main inspection of the rule. */
855 1.29 rmind if (!npf_rule_inspect(rl, &bc_args, di_mask, ifid)) {
856 1.17 rmind n = skip_to;
857 1.1 rmind continue;
858 1.1 rmind }
859 1.17 rmind
860 1.17 rmind if (NPF_DYNAMIC_GROUP_P(attr)) {
861 1.17 rmind /*
862 1.17 rmind * If this is a dynamic rule, re-inspect the subrules.
863 1.17 rmind * If it has any matching rule, then it is final.
864 1.17 rmind */
865 1.29 rmind rl = npf_rule_reinspect(rl, &bc_args, di_mask, ifid);
866 1.17 rmind if (rl != NULL) {
867 1.17 rmind final_rl = rl;
868 1.17 rmind break;
869 1.17 rmind }
870 1.17 rmind } else if ((attr & NPF_RULE_GROUP) == 0) {
871 1.17 rmind /*
872 1.17 rmind * Groups themselves are not matching.
873 1.17 rmind */
874 1.17 rmind final_rl = rl;
875 1.1 rmind }
876 1.17 rmind
877 1.1 rmind /* Set the matching rule and check for "final". */
878 1.17 rmind if (attr & NPF_RULE_FINAL) {
879 1.2 rmind break;
880 1.1 rmind }
881 1.17 rmind n++;
882 1.2 rmind }
883 1.16 rmind
884 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
885 1.7 rmind return final_rl;
886 1.1 rmind }
887 1.1 rmind
888 1.1 rmind /*
889 1.17 rmind * npf_rule_conclude: return decision and the flags for conclusion.
890 1.1 rmind *
891 1.1 rmind * => Returns ENETUNREACH if "block" and 0 if "pass".
892 1.1 rmind */
893 1.1 rmind int
894 1.17 rmind npf_rule_conclude(const npf_rule_t *rl, int *retfl)
895 1.1 rmind {
896 1.1 rmind /* If not passing - drop the packet. */
897 1.4 rmind *retfl = rl->r_attr;
898 1.17 rmind return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
899 1.1 rmind }
900