xref: /freebsd/sbin/pfctl/pfctl_table.c (revision 041ce1d690f1c58bd4a93ab808ded53444af2092)
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 <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <net/if.h>
42 #include <net/pfvar.h>
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <netdb.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 
54 #include "pfctl_parser.h"
55 #include "pfctl.h"
56 
57 extern void	usage(void);
58 static void	print_table(const struct pfr_table *, int, int);
59 static int	print_tstats(const struct pfr_tstats *, int);
60 static int	load_addr(struct pfr_buffer *, int, char *[], char *, int, int);
61 static void	print_addrx(struct pfr_addr *, struct pfr_addr *, int);
62 static int 	nonzero_astats(struct pfr_astats *);
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 		if ((opts & PF_OPT_RECURSE) == 0)	\
83 				radix_perror();		\
84 			goto _error;			\
85 		}					\
86 	} while (0)
87 
88 #define CREATE_TABLE do {						\
89 		warn_duplicate_tables(table.pfrt_name,			\
90 		    table.pfrt_anchor);					\
91 		table.pfrt_flags |= PFR_TFLAG_PERSIST;			\
92 		if ((!(opts & PF_OPT_NOACTION) ||			\
93 		    (opts & PF_OPT_DUMMYACTION)) &&			\
94 		    (pfr_add_table(&table, &nadd, flags)) &&		\
95 		    (errno != EPERM)) {					\
96 			radix_perror();					\
97 			goto _error;					\
98 		}							\
99 		if (nadd) {						\
100 			xprintf(opts, "%d table created", nadd);	\
101 			if (opts & PF_OPT_NOACTION)			\
102 				return (0);				\
103 		}							\
104 		table.pfrt_flags &= ~PFR_TFLAG_PERSIST;			\
105 	} while(0)
106 
107 int
pfctl_do_clear_tables(const char * anchor,int opts)108 pfctl_do_clear_tables(const char *anchor, int opts)
109 {
110 	int	rv;
111 
112 	if ((rv = pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts)) == -1) {
113 		if ((opts & PF_OPT_IGNFAIL) == 0)
114 			exit(1);
115 	}
116 
117 	return (rv);
118 }
119 
120 void
pfctl_show_tables(const char * anchor,int opts)121 pfctl_show_tables(const char *anchor, int opts)
122 {
123 	if (pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts))
124 		exit(1);
125 }
126 
127 int
pfctl_table(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)128 pfctl_table(int argc, char *argv[], char *tname, const char *command,
129     char *file, const char *anchor, int opts)
130 {
131 	struct pfr_table	 table;
132 	struct pfr_buffer	 b, b2;
133 	struct pfr_addr		*a, *a2;
134 	int			 nadd = 0, ndel = 0, nchange = 0, nzero = 0;
135 	int			 rv = 0, flags = 0, nmatch = 0;
136 	void			*p;
137 
138 	if (command == NULL)
139 		usage();
140 	if (opts & PF_OPT_NOACTION)
141 		flags |= PFR_FLAG_DUMMY;
142 
143 	bzero(&b, sizeof(b));
144 	bzero(&b2, sizeof(b2));
145 	bzero(&table, sizeof(table));
146 	if (tname != NULL) {
147 		if (strlen(tname) >= PF_TABLE_NAME_SIZE)
148 			usage();
149 		if (strlcpy(table.pfrt_name, tname,
150 		    sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
151 			errx(1, "pfctl_table: strlcpy");
152 	}
153 	if (strlcpy(table.pfrt_anchor, anchor,
154 	    sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
155 		errx(1, "pfctl_table: strlcpy");
156 
157 	if (!strcmp(command, "-F")) {
158 		if (argc || file != NULL)
159 			usage();
160 		RVTEST(pfctl_clear_tables(pfh, &table, &ndel, flags));
161 		xprintf(opts, "%d tables deleted", ndel);
162 	} else if (!strcmp(command, "-s")) {
163 		b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
164 		    PFRB_TSTATS : PFRB_TABLES;
165 		if (argc || file != NULL)
166 			usage();
167 
168 		if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
169 			pfctl_print_title("TABLES:");
170 
171 		if (opts & PF_OPT_VERBOSE2) {
172 			uintptr_t arg = opts & PF_OPT_DEBUG;
173 			pfctl_get_tstats(pfh, &table,
174 			    (pfctl_get_tstats_fn)print_tstats, (void *)arg);
175 		} else {
176 			for (;;) {
177 				pfr_buf_grow(&b, b.pfrb_size);
178 				b.pfrb_size = b.pfrb_msize;
179 				RVTEST(pfr_get_tables(&table,
180 				    b.pfrb_caddr, &b.pfrb_size, flags));
181 				if (b.pfrb_size <= b.pfrb_msize)
182 					break;
183 			}
184 
185 			if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
186 				pfctl_print_title("TABLES:");
187 
188 			PFRB_FOREACH(p, &b)
189 				print_table(p, opts & PF_OPT_VERBOSE,
190 				    opts & PF_OPT_DEBUG);
191 		}
192 	} else if (!strcmp(command, "kill")) {
193 		if (argc || file != NULL)
194 			usage();
195 		RVTEST(pfr_del_table(&table, &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, opts))
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) ||
215 				    a->pfra_fback != PFR_FB_NONE)
216 					print_addrx(a, NULL,
217 					    opts & PF_OPT_USEDNS);
218 	} else if (!strcmp(command, "delete")) {
219 		b.pfrb_type = PFRB_ADDRS;
220 		if (load_addr(&b, argc, argv, file, 0, opts))
221 			goto _error;
222 		if (opts & PF_OPT_VERBOSE)
223 			flags |= PFR_FLAG_FEEDBACK;
224 		RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
225 		    &ndel, flags));
226 		xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
227 		if (opts & PF_OPT_VERBOSE)
228 			PFRB_FOREACH(a, &b)
229 				if ((opts & PF_OPT_VERBOSE2) ||
230 				    a->pfra_fback != PFR_FB_NONE)
231 					print_addrx(a, NULL,
232 					    opts & PF_OPT_USEDNS);
233 	} else if (!strcmp(command, "replace")) {
234 		b.pfrb_type = PFRB_ADDRS;
235 		if (load_addr(&b, argc, argv, file, 0, opts))
236 			goto _error;
237 		CREATE_TABLE;
238 		if (opts & PF_OPT_VERBOSE)
239 			flags |= PFR_FLAG_FEEDBACK;
240 		for (;;) {
241 			int sz2 = b.pfrb_msize;
242 
243 			RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
244 			    &sz2, &nadd, &ndel, &nchange, flags));
245 			if (sz2 <= b.pfrb_msize) {
246 				b.pfrb_size = sz2;
247 				break;
248 			} else
249 				pfr_buf_grow(&b, sz2);
250 		}
251 		if (nadd)
252 			xprintf(opts, "%d addresses added", nadd);
253 		if (ndel)
254 			xprintf(opts, "%d addresses deleted", ndel);
255 		if (nchange)
256 			xprintf(opts, "%d addresses changed", nchange);
257 		if (!nadd && !ndel && !nchange)
258 			xprintf(opts, "no changes");
259 		if (opts & PF_OPT_VERBOSE)
260 			PFRB_FOREACH(a, &b)
261 				if ((opts & PF_OPT_VERBOSE2) ||
262 				    a->pfra_fback != PFR_FB_NONE)
263 					print_addrx(a, NULL,
264 					    opts & PF_OPT_USEDNS);
265 	} else if (!strcmp(command, "expire")) {
266 		const char		*errstr;
267 		u_int			 lifetime;
268 
269 		b.pfrb_type = PFRB_ASTATS;
270 		b2.pfrb_type = PFRB_ADDRS;
271 		if (argc != 1 || file != NULL)
272 			usage();
273 		lifetime = strtonum(*argv, 0, UINT_MAX, &errstr);
274 		if (errstr)
275 			errx(1, "expiry time: %s", errstr);
276 		for (;;) {
277 			pfr_buf_grow(&b, b.pfrb_size);
278 			b.pfrb_size = b.pfrb_msize;
279 			RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
280 			    &b.pfrb_size, flags));
281 			if (b.pfrb_size <= b.pfrb_msize)
282 				break;
283 		}
284 		PFRB_FOREACH(p, &b) {
285 			((struct pfr_astats *)p)->pfras_a.pfra_fback = PFR_FB_NONE;
286 			if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero >
287 			    lifetime)
288 				if (pfr_buf_add(&b2,
289 				    &((struct pfr_astats *)p)->pfras_a))
290 					err(1, "duplicate buffer");
291 		}
292 
293 		if (opts & PF_OPT_VERBOSE)
294 			flags |= PFR_FLAG_FEEDBACK;
295 		RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size,
296 		    &ndel, flags));
297 		xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size);
298 		if (opts & PF_OPT_VERBOSE)
299 			PFRB_FOREACH(a, &b2)
300 				if ((opts & PF_OPT_VERBOSE2) ||
301 				    a->pfra_fback != PFR_FB_NONE)
302 					print_addrx(a, NULL,
303 					    opts & PF_OPT_USEDNS);
304 	} else if (!strcmp(command, "reset")) {
305 		struct pfr_astats 	*as;
306 
307 		b.pfrb_type = PFRB_ASTATS;
308 		b2.pfrb_type = PFRB_ADDRS;
309 		if (argc || file != NULL)
310 			usage();
311 		do {
312 			pfr_buf_grow(&b, b.pfrb_size);
313 			b.pfrb_size = b.pfrb_msize;
314 			RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
315 			    &b.pfrb_size, flags));
316 		} while (b.pfrb_size > b.pfrb_msize);
317 		PFRB_FOREACH(as, &b) {
318 			as->pfras_a.pfra_fback = 0;
319 			if (nonzero_astats(as))
320 				if (pfr_buf_add(&b2, &as->pfras_a))
321 					err(1, "duplicate buffer");
322 		}
323 
324 		if (opts & PF_OPT_VERBOSE)
325 			flags |= PFR_FLAG_FEEDBACK;
326 		RVTEST(pfr_clr_astats(&table, b2.pfrb_caddr, b2.pfrb_size,
327 		    &nzero, flags));
328 		xprintf(opts, "%d/%d stats cleared", nzero, b.pfrb_size);
329 		if (opts & PF_OPT_VERBOSE)
330 			PFRB_FOREACH(a, &b2)
331 				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
332 					print_addrx(a, NULL,
333 					    opts & PF_OPT_USEDNS);
334 	} else if (!strcmp(command, "show")) {
335 		b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
336 			PFRB_ASTATS : PFRB_ADDRS;
337 		if (argc || file != NULL)
338 			usage();
339 		for (;;) {
340 			pfr_buf_grow(&b, b.pfrb_size);
341 			b.pfrb_size = b.pfrb_msize;
342 			if (opts & PF_OPT_VERBOSE)
343 				RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
344 				    &b.pfrb_size, flags));
345 			else
346 				RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
347 				    &b.pfrb_size, flags));
348 			if (b.pfrb_size <= b.pfrb_msize)
349 				break;
350 		}
351 		PFRB_FOREACH(p, &b)
352 			if (opts & PF_OPT_VERBOSE)
353 				print_astats(p, opts & PF_OPT_USEDNS);
354 			else
355 				print_addrx(p, NULL, opts & PF_OPT_USEDNS);
356 	} else if (!strcmp(command, "test")) {
357 		b.pfrb_type = PFRB_ADDRS;
358 		b2.pfrb_type = PFRB_ADDRS;
359 
360 		if (load_addr(&b, argc, argv, file, 1, opts))
361 			goto _error;
362 		if (opts & PF_OPT_VERBOSE2) {
363 			flags |= PFR_FLAG_REPLACE;
364 			PFRB_FOREACH(a, &b)
365 				if (pfr_buf_add(&b2, a))
366 					err(1, "duplicate buffer");
367 		}
368 		RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
369 		    &nmatch, flags));
370 		xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
371 		if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
372 			PFRB_FOREACH(a, &b)
373 				if (a->pfra_fback == PFR_FB_MATCH)
374 					print_addrx(a, NULL,
375 					    opts & PF_OPT_USEDNS);
376 		if (opts & PF_OPT_VERBOSE2) {
377 			a2 = NULL;
378 			PFRB_FOREACH(a, &b) {
379 				a2 = pfr_buf_next(&b2, a2);
380 				print_addrx(a2, a, opts & PF_OPT_USEDNS);
381 			}
382 		}
383 		if (nmatch < b.pfrb_size)
384 			rv = 2;
385 	} else if (!strcmp(command, "zero") && (argc || file != NULL)) {
386 		b.pfrb_type = PFRB_ADDRS;
387 		if (load_addr(&b, argc, argv, file, 0, opts))
388 			goto _error;
389 		if (opts & PF_OPT_VERBOSE)
390 			flags |= PFR_FLAG_FEEDBACK;
391 		RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size,
392 		    &nzero, flags));
393 		xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size);
394 		if (opts & PF_OPT_VERBOSE)
395 			PFRB_FOREACH(a, &b)
396 				if (opts & PF_OPT_VERBOSE2 ||
397 				    a->pfra_fback != PFR_FB_NONE)
398 					print_addrx(a, NULL,
399 					    opts & PF_OPT_USEDNS);
400 	} else if (!strcmp(command, "zero")) {
401 		flags |= PFR_FLAG_ADDRSTOO;
402 		RVTEST(pfctl_clear_tstats(pfh, &table, &nzero, flags));
403 		xprintf(opts, "%d table/stats cleared", nzero);
404 	} else
405 		warnx("pfctl_table: unknown command '%s'", command);
406 	goto _cleanup;
407 
408 _error:
409 	rv = -1;
410 _cleanup:
411 	pfr_buf_clear(&b);
412 	pfr_buf_clear(&b2);
413 	return (rv);
414 }
415 
416 void
print_table(const struct pfr_table * ta,int verbose,int debug)417 print_table(const struct pfr_table *ta, int verbose, int debug)
418 {
419 	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
420 		return;
421 	if (verbose) {
422 		printf("%c%c%c%c%c%c%c\t%s",
423 		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
424 		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
425 		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
426 		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
427 		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
428 		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
429 		    (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
430 		    ta->pfrt_name);
431 		if (ta->pfrt_anchor[0])
432 			printf("\t%s", ta->pfrt_anchor);
433 		puts("");
434 	} else
435 		puts(ta->pfrt_name);
436 }
437 
438 int
print_tstats(const struct pfr_tstats * ts,int debug)439 print_tstats(const struct pfr_tstats *ts, int debug)
440 {
441 	time_t	time = ts->pfrts_tzero;
442 	int	dir, op;
443 
444 	if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
445 		return (0);
446 	print_table(&ts->pfrts_t, 1, debug);
447 	printf("\tAddresses:   %d\n", ts->pfrts_cnt);
448 	printf("\tCleared:     %s", ctime(&time));
449 	printf("\tReferences:  [ Anchors: %-18d Rules: %-18d ]\n",
450 	    ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
451 	    ts->pfrts_refcnt[PFR_REFCNT_RULE]);
452 	printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
453 	    (unsigned long long)ts->pfrts_nomatch,
454 	    (unsigned long long)ts->pfrts_match);
455 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
456 		for (op = 0; op < PFR_OP_TABLE_MAX; op++)
457 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
458 			    stats_text[dir][op],
459 			    (unsigned long long)ts->pfrts_packets[dir][op],
460 			    (unsigned long long)ts->pfrts_bytes[dir][op]);
461 
462 	return (0);
463 }
464 
465 int
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork,int opts)466 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
467     int nonetwork, int opts)
468 {
469 	while (argc--)
470 		if (append_addr(b, *argv++, nonetwork, opts)) {
471 			if (errno)
472 				warn("cannot decode %s", argv[-1]);
473 			return (-1);
474 		}
475 	if (pfr_buf_load(b, file, nonetwork, append_addr, opts)) {
476 		warn("cannot load %s", file);
477 		return (-1);
478 	}
479 	return (0);
480 }
481 
482 void
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)483 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
484 {
485 	char		ch, buf[256] = "{error}";
486 	char		fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
487 	unsigned int	fback, hostnet;
488 
489 	fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
490 	ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
491 	hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
492 	inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
493 	printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
494 	if (ad->pfra_net < hostnet)
495 		printf("/%d", ad->pfra_net);
496 	if (rad != NULL && fback != PFR_FB_NONE) {
497 		if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
498 			errx(1, "print_addrx: strlcpy");
499 		inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
500 		printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
501 		if (rad->pfra_net < hostnet)
502 			printf("/%d", rad->pfra_net);
503 	}
504 	if (rad != NULL && fback == PFR_FB_NONE)
505 		printf("\t nomatch");
506 	if (dns && ad->pfra_net == hostnet) {
507 		char host[NI_MAXHOST];
508 		struct sockaddr_storage ss;
509 
510 		strlcpy(host, "?", sizeof(host));
511 		bzero(&ss, sizeof(ss));
512 		ss.ss_family = ad->pfra_af;
513 		if (ss.ss_family == AF_INET) {
514 			struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
515 
516 			sin->sin_len = sizeof(*sin);
517 			sin->sin_addr = ad->pfra_ip4addr;
518 		} else {
519 			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
520 
521 			sin6->sin6_len = sizeof(*sin6);
522 			sin6->sin6_addr = ad->pfra_ip6addr;
523 		}
524 		if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
525 		    sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
526 			printf("\t(%s)", host);
527 	}
528 	printf("\n");
529 }
530 
531 int
nonzero_astats(struct pfr_astats * as)532 nonzero_astats(struct pfr_astats *as)
533 {
534 	uint64_t s = 0;
535 
536 	for (int dir = 0; dir < PFR_DIR_MAX; dir++)
537 		for (int op = 0; op < PFR_OP_ADDR_MAX; op++)
538 			s |= as->pfras_packets[dir][op] |
539 			     as->pfras_bytes[dir][op];
540 
541 	return (!!s);
542 }
543 
544 void
print_astats(struct pfr_astats * as,int dns)545 print_astats(struct pfr_astats *as, int dns)
546 {
547 	time_t	time = as->pfras_tzero;
548 	int	dir, op;
549 
550 	print_addrx(&as->pfras_a, NULL, dns);
551 	printf("\tCleared:     %s", ctime(&time));
552  	if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
553 		return;
554 	for (dir = 0; dir < PFR_DIR_MAX; dir++)
555 		for (op = 0; op < PFR_OP_ADDR_MAX; op++)
556 			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
557 			    stats_text[dir][op],
558 			    (unsigned long long)as->pfras_packets[dir][op],
559 			    (unsigned long long)as->pfras_bytes[dir][op]);
560 }
561 
562 void
radix_perror(void)563 radix_perror(void)
564 {
565 	extern char *__progname;
566 	fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
567 }
568 
569 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,struct pfr_buffer * ab,u_int32_t ticket)570 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
571     struct pfr_buffer *ab, u_int32_t ticket)
572 {
573 	struct pfr_table tbl;
574 
575 	bzero(&tbl, sizeof(tbl));
576 	if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
577 	    sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
578 	    sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
579 		errx(1, "pfctl_define_table: strlcpy");
580 	tbl.pfrt_flags = flags;
581 
582 	return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
583 	    NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
584 }
585 
586 void
warn_duplicate_tables(const char * tablename,const char * anchorname)587 warn_duplicate_tables(const char *tablename, const char *anchorname)
588 {
589 	struct pfr_buffer b;
590 	struct pfr_table *t;
591 
592 	bzero(&b, sizeof(b));
593 	b.pfrb_type = PFRB_TABLES;
594 	for (;;) {
595 		pfr_buf_grow(&b, b.pfrb_size);
596 		b.pfrb_size = b.pfrb_msize;
597 		if (pfr_get_tables(NULL, b.pfrb_caddr,
598 		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
599 			err(1, "pfr_get_tables");
600 		if (b.pfrb_size <= b.pfrb_msize)
601 			break;
602 	}
603 	PFRB_FOREACH(t, &b) {
604 		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
605 			continue;
606 		if (!strcmp(anchorname, t->pfrt_anchor))
607 			continue;
608 		if (!strcmp(tablename, t->pfrt_name))
609 			warnx("warning: table <%s> already defined"
610 			    " in anchor \"%s\"", tablename,
611 			    t->pfrt_anchor[0] ? t->pfrt_anchor : "/");
612 	}
613 	pfr_buf_clear(&b);
614 }
615 
616 void
xprintf(int opts,const char * fmt,...)617 xprintf(int opts, const char *fmt, ...)
618 {
619 	va_list args;
620 
621 	if (opts & PF_OPT_QUIET)
622 		return;
623 
624 	va_start(args, fmt);
625 	vfprintf(stderr, fmt, args);
626 	va_end(args);
627 
628 	if (opts & PF_OPT_DUMMYACTION)
629 		fprintf(stderr, " (dummy).\n");
630 	else if (opts & PF_OPT_NOACTION)
631 		fprintf(stderr, " (syntax only).\n");
632 	else
633 		fprintf(stderr, ".\n");
634 }
635 
636 
637 /* interface stuff */
638 
639 void
pfctl_show_ifaces(const char * filter,int opts)640 pfctl_show_ifaces(const char *filter, int opts)
641 {
642 	struct pfr_buffer	 b;
643 	struct pfi_kif		*p;
644 
645 	bzero(&b, sizeof(b));
646 	b.pfrb_type = PFRB_IFACES;
647 	for (;;) {
648 		pfr_buf_grow(&b, b.pfrb_size);
649 		b.pfrb_size = b.pfrb_msize;
650 		if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
651 			radix_perror();
652 			exit(1);
653 		}
654 		if (b.pfrb_size <= b.pfrb_msize)
655 			break;
656 	}
657 	if (opts & PF_OPT_SHOWALL)
658 		pfctl_print_title("INTERFACES:");
659 	PFRB_FOREACH(p, &b)
660 		print_iface(p, opts);
661 }
662 
663 void
print_iface(struct pfi_kif * p,int opts)664 print_iface(struct pfi_kif *p, int opts)
665 {
666 	time_t	tzero = p->pfik_tzero;
667 	int	i, af, dir, act;
668 
669 	printf("%s", p->pfik_name);
670 	if (opts & PF_OPT_VERBOSE) {
671 		if (p->pfik_flags & PFI_IFLAG_SKIP)
672 			printf(" (skip)");
673 	}
674 	printf("\n");
675 
676 	if (!(opts & PF_OPT_VERBOSE2))
677 		return;
678 	printf("\tCleared:     %s", ctime(&tzero));
679 	printf("\tReferences:  %-18d\n", p->pfik_rulerefs);
680 	for (i = 0; i < 8; i++) {
681 		af = (i>>2) & 1;
682 		dir = (i>>1) &1;
683 		act = i & 1;
684 		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
685 		    istats_text[af][dir][act],
686 		    (unsigned long long)p->pfik_packets[af][dir][act],
687 		    (unsigned long long)p->pfik_bytes[af][dir][act]);
688 	}
689 }
690