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