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
) {
114 /* Userland equivalents so we can lend code to pfctl et al. */
116 #include <arpa/inet.h>
121 #define rs_malloc(x) malloc(x)
122 #define rs_free(x) free(x)
125 #include <sys/stdarg.h>
126 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
128 #define DPFPRINTF(format, x...) ((void)0)
133 struct pf_anchor_global pf_anchors
;
134 struct pf_anchor pf_main_anchor
;
136 static __inline
int pf_anchor_compare(struct pf_anchor
*, struct pf_anchor
*);
138 RB_GENERATE(pf_anchor_global
, pf_anchor
, entry_global
, pf_anchor_compare
);
139 RB_GENERATE(pf_anchor_node
, pf_anchor
, entry_node
, pf_anchor_compare
);
142 pf_anchor_compare(struct pf_anchor
*a
, struct pf_anchor
*b
)
144 int c
= strcmp(a
->path
, b
->path
);
146 return (c
? (c
< 0 ? -1 : 1) : 0);
150 pf_get_ruleset_number(u_int8_t action
)
155 return (PF_RULESET_SCRUB
);
158 return (PF_RULESET_FILTER
);
161 return (PF_RULESET_NAT
);
164 return (PF_RULESET_BINAT
);
169 return (PF_RULESET_RDR
);
173 return (PF_RULESET_DUMMYNET
);
174 #endif /* DUMMYNET */
176 return (PF_RULESET_MAX
);
181 pf_init_ruleset(struct pf_ruleset
*ruleset
)
185 memset(ruleset
, 0, sizeof (struct pf_ruleset
));
186 for (i
= 0; i
< PF_RULESET_MAX
; i
++) {
187 TAILQ_INIT(&ruleset
->rules
[i
].queues
[0]);
188 TAILQ_INIT(&ruleset
->rules
[i
].queues
[1]);
189 ruleset
->rules
[i
].active
.ptr
= &ruleset
->rules
[i
].queues
[0];
190 ruleset
->rules
[i
].inactive
.ptr
= &ruleset
->rules
[i
].queues
[1];
195 pf_find_anchor(const char *path
)
197 struct pf_anchor
*key
, *found
;
199 key
= (struct pf_anchor
*)rs_malloc(sizeof (*key
));
200 memset(key
, 0, sizeof (*key
));
201 strlcpy(key
->path
, path
, sizeof (key
->path
));
202 found
= RB_FIND(pf_anchor_global
, &pf_anchors
, key
);
208 pf_find_ruleset(const char *path
)
210 struct pf_anchor
*anchor
;
215 return (&pf_main_ruleset
);
216 anchor
= pf_find_anchor(path
);
220 return (&anchor
->ruleset
);
224 pf_find_ruleset_with_owner(const char *path
, const char *owner
, int is_anchor
,
227 struct pf_anchor
*anchor
;
232 return (&pf_main_ruleset
);
233 anchor
= pf_find_anchor(path
);
234 if (anchor
== NULL
) {
238 if ((owner
&& (!strcmp(owner
, anchor
->owner
)))
239 || (is_anchor
&& !strcmp(anchor
->owner
, "")))
240 return (&anchor
->ruleset
);
247 pf_find_or_create_ruleset(const char *path
)
249 char *p
, *q
= NULL
, *r
;
250 struct pf_ruleset
*ruleset
;
251 struct pf_anchor
*anchor
= 0, *dup
, *parent
= NULL
;
254 return (&pf_main_ruleset
);
257 ruleset
= pf_find_ruleset(path
);
260 p
= (char *)rs_malloc(MAXPATHLEN
);
261 bzero(p
, MAXPATHLEN
);
262 strlcpy(p
, path
, MAXPATHLEN
);
263 while (parent
== NULL
&& (q
= strrchr(p
, '/')) != NULL
) {
265 if ((ruleset
= pf_find_ruleset(p
)) != NULL
) {
266 parent
= ruleset
->anchor
;
274 strlcpy(p
, path
, MAXPATHLEN
);
279 while ((r
= strchr(q
, '/')) != NULL
|| *q
) {
282 if (!*q
|| strlen(q
) >= PF_ANCHOR_NAME_SIZE
||
283 (parent
!= NULL
&& strlen(parent
->path
) >=
284 MAXPATHLEN
- PF_ANCHOR_NAME_SIZE
- 1)) {
288 anchor
= (struct pf_anchor
*)rs_malloc(sizeof (*anchor
));
289 if (anchor
== NULL
) {
293 memset(anchor
, 0, sizeof (*anchor
));
294 RB_INIT(&anchor
->children
);
295 strlcpy(anchor
->name
, q
, sizeof (anchor
->name
));
296 if (parent
!= NULL
) {
297 strlcpy(anchor
->path
, parent
->path
,
298 sizeof (anchor
->path
));
299 strlcat(anchor
->path
, "/", sizeof (anchor
->path
));
301 strlcat(anchor
->path
, anchor
->name
, sizeof (anchor
->path
));
302 if ((dup
= RB_INSERT(pf_anchor_global
, &pf_anchors
, anchor
)) !=
304 printf("pf_find_or_create_ruleset: RB_INSERT1 "
305 "'%s' '%s' collides with '%s' '%s'\n",
306 anchor
->path
, anchor
->name
, dup
->path
, dup
->name
);
311 if (parent
!= NULL
) {
312 anchor
->parent
= parent
;
313 if ((dup
= RB_INSERT(pf_anchor_node
, &parent
->children
,
315 printf("pf_find_or_create_ruleset: "
316 "RB_INSERT2 '%s' '%s' collides with "
317 "'%s' '%s'\n", anchor
->path
, anchor
->name
,
318 dup
->path
, dup
->name
);
319 RB_REMOVE(pf_anchor_global
, &pf_anchors
,
326 pf_init_ruleset(&anchor
->ruleset
);
327 anchor
->ruleset
.anchor
= anchor
;
334 if(strncmp("com.apple.nlc", anchor
->name
,
335 sizeof("com.apple.nlc")) == 0)
336 is_nlc_enabled_glb
= TRUE
;
340 return (anchor
? &anchor
->ruleset
: 0);
344 pf_remove_if_empty_ruleset(struct pf_ruleset
*ruleset
)
346 struct pf_anchor
*parent
;
349 while (ruleset
!= NULL
) {
350 if (ruleset
== &pf_main_ruleset
|| ruleset
->anchor
== NULL
||
351 !RB_EMPTY(&ruleset
->anchor
->children
) ||
352 ruleset
->anchor
->refcnt
> 0 || ruleset
->tables
> 0 ||
355 for (i
= 0; i
< PF_RULESET_MAX
; ++i
)
356 if (!TAILQ_EMPTY(ruleset
->rules
[i
].active
.ptr
) ||
357 !TAILQ_EMPTY(ruleset
->rules
[i
].inactive
.ptr
) ||
358 ruleset
->rules
[i
].inactive
.open
)
360 RB_REMOVE(pf_anchor_global
, &pf_anchors
, ruleset
->anchor
);
362 if(strncmp("com.apple.nlc", ruleset
->anchor
->name
,
363 sizeof("com.apple.nlc")) == 0) {
364 struct dummynet_event dn_event
;
365 bzero(&dn_event
, sizeof(dn_event
));
366 dn_event
.dn_event_code
= DUMMYNET_NLC_DISABLED
;
367 dummynet_event_enqueue_nwk_wq_entry(&dn_event
);
368 is_nlc_enabled_glb
= FALSE
;
371 if ((parent
= ruleset
->anchor
->parent
) != NULL
)
372 RB_REMOVE(pf_anchor_node
, &parent
->children
,
374 rs_free(ruleset
->anchor
);
377 ruleset
= &parent
->ruleset
;
382 pf_anchor_setup(struct pf_rule
*r
, const struct pf_ruleset
*s
,
386 struct pf_ruleset
*ruleset
;
389 r
->anchor_relative
= 0;
390 r
->anchor_wildcard
= 0;
393 path
= (char *)rs_malloc(MAXPATHLEN
);
394 bzero(path
, MAXPATHLEN
);
396 strlcpy(path
, name
+ 1, MAXPATHLEN
);
399 r
->anchor_relative
= 1;
400 if (s
->anchor
== NULL
|| !s
->anchor
->path
[0])
403 strlcpy(path
, s
->anchor
->path
, MAXPATHLEN
);
404 while (name
[0] == '.' && name
[1] == '.' && name
[2] == '/') {
406 printf("pf_anchor_setup: .. beyond root\n");
410 if ((p
= strrchr(path
, '/')) != NULL
)
414 r
->anchor_relative
++;
418 strlcat(path
, "/", MAXPATHLEN
);
419 strlcat(path
, name
, MAXPATHLEN
);
421 if ((p
= strrchr(path
, '/')) != NULL
&& strcmp(p
, "/*") == 0) {
422 r
->anchor_wildcard
= 1;
425 ruleset
= pf_find_or_create_ruleset(path
);
427 if (ruleset
== NULL
|| ruleset
->anchor
== NULL
) {
428 printf("pf_anchor_setup: ruleset\n");
431 r
->anchor
= ruleset
->anchor
;
437 pf_anchor_copyout(const struct pf_ruleset
*rs
, const struct pf_rule
*r
,
438 struct pfioc_rule
*pr
)
440 pr
->anchor_call
[0] = 0;
441 if (r
->anchor
== NULL
)
443 if (!r
->anchor_relative
) {
444 strlcpy(pr
->anchor_call
, "/", sizeof (pr
->anchor_call
));
445 strlcat(pr
->anchor_call
, r
->anchor
->path
,
446 sizeof (pr
->anchor_call
));
451 a
= (char *)rs_malloc(MAXPATHLEN
);
452 bzero(a
, MAXPATHLEN
);
453 if (rs
->anchor
== NULL
)
456 strlcpy(a
, rs
->anchor
->path
, MAXPATHLEN
);
457 for (i
= 1; i
< r
->anchor_relative
; ++i
) {
458 if ((p
= strrchr(a
, '/')) == NULL
)
461 strlcat(pr
->anchor_call
, "../",
462 sizeof (pr
->anchor_call
));
464 if (strncmp(a
, r
->anchor
->path
, strlen(a
))) {
465 printf("pf_anchor_copyout: '%s' '%s'\n", a
,
470 if (strlen(r
->anchor
->path
) > strlen(a
))
471 strlcat(pr
->anchor_call
, r
->anchor
->path
+ (a
[0] ?
472 strlen(a
) + 1 : 0), sizeof (pr
->anchor_call
));
475 if (r
->anchor_wildcard
)
476 strlcat(pr
->anchor_call
, pr
->anchor_call
[0] ? "/*" : "*",
477 sizeof (pr
->anchor_call
));
482 pf_anchor_remove(struct pf_rule
*r
)
484 if (r
->anchor
== NULL
)
486 if (r
->anchor
->refcnt
<= 0) {
487 printf("pf_anchor_remove: broken refcount\n");
491 if (!--r
->anchor
->refcnt
)
492 pf_remove_if_empty_ruleset(&r
->anchor
->ruleset
);