]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/pfctl/pfctl.c
MFC r333084:
[FreeBSD/FreeBSD.git] / sbin / pfctl / pfctl.c
1 /*      $OpenBSD: pfctl.c,v 1.278 2008/08/31 20:18:17 jmc Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2001 Daniel Hartmeier
7  * Copyright (c) 2002,2003 Henning Brauer
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  *    - Redistributions of source code must retain the above copyright
15  *      notice, this list of conditions and the following disclaimer.
16  *    - Redistributions in binary form must reproduce the above
17  *      copyright notice, this list of conditions and the following
18  *      disclaimer in the documentation and/or other materials provided
19  *      with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <sys/endian.h>
44
45 #include <net/if.h>
46 #include <netinet/in.h>
47 #include <net/pfvar.h>
48 #include <arpa/inet.h>
49 #include <net/altq/altq.h>
50 #include <sys/sysctl.h>
51
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <limits.h>
56 #include <netdb.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "pfctl_parser.h"
64 #include "pfctl.h"
65
66 void     usage(void);
67 int      pfctl_enable(int, int);
68 int      pfctl_disable(int, int);
69 int      pfctl_clear_stats(int, int);
70 int      pfctl_get_skip_ifaces(void);
71 int      pfctl_check_skip_ifaces(char *);
72 int      pfctl_clear_skip_ifaces(struct pfctl *);
73 int      pfctl_clear_interface_flags(int, int);
74 int      pfctl_clear_rules(int, int, char *);
75 int      pfctl_clear_nat(int, int, char *);
76 int      pfctl_clear_altq(int, int);
77 int      pfctl_clear_src_nodes(int, int);
78 int      pfctl_clear_states(int, const char *, int);
79 void     pfctl_addrprefix(char *, struct pf_addr *);
80 int      pfctl_kill_src_nodes(int, const char *, int);
81 int      pfctl_net_kill_states(int, const char *, int);
82 int      pfctl_label_kill_states(int, const char *, int);
83 int      pfctl_id_kill_states(int, const char *, int);
84 void     pfctl_init_options(struct pfctl *);
85 int      pfctl_load_options(struct pfctl *);
86 int      pfctl_load_limit(struct pfctl *, unsigned int, unsigned int);
87 int      pfctl_load_timeout(struct pfctl *, unsigned int, unsigned int);
88 int      pfctl_load_debug(struct pfctl *, unsigned int);
89 int      pfctl_load_logif(struct pfctl *, char *);
90 int      pfctl_load_hostid(struct pfctl *, unsigned int);
91 int      pfctl_get_pool(int, struct pf_pool *, u_int32_t, u_int32_t, int,
92             char *);
93 void     pfctl_print_rule_counters(struct pf_rule *, int);
94 int      pfctl_show_rules(int, char *, int, enum pfctl_show, char *, int);
95 int      pfctl_show_nat(int, int, char *);
96 int      pfctl_show_src_nodes(int, int);
97 int      pfctl_show_states(int, const char *, int);
98 int      pfctl_show_status(int, int);
99 int      pfctl_show_timeouts(int, int);
100 int      pfctl_show_limits(int, int);
101 void     pfctl_debug(int, u_int32_t, int);
102 int      pfctl_test_altqsupport(int, int);
103 int      pfctl_show_anchors(int, int, char *);
104 int      pfctl_ruleset_trans(struct pfctl *, char *, struct pf_anchor *);
105 int      pfctl_load_ruleset(struct pfctl *, char *,
106                 struct pf_ruleset *, int, int);
107 int      pfctl_load_rule(struct pfctl *, char *, struct pf_rule *, int);
108 const char      *pfctl_lookup_option(char *, const char **);
109
110 struct pf_anchor_global  pf_anchors;
111 struct pf_anchor         pf_main_anchor;
112 static struct pfr_buffer skip_b;
113
114 const char      *clearopt;
115 char            *rulesopt;
116 const char      *showopt;
117 const char      *debugopt;
118 char            *anchoropt;
119 const char      *optiopt = NULL;
120 char            *pf_device = "/dev/pf";
121 char            *ifaceopt;
122 char            *tableopt;
123 const char      *tblcmdopt;
124 int              src_node_killers;
125 char            *src_node_kill[2];
126 int              state_killers;
127 char            *state_kill[2];
128 int              loadopt;
129 int              altqsupport;
130
131 int              dev = -1;
132 int              first_title = 1;
133 int              labels = 0;
134
135 #define INDENT(d, o)    do {                                            \
136                                 if (o) {                                \
137                                         int i;                          \
138                                         for (i=0; i < d; i++)           \
139                                                 printf("  ");           \
140                                 }                                       \
141                         } while (0);                                    \
142
143
144 static const struct {
145         const char      *name;
146         int             index;
147 } pf_limits[] = {
148         { "states",             PF_LIMIT_STATES },
149         { "src-nodes",          PF_LIMIT_SRC_NODES },
150         { "frags",              PF_LIMIT_FRAGS },
151         { "table-entries",      PF_LIMIT_TABLE_ENTRIES },
152         { NULL,                 0 }
153 };
154
155 struct pf_hint {
156         const char      *name;
157         int             timeout;
158 };
159 static const struct pf_hint pf_hint_normal[] = {
160         { "tcp.first",          2 * 60 },
161         { "tcp.opening",        30 },
162         { "tcp.established",    24 * 60 * 60 },
163         { "tcp.closing",        15 * 60 },
164         { "tcp.finwait",        45 },
165         { "tcp.closed",         90 },
166         { "tcp.tsdiff",         30 },
167         { NULL,                 0 }
168 };
169 static const struct pf_hint pf_hint_satellite[] = {
170         { "tcp.first",          3 * 60 },
171         { "tcp.opening",        30 + 5 },
172         { "tcp.established",    24 * 60 * 60 },
173         { "tcp.closing",        15 * 60 + 5 },
174         { "tcp.finwait",        45 + 5 },
175         { "tcp.closed",         90 + 5 },
176         { "tcp.tsdiff",         60 },
177         { NULL,                 0 }
178 };
179 static const struct pf_hint pf_hint_conservative[] = {
180         { "tcp.first",          60 * 60 },
181         { "tcp.opening",        15 * 60 },
182         { "tcp.established",    5 * 24 * 60 * 60 },
183         { "tcp.closing",        60 * 60 },
184         { "tcp.finwait",        10 * 60 },
185         { "tcp.closed",         3 * 60 },
186         { "tcp.tsdiff",         60 },
187         { NULL,                 0 }
188 };
189 static const struct pf_hint pf_hint_aggressive[] = {
190         { "tcp.first",          30 },
191         { "tcp.opening",        5 },
192         { "tcp.established",    5 * 60 * 60 },
193         { "tcp.closing",        60 },
194         { "tcp.finwait",        30 },
195         { "tcp.closed",         30 },
196         { "tcp.tsdiff",         10 },
197         { NULL,                 0 }
198 };
199
200 static const struct {
201         const char *name;
202         const struct pf_hint *hint;
203 } pf_hints[] = {
204         { "normal",             pf_hint_normal },
205         { "satellite",          pf_hint_satellite },
206         { "high-latency",       pf_hint_satellite },
207         { "conservative",       pf_hint_conservative },
208         { "aggressive",         pf_hint_aggressive },
209         { NULL,                 NULL }
210 };
211
212 static const char *clearopt_list[] = {
213         "nat", "queue", "rules", "Sources",
214         "states", "info", "Tables", "osfp", "all", NULL
215 };
216
217 static const char *showopt_list[] = {
218         "nat", "queue", "rules", "Anchors", "Sources", "states", "info",
219         "Interfaces", "labels", "timeouts", "memory", "Tables", "osfp",
220         "all", NULL
221 };
222
223 static const char *tblcmdopt_list[] = {
224         "kill", "flush", "add", "delete", "load", "replace", "show",
225         "test", "zero", "expire", NULL
226 };
227
228 static const char *debugopt_list[] = {
229         "none", "urgent", "misc", "loud", NULL
230 };
231
232 static const char *optiopt_list[] = {
233         "none", "basic", "profile", NULL
234 };
235
236 void
237 usage(void)
238 {
239         extern char *__progname;
240
241         fprintf(stderr,
242 "usage: %s [-AdeghmNnOPqRrvz] [-a anchor] [-D macro=value] [-F modifier]\n"
243         "\t[-f file] [-i interface] [-K host | network]\n"
244         "\t[-k host | network | label | id] [-o level] [-p device]\n"
245         "\t[-s modifier] [-t table -T command [address ...]] [-x level]\n",
246             __progname);
247
248         exit(1);
249 }
250
251 int
252 pfctl_enable(int dev, int opts)
253 {
254         if (ioctl(dev, DIOCSTART)) {
255                 if (errno == EEXIST)
256                         errx(1, "pf already enabled");
257                 else if (errno == ESRCH)
258                         errx(1, "pfil registeration failed");
259                 else
260                         err(1, "DIOCSTART");
261         }
262         if ((opts & PF_OPT_QUIET) == 0)
263                 fprintf(stderr, "pf enabled\n");
264
265         if (altqsupport && ioctl(dev, DIOCSTARTALTQ))
266                 if (errno != EEXIST)
267                         err(1, "DIOCSTARTALTQ");
268
269         return (0);
270 }
271
272 int
273 pfctl_disable(int dev, int opts)
274 {
275         if (ioctl(dev, DIOCSTOP)) {
276                 if (errno == ENOENT)
277                         errx(1, "pf not enabled");
278                 else
279                         err(1, "DIOCSTOP");
280         }
281         if ((opts & PF_OPT_QUIET) == 0)
282                 fprintf(stderr, "pf disabled\n");
283
284         if (altqsupport && ioctl(dev, DIOCSTOPALTQ))
285                         if (errno != ENOENT)
286                                 err(1, "DIOCSTOPALTQ");
287
288         return (0);
289 }
290
291 int
292 pfctl_clear_stats(int dev, int opts)
293 {
294         if (ioctl(dev, DIOCCLRSTATUS))
295                 err(1, "DIOCCLRSTATUS");
296         if ((opts & PF_OPT_QUIET) == 0)
297                 fprintf(stderr, "pf: statistics cleared\n");
298         return (0);
299 }
300
301 int
302 pfctl_get_skip_ifaces(void)
303 {
304         bzero(&skip_b, sizeof(skip_b));
305         skip_b.pfrb_type = PFRB_IFACES;
306         for (;;) {
307                 pfr_buf_grow(&skip_b, skip_b.pfrb_size);
308                 skip_b.pfrb_size = skip_b.pfrb_msize;
309                 if (pfi_get_ifaces(NULL, skip_b.pfrb_caddr, &skip_b.pfrb_size))
310                         err(1, "pfi_get_ifaces");
311                 if (skip_b.pfrb_size <= skip_b.pfrb_msize)
312                         break;
313         }
314         return (0);
315 }
316
317 int
318 pfctl_check_skip_ifaces(char *ifname)
319 {
320         struct pfi_kif          *p;
321
322         PFRB_FOREACH(p, &skip_b)
323                 if ((p->pfik_flags & PFI_IFLAG_SKIP) && !strcmp(ifname, p->pfik_name))
324                         p->pfik_flags &= ~PFI_IFLAG_SKIP;
325         return (0);
326 }
327
328 int
329 pfctl_clear_skip_ifaces(struct pfctl *pf)
330 {
331         struct pfi_kif          *p;
332
333         PFRB_FOREACH(p, &skip_b)
334                 if (p->pfik_flags & PFI_IFLAG_SKIP)
335                         pfctl_set_interface_flags(pf, p->pfik_name, PFI_IFLAG_SKIP, 0);
336         return (0);
337 }
338
339 int
340 pfctl_clear_interface_flags(int dev, int opts)
341 {
342         struct pfioc_iface      pi;
343
344         if ((opts & PF_OPT_NOACTION) == 0) {
345                 bzero(&pi, sizeof(pi));
346                 pi.pfiio_flags = PFI_IFLAG_SKIP;
347
348                 if (ioctl(dev, DIOCCLRIFFLAG, &pi))
349                         err(1, "DIOCCLRIFFLAG");
350                 if ((opts & PF_OPT_QUIET) == 0)
351                         fprintf(stderr, "pf: interface flags reset\n");
352         }
353         return (0);
354 }
355
356 int
357 pfctl_clear_rules(int dev, int opts, char *anchorname)
358 {
359         struct pfr_buffer t;
360
361         memset(&t, 0, sizeof(t));
362         t.pfrb_type = PFRB_TRANS;
363         if (pfctl_add_trans(&t, PF_RULESET_SCRUB, anchorname) ||
364             pfctl_add_trans(&t, PF_RULESET_FILTER, anchorname) ||
365             pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
366             pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
367                 err(1, "pfctl_clear_rules");
368         if ((opts & PF_OPT_QUIET) == 0)
369                 fprintf(stderr, "rules cleared\n");
370         return (0);
371 }
372
373 int
374 pfctl_clear_nat(int dev, int opts, char *anchorname)
375 {
376         struct pfr_buffer t;
377
378         memset(&t, 0, sizeof(t));
379         t.pfrb_type = PFRB_TRANS;
380         if (pfctl_add_trans(&t, PF_RULESET_NAT, anchorname) ||
381             pfctl_add_trans(&t, PF_RULESET_BINAT, anchorname) ||
382             pfctl_add_trans(&t, PF_RULESET_RDR, anchorname) ||
383             pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
384             pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
385                 err(1, "pfctl_clear_nat");
386         if ((opts & PF_OPT_QUIET) == 0)
387                 fprintf(stderr, "nat cleared\n");
388         return (0);
389 }
390
391 int
392 pfctl_clear_altq(int dev, int opts)
393 {
394         struct pfr_buffer t;
395
396         if (!altqsupport)
397                 return (-1);
398         memset(&t, 0, sizeof(t));
399         t.pfrb_type = PFRB_TRANS;
400         if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "") ||
401             pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
402             pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
403                 err(1, "pfctl_clear_altq");
404         if ((opts & PF_OPT_QUIET) == 0)
405                 fprintf(stderr, "altq cleared\n");
406         return (0);
407 }
408
409 int
410 pfctl_clear_src_nodes(int dev, int opts)
411 {
412         if (ioctl(dev, DIOCCLRSRCNODES))
413                 err(1, "DIOCCLRSRCNODES");
414         if ((opts & PF_OPT_QUIET) == 0)
415                 fprintf(stderr, "source tracking entries cleared\n");
416         return (0);
417 }
418
419 int
420 pfctl_clear_states(int dev, const char *iface, int opts)
421 {
422         struct pfioc_state_kill psk;
423
424         memset(&psk, 0, sizeof(psk));
425         if (iface != NULL && strlcpy(psk.psk_ifname, iface,
426             sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
427                 errx(1, "invalid interface: %s", iface);
428
429         if (ioctl(dev, DIOCCLRSTATES, &psk))
430                 err(1, "DIOCCLRSTATES");
431         if ((opts & PF_OPT_QUIET) == 0)
432                 fprintf(stderr, "%d states cleared\n", psk.psk_killed);
433         return (0);
434 }
435
436 void
437 pfctl_addrprefix(char *addr, struct pf_addr *mask)
438 {
439         char *p;
440         const char *errstr;
441         int prefix, ret_ga, q, r;
442         struct addrinfo hints, *res;
443
444         if ((p = strchr(addr, '/')) == NULL)
445                 return;
446
447         *p++ = '\0';
448         prefix = strtonum(p, 0, 128, &errstr);
449         if (errstr)
450                 errx(1, "prefix is %s: %s", errstr, p);
451
452         bzero(&hints, sizeof(hints));
453         /* prefix only with numeric addresses */
454         hints.ai_flags |= AI_NUMERICHOST;
455
456         if ((ret_ga = getaddrinfo(addr, NULL, &hints, &res))) {
457                 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
458                 /* NOTREACHED */
459         }
460
461         if (res->ai_family == AF_INET && prefix > 32)
462                 errx(1, "prefix too long for AF_INET");
463         else if (res->ai_family == AF_INET6 && prefix > 128)
464                 errx(1, "prefix too long for AF_INET6");
465
466         q = prefix >> 3;
467         r = prefix & 7;
468         switch (res->ai_family) {
469         case AF_INET:
470                 bzero(&mask->v4, sizeof(mask->v4));
471                 mask->v4.s_addr = htonl((u_int32_t)
472                     (0xffffffffffULL << (32 - prefix)));
473                 break;
474         case AF_INET6:
475                 bzero(&mask->v6, sizeof(mask->v6));
476                 if (q > 0)
477                         memset((void *)&mask->v6, 0xff, q);
478                 if (r > 0)
479                         *((u_char *)&mask->v6 + q) =
480                             (0xff00 >> r) & 0xff;
481                 break;
482         }
483         freeaddrinfo(res);
484 }
485
486 int
487 pfctl_kill_src_nodes(int dev, const char *iface, int opts)
488 {
489         struct pfioc_src_node_kill psnk;
490         struct addrinfo *res[2], *resp[2];
491         struct sockaddr last_src, last_dst;
492         int killed, sources, dests;
493         int ret_ga;
494
495         killed = sources = dests = 0;
496
497         memset(&psnk, 0, sizeof(psnk));
498         memset(&psnk.psnk_src.addr.v.a.mask, 0xff,
499             sizeof(psnk.psnk_src.addr.v.a.mask));
500         memset(&last_src, 0xff, sizeof(last_src));
501         memset(&last_dst, 0xff, sizeof(last_dst));
502
503         pfctl_addrprefix(src_node_kill[0], &psnk.psnk_src.addr.v.a.mask);
504
505         if ((ret_ga = getaddrinfo(src_node_kill[0], NULL, NULL, &res[0]))) {
506                 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
507                 /* NOTREACHED */
508         }
509         for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
510                 if (resp[0]->ai_addr == NULL)
511                         continue;
512                 /* We get lots of duplicates.  Catch the easy ones */
513                 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
514                         continue;
515                 last_src = *(struct sockaddr *)resp[0]->ai_addr;
516
517                 psnk.psnk_af = resp[0]->ai_family;
518                 sources++;
519
520                 if (psnk.psnk_af == AF_INET)
521                         psnk.psnk_src.addr.v.a.addr.v4 =
522                             ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
523                 else if (psnk.psnk_af == AF_INET6)
524                         psnk.psnk_src.addr.v.a.addr.v6 =
525                             ((struct sockaddr_in6 *)resp[0]->ai_addr)->
526                             sin6_addr;
527                 else
528                         errx(1, "Unknown address family %d", psnk.psnk_af);
529
530                 if (src_node_killers > 1) {
531                         dests = 0;
532                         memset(&psnk.psnk_dst.addr.v.a.mask, 0xff,
533                             sizeof(psnk.psnk_dst.addr.v.a.mask));
534                         memset(&last_dst, 0xff, sizeof(last_dst));
535                         pfctl_addrprefix(src_node_kill[1],
536                             &psnk.psnk_dst.addr.v.a.mask);
537                         if ((ret_ga = getaddrinfo(src_node_kill[1], NULL, NULL,
538                             &res[1]))) {
539                                 errx(1, "getaddrinfo: %s",
540                                     gai_strerror(ret_ga));
541                                 /* NOTREACHED */
542                         }
543                         for (resp[1] = res[1]; resp[1];
544                             resp[1] = resp[1]->ai_next) {
545                                 if (resp[1]->ai_addr == NULL)
546                                         continue;
547                                 if (psnk.psnk_af != resp[1]->ai_family)
548                                         continue;
549
550                                 if (memcmp(&last_dst, resp[1]->ai_addr,
551                                     sizeof(last_dst)) == 0)
552                                         continue;
553                                 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
554
555                                 dests++;
556
557                                 if (psnk.psnk_af == AF_INET)
558                                         psnk.psnk_dst.addr.v.a.addr.v4 =
559                                             ((struct sockaddr_in *)resp[1]->
560                                             ai_addr)->sin_addr;
561                                 else if (psnk.psnk_af == AF_INET6)
562                                         psnk.psnk_dst.addr.v.a.addr.v6 =
563                                             ((struct sockaddr_in6 *)resp[1]->
564                                             ai_addr)->sin6_addr;
565                                 else
566                                         errx(1, "Unknown address family %d",
567                                             psnk.psnk_af);
568
569                                 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
570                                         err(1, "DIOCKILLSRCNODES");
571                                 killed += psnk.psnk_killed;
572                         }
573                         freeaddrinfo(res[1]);
574                 } else {
575                         if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
576                                 err(1, "DIOCKILLSRCNODES");
577                         killed += psnk.psnk_killed;
578                 }
579         }
580
581         freeaddrinfo(res[0]);
582
583         if ((opts & PF_OPT_QUIET) == 0)
584                 fprintf(stderr, "killed %d src nodes from %d sources and %d "
585                     "destinations\n", killed, sources, dests);
586         return (0);
587 }
588
589 int
590 pfctl_net_kill_states(int dev, const char *iface, int opts)
591 {
592         struct pfioc_state_kill psk;
593         struct addrinfo *res[2], *resp[2];
594         struct sockaddr last_src, last_dst;
595         int killed, sources, dests;
596         int ret_ga;
597
598         killed = sources = dests = 0;
599
600         memset(&psk, 0, sizeof(psk));
601         memset(&psk.psk_src.addr.v.a.mask, 0xff,
602             sizeof(psk.psk_src.addr.v.a.mask));
603         memset(&last_src, 0xff, sizeof(last_src));
604         memset(&last_dst, 0xff, sizeof(last_dst));
605         if (iface != NULL && strlcpy(psk.psk_ifname, iface,
606             sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
607                 errx(1, "invalid interface: %s", iface);
608
609         pfctl_addrprefix(state_kill[0], &psk.psk_src.addr.v.a.mask);
610
611         if ((ret_ga = getaddrinfo(state_kill[0], NULL, NULL, &res[0]))) {
612                 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
613                 /* NOTREACHED */
614         }
615         for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
616                 if (resp[0]->ai_addr == NULL)
617                         continue;
618                 /* We get lots of duplicates.  Catch the easy ones */
619                 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
620                         continue;
621                 last_src = *(struct sockaddr *)resp[0]->ai_addr;
622
623                 psk.psk_af = resp[0]->ai_family;
624                 sources++;
625
626                 if (psk.psk_af == AF_INET)
627                         psk.psk_src.addr.v.a.addr.v4 =
628                             ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
629                 else if (psk.psk_af == AF_INET6)
630                         psk.psk_src.addr.v.a.addr.v6 =
631                             ((struct sockaddr_in6 *)resp[0]->ai_addr)->
632                             sin6_addr;
633                 else
634                         errx(1, "Unknown address family %d", psk.psk_af);
635
636                 if (state_killers > 1) {
637                         dests = 0;
638                         memset(&psk.psk_dst.addr.v.a.mask, 0xff,
639                             sizeof(psk.psk_dst.addr.v.a.mask));
640                         memset(&last_dst, 0xff, sizeof(last_dst));
641                         pfctl_addrprefix(state_kill[1],
642                             &psk.psk_dst.addr.v.a.mask);
643                         if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL,
644                             &res[1]))) {
645                                 errx(1, "getaddrinfo: %s",
646                                     gai_strerror(ret_ga));
647                                 /* NOTREACHED */
648                         }
649                         for (resp[1] = res[1]; resp[1];
650                             resp[1] = resp[1]->ai_next) {
651                                 if (resp[1]->ai_addr == NULL)
652                                         continue;
653                                 if (psk.psk_af != resp[1]->ai_family)
654                                         continue;
655
656                                 if (memcmp(&last_dst, resp[1]->ai_addr,
657                                     sizeof(last_dst)) == 0)
658                                         continue;
659                                 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
660
661                                 dests++;
662
663                                 if (psk.psk_af == AF_INET)
664                                         psk.psk_dst.addr.v.a.addr.v4 =
665                                             ((struct sockaddr_in *)resp[1]->
666                                             ai_addr)->sin_addr;
667                                 else if (psk.psk_af == AF_INET6)
668                                         psk.psk_dst.addr.v.a.addr.v6 =
669                                             ((struct sockaddr_in6 *)resp[1]->
670                                             ai_addr)->sin6_addr;
671                                 else
672                                         errx(1, "Unknown address family %d",
673                                             psk.psk_af);
674
675                                 if (ioctl(dev, DIOCKILLSTATES, &psk))
676                                         err(1, "DIOCKILLSTATES");
677                                 killed += psk.psk_killed;
678                         }
679                         freeaddrinfo(res[1]);
680                 } else {
681                         if (ioctl(dev, DIOCKILLSTATES, &psk))
682                                 err(1, "DIOCKILLSTATES");
683                         killed += psk.psk_killed;
684                 }
685         }
686
687         freeaddrinfo(res[0]);
688
689         if ((opts & PF_OPT_QUIET) == 0)
690                 fprintf(stderr, "killed %d states from %d sources and %d "
691                     "destinations\n", killed, sources, dests);
692         return (0);
693 }
694
695 int
696 pfctl_label_kill_states(int dev, const char *iface, int opts)
697 {
698         struct pfioc_state_kill psk;
699
700         if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
701                 warnx("no label specified");
702                 usage();
703         }
704         memset(&psk, 0, sizeof(psk));
705         if (iface != NULL && strlcpy(psk.psk_ifname, iface,
706             sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
707                 errx(1, "invalid interface: %s", iface);
708
709         if (strlcpy(psk.psk_label, state_kill[1], sizeof(psk.psk_label)) >=
710             sizeof(psk.psk_label))
711                 errx(1, "label too long: %s", state_kill[1]);
712
713         if (ioctl(dev, DIOCKILLSTATES, &psk))
714                 err(1, "DIOCKILLSTATES");
715
716         if ((opts & PF_OPT_QUIET) == 0)
717                 fprintf(stderr, "killed %d states\n", psk.psk_killed);
718
719         return (0);
720 }
721
722 int
723 pfctl_id_kill_states(int dev, const char *iface, int opts)
724 {
725         struct pfioc_state_kill psk;
726         
727         if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
728                 warnx("no id specified");
729                 usage();
730         }
731
732         memset(&psk, 0, sizeof(psk));
733         if ((sscanf(state_kill[1], "%jx/%x",
734             &psk.psk_pfcmp.id, &psk.psk_pfcmp.creatorid)) == 2)
735                 HTONL(psk.psk_pfcmp.creatorid);
736         else if ((sscanf(state_kill[1], "%jx", &psk.psk_pfcmp.id)) == 1) {
737                 psk.psk_pfcmp.creatorid = 0;
738         } else {
739                 warnx("wrong id format specified");
740                 usage();
741         }
742         if (psk.psk_pfcmp.id == 0) {
743                 warnx("cannot kill id 0");
744                 usage();
745         }
746
747         psk.psk_pfcmp.id = htobe64(psk.psk_pfcmp.id);
748         if (ioctl(dev, DIOCKILLSTATES, &psk))
749                 err(1, "DIOCKILLSTATES");
750
751         if ((opts & PF_OPT_QUIET) == 0)
752                 fprintf(stderr, "killed %d states\n", psk.psk_killed);
753
754         return (0);
755 }
756
757 int
758 pfctl_get_pool(int dev, struct pf_pool *pool, u_int32_t nr,
759     u_int32_t ticket, int r_action, char *anchorname)
760 {
761         struct pfioc_pooladdr pp;
762         struct pf_pooladdr *pa;
763         u_int32_t pnr, mpnr;
764
765         memset(&pp, 0, sizeof(pp));
766         memcpy(pp.anchor, anchorname, sizeof(pp.anchor));
767         pp.r_action = r_action;
768         pp.r_num = nr;
769         pp.ticket = ticket;
770         if (ioctl(dev, DIOCGETADDRS, &pp)) {
771                 warn("DIOCGETADDRS");
772                 return (-1);
773         }
774         mpnr = pp.nr;
775         TAILQ_INIT(&pool->list);
776         for (pnr = 0; pnr < mpnr; ++pnr) {
777                 pp.nr = pnr;
778                 if (ioctl(dev, DIOCGETADDR, &pp)) {
779                         warn("DIOCGETADDR");
780                         return (-1);
781                 }
782                 pa = calloc(1, sizeof(struct pf_pooladdr));
783                 if (pa == NULL)
784                         err(1, "calloc");
785                 bcopy(&pp.addr, pa, sizeof(struct pf_pooladdr));
786                 TAILQ_INSERT_TAIL(&pool->list, pa, entries);
787         }
788
789         return (0);
790 }
791
792 void
793 pfctl_move_pool(struct pf_pool *src, struct pf_pool *dst)
794 {
795         struct pf_pooladdr *pa;
796
797         while ((pa = TAILQ_FIRST(&src->list)) != NULL) {
798                 TAILQ_REMOVE(&src->list, pa, entries);
799                 TAILQ_INSERT_TAIL(&dst->list, pa, entries);
800         }
801 }
802
803 void
804 pfctl_clear_pool(struct pf_pool *pool)
805 {
806         struct pf_pooladdr *pa;
807
808         while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
809                 TAILQ_REMOVE(&pool->list, pa, entries);
810                 free(pa);
811         }
812 }
813
814 void
815 pfctl_print_rule_counters(struct pf_rule *rule, int opts)
816 {
817         if (opts & PF_OPT_DEBUG) {
818                 const char *t[PF_SKIP_COUNT] = { "i", "d", "f",
819                     "p", "sa", "sp", "da", "dp" };
820                 int i;
821
822                 printf("  [ Skip steps: ");
823                 for (i = 0; i < PF_SKIP_COUNT; ++i) {
824                         if (rule->skip[i].nr == rule->nr + 1)
825                                 continue;
826                         printf("%s=", t[i]);
827                         if (rule->skip[i].nr == -1)
828                                 printf("end ");
829                         else
830                                 printf("%u ", rule->skip[i].nr);
831                 }
832                 printf("]\n");
833
834                 printf("  [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n",
835                     rule->qname, rule->qid, rule->pqname, rule->pqid);
836         }
837         if (opts & PF_OPT_VERBOSE) {
838                 printf("  [ Evaluations: %-8llu  Packets: %-8llu  "
839                             "Bytes: %-10llu  States: %-6ju]\n",
840                             (unsigned long long)rule->evaluations,
841                             (unsigned long long)(rule->packets[0] +
842                             rule->packets[1]),
843                             (unsigned long long)(rule->bytes[0] +
844                             rule->bytes[1]), (uintmax_t)rule->u_states_cur);
845                 if (!(opts & PF_OPT_DEBUG))
846                         printf("  [ Inserted: uid %u pid %u "
847                             "State Creations: %-6ju]\n",
848                             (unsigned)rule->cuid, (unsigned)rule->cpid,
849                             (uintmax_t)rule->u_states_tot);
850         }
851 }
852
853 void
854 pfctl_print_title(char *title)
855 {
856         if (!first_title)
857                 printf("\n");
858         first_title = 0;
859         printf("%s\n", title);
860 }
861
862 int
863 pfctl_show_rules(int dev, char *path, int opts, enum pfctl_show format,
864     char *anchorname, int depth)
865 {
866         struct pfioc_rule pr;
867         u_int32_t nr, mnr, header = 0;
868         int rule_numbers = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG);
869         int numeric = opts & PF_OPT_NUMERIC;
870         int len = strlen(path);
871         int brace;
872         char *p;
873
874         if (path[0])
875                 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname);
876         else
877                 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname);
878
879         memset(&pr, 0, sizeof(pr));
880         memcpy(pr.anchor, path, sizeof(pr.anchor));
881         if (opts & PF_OPT_SHOWALL) {
882                 pr.rule.action = PF_PASS;
883                 if (ioctl(dev, DIOCGETRULES, &pr)) {
884                         warn("DIOCGETRULES");
885                         goto error;
886                 }
887                 header++;
888         }
889         pr.rule.action = PF_SCRUB;
890         if (ioctl(dev, DIOCGETRULES, &pr)) {
891                 warn("DIOCGETRULES");
892                 goto error;
893         }
894         if (opts & PF_OPT_SHOWALL) {
895                 if (format == PFCTL_SHOW_RULES && (pr.nr > 0 || header))
896                         pfctl_print_title("FILTER RULES:");
897                 else if (format == PFCTL_SHOW_LABELS && labels)
898                         pfctl_print_title("LABEL COUNTERS:");
899         }
900         mnr = pr.nr;
901         if (opts & PF_OPT_CLRRULECTRS)
902                 pr.action = PF_GET_CLR_CNTR;
903
904         for (nr = 0; nr < mnr; ++nr) {
905                 pr.nr = nr;
906                 if (ioctl(dev, DIOCGETRULE, &pr)) {
907                         warn("DIOCGETRULE");
908                         goto error;
909                 }
910
911                 if (pfctl_get_pool(dev, &pr.rule.rpool,
912                     nr, pr.ticket, PF_SCRUB, path) != 0)
913                         goto error;
914
915                 switch (format) {
916                 case PFCTL_SHOW_LABELS:
917                         break;
918                 case PFCTL_SHOW_RULES:
919                         if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
920                                 labels = 1;
921                         print_rule(&pr.rule, pr.anchor_call, rule_numbers, numeric);
922                         printf("\n");
923                         pfctl_print_rule_counters(&pr.rule, opts);
924                         break;
925                 case PFCTL_SHOW_NOTHING:
926                         break;
927                 }
928                 pfctl_clear_pool(&pr.rule.rpool);
929         }
930         pr.rule.action = PF_PASS;
931         if (ioctl(dev, DIOCGETRULES, &pr)) {
932                 warn("DIOCGETRULES");
933                 goto error;
934         }
935         mnr = pr.nr;
936         for (nr = 0; nr < mnr; ++nr) {
937                 pr.nr = nr;
938                 if (ioctl(dev, DIOCGETRULE, &pr)) {
939                         warn("DIOCGETRULE");
940                         goto error;
941                 }
942
943                 if (pfctl_get_pool(dev, &pr.rule.rpool,
944                     nr, pr.ticket, PF_PASS, path) != 0)
945                         goto error;
946
947                 switch (format) {
948                 case PFCTL_SHOW_LABELS:
949                         if (pr.rule.label[0]) {
950                                 printf("%s %llu %llu %llu %llu"
951                                     " %llu %llu %llu %ju\n",
952                                     pr.rule.label,
953                                     (unsigned long long)pr.rule.evaluations,
954                                     (unsigned long long)(pr.rule.packets[0] +
955                                     pr.rule.packets[1]),
956                                     (unsigned long long)(pr.rule.bytes[0] +
957                                     pr.rule.bytes[1]),
958                                     (unsigned long long)pr.rule.packets[0],
959                                     (unsigned long long)pr.rule.bytes[0],
960                                     (unsigned long long)pr.rule.packets[1],
961                                     (unsigned long long)pr.rule.bytes[1],
962                                     (uintmax_t)pr.rule.u_states_tot);
963                         }
964                         break;
965                 case PFCTL_SHOW_RULES:
966                         brace = 0;
967                         if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
968                                 labels = 1;
969                         INDENT(depth, !(opts & PF_OPT_VERBOSE));
970                         if (pr.anchor_call[0] &&
971                            ((((p = strrchr(pr.anchor_call, '_')) != NULL) &&
972                            ((void *)p == (void *)pr.anchor_call ||
973                            *(--p) == '/')) || (opts & PF_OPT_RECURSE))) {
974                                 brace++;
975                                 if ((p = strrchr(pr.anchor_call, '/')) !=
976                                     NULL)
977                                         p++;
978                                 else
979                                         p = &pr.anchor_call[0];
980                         } else
981                                 p = &pr.anchor_call[0];
982                 
983                         print_rule(&pr.rule, p, rule_numbers, numeric);
984                         if (brace)
985                                 printf(" {\n");
986                         else
987                                 printf("\n");
988                         pfctl_print_rule_counters(&pr.rule, opts);
989                         if (brace) { 
990                                 pfctl_show_rules(dev, path, opts, format,
991                                     p, depth + 1);
992                                 INDENT(depth, !(opts & PF_OPT_VERBOSE));
993                                 printf("}\n");
994                         }
995                         break;
996                 case PFCTL_SHOW_NOTHING:
997                         break;
998                 }
999                 pfctl_clear_pool(&pr.rule.rpool);
1000         }
1001         path[len] = '\0';
1002         return (0);
1003
1004  error:
1005         path[len] = '\0';
1006         return (-1);
1007 }
1008
1009 int
1010 pfctl_show_nat(int dev, int opts, char *anchorname)
1011 {
1012         struct pfioc_rule pr;
1013         u_int32_t mnr, nr;
1014         static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT };
1015         int i, dotitle = opts & PF_OPT_SHOWALL;
1016
1017         memset(&pr, 0, sizeof(pr));
1018         memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
1019         for (i = 0; i < 3; i++) {
1020                 pr.rule.action = nattype[i];
1021                 if (ioctl(dev, DIOCGETRULES, &pr)) {
1022                         warn("DIOCGETRULES");
1023                         return (-1);
1024                 }
1025                 mnr = pr.nr;
1026                 for (nr = 0; nr < mnr; ++nr) {
1027                         pr.nr = nr;
1028                         if (ioctl(dev, DIOCGETRULE, &pr)) {
1029                                 warn("DIOCGETRULE");
1030                                 return (-1);
1031                         }
1032                         if (pfctl_get_pool(dev, &pr.rule.rpool, nr,
1033                             pr.ticket, nattype[i], anchorname) != 0)
1034                                 return (-1);
1035                         if (dotitle) {
1036                                 pfctl_print_title("TRANSLATION RULES:");
1037                                 dotitle = 0;
1038                         }
1039                         print_rule(&pr.rule, pr.anchor_call,
1040                             opts & PF_OPT_VERBOSE2, opts & PF_OPT_NUMERIC);
1041                         printf("\n");
1042                         pfctl_print_rule_counters(&pr.rule, opts);
1043                         pfctl_clear_pool(&pr.rule.rpool);
1044                 }
1045         }
1046         return (0);
1047 }
1048
1049 int
1050 pfctl_show_src_nodes(int dev, int opts)
1051 {
1052         struct pfioc_src_nodes psn;
1053         struct pf_src_node *p;
1054         char *inbuf = NULL, *newinbuf = NULL;
1055         unsigned int len = 0;
1056         int i;
1057
1058         memset(&psn, 0, sizeof(psn));
1059         for (;;) {
1060                 psn.psn_len = len;
1061                 if (len) {
1062                         newinbuf = realloc(inbuf, len);
1063                         if (newinbuf == NULL)
1064                                 err(1, "realloc");
1065                         psn.psn_buf = inbuf = newinbuf;
1066                 }
1067                 if (ioctl(dev, DIOCGETSRCNODES, &psn) < 0) {
1068                         warn("DIOCGETSRCNODES");
1069                         free(inbuf);
1070                         return (-1);
1071                 }
1072                 if (psn.psn_len + sizeof(struct pfioc_src_nodes) < len)
1073                         break;
1074                 if (len == 0 && psn.psn_len == 0)
1075                         goto done;
1076                 if (len == 0 && psn.psn_len != 0)
1077                         len = psn.psn_len;
1078                 if (psn.psn_len == 0)
1079                         goto done;      /* no src_nodes */
1080                 len *= 2;
1081         }
1082         p = psn.psn_src_nodes;
1083         if (psn.psn_len > 0 && (opts & PF_OPT_SHOWALL))
1084                 pfctl_print_title("SOURCE TRACKING NODES:");
1085         for (i = 0; i < psn.psn_len; i += sizeof(*p)) {
1086                 print_src_node(p, opts);
1087                 p++;
1088         }
1089 done:
1090         free(inbuf);
1091         return (0);
1092 }
1093
1094 int
1095 pfctl_show_states(int dev, const char *iface, int opts)
1096 {
1097         struct pfioc_states ps;
1098         struct pfsync_state *p;
1099         char *inbuf = NULL, *newinbuf = NULL;
1100         unsigned int len = 0;
1101         int i, dotitle = (opts & PF_OPT_SHOWALL);
1102
1103         memset(&ps, 0, sizeof(ps));
1104         for (;;) {
1105                 ps.ps_len = len;
1106                 if (len) {
1107                         newinbuf = realloc(inbuf, len);
1108                         if (newinbuf == NULL)
1109                                 err(1, "realloc");
1110                         ps.ps_buf = inbuf = newinbuf;
1111                 }
1112                 if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
1113                         warn("DIOCGETSTATES");
1114                         free(inbuf);
1115                         return (-1);
1116                 }
1117                 if (ps.ps_len + sizeof(struct pfioc_states) < len)
1118                         break;
1119                 if (len == 0 && ps.ps_len == 0)
1120                         goto done;
1121                 if (len == 0 && ps.ps_len != 0)
1122                         len = ps.ps_len;
1123                 if (ps.ps_len == 0)
1124                         goto done;      /* no states */
1125                 len *= 2;
1126         }
1127         p = ps.ps_states;
1128         for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
1129                 if (iface != NULL && strcmp(p->ifname, iface))
1130                         continue;
1131                 if (dotitle) {
1132                         pfctl_print_title("STATES:");
1133                         dotitle = 0;
1134                 }
1135                 print_state(p, opts);
1136         }
1137 done:
1138         free(inbuf);
1139         return (0);
1140 }
1141
1142 int
1143 pfctl_show_status(int dev, int opts)
1144 {
1145         struct pf_status status;
1146
1147         if (ioctl(dev, DIOCGETSTATUS, &status)) {
1148                 warn("DIOCGETSTATUS");
1149                 return (-1);
1150         }
1151         if (opts & PF_OPT_SHOWALL)
1152                 pfctl_print_title("INFO:");
1153         print_status(&status, opts);
1154         return (0);
1155 }
1156
1157 int
1158 pfctl_show_timeouts(int dev, int opts)
1159 {
1160         struct pfioc_tm pt;
1161         int i;
1162
1163         if (opts & PF_OPT_SHOWALL)
1164                 pfctl_print_title("TIMEOUTS:");
1165         memset(&pt, 0, sizeof(pt));
1166         for (i = 0; pf_timeouts[i].name; i++) {
1167                 pt.timeout = pf_timeouts[i].timeout;
1168                 if (ioctl(dev, DIOCGETTIMEOUT, &pt))
1169                         err(1, "DIOCGETTIMEOUT");
1170                 printf("%-20s %10d", pf_timeouts[i].name, pt.seconds);
1171                 if (pf_timeouts[i].timeout >= PFTM_ADAPTIVE_START &&
1172                     pf_timeouts[i].timeout <= PFTM_ADAPTIVE_END)
1173                         printf(" states");
1174                 else
1175                         printf("s");
1176                 printf("\n");
1177         }
1178         return (0);
1179
1180 }
1181
1182 int
1183 pfctl_show_limits(int dev, int opts)
1184 {
1185         struct pfioc_limit pl;
1186         int i;
1187
1188         if (opts & PF_OPT_SHOWALL)
1189                 pfctl_print_title("LIMITS:");
1190         memset(&pl, 0, sizeof(pl));
1191         for (i = 0; pf_limits[i].name; i++) {
1192                 pl.index = pf_limits[i].index;
1193                 if (ioctl(dev, DIOCGETLIMIT, &pl))
1194                         err(1, "DIOCGETLIMIT");
1195                 printf("%-13s ", pf_limits[i].name);
1196                 if (pl.limit == UINT_MAX)
1197                         printf("unlimited\n");
1198                 else
1199                         printf("hard limit %8u\n", pl.limit);
1200         }
1201         return (0);
1202 }
1203
1204 /* callbacks for rule/nat/rdr/addr */
1205 int
1206 pfctl_add_pool(struct pfctl *pf, struct pf_pool *p, sa_family_t af)
1207 {
1208         struct pf_pooladdr *pa;
1209
1210         if ((pf->opts & PF_OPT_NOACTION) == 0) {
1211                 if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr))
1212                         err(1, "DIOCBEGINADDRS");
1213         }
1214
1215         pf->paddr.af = af;
1216         TAILQ_FOREACH(pa, &p->list, entries) {
1217                 memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr));
1218                 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1219                         if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr))
1220                                 err(1, "DIOCADDADDR");
1221                 }
1222         }
1223         return (0);
1224 }
1225
1226 int
1227 pfctl_add_rule(struct pfctl *pf, struct pf_rule *r, const char *anchor_call)
1228 {
1229         u_int8_t                rs_num;
1230         struct pf_rule          *rule;
1231         struct pf_ruleset       *rs;
1232         char                    *p;
1233
1234         rs_num = pf_get_ruleset_number(r->action);
1235         if (rs_num == PF_RULESET_MAX)
1236                 errx(1, "Invalid rule type %d", r->action);
1237
1238         rs = &pf->anchor->ruleset;
1239
1240         if (anchor_call[0] && r->anchor == NULL) {
1241                 /* 
1242                  * Don't make non-brace anchors part of the main anchor pool.
1243                  */
1244                 if ((r->anchor = calloc(1, sizeof(*r->anchor))) == NULL)
1245                         err(1, "pfctl_add_rule: calloc");
1246                 
1247                 pf_init_ruleset(&r->anchor->ruleset);
1248                 r->anchor->ruleset.anchor = r->anchor;
1249                 if (strlcpy(r->anchor->path, anchor_call,
1250                     sizeof(rule->anchor->path)) >= sizeof(rule->anchor->path))
1251                         errx(1, "pfctl_add_rule: strlcpy");
1252                 if ((p = strrchr(anchor_call, '/')) != NULL) {
1253                         if (!strlen(p))
1254                                 err(1, "pfctl_add_rule: bad anchor name %s",
1255                                     anchor_call);
1256                 } else
1257                         p = (char *)anchor_call;
1258                 if (strlcpy(r->anchor->name, p,
1259                     sizeof(rule->anchor->name)) >= sizeof(rule->anchor->name))
1260                         errx(1, "pfctl_add_rule: strlcpy");
1261         }
1262
1263         if ((rule = calloc(1, sizeof(*rule))) == NULL)
1264                 err(1, "calloc");
1265         bcopy(r, rule, sizeof(*rule));
1266         TAILQ_INIT(&rule->rpool.list);
1267         pfctl_move_pool(&r->rpool, &rule->rpool);
1268
1269         TAILQ_INSERT_TAIL(rs->rules[rs_num].active.ptr, rule, entries);
1270         return (0);
1271 }
1272
1273 int
1274 pfctl_ruleset_trans(struct pfctl *pf, char *path, struct pf_anchor *a)
1275 {
1276         int osize = pf->trans->pfrb_size;
1277
1278         if ((pf->loadopt & PFCTL_FLAG_NAT) != 0) {
1279                 if (pfctl_add_trans(pf->trans, PF_RULESET_NAT, path) ||
1280                     pfctl_add_trans(pf->trans, PF_RULESET_BINAT, path) ||
1281                     pfctl_add_trans(pf->trans, PF_RULESET_RDR, path))
1282                         return (1);
1283         }
1284         if (a == pf->astack[0] && ((altqsupport &&
1285             (pf->loadopt & PFCTL_FLAG_ALTQ) != 0))) {
1286                 if (pfctl_add_trans(pf->trans, PF_RULESET_ALTQ, path))
1287                         return (2);
1288         }
1289         if ((pf->loadopt & PFCTL_FLAG_FILTER) != 0) {
1290                 if (pfctl_add_trans(pf->trans, PF_RULESET_SCRUB, path) ||
1291                     pfctl_add_trans(pf->trans, PF_RULESET_FILTER, path))
1292                         return (3);
1293         }
1294         if (pf->loadopt & PFCTL_FLAG_TABLE)
1295                 if (pfctl_add_trans(pf->trans, PF_RULESET_TABLE, path))
1296                         return (4);
1297         if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize))
1298                 return (5);
1299
1300         return (0);
1301 }
1302
1303 int
1304 pfctl_load_ruleset(struct pfctl *pf, char *path, struct pf_ruleset *rs,
1305     int rs_num, int depth)
1306 {
1307         struct pf_rule *r;
1308         int             error, len = strlen(path);
1309         int             brace = 0;
1310
1311         pf->anchor = rs->anchor;
1312
1313         if (path[0])
1314                 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->anchor->name);
1315         else
1316                 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->anchor->name);
1317
1318         if (depth) {
1319                 if (TAILQ_FIRST(rs->rules[rs_num].active.ptr) != NULL) {
1320                         brace++;
1321                         if (pf->opts & PF_OPT_VERBOSE)
1322                                 printf(" {\n");
1323                         if ((pf->opts & PF_OPT_NOACTION) == 0 &&
1324                             (error = pfctl_ruleset_trans(pf,
1325                             path, rs->anchor))) {
1326                                 printf("pfctl_load_rulesets: "
1327                                     "pfctl_ruleset_trans %d\n", error);
1328                                 goto error;
1329                         }
1330                 } else if (pf->opts & PF_OPT_VERBOSE)
1331                         printf("\n");
1332
1333         }
1334
1335         if (pf->optimize && rs_num == PF_RULESET_FILTER)
1336                 pfctl_optimize_ruleset(pf, rs);
1337
1338         while ((r = TAILQ_FIRST(rs->rules[rs_num].active.ptr)) != NULL) {
1339                 TAILQ_REMOVE(rs->rules[rs_num].active.ptr, r, entries);
1340                 if ((error = pfctl_load_rule(pf, path, r, depth)))
1341                         goto error;
1342                 if (r->anchor) {
1343                         if ((error = pfctl_load_ruleset(pf, path,
1344                             &r->anchor->ruleset, rs_num, depth + 1)))
1345                                 goto error;
1346                 } else if (pf->opts & PF_OPT_VERBOSE)
1347                         printf("\n");
1348                 free(r);
1349         }
1350         if (brace && pf->opts & PF_OPT_VERBOSE) {
1351                 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE));
1352                 printf("}\n");
1353         }
1354         path[len] = '\0';
1355         return (0);
1356
1357  error:
1358         path[len] = '\0';
1359         return (error);
1360
1361 }
1362
1363 int
1364 pfctl_load_rule(struct pfctl *pf, char *path, struct pf_rule *r, int depth)
1365 {
1366         u_int8_t                rs_num = pf_get_ruleset_number(r->action);
1367         char                    *name;
1368         struct pfioc_rule       pr;
1369         int                     len = strlen(path);
1370
1371         bzero(&pr, sizeof(pr));
1372         /* set up anchor before adding to path for anchor_call */
1373         if ((pf->opts & PF_OPT_NOACTION) == 0)
1374                 pr.ticket = pfctl_get_ticket(pf->trans, rs_num, path);
1375         if (strlcpy(pr.anchor, path, sizeof(pr.anchor)) >= sizeof(pr.anchor))
1376                 errx(1, "pfctl_load_rule: strlcpy");
1377
1378         if (r->anchor) {
1379                 if (r->anchor->match) {
1380                         if (path[0])
1381                                 snprintf(&path[len], MAXPATHLEN - len,
1382                                     "/%s", r->anchor->name);
1383                         else
1384                                 snprintf(&path[len], MAXPATHLEN - len,
1385                                     "%s", r->anchor->name);
1386                         name = path;
1387                 } else
1388                         name = r->anchor->path;
1389         } else
1390                 name = "";
1391
1392         if ((pf->opts & PF_OPT_NOACTION) == 0) {
1393                 if (pfctl_add_pool(pf, &r->rpool, r->af))
1394                         return (1);
1395                 pr.pool_ticket = pf->paddr.ticket;
1396                 memcpy(&pr.rule, r, sizeof(pr.rule));
1397                 if (r->anchor && strlcpy(pr.anchor_call, name,
1398                     sizeof(pr.anchor_call)) >= sizeof(pr.anchor_call))
1399                         errx(1, "pfctl_load_rule: strlcpy");
1400                 if (ioctl(pf->dev, DIOCADDRULE, &pr))
1401                         err(1, "DIOCADDRULE");
1402         }
1403
1404         if (pf->opts & PF_OPT_VERBOSE) {
1405                 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2));
1406                 print_rule(r, r->anchor ? r->anchor->name : "",
1407                     pf->opts & PF_OPT_VERBOSE2,
1408                     pf->opts & PF_OPT_NUMERIC);
1409         }
1410         path[len] = '\0';
1411         pfctl_clear_pool(&r->rpool);
1412         return (0);
1413 }
1414
1415 int
1416 pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
1417 {
1418         if (altqsupport &&
1419             (loadopt & PFCTL_FLAG_ALTQ) != 0) {
1420                 memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq));
1421                 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1422                         if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) {
1423                                 if (errno == ENXIO)
1424                                         errx(1, "qtype not configured");
1425                                 else if (errno == ENODEV)
1426                                         errx(1, "%s: driver does not support "
1427                                             "altq", a->ifname);
1428                                 else
1429                                         err(1, "DIOCADDALTQ");
1430                         }
1431                 }
1432                 pfaltq_store(&pf->paltq->altq);
1433         }
1434         return (0);
1435 }
1436
1437 int
1438 pfctl_rules(int dev, char *filename, int opts, int optimize,
1439     char *anchorname, struct pfr_buffer *trans)
1440 {
1441 #define ERR(x) do { warn(x); goto _error; } while(0)
1442 #define ERRX(x) do { warnx(x); goto _error; } while(0)
1443
1444         struct pfr_buffer       *t, buf;
1445         struct pfioc_altq        pa;
1446         struct pfctl             pf;
1447         struct pf_ruleset       *rs;
1448         struct pfr_table         trs;
1449         char                    *path;
1450         int                      osize;
1451
1452         RB_INIT(&pf_anchors);
1453         memset(&pf_main_anchor, 0, sizeof(pf_main_anchor));
1454         pf_init_ruleset(&pf_main_anchor.ruleset);
1455         pf_main_anchor.ruleset.anchor = &pf_main_anchor;
1456         if (trans == NULL) {
1457                 bzero(&buf, sizeof(buf));
1458                 buf.pfrb_type = PFRB_TRANS;
1459                 t = &buf;
1460                 osize = 0;
1461         } else {
1462                 t = trans;
1463                 osize = t->pfrb_size;
1464         }
1465
1466         memset(&pa, 0, sizeof(pa));
1467         memset(&pf, 0, sizeof(pf));
1468         memset(&trs, 0, sizeof(trs));
1469         if ((path = calloc(1, MAXPATHLEN)) == NULL)
1470                 ERRX("pfctl_rules: calloc");
1471         if (strlcpy(trs.pfrt_anchor, anchorname,
1472             sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor))
1473                 ERRX("pfctl_rules: strlcpy");
1474         pf.dev = dev;
1475         pf.opts = opts;
1476         pf.optimize = optimize;
1477         pf.loadopt = loadopt;
1478
1479         /* non-brace anchor, create without resolving the path */
1480         if ((pf.anchor = calloc(1, sizeof(*pf.anchor))) == NULL)
1481                 ERRX("pfctl_rules: calloc");
1482         rs = &pf.anchor->ruleset;
1483         pf_init_ruleset(rs);
1484         rs->anchor = pf.anchor;
1485         if (strlcpy(pf.anchor->path, anchorname,
1486             sizeof(pf.anchor->path)) >= sizeof(pf.anchor->path))
1487                 errx(1, "pfctl_add_rule: strlcpy");
1488         if (strlcpy(pf.anchor->name, anchorname,
1489             sizeof(pf.anchor->name)) >= sizeof(pf.anchor->name))
1490                 errx(1, "pfctl_add_rule: strlcpy");
1491
1492
1493         pf.astack[0] = pf.anchor;
1494         pf.asd = 0;
1495         if (anchorname[0])
1496                 pf.loadopt &= ~PFCTL_FLAG_ALTQ;
1497         pf.paltq = &pa;
1498         pf.trans = t;
1499         pfctl_init_options(&pf);
1500
1501         if ((opts & PF_OPT_NOACTION) == 0) {
1502                 /*
1503                  * XXX For the time being we need to open transactions for
1504                  * the main ruleset before parsing, because tables are still
1505                  * loaded at parse time.
1506                  */
1507                 if (pfctl_ruleset_trans(&pf, anchorname, pf.anchor))
1508                         ERRX("pfctl_rules");
1509                 if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ))
1510                         pa.ticket =
1511                             pfctl_get_ticket(t, PF_RULESET_ALTQ, anchorname);
1512                 if (pf.loadopt & PFCTL_FLAG_TABLE)
1513                         pf.astack[0]->ruleset.tticket =
1514                             pfctl_get_ticket(t, PF_RULESET_TABLE, anchorname);
1515         }
1516
1517         if (parse_config(filename, &pf) < 0) {
1518                 if ((opts & PF_OPT_NOACTION) == 0)
1519                         ERRX("Syntax error in config file: "
1520                             "pf rules not loaded");
1521                 else
1522                         goto _error;
1523         }
1524         if (loadopt & PFCTL_FLAG_OPTION)
1525                 pfctl_clear_skip_ifaces(&pf);
1526
1527         if ((pf.loadopt & PFCTL_FLAG_FILTER &&
1528             (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_SCRUB, 0))) ||
1529             (pf.loadopt & PFCTL_FLAG_NAT &&
1530             (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_NAT, 0) ||
1531             pfctl_load_ruleset(&pf, path, rs, PF_RULESET_RDR, 0) ||
1532             pfctl_load_ruleset(&pf, path, rs, PF_RULESET_BINAT, 0))) ||
1533             (pf.loadopt & PFCTL_FLAG_FILTER &&
1534             pfctl_load_ruleset(&pf, path, rs, PF_RULESET_FILTER, 0))) {
1535                 if ((opts & PF_OPT_NOACTION) == 0)
1536                         ERRX("Unable to load rules into kernel");
1537                 else
1538                         goto _error;
1539         }
1540
1541         if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))
1542                 if (check_commit_altq(dev, opts) != 0)
1543                         ERRX("errors in altq config");
1544
1545         /* process "load anchor" directives */
1546         if (!anchorname[0])
1547                 if (pfctl_load_anchors(dev, &pf, t) == -1)
1548                         ERRX("load anchors");
1549
1550         if (trans == NULL && (opts & PF_OPT_NOACTION) == 0) {
1551                 if (!anchorname[0])
1552                         if (pfctl_load_options(&pf))
1553                                 goto _error;
1554                 if (pfctl_trans(dev, t, DIOCXCOMMIT, osize))
1555                         ERR("DIOCXCOMMIT");
1556         }
1557         return (0);
1558
1559 _error:
1560         if (trans == NULL) {    /* main ruleset */
1561                 if ((opts & PF_OPT_NOACTION) == 0)
1562                         if (pfctl_trans(dev, t, DIOCXROLLBACK, osize))
1563                                 err(1, "DIOCXROLLBACK");
1564                 exit(1);
1565         } else {                /* sub ruleset */
1566                 return (-1);
1567         }
1568
1569 #undef ERR
1570 #undef ERRX
1571 }
1572
1573 FILE *
1574 pfctl_fopen(const char *name, const char *mode)
1575 {
1576         struct stat      st;
1577         FILE            *fp;
1578
1579         fp = fopen(name, mode);
1580         if (fp == NULL)
1581                 return (NULL);
1582         if (fstat(fileno(fp), &st)) {
1583                 fclose(fp);
1584                 return (NULL);
1585         }
1586         if (S_ISDIR(st.st_mode)) {
1587                 fclose(fp);
1588                 errno = EISDIR;
1589                 return (NULL);
1590         }
1591         return (fp);
1592 }
1593
1594 void
1595 pfctl_init_options(struct pfctl *pf)
1596 {
1597
1598         pf->timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
1599         pf->timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
1600         pf->timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
1601         pf->timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
1602         pf->timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
1603         pf->timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
1604         pf->timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
1605         pf->timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
1606         pf->timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
1607         pf->timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
1608         pf->timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
1609         pf->timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
1610         pf->timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
1611         pf->timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
1612         pf->timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
1613         pf->timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
1614         pf->timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
1615         pf->timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
1616         pf->timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
1617         pf->timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
1618
1619         pf->limit[PF_LIMIT_STATES] = PFSTATE_HIWAT;
1620         pf->limit[PF_LIMIT_FRAGS] = PFFRAG_FRENT_HIWAT;
1621         pf->limit[PF_LIMIT_SRC_NODES] = PFSNODE_HIWAT;
1622         pf->limit[PF_LIMIT_TABLE_ENTRIES] = PFR_KENTRY_HIWAT;
1623
1624         pf->debug = PF_DEBUG_URGENT;
1625 }
1626
1627 int
1628 pfctl_load_options(struct pfctl *pf)
1629 {
1630         int i, error = 0;
1631
1632         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1633                 return (0);
1634
1635         /* load limits */
1636         for (i = 0; i < PF_LIMIT_MAX; i++) {
1637                 if ((pf->opts & PF_OPT_MERGE) && !pf->limit_set[i])
1638                         continue;
1639                 if (pfctl_load_limit(pf, i, pf->limit[i]))
1640                         error = 1;
1641         }
1642
1643         /*
1644          * If we've set the limit, but haven't explicitly set adaptive
1645          * timeouts, do it now with a start of 60% and end of 120%.
1646          */
1647         if (pf->limit_set[PF_LIMIT_STATES] &&
1648             !pf->timeout_set[PFTM_ADAPTIVE_START] &&
1649             !pf->timeout_set[PFTM_ADAPTIVE_END]) {
1650                 pf->timeout[PFTM_ADAPTIVE_START] =
1651                         (pf->limit[PF_LIMIT_STATES] / 10) * 6;
1652                 pf->timeout_set[PFTM_ADAPTIVE_START] = 1;
1653                 pf->timeout[PFTM_ADAPTIVE_END] =
1654                         (pf->limit[PF_LIMIT_STATES] / 10) * 12;
1655                 pf->timeout_set[PFTM_ADAPTIVE_END] = 1;
1656         }
1657
1658         /* load timeouts */
1659         for (i = 0; i < PFTM_MAX; i++) {
1660                 if ((pf->opts & PF_OPT_MERGE) && !pf->timeout_set[i])
1661                         continue;
1662                 if (pfctl_load_timeout(pf, i, pf->timeout[i]))
1663                         error = 1;
1664         }
1665
1666         /* load debug */
1667         if (!(pf->opts & PF_OPT_MERGE) || pf->debug_set)
1668                 if (pfctl_load_debug(pf, pf->debug))
1669                         error = 1;
1670
1671         /* load logif */
1672         if (!(pf->opts & PF_OPT_MERGE) || pf->ifname_set)
1673                 if (pfctl_load_logif(pf, pf->ifname))
1674                         error = 1;
1675
1676         /* load hostid */
1677         if (!(pf->opts & PF_OPT_MERGE) || pf->hostid_set)
1678                 if (pfctl_load_hostid(pf, pf->hostid))
1679                         error = 1;
1680
1681         return (error);
1682 }
1683
1684 int
1685 pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit)
1686 {
1687         int i;
1688
1689
1690         for (i = 0; pf_limits[i].name; i++) {
1691                 if (strcasecmp(opt, pf_limits[i].name) == 0) {
1692                         pf->limit[pf_limits[i].index] = limit;
1693                         pf->limit_set[pf_limits[i].index] = 1;
1694                         break;
1695                 }
1696         }
1697         if (pf_limits[i].name == NULL) {
1698                 warnx("Bad pool name.");
1699                 return (1);
1700         }
1701
1702         if (pf->opts & PF_OPT_VERBOSE)
1703                 printf("set limit %s %d\n", opt, limit);
1704
1705         return (0);
1706 }
1707
1708 int
1709 pfctl_load_limit(struct pfctl *pf, unsigned int index, unsigned int limit)
1710 {
1711         struct pfioc_limit pl;
1712
1713         memset(&pl, 0, sizeof(pl));
1714         pl.index = index;
1715         pl.limit = limit;
1716         if (ioctl(pf->dev, DIOCSETLIMIT, &pl)) {
1717                 if (errno == EBUSY)
1718                         warnx("Current pool size exceeds requested hard limit");
1719                 else
1720                         warnx("DIOCSETLIMIT");
1721                 return (1);
1722         }
1723         return (0);
1724 }
1725
1726 int
1727 pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
1728 {
1729         int i;
1730
1731         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1732                 return (0);
1733
1734         for (i = 0; pf_timeouts[i].name; i++) {
1735                 if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
1736                         pf->timeout[pf_timeouts[i].timeout] = seconds;
1737                         pf->timeout_set[pf_timeouts[i].timeout] = 1;
1738                         break;
1739                 }
1740         }
1741
1742         if (pf_timeouts[i].name == NULL) {
1743                 warnx("Bad timeout name.");
1744                 return (1);
1745         }
1746
1747
1748         if (pf->opts & PF_OPT_VERBOSE && ! quiet)
1749                 printf("set timeout %s %d\n", opt, seconds);
1750
1751         return (0);
1752 }
1753
1754 int
1755 pfctl_load_timeout(struct pfctl *pf, unsigned int timeout, unsigned int seconds)
1756 {
1757         struct pfioc_tm pt;
1758
1759         memset(&pt, 0, sizeof(pt));
1760         pt.timeout = timeout;
1761         pt.seconds = seconds;
1762         if (ioctl(pf->dev, DIOCSETTIMEOUT, &pt)) {
1763                 warnx("DIOCSETTIMEOUT");
1764                 return (1);
1765         }
1766         return (0);
1767 }
1768
1769 int
1770 pfctl_set_optimization(struct pfctl *pf, const char *opt)
1771 {
1772         const struct pf_hint *hint;
1773         int i, r;
1774
1775         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1776                 return (0);
1777
1778         for (i = 0; pf_hints[i].name; i++)
1779                 if (strcasecmp(opt, pf_hints[i].name) == 0)
1780                         break;
1781
1782         hint = pf_hints[i].hint;
1783         if (hint == NULL) {
1784                 warnx("invalid state timeouts optimization");
1785                 return (1);
1786         }
1787
1788         for (i = 0; hint[i].name; i++)
1789                 if ((r = pfctl_set_timeout(pf, hint[i].name,
1790                     hint[i].timeout, 1)))
1791                         return (r);
1792
1793         if (pf->opts & PF_OPT_VERBOSE)
1794                 printf("set optimization %s\n", opt);
1795
1796         return (0);
1797 }
1798
1799 int
1800 pfctl_set_logif(struct pfctl *pf, char *ifname)
1801 {
1802
1803         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1804                 return (0);
1805
1806         if (!strcmp(ifname, "none")) {
1807                 free(pf->ifname);
1808                 pf->ifname = NULL;
1809         } else {
1810                 pf->ifname = strdup(ifname);
1811                 if (!pf->ifname)
1812                         errx(1, "pfctl_set_logif: strdup");
1813         }
1814         pf->ifname_set = 1;
1815
1816         if (pf->opts & PF_OPT_VERBOSE)
1817                 printf("set loginterface %s\n", ifname);
1818
1819         return (0);
1820 }
1821
1822 int
1823 pfctl_load_logif(struct pfctl *pf, char *ifname)
1824 {
1825         struct pfioc_if pi;
1826
1827         memset(&pi, 0, sizeof(pi));
1828         if (ifname && strlcpy(pi.ifname, ifname,
1829             sizeof(pi.ifname)) >= sizeof(pi.ifname)) {
1830                 warnx("pfctl_load_logif: strlcpy");
1831                 return (1);
1832         }
1833         if (ioctl(pf->dev, DIOCSETSTATUSIF, &pi)) {
1834                 warnx("DIOCSETSTATUSIF");
1835                 return (1);
1836         }
1837         return (0);
1838 }
1839
1840 int
1841 pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
1842 {
1843         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1844                 return (0);
1845
1846         HTONL(hostid);
1847
1848         pf->hostid = hostid;
1849         pf->hostid_set = 1;
1850
1851         if (pf->opts & PF_OPT_VERBOSE)
1852                 printf("set hostid 0x%08x\n", ntohl(hostid));
1853
1854         return (0);
1855 }
1856
1857 int
1858 pfctl_load_hostid(struct pfctl *pf, u_int32_t hostid)
1859 {
1860         if (ioctl(dev, DIOCSETHOSTID, &hostid)) {
1861                 warnx("DIOCSETHOSTID");
1862                 return (1);
1863         }
1864         return (0);
1865 }
1866
1867 int
1868 pfctl_set_debug(struct pfctl *pf, char *d)
1869 {
1870         u_int32_t       level;
1871
1872         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1873                 return (0);
1874
1875         if (!strcmp(d, "none"))
1876                 pf->debug = PF_DEBUG_NONE;
1877         else if (!strcmp(d, "urgent"))
1878                 pf->debug = PF_DEBUG_URGENT;
1879         else if (!strcmp(d, "misc"))
1880                 pf->debug = PF_DEBUG_MISC;
1881         else if (!strcmp(d, "loud"))
1882                 pf->debug = PF_DEBUG_NOISY;
1883         else {
1884                 warnx("unknown debug level \"%s\"", d);
1885                 return (-1);
1886         }
1887
1888         pf->debug_set = 1;
1889         level = pf->debug;
1890
1891         if ((pf->opts & PF_OPT_NOACTION) == 0)
1892                 if (ioctl(dev, DIOCSETDEBUG, &level))
1893                         err(1, "DIOCSETDEBUG");
1894
1895         if (pf->opts & PF_OPT_VERBOSE)
1896                 printf("set debug %s\n", d);
1897
1898         return (0);
1899 }
1900
1901 int
1902 pfctl_load_debug(struct pfctl *pf, unsigned int level)
1903 {
1904         if (ioctl(pf->dev, DIOCSETDEBUG, &level)) {
1905                 warnx("DIOCSETDEBUG");
1906                 return (1);
1907         }
1908         return (0);
1909 }
1910
1911 int
1912 pfctl_set_interface_flags(struct pfctl *pf, char *ifname, int flags, int how)
1913 {
1914         struct pfioc_iface      pi;
1915
1916         if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1917                 return (0);
1918
1919         bzero(&pi, sizeof(pi));
1920
1921         pi.pfiio_flags = flags;
1922
1923         if (strlcpy(pi.pfiio_name, ifname, sizeof(pi.pfiio_name)) >=
1924             sizeof(pi.pfiio_name))
1925                 errx(1, "pfctl_set_interface_flags: strlcpy");
1926
1927         if ((pf->opts & PF_OPT_NOACTION) == 0) {
1928                 if (how == 0) {
1929                         if (ioctl(pf->dev, DIOCCLRIFFLAG, &pi))
1930                                 err(1, "DIOCCLRIFFLAG");
1931                 } else {
1932                         if (ioctl(pf->dev, DIOCSETIFFLAG, &pi))
1933                                 err(1, "DIOCSETIFFLAG");
1934                         pfctl_check_skip_ifaces(ifname);
1935                 }
1936         }
1937         return (0);
1938 }
1939
1940 void
1941 pfctl_debug(int dev, u_int32_t level, int opts)
1942 {
1943         if (ioctl(dev, DIOCSETDEBUG, &level))
1944                 err(1, "DIOCSETDEBUG");
1945         if ((opts & PF_OPT_QUIET) == 0) {
1946                 fprintf(stderr, "debug level set to '");
1947                 switch (level) {
1948                 case PF_DEBUG_NONE:
1949                         fprintf(stderr, "none");
1950                         break;
1951                 case PF_DEBUG_URGENT:
1952                         fprintf(stderr, "urgent");
1953                         break;
1954                 case PF_DEBUG_MISC:
1955                         fprintf(stderr, "misc");
1956                         break;
1957                 case PF_DEBUG_NOISY:
1958                         fprintf(stderr, "loud");
1959                         break;
1960                 default:
1961                         fprintf(stderr, "<invalid>");
1962                         break;
1963                 }
1964                 fprintf(stderr, "'\n");
1965         }
1966 }
1967
1968 int
1969 pfctl_test_altqsupport(int dev, int opts)
1970 {
1971         struct pfioc_altq pa;
1972
1973         if (ioctl(dev, DIOCGETALTQS, &pa)) {
1974                 if (errno == ENODEV) {
1975                         if (opts & PF_OPT_VERBOSE)
1976                                 fprintf(stderr, "No ALTQ support in kernel\n"
1977                                     "ALTQ related functions disabled\n");
1978                         return (0);
1979                 } else
1980                         err(1, "DIOCGETALTQS");
1981         }
1982         return (1);
1983 }
1984
1985 int
1986 pfctl_show_anchors(int dev, int opts, char *anchorname)
1987 {
1988         struct pfioc_ruleset     pr;
1989         u_int32_t                mnr, nr;
1990
1991         memset(&pr, 0, sizeof(pr));
1992         memcpy(pr.path, anchorname, sizeof(pr.path));
1993         if (ioctl(dev, DIOCGETRULESETS, &pr)) {
1994                 if (errno == EINVAL)
1995                         fprintf(stderr, "Anchor '%s' not found.\n",
1996                             anchorname);
1997                 else
1998                         err(1, "DIOCGETRULESETS");
1999                 return (-1);
2000         }
2001         mnr = pr.nr;
2002         for (nr = 0; nr < mnr; ++nr) {
2003                 char sub[MAXPATHLEN];
2004
2005                 pr.nr = nr;
2006                 if (ioctl(dev, DIOCGETRULESET, &pr))
2007                         err(1, "DIOCGETRULESET");
2008                 if (!strcmp(pr.name, PF_RESERVED_ANCHOR))
2009                         continue;
2010                 sub[0] = 0;
2011                 if (pr.path[0]) {
2012                         strlcat(sub, pr.path, sizeof(sub));
2013                         strlcat(sub, "/", sizeof(sub));
2014                 }
2015                 strlcat(sub, pr.name, sizeof(sub));
2016                 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE))
2017                         printf("  %s\n", sub);
2018                 if ((opts & PF_OPT_VERBOSE) && pfctl_show_anchors(dev, opts, sub))
2019                         return (-1);
2020         }
2021         return (0);
2022 }
2023
2024 const char *
2025 pfctl_lookup_option(char *cmd, const char **list)
2026 {
2027         if (cmd != NULL && *cmd)
2028                 for (; *list; list++)
2029                         if (!strncmp(cmd, *list, strlen(cmd)))
2030                                 return (*list);
2031         return (NULL);
2032 }
2033
2034 int
2035 main(int argc, char *argv[])
2036 {
2037         int      error = 0;
2038         int      ch;
2039         int      mode = O_RDONLY;
2040         int      opts = 0;
2041         int      optimize = PF_OPTIMIZE_BASIC;
2042         char     anchorname[MAXPATHLEN];
2043         char    *path;
2044
2045         if (argc < 2)
2046                 usage();
2047
2048         while ((ch = getopt(argc, argv,
2049             "a:AdD:eqf:F:ghi:k:K:mnNOo:Pp:rRs:t:T:vx:z")) != -1) {
2050                 switch (ch) {
2051                 case 'a':
2052                         anchoropt = optarg;
2053                         break;
2054                 case 'd':
2055                         opts |= PF_OPT_DISABLE;
2056                         mode = O_RDWR;
2057                         break;
2058                 case 'D':
2059                         if (pfctl_cmdline_symset(optarg) < 0)
2060                                 warnx("could not parse macro definition %s",
2061                                     optarg);
2062                         break;
2063                 case 'e':
2064                         opts |= PF_OPT_ENABLE;
2065                         mode = O_RDWR;
2066                         break;
2067                 case 'q':
2068                         opts |= PF_OPT_QUIET;
2069                         break;
2070                 case 'F':
2071                         clearopt = pfctl_lookup_option(optarg, clearopt_list);
2072                         if (clearopt == NULL) {
2073                                 warnx("Unknown flush modifier '%s'", optarg);
2074                                 usage();
2075                         }
2076                         mode = O_RDWR;
2077                         break;
2078                 case 'i':
2079                         ifaceopt = optarg;
2080                         break;
2081                 case 'k':
2082                         if (state_killers >= 2) {
2083                                 warnx("can only specify -k twice");
2084                                 usage();
2085                                 /* NOTREACHED */
2086                         }
2087                         state_kill[state_killers++] = optarg;
2088                         mode = O_RDWR;
2089                         break;
2090                 case 'K':
2091                         if (src_node_killers >= 2) {
2092                                 warnx("can only specify -K twice");
2093                                 usage();
2094                                 /* NOTREACHED */
2095                         }
2096                         src_node_kill[src_node_killers++] = optarg;
2097                         mode = O_RDWR;
2098                         break;
2099                 case 'm':
2100                         opts |= PF_OPT_MERGE;
2101                         break;
2102                 case 'n':
2103                         opts |= PF_OPT_NOACTION;
2104                         break;
2105                 case 'N':
2106                         loadopt |= PFCTL_FLAG_NAT;
2107                         break;
2108                 case 'r':
2109                         opts |= PF_OPT_USEDNS;
2110                         break;
2111                 case 'f':
2112                         rulesopt = optarg;
2113                         mode = O_RDWR;
2114                         break;
2115                 case 'g':
2116                         opts |= PF_OPT_DEBUG;
2117                         break;
2118                 case 'A':
2119                         loadopt |= PFCTL_FLAG_ALTQ;
2120                         break;
2121                 case 'R':
2122                         loadopt |= PFCTL_FLAG_FILTER;
2123                         break;
2124                 case 'o':
2125                         optiopt = pfctl_lookup_option(optarg, optiopt_list);
2126                         if (optiopt == NULL) {
2127                                 warnx("Unknown optimization '%s'", optarg);
2128                                 usage();
2129                         }
2130                         opts |= PF_OPT_OPTIMIZE;
2131                         break;
2132                 case 'O':
2133                         loadopt |= PFCTL_FLAG_OPTION;
2134                         break;
2135                 case 'p':
2136                         pf_device = optarg;
2137                         break;
2138                 case 'P':
2139                         opts |= PF_OPT_NUMERIC;
2140                         break;
2141                 case 's':
2142                         showopt = pfctl_lookup_option(optarg, showopt_list);
2143                         if (showopt == NULL) {
2144                                 warnx("Unknown show modifier '%s'", optarg);
2145                                 usage();
2146                         }
2147                         break;
2148                 case 't':
2149                         tableopt = optarg;
2150                         break;
2151                 case 'T':
2152                         tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
2153                         if (tblcmdopt == NULL) {
2154                                 warnx("Unknown table command '%s'", optarg);
2155                                 usage();
2156                         }
2157                         break;
2158                 case 'v':
2159                         if (opts & PF_OPT_VERBOSE)
2160                                 opts |= PF_OPT_VERBOSE2;
2161                         opts |= PF_OPT_VERBOSE;
2162                         break;
2163                 case 'x':
2164                         debugopt = pfctl_lookup_option(optarg, debugopt_list);
2165                         if (debugopt == NULL) {
2166                                 warnx("Unknown debug level '%s'", optarg);
2167                                 usage();
2168                         }
2169                         mode = O_RDWR;
2170                         break;
2171                 case 'z':
2172                         opts |= PF_OPT_CLRRULECTRS;
2173                         mode = O_RDWR;
2174                         break;
2175                 case 'h':
2176                         /* FALLTHROUGH */
2177                 default:
2178                         usage();
2179                         /* NOTREACHED */
2180                 }
2181         }
2182
2183         if (tblcmdopt != NULL) {
2184                 argc -= optind;
2185                 argv += optind;
2186                 ch = *tblcmdopt;
2187                 if (ch == 'l') {
2188                         loadopt |= PFCTL_FLAG_TABLE;
2189                         tblcmdopt = NULL;
2190                 } else
2191                         mode = strchr("acdefkrz", ch) ? O_RDWR : O_RDONLY;
2192         } else if (argc != optind) {
2193                 warnx("unknown command line argument: %s ...", argv[optind]);
2194                 usage();
2195                 /* NOTREACHED */
2196         }
2197         if (loadopt == 0)
2198                 loadopt = ~0;
2199
2200         if ((path = calloc(1, MAXPATHLEN)) == NULL)
2201                 errx(1, "pfctl: calloc");
2202         memset(anchorname, 0, sizeof(anchorname));
2203         if (anchoropt != NULL) {
2204                 int len = strlen(anchoropt);
2205
2206                 if (anchoropt[len - 1] == '*') {
2207                         if (len >= 2 && anchoropt[len - 2] == '/')
2208                                 anchoropt[len - 2] = '\0';
2209                         else
2210                                 anchoropt[len - 1] = '\0';
2211                         opts |= PF_OPT_RECURSE;
2212                 }
2213                 if (strlcpy(anchorname, anchoropt,
2214                     sizeof(anchorname)) >= sizeof(anchorname))
2215                         errx(1, "anchor name '%s' too long",
2216                             anchoropt);
2217                 loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE;
2218         }
2219
2220         if ((opts & PF_OPT_NOACTION) == 0) {
2221                 dev = open(pf_device, mode);
2222                 if (dev == -1)
2223                         err(1, "%s", pf_device);
2224                 altqsupport = pfctl_test_altqsupport(dev, opts);
2225         } else {
2226                 dev = open(pf_device, O_RDONLY);
2227                 if (dev >= 0)
2228                         opts |= PF_OPT_DUMMYACTION;
2229                 /* turn off options */
2230                 opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
2231                 clearopt = showopt = debugopt = NULL;
2232 #if !defined(ENABLE_ALTQ)
2233                 altqsupport = 0;
2234 #else
2235                 altqsupport = 1;
2236 #endif
2237         }
2238
2239         if (opts & PF_OPT_DISABLE)
2240                 if (pfctl_disable(dev, opts))
2241                         error = 1;
2242
2243         if (showopt != NULL) {
2244                 switch (*showopt) {
2245                 case 'A':
2246                         pfctl_show_anchors(dev, opts, anchorname);
2247                         break;
2248                 case 'r':
2249                         pfctl_load_fingerprints(dev, opts);
2250                         pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
2251                             anchorname, 0);
2252                         break;
2253                 case 'l':
2254                         pfctl_load_fingerprints(dev, opts);
2255                         pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
2256                             anchorname, 0);
2257                         break;
2258                 case 'n':
2259                         pfctl_load_fingerprints(dev, opts);
2260                         pfctl_show_nat(dev, opts, anchorname);
2261                         break;
2262                 case 'q':
2263                         pfctl_show_altq(dev, ifaceopt, opts,
2264                             opts & PF_OPT_VERBOSE2);
2265                         break;
2266                 case 's':
2267                         pfctl_show_states(dev, ifaceopt, opts);
2268                         break;
2269                 case 'S':
2270                         pfctl_show_src_nodes(dev, opts);
2271                         break;
2272                 case 'i':
2273                         pfctl_show_status(dev, opts);
2274                         break;
2275                 case 't':
2276                         pfctl_show_timeouts(dev, opts);
2277                         break;
2278                 case 'm':
2279                         pfctl_show_limits(dev, opts);
2280                         break;
2281                 case 'a':
2282                         opts |= PF_OPT_SHOWALL;
2283                         pfctl_load_fingerprints(dev, opts);
2284
2285                         pfctl_show_nat(dev, opts, anchorname);
2286                         pfctl_show_rules(dev, path, opts, 0, anchorname, 0);
2287                         pfctl_show_altq(dev, ifaceopt, opts, 0);
2288                         pfctl_show_states(dev, ifaceopt, opts);
2289                         pfctl_show_src_nodes(dev, opts);
2290                         pfctl_show_status(dev, opts);
2291                         pfctl_show_rules(dev, path, opts, 1, anchorname, 0);
2292                         pfctl_show_timeouts(dev, opts);
2293                         pfctl_show_limits(dev, opts);
2294                         pfctl_show_tables(anchorname, opts);
2295                         pfctl_show_fingerprints(opts);
2296                         break;
2297                 case 'T':
2298                         pfctl_show_tables(anchorname, opts);
2299                         break;
2300                 case 'o':
2301                         pfctl_load_fingerprints(dev, opts);
2302                         pfctl_show_fingerprints(opts);
2303                         break;
2304                 case 'I':
2305                         pfctl_show_ifaces(ifaceopt, opts);
2306                         break;
2307                 }
2308         }
2309
2310         if ((opts & PF_OPT_CLRRULECTRS) && showopt == NULL)
2311                 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
2312                     anchorname, 0);
2313
2314         if (clearopt != NULL) {
2315                 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL)
2316                         errx(1, "anchor names beginning with '_' cannot "
2317                             "be modified from the command line");
2318
2319                 switch (*clearopt) {
2320                 case 'r':
2321                         pfctl_clear_rules(dev, opts, anchorname);
2322                         break;
2323                 case 'n':
2324                         pfctl_clear_nat(dev, opts, anchorname);
2325                         break;
2326                 case 'q':
2327                         pfctl_clear_altq(dev, opts);
2328                         break;
2329                 case 's':
2330                         pfctl_clear_states(dev, ifaceopt, opts);
2331                         break;
2332                 case 'S':
2333                         pfctl_clear_src_nodes(dev, opts);
2334                         break;
2335                 case 'i':
2336                         pfctl_clear_stats(dev, opts);
2337                         break;
2338                 case 'a':
2339                         pfctl_clear_rules(dev, opts, anchorname);
2340                         pfctl_clear_nat(dev, opts, anchorname);
2341                         pfctl_clear_tables(anchorname, opts);
2342                         if (!*anchorname) {
2343                                 pfctl_clear_altq(dev, opts);
2344                                 pfctl_clear_states(dev, ifaceopt, opts);
2345                                 pfctl_clear_src_nodes(dev, opts);
2346                                 pfctl_clear_stats(dev, opts);
2347                                 pfctl_clear_fingerprints(dev, opts);
2348                                 pfctl_clear_interface_flags(dev, opts);
2349                         }
2350                         break;
2351                 case 'o':
2352                         pfctl_clear_fingerprints(dev, opts);
2353                         break;
2354                 case 'T':
2355                         pfctl_clear_tables(anchorname, opts);
2356                         break;
2357                 }
2358         }
2359         if (state_killers) {
2360                 if (!strcmp(state_kill[0], "label"))
2361                         pfctl_label_kill_states(dev, ifaceopt, opts);
2362                 else if (!strcmp(state_kill[0], "id"))
2363                         pfctl_id_kill_states(dev, ifaceopt, opts);
2364                 else
2365                         pfctl_net_kill_states(dev, ifaceopt, opts);
2366         }
2367
2368         if (src_node_killers)
2369                 pfctl_kill_src_nodes(dev, ifaceopt, opts);
2370
2371         if (tblcmdopt != NULL) {
2372                 error = pfctl_command_tables(argc, argv, tableopt,
2373                     tblcmdopt, rulesopt, anchorname, opts);
2374                 rulesopt = NULL;
2375         }
2376         if (optiopt != NULL) {
2377                 switch (*optiopt) {
2378                 case 'n':
2379                         optimize = 0;
2380                         break;
2381                 case 'b':
2382                         optimize |= PF_OPTIMIZE_BASIC;
2383                         break;
2384                 case 'o':
2385                 case 'p':
2386                         optimize |= PF_OPTIMIZE_PROFILE;
2387                         break;
2388                 }
2389         }
2390
2391         if ((rulesopt != NULL) && (loadopt & PFCTL_FLAG_OPTION) &&
2392             !anchorname[0])
2393                 if (pfctl_get_skip_ifaces())
2394                         error = 1;
2395
2396         if (rulesopt != NULL && !(opts & (PF_OPT_MERGE|PF_OPT_NOACTION)) &&
2397             !anchorname[0] && (loadopt & PFCTL_FLAG_OPTION))
2398                 if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
2399                         error = 1;
2400
2401         if (rulesopt != NULL) {
2402                 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL)
2403                         errx(1, "anchor names beginning with '_' cannot "
2404                             "be modified from the command line");
2405                 if (pfctl_rules(dev, rulesopt, opts, optimize,
2406                     anchorname, NULL))
2407                         error = 1;
2408                 else if (!(opts & PF_OPT_NOACTION) &&
2409                     (loadopt & PFCTL_FLAG_TABLE))
2410                         warn_namespace_collision(NULL);
2411         }
2412
2413         if (opts & PF_OPT_ENABLE)
2414                 if (pfctl_enable(dev, opts))
2415                         error = 1;
2416
2417         if (debugopt != NULL) {
2418                 switch (*debugopt) {
2419                 case 'n':
2420                         pfctl_debug(dev, PF_DEBUG_NONE, opts);
2421                         break;
2422                 case 'u':
2423                         pfctl_debug(dev, PF_DEBUG_URGENT, opts);
2424                         break;
2425                 case 'm':
2426                         pfctl_debug(dev, PF_DEBUG_MISC, opts);
2427                         break;
2428                 case 'l':
2429                         pfctl_debug(dev, PF_DEBUG_NOISY, opts);
2430                         break;
2431                 }
2432         }
2433
2434         exit(error);
2435 }