xref: /openbsd/sbin/pfctl/pfctl_table.c (revision 898866c2)
1 /*	$OpenBSD: pfctl_table.c,v 1.81 2019/04/18 22:29:41 kn 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 <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <net/if.h>
40 #include <net/pfvar.h>
41 
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <netdb.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <time.h>
51 #include <limits.h>
52 
53 #include "pfctl_parser.h"
54 #include "pfctl.h"
55 
56 extern void	usage(void);
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, 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/Match:",	"In/Pass:",	"In/XPass:" },
68 	{ "Out/Block:",	"Out/Match:",	"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 		warn_duplicate_tables(table.pfrt_name,			\
87 		    table.pfrt_anchor);					\
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 			xprintf(opts, "%d table created", nadd);	\
98 			if (opts & PF_OPT_NOACTION)			\
99 				return (0);				\
100 		}							\
101 		table.pfrt_flags &= ~PFR_TFLAG_PERSIST;			\
102 	} while(0)
103 
104 void
105 pfctl_clear_tables(const char *anchor, int opts)
106 {
107 	if (pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts) == -1)
108 		exit(1);
109 }
110 
111 void
112 pfctl_show_tables(const char *anchor, int opts)
113 {
114 	if (pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts) == -1)
115 		exit(1);
116 }
117 
118 int
119 pfctl_table(int argc, char *argv[], char *tname, const char *command,
120     char *file, const char *anchor, int opts)
121 {
122 	struct pfr_table	 table;
123 	struct pfr_buffer	 b, b2;
124 	struct pfr_addr		*a, *a2;
125 	int			 nadd = 0, ndel = 0, nchange = 0, nzero = 0;
126 	int			 rv = 0, flags = 0, nmatch = 0;
127 	void			*p;
128 
129 	if (command == NULL)
130 		usage();
131 	if (opts & PF_OPT_NOACTION)
132 		flags |= PFR_FLAG_DUMMY;
133 
134 	bzero(&b, sizeof(b));
135 	bzero(&b2, sizeof(b2));
136 	bzero(&table, sizeof(table));
137 	if (tname != NULL) {
138 		if (strlen(tname) >= PF_TABLE_NAME_SIZE)
139 			usage();
140 		if (strlcpy(table.pfrt_name, tname,
141 		    sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
142 			errx(1, "pfctl_table: strlcpy");
143 	}
144 	if (strlcpy(table.pfrt_anchor, anchor,
145 	    sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
146 		errx(1, "pfctl_table: strlcpy");
147 
148 	if (!strcmp(command, "-F")) {
149 		if (argc || file != NULL)
150 			usage();
151 		RVTEST(pfr_clr_tables(&table, &ndel, flags));
152 		xprintf(opts, "%d tables deleted", ndel);
153 	} else if (!strcmp(command, "-s")) {
154 		b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
155 		    PFRB_TSTATS : PFRB_TABLES;
156 		if (argc || file != NULL)
157 			usage();
158 		for (;;) {
159 			pfr_buf_grow(&b, b.pfrb_size);
160 			b.pfrb_size = b.pfrb_msize;
161 			if (opts & PF_OPT_VERBOSE2)
162 				RVTEST(pfr_get_tstats(&table,
163 				    b.pfrb_caddr, &b.pfrb_size, flags));
164 			else
165 				RVTEST(pfr_get_tables(&table,
166 				    b.pfrb_caddr, &b.pfrb_size, flags));
167 			if (b.pfrb_size <= b.pfrb_msize)
168 				break;
169 		}
170 
171 		if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
172 			pfctl_print_title("TABLES:");
173 
174 		PFRB_FOREACH(p, &b)
175 			if (opts & PF_OPT_VERBOSE2)
176 				print_tstats(p, opts & PF_OPT_DEBUG);
177 			else
178 				print_table(p, opts & PF_OPT_VERBOSE,
179 				    opts & PF_OPT_DEBUG);
180 	} else if (!strcmp(command, "kill")) {
181 		if (argc || file != NULL)
182 			usage();
183 		RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
184 		xprintf(opts, "%d table deleted", ndel);
185 	} else if (!strcmp(command, "flush")) {
186 		if (argc || file != NULL)
187 			usage();
188 		RVTEST(pfr_clr_addrs(&table, &ndel, flags));
189 		xprintf(opts, "%d addresses deleted", ndel);
190 	} else if (!strcmp(command, "add")) {
191 		b.pfrb_type = PFRB_ADDRS;
192 		if (load_addr(&b, argc, argv, file, 0, opts))
193 			goto _error;
194 		CREATE_TABLE;
195 		if (opts & PF_OPT_VERBOSE)
196 			flags |= PFR_FLAG_FEEDBACK;
197 		RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
198 		    &nadd, flags));
199 		xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
200 		if (opts & PF_OPT_VERBOSE)
201 			PFRB_FOREACH(a, &b)
202 				if (opts & PF_OPT_VERBOSE2 ||
203 				    a->pfra_fback != PFR_FB_NONE)
204 					print_addrx(a, NULL,
205 					    opts & PF_OPT_USEDNS);
206 	} else if (!strcmp(command, "delete")) {
207 		b.pfrb_type = PFRB_ADDRS;
208 		if (load_addr(&b, argc, argv, file, 0, opts))
209 			goto _error;
210 		if (opts & PF_OPT_VERBOSE)
211 			flags |= PFR_FLAG_FEEDBACK;
212 		RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
213 		    &ndel, flags));
214 		xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
215 		if (opts & PF_OPT_VERBOSE)
216 			PFRB_FOREACH(a, &b)
217 				if (opts & PF_OPT_VERBOSE2 ||
218 				    a->pfra_fback != PFR_FB_NONE)
219 					print_addrx(a, NULL,
220 					    opts & PF_OPT_USEDNS);
221 	} else if (!strcmp(command, "replace")) {
222 		b.pfrb_type = PFRB_ADDRS;
223 		if (load_addr(&b, argc, argv, file, 0, opts))
224 			goto _error;
225 		CREATE_TABLE;
226 		if (opts & PF_OPT_VERBOSE)
227 			flags |= PFR_FLAG_FEEDBACK;
228 		for (;;) {
229 			int sz2 = b.pfrb_msize;
230 
231 			RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
232 			    &sz2, &nadd, &ndel, &nchange, flags));
233 			if (sz2 <= b.pfrb_msize) {
234 				b.pfrb_size = sz2;
235 				break;
236 			} else
237 				pfr_buf_grow(&b, sz2);
238 		}
239 		if (nadd)
240 			xprintf(opts, "%d addresses added", nadd);
241 		if (ndel)
242 			xprintf(opts, "%d addresses deleted", ndel);
243 		if (nchange)
244 			xprintf(opts, "%d addresses changed", nchange);
245 		if (!nadd && !ndel && !nchange)
246 			xprintf(opts, "no changes");
247 		if (opts & PF_OPT_VERBOSE)
248 			PFRB_FOREACH(a, &b)
249 				if (opts & PF_OPT_VERBOSE2 ||
250 				    a->pfra_fback != PFR_FB_NONE)
251 					print_addrx(a, NULL,
252 					    opts & PF_OPT_USEDNS);
253 	} else if (!strcmp(command, "expire")) {
254 		const char		*errstr;
255 		u_int			 lifetime;
256 
257 		b.pfrb_type = PFRB_ASTATS;
258 		b2.pfrb_type = PFRB_ADDRS;
259 		if (argc != 1 || file != NULL)
260 			usage();
261 		lifetime = strtonum(*argv, 0, UINT_MAX, &errstr);
262 		if (errstr)
263 			errx(1, "expiry time: %s", errstr);
264 		for (;;) {
265 			pfr_buf_grow(&b, b.pfrb_size);
266 			b.pfrb_size = b.pfrb_msize;
267 			RVTEST(pfr_get_astats(&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 			((struct pfr_astats *)p)->pfras_a.pfra_fback = PFR_FB_NONE;
274 			if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero >
275 			     lifetime)
276 				if (pfr_buf_add(&b2,
277 				    &((struct pfr_astats *)p)->pfras_a))
278 					err(1, "duplicate buffer");
279 		}
280 
281 		if (opts & PF_OPT_VERBOSE)
282 			flags |= PFR_FLAG_FEEDBACK;
283 		RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size,
284 		    &ndel, flags));
285 		xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size);
286 		if (opts & PF_OPT_VERBOSE)
287 			PFRB_FOREACH(a, &b2)
288 				if (opts & PF_OPT_VERBOSE2 ||
289 				    a->pfra_fback != PFR_FB_NONE)
290 					print_addrx(a, NULL,
291 					    opts & PF_OPT_USEDNS);
292 	} else if (!strcmp(command, "show")) {
293 		b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
294 			PFRB_ASTATS : PFRB_ADDRS;
295 		if (argc || file != NULL)
296 			usage();
297 		for (;;) {
298 			pfr_buf_grow(&b, b.pfrb_size);
299 			b.pfrb_size = b.pfrb_msize;
300 			if (opts & PF_OPT_VERBOSE)
301 				RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
302 				    &b.pfrb_size, flags));
303 			else
304 				RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
305 				    &b.pfrb_size, flags));
306 			if (b.pfrb_size <= b.pfrb_msize)
307 				break;
308 		}
309 		PFRB_FOREACH(p, &b)
310 			if (opts & PF_OPT_VERBOSE)
311 				print_astats(p, opts & PF_OPT_USEDNS);
312 			else
313 				print_addrx(p, NULL, opts & PF_OPT_USEDNS);
314 	} else if (!strcmp(command, "test")) {
315 		b.pfrb_type = PFRB_ADDRS;
316 		b2.pfrb_type = PFRB_ADDRS;
317 
318 		if (load_addr(&b, argc, argv, file, 1, opts))
319 			goto _error;
320 		if (opts & PF_OPT_VERBOSE2) {
321 			flags |= PFR_FLAG_REPLACE;
322 			PFRB_FOREACH(a, &b)
323 				if (pfr_buf_add(&b2, a))
324 					err(1, "duplicate buffer");
325 		}
326 		RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
327 		    &nmatch, flags));
328 		xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
329 		if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
330 			PFRB_FOREACH(a, &b)
331 				if (a->pfra_fback == PFR_FB_MATCH)
332 					print_addrx(a, NULL,
333 					    opts & PF_OPT_USEDNS);
334 		if (opts & PF_OPT_VERBOSE2) {
335 			a2 = NULL;
336 			PFRB_FOREACH(a, &b) {
337 				a2 = pfr_buf_next(&b2, a2);
338 				print_addrx(a2, a, opts & PF_OPT_USEDNS);
339 			}
340 		}
341 		if (nmatch < b.pfrb_size)
342 			rv = 2;
343 	} else if (!strcmp(command, "zero")) {
344 		if (argc || file != NULL)
345 			usage();
346 		flags |= PFR_FLAG_ADDRSTOO;
347 		RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
348 		xprintf(opts, "%d table/stats cleared", nzero);
349 	} else
350 		warnx("pfctl_table: unknown command '%s'", command);
351 	goto _cleanup;
352 
353 _error:
354 	rv = -1;
355 _cleanup:
356 	pfr_buf_clear(&b);
357 	pfr_buf_clear(&b2);
358 	return (rv);
359 }
360 
361 void
362 print_table(struct pfr_table *ta, int verbose, int debug)
363 {
364 	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
365 		return;
366 	if (verbose) {
367 		printf("%c%c%c%c%c%c%c\t%s",
368 		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
369 		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
370 		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
371 		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
372 		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
373 		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
374 		    (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
375 		    ta->pfrt_name);
376 		if (ta->pfrt_anchor[0])
377 			printf("\t%s", ta->pfrt_anchor);
378 		puts("");
379 	} else
380 		puts(ta->pfrt_name);
381 }
382 
383 void
384 print_tstats(struct pfr_tstats *ts, int debug)
385 {
386 	time_t	time = ts->pfrts_tzero;
387 	int	dir, op;
388 
389 	if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
390 		return;
391 	print_table(&ts->pfrts_t, 1, debug);
392 	printf("\tAddresses:   %d\n", ts->pfrts_cnt);
393 	printf("\tCleared:     %s", ctime(&time));
394 	printf("\tReferences:  [ Anchors: %-18d Rules: %-18d ]\n",
395 	    ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
396 	    ts->pfrts_refcnt[PFR_REFCNT_RULE]);
397 	printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
398 	    (unsigned long long)ts->pfrts_nomatch,
399 	    (unsigned long long)ts->pfrts_match);
400 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
401 		for (op = 0; op < PFR_OP_TABLE_MAX; op++)
402 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
403 			    stats_text[dir][op],
404 			    (unsigned long long)ts->pfrts_packets[dir][op],
405 			    (unsigned long long)ts->pfrts_bytes[dir][op]);
406 }
407 
408 int
409 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
410     int nonetwork, int opts)
411 {
412 	int	ev = 0;
413 	while (argc--)
414 		if ((ev = append_addr(b, *argv++, nonetwork, opts)) == -1) {
415 			if (errno)
416 				warn("cannot decode %s", argv[-1]);
417 			return (-1);
418 		}
419 	if (ev == 1) { /* expected further append_addr call */
420 		warnx("failed to decode %s", argv[-1]);
421 		return (-1);
422 	}
423 	if (pfr_buf_load(b, file, nonetwork, opts)) {
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 		struct sockaddr_storage ss;
457 
458 		strlcpy(host, "?", sizeof(host));
459 		bzero(&ss, sizeof(ss));
460 		ss.ss_family = ad->pfra_af;
461 		if (ss.ss_family == AF_INET) {
462 			struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
463 
464 			sin->sin_len = sizeof(*sin);
465 			sin->sin_addr = ad->pfra_ip4addr;
466 		} else {
467 			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
468 
469 			sin6->sin6_len = sizeof(*sin6);
470 			sin6->sin6_addr = ad->pfra_ip6addr;
471 		}
472 		if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
473 		    sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
474 			printf("\t(%s)", host);
475 	}
476 	if (ad->pfra_ifname[0] != '\0')
477 		printf("@%s", ad->pfra_ifname);
478 	printf("\n");
479 }
480 
481 void
482 print_astats(struct pfr_astats *as, int dns)
483 {
484 	time_t	time = as->pfras_tzero;
485 	int	dir, op;
486 
487 	print_addrx(&as->pfras_a, NULL, dns);
488 	printf("\tCleared:     %s", ctime(&time));
489 	if (as->pfras_a.pfra_states)
490 		printf("\tActive States:      %d\n", as->pfras_a.pfra_states);
491 	if (as->pfras_a.pfra_type == PFRKE_COST)
492 		printf("\tWeight:             %d\n", as->pfras_a.pfra_weight);
493 	if (as->pfras_a.pfra_ifname[0])
494 		printf("\tInterface:          %s\n", as->pfras_a.pfra_ifname);
495 	if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
496 		return;
497 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
498 		for (op = 0; op < PFR_OP_ADDR_MAX; op++)
499 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
500 			    stats_text[dir][op],
501 			    (unsigned long long)as->pfras_packets[dir][op],
502 			    (unsigned long long)as->pfras_bytes[dir][op]);
503 }
504 
505 void
506 radix_perror(void)
507 {
508 	extern char *__progname;
509 	fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
510 }
511 
512 int
513 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
514     struct pfr_buffer *ab, u_int32_t ticket)
515 {
516 	struct pfr_table tbl;
517 
518 	bzero(&tbl, sizeof(tbl));
519 	if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
520 	    sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
521 	    sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
522 		errx(1, "pfctl_define_table: strlcpy");
523 	tbl.pfrt_flags = flags;
524 
525 	return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
526 	    NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
527 }
528 
529 void
530 warn_duplicate_tables(const char *tablename, const char *anchorname)
531 {
532 	struct pfr_buffer b;
533 	struct pfr_table *t;
534 
535 	bzero(&b, sizeof(b));
536 	b.pfrb_type = PFRB_TABLES;
537 	for (;;) {
538 		pfr_buf_grow(&b, b.pfrb_size);
539 		b.pfrb_size = b.pfrb_msize;
540 		if (pfr_get_tables(NULL, b.pfrb_caddr,
541 		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
542 			err(1, "pfr_get_tables");
543 		if (b.pfrb_size <= b.pfrb_msize)
544 			break;
545 	}
546 	PFRB_FOREACH(t, &b) {
547 		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
548 			continue;
549 		if (!strcmp(anchorname, t->pfrt_anchor))
550 			continue;
551 		if (!strcmp(tablename, t->pfrt_name))
552 			warnx("warning: table <%s> already defined"
553 			    " in anchor \"%s\"", tablename,
554 			    t->pfrt_anchor[0] ? t->pfrt_anchor : "/");
555 	}
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 void
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 			exit(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 }
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:  [ States:  %-18d Rules: %-18d ]\n",
625 	    p->pfik_states, p->pfik_rules);
626 	for (i = 0; i < 8; i++) {
627 		af = (i>>2) & 1;
628 		dir = (i>>1) &1;
629 		act = i & 1;
630 		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
631 		    istats_text[af][dir][act],
632 		    (unsigned long long)p->pfik_packets[af][dir][act],
633 		    (unsigned long long)p->pfik_bytes[af][dir][act]);
634 	}
635 }
636