2 * Copyright (c) 2001 Daniel Hartmeier
3 * Copyright (c) 2002,2003 Henning Brauer
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * Effort sponsored in part by the Defense Advanced Research Projects
31 * Agency (DARPA) and Air Force Research Laboratory, Air Force
32 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34 * $OpenBSD: pf_ruleset.c,v 1.2 2008/12/18 15:31:37 dhill Exp $
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/socket.h>
43 # include <sys/systm.h>
44 # include <sys/refcount.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <netinet/tcp.h>
55 #include <net/pfvar.h>
58 #include <netinet/ip6.h>
63 #define DPFPRINTF(format, x...) \
64 if (V_pf_status.debug >= PF_DEBUG_NOISY) \
66 #define rs_malloc(x) malloc(x, M_TEMP, M_NOWAIT|M_ZERO)
67 #define rs_free(x) free(x, M_TEMP)
70 /* Userland equivalents so we can lend code to pfctl et al. */
72 #include <arpa/inet.h>
77 #define rs_malloc(x) calloc(1, x)
78 #define rs_free(x) free(x)
81 #include <sys/stdarg.h>
82 #define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
84 #define DPFPRINTF(format, x...) ((void)0)
89 VNET_DEFINE(struct pf_anchor_global, pf_anchors);
90 VNET_DEFINE(struct pf_anchor, pf_main_anchor);
92 struct pf_anchor_global pf_anchors;
93 struct pf_anchor pf_main_anchor;
95 #define V_pf_anchors pf_anchors
96 #undef pf_main_ruleset
97 #define pf_main_ruleset pf_main_anchor.ruleset
100 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
102 static struct pf_anchor *pf_find_anchor(const char *);
104 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
105 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
108 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
110 int c = strcmp(a->path, b->path);
112 return (c ? (c < 0 ? -1 : 1) : 0);
116 pf_get_ruleset_number(u_int8_t action)
121 return (PF_RULESET_SCRUB);
125 return (PF_RULESET_FILTER);
129 return (PF_RULESET_NAT);
133 return (PF_RULESET_BINAT);
137 return (PF_RULESET_RDR);
140 return (PF_RULESET_MAX);
146 pf_init_ruleset(struct pf_ruleset *ruleset)
150 memset(ruleset, 0, sizeof(struct pf_ruleset));
151 for (i = 0; i < PF_RULESET_MAX; i++) {
152 TAILQ_INIT(&ruleset->rules[i].queues[0]);
153 TAILQ_INIT(&ruleset->rules[i].queues[1]);
154 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
155 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
159 static struct pf_anchor *
160 pf_find_anchor(const char *path)
162 struct pf_anchor *key, *found;
164 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
167 strlcpy(key->path, path, sizeof(key->path));
168 found = RB_FIND(pf_anchor_global, &V_pf_anchors, key);
174 pf_find_ruleset(const char *path)
176 struct pf_anchor *anchor;
181 return (&pf_main_ruleset);
182 anchor = pf_find_anchor(path);
186 return (&anchor->ruleset);
190 pf_find_or_create_ruleset(const char *path)
193 struct pf_ruleset *ruleset;
194 struct pf_anchor *anchor = NULL, *dup, *parent = NULL;
197 return (&pf_main_ruleset);
200 ruleset = pf_find_ruleset(path);
203 p = (char *)rs_malloc(MAXPATHLEN);
206 strlcpy(p, path, MAXPATHLEN);
207 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
209 if ((ruleset = pf_find_ruleset(p)) != NULL) {
210 parent = ruleset->anchor;
218 strlcpy(p, path, MAXPATHLEN);
223 while ((r = strchr(q, '/')) != NULL || *q) {
226 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
227 (parent != NULL && strlen(parent->path) >=
228 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
232 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
233 if (anchor == NULL) {
237 RB_INIT(&anchor->children);
238 strlcpy(anchor->name, q, sizeof(anchor->name));
239 if (parent != NULL) {
240 strlcpy(anchor->path, parent->path,
241 sizeof(anchor->path));
242 strlcat(anchor->path, "/", sizeof(anchor->path));
244 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
245 if ((dup = RB_INSERT(pf_anchor_global, &V_pf_anchors, anchor)) !=
247 printf("pf_find_or_create_ruleset: RB_INSERT1 "
248 "'%s' '%s' collides with '%s' '%s'\n",
249 anchor->path, anchor->name, dup->path, dup->name);
254 if (parent != NULL) {
255 anchor->parent = parent;
256 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
258 printf("pf_find_or_create_ruleset: "
259 "RB_INSERT2 '%s' '%s' collides with "
260 "'%s' '%s'\n", anchor->path, anchor->name,
261 dup->path, dup->name);
262 RB_REMOVE(pf_anchor_global, &V_pf_anchors,
269 pf_init_ruleset(&anchor->ruleset);
270 anchor->ruleset.anchor = anchor;
278 return (&anchor->ruleset);
282 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
284 struct pf_anchor *parent;
287 while (ruleset != NULL) {
288 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
289 !RB_EMPTY(&ruleset->anchor->children) ||
290 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
293 for (i = 0; i < PF_RULESET_MAX; ++i)
294 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
295 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
296 ruleset->rules[i].inactive.open)
298 RB_REMOVE(pf_anchor_global, &V_pf_anchors, ruleset->anchor);
299 if ((parent = ruleset->anchor->parent) != NULL)
300 RB_REMOVE(pf_anchor_node, &parent->children,
302 rs_free(ruleset->anchor);
305 ruleset = &parent->ruleset;
310 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
314 struct pf_ruleset *ruleset;
317 r->anchor_relative = 0;
318 r->anchor_wildcard = 0;
321 path = (char *)rs_malloc(MAXPATHLEN);
325 strlcpy(path, name + 1, MAXPATHLEN);
328 r->anchor_relative = 1;
329 if (s->anchor == NULL || !s->anchor->path[0])
332 strlcpy(path, s->anchor->path, MAXPATHLEN);
333 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
335 printf("pf_anchor_setup: .. beyond root\n");
339 if ((p = strrchr(path, '/')) != NULL)
343 r->anchor_relative++;
347 strlcat(path, "/", MAXPATHLEN);
348 strlcat(path, name, MAXPATHLEN);
350 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
351 r->anchor_wildcard = 1;
354 ruleset = pf_find_or_create_ruleset(path);
356 if (ruleset == NULL || ruleset->anchor == NULL) {
357 printf("pf_anchor_setup: ruleset\n");
360 r->anchor = ruleset->anchor;
366 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
367 struct pfioc_rule *pr)
369 pr->anchor_call[0] = 0;
370 if (r->anchor == NULL)
372 if (!r->anchor_relative) {
373 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
374 strlcat(pr->anchor_call, r->anchor->path,
375 sizeof(pr->anchor_call));
380 a = (char *)rs_malloc(MAXPATHLEN);
383 if (rs->anchor == NULL)
386 strlcpy(a, rs->anchor->path, MAXPATHLEN);
387 for (i = 1; i < r->anchor_relative; ++i) {
388 if ((p = strrchr(a, '/')) == NULL)
391 strlcat(pr->anchor_call, "../",
392 sizeof(pr->anchor_call));
394 if (strncmp(a, r->anchor->path, strlen(a))) {
395 printf("pf_anchor_copyout: '%s' '%s'\n", a,
400 if (strlen(r->anchor->path) > strlen(a))
401 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
402 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
405 if (r->anchor_wildcard)
406 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
407 sizeof(pr->anchor_call));
412 pf_anchor_remove(struct pf_rule *r)
414 if (r->anchor == NULL)
416 if (r->anchor->refcnt <= 0) {
417 printf("pf_anchor_remove: broken refcount\n");
421 if (!--r->anchor->refcnt)
422 pf_remove_if_empty_ruleset(&r->anchor->ruleset);