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