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