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 int pfctl_table(int, char *[], char *, const char *, char *,
59 const char *, int);
60 static void print_table(const struct pfr_table *, int, int);
61 static int print_tstats(const struct pfr_tstats *, int);
62 static int load_addr(struct pfr_buffer *, int, char *[], char *, int, 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_table(&table, &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 void
pfctl_do_clear_tables(const char * anchor,int opts)108 pfctl_do_clear_tables(const char *anchor, int opts)
109 {
110 if (pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts))
111 exit(1);
112 }
113
114 void
pfctl_show_tables(const char * anchor,int opts)115 pfctl_show_tables(const char *anchor, int opts)
116 {
117 if (pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts))
118 exit(1);
119 }
120
121 int
pfctl_command_tables(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)122 pfctl_command_tables(int argc, char *argv[], char *tname,
123 const char *command, char *file, const char *anchor, int opts)
124 {
125 if (tname == NULL || command == NULL)
126 usage();
127 return pfctl_table(argc, argv, tname, command, file, anchor, opts);
128 }
129
130 int
pfctl_table(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)131 pfctl_table(int argc, char *argv[], char *tname, const char *command,
132 char *file, const char *anchor, int opts)
133 {
134 struct pfr_table table;
135 struct pfr_buffer b, b2;
136 struct pfr_addr *a, *a2;
137 int nadd = 0, ndel = 0, nchange = 0, nzero = 0;
138 int rv = 0, flags = 0, nmatch = 0;
139 void *p;
140
141 if (command == NULL)
142 usage();
143 if (opts & PF_OPT_NOACTION)
144 flags |= PFR_FLAG_DUMMY;
145
146 bzero(&b, sizeof(b));
147 bzero(&b2, sizeof(b2));
148 bzero(&table, sizeof(table));
149 if (tname != NULL) {
150 if (strlen(tname) >= PF_TABLE_NAME_SIZE)
151 usage();
152 if (strlcpy(table.pfrt_name, tname,
153 sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
154 errx(1, "pfctl_table: strlcpy");
155 }
156 if (strlcpy(table.pfrt_anchor, anchor,
157 sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
158 errx(1, "pfctl_table: strlcpy");
159
160 if (!strcmp(command, "-F")) {
161 if (argc || file != NULL)
162 usage();
163 RVTEST(pfctl_clear_tables(pfh, &table, &ndel, flags));
164 xprintf(opts, "%d tables deleted", ndel);
165 } else if (!strcmp(command, "-s")) {
166 b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
167 PFRB_TSTATS : PFRB_TABLES;
168 if (argc || file != NULL)
169 usage();
170
171 if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
172 pfctl_print_title("TABLES:");
173
174 if (opts & PF_OPT_VERBOSE2) {
175 uintptr_t arg = opts & PF_OPT_DEBUG;
176 pfctl_get_tstats(pfh, &table,
177 (pfctl_get_tstats_fn)print_tstats, (void *)arg);
178 } else {
179 for (;;) {
180 pfr_buf_grow(&b, b.pfrb_size);
181 b.pfrb_size = b.pfrb_msize;
182 RVTEST(pfr_get_tables(&table,
183 b.pfrb_caddr, &b.pfrb_size, flags));
184 if (b.pfrb_size <= b.pfrb_msize)
185 break;
186 }
187
188 if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
189 pfctl_print_title("TABLES:");
190
191 PFRB_FOREACH(p, &b)
192 print_table(p, opts & PF_OPT_VERBOSE,
193 opts & PF_OPT_DEBUG);
194 }
195 } else if (!strcmp(command, "kill")) {
196 if (argc || file != NULL)
197 usage();
198 RVTEST(pfr_del_table(&table, &ndel, flags));
199 xprintf(opts, "%d table deleted", ndel);
200 } else if (!strcmp(command, "flush")) {
201 if (argc || file != NULL)
202 usage();
203 RVTEST(pfr_clr_addrs(&table, &ndel, flags));
204 xprintf(opts, "%d addresses deleted", ndel);
205 } else if (!strcmp(command, "add")) {
206 b.pfrb_type = PFRB_ADDRS;
207 if (load_addr(&b, argc, argv, file, 0, opts))
208 goto _error;
209 CREATE_TABLE;
210 if (opts & PF_OPT_VERBOSE)
211 flags |= PFR_FLAG_FEEDBACK;
212 RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
213 &nadd, flags));
214 xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
215 if (opts & PF_OPT_VERBOSE)
216 PFRB_FOREACH(a, &b)
217 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
218 print_addrx(a, NULL,
219 opts & PF_OPT_USEDNS);
220 } else if (!strcmp(command, "delete")) {
221 b.pfrb_type = PFRB_ADDRS;
222 if (load_addr(&b, argc, argv, file, 0, opts))
223 goto _error;
224 if (opts & PF_OPT_VERBOSE)
225 flags |= PFR_FLAG_FEEDBACK;
226 RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
227 &ndel, flags));
228 xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
229 if (opts & PF_OPT_VERBOSE)
230 PFRB_FOREACH(a, &b)
231 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
232 print_addrx(a, NULL,
233 opts & PF_OPT_USEDNS);
234 } else if (!strcmp(command, "replace")) {
235 b.pfrb_type = PFRB_ADDRS;
236 if (load_addr(&b, argc, argv, file, 0, opts))
237 goto _error;
238 CREATE_TABLE;
239 if (opts & PF_OPT_VERBOSE)
240 flags |= PFR_FLAG_FEEDBACK;
241 for (;;) {
242 int sz2 = b.pfrb_msize;
243
244 RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
245 &sz2, &nadd, &ndel, &nchange, flags));
246 if (sz2 <= b.pfrb_msize) {
247 b.pfrb_size = sz2;
248 break;
249 } else
250 pfr_buf_grow(&b, sz2);
251 }
252 if (nadd)
253 xprintf(opts, "%d addresses added", nadd);
254 if (ndel)
255 xprintf(opts, "%d addresses deleted", ndel);
256 if (nchange)
257 xprintf(opts, "%d addresses changed", nchange);
258 if (!nadd && !ndel && !nchange)
259 xprintf(opts, "no changes");
260 if (opts & PF_OPT_VERBOSE)
261 PFRB_FOREACH(a, &b)
262 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
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 = 0;
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) || a->pfra_fback)
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%s",
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 ta->pfrt_name);
430 if (ta->pfrt_anchor[0])
431 printf("\t%s", ta->pfrt_anchor);
432 puts("");
433 } else
434 puts(ta->pfrt_name);
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
443 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
444 return (0);
445 print_table(&ts->pfrts_t, 1, debug);
446 printf("\tAddresses: %d\n", ts->pfrts_cnt);
447 printf("\tCleared: %s", ctime(&time));
448 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
449 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
450 ts->pfrts_refcnt[PFR_REFCNT_RULE]);
451 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
452 (unsigned long long)ts->pfrts_nomatch,
453 (unsigned long long)ts->pfrts_match);
454 for (dir = 0; dir < PFR_DIR_MAX; dir++)
455 for (op = 0; op < PFR_OP_TABLE_MAX; op++)
456 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
457 stats_text[dir][op],
458 (unsigned long long)ts->pfrts_packets[dir][op],
459 (unsigned long long)ts->pfrts_bytes[dir][op]);
460
461 return (0);
462 }
463
464 int
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork,int opts)465 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
466 int nonetwork, int opts)
467 {
468 while (argc--)
469 if (append_addr(b, *argv++, nonetwork, opts)) {
470 if (errno)
471 warn("cannot decode %s", argv[-1]);
472 return (-1);
473 }
474 if (pfr_buf_load(b, file, nonetwork, append_addr, opts)) {
475 warn("cannot load %s", file);
476 return (-1);
477 }
478 return (0);
479 }
480
481 void
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)482 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
483 {
484 char ch, buf[256] = "{error}";
485 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
486 unsigned int fback, hostnet;
487
488 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
489 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
490 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
491 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
492 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
493 if (ad->pfra_net < hostnet)
494 printf("/%d", ad->pfra_net);
495 if (rad != NULL && fback != PFR_FB_NONE) {
496 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
497 errx(1, "print_addrx: strlcpy");
498 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
499 printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
500 if (rad->pfra_net < hostnet)
501 printf("/%d", rad->pfra_net);
502 }
503 if (rad != NULL && fback == PFR_FB_NONE)
504 printf("\t nomatch");
505 if (dns && ad->pfra_net == hostnet) {
506 char host[NI_MAXHOST];
507 struct sockaddr_storage ss;
508
509 strlcpy(host, "?", sizeof(host));
510 bzero(&ss, sizeof(ss));
511 ss.ss_family = ad->pfra_af;
512 if (ss.ss_family == AF_INET) {
513 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
514
515 sin->sin_len = sizeof(*sin);
516 sin->sin_addr = ad->pfra_ip4addr;
517 } else {
518 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
519
520 sin6->sin6_len = sizeof(*sin6);
521 sin6->sin6_addr = ad->pfra_ip6addr;
522 }
523 if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host,
524 sizeof(host), NULL, 0, NI_NAMEREQD) == 0)
525 printf("\t(%s)", host);
526 }
527 printf("\n");
528 }
529
530 int
nonzero_astats(struct pfr_astats * as)531 nonzero_astats(struct pfr_astats *as)
532 {
533 uint64_t s = 0;
534
535 for (int dir = 0; dir < PFR_DIR_MAX; dir++)
536 for (int op = 0; op < PFR_OP_ADDR_MAX; op++)
537 s |= as->pfras_packets[dir][op] |
538 as->pfras_bytes[dir][op];
539
540 return (!!s);
541 }
542
543 void
print_astats(struct pfr_astats * as,int dns)544 print_astats(struct pfr_astats *as, int dns)
545 {
546 time_t time = as->pfras_tzero;
547 int dir, op;
548
549 print_addrx(&as->pfras_a, NULL, dns);
550 printf("\tCleared: %s", ctime(&time));
551 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
552 return;
553 for (dir = 0; dir < PFR_DIR_MAX; dir++)
554 for (op = 0; op < PFR_OP_ADDR_MAX; op++)
555 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
556 stats_text[dir][op],
557 (unsigned long long)as->pfras_packets[dir][op],
558 (unsigned long long)as->pfras_bytes[dir][op]);
559 }
560
561 void
radix_perror(void)562 radix_perror(void)
563 {
564 extern char *__progname;
565 fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
566 }
567
568 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,struct pfr_buffer * ab,u_int32_t ticket)569 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
570 struct pfr_buffer *ab, u_int32_t ticket)
571 {
572 struct pfr_table tbl;
573
574 bzero(&tbl, sizeof(tbl));
575 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
576 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
577 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
578 errx(1, "pfctl_define_table: strlcpy");
579 tbl.pfrt_flags = flags;
580
581 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
582 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
583 }
584
585 void
warn_namespace_collision(const char * filter)586 warn_namespace_collision(const char *filter)
587 {
588 struct pfr_buffer b;
589 struct pfr_table *t;
590 const char *name = NULL, *lastcoll;
591 int coll = 0;
592
593 bzero(&b, sizeof(b));
594 b.pfrb_type = PFRB_TABLES;
595 for (;;) {
596 pfr_buf_grow(&b, b.pfrb_size);
597 b.pfrb_size = b.pfrb_msize;
598 if (pfr_get_tables(NULL, b.pfrb_caddr,
599 &b.pfrb_size, PFR_FLAG_ALLRSETS))
600 err(1, "pfr_get_tables");
601 if (b.pfrb_size <= b.pfrb_msize)
602 break;
603 }
604 PFRB_FOREACH(t, &b) {
605 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
606 continue;
607 if (filter != NULL && strcmp(filter, t->pfrt_name))
608 continue;
609 if (!t->pfrt_anchor[0])
610 name = t->pfrt_name;
611 else if (name != NULL && !strcmp(name, t->pfrt_name)) {
612 coll++;
613 lastcoll = name;
614 name = NULL;
615 }
616 }
617 if (coll == 1)
618 warnx("warning: namespace collision with <%s> global table.",
619 lastcoll);
620 else if (coll > 1)
621 warnx("warning: namespace collisions with %d global tables.",
622 coll);
623 pfr_buf_clear(&b);
624 }
625
626 void
xprintf(int opts,const char * fmt,...)627 xprintf(int opts, const char *fmt, ...)
628 {
629 va_list args;
630
631 if (opts & PF_OPT_QUIET)
632 return;
633
634 va_start(args, fmt);
635 vfprintf(stderr, fmt, args);
636 va_end(args);
637
638 if (opts & PF_OPT_DUMMYACTION)
639 fprintf(stderr, " (dummy).\n");
640 else if (opts & PF_OPT_NOACTION)
641 fprintf(stderr, " (syntax only).\n");
642 else
643 fprintf(stderr, ".\n");
644 }
645
646
647 /* interface stuff */
648
649 void
pfctl_show_ifaces(const char * filter,int opts)650 pfctl_show_ifaces(const char *filter, int opts)
651 {
652 struct pfr_buffer b;
653 struct pfi_kif *p;
654
655 bzero(&b, sizeof(b));
656 b.pfrb_type = PFRB_IFACES;
657 for (;;) {
658 pfr_buf_grow(&b, b.pfrb_size);
659 b.pfrb_size = b.pfrb_msize;
660 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
661 radix_perror();
662 exit(1);
663 }
664 if (b.pfrb_size <= b.pfrb_msize)
665 break;
666 }
667 if (opts & PF_OPT_SHOWALL)
668 pfctl_print_title("INTERFACES:");
669 PFRB_FOREACH(p, &b)
670 print_iface(p, opts);
671 }
672
673 void
print_iface(struct pfi_kif * p,int opts)674 print_iface(struct pfi_kif *p, int opts)
675 {
676 time_t tzero = p->pfik_tzero;
677 int i, af, dir, act;
678
679 printf("%s", p->pfik_name);
680 if (opts & PF_OPT_VERBOSE) {
681 if (p->pfik_flags & PFI_IFLAG_SKIP)
682 printf(" (skip)");
683 }
684 printf("\n");
685
686 if (!(opts & PF_OPT_VERBOSE2))
687 return;
688 printf("\tCleared: %s", ctime(&tzero));
689 printf("\tReferences: %-18d\n", p->pfik_rulerefs);
690 for (i = 0; i < 8; i++) {
691 af = (i>>2) & 1;
692 dir = (i>>1) &1;
693 act = i & 1;
694 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
695 istats_text[af][dir][act],
696 (unsigned long long)p->pfik_packets[af][dir][act],
697 (unsigned long long)p->pfik_bytes[af][dir][act]);
698 }
699 }
700