]>
Commit | Line | Data |
---|---|---|
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 | ||
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); | |
b0d623f7 A |
155 | case PF_PASS: |
156 | case PF_DROP: | |
157 | return (PF_RULESET_FILTER); | |
b0d623f7 A |
158 | case PF_NAT: |
159 | case PF_NONAT: | |
160 | return (PF_RULESET_NAT); | |
b0d623f7 A |
161 | case PF_BINAT: |
162 | case PF_NOBINAT: | |
163 | return (PF_RULESET_BINAT); | |
b0d623f7 A |
164 | case PF_RDR: |
165 | case PF_NORDR: | |
3e170ce0 A |
166 | case PF_NAT64: |
167 | case PF_NONAT64: | |
b0d623f7 | 168 | return (PF_RULESET_RDR); |
316670eb A |
169 | #if DUMMYNET |
170 | case PF_DUMMYNET: | |
171 | case PF_NODUMMYNET: | |
172 | return (PF_RULESET_DUMMYNET); | |
173 | #endif /* DUMMYNET */ | |
b0d623f7 A |
174 | default: |
175 | return (PF_RULESET_MAX); | |
b0d623f7 A |
176 | } |
177 | } | |
178 | ||
179 | void | |
180 | pf_init_ruleset(struct pf_ruleset *ruleset) | |
181 | { | |
182 | int i; | |
183 | ||
184 | memset(ruleset, 0, sizeof (struct pf_ruleset)); | |
185 | for (i = 0; i < PF_RULESET_MAX; i++) { | |
186 | TAILQ_INIT(&ruleset->rules[i].queues[0]); | |
187 | TAILQ_INIT(&ruleset->rules[i].queues[1]); | |
188 | ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0]; | |
189 | ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1]; | |
190 | } | |
191 | } | |
192 | ||
193 | struct pf_anchor * | |
194 | pf_find_anchor(const char *path) | |
195 | { | |
196 | struct pf_anchor *key, *found; | |
197 | ||
198 | key = (struct pf_anchor *)rs_malloc(sizeof (*key)); | |
199 | memset(key, 0, sizeof (*key)); | |
200 | strlcpy(key->path, path, sizeof (key->path)); | |
201 | found = RB_FIND(pf_anchor_global, &pf_anchors, key); | |
202 | rs_free(key); | |
203 | return (found); | |
204 | } | |
205 | ||
206 | struct pf_ruleset * | |
207 | pf_find_ruleset(const char *path) | |
208 | { | |
209 | struct pf_anchor *anchor; | |
210 | ||
211 | while (*path == '/') | |
212 | path++; | |
213 | if (!*path) | |
214 | return (&pf_main_ruleset); | |
215 | anchor = pf_find_anchor(path); | |
216 | if (anchor == NULL) | |
217 | return (NULL); | |
218 | else | |
219 | return (&anchor->ruleset); | |
220 | } | |
221 | ||
316670eb A |
222 | struct pf_ruleset * |
223 | pf_find_ruleset_with_owner(const char *path, const char *owner, int is_anchor, | |
224 | int *error) | |
225 | { | |
226 | struct pf_anchor *anchor; | |
227 | ||
228 | while (*path == '/') | |
229 | path++; | |
230 | if (!*path) | |
231 | return (&pf_main_ruleset); | |
232 | anchor = pf_find_anchor(path); | |
233 | if (anchor == NULL) { | |
234 | *error = EINVAL; | |
235 | return (NULL); | |
236 | } else { | |
3e170ce0 | 237 | if ((owner && (!strcmp(owner, anchor->owner))) |
316670eb A |
238 | || (is_anchor && !strcmp(anchor->owner, ""))) |
239 | return (&anchor->ruleset); | |
240 | *error = EPERM; | |
241 | return NULL; | |
242 | } | |
243 | } | |
244 | ||
b0d623f7 A |
245 | struct pf_ruleset * |
246 | pf_find_or_create_ruleset(const char *path) | |
247 | { | |
248 | char *p, *q, *r; | |
249 | struct pf_ruleset *ruleset; | |
250 | struct pf_anchor *anchor = 0, *dup, *parent = NULL; | |
251 | ||
252 | if (path[0] == 0) | |
253 | return (&pf_main_ruleset); | |
254 | while (*path == '/') | |
255 | path++; | |
256 | ruleset = pf_find_ruleset(path); | |
257 | if (ruleset != NULL) | |
258 | return (ruleset); | |
259 | p = (char *)rs_malloc(MAXPATHLEN); | |
260 | bzero(p, MAXPATHLEN); | |
261 | strlcpy(p, path, MAXPATHLEN); | |
262 | while (parent == NULL && (q = strrchr(p, '/')) != NULL) { | |
263 | *q = 0; | |
264 | if ((ruleset = pf_find_ruleset(p)) != NULL) { | |
265 | parent = ruleset->anchor; | |
266 | break; | |
267 | } | |
268 | } | |
269 | if (q == NULL) | |
270 | q = p; | |
271 | else | |
272 | q++; | |
273 | strlcpy(p, path, MAXPATHLEN); | |
274 | if (!*q) { | |
275 | rs_free(p); | |
276 | return (NULL); | |
277 | } | |
278 | while ((r = strchr(q, '/')) != NULL || *q) { | |
279 | if (r != NULL) | |
280 | *r = 0; | |
281 | if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE || | |
282 | (parent != NULL && strlen(parent->path) >= | |
283 | MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) { | |
284 | rs_free(p); | |
285 | return (NULL); | |
286 | } | |
287 | anchor = (struct pf_anchor *)rs_malloc(sizeof (*anchor)); | |
288 | if (anchor == NULL) { | |
289 | rs_free(p); | |
290 | return (NULL); | |
291 | } | |
292 | memset(anchor, 0, sizeof (*anchor)); | |
293 | RB_INIT(&anchor->children); | |
294 | strlcpy(anchor->name, q, sizeof (anchor->name)); | |
295 | if (parent != NULL) { | |
296 | strlcpy(anchor->path, parent->path, | |
297 | sizeof (anchor->path)); | |
298 | strlcat(anchor->path, "/", sizeof (anchor->path)); | |
299 | } | |
300 | strlcat(anchor->path, anchor->name, sizeof (anchor->path)); | |
301 | if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) != | |
302 | NULL) { | |
303 | printf("pf_find_or_create_ruleset: RB_INSERT1 " | |
304 | "'%s' '%s' collides with '%s' '%s'\n", | |
305 | anchor->path, anchor->name, dup->path, dup->name); | |
306 | rs_free(anchor); | |
307 | rs_free(p); | |
308 | return (NULL); | |
309 | } | |
310 | if (parent != NULL) { | |
311 | anchor->parent = parent; | |
312 | if ((dup = RB_INSERT(pf_anchor_node, &parent->children, | |
313 | anchor)) != NULL) { | |
314 | printf("pf_find_or_create_ruleset: " | |
315 | "RB_INSERT2 '%s' '%s' collides with " | |
316 | "'%s' '%s'\n", anchor->path, anchor->name, | |
317 | dup->path, dup->name); | |
318 | RB_REMOVE(pf_anchor_global, &pf_anchors, | |
319 | anchor); | |
320 | rs_free(anchor); | |
321 | rs_free(p); | |
322 | return (NULL); | |
323 | } | |
324 | } | |
325 | pf_init_ruleset(&anchor->ruleset); | |
326 | anchor->ruleset.anchor = anchor; | |
327 | parent = anchor; | |
328 | if (r != NULL) | |
329 | q = r + 1; | |
330 | else | |
331 | *q = 0; | |
332 | } | |
333 | rs_free(p); | |
334 | return (anchor ? &anchor->ruleset : 0); | |
335 | } | |
336 | ||
337 | void | |
338 | pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset) | |
339 | { | |
340 | struct pf_anchor *parent; | |
341 | int i; | |
342 | ||
343 | while (ruleset != NULL) { | |
344 | if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL || | |
345 | !RB_EMPTY(&ruleset->anchor->children) || | |
346 | ruleset->anchor->refcnt > 0 || ruleset->tables > 0 || | |
347 | ruleset->topen) | |
348 | return; | |
349 | for (i = 0; i < PF_RULESET_MAX; ++i) | |
350 | if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) || | |
351 | !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) || | |
352 | ruleset->rules[i].inactive.open) | |
353 | return; | |
354 | RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor); | |
355 | if ((parent = ruleset->anchor->parent) != NULL) | |
356 | RB_REMOVE(pf_anchor_node, &parent->children, | |
357 | ruleset->anchor); | |
358 | rs_free(ruleset->anchor); | |
359 | if (parent == NULL) | |
360 | return; | |
361 | ruleset = &parent->ruleset; | |
362 | } | |
363 | } | |
364 | ||
365 | int | |
366 | pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s, | |
367 | const char *name) | |
368 | { | |
369 | char *p, *path; | |
370 | struct pf_ruleset *ruleset; | |
371 | ||
372 | r->anchor = NULL; | |
373 | r->anchor_relative = 0; | |
374 | r->anchor_wildcard = 0; | |
375 | if (!name[0]) | |
376 | return (0); | |
377 | path = (char *)rs_malloc(MAXPATHLEN); | |
378 | bzero(path, MAXPATHLEN); | |
379 | if (name[0] == '/') | |
380 | strlcpy(path, name + 1, MAXPATHLEN); | |
381 | else { | |
382 | /* relative path */ | |
383 | r->anchor_relative = 1; | |
384 | if (s->anchor == NULL || !s->anchor->path[0]) | |
385 | path[0] = 0; | |
386 | else | |
387 | strlcpy(path, s->anchor->path, MAXPATHLEN); | |
388 | while (name[0] == '.' && name[1] == '.' && name[2] == '/') { | |
389 | if (!path[0]) { | |
390 | printf("pf_anchor_setup: .. beyond root\n"); | |
391 | rs_free(path); | |
392 | return (1); | |
393 | } | |
394 | if ((p = strrchr(path, '/')) != NULL) | |
395 | *p = 0; | |
396 | else | |
397 | path[0] = 0; | |
398 | r->anchor_relative++; | |
399 | name += 3; | |
400 | } | |
401 | if (path[0]) | |
402 | strlcat(path, "/", MAXPATHLEN); | |
403 | strlcat(path, name, MAXPATHLEN); | |
404 | } | |
405 | if ((p = strrchr(path, '/')) != NULL && strcmp(p, "/*") == 0) { | |
406 | r->anchor_wildcard = 1; | |
407 | *p = 0; | |
408 | } | |
409 | ruleset = pf_find_or_create_ruleset(path); | |
410 | rs_free(path); | |
411 | if (ruleset == NULL || ruleset->anchor == NULL) { | |
412 | printf("pf_anchor_setup: ruleset\n"); | |
413 | return (1); | |
414 | } | |
415 | r->anchor = ruleset->anchor; | |
416 | r->anchor->refcnt++; | |
417 | return (0); | |
418 | } | |
419 | ||
420 | int | |
421 | pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r, | |
422 | struct pfioc_rule *pr) | |
423 | { | |
424 | pr->anchor_call[0] = 0; | |
425 | if (r->anchor == NULL) | |
426 | return (0); | |
427 | if (!r->anchor_relative) { | |
428 | strlcpy(pr->anchor_call, "/", sizeof (pr->anchor_call)); | |
429 | strlcat(pr->anchor_call, r->anchor->path, | |
430 | sizeof (pr->anchor_call)); | |
431 | } else { | |
432 | char *a, *p; | |
433 | int i; | |
434 | ||
435 | a = (char *)rs_malloc(MAXPATHLEN); | |
436 | bzero(a, MAXPATHLEN); | |
437 | if (rs->anchor == NULL) | |
438 | a[0] = 0; | |
439 | else | |
440 | strlcpy(a, rs->anchor->path, MAXPATHLEN); | |
441 | for (i = 1; i < r->anchor_relative; ++i) { | |
442 | if ((p = strrchr(a, '/')) == NULL) | |
443 | p = a; | |
444 | *p = 0; | |
445 | strlcat(pr->anchor_call, "../", | |
446 | sizeof (pr->anchor_call)); | |
447 | } | |
448 | if (strncmp(a, r->anchor->path, strlen(a))) { | |
449 | printf("pf_anchor_copyout: '%s' '%s'\n", a, | |
450 | r->anchor->path); | |
451 | rs_free(a); | |
452 | return (1); | |
453 | } | |
454 | if (strlen(r->anchor->path) > strlen(a)) | |
455 | strlcat(pr->anchor_call, r->anchor->path + (a[0] ? | |
456 | strlen(a) + 1 : 0), sizeof (pr->anchor_call)); | |
457 | rs_free(a); | |
458 | } | |
459 | if (r->anchor_wildcard) | |
460 | strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*", | |
461 | sizeof (pr->anchor_call)); | |
462 | return (0); | |
463 | } | |
464 | ||
465 | void | |
466 | pf_anchor_remove(struct pf_rule *r) | |
467 | { | |
468 | if (r->anchor == NULL) | |
469 | return; | |
470 | if (r->anchor->refcnt <= 0) { | |
471 | printf("pf_anchor_remove: broken refcount\n"); | |
472 | r->anchor = NULL; | |
473 | return; | |
474 | } | |
475 | if (!--r->anchor->refcnt) | |
476 | pf_remove_if_empty_ruleset(&r->anchor->ruleset); | |
477 | r->anchor = NULL; | |
478 | } |