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