1 /* $OpenBSD: pfctl.c,v 1.278 2008/08/31 20:18:17 jmc Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2001 Daniel Hartmeier 7 * Copyright (c) 2002,2003 Henning Brauer 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * - Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * - Redistributions in binary form must reproduce the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer in the documentation and/or other materials provided 19 * with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #define PFIOC_USE_LATEST 40 41 #include <sys/types.h> 42 #include <sys/ioctl.h> 43 #include <sys/socket.h> 44 #include <sys/stat.h> 45 #include <sys/endian.h> 46 47 #include <net/if.h> 48 #include <netinet/in.h> 49 #include <net/pfvar.h> 50 #include <arpa/inet.h> 51 #include <net/altq/altq.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <libpfctl.h> 57 #include <limits.h> 58 #include <netdb.h> 59 #include <stdint.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "pfctl_parser.h" 66 #include "pfctl.h" 67 68 void usage(void); 69 int pfctl_enable(int, int); 70 int pfctl_disable(int, int); 71 int pfctl_clear_stats(int, int); 72 int pfctl_get_skip_ifaces(void); 73 int pfctl_check_skip_ifaces(char *); 74 int pfctl_adjust_skip_ifaces(struct pfctl *); 75 int pfctl_clear_interface_flags(int, int); 76 int pfctl_flush_eth_rules(int, int, char *); 77 int pfctl_flush_rules(int, int, char *); 78 int pfctl_flush_nat(int, int, char *); 79 int pfctl_clear_altq(int, int); 80 int pfctl_clear_src_nodes(int, int); 81 int pfctl_clear_iface_states(int, const char *, int); 82 void pfctl_addrprefix(char *, struct pf_addr *); 83 int pfctl_kill_src_nodes(int, const char *, int); 84 int pfctl_net_kill_states(int, const char *, int); 85 int pfctl_gateway_kill_states(int, const char *, int); 86 int pfctl_label_kill_states(int, const char *, int); 87 int pfctl_id_kill_states(int, const char *, int); 88 void pfctl_init_options(struct pfctl *); 89 int pfctl_load_options(struct pfctl *); 90 int pfctl_load_limit(struct pfctl *, unsigned int, unsigned int); 91 int pfctl_load_timeout(struct pfctl *, unsigned int, unsigned int); 92 int pfctl_load_debug(struct pfctl *, unsigned int); 93 int pfctl_load_logif(struct pfctl *, char *); 94 int pfctl_load_hostid(struct pfctl *, u_int32_t); 95 int pfctl_load_reassembly(struct pfctl *, u_int32_t); 96 int pfctl_load_syncookies(struct pfctl *, u_int8_t); 97 int pfctl_get_pool(int, struct pfctl_pool *, u_int32_t, u_int32_t, int, 98 char *); 99 void pfctl_print_eth_rule_counters(struct pfctl_eth_rule *, int); 100 void pfctl_print_rule_counters(struct pfctl_rule *, int); 101 int pfctl_show_eth_rules(int, char *, int, enum pfctl_show, char *, int, int); 102 int pfctl_show_rules(int, char *, int, enum pfctl_show, char *, int, int); 103 int pfctl_show_nat(int, char *, int, char *, int); 104 int pfctl_show_src_nodes(int, int); 105 int pfctl_show_states(int, const char *, int); 106 int pfctl_show_status(int, int); 107 int pfctl_show_running(int); 108 int pfctl_show_timeouts(int, int); 109 int pfctl_show_limits(int, int); 110 void pfctl_debug(int, u_int32_t, int); 111 int pfctl_test_altqsupport(int, int); 112 int pfctl_show_anchors(int, int, char *); 113 int pfctl_show_eth_anchors(int, int, char *); 114 int pfctl_ruleset_trans(struct pfctl *, char *, struct pfctl_anchor *, bool); 115 int pfctl_eth_ruleset_trans(struct pfctl *, char *, 116 struct pfctl_eth_anchor *); 117 int pfctl_load_eth_ruleset(struct pfctl *, char *, 118 struct pfctl_eth_ruleset *, int); 119 int pfctl_load_eth_rule(struct pfctl *, char *, struct pfctl_eth_rule *, 120 int); 121 int pfctl_load_ruleset(struct pfctl *, char *, 122 struct pfctl_ruleset *, int, int); 123 int pfctl_load_rule(struct pfctl *, char *, struct pfctl_rule *, int); 124 const char *pfctl_lookup_option(char *, const char * const *); 125 126 static struct pfctl_anchor_global pf_anchors; 127 struct pfctl_anchor pf_main_anchor; 128 struct pfctl_eth_anchor pf_eth_main_anchor; 129 static struct pfr_buffer skip_b; 130 131 static const char *clearopt; 132 static char *rulesopt; 133 static const char *showopt; 134 static const char *debugopt; 135 static char *anchoropt; 136 static const char *optiopt = NULL; 137 static const char *pf_device = "/dev/pf"; 138 static char *ifaceopt; 139 static char *tableopt; 140 static const char *tblcmdopt; 141 static int src_node_killers; 142 static char *src_node_kill[2]; 143 static int state_killers; 144 static char *state_kill[2]; 145 int loadopt; 146 int altqsupport; 147 148 int dev = -1; 149 static int first_title = 1; 150 static int labels = 0; 151 152 #define INDENT(d, o) do { \ 153 if (o) { \ 154 int i; \ 155 for (i=0; i < d; i++) \ 156 printf(" "); \ 157 } \ 158 } while (0); \ 159 160 161 static const struct { 162 const char *name; 163 int index; 164 } pf_limits[] = { 165 { "states", PF_LIMIT_STATES }, 166 { "src-nodes", PF_LIMIT_SRC_NODES }, 167 { "frags", PF_LIMIT_FRAGS }, 168 { "table-entries", PF_LIMIT_TABLE_ENTRIES }, 169 { NULL, 0 } 170 }; 171 172 struct pf_hint { 173 const char *name; 174 int timeout; 175 }; 176 static const struct pf_hint pf_hint_normal[] = { 177 { "tcp.first", 2 * 60 }, 178 { "tcp.opening", 30 }, 179 { "tcp.established", 24 * 60 * 60 }, 180 { "tcp.closing", 15 * 60 }, 181 { "tcp.finwait", 45 }, 182 { "tcp.closed", 90 }, 183 { "tcp.tsdiff", 30 }, 184 { NULL, 0 } 185 }; 186 static const struct pf_hint pf_hint_satellite[] = { 187 { "tcp.first", 3 * 60 }, 188 { "tcp.opening", 30 + 5 }, 189 { "tcp.established", 24 * 60 * 60 }, 190 { "tcp.closing", 15 * 60 + 5 }, 191 { "tcp.finwait", 45 + 5 }, 192 { "tcp.closed", 90 + 5 }, 193 { "tcp.tsdiff", 60 }, 194 { NULL, 0 } 195 }; 196 static const struct pf_hint pf_hint_conservative[] = { 197 { "tcp.first", 60 * 60 }, 198 { "tcp.opening", 15 * 60 }, 199 { "tcp.established", 5 * 24 * 60 * 60 }, 200 { "tcp.closing", 60 * 60 }, 201 { "tcp.finwait", 10 * 60 }, 202 { "tcp.closed", 3 * 60 }, 203 { "tcp.tsdiff", 60 }, 204 { NULL, 0 } 205 }; 206 static const struct pf_hint pf_hint_aggressive[] = { 207 { "tcp.first", 30 }, 208 { "tcp.opening", 5 }, 209 { "tcp.established", 5 * 60 * 60 }, 210 { "tcp.closing", 60 }, 211 { "tcp.finwait", 30 }, 212 { "tcp.closed", 30 }, 213 { "tcp.tsdiff", 10 }, 214 { NULL, 0 } 215 }; 216 217 static const struct { 218 const char *name; 219 const struct pf_hint *hint; 220 } pf_hints[] = { 221 { "normal", pf_hint_normal }, 222 { "satellite", pf_hint_satellite }, 223 { "high-latency", pf_hint_satellite }, 224 { "conservative", pf_hint_conservative }, 225 { "aggressive", pf_hint_aggressive }, 226 { NULL, NULL } 227 }; 228 229 static const char * const clearopt_list[] = { 230 "nat", "queue", "rules", "Sources", 231 "states", "info", "Tables", "osfp", "all", 232 "ethernet", NULL 233 }; 234 235 static const char * const showopt_list[] = { 236 "ether", "nat", "queue", "rules", "Anchors", "Sources", "states", 237 "info", "Interfaces", "labels", "timeouts", "memory", "Tables", 238 "osfp", "Running", "all", NULL 239 }; 240 241 static const char * const tblcmdopt_list[] = { 242 "kill", "flush", "add", "delete", "load", "replace", "show", 243 "test", "zero", "expire", NULL 244 }; 245 246 static const char * const debugopt_list[] = { 247 "none", "urgent", "misc", "loud", NULL 248 }; 249 250 static const char * const optiopt_list[] = { 251 "none", "basic", "profile", NULL 252 }; 253 254 void 255 usage(void) 256 { 257 extern char *__progname; 258 259 fprintf(stderr, 260 "usage: %s [-AdeghMmNnOPqRrvz] [-a anchor] [-D macro=value] [-F modifier]\n" 261 "\t[-f file] [-i interface] [-K host | network]\n" 262 "\t[-k host | network | gateway | label | id] [-o level] [-p device]\n" 263 "\t[-s modifier] [-t table -T command [address ...]] [-x level]\n", 264 __progname); 265 266 exit(1); 267 } 268 269 /* 270 * Cache protocol number to name translations. 271 * 272 * Translation is performed a lot e.g., when dumping states and 273 * getprotobynumber is incredibly expensive. 274 * 275 * Note from the getprotobynumber(3) manpage: 276 * <quote> 277 * These functions use a thread-specific data space; if the data is needed 278 * for future use, it should be copied before any subsequent calls overwrite 279 * it. Only the Internet protocols are currently understood. 280 * </quote> 281 * 282 * Consequently we only cache the name and strdup it for safety. 283 * 284 * At the time of writing this comment the last entry in /etc/protocols is: 285 * divert 258 DIVERT # Divert pseudo-protocol [non IANA] 286 */ 287 const char * 288 pfctl_proto2name(int proto) 289 { 290 static const char *pfctl_proto_cache[259]; 291 struct protoent *p; 292 293 if (proto >= nitems(pfctl_proto_cache)) { 294 p = getprotobynumber(proto); 295 if (p == NULL) { 296 return (NULL); 297 } 298 return (p->p_name); 299 } 300 301 if (pfctl_proto_cache[proto] == NULL) { 302 p = getprotobynumber(proto); 303 if (p == NULL) { 304 return (NULL); 305 } 306 pfctl_proto_cache[proto] = strdup(p->p_name); 307 } 308 309 return (pfctl_proto_cache[proto]); 310 } 311 312 int 313 pfctl_enable(int dev, int opts) 314 { 315 if (ioctl(dev, DIOCSTART)) { 316 if (errno == EEXIST) 317 errx(1, "pf already enabled"); 318 else if (errno == ESRCH) 319 errx(1, "pfil registeration failed"); 320 else 321 err(1, "DIOCSTART"); 322 } 323 if ((opts & PF_OPT_QUIET) == 0) 324 fprintf(stderr, "pf enabled\n"); 325 326 if (altqsupport && ioctl(dev, DIOCSTARTALTQ)) 327 if (errno != EEXIST) 328 err(1, "DIOCSTARTALTQ"); 329 330 return (0); 331 } 332 333 int 334 pfctl_disable(int dev, int opts) 335 { 336 if (ioctl(dev, DIOCSTOP)) { 337 if (errno == ENOENT) 338 errx(1, "pf not enabled"); 339 else 340 err(1, "DIOCSTOP"); 341 } 342 if ((opts & PF_OPT_QUIET) == 0) 343 fprintf(stderr, "pf disabled\n"); 344 345 if (altqsupport && ioctl(dev, DIOCSTOPALTQ)) 346 if (errno != ENOENT) 347 err(1, "DIOCSTOPALTQ"); 348 349 return (0); 350 } 351 352 int 353 pfctl_clear_stats(int dev, int opts) 354 { 355 if (ioctl(dev, DIOCCLRSTATUS)) 356 err(1, "DIOCCLRSTATUS"); 357 if ((opts & PF_OPT_QUIET) == 0) 358 fprintf(stderr, "pf: statistics cleared\n"); 359 return (0); 360 } 361 362 int 363 pfctl_get_skip_ifaces(void) 364 { 365 bzero(&skip_b, sizeof(skip_b)); 366 skip_b.pfrb_type = PFRB_IFACES; 367 for (;;) { 368 pfr_buf_grow(&skip_b, skip_b.pfrb_size); 369 skip_b.pfrb_size = skip_b.pfrb_msize; 370 if (pfi_get_ifaces(NULL, skip_b.pfrb_caddr, &skip_b.pfrb_size)) 371 err(1, "pfi_get_ifaces"); 372 if (skip_b.pfrb_size <= skip_b.pfrb_msize) 373 break; 374 } 375 return (0); 376 } 377 378 int 379 pfctl_check_skip_ifaces(char *ifname) 380 { 381 struct pfi_kif *p; 382 struct node_host *h = NULL, *n = NULL; 383 384 PFRB_FOREACH(p, &skip_b) { 385 if (!strcmp(ifname, p->pfik_name) && 386 (p->pfik_flags & PFI_IFLAG_SKIP)) 387 p->pfik_flags &= ~PFI_IFLAG_SKIP; 388 if (!strcmp(ifname, p->pfik_name) && p->pfik_group != NULL) { 389 if ((h = ifa_grouplookup(p->pfik_name, 0)) == NULL) 390 continue; 391 392 for (n = h; n != NULL; n = n->next) { 393 if (p->pfik_ifp == NULL) 394 continue; 395 if (strncmp(p->pfik_name, ifname, IFNAMSIZ)) 396 continue; 397 398 p->pfik_flags &= ~PFI_IFLAG_SKIP; 399 } 400 } 401 } 402 return (0); 403 } 404 405 int 406 pfctl_adjust_skip_ifaces(struct pfctl *pf) 407 { 408 struct pfi_kif *p, *pp; 409 struct node_host *h = NULL, *n = NULL; 410 411 PFRB_FOREACH(p, &skip_b) { 412 if (p->pfik_group == NULL || !(p->pfik_flags & PFI_IFLAG_SKIP)) 413 continue; 414 415 pfctl_set_interface_flags(pf, p->pfik_name, PFI_IFLAG_SKIP, 0); 416 if ((h = ifa_grouplookup(p->pfik_name, 0)) == NULL) 417 continue; 418 419 for (n = h; n != NULL; n = n->next) 420 PFRB_FOREACH(pp, &skip_b) { 421 if (pp->pfik_ifp == NULL) 422 continue; 423 424 if (strncmp(pp->pfik_name, n->ifname, IFNAMSIZ)) 425 continue; 426 427 if (!(pp->pfik_flags & PFI_IFLAG_SKIP)) 428 pfctl_set_interface_flags(pf, 429 pp->pfik_name, PFI_IFLAG_SKIP, 1); 430 if (pp->pfik_flags & PFI_IFLAG_SKIP) 431 pp->pfik_flags &= ~PFI_IFLAG_SKIP; 432 } 433 } 434 435 PFRB_FOREACH(p, &skip_b) { 436 if (p->pfik_ifp == NULL || ! (p->pfik_flags & PFI_IFLAG_SKIP)) 437 continue; 438 439 pfctl_set_interface_flags(pf, p->pfik_name, PFI_IFLAG_SKIP, 0); 440 } 441 442 return (0); 443 } 444 445 int 446 pfctl_clear_interface_flags(int dev, int opts) 447 { 448 struct pfioc_iface pi; 449 450 if ((opts & PF_OPT_NOACTION) == 0) { 451 bzero(&pi, sizeof(pi)); 452 pi.pfiio_flags = PFI_IFLAG_SKIP; 453 454 if (ioctl(dev, DIOCCLRIFFLAG, &pi)) 455 err(1, "DIOCCLRIFFLAG"); 456 if ((opts & PF_OPT_QUIET) == 0) 457 fprintf(stderr, "pf: interface flags reset\n"); 458 } 459 return (0); 460 } 461 462 int 463 pfctl_flush_eth_rules(int dev, int opts, char *anchorname) 464 { 465 int ret; 466 467 ret = pfctl_clear_eth_rules(dev, anchorname); 468 if (ret != 0) 469 err(1, "pfctl_clear_eth_rules"); 470 471 if ((opts & PF_OPT_QUIET) == 0) 472 fprintf(stderr, "Ethernet rules cleared\n"); 473 474 return (ret); 475 } 476 477 int 478 pfctl_flush_rules(int dev, int opts, char *anchorname) 479 { 480 int ret; 481 482 ret = pfctl_clear_rules(dev, anchorname); 483 if (ret != 0) 484 err(1, "pfctl_clear_rules"); 485 if ((opts & PF_OPT_QUIET) == 0) 486 fprintf(stderr, "rules cleared\n"); 487 return (0); 488 } 489 490 int 491 pfctl_flush_nat(int dev, int opts, char *anchorname) 492 { 493 int ret; 494 495 ret = pfctl_clear_nat(dev, anchorname); 496 if (ret != 0) 497 err(1, "pfctl_clear_nat"); 498 if ((opts & PF_OPT_QUIET) == 0) 499 fprintf(stderr, "nat cleared\n"); 500 return (0); 501 } 502 503 int 504 pfctl_clear_altq(int dev, int opts) 505 { 506 struct pfr_buffer t; 507 508 if (!altqsupport) 509 return (-1); 510 memset(&t, 0, sizeof(t)); 511 t.pfrb_type = PFRB_TRANS; 512 if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "") || 513 pfctl_trans(dev, &t, DIOCXBEGIN, 0) || 514 pfctl_trans(dev, &t, DIOCXCOMMIT, 0)) 515 err(1, "pfctl_clear_altq"); 516 if ((opts & PF_OPT_QUIET) == 0) 517 fprintf(stderr, "altq cleared\n"); 518 return (0); 519 } 520 521 int 522 pfctl_clear_src_nodes(int dev, int opts) 523 { 524 if (ioctl(dev, DIOCCLRSRCNODES)) 525 err(1, "DIOCCLRSRCNODES"); 526 if ((opts & PF_OPT_QUIET) == 0) 527 fprintf(stderr, "source tracking entries cleared\n"); 528 return (0); 529 } 530 531 int 532 pfctl_clear_iface_states(int dev, const char *iface, int opts) 533 { 534 struct pfctl_kill kill; 535 unsigned int killed; 536 537 memset(&kill, 0, sizeof(kill)); 538 if (iface != NULL && strlcpy(kill.ifname, iface, 539 sizeof(kill.ifname)) >= sizeof(kill.ifname)) 540 errx(1, "invalid interface: %s", iface); 541 542 if (opts & PF_OPT_KILLMATCH) 543 kill.kill_match = true; 544 545 if (pfctl_clear_states(dev, &kill, &killed)) 546 err(1, "DIOCCLRSTATES"); 547 if ((opts & PF_OPT_QUIET) == 0) 548 fprintf(stderr, "%d states cleared\n", killed); 549 return (0); 550 } 551 552 void 553 pfctl_addrprefix(char *addr, struct pf_addr *mask) 554 { 555 char *p; 556 const char *errstr; 557 int prefix, ret_ga, q, r; 558 struct addrinfo hints, *res; 559 560 if ((p = strchr(addr, '/')) == NULL) 561 return; 562 563 *p++ = '\0'; 564 prefix = strtonum(p, 0, 128, &errstr); 565 if (errstr) 566 errx(1, "prefix is %s: %s", errstr, p); 567 568 bzero(&hints, sizeof(hints)); 569 /* prefix only with numeric addresses */ 570 hints.ai_flags |= AI_NUMERICHOST; 571 572 if ((ret_ga = getaddrinfo(addr, NULL, &hints, &res))) { 573 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga)); 574 /* NOTREACHED */ 575 } 576 577 if (res->ai_family == AF_INET && prefix > 32) 578 errx(1, "prefix too long for AF_INET"); 579 else if (res->ai_family == AF_INET6 && prefix > 128) 580 errx(1, "prefix too long for AF_INET6"); 581 582 q = prefix >> 3; 583 r = prefix & 7; 584 switch (res->ai_family) { 585 case AF_INET: 586 bzero(&mask->v4, sizeof(mask->v4)); 587 mask->v4.s_addr = htonl((u_int32_t) 588 (0xffffffffffULL << (32 - prefix))); 589 break; 590 case AF_INET6: 591 bzero(&mask->v6, sizeof(mask->v6)); 592 if (q > 0) 593 memset((void *)&mask->v6, 0xff, q); 594 if (r > 0) 595 *((u_char *)&mask->v6 + q) = 596 (0xff00 >> r) & 0xff; 597 break; 598 } 599 freeaddrinfo(res); 600 } 601 602 int 603 pfctl_kill_src_nodes(int dev, const char *iface, int opts) 604 { 605 struct pfioc_src_node_kill psnk; 606 struct addrinfo *res[2], *resp[2]; 607 struct sockaddr last_src, last_dst; 608 int killed, sources, dests; 609 int ret_ga; 610 611 killed = sources = dests = 0; 612 613 memset(&psnk, 0, sizeof(psnk)); 614 memset(&psnk.psnk_src.addr.v.a.mask, 0xff, 615 sizeof(psnk.psnk_src.addr.v.a.mask)); 616 memset(&last_src, 0xff, sizeof(last_src)); 617 memset(&last_dst, 0xff, sizeof(last_dst)); 618 619 pfctl_addrprefix(src_node_kill[0], &psnk.psnk_src.addr.v.a.mask); 620 621 if ((ret_ga = getaddrinfo(src_node_kill[0], NULL, NULL, &res[0]))) { 622 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga)); 623 /* NOTREACHED */ 624 } 625 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) { 626 if (resp[0]->ai_addr == NULL) 627 continue; 628 /* We get lots of duplicates. Catch the easy ones */ 629 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0) 630 continue; 631 last_src = *(struct sockaddr *)resp[0]->ai_addr; 632 633 psnk.psnk_af = resp[0]->ai_family; 634 sources++; 635 636 if (psnk.psnk_af == AF_INET) 637 psnk.psnk_src.addr.v.a.addr.v4 = 638 ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr; 639 else if (psnk.psnk_af == AF_INET6) 640 psnk.psnk_src.addr.v.a.addr.v6 = 641 ((struct sockaddr_in6 *)resp[0]->ai_addr)-> 642 sin6_addr; 643 else 644 errx(1, "Unknown address family %d", psnk.psnk_af); 645 646 if (src_node_killers > 1) { 647 dests = 0; 648 memset(&psnk.psnk_dst.addr.v.a.mask, 0xff, 649 sizeof(psnk.psnk_dst.addr.v.a.mask)); 650 memset(&last_dst, 0xff, sizeof(last_dst)); 651 pfctl_addrprefix(src_node_kill[1], 652 &psnk.psnk_dst.addr.v.a.mask); 653 if ((ret_ga = getaddrinfo(src_node_kill[1], NULL, NULL, 654 &res[1]))) { 655 errx(1, "getaddrinfo: %s", 656 gai_strerror(ret_ga)); 657 /* NOTREACHED */ 658 } 659 for (resp[1] = res[1]; resp[1]; 660 resp[1] = resp[1]->ai_next) { 661 if (resp[1]->ai_addr == NULL) 662 continue; 663 if (psnk.psnk_af != resp[1]->ai_family) 664 continue; 665 666 if (memcmp(&last_dst, resp[1]->ai_addr, 667 sizeof(last_dst)) == 0) 668 continue; 669 last_dst = *(struct sockaddr *)resp[1]->ai_addr; 670 671 dests++; 672 673 if (psnk.psnk_af == AF_INET) 674 psnk.psnk_dst.addr.v.a.addr.v4 = 675 ((struct sockaddr_in *)resp[1]-> 676 ai_addr)->sin_addr; 677 else if (psnk.psnk_af == AF_INET6) 678 psnk.psnk_dst.addr.v.a.addr.v6 = 679 ((struct sockaddr_in6 *)resp[1]-> 680 ai_addr)->sin6_addr; 681 else 682 errx(1, "Unknown address family %d", 683 psnk.psnk_af); 684 685 if (ioctl(dev, DIOCKILLSRCNODES, &psnk)) 686 err(1, "DIOCKILLSRCNODES"); 687 killed += psnk.psnk_killed; 688 } 689 freeaddrinfo(res[1]); 690 } else { 691 if (ioctl(dev, DIOCKILLSRCNODES, &psnk)) 692 err(1, "DIOCKILLSRCNODES"); 693 killed += psnk.psnk_killed; 694 } 695 } 696 697 freeaddrinfo(res[0]); 698 699 if ((opts & PF_OPT_QUIET) == 0) 700 fprintf(stderr, "killed %d src nodes from %d sources and %d " 701 "destinations\n", killed, sources, dests); 702 return (0); 703 } 704 705 int 706 pfctl_net_kill_states(int dev, const char *iface, int opts) 707 { 708 struct pfctl_kill kill; 709 struct addrinfo *res[2], *resp[2]; 710 struct sockaddr last_src, last_dst; 711 unsigned int newkilled; 712 int killed, sources, dests; 713 int ret_ga; 714 715 killed = sources = dests = 0; 716 717 memset(&kill, 0, sizeof(kill)); 718 memset(&kill.src.addr.v.a.mask, 0xff, 719 sizeof(kill.src.addr.v.a.mask)); 720 memset(&last_src, 0xff, sizeof(last_src)); 721 memset(&last_dst, 0xff, sizeof(last_dst)); 722 if (iface != NULL && strlcpy(kill.ifname, iface, 723 sizeof(kill.ifname)) >= sizeof(kill.ifname)) 724 errx(1, "invalid interface: %s", iface); 725 726 pfctl_addrprefix(state_kill[0], &kill.src.addr.v.a.mask); 727 728 if (opts & PF_OPT_KILLMATCH) 729 kill.kill_match = true; 730 731 if ((ret_ga = getaddrinfo(state_kill[0], NULL, NULL, &res[0]))) { 732 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga)); 733 /* NOTREACHED */ 734 } 735 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) { 736 if (resp[0]->ai_addr == NULL) 737 continue; 738 /* We get lots of duplicates. Catch the easy ones */ 739 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0) 740 continue; 741 last_src = *(struct sockaddr *)resp[0]->ai_addr; 742 743 kill.af = resp[0]->ai_family; 744 sources++; 745 746 if (kill.af == AF_INET) 747 kill.src.addr.v.a.addr.v4 = 748 ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr; 749 else if (kill.af == AF_INET6) 750 kill.src.addr.v.a.addr.v6 = 751 ((struct sockaddr_in6 *)resp[0]->ai_addr)-> 752 sin6_addr; 753 else 754 errx(1, "Unknown address family %d", kill.af); 755 756 if (state_killers > 1) { 757 dests = 0; 758 memset(&kill.dst.addr.v.a.mask, 0xff, 759 sizeof(kill.dst.addr.v.a.mask)); 760 memset(&last_dst, 0xff, sizeof(last_dst)); 761 pfctl_addrprefix(state_kill[1], 762 &kill.dst.addr.v.a.mask); 763 if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL, 764 &res[1]))) { 765 errx(1, "getaddrinfo: %s", 766 gai_strerror(ret_ga)); 767 /* NOTREACHED */ 768 } 769 for (resp[1] = res[1]; resp[1]; 770 resp[1] = resp[1]->ai_next) { 771 if (resp[1]->ai_addr == NULL) 772 continue; 773 if (kill.af != resp[1]->ai_family) 774 continue; 775 776 if (memcmp(&last_dst, resp[1]->ai_addr, 777 sizeof(last_dst)) == 0) 778 continue; 779 last_dst = *(struct sockaddr *)resp[1]->ai_addr; 780 781 dests++; 782 783 if (kill.af == AF_INET) 784 kill.dst.addr.v.a.addr.v4 = 785 ((struct sockaddr_in *)resp[1]-> 786 ai_addr)->sin_addr; 787 else if (kill.af == AF_INET6) 788 kill.dst.addr.v.a.addr.v6 = 789 ((struct sockaddr_in6 *)resp[1]-> 790 ai_addr)->sin6_addr; 791 else 792 errx(1, "Unknown address family %d", 793 kill.af); 794 795 if (pfctl_kill_states(dev, &kill, &newkilled)) 796 err(1, "DIOCKILLSTATES"); 797 killed += newkilled; 798 } 799 freeaddrinfo(res[1]); 800 } else { 801 if (pfctl_kill_states(dev, &kill, &newkilled)) 802 err(1, "DIOCKILLSTATES"); 803 killed += newkilled; 804 } 805 } 806 807 freeaddrinfo(res[0]); 808 809 if ((opts & PF_OPT_QUIET) == 0) 810 fprintf(stderr, "killed %d states from %d sources and %d " 811 "destinations\n", killed, sources, dests); 812 return (0); 813 } 814 815 int 816 pfctl_gateway_kill_states(int dev, const char *iface, int opts) 817 { 818 struct pfctl_kill kill; 819 struct addrinfo *res, *resp; 820 struct sockaddr last_src; 821 unsigned int newkilled; 822 int killed = 0; 823 int ret_ga; 824 825 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) { 826 warnx("no gateway specified"); 827 usage(); 828 } 829 830 memset(&kill, 0, sizeof(kill)); 831 memset(&kill.rt_addr.addr.v.a.mask, 0xff, 832 sizeof(kill.rt_addr.addr.v.a.mask)); 833 memset(&last_src, 0xff, sizeof(last_src)); 834 if (iface != NULL && strlcpy(kill.ifname, iface, 835 sizeof(kill.ifname)) >= sizeof(kill.ifname)) 836 errx(1, "invalid interface: %s", iface); 837 838 if (opts & PF_OPT_KILLMATCH) 839 kill.kill_match = true; 840 841 pfctl_addrprefix(state_kill[1], &kill.rt_addr.addr.v.a.mask); 842 843 if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL, &res))) { 844 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga)); 845 /* NOTREACHED */ 846 } 847 for (resp = res; resp; resp = resp->ai_next) { 848 if (resp->ai_addr == NULL) 849 continue; 850 /* We get lots of duplicates. Catch the easy ones */ 851 if (memcmp(&last_src, resp->ai_addr, sizeof(last_src)) == 0) 852 continue; 853 last_src = *(struct sockaddr *)resp->ai_addr; 854 855 kill.af = resp->ai_family; 856 857 if (kill.af == AF_INET) 858 kill.rt_addr.addr.v.a.addr.v4 = 859 ((struct sockaddr_in *)resp->ai_addr)->sin_addr; 860 else if (kill.af == AF_INET6) 861 kill.rt_addr.addr.v.a.addr.v6 = 862 ((struct sockaddr_in6 *)resp->ai_addr)-> 863 sin6_addr; 864 else 865 errx(1, "Unknown address family %d", kill.af); 866 867 if (pfctl_kill_states(dev, &kill, &newkilled)) 868 err(1, "DIOCKILLSTATES"); 869 killed += newkilled; 870 } 871 872 freeaddrinfo(res); 873 874 if ((opts & PF_OPT_QUIET) == 0) 875 fprintf(stderr, "killed %d states\n", killed); 876 return (0); 877 } 878 879 int 880 pfctl_label_kill_states(int dev, const char *iface, int opts) 881 { 882 struct pfctl_kill kill; 883 unsigned int killed; 884 885 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) { 886 warnx("no label specified"); 887 usage(); 888 } 889 memset(&kill, 0, sizeof(kill)); 890 if (iface != NULL && strlcpy(kill.ifname, iface, 891 sizeof(kill.ifname)) >= sizeof(kill.ifname)) 892 errx(1, "invalid interface: %s", iface); 893 894 if (opts & PF_OPT_KILLMATCH) 895 kill.kill_match = true; 896 897 if (strlcpy(kill.label, state_kill[1], sizeof(kill.label)) >= 898 sizeof(kill.label)) 899 errx(1, "label too long: %s", state_kill[1]); 900 901 if (pfctl_kill_states(dev, &kill, &killed)) 902 err(1, "DIOCKILLSTATES"); 903 904 if ((opts & PF_OPT_QUIET) == 0) 905 fprintf(stderr, "killed %d states\n", killed); 906 907 return (0); 908 } 909 910 int 911 pfctl_id_kill_states(int dev, const char *iface, int opts) 912 { 913 struct pfctl_kill kill; 914 unsigned int killed; 915 916 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) { 917 warnx("no id specified"); 918 usage(); 919 } 920 921 memset(&kill, 0, sizeof(kill)); 922 923 if (opts & PF_OPT_KILLMATCH) 924 kill.kill_match = true; 925 926 if ((sscanf(state_kill[1], "%jx/%x", 927 &kill.cmp.id, &kill.cmp.creatorid)) == 2) { 928 } 929 else if ((sscanf(state_kill[1], "%jx", &kill.cmp.id)) == 1) { 930 kill.cmp.creatorid = 0; 931 } else { 932 warnx("wrong id format specified"); 933 usage(); 934 } 935 if (kill.cmp.id == 0) { 936 warnx("cannot kill id 0"); 937 usage(); 938 } 939 940 if (pfctl_kill_states(dev, &kill, &killed)) 941 err(1, "DIOCKILLSTATES"); 942 943 if ((opts & PF_OPT_QUIET) == 0) 944 fprintf(stderr, "killed %d states\n", killed); 945 946 return (0); 947 } 948 949 int 950 pfctl_get_pool(int dev, struct pfctl_pool *pool, u_int32_t nr, 951 u_int32_t ticket, int r_action, char *anchorname) 952 { 953 struct pfioc_pooladdr pp; 954 struct pf_pooladdr *pa; 955 u_int32_t pnr, mpnr; 956 957 memset(&pp, 0, sizeof(pp)); 958 memcpy(pp.anchor, anchorname, sizeof(pp.anchor)); 959 pp.r_action = r_action; 960 pp.r_num = nr; 961 pp.ticket = ticket; 962 if (ioctl(dev, DIOCGETADDRS, &pp)) { 963 warn("DIOCGETADDRS"); 964 return (-1); 965 } 966 mpnr = pp.nr; 967 TAILQ_INIT(&pool->list); 968 for (pnr = 0; pnr < mpnr; ++pnr) { 969 pp.nr = pnr; 970 if (ioctl(dev, DIOCGETADDR, &pp)) { 971 warn("DIOCGETADDR"); 972 return (-1); 973 } 974 pa = calloc(1, sizeof(struct pf_pooladdr)); 975 if (pa == NULL) 976 err(1, "calloc"); 977 bcopy(&pp.addr, pa, sizeof(struct pf_pooladdr)); 978 TAILQ_INSERT_TAIL(&pool->list, pa, entries); 979 } 980 981 return (0); 982 } 983 984 void 985 pfctl_move_pool(struct pfctl_pool *src, struct pfctl_pool *dst) 986 { 987 struct pf_pooladdr *pa; 988 989 while ((pa = TAILQ_FIRST(&src->list)) != NULL) { 990 TAILQ_REMOVE(&src->list, pa, entries); 991 TAILQ_INSERT_TAIL(&dst->list, pa, entries); 992 } 993 } 994 995 void 996 pfctl_clear_pool(struct pfctl_pool *pool) 997 { 998 struct pf_pooladdr *pa; 999 1000 while ((pa = TAILQ_FIRST(&pool->list)) != NULL) { 1001 TAILQ_REMOVE(&pool->list, pa, entries); 1002 free(pa); 1003 } 1004 } 1005 1006 void 1007 pfctl_print_eth_rule_counters(struct pfctl_eth_rule *rule, int opts) 1008 { 1009 if (opts & PF_OPT_VERBOSE) { 1010 printf(" [ Evaluations: %-8llu Packets: %-8llu " 1011 "Bytes: %-10llu]\n", 1012 (unsigned long long)rule->evaluations, 1013 (unsigned long long)(rule->packets[0] + 1014 rule->packets[1]), 1015 (unsigned long long)(rule->bytes[0] + 1016 rule->bytes[1])); 1017 } 1018 if (opts & PF_OPT_VERBOSE2) { 1019 char timestr[30]; 1020 1021 if (rule->last_active_timestamp != 0) { 1022 bcopy(ctime(&rule->last_active_timestamp), timestr, 1023 sizeof(timestr)); 1024 *strchr(timestr, '\n') = '\0'; 1025 } else { 1026 snprintf(timestr, sizeof(timestr), "N/A"); 1027 } 1028 printf(" [ Last Active Time: %s ]\n", timestr); 1029 } 1030 } 1031 1032 void 1033 pfctl_print_rule_counters(struct pfctl_rule *rule, int opts) 1034 { 1035 if (opts & PF_OPT_DEBUG) { 1036 const char *t[PF_SKIP_COUNT] = { "i", "d", "f", 1037 "p", "sa", "sp", "da", "dp" }; 1038 int i; 1039 1040 printf(" [ Skip steps: "); 1041 for (i = 0; i < PF_SKIP_COUNT; ++i) { 1042 if (rule->skip[i].nr == rule->nr + 1) 1043 continue; 1044 printf("%s=", t[i]); 1045 if (rule->skip[i].nr == -1) 1046 printf("end "); 1047 else 1048 printf("%u ", rule->skip[i].nr); 1049 } 1050 printf("]\n"); 1051 1052 printf(" [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n", 1053 rule->qname, rule->qid, rule->pqname, rule->pqid); 1054 } 1055 if (opts & PF_OPT_VERBOSE) { 1056 printf(" [ Evaluations: %-8llu Packets: %-8llu " 1057 "Bytes: %-10llu States: %-6ju]\n", 1058 (unsigned long long)rule->evaluations, 1059 (unsigned long long)(rule->packets[0] + 1060 rule->packets[1]), 1061 (unsigned long long)(rule->bytes[0] + 1062 rule->bytes[1]), (uintmax_t)rule->states_cur); 1063 if (!(opts & PF_OPT_DEBUG)) 1064 printf(" [ Inserted: uid %u pid %u " 1065 "State Creations: %-6ju]\n", 1066 (unsigned)rule->cuid, (unsigned)rule->cpid, 1067 (uintmax_t)rule->states_tot); 1068 } 1069 if (opts & PF_OPT_VERBOSE2) { 1070 char timestr[30]; 1071 if (rule->last_active_timestamp != 0) { 1072 bcopy(ctime(&rule->last_active_timestamp), timestr, 1073 sizeof(timestr)); 1074 *strchr(timestr, '\n') = '\0'; 1075 } else { 1076 snprintf(timestr, sizeof(timestr), "N/A"); 1077 } 1078 printf(" [ Last Active Time: %s ]\n", timestr); 1079 } 1080 } 1081 1082 void 1083 pfctl_print_title(char *title) 1084 { 1085 if (!first_title) 1086 printf("\n"); 1087 first_title = 0; 1088 printf("%s\n", title); 1089 } 1090 1091 int 1092 pfctl_show_eth_rules(int dev, char *path, int opts, enum pfctl_show format, 1093 char *anchorname, int depth, int wildcard) 1094 { 1095 char anchor_call[MAXPATHLEN]; 1096 struct pfctl_eth_rules_info info; 1097 struct pfctl_eth_rule rule; 1098 int brace; 1099 int dotitle = opts & PF_OPT_SHOWALL; 1100 int len = strlen(path); 1101 char *npath, *p; 1102 1103 /* 1104 * Truncate a trailing / and * on an anchorname before searching for 1105 * the ruleset, this is syntactic sugar that doesn't actually make it 1106 * to the kernel. 1107 */ 1108 if ((p = strrchr(anchorname, '/')) != NULL && 1109 p[1] == '*' && p[2] == '\0') { 1110 p[0] = '\0'; 1111 } 1112 1113 if (anchorname[0] == '/') { 1114 if ((npath = calloc(1, MAXPATHLEN)) == NULL) 1115 errx(1, "pfctl_rules: calloc"); 1116 snprintf(npath, MAXPATHLEN, "%s", anchorname); 1117 } else { 1118 if (path[0]) 1119 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname); 1120 else 1121 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname); 1122 npath = path; 1123 } 1124 1125 /* 1126 * If this anchor was called with a wildcard path, go through 1127 * the rulesets in the anchor rather than the rules. 1128 */ 1129 if (wildcard && (opts & PF_OPT_RECURSE)) { 1130 struct pfctl_eth_rulesets_info ri; 1131 u_int32_t mnr, nr; 1132 1133 if (pfctl_get_eth_rulesets_info(dev, &ri, npath)) { 1134 if (errno == EINVAL) { 1135 fprintf(stderr, "Anchor '%s' " 1136 "not found.\n", anchorname); 1137 } else { 1138 warn("DIOCGETETHRULESETS"); 1139 return (-1); 1140 } 1141 } 1142 mnr = ri.nr; 1143 1144 pfctl_print_eth_rule_counters(&rule, opts); 1145 for (nr = 0; nr < mnr; ++nr) { 1146 struct pfctl_eth_ruleset_info rs; 1147 1148 if (pfctl_get_eth_ruleset(dev, npath, nr, &rs)) 1149 err(1, "DIOCGETETHRULESET"); 1150 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1151 printf("anchor \"%s\" all {\n", rs.name); 1152 pfctl_show_eth_rules(dev, npath, opts, 1153 format, rs.name, depth + 1, 0); 1154 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1155 printf("}\n"); 1156 } 1157 path[len] = '\0'; 1158 return (0); 1159 } 1160 1161 if (pfctl_get_eth_rules_info(dev, &info, path)) { 1162 warn("DIOCGETETHRULES"); 1163 return (-1); 1164 } 1165 for (int nr = 0; nr < info.nr; nr++) { 1166 brace = 0; 1167 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1168 if (pfctl_get_eth_rule(dev, nr, info.ticket, path, &rule, 1169 opts & PF_OPT_CLRRULECTRS, anchor_call) != 0) { 1170 warn("DIOCGETETHRULE"); 1171 return (-1); 1172 } 1173 if (anchor_call[0] && 1174 ((((p = strrchr(anchor_call, '_')) != NULL) && 1175 (p == anchor_call || 1176 *(--p) == '/')) || (opts & PF_OPT_RECURSE))) { 1177 brace++; 1178 int aclen = strlen(anchor_call); 1179 if (anchor_call[aclen - 1] == '*') 1180 anchor_call[aclen - 2] = '\0'; 1181 } 1182 p = &anchor_call[0]; 1183 if (dotitle) { 1184 pfctl_print_title("ETH RULES:"); 1185 dotitle = 0; 1186 } 1187 print_eth_rule(&rule, anchor_call, 1188 opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG)); 1189 if (brace) 1190 printf(" {\n"); 1191 else 1192 printf("\n"); 1193 pfctl_print_eth_rule_counters(&rule, opts); 1194 if (brace) { 1195 pfctl_show_eth_rules(dev, path, opts, format, 1196 p, depth + 1, rule.anchor_wildcard); 1197 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1198 printf("}\n"); 1199 } 1200 } 1201 1202 path[len] = '\0'; 1203 return (0); 1204 } 1205 1206 int 1207 pfctl_show_rules(int dev, char *path, int opts, enum pfctl_show format, 1208 char *anchorname, int depth, int wildcard) 1209 { 1210 struct pfctl_rules_info ri; 1211 struct pfctl_rule rule; 1212 char anchor_call[MAXPATHLEN]; 1213 u_int32_t nr, header = 0; 1214 int rule_numbers = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG); 1215 int numeric = opts & PF_OPT_NUMERIC; 1216 int len = strlen(path), ret = 0; 1217 char *npath, *p; 1218 1219 /* 1220 * Truncate a trailing / and * on an anchorname before searching for 1221 * the ruleset, this is syntactic sugar that doesn't actually make it 1222 * to the kernel. 1223 */ 1224 if ((p = strrchr(anchorname, '/')) != NULL && 1225 p[1] == '*' && p[2] == '\0') { 1226 p[0] = '\0'; 1227 } 1228 1229 if (anchorname[0] == '/') { 1230 if ((npath = calloc(1, MAXPATHLEN)) == NULL) 1231 errx(1, "pfctl_rules: calloc"); 1232 snprintf(npath, MAXPATHLEN, "%s", anchorname); 1233 } else { 1234 if (path[0]) 1235 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname); 1236 else 1237 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname); 1238 npath = path; 1239 } 1240 1241 /* 1242 * If this anchor was called with a wildcard path, go through 1243 * the rulesets in the anchor rather than the rules. 1244 */ 1245 if (wildcard && (opts & PF_OPT_RECURSE)) { 1246 struct pfioc_ruleset prs; 1247 u_int32_t mnr, nr; 1248 1249 memset(&prs, 0, sizeof(prs)); 1250 memcpy(prs.path, npath, sizeof(prs.path)); 1251 if (ioctl(dev, DIOCGETRULESETS, &prs)) { 1252 if (errno == EINVAL) 1253 fprintf(stderr, "Anchor '%s' " 1254 "not found.\n", anchorname); 1255 else 1256 err(1, "DIOCGETRULESETS"); 1257 } 1258 mnr = prs.nr; 1259 1260 pfctl_print_rule_counters(&rule, opts); 1261 for (nr = 0; nr < mnr; ++nr) { 1262 prs.nr = nr; 1263 if (ioctl(dev, DIOCGETRULESET, &prs)) 1264 err(1, "DIOCGETRULESET"); 1265 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1266 printf("anchor \"%s\" all {\n", prs.name); 1267 pfctl_show_rules(dev, npath, opts, 1268 format, prs.name, depth + 1, 0); 1269 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1270 printf("}\n"); 1271 } 1272 path[len] = '\0'; 1273 return (0); 1274 } 1275 1276 if (opts & PF_OPT_SHOWALL) { 1277 ret = pfctl_get_rules_info(dev, &ri, PF_PASS, path); 1278 if (ret != 0) { 1279 warn("DIOCGETRULES"); 1280 goto error; 1281 } 1282 header++; 1283 } 1284 ret = pfctl_get_rules_info(dev, &ri, PF_SCRUB, path); 1285 if (ret != 0) { 1286 warn("DIOCGETRULES"); 1287 goto error; 1288 } 1289 if (opts & PF_OPT_SHOWALL) { 1290 if (format == PFCTL_SHOW_RULES && (ri.nr > 0 || header)) 1291 pfctl_print_title("FILTER RULES:"); 1292 else if (format == PFCTL_SHOW_LABELS && labels) 1293 pfctl_print_title("LABEL COUNTERS:"); 1294 } 1295 1296 for (nr = 0; nr < ri.nr; ++nr) { 1297 if (pfctl_get_clear_rule(dev, nr, ri.ticket, path, PF_SCRUB, 1298 &rule, anchor_call, opts & PF_OPT_CLRRULECTRS)) { 1299 warn("DIOCGETRULENV"); 1300 goto error; 1301 } 1302 1303 if (pfctl_get_pool(dev, &rule.rpool, 1304 nr, ri.ticket, PF_SCRUB, path) != 0) 1305 goto error; 1306 1307 switch (format) { 1308 case PFCTL_SHOW_LABELS: 1309 break; 1310 case PFCTL_SHOW_RULES: 1311 if (rule.label[0][0] && (opts & PF_OPT_SHOWALL)) 1312 labels = 1; 1313 print_rule(&rule, anchor_call, rule_numbers, numeric); 1314 printf("\n"); 1315 pfctl_print_rule_counters(&rule, opts); 1316 break; 1317 case PFCTL_SHOW_NOTHING: 1318 break; 1319 } 1320 pfctl_clear_pool(&rule.rpool); 1321 } 1322 ret = pfctl_get_rules_info(dev, &ri, PF_PASS, path); 1323 if (ret != 0) { 1324 warn("DIOCGETRULES"); 1325 goto error; 1326 } 1327 for (nr = 0; nr < ri.nr; ++nr) { 1328 if (pfctl_get_clear_rule(dev, nr, ri.ticket, path, PF_PASS, 1329 &rule, anchor_call, opts & PF_OPT_CLRRULECTRS)) { 1330 warn("DIOCGETRULE"); 1331 goto error; 1332 } 1333 1334 if (pfctl_get_pool(dev, &rule.rpool, 1335 nr, ri.ticket, PF_PASS, path) != 0) 1336 goto error; 1337 1338 switch (format) { 1339 case PFCTL_SHOW_LABELS: { 1340 bool show = false; 1341 int i = 0; 1342 1343 while (rule.label[i][0]) { 1344 printf("%s ", rule.label[i++]); 1345 show = true; 1346 } 1347 1348 if (show) { 1349 printf("%llu %llu %llu %llu" 1350 " %llu %llu %llu %ju\n", 1351 (unsigned long long)rule.evaluations, 1352 (unsigned long long)(rule.packets[0] + 1353 rule.packets[1]), 1354 (unsigned long long)(rule.bytes[0] + 1355 rule.bytes[1]), 1356 (unsigned long long)rule.packets[0], 1357 (unsigned long long)rule.bytes[0], 1358 (unsigned long long)rule.packets[1], 1359 (unsigned long long)rule.bytes[1], 1360 (uintmax_t)rule.states_tot); 1361 } 1362 break; 1363 } 1364 case PFCTL_SHOW_RULES: 1365 if (rule.label[0][0] && (opts & PF_OPT_SHOWALL)) 1366 labels = 1; 1367 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1368 print_rule(&rule, anchor_call, rule_numbers, numeric); 1369 1370 /* 1371 * If this is a 'unnamed' brace notation 1372 * anchor, OR the user has explicitly requested 1373 * recursion, print it recursively. 1374 */ 1375 if (anchor_call[0] && 1376 (((p = strrchr(anchor_call, '/')) ? 1377 p[1] == '_' : anchor_call[0] == '_') || 1378 opts & PF_OPT_RECURSE)) { 1379 printf(" {\n"); 1380 pfctl_print_rule_counters(&rule, opts); 1381 pfctl_show_rules(dev, npath, opts, format, 1382 anchor_call, depth + 1, 1383 rule.anchor_wildcard); 1384 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1385 printf("}\n"); 1386 } else { 1387 printf("\n"); 1388 pfctl_print_rule_counters(&rule, opts); 1389 } 1390 break; 1391 case PFCTL_SHOW_NOTHING: 1392 break; 1393 } 1394 pfctl_clear_pool(&rule.rpool); 1395 } 1396 1397 error: 1398 path[len] = '\0'; 1399 return (ret); 1400 } 1401 1402 int 1403 pfctl_show_nat(int dev, char *path, int opts, char *anchorname, int depth) 1404 { 1405 struct pfctl_rules_info ri; 1406 struct pfctl_rule rule; 1407 char anchor_call[MAXPATHLEN]; 1408 u_int32_t nr; 1409 static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT }; 1410 int i, dotitle = opts & PF_OPT_SHOWALL; 1411 int brace, ret; 1412 int len = strlen(path); 1413 char *p; 1414 1415 if (path[0]) 1416 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname); 1417 else 1418 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname); 1419 1420 for (i = 0; i < 3; i++) { 1421 ret = pfctl_get_rules_info(dev, &ri, nattype[i], path); 1422 if (ret != 0) { 1423 warn("DIOCGETRULES"); 1424 return (-1); 1425 } 1426 for (nr = 0; nr < ri.nr; ++nr) { 1427 brace = 0; 1428 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1429 1430 if (pfctl_get_rule(dev, nr, ri.ticket, path, 1431 nattype[i], &rule, anchor_call)) { 1432 warn("DIOCGETRULE"); 1433 return (-1); 1434 } 1435 if (pfctl_get_pool(dev, &rule.rpool, nr, 1436 ri.ticket, nattype[i], path) != 0) 1437 return (-1); 1438 1439 if (anchor_call[0] && 1440 ((((p = strrchr(anchor_call, '_')) != NULL) && 1441 (p == anchor_call || 1442 *(--p) == '/')) || (opts & PF_OPT_RECURSE))) { 1443 brace++; 1444 if ((p = strrchr(anchor_call, '/')) != 1445 NULL) 1446 p++; 1447 else 1448 p = &anchor_call[0]; 1449 } else 1450 p = &anchor_call[0]; 1451 1452 if (dotitle) { 1453 pfctl_print_title("TRANSLATION RULES:"); 1454 dotitle = 0; 1455 } 1456 print_rule(&rule, anchor_call, 1457 opts & PF_OPT_VERBOSE2, opts & PF_OPT_NUMERIC); 1458 if (brace) 1459 printf(" {\n"); 1460 else 1461 printf("\n"); 1462 pfctl_print_rule_counters(&rule, opts); 1463 pfctl_clear_pool(&rule.rpool); 1464 if (brace) { 1465 pfctl_show_nat(dev, path, opts, p, depth + 1); 1466 INDENT(depth, !(opts & PF_OPT_VERBOSE)); 1467 printf("}\n"); 1468 } 1469 } 1470 } 1471 return (0); 1472 } 1473 1474 int 1475 pfctl_show_src_nodes(int dev, int opts) 1476 { 1477 struct pfioc_src_nodes psn; 1478 struct pf_src_node *p; 1479 char *inbuf = NULL, *newinbuf = NULL; 1480 unsigned int len = 0; 1481 int i; 1482 1483 memset(&psn, 0, sizeof(psn)); 1484 for (;;) { 1485 psn.psn_len = len; 1486 if (len) { 1487 newinbuf = realloc(inbuf, len); 1488 if (newinbuf == NULL) 1489 err(1, "realloc"); 1490 psn.psn_buf = inbuf = newinbuf; 1491 } 1492 if (ioctl(dev, DIOCGETSRCNODES, &psn) < 0) { 1493 warn("DIOCGETSRCNODES"); 1494 free(inbuf); 1495 return (-1); 1496 } 1497 if (psn.psn_len + sizeof(struct pfioc_src_nodes) < len) 1498 break; 1499 if (len == 0 && psn.psn_len == 0) 1500 goto done; 1501 if (len == 0 && psn.psn_len != 0) 1502 len = psn.psn_len; 1503 if (psn.psn_len == 0) 1504 goto done; /* no src_nodes */ 1505 len *= 2; 1506 } 1507 p = psn.psn_src_nodes; 1508 if (psn.psn_len > 0 && (opts & PF_OPT_SHOWALL)) 1509 pfctl_print_title("SOURCE TRACKING NODES:"); 1510 for (i = 0; i < psn.psn_len; i += sizeof(*p)) { 1511 print_src_node(p, opts); 1512 p++; 1513 } 1514 done: 1515 free(inbuf); 1516 return (0); 1517 } 1518 1519 int 1520 pfctl_show_states(int dev, const char *iface, int opts) 1521 { 1522 struct pfctl_states states; 1523 struct pfctl_state *s; 1524 int dotitle = (opts & PF_OPT_SHOWALL); 1525 1526 memset(&states, 0, sizeof(states)); 1527 1528 if (pfctl_get_states(dev, &states)) 1529 return (-1); 1530 1531 TAILQ_FOREACH(s, &states.states, entry) { 1532 if (iface != NULL && strcmp(s->ifname, iface)) 1533 continue; 1534 if (dotitle) { 1535 pfctl_print_title("STATES:"); 1536 dotitle = 0; 1537 } 1538 print_state(s, opts); 1539 } 1540 1541 pfctl_free_states(&states); 1542 1543 return (0); 1544 } 1545 1546 int 1547 pfctl_show_status(int dev, int opts) 1548 { 1549 struct pfctl_status *status; 1550 struct pfctl_syncookies cookies; 1551 1552 if ((status = pfctl_get_status(dev)) == NULL) { 1553 warn("DIOCGETSTATUS"); 1554 return (-1); 1555 } 1556 if (pfctl_get_syncookies(dev, &cookies)) { 1557 pfctl_free_status(status); 1558 warn("DIOCGETSYNCOOKIES"); 1559 return (-1); 1560 } 1561 if (opts & PF_OPT_SHOWALL) 1562 pfctl_print_title("INFO:"); 1563 print_status(status, &cookies, opts); 1564 pfctl_free_status(status); 1565 return (0); 1566 } 1567 1568 int 1569 pfctl_show_running(int dev) 1570 { 1571 struct pfctl_status *status; 1572 int running; 1573 1574 if ((status = pfctl_get_status(dev)) == NULL) { 1575 warn("DIOCGETSTATUS"); 1576 return (-1); 1577 } 1578 1579 running = status->running; 1580 1581 print_running(status); 1582 pfctl_free_status(status); 1583 return (!running); 1584 } 1585 1586 int 1587 pfctl_show_timeouts(int dev, int opts) 1588 { 1589 struct pfioc_tm pt; 1590 int i; 1591 1592 if (opts & PF_OPT_SHOWALL) 1593 pfctl_print_title("TIMEOUTS:"); 1594 memset(&pt, 0, sizeof(pt)); 1595 for (i = 0; pf_timeouts[i].name; i++) { 1596 pt.timeout = pf_timeouts[i].timeout; 1597 if (ioctl(dev, DIOCGETTIMEOUT, &pt)) 1598 err(1, "DIOCGETTIMEOUT"); 1599 printf("%-20s %10d", pf_timeouts[i].name, pt.seconds); 1600 if (pf_timeouts[i].timeout >= PFTM_ADAPTIVE_START && 1601 pf_timeouts[i].timeout <= PFTM_ADAPTIVE_END) 1602 printf(" states"); 1603 else 1604 printf("s"); 1605 printf("\n"); 1606 } 1607 return (0); 1608 1609 } 1610 1611 int 1612 pfctl_show_limits(int dev, int opts) 1613 { 1614 struct pfioc_limit pl; 1615 int i; 1616 1617 if (opts & PF_OPT_SHOWALL) 1618 pfctl_print_title("LIMITS:"); 1619 memset(&pl, 0, sizeof(pl)); 1620 for (i = 0; pf_limits[i].name; i++) { 1621 pl.index = pf_limits[i].index; 1622 if (ioctl(dev, DIOCGETLIMIT, &pl)) 1623 err(1, "DIOCGETLIMIT"); 1624 printf("%-13s ", pf_limits[i].name); 1625 if (pl.limit == UINT_MAX) 1626 printf("unlimited\n"); 1627 else 1628 printf("hard limit %8u\n", pl.limit); 1629 } 1630 return (0); 1631 } 1632 1633 /* callbacks for rule/nat/rdr/addr */ 1634 int 1635 pfctl_add_pool(struct pfctl *pf, struct pfctl_pool *p, sa_family_t af) 1636 { 1637 struct pf_pooladdr *pa; 1638 1639 if ((pf->opts & PF_OPT_NOACTION) == 0) { 1640 if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr)) 1641 err(1, "DIOCBEGINADDRS"); 1642 } 1643 1644 pf->paddr.af = af; 1645 TAILQ_FOREACH(pa, &p->list, entries) { 1646 memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr)); 1647 if ((pf->opts & PF_OPT_NOACTION) == 0) { 1648 if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr)) 1649 err(1, "DIOCADDADDR"); 1650 } 1651 } 1652 return (0); 1653 } 1654 1655 int 1656 pfctl_append_rule(struct pfctl *pf, struct pfctl_rule *r, 1657 const char *anchor_call) 1658 { 1659 u_int8_t rs_num; 1660 struct pfctl_rule *rule; 1661 struct pfctl_ruleset *rs; 1662 char *p; 1663 1664 rs_num = pf_get_ruleset_number(r->action); 1665 if (rs_num == PF_RULESET_MAX) 1666 errx(1, "Invalid rule type %d", r->action); 1667 1668 rs = &pf->anchor->ruleset; 1669 1670 if (anchor_call[0] && r->anchor == NULL) { 1671 /* 1672 * Don't make non-brace anchors part of the main anchor pool. 1673 */ 1674 if ((r->anchor = calloc(1, sizeof(*r->anchor))) == NULL) 1675 err(1, "pfctl_append_rule: calloc"); 1676 1677 pf_init_ruleset(&r->anchor->ruleset); 1678 r->anchor->ruleset.anchor = r->anchor; 1679 if (strlcpy(r->anchor->path, anchor_call, 1680 sizeof(rule->anchor->path)) >= sizeof(rule->anchor->path)) 1681 errx(1, "pfctl_append_rule: strlcpy"); 1682 if ((p = strrchr(anchor_call, '/')) != NULL) { 1683 if (!strlen(p)) 1684 err(1, "pfctl_append_rule: bad anchor name %s", 1685 anchor_call); 1686 } else 1687 p = (char *)anchor_call; 1688 if (strlcpy(r->anchor->name, p, 1689 sizeof(rule->anchor->name)) >= sizeof(rule->anchor->name)) 1690 errx(1, "pfctl_append_rule: strlcpy"); 1691 } 1692 1693 if ((rule = calloc(1, sizeof(*rule))) == NULL) 1694 err(1, "calloc"); 1695 bcopy(r, rule, sizeof(*rule)); 1696 TAILQ_INIT(&rule->rpool.list); 1697 pfctl_move_pool(&r->rpool, &rule->rpool); 1698 1699 TAILQ_INSERT_TAIL(rs->rules[rs_num].active.ptr, rule, entries); 1700 return (0); 1701 } 1702 1703 int 1704 pfctl_append_eth_rule(struct pfctl *pf, struct pfctl_eth_rule *r, 1705 const char *anchor_call) 1706 { 1707 struct pfctl_eth_rule *rule; 1708 struct pfctl_eth_ruleset *rs; 1709 char *p; 1710 1711 rs = &pf->eanchor->ruleset; 1712 1713 if (anchor_call[0] && r->anchor == NULL) { 1714 /* 1715 * Don't make non-brace anchors part of the main anchor pool. 1716 */ 1717 if ((r->anchor = calloc(1, sizeof(*r->anchor))) == NULL) 1718 err(1, "pfctl_append_rule: calloc"); 1719 1720 pf_init_eth_ruleset(&r->anchor->ruleset); 1721 r->anchor->ruleset.anchor = r->anchor; 1722 if (strlcpy(r->anchor->path, anchor_call, 1723 sizeof(rule->anchor->path)) >= sizeof(rule->anchor->path)) 1724 errx(1, "pfctl_append_rule: strlcpy"); 1725 if ((p = strrchr(anchor_call, '/')) != NULL) { 1726 if (!strlen(p)) 1727 err(1, "pfctl_append_eth_rule: bad anchor name %s", 1728 anchor_call); 1729 } else 1730 p = (char *)anchor_call; 1731 if (strlcpy(r->anchor->name, p, 1732 sizeof(rule->anchor->name)) >= sizeof(rule->anchor->name)) 1733 errx(1, "pfctl_append_eth_rule: strlcpy"); 1734 } 1735 1736 if ((rule = calloc(1, sizeof(*rule))) == NULL) 1737 err(1, "calloc"); 1738 bcopy(r, rule, sizeof(*rule)); 1739 1740 TAILQ_INSERT_TAIL(&rs->rules, rule, entries); 1741 return (0); 1742 } 1743 1744 int 1745 pfctl_eth_ruleset_trans(struct pfctl *pf, char *path, 1746 struct pfctl_eth_anchor *a) 1747 { 1748 int osize = pf->trans->pfrb_size; 1749 1750 if ((pf->loadopt & PFCTL_FLAG_ETH) != 0) { 1751 if (pfctl_add_trans(pf->trans, PF_RULESET_ETH, path)) 1752 return (1); 1753 } 1754 if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize)) 1755 return (5); 1756 1757 return (0); 1758 } 1759 1760 int 1761 pfctl_ruleset_trans(struct pfctl *pf, char *path, struct pfctl_anchor *a, bool do_eth) 1762 { 1763 int osize = pf->trans->pfrb_size; 1764 1765 if ((pf->loadopt & PFCTL_FLAG_ETH) != 0 && do_eth) { 1766 if (pfctl_add_trans(pf->trans, PF_RULESET_ETH, path)) 1767 return (1); 1768 } 1769 if ((pf->loadopt & PFCTL_FLAG_NAT) != 0) { 1770 if (pfctl_add_trans(pf->trans, PF_RULESET_NAT, path) || 1771 pfctl_add_trans(pf->trans, PF_RULESET_BINAT, path) || 1772 pfctl_add_trans(pf->trans, PF_RULESET_RDR, path)) 1773 return (1); 1774 } 1775 if (a == pf->astack[0] && ((altqsupport && 1776 (pf->loadopt & PFCTL_FLAG_ALTQ) != 0))) { 1777 if (pfctl_add_trans(pf->trans, PF_RULESET_ALTQ, path)) 1778 return (2); 1779 } 1780 if ((pf->loadopt & PFCTL_FLAG_FILTER) != 0) { 1781 if (pfctl_add_trans(pf->trans, PF_RULESET_SCRUB, path) || 1782 pfctl_add_trans(pf->trans, PF_RULESET_FILTER, path)) 1783 return (3); 1784 } 1785 if (pf->loadopt & PFCTL_FLAG_TABLE) 1786 if (pfctl_add_trans(pf->trans, PF_RULESET_TABLE, path)) 1787 return (4); 1788 if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize)) 1789 return (5); 1790 1791 return (0); 1792 } 1793 1794 int 1795 pfctl_load_eth_ruleset(struct pfctl *pf, char *path, 1796 struct pfctl_eth_ruleset *rs, int depth) 1797 { 1798 struct pfctl_eth_rule *r; 1799 int error, len = strlen(path); 1800 int brace = 0; 1801 1802 pf->eanchor = rs->anchor; 1803 if (path[0]) 1804 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->eanchor->name); 1805 else 1806 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->eanchor->name); 1807 1808 if (depth) { 1809 if (TAILQ_FIRST(&rs->rules) != NULL) { 1810 brace++; 1811 if (pf->opts & PF_OPT_VERBOSE) 1812 printf(" {\n"); 1813 if ((pf->opts & PF_OPT_NOACTION) == 0 && 1814 (error = pfctl_eth_ruleset_trans(pf, 1815 path, rs->anchor))) { 1816 printf("pfctl_load_eth_rulesets: " 1817 "pfctl_eth_ruleset_trans %d\n", error); 1818 goto error; 1819 } 1820 } else if (pf->opts & PF_OPT_VERBOSE) 1821 printf("\n"); 1822 } 1823 1824 while ((r = TAILQ_FIRST(&rs->rules)) != NULL) { 1825 TAILQ_REMOVE(&rs->rules, r, entries); 1826 1827 error = pfctl_load_eth_rule(pf, path, r, depth); 1828 if (error) 1829 return (error); 1830 1831 if (r->anchor) { 1832 if ((error = pfctl_load_eth_ruleset(pf, path, 1833 &r->anchor->ruleset, depth + 1))) 1834 return (error); 1835 } else if (pf->opts & PF_OPT_VERBOSE) 1836 printf("\n"); 1837 free(r); 1838 } 1839 if (brace && pf->opts & PF_OPT_VERBOSE) { 1840 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE)); 1841 printf("}\n"); 1842 } 1843 path[len] = '\0'; 1844 1845 return (0); 1846 error: 1847 path[len] = '\0'; 1848 return (error); 1849 } 1850 1851 int 1852 pfctl_load_eth_rule(struct pfctl *pf, char *path, struct pfctl_eth_rule *r, 1853 int depth) 1854 { 1855 char *name; 1856 char anchor[PF_ANCHOR_NAME_SIZE]; 1857 int len = strlen(path); 1858 1859 if (strlcpy(anchor, path, sizeof(anchor)) >= sizeof(anchor)) 1860 errx(1, "pfctl_load_eth_rule: strlcpy"); 1861 1862 if (r->anchor) { 1863 if (r->anchor->match) { 1864 if (path[0]) 1865 snprintf(&path[len], MAXPATHLEN - len, 1866 "/%s", r->anchor->name); 1867 else 1868 snprintf(&path[len], MAXPATHLEN - len, 1869 "%s", r->anchor->name); 1870 name = r->anchor->name; 1871 } else 1872 name = r->anchor->path; 1873 } else 1874 name = ""; 1875 1876 if ((pf->opts & PF_OPT_NOACTION) == 0) 1877 if (pfctl_add_eth_rule(pf->dev, r, anchor, name, 1878 pf->eth_ticket)) 1879 err(1, "DIOCADDETHRULENV"); 1880 1881 if (pf->opts & PF_OPT_VERBOSE) { 1882 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2)); 1883 print_eth_rule(r, r->anchor ? r->anchor->name : "", 1884 pf->opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG)); 1885 } 1886 1887 path[len] = '\0'; 1888 1889 return (0); 1890 } 1891 1892 int 1893 pfctl_load_ruleset(struct pfctl *pf, char *path, struct pfctl_ruleset *rs, 1894 int rs_num, int depth) 1895 { 1896 struct pfctl_rule *r; 1897 int error, len = strlen(path); 1898 int brace = 0; 1899 1900 pf->anchor = rs->anchor; 1901 1902 if (path[0]) 1903 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->anchor->name); 1904 else 1905 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->anchor->name); 1906 1907 if (depth) { 1908 if (TAILQ_FIRST(rs->rules[rs_num].active.ptr) != NULL) { 1909 brace++; 1910 if (pf->opts & PF_OPT_VERBOSE) 1911 printf(" {\n"); 1912 if ((pf->opts & PF_OPT_NOACTION) == 0 && 1913 (error = pfctl_ruleset_trans(pf, 1914 path, rs->anchor, false))) { 1915 printf("pfctl_load_rulesets: " 1916 "pfctl_ruleset_trans %d\n", error); 1917 goto error; 1918 } 1919 } else if (pf->opts & PF_OPT_VERBOSE) 1920 printf("\n"); 1921 1922 } 1923 1924 if (pf->optimize && rs_num == PF_RULESET_FILTER) 1925 pfctl_optimize_ruleset(pf, rs); 1926 1927 while ((r = TAILQ_FIRST(rs->rules[rs_num].active.ptr)) != NULL) { 1928 TAILQ_REMOVE(rs->rules[rs_num].active.ptr, r, entries); 1929 1930 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++) 1931 expand_label(r->label[i], PF_RULE_LABEL_SIZE, r); 1932 expand_label(r->tagname, PF_TAG_NAME_SIZE, r); 1933 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r); 1934 1935 if ((error = pfctl_load_rule(pf, path, r, depth))) 1936 goto error; 1937 if (r->anchor) { 1938 if ((error = pfctl_load_ruleset(pf, path, 1939 &r->anchor->ruleset, rs_num, depth + 1))) 1940 goto error; 1941 } else if (pf->opts & PF_OPT_VERBOSE) 1942 printf("\n"); 1943 free(r); 1944 } 1945 if (brace && pf->opts & PF_OPT_VERBOSE) { 1946 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE)); 1947 printf("}\n"); 1948 } 1949 path[len] = '\0'; 1950 return (0); 1951 1952 error: 1953 path[len] = '\0'; 1954 return (error); 1955 1956 } 1957 1958 int 1959 pfctl_load_rule(struct pfctl *pf, char *path, struct pfctl_rule *r, int depth) 1960 { 1961 u_int8_t rs_num = pf_get_ruleset_number(r->action); 1962 char *name; 1963 u_int32_t ticket; 1964 char anchor[PF_ANCHOR_NAME_SIZE]; 1965 int len = strlen(path); 1966 int error; 1967 bool was_present; 1968 1969 /* set up anchor before adding to path for anchor_call */ 1970 if ((pf->opts & PF_OPT_NOACTION) == 0) 1971 ticket = pfctl_get_ticket(pf->trans, rs_num, path); 1972 if (strlcpy(anchor, path, sizeof(anchor)) >= sizeof(anchor)) 1973 errx(1, "pfctl_load_rule: strlcpy"); 1974 1975 if (r->anchor) { 1976 if (r->anchor->match) { 1977 if (path[0]) 1978 snprintf(&path[len], MAXPATHLEN - len, 1979 "/%s", r->anchor->name); 1980 else 1981 snprintf(&path[len], MAXPATHLEN - len, 1982 "%s", r->anchor->name); 1983 name = r->anchor->name; 1984 } else 1985 name = r->anchor->path; 1986 } else 1987 name = ""; 1988 1989 was_present = false; 1990 if ((pf->opts & PF_OPT_NOACTION) == 0) { 1991 if (pfctl_add_pool(pf, &r->rpool, r->af)) 1992 return (1); 1993 error = pfctl_add_rule(pf->dev, r, anchor, name, ticket, 1994 pf->paddr.ticket); 1995 switch (error) { 1996 case 0: 1997 /* things worked, do nothing */ 1998 break; 1999 case EEXIST: 2000 /* an identical rule is already present */ 2001 was_present = true; 2002 break; 2003 default: 2004 err(1, "DIOCADDRULENV"); 2005 } 2006 } 2007 2008 if (pf->opts & PF_OPT_VERBOSE) { 2009 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2)); 2010 print_rule(r, name, 2011 pf->opts & PF_OPT_VERBOSE2, 2012 pf->opts & PF_OPT_NUMERIC); 2013 if (was_present) 2014 printf(" -- rule was already present"); 2015 } 2016 path[len] = '\0'; 2017 pfctl_clear_pool(&r->rpool); 2018 return (0); 2019 } 2020 2021 int 2022 pfctl_add_altq(struct pfctl *pf, struct pf_altq *a) 2023 { 2024 if (altqsupport && 2025 (loadopt & PFCTL_FLAG_ALTQ) != 0) { 2026 memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq)); 2027 if ((pf->opts & PF_OPT_NOACTION) == 0) { 2028 if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) { 2029 if (errno == ENXIO) 2030 errx(1, "qtype not configured"); 2031 else if (errno == ENODEV) 2032 errx(1, "%s: driver does not support " 2033 "altq", a->ifname); 2034 else 2035 err(1, "DIOCADDALTQ"); 2036 } 2037 } 2038 pfaltq_store(&pf->paltq->altq); 2039 } 2040 return (0); 2041 } 2042 2043 int 2044 pfctl_rules(int dev, char *filename, int opts, int optimize, 2045 char *anchorname, struct pfr_buffer *trans) 2046 { 2047 #define ERR(x) do { warn(x); goto _error; } while(0) 2048 #define ERRX(x) do { warnx(x); goto _error; } while(0) 2049 2050 struct pfr_buffer *t, buf; 2051 struct pfioc_altq pa; 2052 struct pfctl pf; 2053 struct pfctl_ruleset *rs; 2054 struct pfctl_eth_ruleset *ethrs; 2055 struct pfr_table trs; 2056 char *path; 2057 int osize; 2058 2059 RB_INIT(&pf_anchors); 2060 memset(&pf_main_anchor, 0, sizeof(pf_main_anchor)); 2061 pf_init_ruleset(&pf_main_anchor.ruleset); 2062 pf_main_anchor.ruleset.anchor = &pf_main_anchor; 2063 2064 memset(&pf_eth_main_anchor, 0, sizeof(pf_eth_main_anchor)); 2065 pf_init_eth_ruleset(&pf_eth_main_anchor.ruleset); 2066 pf_eth_main_anchor.ruleset.anchor = &pf_eth_main_anchor; 2067 2068 if (trans == NULL) { 2069 bzero(&buf, sizeof(buf)); 2070 buf.pfrb_type = PFRB_TRANS; 2071 t = &buf; 2072 osize = 0; 2073 } else { 2074 t = trans; 2075 osize = t->pfrb_size; 2076 } 2077 2078 memset(&pa, 0, sizeof(pa)); 2079 pa.version = PFIOC_ALTQ_VERSION; 2080 memset(&pf, 0, sizeof(pf)); 2081 memset(&trs, 0, sizeof(trs)); 2082 if ((path = calloc(1, MAXPATHLEN)) == NULL) 2083 ERRX("pfctl_rules: calloc"); 2084 if (strlcpy(trs.pfrt_anchor, anchorname, 2085 sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor)) 2086 ERRX("pfctl_rules: strlcpy"); 2087 pf.dev = dev; 2088 pf.opts = opts; 2089 pf.optimize = optimize; 2090 pf.loadopt = loadopt; 2091 2092 /* non-brace anchor, create without resolving the path */ 2093 if ((pf.anchor = calloc(1, sizeof(*pf.anchor))) == NULL) 2094 ERRX("pfctl_rules: calloc"); 2095 rs = &pf.anchor->ruleset; 2096 pf_init_ruleset(rs); 2097 rs->anchor = pf.anchor; 2098 if (strlcpy(pf.anchor->path, anchorname, 2099 sizeof(pf.anchor->path)) >= sizeof(pf.anchor->path)) 2100 errx(1, "pfctl_rules: strlcpy"); 2101 if (strlcpy(pf.anchor->name, anchorname, 2102 sizeof(pf.anchor->name)) >= sizeof(pf.anchor->name)) 2103 errx(1, "pfctl_rules: strlcpy"); 2104 2105 2106 pf.astack[0] = pf.anchor; 2107 pf.asd = 0; 2108 if (anchorname[0]) 2109 pf.loadopt &= ~PFCTL_FLAG_ALTQ; 2110 pf.paltq = &pa; 2111 pf.trans = t; 2112 pfctl_init_options(&pf); 2113 2114 /* Set up ethernet anchor */ 2115 if ((pf.eanchor = calloc(1, sizeof(*pf.eanchor))) == NULL) 2116 ERRX("pfctl_rules: calloc"); 2117 2118 if (strlcpy(pf.eanchor->path, anchorname, 2119 sizeof(pf.eanchor->path)) >= sizeof(pf.eanchor->path)) 2120 errx(1, "pfctl_rules: strlcpy"); 2121 if (strlcpy(pf.eanchor->name, anchorname, 2122 sizeof(pf.eanchor->name)) >= sizeof(pf.eanchor->name)) 2123 errx(1, "pfctl_rules: strlcpy"); 2124 2125 ethrs = &pf.eanchor->ruleset; 2126 pf_init_eth_ruleset(ethrs); 2127 ethrs->anchor = pf.eanchor; 2128 pf.eastack[0] = pf.eanchor; 2129 2130 if ((opts & PF_OPT_NOACTION) == 0) { 2131 /* 2132 * XXX For the time being we need to open transactions for 2133 * the main ruleset before parsing, because tables are still 2134 * loaded at parse time. 2135 */ 2136 if (pfctl_ruleset_trans(&pf, anchorname, pf.anchor, true)) 2137 ERRX("pfctl_rules"); 2138 if (pf.loadopt & PFCTL_FLAG_ETH) 2139 pf.eth_ticket = pfctl_get_ticket(t, PF_RULESET_ETH, anchorname); 2140 if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ)) 2141 pa.ticket = 2142 pfctl_get_ticket(t, PF_RULESET_ALTQ, anchorname); 2143 if (pf.loadopt & PFCTL_FLAG_TABLE) 2144 pf.astack[0]->ruleset.tticket = 2145 pfctl_get_ticket(t, PF_RULESET_TABLE, anchorname); 2146 } 2147 2148 if (parse_config(filename, &pf) < 0) { 2149 if ((opts & PF_OPT_NOACTION) == 0) 2150 ERRX("Syntax error in config file: " 2151 "pf rules not loaded"); 2152 else 2153 goto _error; 2154 } 2155 if (loadopt & PFCTL_FLAG_OPTION) 2156 pfctl_adjust_skip_ifaces(&pf); 2157 2158 if ((pf.loadopt & PFCTL_FLAG_FILTER && 2159 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_SCRUB, 0))) || 2160 (pf.loadopt & PFCTL_FLAG_ETH && 2161 (pfctl_load_eth_ruleset(&pf, path, ethrs, 0))) || 2162 (pf.loadopt & PFCTL_FLAG_NAT && 2163 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_NAT, 0) || 2164 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_RDR, 0) || 2165 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_BINAT, 0))) || 2166 (pf.loadopt & PFCTL_FLAG_FILTER && 2167 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_FILTER, 0))) { 2168 if ((opts & PF_OPT_NOACTION) == 0) 2169 ERRX("Unable to load rules into kernel"); 2170 else 2171 goto _error; 2172 } 2173 2174 if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0)) 2175 if (check_commit_altq(dev, opts) != 0) 2176 ERRX("errors in altq config"); 2177 2178 /* process "load anchor" directives */ 2179 if (!anchorname[0]) 2180 if (pfctl_load_anchors(dev, &pf, t) == -1) 2181 ERRX("load anchors"); 2182 2183 if (trans == NULL && (opts & PF_OPT_NOACTION) == 0) { 2184 if (!anchorname[0]) 2185 if (pfctl_load_options(&pf)) 2186 goto _error; 2187 if (pfctl_trans(dev, t, DIOCXCOMMIT, osize)) 2188 ERR("DIOCXCOMMIT"); 2189 } 2190 free(path); 2191 return (0); 2192 2193 _error: 2194 if (trans == NULL) { /* main ruleset */ 2195 if ((opts & PF_OPT_NOACTION) == 0) 2196 if (pfctl_trans(dev, t, DIOCXROLLBACK, osize)) 2197 err(1, "DIOCXROLLBACK"); 2198 exit(1); 2199 } else { /* sub ruleset */ 2200 free(path); 2201 return (-1); 2202 } 2203 2204 #undef ERR 2205 #undef ERRX 2206 } 2207 2208 FILE * 2209 pfctl_fopen(const char *name, const char *mode) 2210 { 2211 struct stat st; 2212 FILE *fp; 2213 2214 fp = fopen(name, mode); 2215 if (fp == NULL) 2216 return (NULL); 2217 if (fstat(fileno(fp), &st)) { 2218 fclose(fp); 2219 return (NULL); 2220 } 2221 if (S_ISDIR(st.st_mode)) { 2222 fclose(fp); 2223 errno = EISDIR; 2224 return (NULL); 2225 } 2226 return (fp); 2227 } 2228 2229 void 2230 pfctl_init_options(struct pfctl *pf) 2231 { 2232 2233 pf->timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL; 2234 pf->timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL; 2235 pf->timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL; 2236 pf->timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL; 2237 pf->timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL; 2238 pf->timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL; 2239 pf->timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL; 2240 pf->timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL; 2241 pf->timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL; 2242 pf->timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL; 2243 pf->timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL; 2244 pf->timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL; 2245 pf->timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL; 2246 pf->timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL; 2247 pf->timeout[PFTM_FRAG] = PFTM_FRAG_VAL; 2248 pf->timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL; 2249 pf->timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL; 2250 pf->timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL; 2251 pf->timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START; 2252 pf->timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END; 2253 2254 pf->limit[PF_LIMIT_STATES] = PFSTATE_HIWAT; 2255 pf->limit[PF_LIMIT_FRAGS] = PFFRAG_FRENT_HIWAT; 2256 pf->limit[PF_LIMIT_SRC_NODES] = PFSNODE_HIWAT; 2257 pf->limit[PF_LIMIT_TABLE_ENTRIES] = PFR_KENTRY_HIWAT; 2258 2259 pf->debug = PF_DEBUG_URGENT; 2260 pf->reassemble = 0; 2261 2262 pf->syncookies = false; 2263 pf->syncookieswat[0] = PF_SYNCOOKIES_LOWATPCT; 2264 pf->syncookieswat[1] = PF_SYNCOOKIES_HIWATPCT; 2265 } 2266 2267 int 2268 pfctl_load_options(struct pfctl *pf) 2269 { 2270 int i, error = 0; 2271 2272 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2273 return (0); 2274 2275 /* load limits */ 2276 for (i = 0; i < PF_LIMIT_MAX; i++) { 2277 if ((pf->opts & PF_OPT_MERGE) && !pf->limit_set[i]) 2278 continue; 2279 if (pfctl_load_limit(pf, i, pf->limit[i])) 2280 error = 1; 2281 } 2282 2283 /* 2284 * If we've set the limit, but haven't explicitly set adaptive 2285 * timeouts, do it now with a start of 60% and end of 120%. 2286 */ 2287 if (pf->limit_set[PF_LIMIT_STATES] && 2288 !pf->timeout_set[PFTM_ADAPTIVE_START] && 2289 !pf->timeout_set[PFTM_ADAPTIVE_END]) { 2290 pf->timeout[PFTM_ADAPTIVE_START] = 2291 (pf->limit[PF_LIMIT_STATES] / 10) * 6; 2292 pf->timeout_set[PFTM_ADAPTIVE_START] = 1; 2293 pf->timeout[PFTM_ADAPTIVE_END] = 2294 (pf->limit[PF_LIMIT_STATES] / 10) * 12; 2295 pf->timeout_set[PFTM_ADAPTIVE_END] = 1; 2296 } 2297 2298 /* load timeouts */ 2299 for (i = 0; i < PFTM_MAX; i++) { 2300 if ((pf->opts & PF_OPT_MERGE) && !pf->timeout_set[i]) 2301 continue; 2302 if (pfctl_load_timeout(pf, i, pf->timeout[i])) 2303 error = 1; 2304 } 2305 2306 /* load debug */ 2307 if (!(pf->opts & PF_OPT_MERGE) || pf->debug_set) 2308 if (pfctl_load_debug(pf, pf->debug)) 2309 error = 1; 2310 2311 /* load logif */ 2312 if (!(pf->opts & PF_OPT_MERGE) || pf->ifname_set) 2313 if (pfctl_load_logif(pf, pf->ifname)) 2314 error = 1; 2315 2316 /* load hostid */ 2317 if (!(pf->opts & PF_OPT_MERGE) || pf->hostid_set) 2318 if (pfctl_load_hostid(pf, pf->hostid)) 2319 error = 1; 2320 2321 /* load reassembly settings */ 2322 if (!(pf->opts & PF_OPT_MERGE) || pf->reass_set) 2323 if (pfctl_load_reassembly(pf, pf->reassemble)) 2324 error = 1; 2325 2326 /* load keepcounters */ 2327 if (pfctl_set_keepcounters(pf->dev, pf->keep_counters)) 2328 error = 1; 2329 2330 /* load syncookies settings */ 2331 if (pfctl_load_syncookies(pf, pf->syncookies)) 2332 error = 1; 2333 2334 return (error); 2335 } 2336 2337 int 2338 pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit) 2339 { 2340 int i; 2341 2342 2343 for (i = 0; pf_limits[i].name; i++) { 2344 if (strcasecmp(opt, pf_limits[i].name) == 0) { 2345 pf->limit[pf_limits[i].index] = limit; 2346 pf->limit_set[pf_limits[i].index] = 1; 2347 break; 2348 } 2349 } 2350 if (pf_limits[i].name == NULL) { 2351 warnx("Bad pool name."); 2352 return (1); 2353 } 2354 2355 if (pf->opts & PF_OPT_VERBOSE) 2356 printf("set limit %s %d\n", opt, limit); 2357 2358 return (0); 2359 } 2360 2361 int 2362 pfctl_load_limit(struct pfctl *pf, unsigned int index, unsigned int limit) 2363 { 2364 struct pfioc_limit pl; 2365 2366 memset(&pl, 0, sizeof(pl)); 2367 pl.index = index; 2368 pl.limit = limit; 2369 if (ioctl(pf->dev, DIOCSETLIMIT, &pl)) { 2370 if (errno == EBUSY) 2371 warnx("Current pool size exceeds requested hard limit"); 2372 else 2373 warnx("DIOCSETLIMIT"); 2374 return (1); 2375 } 2376 return (0); 2377 } 2378 2379 int 2380 pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet) 2381 { 2382 int i; 2383 2384 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2385 return (0); 2386 2387 for (i = 0; pf_timeouts[i].name; i++) { 2388 if (strcasecmp(opt, pf_timeouts[i].name) == 0) { 2389 pf->timeout[pf_timeouts[i].timeout] = seconds; 2390 pf->timeout_set[pf_timeouts[i].timeout] = 1; 2391 break; 2392 } 2393 } 2394 2395 if (pf_timeouts[i].name == NULL) { 2396 warnx("Bad timeout name."); 2397 return (1); 2398 } 2399 2400 2401 if (pf->opts & PF_OPT_VERBOSE && ! quiet) 2402 printf("set timeout %s %d\n", opt, seconds); 2403 2404 return (0); 2405 } 2406 2407 int 2408 pfctl_load_timeout(struct pfctl *pf, unsigned int timeout, unsigned int seconds) 2409 { 2410 struct pfioc_tm pt; 2411 2412 memset(&pt, 0, sizeof(pt)); 2413 pt.timeout = timeout; 2414 pt.seconds = seconds; 2415 if (ioctl(pf->dev, DIOCSETTIMEOUT, &pt)) { 2416 warnx("DIOCSETTIMEOUT"); 2417 return (1); 2418 } 2419 return (0); 2420 } 2421 2422 int 2423 pfctl_set_reassembly(struct pfctl *pf, int on, int nodf) 2424 { 2425 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2426 return (0); 2427 2428 pf->reass_set = 1; 2429 if (on) { 2430 pf->reassemble = PF_REASS_ENABLED; 2431 if (nodf) 2432 pf->reassemble |= PF_REASS_NODF; 2433 } else { 2434 pf->reassemble = 0; 2435 } 2436 2437 if (pf->opts & PF_OPT_VERBOSE) 2438 printf("set reassemble %s %s\n", on ? "yes" : "no", 2439 nodf ? "no-df" : ""); 2440 2441 return (0); 2442 } 2443 2444 int 2445 pfctl_set_optimization(struct pfctl *pf, const char *opt) 2446 { 2447 const struct pf_hint *hint; 2448 int i, r; 2449 2450 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2451 return (0); 2452 2453 for (i = 0; pf_hints[i].name; i++) 2454 if (strcasecmp(opt, pf_hints[i].name) == 0) 2455 break; 2456 2457 hint = pf_hints[i].hint; 2458 if (hint == NULL) { 2459 warnx("invalid state timeouts optimization"); 2460 return (1); 2461 } 2462 2463 for (i = 0; hint[i].name; i++) 2464 if ((r = pfctl_set_timeout(pf, hint[i].name, 2465 hint[i].timeout, 1))) 2466 return (r); 2467 2468 if (pf->opts & PF_OPT_VERBOSE) 2469 printf("set optimization %s\n", opt); 2470 2471 return (0); 2472 } 2473 2474 int 2475 pfctl_set_logif(struct pfctl *pf, char *ifname) 2476 { 2477 2478 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2479 return (0); 2480 2481 if (!strcmp(ifname, "none")) { 2482 free(pf->ifname); 2483 pf->ifname = NULL; 2484 } else { 2485 pf->ifname = strdup(ifname); 2486 if (!pf->ifname) 2487 errx(1, "pfctl_set_logif: strdup"); 2488 } 2489 pf->ifname_set = 1; 2490 2491 if (pf->opts & PF_OPT_VERBOSE) 2492 printf("set loginterface %s\n", ifname); 2493 2494 return (0); 2495 } 2496 2497 int 2498 pfctl_load_logif(struct pfctl *pf, char *ifname) 2499 { 2500 struct pfioc_if pi; 2501 2502 memset(&pi, 0, sizeof(pi)); 2503 if (ifname && strlcpy(pi.ifname, ifname, 2504 sizeof(pi.ifname)) >= sizeof(pi.ifname)) { 2505 warnx("pfctl_load_logif: strlcpy"); 2506 return (1); 2507 } 2508 if (ioctl(pf->dev, DIOCSETSTATUSIF, &pi)) { 2509 warnx("DIOCSETSTATUSIF"); 2510 return (1); 2511 } 2512 return (0); 2513 } 2514 2515 int 2516 pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid) 2517 { 2518 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2519 return (0); 2520 2521 HTONL(hostid); 2522 2523 pf->hostid = hostid; 2524 pf->hostid_set = 1; 2525 2526 if (pf->opts & PF_OPT_VERBOSE) 2527 printf("set hostid 0x%08x\n", ntohl(hostid)); 2528 2529 return (0); 2530 } 2531 2532 int 2533 pfctl_load_hostid(struct pfctl *pf, u_int32_t hostid) 2534 { 2535 if (ioctl(dev, DIOCSETHOSTID, &hostid)) { 2536 warnx("DIOCSETHOSTID"); 2537 return (1); 2538 } 2539 return (0); 2540 } 2541 2542 int 2543 pfctl_load_reassembly(struct pfctl *pf, u_int32_t reassembly) 2544 { 2545 if (ioctl(dev, DIOCSETREASS, &reassembly)) { 2546 warnx("DIOCSETREASS"); 2547 return (1); 2548 } 2549 return (0); 2550 } 2551 2552 int 2553 pfctl_load_syncookies(struct pfctl *pf, u_int8_t val) 2554 { 2555 struct pfctl_syncookies cookies; 2556 2557 bzero(&cookies, sizeof(cookies)); 2558 2559 cookies.mode = val; 2560 cookies.lowwater = pf->syncookieswat[0]; 2561 cookies.highwater = pf->syncookieswat[1]; 2562 2563 if (pfctl_set_syncookies(dev, &cookies)) { 2564 warnx("DIOCSETSYNCOOKIES"); 2565 return (1); 2566 } 2567 return (0); 2568 } 2569 2570 int 2571 pfctl_cfg_syncookies(struct pfctl *pf, uint8_t val, struct pfctl_watermarks *w) 2572 { 2573 if (val != PF_SYNCOOKIES_ADAPTIVE && w != NULL) { 2574 warnx("syncookies start/end only apply to adaptive"); 2575 return (1); 2576 } 2577 if (val == PF_SYNCOOKIES_ADAPTIVE && w != NULL) { 2578 if (!w->hi) 2579 w->hi = PF_SYNCOOKIES_HIWATPCT; 2580 if (!w->lo) 2581 w->lo = w->hi / 2; 2582 if (w->lo >= w->hi) { 2583 warnx("start must be higher than end"); 2584 return (1); 2585 } 2586 pf->syncookieswat[0] = w->lo; 2587 pf->syncookieswat[1] = w->hi; 2588 pf->syncookieswat_set = 1; 2589 } 2590 2591 if (pf->opts & PF_OPT_VERBOSE) { 2592 if (val == PF_SYNCOOKIES_NEVER) 2593 printf("set syncookies never\n"); 2594 else if (val == PF_SYNCOOKIES_ALWAYS) 2595 printf("set syncookies always\n"); 2596 else if (val == PF_SYNCOOKIES_ADAPTIVE) { 2597 if (pf->syncookieswat_set) 2598 printf("set syncookies adaptive (start %u%%, " 2599 "end %u%%)\n", pf->syncookieswat[1], 2600 pf->syncookieswat[0]); 2601 else 2602 printf("set syncookies adaptive\n"); 2603 } else { /* cannot happen */ 2604 warnx("king bula ate all syncookies"); 2605 return (1); 2606 } 2607 } 2608 2609 pf->syncookies = val; 2610 return (0); 2611 } 2612 2613 int 2614 pfctl_set_debug(struct pfctl *pf, char *d) 2615 { 2616 u_int32_t level; 2617 2618 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2619 return (0); 2620 2621 if (!strcmp(d, "none")) 2622 pf->debug = PF_DEBUG_NONE; 2623 else if (!strcmp(d, "urgent")) 2624 pf->debug = PF_DEBUG_URGENT; 2625 else if (!strcmp(d, "misc")) 2626 pf->debug = PF_DEBUG_MISC; 2627 else if (!strcmp(d, "loud")) 2628 pf->debug = PF_DEBUG_NOISY; 2629 else { 2630 warnx("unknown debug level \"%s\"", d); 2631 return (-1); 2632 } 2633 2634 pf->debug_set = 1; 2635 level = pf->debug; 2636 2637 if ((pf->opts & PF_OPT_NOACTION) == 0) 2638 if (ioctl(dev, DIOCSETDEBUG, &level)) 2639 err(1, "DIOCSETDEBUG"); 2640 2641 if (pf->opts & PF_OPT_VERBOSE) 2642 printf("set debug %s\n", d); 2643 2644 return (0); 2645 } 2646 2647 int 2648 pfctl_load_debug(struct pfctl *pf, unsigned int level) 2649 { 2650 if (ioctl(pf->dev, DIOCSETDEBUG, &level)) { 2651 warnx("DIOCSETDEBUG"); 2652 return (1); 2653 } 2654 return (0); 2655 } 2656 2657 int 2658 pfctl_set_interface_flags(struct pfctl *pf, char *ifname, int flags, int how) 2659 { 2660 struct pfioc_iface pi; 2661 struct node_host *h = NULL, *n = NULL; 2662 2663 if ((loadopt & PFCTL_FLAG_OPTION) == 0) 2664 return (0); 2665 2666 bzero(&pi, sizeof(pi)); 2667 2668 pi.pfiio_flags = flags; 2669 2670 /* Make sure our cache matches the kernel. If we set or clear the flag 2671 * for a group this applies to all members. */ 2672 h = ifa_grouplookup(ifname, 0); 2673 for (n = h; n != NULL; n = n->next) 2674 pfctl_set_interface_flags(pf, n->ifname, flags, how); 2675 2676 if (strlcpy(pi.pfiio_name, ifname, sizeof(pi.pfiio_name)) >= 2677 sizeof(pi.pfiio_name)) 2678 errx(1, "pfctl_set_interface_flags: strlcpy"); 2679 2680 if ((pf->opts & PF_OPT_NOACTION) == 0) { 2681 if (how == 0) { 2682 if (ioctl(pf->dev, DIOCCLRIFFLAG, &pi)) 2683 err(1, "DIOCCLRIFFLAG"); 2684 } else { 2685 if (ioctl(pf->dev, DIOCSETIFFLAG, &pi)) 2686 err(1, "DIOCSETIFFLAG"); 2687 pfctl_check_skip_ifaces(ifname); 2688 } 2689 } 2690 return (0); 2691 } 2692 2693 void 2694 pfctl_debug(int dev, u_int32_t level, int opts) 2695 { 2696 if (ioctl(dev, DIOCSETDEBUG, &level)) 2697 err(1, "DIOCSETDEBUG"); 2698 if ((opts & PF_OPT_QUIET) == 0) { 2699 fprintf(stderr, "debug level set to '"); 2700 switch (level) { 2701 case PF_DEBUG_NONE: 2702 fprintf(stderr, "none"); 2703 break; 2704 case PF_DEBUG_URGENT: 2705 fprintf(stderr, "urgent"); 2706 break; 2707 case PF_DEBUG_MISC: 2708 fprintf(stderr, "misc"); 2709 break; 2710 case PF_DEBUG_NOISY: 2711 fprintf(stderr, "loud"); 2712 break; 2713 default: 2714 fprintf(stderr, "<invalid>"); 2715 break; 2716 } 2717 fprintf(stderr, "'\n"); 2718 } 2719 } 2720 2721 int 2722 pfctl_test_altqsupport(int dev, int opts) 2723 { 2724 struct pfioc_altq pa; 2725 2726 pa.version = PFIOC_ALTQ_VERSION; 2727 if (ioctl(dev, DIOCGETALTQS, &pa)) { 2728 if (errno == ENODEV) { 2729 if (opts & PF_OPT_VERBOSE) 2730 fprintf(stderr, "No ALTQ support in kernel\n" 2731 "ALTQ related functions disabled\n"); 2732 return (0); 2733 } else 2734 err(1, "DIOCGETALTQS"); 2735 } 2736 return (1); 2737 } 2738 2739 int 2740 pfctl_show_anchors(int dev, int opts, char *anchorname) 2741 { 2742 struct pfioc_ruleset pr; 2743 u_int32_t mnr, nr; 2744 2745 memset(&pr, 0, sizeof(pr)); 2746 memcpy(pr.path, anchorname, sizeof(pr.path)); 2747 if (ioctl(dev, DIOCGETRULESETS, &pr)) { 2748 if (errno == EINVAL) 2749 fprintf(stderr, "Anchor '%s' not found.\n", 2750 anchorname); 2751 else 2752 err(1, "DIOCGETRULESETS"); 2753 return (-1); 2754 } 2755 mnr = pr.nr; 2756 for (nr = 0; nr < mnr; ++nr) { 2757 char sub[MAXPATHLEN]; 2758 2759 pr.nr = nr; 2760 if (ioctl(dev, DIOCGETRULESET, &pr)) 2761 err(1, "DIOCGETRULESET"); 2762 if (!strcmp(pr.name, PF_RESERVED_ANCHOR)) 2763 continue; 2764 sub[0] = 0; 2765 if (pr.path[0]) { 2766 strlcat(sub, pr.path, sizeof(sub)); 2767 strlcat(sub, "/", sizeof(sub)); 2768 } 2769 strlcat(sub, pr.name, sizeof(sub)); 2770 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE)) 2771 printf(" %s\n", sub); 2772 if ((opts & PF_OPT_VERBOSE) && pfctl_show_anchors(dev, opts, sub)) 2773 return (-1); 2774 } 2775 return (0); 2776 } 2777 2778 int 2779 pfctl_show_eth_anchors(int dev, int opts, char *anchorname) 2780 { 2781 struct pfctl_eth_rulesets_info ri; 2782 struct pfctl_eth_ruleset_info rs; 2783 int ret; 2784 2785 if ((ret = pfctl_get_eth_rulesets_info(dev, &ri, anchorname)) != 0) { 2786 if (ret == ENOENT) 2787 fprintf(stderr, "Anchor '%s' not found.\n", 2788 anchorname); 2789 else 2790 err(1, "DIOCGETETHRULESETS"); 2791 return (-1); 2792 } 2793 2794 for (int nr = 0; nr < ri.nr; nr++) { 2795 char sub[MAXPATHLEN]; 2796 2797 if (pfctl_get_eth_ruleset(dev, anchorname, nr, &rs) != 0) 2798 err(1, "DIOCGETETHRULESET"); 2799 2800 if (!strcmp(rs.name, PF_RESERVED_ANCHOR)) 2801 continue; 2802 sub[0] = 0; 2803 if (rs.path[0]) { 2804 strlcat(sub, rs.path, sizeof(sub)); 2805 strlcat(sub, "/", sizeof(sub)); 2806 } 2807 strlcat(sub, rs.name, sizeof(sub)); 2808 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE)) 2809 printf(" %s\n", sub); 2810 if ((opts & PF_OPT_VERBOSE) && pfctl_show_eth_anchors(dev, opts, sub)) 2811 return (-1); 2812 } 2813 return (0); 2814 } 2815 2816 const char * 2817 pfctl_lookup_option(char *cmd, const char * const *list) 2818 { 2819 if (cmd != NULL && *cmd) 2820 for (; *list; list++) 2821 if (!strncmp(cmd, *list, strlen(cmd))) 2822 return (*list); 2823 return (NULL); 2824 } 2825 2826 int 2827 main(int argc, char *argv[]) 2828 { 2829 int error = 0; 2830 int ch; 2831 int mode = O_RDONLY; 2832 int opts = 0; 2833 int optimize = PF_OPTIMIZE_BASIC; 2834 char anchorname[MAXPATHLEN]; 2835 char *path; 2836 2837 if (argc < 2) 2838 usage(); 2839 2840 while ((ch = getopt(argc, argv, 2841 "a:AdD:eqf:F:ghi:k:K:mMnNOo:Pp:rRs:t:T:vx:z")) != -1) { 2842 switch (ch) { 2843 case 'a': 2844 anchoropt = optarg; 2845 break; 2846 case 'd': 2847 opts |= PF_OPT_DISABLE; 2848 mode = O_RDWR; 2849 break; 2850 case 'D': 2851 if (pfctl_cmdline_symset(optarg) < 0) 2852 warnx("could not parse macro definition %s", 2853 optarg); 2854 break; 2855 case 'e': 2856 opts |= PF_OPT_ENABLE; 2857 mode = O_RDWR; 2858 break; 2859 case 'q': 2860 opts |= PF_OPT_QUIET; 2861 break; 2862 case 'F': 2863 clearopt = pfctl_lookup_option(optarg, clearopt_list); 2864 if (clearopt == NULL) { 2865 warnx("Unknown flush modifier '%s'", optarg); 2866 usage(); 2867 } 2868 mode = O_RDWR; 2869 break; 2870 case 'i': 2871 ifaceopt = optarg; 2872 break; 2873 case 'k': 2874 if (state_killers >= 2) { 2875 warnx("can only specify -k twice"); 2876 usage(); 2877 /* NOTREACHED */ 2878 } 2879 state_kill[state_killers++] = optarg; 2880 mode = O_RDWR; 2881 break; 2882 case 'K': 2883 if (src_node_killers >= 2) { 2884 warnx("can only specify -K twice"); 2885 usage(); 2886 /* NOTREACHED */ 2887 } 2888 src_node_kill[src_node_killers++] = optarg; 2889 mode = O_RDWR; 2890 break; 2891 case 'm': 2892 opts |= PF_OPT_MERGE; 2893 break; 2894 case 'M': 2895 opts |= PF_OPT_KILLMATCH; 2896 break; 2897 case 'n': 2898 opts |= PF_OPT_NOACTION; 2899 break; 2900 case 'N': 2901 loadopt |= PFCTL_FLAG_NAT; 2902 break; 2903 case 'r': 2904 opts |= PF_OPT_USEDNS; 2905 break; 2906 case 'f': 2907 rulesopt = optarg; 2908 mode = O_RDWR; 2909 break; 2910 case 'g': 2911 opts |= PF_OPT_DEBUG; 2912 break; 2913 case 'A': 2914 loadopt |= PFCTL_FLAG_ALTQ; 2915 break; 2916 case 'R': 2917 loadopt |= PFCTL_FLAG_FILTER; 2918 break; 2919 case 'o': 2920 optiopt = pfctl_lookup_option(optarg, optiopt_list); 2921 if (optiopt == NULL) { 2922 warnx("Unknown optimization '%s'", optarg); 2923 usage(); 2924 } 2925 opts |= PF_OPT_OPTIMIZE; 2926 break; 2927 case 'O': 2928 loadopt |= PFCTL_FLAG_OPTION; 2929 break; 2930 case 'p': 2931 pf_device = optarg; 2932 break; 2933 case 'P': 2934 opts |= PF_OPT_NUMERIC; 2935 break; 2936 case 's': 2937 showopt = pfctl_lookup_option(optarg, showopt_list); 2938 if (showopt == NULL) { 2939 warnx("Unknown show modifier '%s'", optarg); 2940 usage(); 2941 } 2942 break; 2943 case 't': 2944 tableopt = optarg; 2945 break; 2946 case 'T': 2947 tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list); 2948 if (tblcmdopt == NULL) { 2949 warnx("Unknown table command '%s'", optarg); 2950 usage(); 2951 } 2952 break; 2953 case 'v': 2954 if (opts & PF_OPT_VERBOSE) 2955 opts |= PF_OPT_VERBOSE2; 2956 opts |= PF_OPT_VERBOSE; 2957 break; 2958 case 'x': 2959 debugopt = pfctl_lookup_option(optarg, debugopt_list); 2960 if (debugopt == NULL) { 2961 warnx("Unknown debug level '%s'", optarg); 2962 usage(); 2963 } 2964 mode = O_RDWR; 2965 break; 2966 case 'z': 2967 opts |= PF_OPT_CLRRULECTRS; 2968 mode = O_RDWR; 2969 break; 2970 case 'h': 2971 /* FALLTHROUGH */ 2972 default: 2973 usage(); 2974 /* NOTREACHED */ 2975 } 2976 } 2977 2978 if (tblcmdopt != NULL) { 2979 argc -= optind; 2980 argv += optind; 2981 ch = *tblcmdopt; 2982 if (ch == 'l') { 2983 loadopt |= PFCTL_FLAG_TABLE; 2984 tblcmdopt = NULL; 2985 } else 2986 mode = strchr("acdefkrz", ch) ? O_RDWR : O_RDONLY; 2987 } else if (argc != optind) { 2988 warnx("unknown command line argument: %s ...", argv[optind]); 2989 usage(); 2990 /* NOTREACHED */ 2991 } 2992 if (loadopt == 0) 2993 loadopt = ~0; 2994 2995 if ((path = calloc(1, MAXPATHLEN)) == NULL) 2996 errx(1, "pfctl: calloc"); 2997 memset(anchorname, 0, sizeof(anchorname)); 2998 if (anchoropt != NULL) { 2999 int len = strlen(anchoropt); 3000 3001 if (len >= 1 && anchoropt[len - 1] == '*') { 3002 if (len >= 2 && anchoropt[len - 2] == '/') 3003 anchoropt[len - 2] = '\0'; 3004 else 3005 anchoropt[len - 1] = '\0'; 3006 opts |= PF_OPT_RECURSE; 3007 } 3008 if (strlcpy(anchorname, anchoropt, 3009 sizeof(anchorname)) >= sizeof(anchorname)) 3010 errx(1, "anchor name '%s' too long", 3011 anchoropt); 3012 loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE|PFCTL_FLAG_ETH; 3013 } 3014 3015 if ((opts & PF_OPT_NOACTION) == 0) { 3016 dev = open(pf_device, mode); 3017 if (dev == -1) 3018 err(1, "%s", pf_device); 3019 altqsupport = pfctl_test_altqsupport(dev, opts); 3020 } else { 3021 dev = open(pf_device, O_RDONLY); 3022 if (dev >= 0) 3023 opts |= PF_OPT_DUMMYACTION; 3024 /* turn off options */ 3025 opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE); 3026 clearopt = showopt = debugopt = NULL; 3027 #if !defined(ENABLE_ALTQ) 3028 altqsupport = 0; 3029 #else 3030 altqsupport = 1; 3031 #endif 3032 } 3033 3034 if (opts & PF_OPT_DISABLE) 3035 if (pfctl_disable(dev, opts)) 3036 error = 1; 3037 3038 if (showopt != NULL) { 3039 switch (*showopt) { 3040 case 'A': 3041 pfctl_show_anchors(dev, opts, anchorname); 3042 pfctl_show_eth_anchors(dev, opts, anchorname); 3043 break; 3044 case 'r': 3045 pfctl_load_fingerprints(dev, opts); 3046 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES, 3047 anchorname, 0, 0); 3048 break; 3049 case 'l': 3050 pfctl_load_fingerprints(dev, opts); 3051 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS, 3052 anchorname, 0, 0); 3053 break; 3054 case 'n': 3055 pfctl_load_fingerprints(dev, opts); 3056 pfctl_show_nat(dev, path, opts, anchorname, 0); 3057 break; 3058 case 'q': 3059 pfctl_show_altq(dev, ifaceopt, opts, 3060 opts & PF_OPT_VERBOSE2); 3061 break; 3062 case 's': 3063 pfctl_show_states(dev, ifaceopt, opts); 3064 break; 3065 case 'S': 3066 pfctl_show_src_nodes(dev, opts); 3067 break; 3068 case 'i': 3069 pfctl_show_status(dev, opts); 3070 break; 3071 case 'R': 3072 error = pfctl_show_running(dev); 3073 break; 3074 case 't': 3075 pfctl_show_timeouts(dev, opts); 3076 break; 3077 case 'm': 3078 pfctl_show_limits(dev, opts); 3079 break; 3080 case 'e': 3081 pfctl_show_eth_rules(dev, path, opts, 0, anchorname, 0, 3082 0); 3083 break; 3084 case 'a': 3085 opts |= PF_OPT_SHOWALL; 3086 pfctl_load_fingerprints(dev, opts); 3087 3088 pfctl_show_eth_rules(dev, path, opts, 0, anchorname, 0, 3089 0); 3090 3091 pfctl_show_nat(dev, path, opts, anchorname, 0); 3092 pfctl_show_rules(dev, path, opts, 0, anchorname, 0, 0); 3093 pfctl_show_altq(dev, ifaceopt, opts, 0); 3094 pfctl_show_states(dev, ifaceopt, opts); 3095 pfctl_show_src_nodes(dev, opts); 3096 pfctl_show_status(dev, opts); 3097 pfctl_show_rules(dev, path, opts, 1, anchorname, 0, 0); 3098 pfctl_show_timeouts(dev, opts); 3099 pfctl_show_limits(dev, opts); 3100 pfctl_show_tables(anchorname, opts); 3101 pfctl_show_fingerprints(opts); 3102 break; 3103 case 'T': 3104 pfctl_show_tables(anchorname, opts); 3105 break; 3106 case 'o': 3107 pfctl_load_fingerprints(dev, opts); 3108 pfctl_show_fingerprints(opts); 3109 break; 3110 case 'I': 3111 pfctl_show_ifaces(ifaceopt, opts); 3112 break; 3113 } 3114 } 3115 3116 if ((opts & PF_OPT_CLRRULECTRS) && showopt == NULL) { 3117 pfctl_show_eth_rules(dev, path, opts, PFCTL_SHOW_NOTHING, 3118 anchorname, 0, 0); 3119 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_NOTHING, 3120 anchorname, 0, 0); 3121 } 3122 3123 if (clearopt != NULL) { 3124 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL) 3125 errx(1, "anchor names beginning with '_' cannot " 3126 "be modified from the command line"); 3127 3128 switch (*clearopt) { 3129 case 'e': 3130 pfctl_flush_eth_rules(dev, opts, anchorname); 3131 break; 3132 case 'r': 3133 pfctl_flush_rules(dev, opts, anchorname); 3134 break; 3135 case 'n': 3136 pfctl_flush_nat(dev, opts, anchorname); 3137 break; 3138 case 'q': 3139 pfctl_clear_altq(dev, opts); 3140 break; 3141 case 's': 3142 pfctl_clear_iface_states(dev, ifaceopt, opts); 3143 break; 3144 case 'S': 3145 pfctl_clear_src_nodes(dev, opts); 3146 break; 3147 case 'i': 3148 pfctl_clear_stats(dev, opts); 3149 break; 3150 case 'a': 3151 pfctl_flush_eth_rules(dev, opts, anchorname); 3152 pfctl_flush_rules(dev, opts, anchorname); 3153 pfctl_flush_nat(dev, opts, anchorname); 3154 pfctl_clear_tables(anchorname, opts); 3155 if (!*anchorname) { 3156 pfctl_clear_altq(dev, opts); 3157 pfctl_clear_iface_states(dev, ifaceopt, opts); 3158 pfctl_clear_src_nodes(dev, opts); 3159 pfctl_clear_stats(dev, opts); 3160 pfctl_clear_fingerprints(dev, opts); 3161 pfctl_clear_interface_flags(dev, opts); 3162 } 3163 break; 3164 case 'o': 3165 pfctl_clear_fingerprints(dev, opts); 3166 break; 3167 case 'T': 3168 pfctl_clear_tables(anchorname, opts); 3169 break; 3170 } 3171 } 3172 if (state_killers) { 3173 if (!strcmp(state_kill[0], "label")) 3174 pfctl_label_kill_states(dev, ifaceopt, opts); 3175 else if (!strcmp(state_kill[0], "id")) 3176 pfctl_id_kill_states(dev, ifaceopt, opts); 3177 else if (!strcmp(state_kill[0], "gateway")) 3178 pfctl_gateway_kill_states(dev, ifaceopt, opts); 3179 else 3180 pfctl_net_kill_states(dev, ifaceopt, opts); 3181 } 3182 3183 if (src_node_killers) 3184 pfctl_kill_src_nodes(dev, ifaceopt, opts); 3185 3186 if (tblcmdopt != NULL) { 3187 error = pfctl_command_tables(argc, argv, tableopt, 3188 tblcmdopt, rulesopt, anchorname, opts); 3189 rulesopt = NULL; 3190 } 3191 if (optiopt != NULL) { 3192 switch (*optiopt) { 3193 case 'n': 3194 optimize = 0; 3195 break; 3196 case 'b': 3197 optimize |= PF_OPTIMIZE_BASIC; 3198 break; 3199 case 'o': 3200 case 'p': 3201 optimize |= PF_OPTIMIZE_PROFILE; 3202 break; 3203 } 3204 } 3205 3206 if ((rulesopt != NULL) && (loadopt & PFCTL_FLAG_OPTION) && 3207 !anchorname[0] && !(opts & PF_OPT_NOACTION)) 3208 if (pfctl_get_skip_ifaces()) 3209 error = 1; 3210 3211 if (rulesopt != NULL && !(opts & (PF_OPT_MERGE|PF_OPT_NOACTION)) && 3212 !anchorname[0] && (loadopt & PFCTL_FLAG_OPTION)) 3213 if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE)) 3214 error = 1; 3215 3216 if (rulesopt != NULL) { 3217 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL) 3218 errx(1, "anchor names beginning with '_' cannot " 3219 "be modified from the command line"); 3220 if (pfctl_rules(dev, rulesopt, opts, optimize, 3221 anchorname, NULL)) 3222 error = 1; 3223 else if (!(opts & PF_OPT_NOACTION) && 3224 (loadopt & PFCTL_FLAG_TABLE)) 3225 warn_namespace_collision(NULL); 3226 } 3227 3228 if (opts & PF_OPT_ENABLE) 3229 if (pfctl_enable(dev, opts)) 3230 error = 1; 3231 3232 if (debugopt != NULL) { 3233 switch (*debugopt) { 3234 case 'n': 3235 pfctl_debug(dev, PF_DEBUG_NONE, opts); 3236 break; 3237 case 'u': 3238 pfctl_debug(dev, PF_DEBUG_URGENT, opts); 3239 break; 3240 case 'm': 3241 pfctl_debug(dev, PF_DEBUG_MISC, opts); 3242 break; 3243 case 'l': 3244 pfctl_debug(dev, PF_DEBUG_NOISY, opts); 3245 break; 3246 } 3247 } 3248 3249 exit(error); 3250 } 3251