npf_ruleset.c revision 1.22 1 1.22 rmind /* $NetBSD: npf_ruleset.c,v 1.22 2013/08/30 15:00:08 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.22 rmind __KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.22 2013/08/30 15:00:08 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_ncode.h"
54 1.1 rmind #include "npf_impl.h"
55 1.1 rmind
56 1.4 rmind struct npf_ruleset {
57 1.18 rmind /*
58 1.18 rmind * - List of all rules.
59 1.18 rmind * - Dynamic (i.e. named) rules.
60 1.18 rmind * - G/C list for convenience.
61 1.18 rmind */
62 1.17 rmind LIST_HEAD(, npf_rule) rs_all;
63 1.17 rmind LIST_HEAD(, npf_rule) rs_dynamic;
64 1.18 rmind LIST_HEAD(, npf_rule) rs_gc;
65 1.17 rmind
66 1.19 rmind /* Unique ID counter. */
67 1.19 rmind uint64_t rs_idcnt;
68 1.19 rmind
69 1.17 rmind /* Number of array slots and active rules. */
70 1.17 rmind u_int rs_slots;
71 1.17 rmind u_int rs_nitems;
72 1.17 rmind
73 1.17 rmind /* Array of ordered rules. */
74 1.17 rmind npf_rule_t * rs_rules[];
75 1.4 rmind };
76 1.4 rmind
77 1.1 rmind struct npf_rule {
78 1.17 rmind /* Attributes, interface and skip slot. */
79 1.4 rmind uint32_t r_attr;
80 1.4 rmind u_int r_ifid;
81 1.17 rmind u_int r_skip_to;
82 1.17 rmind
83 1.17 rmind /* Code to process, if any. */
84 1.17 rmind int r_type;
85 1.20 rmind bpfjit_function_t r_jcode;
86 1.17 rmind void * r_code;
87 1.17 rmind size_t r_clen;
88 1.17 rmind
89 1.17 rmind /* NAT policy (optional), rule procedure and subset. */
90 1.17 rmind npf_natpolicy_t * r_natp;
91 1.4 rmind npf_rproc_t * r_rproc;
92 1.17 rmind
93 1.17 rmind /* Rule priority: (highest) 1, 2 ... n (lowest). */
94 1.17 rmind pri_t r_priority;
95 1.17 rmind
96 1.17 rmind /*
97 1.17 rmind * Dynamic group: subset queue and a dynamic group list entry.
98 1.17 rmind * Dynamic rule: entry and the parent rule (the group).
99 1.17 rmind */
100 1.17 rmind union {
101 1.17 rmind TAILQ_HEAD(npf_ruleq, npf_rule) r_subset;
102 1.17 rmind TAILQ_ENTRY(npf_rule) r_entry;
103 1.17 rmind } /* C11 */;
104 1.17 rmind union {
105 1.17 rmind LIST_ENTRY(npf_rule) r_dentry;
106 1.17 rmind npf_rule_t * r_parent;
107 1.17 rmind } /* C11 */;
108 1.17 rmind
109 1.19 rmind /* Rule ID and the original dictionary. */
110 1.19 rmind uint64_t r_id;
111 1.18 rmind prop_dictionary_t r_dict;
112 1.18 rmind
113 1.17 rmind /* Rule name and all-list entry. */
114 1.17 rmind char r_name[NPF_RULE_MAXNAMELEN];
115 1.17 rmind LIST_ENTRY(npf_rule) r_aentry;
116 1.17 rmind
117 1.17 rmind /* Key (optional). */
118 1.17 rmind uint8_t r_key[NPF_RULE_MAXKEYLEN];
119 1.1 rmind };
120 1.1 rmind
121 1.17 rmind #define NPF_DYNAMIC_GROUP_P(attr) \
122 1.17 rmind (((attr) & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP)
123 1.17 rmind
124 1.19 rmind #define NPF_DYNAMIC_RULE_P(attr) \
125 1.19 rmind (((attr) & NPF_DYNAMIC_GROUP) == NPF_RULE_DYNAMIC)
126 1.19 rmind
127 1.1 rmind npf_ruleset_t *
128 1.17 rmind npf_ruleset_create(size_t slots)
129 1.1 rmind {
130 1.17 rmind size_t len = offsetof(npf_ruleset_t, rs_rules[slots]);
131 1.1 rmind npf_ruleset_t *rlset;
132 1.1 rmind
133 1.17 rmind rlset = kmem_zalloc(len, KM_SLEEP);
134 1.17 rmind LIST_INIT(&rlset->rs_dynamic);
135 1.17 rmind LIST_INIT(&rlset->rs_all);
136 1.19 rmind LIST_INIT(&rlset->rs_gc);
137 1.19 rmind rlset->rs_slots = slots;
138 1.19 rmind
139 1.1 rmind return rlset;
140 1.1 rmind }
141 1.1 rmind
142 1.17 rmind static void
143 1.17 rmind npf_ruleset_unlink(npf_ruleset_t *rlset, npf_rule_t *rl)
144 1.17 rmind {
145 1.17 rmind if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
146 1.17 rmind LIST_REMOVE(rl, r_dentry);
147 1.17 rmind }
148 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
149 1.17 rmind npf_rule_t *rg = rl->r_parent;
150 1.17 rmind TAILQ_REMOVE(&rg->r_subset, rl, r_entry);
151 1.17 rmind }
152 1.17 rmind LIST_REMOVE(rl, r_aentry);
153 1.17 rmind }
154 1.17 rmind
155 1.1 rmind void
156 1.1 rmind npf_ruleset_destroy(npf_ruleset_t *rlset)
157 1.1 rmind {
158 1.17 rmind size_t len = offsetof(npf_ruleset_t, rs_rules[rlset->rs_slots]);
159 1.1 rmind npf_rule_t *rl;
160 1.1 rmind
161 1.17 rmind while ((rl = LIST_FIRST(&rlset->rs_all)) != NULL) {
162 1.17 rmind npf_ruleset_unlink(rlset, rl);
163 1.1 rmind npf_rule_free(rl);
164 1.1 rmind }
165 1.17 rmind KASSERT(LIST_EMPTY(&rlset->rs_dynamic));
166 1.18 rmind KASSERT(LIST_EMPTY(&rlset->rs_gc));
167 1.17 rmind kmem_free(rlset, len);
168 1.1 rmind }
169 1.1 rmind
170 1.1 rmind /*
171 1.1 rmind * npf_ruleset_insert: insert the rule into the specified ruleset.
172 1.1 rmind */
173 1.1 rmind void
174 1.1 rmind npf_ruleset_insert(npf_ruleset_t *rlset, npf_rule_t *rl)
175 1.1 rmind {
176 1.17 rmind u_int n = rlset->rs_nitems;
177 1.17 rmind
178 1.17 rmind KASSERT(n < rlset->rs_slots);
179 1.17 rmind
180 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
181 1.17 rmind if (NPF_DYNAMIC_GROUP_P(rl->r_attr)) {
182 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_dynamic, rl, r_dentry);
183 1.17 rmind }
184 1.17 rmind
185 1.17 rmind rlset->rs_rules[n] = rl;
186 1.17 rmind rlset->rs_nitems++;
187 1.17 rmind
188 1.17 rmind if (rl->r_skip_to < ++n) {
189 1.17 rmind rl->r_skip_to = n;
190 1.17 rmind }
191 1.17 rmind }
192 1.17 rmind
193 1.17 rmind static npf_rule_t *
194 1.17 rmind npf_ruleset_lookup(npf_ruleset_t *rlset, const char *name)
195 1.17 rmind {
196 1.17 rmind npf_rule_t *rl;
197 1.17 rmind
198 1.17 rmind KASSERT(npf_config_locked_p());
199 1.17 rmind
200 1.17 rmind LIST_FOREACH(rl, &rlset->rs_dynamic, r_dentry) {
201 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(rl->r_attr));
202 1.17 rmind if (strncmp(rl->r_name, name, NPF_RULE_MAXNAMELEN) == 0)
203 1.17 rmind break;
204 1.17 rmind }
205 1.17 rmind return rl;
206 1.17 rmind }
207 1.17 rmind
208 1.17 rmind int
209 1.17 rmind npf_ruleset_add(npf_ruleset_t *rlset, const char *rname, npf_rule_t *rl)
210 1.17 rmind {
211 1.17 rmind npf_rule_t *rg, *it;
212 1.17 rmind pri_t priocmd;
213 1.17 rmind
214 1.17 rmind rg = npf_ruleset_lookup(rlset, rname);
215 1.17 rmind if (rg == NULL) {
216 1.19 rmind return ESRCH;
217 1.19 rmind }
218 1.19 rmind if (!NPF_DYNAMIC_RULE_P(rl->r_attr)) {
219 1.19 rmind return EINVAL;
220 1.17 rmind }
221 1.17 rmind
222 1.19 rmind /* Dynamic rule - assign a unique ID and save the parent. */
223 1.19 rmind rl->r_id = ++rlset->rs_idcnt;
224 1.17 rmind rl->r_parent = rg;
225 1.17 rmind
226 1.17 rmind /*
227 1.17 rmind * Rule priority: (highest) 1, 2 ... n (lowest).
228 1.17 rmind * Negative priority indicates an operation and is reset to zero.
229 1.17 rmind */
230 1.17 rmind if ((priocmd = rl->r_priority) < 0) {
231 1.17 rmind rl->r_priority = 0;
232 1.17 rmind }
233 1.17 rmind
234 1.17 rmind switch (priocmd) {
235 1.17 rmind case NPF_PRI_FIRST:
236 1.17 rmind TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
237 1.17 rmind if (rl->r_priority <= it->r_priority)
238 1.17 rmind break;
239 1.17 rmind }
240 1.17 rmind if (it) {
241 1.17 rmind TAILQ_INSERT_BEFORE(it, rl, r_entry);
242 1.17 rmind } else {
243 1.17 rmind TAILQ_INSERT_HEAD(&rg->r_subset, rl, r_entry);
244 1.17 rmind }
245 1.17 rmind break;
246 1.17 rmind case NPF_PRI_LAST:
247 1.17 rmind default:
248 1.17 rmind TAILQ_FOREACH(it, &rg->r_subset, r_entry) {
249 1.17 rmind if (rl->r_priority < it->r_priority)
250 1.17 rmind break;
251 1.17 rmind }
252 1.17 rmind if (it) {
253 1.17 rmind TAILQ_INSERT_BEFORE(it, rl, r_entry);
254 1.17 rmind } else {
255 1.17 rmind TAILQ_INSERT_TAIL(&rg->r_subset, rl, r_entry);
256 1.17 rmind }
257 1.17 rmind break;
258 1.17 rmind }
259 1.17 rmind
260 1.17 rmind /* Finally, add into the all-list. */
261 1.17 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
262 1.17 rmind return 0;
263 1.17 rmind }
264 1.17 rmind
265 1.18 rmind int
266 1.19 rmind npf_ruleset_remove(npf_ruleset_t *rlset, const char *rname, uint64_t id)
267 1.17 rmind {
268 1.17 rmind npf_rule_t *rg, *rl;
269 1.17 rmind
270 1.17 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
271 1.19 rmind return ESRCH;
272 1.17 rmind }
273 1.17 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
274 1.17 rmind /* Compare ID. On match, remove and return. */
275 1.19 rmind if (rl->r_id == id) {
276 1.17 rmind npf_ruleset_unlink(rlset, rl);
277 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
278 1.19 rmind return 0;
279 1.17 rmind }
280 1.17 rmind }
281 1.19 rmind return ENOENT;
282 1.17 rmind }
283 1.17 rmind
284 1.18 rmind int
285 1.17 rmind npf_ruleset_remkey(npf_ruleset_t *rlset, const char *rname,
286 1.17 rmind const void *key, size_t len)
287 1.17 rmind {
288 1.17 rmind npf_rule_t *rg, *rl;
289 1.1 rmind
290 1.17 rmind KASSERT(len && len <= NPF_RULE_MAXKEYLEN);
291 1.17 rmind
292 1.17 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
293 1.19 rmind return ESRCH;
294 1.17 rmind }
295 1.18 rmind
296 1.17 rmind /* Find the last in the list. */
297 1.17 rmind TAILQ_FOREACH_REVERSE(rl, &rg->r_subset, npf_ruleq, r_entry) {
298 1.17 rmind /* Compare the key. On match, remove and return. */
299 1.17 rmind if (memcmp(rl->r_key, key, len) == 0) {
300 1.17 rmind npf_ruleset_unlink(rlset, rl);
301 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
302 1.19 rmind return 0;
303 1.17 rmind }
304 1.1 rmind }
305 1.19 rmind return ENOENT;
306 1.18 rmind }
307 1.18 rmind
308 1.18 rmind prop_dictionary_t
309 1.18 rmind npf_ruleset_list(npf_ruleset_t *rlset, const char *rname)
310 1.18 rmind {
311 1.18 rmind prop_dictionary_t rldict;
312 1.18 rmind prop_array_t rules;
313 1.18 rmind npf_rule_t *rg, *rl;
314 1.18 rmind
315 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
316 1.18 rmind return NULL;
317 1.18 rmind }
318 1.18 rmind if ((rldict = prop_dictionary_create()) == NULL) {
319 1.18 rmind return NULL;
320 1.18 rmind }
321 1.18 rmind if ((rules = prop_array_create()) == NULL) {
322 1.18 rmind prop_object_release(rldict);
323 1.18 rmind return NULL;
324 1.18 rmind }
325 1.18 rmind
326 1.18 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
327 1.18 rmind if (rl->r_dict && !prop_array_add(rules, rl->r_dict)) {
328 1.18 rmind prop_object_release(rldict);
329 1.19 rmind prop_object_release(rules);
330 1.18 rmind return NULL;
331 1.18 rmind }
332 1.18 rmind }
333 1.19 rmind
334 1.18 rmind if (!prop_dictionary_set(rldict, "rules", rules)) {
335 1.18 rmind prop_object_release(rldict);
336 1.18 rmind rldict = NULL;
337 1.18 rmind }
338 1.18 rmind prop_object_release(rules);
339 1.18 rmind return rldict;
340 1.18 rmind }
341 1.18 rmind
342 1.18 rmind int
343 1.18 rmind npf_ruleset_flush(npf_ruleset_t *rlset, const char *rname)
344 1.18 rmind {
345 1.18 rmind npf_rule_t *rg, *rl;
346 1.18 rmind
347 1.18 rmind if ((rg = npf_ruleset_lookup(rlset, rname)) == NULL) {
348 1.19 rmind return ESRCH;
349 1.18 rmind }
350 1.18 rmind while ((rl = TAILQ_FIRST(&rg->r_subset)) != NULL) {
351 1.18 rmind npf_ruleset_unlink(rlset, rl);
352 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_gc, rl, r_aentry);
353 1.18 rmind }
354 1.18 rmind return 0;
355 1.18 rmind }
356 1.18 rmind
357 1.18 rmind void
358 1.18 rmind npf_ruleset_gc(npf_ruleset_t *rlset)
359 1.18 rmind {
360 1.18 rmind npf_rule_t *rl;
361 1.18 rmind
362 1.18 rmind while ((rl = LIST_FIRST(&rlset->rs_gc)) != NULL) {
363 1.18 rmind LIST_REMOVE(rl, r_aentry);
364 1.18 rmind npf_rule_free(rl);
365 1.18 rmind }
366 1.17 rmind }
367 1.17 rmind
368 1.17 rmind /*
369 1.17 rmind * npf_ruleset_reload: share the dynamic rules.
370 1.17 rmind *
371 1.17 rmind * => Active ruleset should be exclusively locked.
372 1.17 rmind */
373 1.17 rmind void
374 1.18 rmind npf_ruleset_reload(npf_ruleset_t *rlset, npf_ruleset_t *arlset)
375 1.17 rmind {
376 1.19 rmind npf_rule_t *rg;
377 1.17 rmind
378 1.17 rmind KASSERT(npf_config_locked_p());
379 1.17 rmind
380 1.19 rmind LIST_FOREACH(rg, &rlset->rs_dynamic, r_dentry) {
381 1.19 rmind npf_rule_t *arg, *rl;
382 1.18 rmind
383 1.19 rmind if ((arg = npf_ruleset_lookup(arlset, rg->r_name)) == NULL) {
384 1.17 rmind continue;
385 1.17 rmind }
386 1.18 rmind
387 1.18 rmind /*
388 1.18 rmind * Copy the list-head structure and move the rules from the
389 1.18 rmind * old ruleset to the new by reinserting to a new all-rules
390 1.19 rmind * list and resetting the parent rule. Note that the rules
391 1.19 rmind * are still active and therefore accessible for inspection
392 1.19 rmind * via the old ruleset.
393 1.18 rmind */
394 1.19 rmind memcpy(&rg->r_subset, &arg->r_subset, sizeof(rg->r_subset));
395 1.19 rmind TAILQ_FOREACH(rl, &rg->r_subset, r_entry) {
396 1.18 rmind LIST_REMOVE(rl, r_aentry);
397 1.18 rmind LIST_INSERT_HEAD(&rlset->rs_all, rl, r_aentry);
398 1.19 rmind rl->r_parent = rg;
399 1.18 rmind }
400 1.1 rmind }
401 1.19 rmind
402 1.19 rmind /* Inherit the ID counter. */
403 1.19 rmind rlset->rs_idcnt = arlset->rs_idcnt;
404 1.1 rmind }
405 1.1 rmind
406 1.1 rmind /*
407 1.4 rmind * npf_ruleset_matchnat: find a matching NAT policy in the ruleset.
408 1.1 rmind */
409 1.4 rmind npf_rule_t *
410 1.4 rmind npf_ruleset_matchnat(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
411 1.1 rmind {
412 1.4 rmind npf_rule_t *rl;
413 1.1 rmind
414 1.4 rmind /* Find a matching NAT policy in the old ruleset. */
415 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
416 1.4 rmind if (npf_nat_matchpolicy(rl->r_natp, mnp))
417 1.4 rmind break;
418 1.4 rmind }
419 1.4 rmind return rl;
420 1.1 rmind }
421 1.1 rmind
422 1.6 rmind npf_rule_t *
423 1.6 rmind npf_ruleset_sharepm(npf_ruleset_t *rlset, npf_natpolicy_t *mnp)
424 1.6 rmind {
425 1.6 rmind npf_natpolicy_t *np;
426 1.6 rmind npf_rule_t *rl;
427 1.6 rmind
428 1.6 rmind /* Find a matching NAT policy in the old ruleset. */
429 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
430 1.6 rmind /*
431 1.6 rmind * NAT policy might not yet be set during the creation of
432 1.6 rmind * the ruleset (in such case, rule is for our policy), or
433 1.6 rmind * policies might be equal due to rule exchange on reload.
434 1.6 rmind */
435 1.6 rmind np = rl->r_natp;
436 1.6 rmind if (np == NULL || np == mnp)
437 1.6 rmind continue;
438 1.6 rmind if (npf_nat_sharepm(np, mnp))
439 1.6 rmind break;
440 1.6 rmind }
441 1.6 rmind return rl;
442 1.6 rmind }
443 1.6 rmind
444 1.1 rmind /*
445 1.13 rmind * npf_ruleset_freealg: inspect the ruleset and disassociate specified
446 1.13 rmind * ALG from all NAT entries using it.
447 1.13 rmind */
448 1.13 rmind void
449 1.13 rmind npf_ruleset_freealg(npf_ruleset_t *rlset, npf_alg_t *alg)
450 1.13 rmind {
451 1.13 rmind npf_rule_t *rl;
452 1.17 rmind npf_natpolicy_t *np;
453 1.13 rmind
454 1.17 rmind LIST_FOREACH(rl, &rlset->rs_all, r_aentry) {
455 1.17 rmind if ((np = rl->r_natp) != NULL) {
456 1.13 rmind npf_nat_freealg(np, alg);
457 1.13 rmind }
458 1.13 rmind }
459 1.13 rmind }
460 1.13 rmind
461 1.13 rmind /*
462 1.4 rmind * npf_ruleset_natreload: minimum reload of NAT policies by maching
463 1.6 rmind * two (active and new) NAT rulesets.
464 1.4 rmind *
465 1.4 rmind * => Active ruleset should be exclusively locked.
466 1.1 rmind */
467 1.4 rmind void
468 1.4 rmind npf_ruleset_natreload(npf_ruleset_t *nrlset, npf_ruleset_t *arlset)
469 1.1 rmind {
470 1.4 rmind npf_natpolicy_t *np, *anp;
471 1.4 rmind npf_rule_t *rl, *arl;
472 1.4 rmind
473 1.4 rmind /* Scan a new NAT ruleset against NAT policies in old ruleset. */
474 1.17 rmind LIST_FOREACH(rl, &nrlset->rs_all, r_aentry) {
475 1.4 rmind np = rl->r_natp;
476 1.4 rmind arl = npf_ruleset_matchnat(arlset, np);
477 1.4 rmind if (arl == NULL) {
478 1.4 rmind continue;
479 1.4 rmind }
480 1.4 rmind /* On match - we exchange NAT policies. */
481 1.4 rmind anp = arl->r_natp;
482 1.4 rmind rl->r_natp = anp;
483 1.4 rmind arl->r_natp = np;
484 1.6 rmind /* Update other NAT policies to share portmap. */
485 1.6 rmind (void)npf_ruleset_sharepm(nrlset, anp);
486 1.1 rmind }
487 1.4 rmind }
488 1.4 rmind
489 1.1 rmind /*
490 1.6 rmind * npf_rule_alloc: allocate a rule and copy n-code from user-space.
491 1.1 rmind */
492 1.4 rmind npf_rule_t *
493 1.17 rmind npf_rule_alloc(prop_dictionary_t rldict)
494 1.1 rmind {
495 1.4 rmind npf_rule_t *rl;
496 1.7 rmind const char *rname;
497 1.1 rmind
498 1.4 rmind /* Allocate a rule structure. */
499 1.11 rmind rl = kmem_zalloc(sizeof(npf_rule_t), KM_SLEEP);
500 1.17 rmind TAILQ_INIT(&rl->r_subset);
501 1.4 rmind rl->r_natp = NULL;
502 1.4 rmind
503 1.11 rmind /* Name (optional) */
504 1.7 rmind if (prop_dictionary_get_cstring_nocopy(rldict, "name", &rname)) {
505 1.17 rmind strlcpy(rl->r_name, rname, NPF_RULE_MAXNAMELEN);
506 1.7 rmind } else {
507 1.7 rmind rl->r_name[0] = '\0';
508 1.7 rmind }
509 1.7 rmind
510 1.11 rmind /* Attributes, priority and interface ID (optional). */
511 1.7 rmind prop_dictionary_get_uint32(rldict, "attributes", &rl->r_attr);
512 1.7 rmind prop_dictionary_get_int32(rldict, "priority", &rl->r_priority);
513 1.7 rmind prop_dictionary_get_uint32(rldict, "interface", &rl->r_ifid);
514 1.4 rmind
515 1.17 rmind /* Get the skip-to index. No need to validate it. */
516 1.17 rmind prop_dictionary_get_uint32(rldict, "skip-to", &rl->r_skip_to);
517 1.17 rmind
518 1.17 rmind /* Key (optional). */
519 1.17 rmind prop_object_t obj = prop_dictionary_get(rldict, "key");
520 1.17 rmind const void *key = prop_data_data_nocopy(obj);
521 1.17 rmind
522 1.17 rmind if (key) {
523 1.17 rmind size_t len = prop_data_size(obj);
524 1.17 rmind if (len > NPF_RULE_MAXKEYLEN) {
525 1.17 rmind kmem_free(rl, sizeof(npf_rule_t));
526 1.17 rmind return NULL;
527 1.17 rmind }
528 1.17 rmind memcpy(rl->r_key, key, len);
529 1.4 rmind }
530 1.18 rmind
531 1.19 rmind if (NPF_DYNAMIC_RULE_P(rl->r_attr)) {
532 1.18 rmind rl->r_dict = prop_dictionary_copy(rldict);
533 1.18 rmind }
534 1.18 rmind
535 1.17 rmind return rl;
536 1.17 rmind }
537 1.17 rmind
538 1.17 rmind /*
539 1.17 rmind * npf_rule_setcode: assign filter code to the rule.
540 1.17 rmind *
541 1.20 rmind * => The code must be validated by the caller.
542 1.20 rmind * => JIT compilation may be performed here.
543 1.17 rmind */
544 1.17 rmind void
545 1.17 rmind npf_rule_setcode(npf_rule_t *rl, const int type, void *code, size_t size)
546 1.17 rmind {
547 1.20 rmind /* Perform BPF JIT if possible. */
548 1.20 rmind if (type == NPF_CODE_BPF && (membar_consumer(),
549 1.20 rmind bpfjit_module_ops.bj_generate_code != NULL)) {
550 1.20 rmind KASSERT(rl->r_jcode == NULL);
551 1.20 rmind rl->r_jcode = bpfjit_module_ops.bj_generate_code(code, size);
552 1.20 rmind }
553 1.17 rmind rl->r_type = type;
554 1.17 rmind rl->r_code = code;
555 1.17 rmind rl->r_clen = size;
556 1.17 rmind }
557 1.17 rmind
558 1.17 rmind /*
559 1.17 rmind * npf_rule_setrproc: assign a rule procedure and hold a reference on it.
560 1.17 rmind */
561 1.17 rmind void
562 1.17 rmind npf_rule_setrproc(npf_rule_t *rl, npf_rproc_t *rp)
563 1.17 rmind {
564 1.17 rmind npf_rproc_acquire(rp);
565 1.6 rmind rl->r_rproc = rp;
566 1.1 rmind }
567 1.1 rmind
568 1.1 rmind /*
569 1.1 rmind * npf_rule_free: free the specified rule.
570 1.1 rmind */
571 1.1 rmind void
572 1.1 rmind npf_rule_free(npf_rule_t *rl)
573 1.1 rmind {
574 1.4 rmind npf_natpolicy_t *np = rl->r_natp;
575 1.4 rmind npf_rproc_t *rp = rl->r_rproc;
576 1.1 rmind
577 1.4 rmind if (np) {
578 1.4 rmind /* Free NAT policy. */
579 1.4 rmind npf_nat_freepolicy(np);
580 1.4 rmind }
581 1.4 rmind if (rp) {
582 1.6 rmind /* Release rule procedure. */
583 1.4 rmind npf_rproc_release(rp);
584 1.4 rmind }
585 1.17 rmind if (rl->r_code) {
586 1.20 rmind /* Free byte-code. */
587 1.17 rmind kmem_free(rl->r_code, rl->r_clen);
588 1.1 rmind }
589 1.20 rmind if (rl->r_jcode) {
590 1.20 rmind /* Free JIT code. */
591 1.20 rmind KASSERT(bpfjit_module_ops.bj_free_code != NULL);
592 1.20 rmind bpfjit_module_ops.bj_free_code(rl->r_jcode);
593 1.20 rmind }
594 1.18 rmind if (rl->r_dict) {
595 1.18 rmind /* Destroy the dictionary. */
596 1.18 rmind prop_object_release(rl->r_dict);
597 1.18 rmind }
598 1.4 rmind kmem_free(rl, sizeof(npf_rule_t));
599 1.1 rmind }
600 1.1 rmind
601 1.1 rmind /*
602 1.19 rmind * npf_rule_getid: return the unique ID of a rule.
603 1.10 rmind * npf_rule_getrproc: acquire a reference and return rule procedure, if any.
604 1.1 rmind * npf_rule_getnat: get NAT policy assigned to the rule.
605 1.1 rmind */
606 1.1 rmind
607 1.19 rmind uint64_t
608 1.19 rmind npf_rule_getid(const npf_rule_t *rl)
609 1.19 rmind {
610 1.19 rmind KASSERT(NPF_DYNAMIC_RULE_P(rl->r_attr));
611 1.19 rmind return rl->r_id;
612 1.19 rmind }
613 1.19 rmind
614 1.10 rmind npf_rproc_t *
615 1.10 rmind npf_rule_getrproc(npf_rule_t *rl)
616 1.10 rmind {
617 1.10 rmind npf_rproc_t *rp = rl->r_rproc;
618 1.10 rmind
619 1.10 rmind if (rp) {
620 1.10 rmind npf_rproc_acquire(rp);
621 1.10 rmind }
622 1.10 rmind return rp;
623 1.10 rmind }
624 1.10 rmind
625 1.1 rmind npf_natpolicy_t *
626 1.1 rmind npf_rule_getnat(const npf_rule_t *rl)
627 1.1 rmind {
628 1.4 rmind return rl->r_natp;
629 1.1 rmind }
630 1.1 rmind
631 1.4 rmind /*
632 1.4 rmind * npf_rule_setnat: assign NAT policy to the rule and insert into the
633 1.4 rmind * NAT policy list in the ruleset.
634 1.4 rmind */
635 1.1 rmind void
636 1.1 rmind npf_rule_setnat(npf_rule_t *rl, npf_natpolicy_t *np)
637 1.1 rmind {
638 1.3 rmind
639 1.4 rmind KASSERT(rl->r_natp == NULL);
640 1.4 rmind rl->r_natp = np;
641 1.1 rmind }
642 1.1 rmind
643 1.17 rmind /*
644 1.17 rmind * npf_rule_inspect: match the interface, direction and run the filter code.
645 1.17 rmind * Returns true if rule matches, false otherise.
646 1.17 rmind */
647 1.17 rmind static inline bool
648 1.17 rmind npf_rule_inspect(npf_cache_t *npc, nbuf_t *nbuf, const npf_rule_t *rl,
649 1.17 rmind const int di_mask, const int layer)
650 1.17 rmind {
651 1.17 rmind const ifnet_t *ifp = nbuf->nb_ifp;
652 1.17 rmind const void *code;
653 1.17 rmind
654 1.17 rmind /* Match the interface. */
655 1.17 rmind if (rl->r_ifid && rl->r_ifid != ifp->if_index) {
656 1.17 rmind return false;
657 1.17 rmind }
658 1.17 rmind
659 1.17 rmind /* Match the direction. */
660 1.17 rmind if ((rl->r_attr & NPF_RULE_DIMASK) != NPF_RULE_DIMASK) {
661 1.17 rmind if ((rl->r_attr & di_mask) == 0)
662 1.17 rmind return false;
663 1.17 rmind }
664 1.17 rmind
665 1.20 rmind /* Execute JIT code, if any. */
666 1.20 rmind if (__predict_true(rl->r_jcode)) {
667 1.20 rmind struct mbuf *m = nbuf_head_mbuf(nbuf);
668 1.20 rmind size_t pktlen = m_length(m);
669 1.20 rmind
670 1.20 rmind return rl->r_jcode((unsigned char *)m, pktlen, 0) != 0;
671 1.20 rmind }
672 1.20 rmind
673 1.20 rmind /* Execute the byte-code, if any. */
674 1.17 rmind if ((code = rl->r_code) == NULL) {
675 1.17 rmind return true;
676 1.17 rmind }
677 1.17 rmind
678 1.17 rmind switch (rl->r_type) {
679 1.17 rmind case NPF_CODE_NC:
680 1.17 rmind return npf_ncode_process(npc, code, nbuf, layer) == 0;
681 1.17 rmind case NPF_CODE_BPF: {
682 1.17 rmind struct mbuf *m = nbuf_head_mbuf(nbuf);
683 1.17 rmind size_t pktlen = m_length(m);
684 1.22 rmind return bpf_filter(bpf_def_ctx, NULL, code,
685 1.22 rmind (unsigned char *)m, pktlen, 0) != 0;
686 1.17 rmind }
687 1.17 rmind default:
688 1.17 rmind KASSERT(false);
689 1.17 rmind }
690 1.17 rmind return false;
691 1.17 rmind }
692 1.17 rmind
693 1.17 rmind /*
694 1.17 rmind * npf_rule_reinspect: re-inspect the dynamic rule by iterating its list.
695 1.17 rmind * This is only for the dynamic rules. Subrules cannot have nested rules.
696 1.17 rmind */
697 1.17 rmind static npf_rule_t *
698 1.17 rmind npf_rule_reinspect(npf_cache_t *npc, nbuf_t *nbuf, const npf_rule_t *drl,
699 1.17 rmind const int di_mask, const int layer)
700 1.7 rmind {
701 1.17 rmind npf_rule_t *final_rl = NULL, *rl;
702 1.17 rmind
703 1.17 rmind KASSERT(NPF_DYNAMIC_GROUP_P(drl->r_attr));
704 1.7 rmind
705 1.17 rmind TAILQ_FOREACH(rl, &drl->r_subset, r_entry) {
706 1.17 rmind if (!npf_rule_inspect(npc, nbuf, rl, di_mask, layer)) {
707 1.7 rmind continue;
708 1.17 rmind }
709 1.17 rmind if (rl->r_attr & NPF_RULE_FINAL) {
710 1.17 rmind return rl;
711 1.17 rmind }
712 1.17 rmind final_rl = rl;
713 1.7 rmind }
714 1.17 rmind return final_rl;
715 1.7 rmind }
716 1.1 rmind
717 1.1 rmind /*
718 1.7 rmind * npf_ruleset_inspect: inspect the packet against the given ruleset.
719 1.1 rmind *
720 1.7 rmind * Loop through the rules in the set and run n-code processor of each rule
721 1.7 rmind * against the packet (nbuf chain). If sub-ruleset is found, inspect it.
722 1.7 rmind *
723 1.9 rmind * => Caller is responsible for nbuf chain protection.
724 1.1 rmind */
725 1.1 rmind npf_rule_t *
726 1.15 rmind npf_ruleset_inspect(npf_cache_t *npc, nbuf_t *nbuf,
727 1.17 rmind const npf_ruleset_t *rlset, const int di, const int layer)
728 1.1 rmind {
729 1.7 rmind const int di_mask = (di & PFIL_IN) ? NPF_RULE_IN : NPF_RULE_OUT;
730 1.17 rmind const u_int nitems = rlset->rs_nitems;
731 1.17 rmind npf_rule_t *final_rl = NULL;
732 1.17 rmind u_int n = 0;
733 1.1 rmind
734 1.1 rmind KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
735 1.17 rmind
736 1.17 rmind while (n < nitems) {
737 1.17 rmind npf_rule_t *rl = rlset->rs_rules[n];
738 1.17 rmind const u_int skip_to = rl->r_skip_to;
739 1.17 rmind const uint32_t attr = rl->r_attr;
740 1.17 rmind
741 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
742 1.1 rmind KASSERT(!final_rl || rl->r_priority >= final_rl->r_priority);
743 1.17 rmind KASSERT(n < skip_to);
744 1.1 rmind
745 1.17 rmind /* Group is a barrier: return a matching if found any. */
746 1.17 rmind if ((attr & NPF_RULE_GROUP) != 0 && final_rl) {
747 1.17 rmind break;
748 1.17 rmind }
749 1.17 rmind
750 1.17 rmind /* Main inspection of the rule. */
751 1.17 rmind if (!npf_rule_inspect(npc, nbuf, rl, di_mask, layer)) {
752 1.17 rmind n = skip_to;
753 1.1 rmind continue;
754 1.1 rmind }
755 1.17 rmind
756 1.17 rmind if (NPF_DYNAMIC_GROUP_P(attr)) {
757 1.17 rmind /*
758 1.17 rmind * If this is a dynamic rule, re-inspect the subrules.
759 1.17 rmind * If it has any matching rule, then it is final.
760 1.17 rmind */
761 1.17 rmind rl = npf_rule_reinspect(npc, nbuf, rl, di_mask, layer);
762 1.17 rmind if (rl != NULL) {
763 1.17 rmind final_rl = rl;
764 1.17 rmind break;
765 1.17 rmind }
766 1.17 rmind } else if ((attr & NPF_RULE_GROUP) == 0) {
767 1.17 rmind /*
768 1.17 rmind * Groups themselves are not matching.
769 1.17 rmind */
770 1.17 rmind final_rl = rl;
771 1.1 rmind }
772 1.17 rmind
773 1.1 rmind /* Set the matching rule and check for "final". */
774 1.17 rmind if (attr & NPF_RULE_FINAL) {
775 1.2 rmind break;
776 1.1 rmind }
777 1.17 rmind n++;
778 1.2 rmind }
779 1.16 rmind
780 1.16 rmind KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
781 1.7 rmind return final_rl;
782 1.1 rmind }
783 1.1 rmind
784 1.1 rmind /*
785 1.17 rmind * npf_rule_conclude: return decision and the flags for conclusion.
786 1.1 rmind *
787 1.1 rmind * => Returns ENETUNREACH if "block" and 0 if "pass".
788 1.1 rmind */
789 1.1 rmind int
790 1.17 rmind npf_rule_conclude(const npf_rule_t *rl, int *retfl)
791 1.1 rmind {
792 1.1 rmind /* If not passing - drop the packet. */
793 1.4 rmind *retfl = rl->r_attr;
794 1.17 rmind return (rl->r_attr & NPF_RULE_PASS) ? 0 : ENETUNREACH;
795 1.1 rmind }
796 1.1 rmind
797 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
798 1.1 rmind
799 1.1 rmind void
800 1.12 rmind npf_rulenc_dump(const npf_rule_t *rl)
801 1.1 rmind {
802 1.17 rmind const uint32_t *op = rl->r_code;
803 1.17 rmind size_t n = rl->r_clen;
804 1.1 rmind
805 1.2 rmind while (n) {
806 1.1 rmind printf("\t> |0x%02x|\n", (uint32_t)*op);
807 1.1 rmind op++;
808 1.1 rmind n -= sizeof(*op);
809 1.2 rmind }
810 1.1 rmind printf("-> %s\n", (rl->r_attr & NPF_RULE_PASS) ? "pass" : "block");
811 1.1 rmind }
812 1.1 rmind
813 1.1 rmind #endif
814