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%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 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,struct pfr_buffer * ab,u_int32_t ticket)562 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
563 struct pfr_buffer *ab, u_int32_t ticket)
564 {
565 struct pfr_table tbl;
566
567 bzero(&tbl, sizeof(tbl));
568 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
569 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
570 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
571 errx(1, "pfctl_define_table: strlcpy");
572 tbl.pfrt_flags = flags;
573
574 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
575 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
576 }
577
578 void
warn_duplicate_tables(const char * tablename,const char * anchorname)579 warn_duplicate_tables(const char *tablename, const char *anchorname)
580 {
581 struct pfr_buffer b;
582 struct pfr_table *t;
583
584 bzero(&b, sizeof(b));
585 b.pfrb_type = PFRB_TABLES;
586 for (;;) {
587 pfr_buf_grow(&b, b.pfrb_size);
588 b.pfrb_size = b.pfrb_msize;
589 if (pfr_get_tables(NULL, b.pfrb_caddr,
590 &b.pfrb_size, PFR_FLAG_ALLRSETS))
591 err(1, "pfr_get_tables");
592 if (b.pfrb_size <= b.pfrb_msize)
593 break;
594 }
595 PFRB_FOREACH(t, &b) {
596 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
597 continue;
598 if (!strcmp(anchorname, t->pfrt_anchor))
599 continue;
600 if (!strcmp(tablename, t->pfrt_name))
601 warnx("warning: table <%s> already defined"
602 " in anchor \"%s\"", tablename,
603 t->pfrt_anchor[0] ? t->pfrt_anchor : "/");
604 }
605 pfr_buf_clear(&b);
606 }
607
608 void
xprintf(int opts,const char * fmt,...)609 xprintf(int opts, const char *fmt, ...)
610 {
611 va_list args;
612
613 if (opts & PF_OPT_QUIET)
614 return;
615
616 va_start(args, fmt);
617 vfprintf(stderr, fmt, args);
618 va_end(args);
619
620 if (opts & PF_OPT_DUMMYACTION)
621 fprintf(stderr, " (dummy).\n");
622 else if (opts & PF_OPT_NOACTION)
623 fprintf(stderr, " (syntax only).\n");
624 else
625 fprintf(stderr, ".\n");
626 }
627
628
629 /* interface stuff */
630
631 void
pfctl_show_ifaces(const char * filter,int opts)632 pfctl_show_ifaces(const char *filter, int opts)
633 {
634 struct pfr_buffer b;
635 struct pfi_kif *p;
636
637 bzero(&b, sizeof(b));
638 b.pfrb_type = PFRB_IFACES;
639 for (;;) {
640 pfr_buf_grow(&b, b.pfrb_size);
641 b.pfrb_size = b.pfrb_msize;
642 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size))
643 errx(1, "%s", pf_strerror(errno));
644 if (b.pfrb_size <= b.pfrb_msize)
645 break;
646 }
647 if (opts & PF_OPT_SHOWALL)
648 pfctl_print_title("INTERFACES:");
649 PFRB_FOREACH(p, &b)
650 print_iface(p, opts);
651 }
652
653 void
print_iface(struct pfi_kif * p,int opts)654 print_iface(struct pfi_kif *p, int opts)
655 {
656 time_t tzero = p->pfik_tzero;
657 int i, af, dir, act;
658
659 printf("%s", p->pfik_name);
660 if (opts & PF_OPT_VERBOSE) {
661 if (p->pfik_flags & PFI_IFLAG_SKIP)
662 printf(" (skip)");
663 }
664 printf("\n");
665
666 if (!(opts & PF_OPT_VERBOSE2))
667 return;
668 printf("\tCleared: %s", ctime(&tzero));
669 printf("\tReferences: %-18d\n", p->pfik_rulerefs);
670 for (i = 0; i < 8; i++) {
671 af = (i>>2) & 1;
672 dir = (i>>1) &1;
673 act = i & 1;
674 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
675 istats_text[af][dir][act],
676 (unsigned long long)p->pfik_packets[af][dir][act],
677 (unsigned long long)p->pfik_bytes[af][dir][act]);
678 }
679 }
680