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.
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>
64 # define DPFPRINTF(format, x...) \
65 if (pf_status.debug >= PF_DEBUG_NOISY) \
68 #define rs_malloc(x) malloc(x, M_TEMP, M_NOWAIT)
70 #define rs_malloc(x) malloc(x, M_TEMP, M_WAITOK)
72 #define rs_free(x) free(x, M_TEMP)
75 /* Userland equivalents so we can lend code to pfctl et al. */
77 # include <arpa/inet.h>
82 # define rs_malloc(x) malloc(x)
83 # define rs_free(x) free(x)
86 # include <sys/stdarg.h>
87 # define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
89 # define DPFPRINTF(format, x...) ((void)0)
94 struct pf_anchor_global pf_anchors;
95 struct pf_anchor pf_main_anchor;
99 int pf_get_ruleset_number(u_int8_t);
100 void pf_init_ruleset(struct pf_ruleset *);
101 int pf_anchor_setup(struct pf_rule *,
102 const struct pf_ruleset *, const char *);
103 int pf_anchor_copyout(const struct pf_ruleset *,
104 const struct pf_rule *, struct pfioc_rule *);
105 void pf_anchor_remove(struct pf_rule *);
108 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
110 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
111 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
114 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
116 int c = strcmp(a->path, b->path);
118 return (c ? (c < 0 ? -1 : 1) : 0);
122 pf_get_ruleset_number(u_int8_t action)
127 return (PF_RULESET_SCRUB);
131 return (PF_RULESET_FILTER);
135 return (PF_RULESET_NAT);
139 return (PF_RULESET_BINAT);
143 return (PF_RULESET_RDR);
146 return (PF_RULESET_MAX);
152 pf_init_ruleset(struct pf_ruleset *ruleset)
156 memset(ruleset, 0, sizeof(struct pf_ruleset));
157 for (i = 0; i < PF_RULESET_MAX; i++) {
158 TAILQ_INIT(&ruleset->rules[i].queues[0]);
159 TAILQ_INIT(&ruleset->rules[i].queues[1]);
160 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
161 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
166 pf_find_anchor(const char *path)
168 struct pf_anchor *key, *found;
170 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
171 memset(key, 0, sizeof(*key));
172 strlcpy(key->path, path, sizeof(key->path));
173 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
179 pf_find_ruleset(const char *path)
181 struct pf_anchor *anchor;
186 return (&pf_main_ruleset);
187 anchor = pf_find_anchor(path);
191 return (&anchor->ruleset);
195 pf_find_or_create_ruleset(const char *path)
198 struct pf_ruleset *ruleset;
200 struct pf_anchor *anchor = NULL, *dup, *parent = NULL;
202 struct pf_anchor *anchor, *dup, *parent = NULL;
206 return (&pf_main_ruleset);
209 ruleset = pf_find_ruleset(path);
212 p = (char *)rs_malloc(MAXPATHLEN);
213 bzero(p, MAXPATHLEN);
214 strlcpy(p, path, MAXPATHLEN);
215 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
217 if ((ruleset = pf_find_ruleset(p)) != NULL) {
218 parent = ruleset->anchor;
226 strlcpy(p, path, MAXPATHLEN);
231 while ((r = strchr(q, '/')) != NULL || *q) {
234 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
235 (parent != NULL && strlen(parent->path) >=
236 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
240 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
241 if (anchor == NULL) {
245 memset(anchor, 0, sizeof(*anchor));
246 RB_INIT(&anchor->children);
247 strlcpy(anchor->name, q, sizeof(anchor->name));
248 if (parent != NULL) {
249 strlcpy(anchor->path, parent->path,
250 sizeof(anchor->path));
251 strlcat(anchor->path, "/", sizeof(anchor->path));
253 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
254 if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
256 printf("pf_find_or_create_ruleset: RB_INSERT1 "
257 "'%s' '%s' collides with '%s' '%s'\n",
258 anchor->path, anchor->name, dup->path, dup->name);
263 if (parent != NULL) {
264 anchor->parent = parent;
265 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
267 printf("pf_find_or_create_ruleset: "
268 "RB_INSERT2 '%s' '%s' collides with "
269 "'%s' '%s'\n", anchor->path, anchor->name,
270 dup->path, dup->name);
271 RB_REMOVE(pf_anchor_global, &pf_anchors,
278 pf_init_ruleset(&anchor->ruleset);
279 anchor->ruleset.anchor = anchor;
287 return (&anchor->ruleset);
291 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
293 struct pf_anchor *parent;
296 while (ruleset != NULL) {
297 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
298 !RB_EMPTY(&ruleset->anchor->children) ||
299 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
302 for (i = 0; i < PF_RULESET_MAX; ++i)
303 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
304 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
305 ruleset->rules[i].inactive.open)
307 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
308 if ((parent = ruleset->anchor->parent) != NULL)
309 RB_REMOVE(pf_anchor_node, &parent->children,
311 rs_free(ruleset->anchor);
314 ruleset = &parent->ruleset;
319 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
323 struct pf_ruleset *ruleset;
326 r->anchor_relative = 0;
327 r->anchor_wildcard = 0;
330 path = (char *)rs_malloc(MAXPATHLEN);
331 bzero(path, MAXPATHLEN);
333 strlcpy(path, name + 1, MAXPATHLEN);
336 r->anchor_relative = 1;
337 if (s->anchor == NULL || !s->anchor->path[0])
340 strlcpy(path, s->anchor->path, MAXPATHLEN);
341 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
343 printf("pf_anchor_setup: .. beyond root\n");
347 if ((p = strrchr(path, '/')) != NULL)
351 r->anchor_relative++;
355 strlcat(path, "/", MAXPATHLEN);
356 strlcat(path, name, MAXPATHLEN);
358 if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
359 r->anchor_wildcard = 1;
362 ruleset = pf_find_or_create_ruleset(path);
364 if (ruleset == NULL || ruleset->anchor == NULL) {
365 printf("pf_anchor_setup: ruleset\n");
368 r->anchor = ruleset->anchor;
374 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
375 struct pfioc_rule *pr)
377 pr->anchor_call[0] = 0;
378 if (r->anchor == NULL)
380 if (!r->anchor_relative) {
381 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
382 strlcat(pr->anchor_call, r->anchor->path,
383 sizeof(pr->anchor_call));
388 a = (char *)rs_malloc(MAXPATHLEN);
389 bzero(a, MAXPATHLEN);
390 if (rs->anchor == NULL)
393 strlcpy(a, rs->anchor->path, MAXPATHLEN);
394 for (i = 1; i < r->anchor_relative; ++i) {
395 if ((p = strrchr(a, '/')) == NULL)
398 strlcat(pr->anchor_call, "../",
399 sizeof(pr->anchor_call));
401 if (strncmp(a, r->anchor->path, strlen(a))) {
402 printf("pf_anchor_copyout: '%s' '%s'\n", a,
407 if (strlen(r->anchor->path) > strlen(a))
408 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
409 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
412 if (r->anchor_wildcard)
413 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
414 sizeof(pr->anchor_call));
419 pf_anchor_remove(struct pf_rule *r)
421 if (r->anchor == NULL)
423 if (r->anchor->refcnt <= 0) {
424 printf("pf_anchor_remove: broken refcount\n");
428 if (!--r->anchor->refcnt)
429 pf_remove_if_empty_ruleset(&r->anchor->ruleset);