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