GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/pfctl/pfctl_table.c Lines: 82 371 22.1 %
Date: 2017-11-13 Branches: 44 385 11.4 %

Line Branch Exec Source
1
/*	$OpenBSD: pfctl_table.c,v 1.77 2017/08/11 22:30:38 benno 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 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, 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/Match:",	"In/Pass:",	"In/XPass:" },
70
	{ "Out/Block:",	"Out/Match:",	"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
void
106
pfctl_clear_tables(const char *anchor, int opts)
107
{
108
	if (pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts) == -1)
109
		exit(1);
110
}
111
112
void
113
pfctl_show_tables(const char *anchor, int opts)
114
{
115
	if (pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts) == -1)
116
		exit(1);
117
}
118
119
int
120
pfctl_command_tables(int argc, char *argv[], char *tname,
121
    const char *command, char *file, const char *anchor, int opts)
122
{
123
60
	if (tname == NULL || command == NULL)
124
		usage();
125
30
	return pfctl_table(argc, argv, tname, command, file, anchor, opts);
126
}
127
128
int
129
pfctl_table(int argc, char *argv[], char *tname, const char *command,
130
    char *file, const char *anchor, int opts)
131
{
132
60
	struct pfr_table	 table;
133
30
	struct pfr_buffer	 b, b2;
134
	struct pfr_addr		*a, *a2;
135
30
	int			 nadd = 0, ndel = 0, nchange = 0, nzero = 0;
136
30
	int			 rv = 0, flags = 0, nmatch = 0;
137
	void			*p;
138
139
30
	if (command == NULL)
140
		usage();
141
30
	if (opts & PF_OPT_NOACTION)
142
		flags |= PFR_FLAG_DUMMY;
143
144
30
	bzero(&b, sizeof(b));
145
30
	bzero(&b2, sizeof(b2));
146
30
	bzero(&table, sizeof(table));
147
30
	if (tname != NULL) {
148
30
		if (strlen(tname) >= PF_TABLE_NAME_SIZE)
149
			usage();
150
60
		if (strlcpy(table.pfrt_name, tname,
151
30
		    sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
152
			errx(1, "pfctl_table: strlcpy");
153
	}
154
60
	if (strlcpy(table.pfrt_anchor, anchor,
155
30
	    sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
156
		errx(1, "pfctl_table: strlcpy");
157
158
30
	if (!strcmp(command, "-F")) {
159
		if (argc || file != NULL)
160
			usage();
161
		RVTEST(pfr_clr_tables(&table, &ndel, flags));
162
		xprintf(opts, "%d tables deleted", ndel);
163
30
	} else if (!strcmp(command, "-s")) {
164
		b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
165
		    PFRB_TSTATS : PFRB_TABLES;
166
		if (argc || file != NULL)
167
			usage();
168
		for (;;) {
169
			pfr_buf_grow(&b, b.pfrb_size);
170
			b.pfrb_size = b.pfrb_msize;
171
			if (opts & PF_OPT_VERBOSE2)
172
				RVTEST(pfr_get_tstats(&table,
173
				    b.pfrb_caddr, &b.pfrb_size, flags));
174
			else
175
				RVTEST(pfr_get_tables(&table,
176
				    b.pfrb_caddr, &b.pfrb_size, flags));
177
			if (b.pfrb_size <= b.pfrb_msize)
178
				break;
179
		}
180
181
		if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
182
			pfctl_print_title("TABLES:");
183
184
		PFRB_FOREACH(p, &b)
185
			if (opts & PF_OPT_VERBOSE2)
186
				print_tstats(p, opts & PF_OPT_DEBUG);
187
			else
188
				print_table(p, opts & PF_OPT_VERBOSE,
189
				    opts & PF_OPT_DEBUG);
190
30
	} else if (!strcmp(command, "kill")) {
191
		if (argc || file != NULL)
192
			usage();
193
		RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
194
		xprintf(opts, "%d table deleted", ndel);
195
30
	} else if (!strcmp(command, "flush")) {
196
		if (argc || file != NULL)
197
			usage();
198
		RVTEST(pfr_clr_addrs(&table, &ndel, flags));
199
		xprintf(opts, "%d addresses deleted", ndel);
200
30
	} else if (!strcmp(command, "add")) {
201
		b.pfrb_type = PFRB_ADDRS;
202
		if (load_addr(&b, argc, argv, file, 0, opts))
203
			goto _error;
204
		CREATE_TABLE;
205
		if (opts & PF_OPT_VERBOSE)
206
			flags |= PFR_FLAG_FEEDBACK;
207
		RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
208
		    &nadd, flags));
209
		xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
210
		if (opts & PF_OPT_VERBOSE)
211
			PFRB_FOREACH(a, &b)
212
				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
213
					print_addrx(a, NULL,
214
					    opts & PF_OPT_USEDNS);
215
30
	} else if (!strcmp(command, "delete")) {
216
		b.pfrb_type = PFRB_ADDRS;
217
		if (load_addr(&b, argc, argv, file, 0, opts))
218
			goto _error;
219
		if (opts & PF_OPT_VERBOSE)
220
			flags |= PFR_FLAG_FEEDBACK;
221
		RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
222
		    &ndel, flags));
223
		xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
224
		if (opts & PF_OPT_VERBOSE)
225
			PFRB_FOREACH(a, &b)
226
				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
227
					print_addrx(a, NULL,
228
					    opts & PF_OPT_USEDNS);
229
30
	} else if (!strcmp(command, "replace")) {
230
		b.pfrb_type = PFRB_ADDRS;
231
		if (load_addr(&b, argc, argv, file, 0, opts))
232
			goto _error;
233
		CREATE_TABLE;
234
		if (opts & PF_OPT_VERBOSE)
235
			flags |= PFR_FLAG_FEEDBACK;
236
		for (;;) {
237
			int sz2 = b.pfrb_msize;
238
239
			RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
240
			    &sz2, &nadd, &ndel, &nchange, flags));
241
			if (sz2 <= b.pfrb_msize) {
242
				b.pfrb_size = sz2;
243
				break;
244
			} else
245
				pfr_buf_grow(&b, sz2);
246
		}
247
		if (nadd)
248
			xprintf(opts, "%d addresses added", nadd);
249
		if (ndel)
250
			xprintf(opts, "%d addresses deleted", ndel);
251
		if (nchange)
252
			xprintf(opts, "%d addresses changed", nchange);
253
		if (!nadd && !ndel && !nchange)
254
			xprintf(opts, "no changes");
255
		if (opts & PF_OPT_VERBOSE)
256
			PFRB_FOREACH(a, &b)
257
				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
258
					print_addrx(a, NULL,
259
					    opts & PF_OPT_USEDNS);
260
30
	} else if (!strcmp(command, "expire")) {
261
		const char		*errstr;
262
		u_int			 lifetime;
263
264
		b.pfrb_type = PFRB_ASTATS;
265
		b2.pfrb_type = PFRB_ADDRS;
266
		if (argc != 1 || file != NULL)
267
			usage();
268
		lifetime = strtonum(*argv, 0, UINT_MAX, &errstr);
269
		if (errstr)
270
			errx(1, "expiry time: %s", errstr);
271
		for (;;) {
272
			pfr_buf_grow(&b, b.pfrb_size);
273
			b.pfrb_size = b.pfrb_msize;
274
			RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
275
			    &b.pfrb_size, flags));
276
			if (b.pfrb_size <= b.pfrb_msize)
277
				break;
278
		}
279
		PFRB_FOREACH(p, &b) {
280
			((struct pfr_astats *)p)->pfras_a.pfra_fback = 0;
281
			if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero >
282
			     lifetime)
283
				if (pfr_buf_add(&b2,
284
				    &((struct pfr_astats *)p)->pfras_a))
285
					err(1, "duplicate buffer");
286
		}
287
288
		if (opts & PF_OPT_VERBOSE)
289
			flags |= PFR_FLAG_FEEDBACK;
290
		RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size,
291
		    &ndel, flags));
292
		xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size);
293
		if (opts & PF_OPT_VERBOSE)
294
			PFRB_FOREACH(a, &b2)
295
				if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
296
					print_addrx(a, NULL,
297
					    opts & PF_OPT_USEDNS);
298

30
	} else if (!strcmp(command, "show")) {
299
30
		b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
300
			PFRB_ASTATS : PFRB_ADDRS;
301
30
		if (argc || file != NULL)
302
			usage();
303
30
		for (;;) {
304
30
			pfr_buf_grow(&b, b.pfrb_size);
305
30
			b.pfrb_size = b.pfrb_msize;
306
30
			if (opts & PF_OPT_VERBOSE)
307
				RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
308
				    &b.pfrb_size, flags));
309
			else
310

60
				RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
311
				    &b.pfrb_size, flags));
312
30
			if (b.pfrb_size <= b.pfrb_msize)
313
				break;
314
		}
315
148
		PFRB_FOREACH(p, &b)
316
44
			if (opts & PF_OPT_VERBOSE)
317
				print_astats(p, opts & PF_OPT_USEDNS);
318
			else
319
44
				print_addrx(p, NULL, opts & PF_OPT_USEDNS);
320
	} else if (!strcmp(command, "test")) {
321
		b.pfrb_type = PFRB_ADDRS;
322
		b2.pfrb_type = PFRB_ADDRS;
323
324
		if (load_addr(&b, argc, argv, file, 1, opts))
325
			goto _error;
326
		if (opts & PF_OPT_VERBOSE2) {
327
			flags |= PFR_FLAG_REPLACE;
328
			PFRB_FOREACH(a, &b)
329
				if (pfr_buf_add(&b2, a))
330
					err(1, "duplicate buffer");
331
		}
332
		RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
333
		    &nmatch, flags));
334
		xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
335
		if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
336
			PFRB_FOREACH(a, &b)
337
				if (a->pfra_fback == PFR_FB_MATCH)
338
					print_addrx(a, NULL,
339
					    opts & PF_OPT_USEDNS);
340
		if (opts & PF_OPT_VERBOSE2) {
341
			a2 = NULL;
342
			PFRB_FOREACH(a, &b) {
343
				a2 = pfr_buf_next(&b2, a2);
344
				print_addrx(a2, a, opts & PF_OPT_USEDNS);
345
			}
346
		}
347
		if (nmatch < b.pfrb_size)
348
			rv = 2;
349
	} else if (!strcmp(command, "zero")) {
350
		if (argc || file != NULL)
351
			usage();
352
		flags |= PFR_FLAG_ADDRSTOO;
353
		RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
354
		xprintf(opts, "%d table/stats cleared", nzero);
355
	} else
356
		warnx("pfctl_table: unknown command '%s'", command);
357
	goto _cleanup;
358
359
_error:
360
	rv = -1;
361
_cleanup:
362
30
	pfr_buf_clear(&b);
363
30
	pfr_buf_clear(&b2);
364
30
	return (rv);
365
30
}
366
367
void
368
print_table(struct pfr_table *ta, int verbose, int debug)
369
{
370
	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
371
		return;
372
	if (verbose) {
373
		printf("%c%c%c%c%c%c%c\t%s",
374
		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
375
		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
376
		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
377
		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
378
		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
379
		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
380
		    (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
381
		    ta->pfrt_name);
382
		if (ta->pfrt_anchor[0])
383
			printf("\t%s", ta->pfrt_anchor);
384
		puts("");
385
	} else
386
		puts(ta->pfrt_name);
387
}
388
389
void
390
print_tstats(struct pfr_tstats *ts, int debug)
391
{
392
	time_t	time = ts->pfrts_tzero;
393
	int	dir, op;
394
395
	if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
396
		return;
397
	print_table(&ts->pfrts_t, 1, debug);
398
	printf("\tAddresses:   %d\n", ts->pfrts_cnt);
399
	printf("\tCleared:     %s", ctime(&time));
400
	printf("\tReferences:  [ Anchors: %-18d Rules: %-18d ]\n",
401
	    ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
402
	    ts->pfrts_refcnt[PFR_REFCNT_RULE]);
403
	printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
404
	    (unsigned long long)ts->pfrts_nomatch,
405
	    (unsigned long long)ts->pfrts_match);
406
	for (dir = 0; dir < PFR_DIR_MAX; dir++)
407
		for (op = 0; op < PFR_OP_TABLE_MAX; op++)
408
			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
409
			    stats_text[dir][op],
410
			    (unsigned long long)ts->pfrts_packets[dir][op],
411
			    (unsigned long long)ts->pfrts_bytes[dir][op]);
412
}
413
414
int
415
load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
416
    int nonetwork, int opts)
417
{
418
	int	ev = 0;
419
	while (argc--)
420
		if ((ev = append_addr(b, *argv++, nonetwork, opts)) == -1) {
421
			if (errno)
422
				warn("cannot decode %s", argv[-1]);
423
			return (-1);
424
		}
425
	if (ev == 1) { /* expected further append_addr call */
426
		warnx("failed to decode %s", argv[-1]);
427
		return (-1);
428
	}
429
	if (pfr_buf_load(b, file, nonetwork, opts)) {
430
		warn("cannot load %s", file);
431
		return (-1);
432
	}
433
	return (0);
434
}
435
436
void
437
print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
438
{
439
88
	char		ch, buf[256] = "{error}";
440
44
	char		fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
441
	unsigned int	fback, hostnet;
442
443
44
	fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
444
132
	ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
445
44
	hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
446
44
	inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
447
44
	printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
448
44
	if (ad->pfra_net < hostnet)
449
22
		printf("/%d", ad->pfra_net);
450
44
	if (rad != NULL && fback != PFR_FB_NONE) {
451
		if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
452
			errx(1, "print_addrx: strlcpy");
453
		inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
454
		printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
455
		if (rad->pfra_net < hostnet)
456
			printf("/%d", rad->pfra_net);
457
	}
458
44
	if (rad != NULL && fback == PFR_FB_NONE)
459
		printf("\t nomatch");
460

44
	if (dns && ad->pfra_net == hostnet) {
461
		char host[NI_MAXHOST];
462
		struct sockaddr_storage ss;
463
464
		strlcpy(host, "?", sizeof(host));
465
		bzero(&ss, sizeof(ss));
466
		ss.ss_family = ad->pfra_af;
467
		if (ss.ss_family == AF_INET) {
468
			struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
469
470
			sin->sin_len = sizeof(*sin);
471
			sin->sin_addr = ad->pfra_ip4addr;
472
		} else {
473
			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
474
475
			sin6->sin6_len = sizeof(*sin6);
476
			sin6->sin6_addr = ad->pfra_ip6addr;
477
		}
478
		if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
479
		    sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
480
			printf("\t(%s)", host);
481
	}
482
44
	if (ad->pfra_ifname[0] != '\0')
483
		printf("@%s", ad->pfra_ifname);
484
44
	printf("\n");
485
44
}
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_states)
496
		printf("\tActive States:      %d\n", as->pfras_a.pfra_states);
497
	if (as->pfras_a.pfra_type == PFRKE_COST)
498
		printf("\tWeight:             %d\n", as->pfras_a.pfra_weight);
499
	if (as->pfras_a.pfra_ifname[0])
500
		printf("\tInterface:          %s\n", as->pfras_a.pfra_ifname);
501
	if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
502
		return;
503
	for (dir = 0; dir < PFR_DIR_MAX; dir++)
504
		for (op = 0; op < PFR_OP_ADDR_MAX; op++)
505
			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
506
			    stats_text[dir][op],
507
			    (unsigned long long)as->pfras_packets[dir][op],
508
			    (unsigned long long)as->pfras_bytes[dir][op]);
509
}
510
511
void
512
radix_perror(void)
513
{
514
	extern char *__progname;
515
	fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
516
}
517
518
int
519
pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
520
    struct pfr_buffer *ab, u_int32_t ticket)
521
{
522
60
	struct pfr_table tbl;
523
524
30
	bzero(&tbl, sizeof(tbl));
525
60
	if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
526
60
	    sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
527
30
	    sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
528
		errx(1, "pfctl_define_table: strlcpy");
529
30
	tbl.pfrt_flags = flags;
530
531
90
	return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
532
30
	    NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
533
30
}
534
535
void
536
warn_namespace_collision(const char *filter)
537
{
538
344
	struct pfr_buffer b;
539
	struct pfr_table *t;
540
	const char *name = NULL, *lastcoll;
541
	int coll = 0;
542
543
172
	bzero(&b, sizeof(b));
544
172
	b.pfrb_type = PFRB_TABLES;
545
172
	for (;;) {
546
172
		pfr_buf_grow(&b, b.pfrb_size);
547
172
		b.pfrb_size = b.pfrb_msize;
548
172
		if (pfr_get_tables(NULL, b.pfrb_caddr,
549
		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
550
			err(1, "pfr_get_tables");
551
172
		if (b.pfrb_size <= b.pfrb_msize)
552
			break;
553
	}
554
1244
	PFRB_FOREACH(t, &b) {
555
450
		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
556
			continue;
557

203
		if (filter != NULL && strcmp(filter, t->pfrt_name))
558
			continue;
559
203
		if (!t->pfrt_anchor[0])
560
			name = t->pfrt_name;
561

203
		else if (name != NULL && !strcmp(name, t->pfrt_name)) {
562
			coll++;
563
			lastcoll = name;
564
			name = NULL;
565
		}
566
	}
567
172
	if (coll == 1)
568
		warnx("warning: namespace collision with <%s> global table.",
569
		    lastcoll);
570
172
	else if (coll > 1)
571
		warnx("warning: namespace collisions with %d global tables.",
572
		    coll);
573
172
	pfr_buf_clear(&b);
574
172
}
575
576
void
577
xprintf(int opts, const char *fmt, ...)
578
{
579
	va_list args;
580
581
	if (opts & PF_OPT_QUIET)
582
		return;
583
584
	va_start(args, fmt);
585
	vfprintf(stderr, fmt, args);
586
	va_end(args);
587
588
	if (opts & PF_OPT_DUMMYACTION)
589
		fprintf(stderr, " (dummy).\n");
590
	else if (opts & PF_OPT_NOACTION)
591
		fprintf(stderr, " (syntax only).\n");
592
	else
593
		fprintf(stderr, ".\n");
594
}
595
596
597
/* interface stuff */
598
599
void
600
pfctl_show_ifaces(const char *filter, int opts)
601
{
602
	struct pfr_buffer	 b;
603
	struct pfi_kif		*p;
604
	int			 i = 0;
605
606
	bzero(&b, sizeof(b));
607
	b.pfrb_type = PFRB_IFACES;
608
	for (;;) {
609
		pfr_buf_grow(&b, b.pfrb_size);
610
		b.pfrb_size = b.pfrb_msize;
611
		if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
612
			radix_perror();
613
			exit(1);
614
		}
615
		if (b.pfrb_size <= b.pfrb_msize)
616
			break;
617
		i++;
618
	}
619
	if (opts & PF_OPT_SHOWALL)
620
		pfctl_print_title("INTERFACES:");
621
	PFRB_FOREACH(p, &b)
622
		print_iface(p, opts);
623
}
624
625
void
626
print_iface(struct pfi_kif *p, int opts)
627
{
628
	time_t	tzero = p->pfik_tzero;
629
	int	i, af, dir, act;
630
631
	printf("%s", p->pfik_name);
632
	if (opts & PF_OPT_VERBOSE) {
633
		if (p->pfik_flags & PFI_IFLAG_SKIP)
634
			printf(" (skip)");
635
	}
636
	printf("\n");
637
638
	if (!(opts & PF_OPT_VERBOSE2))
639
		return;
640
	printf("\tCleared:     %s", ctime(&tzero));
641
	printf("\tReferences:  [ States:  %-18d Rules: %-18d ]\n",
642
	    p->pfik_states, p->pfik_rules);
643
	for (i = 0; i < 8; i++) {
644
		af = (i>>2) & 1;
645
		dir = (i>>1) &1;
646
		act = i & 1;
647
		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
648
		    istats_text[af][dir][act],
649
		    (unsigned long long)p->pfik_packets[af][dir][act],
650
		    (unsigned long long)p->pfik_bytes[af][dir][act]);
651
	}
652
}