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