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