1 /* $OpenBSD: pf_ruleset.c,v 1.1 2006/10/27 13:56:51 mcbride 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.
38 #include <sys/param.h>
39 #include <sys/socket.h>
41 # include <sys/systm.h>
42 # include <sys/malloc.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/tcp.h>
52 #include <net/pf/pfvar.h>
56 # define DPFPRINTF(format, x...) \
57 if (pf_status.debug >= PF_DEBUG_NOISY) \
59 #define rs_malloc(x) kmalloc(x, M_PFRS, M_WAITOK)
60 #define rs_free(x) kfree(x, M_PFRS)
61 #define printf kprintf
63 static MALLOC_DEFINE(M_PFRS, "pfrulesetpl", "pf ruleset pool list");
66 /* Userland equivalents so we can lend code to pfctl et al. */
68 # include <arpa/inet.h>
73 # define rs_malloc(x) malloc(x)
74 # define rs_free(x) free(x)
77 # include <sys/stdarg.h>
78 # define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
80 # define DPFPRINTF(format, x...) ((void)0)
84 struct pf_anchor_global pf_anchors;
85 struct pf_anchor pf_main_anchor;
89 int pf_get_ruleset_number(u_int8_t);
90 void pf_init_ruleset(struct pf_ruleset *);
91 int pf_anchor_setup(struct pf_rule *,
92 const struct pf_ruleset *, const char *);
93 int pf_anchor_copyout(const struct pf_ruleset *,
94 const struct pf_rule *, struct pfioc_rule *);
95 void pf_anchor_remove(struct pf_rule *);
98 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
100 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
101 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
104 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
106 int c = strcmp(a->path, b->path);
108 return (c ? (c < 0 ? -1 : 1) : 0);
112 pf_get_ruleset_number(u_int8_t action)
117 return (PF_RULESET_SCRUB);
121 return (PF_RULESET_FILTER);
125 return (PF_RULESET_NAT);
129 return (PF_RULESET_BINAT);
133 return (PF_RULESET_RDR);
136 return (PF_RULESET_MAX);
142 pf_init_ruleset(struct pf_ruleset *ruleset)
146 memset(ruleset, 0, sizeof(struct pf_ruleset));
147 for (i = 0; i < PF_RULESET_MAX; i++) {
148 TAILQ_INIT(&ruleset->rules[i].queues[0]);
149 TAILQ_INIT(&ruleset->rules[i].queues[1]);
150 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
151 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
156 pf_find_anchor(const char *path)
158 struct pf_anchor *key, *found;
160 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
161 memset(key, 0, sizeof(*key));
162 strlcpy(key->path, path, sizeof(key->path));
163 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
169 pf_find_ruleset(const char *path)
171 struct pf_anchor *anchor;
176 return (&pf_main_ruleset);
177 anchor = pf_find_anchor(path);
181 return (&anchor->ruleset);
185 pf_find_or_create_ruleset(const char *path)
188 struct pf_ruleset *ruleset;
189 struct pf_anchor *anchor = NULL, *dup, *parent = NULL;
192 return (&pf_main_ruleset);
195 ruleset = pf_find_ruleset(path);
198 p = (char *)rs_malloc(MAXPATHLEN);
199 bzero(p, MAXPATHLEN);
200 strlcpy(p, path, MAXPATHLEN);
201 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
203 if ((ruleset = pf_find_ruleset(p)) != NULL) {
204 parent = ruleset->anchor;
212 strlcpy(p, path, MAXPATHLEN);
217 while ((r = strchr(q, '/')) != NULL || *q) {
220 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
221 (parent != NULL && strlen(parent->path) >=
222 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
226 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
227 if (anchor == NULL) {
231 memset(anchor, 0, sizeof(*anchor));
232 RB_INIT(&anchor->children);
233 strlcpy(anchor->name, q, sizeof(anchor->name));
234 if (parent != NULL) {
235 strlcpy(anchor->path, parent->path,
236 sizeof(anchor->path));
237 strlcat(anchor->path, "/", sizeof(anchor->path));
239 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
240 if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
242 printf("pf_find_or_create_ruleset: RB_INSERT1 "
243 "'%s' '%s' collides with '%s' '%s'\n",
244 anchor->path, anchor->name, dup->path, dup->name);
249 if (parent != NULL) {
250 anchor->parent = parent;
251 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
253 printf("pf_find_or_create_ruleset: "
254 "RB_INSERT2 '%s' '%s' collides with "
255 "'%s' '%s'\n", anchor->path, anchor->name,
256 dup->path, dup->name);
257 RB_REMOVE(pf_anchor_global, &pf_anchors,
264 pf_init_ruleset(&anchor->ruleset);
265 anchor->ruleset.anchor = anchor;
273 return (&anchor->ruleset);
277 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
279 struct pf_anchor *parent;
282 while (ruleset != NULL) {
283 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
284 !RB_EMPTY(&ruleset->anchor->children) ||
285 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
288 for (i = 0; i < PF_RULESET_MAX; ++i)
289 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
290 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
291 ruleset->rules[i].inactive.open)
293 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
294 if ((parent = ruleset->anchor->parent) != NULL)
295 RB_REMOVE(pf_anchor_node, &parent->children,
297 rs_free(ruleset->anchor);
300 ruleset = &parent->ruleset;
305 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
309 struct pf_ruleset *ruleset;
312 r->anchor_relative = 0;
313 r->anchor_wildcard = 0;
316 path = (char *)rs_malloc(MAXPATHLEN);
317 bzero(path, MAXPATHLEN);
319 strlcpy(path, name + 1, MAXPATHLEN);
322 r->anchor_relative = 1;
323 if (s->anchor == NULL || !s->anchor->path[0])
326 strlcpy(path, s->anchor->path, MAXPATHLEN);
327 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
329 printf("pf_anchor_setup: .. beyond root\n");
333 if ((p = strrchr(path, '/')) != NULL)
337 r->anchor_relative++;
341 strlcat(path, "/", MAXPATHLEN);
342 strlcat(path, name, MAXPATHLEN);
344 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
345 r->anchor_wildcard = 1;
348 ruleset = pf_find_or_create_ruleset(path);
350 if (ruleset == NULL || ruleset->anchor == NULL) {
351 printf("pf_anchor_setup: ruleset\n");
354 r->anchor = ruleset->anchor;
360 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
361 struct pfioc_rule *pr)
363 pr->anchor_call[0] = 0;
364 if (r->anchor == NULL)
366 if (!r->anchor_relative) {
367 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
368 strlcat(pr->anchor_call, r->anchor->path,
369 sizeof(pr->anchor_call));
374 a = (char *)rs_malloc(MAXPATHLEN);
375 bzero(a, MAXPATHLEN);
376 if (rs->anchor == NULL)
379 strlcpy(a, rs->anchor->path, MAXPATHLEN);
380 for (i = 1; i < r->anchor_relative; ++i) {
381 if ((p = strrchr(a, '/')) == NULL)
384 strlcat(pr->anchor_call, "../",
385 sizeof(pr->anchor_call));
387 if (strncmp(a, r->anchor->path, strlen(a))) {
388 printf("pf_anchor_copyout: '%s' '%s'\n", a,
393 if (strlen(r->anchor->path) > strlen(a))
394 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
395 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
398 if (r->anchor_wildcard)
399 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
400 sizeof(pr->anchor_call));
405 pf_anchor_remove(struct pf_rule *r)
407 if (r->anchor == NULL)
409 if (r->anchor->refcnt <= 0) {
410 printf("pf_anchor_remove: broken refcount\n");
414 if (!--r->anchor->refcnt)
415 pf_remove_if_empty_ruleset(&r->anchor->ruleset);