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