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