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/ip_dummynet.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/tcp.h>
84 #include <net/pfvar.h>
87 #include <netinet/ip6.h>
92 #define DPFPRINTF(format, x ...) \
93 if (pf_status.debug >= PF_DEBUG_NOISY) \
95 #define rs_malloc(x) _MALLOC(x, M_TEMP, M_WAITOK)
96 #define rs_free(x) _FREE(x, M_TEMP)
97 #define strrchr _strrchr
100 _strrchr(const char *c
, int ch
)
102 char *p
= (char *)(size_t)c
, *save
;
104 for (save
= NULL
;; ++p
) {
116 /* Userland equivalents so we can lend code to pfctl et al. */
118 #include <arpa/inet.h>
123 #define rs_malloc(x) malloc(x)
124 #define rs_free(x) free(x)
127 #include <sys/stdarg.h>
128 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
130 #define DPFPRINTF(format, x...) ((void)0)
135 struct pf_anchor_global pf_anchors
;
136 struct pf_anchor pf_main_anchor
;
138 static __inline
int pf_anchor_compare(struct pf_anchor
*, struct pf_anchor
*);
140 RB_GENERATE(pf_anchor_global
, pf_anchor
, entry_global
, pf_anchor_compare
);
141 RB_GENERATE(pf_anchor_node
, pf_anchor
, entry_node
, pf_anchor_compare
);
144 pf_anchor_compare(struct pf_anchor
*a
, struct pf_anchor
*b
)
146 int c
= strcmp(a
->path
, b
->path
);
148 return c
? (c
< 0 ? -1 : 1) : 0;
152 pf_get_ruleset_number(u_int8_t action
)
157 return PF_RULESET_SCRUB
;
160 return PF_RULESET_FILTER
;
163 return PF_RULESET_NAT
;
166 return PF_RULESET_BINAT
;
171 return PF_RULESET_RDR
;
175 return PF_RULESET_DUMMYNET
;
176 #endif /* DUMMYNET */
178 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
;
214 while (*path
== '/') {
218 return &pf_main_ruleset
;
220 anchor
= pf_find_anchor(path
);
221 if (anchor
== NULL
) {
224 return &anchor
->ruleset
;
229 pf_find_ruleset_with_owner(const char *path
, const char *owner
, int is_anchor
,
232 struct pf_anchor
*anchor
;
234 while (*path
== '/') {
238 return &pf_main_ruleset
;
240 anchor
= pf_find_anchor(path
);
241 if (anchor
== NULL
) {
245 if ((owner
&& (!strcmp(owner
, anchor
->owner
)))
246 || (is_anchor
&& !strcmp(anchor
->owner
, ""))) {
247 return &anchor
->ruleset
;
255 pf_find_or_create_ruleset(const char *path
)
257 char *p
, *q
= NULL
, *r
;
258 struct pf_ruleset
*ruleset
;
259 struct pf_anchor
*anchor
= 0, *dup
, *parent
= NULL
;
262 return &pf_main_ruleset
;
264 while (*path
== '/') {
267 ruleset
= pf_find_ruleset(path
);
268 if (ruleset
!= NULL
) {
271 p
= (char *)rs_malloc(MAXPATHLEN
);
272 bzero(p
, MAXPATHLEN
);
273 strlcpy(p
, path
, MAXPATHLEN
);
274 while (parent
== NULL
&& (q
= strrchr(p
, '/')) != NULL
) {
276 if ((ruleset
= pf_find_ruleset(p
)) != NULL
) {
277 parent
= ruleset
->anchor
;
286 strlcpy(p
, path
, MAXPATHLEN
);
291 while ((r
= strchr(q
, '/')) != NULL
|| *q
) {
295 if (!*q
|| strlen(q
) >= PF_ANCHOR_NAME_SIZE
||
296 (parent
!= NULL
&& strlen(parent
->path
) >=
297 MAXPATHLEN
- PF_ANCHOR_NAME_SIZE
- 1)) {
301 anchor
= (struct pf_anchor
*)rs_malloc(sizeof(*anchor
));
302 if (anchor
== NULL
) {
306 memset(anchor
, 0, sizeof(*anchor
));
307 RB_INIT(&anchor
->children
);
308 strlcpy(anchor
->name
, q
, sizeof(anchor
->name
));
309 if (parent
!= NULL
) {
310 strlcpy(anchor
->path
, parent
->path
,
311 sizeof(anchor
->path
));
312 strlcat(anchor
->path
, "/", sizeof(anchor
->path
));
314 strlcat(anchor
->path
, anchor
->name
, sizeof(anchor
->path
));
315 if ((dup
= RB_INSERT(pf_anchor_global
, &pf_anchors
, anchor
)) !=
317 printf("pf_find_or_create_ruleset: RB_INSERT1 "
318 "'%s' '%s' collides with '%s' '%s'\n",
319 anchor
->path
, anchor
->name
, dup
->path
, dup
->name
);
324 if (parent
!= NULL
) {
325 anchor
->parent
= parent
;
326 if ((dup
= RB_INSERT(pf_anchor_node
, &parent
->children
,
328 printf("pf_find_or_create_ruleset: "
329 "RB_INSERT2 '%s' '%s' collides with "
330 "'%s' '%s'\n", anchor
->path
, anchor
->name
,
331 dup
->path
, dup
->name
);
332 RB_REMOVE(pf_anchor_global
, &pf_anchors
,
339 pf_init_ruleset(&anchor
->ruleset
);
340 anchor
->ruleset
.anchor
= anchor
;
348 if (strncmp("com.apple.nlc", anchor
->name
,
349 sizeof("com.apple.nlc")) == 0) {
350 is_nlc_enabled_glb
= TRUE
;
355 return anchor
? &anchor
->ruleset
: 0;
359 pf_remove_if_empty_ruleset(struct pf_ruleset
*ruleset
)
361 struct pf_anchor
*parent
;
364 while (ruleset
!= NULL
) {
365 if (ruleset
== &pf_main_ruleset
|| ruleset
->anchor
== NULL
||
366 !RB_EMPTY(&ruleset
->anchor
->children
) ||
367 ruleset
->anchor
->refcnt
> 0 || ruleset
->tables
> 0 ||
371 for (i
= 0; i
< PF_RULESET_MAX
; ++i
) {
372 if (!TAILQ_EMPTY(ruleset
->rules
[i
].active
.ptr
) ||
373 !TAILQ_EMPTY(ruleset
->rules
[i
].inactive
.ptr
) ||
374 ruleset
->rules
[i
].inactive
.open
) {
378 RB_REMOVE(pf_anchor_global
, &pf_anchors
, ruleset
->anchor
);
380 if (strncmp("com.apple.nlc", ruleset
->anchor
->name
,
381 sizeof("com.apple.nlc")) == 0) {
382 struct dummynet_event dn_event
;
383 bzero(&dn_event
, sizeof(dn_event
));
384 dn_event
.dn_event_code
= DUMMYNET_NLC_DISABLED
;
385 dummynet_event_enqueue_nwk_wq_entry(&dn_event
);
386 is_nlc_enabled_glb
= FALSE
;
389 if ((parent
= ruleset
->anchor
->parent
) != NULL
) {
390 RB_REMOVE(pf_anchor_node
, &parent
->children
,
393 rs_free(ruleset
->anchor
);
394 if (parent
== NULL
) {
397 ruleset
= &parent
->ruleset
;
402 pf_anchor_setup(struct pf_rule
*r
, const struct pf_ruleset
*s
,
406 struct pf_ruleset
*ruleset
;
409 r
->anchor_relative
= 0;
410 r
->anchor_wildcard
= 0;
414 path
= (char *)rs_malloc(MAXPATHLEN
);
415 bzero(path
, MAXPATHLEN
);
416 if (name
[0] == '/') {
417 strlcpy(path
, name
+ 1, MAXPATHLEN
);
420 r
->anchor_relative
= 1;
421 if (s
->anchor
== NULL
|| !s
->anchor
->path
[0]) {
424 strlcpy(path
, s
->anchor
->path
, MAXPATHLEN
);
426 while (name
[0] == '.' && name
[1] == '.' && name
[2] == '/') {
428 printf("pf_anchor_setup: .. beyond root\n");
432 if ((p
= strrchr(path
, '/')) != NULL
) {
437 r
->anchor_relative
++;
441 strlcat(path
, "/", MAXPATHLEN
);
443 strlcat(path
, name
, MAXPATHLEN
);
445 if ((p
= strrchr(path
, '/')) != NULL
&& strcmp(p
, "/*") == 0) {
446 r
->anchor_wildcard
= 1;
449 ruleset
= pf_find_or_create_ruleset(path
);
451 if (ruleset
== NULL
|| ruleset
->anchor
== NULL
) {
452 printf("pf_anchor_setup: ruleset\n");
455 r
->anchor
= ruleset
->anchor
;
461 pf_anchor_copyout(const struct pf_ruleset
*rs
, const struct pf_rule
*r
,
462 struct pfioc_rule
*pr
)
464 pr
->anchor_call
[0] = 0;
465 if (r
->anchor
== NULL
) {
468 if (!r
->anchor_relative
) {
469 strlcpy(pr
->anchor_call
, "/", sizeof(pr
->anchor_call
));
470 strlcat(pr
->anchor_call
, r
->anchor
->path
,
471 sizeof(pr
->anchor_call
));
476 a
= (char *)rs_malloc(MAXPATHLEN
);
477 bzero(a
, MAXPATHLEN
);
478 if (rs
->anchor
== NULL
) {
481 strlcpy(a
, rs
->anchor
->path
, MAXPATHLEN
);
483 for (i
= 1; i
< r
->anchor_relative
; ++i
) {
484 if ((p
= strrchr(a
, '/')) == NULL
) {
488 strlcat(pr
->anchor_call
, "../",
489 sizeof(pr
->anchor_call
));
491 if (strncmp(a
, r
->anchor
->path
, strlen(a
))) {
492 printf("pf_anchor_copyout: '%s' '%s'\n", a
,
497 if (strlen(r
->anchor
->path
) > strlen(a
)) {
498 strlcat(pr
->anchor_call
, r
->anchor
->path
+ (a
[0] ?
499 strlen(a
) + 1 : 0), sizeof(pr
->anchor_call
));
503 if (r
->anchor_wildcard
) {
504 strlcat(pr
->anchor_call
, pr
->anchor_call
[0] ? "/*" : "*",
505 sizeof(pr
->anchor_call
));
511 pf_anchor_remove(struct pf_rule
*r
)
513 if (r
->anchor
== NULL
) {
516 if (r
->anchor
->refcnt
<= 0) {
517 printf("pf_anchor_remove: broken refcount\n");
521 if (!--r
->anchor
->refcnt
) {
522 pf_remove_if_empty_ruleset(&r
->anchor
->ruleset
);