Home | History | Annotate | Line # | Download | only in pfctl
pfctl.c revision 1.3
      1  1.3    yamt /*	$NetBSD: pfctl.c,v 1.3 2004/11/14 11:26:48 yamt Exp $	*/
      2  1.3    yamt /*	$OpenBSD: pfctl.c,v 1.222 2004/08/26 16:35:36 jaredy Exp $ */
      3  1.1  itojun 
      4  1.1  itojun /*
      5  1.1  itojun  * Copyright (c) 2001 Daniel Hartmeier
      6  1.1  itojun  * Copyright (c) 2002,2003 Henning Brauer
      7  1.1  itojun  * All rights reserved.
      8  1.1  itojun  *
      9  1.1  itojun  * Redistribution and use in source and binary forms, with or without
     10  1.1  itojun  * modification, are permitted provided that the following conditions
     11  1.1  itojun  * are met:
     12  1.1  itojun  *
     13  1.1  itojun  *    - Redistributions of source code must retain the above copyright
     14  1.1  itojun  *      notice, this list of conditions and the following disclaimer.
     15  1.1  itojun  *    - Redistributions in binary form must reproduce the above
     16  1.1  itojun  *      copyright notice, this list of conditions and the following
     17  1.1  itojun  *      disclaimer in the documentation and/or other materials provided
     18  1.1  itojun  *      with the distribution.
     19  1.1  itojun  *
     20  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.1  itojun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.1  itojun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.1  itojun  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.1  itojun  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.1  itojun  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1  itojun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     28  1.1  itojun  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     30  1.1  itojun  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1  itojun  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1  itojun  *
     33  1.1  itojun  */
     34  1.1  itojun 
     35  1.1  itojun #include <sys/types.h>
     36  1.1  itojun #include <sys/ioctl.h>
     37  1.1  itojun #include <sys/socket.h>
     38  1.3    yamt #include <sys/stat.h>
     39  1.1  itojun 
     40  1.1  itojun #include <net/if.h>
     41  1.1  itojun #include <netinet/in.h>
     42  1.1  itojun #include <net/pfvar.h>
     43  1.1  itojun #include <arpa/inet.h>
     44  1.1  itojun #include <altq/altq.h>
     45  1.1  itojun 
     46  1.1  itojun #include <err.h>
     47  1.1  itojun #include <errno.h>
     48  1.1  itojun #include <fcntl.h>
     49  1.1  itojun #include <limits.h>
     50  1.1  itojun #include <netdb.h>
     51  1.1  itojun #include <stdio.h>
     52  1.1  itojun #include <stdlib.h>
     53  1.1  itojun #include <string.h>
     54  1.1  itojun #include <unistd.h>
     55  1.1  itojun 
     56  1.1  itojun #include "pfctl_parser.h"
     57  1.1  itojun #include "pfctl.h"
     58  1.1  itojun 
     59  1.1  itojun void	 usage(void);
     60  1.1  itojun int	 pfctl_enable(int, int);
     61  1.1  itojun int	 pfctl_disable(int, int);
     62  1.1  itojun int	 pfctl_clear_stats(int, int);
     63  1.3    yamt int	 pfctl_clear_rules(int, int, char *);
     64  1.3    yamt int	 pfctl_clear_nat(int, int, char *);
     65  1.1  itojun int	 pfctl_clear_altq(int, int);
     66  1.1  itojun int	 pfctl_clear_src_nodes(int, int);
     67  1.1  itojun int	 pfctl_clear_states(int, const char *, int);
     68  1.1  itojun int	 pfctl_kill_states(int, const char *, int);
     69  1.1  itojun int	 pfctl_get_pool(int, struct pf_pool *, u_int32_t, u_int32_t, int,
     70  1.3    yamt 	    char *);
     71  1.1  itojun void	 pfctl_print_rule_counters(struct pf_rule *, int);
     72  1.3    yamt int	 pfctl_show_rules(int, int, int, char *);
     73  1.3    yamt int	 pfctl_show_nat(int, int, char *);
     74  1.1  itojun int	 pfctl_show_src_nodes(int, int);
     75  1.1  itojun int	 pfctl_show_states(int, const char *, int);
     76  1.1  itojun int	 pfctl_show_status(int, int);
     77  1.1  itojun int	 pfctl_show_timeouts(int, int);
     78  1.1  itojun int	 pfctl_show_limits(int, int);
     79  1.1  itojun int	 pfctl_debug(int, u_int32_t, int);
     80  1.1  itojun int	 pfctl_clear_rule_counters(int, int);
     81  1.1  itojun int	 pfctl_test_altqsupport(int, int);
     82  1.1  itojun int	 pfctl_show_anchors(int, int, char *);
     83  1.1  itojun const char	*pfctl_lookup_option(char *, const char **);
     84  1.1  itojun 
     85  1.1  itojun const char	*clearopt;
     86  1.1  itojun char		*rulesopt;
     87  1.1  itojun const char	*showopt;
     88  1.1  itojun const char	*debugopt;
     89  1.1  itojun char		*anchoropt;
     90  1.1  itojun char		*pf_device = "/dev/pf";
     91  1.1  itojun char		*ifaceopt;
     92  1.1  itojun char		*tableopt;
     93  1.1  itojun const char	*tblcmdopt;
     94  1.1  itojun int		 state_killers;
     95  1.1  itojun char		*state_kill[2];
     96  1.1  itojun int		 loadopt;
     97  1.1  itojun int		 altqsupport;
     98  1.1  itojun 
     99  1.1  itojun int		 dev = -1;
    100  1.1  itojun int		 first_title = 1;
    101  1.1  itojun int		 labels = 0;
    102  1.1  itojun 
    103  1.1  itojun const char	*infile;
    104  1.1  itojun 
    105  1.1  itojun static const struct {
    106  1.1  itojun 	const char	*name;
    107  1.1  itojun 	int		index;
    108  1.1  itojun } pf_limits[] = {
    109  1.1  itojun 	{ "states",	PF_LIMIT_STATES },
    110  1.1  itojun 	{ "src-nodes",	PF_LIMIT_SRC_NODES },
    111  1.1  itojun 	{ "frags",	PF_LIMIT_FRAGS },
    112  1.1  itojun 	{ NULL,		0 }
    113  1.1  itojun };
    114  1.1  itojun 
    115  1.1  itojun struct pf_hint {
    116  1.1  itojun 	const char	*name;
    117  1.1  itojun 	int		timeout;
    118  1.1  itojun };
    119  1.1  itojun static const struct pf_hint pf_hint_normal[] = {
    120  1.1  itojun 	{ "tcp.first",		2 * 60 },
    121  1.1  itojun 	{ "tcp.opening",	30 },
    122  1.1  itojun 	{ "tcp.established",	24 * 60 * 60 },
    123  1.1  itojun 	{ "tcp.closing",	15 * 60 },
    124  1.1  itojun 	{ "tcp.finwait",	45 },
    125  1.1  itojun 	{ "tcp.closed",		90 },
    126  1.3    yamt 	{ "tcp.tsdiff",		30 },
    127  1.1  itojun 	{ NULL,			0 }
    128  1.1  itojun };
    129  1.1  itojun static const struct pf_hint pf_hint_satellite[] = {
    130  1.1  itojun 	{ "tcp.first",		3 * 60 },
    131  1.1  itojun 	{ "tcp.opening",	30 + 5 },
    132  1.1  itojun 	{ "tcp.established",	24 * 60 * 60 },
    133  1.1  itojun 	{ "tcp.closing",	15 * 60 + 5 },
    134  1.1  itojun 	{ "tcp.finwait",	45 + 5 },
    135  1.1  itojun 	{ "tcp.closed",		90 + 5 },
    136  1.3    yamt 	{ "tcp.tsdiff",		60 },
    137  1.1  itojun 	{ NULL,			0 }
    138  1.1  itojun };
    139  1.1  itojun static const struct pf_hint pf_hint_conservative[] = {
    140  1.1  itojun 	{ "tcp.first",		60 * 60 },
    141  1.1  itojun 	{ "tcp.opening",	15 * 60 },
    142  1.1  itojun 	{ "tcp.established",	5 * 24 * 60 * 60 },
    143  1.1  itojun 	{ "tcp.closing",	60 * 60 },
    144  1.1  itojun 	{ "tcp.finwait",	10 * 60 },
    145  1.1  itojun 	{ "tcp.closed",		3 * 60 },
    146  1.3    yamt 	{ "tcp.tsdiff",		60 },
    147  1.1  itojun 	{ NULL,			0 }
    148  1.1  itojun };
    149  1.1  itojun static const struct pf_hint pf_hint_aggressive[] = {
    150  1.1  itojun 	{ "tcp.first",		30 },
    151  1.1  itojun 	{ "tcp.opening",	5 },
    152  1.1  itojun 	{ "tcp.established",	5 * 60 * 60 },
    153  1.1  itojun 	{ "tcp.closing",	60 },
    154  1.1  itojun 	{ "tcp.finwait",	30 },
    155  1.1  itojun 	{ "tcp.closed",		30 },
    156  1.3    yamt 	{ "tcp.tsdiff",		10 },
    157  1.1  itojun 	{ NULL,			0 }
    158  1.1  itojun };
    159  1.1  itojun 
    160  1.1  itojun static const struct {
    161  1.1  itojun 	const char *name;
    162  1.1  itojun 	const struct pf_hint *hint;
    163  1.1  itojun } pf_hints[] = {
    164  1.1  itojun 	{ "normal",		pf_hint_normal },
    165  1.1  itojun 	{ "satellite",		pf_hint_satellite },
    166  1.1  itojun 	{ "high-latency",	pf_hint_satellite },
    167  1.1  itojun 	{ "conservative",	pf_hint_conservative },
    168  1.1  itojun 	{ "aggressive",		pf_hint_aggressive },
    169  1.1  itojun 	{ NULL,			NULL }
    170  1.1  itojun };
    171  1.1  itojun 
    172  1.1  itojun static const char *clearopt_list[] = {
    173  1.1  itojun 	"nat", "queue", "rules", "Sources",
    174  1.1  itojun 	"state", "info", "Tables", "osfp", "all", NULL
    175  1.1  itojun };
    176  1.1  itojun 
    177  1.1  itojun static const char *showopt_list[] = {
    178  1.1  itojun 	"nat", "queue", "rules", "Anchors", "Sources", "state", "info",
    179  1.1  itojun 	"Interfaces", "labels", "timeouts", "memory", "Tables", "osfp",
    180  1.1  itojun 	"all", NULL
    181  1.1  itojun };
    182  1.1  itojun 
    183  1.1  itojun static const char *tblcmdopt_list[] = {
    184  1.1  itojun 	"kill", "flush", "add", "delete", "load", "replace", "show",
    185  1.1  itojun 	"test", "zero", NULL
    186  1.1  itojun };
    187  1.1  itojun 
    188  1.1  itojun static const char *debugopt_list[] = {
    189  1.1  itojun 	"none", "urgent", "misc", "loud", NULL
    190  1.1  itojun };
    191  1.1  itojun 
    192  1.1  itojun 
    193  1.1  itojun void
    194  1.1  itojun usage(void)
    195  1.1  itojun {
    196  1.1  itojun 	extern char *__progname;
    197  1.1  itojun 
    198  1.3    yamt 	fprintf(stderr, "usage: %s [-AdeghNnOoqRrvz] ", __progname);
    199  1.3    yamt 	fprintf(stderr, "[-a anchor] [-D macro=value] [-F modifier]\n");
    200  1.1  itojun 	fprintf(stderr, "             ");
    201  1.3    yamt 	fprintf(stderr, "[-f file] [-i interface] [-k host] ");
    202  1.3    yamt 	fprintf(stderr, "[-p device] [-s modifier]\n");
    203  1.1  itojun 	fprintf(stderr, "             ");
    204  1.3    yamt 	fprintf(stderr, "[-t table -T command [address ...]] ");
    205  1.3    yamt 	fprintf(stderr, "[-x level]\n");
    206  1.1  itojun 	exit(1);
    207  1.1  itojun }
    208  1.1  itojun 
    209  1.1  itojun int
    210  1.1  itojun pfctl_enable(int dev, int opts)
    211  1.1  itojun {
    212  1.1  itojun 	if (ioctl(dev, DIOCSTART)) {
    213  1.1  itojun 		if (errno == EEXIST)
    214  1.1  itojun 			errx(1, "pf already enabled");
    215  1.1  itojun 		else
    216  1.1  itojun 			err(1, "DIOCSTART");
    217  1.1  itojun 	}
    218  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    219  1.1  itojun 		fprintf(stderr, "pf enabled\n");
    220  1.1  itojun 
    221  1.1  itojun 	if (altqsupport && ioctl(dev, DIOCSTARTALTQ))
    222  1.1  itojun 		if (errno != EEXIST)
    223  1.1  itojun 			err(1, "DIOCSTARTALTQ");
    224  1.1  itojun 
    225  1.1  itojun 	return (0);
    226  1.1  itojun }
    227  1.1  itojun 
    228  1.1  itojun int
    229  1.1  itojun pfctl_disable(int dev, int opts)
    230  1.1  itojun {
    231  1.1  itojun 	if (ioctl(dev, DIOCSTOP)) {
    232  1.1  itojun 		if (errno == ENOENT)
    233  1.1  itojun 			errx(1, "pf not enabled");
    234  1.1  itojun 		else
    235  1.1  itojun 			err(1, "DIOCSTOP");
    236  1.1  itojun 	}
    237  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    238  1.1  itojun 		fprintf(stderr, "pf disabled\n");
    239  1.1  itojun 
    240  1.1  itojun 	if (altqsupport && ioctl(dev, DIOCSTOPALTQ))
    241  1.1  itojun 			if (errno != ENOENT)
    242  1.1  itojun 				err(1, "DIOCSTOPALTQ");
    243  1.1  itojun 
    244  1.1  itojun 	return (0);
    245  1.1  itojun }
    246  1.1  itojun 
    247  1.1  itojun int
    248  1.1  itojun pfctl_clear_stats(int dev, int opts)
    249  1.1  itojun {
    250  1.1  itojun 	if (ioctl(dev, DIOCCLRSTATUS))
    251  1.1  itojun 		err(1, "DIOCCLRSTATUS");
    252  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    253  1.1  itojun 		fprintf(stderr, "pf: statistics cleared\n");
    254  1.1  itojun 	return (0);
    255  1.1  itojun }
    256  1.1  itojun 
    257  1.1  itojun int
    258  1.3    yamt pfctl_clear_rules(int dev, int opts, char *anchorname)
    259  1.1  itojun {
    260  1.1  itojun 	struct pfr_buffer t;
    261  1.1  itojun 
    262  1.1  itojun 	memset(&t, 0, sizeof(t));
    263  1.1  itojun 	t.pfrb_type = PFRB_TRANS;
    264  1.3    yamt 	if (pfctl_add_trans(&t, PF_RULESET_SCRUB, anchorname) ||
    265  1.3    yamt 	    pfctl_add_trans(&t, PF_RULESET_FILTER, anchorname) ||
    266  1.1  itojun 	    pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
    267  1.1  itojun 	    pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
    268  1.1  itojun 		err(1, "pfctl_clear_rules");
    269  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    270  1.1  itojun 		fprintf(stderr, "rules cleared\n");
    271  1.1  itojun 	return (0);
    272  1.1  itojun }
    273  1.1  itojun 
    274  1.1  itojun int
    275  1.3    yamt pfctl_clear_nat(int dev, int opts, char *anchorname)
    276  1.1  itojun {
    277  1.1  itojun 	struct pfr_buffer t;
    278  1.1  itojun 
    279  1.1  itojun 	memset(&t, 0, sizeof(t));
    280  1.1  itojun 	t.pfrb_type = PFRB_TRANS;
    281  1.3    yamt 	if (pfctl_add_trans(&t, PF_RULESET_NAT, anchorname) ||
    282  1.3    yamt 	    pfctl_add_trans(&t, PF_RULESET_BINAT, anchorname) ||
    283  1.3    yamt 	    pfctl_add_trans(&t, PF_RULESET_RDR, anchorname) ||
    284  1.1  itojun 	    pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
    285  1.1  itojun 	    pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
    286  1.1  itojun 		err(1, "pfctl_clear_nat");
    287  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    288  1.1  itojun 		fprintf(stderr, "nat cleared\n");
    289  1.1  itojun 	return (0);
    290  1.1  itojun }
    291  1.1  itojun 
    292  1.1  itojun int
    293  1.1  itojun pfctl_clear_altq(int dev, int opts)
    294  1.1  itojun {
    295  1.1  itojun 	struct pfr_buffer t;
    296  1.1  itojun 
    297  1.1  itojun 	if (!altqsupport)
    298  1.1  itojun 		return (-1);
    299  1.1  itojun 	memset(&t, 0, sizeof(t));
    300  1.1  itojun 	t.pfrb_type = PFRB_TRANS;
    301  1.3    yamt 	if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "") ||
    302  1.1  itojun 	    pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
    303  1.1  itojun 	    pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
    304  1.1  itojun 		err(1, "pfctl_clear_altq");
    305  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    306  1.1  itojun 		fprintf(stderr, "altq cleared\n");
    307  1.1  itojun 	return (0);
    308  1.1  itojun }
    309  1.1  itojun 
    310  1.1  itojun int
    311  1.1  itojun pfctl_clear_src_nodes(int dev, int opts)
    312  1.1  itojun {
    313  1.1  itojun 	if (ioctl(dev, DIOCCLRSRCNODES))
    314  1.1  itojun 		err(1, "DIOCCLRSRCNODES");
    315  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    316  1.1  itojun 		fprintf(stderr, "source tracking entries cleared\n");
    317  1.1  itojun 	return (0);
    318  1.1  itojun }
    319  1.1  itojun 
    320  1.1  itojun int
    321  1.1  itojun pfctl_clear_states(int dev, const char *iface, int opts)
    322  1.1  itojun {
    323  1.1  itojun 	struct pfioc_state_kill psk;
    324  1.1  itojun 
    325  1.1  itojun 	memset(&psk, 0, sizeof(psk));
    326  1.1  itojun 	if (iface != NULL && strlcpy(psk.psk_ifname, iface,
    327  1.1  itojun 	    sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
    328  1.1  itojun 		errx(1, "invalid interface: %s", iface);
    329  1.1  itojun 
    330  1.1  itojun 	if (ioctl(dev, DIOCCLRSTATES, &psk))
    331  1.1  itojun 		err(1, "DIOCCLRSTATES");
    332  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    333  1.1  itojun 		fprintf(stderr, "%d states cleared\n", psk.psk_af);
    334  1.1  itojun 	return (0);
    335  1.1  itojun }
    336  1.1  itojun 
    337  1.1  itojun int
    338  1.1  itojun pfctl_kill_states(int dev, const char *iface, int opts)
    339  1.1  itojun {
    340  1.1  itojun 	struct pfioc_state_kill psk;
    341  1.1  itojun 	struct addrinfo *res[2], *resp[2];
    342  1.1  itojun 	struct sockaddr last_src, last_dst;
    343  1.1  itojun 	int killed, sources, dests;
    344  1.1  itojun 	int ret_ga;
    345  1.1  itojun 
    346  1.1  itojun 	killed = sources = dests = 0;
    347  1.1  itojun 
    348  1.1  itojun 	memset(&psk, 0, sizeof(psk));
    349  1.1  itojun 	memset(&psk.psk_src.addr.v.a.mask, 0xff,
    350  1.1  itojun 	    sizeof(psk.psk_src.addr.v.a.mask));
    351  1.1  itojun 	memset(&last_src, 0xff, sizeof(last_src));
    352  1.1  itojun 	memset(&last_dst, 0xff, sizeof(last_dst));
    353  1.1  itojun 	if (iface != NULL && strlcpy(psk.psk_ifname, iface,
    354  1.1  itojun 	    sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
    355  1.1  itojun 		errx(1, "invalid interface: %s", iface);
    356  1.1  itojun 
    357  1.1  itojun 	if ((ret_ga = getaddrinfo(state_kill[0], NULL, NULL, &res[0]))) {
    358  1.1  itojun 		errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
    359  1.1  itojun 		/* NOTREACHED */
    360  1.1  itojun 	}
    361  1.1  itojun 	for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
    362  1.1  itojun 		if (resp[0]->ai_addr == NULL)
    363  1.1  itojun 			continue;
    364  1.1  itojun 		/* We get lots of duplicates.  Catch the easy ones */
    365  1.1  itojun 		if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
    366  1.1  itojun 			continue;
    367  1.1  itojun 		last_src = *(struct sockaddr *)resp[0]->ai_addr;
    368  1.1  itojun 
    369  1.1  itojun 		psk.psk_af = resp[0]->ai_family;
    370  1.1  itojun 		sources++;
    371  1.1  itojun 
    372  1.1  itojun 		if (psk.psk_af == AF_INET)
    373  1.1  itojun 			psk.psk_src.addr.v.a.addr.v4 =
    374  1.1  itojun 			    ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
    375  1.1  itojun 		else if (psk.psk_af == AF_INET6)
    376  1.1  itojun 			psk.psk_src.addr.v.a.addr.v6 =
    377  1.1  itojun 			    ((struct sockaddr_in6 *)resp[0]->ai_addr)->
    378  1.1  itojun 			    sin6_addr;
    379  1.1  itojun 		else
    380  1.1  itojun 			errx(1, "Unknown address family %d", psk.psk_af);
    381  1.1  itojun 
    382  1.1  itojun 		if (state_killers > 1) {
    383  1.1  itojun 			dests = 0;
    384  1.1  itojun 			memset(&psk.psk_dst.addr.v.a.mask, 0xff,
    385  1.1  itojun 			    sizeof(psk.psk_dst.addr.v.a.mask));
    386  1.1  itojun 			memset(&last_dst, 0xff, sizeof(last_dst));
    387  1.1  itojun 			if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL,
    388  1.1  itojun 			    &res[1]))) {
    389  1.1  itojun 				errx(1, "getaddrinfo: %s",
    390  1.1  itojun 				    gai_strerror(ret_ga));
    391  1.1  itojun 				/* NOTREACHED */
    392  1.1  itojun 			}
    393  1.1  itojun 			for (resp[1] = res[1]; resp[1];
    394  1.1  itojun 			    resp[1] = resp[1]->ai_next) {
    395  1.1  itojun 				if (resp[1]->ai_addr == NULL)
    396  1.1  itojun 					continue;
    397  1.1  itojun 				if (psk.psk_af != resp[1]->ai_family)
    398  1.1  itojun 					continue;
    399  1.1  itojun 
    400  1.1  itojun 				if (memcmp(&last_dst, resp[1]->ai_addr,
    401  1.1  itojun 				    sizeof(last_dst)) == 0)
    402  1.1  itojun 					continue;
    403  1.1  itojun 				last_dst = *(struct sockaddr *)resp[1]->ai_addr;
    404  1.1  itojun 
    405  1.1  itojun 				dests++;
    406  1.1  itojun 
    407  1.1  itojun 				if (psk.psk_af == AF_INET)
    408  1.1  itojun 					psk.psk_dst.addr.v.a.addr.v4 =
    409  1.1  itojun 					    ((struct sockaddr_in *)resp[1]->
    410  1.1  itojun 					    ai_addr)->sin_addr;
    411  1.1  itojun 				else if (psk.psk_af == AF_INET6)
    412  1.1  itojun 					psk.psk_dst.addr.v.a.addr.v6 =
    413  1.1  itojun 					    ((struct sockaddr_in6 *)resp[1]->
    414  1.1  itojun 					    ai_addr)->sin6_addr;
    415  1.1  itojun 				else
    416  1.1  itojun 					errx(1, "Unknown address family %d",
    417  1.1  itojun 					    psk.psk_af);
    418  1.1  itojun 
    419  1.1  itojun 				if (ioctl(dev, DIOCKILLSTATES, &psk))
    420  1.1  itojun 					err(1, "DIOCKILLSTATES");
    421  1.1  itojun 				killed += psk.psk_af;
    422  1.1  itojun 				/* fixup psk.psk_af */
    423  1.1  itojun 				psk.psk_af = resp[1]->ai_family;
    424  1.1  itojun 			}
    425  1.1  itojun 			freeaddrinfo(res[1]);
    426  1.1  itojun 		} else {
    427  1.1  itojun 			if (ioctl(dev, DIOCKILLSTATES, &psk))
    428  1.1  itojun 				err(1, "DIOCKILLSTATES");
    429  1.1  itojun 			killed += psk.psk_af;
    430  1.1  itojun 			/* fixup psk.psk_af */
    431  1.1  itojun 			psk.psk_af = res[0]->ai_family;
    432  1.1  itojun 		}
    433  1.1  itojun 	}
    434  1.1  itojun 
    435  1.1  itojun 	freeaddrinfo(res[0]);
    436  1.1  itojun 
    437  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
    438  1.1  itojun 		fprintf(stderr, "killed %d states from %d sources and %d "
    439  1.1  itojun 		    "destinations\n", killed, sources, dests);
    440  1.1  itojun 	return (0);
    441  1.1  itojun }
    442  1.1  itojun 
    443  1.1  itojun int
    444  1.1  itojun pfctl_get_pool(int dev, struct pf_pool *pool, u_int32_t nr,
    445  1.3    yamt     u_int32_t ticket, int r_action, char *anchorname)
    446  1.1  itojun {
    447  1.1  itojun 	struct pfioc_pooladdr pp;
    448  1.1  itojun 	struct pf_pooladdr *pa;
    449  1.1  itojun 	u_int32_t pnr, mpnr;
    450  1.1  itojun 
    451  1.1  itojun 	memset(&pp, 0, sizeof(pp));
    452  1.1  itojun 	memcpy(pp.anchor, anchorname, sizeof(pp.anchor));
    453  1.1  itojun 	pp.r_action = r_action;
    454  1.1  itojun 	pp.r_num = nr;
    455  1.1  itojun 	pp.ticket = ticket;
    456  1.1  itojun 	if (ioctl(dev, DIOCGETADDRS, &pp)) {
    457  1.1  itojun 		warn("DIOCGETADDRS");
    458  1.1  itojun 		return (-1);
    459  1.1  itojun 	}
    460  1.1  itojun 	mpnr = pp.nr;
    461  1.1  itojun 	TAILQ_INIT(&pool->list);
    462  1.1  itojun 	for (pnr = 0; pnr < mpnr; ++pnr) {
    463  1.1  itojun 		pp.nr = pnr;
    464  1.1  itojun 		if (ioctl(dev, DIOCGETADDR, &pp)) {
    465  1.1  itojun 			warn("DIOCGETADDR");
    466  1.1  itojun 			return (-1);
    467  1.1  itojun 		}
    468  1.1  itojun 		pa = calloc(1, sizeof(struct pf_pooladdr));
    469  1.1  itojun 		if (pa == NULL)
    470  1.1  itojun 			err(1, "calloc");
    471  1.1  itojun 		bcopy(&pp.addr, pa, sizeof(struct pf_pooladdr));
    472  1.1  itojun 		TAILQ_INSERT_TAIL(&pool->list, pa, entries);
    473  1.1  itojun 	}
    474  1.1  itojun 
    475  1.1  itojun 	return (0);
    476  1.1  itojun }
    477  1.1  itojun 
    478  1.1  itojun void
    479  1.1  itojun pfctl_clear_pool(struct pf_pool *pool)
    480  1.1  itojun {
    481  1.1  itojun 	struct pf_pooladdr *pa;
    482  1.1  itojun 
    483  1.1  itojun 	while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
    484  1.1  itojun 		TAILQ_REMOVE(&pool->list, pa, entries);
    485  1.1  itojun 		free(pa);
    486  1.1  itojun 	}
    487  1.1  itojun }
    488  1.1  itojun 
    489  1.1  itojun void
    490  1.1  itojun pfctl_print_rule_counters(struct pf_rule *rule, int opts)
    491  1.1  itojun {
    492  1.1  itojun 	if (opts & PF_OPT_DEBUG) {
    493  1.1  itojun 		const char *t[PF_SKIP_COUNT] = { "i", "d", "f",
    494  1.1  itojun 		    "p", "sa", "sp", "da", "dp" };
    495  1.1  itojun 		int i;
    496  1.1  itojun 
    497  1.1  itojun 		printf("  [ Skip steps: ");
    498  1.1  itojun 		for (i = 0; i < PF_SKIP_COUNT; ++i) {
    499  1.1  itojun 			if (rule->skip[i].nr == rule->nr + 1)
    500  1.1  itojun 				continue;
    501  1.1  itojun 			printf("%s=", t[i]);
    502  1.1  itojun 			if (rule->skip[i].nr == -1)
    503  1.1  itojun 				printf("end ");
    504  1.1  itojun 			else
    505  1.1  itojun 				printf("%u ", rule->skip[i].nr);
    506  1.1  itojun 		}
    507  1.1  itojun 		printf("]\n");
    508  1.1  itojun 
    509  1.1  itojun 		printf("  [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n",
    510  1.1  itojun 		    rule->qname, rule->qid, rule->pqname, rule->pqid);
    511  1.1  itojun 	}
    512  1.1  itojun 	if (opts & PF_OPT_VERBOSE)
    513  1.1  itojun 		printf("  [ Evaluations: %-8llu  Packets: %-8llu  "
    514  1.1  itojun 			    "Bytes: %-10llu  States: %-6u]\n",
    515  1.1  itojun 			    (unsigned long long)rule->evaluations,
    516  1.1  itojun 			    (unsigned long long)rule->packets,
    517  1.1  itojun 			    (unsigned long long)rule->bytes, rule->states);
    518  1.1  itojun }
    519  1.1  itojun 
    520  1.1  itojun void
    521  1.1  itojun pfctl_print_title(char *title)
    522  1.1  itojun {
    523  1.1  itojun 	if (!first_title)
    524  1.1  itojun 		printf("\n");
    525  1.1  itojun 	first_title = 0;
    526  1.1  itojun 	printf("%s\n", title);
    527  1.1  itojun }
    528  1.1  itojun 
    529  1.1  itojun int
    530  1.3    yamt pfctl_show_rules(int dev, int opts, int format, char *anchorname)
    531  1.1  itojun {
    532  1.1  itojun 	struct pfioc_rule pr;
    533  1.1  itojun 	u_int32_t nr, mnr, header = 0;
    534  1.1  itojun 	int rule_numbers = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG);
    535  1.1  itojun 
    536  1.1  itojun 	memset(&pr, 0, sizeof(pr));
    537  1.1  itojun 	memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
    538  1.1  itojun 	if (opts & PF_OPT_SHOWALL) {
    539  1.1  itojun 		pr.rule.action = PF_PASS;
    540  1.1  itojun 		if (ioctl(dev, DIOCGETRULES, &pr)) {
    541  1.1  itojun 			warn("DIOCGETRULES");
    542  1.1  itojun 			return (-1);
    543  1.1  itojun 		}
    544  1.1  itojun 		header++;
    545  1.1  itojun 	}
    546  1.1  itojun 	pr.rule.action = PF_SCRUB;
    547  1.1  itojun 	if (ioctl(dev, DIOCGETRULES, &pr)) {
    548  1.1  itojun 		warn("DIOCGETRULES");
    549  1.1  itojun 		return (-1);
    550  1.1  itojun 	}
    551  1.1  itojun 	if (opts & PF_OPT_SHOWALL) {
    552  1.1  itojun 		if (format == 0 && (pr.nr > 0 || header))
    553  1.1  itojun 			pfctl_print_title("FILTER RULES:");
    554  1.1  itojun 		else if (format == 1 && labels)
    555  1.1  itojun 			pfctl_print_title("LABEL COUNTERS:");
    556  1.1  itojun 	}
    557  1.1  itojun 	mnr = pr.nr;
    558  1.1  itojun 	for (nr = 0; nr < mnr; ++nr) {
    559  1.1  itojun 		pr.nr = nr;
    560  1.1  itojun 		if (ioctl(dev, DIOCGETRULE, &pr)) {
    561  1.1  itojun 			warn("DIOCGETRULE");
    562  1.1  itojun 			return (-1);
    563  1.1  itojun 		}
    564  1.1  itojun 
    565  1.1  itojun 		if (pfctl_get_pool(dev, &pr.rule.rpool,
    566  1.3    yamt 		    nr, pr.ticket, PF_SCRUB, anchorname) != 0)
    567  1.1  itojun 			return (-1);
    568  1.1  itojun 
    569  1.1  itojun 		switch (format) {
    570  1.1  itojun 		case 1:
    571  1.1  itojun 			if (pr.rule.label[0]) {
    572  1.1  itojun 				printf("%s ", pr.rule.label);
    573  1.1  itojun 				printf("%llu %llu %llu\n",
    574  1.1  itojun 				    (unsigned long long)pr.rule.evaluations,
    575  1.1  itojun 				    (unsigned long long)pr.rule.packets,
    576  1.1  itojun 				    (unsigned long long)pr.rule.bytes);
    577  1.1  itojun 			}
    578  1.1  itojun 			break;
    579  1.1  itojun 		default:
    580  1.1  itojun 			if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
    581  1.1  itojun 				labels = 1;
    582  1.3    yamt 			print_rule(&pr.rule, pr.anchor_call, rule_numbers);
    583  1.1  itojun 			pfctl_print_rule_counters(&pr.rule, opts);
    584  1.1  itojun 		}
    585  1.1  itojun 		pfctl_clear_pool(&pr.rule.rpool);
    586  1.1  itojun 	}
    587  1.1  itojun 	pr.rule.action = PF_PASS;
    588  1.1  itojun 	if (ioctl(dev, DIOCGETRULES, &pr)) {
    589  1.1  itojun 		warn("DIOCGETRULES");
    590  1.1  itojun 		return (-1);
    591  1.1  itojun 	}
    592  1.1  itojun 	mnr = pr.nr;
    593  1.1  itojun 	for (nr = 0; nr < mnr; ++nr) {
    594  1.1  itojun 		pr.nr = nr;
    595  1.1  itojun 		if (ioctl(dev, DIOCGETRULE, &pr)) {
    596  1.1  itojun 			warn("DIOCGETRULE");
    597  1.1  itojun 			return (-1);
    598  1.1  itojun 		}
    599  1.1  itojun 
    600  1.1  itojun 		if (pfctl_get_pool(dev, &pr.rule.rpool,
    601  1.3    yamt 		    nr, pr.ticket, PF_PASS, anchorname) != 0)
    602  1.1  itojun 			return (-1);
    603  1.1  itojun 
    604  1.1  itojun 		switch (format) {
    605  1.1  itojun 		case 1:
    606  1.1  itojun 			if (pr.rule.label[0]) {
    607  1.1  itojun 				printf("%s ", pr.rule.label);
    608  1.1  itojun 				printf("%llu %llu %llu\n",
    609  1.1  itojun 				    (unsigned long long)pr.rule.evaluations,
    610  1.1  itojun 				    (unsigned long long)pr.rule.packets,
    611  1.1  itojun 				    (unsigned long long)pr.rule.bytes);
    612  1.1  itojun 			}
    613  1.1  itojun 			break;
    614  1.1  itojun 		default:
    615  1.1  itojun 			if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
    616  1.1  itojun 				labels = 1;
    617  1.3    yamt 			print_rule(&pr.rule, pr.anchor_call, rule_numbers);
    618  1.1  itojun 			pfctl_print_rule_counters(&pr.rule, opts);
    619  1.1  itojun 		}
    620  1.1  itojun 		pfctl_clear_pool(&pr.rule.rpool);
    621  1.1  itojun 	}
    622  1.1  itojun 	return (0);
    623  1.1  itojun }
    624  1.1  itojun 
    625  1.1  itojun int
    626  1.3    yamt pfctl_show_nat(int dev, int opts, char *anchorname)
    627  1.1  itojun {
    628  1.1  itojun 	struct pfioc_rule pr;
    629  1.1  itojun 	u_int32_t mnr, nr;
    630  1.1  itojun 	static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT };
    631  1.1  itojun 	int i, dotitle = opts & PF_OPT_SHOWALL;
    632  1.1  itojun 
    633  1.1  itojun 	memset(&pr, 0, sizeof(pr));
    634  1.1  itojun 	memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
    635  1.1  itojun 	for (i = 0; i < 3; i++) {
    636  1.1  itojun 		pr.rule.action = nattype[i];
    637  1.1  itojun 		if (ioctl(dev, DIOCGETRULES, &pr)) {
    638  1.1  itojun 			warn("DIOCGETRULES");
    639  1.1  itojun 			return (-1);
    640  1.1  itojun 		}
    641  1.1  itojun 		mnr = pr.nr;
    642  1.1  itojun 		for (nr = 0; nr < mnr; ++nr) {
    643  1.1  itojun 			pr.nr = nr;
    644  1.1  itojun 			if (ioctl(dev, DIOCGETRULE, &pr)) {
    645  1.1  itojun 				warn("DIOCGETRULE");
    646  1.1  itojun 				return (-1);
    647  1.1  itojun 			}
    648  1.1  itojun 			if (pfctl_get_pool(dev, &pr.rule.rpool, nr,
    649  1.3    yamt 			    pr.ticket, nattype[i], anchorname) != 0)
    650  1.1  itojun 				return (-1);
    651  1.1  itojun 			if (dotitle) {
    652  1.1  itojun 				pfctl_print_title("TRANSLATION RULES:");
    653  1.1  itojun 				dotitle = 0;
    654  1.1  itojun 			}
    655  1.3    yamt 			print_rule(&pr.rule, pr.anchor_call,
    656  1.3    yamt 			    opts & PF_OPT_VERBOSE2);
    657  1.1  itojun 			pfctl_print_rule_counters(&pr.rule, opts);
    658  1.1  itojun 			pfctl_clear_pool(&pr.rule.rpool);
    659  1.1  itojun 		}
    660  1.1  itojun 	}
    661  1.1  itojun 	return (0);
    662  1.1  itojun }
    663  1.1  itojun 
    664  1.1  itojun int
    665  1.1  itojun pfctl_show_src_nodes(int dev, int opts)
    666  1.1  itojun {
    667  1.1  itojun 	struct pfioc_src_nodes psn;
    668  1.1  itojun 	struct pf_src_node *p;
    669  1.1  itojun 	char *inbuf = NULL, *newinbuf = NULL;
    670  1.1  itojun 	unsigned len = 0;
    671  1.1  itojun 	int i;
    672  1.1  itojun 
    673  1.1  itojun 	memset(&psn, 0, sizeof(psn));
    674  1.1  itojun 	for (;;) {
    675  1.1  itojun 		psn.psn_len = len;
    676  1.1  itojun 		if (len) {
    677  1.1  itojun 			newinbuf = realloc(inbuf, len);
    678  1.1  itojun 			if (newinbuf == NULL)
    679  1.1  itojun 				err(1, "realloc");
    680  1.1  itojun 			psn.psn_buf = inbuf = newinbuf;
    681  1.1  itojun 		}
    682  1.1  itojun 		if (ioctl(dev, DIOCGETSRCNODES, &psn) < 0) {
    683  1.1  itojun 			warn("DIOCGETSRCNODES");
    684  1.1  itojun 			return (-1);
    685  1.1  itojun 		}
    686  1.1  itojun 		if (psn.psn_len + sizeof(struct pfioc_src_nodes) < len)
    687  1.1  itojun 			break;
    688  1.1  itojun 		if (len == 0 && psn.psn_len == 0)
    689  1.1  itojun 			return (0);
    690  1.1  itojun 		if (len == 0 && psn.psn_len != 0)
    691  1.1  itojun 			len = psn.psn_len;
    692  1.1  itojun 		if (psn.psn_len == 0)
    693  1.1  itojun 			return (0);	/* no src_nodes */
    694  1.1  itojun 		len *= 2;
    695  1.1  itojun 	}
    696  1.1  itojun 	p = psn.psn_src_nodes;
    697  1.1  itojun 	if (psn.psn_len > 0 && (opts & PF_OPT_SHOWALL))
    698  1.1  itojun 		pfctl_print_title("SOURCE TRACKING NODES:");
    699  1.1  itojun 	for (i = 0; i < psn.psn_len; i += sizeof(*p)) {
    700  1.1  itojun 		print_src_node(p, opts);
    701  1.1  itojun 		p++;
    702  1.1  itojun 	}
    703  1.1  itojun 	return (0);
    704  1.1  itojun }
    705  1.1  itojun 
    706  1.1  itojun int
    707  1.1  itojun pfctl_show_states(int dev, const char *iface, int opts)
    708  1.1  itojun {
    709  1.1  itojun 	struct pfioc_states ps;
    710  1.1  itojun 	struct pf_state *p;
    711  1.1  itojun 	char *inbuf = NULL, *newinbuf = NULL;
    712  1.1  itojun 	unsigned len = 0;
    713  1.1  itojun 	int i, dotitle = (opts & PF_OPT_SHOWALL);
    714  1.1  itojun 
    715  1.1  itojun 	memset(&ps, 0, sizeof(ps));
    716  1.1  itojun 	for (;;) {
    717  1.1  itojun 		ps.ps_len = len;
    718  1.1  itojun 		if (len) {
    719  1.1  itojun 			newinbuf = realloc(inbuf, len);
    720  1.1  itojun 			if (newinbuf == NULL)
    721  1.1  itojun 				err(1, "realloc");
    722  1.1  itojun 			ps.ps_buf = inbuf = newinbuf;
    723  1.1  itojun 		}
    724  1.1  itojun 		if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
    725  1.1  itojun 			warn("DIOCGETSTATES");
    726  1.1  itojun 			return (-1);
    727  1.1  itojun 		}
    728  1.1  itojun 		if (ps.ps_len + sizeof(struct pfioc_states) < len)
    729  1.1  itojun 			break;
    730  1.1  itojun 		if (len == 0 && ps.ps_len == 0)
    731  1.1  itojun 			return (0);
    732  1.1  itojun 		if (len == 0 && ps.ps_len != 0)
    733  1.1  itojun 			len = ps.ps_len;
    734  1.1  itojun 		if (ps.ps_len == 0)
    735  1.1  itojun 			return (0);	/* no states */
    736  1.1  itojun 		len *= 2;
    737  1.1  itojun 	}
    738  1.1  itojun 	p = ps.ps_states;
    739  1.1  itojun 	for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
    740  1.1  itojun 		if (iface != NULL && strcmp(p->u.ifname, iface))
    741  1.1  itojun 			continue;
    742  1.1  itojun 		if (dotitle) {
    743  1.1  itojun 			pfctl_print_title("STATES:");
    744  1.1  itojun 			dotitle = 0;
    745  1.1  itojun 		}
    746  1.1  itojun 		print_state(p, opts);
    747  1.1  itojun 	}
    748  1.1  itojun 	return (0);
    749  1.1  itojun }
    750  1.1  itojun 
    751  1.1  itojun int
    752  1.1  itojun pfctl_show_status(int dev, int opts)
    753  1.1  itojun {
    754  1.1  itojun 	struct pf_status status;
    755  1.1  itojun 
    756  1.1  itojun 	if (ioctl(dev, DIOCGETSTATUS, &status)) {
    757  1.1  itojun 		warn("DIOCGETSTATUS");
    758  1.1  itojun 		return (-1);
    759  1.1  itojun 	}
    760  1.1  itojun 	if (opts & PF_OPT_SHOWALL)
    761  1.1  itojun 		pfctl_print_title("INFO:");
    762  1.1  itojun 	print_status(&status, opts);
    763  1.1  itojun 	return (0);
    764  1.1  itojun }
    765  1.1  itojun 
    766  1.1  itojun int
    767  1.1  itojun pfctl_show_timeouts(int dev, int opts)
    768  1.1  itojun {
    769  1.1  itojun 	struct pfioc_tm pt;
    770  1.1  itojun 	int i;
    771  1.1  itojun 
    772  1.1  itojun 	if (opts & PF_OPT_SHOWALL)
    773  1.1  itojun 		pfctl_print_title("TIMEOUTS:");
    774  1.1  itojun 	memset(&pt, 0, sizeof(pt));
    775  1.1  itojun 	for (i = 0; pf_timeouts[i].name; i++) {
    776  1.1  itojun 		pt.timeout = pf_timeouts[i].timeout;
    777  1.1  itojun 		if (ioctl(dev, DIOCGETTIMEOUT, &pt))
    778  1.1  itojun 			err(1, "DIOCGETTIMEOUT");
    779  1.1  itojun 		printf("%-20s %10d", pf_timeouts[i].name, pt.seconds);
    780  1.3    yamt 		if (pf_timeouts[i].timeout >= PFTM_ADAPTIVE_START &&
    781  1.3    yamt 		    pf_timeouts[i].timeout <= PFTM_ADAPTIVE_END)
    782  1.1  itojun 			printf(" states");
    783  1.1  itojun 		else
    784  1.1  itojun 			printf("s");
    785  1.1  itojun 		printf("\n");
    786  1.1  itojun 	}
    787  1.1  itojun 	return (0);
    788  1.1  itojun 
    789  1.1  itojun }
    790  1.1  itojun 
    791  1.1  itojun int
    792  1.1  itojun pfctl_show_limits(int dev, int opts)
    793  1.1  itojun {
    794  1.1  itojun 	struct pfioc_limit pl;
    795  1.1  itojun 	int i;
    796  1.1  itojun 
    797  1.1  itojun 	if (opts & PF_OPT_SHOWALL)
    798  1.1  itojun 		pfctl_print_title("LIMITS:");
    799  1.1  itojun 	memset(&pl, 0, sizeof(pl));
    800  1.1  itojun 	for (i = 0; pf_limits[i].name; i++) {
    801  1.1  itojun 		pl.index = pf_limits[i].index;
    802  1.1  itojun 		if (ioctl(dev, DIOCGETLIMIT, &pl))
    803  1.1  itojun 			err(1, "DIOCGETLIMIT");
    804  1.1  itojun 		printf("%-10s ", pf_limits[i].name);
    805  1.1  itojun 		if (pl.limit == UINT_MAX)
    806  1.1  itojun 			printf("unlimited\n");
    807  1.1  itojun 		else
    808  1.1  itojun 			printf("hard limit %6u\n", pl.limit);
    809  1.1  itojun 	}
    810  1.1  itojun 	return (0);
    811  1.1  itojun }
    812  1.1  itojun 
    813  1.1  itojun /* callbacks for rule/nat/rdr/addr */
    814  1.1  itojun int
    815  1.1  itojun pfctl_add_pool(struct pfctl *pf, struct pf_pool *p, sa_family_t af)
    816  1.1  itojun {
    817  1.1  itojun 	struct pf_pooladdr *pa;
    818  1.1  itojun 
    819  1.1  itojun 	if ((pf->opts & PF_OPT_NOACTION) == 0) {
    820  1.1  itojun 		if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr))
    821  1.1  itojun 			err(1, "DIOCBEGINADDRS");
    822  1.1  itojun 	}
    823  1.1  itojun 
    824  1.1  itojun 	pf->paddr.af = af;
    825  1.1  itojun 	TAILQ_FOREACH(pa, &p->list, entries) {
    826  1.1  itojun 		memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr));
    827  1.1  itojun 		if ((pf->opts & PF_OPT_NOACTION) == 0) {
    828  1.1  itojun 			if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr))
    829  1.1  itojun 				err(1, "DIOCADDADDR");
    830  1.1  itojun 		}
    831  1.1  itojun 	}
    832  1.1  itojun 	return (0);
    833  1.1  itojun }
    834  1.1  itojun 
    835  1.1  itojun int
    836  1.3    yamt pfctl_add_rule(struct pfctl *pf, struct pf_rule *r, const char *anchor_call)
    837  1.1  itojun {
    838  1.1  itojun 	u_int8_t		rs_num;
    839  1.1  itojun 	struct pfioc_rule	pr;
    840  1.1  itojun 
    841  1.1  itojun 	switch (r->action) {
    842  1.1  itojun 	case PF_SCRUB:
    843  1.1  itojun 		if ((loadopt & PFCTL_FLAG_FILTER) == 0)
    844  1.1  itojun 			return (0);
    845  1.1  itojun 		rs_num = PF_RULESET_SCRUB;
    846  1.1  itojun 		break;
    847  1.1  itojun 	case PF_DROP:
    848  1.1  itojun 	case PF_PASS:
    849  1.1  itojun 		if ((loadopt & PFCTL_FLAG_FILTER) == 0)
    850  1.1  itojun 			return (0);
    851  1.1  itojun 		rs_num = PF_RULESET_FILTER;
    852  1.1  itojun 		break;
    853  1.1  itojun 	case PF_NAT:
    854  1.1  itojun 	case PF_NONAT:
    855  1.1  itojun 		if ((loadopt & PFCTL_FLAG_NAT) == 0)
    856  1.1  itojun 			return (0);
    857  1.1  itojun 		rs_num = PF_RULESET_NAT;
    858  1.1  itojun 		break;
    859  1.1  itojun 	case PF_RDR:
    860  1.1  itojun 	case PF_NORDR:
    861  1.1  itojun 		if ((loadopt & PFCTL_FLAG_NAT) == 0)
    862  1.1  itojun 			return (0);
    863  1.1  itojun 		rs_num = PF_RULESET_RDR;
    864  1.1  itojun 		break;
    865  1.1  itojun 	case PF_BINAT:
    866  1.1  itojun 	case PF_NOBINAT:
    867  1.1  itojun 		if ((loadopt & PFCTL_FLAG_NAT) == 0)
    868  1.1  itojun 			return (0);
    869  1.1  itojun 		rs_num = PF_RULESET_BINAT;
    870  1.1  itojun 		break;
    871  1.1  itojun 	default:
    872  1.3    yamt 		errx(1, "Invalid rule type %d", r->action);
    873  1.1  itojun 		break;
    874  1.1  itojun 	}
    875  1.1  itojun 
    876  1.3    yamt 
    877  1.3    yamt 	if ((pf->opts & PF_OPT_OPTIMIZE) && rs_num == PF_RULESET_FILTER) {
    878  1.3    yamt 		/*
    879  1.3    yamt 		 * We'll do an optimization post-pass before finally adding the
    880  1.3    yamt 		 * rules.  Then we'll disable the optimization flag and feed
    881  1.3    yamt 		 * the rules right back into this function.
    882  1.3    yamt 		 */
    883  1.3    yamt 		struct pf_opt_rule *pfr;
    884  1.3    yamt 		struct pf_pooladdr *pa;
    885  1.3    yamt 
    886  1.3    yamt 		if ((pfr = calloc(1, sizeof(*pfr))) == NULL)
    887  1.3    yamt 			err(1, "calloc");
    888  1.3    yamt 		memcpy(&pfr->por_rule, r, sizeof(*r));
    889  1.3    yamt 		if (strlcpy(pfr->por_anchor, anchor_call,
    890  1.3    yamt 		    sizeof(pfr->por_anchor)) >= sizeof(pfr->por_anchor))
    891  1.3    yamt 			errx(1, "pfctl_add_rule: strlcpy");
    892  1.3    yamt 		TAILQ_INSERT_TAIL(&pf->opt_queue, pfr, por_entry);
    893  1.3    yamt 
    894  1.3    yamt 		if (TAILQ_FIRST(&r->rpool.list) != NULL)  {
    895  1.3    yamt 			TAILQ_INIT(&pfr->por_rule.rpool.list);
    896  1.3    yamt 			while ((pa = TAILQ_FIRST(&r->rpool.list)) != NULL) {
    897  1.3    yamt 				TAILQ_REMOVE(&r->rpool.list, pa, entries);
    898  1.3    yamt 				TAILQ_INSERT_TAIL(&pfr->por_rule.rpool.list, pa,
    899  1.3    yamt 			    	entries);
    900  1.3    yamt 			}
    901  1.3    yamt 		} else {
    902  1.3    yamt 			memset(&pfr->por_rule.rpool, 0,
    903  1.3    yamt 			    sizeof(pfr->por_rule.rpool));
    904  1.3    yamt 
    905  1.3    yamt 		}
    906  1.3    yamt 		return (0);
    907  1.3    yamt 	}
    908  1.3    yamt 
    909  1.1  itojun 	if ((pf->opts & PF_OPT_NOACTION) == 0) {
    910  1.1  itojun 		bzero(&pr, sizeof(pr));
    911  1.1  itojun 		if (strlcpy(pr.anchor, pf->anchor, sizeof(pr.anchor)) >=
    912  1.3    yamt 		    sizeof(pr.anchor))
    913  1.1  itojun 			errx(1, "pfctl_add_rule: strlcpy");
    914  1.1  itojun 		if (pfctl_add_pool(pf, &r->rpool, r->af))
    915  1.1  itojun 			return (1);
    916  1.3    yamt 		pr.ticket = pfctl_get_ticket(pf->trans, rs_num, pf->anchor);
    917  1.1  itojun 		pr.pool_ticket = pf->paddr.ticket;
    918  1.1  itojun 		memcpy(&pr.rule, r, sizeof(pr.rule));
    919  1.3    yamt 		strlcpy(pr.anchor_call, anchor_call, sizeof(pr.anchor_call));
    920  1.1  itojun 		if (ioctl(pf->dev, DIOCADDRULE, &pr))
    921  1.1  itojun 			err(1, "DIOCADDRULE");
    922  1.1  itojun 	}
    923  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE)
    924  1.3    yamt 		print_rule(r, anchor_call, pf->opts & PF_OPT_VERBOSE2);
    925  1.1  itojun 	pfctl_clear_pool(&r->rpool);
    926  1.1  itojun 	return (0);
    927  1.1  itojun }
    928  1.1  itojun 
    929  1.1  itojun int
    930  1.1  itojun pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
    931  1.1  itojun {
    932  1.1  itojun 	if (altqsupport &&
    933  1.1  itojun 	    (loadopt & PFCTL_FLAG_ALTQ) != 0) {
    934  1.1  itojun 		memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq));
    935  1.1  itojun 		if ((pf->opts & PF_OPT_NOACTION) == 0) {
    936  1.1  itojun 			if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) {
    937  1.1  itojun 				if (errno == ENXIO)
    938  1.1  itojun 					errx(1, "qtype not configured");
    939  1.1  itojun 				else if (errno == ENODEV)
    940  1.1  itojun 					errx(1, "%s: driver does not support "
    941  1.1  itojun 					    "altq", a->ifname);
    942  1.1  itojun 				else
    943  1.1  itojun 					err(1, "DIOCADDALTQ");
    944  1.1  itojun 			}
    945  1.1  itojun 		}
    946  1.1  itojun 		pfaltq_store(&pf->paltq->altq);
    947  1.1  itojun 	}
    948  1.1  itojun 	return (0);
    949  1.1  itojun }
    950  1.1  itojun 
    951  1.1  itojun int
    952  1.1  itojun pfctl_rules(int dev, char *filename, int opts, char *anchorname,
    953  1.3    yamt     struct pfr_buffer *trans)
    954  1.1  itojun {
    955  1.1  itojun #define ERR(x) do { warn(x); goto _error; } while(0)
    956  1.1  itojun #define ERRX(x) do { warnx(x); goto _error; } while(0)
    957  1.1  itojun 
    958  1.1  itojun 	FILE			*fin;
    959  1.1  itojun 	struct pfr_buffer	*t, buf;
    960  1.1  itojun 	struct pfioc_altq	 pa;
    961  1.1  itojun 	struct pfctl		 pf;
    962  1.1  itojun 	struct pfr_table	 trs;
    963  1.1  itojun 	int			 osize;
    964  1.1  itojun 
    965  1.1  itojun 	if (trans == NULL) {
    966  1.1  itojun 	    bzero(&buf, sizeof(buf));
    967  1.1  itojun 	    buf.pfrb_type = PFRB_TRANS;
    968  1.1  itojun 	    t = &buf;
    969  1.1  itojun 	    osize = 0;
    970  1.1  itojun 	} else {
    971  1.1  itojun 	    t = trans;
    972  1.1  itojun 	    osize = t->pfrb_size;
    973  1.1  itojun 	}
    974  1.1  itojun 
    975  1.1  itojun 	memset(&pa, 0, sizeof(pa));
    976  1.1  itojun 	memset(&pf, 0, sizeof(pf));
    977  1.1  itojun 	memset(&trs, 0, sizeof(trs));
    978  1.1  itojun 	if (strlcpy(trs.pfrt_anchor, anchorname,
    979  1.3    yamt 	    sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor))
    980  1.1  itojun 		ERRX("pfctl_rules: strlcpy");
    981  1.1  itojun 	if (strcmp(filename, "-") == 0) {
    982  1.1  itojun 		fin = stdin;
    983  1.1  itojun 		infile = "stdin";
    984  1.1  itojun 	} else {
    985  1.3    yamt 		if ((fin = pfctl_fopen(filename, "r")) == NULL) {
    986  1.1  itojun 			warn("%s", filename);
    987  1.1  itojun 			return (1);
    988  1.1  itojun 		}
    989  1.1  itojun 		infile = filename;
    990  1.1  itojun 	}
    991  1.1  itojun 	pf.dev = dev;
    992  1.1  itojun 	pf.opts = opts;
    993  1.1  itojun 	pf.loadopt = loadopt;
    994  1.1  itojun 	if (anchorname[0])
    995  1.1  itojun 		pf.loadopt &= ~PFCTL_FLAG_ALTQ;
    996  1.1  itojun 	pf.paltq = &pa;
    997  1.1  itojun 	pf.trans = t;
    998  1.1  itojun 	pf.rule_nr = 0;
    999  1.1  itojun 	pf.anchor = anchorname;
   1000  1.3    yamt 	TAILQ_INIT(&pf.opt_queue);
   1001  1.1  itojun 
   1002  1.1  itojun 	if ((opts & PF_OPT_NOACTION) == 0) {
   1003  1.1  itojun 		if ((pf.loadopt & PFCTL_FLAG_NAT) != 0) {
   1004  1.3    yamt 			if (pfctl_add_trans(t, PF_RULESET_NAT, anchorname) ||
   1005  1.3    yamt 			    pfctl_add_trans(t, PF_RULESET_BINAT, anchorname) ||
   1006  1.3    yamt 			    pfctl_add_trans(t, PF_RULESET_RDR, anchorname))
   1007  1.1  itojun 				ERR("pfctl_rules");
   1008  1.1  itojun 		}
   1009  1.1  itojun 		if (((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))) {
   1010  1.3    yamt 			if (pfctl_add_trans(t, PF_RULESET_ALTQ, anchorname))
   1011  1.1  itojun 				ERR("pfctl_rules");
   1012  1.1  itojun 		}
   1013  1.1  itojun 		if ((pf.loadopt & PFCTL_FLAG_FILTER) != 0) {
   1014  1.3    yamt 			if (pfctl_add_trans(t, PF_RULESET_SCRUB, anchorname) ||
   1015  1.3    yamt 			    pfctl_add_trans(t, PF_RULESET_FILTER, anchorname))
   1016  1.1  itojun 				ERR("pfctl_rules");
   1017  1.1  itojun 		}
   1018  1.1  itojun 		if (pf.loadopt & PFCTL_FLAG_TABLE) {
   1019  1.3    yamt 			if (pfctl_add_trans(t, PF_RULESET_TABLE, anchorname))
   1020  1.1  itojun 				ERR("pfctl_rules");
   1021  1.1  itojun 		}
   1022  1.1  itojun 		if (pfctl_trans(dev, t, DIOCXBEGIN, osize))
   1023  1.1  itojun 			ERR("DIOCXBEGIN");
   1024  1.1  itojun 		if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ))
   1025  1.1  itojun 			pa.ticket = pfctl_get_ticket(t, PF_RULESET_ALTQ,
   1026  1.3    yamt 			    anchorname);
   1027  1.1  itojun 		if (pf.loadopt & PFCTL_FLAG_TABLE)
   1028  1.1  itojun 			pf.tticket = pfctl_get_ticket(t, PF_RULESET_TABLE,
   1029  1.3    yamt 			    anchorname);
   1030  1.1  itojun 	}
   1031  1.1  itojun 	if (parse_rules(fin, &pf) < 0) {
   1032  1.1  itojun 		if ((opts & PF_OPT_NOACTION) == 0)
   1033  1.1  itojun 			ERRX("Syntax error in config file: "
   1034  1.1  itojun 			    "pf rules not loaded");
   1035  1.1  itojun 		else
   1036  1.1  itojun 			goto _error;
   1037  1.1  itojun 	}
   1038  1.3    yamt 	if (pf.opts & PF_OPT_OPTIMIZE) {
   1039  1.3    yamt 		if (pfctl_optimize_rules(&pf))
   1040  1.3    yamt 			ERRX("Failed to optimize ruleset: pf rules not loaded");
   1041  1.3    yamt 	}
   1042  1.3    yamt 
   1043  1.1  itojun 	if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))
   1044  1.1  itojun 		if (check_commit_altq(dev, opts) != 0)
   1045  1.1  itojun 			ERRX("errors in altq config");
   1046  1.1  itojun 	if (fin != stdin)
   1047  1.1  itojun 		fclose(fin);
   1048  1.1  itojun 
   1049  1.1  itojun 	/* process "load anchor" directives */
   1050  1.3    yamt 	if (!anchorname[0])
   1051  1.1  itojun 		if (pfctl_load_anchors(dev, opts, t) == -1)
   1052  1.1  itojun 			ERRX("load anchors");
   1053  1.1  itojun 
   1054  1.1  itojun 	if (trans == NULL && (opts & PF_OPT_NOACTION) == 0)
   1055  1.1  itojun 		if (pfctl_trans(dev, t, DIOCXCOMMIT, 0))
   1056  1.1  itojun 			ERR("DIOCXCOMMIT");
   1057  1.1  itojun 	return (0);
   1058  1.1  itojun 
   1059  1.1  itojun _error:
   1060  1.1  itojun 	if (trans == NULL) {	/* main ruleset */
   1061  1.1  itojun 		if ((opts & PF_OPT_NOACTION) == 0)
   1062  1.1  itojun 			if (pfctl_trans(dev, t, DIOCXROLLBACK, 0))
   1063  1.1  itojun 				err(1, "DIOCXROLLBACK");
   1064  1.1  itojun 		exit(1);
   1065  1.1  itojun 	} else			/* sub ruleset */
   1066  1.1  itojun 		return (-1);
   1067  1.1  itojun 
   1068  1.1  itojun #undef ERR
   1069  1.1  itojun #undef ERRX
   1070  1.1  itojun }
   1071  1.1  itojun 
   1072  1.3    yamt FILE *
   1073  1.3    yamt pfctl_fopen(const char *name, const char *mode)
   1074  1.3    yamt {
   1075  1.3    yamt 	struct stat	 st;
   1076  1.3    yamt 	FILE		*fp;
   1077  1.3    yamt 
   1078  1.3    yamt 	fp = fopen(name, mode);
   1079  1.3    yamt 	if (fp == NULL)
   1080  1.3    yamt 		return (NULL);
   1081  1.3    yamt 	if (fstat(fileno(fp), &st)) {
   1082  1.3    yamt 		fclose(fp);
   1083  1.3    yamt 		return (NULL);
   1084  1.3    yamt 	}
   1085  1.3    yamt 	if (S_ISDIR(st.st_mode)) {
   1086  1.3    yamt 		fclose(fp);
   1087  1.3    yamt 		errno = EISDIR;
   1088  1.3    yamt 		return (NULL);
   1089  1.3    yamt 	}
   1090  1.3    yamt 	return (fp);
   1091  1.3    yamt }
   1092  1.3    yamt 
   1093  1.1  itojun int
   1094  1.1  itojun pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit)
   1095  1.1  itojun {
   1096  1.1  itojun 	struct pfioc_limit pl;
   1097  1.1  itojun 	int i;
   1098  1.1  itojun 
   1099  1.1  itojun 	if ((loadopt & PFCTL_FLAG_OPTION) == 0)
   1100  1.1  itojun 		return (0);
   1101  1.1  itojun 
   1102  1.1  itojun 	memset(&pl, 0, sizeof(pl));
   1103  1.1  itojun 	for (i = 0; pf_limits[i].name; i++) {
   1104  1.1  itojun 		if (strcasecmp(opt, pf_limits[i].name) == 0) {
   1105  1.1  itojun 			pl.index = pf_limits[i].index;
   1106  1.1  itojun 			pl.limit = limit;
   1107  1.1  itojun 			if ((pf->opts & PF_OPT_NOACTION) == 0) {
   1108  1.1  itojun 				if (ioctl(pf->dev, DIOCSETLIMIT, &pl)) {
   1109  1.1  itojun 					if (errno == EBUSY) {
   1110  1.1  itojun 						warnx("Current pool "
   1111  1.1  itojun 						    "size exceeds requested "
   1112  1.1  itojun 						    "hard limit");
   1113  1.1  itojun 						return (1);
   1114  1.1  itojun 					} else
   1115  1.1  itojun 						err(1, "DIOCSETLIMIT");
   1116  1.1  itojun 				}
   1117  1.1  itojun 			}
   1118  1.1  itojun 			break;
   1119  1.1  itojun 		}
   1120  1.1  itojun 	}
   1121  1.1  itojun 	if (pf_limits[i].name == NULL) {
   1122  1.1  itojun 		warnx("Bad pool name.");
   1123  1.1  itojun 		return (1);
   1124  1.1  itojun 	}
   1125  1.1  itojun 
   1126  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE)
   1127  1.1  itojun 		printf("set limit %s %d\n", opt, limit);
   1128  1.1  itojun 
   1129  1.1  itojun 	return (0);
   1130  1.1  itojun }
   1131  1.1  itojun 
   1132  1.1  itojun int
   1133  1.1  itojun pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
   1134  1.1  itojun {
   1135  1.1  itojun 	struct pfioc_tm pt;
   1136  1.1  itojun 	int i;
   1137  1.1  itojun 
   1138  1.1  itojun 	if ((loadopt & PFCTL_FLAG_OPTION) == 0)
   1139  1.1  itojun 		return (0);
   1140  1.1  itojun 
   1141  1.1  itojun 	memset(&pt, 0, sizeof(pt));
   1142  1.1  itojun 	for (i = 0; pf_timeouts[i].name; i++) {
   1143  1.1  itojun 		if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
   1144  1.1  itojun 			pt.timeout = pf_timeouts[i].timeout;
   1145  1.1  itojun 			break;
   1146  1.1  itojun 		}
   1147  1.1  itojun 	}
   1148  1.1  itojun 
   1149  1.1  itojun 	if (pf_timeouts[i].name == NULL) {
   1150  1.1  itojun 		warnx("Bad timeout name.");
   1151  1.1  itojun 		return (1);
   1152  1.1  itojun 	}
   1153  1.1  itojun 
   1154  1.1  itojun 	pt.seconds = seconds;
   1155  1.1  itojun 	if ((pf->opts & PF_OPT_NOACTION) == 0) {
   1156  1.1  itojun 		if (ioctl(pf->dev, DIOCSETTIMEOUT, &pt))
   1157  1.1  itojun 			err(1, "DIOCSETTIMEOUT");
   1158  1.1  itojun 	}
   1159  1.1  itojun 
   1160  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE && ! quiet)
   1161  1.1  itojun 		printf("set timeout %s %d\n", opt, seconds);
   1162  1.1  itojun 
   1163  1.1  itojun 	return (0);
   1164  1.1  itojun }
   1165  1.1  itojun 
   1166  1.1  itojun int
   1167  1.1  itojun pfctl_set_optimization(struct pfctl *pf, const char *opt)
   1168  1.1  itojun {
   1169  1.1  itojun 	const struct pf_hint *hint;
   1170  1.1  itojun 	int i, r;
   1171  1.1  itojun 
   1172  1.1  itojun 	if ((loadopt & PFCTL_FLAG_OPTION) == 0)
   1173  1.1  itojun 		return (0);
   1174  1.1  itojun 
   1175  1.1  itojun 	for (i = 0; pf_hints[i].name; i++)
   1176  1.1  itojun 		if (strcasecmp(opt, pf_hints[i].name) == 0)
   1177  1.1  itojun 			break;
   1178  1.1  itojun 
   1179  1.1  itojun 	hint = pf_hints[i].hint;
   1180  1.1  itojun 	if (hint == NULL) {
   1181  1.1  itojun 		warnx("Bad hint name.");
   1182  1.1  itojun 		return (1);
   1183  1.1  itojun 	}
   1184  1.1  itojun 
   1185  1.1  itojun 	for (i = 0; hint[i].name; i++)
   1186  1.1  itojun 		if ((r = pfctl_set_timeout(pf, hint[i].name,
   1187  1.1  itojun 		    hint[i].timeout, 1)))
   1188  1.1  itojun 			return (r);
   1189  1.1  itojun 
   1190  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE)
   1191  1.1  itojun 		printf("set optimization %s\n", opt);
   1192  1.1  itojun 
   1193  1.1  itojun 	return (0);
   1194  1.1  itojun }
   1195  1.1  itojun 
   1196  1.1  itojun int
   1197  1.1  itojun pfctl_set_logif(struct pfctl *pf, char *ifname)
   1198  1.1  itojun {
   1199  1.1  itojun 	struct pfioc_if pi;
   1200  1.1  itojun 
   1201  1.1  itojun 	if ((loadopt & PFCTL_FLAG_OPTION) == 0)
   1202  1.1  itojun 		return (0);
   1203  1.1  itojun 
   1204  1.1  itojun 	memset(&pi, 0, sizeof(pi));
   1205  1.1  itojun 	if ((pf->opts & PF_OPT_NOACTION) == 0) {
   1206  1.1  itojun 		if (!strcmp(ifname, "none"))
   1207  1.1  itojun 			bzero(pi.ifname, sizeof(pi.ifname));
   1208  1.1  itojun 		else {
   1209  1.1  itojun 			if (strlcpy(pi.ifname, ifname,
   1210  1.1  itojun 			    sizeof(pi.ifname)) >= sizeof(pi.ifname))
   1211  1.1  itojun 				errx(1, "pfctl_set_logif: strlcpy");
   1212  1.1  itojun 		}
   1213  1.1  itojun 		if (ioctl(pf->dev, DIOCSETSTATUSIF, &pi))
   1214  1.1  itojun 			err(1, "DIOCSETSTATUSIF");
   1215  1.1  itojun 	}
   1216  1.1  itojun 
   1217  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE)
   1218  1.1  itojun 		printf("set loginterface %s\n", ifname);
   1219  1.1  itojun 
   1220  1.1  itojun 	return (0);
   1221  1.1  itojun }
   1222  1.1  itojun 
   1223  1.1  itojun int
   1224  1.1  itojun pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
   1225  1.1  itojun {
   1226  1.1  itojun 	if ((loadopt & PFCTL_FLAG_OPTION) == 0)
   1227  1.1  itojun 		return (0);
   1228  1.1  itojun 
   1229  1.1  itojun 	HTONL(hostid);
   1230  1.1  itojun 
   1231  1.1  itojun 	if ((pf->opts & PF_OPT_NOACTION) == 0)
   1232  1.1  itojun 		if (ioctl(dev, DIOCSETHOSTID, &hostid))
   1233  1.1  itojun 			err(1, "DIOCSETHOSTID");
   1234  1.1  itojun 
   1235  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE)
   1236  1.1  itojun 		printf("set hostid 0x%08x\n", ntohl(hostid));
   1237  1.1  itojun 
   1238  1.1  itojun 	return (0);
   1239  1.1  itojun }
   1240  1.1  itojun 
   1241  1.1  itojun int
   1242  1.1  itojun pfctl_set_debug(struct pfctl *pf, char *d)
   1243  1.1  itojun {
   1244  1.1  itojun 	u_int32_t	level;
   1245  1.1  itojun 
   1246  1.1  itojun 	if ((loadopt & PFCTL_FLAG_OPTION) == 0)
   1247  1.1  itojun 		return (0);
   1248  1.1  itojun 
   1249  1.1  itojun 	if (!strcmp(d, "none"))
   1250  1.1  itojun 		level = PF_DEBUG_NONE;
   1251  1.1  itojun 	else if (!strcmp(d, "urgent"))
   1252  1.1  itojun 		level = PF_DEBUG_URGENT;
   1253  1.1  itojun 	else if (!strcmp(d, "misc"))
   1254  1.1  itojun 		level = PF_DEBUG_MISC;
   1255  1.1  itojun 	else if (!strcmp(d, "loud"))
   1256  1.1  itojun 		level = PF_DEBUG_NOISY;
   1257  1.1  itojun 	else {
   1258  1.1  itojun 		warnx("unknown debug level \"%s\"", d);
   1259  1.1  itojun 		return (-1);
   1260  1.1  itojun 	}
   1261  1.1  itojun 
   1262  1.1  itojun 	if ((pf->opts & PF_OPT_NOACTION) == 0)
   1263  1.1  itojun 		if (ioctl(dev, DIOCSETDEBUG, &level))
   1264  1.1  itojun 			err(1, "DIOCSETDEBUG");
   1265  1.1  itojun 
   1266  1.1  itojun 	if (pf->opts & PF_OPT_VERBOSE)
   1267  1.1  itojun 		printf("set debug %s\n", d);
   1268  1.1  itojun 
   1269  1.1  itojun 	return (0);
   1270  1.1  itojun }
   1271  1.1  itojun 
   1272  1.1  itojun int
   1273  1.1  itojun pfctl_debug(int dev, u_int32_t level, int opts)
   1274  1.1  itojun {
   1275  1.1  itojun 	if (ioctl(dev, DIOCSETDEBUG, &level))
   1276  1.1  itojun 		err(1, "DIOCSETDEBUG");
   1277  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0) {
   1278  1.1  itojun 		fprintf(stderr, "debug level set to '");
   1279  1.1  itojun 		switch (level) {
   1280  1.1  itojun 		case PF_DEBUG_NONE:
   1281  1.1  itojun 			fprintf(stderr, "none");
   1282  1.1  itojun 			break;
   1283  1.1  itojun 		case PF_DEBUG_URGENT:
   1284  1.1  itojun 			fprintf(stderr, "urgent");
   1285  1.1  itojun 			break;
   1286  1.1  itojun 		case PF_DEBUG_MISC:
   1287  1.1  itojun 			fprintf(stderr, "misc");
   1288  1.1  itojun 			break;
   1289  1.1  itojun 		case PF_DEBUG_NOISY:
   1290  1.1  itojun 			fprintf(stderr, "loud");
   1291  1.1  itojun 			break;
   1292  1.1  itojun 		default:
   1293  1.1  itojun 			fprintf(stderr, "<invalid>");
   1294  1.1  itojun 			break;
   1295  1.1  itojun 		}
   1296  1.1  itojun 		fprintf(stderr, "'\n");
   1297  1.1  itojun 	}
   1298  1.1  itojun 	return (0);
   1299  1.1  itojun }
   1300  1.1  itojun 
   1301  1.1  itojun int
   1302  1.1  itojun pfctl_clear_rule_counters(int dev, int opts)
   1303  1.1  itojun {
   1304  1.1  itojun 	if (ioctl(dev, DIOCCLRRULECTRS))
   1305  1.1  itojun 		err(1, "DIOCCLRRULECTRS");
   1306  1.1  itojun 	if ((opts & PF_OPT_QUIET) == 0)
   1307  1.1  itojun 		fprintf(stderr, "pf: rule counters cleared\n");
   1308  1.1  itojun 	return (0);
   1309  1.1  itojun }
   1310  1.1  itojun 
   1311  1.1  itojun int
   1312  1.1  itojun pfctl_test_altqsupport(int dev, int opts)
   1313  1.1  itojun {
   1314  1.1  itojun 	struct pfioc_altq pa;
   1315  1.1  itojun 
   1316  1.1  itojun 	if (ioctl(dev, DIOCGETALTQS, &pa)) {
   1317  1.1  itojun 		if (errno == ENODEV) {
   1318  1.1  itojun 			if (!(opts & PF_OPT_QUIET))
   1319  1.1  itojun 				fprintf(stderr, "No ALTQ support in kernel\n"
   1320  1.1  itojun 				    "ALTQ related functions disabled\n");
   1321  1.1  itojun 			return (0);
   1322  1.1  itojun 		} else
   1323  1.1  itojun 			err(1, "DIOCGETALTQS");
   1324  1.1  itojun 	}
   1325  1.1  itojun 	return (1);
   1326  1.1  itojun }
   1327  1.1  itojun 
   1328  1.1  itojun int
   1329  1.1  itojun pfctl_show_anchors(int dev, int opts, char *anchorname)
   1330  1.1  itojun {
   1331  1.3    yamt 	struct pfioc_ruleset	 pr;
   1332  1.3    yamt 	u_int32_t		 mnr, nr;
   1333  1.1  itojun 
   1334  1.3    yamt 	memset(&pr, 0, sizeof(pr));
   1335  1.3    yamt 	memcpy(pr.path, anchorname, sizeof(pr.path));
   1336  1.3    yamt 	if (ioctl(dev, DIOCGETRULESETS, &pr)) {
   1337  1.3    yamt 		if (errno == EINVAL)
   1338  1.3    yamt 			fprintf(stderr, "Anchor '%s' not found.\n",
   1339  1.3    yamt 			    anchorname);
   1340  1.3    yamt 		else
   1341  1.3    yamt 			err(1, "DIOCGETRULESETS");
   1342  1.3    yamt 		return (-1);
   1343  1.3    yamt 	}
   1344  1.3    yamt 	mnr = pr.nr;
   1345  1.3    yamt 	for (nr = 0; nr < mnr; ++nr) {
   1346  1.3    yamt 		char sub[MAXPATHLEN];
   1347  1.1  itojun 
   1348  1.3    yamt 		pr.nr = nr;
   1349  1.3    yamt 		if (ioctl(dev, DIOCGETRULESET, &pr))
   1350  1.3    yamt 			err(1, "DIOCGETRULESET");
   1351  1.3    yamt 		if (!strcmp(pr.name, PF_RESERVED_ANCHOR))
   1352  1.3    yamt 			continue;
   1353  1.3    yamt 		sub[0] = 0;
   1354  1.3    yamt 		if (pr.path[0]) {
   1355  1.3    yamt 			strlcat(sub, pr.path, sizeof(sub));
   1356  1.3    yamt 			strlcat(sub, "/", sizeof(sub));
   1357  1.3    yamt 		}
   1358  1.3    yamt 		strlcat(sub, pr.name, sizeof(sub));
   1359  1.3    yamt 		printf("  %s\n", sub);
   1360  1.3    yamt 		if (opts & PF_OPT_VERBOSE && pfctl_show_anchors(dev, opts, sub))
   1361  1.1  itojun 			return (-1);
   1362  1.1  itojun 	}
   1363  1.1  itojun 	return (0);
   1364  1.1  itojun }
   1365  1.1  itojun 
   1366  1.1  itojun const char *
   1367  1.1  itojun pfctl_lookup_option(char *cmd, const char **list)
   1368  1.1  itojun {
   1369  1.1  itojun 	if (cmd != NULL && *cmd)
   1370  1.1  itojun 		for (; *list; list++)
   1371  1.1  itojun 			if (!strncmp(cmd, *list, strlen(cmd)))
   1372  1.1  itojun 				return (*list);
   1373  1.1  itojun 	return (NULL);
   1374  1.1  itojun }
   1375  1.1  itojun 
   1376  1.1  itojun int
   1377  1.1  itojun main(int argc, char *argv[])
   1378  1.1  itojun {
   1379  1.1  itojun 	int	error = 0;
   1380  1.1  itojun 	int	ch;
   1381  1.1  itojun 	int	mode = O_RDONLY;
   1382  1.1  itojun 	int	opts = 0;
   1383  1.3    yamt 	char	anchorname[MAXPATHLEN];
   1384  1.1  itojun 
   1385  1.1  itojun 	if (argc < 2)
   1386  1.1  itojun 		usage();
   1387  1.1  itojun 
   1388  1.1  itojun 	while ((ch = getopt(argc, argv,
   1389  1.3    yamt 	    "a:AdD:eqf:F:ghi:k:nNOop:rRs:t:T:vx:z")) != -1) {
   1390  1.1  itojun 		switch (ch) {
   1391  1.1  itojun 		case 'a':
   1392  1.1  itojun 			anchoropt = optarg;
   1393  1.1  itojun 			break;
   1394  1.1  itojun 		case 'd':
   1395  1.1  itojun 			opts |= PF_OPT_DISABLE;
   1396  1.1  itojun 			mode = O_RDWR;
   1397  1.1  itojun 			break;
   1398  1.1  itojun 		case 'D':
   1399  1.1  itojun 			if (pfctl_cmdline_symset(optarg) < 0)
   1400  1.1  itojun 				warnx("could not parse macro definition %s",
   1401  1.1  itojun 				    optarg);
   1402  1.1  itojun 			break;
   1403  1.1  itojun 		case 'e':
   1404  1.1  itojun 			opts |= PF_OPT_ENABLE;
   1405  1.1  itojun 			mode = O_RDWR;
   1406  1.1  itojun 			break;
   1407  1.1  itojun 		case 'q':
   1408  1.1  itojun 			opts |= PF_OPT_QUIET;
   1409  1.1  itojun 			break;
   1410  1.1  itojun 		case 'F':
   1411  1.1  itojun 			clearopt = pfctl_lookup_option(optarg, clearopt_list);
   1412  1.1  itojun 			if (clearopt == NULL) {
   1413  1.1  itojun 				warnx("Unknown flush modifier '%s'", optarg);
   1414  1.1  itojun 				usage();
   1415  1.1  itojun 			}
   1416  1.1  itojun 			mode = O_RDWR;
   1417  1.1  itojun 			break;
   1418  1.1  itojun 		case 'i':
   1419  1.1  itojun 			ifaceopt = optarg;
   1420  1.1  itojun 			break;
   1421  1.1  itojun 		case 'k':
   1422  1.1  itojun 			if (state_killers >= 2) {
   1423  1.1  itojun 				warnx("can only specify -k twice");
   1424  1.1  itojun 				usage();
   1425  1.1  itojun 				/* NOTREACHED */
   1426  1.1  itojun 			}
   1427  1.1  itojun 			state_kill[state_killers++] = optarg;
   1428  1.1  itojun 			mode = O_RDWR;
   1429  1.1  itojun 			break;
   1430  1.1  itojun 		case 'n':
   1431  1.1  itojun 			opts |= PF_OPT_NOACTION;
   1432  1.1  itojun 			break;
   1433  1.1  itojun 		case 'N':
   1434  1.1  itojun 			loadopt |= PFCTL_FLAG_NAT;
   1435  1.1  itojun 			break;
   1436  1.1  itojun 		case 'r':
   1437  1.1  itojun 			opts |= PF_OPT_USEDNS;
   1438  1.1  itojun 			break;
   1439  1.1  itojun 		case 'f':
   1440  1.1  itojun 			rulesopt = optarg;
   1441  1.1  itojun 			mode = O_RDWR;
   1442  1.1  itojun 			break;
   1443  1.1  itojun 		case 'g':
   1444  1.1  itojun 			opts |= PF_OPT_DEBUG;
   1445  1.1  itojun 			break;
   1446  1.1  itojun 		case 'A':
   1447  1.1  itojun 			loadopt |= PFCTL_FLAG_ALTQ;
   1448  1.1  itojun 			break;
   1449  1.1  itojun 		case 'R':
   1450  1.1  itojun 			loadopt |= PFCTL_FLAG_FILTER;
   1451  1.1  itojun 			break;
   1452  1.3    yamt 		case 'o':
   1453  1.3    yamt 			if (opts & PF_OPT_OPTIMIZE)
   1454  1.3    yamt 				opts |= PF_OPT_OPTIMIZE_PROFILE;
   1455  1.3    yamt 			else
   1456  1.3    yamt 				opts |= PF_OPT_OPTIMIZE;
   1457  1.3    yamt 			break;
   1458  1.1  itojun 		case 'O':
   1459  1.1  itojun 			loadopt |= PFCTL_FLAG_OPTION;
   1460  1.1  itojun 			break;
   1461  1.1  itojun 		case 'p':
   1462  1.1  itojun 			pf_device = optarg;
   1463  1.1  itojun 			break;
   1464  1.1  itojun 		case 's':
   1465  1.1  itojun 			showopt = pfctl_lookup_option(optarg, showopt_list);
   1466  1.1  itojun 			if (showopt == NULL) {
   1467  1.1  itojun 				warnx("Unknown show modifier '%s'", optarg);
   1468  1.1  itojun 				usage();
   1469  1.1  itojun 			}
   1470  1.1  itojun 			break;
   1471  1.1  itojun 		case 't':
   1472  1.1  itojun 			tableopt = optarg;
   1473  1.1  itojun 			break;
   1474  1.1  itojun 		case 'T':
   1475  1.1  itojun 			tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
   1476  1.1  itojun 			if (tblcmdopt == NULL) {
   1477  1.1  itojun 				warnx("Unknown table command '%s'", optarg);
   1478  1.1  itojun 				usage();
   1479  1.1  itojun 			}
   1480  1.1  itojun 			break;
   1481  1.1  itojun 		case 'v':
   1482  1.1  itojun 			if (opts & PF_OPT_VERBOSE)
   1483  1.1  itojun 				opts |= PF_OPT_VERBOSE2;
   1484  1.1  itojun 			opts |= PF_OPT_VERBOSE;
   1485  1.1  itojun 			break;
   1486  1.1  itojun 		case 'x':
   1487  1.1  itojun 			debugopt = pfctl_lookup_option(optarg, debugopt_list);
   1488  1.1  itojun 			if (debugopt == NULL) {
   1489  1.1  itojun 				warnx("Unknown debug level '%s'", optarg);
   1490  1.1  itojun 				usage();
   1491  1.1  itojun 			}
   1492  1.1  itojun 			mode = O_RDWR;
   1493  1.1  itojun 			break;
   1494  1.1  itojun 		case 'z':
   1495  1.1  itojun 			opts |= PF_OPT_CLRRULECTRS;
   1496  1.1  itojun 			mode = O_RDWR;
   1497  1.1  itojun 			break;
   1498  1.1  itojun 		case 'h':
   1499  1.1  itojun 			/* FALLTHROUGH */
   1500  1.1  itojun 		default:
   1501  1.1  itojun 			usage();
   1502  1.1  itojun 			/* NOTREACHED */
   1503  1.1  itojun 		}
   1504  1.1  itojun 	}
   1505  1.1  itojun 
   1506  1.1  itojun 	if (tblcmdopt != NULL) {
   1507  1.1  itojun 		argc -= optind;
   1508  1.1  itojun 		argv += optind;
   1509  1.1  itojun 		ch = *tblcmdopt;
   1510  1.1  itojun 		if (ch == 'l') {
   1511  1.1  itojun 			loadopt |= PFCTL_FLAG_TABLE;
   1512  1.1  itojun 			tblcmdopt = NULL;
   1513  1.1  itojun 		} else
   1514  1.1  itojun 			mode = strchr("acdfkrz", ch) ? O_RDWR : O_RDONLY;
   1515  1.1  itojun 	} else if (argc != optind) {
   1516  1.1  itojun 		warnx("unknown command line argument: %s ...", argv[optind]);
   1517  1.1  itojun 		usage();
   1518  1.1  itojun 		/* NOTREACHED */
   1519  1.1  itojun 	}
   1520  1.1  itojun 	if (loadopt == 0)
   1521  1.1  itojun 		loadopt = ~0;
   1522  1.1  itojun 
   1523  1.1  itojun 	memset(anchorname, 0, sizeof(anchorname));
   1524  1.1  itojun 	if (anchoropt != NULL) {
   1525  1.3    yamt 		if (strlcpy(anchorname, anchoropt,
   1526  1.3    yamt 		    sizeof(anchorname)) >= sizeof(anchorname))
   1527  1.3    yamt 			errx(1, "anchor name '%s' too long",
   1528  1.3    yamt 			    anchoropt);
   1529  1.1  itojun 		loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE;
   1530  1.1  itojun 	}
   1531  1.1  itojun 
   1532  1.1  itojun 	if ((opts & PF_OPT_NOACTION) == 0) {
   1533  1.1  itojun 		dev = open(pf_device, mode);
   1534  1.1  itojun 		if (dev == -1)
   1535  1.1  itojun 			err(1, "%s", pf_device);
   1536  1.1  itojun 		altqsupport = pfctl_test_altqsupport(dev, opts);
   1537  1.1  itojun 	} else {
   1538  1.1  itojun 		dev = open(pf_device, O_RDONLY);
   1539  1.1  itojun 		if (dev >= 0)
   1540  1.1  itojun 			opts |= PF_OPT_DUMMYACTION;
   1541  1.1  itojun 		/* turn off options */
   1542  1.1  itojun 		opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
   1543  1.1  itojun 		clearopt = showopt = debugopt = NULL;
   1544  1.1  itojun 		altqsupport = 1;
   1545  1.1  itojun 	}
   1546  1.1  itojun 
   1547  1.1  itojun 	if (opts & PF_OPT_DISABLE)
   1548  1.1  itojun 		if (pfctl_disable(dev, opts))
   1549  1.1  itojun 			error = 1;
   1550  1.1  itojun 
   1551  1.1  itojun 	if (showopt != NULL) {
   1552  1.1  itojun 		switch (*showopt) {
   1553  1.1  itojun 		case 'A':
   1554  1.1  itojun 			pfctl_show_anchors(dev, opts, anchorname);
   1555  1.1  itojun 			break;
   1556  1.1  itojun 		case 'r':
   1557  1.1  itojun 			pfctl_load_fingerprints(dev, opts);
   1558  1.3    yamt 			pfctl_show_rules(dev, opts, 0, anchorname);
   1559  1.1  itojun 			break;
   1560  1.1  itojun 		case 'l':
   1561  1.1  itojun 			pfctl_load_fingerprints(dev, opts);
   1562  1.3    yamt 			pfctl_show_rules(dev, opts, 1, anchorname);
   1563  1.1  itojun 			break;
   1564  1.1  itojun 		case 'n':
   1565  1.1  itojun 			pfctl_load_fingerprints(dev, opts);
   1566  1.3    yamt 			pfctl_show_nat(dev, opts, anchorname);
   1567  1.1  itojun 			break;
   1568  1.1  itojun 		case 'q':
   1569  1.1  itojun 			pfctl_show_altq(dev, ifaceopt, opts,
   1570  1.1  itojun 			    opts & PF_OPT_VERBOSE2);
   1571  1.1  itojun 			break;
   1572  1.1  itojun 		case 's':
   1573  1.1  itojun 			pfctl_show_states(dev, ifaceopt, opts);
   1574  1.1  itojun 			break;
   1575  1.1  itojun 		case 'S':
   1576  1.1  itojun 			pfctl_show_src_nodes(dev, opts);
   1577  1.1  itojun 			break;
   1578  1.1  itojun 		case 'i':
   1579  1.1  itojun 			pfctl_show_status(dev, opts);
   1580  1.1  itojun 			break;
   1581  1.1  itojun 		case 't':
   1582  1.1  itojun 			pfctl_show_timeouts(dev, opts);
   1583  1.1  itojun 			break;
   1584  1.1  itojun 		case 'm':
   1585  1.1  itojun 			pfctl_show_limits(dev, opts);
   1586  1.1  itojun 			break;
   1587  1.1  itojun 		case 'a':
   1588  1.1  itojun 			opts |= PF_OPT_SHOWALL;
   1589  1.1  itojun 			pfctl_load_fingerprints(dev, opts);
   1590  1.1  itojun 
   1591  1.3    yamt 			pfctl_show_nat(dev, opts, anchorname);
   1592  1.3    yamt 			pfctl_show_rules(dev, opts, 0, anchorname);
   1593  1.1  itojun 			pfctl_show_altq(dev, ifaceopt, opts, 0);
   1594  1.1  itojun 			pfctl_show_states(dev, ifaceopt, opts);
   1595  1.1  itojun 			pfctl_show_src_nodes(dev, opts);
   1596  1.1  itojun 			pfctl_show_status(dev, opts);
   1597  1.3    yamt 			pfctl_show_rules(dev, opts, 1, anchorname);
   1598  1.1  itojun 			pfctl_show_timeouts(dev, opts);
   1599  1.1  itojun 			pfctl_show_limits(dev, opts);
   1600  1.3    yamt 			pfctl_show_tables(anchorname, opts);
   1601  1.1  itojun 			pfctl_show_fingerprints(opts);
   1602  1.1  itojun 			break;
   1603  1.1  itojun 		case 'T':
   1604  1.3    yamt 			pfctl_show_tables(anchorname, opts);
   1605  1.1  itojun 			break;
   1606  1.1  itojun 		case 'o':
   1607  1.1  itojun 			pfctl_load_fingerprints(dev, opts);
   1608  1.1  itojun 			pfctl_show_fingerprints(opts);
   1609  1.1  itojun 			break;
   1610  1.1  itojun 		case 'I':
   1611  1.1  itojun 			pfctl_show_ifaces(ifaceopt, opts);
   1612  1.1  itojun 			break;
   1613  1.1  itojun 		}
   1614  1.1  itojun 	}
   1615  1.1  itojun 
   1616  1.1  itojun 	if (clearopt != NULL) {
   1617  1.1  itojun 		switch (*clearopt) {
   1618  1.1  itojun 		case 'r':
   1619  1.3    yamt 			pfctl_clear_rules(dev, opts, anchorname);
   1620  1.1  itojun 			break;
   1621  1.1  itojun 		case 'n':
   1622  1.3    yamt 			pfctl_clear_nat(dev, opts, anchorname);
   1623  1.1  itojun 			break;
   1624  1.1  itojun 		case 'q':
   1625  1.1  itojun 			pfctl_clear_altq(dev, opts);
   1626  1.1  itojun 			break;
   1627  1.1  itojun 		case 's':
   1628  1.1  itojun 			pfctl_clear_states(dev, ifaceopt, opts);
   1629  1.1  itojun 			break;
   1630  1.1  itojun 		case 'S':
   1631  1.1  itojun 			pfctl_clear_src_nodes(dev, opts);
   1632  1.1  itojun 			break;
   1633  1.1  itojun 		case 'i':
   1634  1.1  itojun 			pfctl_clear_stats(dev, opts);
   1635  1.1  itojun 			break;
   1636  1.1  itojun 		case 'a':
   1637  1.3    yamt 			pfctl_clear_rules(dev, opts, anchorname);
   1638  1.3    yamt 			pfctl_clear_nat(dev, opts, anchorname);
   1639  1.3    yamt 			pfctl_clear_tables(anchorname, opts);
   1640  1.3    yamt 			if (!*anchorname) {
   1641  1.1  itojun 				pfctl_clear_altq(dev, opts);
   1642  1.1  itojun 				pfctl_clear_states(dev, ifaceopt, opts);
   1643  1.1  itojun 				pfctl_clear_src_nodes(dev, opts);
   1644  1.1  itojun 				pfctl_clear_stats(dev, opts);
   1645  1.1  itojun 				pfctl_clear_fingerprints(dev, opts);
   1646  1.1  itojun 			}
   1647  1.1  itojun 			break;
   1648  1.1  itojun 		case 'o':
   1649  1.1  itojun 			pfctl_clear_fingerprints(dev, opts);
   1650  1.1  itojun 			break;
   1651  1.1  itojun 		case 'T':
   1652  1.3    yamt 			pfctl_clear_tables(anchorname, opts);
   1653  1.1  itojun 			break;
   1654  1.1  itojun 		}
   1655  1.1  itojun 	}
   1656  1.1  itojun 	if (state_killers)
   1657  1.1  itojun 		pfctl_kill_states(dev, ifaceopt, opts);
   1658  1.1  itojun 
   1659  1.1  itojun 	if (tblcmdopt != NULL) {
   1660  1.1  itojun 		error = pfctl_command_tables(argc, argv, tableopt,
   1661  1.3    yamt 		    tblcmdopt, rulesopt, anchorname, opts);
   1662  1.1  itojun 		rulesopt = NULL;
   1663  1.1  itojun 	}
   1664  1.1  itojun 
   1665  1.1  itojun 	if (rulesopt != NULL)
   1666  1.1  itojun 		if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
   1667  1.1  itojun 			error = 1;
   1668  1.1  itojun 
   1669  1.1  itojun 	if (rulesopt != NULL) {
   1670  1.3    yamt 		if (pfctl_rules(dev, rulesopt, opts, anchorname, NULL))
   1671  1.1  itojun 			error = 1;
   1672  1.1  itojun 		else if (!(opts & PF_OPT_NOACTION) &&
   1673  1.1  itojun 		    (loadopt & PFCTL_FLAG_TABLE))
   1674  1.1  itojun 			warn_namespace_collision(NULL);
   1675  1.1  itojun 	}
   1676  1.1  itojun 
   1677  1.1  itojun 	if (opts & PF_OPT_ENABLE)
   1678  1.1  itojun 		if (pfctl_enable(dev, opts))
   1679  1.1  itojun 			error = 1;
   1680  1.1  itojun 
   1681  1.1  itojun 	if (debugopt != NULL) {
   1682  1.1  itojun 		switch (*debugopt) {
   1683  1.1  itojun 		case 'n':
   1684  1.1  itojun 			pfctl_debug(dev, PF_DEBUG_NONE, opts);
   1685  1.1  itojun 			break;
   1686  1.1  itojun 		case 'u':
   1687  1.1  itojun 			pfctl_debug(dev, PF_DEBUG_URGENT, opts);
   1688  1.1  itojun 			break;
   1689  1.1  itojun 		case 'm':
   1690  1.1  itojun 			pfctl_debug(dev, PF_DEBUG_MISC, opts);
   1691  1.1  itojun 			break;
   1692  1.1  itojun 		case 'l':
   1693  1.1  itojun 			pfctl_debug(dev, PF_DEBUG_NOISY, opts);
   1694  1.1  itojun 			break;
   1695  1.1  itojun 		}
   1696  1.1  itojun 	}
   1697  1.1  itojun 
   1698  1.1  itojun 	if (opts & PF_OPT_CLRRULECTRS) {
   1699  1.1  itojun 		if (pfctl_clear_rule_counters(dev, opts))
   1700  1.1  itojun 			error = 1;
   1701  1.1  itojun 	}
   1702  1.1  itojun 	exit(error);
   1703  1.1  itojun }
   1704