xref: /freebsd/sbin/pfctl/pf_ruleset.c (revision dbaad75f2834f40bfe74ebe393d2101967052036)
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 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/socket.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 #include <arpa/inet.h>
60 #include <errno.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #define rs_malloc(x)		 calloc(1, x)
65 #define rs_free(x)		 free(x)
66 
67 #include "pfctl.h"
68 
69 #ifdef PFDEBUG
70 #include <sys/stdarg.h>
71 #define DPFPRINTF(format, x...)	fprintf(stderr, format , ##x)
72 #else
73 #define DPFPRINTF(format, x...)	((void)0)
74 #endif /* PFDEBUG */
75 
76 struct pfctl_anchor_global	 pf_anchors;
77 struct pfctl_anchor	 pf_main_anchor;
78 #undef V_pf_anchors
79 #define V_pf_anchors		 pf_anchors
80 #undef pf_main_ruleset
81 #define pf_main_ruleset		 pf_main_anchor.ruleset
82 
83 static __inline int		pf_anchor_compare(struct pfctl_anchor *,
84 				    struct pfctl_anchor *);
85 static struct pfctl_anchor	*pf_find_anchor(const char *);
86 
87 RB_GENERATE(pfctl_anchor_global, pfctl_anchor, entry_global,
88     pf_anchor_compare);
89 RB_GENERATE(pfctl_anchor_node, pfctl_anchor, entry_node, pf_anchor_compare);
90 
91 static __inline int
92 pf_anchor_compare(struct pfctl_anchor *a, struct pfctl_anchor *b)
93 {
94 	int c = strcmp(a->path, b->path);
95 
96 	return (c ? (c < 0 ? -1 : 1) : 0);
97 }
98 
99 int
100 pf_get_ruleset_number(u_int8_t action)
101 {
102 	switch (action) {
103 	case PF_SCRUB:
104 	case PF_NOSCRUB:
105 		return (PF_RULESET_SCRUB);
106 		break;
107 	case PF_PASS:
108 	case PF_DROP:
109 	case PF_MATCH:
110 		return (PF_RULESET_FILTER);
111 		break;
112 	case PF_NAT:
113 	case PF_NONAT:
114 		return (PF_RULESET_NAT);
115 		break;
116 	case PF_BINAT:
117 	case PF_NOBINAT:
118 		return (PF_RULESET_BINAT);
119 		break;
120 	case PF_RDR:
121 	case PF_NORDR:
122 		return (PF_RULESET_RDR);
123 		break;
124 	default:
125 		return (PF_RULESET_MAX);
126 		break;
127 	}
128 }
129 
130 void
131 pf_init_ruleset(struct pfctl_ruleset *ruleset)
132 {
133 	int	i;
134 
135 	memset(ruleset, 0, sizeof(struct pfctl_ruleset));
136 	for (i = 0; i < PF_RULESET_MAX; i++) {
137 		TAILQ_INIT(&ruleset->rules[i].queues[0]);
138 		TAILQ_INIT(&ruleset->rules[i].queues[1]);
139 		ruleset->rules[i].active.ptr = &ruleset->rules[i].queues[0];
140 		ruleset->rules[i].inactive.ptr = &ruleset->rules[i].queues[1];
141 	}
142 }
143 
144 static struct pfctl_anchor *
145 pf_find_anchor(const char *path)
146 {
147 	struct pfctl_anchor	*key, *found;
148 
149 	key = (struct pfctl_anchor *)rs_malloc(sizeof(*key));
150 	if (key == NULL)
151 		return (NULL);
152 	strlcpy(key->path, path, sizeof(key->path));
153 	found = RB_FIND(pfctl_anchor_global, &V_pf_anchors, key);
154 	rs_free(key);
155 	return (found);
156 }
157 
158 struct pfctl_ruleset *
159 pf_find_ruleset(const char *path)
160 {
161 	struct pfctl_anchor	*anchor;
162 
163 	while (*path == '/')
164 		path++;
165 	if (!*path)
166 		return (&pf_main_ruleset);
167 	anchor = pf_find_anchor(path);
168 	if (anchor == NULL)
169 		return (NULL);
170 	else
171 		return (&anchor->ruleset);
172 }
173 
174 struct pfctl_ruleset *
175 pf_find_or_create_ruleset(const char *path)
176 {
177 	char			*p, *q, *r;
178 	struct pfctl_ruleset	*ruleset;
179 	struct pfctl_anchor	*anchor = NULL, *dup, *parent = NULL;
180 
181 	if (path[0] == 0)
182 		return (&pf_main_ruleset);
183 	while (*path == '/')
184 		path++;
185 	ruleset = pf_find_ruleset(path);
186 	if (ruleset != NULL)
187 		return (ruleset);
188 	p = (char *)rs_malloc(MAXPATHLEN);
189 	if (p == NULL)
190 		return (NULL);
191 	strlcpy(p, path, MAXPATHLEN);
192 	while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
193 		*q = 0;
194 		if ((ruleset = pf_find_ruleset(p)) != NULL) {
195 			parent = ruleset->anchor;
196 			break;
197 		}
198 	}
199 	if (q == NULL)
200 		q = p;
201 	else
202 		q++;
203 	strlcpy(p, path, MAXPATHLEN);
204 	if (!*q) {
205 		rs_free(p);
206 		return (NULL);
207 	}
208 	while ((r = strchr(q, '/')) != NULL || *q) {
209 		if (r != NULL)
210 			*r = 0;
211 		if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
212 		    (parent != NULL && strlen(parent->path) >=
213 		    MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
214 			rs_free(p);
215 			return (NULL);
216 		}
217 		anchor = (struct pfctl_anchor *)rs_malloc(sizeof(*anchor));
218 		if (anchor == NULL) {
219 			rs_free(p);
220 			return (NULL);
221 		}
222 		RB_INIT(&anchor->children);
223 		strlcpy(anchor->name, q, sizeof(anchor->name));
224 		if (parent != NULL) {
225 			strlcpy(anchor->path, parent->path,
226 			    sizeof(anchor->path));
227 			strlcat(anchor->path, "/", sizeof(anchor->path));
228 		}
229 		strlcat(anchor->path, anchor->name, sizeof(anchor->path));
230 		if ((dup = RB_INSERT(pfctl_anchor_global, &V_pf_anchors, anchor)) !=
231 		    NULL) {
232 			printf("pf_find_or_create_ruleset: RB_INSERT1 "
233 			    "'%s' '%s' collides with '%s' '%s'\n",
234 			    anchor->path, anchor->name, dup->path, dup->name);
235 			rs_free(anchor);
236 			rs_free(p);
237 			return (NULL);
238 		}
239 		if (parent != NULL) {
240 			anchor->parent = parent;
241 			if ((dup = RB_INSERT(pfctl_anchor_node, &parent->children,
242 			    anchor)) != NULL) {
243 				printf("pf_find_or_create_ruleset: "
244 				    "RB_INSERT2 '%s' '%s' collides with "
245 				    "'%s' '%s'\n", anchor->path, anchor->name,
246 				    dup->path, dup->name);
247 				RB_REMOVE(pfctl_anchor_global, &V_pf_anchors,
248 				    anchor);
249 				rs_free(anchor);
250 				rs_free(p);
251 				return (NULL);
252 			}
253 		}
254 		pf_init_ruleset(&anchor->ruleset);
255 		anchor->ruleset.anchor = anchor;
256 		parent = anchor;
257 		if (r != NULL)
258 			q = r + 1;
259 		else
260 			*q = 0;
261 	}
262 	rs_free(p);
263 	return (&anchor->ruleset);
264 }
265 
266 void
267 pf_remove_if_empty_ruleset(struct pfctl_ruleset *ruleset)
268 {
269 	struct pfctl_anchor	*parent;
270 	int			 i;
271 
272 	while (ruleset != NULL) {
273 		if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
274 		    !RB_EMPTY(&ruleset->anchor->children) ||
275 		    ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
276 		    ruleset->topen)
277 			return;
278 		for (i = 0; i < PF_RULESET_MAX; ++i)
279 			if (!TAILQ_EMPTY(ruleset->rules[i].active.ptr) ||
280 			    !TAILQ_EMPTY(ruleset->rules[i].inactive.ptr) ||
281 			    ruleset->rules[i].inactive.open)
282 				return;
283 		RB_REMOVE(pfctl_anchor_global, &V_pf_anchors, ruleset->anchor);
284 		if ((parent = ruleset->anchor->parent) != NULL)
285 			RB_REMOVE(pfctl_anchor_node, &parent->children,
286 			    ruleset->anchor);
287 		rs_free(ruleset->anchor);
288 		if (parent == NULL)
289 			return;
290 		ruleset = &parent->ruleset;
291 	}
292 }
293 int
294 pfctl_anchor_setup(struct pfctl_rule *r, const struct pfctl_ruleset *s,
295     const char *name)
296 {
297 	char			*p, *path;
298 	struct pfctl_ruleset	*ruleset;
299 
300 	r->anchor = NULL;
301 	r->anchor_relative = 0;
302 	r->anchor_wildcard = 0;
303 	if (!name[0])
304 		return (0);
305 	path = (char *)rs_malloc(MAXPATHLEN);
306 	if (path == NULL)
307 		return (1);
308 	if (name[0] == '/')
309 		strlcpy(path, name + 1, MAXPATHLEN);
310 	else {
311 		/* relative path */
312 		r->anchor_relative = 1;
313 		if (s->anchor == NULL || !s->anchor->path[0])
314 			path[0] = 0;
315 		else
316 			strlcpy(path, s->anchor->path, MAXPATHLEN);
317 		while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
318 			if (!path[0]) {
319 				printf("pfctl_anchor_setup: .. beyond root\n");
320 				rs_free(path);
321 				return (1);
322 			}
323 			if ((p = strrchr(path, '/')) != NULL)
324 				*p = 0;
325 			else
326 				path[0] = 0;
327 			r->anchor_relative++;
328 			name += 3;
329 		}
330 		if (path[0])
331 			strlcat(path, "/", MAXPATHLEN);
332 		strlcat(path, name, MAXPATHLEN);
333 	}
334 	if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
335 		r->anchor_wildcard = 1;
336 		*p = 0;
337 	}
338 	ruleset = pf_find_or_create_ruleset(path);
339 	rs_free(path);
340 	if (ruleset == NULL || ruleset->anchor == NULL) {
341 		printf("pfctl_anchor_setup: ruleset\n");
342 		return (1);
343 	}
344 	r->anchor = ruleset->anchor;
345 	r->anchor->refcnt++;
346 	return (0);
347 }
348