GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/pfctl/pfctl_table.c Lines: 0 332 0.0 %
Date: 2016-12-06 Branches: 0 372 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: pfctl_table.c,v 1.74 2015/01/20 17:19:05 deraadt 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);
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
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")) {
348
		if (argc || file != NULL)
349
			usage();
350
		flags |= PFR_FLAG_ADDRSTOO;
351
		RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
352
		xprintf(opts, "%d table/stats cleared", nzero);
353
	} else
354
		warnx("pfctl_table: unknown command '%s'", command);
355
	goto _cleanup;
356
357
_error:
358
	rv = -1;
359
_cleanup:
360
	pfr_buf_clear(&b);
361
	pfr_buf_clear(&b2);
362
	return (rv);
363
}
364
365
void
366
print_table(struct pfr_table *ta, int verbose, int debug)
367
{
368
	if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
369
		return;
370
	if (verbose) {
371
		printf("%c%c%c%c%c%c%c\t%s",
372
		    (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
373
		    (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
374
		    (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
375
		    (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
376
		    (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
377
		    (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
378
		    (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
379
		    ta->pfrt_name);
380
		if (ta->pfrt_anchor[0])
381
			printf("\t%s", ta->pfrt_anchor);
382
		puts("");
383
	} else
384
		puts(ta->pfrt_name);
385
}
386
387
void
388
print_tstats(struct pfr_tstats *ts, int debug)
389
{
390
	time_t	time = ts->pfrts_tzero;
391
	int	dir, op;
392
393
	if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
394
		return;
395
	print_table(&ts->pfrts_t, 1, debug);
396
	printf("\tAddresses:   %d\n", ts->pfrts_cnt);
397
	printf("\tCleared:     %s", ctime(&time));
398
	printf("\tReferences:  [ Anchors: %-18d Rules: %-18d ]\n",
399
	    ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
400
	    ts->pfrts_refcnt[PFR_REFCNT_RULE]);
401
	printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
402
	    (unsigned long long)ts->pfrts_nomatch,
403
	    (unsigned long long)ts->pfrts_match);
404
	for (dir = 0; dir < PFR_DIR_MAX; dir++)
405
		for (op = 0; op < PFR_OP_TABLE_MAX; op++)
406
			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
407
			    stats_text[dir][op],
408
			    (unsigned long long)ts->pfrts_packets[dir][op],
409
			    (unsigned long long)ts->pfrts_bytes[dir][op]);
410
}
411
412
int
413
load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
414
    int nonetwork)
415
{
416
	int	ev = 0;
417
	while (argc--)
418
		if ((ev = append_addr(b, *argv++, nonetwork)) == -1) {
419
			if (errno)
420
				warn("cannot decode %s", argv[-1]);
421
			return (-1);
422
		}
423
	if (ev == 1) { /* expected further append_addr call */
424
		warnx("failed to decode %s", argv[-1]);
425
		return (-1);
426
	}
427
	if (pfr_buf_load(b, file, nonetwork)) {
428
		warn("cannot load %s", file);
429
		return (-1);
430
	}
431
	return (0);
432
}
433
434
void
435
print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
436
{
437
	char		ch, buf[256] = "{error}";
438
	char		fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
439
	unsigned int	fback, hostnet;
440
441
	fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
442
	ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
443
	hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
444
	inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
445
	printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
446
	if (ad->pfra_net < hostnet)
447
		printf("/%d", ad->pfra_net);
448
	if (rad != NULL && fback != PFR_FB_NONE) {
449
		if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
450
			errx(1, "print_addrx: strlcpy");
451
		inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
452
		printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
453
		if (rad->pfra_net < hostnet)
454
			printf("/%d", rad->pfra_net);
455
	}
456
	if (rad != NULL && fback == PFR_FB_NONE)
457
		printf("\t nomatch");
458
	if (dns && ad->pfra_net == hostnet) {
459
		char host[NI_MAXHOST];
460
		struct sockaddr_storage ss;
461
462
		strlcpy(host, "?", sizeof(host));
463
		bzero(&ss, sizeof(ss));
464
		ss.ss_family = ad->pfra_af;
465
		if (ss.ss_family == AF_INET) {
466
			struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
467
468
			sin->sin_len = sizeof(*sin);
469
			sin->sin_addr = ad->pfra_ip4addr;
470
		} else {
471
			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
472
473
			sin6->sin6_len = sizeof(*sin6);
474
			sin6->sin6_addr = ad->pfra_ip6addr;
475
		}
476
		if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
477
		    sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
478
			printf("\t(%s)", host);
479
	}
480
	if (ad->pfra_ifname[0] != '\0')
481
		printf("@%s", ad->pfra_ifname);
482
	printf("\n");
483
}
484
485
void
486
print_astats(struct pfr_astats *as, int dns)
487
{
488
	time_t	time = as->pfras_tzero;
489
	int	dir, op;
490
491
	print_addrx(&as->pfras_a, NULL, dns);
492
	printf("\tCleared:     %s", ctime(&time));
493
	if (as->pfras_a.pfra_states)
494
		printf("\tActive States:      %d\n", as->pfras_a.pfra_states);
495
	if (as->pfras_a.pfra_type == PFRKE_COST)
496
		printf("\tWeight:             %d\n", as->pfras_a.pfra_weight);
497
	if (as->pfras_a.pfra_ifname[0])
498
		printf("\tInterface:          %s\n", as->pfras_a.pfra_ifname);
499
 	if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
500
		return;
501
	for (dir = 0; dir < PFR_DIR_MAX; dir++)
502
		for (op = 0; op < PFR_OP_ADDR_MAX; op++)
503
			printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
504
			    stats_text[dir][op],
505
			    (unsigned long long)as->pfras_packets[dir][op],
506
			    (unsigned long long)as->pfras_bytes[dir][op]);
507
}
508
509
void
510
radix_perror(void)
511
{
512
	extern char *__progname;
513
	fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
514
}
515
516
int
517
pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
518
    struct pfr_buffer *ab, u_int32_t ticket)
519
{
520
	struct pfr_table tbl;
521
522
	bzero(&tbl, sizeof(tbl));
523
	if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
524
	    sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
525
	    sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
526
		errx(1, "pfctl_define_table: strlcpy");
527
	tbl.pfrt_flags = flags;
528
529
	return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
530
	    NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
531
}
532
533
void
534
warn_namespace_collision(const char *filter)
535
{
536
	struct pfr_buffer b;
537
	struct pfr_table *t;
538
	const char *name = NULL, *lastcoll;
539
	int coll = 0;
540
541
	bzero(&b, sizeof(b));
542
	b.pfrb_type = PFRB_TABLES;
543
	for (;;) {
544
		pfr_buf_grow(&b, b.pfrb_size);
545
		b.pfrb_size = b.pfrb_msize;
546
		if (pfr_get_tables(NULL, b.pfrb_caddr,
547
		    &b.pfrb_size, PFR_FLAG_ALLRSETS))
548
			err(1, "pfr_get_tables");
549
		if (b.pfrb_size <= b.pfrb_msize)
550
			break;
551
	}
552
	PFRB_FOREACH(t, &b) {
553
		if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
554
			continue;
555
		if (filter != NULL && strcmp(filter, t->pfrt_name))
556
			continue;
557
		if (!t->pfrt_anchor[0])
558
			name = t->pfrt_name;
559
		else if (name != NULL && !strcmp(name, t->pfrt_name)) {
560
			coll++;
561
			lastcoll = name;
562
			name = NULL;
563
		}
564
	}
565
	if (coll == 1)
566
		warnx("warning: namespace collision with <%s> global table.",
567
		    lastcoll);
568
	else if (coll > 1)
569
		warnx("warning: namespace collisions with %d global tables.",
570
		    coll);
571
	pfr_buf_clear(&b);
572
}
573
574
void
575
xprintf(int opts, const char *fmt, ...)
576
{
577
	va_list args;
578
579
	if (opts & PF_OPT_QUIET)
580
		return;
581
582
	va_start(args, fmt);
583
	vfprintf(stderr, fmt, args);
584
	va_end(args);
585
586
	if (opts & PF_OPT_DUMMYACTION)
587
		fprintf(stderr, " (dummy).\n");
588
	else if (opts & PF_OPT_NOACTION)
589
		fprintf(stderr, " (syntax only).\n");
590
	else
591
		fprintf(stderr, ".\n");
592
}
593
594
595
/* interface stuff */
596
597
int
598
pfctl_show_ifaces(const char *filter, int opts)
599
{
600
	struct pfr_buffer	 b;
601
	struct pfi_kif		*p;
602
	int			 i = 0;
603
604
	bzero(&b, sizeof(b));
605
	b.pfrb_type = PFRB_IFACES;
606
	for (;;) {
607
		pfr_buf_grow(&b, b.pfrb_size);
608
		b.pfrb_size = b.pfrb_msize;
609
		if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
610
			radix_perror();
611
			return (1);
612
		}
613
		if (b.pfrb_size <= b.pfrb_msize)
614
			break;
615
		i++;
616
	}
617
	if (opts & PF_OPT_SHOWALL)
618
		pfctl_print_title("INTERFACES:");
619
	PFRB_FOREACH(p, &b)
620
		print_iface(p, opts);
621
	return (0);
622
}
623
624
void
625
print_iface(struct pfi_kif *p, int opts)
626
{
627
	time_t	tzero = p->pfik_tzero;
628
	int	i, af, dir, act;
629
630
	printf("%s", p->pfik_name);
631
	if (opts & PF_OPT_VERBOSE) {
632
		if (p->pfik_flags & PFI_IFLAG_SKIP)
633
			printf(" (skip)");
634
	}
635
	printf("\n");
636
637
	if (!(opts & PF_OPT_VERBOSE2))
638
		return;
639
	printf("\tCleared:     %s", ctime(&tzero));
640
	printf("\tReferences:  [ States:  %-18d Rules: %-18d ]\n",
641
	    p->pfik_states, p->pfik_rules);
642
	for (i = 0; i < 8; i++) {
643
		af = (i>>2) & 1;
644
		dir = (i>>1) &1;
645
		act = i & 1;
646
		printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
647
		    istats_text[af][dir][act],
648
		    (unsigned long long)p->pfik_packets[af][dir][act],
649
		    (unsigned long long)p->pfik_bytes[af][dir][act]);
650
	}
651
}