xref: /freebsd/sbin/pfctl/pfctl_table.c (revision 17aab35a77a1b1bf02fc85bb8ffadccb0ca5006d)
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 
56 extern void	usage(void);
57 static int	pfctl_table(int, char *[], char *, const char *, char *,
58 		    const char *, int);
59 static void	print_table(struct pfr_table *, int, int);
60 static void	print_tstats(struct pfr_tstats *, int);
61 static int	load_addr(struct pfr_buffer *, int, char *[], char *, int);
62 static void	print_addrx(struct pfr_addr *, struct pfr_addr *, int);
63 static void	print_astats(struct pfr_astats *, int);
64 static void	radix_perror(void);
65 static void	xprintf(int, const char *, ...);
66 static void	print_iface(struct pfi_kif *, int);
67 
68 static 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 
73 static 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 
105 int
106 pfctl_clear_tables(const char *anchor, int opts)
107 {
108 	return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts);
109 }
110 
111 int
112 pfctl_show_tables(const char *anchor, int opts)
113 {
114 	return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts);
115 }
116 
117 int
118 pfctl_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 
126 int
127 pfctl_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") && (argc || file != NULL)) {
348 		b.pfrb_type = PFRB_ADDRS;
349 		if (load_addr(&b, argc, argv, file, 0))
350 			goto _error;
351 		if (opts & PF_OPT_VERBOSE)
352 			flags |= PFR_FLAG_FEEDBACK;
353 		RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size,
354 		    &nzero, flags));
355 		xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size);
356 		if (opts & PF_OPT_VERBOSE)
357 			PFRB_FOREACH(a, &b)
358 				if (opts & PF_OPT_VERBOSE2 ||
359 				    a->pfra_fback != PFR_FB_NONE)
360 					print_addrx(a, NULL,
361 					    opts & PF_OPT_USEDNS);
362 	} else if (!strcmp(command, "zero")) {
363 		flags |= PFR_FLAG_ADDRSTOO;
364 		RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
365 		xprintf(opts, "%d table/stats cleared", nzero);
366 	} else
367 		warnx("pfctl_table: unknown command '%s'", command);
368 	goto _cleanup;
369 
370 _error:
371 	rv = -1;
372 _cleanup:
373 	pfr_buf_clear(&b);
374 	pfr_buf_clear(&b2);
375 	return (rv);
376 }
377 
378 void
379 print_table(struct pfr_table *ta, int verbose, int debug)
380 {
381 	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
382 		return;
383 	if (verbose) {
384 		printf("%c%c%c%c%c%c%c\t%s",
385 		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
386 		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
387 		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
388 		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
389 		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
390 		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
391 		    (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
392 		    ta->pfrt_name);
393 		if (ta->pfrt_anchor[0])
394 			printf("\t%s", ta->pfrt_anchor);
395 		puts("");
396 	} else
397 		puts(ta->pfrt_name);
398 }
399 
400 void
401 print_tstats(struct pfr_tstats *ts, int debug)
402 {
403 	time_t	time = ts->pfrts_tzero;
404 	int	dir, op;
405 
406 	if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
407 		return;
408 	print_table(&ts->pfrts_t, 1, debug);
409 	printf("\tAddresses:   %d\n", ts->pfrts_cnt);
410 	printf("\tCleared:     %s", ctime(&time));
411 	printf("\tReferences:  [ Anchors: %-18d Rules: %-18d ]\n",
412 	    ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
413 	    ts->pfrts_refcnt[PFR_REFCNT_RULE]);
414 	printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
415 	    (unsigned long long)ts->pfrts_nomatch,
416 	    (unsigned long long)ts->pfrts_match);
417 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
418 		for (op = 0; op < PFR_OP_TABLE_MAX; op++)
419 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
420 			    stats_text[dir][op],
421 			    (unsigned long long)ts->pfrts_packets[dir][op],
422 			    (unsigned long long)ts->pfrts_bytes[dir][op]);
423 }
424 
425 int
426 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
427     int nonetwork)
428 {
429 	while (argc--)
430 		if (append_addr(b, *argv++, nonetwork)) {
431 			if (errno)
432 				warn("cannot decode %s", argv[-1]);
433 			return (-1);
434 		}
435 	if (pfr_buf_load(b, file, nonetwork, append_addr)) {
436 		warn("cannot load %s", file);
437 		return (-1);
438 	}
439 	return (0);
440 }
441 
442 void
443 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
444 {
445 	char		ch, buf[256] = "{error}";
446 	char		fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
447 	unsigned int	fback, hostnet;
448 
449 	fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
450 	ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
451 	hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
452 	inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
453 	printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
454 	if (ad->pfra_net < hostnet)
455 		printf("/%d", ad->pfra_net);
456 	if (rad != NULL && fback != PFR_FB_NONE) {
457 		if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
458 			errx(1, "print_addrx: strlcpy");
459 		inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
460 		printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
461 		if (rad->pfra_net < hostnet)
462 			printf("/%d", rad->pfra_net);
463 	}
464 	if (rad != NULL && fback == PFR_FB_NONE)
465 		printf("\t nomatch");
466 	if (dns && ad->pfra_net == hostnet) {
467 		char host[NI_MAXHOST];
468 		union sockaddr_union sa;
469 
470 		strlcpy(host, "?", sizeof(host));
471 		bzero(&sa, sizeof(sa));
472 		sa.sa.sa_family = ad->pfra_af;
473 		if (sa.sa.sa_family == AF_INET) {
474 			sa.sa.sa_len = sizeof(sa.sin);
475 			sa.sin.sin_addr = ad->pfra_ip4addr;
476 		} else {
477 			sa.sa.sa_len = sizeof(sa.sin6);
478 			sa.sin6.sin6_addr = ad->pfra_ip6addr;
479 		}
480 		if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host),
481 		    NULL, 0, NI_NAMEREQD) == 0)
482 			printf("\t(%s)", host);
483 	}
484 	printf("\n");
485 }
486 
487 void
488 print_astats(struct pfr_astats *as, int dns)
489 {
490 	time_t	time = as->pfras_tzero;
491 	int	dir, op;
492 
493 	print_addrx(&as->pfras_a, NULL, dns);
494 	printf("\tCleared:     %s", ctime(&time));
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_namespace_collision(const char *filter)
531 {
532 	struct pfr_buffer b;
533 	struct pfr_table *t;
534 	const char *name = NULL, *lastcoll;
535 	int coll = 0;
536 
537 	bzero(&b, sizeof(b));
538 	b.pfrb_type = PFRB_TABLES;
539 	for (;;) {
540 		pfr_buf_grow(&b, b.pfrb_size);
541 		b.pfrb_size = b.pfrb_msize;
542 		if (pfr_get_tables(NULL, b.pfrb_caddr,
543 		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
544 			err(1, "pfr_get_tables");
545 		if (b.pfrb_size <= b.pfrb_msize)
546 			break;
547 	}
548 	PFRB_FOREACH(t, &b) {
549 		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
550 			continue;
551 		if (filter != NULL && strcmp(filter, t->pfrt_name))
552 			continue;
553 		if (!t->pfrt_anchor[0])
554 			name = t->pfrt_name;
555 		else if (name != NULL && !strcmp(name, t->pfrt_name)) {
556 			coll++;
557 			lastcoll = name;
558 			name = NULL;
559 		}
560 	}
561 	if (coll == 1)
562 		warnx("warning: namespace collision with <%s> global table.",
563 		    lastcoll);
564 	else if (coll > 1)
565 		warnx("warning: namespace collisions with %d global tables.",
566 		    coll);
567 	pfr_buf_clear(&b);
568 }
569 
570 void
571 xprintf(int opts, const char *fmt, ...)
572 {
573 	va_list args;
574 
575 	if (opts & PF_OPT_QUIET)
576 		return;
577 
578 	va_start(args, fmt);
579 	vfprintf(stderr, fmt, args);
580 	va_end(args);
581 
582 	if (opts & PF_OPT_DUMMYACTION)
583 		fprintf(stderr, " (dummy).\n");
584 	else if (opts & PF_OPT_NOACTION)
585 		fprintf(stderr, " (syntax only).\n");
586 	else
587 		fprintf(stderr, ".\n");
588 }
589 
590 
591 /* interface stuff */
592 
593 int
594 pfctl_show_ifaces(const char *filter, int opts)
595 {
596 	struct pfr_buffer	 b;
597 	struct pfi_kif		*p;
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 			return (1);
607 		}
608 		if (b.pfrb_size <= b.pfrb_msize)
609 			break;
610 	}
611 	if (opts & PF_OPT_SHOWALL)
612 		pfctl_print_title("INTERFACES:");
613 	PFRB_FOREACH(p, &b)
614 		print_iface(p, opts);
615 	return (0);
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:  %-18d\n", p->pfik_rulerefs);
635 	for (i = 0; i < 8; i++) {
636 		af = (i>>2) & 1;
637 		dir = (i>>1) &1;
638 		act = i & 1;
639 		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
640 		    istats_text[af][dir][act],
641 		    (unsigned long long)p->pfik_packets[af][dir][act],
642 		    (unsigned long long)p->pfik_bytes[af][dir][act]);
643 	}
644 }
645