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