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