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