]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/pf_ruleset.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / net / pf_ruleset.c
CommitLineData
b0d623f7 1/*
39037602 2 * Copyright (c) 2007-2016 Apple Inc. All rights reserved.
b0d623f7
A
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
3e170ce0 35 * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
b0d623f7
A
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
5ba3f43e 77#include <netinet/ip_dummynet.h>
b0d623f7
A
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
b0d623f7 86#include <netinet/ip6.h>
b0d623f7
A
87
88
89#ifdef KERNEL
0a7de745
A
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)
b0d623f7
A
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>
0a7de745
A
104#define rs_malloc(x) malloc(x)
105#define rs_free(x) free(x)
b0d623f7
A
106
107#ifdef PFDEBUG
108#include <sys/stdarg.h>
0a7de745 109#define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
b0d623f7 110#else
0a7de745 111#define DPFPRINTF(format, x...) ((void)0)
b0d623f7
A
112#endif /* PFDEBUG */
113#endif /* KERNEL */
114
115
0a7de745
A
116struct pf_anchor_global pf_anchors;
117struct pf_anchor pf_main_anchor;
b0d623f7
A
118
119static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
120
121RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
122RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
123
124static __inline int
125pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
126{
127 int c = strcmp(a->path, b->path);
128
0a7de745 129 return c ? (c < 0 ? -1 : 1) : 0;
b0d623f7
A
130}
131
132int
133pf_get_ruleset_number(u_int8_t action)
134{
135 switch (action) {
136 case PF_SCRUB:
137 case PF_NOSCRUB:
0a7de745 138 return PF_RULESET_SCRUB;
b0d623f7
A
139 case PF_PASS:
140 case PF_DROP:
0a7de745 141 return PF_RULESET_FILTER;
b0d623f7
A
142 case PF_NAT:
143 case PF_NONAT:
0a7de745 144 return PF_RULESET_NAT;
b0d623f7
A
145 case PF_BINAT:
146 case PF_NOBINAT:
0a7de745 147 return PF_RULESET_BINAT;
b0d623f7
A
148 case PF_RDR:
149 case PF_NORDR:
3e170ce0
A
150 case PF_NAT64:
151 case PF_NONAT64:
0a7de745 152 return PF_RULESET_RDR;
316670eb
A
153#if DUMMYNET
154 case PF_DUMMYNET:
155 case PF_NODUMMYNET:
0a7de745 156 return PF_RULESET_DUMMYNET;
316670eb 157#endif /* DUMMYNET */
b0d623f7 158 default:
0a7de745 159 return PF_RULESET_MAX;
b0d623f7
A
160 }
161}
162
163void
164pf_init_ruleset(struct pf_ruleset *ruleset)
165{
0a7de745 166 int i;
b0d623f7 167
0a7de745 168 memset(ruleset, 0, sizeof(struct pf_ruleset));
b0d623f7
A
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
177struct pf_anchor *
178pf_find_anchor(const char *path)
179{
0a7de745 180 struct pf_anchor *key, *found;
b0d623f7 181
0a7de745
A
182 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
183 memset(key, 0, sizeof(*key));
184 strlcpy(key->path, path, sizeof(key->path));
b0d623f7
A
185 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
186 rs_free(key);
0a7de745 187 return found;
b0d623f7
A
188}
189
190struct pf_ruleset *
191pf_find_ruleset(const char *path)
192{
0a7de745 193 struct pf_anchor *anchor;
b0d623f7 194
0a7de745 195 while (*path == '/') {
b0d623f7 196 path++;
0a7de745
A
197 }
198 if (!*path) {
199 return &pf_main_ruleset;
200 }
b0d623f7 201 anchor = pf_find_anchor(path);
0a7de745
A
202 if (anchor == NULL) {
203 return NULL;
204 } else {
205 return &anchor->ruleset;
206 }
b0d623f7
A
207}
208
316670eb
A
209struct pf_ruleset *
210pf_find_ruleset_with_owner(const char *path, const char *owner, int is_anchor,
211 int *error)
212{
0a7de745 213 struct pf_anchor *anchor;
316670eb 214
0a7de745 215 while (*path == '/') {
316670eb 216 path++;
0a7de745
A
217 }
218 if (!*path) {
219 return &pf_main_ruleset;
220 }
316670eb
A
221 anchor = pf_find_anchor(path);
222 if (anchor == NULL) {
223 *error = EINVAL;
0a7de745 224 return NULL;
316670eb 225 } else {
3e170ce0 226 if ((owner && (!strcmp(owner, anchor->owner)))
0a7de745
A
227 || (is_anchor && !strcmp(anchor->owner, ""))) {
228 return &anchor->ruleset;
229 }
316670eb
A
230 *error = EPERM;
231 return NULL;
232 }
233}
234
b0d623f7
A
235struct pf_ruleset *
236pf_find_or_create_ruleset(const char *path)
237{
0a7de745
A
238 char *p, *q = NULL, *r;
239 struct pf_ruleset *ruleset;
240 struct pf_anchor *anchor = 0, *dup, *parent = NULL;
b0d623f7 241
0a7de745
A
242 if (path[0] == 0) {
243 return &pf_main_ruleset;
244 }
245 while (*path == '/') {
b0d623f7 246 path++;
0a7de745 247 }
b0d623f7 248 ruleset = pf_find_ruleset(path);
0a7de745
A
249 if (ruleset != NULL) {
250 return ruleset;
251 }
b0d623f7
A
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 }
0a7de745 262 if (q == NULL) {
b0d623f7 263 q = p;
0a7de745 264 } else {
b0d623f7 265 q++;
0a7de745 266 }
b0d623f7
A
267 strlcpy(p, path, MAXPATHLEN);
268 if (!*q) {
269 rs_free(p);
0a7de745 270 return NULL;
b0d623f7
A
271 }
272 while ((r = strchr(q, '/')) != NULL || *q) {
0a7de745 273 if (r != NULL) {
b0d623f7 274 *r = 0;
0a7de745 275 }
b0d623f7
A
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);
0a7de745 280 return NULL;
b0d623f7 281 }
0a7de745 282 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
b0d623f7
A
283 if (anchor == NULL) {
284 rs_free(p);
0a7de745 285 return NULL;
b0d623f7 286 }
0a7de745 287 memset(anchor, 0, sizeof(*anchor));
b0d623f7 288 RB_INIT(&anchor->children);
0a7de745 289 strlcpy(anchor->name, q, sizeof(anchor->name));
b0d623f7
A
290 if (parent != NULL) {
291 strlcpy(anchor->path, parent->path,
0a7de745
A
292 sizeof(anchor->path));
293 strlcat(anchor->path, "/", sizeof(anchor->path));
b0d623f7 294 }
0a7de745 295 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
b0d623f7
A
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);
0a7de745 303 return NULL;
b0d623f7
A
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);
0a7de745 317 return NULL;
b0d623f7
A
318 }
319 }
320 pf_init_ruleset(&anchor->ruleset);
321 anchor->ruleset.anchor = anchor;
322 parent = anchor;
0a7de745 323 if (r != NULL) {
b0d623f7 324 q = r + 1;
0a7de745 325 } else {
b0d623f7 326 *q = 0;
0a7de745 327 }
5ba3f43e 328#if DUMMYNET
0a7de745
A
329 if (strncmp("com.apple.nlc", anchor->name,
330 sizeof("com.apple.nlc")) == 0) {
5ba3f43e 331 is_nlc_enabled_glb = TRUE;
0a7de745 332 }
5ba3f43e 333#endif
b0d623f7
A
334 }
335 rs_free(p);
0a7de745 336 return anchor ? &anchor->ruleset : 0;
b0d623f7
A
337}
338
339void
340pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
341{
0a7de745
A
342 struct pf_anchor *parent;
343 int i;
b0d623f7
A
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 ||
0a7de745 349 ruleset->topen) {
b0d623f7 350 return;
0a7de745
A
351 }
352 for (i = 0; i < PF_RULESET_MAX; ++i) {
b0d623f7
A
353 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
354 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
0a7de745 355 ruleset->rules[i].inactive.open) {
b0d623f7 356 return;
0a7de745
A
357 }
358 }
b0d623f7 359 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
5ba3f43e 360#if DUMMYNET
0a7de745 361 if (strncmp("com.apple.nlc", ruleset->anchor->name,
5ba3f43e
A
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
0a7de745 370 if ((parent = ruleset->anchor->parent) != NULL) {
b0d623f7
A
371 RB_REMOVE(pf_anchor_node, &parent->children,
372 ruleset->anchor);
0a7de745 373 }
b0d623f7 374 rs_free(ruleset->anchor);
0a7de745 375 if (parent == NULL) {
b0d623f7 376 return;
0a7de745 377 }
b0d623f7
A
378 ruleset = &parent->ruleset;
379 }
380}
381
382int
383pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
384 const char *name)
385{
0a7de745
A
386 char *p, *path;
387 struct pf_ruleset *ruleset;
b0d623f7
A
388
389 r->anchor = NULL;
390 r->anchor_relative = 0;
391 r->anchor_wildcard = 0;
0a7de745
A
392 if (!name[0]) {
393 return 0;
394 }
b0d623f7
A
395 path = (char *)rs_malloc(MAXPATHLEN);
396 bzero(path, MAXPATHLEN);
0a7de745 397 if (name[0] == '/') {
b0d623f7 398 strlcpy(path, name + 1, MAXPATHLEN);
0a7de745 399 } else {
b0d623f7
A
400 /* relative path */
401 r->anchor_relative = 1;
0a7de745 402 if (s->anchor == NULL || !s->anchor->path[0]) {
b0d623f7 403 path[0] = 0;
0a7de745 404 } else {
b0d623f7 405 strlcpy(path, s->anchor->path, MAXPATHLEN);
0a7de745 406 }
b0d623f7
A
407 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
408 if (!path[0]) {
409 printf("pf_anchor_setup: .. beyond root\n");
410 rs_free(path);
0a7de745 411 return 1;
b0d623f7 412 }
0a7de745 413 if ((p = strrchr(path, '/')) != NULL) {
b0d623f7 414 *p = 0;
0a7de745 415 } else {
b0d623f7 416 path[0] = 0;
0a7de745 417 }
b0d623f7
A
418 r->anchor_relative++;
419 name += 3;
420 }
0a7de745 421 if (path[0]) {
b0d623f7 422 strlcat(path, "/", MAXPATHLEN);
0a7de745 423 }
b0d623f7
A
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");
0a7de745 434 return 1;
b0d623f7
A
435 }
436 r->anchor = ruleset->anchor;
437 r->anchor->refcnt++;
0a7de745 438 return 0;
b0d623f7
A
439}
440
441int
442pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
443 struct pfioc_rule *pr)
444{
445 pr->anchor_call[0] = 0;
0a7de745
A
446 if (r->anchor == NULL) {
447 return 0;
448 }
b0d623f7 449 if (!r->anchor_relative) {
0a7de745 450 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
b0d623f7 451 strlcat(pr->anchor_call, r->anchor->path,
0a7de745 452 sizeof(pr->anchor_call));
b0d623f7 453 } else {
0a7de745
A
454 char *a, *p;
455 int i;
b0d623f7
A
456
457 a = (char *)rs_malloc(MAXPATHLEN);
458 bzero(a, MAXPATHLEN);
0a7de745 459 if (rs->anchor == NULL) {
b0d623f7 460 a[0] = 0;
0a7de745 461 } else {
b0d623f7 462 strlcpy(a, rs->anchor->path, MAXPATHLEN);
0a7de745 463 }
b0d623f7 464 for (i = 1; i < r->anchor_relative; ++i) {
0a7de745 465 if ((p = strrchr(a, '/')) == NULL) {
b0d623f7 466 p = a;
0a7de745 467 }
b0d623f7
A
468 *p = 0;
469 strlcat(pr->anchor_call, "../",
0a7de745 470 sizeof(pr->anchor_call));
b0d623f7
A
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);
0a7de745 476 return 1;
b0d623f7 477 }
0a7de745 478 if (strlen(r->anchor->path) > strlen(a)) {
b0d623f7 479 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
0a7de745
A
480 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
481 }
b0d623f7
A
482 rs_free(a);
483 }
0a7de745 484 if (r->anchor_wildcard) {
b0d623f7 485 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
0a7de745
A
486 sizeof(pr->anchor_call));
487 }
488 return 0;
b0d623f7
A
489}
490
491void
492pf_anchor_remove(struct pf_rule *r)
493{
0a7de745 494 if (r->anchor == NULL) {
b0d623f7 495 return;
0a7de745 496 }
b0d623f7
A
497 if (r->anchor->refcnt <= 0) {
498 printf("pf_anchor_remove: broken refcount\n");
499 r->anchor = NULL;
500 return;
501 }
0a7de745 502 if (!--r->anchor->refcnt) {
b0d623f7 503 pf_remove_if_empty_ruleset(&r->anchor->ruleset);
0a7de745 504 }
b0d623f7
A
505 r->anchor = NULL;
506}