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 <net/if.h>
40 #include <net/pfvar.h>
41 #include <arpa/inet.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <netdb.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <time.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 int nonzero_astats(struct pfr_astats *);
64 static void print_astats(struct pfr_astats *, int);
65 static void radix_perror(void);
66 static void xprintf(int, const char *, ...);
67 static void print_iface(struct pfi_kif *, int);
68
69 static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = {
70 { "In/Block:", "In/Pass:", "In/XPass:" },
71 { "Out/Block:", "Out/Pass:", "Out/XPass:" }
72 };
73
74 static const char *istats_text[2][2][2] = {
75 { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } },
76 { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } }
77 };
78
79 #define RVTEST(fct) do { \
80 if ((!(opts & PF_OPT_NOACTION) || \
81 (opts & PF_OPT_DUMMYACTION)) && \
82 (fct)) { \
83 radix_perror(); \
84 goto _error; \
85 } \
86 } while (0)
87
88 #define CREATE_TABLE do { \
89 table.pfrt_flags |= PFR_TFLAG_PERSIST; \
90 if ((!(opts & PF_OPT_NOACTION) || \
91 (opts & PF_OPT_DUMMYACTION)) && \
92 (pfr_add_tables(&table, 1, &nadd, flags)) && \
93 (errno != EPERM)) { \
94 radix_perror(); \
95 goto _error; \
96 } \
97 if (nadd) { \
98 warn_namespace_collision(table.pfrt_name); \
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 return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts);
110 }
111
112 int
pfctl_show_tables(const char * anchor,int opts)113 pfctl_show_tables(const char *anchor, int opts)
114 {
115 return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts);
116 }
117
118 int
pfctl_command_tables(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)119 pfctl_command_tables(int argc, char *argv[], char *tname,
120 const char *command, char *file, const char *anchor, int opts)
121 {
122 if (tname == NULL || command == NULL)
123 usage();
124 return pfctl_table(argc, argv, tname, command, file, anchor, opts);
125 }
126
127 int
pfctl_table(int argc,char * argv[],char * tname,const char * command,char * file,const char * anchor,int opts)128 pfctl_table(int argc, char *argv[], char *tname, const char *command,
129 char *file, const char *anchor, int opts)
130 {
131 struct pfr_table table;
132 struct pfr_buffer b, b2;
133 struct pfr_addr *a, *a2;
134 int nadd = 0, ndel = 0, nchange = 0, nzero = 0;
135 int rv = 0, flags = 0, nmatch = 0;
136 void *p;
137
138 if (command == NULL)
139 usage();
140 if (opts & PF_OPT_NOACTION)
141 flags |= PFR_FLAG_DUMMY;
142
143 bzero(&b, sizeof(b));
144 bzero(&b2, sizeof(b2));
145 bzero(&table, sizeof(table));
146 if (tname != NULL) {
147 if (strlen(tname) >= PF_TABLE_NAME_SIZE)
148 usage();
149 if (strlcpy(table.pfrt_name, tname,
150 sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
151 errx(1, "pfctl_table: strlcpy");
152 }
153 if (strlcpy(table.pfrt_anchor, anchor,
154 sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
155 errx(1, "pfctl_table: strlcpy");
156
157 if (!strcmp(command, "-F")) {
158 if (argc || file != NULL)
159 usage();
160 RVTEST(pfctl_clear_tables(pfh, &table, &ndel, flags));
161 xprintf(opts, "%d tables deleted", ndel);
162 } else if (!strcmp(command, "-s")) {
163 b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
164 PFRB_TSTATS : PFRB_TABLES;
165 if (argc || file != NULL)
166 usage();
167 for (;;) {
168 pfr_buf_grow(&b, b.pfrb_size);
169 b.pfrb_size = b.pfrb_msize;
170 if (opts & PF_OPT_VERBOSE2)
171 RVTEST(pfr_get_tstats(&table,
172 b.pfrb_caddr, &b.pfrb_size, flags));
173 else
174 RVTEST(pfr_get_tables(&table,
175 b.pfrb_caddr, &b.pfrb_size, flags));
176 if (b.pfrb_size <= b.pfrb_msize)
177 break;
178 }
179
180 if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
181 pfctl_print_title("TABLES:");
182
183 PFRB_FOREACH(p, &b)
184 if (opts & PF_OPT_VERBOSE2)
185 print_tstats(p, opts & PF_OPT_DEBUG);
186 else
187 print_table(p, opts & PF_OPT_VERBOSE,
188 opts & PF_OPT_DEBUG);
189 } else if (!strcmp(command, "kill")) {
190 if (argc || file != NULL)
191 usage();
192 RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
193 xprintf(opts, "%d table deleted", ndel);
194 } else if (!strcmp(command, "flush")) {
195 if (argc || file != NULL)
196 usage();
197 RVTEST(pfr_clr_addrs(&table, &ndel, flags));
198 xprintf(opts, "%d addresses deleted", ndel);
199 } else if (!strcmp(command, "add")) {
200 b.pfrb_type = PFRB_ADDRS;
201 if (load_addr(&b, argc, argv, file, 0))
202 goto _error;
203 CREATE_TABLE;
204 if (opts & PF_OPT_VERBOSE)
205 flags |= PFR_FLAG_FEEDBACK;
206 RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
207 &nadd, flags));
208 xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
209 if (opts & PF_OPT_VERBOSE)
210 PFRB_FOREACH(a, &b)
211 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
212 print_addrx(a, NULL,
213 opts & PF_OPT_USEDNS);
214 } else if (!strcmp(command, "delete")) {
215 b.pfrb_type = PFRB_ADDRS;
216 if (load_addr(&b, argc, argv, file, 0))
217 goto _error;
218 if (opts & PF_OPT_VERBOSE)
219 flags |= PFR_FLAG_FEEDBACK;
220 RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
221 &ndel, flags));
222 xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
223 if (opts & PF_OPT_VERBOSE)
224 PFRB_FOREACH(a, &b)
225 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
226 print_addrx(a, NULL,
227 opts & PF_OPT_USEDNS);
228 } else if (!strcmp(command, "replace")) {
229 b.pfrb_type = PFRB_ADDRS;
230 if (load_addr(&b, argc, argv, file, 0))
231 goto _error;
232 CREATE_TABLE;
233 if (opts & PF_OPT_VERBOSE)
234 flags |= PFR_FLAG_FEEDBACK;
235 for (;;) {
236 int sz2 = b.pfrb_msize;
237
238 RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
239 &sz2, &nadd, &ndel, &nchange, flags));
240 if (sz2 <= b.pfrb_msize) {
241 b.pfrb_size = sz2;
242 break;
243 } else
244 pfr_buf_grow(&b, sz2);
245 }
246 if (nadd)
247 xprintf(opts, "%d addresses added", nadd);
248 if (ndel)
249 xprintf(opts, "%d addresses deleted", ndel);
250 if (nchange)
251 xprintf(opts, "%d addresses changed", nchange);
252 if (!nadd && !ndel && !nchange)
253 xprintf(opts, "no changes");
254 if (opts & PF_OPT_VERBOSE)
255 PFRB_FOREACH(a, &b)
256 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
257 print_addrx(a, NULL,
258 opts & PF_OPT_USEDNS);
259 } else if (!strcmp(command, "expire")) {
260 const char *errstr;
261 u_int lifetime;
262
263 b.pfrb_type = PFRB_ASTATS;
264 b2.pfrb_type = PFRB_ADDRS;
265 if (argc != 1 || file != NULL)
266 usage();
267 lifetime = strtonum(*argv, 0, UINT_MAX, &errstr);
268 if (errstr)
269 errx(1, "expiry time: %s", errstr);
270 for (;;) {
271 pfr_buf_grow(&b, b.pfrb_size);
272 b.pfrb_size = b.pfrb_msize;
273 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
274 &b.pfrb_size, flags));
275 if (b.pfrb_size <= b.pfrb_msize)
276 break;
277 }
278 PFRB_FOREACH(p, &b) {
279 ((struct pfr_astats *)p)->pfras_a.pfra_fback = 0;
280 if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero >
281 lifetime)
282 if (pfr_buf_add(&b2,
283 &((struct pfr_astats *)p)->pfras_a))
284 err(1, "duplicate buffer");
285 }
286
287 if (opts & PF_OPT_VERBOSE)
288 flags |= PFR_FLAG_FEEDBACK;
289 RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size,
290 &ndel, flags));
291 xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size);
292 if (opts & PF_OPT_VERBOSE)
293 PFRB_FOREACH(a, &b2)
294 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
295 print_addrx(a, NULL,
296 opts & PF_OPT_USEDNS);
297 } else if (!strcmp(command, "reset")) {
298 struct pfr_astats *as;
299
300 b.pfrb_type = PFRB_ASTATS;
301 b2.pfrb_type = PFRB_ADDRS;
302 if (argc || file != NULL)
303 usage();
304 do {
305 pfr_buf_grow(&b, b.pfrb_size);
306 b.pfrb_size = b.pfrb_msize;
307 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
308 &b.pfrb_size, flags));
309 } while (b.pfrb_size > b.pfrb_msize);
310 PFRB_FOREACH(as, &b) {
311 as->pfras_a.pfra_fback = 0;
312 if (nonzero_astats(as))
313 if (pfr_buf_add(&b2, &as->pfras_a))
314 err(1, "duplicate buffer");
315 }
316
317 if (opts & PF_OPT_VERBOSE)
318 flags |= PFR_FLAG_FEEDBACK;
319 RVTEST(pfr_clr_astats(&table, b2.pfrb_caddr, b2.pfrb_size,
320 &nzero, flags));
321 xprintf(opts, "%d/%d stats cleared", nzero, b.pfrb_size);
322 if (opts & PF_OPT_VERBOSE)
323 PFRB_FOREACH(a, &b2)
324 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
325 print_addrx(a, NULL,
326 opts & PF_OPT_USEDNS);
327 } else if (!strcmp(command, "show")) {
328 b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
329 PFRB_ASTATS : PFRB_ADDRS;
330 if (argc || file != NULL)
331 usage();
332 for (;;) {
333 pfr_buf_grow(&b, b.pfrb_size);
334 b.pfrb_size = b.pfrb_msize;
335 if (opts & PF_OPT_VERBOSE)
336 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
337 &b.pfrb_size, flags));
338 else
339 RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
340 &b.pfrb_size, flags));
341 if (b.pfrb_size <= b.pfrb_msize)
342 break;
343 }
344 PFRB_FOREACH(p, &b)
345 if (opts & PF_OPT_VERBOSE)
346 print_astats(p, opts & PF_OPT_USEDNS);
347 else
348 print_addrx(p, NULL, opts & PF_OPT_USEDNS);
349 } else if (!strcmp(command, "test")) {
350 b.pfrb_type = PFRB_ADDRS;
351 b2.pfrb_type = PFRB_ADDRS;
352
353 if (load_addr(&b, argc, argv, file, 1))
354 goto _error;
355 if (opts & PF_OPT_VERBOSE2) {
356 flags |= PFR_FLAG_REPLACE;
357 PFRB_FOREACH(a, &b)
358 if (pfr_buf_add(&b2, a))
359 err(1, "duplicate buffer");
360 }
361 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
362 &nmatch, flags));
363 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
364 if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
365 PFRB_FOREACH(a, &b)
366 if (a->pfra_fback == PFR_FB_MATCH)
367 print_addrx(a, NULL,
368 opts & PF_OPT_USEDNS);
369 if (opts & PF_OPT_VERBOSE2) {
370 a2 = NULL;
371 PFRB_FOREACH(a, &b) {
372 a2 = pfr_buf_next(&b2, a2);
373 print_addrx(a2, a, opts & PF_OPT_USEDNS);
374 }
375 }
376 if (nmatch < b.pfrb_size)
377 rv = 2;
378 } else if (!strcmp(command, "zero") && (argc || file != NULL)) {
379 b.pfrb_type = PFRB_ADDRS;
380 if (load_addr(&b, argc, argv, file, 0))
381 goto _error;
382 if (opts & PF_OPT_VERBOSE)
383 flags |= PFR_FLAG_FEEDBACK;
384 RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size,
385 &nzero, flags));
386 xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size);
387 if (opts & PF_OPT_VERBOSE)
388 PFRB_FOREACH(a, &b)
389 if (opts & PF_OPT_VERBOSE2 ||
390 a->pfra_fback != PFR_FB_NONE)
391 print_addrx(a, NULL,
392 opts & PF_OPT_USEDNS);
393 } else if (!strcmp(command, "zero")) {
394 flags |= PFR_FLAG_ADDRSTOO;
395 RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
396 xprintf(opts, "%d table/stats cleared", nzero);
397 } else
398 warnx("pfctl_table: unknown command '%s'", command);
399 goto _cleanup;
400
401 _error:
402 rv = -1;
403 _cleanup:
404 pfr_buf_clear(&b);
405 pfr_buf_clear(&b2);
406 return (rv);
407 }
408
409 void
print_table(struct pfr_table * ta,int verbose,int debug)410 print_table(struct pfr_table *ta, int verbose, int debug)
411 {
412 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
413 return;
414 if (verbose) {
415 printf("%c%c%c%c%c%c%c\t%s",
416 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
417 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
418 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
419 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
420 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
421 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
422 (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
423 ta->pfrt_name);
424 if (ta->pfrt_anchor[0])
425 printf("\t%s", ta->pfrt_anchor);
426 puts("");
427 } else
428 puts(ta->pfrt_name);
429 }
430
431 void
print_tstats(struct pfr_tstats * ts,int debug)432 print_tstats(struct pfr_tstats *ts, int debug)
433 {
434 time_t time = ts->pfrts_tzero;
435 int dir, op;
436
437 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
438 return;
439 print_table(&ts->pfrts_t, 1, debug);
440 printf("\tAddresses: %d\n", ts->pfrts_cnt);
441 printf("\tCleared: %s", ctime(&time));
442 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
443 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
444 ts->pfrts_refcnt[PFR_REFCNT_RULE]);
445 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
446 (unsigned long long)ts->pfrts_nomatch,
447 (unsigned long long)ts->pfrts_match);
448 for (dir = 0; dir < PFR_DIR_MAX; dir++)
449 for (op = 0; op < PFR_OP_TABLE_MAX; op++)
450 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
451 stats_text[dir][op],
452 (unsigned long long)ts->pfrts_packets[dir][op],
453 (unsigned long long)ts->pfrts_bytes[dir][op]);
454 }
455
456 int
load_addr(struct pfr_buffer * b,int argc,char * argv[],char * file,int nonetwork)457 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
458 int nonetwork)
459 {
460 while (argc--)
461 if (append_addr(b, *argv++, nonetwork)) {
462 if (errno)
463 warn("cannot decode %s", argv[-1]);
464 return (-1);
465 }
466 if (pfr_buf_load(b, file, nonetwork, append_addr)) {
467 warn("cannot load %s", file);
468 return (-1);
469 }
470 return (0);
471 }
472
473 void
print_addrx(struct pfr_addr * ad,struct pfr_addr * rad,int dns)474 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
475 {
476 char ch, buf[256] = "{error}";
477 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' };
478 unsigned int fback, hostnet;
479
480 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
481 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
482 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
483 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
484 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
485 if (ad->pfra_net < hostnet)
486 printf("/%d", ad->pfra_net);
487 if (rad != NULL && fback != PFR_FB_NONE) {
488 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
489 errx(1, "print_addrx: strlcpy");
490 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
491 printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
492 if (rad->pfra_net < hostnet)
493 printf("/%d", rad->pfra_net);
494 }
495 if (rad != NULL && fback == PFR_FB_NONE)
496 printf("\t nomatch");
497 if (dns && ad->pfra_net == hostnet) {
498 char host[NI_MAXHOST];
499 union sockaddr_union sa;
500
501 strlcpy(host, "?", sizeof(host));
502 bzero(&sa, sizeof(sa));
503 sa.sa.sa_family = ad->pfra_af;
504 if (sa.sa.sa_family == AF_INET) {
505 sa.sa.sa_len = sizeof(sa.sin);
506 sa.sin.sin_addr = ad->pfra_ip4addr;
507 } else {
508 sa.sa.sa_len = sizeof(sa.sin6);
509 sa.sin6.sin6_addr = ad->pfra_ip6addr;
510 }
511 if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host),
512 NULL, 0, NI_NAMEREQD) == 0)
513 printf("\t(%s)", host);
514 }
515 printf("\n");
516 }
517
518 int
nonzero_astats(struct pfr_astats * as)519 nonzero_astats(struct pfr_astats *as)
520 {
521 uint64_t s = 0;
522
523 for (int dir = 0; dir < PFR_DIR_MAX; dir++)
524 for (int op = 0; op < PFR_OP_ADDR_MAX; op++)
525 s |= as->pfras_packets[dir][op] |
526 as->pfras_bytes[dir][op];
527
528 return (!!s);
529 }
530
531 void
print_astats(struct pfr_astats * as,int dns)532 print_astats(struct pfr_astats *as, int dns)
533 {
534 time_t time = as->pfras_tzero;
535 int dir, op;
536
537 print_addrx(&as->pfras_a, NULL, dns);
538 printf("\tCleared: %s", ctime(&time));
539 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT)
540 return;
541 for (dir = 0; dir < PFR_DIR_MAX; dir++)
542 for (op = 0; op < PFR_OP_ADDR_MAX; op++)
543 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
544 stats_text[dir][op],
545 (unsigned long long)as->pfras_packets[dir][op],
546 (unsigned long long)as->pfras_bytes[dir][op]);
547 }
548
549 void
radix_perror(void)550 radix_perror(void)
551 {
552 extern char *__progname;
553 fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
554 }
555
556 int
pfctl_define_table(char * name,int flags,int addrs,const char * anchor,struct pfr_buffer * ab,u_int32_t ticket)557 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
558 struct pfr_buffer *ab, u_int32_t ticket)
559 {
560 struct pfr_table tbl;
561
562 bzero(&tbl, sizeof(tbl));
563 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
564 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
565 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
566 errx(1, "pfctl_define_table: strlcpy");
567 tbl.pfrt_flags = flags;
568
569 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
570 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
571 }
572
573 void
warn_namespace_collision(const char * filter)574 warn_namespace_collision(const char *filter)
575 {
576 struct pfr_buffer b;
577 struct pfr_table *t;
578 const char *name = NULL, *lastcoll;
579 int coll = 0;
580
581 bzero(&b, sizeof(b));
582 b.pfrb_type = PFRB_TABLES;
583 for (;;) {
584 pfr_buf_grow(&b, b.pfrb_size);
585 b.pfrb_size = b.pfrb_msize;
586 if (pfr_get_tables(NULL, b.pfrb_caddr,
587 &b.pfrb_size, PFR_FLAG_ALLRSETS))
588 err(1, "pfr_get_tables");
589 if (b.pfrb_size <= b.pfrb_msize)
590 break;
591 }
592 PFRB_FOREACH(t, &b) {
593 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
594 continue;
595 if (filter != NULL && strcmp(filter, t->pfrt_name))
596 continue;
597 if (!t->pfrt_anchor[0])
598 name = t->pfrt_name;
599 else if (name != NULL && !strcmp(name, t->pfrt_name)) {
600 coll++;
601 lastcoll = name;
602 name = NULL;
603 }
604 }
605 if (coll == 1)
606 warnx("warning: namespace collision with <%s> global table.",
607 lastcoll);
608 else if (coll > 1)
609 warnx("warning: namespace collisions with %d global tables.",
610 coll);
611 pfr_buf_clear(&b);
612 }
613
614 void
xprintf(int opts,const char * fmt,...)615 xprintf(int opts, const char *fmt, ...)
616 {
617 va_list args;
618
619 if (opts & PF_OPT_QUIET)
620 return;
621
622 va_start(args, fmt);
623 vfprintf(stderr, fmt, args);
624 va_end(args);
625
626 if (opts & PF_OPT_DUMMYACTION)
627 fprintf(stderr, " (dummy).\n");
628 else if (opts & PF_OPT_NOACTION)
629 fprintf(stderr, " (syntax only).\n");
630 else
631 fprintf(stderr, ".\n");
632 }
633
634
635 /* interface stuff */
636
637 int
pfctl_show_ifaces(const char * filter,int opts)638 pfctl_show_ifaces(const char *filter, int opts)
639 {
640 struct pfr_buffer b;
641 struct pfi_kif *p;
642
643 bzero(&b, sizeof(b));
644 b.pfrb_type = PFRB_IFACES;
645 for (;;) {
646 pfr_buf_grow(&b, b.pfrb_size);
647 b.pfrb_size = b.pfrb_msize;
648 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
649 radix_perror();
650 return (1);
651 }
652 if (b.pfrb_size <= b.pfrb_msize)
653 break;
654 }
655 if (opts & PF_OPT_SHOWALL)
656 pfctl_print_title("INTERFACES:");
657 PFRB_FOREACH(p, &b)
658 print_iface(p, opts);
659 return (0);
660 }
661
662 void
print_iface(struct pfi_kif * p,int opts)663 print_iface(struct pfi_kif *p, int opts)
664 {
665 time_t tzero = p->pfik_tzero;
666 int i, af, dir, act;
667
668 printf("%s", p->pfik_name);
669 if (opts & PF_OPT_VERBOSE) {
670 if (p->pfik_flags & PFI_IFLAG_SKIP)
671 printf(" (skip)");
672 }
673 printf("\n");
674
675 if (!(opts & PF_OPT_VERBOSE2))
676 return;
677 printf("\tCleared: %s", ctime(&tzero));
678 printf("\tReferences: %-18d\n", p->pfik_rulerefs);
679 for (i = 0; i < 8; i++) {
680 af = (i>>2) & 1;
681 dir = (i>>1) &1;
682 act = i & 1;
683 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
684 istats_text[af][dir][act],
685 (unsigned long long)p->pfik_packets[af][dir][act],
686 (unsigned long long)p->pfik_bytes[af][dir][act]);
687 }
688 }
689