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 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 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 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 RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size, 240 &nadd, &ndel, &nchange, flags)); 241 if (nadd) 242 xprintf(opts, "%d addresses added", nadd); 243 if (ndel) 244 xprintf(opts, "%d addresses deleted", ndel); 245 if (nchange) 246 xprintf(opts, "%d addresses changed", nchange); 247 if (!nadd && !ndel && !nchange) 248 xprintf(opts, "no changes"); 249 if (opts & PF_OPT_VERBOSE) 250 PFRB_FOREACH(a, &b) 251 if ((opts & PF_OPT_VERBOSE2) || 252 a->pfra_fback != PFR_FB_NONE) 253 print_addrx(a, NULL, 254 opts & PF_OPT_USEDNS); 255 } else if (!strcmp(command, "expire")) { 256 const char *errstr; 257 u_int lifetime; 258 259 b.pfrb_type = PFRB_ASTATS; 260 b2.pfrb_type = PFRB_ADDRS; 261 if (argc != 1 || file != NULL) 262 usage(); 263 lifetime = strtonum(*argv, 0, UINT_MAX, &errstr); 264 if (errstr) 265 errx(1, "expiry time: %s", errstr); 266 for (;;) { 267 pfr_buf_grow(&b, b.pfrb_size); 268 b.pfrb_size = b.pfrb_msize; 269 RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 270 &b.pfrb_size, flags)); 271 if (b.pfrb_size <= b.pfrb_msize) 272 break; 273 } 274 PFRB_FOREACH(p, &b) { 275 ((struct pfr_astats *)p)->pfras_a.pfra_fback = PFR_FB_NONE; 276 if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero > 277 lifetime) 278 if (pfr_buf_add(&b2, 279 &((struct pfr_astats *)p)->pfras_a)) 280 err(1, "duplicate buffer"); 281 } 282 283 if (opts & PF_OPT_VERBOSE) 284 flags |= PFR_FLAG_FEEDBACK; 285 RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size, 286 &ndel, flags)); 287 xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size); 288 if (opts & PF_OPT_VERBOSE) 289 PFRB_FOREACH(a, &b2) 290 if ((opts & PF_OPT_VERBOSE2) || 291 a->pfra_fback != PFR_FB_NONE) 292 print_addrx(a, NULL, 293 opts & PF_OPT_USEDNS); 294 } else if (!strcmp(command, "reset")) { 295 struct pfr_astats *as; 296 297 b.pfrb_type = PFRB_ASTATS; 298 b2.pfrb_type = PFRB_ADDRS; 299 if (argc || file != NULL) 300 usage(); 301 do { 302 pfr_buf_grow(&b, b.pfrb_size); 303 b.pfrb_size = b.pfrb_msize; 304 RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 305 &b.pfrb_size, flags)); 306 } while (b.pfrb_size > b.pfrb_msize); 307 PFRB_FOREACH(as, &b) { 308 as->pfras_a.pfra_fback = 0; 309 if (nonzero_astats(as)) 310 if (pfr_buf_add(&b2, &as->pfras_a)) 311 err(1, "duplicate buffer"); 312 } 313 314 if (opts & PF_OPT_VERBOSE) 315 flags |= PFR_FLAG_FEEDBACK; 316 RVTEST(pfr_clr_astats(&table, b2.pfrb_caddr, b2.pfrb_size, 317 &nzero, flags)); 318 xprintf(opts, "%d/%d stats cleared", nzero, b.pfrb_size); 319 if (opts & PF_OPT_VERBOSE) 320 PFRB_FOREACH(a, &b2) 321 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 322 print_addrx(a, NULL, 323 opts & PF_OPT_USEDNS); 324 } else if (!strcmp(command, "show")) { 325 b.pfrb_type = (opts & PF_OPT_VERBOSE) ? 326 PFRB_ASTATS : PFRB_ADDRS; 327 if (argc || file != NULL) 328 usage(); 329 for (;;) { 330 pfr_buf_grow(&b, b.pfrb_size); 331 b.pfrb_size = b.pfrb_msize; 332 if (opts & PF_OPT_VERBOSE) 333 RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 334 &b.pfrb_size, flags)); 335 else 336 RVTEST(pfr_get_addrs(&table, b.pfrb_caddr, 337 &b.pfrb_size, flags)); 338 if (b.pfrb_size <= b.pfrb_msize) 339 break; 340 } 341 PFRB_FOREACH(p, &b) 342 if (opts & PF_OPT_VERBOSE) 343 print_astats(p, opts & PF_OPT_USEDNS); 344 else 345 print_addrx(p, NULL, opts & PF_OPT_USEDNS); 346 } else if (!strcmp(command, "test")) { 347 b.pfrb_type = PFRB_ADDRS; 348 b2.pfrb_type = PFRB_ADDRS; 349 350 if (load_addr(&b, argc, argv, file, 1, opts)) 351 goto _error; 352 if (opts & PF_OPT_VERBOSE2) { 353 flags |= PFR_FLAG_REPLACE; 354 PFRB_FOREACH(a, &b) 355 if (pfr_buf_add(&b2, a)) 356 err(1, "duplicate buffer"); 357 } 358 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size, 359 &nmatch, flags)); 360 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size); 361 if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2)) 362 PFRB_FOREACH(a, &b) 363 if (a->pfra_fback == PFR_FB_MATCH) 364 print_addrx(a, NULL, 365 opts & PF_OPT_USEDNS); 366 if (opts & PF_OPT_VERBOSE2) { 367 a2 = NULL; 368 PFRB_FOREACH(a, &b) { 369 a2 = pfr_buf_next(&b2, a2); 370 print_addrx(a2, a, opts & PF_OPT_USEDNS); 371 } 372 } 373 if (nmatch < b.pfrb_size) 374 rv = 2; 375 } else if (!strcmp(command, "zero") && (argc || file != NULL)) { 376 b.pfrb_type = PFRB_ADDRS; 377 if (load_addr(&b, argc, argv, file, 0, opts)) 378 goto _error; 379 if (opts & PF_OPT_VERBOSE) 380 flags |= PFR_FLAG_FEEDBACK; 381 RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size, 382 &nzero, flags)); 383 xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size); 384 if (opts & PF_OPT_VERBOSE) 385 PFRB_FOREACH(a, &b) 386 if (opts & PF_OPT_VERBOSE2 || 387 a->pfra_fback != PFR_FB_NONE) 388 print_addrx(a, NULL, 389 opts & PF_OPT_USEDNS); 390 } else if (!strcmp(command, "zero")) { 391 flags |= PFR_FLAG_ADDRSTOO; 392 RVTEST(pfctl_clear_tstats(pfh, &table, &nzero, flags)); 393 xprintf(opts, "%d table/stats cleared", nzero); 394 } else 395 warnx("pfctl_table: unknown command '%s'", command); 396 goto _cleanup; 397 398 _error: 399 rv = -1; 400 _cleanup: 401 pfr_buf_clear(&b); 402 pfr_buf_clear(&b2); 403 return (rv); 404 } 405 406 void 407 print_table(const struct pfr_table *ta, int verbose, int debug) 408 { 409 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE)) 410 return; 411 if (verbose) 412 printf("%c%c%c%c%c%c%c\t", 413 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-', 414 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-', 415 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-', 416 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-', 417 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-', 418 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-', 419 (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-'); 420 421 printf("%s", ta->pfrt_name); 422 if (ta->pfrt_anchor[0] != '\0') 423 printf("@%s", ta->pfrt_anchor); 424 425 printf("\n"); 426 } 427 428 int 429 print_tstats(const struct pfr_tstats *ts, int debug) 430 { 431 time_t time = ts->pfrts_tzero; 432 int dir, op; 433 char *ct; 434 435 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE)) 436 return (0); 437 ct = ctime(&time); 438 print_table(&ts->pfrts_t, 1, debug); 439 printf("\tAddresses: %d\n", ts->pfrts_cnt); 440 if (ct) 441 printf("\tCleared: %s", ct); 442 else 443 printf("\tCleared: %lld\n", (long long)time); 444 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n", 445 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR], 446 ts->pfrts_refcnt[PFR_REFCNT_RULE]); 447 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n", 448 (unsigned long long)ts->pfrts_nomatch, 449 (unsigned long long)ts->pfrts_match); 450 for (dir = 0; dir < PFR_DIR_MAX; dir++) 451 for (op = 0; op < PFR_OP_TABLE_MAX; op++) 452 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 453 stats_text[dir][op], 454 (unsigned long long)ts->pfrts_packets[dir][op], 455 (unsigned long long)ts->pfrts_bytes[dir][op]); 456 457 return (0); 458 } 459 460 int 461 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file, 462 int nonetwork, int opts) 463 { 464 while (argc--) 465 if (append_addr(b, *argv++, nonetwork, opts)) { 466 if (errno) 467 warn("cannot decode %s", argv[-1]); 468 return (-1); 469 } 470 if (pfr_buf_load(b, file, nonetwork, append_addr, opts)) { 471 warn("cannot load %s", file); 472 return (-1); 473 } 474 return (0); 475 } 476 477 void 478 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns) 479 { 480 char ch, buf[256] = "{error}"; 481 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' }; 482 unsigned int fback, hostnet; 483 484 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback; 485 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?'; 486 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32; 487 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf)); 488 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf); 489 if (ad->pfra_net < hostnet) 490 printf("/%d", ad->pfra_net); 491 if (rad != NULL && fback != PFR_FB_NONE) { 492 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf)) 493 errx(1, "print_addrx: strlcpy"); 494 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf)); 495 printf("\t%c%s", (rad->pfra_not?'!':' '), buf); 496 if (rad->pfra_net < hostnet) 497 printf("/%d", rad->pfra_net); 498 } 499 if (rad != NULL && fback == PFR_FB_NONE) 500 printf("\t nomatch"); 501 if (dns && ad->pfra_net == hostnet) { 502 char host[NI_MAXHOST]; 503 struct sockaddr_storage ss; 504 505 strlcpy(host, "?", sizeof(host)); 506 bzero(&ss, sizeof(ss)); 507 ss.ss_family = ad->pfra_af; 508 if (ss.ss_family == AF_INET) { 509 struct sockaddr_in *sin = (struct sockaddr_in *)&ss; 510 511 sin->sin_len = sizeof(*sin); 512 sin->sin_addr = ad->pfra_ip4addr; 513 } else { 514 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss; 515 516 sin6->sin6_len = sizeof(*sin6); 517 sin6->sin6_addr = ad->pfra_ip6addr; 518 } 519 if (getnameinfo((struct sockaddr *)&ss, ss.ss_len, host, 520 sizeof(host), NULL, 0, NI_NAMEREQD) == 0) 521 printf("\t(%s)", host); 522 } 523 printf("\n"); 524 } 525 526 int 527 nonzero_astats(struct pfr_astats *as) 528 { 529 uint64_t s = 0; 530 531 for (int dir = 0; dir < PFR_DIR_MAX; dir++) 532 for (int op = 0; op < PFR_OP_ADDR_MAX; op++) 533 s |= as->pfras_packets[dir][op] | 534 as->pfras_bytes[dir][op]; 535 536 return (!!s); 537 } 538 539 void 540 print_astats(struct pfr_astats *as, int dns) 541 { 542 time_t time = as->pfras_tzero; 543 int dir, op; 544 char *ct; 545 546 ct = ctime(&time); 547 print_addrx(&as->pfras_a, NULL, dns); 548 if (ct) 549 printf("\tCleared: %s", ct); 550 else 551 printf("\tCleared: %lld\n", (long long)time); 552 if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT) 553 return; 554 for (dir = 0; dir < PFR_DIR_MAX; dir++) 555 for (op = 0; op < PFR_OP_ADDR_MAX; op++) 556 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 557 stats_text[dir][op], 558 (unsigned long long)as->pfras_packets[dir][op], 559 (unsigned long long)as->pfras_bytes[dir][op]); 560 } 561 562 int 563 pfctl_define_table(char *name, int flags, int addrs, const char *anchor, 564 struct pfr_buffer *ab, u_int32_t ticket, struct pfr_uktable *ukt) 565 { 566 struct pfr_table tbl_buf; 567 struct pfr_table *tbl; 568 569 if (ukt == NULL) { 570 bzero(&tbl_buf, sizeof(tbl_buf)); 571 tbl = &tbl_buf; 572 } else { 573 if (ab->pfrb_size != 0) { 574 /* 575 * copy IP addresses which come with table from 576 * temporal buffer to buffer attached to table. 577 */ 578 ukt->pfrukt_addrs = *ab; 579 ab->pfrb_size = 0; 580 ab->pfrb_msize = 0; 581 ab->pfrb_caddr = NULL; 582 } else 583 memset(&ukt->pfrukt_addrs, 0, 584 sizeof(struct pfr_buffer)); 585 586 tbl = &ukt->pfrukt_t; 587 } 588 589 if (strlcpy(tbl->pfrt_name, name, sizeof(tbl->pfrt_name)) >= 590 sizeof(tbl->pfrt_name) || 591 strlcpy(tbl->pfrt_anchor, anchor, sizeof(tbl->pfrt_anchor)) >= 592 sizeof(tbl->pfrt_anchor)) 593 errx(1, "%s: strlcpy", __func__); 594 tbl->pfrt_flags = flags; 595 DBGPRINT("%s %s@%s [%x]\n", __func__, tbl->pfrt_name, tbl->pfrt_anchor, 596 tbl->pfrt_flags); 597 598 /* 599 * non-root anchors processed by parse.y are loaded to kernel later. 600 * Here we load tables, which are either created for root anchor 601 * or by 'pfctl -t ... -T ...' command. 602 */ 603 if (ukt != NULL) 604 return (0); 605 606 return (pfr_ina_define(tbl, ab->pfrb_caddr, ab->pfrb_size, NULL, NULL, 607 ticket, addrs ? PFR_FLAG_ADDRSTOO : 0)); 608 } 609 610 void 611 warn_duplicate_tables(const char *tablename, const char *anchorname) 612 { 613 struct pfr_buffer b; 614 struct pfr_table *t; 615 616 bzero(&b, sizeof(b)); 617 b.pfrb_type = PFRB_TABLES; 618 for (;;) { 619 pfr_buf_grow(&b, b.pfrb_size); 620 b.pfrb_size = b.pfrb_msize; 621 if (pfr_get_tables(NULL, b.pfrb_caddr, 622 &b.pfrb_size, PFR_FLAG_ALLRSETS)) 623 err(1, "pfr_get_tables"); 624 if (b.pfrb_size <= b.pfrb_msize) 625 break; 626 } 627 PFRB_FOREACH(t, &b) { 628 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE)) 629 continue; 630 if (!strcmp(anchorname, t->pfrt_anchor)) 631 continue; 632 if (!strcmp(tablename, t->pfrt_name)) 633 warnx("warning: table <%s> already defined" 634 " in anchor \"%s\"", tablename, 635 t->pfrt_anchor[0] ? t->pfrt_anchor : "/"); 636 } 637 pfr_buf_clear(&b); 638 } 639 640 void 641 xprintf(int opts, const char *fmt, ...) 642 { 643 va_list args; 644 645 if (opts & PF_OPT_QUIET) 646 return; 647 648 va_start(args, fmt); 649 vfprintf(stderr, fmt, args); 650 va_end(args); 651 652 if (opts & PF_OPT_DUMMYACTION) 653 fprintf(stderr, " (dummy).\n"); 654 else if (opts & PF_OPT_NOACTION) 655 fprintf(stderr, " (syntax only).\n"); 656 else 657 fprintf(stderr, ".\n"); 658 } 659 660 661 /* interface stuff */ 662 663 void 664 pfctl_show_ifaces(const char *filter, int opts) 665 { 666 struct pfr_buffer b; 667 struct pfi_kif *p; 668 669 bzero(&b, sizeof(b)); 670 b.pfrb_type = PFRB_IFACES; 671 for (;;) { 672 pfr_buf_grow(&b, b.pfrb_size); 673 b.pfrb_size = b.pfrb_msize; 674 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) 675 errx(1, "%s", pf_strerror(errno)); 676 if (b.pfrb_size <= b.pfrb_msize) 677 break; 678 } 679 if (opts & PF_OPT_SHOWALL) 680 pfctl_print_title("INTERFACES:"); 681 PFRB_FOREACH(p, &b) 682 print_iface(p, opts); 683 } 684 685 void 686 print_iface(struct pfi_kif *p, int opts) 687 { 688 time_t tzero = p->pfik_tzero; 689 int i, af, dir, act; 690 char *ct; 691 692 printf("%s", p->pfik_name); 693 if (opts & PF_OPT_VERBOSE) { 694 if (p->pfik_flags & PFI_IFLAG_SKIP) 695 printf(" (skip)"); 696 } 697 printf("\n"); 698 699 if (!(opts & PF_OPT_VERBOSE2)) 700 return; 701 ct = ctime(&tzero); 702 if (ct) 703 printf("\tCleared: %s", ct); 704 else 705 printf("\tCleared: %lld\n", (long long)tzero); 706 printf("\tReferences: %-18d\n", p->pfik_rulerefs); 707 for (i = 0; i < 8; i++) { 708 af = (i>>2) & 1; 709 dir = (i>>1) &1; 710 act = i & 1; 711 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 712 istats_text[af][dir][act], 713 (unsigned long long)p->pfik_packets[af][dir][act], 714 (unsigned long long)p->pfik_bytes[af][dir][act]); 715 } 716 } 717