]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/pf_ruleset.c
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / net / pf_ruleset.c
1 /*
2 * Copyright (c) 2007-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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 $ */
31
32 /*
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.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
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.
48 *
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.
61 *
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.
65 *
66 */
67
68 #include <sys/param.h>
69 #include <sys/socket.h>
70 #ifdef KERNEL
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <libkern/libkern.h>
74 #endif /* KERNEL */
75 #include <sys/mbuf.h>
76
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>
82
83 #include <net/if.h>
84 #include <net/pfvar.h>
85
86 #include <netinet/ip6.h>
87
88
89 #ifdef KERNEL
90 #define DPFPRINTF(format, x ...) \
91 if (pf_status.debug >= PF_DEBUG_NOISY) \
92 printf(format, ##x)
93 #define rs_malloc(x) _MALLOC(x, M_TEMP, M_WAITOK)
94 #define rs_free(x) _FREE(x, M_TEMP)
95
96 #else
97 /* Userland equivalents so we can lend code to pfctl et al. */
98
99 #include <arpa/inet.h>
100 #include <errno.h>
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include <string.h>
104 #define rs_malloc(x) malloc(x)
105 #define rs_free(x) free(x)
106
107 #ifdef PFDEBUG
108 #include <sys/stdarg.h>
109 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
110 #else
111 #define DPFPRINTF(format, x...) ((void)0)
112 #endif /* PFDEBUG */
113 #endif /* KERNEL */
114
115
116 struct pf_anchor_global pf_anchors;
117 struct pf_anchor pf_main_anchor;
118
119 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
120
121 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
122 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
123
124 static __inline int
125 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
126 {
127 int c = strcmp(a->path, b->path);
128
129 return c ? (c < 0 ? -1 : 1) : 0;
130 }
131
132 int
133 pf_get_ruleset_number(u_int8_t action)
134 {
135 switch (action) {
136 case PF_SCRUB:
137 case PF_NOSCRUB:
138 return PF_RULESET_SCRUB;
139 case PF_PASS:
140 case PF_DROP:
141 return PF_RULESET_FILTER;
142 case PF_NAT:
143 case PF_NONAT:
144 return PF_RULESET_NAT;
145 case PF_BINAT:
146 case PF_NOBINAT:
147 return PF_RULESET_BINAT;
148 case PF_RDR:
149 case PF_NORDR:
150 case PF_NAT64:
151 case PF_NONAT64:
152 return PF_RULESET_RDR;
153 #if DUMMYNET
154 case PF_DUMMYNET:
155 case PF_NODUMMYNET:
156 return PF_RULESET_DUMMYNET;
157 #endif /* DUMMYNET */
158 default:
159 return PF_RULESET_MAX;
160 }
161 }
162
163 void
164 pf_init_ruleset(struct pf_ruleset *ruleset)
165 {
166 int i;
167
168 memset(ruleset, 0, sizeof(struct pf_ruleset));
169 for (i = 0; i < PF_RULESET_MAX; i++) {
170 TAILQ_INIT(&ruleset->rules[i].queues[0]);
171 TAILQ_INIT(&ruleset->rules[i].queues[1]);
172 ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
173 ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
174 }
175 }
176
177 struct pf_anchor *
178 pf_find_anchor(const char *path)
179 {
180 struct pf_anchor *key, *found;
181
182 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
183 memset(key, 0, sizeof(*key));
184 strlcpy(key->path, path, sizeof(key->path));
185 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
186 rs_free(key);
187 return found;
188 }
189
190 struct pf_ruleset *
191 pf_find_ruleset(const char *path)
192 {
193 struct pf_anchor *anchor;
194
195 while (*path == '/') {
196 path++;
197 }
198 if (!*path) {
199 return &pf_main_ruleset;
200 }
201 anchor = pf_find_anchor(path);
202 if (anchor == NULL) {
203 return NULL;
204 } else {
205 return &anchor->ruleset;
206 }
207 }
208
209 struct pf_ruleset *
210 pf_find_ruleset_with_owner(const char *path, const char *owner, int is_anchor,
211 int *error)
212 {
213 struct pf_anchor *anchor;
214
215 while (*path == '/') {
216 path++;
217 }
218 if (!*path) {
219 return &pf_main_ruleset;
220 }
221 anchor = pf_find_anchor(path);
222 if (anchor == NULL) {
223 *error = EINVAL;
224 return NULL;
225 } else {
226 if ((owner && (!strcmp(owner, anchor->owner)))
227 || (is_anchor && !strcmp(anchor->owner, ""))) {
228 return &anchor->ruleset;
229 }
230 *error = EPERM;
231 return NULL;
232 }
233 }
234
235 struct pf_ruleset *
236 pf_find_or_create_ruleset(const char *path)
237 {
238 char *p, *q = NULL, *r;
239 struct pf_ruleset *ruleset;
240 struct pf_anchor *anchor = 0, *dup, *parent = NULL;
241
242 if (path[0] == 0) {
243 return &pf_main_ruleset;
244 }
245 while (*path == '/') {
246 path++;
247 }
248 ruleset = pf_find_ruleset(path);
249 if (ruleset != NULL) {
250 return ruleset;
251 }
252 p = (char *)rs_malloc(MAXPATHLEN);
253 bzero(p, MAXPATHLEN);
254 strlcpy(p, path, MAXPATHLEN);
255 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
256 *q = 0;
257 if ((ruleset = pf_find_ruleset(p)) != NULL) {
258 parent = ruleset->anchor;
259 break;
260 }
261 }
262 if (q == NULL) {
263 q = p;
264 } else {
265 q++;
266 }
267 strlcpy(p, path, MAXPATHLEN);
268 if (!*q) {
269 rs_free(p);
270 return NULL;
271 }
272 while ((r = strchr(q, '/')) != NULL || *q) {
273 if (r != NULL) {
274 *r = 0;
275 }
276 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
277 (parent != NULL && strlen(parent->path) >=
278 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
279 rs_free(p);
280 return NULL;
281 }
282 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
283 if (anchor == NULL) {
284 rs_free(p);
285 return NULL;
286 }
287 memset(anchor, 0, sizeof(*anchor));
288 RB_INIT(&anchor->children);
289 strlcpy(anchor->name, q, sizeof(anchor->name));
290 if (parent != NULL) {
291 strlcpy(anchor->path, parent->path,
292 sizeof(anchor->path));
293 strlcat(anchor->path, "/", sizeof(anchor->path));
294 }
295 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
296 if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
297 NULL) {
298 printf("pf_find_or_create_ruleset: RB_INSERT1 "
299 "'%s' '%s' collides with '%s' '%s'\n",
300 anchor->path, anchor->name, dup->path, dup->name);
301 rs_free(anchor);
302 rs_free(p);
303 return NULL;
304 }
305 if (parent != NULL) {
306 anchor->parent = parent;
307 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
308 anchor)) != NULL) {
309 printf("pf_find_or_create_ruleset: "
310 "RB_INSERT2 '%s' '%s' collides with "
311 "'%s' '%s'\n", anchor->path, anchor->name,
312 dup->path, dup->name);
313 RB_REMOVE(pf_anchor_global, &pf_anchors,
314 anchor);
315 rs_free(anchor);
316 rs_free(p);
317 return NULL;
318 }
319 }
320 pf_init_ruleset(&anchor->ruleset);
321 anchor->ruleset.anchor = anchor;
322 parent = anchor;
323 if (r != NULL) {
324 q = r + 1;
325 } else {
326 *q = 0;
327 }
328 #if DUMMYNET
329 if (strncmp("com.apple.nlc", anchor->name,
330 sizeof("com.apple.nlc")) == 0) {
331 is_nlc_enabled_glb = TRUE;
332 }
333 #endif
334 }
335 rs_free(p);
336 return anchor ? &anchor->ruleset : 0;
337 }
338
339 void
340 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
341 {
342 struct pf_anchor *parent;
343 int i;
344
345 while (ruleset != NULL) {
346 if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
347 !RB_EMPTY(&ruleset->anchor->children) ||
348 ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
349 ruleset->topen) {
350 return;
351 }
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) {
356 return;
357 }
358 }
359 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
360 #if DUMMYNET
361 if (strncmp("com.apple.nlc", ruleset->anchor->name,
362 sizeof("com.apple.nlc")) == 0) {
363 struct dummynet_event dn_event;
364 bzero(&dn_event, sizeof(dn_event));
365 dn_event.dn_event_code = DUMMYNET_NLC_DISABLED;
366 dummynet_event_enqueue_nwk_wq_entry(&dn_event);
367 is_nlc_enabled_glb = FALSE;
368 }
369 #endif
370 if ((parent = ruleset->anchor->parent) != NULL) {
371 RB_REMOVE(pf_anchor_node, &parent->children,
372 ruleset->anchor);
373 }
374 rs_free(ruleset->anchor);
375 if (parent == NULL) {
376 return;
377 }
378 ruleset = &parent->ruleset;
379 }
380 }
381
382 int
383 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
384 const char *name)
385 {
386 char *p, *path;
387 struct pf_ruleset *ruleset;
388
389 r->anchor = NULL;
390 r->anchor_relative = 0;
391 r->anchor_wildcard = 0;
392 if (!name[0]) {
393 return 0;
394 }
395 path = (char *)rs_malloc(MAXPATHLEN);
396 bzero(path, MAXPATHLEN);
397 if (name[0] == '/') {
398 strlcpy(path, name + 1, MAXPATHLEN);
399 } else {
400 /* relative path */
401 r->anchor_relative = 1;
402 if (s->anchor == NULL || !s->anchor->path[0]) {
403 path[0] = 0;
404 } else {
405 strlcpy(path, s->anchor->path, MAXPATHLEN);
406 }
407 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
408 if (!path[0]) {
409 printf("pf_anchor_setup: .. beyond root\n");
410 rs_free(path);
411 return 1;
412 }
413 if ((p = strrchr(path, '/')) != NULL) {
414 *p = 0;
415 } else {
416 path[0] = 0;
417 }
418 r->anchor_relative++;
419 name += 3;
420 }
421 if (path[0]) {
422 strlcat(path, "/", MAXPATHLEN);
423 }
424 strlcat(path, name, MAXPATHLEN);
425 }
426 if ((p = strrchr(path, '/')) != NULL && strcmp(p, "/*") == 0) {
427 r->anchor_wildcard = 1;
428 *p = 0;
429 }
430 ruleset = pf_find_or_create_ruleset(path);
431 rs_free(path);
432 if (ruleset == NULL || ruleset->anchor == NULL) {
433 printf("pf_anchor_setup: ruleset\n");
434 return 1;
435 }
436 r->anchor = ruleset->anchor;
437 r->anchor->refcnt++;
438 return 0;
439 }
440
441 int
442 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
443 struct pfioc_rule *pr)
444 {
445 pr->anchor_call[0] = 0;
446 if (r->anchor == NULL) {
447 return 0;
448 }
449 if (!r->anchor_relative) {
450 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
451 strlcat(pr->anchor_call, r->anchor->path,
452 sizeof(pr->anchor_call));
453 } else {
454 char *a, *p;
455 int i;
456
457 a = (char *)rs_malloc(MAXPATHLEN);
458 bzero(a, MAXPATHLEN);
459 if (rs->anchor == NULL) {
460 a[0] = 0;
461 } else {
462 strlcpy(a, rs->anchor->path, MAXPATHLEN);
463 }
464 for (i = 1; i < r->anchor_relative; ++i) {
465 if ((p = strrchr(a, '/')) == NULL) {
466 p = a;
467 }
468 *p = 0;
469 strlcat(pr->anchor_call, "../",
470 sizeof(pr->anchor_call));
471 }
472 if (strncmp(a, r->anchor->path, strlen(a))) {
473 printf("pf_anchor_copyout: '%s' '%s'\n", a,
474 r->anchor->path);
475 rs_free(a);
476 return 1;
477 }
478 if (strlen(r->anchor->path) > strlen(a)) {
479 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
480 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
481 }
482 rs_free(a);
483 }
484 if (r->anchor_wildcard) {
485 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
486 sizeof(pr->anchor_call));
487 }
488 return 0;
489 }
490
491 void
492 pf_anchor_remove(struct pf_rule *r)
493 {
494 if (r->anchor == NULL) {
495 return;
496 }
497 if (r->anchor->refcnt <= 0) {
498 printf("pf_anchor_remove: broken refcount\n");
499 r->anchor = NULL;
500 return;
501 }
502 if (!--r->anchor->refcnt) {
503 pf_remove_if_empty_ruleset(&r->anchor->ruleset);
504 }
505 r->anchor = NULL;
506 }