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>
64 #define DPFPRINTF(format, x...) \
65 if (V_pf_status.debug >= PF_DEBUG_NOISY) \
67 #define rs_malloc(x) malloc(x, M_TEMP, M_NOWAIT|M_ZERO)
68 #define rs_free(x) free(x, M_TEMP)
71 /* Userland equivalents so we can lend code to pfctl et al. */
73 #include <arpa/inet.h>
78 #define rs_malloc(x) calloc(1, x)
79 #define rs_free(x) free(x)
82 #include <sys/stdarg.h>
83 #define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
85 #define DPFPRINTF(format, x...) ((void)0)
90 VNET_DEFINE(struct pf_anchor_global, pf_anchors);
91 VNET_DEFINE(struct pf_anchor, pf_main_anchor);
93 struct pf_anchor_global pf_anchors;
94 struct pf_anchor pf_main_anchor;
96 #define V_pf_anchors pf_anchors
97 #undef pf_main_ruleset
98 #define pf_main_ruleset pf_main_anchor.ruleset
101 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
103 static struct pf_anchor *pf_find_anchor(const char *);
105 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
106 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
109 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
111 int c = strcmp(a->path, b->path);
113 return (c ? (c < 0 ? -1 : 1) : 0);
117 pf_get_ruleset_number(u_int8_t action)
122 return (PF_RULESET_SCRUB);
126 return (PF_RULESET_FILTER);
130 return (PF_RULESET_NAT);
134 return (PF_RULESET_BINAT);
138 return (PF_RULESET_RDR);
141 return (PF_RULESET_MAX);
147 pf_init_ruleset(struct pf_ruleset *ruleset)
151 memset(ruleset, 0, sizeof(struct pf_ruleset));
152 for (i = 0; i < PF_RULESET_MAX; i++) {
153 TAILQ_INIT(&ruleset->rules[i].queues[0]);
154 TAILQ_INIT(&ruleset->rules[i].queues[1]);
155 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
156 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
160 static struct pf_anchor *
161 pf_find_anchor(const char *path)
163 struct pf_anchor *key, *found;
165 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
168 strlcpy(key->path, path, sizeof(key->path));
169 found = RB_FIND(pf_anchor_global, &V_pf_anchors, key);
175 pf_find_ruleset(const char *path)
177 struct pf_anchor *anchor;
182 return (&pf_main_ruleset);
183 anchor = pf_find_anchor(path);
187 return (&anchor->ruleset);
191 pf_find_or_create_ruleset(const char *path)
194 struct pf_ruleset *ruleset;
195 struct pf_anchor *anchor = NULL, *dup, *parent = NULL;
198 return (&pf_main_ruleset);
201 ruleset = pf_find_ruleset(path);
204 p = (char *)rs_malloc(MAXPATHLEN);
207 strlcpy(p, path, MAXPATHLEN);
208 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
210 if ((ruleset = pf_find_ruleset(p)) != NULL) {
211 parent = ruleset->anchor;
219 strlcpy(p, path, MAXPATHLEN);
224 while ((r = strchr(q, '/')) != NULL || *q) {
227 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
228 (parent != NULL && strlen(parent->path) >=
229 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
233 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
234 if (anchor == NULL) {
238 RB_INIT(&anchor->children);
239 strlcpy(anchor->name, q, sizeof(anchor->name));
240 if (parent != NULL) {
241 strlcpy(anchor->path, parent->path,
242 sizeof(anchor->path));
243 strlcat(anchor->path, "/", sizeof(anchor->path));
245 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
246 if ((dup = RB_INSERT(pf_anchor_global, &V_pf_anchors, anchor)) !=
248 printf("pf_find_or_create_ruleset: RB_INSERT1 "
249 "'%s' '%s' collides with '%s' '%s'\n",
250 anchor->path, anchor->name, dup->path, dup->name);
255 if (parent != NULL) {
256 anchor->parent = parent;
257 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
259 printf("pf_find_or_create_ruleset: "
260 "RB_INSERT2 '%s' '%s' collides with "
261 "'%s' '%s'\n", anchor->path, anchor->name,
262 dup->path, dup->name);
263 RB_REMOVE(pf_anchor_global, &V_pf_anchors,
270 pf_init_ruleset(&anchor->ruleset);
271 anchor->ruleset.anchor = anchor;
279 return (&anchor->ruleset);
283 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
285 struct pf_anchor *parent;
288 while (ruleset != NULL) {
289 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
290 !RB_EMPTY(&ruleset->anchor->children) ||
291 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
294 for (i = 0; i < PF_RULESET_MAX; ++i)
295 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
296 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
297 ruleset->rules[i].inactive.open)
299 RB_REMOVE(pf_anchor_global, &V_pf_anchors, ruleset->anchor);
300 if ((parent = ruleset->anchor->parent) != NULL)
301 RB_REMOVE(pf_anchor_node, &parent->children,
303 rs_free(ruleset->anchor);
306 ruleset = &parent->ruleset;
311 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
315 struct pf_ruleset *ruleset;
318 r->anchor_relative = 0;
319 r->anchor_wildcard = 0;
322 path = (char *)rs_malloc(MAXPATHLEN);
326 strlcpy(path, name + 1, MAXPATHLEN);
329 r->anchor_relative = 1;
330 if (s->anchor == NULL || !s->anchor->path[0])
333 strlcpy(path, s->anchor->path, MAXPATHLEN);
334 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
336 printf("pf_anchor_setup: .. beyond root\n");
340 if ((p = strrchr(path, '/')) != NULL)
344 r->anchor_relative++;
348 strlcat(path, "/", MAXPATHLEN);
349 strlcat(path, name, MAXPATHLEN);
351 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
352 r->anchor_wildcard = 1;
355 ruleset = pf_find_or_create_ruleset(path);
357 if (ruleset == NULL || ruleset->anchor == NULL) {
358 printf("pf_anchor_setup: ruleset\n");
361 r->anchor = ruleset->anchor;
367 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
368 struct pfioc_rule *pr)
370 pr->anchor_call[0] = 0;
371 if (r->anchor == NULL)
373 if (!r->anchor_relative) {
374 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
375 strlcat(pr->anchor_call, r->anchor->path,
376 sizeof(pr->anchor_call));
381 a = (char *)rs_malloc(MAXPATHLEN);
384 if (rs->anchor == NULL)
387 strlcpy(a, rs->anchor->path, MAXPATHLEN);
388 for (i = 1; i < r->anchor_relative; ++i) {
389 if ((p = strrchr(a, '/')) == NULL)
392 strlcat(pr->anchor_call, "../",
393 sizeof(pr->anchor_call));
395 if (strncmp(a, r->anchor->path, strlen(a))) {
396 printf("pf_anchor_copyout: '%s' '%s'\n", a,
401 if (strlen(r->anchor->path) > strlen(a))
402 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
403 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
406 if (r->anchor_wildcard)
407 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
408 sizeof(pr->anchor_call));
413 pf_anchor_remove(struct pf_rule *r)
415 if (r->anchor == NULL)
417 if (r->anchor->refcnt <= 0) {
418 printf("pf_anchor_remove: broken refcount\n");
422 if (!--r->anchor->refcnt)
423 pf_remove_if_empty_ruleset(&r->anchor->ruleset);