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