2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002,2003 Henning Brauer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * Effort sponsored in part by the Defense Advanced Research Projects
33 * Agency (DARPA) and Air Force Research Laboratory, Air Force
34 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
36 * $OpenBSD: pf_ruleset.c,v 1.2 2008/12/18 15:31:37 dhill Exp $
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/socket.h>
45 # include <sys/systm.h>
46 # include <sys/refcount.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
57 #include <net/pfvar.h>
60 #include <netinet/ip6.h>
65 #define DPFPRINTF(format, x...) \
66 if (V_pf_status.debug >= PF_DEBUG_NOISY) \
68 #define rs_malloc(x) malloc(x, M_TEMP, M_NOWAIT|M_ZERO)
69 #define rs_free(x) free(x, M_TEMP)
72 /* Userland equivalents so we can lend code to pfctl et al. */
74 #include <arpa/inet.h>
79 #define rs_malloc(x) calloc(1, x)
80 #define rs_free(x) free(x)
83 #include <sys/stdarg.h>
84 #define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
86 #define DPFPRINTF(format, x...) ((void)0)
91 VNET_DEFINE(struct pf_anchor_global, pf_anchors);
92 VNET_DEFINE(struct pf_anchor, pf_main_anchor);
94 struct pf_anchor_global pf_anchors;
95 struct pf_anchor pf_main_anchor;
97 #define V_pf_anchors pf_anchors
98 #undef pf_main_ruleset
99 #define pf_main_ruleset pf_main_anchor.ruleset
102 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
104 static struct pf_anchor *pf_find_anchor(const char *);
106 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
107 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
110 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
112 int c = strcmp(a->path, b->path);
114 return (c ? (c < 0 ? -1 : 1) : 0);
118 pf_get_ruleset_number(u_int8_t action)
123 return (PF_RULESET_SCRUB);
127 return (PF_RULESET_FILTER);
131 return (PF_RULESET_NAT);
135 return (PF_RULESET_BINAT);
139 return (PF_RULESET_RDR);
142 return (PF_RULESET_MAX);
148 pf_init_ruleset(struct pf_ruleset *ruleset)
152 memset(ruleset, 0, sizeof(struct pf_ruleset));
153 for (i = 0; i < PF_RULESET_MAX; i++) {
154 TAILQ_INIT(&ruleset->rules[i].queues[0]);
155 TAILQ_INIT(&ruleset->rules[i].queues[1]);
156 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
157 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
161 static struct pf_anchor *
162 pf_find_anchor(const char *path)
164 struct pf_anchor *key, *found;
166 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
169 strlcpy(key->path, path, sizeof(key->path));
170 found = RB_FIND(pf_anchor_global, &V_pf_anchors, key);
176 pf_find_ruleset(const char *path)
178 struct pf_anchor *anchor;
183 return (&pf_main_ruleset);
184 anchor = pf_find_anchor(path);
188 return (&anchor->ruleset);
192 pf_find_or_create_ruleset(const char *path)
195 struct pf_ruleset *ruleset;
196 struct pf_anchor *anchor = NULL, *dup, *parent = NULL;
199 return (&pf_main_ruleset);
202 ruleset = pf_find_ruleset(path);
205 p = (char *)rs_malloc(MAXPATHLEN);
208 strlcpy(p, path, MAXPATHLEN);
209 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
211 if ((ruleset = pf_find_ruleset(p)) != NULL) {
212 parent = ruleset->anchor;
220 strlcpy(p, path, MAXPATHLEN);
225 while ((r = strchr(q, '/')) != NULL || *q) {
228 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
229 (parent != NULL && strlen(parent->path) >=
230 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
234 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
235 if (anchor == NULL) {
239 RB_INIT(&anchor->children);
240 strlcpy(anchor->name, q, sizeof(anchor->name));
241 if (parent != NULL) {
242 strlcpy(anchor->path, parent->path,
243 sizeof(anchor->path));
244 strlcat(anchor->path, "/", sizeof(anchor->path));
246 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
247 if ((dup = RB_INSERT(pf_anchor_global, &V_pf_anchors, anchor)) !=
249 printf("pf_find_or_create_ruleset: RB_INSERT1 "
250 "'%s' '%s' collides with '%s' '%s'\n",
251 anchor->path, anchor->name, dup->path, dup->name);
256 if (parent != NULL) {
257 anchor->parent = parent;
258 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
260 printf("pf_find_or_create_ruleset: "
261 "RB_INSERT2 '%s' '%s' collides with "
262 "'%s' '%s'\n", anchor->path, anchor->name,
263 dup->path, dup->name);
264 RB_REMOVE(pf_anchor_global, &V_pf_anchors,
271 pf_init_ruleset(&anchor->ruleset);
272 anchor->ruleset.anchor = anchor;
280 return (&anchor->ruleset);
284 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
286 struct pf_anchor *parent;
289 while (ruleset != NULL) {
290 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
291 !RB_EMPTY(&ruleset->anchor->children) ||
292 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
295 for (i = 0; i < PF_RULESET_MAX; ++i)
296 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
297 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
298 ruleset->rules[i].inactive.open)
300 RB_REMOVE(pf_anchor_global, &V_pf_anchors, ruleset->anchor);
301 if ((parent = ruleset->anchor->parent) != NULL)
302 RB_REMOVE(pf_anchor_node, &parent->children,
304 rs_free(ruleset->anchor);
307 ruleset = &parent->ruleset;
312 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
316 struct pf_ruleset *ruleset;
319 r->anchor_relative = 0;
320 r->anchor_wildcard = 0;
323 path = (char *)rs_malloc(MAXPATHLEN);
327 strlcpy(path, name + 1, MAXPATHLEN);
330 r->anchor_relative = 1;
331 if (s->anchor == NULL || !s->anchor->path[0])
334 strlcpy(path, s->anchor->path, MAXPATHLEN);
335 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
337 printf("pf_anchor_setup: .. beyond root\n");
341 if ((p = strrchr(path, '/')) != NULL)
345 r->anchor_relative++;
349 strlcat(path, "/", MAXPATHLEN);
350 strlcat(path, name, MAXPATHLEN);
352 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
353 r->anchor_wildcard = 1;
356 ruleset = pf_find_or_create_ruleset(path);
358 if (ruleset == NULL || ruleset->anchor == NULL) {
359 printf("pf_anchor_setup: ruleset\n");
362 r->anchor = ruleset->anchor;
368 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
369 struct pfioc_rule *pr)
371 pr->anchor_call[0] = 0;
372 if (r->anchor == NULL)
374 if (!r->anchor_relative) {
375 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
376 strlcat(pr->anchor_call, r->anchor->path,
377 sizeof(pr->anchor_call));
382 a = (char *)rs_malloc(MAXPATHLEN);
385 if (rs->anchor == NULL)
388 strlcpy(a, rs->anchor->path, MAXPATHLEN);
389 for (i = 1; i < r->anchor_relative; ++i) {
390 if ((p = strrchr(a, '/')) == NULL)
393 strlcat(pr->anchor_call, "../",
394 sizeof(pr->anchor_call));
396 if (strncmp(a, r->anchor->path, strlen(a))) {
397 printf("pf_anchor_copyout: '%s' '%s'\n", a,
402 if (strlen(r->anchor->path) > strlen(a))
403 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
404 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
407 if (r->anchor_wildcard)
408 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
409 sizeof(pr->anchor_call));
414 pf_anchor_remove(struct pf_rule *r)
416 if (r->anchor == NULL)
418 if (r->anchor->refcnt <= 0) {
419 printf("pf_anchor_remove: broken refcount\n");
423 if (!--r->anchor->refcnt)
424 pf_remove_if_empty_ruleset(&r->anchor->ruleset);