1 /* $OpenBSD: pf_ruleset.c,v 1.2 2008/12/18 15:31:37 dhill Exp $ */
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.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/socket.h>
46 # include <sys/systm.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
56 #include <net/pfvar.h>
59 #include <netinet/ip6.h>
65 #define DPFPRINTF(format, x...) \
66 if (V_pf_status.debug >= PF_DEBUG_NOISY) \
69 #define DPFPRINTF(format, x...) \
70 if (pf_status.debug >= PF_DEBUG_NOISY) \
74 #define rs_malloc(x) malloc(x, M_TEMP, M_NOWAIT|M_ZERO)
76 #define rs_malloc(x) malloc(x, M_TEMP, M_WAITOK|M_CANFAIL|M_ZERO)
78 #define rs_free(x) free(x, M_TEMP)
81 /* Userland equivalents so we can lend code to pfctl et al. */
83 #include <arpa/inet.h>
88 #define rs_malloc(x) calloc(1, x)
89 #define rs_free(x) free(x)
92 #include <sys/stdarg.h>
93 #define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
95 #define DPFPRINTF(format, x...) ((void)0)
99 #if defined(__FreeBSD__) && !defined(_KERNEL)
101 #define V_pf_anchors pf_anchors
103 #undef pf_main_ruleset
104 #define pf_main_ruleset pf_main_anchor.ruleset
107 #if defined(__FreeBSD__) && defined(_KERNEL)
108 VNET_DEFINE(struct pf_anchor_global, pf_anchors);
109 VNET_DEFINE(struct pf_anchor, pf_main_anchor);
111 struct pf_anchor_global pf_anchors;
112 struct pf_anchor pf_main_anchor;
115 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
117 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
118 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
121 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
123 int c = strcmp(a->path, b->path);
125 return (c ? (c < 0 ? -1 : 1) : 0);
129 pf_get_ruleset_number(u_int8_t action)
134 return (PF_RULESET_SCRUB);
138 return (PF_RULESET_FILTER);
142 return (PF_RULESET_NAT);
146 return (PF_RULESET_BINAT);
150 return (PF_RULESET_RDR);
153 return (PF_RULESET_MAX);
159 pf_init_ruleset(struct pf_ruleset *ruleset)
163 memset(ruleset, 0, sizeof(struct pf_ruleset));
164 for (i = 0; i < PF_RULESET_MAX; i++) {
165 TAILQ_INIT(&ruleset->rules[i].queues[0]);
166 TAILQ_INIT(&ruleset->rules[i].queues[1]);
167 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
168 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
173 pf_find_anchor(const char *path)
175 struct pf_anchor *key, *found;
177 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
180 strlcpy(key->path, path, sizeof(key->path));
182 found = RB_FIND(pf_anchor_global, &V_pf_anchors, key);
184 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
191 pf_find_ruleset(const char *path)
193 struct pf_anchor *anchor;
198 return (&pf_main_ruleset);
199 anchor = pf_find_anchor(path);
203 return (&anchor->ruleset);
207 pf_find_or_create_ruleset(const char *path)
210 struct pf_ruleset *ruleset;
212 struct pf_anchor *anchor = NULL, *dup, *parent = NULL;
214 struct pf_anchor *anchor, *dup, *parent = NULL;
218 return (&pf_main_ruleset);
221 ruleset = pf_find_ruleset(path);
224 p = (char *)rs_malloc(MAXPATHLEN);
227 strlcpy(p, path, MAXPATHLEN);
228 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
230 if ((ruleset = pf_find_ruleset(p)) != NULL) {
231 parent = ruleset->anchor;
239 strlcpy(p, path, MAXPATHLEN);
244 while ((r = strchr(q, '/')) != NULL || *q) {
247 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
248 (parent != NULL && strlen(parent->path) >=
249 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
253 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
254 if (anchor == NULL) {
258 RB_INIT(&anchor->children);
259 strlcpy(anchor->name, q, sizeof(anchor->name));
260 if (parent != NULL) {
261 strlcpy(anchor->path, parent->path,
262 sizeof(anchor->path));
263 strlcat(anchor->path, "/", sizeof(anchor->path));
265 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
267 if ((dup = RB_INSERT(pf_anchor_global, &V_pf_anchors, anchor)) !=
269 if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
272 printf("pf_find_or_create_ruleset: RB_INSERT1 "
273 "'%s' '%s' collides with '%s' '%s'\n",
274 anchor->path, anchor->name, dup->path, dup->name);
279 if (parent != NULL) {
280 anchor->parent = parent;
281 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
283 printf("pf_find_or_create_ruleset: "
284 "RB_INSERT2 '%s' '%s' collides with "
285 "'%s' '%s'\n", anchor->path, anchor->name,
286 dup->path, dup->name);
288 RB_REMOVE(pf_anchor_global, &V_pf_anchors,
290 RB_REMOVE(pf_anchor_global, &pf_anchors,
298 pf_init_ruleset(&anchor->ruleset);
299 anchor->ruleset.anchor = anchor;
307 return (&anchor->ruleset);
311 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
313 struct pf_anchor *parent;
316 while (ruleset != NULL) {
317 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
318 !RB_EMPTY(&ruleset->anchor->children) ||
319 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
322 for (i = 0; i < PF_RULESET_MAX; ++i)
323 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
324 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
325 ruleset->rules[i].inactive.open)
328 RB_REMOVE(pf_anchor_global, &V_pf_anchors, ruleset->anchor);
330 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
332 if ((parent = ruleset->anchor->parent) != NULL)
333 RB_REMOVE(pf_anchor_node, &parent->children,
335 rs_free(ruleset->anchor);
338 ruleset = &parent->ruleset;
343 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
347 struct pf_ruleset *ruleset;
350 r->anchor_relative = 0;
351 r->anchor_wildcard = 0;
354 path = (char *)rs_malloc(MAXPATHLEN);
358 strlcpy(path, name + 1, MAXPATHLEN);
361 r->anchor_relative = 1;
362 if (s->anchor == NULL || !s->anchor->path[0])
365 strlcpy(path, s->anchor->path, MAXPATHLEN);
366 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
368 printf("pf_anchor_setup: .. beyond root\n");
372 if ((p = strrchr(path, '/')) != NULL)
376 r->anchor_relative++;
380 strlcat(path, "/", MAXPATHLEN);
381 strlcat(path, name, MAXPATHLEN);
383 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
384 r->anchor_wildcard = 1;
387 ruleset = pf_find_or_create_ruleset(path);
389 if (ruleset == NULL || ruleset->anchor == NULL) {
390 printf("pf_anchor_setup: ruleset\n");
393 r->anchor = ruleset->anchor;
399 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
400 struct pfioc_rule *pr)
402 pr->anchor_call[0] = 0;
403 if (r->anchor == NULL)
405 if (!r->anchor_relative) {
406 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
407 strlcat(pr->anchor_call, r->anchor->path,
408 sizeof(pr->anchor_call));
413 a = (char *)rs_malloc(MAXPATHLEN);
416 if (rs->anchor == NULL)
419 strlcpy(a, rs->anchor->path, MAXPATHLEN);
420 for (i = 1; i < r->anchor_relative; ++i) {
421 if ((p = strrchr(a, '/')) == NULL)
424 strlcat(pr->anchor_call, "../",
425 sizeof(pr->anchor_call));
427 if (strncmp(a, r->anchor->path, strlen(a))) {
428 printf("pf_anchor_copyout: '%s' '%s'\n", a,
433 if (strlen(r->anchor->path) > strlen(a))
434 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
435 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
438 if (r->anchor_wildcard)
439 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
440 sizeof(pr->anchor_call));
445 pf_anchor_remove(struct pf_rule *r)
447 if (r->anchor == NULL)
449 if (r->anchor->refcnt <= 0) {
450 printf("pf_anchor_remove: broken refcount\n");
454 if (!--r->anchor->refcnt)
455 pf_remove_if_empty_ruleset(&r->anchor->ruleset);