1 /*	$OpenBSD: pfctl_table.c,v 1.59 2004/03/15 15:25:44 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 *, 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 		RVTEST(pfr_add_tables(&table, 1, &nadd, flags));	\
89 		if (nadd) {						\
90 			warn_namespace_collision(table.pfrt_name);	\
91 			xprintf(opts, "%d table created", nadd);	\
92 			if (opts & PF_OPT_NOACTION)			\
93 				return (0);				\
94 		}							\
95 		table.pfrt_flags &= ~PFR_TFLAG_PERSIST;			\
96 	} while(0)
97 
98 int
pfctl_clear_tables(const char * anchor,const char * ruleset,int opts)99 pfctl_clear_tables(const char *anchor, const char *ruleset, int opts)
100 {
101 	return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, ruleset, opts);
102 }
103 
104 int
pfctl_show_tables(const char * anchor,const char * ruleset,int opts)105 pfctl_show_tables(const char *anchor, const char *ruleset, int opts)
106 {
107 	return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, ruleset, opts);
108 }
109 
110 int
pfctl_command_tables(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,const char * ruleset,int opts)111 pfctl_command_tables(int argc, char *argv[], char *tname,
112     const char *command, char *file, const char *anchor, const char *ruleset,
113     int opts)
114 {
115 	if (tname == NULL || command == NULL)
116 		usage();
117 	return pfctl_table(argc, argv, tname, command, file, anchor, ruleset,
118 	    opts);
119 }
120 
121 int
pfctl_table(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,const char * ruleset,int opts)122 pfctl_table(int argc, char *argv[], char *tname, const char *command,
123     char *file, const char *anchor, const char *ruleset, int opts)
124 {
125 	struct pfr_table	 table;
126 	struct pfr_buffer	 b, b2;
127 	struct pfr_addr		*a, *a2;
128 	int			 nadd = 0, ndel = 0, nchange = 0, nzero = 0;
129 	int			 rv = 0, flags = 0, nmatch = 0;
130 	void			*p;
131 
132 	if (command == NULL)
133 		usage();
134 	if (opts & PF_OPT_NOACTION)
135 		flags |= PFR_FLAG_DUMMY;
136 
137 	bzero(&b, sizeof(b));
138 	bzero(&b2, sizeof(b2));
139 	bzero(&table, sizeof(table));
140 	if (tname != NULL) {
141 		if (strlen(tname) >= PF_TABLE_NAME_SIZE)
142 			usage();
143 		if (strlcpy(table.pfrt_name, tname,
144 		    sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
145 			errx(1, "pfctl_table: strlcpy");
146 	}
147 	if (strlcpy(table.pfrt_anchor, anchor,
148 	    sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor) ||
149 	    strlcpy(table.pfrt_ruleset, ruleset,
150 	    sizeof(table.pfrt_ruleset)) >= sizeof(table.pfrt_ruleset))
151 		errx(1, "pfctl_table: strlcpy");
152 
153 	if (!strcmp(command, "-F")) {
154 		if (argc || file != NULL)
155 			usage();
156 		RVTEST(pfr_clr_tables(&table, &ndel, flags));
157 		xprintf(opts, "%d tables deleted", ndel);
158 	} else if (!strcmp(command, "-s")) {
159 		b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
160 		    PFRB_TSTATS : PFRB_TABLES;
161 		if (argc || file != NULL)
162 			usage();
163 		for (;;) {
164 			pfr_buf_grow(&b, b.pfrb_size);
165 			b.pfrb_size = b.pfrb_msize;
166 			if (opts & PF_OPT_VERBOSE2)
167 				RVTEST(pfr_get_tstats(&table,
168 				    b.pfrb_caddr, &b.pfrb_size, flags));
169 			else
170 				RVTEST(pfr_get_tables(&table,
171 				    b.pfrb_caddr, &b.pfrb_size, flags));
172 			if (b.pfrb_size <= b.pfrb_msize)
173 				break;
174 		}
175 
176 		if (opts & PF_OPT_SHOWALL && b.pfrb_size > 0)
177 			pfctl_print_title("TABLES:");
178 
179 		PFRB_FOREACH(p, &b)
180 			if (opts & PF_OPT_VERBOSE2)
181 				print_tstats(p, opts & PF_OPT_DEBUG);
182 			else
183 				print_table(p, opts & PF_OPT_VERBOSE,
184 				    opts & PF_OPT_DEBUG);
185 	} else if (!strcmp(command, "kill")) {
186 		if (argc || file != NULL)
187 			usage();
188 		RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
189 		xprintf(opts, "%d table deleted", ndel);
190 	} else if (!strcmp(command, "flush")) {
191 		if (argc || file != NULL)
192 			usage();
193 		RVTEST(pfr_clr_addrs(&table, &ndel, flags));
194 		xprintf(opts, "%d addresses deleted", ndel);
195 	} else if (!strcmp(command, "add")) {
196 		b.pfrb_type = PFRB_ADDRS;
197 		if (load_addr(&b, argc, argv, file, 0))
198 			goto _error;
199 		CREATE_TABLE;
200 		if (opts & PF_OPT_VERBOSE)
201 			flags |= PFR_FLAG_FEEDBACK;
202 		RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
203 		    &nadd, flags));
204 		xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
205 		if (opts & PF_OPT_VERBOSE)
206 			PFRB_FOREACH(a, &b)
207 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
208 					print_addrx(a, NULL,
209 					    opts & PF_OPT_USEDNS);
210 	} else if (!strcmp(command, "delete")) {
211 		b.pfrb_type = PFRB_ADDRS;
212 		if (load_addr(&b, argc, argv, file, 0))
213 			goto _error;
214 		if (opts & PF_OPT_VERBOSE)
215 			flags |= PFR_FLAG_FEEDBACK;
216 		RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
217 		    &ndel, flags));
218 		xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
219 		if (opts & PF_OPT_VERBOSE)
220 			PFRB_FOREACH(a, &b)
221 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
222 					print_addrx(a, NULL,
223 					    opts & PF_OPT_USEDNS);
224 	} else if (!strcmp(command, "replace")) {
225 		b.pfrb_type = PFRB_ADDRS;
226 		if (load_addr(&b, argc, argv, file, 0))
227 			goto _error;
228 		CREATE_TABLE;
229 		if (opts & PF_OPT_VERBOSE)
230 			flags |= PFR_FLAG_FEEDBACK;
231 		for (;;) {
232 			int sz2 = b.pfrb_msize;
233 
234 			RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
235 			    &sz2, &nadd, &ndel, &nchange, flags));
236 			if (sz2 <= b.pfrb_msize) {
237 				b.pfrb_size = sz2;
238 				break;
239 			} else
240 				pfr_buf_grow(&b, sz2);
241 		}
242 		if (nadd)
243 			xprintf(opts, "%d addresses added", nadd);
244 		if (ndel)
245 			xprintf(opts, "%d addresses deleted", ndel);
246 		if (nchange)
247 			xprintf(opts, "%d addresses changed", nchange);
248 		if (!nadd && !ndel && !nchange)
249 			xprintf(opts, "no changes");
250 		if (opts & PF_OPT_VERBOSE)
251 			PFRB_FOREACH(a, &b)
252 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
253 					print_addrx(a, NULL,
254 					    opts & PF_OPT_USEDNS);
255 	} else if (!strcmp(command, "show")) {
256 		b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
257 			PFRB_ASTATS : PFRB_ADDRS;
258 		if (argc || file != NULL)
259 			usage();
260 		for (;;) {
261 			pfr_buf_grow(&b, b.pfrb_size);
262 			b.pfrb_size = b.pfrb_msize;
263 			if (opts & PF_OPT_VERBOSE)
264 				RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
265 				    &b.pfrb_size, flags));
266 			else
267 				RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
268 				    &b.pfrb_size, flags));
269 			if (b.pfrb_size <= b.pfrb_msize)
270 				break;
271 		}
272 		PFRB_FOREACH(p, &b)
273 			if (opts & PF_OPT_VERBOSE)
274 				print_astats(p, opts & PF_OPT_USEDNS);
275 			else
276 				print_addrx(p, NULL, opts & PF_OPT_USEDNS);
277 	} else if (!strcmp(command, "test")) {
278 		b.pfrb_type = PFRB_ADDRS;
279 		b2.pfrb_type = PFRB_ADDRS;
280 
281 		if (load_addr(&b, argc, argv, file, 1))
282 			goto _error;
283 		if (opts & PF_OPT_VERBOSE2) {
284 			flags |= PFR_FLAG_REPLACE;
285 			PFRB_FOREACH(a, &b)
286 				if (pfr_buf_add(&b2, a))
287 					err(1, "duplicate buffer");
288 		}
289 		RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
290 		    &nmatch, flags));
291 		xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
292 		if (opts & PF_OPT_VERBOSE && !(opts & PF_OPT_VERBOSE2))
293 			PFRB_FOREACH(a, &b)
294 				if (a->pfra_fback == PFR_FB_MATCH)
295 					print_addrx(a, NULL,
296 					    opts & PF_OPT_USEDNS);
297 		if (opts & PF_OPT_VERBOSE2) {
298 			a2 = NULL;
299 			PFRB_FOREACH(a, &b) {
300 				a2 = pfr_buf_next(&b2, a2);
301 				print_addrx(a2, a, opts & PF_OPT_USEDNS);
302 			}
303 		}
304 		if (nmatch < b.pfrb_size)
305 			rv = 2;
306 	} else if (!strcmp(command, "zero")) {
307 		if (argc || file != NULL)
308 			usage();
309 		flags |= PFR_FLAG_ADDRSTOO;
310 		RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
311 		xprintf(opts, "%d table/stats cleared", nzero);
312 	} else
313 		warnx("pfctl_table: unknown command '%s'", command);
314 	goto _cleanup;
315 
316 _error:
317 	rv = -1;
318 _cleanup:
319 	pfr_buf_clear(&b);
320 	pfr_buf_clear(&b2);
321 	return (rv);
322 }
323 
324 void
print_table(struct pfr_table * ta,int verbose,int debug)325 print_table(struct pfr_table *ta, int verbose, int debug)
326 {
327 	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
328 		return;
329 	if (verbose) {
330 		printf("%c%c%c%c%c%c\t%s",
331 		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
332 		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
333 		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
334 		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
335 		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
336 		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
337 		    ta->pfrt_name);
338 		if (ta->pfrt_anchor[0])
339 			printf("\t%s", ta->pfrt_anchor);
340 		if (ta->pfrt_ruleset[0])
341 			printf(":%s", ta->pfrt_ruleset);
342 		puts("");
343 	} else
344 		puts(ta->pfrt_name);
345 }
346 
347 void
print_tstats(struct pfr_tstats * ts,int debug)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
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork)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
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)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
print_astats(struct pfr_astats * as,int dns)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
radix_perror(void)451 radix_perror(void)
452 {
453 	extern char *__progname;
454 	fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
455 }
456 
457 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,const char * ruleset,struct pfr_buffer * ab,u_int32_t ticket)458 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
459     const char *ruleset, 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 	    strlcpy(tbl.pfrt_ruleset, ruleset, sizeof(tbl.pfrt_ruleset)) >=
468 	    sizeof(tbl.pfrt_ruleset))
469 		errx(1, "pfctl_define_table: strlcpy");
470 	tbl.pfrt_flags = flags;
471 
472 	return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
473 	    NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
474 }
475 
476 void
warn_namespace_collision(const char * filter)477 warn_namespace_collision(const char *filter)
478 {
479 	struct pfr_buffer b;
480 	struct pfr_table *t;
481 	const char *name = NULL, *lastcoll;
482 	int coll = 0;
483 
484 	bzero(&b, sizeof(b));
485 	b.pfrb_type = PFRB_TABLES;
486 	for (;;) {
487 		pfr_buf_grow(&b, b.pfrb_size);
488 		b.pfrb_size = b.pfrb_msize;
489 		if (pfr_get_tables(NULL, b.pfrb_caddr,
490 		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
491 			err(1, "pfr_get_tables");
492 		if (b.pfrb_size <= b.pfrb_msize)
493 			break;
494 	}
495 	PFRB_FOREACH(t, &b) {
496 		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
497 			continue;
498 		if (filter != NULL && strcmp(filter, t->pfrt_name))
499 			continue;
500 		if (!t->pfrt_anchor[0])
501 			name = t->pfrt_name;
502 		else if (name != NULL && !strcmp(name, t->pfrt_name)) {
503 			coll++;
504 			lastcoll = name;
505 			name = NULL;
506 		}
507 	}
508 	if (coll == 1)
509 		warnx("warning: namespace collision with <%s> global table.",
510 		    lastcoll);
511 	else if (coll > 1)
512 		warnx("warning: namespace collisions with %d global tables.",
513 		    coll);
514 	pfr_buf_clear(&b);
515 }
516 
517 void
xprintf(int opts,const char * fmt,...)518 xprintf(int opts, const char *fmt, ...)
519 {
520 	va_list args;
521 
522 	if (opts & PF_OPT_QUIET)
523 		return;
524 
525 	va_start(args, fmt);
526 	vfprintf(stderr, fmt, args);
527 	va_end(args);
528 
529 	if (opts & PF_OPT_DUMMYACTION)
530 		fprintf(stderr, " (dummy).\n");
531 	else if (opts & PF_OPT_NOACTION)
532 		fprintf(stderr, " (syntax only).\n");
533 	else
534 		fprintf(stderr, ".\n");
535 }
536 
537 
538 /* interface stuff */
539 
540 int
pfctl_show_ifaces(const char * filter,int opts)541 pfctl_show_ifaces(const char *filter, int opts)
542 {
543 	struct pfr_buffer	 b;
544 	struct pfi_if		*p;
545 	int			 i = 0, f = PFI_FLAG_GROUP|PFI_FLAG_INSTANCE;
546 
547 	if (filter != NULL && *filter && !isdigit(filter[strlen(filter)-1]))
548 		f &= ~PFI_FLAG_INSTANCE;
549 	bzero(&b, sizeof(b));
550 	b.pfrb_type = PFRB_IFACES;
551 	for (;;) {
552 		pfr_buf_grow(&b, b.pfrb_size);
553 		b.pfrb_size = b.pfrb_msize;
554 		if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size, f)) {
555 			radix_perror();
556 			return (1);
557 		}
558 		if (b.pfrb_size <= b.pfrb_msize)
559 			break;
560 		i++;
561 	}
562 	if (opts & PF_OPT_SHOWALL)
563 		pfctl_print_title("INTERFACES:");
564 	PFRB_FOREACH(p, &b)
565 		print_iface(p, opts);
566 	return (0);
567 }
568 
569 void
print_iface(struct pfi_if * p,int opts)570 print_iface(struct pfi_if *p, int opts)
571 {
572 	time_t	tzero = p->pfif_tzero;
573 	int	flags = (opts & PF_OPT_VERBOSE) ? p->pfif_flags : 0;
574 	int	first = 1;
575 	int	i, af, dir, act;
576 
577 	printf("%s", p->pfif_name);
578 	oprintf(flags, PFI_IFLAG_INSTANCE, "instance", &first, 0);
579 	oprintf(flags, PFI_IFLAG_GROUP, "group", &first, 0);
580 	oprintf(flags, PFI_IFLAG_CLONABLE, "clonable", &first, 0);
581 	oprintf(flags, PFI_IFLAG_DYNAMIC, "dynamic", &first, 0);
582 	oprintf(flags, PFI_IFLAG_ATTACHED, "attached", &first, 1);
583 	printf("\n");
584 
585 	if (!(opts & PF_OPT_VERBOSE2))
586 		return;
587 	printf("\tCleared:     %s", ctime(&tzero));
588 	printf("\tReferences:  [ States:  %-18d Rules: %-18d ]\n",
589 	    p->pfif_states, p->pfif_rules);
590 	for (i = 0; i < 8; i++) {
591 		af = (i>>2) & 1;
592 		dir = (i>>1) &1;
593 		act = i & 1;
594 		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
595 		    istats_text[af][dir][act],
596 		    (unsigned long long)p->pfif_packets[af][dir][act],
597 		    (unsigned long long)p->pfif_bytes[af][dir][act]);
598 	}
599 }
600 
601 void
oprintf(int flags,int flag,const char * s,int * first,int last)602 oprintf(int flags, int flag, const char *s, int *first, int last)
603 {
604 	if (flags & flag) {
605 		printf(*first ? "\t(%s" : ", %s", s);
606 		*first = 0;
607 	}
608 	if (last && !*first)
609 		printf(")");
610 }
611 
612