xref: /openbsd/sys/net/pf_ruleset.c (revision 6f40fd34)
1 /*	$OpenBSD: pf_ruleset.c,v 1.15 2017/05/30 20:00:48 deraadt Exp $ */
2 
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  */
37 
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #ifdef _KERNEL
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #endif /* _KERNEL */
44 #include <sys/syslog.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
48 #include <netinet/tcp.h>
49 
50 #include <net/if.h>
51 #include <net/pfvar.h>
52 
53 #ifdef INET6
54 #include <netinet/ip6.h>
55 #endif /* INET6 */
56 
57 
58 #ifdef _KERNEL
59 #define rs_malloc(x)		malloc(x, M_TEMP, M_WAITOK|M_CANFAIL|M_ZERO)
60 #define rs_free(x, siz)		free(x, M_TEMP, siz)
61 
62 #else	/* !_KERNEL */
63 /* Userland equivalents so we can lend code to pfctl et al. */
64 
65 #include <arpa/inet.h>
66 #include <errno.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #define rs_malloc(x)		 calloc(1, x)
71 #define rs_free(x, siz)		 freezero(x, siz)
72 
73 #ifdef PFDEBUG
74 #include <sys/stdarg.h>	/* for DPFPRINTF() */
75 #endif	/* PFDEBUG */
76 #endif /* _KERNEL */
77 
78 
79 struct pf_anchor_global	 pf_anchors;
80 struct pf_anchor	 pf_main_anchor;
81 
82 static __inline int pf_anchor_compare(struct pf_anchor *, struct pf_anchor *);
83 
84 RB_GENERATE(pf_anchor_global, pf_anchor, entry_global, pf_anchor_compare);
85 RB_GENERATE(pf_anchor_node, pf_anchor, entry_node, pf_anchor_compare);
86 
87 static __inline int
88 pf_anchor_compare(struct pf_anchor *a, struct pf_anchor *b)
89 {
90 	int c = strcmp(a->path, b->path);
91 
92 	return (c ? (c < 0 ? -1 : 1) : 0);
93 }
94 
95 void
96 pf_init_ruleset(struct pf_ruleset *ruleset)
97 {
98 	memset(ruleset, 0, sizeof(struct pf_ruleset));
99 	TAILQ_INIT(&ruleset->rules.queues[0]);
100 	TAILQ_INIT(&ruleset->rules.queues[1]);
101 	ruleset->rules.active.ptr = &ruleset->rules.queues[0];
102 	ruleset->rules.inactive.ptr = &ruleset->rules.queues[1];
103 }
104 
105 struct pf_anchor *
106 pf_find_anchor(const char *path)
107 {
108 	struct pf_anchor	*key, *found;
109 
110 	key = rs_malloc(sizeof(*key));
111 	if (key == NULL)
112 		return (NULL);
113 	strlcpy(key->path, path, sizeof(key->path));
114 	found = RB_FIND(pf_anchor_global, &pf_anchors, key);
115 	rs_free(key, sizeof (*key));
116 	return (found);
117 }
118 
119 struct pf_ruleset *
120 pf_find_ruleset(const char *path)
121 {
122 	struct pf_anchor	*anchor;
123 
124 	while (*path == '/')
125 		path++;
126 	if (!*path)
127 		return (&pf_main_ruleset);
128 	anchor = pf_find_anchor(path);
129 	if (anchor == NULL)
130 		return (NULL);
131 	else
132 		return (&anchor->ruleset);
133 }
134 
135 struct pf_ruleset *
136 pf_find_or_create_ruleset(const char *path)
137 {
138 	char			*p, *q, *r;
139 	struct pf_ruleset	*ruleset;
140 	struct pf_anchor	*anchor, *dup, *parent = NULL;
141 
142 	if (path[0] == 0)
143 		return (&pf_main_ruleset);
144 	while (*path == '/')
145 		path++;
146 	ruleset = pf_find_ruleset(path);
147 	if (ruleset != NULL)
148 		return (ruleset);
149 	p = rs_malloc(MAXPATHLEN);
150 	if (p == NULL)
151 		return (NULL);
152 	strlcpy(p, path, MAXPATHLEN);
153 	while (parent == NULL && (q = strrchr(p, '/')) != NULL) {
154 		*q = 0;
155 		if ((ruleset = pf_find_ruleset(p)) != NULL) {
156 			parent = ruleset->anchor;
157 			break;
158 		}
159 	}
160 	if (q == NULL)
161 		q = p;
162 	else
163 		q++;
164 	strlcpy(p, path, MAXPATHLEN);
165 	if (!*q) {
166 		rs_free(p, MAXPATHLEN);
167 		return (NULL);
168 	}
169 	while ((r = strchr(q, '/')) != NULL || *q) {
170 		if (r != NULL)
171 			*r = 0;
172 		if (!*q || strlen(q) >= PF_ANCHOR_NAME_SIZE ||
173 		    (parent != NULL && strlen(parent->path) >=
174 		    MAXPATHLEN - PF_ANCHOR_NAME_SIZE - 1)) {
175 			rs_free(p, MAXPATHLEN);
176 			return (NULL);
177 		}
178 		anchor = rs_malloc(sizeof(*anchor));
179 		if (anchor == NULL) {
180 			rs_free(p, MAXPATHLEN);
181 			return (NULL);
182 		}
183 		RB_INIT(&anchor->children);
184 		strlcpy(anchor->name, q, sizeof(anchor->name));
185 		if (parent != NULL) {
186 			strlcpy(anchor->path, parent->path,
187 			    sizeof(anchor->path));
188 			strlcat(anchor->path, "/", sizeof(anchor->path));
189 		}
190 		strlcat(anchor->path, anchor->name, sizeof(anchor->path));
191 		if ((dup = RB_INSERT(pf_anchor_global, &pf_anchors, anchor)) !=
192 		    NULL) {
193 			DPFPRINTF(LOG_NOTICE,
194 			    "pf_find_or_create_ruleset: RB_INSERT1 "
195 			    "'%s' '%s' collides with '%s' '%s'",
196 			    anchor->path, anchor->name, dup->path, dup->name);
197 			rs_free(anchor, sizeof(*anchor));
198 			rs_free(p, MAXPATHLEN);
199 			return (NULL);
200 		}
201 		if (parent != NULL) {
202 			anchor->parent = parent;
203 			if ((dup = RB_INSERT(pf_anchor_node, &parent->children,
204 			    anchor)) != NULL) {
205 				DPFPRINTF(LOG_NOTICE,
206 				    "pf_find_or_create_ruleset: "
207 				    "RB_INSERT2 '%s' '%s' collides with "
208 				    "'%s' '%s'", anchor->path, anchor->name,
209 				    dup->path, dup->name);
210 				RB_REMOVE(pf_anchor_global, &pf_anchors,
211 				    anchor);
212 				rs_free(anchor, sizeof(*anchor));
213 				rs_free(p, MAXPATHLEN);
214 				return (NULL);
215 			}
216 		}
217 		pf_init_ruleset(&anchor->ruleset);
218 		anchor->ruleset.anchor = anchor;
219 		parent = anchor;
220 		if (r != NULL)
221 			q = r + 1;
222 		else
223 			*q = 0;
224 	}
225 	rs_free(p, MAXPATHLEN);
226 	return (&anchor->ruleset);
227 }
228 
229 void
230 pf_remove_if_empty_ruleset(struct pf_ruleset *ruleset)
231 {
232 	struct pf_anchor	*parent;
233 
234 	while (ruleset != NULL) {
235 		if (ruleset == &pf_main_ruleset || ruleset->anchor == NULL ||
236 		    !RB_EMPTY(&ruleset->anchor->children) ||
237 		    ruleset->anchor->refcnt > 0 || ruleset->tables > 0 ||
238 		    ruleset->topen)
239 			return;
240 		if (!TAILQ_EMPTY(ruleset->rules.active.ptr) ||
241 		    !TAILQ_EMPTY(ruleset->rules.inactive.ptr) ||
242 		    ruleset->rules.inactive.open)
243 			return;
244 		RB_REMOVE(pf_anchor_global, &pf_anchors, ruleset->anchor);
245 		if ((parent = ruleset->anchor->parent) != NULL)
246 			RB_REMOVE(pf_anchor_node, &parent->children,
247 			    ruleset->anchor);
248 		rs_free(ruleset->anchor, sizeof(*(ruleset->anchor)));
249 		if (parent == NULL)
250 			return;
251 		ruleset = &parent->ruleset;
252 	}
253 }
254 
255 int
256 pf_anchor_setup(struct pf_rule *r, const struct pf_ruleset *s,
257     const char *name)
258 {
259 	char			*p, *path;
260 	struct pf_ruleset	*ruleset;
261 
262 	r->anchor = NULL;
263 	r->anchor_relative = 0;
264 	r->anchor_wildcard = 0;
265 	if (!name[0])
266 		return (0);
267 	path = rs_malloc(MAXPATHLEN);
268 	if (path == NULL)
269 		return (1);
270 	if (name[0] == '/')
271 		strlcpy(path, name + 1, MAXPATHLEN);
272 	else {
273 		/* relative path */
274 		r->anchor_relative = 1;
275 		if (s->anchor == NULL || !s->anchor->path[0])
276 			path[0] = 0;
277 		else
278 			strlcpy(path, s->anchor->path, MAXPATHLEN);
279 		while (name[0] == '.' && name[1] == '.' && name[2] == '/') {
280 			if (!path[0]) {
281 				DPFPRINTF(LOG_NOTICE,
282 				    "pf_anchor_setup: .. beyond root");
283 				rs_free(path, MAXPATHLEN);
284 				return (1);
285 			}
286 			if ((p = strrchr(path, '/')) != NULL)
287 				*p = 0;
288 			else
289 				path[0] = 0;
290 			r->anchor_relative++;
291 			name += 3;
292 		}
293 		if (path[0])
294 			strlcat(path, "/", MAXPATHLEN);
295 		strlcat(path, name, MAXPATHLEN);
296 	}
297 	if ((p = strrchr(path, '/')) != NULL && !strcmp(p, "/*")) {
298 		r->anchor_wildcard = 1;
299 		*p = 0;
300 	}
301 	ruleset = pf_find_or_create_ruleset(path);
302 	rs_free(path, MAXPATHLEN);
303 	if (ruleset == NULL || ruleset->anchor == NULL) {
304 		DPFPRINTF(LOG_NOTICE,
305 		    "pf_anchor_setup: ruleset");
306 		return (1);
307 	}
308 	r->anchor = ruleset->anchor;
309 	r->anchor->refcnt++;
310 	return (0);
311 }
312 
313 int
314 pf_anchor_copyout(const struct pf_ruleset *rs, const struct pf_rule *r,
315     struct pfioc_rule *pr)
316 {
317 	pr->anchor_call[0] = 0;
318 	if (r->anchor == NULL)
319 		return (0);
320 	if (!r->anchor_relative) {
321 		strlcpy(pr->anchor_call, "/", sizeof(pr->anchor_call));
322 		strlcat(pr->anchor_call, r->anchor->path,
323 		    sizeof(pr->anchor_call));
324 	} else {
325 		char	*a, *p;
326 		int	 i;
327 
328 		a = rs_malloc(MAXPATHLEN);
329 		if (a == NULL)
330 			return (1);
331 		if (rs->anchor == NULL)
332 			a[0] = 0;
333 		else
334 			strlcpy(a, rs->anchor->path, MAXPATHLEN);
335 		for (i = 1; i < r->anchor_relative; ++i) {
336 			if ((p = strrchr(a, '/')) == NULL)
337 				p = a;
338 			*p = 0;
339 			strlcat(pr->anchor_call, "../",
340 			    sizeof(pr->anchor_call));
341 		}
342 		if (strncmp(a, r->anchor->path, strlen(a))) {
343 			DPFPRINTF(LOG_NOTICE,
344 			    "pf_anchor_copyout: '%s' '%s'", a,
345 			    r->anchor->path);
346 			rs_free(a, MAXPATHLEN);
347 			return (1);
348 		}
349 		if (strlen(r->anchor->path) > strlen(a))
350 			strlcat(pr->anchor_call, r->anchor->path + (a[0] ?
351 			    strlen(a) + 1 : 0), sizeof(pr->anchor_call));
352 		rs_free(a, MAXPATHLEN);
353 	}
354 	if (r->anchor_wildcard)
355 		strlcat(pr->anchor_call, pr->anchor_call[0] ? "/*" : "*",
356 		    sizeof(pr->anchor_call));
357 	return (0);
358 }
359 
360 void
361 pf_anchor_remove(struct pf_rule *r)
362 {
363 	if (r->anchor == NULL)
364 		return;
365 	if (r->anchor->refcnt <= 0) {
366 		DPFPRINTF(LOG_NOTICE,
367 		    "pf_anchor_remove: broken refcount");
368 		r->anchor = NULL;
369 		return;
370 	}
371 	if (!--r->anchor->refcnt)
372 		pf_remove_if_empty_ruleset(&r->anchor->ruleset);
373 	r->anchor = NULL;
374 }
375