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