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