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