1 /* $OpenBSD: pfctl.c,v 1.278 2008/08/31 20:18:17 jmc Exp $ */
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002,2003 Henning Brauer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
43 #include <sys/endian.h>
47 #include <netinet/in.h>
48 #include <net/pfvar.h>
49 #include <arpa/inet.h>
50 #include <altq/altq.h>
51 #include <sys/sysctl.h>
64 #include "pfctl_parser.h"
68 int pfctl_enable(int, int);
69 int pfctl_disable(int, int);
70 int pfctl_clear_stats(int, int);
71 int pfctl_clear_interface_flags(int, int);
72 int pfctl_clear_rules(int, int, char *);
73 int pfctl_clear_nat(int, int, char *);
74 int pfctl_clear_altq(int, int);
75 int pfctl_clear_src_nodes(int, int);
76 int pfctl_clear_states(int, const char *, int);
77 void pfctl_addrprefix(char *, struct pf_addr *);
78 int pfctl_kill_src_nodes(int, const char *, int);
79 int pfctl_net_kill_states(int, const char *, int);
80 int pfctl_label_kill_states(int, const char *, int);
81 int pfctl_id_kill_states(int, const char *, int);
82 void pfctl_init_options(struct pfctl *);
83 int pfctl_load_options(struct pfctl *);
84 int pfctl_load_limit(struct pfctl *, unsigned int, unsigned int);
85 int pfctl_load_timeout(struct pfctl *, unsigned int, unsigned int);
86 int pfctl_load_debug(struct pfctl *, unsigned int);
87 int pfctl_load_logif(struct pfctl *, char *);
88 int pfctl_load_hostid(struct pfctl *, unsigned int);
89 int pfctl_get_pool(int, struct pf_pool *, u_int32_t, u_int32_t, int,
91 void pfctl_print_rule_counters(struct pf_rule *, int);
92 int pfctl_show_rules(int, char *, int, enum pfctl_show, char *, int);
93 int pfctl_show_nat(int, int, char *);
94 int pfctl_show_src_nodes(int, int);
95 int pfctl_show_states(int, const char *, int);
96 int pfctl_show_status(int, int);
97 int pfctl_show_timeouts(int, int);
98 int pfctl_show_limits(int, int);
99 void pfctl_debug(int, u_int32_t, int);
100 int pfctl_test_altqsupport(int, int);
101 int pfctl_show_anchors(int, int, char *);
102 int pfctl_ruleset_trans(struct pfctl *, char *, struct pf_anchor *);
103 int pfctl_load_ruleset(struct pfctl *, char *,
104 struct pf_ruleset *, int, int);
105 int pfctl_load_rule(struct pfctl *, char *, struct pf_rule *, int);
106 const char *pfctl_lookup_option(char *, const char **);
108 struct pf_anchor_global pf_anchors;
109 struct pf_anchor pf_main_anchor;
111 const char *clearopt;
114 const char *debugopt;
116 const char *optiopt = NULL;
117 char *pf_device = "/dev/pf";
120 const char *tblcmdopt;
121 int src_node_killers;
122 char *src_node_kill[2];
132 #define INDENT(d, o) do { \
135 for (i=0; i < d; i++) \
141 static const struct {
145 { "states", PF_LIMIT_STATES },
146 { "src-nodes", PF_LIMIT_SRC_NODES },
147 { "frags", PF_LIMIT_FRAGS },
148 { "table-entries", PF_LIMIT_TABLE_ENTRIES },
156 static const struct pf_hint pf_hint_normal[] = {
157 { "tcp.first", 2 * 60 },
158 { "tcp.opening", 30 },
159 { "tcp.established", 24 * 60 * 60 },
160 { "tcp.closing", 15 * 60 },
161 { "tcp.finwait", 45 },
162 { "tcp.closed", 90 },
163 { "tcp.tsdiff", 30 },
166 static const struct pf_hint pf_hint_satellite[] = {
167 { "tcp.first", 3 * 60 },
168 { "tcp.opening", 30 + 5 },
169 { "tcp.established", 24 * 60 * 60 },
170 { "tcp.closing", 15 * 60 + 5 },
171 { "tcp.finwait", 45 + 5 },
172 { "tcp.closed", 90 + 5 },
173 { "tcp.tsdiff", 60 },
176 static const struct pf_hint pf_hint_conservative[] = {
177 { "tcp.first", 60 * 60 },
178 { "tcp.opening", 15 * 60 },
179 { "tcp.established", 5 * 24 * 60 * 60 },
180 { "tcp.closing", 60 * 60 },
181 { "tcp.finwait", 10 * 60 },
182 { "tcp.closed", 3 * 60 },
183 { "tcp.tsdiff", 60 },
186 static const struct pf_hint pf_hint_aggressive[] = {
188 { "tcp.opening", 5 },
189 { "tcp.established", 5 * 60 * 60 },
190 { "tcp.closing", 60 },
191 { "tcp.finwait", 30 },
192 { "tcp.closed", 30 },
193 { "tcp.tsdiff", 10 },
197 static const struct {
199 const struct pf_hint *hint;
201 { "normal", pf_hint_normal },
202 { "satellite", pf_hint_satellite },
203 { "high-latency", pf_hint_satellite },
204 { "conservative", pf_hint_conservative },
205 { "aggressive", pf_hint_aggressive },
209 static const char *clearopt_list[] = {
210 "nat", "queue", "rules", "Sources",
211 "states", "info", "Tables", "osfp", "all", NULL
214 static const char *showopt_list[] = {
215 "nat", "queue", "rules", "Anchors", "Sources", "states", "info",
216 "Interfaces", "labels", "timeouts", "memory", "Tables", "osfp",
220 static const char *tblcmdopt_list[] = {
221 "kill", "flush", "add", "delete", "load", "replace", "show",
222 "test", "zero", "expire", NULL
225 static const char *debugopt_list[] = {
226 "none", "urgent", "misc", "loud", NULL
229 static const char *optiopt_list[] = {
230 "none", "basic", "profile", NULL
236 extern char *__progname;
238 fprintf(stderr, "usage: %s [-AdeghmNnOPqRrvz] ", __progname);
239 fprintf(stderr, "[-a anchor] [-D macro=value] [-F modifier]\n");
240 fprintf(stderr, "\t[-f file] [-i interface] [-K host | network]\n");
241 fprintf(stderr, "\t[-k host | network | label | id] ");
242 fprintf(stderr, "[-o level] [-p device]\n");
243 fprintf(stderr, "\t[-s modifier] ");
244 fprintf(stderr, "[-t table -T command [address ...]] [-x level]\n");
249 pfctl_enable(int dev, int opts)
251 if (ioctl(dev, DIOCSTART)) {
253 errx(1, "pf already enabled");
255 else if (errno == ESRCH)
256 errx(1, "pfil registeration failed");
261 if ((opts & PF_OPT_QUIET) == 0)
262 fprintf(stderr, "pf enabled\n");
264 if (altqsupport && ioctl(dev, DIOCSTARTALTQ))
266 err(1, "DIOCSTARTALTQ");
272 pfctl_disable(int dev, int opts)
274 if (ioctl(dev, DIOCSTOP)) {
276 errx(1, "pf not enabled");
280 if ((opts & PF_OPT_QUIET) == 0)
281 fprintf(stderr, "pf disabled\n");
283 if (altqsupport && ioctl(dev, DIOCSTOPALTQ))
285 err(1, "DIOCSTOPALTQ");
291 pfctl_clear_stats(int dev, int opts)
293 if (ioctl(dev, DIOCCLRSTATUS))
294 err(1, "DIOCCLRSTATUS");
295 if ((opts & PF_OPT_QUIET) == 0)
296 fprintf(stderr, "pf: statistics cleared\n");
301 pfctl_clear_interface_flags(int dev, int opts)
303 struct pfioc_iface pi;
305 if ((opts & PF_OPT_NOACTION) == 0) {
306 bzero(&pi, sizeof(pi));
307 pi.pfiio_flags = PFI_IFLAG_SKIP;
309 if (ioctl(dev, DIOCCLRIFFLAG, &pi))
310 err(1, "DIOCCLRIFFLAG");
311 if ((opts & PF_OPT_QUIET) == 0)
312 fprintf(stderr, "pf: interface flags reset\n");
318 pfctl_clear_rules(int dev, int opts, char *anchorname)
322 memset(&t, 0, sizeof(t));
323 t.pfrb_type = PFRB_TRANS;
324 if (pfctl_add_trans(&t, PF_RULESET_SCRUB, anchorname) ||
325 pfctl_add_trans(&t, PF_RULESET_FILTER, anchorname) ||
326 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
327 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
328 err(1, "pfctl_clear_rules");
329 if ((opts & PF_OPT_QUIET) == 0)
330 fprintf(stderr, "rules cleared\n");
335 pfctl_clear_nat(int dev, int opts, char *anchorname)
339 memset(&t, 0, sizeof(t));
340 t.pfrb_type = PFRB_TRANS;
341 if (pfctl_add_trans(&t, PF_RULESET_NAT, anchorname) ||
342 pfctl_add_trans(&t, PF_RULESET_BINAT, anchorname) ||
343 pfctl_add_trans(&t, PF_RULESET_RDR, anchorname) ||
344 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
345 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
346 err(1, "pfctl_clear_nat");
347 if ((opts & PF_OPT_QUIET) == 0)
348 fprintf(stderr, "nat cleared\n");
353 pfctl_clear_altq(int dev, int opts)
359 memset(&t, 0, sizeof(t));
360 t.pfrb_type = PFRB_TRANS;
361 if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "") ||
362 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
363 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
364 err(1, "pfctl_clear_altq");
365 if ((opts & PF_OPT_QUIET) == 0)
366 fprintf(stderr, "altq cleared\n");
371 pfctl_clear_src_nodes(int dev, int opts)
373 if (ioctl(dev, DIOCCLRSRCNODES))
374 err(1, "DIOCCLRSRCNODES");
375 if ((opts & PF_OPT_QUIET) == 0)
376 fprintf(stderr, "source tracking entries cleared\n");
381 pfctl_clear_states(int dev, const char *iface, int opts)
383 struct pfioc_state_kill psk;
385 memset(&psk, 0, sizeof(psk));
386 if (iface != NULL && strlcpy(psk.psk_ifname, iface,
387 sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
388 errx(1, "invalid interface: %s", iface);
390 if (ioctl(dev, DIOCCLRSTATES, &psk))
391 err(1, "DIOCCLRSTATES");
392 if ((opts & PF_OPT_QUIET) == 0)
393 fprintf(stderr, "%d states cleared\n", psk.psk_killed);
398 pfctl_addrprefix(char *addr, struct pf_addr *mask)
402 int prefix, ret_ga, q, r;
403 struct addrinfo hints, *res;
405 if ((p = strchr(addr, '/')) == NULL)
409 prefix = strtonum(p, 0, 128, &errstr);
411 errx(1, "prefix is %s: %s", errstr, p);
413 bzero(&hints, sizeof(hints));
414 /* prefix only with numeric addresses */
415 hints.ai_flags |= AI_NUMERICHOST;
417 if ((ret_ga = getaddrinfo(addr, NULL, &hints, &res))) {
418 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
422 if (res->ai_family == AF_INET && prefix > 32)
423 errx(1, "prefix too long for AF_INET");
424 else if (res->ai_family == AF_INET6 && prefix > 128)
425 errx(1, "prefix too long for AF_INET6");
429 switch (res->ai_family) {
431 bzero(&mask->v4, sizeof(mask->v4));
432 mask->v4.s_addr = htonl((u_int32_t)
433 (0xffffffffffULL << (32 - prefix)));
436 bzero(&mask->v6, sizeof(mask->v6));
438 memset((void *)&mask->v6, 0xff, q);
440 *((u_char *)&mask->v6 + q) =
441 (0xff00 >> r) & 0xff;
448 pfctl_kill_src_nodes(int dev, const char *iface, int opts)
450 struct pfioc_src_node_kill psnk;
451 struct addrinfo *res[2], *resp[2];
452 struct sockaddr last_src, last_dst;
453 int killed, sources, dests;
456 killed = sources = dests = 0;
458 memset(&psnk, 0, sizeof(psnk));
459 memset(&psnk.psnk_src.addr.v.a.mask, 0xff,
460 sizeof(psnk.psnk_src.addr.v.a.mask));
461 memset(&last_src, 0xff, sizeof(last_src));
462 memset(&last_dst, 0xff, sizeof(last_dst));
464 pfctl_addrprefix(src_node_kill[0], &psnk.psnk_src.addr.v.a.mask);
466 if ((ret_ga = getaddrinfo(src_node_kill[0], NULL, NULL, &res[0]))) {
467 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
470 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
471 if (resp[0]->ai_addr == NULL)
473 /* We get lots of duplicates. Catch the easy ones */
474 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
476 last_src = *(struct sockaddr *)resp[0]->ai_addr;
478 psnk.psnk_af = resp[0]->ai_family;
481 if (psnk.psnk_af == AF_INET)
482 psnk.psnk_src.addr.v.a.addr.v4 =
483 ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
484 else if (psnk.psnk_af == AF_INET6)
485 psnk.psnk_src.addr.v.a.addr.v6 =
486 ((struct sockaddr_in6 *)resp[0]->ai_addr)->
489 errx(1, "Unknown address family %d", psnk.psnk_af);
491 if (src_node_killers > 1) {
493 memset(&psnk.psnk_dst.addr.v.a.mask, 0xff,
494 sizeof(psnk.psnk_dst.addr.v.a.mask));
495 memset(&last_dst, 0xff, sizeof(last_dst));
496 pfctl_addrprefix(src_node_kill[1],
497 &psnk.psnk_dst.addr.v.a.mask);
498 if ((ret_ga = getaddrinfo(src_node_kill[1], NULL, NULL,
500 errx(1, "getaddrinfo: %s",
501 gai_strerror(ret_ga));
504 for (resp[1] = res[1]; resp[1];
505 resp[1] = resp[1]->ai_next) {
506 if (resp[1]->ai_addr == NULL)
508 if (psnk.psnk_af != resp[1]->ai_family)
511 if (memcmp(&last_dst, resp[1]->ai_addr,
512 sizeof(last_dst)) == 0)
514 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
518 if (psnk.psnk_af == AF_INET)
519 psnk.psnk_dst.addr.v.a.addr.v4 =
520 ((struct sockaddr_in *)resp[1]->
522 else if (psnk.psnk_af == AF_INET6)
523 psnk.psnk_dst.addr.v.a.addr.v6 =
524 ((struct sockaddr_in6 *)resp[1]->
527 errx(1, "Unknown address family %d",
530 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
531 err(1, "DIOCKILLSRCNODES");
532 killed += psnk.psnk_killed;
534 freeaddrinfo(res[1]);
536 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
537 err(1, "DIOCKILLSRCNODES");
538 killed += psnk.psnk_killed;
542 freeaddrinfo(res[0]);
544 if ((opts & PF_OPT_QUIET) == 0)
545 fprintf(stderr, "killed %d src nodes from %d sources and %d "
546 "destinations\n", killed, sources, dests);
551 pfctl_net_kill_states(int dev, const char *iface, int opts)
553 struct pfioc_state_kill psk;
554 struct addrinfo *res[2], *resp[2];
555 struct sockaddr last_src, last_dst;
556 int killed, sources, dests;
559 killed = sources = dests = 0;
561 memset(&psk, 0, sizeof(psk));
562 memset(&psk.psk_src.addr.v.a.mask, 0xff,
563 sizeof(psk.psk_src.addr.v.a.mask));
564 memset(&last_src, 0xff, sizeof(last_src));
565 memset(&last_dst, 0xff, sizeof(last_dst));
566 if (iface != NULL && strlcpy(psk.psk_ifname, iface,
567 sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
568 errx(1, "invalid interface: %s", iface);
570 pfctl_addrprefix(state_kill[0], &psk.psk_src.addr.v.a.mask);
572 if ((ret_ga = getaddrinfo(state_kill[0], NULL, NULL, &res[0]))) {
573 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
576 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
577 if (resp[0]->ai_addr == NULL)
579 /* We get lots of duplicates. Catch the easy ones */
580 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
582 last_src = *(struct sockaddr *)resp[0]->ai_addr;
584 psk.psk_af = resp[0]->ai_family;
587 if (psk.psk_af == AF_INET)
588 psk.psk_src.addr.v.a.addr.v4 =
589 ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
590 else if (psk.psk_af == AF_INET6)
591 psk.psk_src.addr.v.a.addr.v6 =
592 ((struct sockaddr_in6 *)resp[0]->ai_addr)->
595 errx(1, "Unknown address family %d", psk.psk_af);
597 if (state_killers > 1) {
599 memset(&psk.psk_dst.addr.v.a.mask, 0xff,
600 sizeof(psk.psk_dst.addr.v.a.mask));
601 memset(&last_dst, 0xff, sizeof(last_dst));
602 pfctl_addrprefix(state_kill[1],
603 &psk.psk_dst.addr.v.a.mask);
604 if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL,
606 errx(1, "getaddrinfo: %s",
607 gai_strerror(ret_ga));
610 for (resp[1] = res[1]; resp[1];
611 resp[1] = resp[1]->ai_next) {
612 if (resp[1]->ai_addr == NULL)
614 if (psk.psk_af != resp[1]->ai_family)
617 if (memcmp(&last_dst, resp[1]->ai_addr,
618 sizeof(last_dst)) == 0)
620 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
624 if (psk.psk_af == AF_INET)
625 psk.psk_dst.addr.v.a.addr.v4 =
626 ((struct sockaddr_in *)resp[1]->
628 else if (psk.psk_af == AF_INET6)
629 psk.psk_dst.addr.v.a.addr.v6 =
630 ((struct sockaddr_in6 *)resp[1]->
633 errx(1, "Unknown address family %d",
636 if (ioctl(dev, DIOCKILLSTATES, &psk))
637 err(1, "DIOCKILLSTATES");
638 killed += psk.psk_killed;
640 freeaddrinfo(res[1]);
642 if (ioctl(dev, DIOCKILLSTATES, &psk))
643 err(1, "DIOCKILLSTATES");
644 killed += psk.psk_killed;
648 freeaddrinfo(res[0]);
650 if ((opts & PF_OPT_QUIET) == 0)
651 fprintf(stderr, "killed %d states from %d sources and %d "
652 "destinations\n", killed, sources, dests);
657 pfctl_label_kill_states(int dev, const char *iface, int opts)
659 struct pfioc_state_kill psk;
661 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
662 warnx("no label specified");
665 memset(&psk, 0, sizeof(psk));
666 if (iface != NULL && strlcpy(psk.psk_ifname, iface,
667 sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
668 errx(1, "invalid interface: %s", iface);
670 if (strlcpy(psk.psk_label, state_kill[1], sizeof(psk.psk_label)) >=
671 sizeof(psk.psk_label))
672 errx(1, "label too long: %s", state_kill[1]);
674 if (ioctl(dev, DIOCKILLSTATES, &psk))
675 err(1, "DIOCKILLSTATES");
677 if ((opts & PF_OPT_QUIET) == 0)
678 fprintf(stderr, "killed %d states\n", psk.psk_killed);
684 pfctl_id_kill_states(int dev, const char *iface, int opts)
686 struct pfioc_state_kill psk;
688 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
689 warnx("no id specified");
693 memset(&psk, 0, sizeof(psk));
694 if ((sscanf(state_kill[1], "%jx/%x",
695 &psk.psk_pfcmp.id, &psk.psk_pfcmp.creatorid)) == 2)
696 HTONL(psk.psk_pfcmp.creatorid);
697 else if ((sscanf(state_kill[1], "%jx", &psk.psk_pfcmp.id)) == 1) {
698 psk.psk_pfcmp.creatorid = 0;
700 warnx("wrong id format specified");
703 if (psk.psk_pfcmp.id == 0) {
704 warnx("cannot kill id 0");
708 psk.psk_pfcmp.id = htobe64(psk.psk_pfcmp.id);
709 if (ioctl(dev, DIOCKILLSTATES, &psk))
710 err(1, "DIOCKILLSTATES");
712 if ((opts & PF_OPT_QUIET) == 0)
713 fprintf(stderr, "killed %d states\n", psk.psk_killed);
719 pfctl_get_pool(int dev, struct pf_pool *pool, u_int32_t nr,
720 u_int32_t ticket, int r_action, char *anchorname)
722 struct pfioc_pooladdr pp;
723 struct pf_pooladdr *pa;
726 memset(&pp, 0, sizeof(pp));
727 memcpy(pp.anchor, anchorname, sizeof(pp.anchor));
728 pp.r_action = r_action;
731 if (ioctl(dev, DIOCGETADDRS, &pp)) {
732 warn("DIOCGETADDRS");
736 TAILQ_INIT(&pool->list);
737 for (pnr = 0; pnr < mpnr; ++pnr) {
739 if (ioctl(dev, DIOCGETADDR, &pp)) {
743 pa = calloc(1, sizeof(struct pf_pooladdr));
746 bcopy(&pp.addr, pa, sizeof(struct pf_pooladdr));
747 TAILQ_INSERT_TAIL(&pool->list, pa, entries);
754 pfctl_move_pool(struct pf_pool *src, struct pf_pool *dst)
756 struct pf_pooladdr *pa;
758 while ((pa = TAILQ_FIRST(&src->list)) != NULL) {
759 TAILQ_REMOVE(&src->list, pa, entries);
760 TAILQ_INSERT_TAIL(&dst->list, pa, entries);
765 pfctl_clear_pool(struct pf_pool *pool)
767 struct pf_pooladdr *pa;
769 while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
770 TAILQ_REMOVE(&pool->list, pa, entries);
776 pfctl_print_rule_counters(struct pf_rule *rule, int opts)
778 if (opts & PF_OPT_DEBUG) {
779 const char *t[PF_SKIP_COUNT] = { "i", "d", "f",
780 "p", "sa", "sp", "da", "dp" };
783 printf(" [ Skip steps: ");
784 for (i = 0; i < PF_SKIP_COUNT; ++i) {
785 if (rule->skip[i].nr == rule->nr + 1)
788 if (rule->skip[i].nr == -1)
791 printf("%u ", rule->skip[i].nr);
795 printf(" [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n",
796 rule->qname, rule->qid, rule->pqname, rule->pqid);
798 if (opts & PF_OPT_VERBOSE) {
799 printf(" [ Evaluations: %-8llu Packets: %-8llu "
800 "Bytes: %-10llu States: %-6ju]\n",
801 (unsigned long long)rule->evaluations,
802 (unsigned long long)(rule->packets[0] +
804 (unsigned long long)(rule->bytes[0] +
805 rule->bytes[1]), (uintmax_t)rule->u_states_cur);
806 if (!(opts & PF_OPT_DEBUG))
807 printf(" [ Inserted: uid %u pid %u "
808 "State Creations: %-6ju]\n",
809 (unsigned)rule->cuid, (unsigned)rule->cpid,
810 (uintmax_t)rule->u_states_tot);
815 pfctl_print_title(char *title)
820 printf("%s\n", title);
824 pfctl_show_rules(int dev, char *path, int opts, enum pfctl_show format,
825 char *anchorname, int depth)
827 struct pfioc_rule pr;
828 u_int32_t nr, mnr, header = 0;
829 int rule_numbers = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG);
830 int numeric = opts & PF_OPT_NUMERIC;
831 int len = strlen(path);
836 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname);
838 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname);
840 memset(&pr, 0, sizeof(pr));
841 memcpy(pr.anchor, path, sizeof(pr.anchor));
842 if (opts & PF_OPT_SHOWALL) {
843 pr.rule.action = PF_PASS;
844 if (ioctl(dev, DIOCGETRULES, &pr)) {
845 warn("DIOCGETRULES");
850 pr.rule.action = PF_SCRUB;
851 if (ioctl(dev, DIOCGETRULES, &pr)) {
852 warn("DIOCGETRULES");
855 if (opts & PF_OPT_SHOWALL) {
856 if (format == PFCTL_SHOW_RULES && (pr.nr > 0 || header))
857 pfctl_print_title("FILTER RULES:");
858 else if (format == PFCTL_SHOW_LABELS && labels)
859 pfctl_print_title("LABEL COUNTERS:");
862 if (opts & PF_OPT_CLRRULECTRS)
863 pr.action = PF_GET_CLR_CNTR;
865 for (nr = 0; nr < mnr; ++nr) {
867 if (ioctl(dev, DIOCGETRULE, &pr)) {
872 if (pfctl_get_pool(dev, &pr.rule.rpool,
873 nr, pr.ticket, PF_SCRUB, path) != 0)
877 case PFCTL_SHOW_LABELS:
879 case PFCTL_SHOW_RULES:
880 if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
882 print_rule(&pr.rule, pr.anchor_call, rule_numbers, numeric);
884 pfctl_print_rule_counters(&pr.rule, opts);
886 case PFCTL_SHOW_NOTHING:
889 pfctl_clear_pool(&pr.rule.rpool);
891 pr.rule.action = PF_PASS;
892 if (ioctl(dev, DIOCGETRULES, &pr)) {
893 warn("DIOCGETRULES");
897 for (nr = 0; nr < mnr; ++nr) {
899 if (ioctl(dev, DIOCGETRULE, &pr)) {
904 if (pfctl_get_pool(dev, &pr.rule.rpool,
905 nr, pr.ticket, PF_PASS, path) != 0)
909 case PFCTL_SHOW_LABELS:
910 if (pr.rule.label[0]) {
911 printf("%s %llu %llu %llu %llu"
912 " %llu %llu %llu %ju\n",
914 (unsigned long long)pr.rule.evaluations,
915 (unsigned long long)(pr.rule.packets[0] +
917 (unsigned long long)(pr.rule.bytes[0] +
919 (unsigned long long)pr.rule.packets[0],
920 (unsigned long long)pr.rule.bytes[0],
921 (unsigned long long)pr.rule.packets[1],
922 (unsigned long long)pr.rule.bytes[1],
923 (uintmax_t)pr.rule.u_states_tot);
926 case PFCTL_SHOW_RULES:
928 if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
930 INDENT(depth, !(opts & PF_OPT_VERBOSE));
931 if (pr.anchor_call[0] &&
932 ((((p = strrchr(pr.anchor_call, '_')) != NULL) &&
933 ((void *)p == (void *)pr.anchor_call ||
934 *(--p) == '/')) || (opts & PF_OPT_RECURSE))) {
936 if ((p = strrchr(pr.anchor_call, '/')) !=
940 p = &pr.anchor_call[0];
942 p = &pr.anchor_call[0];
944 print_rule(&pr.rule, p, rule_numbers, numeric);
949 pfctl_print_rule_counters(&pr.rule, opts);
951 pfctl_show_rules(dev, path, opts, format,
953 INDENT(depth, !(opts & PF_OPT_VERBOSE));
957 case PFCTL_SHOW_NOTHING:
960 pfctl_clear_pool(&pr.rule.rpool);
971 pfctl_show_nat(int dev, int opts, char *anchorname)
973 struct pfioc_rule pr;
975 static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT };
976 int i, dotitle = opts & PF_OPT_SHOWALL;
978 memset(&pr, 0, sizeof(pr));
979 memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
980 for (i = 0; i < 3; i++) {
981 pr.rule.action = nattype[i];
982 if (ioctl(dev, DIOCGETRULES, &pr)) {
983 warn("DIOCGETRULES");
987 for (nr = 0; nr < mnr; ++nr) {
989 if (ioctl(dev, DIOCGETRULE, &pr)) {
993 if (pfctl_get_pool(dev, &pr.rule.rpool, nr,
994 pr.ticket, nattype[i], anchorname) != 0)
997 pfctl_print_title("TRANSLATION RULES:");
1000 print_rule(&pr.rule, pr.anchor_call,
1001 opts & PF_OPT_VERBOSE2, opts & PF_OPT_NUMERIC);
1003 pfctl_print_rule_counters(&pr.rule, opts);
1004 pfctl_clear_pool(&pr.rule.rpool);
1011 pfctl_show_src_nodes(int dev, int opts)
1013 struct pfioc_src_nodes psn;
1014 struct pf_src_node *p;
1015 char *inbuf = NULL, *newinbuf = NULL;
1016 unsigned int len = 0;
1019 memset(&psn, 0, sizeof(psn));
1023 newinbuf = realloc(inbuf, len);
1024 if (newinbuf == NULL)
1026 psn.psn_buf = inbuf = newinbuf;
1028 if (ioctl(dev, DIOCGETSRCNODES, &psn) < 0) {
1029 warn("DIOCGETSRCNODES");
1033 if (psn.psn_len + sizeof(struct pfioc_src_nodes) < len)
1035 if (len == 0 && psn.psn_len == 0)
1037 if (len == 0 && psn.psn_len != 0)
1039 if (psn.psn_len == 0)
1040 goto done; /* no src_nodes */
1043 p = psn.psn_src_nodes;
1044 if (psn.psn_len > 0 && (opts & PF_OPT_SHOWALL))
1045 pfctl_print_title("SOURCE TRACKING NODES:");
1046 for (i = 0; i < psn.psn_len; i += sizeof(*p)) {
1047 print_src_node(p, opts);
1056 pfctl_show_states(int dev, const char *iface, int opts)
1058 struct pfioc_states ps;
1059 struct pfsync_state *p;
1060 char *inbuf = NULL, *newinbuf = NULL;
1061 unsigned int len = 0;
1062 int i, dotitle = (opts & PF_OPT_SHOWALL);
1064 memset(&ps, 0, sizeof(ps));
1068 newinbuf = realloc(inbuf, len);
1069 if (newinbuf == NULL)
1071 ps.ps_buf = inbuf = newinbuf;
1073 if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
1074 warn("DIOCGETSTATES");
1078 if (ps.ps_len + sizeof(struct pfioc_states) < len)
1080 if (len == 0 && ps.ps_len == 0)
1082 if (len == 0 && ps.ps_len != 0)
1085 goto done; /* no states */
1089 for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
1090 if (iface != NULL && strcmp(p->ifname, iface))
1093 pfctl_print_title("STATES:");
1096 print_state(p, opts);
1104 pfctl_show_status(int dev, int opts)
1106 struct pf_status status;
1108 if (ioctl(dev, DIOCGETSTATUS, &status)) {
1109 warn("DIOCGETSTATUS");
1112 if (opts & PF_OPT_SHOWALL)
1113 pfctl_print_title("INFO:");
1114 print_status(&status, opts);
1119 pfctl_show_timeouts(int dev, int opts)
1124 if (opts & PF_OPT_SHOWALL)
1125 pfctl_print_title("TIMEOUTS:");
1126 memset(&pt, 0, sizeof(pt));
1127 for (i = 0; pf_timeouts[i].name; i++) {
1128 pt.timeout = pf_timeouts[i].timeout;
1129 if (ioctl(dev, DIOCGETTIMEOUT, &pt))
1130 err(1, "DIOCGETTIMEOUT");
1131 printf("%-20s %10d", pf_timeouts[i].name, pt.seconds);
1132 if (pf_timeouts[i].timeout >= PFTM_ADAPTIVE_START &&
1133 pf_timeouts[i].timeout <= PFTM_ADAPTIVE_END)
1144 pfctl_show_limits(int dev, int opts)
1146 struct pfioc_limit pl;
1149 if (opts & PF_OPT_SHOWALL)
1150 pfctl_print_title("LIMITS:");
1151 memset(&pl, 0, sizeof(pl));
1152 for (i = 0; pf_limits[i].name; i++) {
1153 pl.index = pf_limits[i].index;
1154 if (ioctl(dev, DIOCGETLIMIT, &pl))
1155 err(1, "DIOCGETLIMIT");
1156 printf("%-13s ", pf_limits[i].name);
1157 if (pl.limit == UINT_MAX)
1158 printf("unlimited\n");
1160 printf("hard limit %8u\n", pl.limit);
1165 /* callbacks for rule/nat/rdr/addr */
1167 pfctl_add_pool(struct pfctl *pf, struct pf_pool *p, sa_family_t af)
1169 struct pf_pooladdr *pa;
1171 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1172 if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr))
1173 err(1, "DIOCBEGINADDRS");
1177 TAILQ_FOREACH(pa, &p->list, entries) {
1178 memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr));
1179 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1180 if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr))
1181 err(1, "DIOCADDADDR");
1188 pfctl_add_rule(struct pfctl *pf, struct pf_rule *r, const char *anchor_call)
1191 struct pf_rule *rule;
1192 struct pf_ruleset *rs;
1195 rs_num = pf_get_ruleset_number(r->action);
1196 if (rs_num == PF_RULESET_MAX)
1197 errx(1, "Invalid rule type %d", r->action);
1199 rs = &pf->anchor->ruleset;
1201 if (anchor_call[0] && r->anchor == NULL) {
1203 * Don't make non-brace anchors part of the main anchor pool.
1205 if ((r->anchor = calloc(1, sizeof(*r->anchor))) == NULL)
1206 err(1, "pfctl_add_rule: calloc");
1208 pf_init_ruleset(&r->anchor->ruleset);
1209 r->anchor->ruleset.anchor = r->anchor;
1210 if (strlcpy(r->anchor->path, anchor_call,
1211 sizeof(rule->anchor->path)) >= sizeof(rule->anchor->path))
1212 errx(1, "pfctl_add_rule: strlcpy");
1213 if ((p = strrchr(anchor_call, '/')) != NULL) {
1215 err(1, "pfctl_add_rule: bad anchor name %s",
1218 p = (char *)anchor_call;
1219 if (strlcpy(r->anchor->name, p,
1220 sizeof(rule->anchor->name)) >= sizeof(rule->anchor->name))
1221 errx(1, "pfctl_add_rule: strlcpy");
1224 if ((rule = calloc(1, sizeof(*rule))) == NULL)
1226 bcopy(r, rule, sizeof(*rule));
1227 TAILQ_INIT(&rule->rpool.list);
1228 pfctl_move_pool(&r->rpool, &rule->rpool);
1230 TAILQ_INSERT_TAIL(rs->rules[rs_num].active.ptr, rule, entries);
1235 pfctl_ruleset_trans(struct pfctl *pf, char *path, struct pf_anchor *a)
1237 int osize = pf->trans->pfrb_size;
1239 if ((pf->loadopt & PFCTL_FLAG_NAT) != 0) {
1240 if (pfctl_add_trans(pf->trans, PF_RULESET_NAT, path) ||
1241 pfctl_add_trans(pf->trans, PF_RULESET_BINAT, path) ||
1242 pfctl_add_trans(pf->trans, PF_RULESET_RDR, path))
1245 if (a == pf->astack[0] && ((altqsupport &&
1246 (pf->loadopt & PFCTL_FLAG_ALTQ) != 0))) {
1247 if (pfctl_add_trans(pf->trans, PF_RULESET_ALTQ, path))
1250 if ((pf->loadopt & PFCTL_FLAG_FILTER) != 0) {
1251 if (pfctl_add_trans(pf->trans, PF_RULESET_SCRUB, path) ||
1252 pfctl_add_trans(pf->trans, PF_RULESET_FILTER, path))
1255 if (pf->loadopt & PFCTL_FLAG_TABLE)
1256 if (pfctl_add_trans(pf->trans, PF_RULESET_TABLE, path))
1258 if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize))
1265 pfctl_load_ruleset(struct pfctl *pf, char *path, struct pf_ruleset *rs,
1266 int rs_num, int depth)
1269 int error, len = strlen(path);
1272 pf->anchor = rs->anchor;
1275 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->anchor->name);
1277 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->anchor->name);
1280 if (TAILQ_FIRST(rs->rules[rs_num].active.ptr) != NULL) {
1282 if (pf->opts & PF_OPT_VERBOSE)
1284 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
1285 (error = pfctl_ruleset_trans(pf,
1286 path, rs->anchor))) {
1287 printf("pfctl_load_rulesets: "
1288 "pfctl_ruleset_trans %d\n", error);
1291 } else if (pf->opts & PF_OPT_VERBOSE)
1296 if (pf->optimize && rs_num == PF_RULESET_FILTER)
1297 pfctl_optimize_ruleset(pf, rs);
1299 while ((r = TAILQ_FIRST(rs->rules[rs_num].active.ptr)) != NULL) {
1300 TAILQ_REMOVE(rs->rules[rs_num].active.ptr, r, entries);
1301 if ((error = pfctl_load_rule(pf, path, r, depth)))
1304 if ((error = pfctl_load_ruleset(pf, path,
1305 &r->anchor->ruleset, rs_num, depth + 1)))
1307 } else if (pf->opts & PF_OPT_VERBOSE)
1311 if (brace && pf->opts & PF_OPT_VERBOSE) {
1312 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE));
1325 pfctl_load_rule(struct pfctl *pf, char *path, struct pf_rule *r, int depth)
1327 u_int8_t rs_num = pf_get_ruleset_number(r->action);
1329 struct pfioc_rule pr;
1330 int len = strlen(path);
1332 bzero(&pr, sizeof(pr));
1333 /* set up anchor before adding to path for anchor_call */
1334 if ((pf->opts & PF_OPT_NOACTION) == 0)
1335 pr.ticket = pfctl_get_ticket(pf->trans, rs_num, path);
1336 if (strlcpy(pr.anchor, path, sizeof(pr.anchor)) >= sizeof(pr.anchor))
1337 errx(1, "pfctl_load_rule: strlcpy");
1340 if (r->anchor->match) {
1342 snprintf(&path[len], MAXPATHLEN - len,
1343 "/%s", r->anchor->name);
1345 snprintf(&path[len], MAXPATHLEN - len,
1346 "%s", r->anchor->name);
1349 name = r->anchor->path;
1353 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1354 if (pfctl_add_pool(pf, &r->rpool, r->af))
1356 pr.pool_ticket = pf->paddr.ticket;
1357 memcpy(&pr.rule, r, sizeof(pr.rule));
1358 if (r->anchor && strlcpy(pr.anchor_call, name,
1359 sizeof(pr.anchor_call)) >= sizeof(pr.anchor_call))
1360 errx(1, "pfctl_load_rule: strlcpy");
1361 if (ioctl(pf->dev, DIOCADDRULE, &pr))
1362 err(1, "DIOCADDRULE");
1365 if (pf->opts & PF_OPT_VERBOSE) {
1366 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2));
1367 print_rule(r, r->anchor ? r->anchor->name : "",
1368 pf->opts & PF_OPT_VERBOSE2,
1369 pf->opts & PF_OPT_NUMERIC);
1372 pfctl_clear_pool(&r->rpool);
1377 pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
1380 (loadopt & PFCTL_FLAG_ALTQ) != 0) {
1381 memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq));
1382 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1383 if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) {
1385 errx(1, "qtype not configured");
1386 else if (errno == ENODEV)
1387 errx(1, "%s: driver does not support "
1390 err(1, "DIOCADDALTQ");
1393 pfaltq_store(&pf->paltq->altq);
1399 pfctl_rules(int dev, char *filename, int opts, int optimize,
1400 char *anchorname, struct pfr_buffer *trans)
1402 #define ERR(x) do { warn(x); goto _error; } while(0)
1403 #define ERRX(x) do { warnx(x); goto _error; } while(0)
1405 struct pfr_buffer *t, buf;
1406 struct pfioc_altq pa;
1408 struct pf_ruleset *rs;
1409 struct pfr_table trs;
1413 RB_INIT(&pf_anchors);
1414 memset(&pf_main_anchor, 0, sizeof(pf_main_anchor));
1415 pf_init_ruleset(&pf_main_anchor.ruleset);
1416 pf_main_anchor.ruleset.anchor = &pf_main_anchor;
1417 if (trans == NULL) {
1418 bzero(&buf, sizeof(buf));
1419 buf.pfrb_type = PFRB_TRANS;
1424 osize = t->pfrb_size;
1427 memset(&pa, 0, sizeof(pa));
1428 memset(&pf, 0, sizeof(pf));
1429 memset(&trs, 0, sizeof(trs));
1430 if ((path = calloc(1, MAXPATHLEN)) == NULL)
1431 ERRX("pfctl_rules: calloc");
1432 if (strlcpy(trs.pfrt_anchor, anchorname,
1433 sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor))
1434 ERRX("pfctl_rules: strlcpy");
1437 pf.optimize = optimize;
1438 pf.loadopt = loadopt;
1440 /* non-brace anchor, create without resolving the path */
1441 if ((pf.anchor = calloc(1, sizeof(*pf.anchor))) == NULL)
1442 ERRX("pfctl_rules: calloc");
1443 rs = &pf.anchor->ruleset;
1444 pf_init_ruleset(rs);
1445 rs->anchor = pf.anchor;
1446 if (strlcpy(pf.anchor->path, anchorname,
1447 sizeof(pf.anchor->path)) >= sizeof(pf.anchor->path))
1448 errx(1, "pfctl_add_rule: strlcpy");
1449 if (strlcpy(pf.anchor->name, anchorname,
1450 sizeof(pf.anchor->name)) >= sizeof(pf.anchor->name))
1451 errx(1, "pfctl_add_rule: strlcpy");
1454 pf.astack[0] = pf.anchor;
1457 pf.loadopt &= ~PFCTL_FLAG_ALTQ;
1460 pfctl_init_options(&pf);
1462 if ((opts & PF_OPT_NOACTION) == 0) {
1464 * XXX For the time being we need to open transactions for
1465 * the main ruleset before parsing, because tables are still
1466 * loaded at parse time.
1468 if (pfctl_ruleset_trans(&pf, anchorname, pf.anchor))
1469 ERRX("pfctl_rules");
1470 if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ))
1472 pfctl_get_ticket(t, PF_RULESET_ALTQ, anchorname);
1473 if (pf.loadopt & PFCTL_FLAG_TABLE)
1474 pf.astack[0]->ruleset.tticket =
1475 pfctl_get_ticket(t, PF_RULESET_TABLE, anchorname);
1478 if (parse_config(filename, &pf) < 0) {
1479 if ((opts & PF_OPT_NOACTION) == 0)
1480 ERRX("Syntax error in config file: "
1481 "pf rules not loaded");
1486 if ((pf.loadopt & PFCTL_FLAG_FILTER &&
1487 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_SCRUB, 0))) ||
1488 (pf.loadopt & PFCTL_FLAG_NAT &&
1489 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_NAT, 0) ||
1490 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_RDR, 0) ||
1491 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_BINAT, 0))) ||
1492 (pf.loadopt & PFCTL_FLAG_FILTER &&
1493 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_FILTER, 0))) {
1494 if ((opts & PF_OPT_NOACTION) == 0)
1495 ERRX("Unable to load rules into kernel");
1500 if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))
1501 if (check_commit_altq(dev, opts) != 0)
1502 ERRX("errors in altq config");
1504 /* process "load anchor" directives */
1506 if (pfctl_load_anchors(dev, &pf, t) == -1)
1507 ERRX("load anchors");
1509 if (trans == NULL && (opts & PF_OPT_NOACTION) == 0) {
1511 if (pfctl_load_options(&pf))
1513 if (pfctl_trans(dev, t, DIOCXCOMMIT, osize))
1519 if (trans == NULL) { /* main ruleset */
1520 if ((opts & PF_OPT_NOACTION) == 0)
1521 if (pfctl_trans(dev, t, DIOCXROLLBACK, osize))
1522 err(1, "DIOCXROLLBACK");
1524 } else { /* sub ruleset */
1533 pfctl_fopen(const char *name, const char *mode)
1538 fp = fopen(name, mode);
1541 if (fstat(fileno(fp), &st)) {
1545 if (S_ISDIR(st.st_mode)) {
1554 pfctl_init_options(struct pfctl *pf)
1557 pf->timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
1558 pf->timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
1559 pf->timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
1560 pf->timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
1561 pf->timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
1562 pf->timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
1563 pf->timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
1564 pf->timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
1565 pf->timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
1566 pf->timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
1567 pf->timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
1568 pf->timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
1569 pf->timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
1570 pf->timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
1571 pf->timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
1572 pf->timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
1573 pf->timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
1574 pf->timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
1575 pf->timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
1576 pf->timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
1578 pf->limit[PF_LIMIT_STATES] = PFSTATE_HIWAT;
1579 pf->limit[PF_LIMIT_FRAGS] = PFFRAG_FRENT_HIWAT;
1580 pf->limit[PF_LIMIT_SRC_NODES] = PFSNODE_HIWAT;
1581 pf->limit[PF_LIMIT_TABLE_ENTRIES] = PFR_KENTRY_HIWAT;
1583 pf->debug = PF_DEBUG_URGENT;
1587 pfctl_load_options(struct pfctl *pf)
1591 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1595 for (i = 0; i < PF_LIMIT_MAX; i++) {
1596 if ((pf->opts & PF_OPT_MERGE) && !pf->limit_set[i])
1598 if (pfctl_load_limit(pf, i, pf->limit[i]))
1603 * If we've set the limit, but haven't explicitly set adaptive
1604 * timeouts, do it now with a start of 60% and end of 120%.
1606 if (pf->limit_set[PF_LIMIT_STATES] &&
1607 !pf->timeout_set[PFTM_ADAPTIVE_START] &&
1608 !pf->timeout_set[PFTM_ADAPTIVE_END]) {
1609 pf->timeout[PFTM_ADAPTIVE_START] =
1610 (pf->limit[PF_LIMIT_STATES] / 10) * 6;
1611 pf->timeout_set[PFTM_ADAPTIVE_START] = 1;
1612 pf->timeout[PFTM_ADAPTIVE_END] =
1613 (pf->limit[PF_LIMIT_STATES] / 10) * 12;
1614 pf->timeout_set[PFTM_ADAPTIVE_END] = 1;
1618 for (i = 0; i < PFTM_MAX; i++) {
1619 if ((pf->opts & PF_OPT_MERGE) && !pf->timeout_set[i])
1621 if (pfctl_load_timeout(pf, i, pf->timeout[i]))
1626 if (!(pf->opts & PF_OPT_MERGE) || pf->debug_set)
1627 if (pfctl_load_debug(pf, pf->debug))
1631 if (!(pf->opts & PF_OPT_MERGE) || pf->ifname_set)
1632 if (pfctl_load_logif(pf, pf->ifname))
1636 if (!(pf->opts & PF_OPT_MERGE) || pf->hostid_set)
1637 if (pfctl_load_hostid(pf, pf->hostid))
1644 pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit)
1649 for (i = 0; pf_limits[i].name; i++) {
1650 if (strcasecmp(opt, pf_limits[i].name) == 0) {
1651 pf->limit[pf_limits[i].index] = limit;
1652 pf->limit_set[pf_limits[i].index] = 1;
1656 if (pf_limits[i].name == NULL) {
1657 warnx("Bad pool name.");
1661 if (pf->opts & PF_OPT_VERBOSE)
1662 printf("set limit %s %d\n", opt, limit);
1668 pfctl_load_limit(struct pfctl *pf, unsigned int index, unsigned int limit)
1670 struct pfioc_limit pl;
1672 memset(&pl, 0, sizeof(pl));
1675 if (ioctl(pf->dev, DIOCSETLIMIT, &pl)) {
1677 warnx("Current pool size exceeds requested hard limit");
1679 warnx("DIOCSETLIMIT");
1686 pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
1690 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1693 for (i = 0; pf_timeouts[i].name; i++) {
1694 if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
1695 pf->timeout[pf_timeouts[i].timeout] = seconds;
1696 pf->timeout_set[pf_timeouts[i].timeout] = 1;
1701 if (pf_timeouts[i].name == NULL) {
1702 warnx("Bad timeout name.");
1707 if (pf->opts & PF_OPT_VERBOSE && ! quiet)
1708 printf("set timeout %s %d\n", opt, seconds);
1714 pfctl_load_timeout(struct pfctl *pf, unsigned int timeout, unsigned int seconds)
1718 memset(&pt, 0, sizeof(pt));
1719 pt.timeout = timeout;
1720 pt.seconds = seconds;
1721 if (ioctl(pf->dev, DIOCSETTIMEOUT, &pt)) {
1722 warnx("DIOCSETTIMEOUT");
1729 pfctl_set_optimization(struct pfctl *pf, const char *opt)
1731 const struct pf_hint *hint;
1734 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1737 for (i = 0; pf_hints[i].name; i++)
1738 if (strcasecmp(opt, pf_hints[i].name) == 0)
1741 hint = pf_hints[i].hint;
1743 warnx("invalid state timeouts optimization");
1747 for (i = 0; hint[i].name; i++)
1748 if ((r = pfctl_set_timeout(pf, hint[i].name,
1749 hint[i].timeout, 1)))
1752 if (pf->opts & PF_OPT_VERBOSE)
1753 printf("set optimization %s\n", opt);
1759 pfctl_set_logif(struct pfctl *pf, char *ifname)
1762 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1765 if (!strcmp(ifname, "none")) {
1769 pf->ifname = strdup(ifname);
1771 errx(1, "pfctl_set_logif: strdup");
1775 if (pf->opts & PF_OPT_VERBOSE)
1776 printf("set loginterface %s\n", ifname);
1782 pfctl_load_logif(struct pfctl *pf, char *ifname)
1786 memset(&pi, 0, sizeof(pi));
1787 if (ifname && strlcpy(pi.ifname, ifname,
1788 sizeof(pi.ifname)) >= sizeof(pi.ifname)) {
1789 warnx("pfctl_load_logif: strlcpy");
1792 if (ioctl(pf->dev, DIOCSETSTATUSIF, &pi)) {
1793 warnx("DIOCSETSTATUSIF");
1800 pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
1802 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1807 pf->hostid = hostid;
1810 if (pf->opts & PF_OPT_VERBOSE)
1811 printf("set hostid 0x%08x\n", ntohl(hostid));
1817 pfctl_load_hostid(struct pfctl *pf, u_int32_t hostid)
1819 if (ioctl(dev, DIOCSETHOSTID, &hostid)) {
1820 warnx("DIOCSETHOSTID");
1827 pfctl_set_debug(struct pfctl *pf, char *d)
1831 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1834 if (!strcmp(d, "none"))
1835 pf->debug = PF_DEBUG_NONE;
1836 else if (!strcmp(d, "urgent"))
1837 pf->debug = PF_DEBUG_URGENT;
1838 else if (!strcmp(d, "misc"))
1839 pf->debug = PF_DEBUG_MISC;
1840 else if (!strcmp(d, "loud"))
1841 pf->debug = PF_DEBUG_NOISY;
1843 warnx("unknown debug level \"%s\"", d);
1849 if ((pf->opts & PF_OPT_NOACTION) == 0)
1850 if (ioctl(dev, DIOCSETDEBUG, &level))
1851 err(1, "DIOCSETDEBUG");
1853 if (pf->opts & PF_OPT_VERBOSE)
1854 printf("set debug %s\n", d);
1860 pfctl_load_debug(struct pfctl *pf, unsigned int level)
1862 if (ioctl(pf->dev, DIOCSETDEBUG, &level)) {
1863 warnx("DIOCSETDEBUG");
1870 pfctl_set_interface_flags(struct pfctl *pf, char *ifname, int flags, int how)
1872 struct pfioc_iface pi;
1874 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1877 bzero(&pi, sizeof(pi));
1879 pi.pfiio_flags = flags;
1881 if (strlcpy(pi.pfiio_name, ifname, sizeof(pi.pfiio_name)) >=
1882 sizeof(pi.pfiio_name))
1883 errx(1, "pfctl_set_interface_flags: strlcpy");
1885 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1887 if (ioctl(pf->dev, DIOCCLRIFFLAG, &pi))
1888 err(1, "DIOCCLRIFFLAG");
1890 if (ioctl(pf->dev, DIOCSETIFFLAG, &pi))
1891 err(1, "DIOCSETIFFLAG");
1898 pfctl_debug(int dev, u_int32_t level, int opts)
1900 if (ioctl(dev, DIOCSETDEBUG, &level))
1901 err(1, "DIOCSETDEBUG");
1902 if ((opts & PF_OPT_QUIET) == 0) {
1903 fprintf(stderr, "debug level set to '");
1906 fprintf(stderr, "none");
1908 case PF_DEBUG_URGENT:
1909 fprintf(stderr, "urgent");
1912 fprintf(stderr, "misc");
1914 case PF_DEBUG_NOISY:
1915 fprintf(stderr, "loud");
1918 fprintf(stderr, "<invalid>");
1921 fprintf(stderr, "'\n");
1926 pfctl_test_altqsupport(int dev, int opts)
1928 struct pfioc_altq pa;
1930 if (ioctl(dev, DIOCGETALTQS, &pa)) {
1931 if (errno == ENODEV) {
1932 if (!(opts & PF_OPT_QUIET))
1933 fprintf(stderr, "No ALTQ support in kernel\n"
1934 "ALTQ related functions disabled\n");
1937 err(1, "DIOCGETALTQS");
1943 pfctl_show_anchors(int dev, int opts, char *anchorname)
1945 struct pfioc_ruleset pr;
1948 memset(&pr, 0, sizeof(pr));
1949 memcpy(pr.path, anchorname, sizeof(pr.path));
1950 if (ioctl(dev, DIOCGETRULESETS, &pr)) {
1951 if (errno == EINVAL)
1952 fprintf(stderr, "Anchor '%s' not found.\n",
1955 err(1, "DIOCGETRULESETS");
1959 for (nr = 0; nr < mnr; ++nr) {
1960 char sub[MAXPATHLEN];
1963 if (ioctl(dev, DIOCGETRULESET, &pr))
1964 err(1, "DIOCGETRULESET");
1965 if (!strcmp(pr.name, PF_RESERVED_ANCHOR))
1969 strlcat(sub, pr.path, sizeof(sub));
1970 strlcat(sub, "/", sizeof(sub));
1972 strlcat(sub, pr.name, sizeof(sub));
1973 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE))
1974 printf(" %s\n", sub);
1975 if ((opts & PF_OPT_VERBOSE) && pfctl_show_anchors(dev, opts, sub))
1982 pfctl_lookup_option(char *cmd, const char **list)
1984 if (cmd != NULL && *cmd)
1985 for (; *list; list++)
1986 if (!strncmp(cmd, *list, strlen(cmd)))
1992 main(int argc, char *argv[])
1996 int mode = O_RDONLY;
1998 int optimize = PF_OPTIMIZE_BASIC;
1999 char anchorname[MAXPATHLEN];
2005 while ((ch = getopt(argc, argv,
2006 "a:AdD:eqf:F:ghi:k:K:mnNOo:Pp:rRs:t:T:vx:z")) != -1) {
2012 opts |= PF_OPT_DISABLE;
2016 if (pfctl_cmdline_symset(optarg) < 0)
2017 warnx("could not parse macro definition %s",
2021 opts |= PF_OPT_ENABLE;
2025 opts |= PF_OPT_QUIET;
2028 clearopt = pfctl_lookup_option(optarg, clearopt_list);
2029 if (clearopt == NULL) {
2030 warnx("Unknown flush modifier '%s'", optarg);
2039 if (state_killers >= 2) {
2040 warnx("can only specify -k twice");
2044 state_kill[state_killers++] = optarg;
2048 if (src_node_killers >= 2) {
2049 warnx("can only specify -K twice");
2053 src_node_kill[src_node_killers++] = optarg;
2057 opts |= PF_OPT_MERGE;
2060 opts |= PF_OPT_NOACTION;
2063 loadopt |= PFCTL_FLAG_NAT;
2066 opts |= PF_OPT_USEDNS;
2073 opts |= PF_OPT_DEBUG;
2076 loadopt |= PFCTL_FLAG_ALTQ;
2079 loadopt |= PFCTL_FLAG_FILTER;
2082 optiopt = pfctl_lookup_option(optarg, optiopt_list);
2083 if (optiopt == NULL) {
2084 warnx("Unknown optimization '%s'", optarg);
2087 opts |= PF_OPT_OPTIMIZE;
2090 loadopt |= PFCTL_FLAG_OPTION;
2096 opts |= PF_OPT_NUMERIC;
2099 showopt = pfctl_lookup_option(optarg, showopt_list);
2100 if (showopt == NULL) {
2101 warnx("Unknown show modifier '%s'", optarg);
2109 tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
2110 if (tblcmdopt == NULL) {
2111 warnx("Unknown table command '%s'", optarg);
2116 if (opts & PF_OPT_VERBOSE)
2117 opts |= PF_OPT_VERBOSE2;
2118 opts |= PF_OPT_VERBOSE;
2121 debugopt = pfctl_lookup_option(optarg, debugopt_list);
2122 if (debugopt == NULL) {
2123 warnx("Unknown debug level '%s'", optarg);
2129 opts |= PF_OPT_CLRRULECTRS;
2140 if (tblcmdopt != NULL) {
2145 loadopt |= PFCTL_FLAG_TABLE;
2148 mode = strchr("acdefkrz", ch) ? O_RDWR : O_RDONLY;
2149 } else if (argc != optind) {
2150 warnx("unknown command line argument: %s ...", argv[optind]);
2157 if ((path = calloc(1, MAXPATHLEN)) == NULL)
2158 errx(1, "pfctl: calloc");
2159 memset(anchorname, 0, sizeof(anchorname));
2160 if (anchoropt != NULL) {
2161 int len = strlen(anchoropt);
2163 if (anchoropt[len - 1] == '*') {
2164 if (len >= 2 && anchoropt[len - 2] == '/')
2165 anchoropt[len - 2] = '\0';
2167 anchoropt[len - 1] = '\0';
2168 opts |= PF_OPT_RECURSE;
2170 if (strlcpy(anchorname, anchoropt,
2171 sizeof(anchorname)) >= sizeof(anchorname))
2172 errx(1, "anchor name '%s' too long",
2174 loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE;
2177 if ((opts & PF_OPT_NOACTION) == 0) {
2178 dev = open(pf_device, mode);
2180 err(1, "%s", pf_device);
2181 altqsupport = pfctl_test_altqsupport(dev, opts);
2183 dev = open(pf_device, O_RDONLY);
2185 opts |= PF_OPT_DUMMYACTION;
2186 /* turn off options */
2187 opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
2188 clearopt = showopt = debugopt = NULL;
2189 #if defined(__FreeBSD__) && !defined(ENABLE_ALTQ)
2196 if (opts & PF_OPT_DISABLE)
2197 if (pfctl_disable(dev, opts))
2200 if (showopt != NULL) {
2203 pfctl_show_anchors(dev, opts, anchorname);
2206 pfctl_load_fingerprints(dev, opts);
2207 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
2211 pfctl_load_fingerprints(dev, opts);
2212 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
2216 pfctl_load_fingerprints(dev, opts);
2217 pfctl_show_nat(dev, opts, anchorname);
2220 pfctl_show_altq(dev, ifaceopt, opts,
2221 opts & PF_OPT_VERBOSE2);
2224 pfctl_show_states(dev, ifaceopt, opts);
2227 pfctl_show_src_nodes(dev, opts);
2230 pfctl_show_status(dev, opts);
2233 pfctl_show_timeouts(dev, opts);
2236 pfctl_show_limits(dev, opts);
2239 opts |= PF_OPT_SHOWALL;
2240 pfctl_load_fingerprints(dev, opts);
2242 pfctl_show_nat(dev, opts, anchorname);
2243 pfctl_show_rules(dev, path, opts, 0, anchorname, 0);
2244 pfctl_show_altq(dev, ifaceopt, opts, 0);
2245 pfctl_show_states(dev, ifaceopt, opts);
2246 pfctl_show_src_nodes(dev, opts);
2247 pfctl_show_status(dev, opts);
2248 pfctl_show_rules(dev, path, opts, 1, anchorname, 0);
2249 pfctl_show_timeouts(dev, opts);
2250 pfctl_show_limits(dev, opts);
2251 pfctl_show_tables(anchorname, opts);
2252 pfctl_show_fingerprints(opts);
2255 pfctl_show_tables(anchorname, opts);
2258 pfctl_load_fingerprints(dev, opts);
2259 pfctl_show_fingerprints(opts);
2262 pfctl_show_ifaces(ifaceopt, opts);
2267 if ((opts & PF_OPT_CLRRULECTRS) && showopt == NULL)
2268 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
2271 if (clearopt != NULL) {
2272 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL)
2273 errx(1, "anchor names beginning with '_' cannot "
2274 "be modified from the command line");
2276 switch (*clearopt) {
2278 pfctl_clear_rules(dev, opts, anchorname);
2281 pfctl_clear_nat(dev, opts, anchorname);
2284 pfctl_clear_altq(dev, opts);
2287 pfctl_clear_states(dev, ifaceopt, opts);
2290 pfctl_clear_src_nodes(dev, opts);
2293 pfctl_clear_stats(dev, opts);
2296 pfctl_clear_rules(dev, opts, anchorname);
2297 pfctl_clear_nat(dev, opts, anchorname);
2298 pfctl_clear_tables(anchorname, opts);
2300 pfctl_clear_altq(dev, opts);
2301 pfctl_clear_states(dev, ifaceopt, opts);
2302 pfctl_clear_src_nodes(dev, opts);
2303 pfctl_clear_stats(dev, opts);
2304 pfctl_clear_fingerprints(dev, opts);
2305 pfctl_clear_interface_flags(dev, opts);
2309 pfctl_clear_fingerprints(dev, opts);
2312 pfctl_clear_tables(anchorname, opts);
2316 if (state_killers) {
2317 if (!strcmp(state_kill[0], "label"))
2318 pfctl_label_kill_states(dev, ifaceopt, opts);
2319 else if (!strcmp(state_kill[0], "id"))
2320 pfctl_id_kill_states(dev, ifaceopt, opts);
2322 pfctl_net_kill_states(dev, ifaceopt, opts);
2325 if (src_node_killers)
2326 pfctl_kill_src_nodes(dev, ifaceopt, opts);
2328 if (tblcmdopt != NULL) {
2329 error = pfctl_command_tables(argc, argv, tableopt,
2330 tblcmdopt, rulesopt, anchorname, opts);
2333 if (optiopt != NULL) {
2339 optimize |= PF_OPTIMIZE_BASIC;
2343 optimize |= PF_OPTIMIZE_PROFILE;
2348 if ((rulesopt != NULL) && (loadopt & PFCTL_FLAG_OPTION) &&
2350 if (pfctl_clear_interface_flags(dev, opts | PF_OPT_QUIET))
2353 if (rulesopt != NULL && !(opts & (PF_OPT_MERGE|PF_OPT_NOACTION)) &&
2354 !anchorname[0] && (loadopt & PFCTL_FLAG_OPTION))
2355 if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
2358 if (rulesopt != NULL) {
2359 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL)
2360 errx(1, "anchor names beginning with '_' cannot "
2361 "be modified from the command line");
2362 if (pfctl_rules(dev, rulesopt, opts, optimize,
2365 else if (!(opts & PF_OPT_NOACTION) &&
2366 (loadopt & PFCTL_FLAG_TABLE))
2367 warn_namespace_collision(NULL);
2370 if (opts & PF_OPT_ENABLE)
2371 if (pfctl_enable(dev, opts))
2374 if (debugopt != NULL) {
2375 switch (*debugopt) {
2377 pfctl_debug(dev, PF_DEBUG_NONE, opts);
2380 pfctl_debug(dev, PF_DEBUG_URGENT, opts);
2383 pfctl_debug(dev, PF_DEBUG_MISC, opts);
2386 pfctl_debug(dev, PF_DEBUG_NOISY, opts);