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