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>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/tcp.h>
53 #include <net/pfvar.h>
56 #include <netinet/ip6.h>
59 #include <arpa/inet.h>
64 #define rs_malloc(x) calloc(1, x)
65 #define rs_free(x) free(x)
70 #include <sys/stdarg.h>
71 #define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
73 #define DPFPRINTF(format, x...) ((void)0)
76 struct pfctl_anchor_global pf_anchors;
77 struct pfctl_anchor pf_main_anchor;
79 #define V_pf_anchors pf_anchors
80 #undef pf_main_ruleset
81 #define pf_main_ruleset pf_main_anchor.ruleset
83 static __inline int pf_anchor_compare(struct pfctl_anchor *,
84 struct pfctl_anchor *);
85 static struct pfctl_anchor *pf_find_anchor(const char *);
87 RB_GENERATE(pfctl_anchor_global, pfctl_anchor, entry_global,
89 RB_GENERATE(pfctl_anchor_node, pfctl_anchor, entry_node, pf_anchor_compare);
92 pf_anchor_compare(struct pfctl_anchor *a, struct pfctl_anchor *b)
94 int c = strcmp(a->path, b->path);
96 return (c ? (c < 0 ? -1 : 1) : 0);
100 pf_get_ruleset_number(u_int8_t action)
105 return (PF_RULESET_SCRUB);
110 return (PF_RULESET_FILTER);
114 return (PF_RULESET_NAT);
118 return (PF_RULESET_BINAT);
122 return (PF_RULESET_RDR);
125 return (PF_RULESET_MAX);
131 pf_init_ruleset(struct pfctl_ruleset *ruleset)
135 memset(ruleset, 0, sizeof(struct pfctl_ruleset));
136 for (i = 0; i < PF_RULESET_MAX; i++) {
137 TAILQ_INIT(&ruleset->rules[i].queues[0]);
138 TAILQ_INIT(&ruleset->rules[i].queues[1]);
139 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
140 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
144 static struct pfctl_anchor *
145 pf_find_anchor(const char *path)
147 struct pfctl_anchor *key, *found;
149 key = (struct pfctl_anchor *)rs_malloc(sizeof(*key));
152 strlcpy(key->path, path, sizeof(key->path));
153 found = RB_FIND(pfctl_anchor_global, &V_pf_anchors, key);
158 struct pfctl_ruleset *
159 pf_find_ruleset(const char *path)
161 struct pfctl_anchor *anchor;
166 return (&pf_main_ruleset);
167 anchor = pf_find_anchor(path);
171 return (&anchor->ruleset);
174 struct pfctl_ruleset *
175 pf_find_or_create_ruleset(const char *path)
178 struct pfctl_ruleset *ruleset;
179 struct pfctl_anchor *anchor = NULL, *dup, *parent = NULL;
182 return (&pf_main_ruleset);
185 ruleset = pf_find_ruleset(path);
188 p = (char *)rs_malloc(MAXPATHLEN);
191 strlcpy(p, path, MAXPATHLEN);
192 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
194 if ((ruleset = pf_find_ruleset(p)) != NULL) {
195 parent = ruleset->anchor;
203 strlcpy(p, path, MAXPATHLEN);
208 while ((r = strchr(q, '/')) != NULL || *q) {
211 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
212 (parent != NULL && strlen(parent->path) >=
213 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
217 anchor = (struct pfctl_anchor *)rs_malloc(sizeof(*anchor));
218 if (anchor == NULL) {
222 RB_INIT(&anchor->children);
223 strlcpy(anchor->name, q, sizeof(anchor->name));
224 if (parent != NULL) {
225 strlcpy(anchor->path, parent->path,
226 sizeof(anchor->path));
227 strlcat(anchor->path, "/", sizeof(anchor->path));
229 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
230 if ((dup = RB_INSERT(pfctl_anchor_global, &V_pf_anchors, anchor)) !=
232 printf("pf_find_or_create_ruleset: RB_INSERT1 "
233 "'%s' '%s' collides with '%s' '%s'\n",
234 anchor->path, anchor->name, dup->path, dup->name);
239 if (parent != NULL) {
240 anchor->parent = parent;
241 if ((dup = RB_INSERT(pfctl_anchor_node, &parent->children,
243 printf("pf_find_or_create_ruleset: "
244 "RB_INSERT2 '%s' '%s' collides with "
245 "'%s' '%s'\n", anchor->path, anchor->name,
246 dup->path, dup->name);
247 RB_REMOVE(pfctl_anchor_global, &V_pf_anchors,
254 pf_init_ruleset(&anchor->ruleset);
255 anchor->ruleset.anchor = anchor;
263 return (&anchor->ruleset);
267 pf_remove_if_empty_ruleset(struct pfctl_ruleset *ruleset)
269 struct pfctl_anchor *parent;
272 while (ruleset != NULL) {
273 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
274 !RB_EMPTY(&ruleset->anchor->children) ||
275 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
278 for (i = 0; i < PF_RULESET_MAX; ++i)
279 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
280 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
281 ruleset->rules[i].inactive.open)
283 RB_REMOVE(pfctl_anchor_global, &V_pf_anchors, ruleset->anchor);
284 if ((parent = ruleset->anchor->parent) != NULL)
285 RB_REMOVE(pfctl_anchor_node, &parent->children,
287 rs_free(ruleset->anchor);
290 ruleset = &parent->ruleset;
294 pfctl_anchor_setup(struct pfctl_rule *r, const struct pfctl_ruleset *s,
298 struct pfctl_ruleset *ruleset;
301 r->anchor_relative = 0;
302 r->anchor_wildcard = 0;
305 path = (char *)rs_malloc(MAXPATHLEN);
309 strlcpy(path, name + 1, MAXPATHLEN);
312 r->anchor_relative = 1;
313 if (s->anchor == NULL || !s->anchor->path[0])
316 strlcpy(path, s->anchor->path, MAXPATHLEN);
317 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
319 printf("pfctl_anchor_setup: .. beyond root\n");
323 if ((p = strrchr(path, '/')) != NULL)
327 r->anchor_relative++;
331 strlcat(path, "/", MAXPATHLEN);
332 strlcat(path, name, MAXPATHLEN);
334 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
335 r->anchor_wildcard = 1;
338 ruleset = pf_find_or_create_ruleset(path);
340 if (ruleset == NULL || ruleset->anchor == NULL) {
341 printf("pfctl_anchor_setup: ruleset\n");
344 r->anchor = ruleset->anchor;