pfctl_table.c revision 130617
155714Skris/* $OpenBSD: pfctl_table.c,v 1.59 2004/03/15 15:25:44 dhartmei Exp $ */ 255714Skris 355714Skris/* 455714Skris * Copyright (c) 2002 Cedric Berger 555714Skris * All rights reserved. 655714Skris * 755714Skris * Redistribution and use in source and binary forms, with or without 855714Skris * modification, are permitted provided that the following conditions 955714Skris * are met: 1055714Skris * 1155714Skris * - Redistributions of source code must retain the above copyright 1255714Skris * notice, this list of conditions and the following disclaimer. 1355714Skris * - Redistributions in binary form must reproduce the above 1455714Skris * copyright notice, this list of conditions and the following 1555714Skris * disclaimer in the documentation and/or other materials provided 1655714Skris * with the distribution. 1755714Skris * 1855714Skris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1955714Skris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2055714Skris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2155714Skris * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 2255714Skris * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2355714Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2455714Skris * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2555714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2655714Skris * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2755714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 2855714Skris * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2955714Skris * POSSIBILITY OF SUCH DAMAGE. 3055714Skris * 3155714Skris */ 3255714Skris 3355714Skris#include <sys/cdefs.h> 3455714Skris__FBSDID("$FreeBSD: head/contrib/pf/pfctl/pfctl_table.c 130617 2004-06-16 23:39:33Z mlaier $"); 3555714Skris 3655714Skris#include <sys/types.h> 3755714Skris#include <sys/ioctl.h> 3855714Skris#include <sys/socket.h> 3955714Skris 4055714Skris#include <net/if.h> 4155714Skris#include <net/pfvar.h> 4255714Skris#include <arpa/inet.h> 4355714Skris 4455714Skris#include <ctype.h> 4555714Skris#include <err.h> 4655714Skris#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 57extern void usage(void); 58static int pfctl_table(int, char *[], char *, const char *, char *, 59 const char *, const char *, int); 60static void print_table(struct pfr_table *, int, int); 61static void print_tstats(struct pfr_tstats *, int); 62static int load_addr(struct pfr_buffer *, int, char *[], char *, int); 63static void print_addrx(struct pfr_addr *, struct pfr_addr *, int); 64static void print_astats(struct pfr_astats *, int); 65static void radix_perror(void); 66static void xprintf(int, const char *, ...); 67static void print_iface(struct pfi_if *, int); 68static void oprintf(int, int, const char *, int *, int); 69 70static 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 75static 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 RVTEST(pfr_add_tables(&table, 1, &nadd, flags)); \ 92 if (nadd) { \ 93 warn_namespace_collision(table.pfrt_name); \ 94 xprintf(opts, "%d table created", nadd); \ 95 if (opts & PF_OPT_NOACTION) \ 96 return (0); \ 97 } \ 98 table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \ 99 } while(0) 100 101int 102pfctl_clear_tables(const char *anchor, const char *ruleset, int opts) 103{ 104 return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, ruleset, opts); 105} 106 107int 108pfctl_show_tables(const char *anchor, const char *ruleset, int opts) 109{ 110 return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, ruleset, opts); 111} 112 113int 114pfctl_command_tables(int argc, char *argv[], char *tname, 115 const char *command, char *file, const char *anchor, const char *ruleset, 116 int opts) 117{ 118 if (tname == NULL || command == NULL) 119 usage(); 120 return pfctl_table(argc, argv, tname, command, file, anchor, ruleset, 121 opts); 122} 123 124int 125pfctl_table(int argc, char *argv[], char *tname, const char *command, 126 char *file, const char *anchor, const char *ruleset, 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 strlcpy(table.pfrt_ruleset, ruleset, 153 sizeof(table.pfrt_ruleset)) >= sizeof(table.pfrt_ruleset)) 154 errx(1, "pfctl_table: strlcpy"); 155 156 if (!strcmp(command, "-F")) { 157 if (argc || file != NULL) 158 usage(); 159 RVTEST(pfr_clr_tables(&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 for (;;) { 167 pfr_buf_grow(&b, b.pfrb_size); 168 b.pfrb_size = b.pfrb_msize; 169 if (opts & PF_OPT_VERBOSE2) 170 RVTEST(pfr_get_tstats(&table, 171 b.pfrb_caddr, &b.pfrb_size, flags)); 172 else 173 RVTEST(pfr_get_tables(&table, 174 b.pfrb_caddr, &b.pfrb_size, flags)); 175 if (b.pfrb_size <= b.pfrb_msize) 176 break; 177 } 178 179 if (opts & PF_OPT_SHOWALL && b.pfrb_size > 0) 180 pfctl_print_title("TABLES:"); 181 182 PFRB_FOREACH(p, &b) 183 if (opts & PF_OPT_VERBOSE2) 184 print_tstats(p, opts & PF_OPT_DEBUG); 185 else 186 print_table(p, opts & PF_OPT_VERBOSE, 187 opts & PF_OPT_DEBUG); 188 } else if (!strcmp(command, "kill")) { 189 if (argc || file != NULL) 190 usage(); 191 RVTEST(pfr_del_tables(&table, 1, &ndel, flags)); 192 xprintf(opts, "%d table deleted", ndel); 193 } else if (!strcmp(command, "flush")) { 194 if (argc || file != NULL) 195 usage(); 196 RVTEST(pfr_clr_addrs(&table, &ndel, flags)); 197 xprintf(opts, "%d addresses deleted", ndel); 198 } else if (!strcmp(command, "add")) { 199 b.pfrb_type = PFRB_ADDRS; 200 if (load_addr(&b, argc, argv, file, 0)) 201 goto _error; 202 CREATE_TABLE; 203 if (opts & PF_OPT_VERBOSE) 204 flags |= PFR_FLAG_FEEDBACK; 205 RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size, 206 &nadd, flags)); 207 xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size); 208 if (opts & PF_OPT_VERBOSE) 209 PFRB_FOREACH(a, &b) 210 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 211 print_addrx(a, NULL, 212 opts & PF_OPT_USEDNS); 213 } else if (!strcmp(command, "delete")) { 214 b.pfrb_type = PFRB_ADDRS; 215 if (load_addr(&b, argc, argv, file, 0)) 216 goto _error; 217 if (opts & PF_OPT_VERBOSE) 218 flags |= PFR_FLAG_FEEDBACK; 219 RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size, 220 &ndel, flags)); 221 xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size); 222 if (opts & PF_OPT_VERBOSE) 223 PFRB_FOREACH(a, &b) 224 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 225 print_addrx(a, NULL, 226 opts & PF_OPT_USEDNS); 227 } else if (!strcmp(command, "replace")) { 228 b.pfrb_type = PFRB_ADDRS; 229 if (load_addr(&b, argc, argv, file, 0)) 230 goto _error; 231 CREATE_TABLE; 232 if (opts & PF_OPT_VERBOSE) 233 flags |= PFR_FLAG_FEEDBACK; 234 for (;;) { 235 int sz2 = b.pfrb_msize; 236 237 RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size, 238 &sz2, &nadd, &ndel, &nchange, flags)); 239 if (sz2 <= b.pfrb_msize) { 240 b.pfrb_size = sz2; 241 break; 242 } else 243 pfr_buf_grow(&b, sz2); 244 } 245 if (nadd) 246 xprintf(opts, "%d addresses added", nadd); 247 if (ndel) 248 xprintf(opts, "%d addresses deleted", ndel); 249 if (nchange) 250 xprintf(opts, "%d addresses changed", nchange); 251 if (!nadd && !ndel && !nchange) 252 xprintf(opts, "no changes"); 253 if (opts & PF_OPT_VERBOSE) 254 PFRB_FOREACH(a, &b) 255 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 256 print_addrx(a, NULL, 257 opts & PF_OPT_USEDNS); 258 } else if (!strcmp(command, "show")) { 259 b.pfrb_type = (opts & PF_OPT_VERBOSE) ? 260 PFRB_ASTATS : PFRB_ADDRS; 261 if (argc || file != NULL) 262 usage(); 263 for (;;) { 264 pfr_buf_grow(&b, b.pfrb_size); 265 b.pfrb_size = b.pfrb_msize; 266 if (opts & PF_OPT_VERBOSE) 267 RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 268 &b.pfrb_size, flags)); 269 else 270 RVTEST(pfr_get_addrs(&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 if (opts & PF_OPT_VERBOSE) 277 print_astats(p, opts & PF_OPT_USEDNS); 278 else 279 print_addrx(p, NULL, opts & PF_OPT_USEDNS); 280 } else if (!strcmp(command, "test")) { 281 b.pfrb_type = PFRB_ADDRS; 282 b2.pfrb_type = PFRB_ADDRS; 283 284 if (load_addr(&b, argc, argv, file, 1)) 285 goto _error; 286 if (opts & PF_OPT_VERBOSE2) { 287 flags |= PFR_FLAG_REPLACE; 288 PFRB_FOREACH(a, &b) 289 if (pfr_buf_add(&b2, a)) 290 err(1, "duplicate buffer"); 291 } 292 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size, 293 &nmatch, flags)); 294 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size); 295 if (opts & PF_OPT_VERBOSE && !(opts & PF_OPT_VERBOSE2)) 296 PFRB_FOREACH(a, &b) 297 if (a->pfra_fback == PFR_FB_MATCH) 298 print_addrx(a, NULL, 299 opts & PF_OPT_USEDNS); 300 if (opts & PF_OPT_VERBOSE2) { 301 a2 = NULL; 302 PFRB_FOREACH(a, &b) { 303 a2 = pfr_buf_next(&b2, a2); 304 print_addrx(a2, a, opts & PF_OPT_USEDNS); 305 } 306 } 307 if (nmatch < b.pfrb_size) 308 rv = 2; 309 } else if (!strcmp(command, "zero")) { 310 if (argc || file != NULL) 311 usage(); 312 flags |= PFR_FLAG_ADDRSTOO; 313 RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags)); 314 xprintf(opts, "%d table/stats cleared", nzero); 315 } else 316 warnx("pfctl_table: unknown command '%s'", command); 317 goto _cleanup; 318 319_error: 320 rv = -1; 321_cleanup: 322 pfr_buf_clear(&b); 323 pfr_buf_clear(&b2); 324 return (rv); 325} 326 327void 328print_table(struct pfr_table *ta, int verbose, int debug) 329{ 330 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE)) 331 return; 332 if (verbose) { 333 printf("%c%c%c%c%c%c\t%s", 334 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-', 335 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-', 336 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-', 337 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-', 338 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-', 339 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-', 340 ta->pfrt_name); 341 if (ta->pfrt_anchor[0]) 342 printf("\t%s", ta->pfrt_anchor); 343 if (ta->pfrt_ruleset[0]) 344 printf(":%s", ta->pfrt_ruleset); 345 puts(""); 346 } else 347 puts(ta->pfrt_name); 348} 349 350void 351print_tstats(struct pfr_tstats *ts, int debug) 352{ 353 time_t time = ts->pfrts_tzero; 354 int dir, op; 355 356 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE)) 357 return; 358 print_table(&ts->pfrts_t, 1, debug); 359 printf("\tAddresses: %d\n", ts->pfrts_cnt); 360 printf("\tCleared: %s", ctime(&time)); 361 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n", 362 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR], 363 ts->pfrts_refcnt[PFR_REFCNT_RULE]); 364 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n", 365 (unsigned long long)ts->pfrts_nomatch, 366 (unsigned long long)ts->pfrts_match); 367 for (dir = 0; dir < PFR_DIR_MAX; dir++) 368 for (op = 0; op < PFR_OP_TABLE_MAX; op++) 369 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 370 stats_text[dir][op], 371 (unsigned long long)ts->pfrts_packets[dir][op], 372 (unsigned long long)ts->pfrts_bytes[dir][op]); 373} 374 375int 376load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file, 377 int nonetwork) 378{ 379 while (argc--) 380 if (append_addr(b, *argv++, nonetwork)) { 381 if (errno) 382 warn("cannot decode %s", argv[-1]); 383 return (-1); 384 } 385 if (pfr_buf_load(b, file, nonetwork, append_addr)) { 386 warn("cannot load %s", file); 387 return (-1); 388 } 389 return (0); 390} 391 392void 393print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns) 394{ 395 char ch, buf[256] = "{error}"; 396 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y' }; 397 unsigned int fback, hostnet; 398 399 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback; 400 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?'; 401 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32; 402 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf)); 403 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf); 404 if (ad->pfra_net < hostnet) 405 printf("/%d", ad->pfra_net); 406 if (rad != NULL && fback != PFR_FB_NONE) { 407 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf)) 408 errx(1, "print_addrx: strlcpy"); 409 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf)); 410 printf("\t%c%s", (rad->pfra_not?'!':' '), buf); 411 if (rad->pfra_net < hostnet) 412 printf("/%d", rad->pfra_net); 413 } 414 if (rad != NULL && fback == PFR_FB_NONE) 415 printf("\t nomatch"); 416 if (dns && ad->pfra_net == hostnet) { 417 char host[NI_MAXHOST]; 418 union sockaddr_union sa; 419 420 strlcpy(host, "?", sizeof(host)); 421 bzero(&sa, sizeof(sa)); 422 sa.sa.sa_family = ad->pfra_af; 423 if (sa.sa.sa_family == AF_INET) { 424 sa.sa.sa_len = sizeof(sa.sin); 425 sa.sin.sin_addr = ad->pfra_ip4addr; 426 } else { 427 sa.sa.sa_len = sizeof(sa.sin6); 428 sa.sin6.sin6_addr = ad->pfra_ip6addr; 429 } 430 if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host), 431 NULL, 0, NI_NAMEREQD) == 0) 432 printf("\t(%s)", host); 433 } 434 printf("\n"); 435} 436 437void 438print_astats(struct pfr_astats *as, int dns) 439{ 440 time_t time = as->pfras_tzero; 441 int dir, op; 442 443 print_addrx(&as->pfras_a, NULL, dns); 444 printf("\tCleared: %s", ctime(&time)); 445 for (dir = 0; dir < PFR_DIR_MAX; dir++) 446 for (op = 0; op < PFR_OP_ADDR_MAX; op++) 447 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 448 stats_text[dir][op], 449 (unsigned long long)as->pfras_packets[dir][op], 450 (unsigned long long)as->pfras_bytes[dir][op]); 451} 452 453void 454radix_perror(void) 455{ 456 extern char *__progname; 457 fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno)); 458} 459 460int 461pfctl_define_table(char *name, int flags, int addrs, const char *anchor, 462 const char *ruleset, struct pfr_buffer *ab, u_int32_t ticket) 463{ 464 struct pfr_table tbl; 465 466 bzero(&tbl, sizeof(tbl)); 467 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >= 468 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor, 469 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor) || 470 strlcpy(tbl.pfrt_ruleset, ruleset, sizeof(tbl.pfrt_ruleset)) >= 471 sizeof(tbl.pfrt_ruleset)) 472 errx(1, "pfctl_define_table: strlcpy"); 473 tbl.pfrt_flags = flags; 474 475 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL, 476 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0); 477} 478 479void 480warn_namespace_collision(const char *filter) 481{ 482 struct pfr_buffer b; 483 struct pfr_table *t; 484 const char *name = NULL, *lastcoll; 485 int coll = 0; 486 487 bzero(&b, sizeof(b)); 488 b.pfrb_type = PFRB_TABLES; 489 for (;;) { 490 pfr_buf_grow(&b, b.pfrb_size); 491 b.pfrb_size = b.pfrb_msize; 492 if (pfr_get_tables(NULL, b.pfrb_caddr, 493 &b.pfrb_size, PFR_FLAG_ALLRSETS)) 494 err(1, "pfr_get_tables"); 495 if (b.pfrb_size <= b.pfrb_msize) 496 break; 497 } 498 PFRB_FOREACH(t, &b) { 499 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE)) 500 continue; 501 if (filter != NULL && strcmp(filter, t->pfrt_name)) 502 continue; 503 if (!t->pfrt_anchor[0]) 504 name = t->pfrt_name; 505 else if (name != NULL && !strcmp(name, t->pfrt_name)) { 506 coll++; 507 lastcoll = name; 508 name = NULL; 509 } 510 } 511 if (coll == 1) 512 warnx("warning: namespace collision with <%s> global table.", 513 lastcoll); 514 else if (coll > 1) 515 warnx("warning: namespace collisions with %d global tables.", 516 coll); 517 pfr_buf_clear(&b); 518} 519 520void 521xprintf(int opts, const char *fmt, ...) 522{ 523 va_list args; 524 525 if (opts & PF_OPT_QUIET) 526 return; 527 528 va_start(args, fmt); 529 vfprintf(stderr, fmt, args); 530 va_end(args); 531 532 if (opts & PF_OPT_DUMMYACTION) 533 fprintf(stderr, " (dummy).\n"); 534 else if (opts & PF_OPT_NOACTION) 535 fprintf(stderr, " (syntax only).\n"); 536 else 537 fprintf(stderr, ".\n"); 538} 539 540 541/* interface stuff */ 542 543int 544pfctl_show_ifaces(const char *filter, int opts) 545{ 546 struct pfr_buffer b; 547 struct pfi_if *p; 548 int i = 0, f = PFI_FLAG_GROUP|PFI_FLAG_INSTANCE; 549 550 if (filter != NULL && *filter && !isdigit(filter[strlen(filter)-1])) 551 f &= ~PFI_FLAG_INSTANCE; 552 bzero(&b, sizeof(b)); 553 b.pfrb_type = PFRB_IFACES; 554 for (;;) { 555 pfr_buf_grow(&b, b.pfrb_size); 556 b.pfrb_size = b.pfrb_msize; 557 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size, f)) { 558 radix_perror(); 559 return (1); 560 } 561 if (b.pfrb_size <= b.pfrb_msize) 562 break; 563 i++; 564 } 565 if (opts & PF_OPT_SHOWALL) 566 pfctl_print_title("INTERFACES:"); 567 PFRB_FOREACH(p, &b) 568 print_iface(p, opts); 569 return (0); 570} 571 572void 573print_iface(struct pfi_if *p, int opts) 574{ 575 time_t tzero = p->pfif_tzero; 576 int flags = (opts & PF_OPT_VERBOSE) ? p->pfif_flags : 0; 577 int first = 1; 578 int i, af, dir, act; 579 580 printf("%s", p->pfif_name); 581 oprintf(flags, PFI_IFLAG_INSTANCE, "instance", &first, 0); 582 oprintf(flags, PFI_IFLAG_GROUP, "group", &first, 0); 583 oprintf(flags, PFI_IFLAG_CLONABLE, "clonable", &first, 0); 584 oprintf(flags, PFI_IFLAG_DYNAMIC, "dynamic", &first, 0); 585 oprintf(flags, PFI_IFLAG_ATTACHED, "attached", &first, 1); 586#ifdef __FreeBSD__ 587 first = 1; 588 oprintf(flags, PFI_IFLAG_PLACEHOLDER, "placeholder", &first, 1); 589#endif 590 printf("\n"); 591 592 if (!(opts & PF_OPT_VERBOSE2)) 593 return; 594 printf("\tCleared: %s", ctime(&tzero)); 595 printf("\tReferences: [ States: %-18d Rules: %-18d ]\n", 596 p->pfif_states, p->pfif_rules); 597 for (i = 0; i < 8; i++) { 598 af = (i>>2) & 1; 599 dir = (i>>1) &1; 600 act = i & 1; 601 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 602 istats_text[af][dir][act], 603 (unsigned long long)p->pfif_packets[af][dir][act], 604 (unsigned long long)p->pfif_bytes[af][dir][act]); 605 } 606} 607 608void 609oprintf(int flags, int flag, const char *s, int *first, int last) 610{ 611 if (flags & flag) { 612 printf(*first ? "\t(%s" : ", %s", s); 613 *first = 0; 614 } 615 if (last && !*first) 616 printf(")"); 617} 618 619