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