1 /* $OpenBSD: pfctl_table.c,v 1.72 2013/07/05 13:07:57 blambert Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Cedric Berger 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33 #include <sys/types.h> 34 #include <sys/ioctl.h> 35 #include <sys/socket.h> 36 37 #include <net/if.h> 38 #include <net/pfvar.h> 39 #include <arpa/inet.h> 40 41 #include <ctype.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <netdb.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <time.h> 50 51 #include "pfctl_parser.h" 52 #include "pfctl.h" 53 54 extern void usage(void); 55 static int pfctl_table(int, char *[], char *, const char *, char *, 56 const char *, int); 57 static void print_table(struct pfr_table *, int, int); 58 static void print_tstats(struct pfr_tstats *, int); 59 static int load_addr(struct pfr_buffer *, int, char *[], char *, int); 60 static void print_addrx(struct pfr_addr *, struct pfr_addr *, int); 61 static void print_astats(struct pfr_astats *, int); 62 static void radix_perror(void); 63 static void xprintf(int, const char *, ...); 64 static void print_iface(struct pfi_kif *, int); 65 66 static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = { 67 { "In/Block:", "In/Match:", "In/Pass:", "In/XPass:" }, 68 { "Out/Block:", "Out/Match:", "Out/Pass:", "Out/XPass:" } 69 }; 70 71 static const char *istats_text[2][2][2] = { 72 { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } }, 73 { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } } 74 }; 75 76 #define RVTEST(fct) do { \ 77 if ((!(opts & PF_OPT_NOACTION) || \ 78 (opts & PF_OPT_DUMMYACTION)) && \ 79 (fct)) { \ 80 radix_perror(); \ 81 goto _error; \ 82 } \ 83 } while (0) 84 85 #define CREATE_TABLE do { \ 86 table.pfrt_flags |= PFR_TFLAG_PERSIST; \ 87 if ((!(opts & PF_OPT_NOACTION) || \ 88 (opts & PF_OPT_DUMMYACTION)) && \ 89 (pfr_add_tables(&table, 1, &nadd, flags)) && \ 90 (errno != EPERM)) { \ 91 radix_perror(); \ 92 goto _error; \ 93 } \ 94 if (nadd) { \ 95 warn_namespace_collision(table.pfrt_name); \ 96 xprintf(opts, "%d table created", nadd); \ 97 if (opts & PF_OPT_NOACTION) \ 98 return (0); \ 99 } \ 100 table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \ 101 } while(0) 102 103 int 104 pfctl_clear_tables(const char *anchor, int opts) 105 { 106 return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts); 107 } 108 109 int 110 pfctl_show_tables(const char *anchor, int opts) 111 { 112 return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts); 113 } 114 115 int 116 pfctl_command_tables(int argc, char *argv[], char *tname, 117 const char *command, char *file, const char *anchor, int opts) 118 { 119 if (tname == NULL || command == NULL) 120 usage(); 121 return pfctl_table(argc, argv, tname, command, file, anchor, opts); 122 } 123 124 int 125 pfctl_table(int argc, char *argv[], char *tname, const char *command, 126 char *file, const char *anchor, int opts) 127 { 128 struct pfr_table table; 129 struct pfr_buffer b, b2; 130 struct pfr_addr *a, *a2; 131 int nadd = 0, ndel = 0, nchange = 0, nzero = 0; 132 int rv = 0, flags = 0, nmatch = 0; 133 void *p; 134 135 if (command == NULL) 136 usage(); 137 if (opts & PF_OPT_NOACTION) 138 flags |= PFR_FLAG_DUMMY; 139 140 bzero(&b, sizeof(b)); 141 bzero(&b2, sizeof(b2)); 142 bzero(&table, sizeof(table)); 143 if (tname != NULL) { 144 if (strlen(tname) >= PF_TABLE_NAME_SIZE) 145 usage(); 146 if (strlcpy(table.pfrt_name, tname, 147 sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name)) 148 errx(1, "pfctl_table: strlcpy"); 149 } 150 if (strlcpy(table.pfrt_anchor, anchor, 151 sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor)) 152 errx(1, "pfctl_table: strlcpy"); 153 154 if (!strcmp(command, "-F")) { 155 if (argc || file != NULL) 156 usage(); 157 RVTEST(pfr_clr_tables(&table, &ndel, flags)); 158 xprintf(opts, "%d tables deleted", ndel); 159 } else if (!strcmp(command, "-s")) { 160 b.pfrb_type = (opts & PF_OPT_VERBOSE2) ? 161 PFRB_TSTATS : PFRB_TABLES; 162 if (argc || file != NULL) 163 usage(); 164 for (;;) { 165 pfr_buf_grow(&b, b.pfrb_size); 166 b.pfrb_size = b.pfrb_msize; 167 if (opts & PF_OPT_VERBOSE2) 168 RVTEST(pfr_get_tstats(&table, 169 b.pfrb_caddr, &b.pfrb_size, flags)); 170 else 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 if (opts & PF_OPT_VERBOSE2) 182 print_tstats(p, opts & PF_OPT_DEBUG); 183 else 184 print_table(p, opts & PF_OPT_VERBOSE, 185 opts & PF_OPT_DEBUG); 186 } else if (!strcmp(command, "kill")) { 187 if (argc || file != NULL) 188 usage(); 189 RVTEST(pfr_del_tables(&table, 1, &ndel, flags)); 190 xprintf(opts, "%d table deleted", ndel); 191 } else if (!strcmp(command, "flush")) { 192 if (argc || file != NULL) 193 usage(); 194 RVTEST(pfr_clr_addrs(&table, &ndel, flags)); 195 xprintf(opts, "%d addresses deleted", ndel); 196 } else if (!strcmp(command, "add")) { 197 b.pfrb_type = PFRB_ADDRS; 198 if (load_addr(&b, argc, argv, file, 0)) 199 goto _error; 200 CREATE_TABLE; 201 if (opts & PF_OPT_VERBOSE) 202 flags |= PFR_FLAG_FEEDBACK; 203 RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size, 204 &nadd, flags)); 205 xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size); 206 if (opts & PF_OPT_VERBOSE) 207 PFRB_FOREACH(a, &b) 208 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 209 print_addrx(a, NULL, 210 opts & PF_OPT_USEDNS); 211 } else if (!strcmp(command, "delete")) { 212 b.pfrb_type = PFRB_ADDRS; 213 if (load_addr(&b, argc, argv, file, 0)) 214 goto _error; 215 if (opts & PF_OPT_VERBOSE) 216 flags |= PFR_FLAG_FEEDBACK; 217 RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size, 218 &ndel, flags)); 219 xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size); 220 if (opts & PF_OPT_VERBOSE) 221 PFRB_FOREACH(a, &b) 222 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 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)) 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) || a->pfra_fback) 254 print_addrx(a, NULL, 255 opts & PF_OPT_USEDNS); 256 } else if (!strcmp(command, "expire")) { 257 const char *errstr; 258 u_int lifetime; 259 260 b.pfrb_type = PFRB_ASTATS; 261 b2.pfrb_type = PFRB_ADDRS; 262 if (argc != 1 || file != NULL) 263 usage(); 264 lifetime = strtonum(*argv, 0, UINT_MAX, &errstr); 265 if (errstr) 266 errx(1, "expiry time: %s", errstr); 267 for (;;) { 268 pfr_buf_grow(&b, b.pfrb_size); 269 b.pfrb_size = b.pfrb_msize; 270 RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 271 &b.pfrb_size, flags)); 272 if (b.pfrb_size <= b.pfrb_msize) 273 break; 274 } 275 PFRB_FOREACH(p, &b) { 276 ((struct pfr_astats *)p)->pfras_a.pfra_fback = 0; 277 if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero > 278 lifetime) 279 if (pfr_buf_add(&b2, 280 &((struct pfr_astats *)p)->pfras_a)) 281 err(1, "duplicate buffer"); 282 } 283 284 if (opts & PF_OPT_VERBOSE) 285 flags |= PFR_FLAG_FEEDBACK; 286 RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size, 287 &ndel, flags)); 288 xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size); 289 if (opts & PF_OPT_VERBOSE) 290 PFRB_FOREACH(a, &b2) 291 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 292 print_addrx(a, NULL, 293 opts & PF_OPT_USEDNS); 294 } else if (!strcmp(command, "show")) { 295 b.pfrb_type = (opts & PF_OPT_VERBOSE) ? 296 PFRB_ASTATS : PFRB_ADDRS; 297 if (argc || file != NULL) 298 usage(); 299 for (;;) { 300 pfr_buf_grow(&b, b.pfrb_size); 301 b.pfrb_size = b.pfrb_msize; 302 if (opts & PF_OPT_VERBOSE) 303 RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 304 &b.pfrb_size, flags)); 305 else 306 RVTEST(pfr_get_addrs(&table, b.pfrb_caddr, 307 &b.pfrb_size, flags)); 308 if (b.pfrb_size <= b.pfrb_msize) 309 break; 310 } 311 PFRB_FOREACH(p, &b) 312 if (opts & PF_OPT_VERBOSE) 313 print_astats(p, opts & PF_OPT_USEDNS); 314 else 315 print_addrx(p, NULL, opts & PF_OPT_USEDNS); 316 } else if (!strcmp(command, "test")) { 317 b.pfrb_type = PFRB_ADDRS; 318 b2.pfrb_type = PFRB_ADDRS; 319 320 if (load_addr(&b, argc, argv, file, 1)) 321 goto _error; 322 if (opts & PF_OPT_VERBOSE2) { 323 flags |= PFR_FLAG_REPLACE; 324 PFRB_FOREACH(a, &b) 325 if (pfr_buf_add(&b2, a)) 326 err(1, "duplicate buffer"); 327 } 328 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size, 329 &nmatch, flags)); 330 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size); 331 if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2)) 332 PFRB_FOREACH(a, &b) 333 if (a->pfra_fback == PFR_FB_MATCH) 334 print_addrx(a, NULL, 335 opts & PF_OPT_USEDNS); 336 if (opts & PF_OPT_VERBOSE2) { 337 a2 = NULL; 338 PFRB_FOREACH(a, &b) { 339 a2 = pfr_buf_next(&b2, a2); 340 print_addrx(a2, a, opts & PF_OPT_USEDNS); 341 } 342 } 343 if (nmatch < b.pfrb_size) 344 rv = 2; 345 } else if (!strcmp(command, "zero")) { 346 if (argc || file != NULL) 347 usage(); 348 flags |= PFR_FLAG_ADDRSTOO; 349 RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags)); 350 xprintf(opts, "%d table/stats cleared", nzero); 351 } else 352 warnx("pfctl_table: unknown command '%s'", command); 353 goto _cleanup; 354 355 _error: 356 rv = -1; 357 _cleanup: 358 pfr_buf_clear(&b); 359 pfr_buf_clear(&b2); 360 return (rv); 361 } 362 363 void 364 print_table(struct pfr_table *ta, int verbose, int debug) 365 { 366 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE)) 367 return; 368 if (verbose) { 369 printf("%c%c%c%c%c%c%c\t%s", 370 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-', 371 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-', 372 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-', 373 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-', 374 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-', 375 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-', 376 (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-', 377 ta->pfrt_name); 378 if (ta->pfrt_anchor[0]) 379 printf("\t%s", ta->pfrt_anchor); 380 puts(""); 381 } else 382 puts(ta->pfrt_name); 383 } 384 385 void 386 print_tstats(struct pfr_tstats *ts, int debug) 387 { 388 time_t time = ts->pfrts_tzero; 389 int dir, op; 390 391 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE)) 392 return; 393 print_table(&ts->pfrts_t, 1, debug); 394 printf("\tAddresses: %d\n", ts->pfrts_cnt); 395 printf("\tCleared: %s", ctime(&time)); 396 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n", 397 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR], 398 ts->pfrts_refcnt[PFR_REFCNT_RULE]); 399 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n", 400 (unsigned long long)ts->pfrts_nomatch, 401 (unsigned long long)ts->pfrts_match); 402 for (dir = 0; dir < PFR_DIR_MAX; dir++) 403 for (op = 0; op < PFR_OP_TABLE_MAX; op++) 404 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 405 stats_text[dir][op], 406 (unsigned long long)ts->pfrts_packets[dir][op], 407 (unsigned long long)ts->pfrts_bytes[dir][op]); 408 } 409 410 int 411 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file, 412 int nonetwork) 413 { 414 int ev = 0; 415 while (argc--) 416 if ((ev = append_addr(b, *argv++, nonetwork)) == -1) { 417 if (errno) 418 warn("cannot decode %s", argv[-1]); 419 return (-1); 420 } 421 if (ev == 1) { /* expected further append_addr call */ 422 warnx("failed to decode %s", argv[-1]); 423 return (-1); 424 } 425 if (pfr_buf_load(b, file, nonetwork)) { 426 warn("cannot load %s", file); 427 return (-1); 428 } 429 return (0); 430 } 431 432 void 433 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns) 434 { 435 char ch, buf[256] = "{error}"; 436 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' }; 437 unsigned int fback, hostnet; 438 439 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback; 440 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?'; 441 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32; 442 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf)); 443 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf); 444 if (ad->pfra_net < hostnet) 445 printf("/%d", ad->pfra_net); 446 if (rad != NULL && fback != PFR_FB_NONE) { 447 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf)) 448 errx(1, "print_addrx: strlcpy"); 449 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf)); 450 printf("\t%c%s", (rad->pfra_not?'!':' '), buf); 451 if (rad->pfra_net < hostnet) 452 printf("/%d", rad->pfra_net); 453 } 454 if (rad != NULL && fback == PFR_FB_NONE) 455 printf("\t nomatch"); 456 if (dns && ad->pfra_net == hostnet) { 457 char host[NI_MAXHOST]; 458 union sockaddr_union sa; 459 460 strlcpy(host, "?", sizeof(host)); 461 bzero(&sa, sizeof(sa)); 462 sa.sa.sa_family = ad->pfra_af; 463 if (sa.sa.sa_family == AF_INET) { 464 sa.sa.sa_len = sizeof(sa.sin); 465 sa.sin.sin_addr = ad->pfra_ip4addr; 466 } else { 467 sa.sa.sa_len = sizeof(sa.sin6); 468 sa.sin6.sin6_addr = ad->pfra_ip6addr; 469 } 470 if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host), 471 NULL, 0, NI_NAMEREQD) == 0) 472 printf("\t(%s)", host); 473 } 474 if (ad->pfra_ifname[0] != '\0') 475 printf("@%s", ad->pfra_ifname); 476 printf("\n"); 477 } 478 479 void 480 print_astats(struct pfr_astats *as, int dns) 481 { 482 time_t time = as->pfras_tzero; 483 int dir, op; 484 485 print_addrx(&as->pfras_a, NULL, dns); 486 printf("\tCleared: %s", ctime(&time)); 487 if (as->pfras_a.pfra_states) 488 printf("\tActive States: %d\n", as->pfras_a.pfra_states); 489 if (as->pfras_a.pfra_type == PFRKE_COST) 490 printf("\tWeight: %d\n", as->pfras_a.pfra_weight); 491 if (as->pfras_a.pfra_ifname[0]) 492 printf("\tInterface: %s\n", as->pfras_a.pfra_ifname); 493 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT) 494 return; 495 for (dir = 0; dir < PFR_DIR_MAX; dir++) 496 for (op = 0; op < PFR_OP_ADDR_MAX; op++) 497 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 498 stats_text[dir][op], 499 (unsigned long long)as->pfras_packets[dir][op], 500 (unsigned long long)as->pfras_bytes[dir][op]); 501 } 502 503 void 504 radix_perror(void) 505 { 506 extern char *__progname; 507 fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno)); 508 } 509 510 int 511 pfctl_define_table(char *name, int flags, int addrs, const char *anchor, 512 struct pfr_buffer *ab, u_int32_t ticket) 513 { 514 struct pfr_table tbl; 515 516 bzero(&tbl, sizeof(tbl)); 517 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >= 518 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor, 519 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor)) 520 errx(1, "pfctl_define_table: strlcpy"); 521 tbl.pfrt_flags = flags; 522 523 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL, 524 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0); 525 } 526 527 void 528 warn_namespace_collision(const char *filter) 529 { 530 struct pfr_buffer b; 531 struct pfr_table *t; 532 const char *name = NULL, *lastcoll; 533 int coll = 0; 534 535 bzero(&b, sizeof(b)); 536 b.pfrb_type = PFRB_TABLES; 537 for (;;) { 538 pfr_buf_grow(&b, b.pfrb_size); 539 b.pfrb_size = b.pfrb_msize; 540 if (pfr_get_tables(NULL, b.pfrb_caddr, 541 &b.pfrb_size, PFR_FLAG_ALLRSETS)) 542 err(1, "pfr_get_tables"); 543 if (b.pfrb_size <= b.pfrb_msize) 544 break; 545 } 546 PFRB_FOREACH(t, &b) { 547 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE)) 548 continue; 549 if (filter != NULL && strcmp(filter, t->pfrt_name)) 550 continue; 551 if (!t->pfrt_anchor[0]) 552 name = t->pfrt_name; 553 else if (name != NULL && !strcmp(name, t->pfrt_name)) { 554 coll++; 555 lastcoll = name; 556 name = NULL; 557 } 558 } 559 if (coll == 1) 560 warnx("warning: namespace collision with <%s> global table.", 561 lastcoll); 562 else if (coll > 1) 563 warnx("warning: namespace collisions with %d global tables.", 564 coll); 565 pfr_buf_clear(&b); 566 } 567 568 void 569 xprintf(int opts, const char *fmt, ...) 570 { 571 va_list args; 572 573 if (opts & PF_OPT_QUIET) 574 return; 575 576 va_start(args, fmt); 577 vfprintf(stderr, fmt, args); 578 va_end(args); 579 580 if (opts & PF_OPT_DUMMYACTION) 581 fprintf(stderr, " (dummy).\n"); 582 else if (opts & PF_OPT_NOACTION) 583 fprintf(stderr, " (syntax only).\n"); 584 else 585 fprintf(stderr, ".\n"); 586 } 587 588 589 /* interface stuff */ 590 591 int 592 pfctl_show_ifaces(const char *filter, int opts) 593 { 594 struct pfr_buffer b; 595 struct pfi_kif *p; 596 int i = 0; 597 598 bzero(&b, sizeof(b)); 599 b.pfrb_type = PFRB_IFACES; 600 for (;;) { 601 pfr_buf_grow(&b, b.pfrb_size); 602 b.pfrb_size = b.pfrb_msize; 603 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) { 604 radix_perror(); 605 return (1); 606 } 607 if (b.pfrb_size <= b.pfrb_msize) 608 break; 609 i++; 610 } 611 if (opts & PF_OPT_SHOWALL) 612 pfctl_print_title("INTERFACES:"); 613 PFRB_FOREACH(p, &b) 614 print_iface(p, opts); 615 return (0); 616 } 617 618 void 619 print_iface(struct pfi_kif *p, int opts) 620 { 621 time_t tzero = p->pfik_tzero; 622 int i, af, dir, act; 623 624 printf("%s", p->pfik_name); 625 if (opts & PF_OPT_VERBOSE) { 626 if (p->pfik_flags & PFI_IFLAG_SKIP) 627 printf(" (skip)"); 628 } 629 printf("\n"); 630 631 if (!(opts & PF_OPT_VERBOSE2)) 632 return; 633 printf("\tCleared: %s", ctime(&tzero)); 634 printf("\tReferences: [ States: %-18d Rules: %-18d ]\n", 635 p->pfik_states, p->pfik_rules); 636 for (i = 0; i < 8; i++) { 637 af = (i>>2) & 1; 638 dir = (i>>1) &1; 639 act = i & 1; 640 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 641 istats_text[af][dir][act], 642 (unsigned long long)p->pfik_packets[af][dir][act], 643 (unsigned long long)p->pfik_bytes[af][dir][act]); 644 } 645 } 646