]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/pf_ruleset.c
xnu-4903.270.47.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 #if INET6
87 #include <netinet/ip6.h>
88 #endif /* INET6 */
89
90
91 #ifdef KERNEL
92 #define DPFPRINTF(format, x ...) \
93 if (pf_status.debug >= PF_DEBUG_NOISY) \
94 printf(format, ##x)
95 #define rs_malloc(x) _MALLOC(x, M_TEMP, M_WAITOK)
96 #define rs_free(x) _FREE(x, M_TEMP)
97 #define strrchr _strrchr
98
99 static char *
100 _strrchr(const char *c, int ch)
101 {
102 char *p = (char *)(size_t)c, *save;
103
104 for (save = NULL;; ++p) {
105 if (*p == ch) {
106 save = (char *)p;
107 }
108 if (*p == '\0') {
109 return save;
110 }
111 }
112 /* NOTREACHED */
113 }
114
115 #else
116 /* Userland equivalents so we can lend code to pfctl et al. */
117
118 #include <arpa/inet.h>
119 #include <errno.h>
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <string.h>
123 #define rs_malloc(x) malloc(x)
124 #define rs_free(x) free(x)
125
126 #ifdef PFDEBUG
127 #include <sys/stdarg.h>
128 #define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
129 #else
130 #define DPFPRINTF(format, x...) ((void)0)
131 #endif /* PFDEBUG */
132 #endif /* KERNEL */
133
134
135 struct pf_anchor_global pf_anchors;
136 struct pf_anchor pf_main_anchor;
137
138 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
139
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);
142
143 static __inline int
144 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
145 {
146 int c = strcmp(a->path, b->path);
147
148 return c ? (c < 0 ? -1 : 1) : 0;
149 }
150
151 int
152 pf_get_ruleset_number(u_int8_t action)
153 {
154 switch (action) {
155 case PF_SCRUB:
156 case PF_NOSCRUB:
157 return PF_RULESET_SCRUB;
158 case PF_PASS:
159 case PF_DROP:
160 return PF_RULESET_FILTER;
161 case PF_NAT:
162 case PF_NONAT:
163 return PF_RULESET_NAT;
164 case PF_BINAT:
165 case PF_NOBINAT:
166 return PF_RULESET_BINAT;
167 case PF_RDR:
168 case PF_NORDR:
169 case PF_NAT64:
170 case PF_NONAT64:
171 return PF_RULESET_RDR;
172 #if DUMMYNET
173 case PF_DUMMYNET:
174 case PF_NODUMMYNET:
175 return PF_RULESET_DUMMYNET;
176 #endif /* DUMMYNET */
177 default:
178 return PF_RULESET_MAX;
179 }
180 }
181
182 void
183 pf_init_ruleset(struct pf_ruleset *ruleset)
184 {
185 int i;
186
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];
193 }
194 }
195
196 struct pf_anchor *
197 pf_find_anchor(const char *path)
198 {
199 struct pf_anchor *key, *found;
200
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);
205 rs_free(key);
206 return found;
207 }
208
209 struct pf_ruleset *
210 pf_find_ruleset(const char *path)
211 {
212 struct pf_anchor *anchor;
213
214 while (*path == '/') {
215 path++;
216 }
217 if (!*path) {
218 return &pf_main_ruleset;
219 }
220 anchor = pf_find_anchor(path);
221 if (anchor == NULL) {
222 return NULL;
223 } else {
224 return &anchor->ruleset;
225 }
226 }
227
228 struct pf_ruleset *
229 pf_find_ruleset_with_owner(const char *path, const char *owner, int is_anchor,
230 int *error)
231 {
232 struct pf_anchor *anchor;
233
234 while (*path == '/') {
235 path++;
236 }
237 if (!*path) {
238 return &pf_main_ruleset;
239 }
240 anchor = pf_find_anchor(path);
241 if (anchor == NULL) {
242 *error = EINVAL;
243 return NULL;
244 } else {
245 if ((owner && (!strcmp(owner, anchor->owner)))
246 || (is_anchor && !strcmp(anchor->owner, ""))) {
247 return &anchor->ruleset;
248 }
249 *error = EPERM;
250 return NULL;
251 }
252 }
253
254 struct pf_ruleset *
255 pf_find_or_create_ruleset(const char *path)
256 {
257 char *p, *q = NULL, *r;
258 struct pf_ruleset *ruleset;
259 struct pf_anchor *anchor = 0, *dup, *parent = NULL;
260
261 if (path[0] == 0) {
262 return &pf_main_ruleset;
263 }
264 while (*path == '/') {
265 path++;
266 }
267 ruleset = pf_find_ruleset(path);
268 if (ruleset != NULL) {
269 return ruleset;
270 }
271 p = (char *)rs_malloc(MAXPATHLEN);
272 bzero(p, MAXPATHLEN);
273 strlcpy(p, path, MAXPATHLEN);
274 while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
275 *q = 0;
276 if ((ruleset = pf_find_ruleset(p)) != NULL) {
277 parent = ruleset->anchor;
278 break;
279 }
280 }
281 if (q == NULL) {
282 q = p;
283 } else {
284 q++;
285 }
286 strlcpy(p, path, MAXPATHLEN);
287 if (!*q) {
288 rs_free(p);
289 return NULL;
290 }
291 while ((r = strchr(q, '/')) != NULL || *q) {
292 if (r != NULL) {
293 *r = 0;
294 }
295 if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
296 (parent != NULL && strlen(parent->path) >=
297 MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
298 rs_free(p);
299 return NULL;
300 }
301 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
302 if (anchor == NULL) {
303 rs_free(p);
304 return NULL;
305 }
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));
313 }
314 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
315 if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
316 NULL) {
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);
320 rs_free(anchor);
321 rs_free(p);
322 return NULL;
323 }
324 if (parent != NULL) {
325 anchor->parent = parent;
326 if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
327 anchor)) != NULL) {
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,
333 anchor);
334 rs_free(anchor);
335 rs_free(p);
336 return NULL;
337 }
338 }
339 pf_init_ruleset(&anchor->ruleset);
340 anchor->ruleset.anchor = anchor;
341 parent = anchor;
342 if (r != NULL) {
343 q = r + 1;
344 } else {
345 *q = 0;
346 }
347 #if DUMMYNET
348 if (strncmp("com.apple.nlc", anchor->name,
349 sizeof("com.apple.nlc")) == 0) {
350 is_nlc_enabled_glb = TRUE;
351 }
352 #endif
353 }
354 rs_free(p);
355 return anchor ? &anchor->ruleset : 0;
356 }
357
358 void
359 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
360 {
361 struct pf_anchor *parent;
362 int i;
363
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 ||
368 ruleset->topen) {
369 return;
370 }
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) {
375 return;
376 }
377 }
378 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
379 #if DUMMYNET
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;
387 }
388 #endif
389 if ((parent = ruleset->anchor->parent) != NULL) {
390 RB_REMOVE(pf_anchor_node, &parent->children,
391 ruleset->anchor);
392 }
393 rs_free(ruleset->anchor);
394 if (parent == NULL) {
395 return;
396 }
397 ruleset = &parent->ruleset;
398 }
399 }
400
401 int
402 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
403 const char *name)
404 {
405 char *p, *path;
406 struct pf_ruleset *ruleset;
407
408 r->anchor = NULL;
409 r->anchor_relative = 0;
410 r->anchor_wildcard = 0;
411 if (!name[0]) {
412 return 0;
413 }
414 path = (char *)rs_malloc(MAXPATHLEN);
415 bzero(path, MAXPATHLEN);
416 if (name[0] == '/') {
417 strlcpy(path, name + 1, MAXPATHLEN);
418 } else {
419 /* relative path */
420 r->anchor_relative = 1;
421 if (s->anchor == NULL || !s->anchor->path[0]) {
422 path[0] = 0;
423 } else {
424 strlcpy(path, s->anchor->path, MAXPATHLEN);
425 }
426 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
427 if (!path[0]) {
428 printf("pf_anchor_setup: .. beyond root\n");
429 rs_free(path);
430 return 1;
431 }
432 if ((p = strrchr(path, '/')) != NULL) {
433 *p = 0;
434 } else {
435 path[0] = 0;
436 }
437 r->anchor_relative++;
438 name += 3;
439 }
440 if (path[0]) {
441 strlcat(path, "/", MAXPATHLEN);
442 }
443 strlcat(path, name, MAXPATHLEN);
444 }
445 if ((p = strrchr(path, '/')) != NULL && strcmp(p, "/*") == 0) {
446 r->anchor_wildcard = 1;
447 *p = 0;
448 }
449 ruleset = pf_find_or_create_ruleset(path);
450 rs_free(path);
451 if (ruleset == NULL || ruleset->anchor == NULL) {
452 printf("pf_anchor_setup: ruleset\n");
453 return 1;
454 }
455 r->anchor = ruleset->anchor;
456 r->anchor->refcnt++;
457 return 0;
458 }
459
460 int
461 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
462 struct pfioc_rule *pr)
463 {
464 pr->anchor_call[0] = 0;
465 if (r->anchor == NULL) {
466 return 0;
467 }
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));
472 } else {
473 char *a, *p;
474 int i;
475
476 a = (char *)rs_malloc(MAXPATHLEN);
477 bzero(a, MAXPATHLEN);
478 if (rs->anchor == NULL) {
479 a[0] = 0;
480 } else {
481 strlcpy(a, rs->anchor->path, MAXPATHLEN);
482 }
483 for (i = 1; i < r->anchor_relative; ++i) {
484 if ((p = strrchr(a, '/')) == NULL) {
485 p = a;
486 }
487 *p = 0;
488 strlcat(pr->anchor_call, "../",
489 sizeof(pr->anchor_call));
490 }
491 if (strncmp(a, r->anchor->path, strlen(a))) {
492 printf("pf_anchor_copyout: '%s' '%s'\n", a,
493 r->anchor->path);
494 rs_free(a);
495 return 1;
496 }
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));
500 }
501 rs_free(a);
502 }
503 if (r->anchor_wildcard) {
504 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
505 sizeof(pr->anchor_call));
506 }
507 return 0;
508 }
509
510 void
511 pf_anchor_remove(struct pf_rule *r)
512 {
513 if (r->anchor == NULL) {
514 return;
515 }
516 if (r->anchor->refcnt <= 0) {
517 printf("pf_anchor_remove: broken refcount\n");
518 r->anchor = NULL;
519 return;
520 }
521 if (!--r->anchor->refcnt) {
522 pf_remove_if_empty_ruleset(&r->anchor->ruleset);
523 }
524 r->anchor = NULL;
525 }