2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $apfw: pf_ruleset.c,v 1.2 2007/08/10 03:00:16 jhw Exp $ */
30 /* $OpenBSD: pf_ruleset.c,v 1.1 2006/10/27 13:56:51 mcbride Exp $ */
33 * Copyright (c) 2001 Daniel Hartmeier
34 * Copyright (c) 2002,2003 Henning Brauer
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * - Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * - Redistributions in binary form must reproduce the above
44 * copyright notice, this list of conditions and the following
45 * disclaimer in the documentation and/or other materials provided
46 * with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
61 * Effort sponsored in part by the Defense Advanced Research Projects
62 * Agency (DARPA) and Air Force Research Laboratory, Air Force
63 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
67 #include <sys/param.h>
68 #include <sys/socket.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <libkern/libkern.h>
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79 #include <netinet/tcp.h>
82 #include <net/pfvar.h>
85 #include <netinet/ip6.h>
90 #define DPFPRINTF(format, x...) \
91 if (pf_status.debug >= PF_DEBUG_NOISY) \
93 #define rs_malloc(x) _MALLOC(x, M_TEMP, M_WAITOK)
94 #define rs_free(x) _FREE(x, M_TEMP)
95 #define strrchr _strrchr
98 _strrchr(const char *c
, int ch
)
100 char *p
= (char *)(size_t)c
, *save
;
102 for (save
= NULL
; ; ++p
) {
112 /* Userland equivalents so we can lend code to pfctl et al. */
114 #include <arpa/inet.h>
119 #define rs_malloc(x) malloc(x)
120 #define rs_free(x) free(x)
123 #include <sys/stdarg.h>
124 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
126 #define DPFPRINTF(format, x...) ((void)0)
131 struct pf_anchor_global pf_anchors
;
132 struct pf_anchor pf_main_anchor
;
134 static __inline
int pf_anchor_compare(struct pf_anchor
*, struct pf_anchor
*);
136 RB_GENERATE(pf_anchor_global
, pf_anchor
, entry_global
, pf_anchor_compare
);
137 RB_GENERATE(pf_anchor_node
, pf_anchor
, entry_node
, pf_anchor_compare
);
140 pf_anchor_compare(struct pf_anchor
*a
, struct pf_anchor
*b
)
142 int c
= strcmp(a
->path
, b
->path
);
144 return (c
? (c
< 0 ? -1 : 1) : 0);
148 pf_get_ruleset_number(u_int8_t action
)
153 return (PF_RULESET_SCRUB
);
157 return (PF_RULESET_FILTER
);
161 return (PF_RULESET_NAT
);
165 return (PF_RULESET_BINAT
);
169 return (PF_RULESET_RDR
);
172 return (PF_RULESET_MAX
);
178 pf_init_ruleset(struct pf_ruleset
*ruleset
)
182 memset(ruleset
, 0, sizeof (struct pf_ruleset
));
183 for (i
= 0; i
< PF_RULESET_MAX
; i
++) {
184 TAILQ_INIT(&ruleset
->rules
[i
].queues
[0]);
185 TAILQ_INIT(&ruleset
->rules
[i
].queues
[1]);
186 ruleset
->rules
[i
].active
.ptr
= &ruleset
->rules
[i
].queues
[0];
187 ruleset
->rules
[i
].inactive
.ptr
= &ruleset
->rules
[i
].queues
[1];
192 pf_find_anchor(const char *path
)
194 struct pf_anchor
*key
, *found
;
196 key
= (struct pf_anchor
*)rs_malloc(sizeof (*key
));
197 memset(key
, 0, sizeof (*key
));
198 strlcpy(key
->path
, path
, sizeof (key
->path
));
199 found
= RB_FIND(pf_anchor_global
, &pf_anchors
, key
);
205 pf_find_ruleset(const char *path
)
207 struct pf_anchor
*anchor
;
212 return (&pf_main_ruleset
);
213 anchor
= pf_find_anchor(path
);
217 return (&anchor
->ruleset
);
221 pf_find_or_create_ruleset(const char *path
)
224 struct pf_ruleset
*ruleset
;
225 struct pf_anchor
*anchor
= 0, *dup
, *parent
= NULL
;
228 return (&pf_main_ruleset
);
231 ruleset
= pf_find_ruleset(path
);
234 p
= (char *)rs_malloc(MAXPATHLEN
);
235 bzero(p
, MAXPATHLEN
);
236 strlcpy(p
, path
, MAXPATHLEN
);
237 while (parent
== NULL
&& (q
= strrchr(p
, '/')) != NULL
) {
239 if ((ruleset
= pf_find_ruleset(p
)) != NULL
) {
240 parent
= ruleset
->anchor
;
248 strlcpy(p
, path
, MAXPATHLEN
);
253 while ((r
= strchr(q
, '/')) != NULL
|| *q
) {
256 if (!*q
|| strlen(q
) >= PF_ANCHOR_NAME_SIZE
||
257 (parent
!= NULL
&& strlen(parent
->path
) >=
258 MAXPATHLEN
- PF_ANCHOR_NAME_SIZE
- 1)) {
262 anchor
= (struct pf_anchor
*)rs_malloc(sizeof (*anchor
));
263 if (anchor
== NULL
) {
267 memset(anchor
, 0, sizeof (*anchor
));
268 RB_INIT(&anchor
->children
);
269 strlcpy(anchor
->name
, q
, sizeof (anchor
->name
));
270 if (parent
!= NULL
) {
271 strlcpy(anchor
->path
, parent
->path
,
272 sizeof (anchor
->path
));
273 strlcat(anchor
->path
, "/", sizeof (anchor
->path
));
275 strlcat(anchor
->path
, anchor
->name
, sizeof (anchor
->path
));
276 if ((dup
= RB_INSERT(pf_anchor_global
, &pf_anchors
, anchor
)) !=
278 printf("pf_find_or_create_ruleset: RB_INSERT1 "
279 "'%s' '%s' collides with '%s' '%s'\n",
280 anchor
->path
, anchor
->name
, dup
->path
, dup
->name
);
285 if (parent
!= NULL
) {
286 anchor
->parent
= parent
;
287 if ((dup
= RB_INSERT(pf_anchor_node
, &parent
->children
,
289 printf("pf_find_or_create_ruleset: "
290 "RB_INSERT2 '%s' '%s' collides with "
291 "'%s' '%s'\n", anchor
->path
, anchor
->name
,
292 dup
->path
, dup
->name
);
293 RB_REMOVE(pf_anchor_global
, &pf_anchors
,
300 pf_init_ruleset(&anchor
->ruleset
);
301 anchor
->ruleset
.anchor
= anchor
;
309 return (anchor
? &anchor
->ruleset
: 0);
313 pf_remove_if_empty_ruleset(struct pf_ruleset
*ruleset
)
315 struct pf_anchor
*parent
;
318 while (ruleset
!= NULL
) {
319 if (ruleset
== &pf_main_ruleset
|| ruleset
->anchor
== NULL
||
320 !RB_EMPTY(&ruleset
->anchor
->children
) ||
321 ruleset
->anchor
->refcnt
> 0 || ruleset
->tables
> 0 ||
324 for (i
= 0; i
< PF_RULESET_MAX
; ++i
)
325 if (!TAILQ_EMPTY(ruleset
->rules
[i
].active
.ptr
) ||
326 !TAILQ_EMPTY(ruleset
->rules
[i
].inactive
.ptr
) ||
327 ruleset
->rules
[i
].inactive
.open
)
329 RB_REMOVE(pf_anchor_global
, &pf_anchors
, ruleset
->anchor
);
330 if ((parent
= ruleset
->anchor
->parent
) != NULL
)
331 RB_REMOVE(pf_anchor_node
, &parent
->children
,
333 rs_free(ruleset
->anchor
);
336 ruleset
= &parent
->ruleset
;
341 pf_anchor_setup(struct pf_rule
*r
, const struct pf_ruleset
*s
,
345 struct pf_ruleset
*ruleset
;
348 r
->anchor_relative
= 0;
349 r
->anchor_wildcard
= 0;
352 path
= (char *)rs_malloc(MAXPATHLEN
);
353 bzero(path
, MAXPATHLEN
);
355 strlcpy(path
, name
+ 1, MAXPATHLEN
);
358 r
->anchor_relative
= 1;
359 if (s
->anchor
== NULL
|| !s
->anchor
->path
[0])
362 strlcpy(path
, s
->anchor
->path
, MAXPATHLEN
);
363 while (name
[0] == '.' && name
[1] == '.' && name
[2] == '/') {
365 printf("pf_anchor_setup: .. beyond root\n");
369 if ((p
= strrchr(path
, '/')) != NULL
)
373 r
->anchor_relative
++;
377 strlcat(path
, "/", MAXPATHLEN
);
378 strlcat(path
, name
, MAXPATHLEN
);
380 if ((p
= strrchr(path
, '/')) != NULL
&& strcmp(p
, "/*") == 0) {
381 r
->anchor_wildcard
= 1;
384 ruleset
= pf_find_or_create_ruleset(path
);
386 if (ruleset
== NULL
|| ruleset
->anchor
== NULL
) {
387 printf("pf_anchor_setup: ruleset\n");
390 r
->anchor
= ruleset
->anchor
;
396 pf_anchor_copyout(const struct pf_ruleset
*rs
, const struct pf_rule
*r
,
397 struct pfioc_rule
*pr
)
399 pr
->anchor_call
[0] = 0;
400 if (r
->anchor
== NULL
)
402 if (!r
->anchor_relative
) {
403 strlcpy(pr
->anchor_call
, "/", sizeof (pr
->anchor_call
));
404 strlcat(pr
->anchor_call
, r
->anchor
->path
,
405 sizeof (pr
->anchor_call
));
410 a
= (char *)rs_malloc(MAXPATHLEN
);
411 bzero(a
, MAXPATHLEN
);
412 if (rs
->anchor
== NULL
)
415 strlcpy(a
, rs
->anchor
->path
, MAXPATHLEN
);
416 for (i
= 1; i
< r
->anchor_relative
; ++i
) {
417 if ((p
= strrchr(a
, '/')) == NULL
)
420 strlcat(pr
->anchor_call
, "../",
421 sizeof (pr
->anchor_call
));
423 if (strncmp(a
, r
->anchor
->path
, strlen(a
))) {
424 printf("pf_anchor_copyout: '%s' '%s'\n", a
,
429 if (strlen(r
->anchor
->path
) > strlen(a
))
430 strlcat(pr
->anchor_call
, r
->anchor
->path
+ (a
[0] ?
431 strlen(a
) + 1 : 0), sizeof (pr
->anchor_call
));
434 if (r
->anchor_wildcard
)
435 strlcat(pr
->anchor_call
, pr
->anchor_call
[0] ? "/*" : "*",
436 sizeof (pr
->anchor_call
));
441 pf_anchor_remove(struct pf_rule
*r
)
443 if (r
->anchor
== NULL
)
445 if (r
->anchor
->refcnt
<= 0) {
446 printf("pf_anchor_remove: broken refcount\n");
450 if (!--r
->anchor
->refcnt
)
451 pf_remove_if_empty_ruleset(&r
->anchor
->ruleset
);