xref: /freebsd-13-stable/sys/netpfil/pf/pf_ruleset.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2002,2003 Henning Brauer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *    - Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *    - Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Effort sponsored in part by the Defense Advanced Research Projects
33  * Agency (DARPA) and Air Force Research Laboratory, Air Force
34  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35  *
36  *	$OpenBSD: pf_ruleset.c,v 1.2 2008/12/18 15:31:37 dhill Exp $
37  */
38 
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/systm.h>
43 #include <sys/refcount.h>
44 #include <sys/mbuf.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/tcp.h>
50 
51 #include <net/if.h>
52 #include <net/vnet.h>
53 #include <net/pfvar.h>
54 
55 #ifdef INET6
56 #include <netinet/ip6.h>
57 #endif /* INET6 */
58 
59 #ifndef _KERNEL
60 #error "Kernel only file. Please use sbin/pfctl/pf_ruleset.c instead."
61 #endif
62 
63 #define DPFPRINTF(format, x...)				\
64 	if (V_pf_status.debug >= PF_DEBUG_NOISY)	\
65 		printf(format , ##x)
66 #define rs_malloc(x)		malloc(x, M_TEMP, M_NOWAIT|M_ZERO)
67 #define rs_free(x)		free(x, M_TEMP)
68 
69 VNET_DEFINE(struct pf_kanchor_global,	pf_anchors);
70 VNET_DEFINE(struct pf_kanchor,		pf_main_anchor);
71 
72 static __inline int		pf_kanchor_compare(struct pf_kanchor *,
73 				    struct pf_kanchor *);
74 static struct pf_kanchor	*pf_find_kanchor(const char *);
75 
76 RB_GENERATE(pf_kanchor_global, pf_kanchor, entry_global, pf_kanchor_compare);
77 RB_GENERATE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
78 
79 static __inline int
pf_kanchor_compare(struct pf_kanchor * a,struct pf_kanchor * b)80 pf_kanchor_compare(struct pf_kanchor *a, struct pf_kanchor *b)
81 {
82 	int c = strcmp(a->path, b->path);
83 
84 	return (c ? (c < 0 ? -1 : 1) : 0);
85 }
86 
87 int
pf_get_ruleset_number(u_int8_t action)88 pf_get_ruleset_number(u_int8_t action)
89 {
90 	switch (action) {
91 	case PF_SCRUB:
92 	case PF_NOSCRUB:
93 		return (PF_RULESET_SCRUB);
94 		break;
95 	case PF_PASS:
96 	case PF_MATCH:
97 	case PF_DROP:
98 		return (PF_RULESET_FILTER);
99 		break;
100 	case PF_NAT:
101 	case PF_NONAT:
102 		return (PF_RULESET_NAT);
103 		break;
104 	case PF_BINAT:
105 	case PF_NOBINAT:
106 		return (PF_RULESET_BINAT);
107 		break;
108 	case PF_RDR:
109 	case PF_NORDR:
110 		return (PF_RULESET_RDR);
111 		break;
112 	default:
113 		return (PF_RULESET_MAX);
114 		break;
115 	}
116 }
117 
118 static struct pf_kanchor *
pf_find_kanchor(const char * path)119 pf_find_kanchor(const char *path)
120 {
121 	struct pf_kanchor	*key, *found;
122 
123 	key = (struct pf_kanchor *)rs_malloc(sizeof(*key));
124 	if (key == NULL)
125 		return (NULL);
126 	strlcpy(key->path, path, sizeof(key->path));
127 	found = RB_FIND(pf_kanchor_global, &V_pf_anchors, key);
128 	rs_free(key);
129 	return (found);
130 }
131 
132 void
pf_init_kruleset(struct pf_kruleset * ruleset)133 pf_init_kruleset(struct pf_kruleset *ruleset)
134 {
135 	int	i;
136 
137 	memset(ruleset, 0, sizeof(struct pf_kruleset));
138 	for (i = 0; i < PF_RULESET_MAX; i++) {
139 		TAILQ_INIT(&ruleset->rules[i].queues[0]);
140 		TAILQ_INIT(&ruleset->rules[i].queues[1]);
141 		ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
142 		ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
143 	}
144 }
145 
146 struct pf_kruleset *
pf_find_kruleset(const char * path)147 pf_find_kruleset(const char *path)
148 {
149 	struct pf_kanchor	*anchor;
150 
151 	while (*path == '/')
152 		path++;
153 	if (!*path)
154 		return (&pf_main_ruleset);
155 	anchor = pf_find_kanchor(path);
156 	if (anchor == NULL)
157 		return (NULL);
158 	else
159 		return (&anchor->ruleset);
160 }
161 
162 struct pf_kruleset *
pf_find_or_create_kruleset(const char * path)163 pf_find_or_create_kruleset(const char *path)
164 {
165 	char			*p, *q, *r;
166 	struct pf_kruleset	*ruleset;
167 	struct pf_kanchor	*anchor = NULL, *dup, *parent = NULL;
168 
169 	if (path[0] == 0)
170 		return (&pf_main_ruleset);
171 	while (*path == '/')
172 		path++;
173 	ruleset = pf_find_kruleset(path);
174 	if (ruleset != NULL)
175 		return (ruleset);
176 	p = (char *)rs_malloc(MAXPATHLEN);
177 	if (p == NULL)
178 		return (NULL);
179 	strlcpy(p, path, MAXPATHLEN);
180 	while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
181 		*q = 0;
182 		if ((ruleset = pf_find_kruleset(p)) != NULL) {
183 			parent = ruleset->anchor;
184 			break;
185 		}
186 	}
187 	if (q == NULL)
188 		q = p;
189 	else
190 		q++;
191 	strlcpy(p, path, MAXPATHLEN);
192 	if (!*q) {
193 		rs_free(p);
194 		return (NULL);
195 	}
196 	while ((r = strchr(q, '/')) != NULL || *q) {
197 		if (r != NULL)
198 			*r = 0;
199 		if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
200 		    (parent != NULL && strlen(parent->path) >=
201 		    MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
202 			rs_free(p);
203 			return (NULL);
204 		}
205 		anchor = (struct pf_kanchor *)rs_malloc(sizeof(*anchor));
206 		if (anchor == NULL) {
207 			rs_free(p);
208 			return (NULL);
209 		}
210 		RB_INIT(&anchor->children);
211 		strlcpy(anchor->name, q, sizeof(anchor->name));
212 		if (parent != NULL) {
213 			strlcpy(anchor->path, parent->path,
214 			    sizeof(anchor->path));
215 			strlcat(anchor->path, "/", sizeof(anchor->path));
216 		}
217 		strlcat(anchor->path, anchor->name, sizeof(anchor->path));
218 		if ((dup = RB_INSERT(pf_kanchor_global, &V_pf_anchors, anchor)) !=
219 		    NULL) {
220 			printf("pf_find_or_create_ruleset: RB_INSERT1 "
221 			    "'%s' '%s' collides with '%s' '%s'\n",
222 			    anchor->path, anchor->name, dup->path, dup->name);
223 			rs_free(anchor);
224 			rs_free(p);
225 			return (NULL);
226 		}
227 		if (parent != NULL) {
228 			anchor->parent = parent;
229 			if ((dup = RB_INSERT(pf_kanchor_node, &parent->children,
230 			    anchor)) != NULL) {
231 				printf("pf_find_or_create_ruleset: "
232 				    "RB_INSERT2 '%s' '%s' collides with "
233 				    "'%s' '%s'\n", anchor->path, anchor->name,
234 				    dup->path, dup->name);
235 				RB_REMOVE(pf_kanchor_global, &V_pf_anchors,
236 				    anchor);
237 				rs_free(anchor);
238 				rs_free(p);
239 				return (NULL);
240 			}
241 		}
242 		pf_init_kruleset(&anchor->ruleset);
243 		anchor->ruleset.anchor = anchor;
244 		parent = anchor;
245 		if (r != NULL)
246 			q = r + 1;
247 		else
248 			*q = 0;
249 	}
250 	rs_free(p);
251 	return (&anchor->ruleset);
252 }
253 
254 void
pf_remove_if_empty_kruleset(struct pf_kruleset * ruleset)255 pf_remove_if_empty_kruleset(struct pf_kruleset *ruleset)
256 {
257 	struct pf_kanchor	*parent;
258 	int			 i;
259 
260 	while (ruleset != NULL) {
261 		if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
262 		    !RB_EMPTY(&ruleset->anchor->children) ||
263 		    ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
264 		    ruleset->topen)
265 			return;
266 		for (i = 0; i < PF_RULESET_MAX; ++i)
267 			if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
268 			    !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
269 			    ruleset->rules[i].inactive.open)
270 				return;
271 		RB_REMOVE(pf_kanchor_global, &V_pf_anchors, ruleset->anchor);
272 		if ((parent = ruleset->anchor->parent) != NULL)
273 			RB_REMOVE(pf_kanchor_node, &parent->children,
274 			    ruleset->anchor);
275 		rs_free(ruleset->anchor);
276 		if (parent == NULL)
277 			return;
278 		ruleset = &parent->ruleset;
279 	}
280 }
281 
282 int
pf_kanchor_setup(struct pf_krule * r,const struct pf_kruleset * s,const char * name)283 pf_kanchor_setup(struct pf_krule *r, const struct pf_kruleset *s,
284     const char *name)
285 {
286 	char			*p, *path;
287 	struct pf_kruleset	*ruleset;
288 
289 	r->anchor = NULL;
290 	r->anchor_relative = 0;
291 	r->anchor_wildcard = 0;
292 	if (!name[0])
293 		return (0);
294 	path = (char *)rs_malloc(MAXPATHLEN);
295 	if (path == NULL)
296 		return (1);
297 	if (name[0] == '/')
298 		strlcpy(path, name + 1, MAXPATHLEN);
299 	else {
300 		/* relative path */
301 		r->anchor_relative = 1;
302 		if (s->anchor == NULL || !s->anchor->path[0])
303 			path[0] = 0;
304 		else
305 			strlcpy(path, s->anchor->path, MAXPATHLEN);
306 		while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
307 			if (!path[0]) {
308 				DPFPRINTF("pf_anchor_setup: .. beyond root\n");
309 				rs_free(path);
310 				return (1);
311 			}
312 			if ((p = strrchr(path, '/')) != NULL)
313 				*p = 0;
314 			else
315 				path[0] = 0;
316 			r->anchor_relative++;
317 			name += 3;
318 		}
319 		if (path[0])
320 			strlcat(path, "/", MAXPATHLEN);
321 		strlcat(path, name, MAXPATHLEN);
322 	}
323 	if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
324 		r->anchor_wildcard = 1;
325 		*p = 0;
326 	}
327 	ruleset = pf_find_or_create_kruleset(path);
328 	rs_free(path);
329 	if (ruleset == NULL || ruleset->anchor == NULL) {
330 		DPFPRINTF("pf_anchor_setup: ruleset\n");
331 		return (1);
332 	}
333 	r->anchor = ruleset->anchor;
334 	r->anchor->refcnt++;
335 	return (0);
336 }
337 
338 int
pf_kanchor_nvcopyout(const struct pf_kruleset * rs,const struct pf_krule * r,nvlist_t * nvl)339 pf_kanchor_nvcopyout(const struct pf_kruleset *rs, const struct pf_krule *r,
340     nvlist_t *nvl)
341 {
342 	char anchor_call[MAXPATHLEN] = { 0 };
343 
344 	if (r->anchor == NULL)
345 		goto done;
346 	if (!r->anchor_relative) {
347 		strlcpy(anchor_call, "/", sizeof(anchor_call));
348 		strlcat(anchor_call, r->anchor->path,
349 		    sizeof(anchor_call));
350 	} else {
351 		char	 a[MAXPATHLEN];
352 		char	*p;
353 		int	 i;
354 		if (rs->anchor == NULL)
355 			a[0] = 0;
356 		else
357 			strlcpy(a, rs->anchor->path, MAXPATHLEN);
358 		for (i = 1; i < r->anchor_relative; ++i) {
359 			if ((p = strrchr(a, '/')) == NULL)
360 				p = a;
361 			*p = 0;
362 			strlcat(anchor_call, "../",
363 			    sizeof(anchor_call));
364 		}
365 		if (strncmp(a, r->anchor->path, strlen(a))) {
366 			printf("pf_anchor_copyout: '%s' '%s'\n", a,
367 			    r->anchor->path);
368 			return (1);
369 		}
370 		if (strlen(r->anchor->path) > strlen(a))
371 			strlcat(anchor_call, r->anchor->path + (a[0] ?
372 			    strlen(a) + 1 : 0), sizeof(anchor_call));
373 
374 	}
375 	if (r->anchor_wildcard)
376 		strlcat(anchor_call, anchor_call[0] ? "/*" : "*",
377 		    sizeof(anchor_call));
378 
379 done:
380 	nvlist_add_string(nvl, "anchor_call", anchor_call);
381 
382 	return (0);
383 }
384 
385 int
pf_kanchor_copyout(const struct pf_kruleset * rs,const struct pf_krule * r,struct pfioc_rule * pr)386 pf_kanchor_copyout(const struct pf_kruleset *rs, const struct pf_krule *r,
387     struct pfioc_rule *pr)
388 {
389 	pr->anchor_call[0] = 0;
390 	if (r->anchor == NULL)
391 		return (0);
392 	if (!r->anchor_relative) {
393 		strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
394 		strlcat(pr->anchor_call, r->anchor->path,
395 		    sizeof(pr->anchor_call));
396 	} else {
397 		char	*a, *p;
398 		int	 i;
399 
400 		a = (char *)rs_malloc(MAXPATHLEN);
401 		if (a == NULL)
402 			return (1);
403 		if (rs->anchor == NULL)
404 			a[0] = 0;
405 		else
406 			strlcpy(a, rs->anchor->path, MAXPATHLEN);
407 		for (i = 1; i < r->anchor_relative; ++i) {
408 			if ((p = strrchr(a, '/')) == NULL)
409 				p = a;
410 			*p = 0;
411 			strlcat(pr->anchor_call, "../",
412 			    sizeof(pr->anchor_call));
413 		}
414 		if (strncmp(a, r->anchor->path, strlen(a))) {
415 			printf("pf_anchor_copyout: '%s' '%s'\n", a,
416 			    r->anchor->path);
417 			rs_free(a);
418 			return (1);
419 		}
420 		if (strlen(r->anchor->path) > strlen(a))
421 			strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
422 			    strlen(a) + 1 : 0), sizeof(pr->anchor_call));
423 		rs_free(a);
424 	}
425 	if (r->anchor_wildcard)
426 		strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
427 		    sizeof(pr->anchor_call));
428 	return (0);
429 }
430 
431 void
pf_kanchor_remove(struct pf_krule * r)432 pf_kanchor_remove(struct pf_krule *r)
433 {
434 	if (r->anchor == NULL)
435 		return;
436 	if (r->anchor->refcnt <= 0) {
437 		printf("pf_anchor_remove: broken refcount\n");
438 		r->anchor = NULL;
439 		return;
440 	}
441 	if (!--r->anchor->refcnt)
442 		pf_remove_if_empty_kruleset(&r->anchor->ruleset);
443 	r->anchor = NULL;
444 }
445