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