npf_ruleset.c revision 1.30.2.1 1 1.30.2.1 tls /* $NetBSD: npf_ruleset.c,v 1.30.2.1 2014/08/10 06:56:16 tls 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.30.2.1 tls __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.30.2.1 2014/08/10 06:56:16 tls 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.17 rmind size_t 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.19 rmind /* Rule ID and the original dictionary. */
109 1.19 rmind uint64_t r_id;
110 1.18 rmind prop_dictionary_t r_dict;
111 1.18 rmind
112 1.17 rmind /* Rule name and all-list entry. */
113 1.17 rmind char r_name[NPF_RULE_MAXNAMELEN];
114 1.17 rmind LIST_ENTRY(npf_rule) r_aentry;
115 1.17 rmind
116 1.17 rmind /* Key (optional). */
117 1.17 rmind uint8_t r_key[NPF_RULE_MAXKEYLEN];
118 1.1 rmind };
119 1.1 rmind
120 1.30.2.1 tls /*
121 1.30.2.1 tls * Private attributes - must be in the NPF_RULE_PRIVMASK range.
122 1.30.2.1 tls */
123 1.30.2.1 tls #define NPF_RULE_KEEPNAT (0x01000000 & NPF_RULE_PRIVMASK)
124 1.30.2.1 tls
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.18 rmind prop_dictionary_t rldict;
323 1.18 rmind prop_array_t rules;
324 1.18 rmind npf_rule_t *rg, *rl;
325 1.18 rmind
326 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
327 1.18 rmind return NULL;
328 1.18 rmind }
329 1.18 rmind if ((rldict = prop_dictionary_create()) == NULL) {
330 1.18 rmind return NULL;
331 1.18 rmind }
332 1.18 rmind if ((rules = prop_array_create()) == NULL) {
333 1.18 rmind prop_object_release(rldict);
334 1.18 rmind return NULL;
335 1.18 rmind }
336 1.18 rmind
337 1.18 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
338 1.24 rmind KASSERT(rl->r_parent == rg);
339 1.18 rmind if (rl->r_dict && !prop_array_add(rules, rl->r_dict)) {
340 1.18 rmind prop_object_release(rldict);
341 1.19 rmind prop_object_release(rules);
342 1.18 rmind return NULL;
343 1.18 rmind }
344 1.18 rmind }
345 1.19 rmind
346 1.18 rmind if (!prop_dictionary_set(rldict, "rules", rules)) {
347 1.18 rmind prop_object_release(rldict);
348 1.18 rmind rldict = NULL;
349 1.18 rmind }
350 1.18 rmind prop_object_release(rules);
351 1.18 rmind return rldict;
352 1.18 rmind }
353 1.18 rmind
354 1.18 rmind int
355 1.18 rmind npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
356 1.18 rmind {
357 1.18 rmind npf_rule_t *rg, *rl;
358 1.18 rmind
359 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
360 1.19 rmind return ESRCH;
361 1.18 rmind }
362 1.18 rmind while ((rl = TAILQ_FIRST(&rg->r_subset)) != NULL) {
363 1.24 rmind KASSERT(rl->r_parent == rg);
364 1.18 rmind npf_ruleset_unlink(rlset, rl);
365 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
366 1.18 rmind }
367 1.18 rmind return 0;
368 1.18 rmind }
369 1.18 rmind
370 1.18 rmind void
371 1.18 rmind npf_ruleset_gc(npf_ruleset_t *rlset)
372 1.18 rmind {
373 1.18 rmind npf_rule_t *rl;
374 1.18 rmind
375 1.18 rmind while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
376 1.18 rmind LIST_REMOVE(rl, r_aentry);
377 1.18 rmind npf_rule_free(rl);
378 1.18 rmind }
379 1.17 rmind }
380 1.17 rmind
381 1.17 rmind /*
382 1.30.2.1 tls * npf_ruleset_cmpnat: find a matching NAT policy in the ruleset.
383 1.30.2.1 tls */
384 1.30.2.1 tls static inline npf_rule_t *
385 1.30.2.1 tls npf_ruleset_cmpnat(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
386 1.30.2.1 tls {
387 1.30.2.1 tls npf_rule_t *rl;
388 1.30.2.1 tls
389 1.30.2.1 tls /* Find a matching NAT policy in the old ruleset. */
390 1.30.2.1 tls LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
391 1.30.2.1 tls if (rl->r_natp && npf_nat_cmppolicy(rl->r_natp, mnp))
392 1.30.2.1 tls break;
393 1.30.2.1 tls }
394 1.30.2.1 tls return rl;
395 1.30.2.1 tls }
396 1.30.2.1 tls
397 1.30.2.1 tls /*
398 1.30.2.1 tls * npf_ruleset_reload: prepare the new ruleset by scanning the active
399 1.30.2.1 tls * ruleset and 1) sharing the dynamic rules 2) sharing NAT policies.
400 1.17 rmind *
401 1.30.2.1 tls * => The active (old) ruleset should be exclusively locked.
402 1.17 rmind */
403 1.17 rmind void
404 1.30.2.1 tls npf_ruleset_reload(npf_ruleset_t *newset, npf_ruleset_t *oldset)
405 1.17 rmind {
406 1.30.2.1 tls npf_rule_t *rg, *rl;
407 1.30.2.1 tls uint64_t nid = 0;
408 1.17 rmind
409 1.17 rmind KASSERT(npf_config_locked_p());
410 1.17 rmind
411 1.30.2.1 tls /*
412 1.30.2.1 tls * Scan the dynamic rules and share (migrate) if needed.
413 1.30.2.1 tls */
414 1.30.2.1 tls LIST_FOREACH(rg, &newset->rs_dynamic, r_dentry) {
415 1.30.2.1 tls npf_rule_t *actrg;
416 1.18 rmind
417 1.30.2.1 tls /* Look for a dynamic ruleset group with such name. */
418 1.30.2.1 tls actrg = npf_ruleset_lookup(oldset, rg->r_name);
419 1.30.2.1 tls if (actrg == NULL) {
420 1.17 rmind continue;
421 1.17 rmind }
422 1.18 rmind
423 1.18 rmind /*
424 1.24 rmind * Copy the list-head structure. This is necessary because
425 1.24 rmind * the rules are still active and therefore accessible for
426 1.24 rmind * inspection via the old ruleset.
427 1.18 rmind */
428 1.30.2.1 tls memcpy(&rg->r_subset, &actrg->r_subset, sizeof(rg->r_subset));
429 1.19 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
430 1.24 rmind /*
431 1.24 rmind * We can safely migrate to the new all-rule list
432 1.24 rmind * and re-set the parent rule, though.
433 1.24 rmind */
434 1.18 rmind LIST_REMOVE(rl, r_aentry);
435 1.30.2.1 tls LIST_INSERT_HEAD(&newset->rs_all, rl, r_aentry);
436 1.19 rmind rl->r_parent = rg;
437 1.18 rmind }
438 1.1 rmind }
439 1.19 rmind
440 1.30.2.1 tls /*
441 1.30.2.1 tls * Scan all rules in the new ruleset and share NAT policies.
442 1.30.2.1 tls * Also, assign a unique ID for each policy here.
443 1.30.2.1 tls */
444 1.30.2.1 tls LIST_FOREACH(rl, &newset->rs_all, r_aentry) {
445 1.30.2.1 tls npf_natpolicy_t *np;
446 1.30.2.1 tls npf_rule_t *actrl;
447 1.1 rmind
448 1.30.2.1 tls /* Does the rule have a NAT policy associated? */
449 1.30.2.1 tls if ((np = rl->r_natp) == NULL) {
450 1.30.2.1 tls continue;
451 1.30.2.1 tls }
452 1.1 rmind
453 1.30.2.1 tls /* Does it match with any policy in the active ruleset? */
454 1.30.2.1 tls if ((actrl = npf_ruleset_cmpnat(oldset, np)) == NULL) {
455 1.30.2.1 tls npf_nat_setid(np, ++nid);
456 1.30.2.1 tls continue;
457 1.30.2.1 tls }
458 1.30.2.1 tls
459 1.30.2.1 tls /*
460 1.30.2.1 tls * Inherit the matching NAT policy and check other ones
461 1.30.2.1 tls * in the new ruleset for sharing the portmap.
462 1.30.2.1 tls */
463 1.30.2.1 tls rl->r_natp = actrl->r_natp;
464 1.30.2.1 tls npf_ruleset_sharepm(newset, rl->r_natp);
465 1.30.2.1 tls npf_nat_setid(rl->r_natp, ++nid);
466 1.30.2.1 tls
467 1.30.2.1 tls /*
468 1.30.2.1 tls * Finally, mark the active rule to not destroy its NAT
469 1.30.2.1 tls * policy later as we inherited it (but the rule must be
470 1.30.2.1 tls * kept active for now). Destroy the new/unused policy.
471 1.30.2.1 tls */
472 1.30.2.1 tls actrl->r_attr |= NPF_RULE_KEEPNAT;
473 1.30.2.1 tls npf_nat_freepolicy(np);
474 1.4 rmind }
475 1.30.2.1 tls
476 1.30.2.1 tls /* Inherit the ID counter. */
477 1.30.2.1 tls newset->rs_idcnt = oldset->rs_idcnt;
478 1.1 rmind }
479 1.1 rmind
480 1.6 rmind npf_rule_t *
481 1.6 rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
482 1.6 rmind {
483 1.6 rmind npf_natpolicy_t *np;
484 1.6 rmind npf_rule_t *rl;
485 1.6 rmind
486 1.6 rmind /* Find a matching NAT policy in the old ruleset. */
487 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
488 1.6 rmind /*
489 1.6 rmind * NAT policy might not yet be set during the creation of
490 1.6 rmind * the ruleset (in such case, rule is for our policy), or
491 1.6 rmind * policies might be equal due to rule exchange on reload.
492 1.6 rmind */
493 1.6 rmind np = rl->r_natp;
494 1.6 rmind if (np == NULL || np == mnp)
495 1.6 rmind continue;
496 1.6 rmind if (npf_nat_sharepm(np, mnp))
497 1.6 rmind break;
498 1.6 rmind }
499 1.6 rmind return rl;
500 1.6 rmind }
501 1.6 rmind
502 1.30.2.1 tls npf_natpolicy_t *
503 1.30.2.1 tls npf_ruleset_findnat(npf_ruleset_t *rlset, uint64_t id)
504 1.13 rmind {
505 1.13 rmind npf_rule_t *rl;
506 1.13 rmind
507 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
508 1.30.2.1 tls npf_natpolicy_t *np = rl->r_natp;
509 1.30.2.1 tls if (np && npf_nat_getid(np) == id) {
510 1.30.2.1 tls return np;
511 1.13 rmind }
512 1.13 rmind }
513 1.30.2.1 tls return NULL;
514 1.13 rmind }
515 1.13 rmind
516 1.13 rmind /*
517 1.30.2.1 tls * npf_ruleset_freealg: inspect the ruleset and disassociate specified
518 1.30.2.1 tls * ALG from all NAT entries using it.
519 1.1 rmind */
520 1.4 rmind void
521 1.30.2.1 tls npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
522 1.1 rmind {
523 1.30.2.1 tls npf_rule_t *rl;
524 1.30.2.1 tls npf_natpolicy_t *np;
525 1.30 rmind
526 1.30.2.1 tls LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
527 1.30.2.1 tls if ((np = rl->r_natp) != NULL) {
528 1.30.2.1 tls npf_nat_freealg(np, alg);
529 1.4 rmind }
530 1.1 rmind }
531 1.4 rmind }
532 1.4 rmind
533 1.1 rmind /*
534 1.25 rmind * npf_rule_alloc: allocate a rule and initialise it.
535 1.1 rmind */
536 1.4 rmind npf_rule_t *
537 1.17 rmind npf_rule_alloc(prop_dictionary_t rldict)
538 1.1 rmind {
539 1.4 rmind npf_rule_t *rl;
540 1.7 rmind const char *rname;
541 1.1 rmind
542 1.4 rmind /* Allocate a rule structure. */
543 1.11 rmind rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
544 1.17 rmind TAILQ_INIT(&rl->r_subset);
545 1.4 rmind rl->r_natp = NULL;
546 1.4 rmind
547 1.11 rmind /* Name (optional) */
548 1.7 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
549 1.17 rmind strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
550 1.7 rmind } else {
551 1.7 rmind rl->r_name[0] = '\0';
552 1.7 rmind }
553 1.7 rmind
554 1.11 rmind /* Attributes, priority and interface ID (optional). */
555 1.7 rmind prop_dictionary_get_uint32(rldict, "attributes", &rl->r_attr);
556 1.7 rmind prop_dictionary_get_int32(rldict, "priority", &rl->r_priority);
557 1.30.2.1 tls rl->r_attr &= ~NPF_RULE_PRIVMASK;
558 1.26 rmind
559 1.26 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "interface", &rname)) {
560 1.26 rmind if ((rl->r_ifid = npf_ifmap_register(rname)) == 0) {
561 1.26 rmind kmem_free(rl, sizeof(npf_rule_t));
562 1.26 rmind return NULL;
563 1.26 rmind }
564 1.26 rmind } else {
565 1.26 rmind rl->r_ifid = 0;
566 1.26 rmind }
567 1.4 rmind
568 1.17 rmind /* Get the skip-to index. No need to validate it. */
569 1.17 rmind prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
570 1.17 rmind
571 1.17 rmind /* Key (optional). */
572 1.17 rmind prop_object_t obj = prop_dictionary_get(rldict, "key");
573 1.17 rmind const void *key = prop_data_data_nocopy(obj);
574 1.17 rmind
575 1.17 rmind if (key) {
576 1.17 rmind size_t len = prop_data_size(obj);
577 1.17 rmind if (len > NPF_RULE_MAXKEYLEN) {
578 1.17 rmind kmem_free(rl, sizeof(npf_rule_t));
579 1.17 rmind return NULL;
580 1.17 rmind }
581 1.17 rmind memcpy(rl->r_key, key, len);
582 1.4 rmind }
583 1.18 rmind
584 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
585 1.18 rmind rl->r_dict = prop_dictionary_copy(rldict);
586 1.18 rmind }
587 1.18 rmind
588 1.17 rmind return rl;
589 1.17 rmind }
590 1.17 rmind
591 1.17 rmind /*
592 1.17 rmind * npf_rule_setcode: assign filter code to the rule.
593 1.17 rmind *
594 1.20 rmind * => The code must be validated by the caller.
595 1.20 rmind * => JIT compilation may be performed here.
596 1.17 rmind */
597 1.17 rmind void
598 1.17 rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
599 1.17 rmind {
600 1.25 rmind KASSERT(type == NPF_CODE_BPF);
601 1.28 rmind
602 1.28 rmind if ((rl->r_jcode = npf_bpf_compile(code, size)) == NULL) {
603 1.28 rmind rl->r_code = code;
604 1.28 rmind rl->r_clen = size;
605 1.28 rmind } else {
606 1.24 rmind rl->r_code = NULL;
607 1.20 rmind }
608 1.28 rmind rl->r_type = type;
609 1.17 rmind }
610 1.17 rmind
611 1.17 rmind /*
612 1.17 rmind * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
613 1.17 rmind */
614 1.17 rmind void
615 1.17 rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
616 1.17 rmind {
617 1.17 rmind npf_rproc_acquire(rp);
618 1.6 rmind rl->r_rproc = rp;
619 1.1 rmind }
620 1.1 rmind
621 1.1 rmind /*
622 1.1 rmind * npf_rule_free: free the specified rule.
623 1.1 rmind */
624 1.1 rmind void
625 1.1 rmind npf_rule_free(npf_rule_t *rl)
626 1.1 rmind {
627 1.4 rmind npf_natpolicy_t *np = rl->r_natp;
628 1.4 rmind npf_rproc_t *rp = rl->r_rproc;
629 1.1 rmind
630 1.30.2.1 tls if (np && (rl->r_attr & NPF_RULE_KEEPNAT) == 0) {
631 1.4 rmind /* Free NAT policy. */
632 1.4 rmind npf_nat_freepolicy(np);
633 1.4 rmind }
634 1.4 rmind if (rp) {
635 1.6 rmind /* Release rule procedure. */
636 1.4 rmind npf_rproc_release(rp);
637 1.4 rmind }
638 1.17 rmind if (rl->r_code) {
639 1.20 rmind /* Free byte-code. */
640 1.17 rmind kmem_free(rl->r_code, rl->r_clen);
641 1.1 rmind }
642 1.20 rmind if (rl->r_jcode) {
643 1.20 rmind /* Free JIT code. */
644 1.28 rmind bpf_jit_freecode(rl->r_jcode);
645 1.20 rmind }
646 1.18 rmind if (rl->r_dict) {
647 1.18 rmind /* Destroy the dictionary. */
648 1.18 rmind prop_object_release(rl->r_dict);
649 1.18 rmind }
650 1.4 rmind kmem_free(rl, sizeof(npf_rule_t));
651 1.1 rmind }
652 1.1 rmind
653 1.1 rmind /*
654 1.19 rmind * npf_rule_getid: return the unique ID of a rule.
655 1.10 rmind * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
656 1.1 rmind * npf_rule_getnat: get NAT policy assigned to the rule.
657 1.1 rmind */
658 1.1 rmind
659 1.19 rmind uint64_t
660 1.19 rmind npf_rule_getid(const npf_rule_t *rl)
661 1.19 rmind {
662 1.19 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
663 1.19 rmind return rl->r_id;
664 1.19 rmind }
665 1.19 rmind
666 1.10 rmind npf_rproc_t *
667 1.30 rmind npf_rule_getrproc(const npf_rule_t *rl)
668 1.10 rmind {
669 1.10 rmind npf_rproc_t *rp = rl->r_rproc;
670 1.10 rmind
671 1.10 rmind if (rp) {
672 1.10 rmind npf_rproc_acquire(rp);
673 1.10 rmind }
674 1.10 rmind return rp;
675 1.10 rmind }
676 1.10 rmind
677 1.1 rmind npf_natpolicy_t *
678 1.1 rmind npf_rule_getnat(const npf_rule_t *rl)
679 1.1 rmind {
680 1.4 rmind return rl->r_natp;
681 1.1 rmind }
682 1.1 rmind
683 1.4 rmind /*
684 1.4 rmind * npf_rule_setnat: assign NAT policy to the rule and insert into the
685 1.4 rmind * NAT policy list in the ruleset.
686 1.4 rmind */
687 1.1 rmind void
688 1.1 rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
689 1.1 rmind {
690 1.4 rmind KASSERT(rl->r_natp == NULL);
691 1.4 rmind rl->r_natp = np;
692 1.1 rmind }
693 1.1 rmind
694 1.17 rmind /*
695 1.17 rmind * npf_rule_inspect: match the interface, direction and run the filter code.
696 1.29 rmind * Returns true if rule matches and false otherwise.
697 1.17 rmind */
698 1.17 rmind static inline bool
699 1.29 rmind npf_rule_inspect(const npf_rule_t *rl, bpf_args_t *bc_args,
700 1.29 rmind const int di_mask, const u_int ifid)
701 1.17 rmind {
702 1.17 rmind /* Match the interface. */
703 1.29 rmind if (rl->r_ifid && rl->r_ifid != ifid) {
704 1.17 rmind return false;
705 1.17 rmind }
706 1.17 rmind
707 1.17 rmind /* Match the direction. */
708 1.17 rmind if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
709 1.17 rmind if ((rl->r_attr & di_mask) == 0)
710 1.17 rmind return false;
711 1.17 rmind }
712 1.17 rmind
713 1.24 rmind /* Any code? */
714 1.24 rmind if (rl->r_jcode == rl->r_code) {
715 1.24 rmind KASSERT(rl->r_jcode == NULL);
716 1.24 rmind KASSERT(rl->r_code == NULL);
717 1.17 rmind return true;
718 1.17 rmind }
719 1.25 rmind KASSERT(rl->r_type == NPF_CODE_BPF);
720 1.29 rmind return npf_bpf_filter(bc_args, rl->r_code, rl->r_jcode) != 0;
721 1.17 rmind }
722 1.17 rmind
723 1.17 rmind /*
724 1.17 rmind * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
725 1.17 rmind * This is only for the dynamic rules. Subrules cannot have nested rules.
726 1.17 rmind */
727 1.17 rmind static npf_rule_t *
728 1.29 rmind npf_rule_reinspect(const npf_rule_t *drl, bpf_args_t *bc_args,
729 1.29 rmind const int di_mask, const u_int ifid)
730 1.7 rmind {
731 1.17 rmind npf_rule_t *final_rl = NULL, *rl;
732 1.17 rmind
733 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(drl->r_attr));
734 1.7 rmind
735 1.17 rmind TAILQ_FOREACH(rl, &drl->r_subset, r_entry) {
736 1.29 rmind if (!npf_rule_inspect(rl, bc_args, di_mask, ifid)) {
737 1.7 rmind continue;
738 1.17 rmind }
739 1.17 rmind if (rl->r_attr & NPF_RULE_FINAL) {
740 1.17 rmind return rl;
741 1.17 rmind }
742 1.17 rmind final_rl = rl;
743 1.7 rmind }
744 1.17 rmind return final_rl;
745 1.7 rmind }
746 1.1 rmind
747 1.1 rmind /*
748 1.7 rmind * npf_ruleset_inspect: inspect the packet against the given ruleset.
749 1.1 rmind *
750 1.25 rmind * Loop through the rules in the set and run the byte-code of each rule
751 1.7 rmind * against the packet (nbuf chain). If sub-ruleset is found, inspect it.
752 1.1 rmind */
753 1.1 rmind npf_rule_t *
754 1.30.2.1 tls npf_ruleset_inspect(npf_cache_t *npc, const npf_ruleset_t *rlset,
755 1.30.2.1 tls const int di, const int layer)
756 1.1 rmind {
757 1.30.2.1 tls nbuf_t *nbuf = npc->npc_nbuf;
758 1.7 rmind const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
759 1.17 rmind const u_int nitems = rlset->rs_nitems;
760 1.29 rmind const u_int ifid = nbuf->nb_ifid;
761 1.17 rmind npf_rule_t *final_rl = NULL;
762 1.29 rmind bpf_args_t bc_args;
763 1.17 rmind u_int n = 0;
764 1.1 rmind
765 1.1 rmind KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
766 1.17 rmind
767 1.30.2.1 tls /*
768 1.30.2.1 tls * Prepare the external memory store and the arguments for
769 1.30.2.1 tls * the BPF programs to be executed.
770 1.30.2.1 tls */
771 1.30.2.1 tls uint32_t bc_words[NPF_BPF_NWORDS];
772 1.30.2.1 tls npf_bpf_prepare(npc, &bc_args, bc_words);
773 1.30.2.1 tls
774 1.17 rmind while (n < nitems) {
775 1.17 rmind npf_rule_t *rl = rlset->rs_rules[n];
776 1.17 rmind const u_int skip_to = rl->r_skip_to;
777 1.17 rmind const uint32_t attr = rl->r_attr;
778 1.17 rmind
779 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
780 1.1 rmind KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
781 1.17 rmind KASSERT(n < skip_to);
782 1.1 rmind
783 1.17 rmind /* Group is a barrier: return a matching if found any. */
784 1.17 rmind if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
785 1.17 rmind break;
786 1.17 rmind }
787 1.17 rmind
788 1.17 rmind /* Main inspection of the rule. */
789 1.29 rmind if (!npf_rule_inspect(rl, &bc_args, di_mask, ifid)) {
790 1.17 rmind n = skip_to;
791 1.1 rmind continue;
792 1.1 rmind }
793 1.17 rmind
794 1.17 rmind if (NPF_DYNAMIC_GROUP_P(attr)) {
795 1.17 rmind /*
796 1.17 rmind * If this is a dynamic rule, re-inspect the subrules.
797 1.17 rmind * If it has any matching rule, then it is final.
798 1.17 rmind */
799 1.29 rmind rl = npf_rule_reinspect(rl, &bc_args, di_mask, ifid);
800 1.17 rmind if (rl != NULL) {
801 1.17 rmind final_rl = rl;
802 1.17 rmind break;
803 1.17 rmind }
804 1.17 rmind } else if ((attr & NPF_RULE_GROUP) == 0) {
805 1.17 rmind /*
806 1.17 rmind * Groups themselves are not matching.
807 1.17 rmind */
808 1.17 rmind final_rl = rl;
809 1.1 rmind }
810 1.17 rmind
811 1.1 rmind /* Set the matching rule and check for "final". */
812 1.17 rmind if (attr & NPF_RULE_FINAL) {
813 1.2 rmind break;
814 1.1 rmind }
815 1.17 rmind n++;
816 1.2 rmind }
817 1.16 rmind
818 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
819 1.7 rmind return final_rl;
820 1.1 rmind }
821 1.1 rmind
822 1.1 rmind /*
823 1.17 rmind * npf_rule_conclude: return decision and the flags for conclusion.
824 1.1 rmind *
825 1.1 rmind * => Returns ENETUNREACH if "block" and 0 if "pass".
826 1.1 rmind */
827 1.1 rmind int
828 1.17 rmind npf_rule_conclude(const npf_rule_t *rl, int *retfl)
829 1.1 rmind {
830 1.1 rmind /* If not passing - drop the packet. */
831 1.4 rmind *retfl = rl->r_attr;
832 1.17 rmind return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
833 1.1 rmind }
834