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