npf_ruleset.c revision 1.37 1 1.37 rmind /* $NetBSD: npf_ruleset.c,v 1.37 2014/08/11 01:54:12 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.37 rmind __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.37 2014/08/11 01:54:12 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.35 rmind * npf_ruleset_cmpnat: find a matching NAT policy in the ruleset.
425 1.35 rmind */
426 1.35 rmind static inline npf_rule_t *
427 1.35 rmind npf_ruleset_cmpnat(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
428 1.35 rmind {
429 1.35 rmind npf_rule_t *rl;
430 1.35 rmind
431 1.35 rmind /* Find a matching NAT policy in the old ruleset. */
432 1.35 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
433 1.35 rmind if (rl->r_natp && npf_nat_cmppolicy(rl->r_natp, mnp))
434 1.35 rmind break;
435 1.35 rmind }
436 1.35 rmind return rl;
437 1.35 rmind }
438 1.35 rmind
439 1.35 rmind /*
440 1.31 rmind * npf_ruleset_reload: prepare the new ruleset by scanning the active
441 1.31 rmind * ruleset and 1) sharing the dynamic rules 2) sharing NAT policies.
442 1.17 rmind *
443 1.31 rmind * => The active (old) ruleset should be exclusively locked.
444 1.17 rmind */
445 1.17 rmind void
446 1.31 rmind npf_ruleset_reload(npf_ruleset_t *newset, npf_ruleset_t *oldset)
447 1.17 rmind {
448 1.31 rmind npf_rule_t *rg, *rl;
449 1.35 rmind uint64_t nid = 0;
450 1.17 rmind
451 1.17 rmind KASSERT(npf_config_locked_p());
452 1.17 rmind
453 1.31 rmind /*
454 1.31 rmind * Scan the dynamic rules and share (migrate) if needed.
455 1.31 rmind */
456 1.31 rmind LIST_FOREACH(rg, &newset->rs_dynamic, r_dentry) {
457 1.31 rmind npf_rule_t *actrg;
458 1.18 rmind
459 1.31 rmind /* Look for a dynamic ruleset group with such name. */
460 1.31 rmind actrg = npf_ruleset_lookup(oldset, rg->r_name);
461 1.31 rmind if (actrg == NULL) {
462 1.17 rmind continue;
463 1.17 rmind }
464 1.18 rmind
465 1.18 rmind /*
466 1.24 rmind * Copy the list-head structure. This is necessary because
467 1.24 rmind * the rules are still active and therefore accessible for
468 1.24 rmind * inspection via the old ruleset.
469 1.18 rmind */
470 1.31 rmind memcpy(&rg->r_subset, &actrg->r_subset, sizeof(rg->r_subset));
471 1.19 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
472 1.24 rmind /*
473 1.24 rmind * We can safely migrate to the new all-rule list
474 1.24 rmind * and re-set the parent rule, though.
475 1.24 rmind */
476 1.18 rmind LIST_REMOVE(rl, r_aentry);
477 1.31 rmind LIST_INSERT_HEAD(&newset->rs_all, rl, r_aentry);
478 1.19 rmind rl->r_parent = rg;
479 1.18 rmind }
480 1.1 rmind }
481 1.19 rmind
482 1.31 rmind /*
483 1.31 rmind * Scan all rules in the new ruleset and share NAT policies.
484 1.35 rmind * Also, assign a unique ID for each policy here.
485 1.31 rmind */
486 1.31 rmind LIST_FOREACH(rl, &newset->rs_all, r_aentry) {
487 1.31 rmind npf_natpolicy_t *np;
488 1.31 rmind npf_rule_t *actrl;
489 1.31 rmind
490 1.31 rmind /* Does the rule have a NAT policy associated? */
491 1.31 rmind if ((np = rl->r_natp) == NULL) {
492 1.31 rmind continue;
493 1.31 rmind }
494 1.35 rmind
495 1.31 rmind /* Does it match with any policy in the active ruleset? */
496 1.35 rmind if ((actrl = npf_ruleset_cmpnat(oldset, np)) == NULL) {
497 1.35 rmind npf_nat_setid(np, ++nid);
498 1.31 rmind continue;
499 1.31 rmind }
500 1.31 rmind
501 1.31 rmind /*
502 1.31 rmind * Inherit the matching NAT policy and check other ones
503 1.31 rmind * in the new ruleset for sharing the portmap.
504 1.31 rmind */
505 1.31 rmind rl->r_natp = actrl->r_natp;
506 1.31 rmind npf_ruleset_sharepm(newset, rl->r_natp);
507 1.35 rmind npf_nat_setid(rl->r_natp, ++nid);
508 1.31 rmind
509 1.31 rmind /*
510 1.31 rmind * Finally, mark the active rule to not destroy its NAT
511 1.31 rmind * policy later as we inherited it (but the rule must be
512 1.31 rmind * kept active for now). Destroy the new/unused policy.
513 1.31 rmind */
514 1.31 rmind actrl->r_attr |= NPF_RULE_KEEPNAT;
515 1.31 rmind npf_nat_freepolicy(np);
516 1.31 rmind }
517 1.31 rmind
518 1.19 rmind /* Inherit the ID counter. */
519 1.31 rmind newset->rs_idcnt = oldset->rs_idcnt;
520 1.1 rmind }
521 1.1 rmind
522 1.6 rmind npf_rule_t *
523 1.6 rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
524 1.6 rmind {
525 1.6 rmind npf_natpolicy_t *np;
526 1.6 rmind npf_rule_t *rl;
527 1.6 rmind
528 1.6 rmind /* Find a matching NAT policy in the old ruleset. */
529 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
530 1.6 rmind /*
531 1.6 rmind * NAT policy might not yet be set during the creation of
532 1.6 rmind * the ruleset (in such case, rule is for our policy), or
533 1.6 rmind * policies might be equal due to rule exchange on reload.
534 1.6 rmind */
535 1.6 rmind np = rl->r_natp;
536 1.6 rmind if (np == NULL || np == mnp)
537 1.6 rmind continue;
538 1.6 rmind if (npf_nat_sharepm(np, mnp))
539 1.6 rmind break;
540 1.6 rmind }
541 1.6 rmind return rl;
542 1.6 rmind }
543 1.6 rmind
544 1.35 rmind npf_natpolicy_t *
545 1.35 rmind npf_ruleset_findnat(npf_ruleset_t *rlset, uint64_t id)
546 1.35 rmind {
547 1.35 rmind npf_rule_t *rl;
548 1.35 rmind
549 1.35 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
550 1.35 rmind npf_natpolicy_t *np = rl->r_natp;
551 1.35 rmind if (np && npf_nat_getid(np) == id) {
552 1.35 rmind return np;
553 1.35 rmind }
554 1.35 rmind }
555 1.35 rmind return NULL;
556 1.35 rmind }
557 1.35 rmind
558 1.1 rmind /*
559 1.13 rmind * npf_ruleset_freealg: inspect the ruleset and disassociate specified
560 1.13 rmind * ALG from all NAT entries using it.
561 1.13 rmind */
562 1.13 rmind void
563 1.13 rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
564 1.13 rmind {
565 1.13 rmind npf_rule_t *rl;
566 1.17 rmind npf_natpolicy_t *np;
567 1.13 rmind
568 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
569 1.17 rmind if ((np = rl->r_natp) != NULL) {
570 1.13 rmind npf_nat_freealg(np, alg);
571 1.13 rmind }
572 1.13 rmind }
573 1.13 rmind }
574 1.13 rmind
575 1.13 rmind /*
576 1.25 rmind * npf_rule_alloc: allocate a rule and initialise it.
577 1.1 rmind */
578 1.4 rmind npf_rule_t *
579 1.17 rmind npf_rule_alloc(prop_dictionary_t rldict)
580 1.1 rmind {
581 1.4 rmind npf_rule_t *rl;
582 1.7 rmind const char *rname;
583 1.36 rmind prop_data_t d;
584 1.1 rmind
585 1.4 rmind /* Allocate a rule structure. */
586 1.11 rmind rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
587 1.17 rmind TAILQ_INIT(&rl->r_subset);
588 1.4 rmind rl->r_natp = NULL;
589 1.4 rmind
590 1.11 rmind /* Name (optional) */
591 1.7 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
592 1.17 rmind strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
593 1.7 rmind } else {
594 1.7 rmind rl->r_name[0] = '\0';
595 1.7 rmind }
596 1.7 rmind
597 1.11 rmind /* Attributes, priority and interface ID (optional). */
598 1.36 rmind prop_dictionary_get_uint32(rldict, "attr", &rl->r_attr);
599 1.36 rmind prop_dictionary_get_int32(rldict, "prio", &rl->r_priority);
600 1.31 rmind rl->r_attr &= ~NPF_RULE_PRIVMASK;
601 1.26 rmind
602 1.36 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "ifname", &rname)) {
603 1.26 rmind if ((rl->r_ifid = npf_ifmap_register(rname)) == 0) {
604 1.26 rmind kmem_free(rl, sizeof(npf_rule_t));
605 1.26 rmind return NULL;
606 1.26 rmind }
607 1.26 rmind } else {
608 1.26 rmind rl->r_ifid = 0;
609 1.26 rmind }
610 1.4 rmind
611 1.17 rmind /* Get the skip-to index. No need to validate it. */
612 1.17 rmind prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
613 1.17 rmind
614 1.17 rmind /* Key (optional). */
615 1.17 rmind prop_object_t obj = prop_dictionary_get(rldict, "key");
616 1.17 rmind const void *key = prop_data_data_nocopy(obj);
617 1.17 rmind
618 1.17 rmind if (key) {
619 1.17 rmind size_t len = prop_data_size(obj);
620 1.17 rmind if (len > NPF_RULE_MAXKEYLEN) {
621 1.17 rmind kmem_free(rl, sizeof(npf_rule_t));
622 1.17 rmind return NULL;
623 1.17 rmind }
624 1.17 rmind memcpy(rl->r_key, key, len);
625 1.4 rmind }
626 1.18 rmind
627 1.36 rmind if ((d = prop_dictionary_get(rldict, "info")) != NULL) {
628 1.36 rmind rl->r_info = prop_data_copy(d);
629 1.36 rmind }
630 1.36 rmind return rl;
631 1.36 rmind }
632 1.36 rmind
633 1.36 rmind static int
634 1.37 rmind npf_rule_export(const npf_ruleset_t *rlset, const npf_rule_t *rl,
635 1.37 rmind prop_dictionary_t rldict)
636 1.36 rmind {
637 1.37 rmind u_int skip_to = 0;
638 1.36 rmind prop_data_t d;
639 1.36 rmind
640 1.36 rmind prop_dictionary_set_uint32(rldict, "attr", rl->r_attr);
641 1.36 rmind prop_dictionary_set_int32(rldict, "prio", rl->r_priority);
642 1.37 rmind if ((rl->r_skip_to & SKIPTO_ADJ_FLAG) == 0) {
643 1.37 rmind skip_to = rl->r_skip_to & SKIPTO_MASK;
644 1.37 rmind }
645 1.37 rmind prop_dictionary_set_uint32(rldict, "skip-to", skip_to);
646 1.36 rmind prop_dictionary_set_int32(rldict, "code-type", rl->r_type);
647 1.36 rmind if (rl->r_code) {
648 1.36 rmind d = prop_data_create_data(rl->r_code, rl->r_clen);
649 1.36 rmind prop_dictionary_set_and_rel(rldict, "code", d);
650 1.36 rmind }
651 1.36 rmind
652 1.36 rmind if (rl->r_ifid) {
653 1.36 rmind const char *ifname = npf_ifmap_getname(rl->r_ifid);
654 1.36 rmind prop_dictionary_set_cstring(rldict, "ifname", ifname);
655 1.36 rmind }
656 1.36 rmind prop_dictionary_set_uint64(rldict, "id", rl->r_id);
657 1.36 rmind
658 1.36 rmind if (rl->r_name[0]) {
659 1.36 rmind prop_dictionary_set_cstring(rldict, "name", rl->r_name);
660 1.36 rmind }
661 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
662 1.36 rmind d = prop_data_create_data(rl->r_key, NPF_RULE_MAXKEYLEN);
663 1.36 rmind prop_dictionary_set_and_rel(rldict, "key", d);
664 1.18 rmind }
665 1.37 rmind if (rl->r_info) {
666 1.37 rmind prop_dictionary_set(rldict, "info", rl->r_info);
667 1.37 rmind }
668 1.36 rmind return 0;
669 1.17 rmind }
670 1.17 rmind
671 1.17 rmind /*
672 1.17 rmind * npf_rule_setcode: assign filter code to the rule.
673 1.17 rmind *
674 1.20 rmind * => The code must be validated by the caller.
675 1.20 rmind * => JIT compilation may be performed here.
676 1.17 rmind */
677 1.17 rmind void
678 1.17 rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
679 1.17 rmind {
680 1.25 rmind KASSERT(type == NPF_CODE_BPF);
681 1.28 rmind
682 1.28 rmind rl->r_type = type;
683 1.36 rmind rl->r_code = code;
684 1.36 rmind rl->r_clen = size;
685 1.36 rmind rl->r_jcode = npf_bpf_compile(code, size);
686 1.17 rmind }
687 1.17 rmind
688 1.17 rmind /*
689 1.17 rmind * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
690 1.17 rmind */
691 1.17 rmind void
692 1.17 rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
693 1.17 rmind {
694 1.17 rmind npf_rproc_acquire(rp);
695 1.6 rmind rl->r_rproc = rp;
696 1.1 rmind }
697 1.1 rmind
698 1.1 rmind /*
699 1.1 rmind * npf_rule_free: free the specified rule.
700 1.1 rmind */
701 1.1 rmind void
702 1.1 rmind npf_rule_free(npf_rule_t *rl)
703 1.1 rmind {
704 1.4 rmind npf_natpolicy_t *np = rl->r_natp;
705 1.4 rmind npf_rproc_t *rp = rl->r_rproc;
706 1.1 rmind
707 1.31 rmind if (np && (rl->r_attr & NPF_RULE_KEEPNAT) == 0) {
708 1.4 rmind /* Free NAT policy. */
709 1.4 rmind npf_nat_freepolicy(np);
710 1.4 rmind }
711 1.4 rmind if (rp) {
712 1.6 rmind /* Release rule procedure. */
713 1.4 rmind npf_rproc_release(rp);
714 1.4 rmind }
715 1.17 rmind if (rl->r_code) {
716 1.20 rmind /* Free byte-code. */
717 1.17 rmind kmem_free(rl->r_code, rl->r_clen);
718 1.1 rmind }
719 1.20 rmind if (rl->r_jcode) {
720 1.20 rmind /* Free JIT code. */
721 1.28 rmind bpf_jit_freecode(rl->r_jcode);
722 1.20 rmind }
723 1.36 rmind if (rl->r_info) {
724 1.36 rmind prop_object_release(rl->r_info);
725 1.18 rmind }
726 1.4 rmind kmem_free(rl, sizeof(npf_rule_t));
727 1.1 rmind }
728 1.1 rmind
729 1.1 rmind /*
730 1.19 rmind * npf_rule_getid: return the unique ID of a rule.
731 1.10 rmind * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
732 1.1 rmind * npf_rule_getnat: get NAT policy assigned to the rule.
733 1.1 rmind */
734 1.1 rmind
735 1.19 rmind uint64_t
736 1.19 rmind npf_rule_getid(const npf_rule_t *rl)
737 1.19 rmind {
738 1.19 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
739 1.19 rmind return rl->r_id;
740 1.19 rmind }
741 1.19 rmind
742 1.10 rmind npf_rproc_t *
743 1.30 rmind npf_rule_getrproc(const npf_rule_t *rl)
744 1.10 rmind {
745 1.10 rmind npf_rproc_t *rp = rl->r_rproc;
746 1.10 rmind
747 1.10 rmind if (rp) {
748 1.10 rmind npf_rproc_acquire(rp);
749 1.10 rmind }
750 1.10 rmind return rp;
751 1.10 rmind }
752 1.10 rmind
753 1.1 rmind npf_natpolicy_t *
754 1.1 rmind npf_rule_getnat(const npf_rule_t *rl)
755 1.1 rmind {
756 1.4 rmind return rl->r_natp;
757 1.1 rmind }
758 1.1 rmind
759 1.4 rmind /*
760 1.4 rmind * npf_rule_setnat: assign NAT policy to the rule and insert into the
761 1.4 rmind * NAT policy list in the ruleset.
762 1.4 rmind */
763 1.1 rmind void
764 1.1 rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
765 1.1 rmind {
766 1.4 rmind KASSERT(rl->r_natp == NULL);
767 1.4 rmind rl->r_natp = np;
768 1.1 rmind }
769 1.1 rmind
770 1.17 rmind /*
771 1.17 rmind * npf_rule_inspect: match the interface, direction and run the filter code.
772 1.29 rmind * Returns true if rule matches and false otherwise.
773 1.17 rmind */
774 1.17 rmind static inline bool
775 1.29 rmind npf_rule_inspect(const npf_rule_t *rl, bpf_args_t *bc_args,
776 1.29 rmind const int di_mask, const u_int ifid)
777 1.17 rmind {
778 1.17 rmind /* Match the interface. */
779 1.29 rmind if (rl->r_ifid && rl->r_ifid != ifid) {
780 1.17 rmind return false;
781 1.17 rmind }
782 1.17 rmind
783 1.17 rmind /* Match the direction. */
784 1.17 rmind if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
785 1.17 rmind if ((rl->r_attr & di_mask) == 0)
786 1.17 rmind return false;
787 1.17 rmind }
788 1.17 rmind
789 1.24 rmind /* Any code? */
790 1.36 rmind if (!rl->r_code) {
791 1.24 rmind KASSERT(rl->r_jcode == NULL);
792 1.17 rmind return true;
793 1.17 rmind }
794 1.25 rmind KASSERT(rl->r_type == NPF_CODE_BPF);
795 1.29 rmind return npf_bpf_filter(bc_args, rl->r_code, rl->r_jcode) != 0;
796 1.17 rmind }
797 1.17 rmind
798 1.17 rmind /*
799 1.17 rmind * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
800 1.17 rmind * This is only for the dynamic rules. Subrules cannot have nested rules.
801 1.17 rmind */
802 1.17 rmind static npf_rule_t *
803 1.29 rmind npf_rule_reinspect(const npf_rule_t *drl, bpf_args_t *bc_args,
804 1.29 rmind const int di_mask, const u_int ifid)
805 1.7 rmind {
806 1.17 rmind npf_rule_t *final_rl = NULL, *rl;
807 1.17 rmind
808 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(drl->r_attr));
809 1.7 rmind
810 1.17 rmind TAILQ_FOREACH(rl, &drl->r_subset, r_entry) {
811 1.29 rmind if (!npf_rule_inspect(rl, bc_args, di_mask, ifid)) {
812 1.7 rmind continue;
813 1.17 rmind }
814 1.17 rmind if (rl->r_attr & NPF_RULE_FINAL) {
815 1.17 rmind return rl;
816 1.17 rmind }
817 1.17 rmind final_rl = rl;
818 1.7 rmind }
819 1.17 rmind return final_rl;
820 1.7 rmind }
821 1.1 rmind
822 1.1 rmind /*
823 1.7 rmind * npf_ruleset_inspect: inspect the packet against the given ruleset.
824 1.1 rmind *
825 1.25 rmind * Loop through the rules in the set and run the byte-code of each rule
826 1.7 rmind * against the packet (nbuf chain). If sub-ruleset is found, inspect it.
827 1.1 rmind */
828 1.1 rmind npf_rule_t *
829 1.34 rmind npf_ruleset_inspect(npf_cache_t *npc, const npf_ruleset_t *rlset,
830 1.34 rmind const int di, const int layer)
831 1.1 rmind {
832 1.34 rmind nbuf_t *nbuf = npc->npc_nbuf;
833 1.7 rmind const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
834 1.17 rmind const u_int nitems = rlset->rs_nitems;
835 1.29 rmind const u_int ifid = nbuf->nb_ifid;
836 1.17 rmind npf_rule_t *final_rl = NULL;
837 1.29 rmind bpf_args_t bc_args;
838 1.17 rmind u_int n = 0;
839 1.1 rmind
840 1.33 rmind KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
841 1.29 rmind
842 1.33 rmind /*
843 1.33 rmind * Prepare the external memory store and the arguments for
844 1.33 rmind * the BPF programs to be executed.
845 1.33 rmind */
846 1.33 rmind uint32_t bc_words[NPF_BPF_NWORDS];
847 1.34 rmind npf_bpf_prepare(npc, &bc_args, bc_words);
848 1.17 rmind
849 1.17 rmind while (n < nitems) {
850 1.17 rmind npf_rule_t *rl = rlset->rs_rules[n];
851 1.37 rmind const u_int skip_to = rl->r_skip_to & SKIPTO_MASK;
852 1.17 rmind const uint32_t attr = rl->r_attr;
853 1.17 rmind
854 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
855 1.1 rmind KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
856 1.17 rmind KASSERT(n < skip_to);
857 1.1 rmind
858 1.17 rmind /* Group is a barrier: return a matching if found any. */
859 1.17 rmind if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
860 1.17 rmind break;
861 1.17 rmind }
862 1.17 rmind
863 1.17 rmind /* Main inspection of the rule. */
864 1.29 rmind if (!npf_rule_inspect(rl, &bc_args, di_mask, ifid)) {
865 1.17 rmind n = skip_to;
866 1.1 rmind continue;
867 1.1 rmind }
868 1.17 rmind
869 1.17 rmind if (NPF_DYNAMIC_GROUP_P(attr)) {
870 1.17 rmind /*
871 1.17 rmind * If this is a dynamic rule, re-inspect the subrules.
872 1.17 rmind * If it has any matching rule, then it is final.
873 1.17 rmind */
874 1.29 rmind rl = npf_rule_reinspect(rl, &bc_args, di_mask, ifid);
875 1.17 rmind if (rl != NULL) {
876 1.17 rmind final_rl = rl;
877 1.17 rmind break;
878 1.17 rmind }
879 1.17 rmind } else if ((attr & NPF_RULE_GROUP) == 0) {
880 1.17 rmind /*
881 1.17 rmind * Groups themselves are not matching.
882 1.17 rmind */
883 1.17 rmind final_rl = rl;
884 1.1 rmind }
885 1.17 rmind
886 1.1 rmind /* Set the matching rule and check for "final". */
887 1.17 rmind if (attr & NPF_RULE_FINAL) {
888 1.2 rmind break;
889 1.1 rmind }
890 1.17 rmind n++;
891 1.2 rmind }
892 1.16 rmind
893 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
894 1.7 rmind return final_rl;
895 1.1 rmind }
896 1.1 rmind
897 1.1 rmind /*
898 1.17 rmind * npf_rule_conclude: return decision and the flags for conclusion.
899 1.1 rmind *
900 1.1 rmind * => Returns ENETUNREACH if "block" and 0 if "pass".
901 1.1 rmind */
902 1.1 rmind int
903 1.17 rmind npf_rule_conclude(const npf_rule_t *rl, int *retfl)
904 1.1 rmind {
905 1.1 rmind /* If not passing - drop the packet. */
906 1.4 rmind *retfl = rl->r_attr;
907 1.17 rmind return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
908 1.1 rmind }
909