2 * Copyright (c) 2007-2011 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
);
174 return (PF_RULESET_DUMMYNET
);
175 #endif /* DUMMYNET */
177 return (PF_RULESET_MAX
);
183 pf_init_ruleset(struct pf_ruleset
*ruleset
)
187 memset(ruleset
, 0, sizeof (struct pf_ruleset
));
188 for (i
= 0; i
< PF_RULESET_MAX
; i
++) {
189 TAILQ_INIT(&ruleset
->rules
[i
].queues
[0]);
190 TAILQ_INIT(&ruleset
->rules
[i
].queues
[1]);
191 ruleset
->rules
[i
].active
.ptr
= &ruleset
->rules
[i
].queues
[0];
192 ruleset
->rules
[i
].inactive
.ptr
= &ruleset
->rules
[i
].queues
[1];
197 pf_find_anchor(const char *path
)
199 struct pf_anchor
*key
, *found
;
201 key
= (struct pf_anchor
*)rs_malloc(sizeof (*key
));
202 memset(key
, 0, sizeof (*key
));
203 strlcpy(key
->path
, path
, sizeof (key
->path
));
204 found
= RB_FIND(pf_anchor_global
, &pf_anchors
, key
);
210 pf_find_ruleset(const char *path
)
212 struct pf_anchor
*anchor
;
217 return (&pf_main_ruleset
);
218 anchor
= pf_find_anchor(path
);
222 return (&anchor
->ruleset
);
226 pf_find_ruleset_with_owner(const char *path
, const char *owner
, int is_anchor
,
229 struct pf_anchor
*anchor
;
234 return (&pf_main_ruleset
);
235 anchor
= pf_find_anchor(path
);
236 if (anchor
== NULL
) {
240 if ((owner
&& anchor
->owner
&& (!strcmp(owner
, anchor
->owner
)))
241 || (is_anchor
&& !strcmp(anchor
->owner
, "")))
242 return (&anchor
->ruleset
);
249 pf_find_or_create_ruleset(const char *path
)
252 struct pf_ruleset
*ruleset
;
253 struct pf_anchor
*anchor
= 0, *dup
, *parent
= NULL
;
256 return (&pf_main_ruleset
);
259 ruleset
= pf_find_ruleset(path
);
262 p
= (char *)rs_malloc(MAXPATHLEN
);
263 bzero(p
, MAXPATHLEN
);
264 strlcpy(p
, path
, MAXPATHLEN
);
265 while (parent
== NULL
&& (q
= strrchr(p
, '/')) != NULL
) {
267 if ((ruleset
= pf_find_ruleset(p
)) != NULL
) {
268 parent
= ruleset
->anchor
;
276 strlcpy(p
, path
, MAXPATHLEN
);
281 while ((r
= strchr(q
, '/')) != NULL
|| *q
) {
284 if (!*q
|| strlen(q
) >= PF_ANCHOR_NAME_SIZE
||
285 (parent
!= NULL
&& strlen(parent
->path
) >=
286 MAXPATHLEN
- PF_ANCHOR_NAME_SIZE
- 1)) {
290 anchor
= (struct pf_anchor
*)rs_malloc(sizeof (*anchor
));
291 if (anchor
== NULL
) {
295 memset(anchor
, 0, sizeof (*anchor
));
296 RB_INIT(&anchor
->children
);
297 strlcpy(anchor
->name
, q
, sizeof (anchor
->name
));
298 if (parent
!= NULL
) {
299 strlcpy(anchor
->path
, parent
->path
,
300 sizeof (anchor
->path
));
301 strlcat(anchor
->path
, "/", sizeof (anchor
->path
));
303 strlcat(anchor
->path
, anchor
->name
, sizeof (anchor
->path
));
304 if ((dup
= RB_INSERT(pf_anchor_global
, &pf_anchors
, anchor
)) !=
306 printf("pf_find_or_create_ruleset: RB_INSERT1 "
307 "'%s' '%s' collides with '%s' '%s'\n",
308 anchor
->path
, anchor
->name
, dup
->path
, dup
->name
);
313 if (parent
!= NULL
) {
314 anchor
->parent
= parent
;
315 if ((dup
= RB_INSERT(pf_anchor_node
, &parent
->children
,
317 printf("pf_find_or_create_ruleset: "
318 "RB_INSERT2 '%s' '%s' collides with "
319 "'%s' '%s'\n", anchor
->path
, anchor
->name
,
320 dup
->path
, dup
->name
);
321 RB_REMOVE(pf_anchor_global
, &pf_anchors
,
328 pf_init_ruleset(&anchor
->ruleset
);
329 anchor
->ruleset
.anchor
= anchor
;
337 return (anchor
? &anchor
->ruleset
: 0);
341 pf_remove_if_empty_ruleset(struct pf_ruleset
*ruleset
)
343 struct pf_anchor
*parent
;
346 while (ruleset
!= NULL
) {
347 if (ruleset
== &pf_main_ruleset
|| ruleset
->anchor
== NULL
||
348 !RB_EMPTY(&ruleset
->anchor
->children
) ||
349 ruleset
->anchor
->refcnt
> 0 || ruleset
->tables
> 0 ||
352 for (i
= 0; i
< PF_RULESET_MAX
; ++i
)
353 if (!TAILQ_EMPTY(ruleset
->rules
[i
].active
.ptr
) ||
354 !TAILQ_EMPTY(ruleset
->rules
[i
].inactive
.ptr
) ||
355 ruleset
->rules
[i
].inactive
.open
)
357 RB_REMOVE(pf_anchor_global
, &pf_anchors
, ruleset
->anchor
);
358 if ((parent
= ruleset
->anchor
->parent
) != NULL
)
359 RB_REMOVE(pf_anchor_node
, &parent
->children
,
361 rs_free(ruleset
->anchor
);
364 ruleset
= &parent
->ruleset
;
369 pf_anchor_setup(struct pf_rule
*r
, const struct pf_ruleset
*s
,
373 struct pf_ruleset
*ruleset
;
376 r
->anchor_relative
= 0;
377 r
->anchor_wildcard
= 0;
380 path
= (char *)rs_malloc(MAXPATHLEN
);
381 bzero(path
, MAXPATHLEN
);
383 strlcpy(path
, name
+ 1, MAXPATHLEN
);
386 r
->anchor_relative
= 1;
387 if (s
->anchor
== NULL
|| !s
->anchor
->path
[0])
390 strlcpy(path
, s
->anchor
->path
, MAXPATHLEN
);
391 while (name
[0] == '.' && name
[1] == '.' && name
[2] == '/') {
393 printf("pf_anchor_setup: .. beyond root\n");
397 if ((p
= strrchr(path
, '/')) != NULL
)
401 r
->anchor_relative
++;
405 strlcat(path
, "/", MAXPATHLEN
);
406 strlcat(path
, name
, MAXPATHLEN
);
408 if ((p
= strrchr(path
, '/')) != NULL
&& strcmp(p
, "/*") == 0) {
409 r
->anchor_wildcard
= 1;
412 ruleset
= pf_find_or_create_ruleset(path
);
414 if (ruleset
== NULL
|| ruleset
->anchor
== NULL
) {
415 printf("pf_anchor_setup: ruleset\n");
418 r
->anchor
= ruleset
->anchor
;
424 pf_anchor_copyout(const struct pf_ruleset
*rs
, const struct pf_rule
*r
,
425 struct pfioc_rule
*pr
)
427 pr
->anchor_call
[0] = 0;
428 if (r
->anchor
== NULL
)
430 if (!r
->anchor_relative
) {
431 strlcpy(pr
->anchor_call
, "/", sizeof (pr
->anchor_call
));
432 strlcat(pr
->anchor_call
, r
->anchor
->path
,
433 sizeof (pr
->anchor_call
));
438 a
= (char *)rs_malloc(MAXPATHLEN
);
439 bzero(a
, MAXPATHLEN
);
440 if (rs
->anchor
== NULL
)
443 strlcpy(a
, rs
->anchor
->path
, MAXPATHLEN
);
444 for (i
= 1; i
< r
->anchor_relative
; ++i
) {
445 if ((p
= strrchr(a
, '/')) == NULL
)
448 strlcat(pr
->anchor_call
, "../",
449 sizeof (pr
->anchor_call
));
451 if (strncmp(a
, r
->anchor
->path
, strlen(a
))) {
452 printf("pf_anchor_copyout: '%s' '%s'\n", a
,
457 if (strlen(r
->anchor
->path
) > strlen(a
))
458 strlcat(pr
->anchor_call
, r
->anchor
->path
+ (a
[0] ?
459 strlen(a
) + 1 : 0), sizeof (pr
->anchor_call
));
462 if (r
->anchor_wildcard
)
463 strlcat(pr
->anchor_call
, pr
->anchor_call
[0] ? "/*" : "*",
464 sizeof (pr
->anchor_call
));
469 pf_anchor_remove(struct pf_rule
*r
)
471 if (r
->anchor
== NULL
)
473 if (r
->anchor
->refcnt
<= 0) {
474 printf("pf_anchor_remove: broken refcount\n");
478 if (!--r
->anchor
->refcnt
)
479 pf_remove_if_empty_ruleset(&r
->anchor
->ruleset
);