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