Home | History | Annotate | Line # | Download | only in pfctl
pfctl_table.c revision 1.1
      1  1.1  itojun /*	$OpenBSD: pfctl_table.c,v 1.59 2004/03/15 15:25:44 dhartmei Exp $ */
      2  1.1  itojun 
      3  1.1  itojun /*
      4  1.1  itojun  * Copyright (c) 2002 Cedric Berger
      5  1.1  itojun  * All rights reserved.
      6  1.1  itojun  *
      7  1.1  itojun  * Redistribution and use in source and binary forms, with or without
      8  1.1  itojun  * modification, are permitted provided that the following conditions
      9  1.1  itojun  * are met:
     10  1.1  itojun  *
     11  1.1  itojun  *    - Redistributions of source code must retain the above copyright
     12  1.1  itojun  *      notice, this list of conditions and the following disclaimer.
     13  1.1  itojun  *    - Redistributions in binary form must reproduce the above
     14  1.1  itojun  *      copyright notice, this list of conditions and the following
     15  1.1  itojun  *      disclaimer in the documentation and/or other materials provided
     16  1.1  itojun  *      with the distribution.
     17  1.1  itojun  *
     18  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  1.1  itojun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  1.1  itojun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  1.1  itojun  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     22  1.1  itojun  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  1.1  itojun  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  1.1  itojun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     26  1.1  itojun  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     28  1.1  itojun  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  itojun  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  itojun  *
     31  1.1  itojun  */
     32  1.1  itojun 
     33  1.1  itojun #include <sys/types.h>
     34  1.1  itojun #include <sys/ioctl.h>
     35  1.1  itojun #include <sys/socket.h>
     36  1.1  itojun 
     37  1.1  itojun #include <net/if.h>
     38  1.1  itojun #include <net/pfvar.h>
     39  1.1  itojun #include <arpa/inet.h>
     40  1.1  itojun 
     41  1.1  itojun #include <ctype.h>
     42  1.1  itojun #include <err.h>
     43  1.1  itojun #include <errno.h>
     44  1.1  itojun #include <netdb.h>
     45  1.1  itojun #include <stdarg.h>
     46  1.1  itojun #include <stdio.h>
     47  1.1  itojun #include <stdlib.h>
     48  1.1  itojun #include <string.h>
     49  1.1  itojun #include <time.h>
     50  1.1  itojun 
     51  1.1  itojun #include "pfctl_parser.h"
     52  1.1  itojun #include "pfctl.h"
     53  1.1  itojun 
     54  1.1  itojun extern void	usage(void);
     55  1.1  itojun static int	pfctl_table(int, char *[], char *, const char *, char *,
     56  1.1  itojun 		    const char *, const char *, int);
     57  1.1  itojun static void	print_table(struct pfr_table *, int, int);
     58  1.1  itojun static void	print_tstats(struct pfr_tstats *, int);
     59  1.1  itojun static int	load_addr(struct pfr_buffer *, int, char *[], char *, int);
     60  1.1  itojun static void	print_addrx(struct pfr_addr *, struct pfr_addr *, int);
     61  1.1  itojun static void	print_astats(struct pfr_astats *, int);
     62  1.1  itojun static void	radix_perror(void);
     63  1.1  itojun static void	xprintf(int, const char *, ...);
     64  1.1  itojun static void	print_iface(struct pfi_if *, int);
     65  1.1  itojun static void	oprintf(int, int, const char *, int *, int);
     66  1.1  itojun 
     67  1.1  itojun static const char	*stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = {
     68  1.1  itojun 	{ "In/Block:",	"In/Pass:",	"In/XPass:" },
     69  1.1  itojun 	{ "Out/Block:",	"Out/Pass:",	"Out/XPass:" }
     70  1.1  itojun };
     71  1.1  itojun 
     72  1.1  itojun static const char	*istats_text[2][2][2] = {
     73  1.1  itojun 	{ { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } },
     74  1.1  itojun 	{ { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } }
     75  1.1  itojun };
     76  1.1  itojun 
     77  1.1  itojun #define RVTEST(fct) do {				\
     78  1.1  itojun 		if ((!(opts & PF_OPT_NOACTION) ||	\
     79  1.1  itojun 		    (opts & PF_OPT_DUMMYACTION)) &&	\
     80  1.1  itojun 		    (fct)) {				\
     81  1.1  itojun 			radix_perror();			\
     82  1.1  itojun 			goto _error;			\
     83  1.1  itojun 		}					\
     84  1.1  itojun 	} while (0)
     85  1.1  itojun 
     86  1.1  itojun #define CREATE_TABLE do {						\
     87  1.1  itojun 		table.pfrt_flags |= PFR_TFLAG_PERSIST;			\
     88  1.1  itojun 		RVTEST(pfr_add_tables(&table, 1, &nadd, flags));	\
     89  1.1  itojun 		if (nadd) {						\
     90  1.1  itojun 			warn_namespace_collision(table.pfrt_name);	\
     91  1.1  itojun 			xprintf(opts, "%d table created", nadd);	\
     92  1.1  itojun 			if (opts & PF_OPT_NOACTION)			\
     93  1.1  itojun 				return (0);				\
     94  1.1  itojun 		}							\
     95  1.1  itojun 		table.pfrt_flags &= ~PFR_TFLAG_PERSIST;			\
     96  1.1  itojun 	} while(0)
     97  1.1  itojun 
     98  1.1  itojun int
     99  1.1  itojun pfctl_clear_tables(const char *anchor, const char *ruleset, int opts)
    100  1.1  itojun {
    101  1.1  itojun 	return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, ruleset, opts);
    102  1.1  itojun }
    103  1.1  itojun 
    104  1.1  itojun int
    105  1.1  itojun pfctl_show_tables(const char *anchor, const char *ruleset, int opts)
    106  1.1  itojun {
    107  1.1  itojun 	return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, ruleset, opts);
    108  1.1  itojun }
    109  1.1  itojun 
    110  1.1  itojun int
    111  1.1  itojun pfctl_command_tables(int argc, char *argv[], char *tname,
    112  1.1  itojun     const char *command, char *file, const char *anchor, const char *ruleset,
    113  1.1  itojun     int opts)
    114  1.1  itojun {
    115  1.1  itojun 	if (tname == NULL || command == NULL)
    116  1.1  itojun 		usage();
    117  1.1  itojun 	return pfctl_table(argc, argv, tname, command, file, anchor, ruleset,
    118  1.1  itojun 	    opts);
    119  1.1  itojun }
    120  1.1  itojun 
    121  1.1  itojun int
    122  1.1  itojun pfctl_table(int argc, char *argv[], char *tname, const char *command,
    123  1.1  itojun     char *file, const char *anchor, const char *ruleset, int opts)
    124  1.1  itojun {
    125  1.1  itojun 	struct pfr_table	 table;
    126  1.1  itojun 	struct pfr_buffer	 b, b2;
    127  1.1  itojun 	struct pfr_addr		*a, *a2;
    128  1.1  itojun 	int			 nadd = 0, ndel = 0, nchange = 0, nzero = 0;
    129  1.1  itojun 	int			 rv = 0, flags = 0, nmatch = 0;
    130  1.1  itojun 	void			*p;
    131  1.1  itojun 
    132  1.1  itojun 	if (command == NULL)
    133  1.1  itojun 		usage();
    134  1.1  itojun 	if (opts & PF_OPT_NOACTION)
    135  1.1  itojun 		flags |= PFR_FLAG_DUMMY;
    136  1.1  itojun 
    137  1.1  itojun 	bzero(&b, sizeof(b));
    138  1.1  itojun 	bzero(&b2, sizeof(b2));
    139  1.1  itojun 	bzero(&table, sizeof(table));
    140  1.1  itojun 	if (tname != NULL) {
    141  1.1  itojun 		if (strlen(tname) >= PF_TABLE_NAME_SIZE)
    142  1.1  itojun 			usage();
    143  1.1  itojun 		if (strlcpy(table.pfrt_name, tname,
    144  1.1  itojun 		    sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
    145  1.1  itojun 			errx(1, "pfctl_table: strlcpy");
    146  1.1  itojun 	}
    147  1.1  itojun 	if (strlcpy(table.pfrt_anchor, anchor,
    148  1.1  itojun 	    sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor) ||
    149  1.1  itojun 	    strlcpy(table.pfrt_ruleset, ruleset,
    150  1.1  itojun 	    sizeof(table.pfrt_ruleset)) >= sizeof(table.pfrt_ruleset))
    151  1.1  itojun 		errx(1, "pfctl_table: strlcpy");
    152  1.1  itojun 
    153  1.1  itojun 	if (!strcmp(command, "-F")) {
    154  1.1  itojun 		if (argc || file != NULL)
    155  1.1  itojun 			usage();
    156  1.1  itojun 		RVTEST(pfr_clr_tables(&table, &ndel, flags));
    157  1.1  itojun 		xprintf(opts, "%d tables deleted", ndel);
    158  1.1  itojun 	} else if (!strcmp(command, "-s")) {
    159  1.1  itojun 		b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
    160  1.1  itojun 		    PFRB_TSTATS : PFRB_TABLES;
    161  1.1  itojun 		if (argc || file != NULL)
    162  1.1  itojun 			usage();
    163  1.1  itojun 		for (;;) {
    164  1.1  itojun 			pfr_buf_grow(&b, b.pfrb_size);
    165  1.1  itojun 			b.pfrb_size = b.pfrb_msize;
    166  1.1  itojun 			if (opts & PF_OPT_VERBOSE2)
    167  1.1  itojun 				RVTEST(pfr_get_tstats(&table,
    168  1.1  itojun 				    b.pfrb_caddr, &b.pfrb_size, flags));
    169  1.1  itojun 			else
    170  1.1  itojun 				RVTEST(pfr_get_tables(&table,
    171  1.1  itojun 				    b.pfrb_caddr, &b.pfrb_size, flags));
    172  1.1  itojun 			if (b.pfrb_size <= b.pfrb_msize)
    173  1.1  itojun 				break;
    174  1.1  itojun 		}
    175  1.1  itojun 
    176  1.1  itojun 		if (opts & PF_OPT_SHOWALL && b.pfrb_size > 0)
    177  1.1  itojun 			pfctl_print_title("TABLES:");
    178  1.1  itojun 
    179  1.1  itojun 		PFRB_FOREACH(p, &b)
    180  1.1  itojun 			if (opts & PF_OPT_VERBOSE2)
    181  1.1  itojun 				print_tstats(p, opts & PF_OPT_DEBUG);
    182  1.1  itojun 			else
    183  1.1  itojun 				print_table(p, opts & PF_OPT_VERBOSE,
    184  1.1  itojun 				    opts & PF_OPT_DEBUG);
    185  1.1  itojun 	} else if (!strcmp(command, "kill")) {
    186  1.1  itojun 		if (argc || file != NULL)
    187  1.1  itojun 			usage();
    188  1.1  itojun 		RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
    189  1.1  itojun 		xprintf(opts, "%d table deleted", ndel);
    190  1.1  itojun 	} else if (!strcmp(command, "flush")) {
    191  1.1  itojun 		if (argc || file != NULL)
    192  1.1  itojun 			usage();
    193  1.1  itojun 		RVTEST(pfr_clr_addrs(&table, &ndel, flags));
    194  1.1  itojun 		xprintf(opts, "%d addresses deleted", ndel);
    195  1.1  itojun 	} else if (!strcmp(command, "add")) {
    196  1.1  itojun 		b.pfrb_type = PFRB_ADDRS;
    197  1.1  itojun 		if (load_addr(&b, argc, argv, file, 0))
    198  1.1  itojun 			goto _error;
    199  1.1  itojun 		CREATE_TABLE;
    200  1.1  itojun 		if (opts & PF_OPT_VERBOSE)
    201  1.1  itojun 			flags |= PFR_FLAG_FEEDBACK;
    202  1.1  itojun 		RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
    203  1.1  itojun 		    &nadd, flags));
    204  1.1  itojun 		xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
    205  1.1  itojun 		if (opts & PF_OPT_VERBOSE)
    206  1.1  itojun 			PFRB_FOREACH(a, &b)
    207  1.1  itojun 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
    208  1.1  itojun 					print_addrx(a, NULL,
    209  1.1  itojun 					    opts & PF_OPT_USEDNS);
    210  1.1  itojun 	} else if (!strcmp(command, "delete")) {
    211  1.1  itojun 		b.pfrb_type = PFRB_ADDRS;
    212  1.1  itojun 		if (load_addr(&b, argc, argv, file, 0))
    213  1.1  itojun 			goto _error;
    214  1.1  itojun 		if (opts & PF_OPT_VERBOSE)
    215  1.1  itojun 			flags |= PFR_FLAG_FEEDBACK;
    216  1.1  itojun 		RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
    217  1.1  itojun 		    &ndel, flags));
    218  1.1  itojun 		xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
    219  1.1  itojun 		if (opts & PF_OPT_VERBOSE)
    220  1.1  itojun 			PFRB_FOREACH(a, &b)
    221  1.1  itojun 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
    222  1.1  itojun 					print_addrx(a, NULL,
    223  1.1  itojun 					    opts & PF_OPT_USEDNS);
    224  1.1  itojun 	} else if (!strcmp(command, "replace")) {
    225  1.1  itojun 		b.pfrb_type = PFRB_ADDRS;
    226  1.1  itojun 		if (load_addr(&b, argc, argv, file, 0))
    227  1.1  itojun 			goto _error;
    228  1.1  itojun 		CREATE_TABLE;
    229  1.1  itojun 		if (opts & PF_OPT_VERBOSE)
    230  1.1  itojun 			flags |= PFR_FLAG_FEEDBACK;
    231  1.1  itojun 		for (;;) {
    232  1.1  itojun 			int sz2 = b.pfrb_msize;
    233  1.1  itojun 
    234  1.1  itojun 			RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
    235  1.1  itojun 			    &sz2, &nadd, &ndel, &nchange, flags));
    236  1.1  itojun 			if (sz2 <= b.pfrb_msize) {
    237  1.1  itojun 				b.pfrb_size = sz2;
    238  1.1  itojun 				break;
    239  1.1  itojun 			} else
    240  1.1  itojun 				pfr_buf_grow(&b, sz2);
    241  1.1  itojun 		}
    242  1.1  itojun 		if (nadd)
    243  1.1  itojun 			xprintf(opts, "%d addresses added", nadd);
    244  1.1  itojun 		if (ndel)
    245  1.1  itojun 			xprintf(opts, "%d addresses deleted", ndel);
    246  1.1  itojun 		if (nchange)
    247  1.1  itojun 			xprintf(opts, "%d addresses changed", nchange);
    248  1.1  itojun 		if (!nadd && !ndel && !nchange)
    249  1.1  itojun 			xprintf(opts, "no changes");
    250  1.1  itojun 		if (opts & PF_OPT_VERBOSE)
    251  1.1  itojun 			PFRB_FOREACH(a, &b)
    252  1.1  itojun 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
    253  1.1  itojun 					print_addrx(a, NULL,
    254  1.1  itojun 					    opts & PF_OPT_USEDNS);
    255  1.1  itojun 	} else if (!strcmp(command, "show")) {
    256  1.1  itojun 		b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
    257  1.1  itojun 			PFRB_ASTATS : PFRB_ADDRS;
    258  1.1  itojun 		if (argc || file != NULL)
    259  1.1  itojun 			usage();
    260  1.1  itojun 		for (;;) {
    261  1.1  itojun 			pfr_buf_grow(&b, b.pfrb_size);
    262  1.1  itojun 			b.pfrb_size = b.pfrb_msize;
    263  1.1  itojun 			if (opts & PF_OPT_VERBOSE)
    264  1.1  itojun 				RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
    265  1.1  itojun 				    &b.pfrb_size, flags));
    266  1.1  itojun 			else
    267  1.1  itojun 				RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
    268  1.1  itojun 				    &b.pfrb_size, flags));
    269  1.1  itojun 			if (b.pfrb_size <= b.pfrb_msize)
    270  1.1  itojun 				break;
    271  1.1  itojun 		}
    272  1.1  itojun 		PFRB_FOREACH(p, &b)
    273  1.1  itojun 			if (opts & PF_OPT_VERBOSE)
    274  1.1  itojun 				print_astats(p, opts & PF_OPT_USEDNS);
    275  1.1  itojun 			else
    276  1.1  itojun 				print_addrx(p, NULL, opts & PF_OPT_USEDNS);
    277  1.1  itojun 	} else if (!strcmp(command, "test")) {
    278  1.1  itojun 		b.pfrb_type = PFRB_ADDRS;
    279  1.1  itojun 		b2.pfrb_type = PFRB_ADDRS;
    280  1.1  itojun 
    281  1.1  itojun 		if (load_addr(&b, argc, argv, file, 1))
    282  1.1  itojun 			goto _error;
    283  1.1  itojun 		if (opts & PF_OPT_VERBOSE2) {
    284  1.1  itojun 			flags |= PFR_FLAG_REPLACE;
    285  1.1  itojun 			PFRB_FOREACH(a, &b)
    286  1.1  itojun 				if (pfr_buf_add(&b2, a))
    287  1.1  itojun 					err(1, "duplicate buffer");
    288  1.1  itojun 		}
    289  1.1  itojun 		RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
    290  1.1  itojun 		    &nmatch, flags));
    291  1.1  itojun 		xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
    292  1.1  itojun 		if (opts & PF_OPT_VERBOSE && !(opts & PF_OPT_VERBOSE2))
    293  1.1  itojun 			PFRB_FOREACH(a, &b)
    294  1.1  itojun 				if (a->pfra_fback == PFR_FB_MATCH)
    295  1.1  itojun 					print_addrx(a, NULL,
    296  1.1  itojun 					    opts & PF_OPT_USEDNS);
    297  1.1  itojun 		if (opts & PF_OPT_VERBOSE2) {
    298  1.1  itojun 			a2 = NULL;
    299  1.1  itojun 			PFRB_FOREACH(a, &b) {
    300  1.1  itojun 				a2 = pfr_buf_next(&b2, a2);
    301  1.1  itojun 				print_addrx(a2, a, opts & PF_OPT_USEDNS);
    302  1.1  itojun 			}
    303  1.1  itojun 		}
    304  1.1  itojun 		if (nmatch < b.pfrb_size)
    305  1.1  itojun 			rv = 2;
    306  1.1  itojun 	} else if (!strcmp(command, "zero")) {
    307  1.1  itojun 		if (argc || file != NULL)
    308  1.1  itojun 			usage();
    309  1.1  itojun 		flags |= PFR_FLAG_ADDRSTOO;
    310  1.1  itojun 		RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
    311  1.1  itojun 		xprintf(opts, "%d table/stats cleared", nzero);
    312  1.1  itojun 	} else
    313  1.1  itojun 		warnx("pfctl_table: unknown command '%s'", command);
    314  1.1  itojun 	goto _cleanup;
    315  1.1  itojun 
    316  1.1  itojun _error:
    317  1.1  itojun 	rv = -1;
    318  1.1  itojun _cleanup:
    319  1.1  itojun 	pfr_buf_clear(&b);
    320  1.1  itojun 	pfr_buf_clear(&b2);
    321  1.1  itojun 	return (rv);
    322  1.1  itojun }
    323  1.1  itojun 
    324  1.1  itojun void
    325  1.1  itojun print_table(struct pfr_table *ta, int verbose, int debug)
    326  1.1  itojun {
    327  1.1  itojun 	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
    328  1.1  itojun 		return;
    329  1.1  itojun 	if (verbose) {
    330  1.1  itojun 		printf("%c%c%c%c%c%c\t%s",
    331  1.1  itojun 		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
    332  1.1  itojun 		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
    333  1.1  itojun 		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
    334  1.1  itojun 		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
    335  1.1  itojun 		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
    336  1.1  itojun 		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
    337  1.1  itojun 		    ta->pfrt_name);
    338  1.1  itojun 		if (ta->pfrt_anchor[0])
    339  1.1  itojun 			printf("\t%s", ta->pfrt_anchor);
    340  1.1  itojun 		if (ta->pfrt_ruleset[0])
    341  1.1  itojun 			printf(":%s", ta->pfrt_ruleset);
    342  1.1  itojun 		puts("");
    343  1.1  itojun 	} else
    344  1.1  itojun 		puts(ta->pfrt_name);
    345  1.1  itojun }
    346  1.1  itojun 
    347  1.1  itojun void
    348  1.1  itojun print_tstats(struct pfr_tstats *ts, int debug)
    349  1.1  itojun {
    350  1.1  itojun 	time_t	time = ts->pfrts_tzero;
    351  1.1  itojun 	int	dir, op;
    352  1.1  itojun 
    353  1.1  itojun 	if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
    354  1.1  itojun 		return;
    355  1.1  itojun 	print_table(&ts->pfrts_t, 1, debug);
    356  1.1  itojun 	printf("\tAddresses:   %d\n", ts->pfrts_cnt);
    357  1.1  itojun 	printf("\tCleared:     %s", ctime(&time));
    358  1.1  itojun 	printf("\tReferences:  [ Anchors: %-18d Rules: %-18d ]\n",
    359  1.1  itojun 	    ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
    360  1.1  itojun 	    ts->pfrts_refcnt[PFR_REFCNT_RULE]);
    361  1.1  itojun 	printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
    362  1.1  itojun 	    (unsigned long long)ts->pfrts_nomatch,
    363  1.1  itojun 	    (unsigned long long)ts->pfrts_match);
    364  1.1  itojun 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
    365  1.1  itojun 		for (op = 0; op < PFR_OP_TABLE_MAX; op++)
    366  1.1  itojun 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
    367  1.1  itojun 			    stats_text[dir][op],
    368  1.1  itojun 			    (unsigned long long)ts->pfrts_packets[dir][op],
    369  1.1  itojun 			    (unsigned long long)ts->pfrts_bytes[dir][op]);
    370  1.1  itojun }
    371  1.1  itojun 
    372  1.1  itojun int
    373  1.1  itojun load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
    374  1.1  itojun     int nonetwork)
    375  1.1  itojun {
    376  1.1  itojun 	while (argc--)
    377  1.1  itojun 		if (append_addr(b, *argv++, nonetwork)) {
    378  1.1  itojun 			if (errno)
    379  1.1  itojun 				warn("cannot decode %s", argv[-1]);
    380  1.1  itojun 			return (-1);
    381  1.1  itojun 		}
    382  1.1  itojun 	if (pfr_buf_load(b, file, nonetwork, append_addr)) {
    383  1.1  itojun 		warn("cannot load %s", file);
    384  1.1  itojun 		return (-1);
    385  1.1  itojun 	}
    386  1.1  itojun 	return (0);
    387  1.1  itojun }
    388  1.1  itojun 
    389  1.1  itojun void
    390  1.1  itojun print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
    391  1.1  itojun {
    392  1.1  itojun 	char		ch, buf[256] = "{error}";
    393  1.1  itojun 	char		fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y' };
    394  1.1  itojun 	unsigned int	fback, hostnet;
    395  1.1  itojun 
    396  1.1  itojun 	fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
    397  1.1  itojun 	ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
    398  1.1  itojun 	hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
    399  1.1  itojun 	inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
    400  1.1  itojun 	printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
    401  1.1  itojun 	if (ad->pfra_net < hostnet)
    402  1.1  itojun 		printf("/%d", ad->pfra_net);
    403  1.1  itojun 	if (rad != NULL && fback != PFR_FB_NONE) {
    404  1.1  itojun 		if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
    405  1.1  itojun 			errx(1, "print_addrx: strlcpy");
    406  1.1  itojun 		inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
    407  1.1  itojun 		printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
    408  1.1  itojun 		if (rad->pfra_net < hostnet)
    409  1.1  itojun 			printf("/%d", rad->pfra_net);
    410  1.1  itojun 	}
    411  1.1  itojun 	if (rad != NULL && fback == PFR_FB_NONE)
    412  1.1  itojun 		printf("\t nomatch");
    413  1.1  itojun 	if (dns && ad->pfra_net == hostnet) {
    414  1.1  itojun 		char host[NI_MAXHOST];
    415  1.1  itojun 		union sockaddr_union sa;
    416  1.1  itojun 
    417  1.1  itojun 		strlcpy(host, "?", sizeof(host));
    418  1.1  itojun 		bzero(&sa, sizeof(sa));
    419  1.1  itojun 		sa.sa.sa_family = ad->pfra_af;
    420  1.1  itojun 		if (sa.sa.sa_family == AF_INET) {
    421  1.1  itojun 			sa.sa.sa_len = sizeof(sa.sin);
    422  1.1  itojun 			sa.sin.sin_addr = ad->pfra_ip4addr;
    423  1.1  itojun 		} else {
    424  1.1  itojun 			sa.sa.sa_len = sizeof(sa.sin6);
    425  1.1  itojun 			sa.sin6.sin6_addr = ad->pfra_ip6addr;
    426  1.1  itojun 		}
    427  1.1  itojun 		if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host),
    428  1.1  itojun 		    NULL, 0, NI_NAMEREQD) == 0)
    429  1.1  itojun 			printf("\t(%s)", host);
    430  1.1  itojun 	}
    431  1.1  itojun 	printf("\n");
    432  1.1  itojun }
    433  1.1  itojun 
    434  1.1  itojun void
    435  1.1  itojun print_astats(struct pfr_astats *as, int dns)
    436  1.1  itojun {
    437  1.1  itojun 	time_t	time = as->pfras_tzero;
    438  1.1  itojun 	int	dir, op;
    439  1.1  itojun 
    440  1.1  itojun 	print_addrx(&as->pfras_a, NULL, dns);
    441  1.1  itojun 	printf("\tCleared:     %s", ctime(&time));
    442  1.1  itojun 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
    443  1.1  itojun 		for (op = 0; op < PFR_OP_ADDR_MAX; op++)
    444  1.1  itojun 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
    445  1.1  itojun 			    stats_text[dir][op],
    446  1.1  itojun 			    (unsigned long long)as->pfras_packets[dir][op],
    447  1.1  itojun 			    (unsigned long long)as->pfras_bytes[dir][op]);
    448  1.1  itojun }
    449  1.1  itojun 
    450  1.1  itojun void
    451  1.1  itojun radix_perror(void)
    452  1.1  itojun {
    453  1.1  itojun 	extern char *__progname;
    454  1.1  itojun 	fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
    455  1.1  itojun }
    456  1.1  itojun 
    457  1.1  itojun int
    458  1.1  itojun pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
    459  1.1  itojun     const char *ruleset, struct pfr_buffer *ab, u_int32_t ticket)
    460  1.1  itojun {
    461  1.1  itojun 	struct pfr_table tbl;
    462  1.1  itojun 
    463  1.1  itojun 	bzero(&tbl, sizeof(tbl));
    464  1.1  itojun 	if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
    465  1.1  itojun 	    sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
    466  1.1  itojun 	    sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor) ||
    467  1.1  itojun 	    strlcpy(tbl.pfrt_ruleset, ruleset, sizeof(tbl.pfrt_ruleset)) >=
    468  1.1  itojun 	    sizeof(tbl.pfrt_ruleset))
    469  1.1  itojun 		errx(1, "pfctl_define_table: strlcpy");
    470  1.1  itojun 	tbl.pfrt_flags = flags;
    471  1.1  itojun 
    472  1.1  itojun 	return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
    473  1.1  itojun 	    NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
    474  1.1  itojun }
    475  1.1  itojun 
    476  1.1  itojun void
    477  1.1  itojun warn_namespace_collision(const char *filter)
    478  1.1  itojun {
    479  1.1  itojun 	struct pfr_buffer b;
    480  1.1  itojun 	struct pfr_table *t;
    481  1.1  itojun 	const char *name = NULL, *lastcoll;
    482  1.1  itojun 	int coll = 0;
    483  1.1  itojun 
    484  1.1  itojun 	bzero(&b, sizeof(b));
    485  1.1  itojun 	b.pfrb_type = PFRB_TABLES;
    486  1.1  itojun 	for (;;) {
    487  1.1  itojun 		pfr_buf_grow(&b, b.pfrb_size);
    488  1.1  itojun 		b.pfrb_size = b.pfrb_msize;
    489  1.1  itojun 		if (pfr_get_tables(NULL, b.pfrb_caddr,
    490  1.1  itojun 		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
    491  1.1  itojun 			err(1, "pfr_get_tables");
    492  1.1  itojun 		if (b.pfrb_size <= b.pfrb_msize)
    493  1.1  itojun 			break;
    494  1.1  itojun 	}
    495  1.1  itojun 	PFRB_FOREACH(t, &b) {
    496  1.1  itojun 		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
    497  1.1  itojun 			continue;
    498  1.1  itojun 		if (filter != NULL && strcmp(filter, t->pfrt_name))
    499  1.1  itojun 			continue;
    500  1.1  itojun 		if (!t->pfrt_anchor[0])
    501  1.1  itojun 			name = t->pfrt_name;
    502  1.1  itojun 		else if (name != NULL && !strcmp(name, t->pfrt_name)) {
    503  1.1  itojun 			coll++;
    504  1.1  itojun 			lastcoll = name;
    505  1.1  itojun 			name = NULL;
    506  1.1  itojun 		}
    507  1.1  itojun 	}
    508  1.1  itojun 	if (coll == 1)
    509  1.1  itojun 		warnx("warning: namespace collision with <%s> global table.",
    510  1.1  itojun 		    lastcoll);
    511  1.1  itojun 	else if (coll > 1)
    512  1.1  itojun 		warnx("warning: namespace collisions with %d global tables.",
    513  1.1  itojun 		    coll);
    514  1.1  itojun 	pfr_buf_clear(&b);
    515  1.1  itojun }
    516  1.1  itojun 
    517  1.1  itojun void
    518  1.1  itojun xprintf(int opts, const char *fmt, ...)
    519  1.1  itojun {
    520  1.1  itojun 	va_list args;
    521  1.1  itojun 
    522  1.1  itojun 	if (opts & PF_OPT_QUIET)
    523  1.1  itojun 		return;
    524  1.1  itojun 
    525  1.1  itojun 	va_start(args, fmt);
    526  1.1  itojun 	vfprintf(stderr, fmt, args);
    527  1.1  itojun 	va_end(args);
    528  1.1  itojun 
    529  1.1  itojun 	if (opts & PF_OPT_DUMMYACTION)
    530  1.1  itojun 		fprintf(stderr, " (dummy).\n");
    531  1.1  itojun 	else if (opts & PF_OPT_NOACTION)
    532  1.1  itojun 		fprintf(stderr, " (syntax only).\n");
    533  1.1  itojun 	else
    534  1.1  itojun 		fprintf(stderr, ".\n");
    535  1.1  itojun }
    536  1.1  itojun 
    537  1.1  itojun 
    538  1.1  itojun /* interface stuff */
    539  1.1  itojun 
    540  1.1  itojun int
    541  1.1  itojun pfctl_show_ifaces(const char *filter, int opts)
    542  1.1  itojun {
    543  1.1  itojun 	struct pfr_buffer	 b;
    544  1.1  itojun 	struct pfi_if		*p;
    545  1.1  itojun 	int			 i = 0, f = PFI_FLAG_GROUP|PFI_FLAG_INSTANCE;
    546  1.1  itojun 
    547  1.1  itojun 	if (filter != NULL && *filter && !isdigit(filter[strlen(filter)-1]))
    548  1.1  itojun 		f &= ~PFI_FLAG_INSTANCE;
    549  1.1  itojun 	bzero(&b, sizeof(b));
    550  1.1  itojun 	b.pfrb_type = PFRB_IFACES;
    551  1.1  itojun 	for (;;) {
    552  1.1  itojun 		pfr_buf_grow(&b, b.pfrb_size);
    553  1.1  itojun 		b.pfrb_size = b.pfrb_msize;
    554  1.1  itojun 		if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size, f)) {
    555  1.1  itojun 			radix_perror();
    556  1.1  itojun 			return (1);
    557  1.1  itojun 		}
    558  1.1  itojun 		if (b.pfrb_size <= b.pfrb_msize)
    559  1.1  itojun 			break;
    560  1.1  itojun 		i++;
    561  1.1  itojun 	}
    562  1.1  itojun 	if (opts & PF_OPT_SHOWALL)
    563  1.1  itojun 		pfctl_print_title("INTERFACES:");
    564  1.1  itojun 	PFRB_FOREACH(p, &b)
    565  1.1  itojun 		print_iface(p, opts);
    566  1.1  itojun 	return (0);
    567  1.1  itojun }
    568  1.1  itojun 
    569  1.1  itojun void
    570  1.1  itojun print_iface(struct pfi_if *p, int opts)
    571  1.1  itojun {
    572  1.1  itojun 	time_t	tzero = p->pfif_tzero;
    573  1.1  itojun 	int	flags = (opts & PF_OPT_VERBOSE) ? p->pfif_flags : 0;
    574  1.1  itojun 	int	first = 1;
    575  1.1  itojun 	int	i, af, dir, act;
    576  1.1  itojun 
    577  1.1  itojun 	printf("%s", p->pfif_name);
    578  1.1  itojun 	oprintf(flags, PFI_IFLAG_INSTANCE, "instance", &first, 0);
    579  1.1  itojun 	oprintf(flags, PFI_IFLAG_GROUP, "group", &first, 0);
    580  1.1  itojun 	oprintf(flags, PFI_IFLAG_CLONABLE, "clonable", &first, 0);
    581  1.1  itojun 	oprintf(flags, PFI_IFLAG_DYNAMIC, "dynamic", &first, 0);
    582  1.1  itojun 	oprintf(flags, PFI_IFLAG_ATTACHED, "attached", &first, 1);
    583  1.1  itojun 	printf("\n");
    584  1.1  itojun 
    585  1.1  itojun 	if (!(opts & PF_OPT_VERBOSE2))
    586  1.1  itojun 		return;
    587  1.1  itojun 	printf("\tCleared:     %s", ctime(&tzero));
    588  1.1  itojun 	printf("\tReferences:  [ States:  %-18d Rules: %-18d ]\n",
    589  1.1  itojun 	    p->pfif_states, p->pfif_rules);
    590  1.1  itojun 	for (i = 0; i < 8; i++) {
    591  1.1  itojun 		af = (i>>2) & 1;
    592  1.1  itojun 		dir = (i>>1) &1;
    593  1.1  itojun 		act = i & 1;
    594  1.1  itojun 		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
    595  1.1  itojun 		    istats_text[af][dir][act],
    596  1.1  itojun 		    (unsigned long long)p->pfif_packets[af][dir][act],
    597  1.1  itojun 		    (unsigned long long)p->pfif_bytes[af][dir][act]);
    598  1.1  itojun 	}
    599  1.1  itojun }
    600  1.1  itojun 
    601  1.1  itojun void
    602  1.1  itojun oprintf(int flags, int flag, const char *s, int *first, int last)
    603  1.1  itojun {
    604  1.1  itojun 	if (flags & flag) {
    605  1.1  itojun 		printf(*first ? "\t(%s" : ", %s", s);
    606  1.1  itojun 		*first = 0;
    607  1.1  itojun 	}
    608  1.1  itojun 	if (last && !*first)
    609  1.1  itojun 		printf(")");
    610  1.1  itojun }
    611  1.1  itojun 
    612