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