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/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41 #include <net/pfvar.h>
42 #include <arpa/inet.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 int pfctl_table(int, char *[], char *, const char *, char *,
59 const char *, int);
60 static void print_table(struct pfr_table *, int, int);
61 static void print_tstats(struct pfr_tstats *, int);
62 static int load_addr(struct pfr_buffer *, int, char *[], char *, int);
63 static void print_addrx(struct pfr_addr *, struct pfr_addr *, int);
64 static int nonzero_astats(struct pfr_astats *);
65 static void print_astats(struct pfr_astats *, int);
66 static void radix_perror(void);
67 static void xprintf(int, const char *, ...);
68 static void print_iface(struct pfi_kif *, int);
69
70 static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = {
71 { "In/Block:", "In/Pass:", "In/XPass:" },
72 { "Out/Block:", "Out/Pass:", "Out/XPass:" }
73 };
74
75 static const char *istats_text[2][2][2] = {
76 { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } },
77 { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } }
78 };
79
80 #define RVTEST(fct) do { \
81 if ((!(opts & PF_OPT_NOACTION) || \
82 (opts & PF_OPT_DUMMYACTION)) && \
83 (fct)) { \
84 radix_perror(); \
85 goto _error; \
86 } \
87 } while (0)
88
89 #define CREATE_TABLE do { \
90 table.pfrt_flags |= PFR_TFLAG_PERSIST; \
91 if ((!(opts & PF_OPT_NOACTION) || \
92 (opts & PF_OPT_DUMMYACTION)) && \
93 (pfr_add_tables(&table, 1, &nadd, flags)) && \
94 (errno != EPERM)) { \
95 radix_perror(); \
96 goto _error; \
97 } \
98 if (nadd) { \
99 warn_namespace_collision(table.pfrt_name); \
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_clear_tables(const char * anchor,int opts)108 pfctl_clear_tables(const char *anchor, int opts)
109 {
110 return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts);
111 }
112
113 int
pfctl_show_tables(const char * anchor,int opts)114 pfctl_show_tables(const char *anchor, int opts)
115 {
116 return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts);
117 }
118
119 int
pfctl_command_tables(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)120 pfctl_command_tables(int argc, char *argv[], char *tname,
121 const char *command, char *file, const char *anchor, int opts)
122 {
123 if (tname == NULL || command == NULL)
124 usage();
125 return pfctl_table(argc, argv, tname, command, file, anchor, opts);
126 }
127
128 int
pfctl_table(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)129 pfctl_table(int argc, char *argv[], char *tname, const char *command,
130 char *file, const char *anchor, int opts)
131 {
132 struct pfr_table table;
133 struct pfr_buffer b, b2;
134 struct pfr_addr *a, *a2;
135 int nadd = 0, ndel = 0, nchange = 0, nzero = 0;
136 int rv = 0, flags = 0, nmatch = 0;
137 void *p;
138
139 if (command == NULL)
140 usage();
141 if (opts & PF_OPT_NOACTION)
142 flags |= PFR_FLAG_DUMMY;
143
144 bzero(&b, sizeof(b));
145 bzero(&b2, sizeof(b2));
146 bzero(&table, sizeof(table));
147 if (tname != NULL) {
148 if (strlen(tname) >= PF_TABLE_NAME_SIZE)
149 usage();
150 if (strlcpy(table.pfrt_name, tname,
151 sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
152 errx(1, "pfctl_table: strlcpy");
153 }
154 if (strlcpy(table.pfrt_anchor, anchor,
155 sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
156 errx(1, "pfctl_table: strlcpy");
157
158 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 } 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 } 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 } 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 } else if (!strcmp(command, "add")) {
201 b.pfrb_type = PFRB_ADDRS;
202 if (load_addr(&b, argc, argv, file, 0))
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 } else if (!strcmp(command, "delete")) {
216 b.pfrb_type = PFRB_ADDRS;
217 if (load_addr(&b, argc, argv, file, 0))
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 } else if (!strcmp(command, "replace")) {
230 b.pfrb_type = PFRB_ADDRS;
231 if (load_addr(&b, argc, argv, file, 0))
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 } 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 } else if (!strcmp(command, "reset")) {
299 struct pfr_astats *as;
300
301 b.pfrb_type = PFRB_ASTATS;
302 b2.pfrb_type = PFRB_ADDRS;
303 if (argc || file != NULL)
304 usage();
305 do {
306 pfr_buf_grow(&b, b.pfrb_size);
307 b.pfrb_size = b.pfrb_msize;
308 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
309 &b.pfrb_size, flags));
310 } while (b.pfrb_size > b.pfrb_msize);
311 PFRB_FOREACH(as, &b) {
312 as->pfras_a.pfra_fback = 0;
313 if (nonzero_astats(as))
314 if (pfr_buf_add(&b2, &as->pfras_a))
315 err(1, "duplicate buffer");
316 }
317
318 if (opts & PF_OPT_VERBOSE)
319 flags |= PFR_FLAG_FEEDBACK;
320 RVTEST(pfr_clr_astats(&table, b2.pfrb_caddr, b2.pfrb_size,
321 &nzero, flags));
322 xprintf(opts, "%d/%d stats cleared", nzero, b.pfrb_size);
323 if (opts & PF_OPT_VERBOSE)
324 PFRB_FOREACH(a, &b2)
325 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
326 print_addrx(a, NULL,
327 opts & PF_OPT_USEDNS);
328 } else if (!strcmp(command, "show")) {
329 b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
330 PFRB_ASTATS : PFRB_ADDRS;
331 if (argc || file != NULL)
332 usage();
333 for (;;) {
334 pfr_buf_grow(&b, b.pfrb_size);
335 b.pfrb_size = b.pfrb_msize;
336 if (opts & PF_OPT_VERBOSE)
337 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
338 &b.pfrb_size, flags));
339 else
340 RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
341 &b.pfrb_size, flags));
342 if (b.pfrb_size <= b.pfrb_msize)
343 break;
344 }
345 PFRB_FOREACH(p, &b)
346 if (opts & PF_OPT_VERBOSE)
347 print_astats(p, opts & PF_OPT_USEDNS);
348 else
349 print_addrx(p, NULL, opts & PF_OPT_USEDNS);
350 } else if (!strcmp(command, "test")) {
351 b.pfrb_type = PFRB_ADDRS;
352 b2.pfrb_type = PFRB_ADDRS;
353
354 if (load_addr(&b, argc, argv, file, 1))
355 goto _error;
356 if (opts & PF_OPT_VERBOSE2) {
357 flags |= PFR_FLAG_REPLACE;
358 PFRB_FOREACH(a, &b)
359 if (pfr_buf_add(&b2, a))
360 err(1, "duplicate buffer");
361 }
362 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
363 &nmatch, flags));
364 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
365 if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
366 PFRB_FOREACH(a, &b)
367 if (a->pfra_fback == PFR_FB_MATCH)
368 print_addrx(a, NULL,
369 opts & PF_OPT_USEDNS);
370 if (opts & PF_OPT_VERBOSE2) {
371 a2 = NULL;
372 PFRB_FOREACH(a, &b) {
373 a2 = pfr_buf_next(&b2, a2);
374 print_addrx(a2, a, opts & PF_OPT_USEDNS);
375 }
376 }
377 if (nmatch < b.pfrb_size)
378 rv = 2;
379 } else if (!strcmp(command, "zero") && (argc || file != NULL)) {
380 b.pfrb_type = PFRB_ADDRS;
381 if (load_addr(&b, argc, argv, file, 0))
382 goto _error;
383 if (opts & PF_OPT_VERBOSE)
384 flags |= PFR_FLAG_FEEDBACK;
385 RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size,
386 &nzero, flags));
387 xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size);
388 if (opts & PF_OPT_VERBOSE)
389 PFRB_FOREACH(a, &b)
390 if (opts & PF_OPT_VERBOSE2 ||
391 a->pfra_fback != PFR_FB_NONE)
392 print_addrx(a, NULL,
393 opts & PF_OPT_USEDNS);
394 } else if (!strcmp(command, "zero")) {
395 flags |= PFR_FLAG_ADDRSTOO;
396 RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
397 xprintf(opts, "%d table/stats cleared", nzero);
398 } else
399 warnx("pfctl_table: unknown command '%s'", command);
400 goto _cleanup;
401
402 _error:
403 rv = -1;
404 _cleanup:
405 pfr_buf_clear(&b);
406 pfr_buf_clear(&b2);
407 return (rv);
408 }
409
410 void
print_table(struct pfr_table * ta,int verbose,int debug)411 print_table(struct pfr_table *ta, int verbose, int debug)
412 {
413 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
414 return;
415 if (verbose) {
416 printf("%c%c%c%c%c%c%c\t%s",
417 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
418 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
419 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
420 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
421 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
422 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
423 (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
424 ta->pfrt_name);
425 if (ta->pfrt_anchor[0])
426 printf("\t%s", ta->pfrt_anchor);
427 puts("");
428 } else
429 puts(ta->pfrt_name);
430 }
431
432 void
print_tstats(struct pfr_tstats * ts,int debug)433 print_tstats(struct pfr_tstats *ts, int debug)
434 {
435 time_t time = ts->pfrts_tzero;
436 int dir, op;
437
438 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
439 return;
440 print_table(&ts->pfrts_t, 1, debug);
441 printf("\tAddresses: %d\n", ts->pfrts_cnt);
442 printf("\tCleared: %s", ctime(&time));
443 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
444 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
445 ts->pfrts_refcnt[PFR_REFCNT_RULE]);
446 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
447 (unsigned long long)ts->pfrts_nomatch,
448 (unsigned long long)ts->pfrts_match);
449 for (dir = 0; dir < PFR_DIR_MAX; dir++)
450 for (op = 0; op < PFR_OP_TABLE_MAX; op++)
451 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
452 stats_text[dir][op],
453 (unsigned long long)ts->pfrts_packets[dir][op],
454 (unsigned long long)ts->pfrts_bytes[dir][op]);
455 }
456
457 int
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork)458 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
459 int nonetwork)
460 {
461 while (argc--)
462 if (append_addr(b, *argv++, nonetwork)) {
463 if (errno)
464 warn("cannot decode %s", argv[-1]);
465 return (-1);
466 }
467 if (pfr_buf_load(b, file, nonetwork, append_addr)) {
468 warn("cannot load %s", file);
469 return (-1);
470 }
471 return (0);
472 }
473
474 void
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)475 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
476 {
477 char ch, buf[256] = "{error}";
478 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
479 unsigned int fback, hostnet;
480
481 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
482 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
483 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
484 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
485 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
486 if (ad->pfra_net < hostnet)
487 printf("/%d", ad->pfra_net);
488 if (rad != NULL && fback != PFR_FB_NONE) {
489 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
490 errx(1, "print_addrx: strlcpy");
491 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
492 printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
493 if (rad->pfra_net < hostnet)
494 printf("/%d", rad->pfra_net);
495 }
496 if (rad != NULL && fback == PFR_FB_NONE)
497 printf("\t nomatch");
498 if (dns && ad->pfra_net == hostnet) {
499 char host[NI_MAXHOST];
500 union sockaddr_union sa;
501
502 strlcpy(host, "?", sizeof(host));
503 bzero(&sa, sizeof(sa));
504 sa.sa.sa_family = ad->pfra_af;
505 if (sa.sa.sa_family == AF_INET) {
506 sa.sa.sa_len = sizeof(sa.sin);
507 sa.sin.sin_addr = ad->pfra_ip4addr;
508 } else {
509 sa.sa.sa_len = sizeof(sa.sin6);
510 sa.sin6.sin6_addr = ad->pfra_ip6addr;
511 }
512 if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host),
513 NULL, 0, NI_NAMEREQD) == 0)
514 printf("\t(%s)", host);
515 }
516 printf("\n");
517 }
518
519 int
nonzero_astats(struct pfr_astats * as)520 nonzero_astats(struct pfr_astats *as)
521 {
522 uint64_t s = 0;
523
524 for (int dir = 0; dir < PFR_DIR_MAX; dir++)
525 for (int op = 0; op < PFR_OP_ADDR_MAX; op++)
526 s |= as->pfras_packets[dir][op] |
527 as->pfras_bytes[dir][op];
528
529 return (!!s);
530 }
531
532 void
print_astats(struct pfr_astats * as,int dns)533 print_astats(struct pfr_astats *as, int dns)
534 {
535 time_t time = as->pfras_tzero;
536 int dir, op;
537
538 print_addrx(&as->pfras_a, NULL, dns);
539 printf("\tCleared: %s", ctime(&time));
540 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
541 return;
542 for (dir = 0; dir < PFR_DIR_MAX; dir++)
543 for (op = 0; op < PFR_OP_ADDR_MAX; op++)
544 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
545 stats_text[dir][op],
546 (unsigned long long)as->pfras_packets[dir][op],
547 (unsigned long long)as->pfras_bytes[dir][op]);
548 }
549
550 void
radix_perror(void)551 radix_perror(void)
552 {
553 extern char *__progname;
554 fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
555 }
556
557 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,struct pfr_buffer * ab,u_int32_t ticket)558 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
559 struct pfr_buffer *ab, u_int32_t ticket)
560 {
561 struct pfr_table tbl;
562
563 bzero(&tbl, sizeof(tbl));
564 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
565 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
566 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
567 errx(1, "pfctl_define_table: strlcpy");
568 tbl.pfrt_flags = flags;
569
570 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
571 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
572 }
573
574 void
warn_namespace_collision(const char * filter)575 warn_namespace_collision(const char *filter)
576 {
577 struct pfr_buffer b;
578 struct pfr_table *t;
579 const char *name = NULL, *lastcoll;
580 int coll = 0;
581
582 bzero(&b, sizeof(b));
583 b.pfrb_type = PFRB_TABLES;
584 for (;;) {
585 pfr_buf_grow(&b, b.pfrb_size);
586 b.pfrb_size = b.pfrb_msize;
587 if (pfr_get_tables(NULL, b.pfrb_caddr,
588 &b.pfrb_size, PFR_FLAG_ALLRSETS))
589 err(1, "pfr_get_tables");
590 if (b.pfrb_size <= b.pfrb_msize)
591 break;
592 }
593 PFRB_FOREACH(t, &b) {
594 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
595 continue;
596 if (filter != NULL && strcmp(filter, t->pfrt_name))
597 continue;
598 if (!t->pfrt_anchor[0])
599 name = t->pfrt_name;
600 else if (name != NULL && !strcmp(name, t->pfrt_name)) {
601 coll++;
602 lastcoll = name;
603 name = NULL;
604 }
605 }
606 if (coll == 1)
607 warnx("warning: namespace collision with <%s> global table.",
608 lastcoll);
609 else if (coll > 1)
610 warnx("warning: namespace collisions with %d global tables.",
611 coll);
612 pfr_buf_clear(&b);
613 }
614
615 void
xprintf(int opts,const char * fmt,...)616 xprintf(int opts, const char *fmt, ...)
617 {
618 va_list args;
619
620 if (opts & PF_OPT_QUIET)
621 return;
622
623 va_start(args, fmt);
624 vfprintf(stderr, fmt, args);
625 va_end(args);
626
627 if (opts & PF_OPT_DUMMYACTION)
628 fprintf(stderr, " (dummy).\n");
629 else if (opts & PF_OPT_NOACTION)
630 fprintf(stderr, " (syntax only).\n");
631 else
632 fprintf(stderr, ".\n");
633 }
634
635
636 /* interface stuff */
637
638 int
pfctl_show_ifaces(const char * filter,int opts)639 pfctl_show_ifaces(const char *filter, int opts)
640 {
641 struct pfr_buffer b;
642 struct pfi_kif *p;
643
644 bzero(&b, sizeof(b));
645 b.pfrb_type = PFRB_IFACES;
646 for (;;) {
647 pfr_buf_grow(&b, b.pfrb_size);
648 b.pfrb_size = b.pfrb_msize;
649 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
650 radix_perror();
651 return (1);
652 }
653 if (b.pfrb_size <= b.pfrb_msize)
654 break;
655 }
656 if (opts & PF_OPT_SHOWALL)
657 pfctl_print_title("INTERFACES:");
658 PFRB_FOREACH(p, &b)
659 print_iface(p, opts);
660 return (0);
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