]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/pf_ruleset.c
xnu-6153.61.1.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
86#if INET6
87#include <netinet/ip6.h>
88#endif /* INET6 */
89
90
91#ifdef KERNEL
0a7de745
A
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
b0d623f7
A
98
99static char *
100_strrchr(const char *c, int ch)
101{
102 char *p = (char *)(size_t)c, *save;
103
0a7de745
A
104 for (save = NULL;; ++p) {
105 if (*p == ch) {
b0d623f7 106 save = (char *)p;
0a7de745
A
107 }
108 if (*p == '\0') {
109 return save;
110 }
b0d623f7
A
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>
0a7de745
A
123#define rs_malloc(x) malloc(x)
124#define rs_free(x) free(x)
b0d623f7
A
125
126#ifdef PFDEBUG
127#include <sys/stdarg.h>
0a7de745 128#define DPFPRINTF(format, x...) fprintf(stderr, format, ##x)
b0d623f7 129#else
0a7de745 130#define DPFPRINTF(format, x...) ((void)0)
b0d623f7
A
131#endif /* PFDEBUG */
132#endif /* KERNEL */
133
134
0a7de745
A
135struct pf_anchor_global pf_anchors;
136struct pf_anchor pf_main_anchor;
b0d623f7
A
137
138static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
139
140RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
141RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
142
143static __inline int
144pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
145{
146 int c = strcmp(a->path, b->path);
147
0a7de745 148 return c ? (c < 0 ? -1 : 1) : 0;
b0d623f7
A
149}
150
151int
152pf_get_ruleset_number(u_int8_t action)
153{
154 switch (action) {
155 case PF_SCRUB:
156 case PF_NOSCRUB:
0a7de745 157 return PF_RULESET_SCRUB;
b0d623f7
A
158 case PF_PASS:
159 case PF_DROP:
0a7de745 160 return PF_RULESET_FILTER;
b0d623f7
A
161 case PF_NAT:
162 case PF_NONAT:
0a7de745 163 return PF_RULESET_NAT;
b0d623f7
A
164 case PF_BINAT:
165 case PF_NOBINAT:
0a7de745 166 return PF_RULESET_BINAT;
b0d623f7
A
167 case PF_RDR:
168 case PF_NORDR:
3e170ce0
A
169 case PF_NAT64:
170 case PF_NONAT64:
0a7de745 171 return PF_RULESET_RDR;
316670eb
A
172#if DUMMYNET
173 case PF_DUMMYNET:
174 case PF_NODUMMYNET:
0a7de745 175 return PF_RULESET_DUMMYNET;
316670eb 176#endif /* DUMMYNET */
b0d623f7 177 default:
0a7de745 178 return PF_RULESET_MAX;
b0d623f7
A
179 }
180}
181
182void
183pf_init_ruleset(struct pf_ruleset *ruleset)
184{
0a7de745 185 int i;
b0d623f7 186
0a7de745 187 memset(ruleset, 0, sizeof(struct pf_ruleset));
b0d623f7
A
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
196struct pf_anchor *
197pf_find_anchor(const char *path)
198{
0a7de745 199 struct pf_anchor *key, *found;
b0d623f7 200
0a7de745
A
201 key = (struct pf_anchor *)rs_malloc(sizeof(*key));
202 memset(key, 0, sizeof(*key));
203 strlcpy(key->path, path, sizeof(key->path));
b0d623f7
A
204 found = RB_FIND(pf_anchor_global, &pf_anchors, key);
205 rs_free(key);
0a7de745 206 return found;
b0d623f7
A
207}
208
209struct pf_ruleset *
210pf_find_ruleset(const char *path)
211{
0a7de745 212 struct pf_anchor *anchor;
b0d623f7 213
0a7de745 214 while (*path == '/') {
b0d623f7 215 path++;
0a7de745
A
216 }
217 if (!*path) {
218 return &pf_main_ruleset;
219 }
b0d623f7 220 anchor = pf_find_anchor(path);
0a7de745
A
221 if (anchor == NULL) {
222 return NULL;
223 } else {
224 return &anchor->ruleset;
225 }
b0d623f7
A
226}
227
316670eb
A
228struct pf_ruleset *
229pf_find_ruleset_with_owner(const char *path, const char *owner, int is_anchor,
230 int *error)
231{
0a7de745 232 struct pf_anchor *anchor;
316670eb 233
0a7de745 234 while (*path == '/') {
316670eb 235 path++;
0a7de745
A
236 }
237 if (!*path) {
238 return &pf_main_ruleset;
239 }
316670eb
A
240 anchor = pf_find_anchor(path);
241 if (anchor == NULL) {
242 *error = EINVAL;
0a7de745 243 return NULL;
316670eb 244 } else {
3e170ce0 245 if ((owner && (!strcmp(owner, anchor->owner)))
0a7de745
A
246 || (is_anchor && !strcmp(anchor->owner, ""))) {
247 return &anchor->ruleset;
248 }
316670eb
A
249 *error = EPERM;
250 return NULL;
251 }
252}
253
b0d623f7
A
254struct pf_ruleset *
255pf_find_or_create_ruleset(const char *path)
256{
0a7de745
A
257 char *p, *q = NULL, *r;
258 struct pf_ruleset *ruleset;
259 struct pf_anchor *anchor = 0, *dup, *parent = NULL;
b0d623f7 260
0a7de745
A
261 if (path[0] == 0) {
262 return &pf_main_ruleset;
263 }
264 while (*path == '/') {
b0d623f7 265 path++;
0a7de745 266 }
b0d623f7 267 ruleset = pf_find_ruleset(path);
0a7de745
A
268 if (ruleset != NULL) {
269 return ruleset;
270 }
b0d623f7
A
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 }
0a7de745 281 if (q == NULL) {
b0d623f7 282 q = p;
0a7de745 283 } else {
b0d623f7 284 q++;
0a7de745 285 }
b0d623f7
A
286 strlcpy(p, path, MAXPATHLEN);
287 if (!*q) {
288 rs_free(p);
0a7de745 289 return NULL;
b0d623f7
A
290 }
291 while ((r = strchr(q, '/')) != NULL || *q) {
0a7de745 292 if (r != NULL) {
b0d623f7 293 *r = 0;
0a7de745 294 }
b0d623f7
A
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);
0a7de745 299 return NULL;
b0d623f7 300 }
0a7de745 301 anchor = (struct pf_anchor *)rs_malloc(sizeof(*anchor));
b0d623f7
A
302 if (anchor == NULL) {
303 rs_free(p);
0a7de745 304 return NULL;
b0d623f7 305 }
0a7de745 306 memset(anchor, 0, sizeof(*anchor));
b0d623f7 307 RB_INIT(&anchor->children);
0a7de745 308 strlcpy(anchor->name, q, sizeof(anchor->name));
b0d623f7
A
309 if (parent != NULL) {
310 strlcpy(anchor->path, parent->path,
0a7de745
A
311 sizeof(anchor->path));
312 strlcat(anchor->path, "/", sizeof(anchor->path));
b0d623f7 313 }
0a7de745 314 strlcat(anchor->path, anchor->name, sizeof(anchor->path));
b0d623f7
A
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);
0a7de745 322 return NULL;
b0d623f7
A
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);
0a7de745 336 return NULL;
b0d623f7
A
337 }
338 }
339 pf_init_ruleset(&anchor->ruleset);
340 anchor->ruleset.anchor = anchor;
341 parent = anchor;
0a7de745 342 if (r != NULL) {
b0d623f7 343 q = r + 1;
0a7de745 344 } else {
b0d623f7 345 *q = 0;
0a7de745 346 }
5ba3f43e 347#if DUMMYNET
0a7de745
A
348 if (strncmp("com.apple.nlc", anchor->name,
349 sizeof("com.apple.nlc")) == 0) {
5ba3f43e 350 is_nlc_enabled_glb = TRUE;
0a7de745 351 }
5ba3f43e 352#endif
b0d623f7
A
353 }
354 rs_free(p);
0a7de745 355 return anchor ? &anchor->ruleset : 0;
b0d623f7
A
356}
357
358void
359pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
360{
0a7de745
A
361 struct pf_anchor *parent;
362 int i;
b0d623f7
A
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 ||
0a7de745 368 ruleset->topen) {
b0d623f7 369 return;
0a7de745
A
370 }
371 for (i = 0; i < PF_RULESET_MAX; ++i) {
b0d623f7
A
372 if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
373 !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
0a7de745 374 ruleset->rules[i].inactive.open) {
b0d623f7 375 return;
0a7de745
A
376 }
377 }
b0d623f7 378 RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
5ba3f43e 379#if DUMMYNET
0a7de745 380 if (strncmp("com.apple.nlc", ruleset->anchor->name,
5ba3f43e
A
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
0a7de745 389 if ((parent = ruleset->anchor->parent) != NULL) {
b0d623f7
A
390 RB_REMOVE(pf_anchor_node, &parent->children,
391 ruleset->anchor);
0a7de745 392 }
b0d623f7 393 rs_free(ruleset->anchor);
0a7de745 394 if (parent == NULL) {
b0d623f7 395 return;
0a7de745 396 }
b0d623f7
A
397 ruleset = &parent->ruleset;
398 }
399}
400
401int
402pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
403 const char *name)
404{
0a7de745
A
405 char *p, *path;
406 struct pf_ruleset *ruleset;
b0d623f7
A
407
408 r->anchor = NULL;
409 r->anchor_relative = 0;
410 r->anchor_wildcard = 0;
0a7de745
A
411 if (!name[0]) {
412 return 0;
413 }
b0d623f7
A
414 path = (char *)rs_malloc(MAXPATHLEN);
415 bzero(path, MAXPATHLEN);
0a7de745 416 if (name[0] == '/') {
b0d623f7 417 strlcpy(path, name + 1, MAXPATHLEN);
0a7de745 418 } else {
b0d623f7
A
419 /* relative path */
420 r->anchor_relative = 1;
0a7de745 421 if (s->anchor == NULL || !s->anchor->path[0]) {
b0d623f7 422 path[0] = 0;
0a7de745 423 } else {
b0d623f7 424 strlcpy(path, s->anchor->path, MAXPATHLEN);
0a7de745 425 }
b0d623f7
A
426 while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
427 if (!path[0]) {
428 printf("pf_anchor_setup: .. beyond root\n");
429 rs_free(path);
0a7de745 430 return 1;
b0d623f7 431 }
0a7de745 432 if ((p = strrchr(path, '/')) != NULL) {
b0d623f7 433 *p = 0;
0a7de745 434 } else {
b0d623f7 435 path[0] = 0;
0a7de745 436 }
b0d623f7
A
437 r->anchor_relative++;
438 name += 3;
439 }
0a7de745 440 if (path[0]) {
b0d623f7 441 strlcat(path, "/", MAXPATHLEN);
0a7de745 442 }
b0d623f7
A
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");
0a7de745 453 return 1;
b0d623f7
A
454 }
455 r->anchor = ruleset->anchor;
456 r->anchor->refcnt++;
0a7de745 457 return 0;
b0d623f7
A
458}
459
460int
461pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
462 struct pfioc_rule *pr)
463{
464 pr->anchor_call[0] = 0;
0a7de745
A
465 if (r->anchor == NULL) {
466 return 0;
467 }
b0d623f7 468 if (!r->anchor_relative) {
0a7de745 469 strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
b0d623f7 470 strlcat(pr->anchor_call, r->anchor->path,
0a7de745 471 sizeof(pr->anchor_call));
b0d623f7 472 } else {
0a7de745
A
473 char *a, *p;
474 int i;
b0d623f7
A
475
476 a = (char *)rs_malloc(MAXPATHLEN);
477 bzero(a, MAXPATHLEN);
0a7de745 478 if (rs->anchor == NULL) {
b0d623f7 479 a[0] = 0;
0a7de745 480 } else {
b0d623f7 481 strlcpy(a, rs->anchor->path, MAXPATHLEN);
0a7de745 482 }
b0d623f7 483 for (i = 1; i < r->anchor_relative; ++i) {
0a7de745 484 if ((p = strrchr(a, '/')) == NULL) {
b0d623f7 485 p = a;
0a7de745 486 }
b0d623f7
A
487 *p = 0;
488 strlcat(pr->anchor_call, "../",
0a7de745 489 sizeof(pr->anchor_call));
b0d623f7
A
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);
0a7de745 495 return 1;
b0d623f7 496 }
0a7de745 497 if (strlen(r->anchor->path) > strlen(a)) {
b0d623f7 498 strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
0a7de745
A
499 strlen(a) + 1 : 0), sizeof(pr->anchor_call));
500 }
b0d623f7
A
501 rs_free(a);
502 }
0a7de745 503 if (r->anchor_wildcard) {
b0d623f7 504 strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
0a7de745
A
505 sizeof(pr->anchor_call));
506 }
507 return 0;
b0d623f7
A
508}
509
510void
511pf_anchor_remove(struct pf_rule *r)
512{
0a7de745 513 if (r->anchor == NULL) {
b0d623f7 514 return;
0a7de745 515 }
b0d623f7
A
516 if (r->anchor->refcnt <= 0) {
517 printf("pf_anchor_remove: broken refcount\n");
518 r->anchor = NULL;
519 return;
520 }
0a7de745 521 if (!--r->anchor->refcnt) {
b0d623f7 522 pf_remove_if_empty_ruleset(&r->anchor->ruleset);
0a7de745 523 }
b0d623f7
A
524 r->anchor = NULL;
525}