2 * Copyright (c) 2007-2016 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 * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * - Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * - Redistributions in binary form must reproduce the above
45 * copyright notice, this list of conditions and the following
46 * disclaimer in the documentation and/or other materials provided
47 * with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
52 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
53 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
59 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
62 * Effort sponsored in part by the Defense Advanced Research Projects
63 * Agency (DARPA) and Air Force Research Laboratory, Air Force
64 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
68 #include <sys/param.h>
69 #include <sys/socket.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <libkern/libkern.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/tcp.h>
83 #include <net/pfvar.h>
86 #include <netinet/ip6.h>
91 #define DPFPRINTF(format, x...) \
92 if (pf_status.debug >= PF_DEBUG_NOISY) \
94 #define rs_malloc(x) _MALLOC(x, M_TEMP, M_WAITOK)
95 #define rs_free(x) _FREE(x, M_TEMP)
96 #define strrchr _strrchr
99 _strrchr(const char *c
, int ch
)
101 char *p
= (char *)(size_t)c
, *save
;
103 for (save
= NULL
; ; ++p
) {
113 /* Userland equivalents so we can lend code to pfctl et al. */
115 #include <arpa/inet.h>
120 #define rs_malloc(x) malloc(x)
121 #define rs_free(x) free(x)
124 #include <sys/stdarg.h>
125 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
127 #define DPFPRINTF(format, x...) ((void)0)
132 struct pf_anchor_global pf_anchors
;
133 struct pf_anchor pf_main_anchor
;
135 static __inline
int pf_anchor_compare(struct pf_anchor
*, struct pf_anchor
*);
137 RB_GENERATE(pf_anchor_global
, pf_anchor
, entry_global
, pf_anchor_compare
);
138 RB_GENERATE(pf_anchor_node
, pf_anchor
, entry_node
, pf_anchor_compare
);
141 pf_anchor_compare(struct pf_anchor
*a
, struct pf_anchor
*b
)
143 int c
= strcmp(a
->path
, b
->path
);
145 return (c
? (c
< 0 ? -1 : 1) : 0);
149 pf_get_ruleset_number(u_int8_t action
)
154 return (PF_RULESET_SCRUB
);
157 return (PF_RULESET_FILTER
);
160 return (PF_RULESET_NAT
);
163 return (PF_RULESET_BINAT
);
168 return (PF_RULESET_RDR
);
172 return (PF_RULESET_DUMMYNET
);
173 #endif /* DUMMYNET */
175 return (PF_RULESET_MAX
);
180 pf_init_ruleset(struct pf_ruleset
*ruleset
)
184 memset(ruleset
, 0, sizeof (struct pf_ruleset
));
185 for (i
= 0; i
< PF_RULESET_MAX
; i
++) {
186 TAILQ_INIT(&ruleset
->rules
[i
].queues
[0]);
187 TAILQ_INIT(&ruleset
->rules
[i
].queues
[1]);
188 ruleset
->rules
[i
].active
.ptr
= &ruleset
->rules
[i
].queues
[0];
189 ruleset
->rules
[i
].inactive
.ptr
= &ruleset
->rules
[i
].queues
[1];
194 pf_find_anchor(const char *path
)
196 struct pf_anchor
*key
, *found
;
198 key
= (struct pf_anchor
*)rs_malloc(sizeof (*key
));
199 memset(key
, 0, sizeof (*key
));
200 strlcpy(key
->path
, path
, sizeof (key
->path
));
201 found
= RB_FIND(pf_anchor_global
, &pf_anchors
, key
);
207 pf_find_ruleset(const char *path
)
209 struct pf_anchor
*anchor
;
214 return (&pf_main_ruleset
);
215 anchor
= pf_find_anchor(path
);
219 return (&anchor
->ruleset
);
223 pf_find_ruleset_with_owner(const char *path
, const char *owner
, int is_anchor
,
226 struct pf_anchor
*anchor
;
231 return (&pf_main_ruleset
);
232 anchor
= pf_find_anchor(path
);
233 if (anchor
== NULL
) {
237 if ((owner
&& (!strcmp(owner
, anchor
->owner
)))
238 || (is_anchor
&& !strcmp(anchor
->owner
, "")))
239 return (&anchor
->ruleset
);
246 pf_find_or_create_ruleset(const char *path
)
249 struct pf_ruleset
*ruleset
;
250 struct pf_anchor
*anchor
= 0, *dup
, *parent
= NULL
;
253 return (&pf_main_ruleset
);
256 ruleset
= pf_find_ruleset(path
);
259 p
= (char *)rs_malloc(MAXPATHLEN
);
260 bzero(p
, MAXPATHLEN
);
261 strlcpy(p
, path
, MAXPATHLEN
);
262 while (parent
== NULL
&& (q
= strrchr(p
, '/')) != NULL
) {
264 if ((ruleset
= pf_find_ruleset(p
)) != NULL
) {
265 parent
= ruleset
->anchor
;
273 strlcpy(p
, path
, MAXPATHLEN
);
278 while ((r
= strchr(q
, '/')) != NULL
|| *q
) {
281 if (!*q
|| strlen(q
) >= PF_ANCHOR_NAME_SIZE
||
282 (parent
!= NULL
&& strlen(parent
->path
) >=
283 MAXPATHLEN
- PF_ANCHOR_NAME_SIZE
- 1)) {
287 anchor
= (struct pf_anchor
*)rs_malloc(sizeof (*anchor
));
288 if (anchor
== NULL
) {
292 memset(anchor
, 0, sizeof (*anchor
));
293 RB_INIT(&anchor
->children
);
294 strlcpy(anchor
->name
, q
, sizeof (anchor
->name
));
295 if (parent
!= NULL
) {
296 strlcpy(anchor
->path
, parent
->path
,
297 sizeof (anchor
->path
));
298 strlcat(anchor
->path
, "/", sizeof (anchor
->path
));
300 strlcat(anchor
->path
, anchor
->name
, sizeof (anchor
->path
));
301 if ((dup
= RB_INSERT(pf_anchor_global
, &pf_anchors
, anchor
)) !=
303 printf("pf_find_or_create_ruleset: RB_INSERT1 "
304 "'%s' '%s' collides with '%s' '%s'\n",
305 anchor
->path
, anchor
->name
, dup
->path
, dup
->name
);
310 if (parent
!= NULL
) {
311 anchor
->parent
= parent
;
312 if ((dup
= RB_INSERT(pf_anchor_node
, &parent
->children
,
314 printf("pf_find_or_create_ruleset: "
315 "RB_INSERT2 '%s' '%s' collides with "
316 "'%s' '%s'\n", anchor
->path
, anchor
->name
,
317 dup
->path
, dup
->name
);
318 RB_REMOVE(pf_anchor_global
, &pf_anchors
,
325 pf_init_ruleset(&anchor
->ruleset
);
326 anchor
->ruleset
.anchor
= anchor
;
334 return (anchor
? &anchor
->ruleset
: 0);
338 pf_remove_if_empty_ruleset(struct pf_ruleset
*ruleset
)
340 struct pf_anchor
*parent
;
343 while (ruleset
!= NULL
) {
344 if (ruleset
== &pf_main_ruleset
|| ruleset
->anchor
== NULL
||
345 !RB_EMPTY(&ruleset
->anchor
->children
) ||
346 ruleset
->anchor
->refcnt
> 0 || ruleset
->tables
> 0 ||
349 for (i
= 0; i
< PF_RULESET_MAX
; ++i
)
350 if (!TAILQ_EMPTY(ruleset
->rules
[i
].active
.ptr
) ||
351 !TAILQ_EMPTY(ruleset
->rules
[i
].inactive
.ptr
) ||
352 ruleset
->rules
[i
].inactive
.open
)
354 RB_REMOVE(pf_anchor_global
, &pf_anchors
, ruleset
->anchor
);
355 if ((parent
= ruleset
->anchor
->parent
) != NULL
)
356 RB_REMOVE(pf_anchor_node
, &parent
->children
,
358 rs_free(ruleset
->anchor
);
361 ruleset
= &parent
->ruleset
;
366 pf_anchor_setup(struct pf_rule
*r
, const struct pf_ruleset
*s
,
370 struct pf_ruleset
*ruleset
;
373 r
->anchor_relative
= 0;
374 r
->anchor_wildcard
= 0;
377 path
= (char *)rs_malloc(MAXPATHLEN
);
378 bzero(path
, MAXPATHLEN
);
380 strlcpy(path
, name
+ 1, MAXPATHLEN
);
383 r
->anchor_relative
= 1;
384 if (s
->anchor
== NULL
|| !s
->anchor
->path
[0])
387 strlcpy(path
, s
->anchor
->path
, MAXPATHLEN
);
388 while (name
[0] == '.' && name
[1] == '.' && name
[2] == '/') {
390 printf("pf_anchor_setup: .. beyond root\n");
394 if ((p
= strrchr(path
, '/')) != NULL
)
398 r
->anchor_relative
++;
402 strlcat(path
, "/", MAXPATHLEN
);
403 strlcat(path
, name
, MAXPATHLEN
);
405 if ((p
= strrchr(path
, '/')) != NULL
&& strcmp(p
, "/*") == 0) {
406 r
->anchor_wildcard
= 1;
409 ruleset
= pf_find_or_create_ruleset(path
);
411 if (ruleset
== NULL
|| ruleset
->anchor
== NULL
) {
412 printf("pf_anchor_setup: ruleset\n");
415 r
->anchor
= ruleset
->anchor
;
421 pf_anchor_copyout(const struct pf_ruleset
*rs
, const struct pf_rule
*r
,
422 struct pfioc_rule
*pr
)
424 pr
->anchor_call
[0] = 0;
425 if (r
->anchor
== NULL
)
427 if (!r
->anchor_relative
) {
428 strlcpy(pr
->anchor_call
, "/", sizeof (pr
->anchor_call
));
429 strlcat(pr
->anchor_call
, r
->anchor
->path
,
430 sizeof (pr
->anchor_call
));
435 a
= (char *)rs_malloc(MAXPATHLEN
);
436 bzero(a
, MAXPATHLEN
);
437 if (rs
->anchor
== NULL
)
440 strlcpy(a
, rs
->anchor
->path
, MAXPATHLEN
);
441 for (i
= 1; i
< r
->anchor_relative
; ++i
) {
442 if ((p
= strrchr(a
, '/')) == NULL
)
445 strlcat(pr
->anchor_call
, "../",
446 sizeof (pr
->anchor_call
));
448 if (strncmp(a
, r
->anchor
->path
, strlen(a
))) {
449 printf("pf_anchor_copyout: '%s' '%s'\n", a
,
454 if (strlen(r
->anchor
->path
) > strlen(a
))
455 strlcat(pr
->anchor_call
, r
->anchor
->path
+ (a
[0] ?
456 strlen(a
) + 1 : 0), sizeof (pr
->anchor_call
));
459 if (r
->anchor_wildcard
)
460 strlcat(pr
->anchor_call
, pr
->anchor_call
[0] ? "/*" : "*",
461 sizeof (pr
->anchor_call
));
466 pf_anchor_remove(struct pf_rule
*r
)
468 if (r
->anchor
== NULL
)
470 if (r
->anchor
->refcnt
<= 0) {
471 printf("pf_anchor_remove: broken refcount\n");
475 if (!--r
->anchor
->refcnt
)
476 pf_remove_if_empty_ruleset(&r
->anchor
->ruleset
);