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