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>
63 #include "pfctl_parser.h"
67 int pfctl_enable(int, int);
68 int pfctl_disable(int, int);
69 int pfctl_clear_stats(int, int);
70 int pfctl_clear_interface_flags(int, int);
71 int pfctl_clear_rules(int, int, char *);
72 int pfctl_clear_nat(int, int, char *);
73 int pfctl_clear_altq(int, int);
74 int pfctl_clear_src_nodes(int, int);
75 int pfctl_clear_states(int, const char *, int);
76 void pfctl_addrprefix(char *, struct pf_addr *);
77 int pfctl_kill_src_nodes(int, const char *, int);
78 int pfctl_net_kill_states(int, const char *, int);
79 int pfctl_label_kill_states(int, const char *, int);
80 int pfctl_id_kill_states(int, const char *, int);
81 void pfctl_init_options(struct pfctl *);
82 int pfctl_load_options(struct pfctl *);
83 int pfctl_load_limit(struct pfctl *, unsigned int, unsigned int);
84 int pfctl_load_timeout(struct pfctl *, unsigned int, unsigned int);
85 int pfctl_load_debug(struct pfctl *, unsigned int);
86 int pfctl_load_logif(struct pfctl *, char *);
87 int pfctl_load_hostid(struct pfctl *, unsigned int);
88 int pfctl_get_pool(int, struct pf_pool *, u_int32_t, u_int32_t, int,
90 void pfctl_print_rule_counters(struct pf_rule *, int);
91 int pfctl_show_rules(int, char *, int, enum pfctl_show, char *, int);
92 int pfctl_show_nat(int, int, char *);
93 int pfctl_show_src_nodes(int, int);
94 int pfctl_show_states(int, const char *, int);
95 int pfctl_show_status(int, int);
96 int pfctl_show_timeouts(int, int);
97 int pfctl_show_limits(int, int);
98 void pfctl_debug(int, u_int32_t, int);
99 int pfctl_test_altqsupport(int, int);
100 int pfctl_show_anchors(int, int, char *);
101 int pfctl_ruleset_trans(struct pfctl *, char *, struct pf_anchor *);
102 int pfctl_load_ruleset(struct pfctl *, char *,
103 struct pf_ruleset *, int, int);
104 int pfctl_load_rule(struct pfctl *, char *, struct pf_rule *, int);
105 const char *pfctl_lookup_option(char *, const char **);
107 struct pf_anchor_global pf_anchors;
108 struct pf_anchor pf_main_anchor;
110 const char *clearopt;
113 const char *debugopt;
115 const char *optiopt = NULL;
116 char *pf_device = "/dev/pf";
119 const char *tblcmdopt;
120 int src_node_killers;
121 char *src_node_kill[2];
131 #define INDENT(d, o) do { \
134 for (i=0; i < d; i++) \
140 static const struct {
144 { "states", PF_LIMIT_STATES },
145 { "src-nodes", PF_LIMIT_SRC_NODES },
146 { "frags", PF_LIMIT_FRAGS },
147 { "table-entries", PF_LIMIT_TABLE_ENTRIES },
155 static const struct pf_hint pf_hint_normal[] = {
156 { "tcp.first", 2 * 60 },
157 { "tcp.opening", 30 },
158 { "tcp.established", 24 * 60 * 60 },
159 { "tcp.closing", 15 * 60 },
160 { "tcp.finwait", 45 },
161 { "tcp.closed", 90 },
162 { "tcp.tsdiff", 30 },
165 static const struct pf_hint pf_hint_satellite[] = {
166 { "tcp.first", 3 * 60 },
167 { "tcp.opening", 30 + 5 },
168 { "tcp.established", 24 * 60 * 60 },
169 { "tcp.closing", 15 * 60 + 5 },
170 { "tcp.finwait", 45 + 5 },
171 { "tcp.closed", 90 + 5 },
172 { "tcp.tsdiff", 60 },
175 static const struct pf_hint pf_hint_conservative[] = {
176 { "tcp.first", 60 * 60 },
177 { "tcp.opening", 15 * 60 },
178 { "tcp.established", 5 * 24 * 60 * 60 },
179 { "tcp.closing", 60 * 60 },
180 { "tcp.finwait", 10 * 60 },
181 { "tcp.closed", 3 * 60 },
182 { "tcp.tsdiff", 60 },
185 static const struct pf_hint pf_hint_aggressive[] = {
187 { "tcp.opening", 5 },
188 { "tcp.established", 5 * 60 * 60 },
189 { "tcp.closing", 60 },
190 { "tcp.finwait", 30 },
191 { "tcp.closed", 30 },
192 { "tcp.tsdiff", 10 },
196 static const struct {
198 const struct pf_hint *hint;
200 { "normal", pf_hint_normal },
201 { "satellite", pf_hint_satellite },
202 { "high-latency", pf_hint_satellite },
203 { "conservative", pf_hint_conservative },
204 { "aggressive", pf_hint_aggressive },
208 static const char *clearopt_list[] = {
209 "nat", "queue", "rules", "Sources",
210 "states", "info", "Tables", "osfp", "all", NULL
213 static const char *showopt_list[] = {
214 "nat", "queue", "rules", "Anchors", "Sources", "states", "info",
215 "Interfaces", "labels", "timeouts", "memory", "Tables", "osfp",
219 static const char *tblcmdopt_list[] = {
220 "kill", "flush", "add", "delete", "load", "replace", "show",
221 "test", "zero", "expire", NULL
224 static const char *debugopt_list[] = {
225 "none", "urgent", "misc", "loud", NULL
228 static const char *optiopt_list[] = {
229 "none", "basic", "profile", NULL
235 extern char *__progname;
237 fprintf(stderr, "usage: %s [-AdeghmNnOPqRrvz] ", __progname);
238 fprintf(stderr, "[-a anchor] [-D macro=value] [-F modifier]\n");
239 fprintf(stderr, "\t[-f file] [-i interface] [-K host | network]\n");
240 fprintf(stderr, "\t[-k host | network | label | id] ");
241 fprintf(stderr, "[-o level] [-p device]\n");
242 fprintf(stderr, "\t[-s modifier] ");
243 fprintf(stderr, "[-t table -T command [address ...]] [-x level]\n");
248 pfctl_enable(int dev, int opts)
250 if (ioctl(dev, DIOCSTART)) {
252 errx(1, "pf already enabled");
254 else if (errno == ESRCH)
255 errx(1, "pfil registeration failed");
260 if ((opts & PF_OPT_QUIET) == 0)
261 fprintf(stderr, "pf enabled\n");
263 if (altqsupport && ioctl(dev, DIOCSTARTALTQ))
265 err(1, "DIOCSTARTALTQ");
271 pfctl_disable(int dev, int opts)
273 if (ioctl(dev, DIOCSTOP)) {
275 errx(1, "pf not enabled");
279 if ((opts & PF_OPT_QUIET) == 0)
280 fprintf(stderr, "pf disabled\n");
282 if (altqsupport && ioctl(dev, DIOCSTOPALTQ))
284 err(1, "DIOCSTOPALTQ");
290 pfctl_clear_stats(int dev, int opts)
292 if (ioctl(dev, DIOCCLRSTATUS))
293 err(1, "DIOCCLRSTATUS");
294 if ((opts & PF_OPT_QUIET) == 0)
295 fprintf(stderr, "pf: statistics cleared\n");
300 pfctl_clear_interface_flags(int dev, int opts)
302 struct pfioc_iface pi;
304 if ((opts & PF_OPT_NOACTION) == 0) {
305 bzero(&pi, sizeof(pi));
306 pi.pfiio_flags = PFI_IFLAG_SKIP;
308 if (ioctl(dev, DIOCCLRIFFLAG, &pi))
309 err(1, "DIOCCLRIFFLAG");
310 if ((opts & PF_OPT_QUIET) == 0)
311 fprintf(stderr, "pf: interface flags reset\n");
317 pfctl_clear_rules(int dev, int opts, char *anchorname)
321 memset(&t, 0, sizeof(t));
322 t.pfrb_type = PFRB_TRANS;
323 if (pfctl_add_trans(&t, PF_RULESET_SCRUB, anchorname) ||
324 pfctl_add_trans(&t, PF_RULESET_FILTER, anchorname) ||
325 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
326 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
327 err(1, "pfctl_clear_rules");
328 if ((opts & PF_OPT_QUIET) == 0)
329 fprintf(stderr, "rules cleared\n");
334 pfctl_clear_nat(int dev, int opts, char *anchorname)
338 memset(&t, 0, sizeof(t));
339 t.pfrb_type = PFRB_TRANS;
340 if (pfctl_add_trans(&t, PF_RULESET_NAT, anchorname) ||
341 pfctl_add_trans(&t, PF_RULESET_BINAT, anchorname) ||
342 pfctl_add_trans(&t, PF_RULESET_RDR, anchorname) ||
343 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
344 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
345 err(1, "pfctl_clear_nat");
346 if ((opts & PF_OPT_QUIET) == 0)
347 fprintf(stderr, "nat cleared\n");
352 pfctl_clear_altq(int dev, int opts)
358 memset(&t, 0, sizeof(t));
359 t.pfrb_type = PFRB_TRANS;
360 if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "") ||
361 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
362 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
363 err(1, "pfctl_clear_altq");
364 if ((opts & PF_OPT_QUIET) == 0)
365 fprintf(stderr, "altq cleared\n");
370 pfctl_clear_src_nodes(int dev, int opts)
372 if (ioctl(dev, DIOCCLRSRCNODES))
373 err(1, "DIOCCLRSRCNODES");
374 if ((opts & PF_OPT_QUIET) == 0)
375 fprintf(stderr, "source tracking entries cleared\n");
380 pfctl_clear_states(int dev, const char *iface, int opts)
382 struct pfioc_state_kill psk;
384 memset(&psk, 0, sizeof(psk));
385 if (iface != NULL && strlcpy(psk.psk_ifname, iface,
386 sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
387 errx(1, "invalid interface: %s", iface);
389 if (ioctl(dev, DIOCCLRSTATES, &psk))
390 err(1, "DIOCCLRSTATES");
391 if ((opts & PF_OPT_QUIET) == 0)
392 fprintf(stderr, "%d states cleared\n", psk.psk_killed);
397 pfctl_addrprefix(char *addr, struct pf_addr *mask)
401 int prefix, ret_ga, q, r;
402 struct addrinfo hints, *res;
404 if ((p = strchr(addr, '/')) == NULL)
408 prefix = strtonum(p, 0, 128, &errstr);
410 errx(1, "prefix is %s: %s", errstr, p);
412 bzero(&hints, sizeof(hints));
413 /* prefix only with numeric addresses */
414 hints.ai_flags |= AI_NUMERICHOST;
416 if ((ret_ga = getaddrinfo(addr, NULL, &hints, &res))) {
417 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
421 if (res->ai_family == AF_INET && prefix > 32)
422 errx(1, "prefix too long for AF_INET");
423 else if (res->ai_family == AF_INET6 && prefix > 128)
424 errx(1, "prefix too long for AF_INET6");
428 switch (res->ai_family) {
430 bzero(&mask->v4, sizeof(mask->v4));
431 mask->v4.s_addr = htonl((u_int32_t)
432 (0xffffffffffULL << (32 - prefix)));
435 bzero(&mask->v6, sizeof(mask->v6));
437 memset((void *)&mask->v6, 0xff, q);
439 *((u_char *)&mask->v6 + q) =
440 (0xff00 >> r) & 0xff;
447 pfctl_kill_src_nodes(int dev, const char *iface, int opts)
449 struct pfioc_src_node_kill psnk;
450 struct addrinfo *res[2], *resp[2];
451 struct sockaddr last_src, last_dst;
452 int killed, sources, dests;
455 killed = sources = dests = 0;
457 memset(&psnk, 0, sizeof(psnk));
458 memset(&psnk.psnk_src.addr.v.a.mask, 0xff,
459 sizeof(psnk.psnk_src.addr.v.a.mask));
460 memset(&last_src, 0xff, sizeof(last_src));
461 memset(&last_dst, 0xff, sizeof(last_dst));
463 pfctl_addrprefix(src_node_kill[0], &psnk.psnk_src.addr.v.a.mask);
465 if ((ret_ga = getaddrinfo(src_node_kill[0], NULL, NULL, &res[0]))) {
466 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
469 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
470 if (resp[0]->ai_addr == NULL)
472 /* We get lots of duplicates. Catch the easy ones */
473 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
475 last_src = *(struct sockaddr *)resp[0]->ai_addr;
477 psnk.psnk_af = resp[0]->ai_family;
480 if (psnk.psnk_af == AF_INET)
481 psnk.psnk_src.addr.v.a.addr.v4 =
482 ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
483 else if (psnk.psnk_af == AF_INET6)
484 psnk.psnk_src.addr.v.a.addr.v6 =
485 ((struct sockaddr_in6 *)resp[0]->ai_addr)->
488 errx(1, "Unknown address family %d", psnk.psnk_af);
490 if (src_node_killers > 1) {
492 memset(&psnk.psnk_dst.addr.v.a.mask, 0xff,
493 sizeof(psnk.psnk_dst.addr.v.a.mask));
494 memset(&last_dst, 0xff, sizeof(last_dst));
495 pfctl_addrprefix(src_node_kill[1],
496 &psnk.psnk_dst.addr.v.a.mask);
497 if ((ret_ga = getaddrinfo(src_node_kill[1], NULL, NULL,
499 errx(1, "getaddrinfo: %s",
500 gai_strerror(ret_ga));
503 for (resp[1] = res[1]; resp[1];
504 resp[1] = resp[1]->ai_next) {
505 if (resp[1]->ai_addr == NULL)
507 if (psnk.psnk_af != resp[1]->ai_family)
510 if (memcmp(&last_dst, resp[1]->ai_addr,
511 sizeof(last_dst)) == 0)
513 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
517 if (psnk.psnk_af == AF_INET)
518 psnk.psnk_dst.addr.v.a.addr.v4 =
519 ((struct sockaddr_in *)resp[1]->
521 else if (psnk.psnk_af == AF_INET6)
522 psnk.psnk_dst.addr.v.a.addr.v6 =
523 ((struct sockaddr_in6 *)resp[1]->
526 errx(1, "Unknown address family %d",
529 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
530 err(1, "DIOCKILLSRCNODES");
531 killed += psnk.psnk_killed;
533 freeaddrinfo(res[1]);
535 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
536 err(1, "DIOCKILLSRCNODES");
537 killed += psnk.psnk_killed;
541 freeaddrinfo(res[0]);
543 if ((opts & PF_OPT_QUIET) == 0)
544 fprintf(stderr, "killed %d src nodes from %d sources and %d "
545 "destinations\n", killed, sources, dests);
550 pfctl_net_kill_states(int dev, const char *iface, int opts)
552 struct pfioc_state_kill psk;
553 struct addrinfo *res[2], *resp[2];
554 struct sockaddr last_src, last_dst;
555 int killed, sources, dests;
558 killed = sources = dests = 0;
560 memset(&psk, 0, sizeof(psk));
561 memset(&psk.psk_src.addr.v.a.mask, 0xff,
562 sizeof(psk.psk_src.addr.v.a.mask));
563 memset(&last_src, 0xff, sizeof(last_src));
564 memset(&last_dst, 0xff, sizeof(last_dst));
565 if (iface != NULL && strlcpy(psk.psk_ifname, iface,
566 sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
567 errx(1, "invalid interface: %s", iface);
569 pfctl_addrprefix(state_kill[0], &psk.psk_src.addr.v.a.mask);
571 if ((ret_ga = getaddrinfo(state_kill[0], NULL, NULL, &res[0]))) {
572 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
575 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
576 if (resp[0]->ai_addr == NULL)
578 /* We get lots of duplicates. Catch the easy ones */
579 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
581 last_src = *(struct sockaddr *)resp[0]->ai_addr;
583 psk.psk_af = resp[0]->ai_family;
586 if (psk.psk_af == AF_INET)
587 psk.psk_src.addr.v.a.addr.v4 =
588 ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
589 else if (psk.psk_af == AF_INET6)
590 psk.psk_src.addr.v.a.addr.v6 =
591 ((struct sockaddr_in6 *)resp[0]->ai_addr)->
594 errx(1, "Unknown address family %d", psk.psk_af);
596 if (state_killers > 1) {
598 memset(&psk.psk_dst.addr.v.a.mask, 0xff,
599 sizeof(psk.psk_dst.addr.v.a.mask));
600 memset(&last_dst, 0xff, sizeof(last_dst));
601 pfctl_addrprefix(state_kill[1],
602 &psk.psk_dst.addr.v.a.mask);
603 if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL,
605 errx(1, "getaddrinfo: %s",
606 gai_strerror(ret_ga));
609 for (resp[1] = res[1]; resp[1];
610 resp[1] = resp[1]->ai_next) {
611 if (resp[1]->ai_addr == NULL)
613 if (psk.psk_af != resp[1]->ai_family)
616 if (memcmp(&last_dst, resp[1]->ai_addr,
617 sizeof(last_dst)) == 0)
619 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
623 if (psk.psk_af == AF_INET)
624 psk.psk_dst.addr.v.a.addr.v4 =
625 ((struct sockaddr_in *)resp[1]->
627 else if (psk.psk_af == AF_INET6)
628 psk.psk_dst.addr.v.a.addr.v6 =
629 ((struct sockaddr_in6 *)resp[1]->
632 errx(1, "Unknown address family %d",
635 if (ioctl(dev, DIOCKILLSTATES, &psk))
636 err(1, "DIOCKILLSTATES");
637 killed += psk.psk_killed;
639 freeaddrinfo(res[1]);
641 if (ioctl(dev, DIOCKILLSTATES, &psk))
642 err(1, "DIOCKILLSTATES");
643 killed += psk.psk_killed;
647 freeaddrinfo(res[0]);
649 if ((opts & PF_OPT_QUIET) == 0)
650 fprintf(stderr, "killed %d states from %d sources and %d "
651 "destinations\n", killed, sources, dests);
656 pfctl_label_kill_states(int dev, const char *iface, int opts)
658 struct pfioc_state_kill psk;
660 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
661 warnx("no label specified");
664 memset(&psk, 0, sizeof(psk));
665 if (iface != NULL && strlcpy(psk.psk_ifname, iface,
666 sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
667 errx(1, "invalid interface: %s", iface);
669 if (strlcpy(psk.psk_label, state_kill[1], sizeof(psk.psk_label)) >=
670 sizeof(psk.psk_label))
671 errx(1, "label too long: %s", state_kill[1]);
673 if (ioctl(dev, DIOCKILLSTATES, &psk))
674 err(1, "DIOCKILLSTATES");
676 if ((opts & PF_OPT_QUIET) == 0)
677 fprintf(stderr, "killed %d states\n", psk.psk_killed);
683 pfctl_id_kill_states(int dev, const char *iface, int opts)
685 struct pfioc_state_kill psk;
687 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
688 warnx("no id specified");
692 memset(&psk, 0, sizeof(psk));
693 if ((sscanf(state_kill[1], "%jx/%x",
694 &psk.psk_pfcmp.id, &psk.psk_pfcmp.creatorid)) == 2)
695 HTONL(psk.psk_pfcmp.creatorid);
696 else if ((sscanf(state_kill[1], "%jx", &psk.psk_pfcmp.id)) == 1) {
697 psk.psk_pfcmp.creatorid = 0;
699 warnx("wrong id format specified");
702 if (psk.psk_pfcmp.id == 0) {
703 warnx("cannot kill id 0");
707 psk.psk_pfcmp.id = htobe64(psk.psk_pfcmp.id);
708 if (ioctl(dev, DIOCKILLSTATES, &psk))
709 err(1, "DIOCKILLSTATES");
711 if ((opts & PF_OPT_QUIET) == 0)
712 fprintf(stderr, "killed %d states\n", psk.psk_killed);
718 pfctl_get_pool(int dev, struct pf_pool *pool, u_int32_t nr,
719 u_int32_t ticket, int r_action, char *anchorname)
721 struct pfioc_pooladdr pp;
722 struct pf_pooladdr *pa;
725 memset(&pp, 0, sizeof(pp));
726 memcpy(pp.anchor, anchorname, sizeof(pp.anchor));
727 pp.r_action = r_action;
730 if (ioctl(dev, DIOCGETADDRS, &pp)) {
731 warn("DIOCGETADDRS");
735 TAILQ_INIT(&pool->list);
736 for (pnr = 0; pnr < mpnr; ++pnr) {
738 if (ioctl(dev, DIOCGETADDR, &pp)) {
742 pa = calloc(1, sizeof(struct pf_pooladdr));
745 bcopy(&pp.addr, pa, sizeof(struct pf_pooladdr));
746 TAILQ_INSERT_TAIL(&pool->list, pa, entries);
753 pfctl_move_pool(struct pf_pool *src, struct pf_pool *dst)
755 struct pf_pooladdr *pa;
757 while ((pa = TAILQ_FIRST(&src->list)) != NULL) {
758 TAILQ_REMOVE(&src->list, pa, entries);
759 TAILQ_INSERT_TAIL(&dst->list, pa, entries);
764 pfctl_clear_pool(struct pf_pool *pool)
766 struct pf_pooladdr *pa;
768 while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
769 TAILQ_REMOVE(&pool->list, pa, entries);
775 pfctl_print_rule_counters(struct pf_rule *rule, int opts)
777 if (opts & PF_OPT_DEBUG) {
778 const char *t[PF_SKIP_COUNT] = { "i", "d", "f",
779 "p", "sa", "sp", "da", "dp" };
782 printf(" [ Skip steps: ");
783 for (i = 0; i < PF_SKIP_COUNT; ++i) {
784 if (rule->skip[i].nr == rule->nr + 1)
787 if (rule->skip[i].nr == -1)
790 printf("%u ", rule->skip[i].nr);
794 printf(" [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n",
795 rule->qname, rule->qid, rule->pqname, rule->pqid);
797 if (opts & PF_OPT_VERBOSE) {
798 printf(" [ Evaluations: %-8llu Packets: %-8llu "
799 "Bytes: %-10llu States: %-6u]\n",
800 (unsigned long long)rule->evaluations,
801 (unsigned long long)(rule->packets[0] +
803 (unsigned long long)(rule->bytes[0] +
804 rule->bytes[1]), rule->states_cur);
805 if (!(opts & PF_OPT_DEBUG))
806 printf(" [ Inserted: uid %u pid %u "
807 "State Creations: %-6u]\n",
808 (unsigned)rule->cuid, (unsigned)rule->cpid,
814 pfctl_print_title(char *title)
819 printf("%s\n", title);
823 pfctl_show_rules(int dev, char *path, int opts, enum pfctl_show format,
824 char *anchorname, int depth)
826 struct pfioc_rule pr;
827 u_int32_t nr, mnr, header = 0;
828 int rule_numbers = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG);
829 int numeric = opts & PF_OPT_NUMERIC;
830 int len = strlen(path);
835 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname);
837 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname);
839 memset(&pr, 0, sizeof(pr));
840 memcpy(pr.anchor, path, sizeof(pr.anchor));
841 if (opts & PF_OPT_SHOWALL) {
842 pr.rule.action = PF_PASS;
843 if (ioctl(dev, DIOCGETRULES, &pr)) {
844 warn("DIOCGETRULES");
849 pr.rule.action = PF_SCRUB;
850 if (ioctl(dev, DIOCGETRULES, &pr)) {
851 warn("DIOCGETRULES");
854 if (opts & PF_OPT_SHOWALL) {
855 if (format == PFCTL_SHOW_RULES && (pr.nr > 0 || header))
856 pfctl_print_title("FILTER RULES:");
857 else if (format == PFCTL_SHOW_LABELS && labels)
858 pfctl_print_title("LABEL COUNTERS:");
861 if (opts & PF_OPT_CLRRULECTRS)
862 pr.action = PF_GET_CLR_CNTR;
864 for (nr = 0; nr < mnr; ++nr) {
866 if (ioctl(dev, DIOCGETRULE, &pr)) {
871 if (pfctl_get_pool(dev, &pr.rule.rpool,
872 nr, pr.ticket, PF_SCRUB, path) != 0)
876 case PFCTL_SHOW_LABELS:
878 case PFCTL_SHOW_RULES:
879 if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
881 print_rule(&pr.rule, pr.anchor_call, rule_numbers, numeric);
883 pfctl_print_rule_counters(&pr.rule, opts);
885 case PFCTL_SHOW_NOTHING:
888 pfctl_clear_pool(&pr.rule.rpool);
890 pr.rule.action = PF_PASS;
891 if (ioctl(dev, DIOCGETRULES, &pr)) {
892 warn("DIOCGETRULES");
896 for (nr = 0; nr < mnr; ++nr) {
898 if (ioctl(dev, DIOCGETRULE, &pr)) {
903 if (pfctl_get_pool(dev, &pr.rule.rpool,
904 nr, pr.ticket, PF_PASS, path) != 0)
908 case PFCTL_SHOW_LABELS:
909 if (pr.rule.label[0]) {
910 printf("%s %llu %llu %llu %llu"
911 " %llu %llu %llu %llu\n",
913 (unsigned long long)pr.rule.evaluations,
914 (unsigned long long)(pr.rule.packets[0] +
916 (unsigned long long)(pr.rule.bytes[0] +
918 (unsigned long long)pr.rule.packets[0],
919 (unsigned long long)pr.rule.bytes[0],
920 (unsigned long long)pr.rule.packets[1],
921 (unsigned long long)pr.rule.bytes[1],
922 (unsigned long long)pr.rule.states_tot);
925 case PFCTL_SHOW_RULES:
927 if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
929 INDENT(depth, !(opts & PF_OPT_VERBOSE));
930 if (pr.anchor_call[0] &&
931 ((((p = strrchr(pr.anchor_call, '_')) != NULL) &&
932 ((void *)p == (void *)pr.anchor_call ||
933 *(--p) == '/')) || (opts & PF_OPT_RECURSE))) {
935 if ((p = strrchr(pr.anchor_call, '/')) !=
939 p = &pr.anchor_call[0];
941 p = &pr.anchor_call[0];
943 print_rule(&pr.rule, p, rule_numbers, numeric);
948 pfctl_print_rule_counters(&pr.rule, opts);
950 pfctl_show_rules(dev, path, opts, format,
952 INDENT(depth, !(opts & PF_OPT_VERBOSE));
956 case PFCTL_SHOW_NOTHING:
959 pfctl_clear_pool(&pr.rule.rpool);
970 pfctl_show_nat(int dev, int opts, char *anchorname)
972 struct pfioc_rule pr;
974 static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT };
975 int i, dotitle = opts & PF_OPT_SHOWALL;
977 memset(&pr, 0, sizeof(pr));
978 memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
979 for (i = 0; i < 3; i++) {
980 pr.rule.action = nattype[i];
981 if (ioctl(dev, DIOCGETRULES, &pr)) {
982 warn("DIOCGETRULES");
986 for (nr = 0; nr < mnr; ++nr) {
988 if (ioctl(dev, DIOCGETRULE, &pr)) {
992 if (pfctl_get_pool(dev, &pr.rule.rpool, nr,
993 pr.ticket, nattype[i], anchorname) != 0)
996 pfctl_print_title("TRANSLATION RULES:");
999 print_rule(&pr.rule, pr.anchor_call,
1000 opts & PF_OPT_VERBOSE2, opts & PF_OPT_NUMERIC);
1002 pfctl_print_rule_counters(&pr.rule, opts);
1003 pfctl_clear_pool(&pr.rule.rpool);
1010 pfctl_show_src_nodes(int dev, int opts)
1012 struct pfioc_src_nodes psn;
1013 struct pf_src_node *p;
1014 char *inbuf = NULL, *newinbuf = NULL;
1015 unsigned int len = 0;
1018 memset(&psn, 0, sizeof(psn));
1022 newinbuf = realloc(inbuf, len);
1023 if (newinbuf == NULL)
1025 psn.psn_buf = inbuf = newinbuf;
1027 if (ioctl(dev, DIOCGETSRCNODES, &psn) < 0) {
1028 warn("DIOCGETSRCNODES");
1032 if (psn.psn_len + sizeof(struct pfioc_src_nodes) < len)
1034 if (len == 0 && psn.psn_len == 0)
1036 if (len == 0 && psn.psn_len != 0)
1038 if (psn.psn_len == 0)
1039 goto done; /* no src_nodes */
1042 p = psn.psn_src_nodes;
1043 if (psn.psn_len > 0 && (opts & PF_OPT_SHOWALL))
1044 pfctl_print_title("SOURCE TRACKING NODES:");
1045 for (i = 0; i < psn.psn_len; i += sizeof(*p)) {
1046 print_src_node(p, opts);
1055 pfctl_show_states(int dev, const char *iface, int opts)
1057 struct pfioc_states ps;
1058 struct pfsync_state *p;
1059 char *inbuf = NULL, *newinbuf = NULL;
1060 unsigned int len = 0;
1061 int i, dotitle = (opts & PF_OPT_SHOWALL);
1063 memset(&ps, 0, sizeof(ps));
1067 newinbuf = realloc(inbuf, len);
1068 if (newinbuf == NULL)
1070 ps.ps_buf = inbuf = newinbuf;
1072 if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
1073 warn("DIOCGETSTATES");
1077 if (ps.ps_len + sizeof(struct pfioc_states) < len)
1079 if (len == 0 && ps.ps_len == 0)
1081 if (len == 0 && ps.ps_len != 0)
1084 goto done; /* no states */
1088 for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
1089 if (iface != NULL && strcmp(p->ifname, iface))
1092 pfctl_print_title("STATES:");
1095 print_state(p, opts);
1103 pfctl_show_status(int dev, int opts)
1105 struct pf_status status;
1107 if (ioctl(dev, DIOCGETSTATUS, &status)) {
1108 warn("DIOCGETSTATUS");
1111 if (opts & PF_OPT_SHOWALL)
1112 pfctl_print_title("INFO:");
1113 print_status(&status, opts);
1118 pfctl_show_timeouts(int dev, int opts)
1123 if (opts & PF_OPT_SHOWALL)
1124 pfctl_print_title("TIMEOUTS:");
1125 memset(&pt, 0, sizeof(pt));
1126 for (i = 0; pf_timeouts[i].name; i++) {
1127 pt.timeout = pf_timeouts[i].timeout;
1128 if (ioctl(dev, DIOCGETTIMEOUT, &pt))
1129 err(1, "DIOCGETTIMEOUT");
1130 printf("%-20s %10d", pf_timeouts[i].name, pt.seconds);
1131 if (pf_timeouts[i].timeout >= PFTM_ADAPTIVE_START &&
1132 pf_timeouts[i].timeout <= PFTM_ADAPTIVE_END)
1143 pfctl_show_limits(int dev, int opts)
1145 struct pfioc_limit pl;
1148 if (opts & PF_OPT_SHOWALL)
1149 pfctl_print_title("LIMITS:");
1150 memset(&pl, 0, sizeof(pl));
1151 for (i = 0; pf_limits[i].name; i++) {
1152 pl.index = pf_limits[i].index;
1153 if (ioctl(dev, DIOCGETLIMIT, &pl))
1154 err(1, "DIOCGETLIMIT");
1155 printf("%-13s ", pf_limits[i].name);
1156 if (pl.limit == UINT_MAX)
1157 printf("unlimited\n");
1159 printf("hard limit %8u\n", pl.limit);
1164 /* callbacks for rule/nat/rdr/addr */
1166 pfctl_add_pool(struct pfctl *pf, struct pf_pool *p, sa_family_t af)
1168 struct pf_pooladdr *pa;
1170 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1171 if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr))
1172 err(1, "DIOCBEGINADDRS");
1176 TAILQ_FOREACH(pa, &p->list, entries) {
1177 memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr));
1178 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1179 if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr))
1180 err(1, "DIOCADDADDR");
1187 pfctl_add_rule(struct pfctl *pf, struct pf_rule *r, const char *anchor_call)
1190 struct pf_rule *rule;
1191 struct pf_ruleset *rs;
1194 rs_num = pf_get_ruleset_number(r->action);
1195 if (rs_num == PF_RULESET_MAX)
1196 errx(1, "Invalid rule type %d", r->action);
1198 rs = &pf->anchor->ruleset;
1200 if (anchor_call[0] && r->anchor == NULL) {
1202 * Don't make non-brace anchors part of the main anchor pool.
1204 if ((r->anchor = calloc(1, sizeof(*r->anchor))) == NULL)
1205 err(1, "pfctl_add_rule: calloc");
1207 pf_init_ruleset(&r->anchor->ruleset);
1208 r->anchor->ruleset.anchor = r->anchor;
1209 if (strlcpy(r->anchor->path, anchor_call,
1210 sizeof(rule->anchor->path)) >= sizeof(rule->anchor->path))
1211 errx(1, "pfctl_add_rule: strlcpy");
1212 if ((p = strrchr(anchor_call, '/')) != NULL) {
1214 err(1, "pfctl_add_rule: bad anchor name %s",
1217 p = (char *)anchor_call;
1218 if (strlcpy(r->anchor->name, p,
1219 sizeof(rule->anchor->name)) >= sizeof(rule->anchor->name))
1220 errx(1, "pfctl_add_rule: strlcpy");
1223 if ((rule = calloc(1, sizeof(*rule))) == NULL)
1225 bcopy(r, rule, sizeof(*rule));
1226 TAILQ_INIT(&rule->rpool.list);
1227 pfctl_move_pool(&r->rpool, &rule->rpool);
1229 TAILQ_INSERT_TAIL(rs->rules[rs_num].active.ptr, rule, entries);
1234 pfctl_ruleset_trans(struct pfctl *pf, char *path, struct pf_anchor *a)
1236 int osize = pf->trans->pfrb_size;
1238 if ((pf->loadopt & PFCTL_FLAG_NAT) != 0) {
1239 if (pfctl_add_trans(pf->trans, PF_RULESET_NAT, path) ||
1240 pfctl_add_trans(pf->trans, PF_RULESET_BINAT, path) ||
1241 pfctl_add_trans(pf->trans, PF_RULESET_RDR, path))
1244 if (a == pf->astack[0] && ((altqsupport &&
1245 (pf->loadopt & PFCTL_FLAG_ALTQ) != 0))) {
1246 if (pfctl_add_trans(pf->trans, PF_RULESET_ALTQ, path))
1249 if ((pf->loadopt & PFCTL_FLAG_FILTER) != 0) {
1250 if (pfctl_add_trans(pf->trans, PF_RULESET_SCRUB, path) ||
1251 pfctl_add_trans(pf->trans, PF_RULESET_FILTER, path))
1254 if (pf->loadopt & PFCTL_FLAG_TABLE)
1255 if (pfctl_add_trans(pf->trans, PF_RULESET_TABLE, path))
1257 if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize))
1264 pfctl_load_ruleset(struct pfctl *pf, char *path, struct pf_ruleset *rs,
1265 int rs_num, int depth)
1268 int error, len = strlen(path);
1271 pf->anchor = rs->anchor;
1274 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->anchor->name);
1276 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->anchor->name);
1279 if (TAILQ_FIRST(rs->rules[rs_num].active.ptr) != NULL) {
1281 if (pf->opts & PF_OPT_VERBOSE)
1283 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
1284 (error = pfctl_ruleset_trans(pf,
1285 path, rs->anchor))) {
1286 printf("pfctl_load_rulesets: "
1287 "pfctl_ruleset_trans %d\n", error);
1290 } else if (pf->opts & PF_OPT_VERBOSE)
1295 if (pf->optimize && rs_num == PF_RULESET_FILTER)
1296 pfctl_optimize_ruleset(pf, rs);
1298 while ((r = TAILQ_FIRST(rs->rules[rs_num].active.ptr)) != NULL) {
1299 TAILQ_REMOVE(rs->rules[rs_num].active.ptr, r, entries);
1300 if ((error = pfctl_load_rule(pf, path, r, depth)))
1303 if ((error = pfctl_load_ruleset(pf, path,
1304 &r->anchor->ruleset, rs_num, depth + 1)))
1306 } else if (pf->opts & PF_OPT_VERBOSE)
1310 if (brace && pf->opts & PF_OPT_VERBOSE) {
1311 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE));
1324 pfctl_load_rule(struct pfctl *pf, char *path, struct pf_rule *r, int depth)
1326 u_int8_t rs_num = pf_get_ruleset_number(r->action);
1328 struct pfioc_rule pr;
1329 int len = strlen(path);
1331 bzero(&pr, sizeof(pr));
1332 /* set up anchor before adding to path for anchor_call */
1333 if ((pf->opts & PF_OPT_NOACTION) == 0)
1334 pr.ticket = pfctl_get_ticket(pf->trans, rs_num, path);
1335 if (strlcpy(pr.anchor, path, sizeof(pr.anchor)) >= sizeof(pr.anchor))
1336 errx(1, "pfctl_load_rule: strlcpy");
1339 if (r->anchor->match) {
1341 snprintf(&path[len], MAXPATHLEN - len,
1342 "/%s", r->anchor->name);
1344 snprintf(&path[len], MAXPATHLEN - len,
1345 "%s", r->anchor->name);
1348 name = r->anchor->path;
1352 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1353 if (pfctl_add_pool(pf, &r->rpool, r->af))
1355 pr.pool_ticket = pf->paddr.ticket;
1356 memcpy(&pr.rule, r, sizeof(pr.rule));
1357 if (r->anchor && strlcpy(pr.anchor_call, name,
1358 sizeof(pr.anchor_call)) >= sizeof(pr.anchor_call))
1359 errx(1, "pfctl_load_rule: strlcpy");
1360 if (ioctl(pf->dev, DIOCADDRULE, &pr))
1361 err(1, "DIOCADDRULE");
1364 if (pf->opts & PF_OPT_VERBOSE) {
1365 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2));
1366 print_rule(r, r->anchor ? r->anchor->name : "",
1367 pf->opts & PF_OPT_VERBOSE2,
1368 pf->opts & PF_OPT_NUMERIC);
1371 pfctl_clear_pool(&r->rpool);
1376 pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
1379 (loadopt & PFCTL_FLAG_ALTQ) != 0) {
1380 memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq));
1381 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1382 if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) {
1384 errx(1, "qtype not configured");
1385 else if (errno == ENODEV)
1386 errx(1, "%s: driver does not support "
1389 err(1, "DIOCADDALTQ");
1392 pfaltq_store(&pf->paltq->altq);
1398 pfctl_rules(int dev, char *filename, int opts, int optimize,
1399 char *anchorname, struct pfr_buffer *trans)
1401 #define ERR(x) do { warn(x); goto _error; } while(0)
1402 #define ERRX(x) do { warnx(x); goto _error; } while(0)
1404 struct pfr_buffer *t, buf;
1405 struct pfioc_altq pa;
1407 struct pf_ruleset *rs;
1408 struct pfr_table trs;
1412 RB_INIT(&pf_anchors);
1413 memset(&pf_main_anchor, 0, sizeof(pf_main_anchor));
1414 pf_init_ruleset(&pf_main_anchor.ruleset);
1415 pf_main_anchor.ruleset.anchor = &pf_main_anchor;
1416 if (trans == NULL) {
1417 bzero(&buf, sizeof(buf));
1418 buf.pfrb_type = PFRB_TRANS;
1423 osize = t->pfrb_size;
1426 memset(&pa, 0, sizeof(pa));
1427 memset(&pf, 0, sizeof(pf));
1428 memset(&trs, 0, sizeof(trs));
1429 if ((path = calloc(1, MAXPATHLEN)) == NULL)
1430 ERRX("pfctl_rules: calloc");
1431 if (strlcpy(trs.pfrt_anchor, anchorname,
1432 sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor))
1433 ERRX("pfctl_rules: strlcpy");
1436 pf.optimize = optimize;
1437 pf.loadopt = loadopt;
1439 /* non-brace anchor, create without resolving the path */
1440 if ((pf.anchor = calloc(1, sizeof(*pf.anchor))) == NULL)
1441 ERRX("pfctl_rules: calloc");
1442 rs = &pf.anchor->ruleset;
1443 pf_init_ruleset(rs);
1444 rs->anchor = pf.anchor;
1445 if (strlcpy(pf.anchor->path, anchorname,
1446 sizeof(pf.anchor->path)) >= sizeof(pf.anchor->path))
1447 errx(1, "pfctl_add_rule: strlcpy");
1448 if (strlcpy(pf.anchor->name, anchorname,
1449 sizeof(pf.anchor->name)) >= sizeof(pf.anchor->name))
1450 errx(1, "pfctl_add_rule: strlcpy");
1453 pf.astack[0] = pf.anchor;
1456 pf.loadopt &= ~PFCTL_FLAG_ALTQ;
1459 pfctl_init_options(&pf);
1461 if ((opts & PF_OPT_NOACTION) == 0) {
1463 * XXX For the time being we need to open transactions for
1464 * the main ruleset before parsing, because tables are still
1465 * loaded at parse time.
1467 if (pfctl_ruleset_trans(&pf, anchorname, pf.anchor))
1468 ERRX("pfctl_rules");
1469 if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ))
1471 pfctl_get_ticket(t, PF_RULESET_ALTQ, anchorname);
1472 if (pf.loadopt & PFCTL_FLAG_TABLE)
1473 pf.astack[0]->ruleset.tticket =
1474 pfctl_get_ticket(t, PF_RULESET_TABLE, anchorname);
1477 if (parse_config(filename, &pf) < 0) {
1478 if ((opts & PF_OPT_NOACTION) == 0)
1479 ERRX("Syntax error in config file: "
1480 "pf rules not loaded");
1485 if ((pf.loadopt & PFCTL_FLAG_FILTER &&
1486 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_SCRUB, 0))) ||
1487 (pf.loadopt & PFCTL_FLAG_NAT &&
1488 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_NAT, 0) ||
1489 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_RDR, 0) ||
1490 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_BINAT, 0))) ||
1491 (pf.loadopt & PFCTL_FLAG_FILTER &&
1492 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_FILTER, 0))) {
1493 if ((opts & PF_OPT_NOACTION) == 0)
1494 ERRX("Unable to load rules into kernel");
1499 if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))
1500 if (check_commit_altq(dev, opts) != 0)
1501 ERRX("errors in altq config");
1503 /* process "load anchor" directives */
1505 if (pfctl_load_anchors(dev, &pf, t) == -1)
1506 ERRX("load anchors");
1508 if (trans == NULL && (opts & PF_OPT_NOACTION) == 0) {
1510 if (pfctl_load_options(&pf))
1512 if (pfctl_trans(dev, t, DIOCXCOMMIT, osize))
1518 if (trans == NULL) { /* main ruleset */
1519 if ((opts & PF_OPT_NOACTION) == 0)
1520 if (pfctl_trans(dev, t, DIOCXROLLBACK, osize))
1521 err(1, "DIOCXROLLBACK");
1523 } else { /* sub ruleset */
1532 pfctl_fopen(const char *name, const char *mode)
1537 fp = fopen(name, mode);
1540 if (fstat(fileno(fp), &st)) {
1544 if (S_ISDIR(st.st_mode)) {
1553 pfctl_init_options(struct pfctl *pf)
1556 pf->timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
1557 pf->timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
1558 pf->timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
1559 pf->timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
1560 pf->timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
1561 pf->timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
1562 pf->timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
1563 pf->timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
1564 pf->timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
1565 pf->timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
1566 pf->timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
1567 pf->timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
1568 pf->timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
1569 pf->timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
1570 pf->timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
1571 pf->timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
1572 pf->timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
1573 pf->timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
1574 pf->timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
1575 pf->timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
1577 pf->limit[PF_LIMIT_STATES] = PFSTATE_HIWAT;
1578 pf->limit[PF_LIMIT_FRAGS] = PFFRAG_FRENT_HIWAT;
1579 pf->limit[PF_LIMIT_SRC_NODES] = PFSNODE_HIWAT;
1580 pf->limit[PF_LIMIT_TABLE_ENTRIES] = PFR_KENTRY_HIWAT;
1582 pf->debug = PF_DEBUG_URGENT;
1586 pfctl_load_options(struct pfctl *pf)
1590 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1594 for (i = 0; i < PF_LIMIT_MAX; i++) {
1595 if ((pf->opts & PF_OPT_MERGE) && !pf->limit_set[i])
1597 if (pfctl_load_limit(pf, i, pf->limit[i]))
1602 * If we've set the limit, but haven't explicitly set adaptive
1603 * timeouts, do it now with a start of 60% and end of 120%.
1605 if (pf->limit_set[PF_LIMIT_STATES] &&
1606 !pf->timeout_set[PFTM_ADAPTIVE_START] &&
1607 !pf->timeout_set[PFTM_ADAPTIVE_END]) {
1608 pf->timeout[PFTM_ADAPTIVE_START] =
1609 (pf->limit[PF_LIMIT_STATES] / 10) * 6;
1610 pf->timeout_set[PFTM_ADAPTIVE_START] = 1;
1611 pf->timeout[PFTM_ADAPTIVE_END] =
1612 (pf->limit[PF_LIMIT_STATES] / 10) * 12;
1613 pf->timeout_set[PFTM_ADAPTIVE_END] = 1;
1617 for (i = 0; i < PFTM_MAX; i++) {
1618 if ((pf->opts & PF_OPT_MERGE) && !pf->timeout_set[i])
1620 if (pfctl_load_timeout(pf, i, pf->timeout[i]))
1625 if (!(pf->opts & PF_OPT_MERGE) || pf->debug_set)
1626 if (pfctl_load_debug(pf, pf->debug))
1630 if (!(pf->opts & PF_OPT_MERGE) || pf->ifname_set)
1631 if (pfctl_load_logif(pf, pf->ifname))
1635 if (!(pf->opts & PF_OPT_MERGE) || pf->hostid_set)
1636 if (pfctl_load_hostid(pf, pf->hostid))
1643 pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit)
1648 for (i = 0; pf_limits[i].name; i++) {
1649 if (strcasecmp(opt, pf_limits[i].name) == 0) {
1650 pf->limit[pf_limits[i].index] = limit;
1651 pf->limit_set[pf_limits[i].index] = 1;
1655 if (pf_limits[i].name == NULL) {
1656 warnx("Bad pool name.");
1660 if (pf->opts & PF_OPT_VERBOSE)
1661 printf("set limit %s %d\n", opt, limit);
1667 pfctl_load_limit(struct pfctl *pf, unsigned int index, unsigned int limit)
1669 struct pfioc_limit pl;
1671 memset(&pl, 0, sizeof(pl));
1674 if (ioctl(pf->dev, DIOCSETLIMIT, &pl)) {
1676 warnx("Current pool size exceeds requested hard limit");
1678 warnx("DIOCSETLIMIT");
1685 pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
1689 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1692 for (i = 0; pf_timeouts[i].name; i++) {
1693 if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
1694 pf->timeout[pf_timeouts[i].timeout] = seconds;
1695 pf->timeout_set[pf_timeouts[i].timeout] = 1;
1700 if (pf_timeouts[i].name == NULL) {
1701 warnx("Bad timeout name.");
1706 if (pf->opts & PF_OPT_VERBOSE && ! quiet)
1707 printf("set timeout %s %d\n", opt, seconds);
1713 pfctl_load_timeout(struct pfctl *pf, unsigned int timeout, unsigned int seconds)
1717 memset(&pt, 0, sizeof(pt));
1718 pt.timeout = timeout;
1719 pt.seconds = seconds;
1720 if (ioctl(pf->dev, DIOCSETTIMEOUT, &pt)) {
1721 warnx("DIOCSETTIMEOUT");
1728 pfctl_set_optimization(struct pfctl *pf, const char *opt)
1730 const struct pf_hint *hint;
1733 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1736 for (i = 0; pf_hints[i].name; i++)
1737 if (strcasecmp(opt, pf_hints[i].name) == 0)
1740 hint = pf_hints[i].hint;
1742 warnx("invalid state timeouts optimization");
1746 for (i = 0; hint[i].name; i++)
1747 if ((r = pfctl_set_timeout(pf, hint[i].name,
1748 hint[i].timeout, 1)))
1751 if (pf->opts & PF_OPT_VERBOSE)
1752 printf("set optimization %s\n", opt);
1758 pfctl_set_logif(struct pfctl *pf, char *ifname)
1761 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1764 if (!strcmp(ifname, "none")) {
1768 pf->ifname = strdup(ifname);
1770 errx(1, "pfctl_set_logif: strdup");
1774 if (pf->opts & PF_OPT_VERBOSE)
1775 printf("set loginterface %s\n", ifname);
1781 pfctl_load_logif(struct pfctl *pf, char *ifname)
1785 memset(&pi, 0, sizeof(pi));
1786 if (ifname && strlcpy(pi.ifname, ifname,
1787 sizeof(pi.ifname)) >= sizeof(pi.ifname)) {
1788 warnx("pfctl_load_logif: strlcpy");
1791 if (ioctl(pf->dev, DIOCSETSTATUSIF, &pi)) {
1792 warnx("DIOCSETSTATUSIF");
1799 pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
1801 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1806 pf->hostid = hostid;
1809 if (pf->opts & PF_OPT_VERBOSE)
1810 printf("set hostid 0x%08x\n", ntohl(hostid));
1816 pfctl_load_hostid(struct pfctl *pf, u_int32_t hostid)
1818 if (ioctl(dev, DIOCSETHOSTID, &hostid)) {
1819 warnx("DIOCSETHOSTID");
1826 pfctl_set_debug(struct pfctl *pf, char *d)
1830 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1833 if (!strcmp(d, "none"))
1834 pf->debug = PF_DEBUG_NONE;
1835 else if (!strcmp(d, "urgent"))
1836 pf->debug = PF_DEBUG_URGENT;
1837 else if (!strcmp(d, "misc"))
1838 pf->debug = PF_DEBUG_MISC;
1839 else if (!strcmp(d, "loud"))
1840 pf->debug = PF_DEBUG_NOISY;
1842 warnx("unknown debug level \"%s\"", d);
1848 if ((pf->opts & PF_OPT_NOACTION) == 0)
1849 if (ioctl(dev, DIOCSETDEBUG, &level))
1850 err(1, "DIOCSETDEBUG");
1852 if (pf->opts & PF_OPT_VERBOSE)
1853 printf("set debug %s\n", d);
1859 pfctl_load_debug(struct pfctl *pf, unsigned int level)
1861 if (ioctl(pf->dev, DIOCSETDEBUG, &level)) {
1862 warnx("DIOCSETDEBUG");
1869 pfctl_set_interface_flags(struct pfctl *pf, char *ifname, int flags, int how)
1871 struct pfioc_iface pi;
1873 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1876 bzero(&pi, sizeof(pi));
1878 pi.pfiio_flags = flags;
1880 if (strlcpy(pi.pfiio_name, ifname, sizeof(pi.pfiio_name)) >=
1881 sizeof(pi.pfiio_name))
1882 errx(1, "pfctl_set_interface_flags: strlcpy");
1884 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1886 if (ioctl(pf->dev, DIOCCLRIFFLAG, &pi))
1887 err(1, "DIOCCLRIFFLAG");
1889 if (ioctl(pf->dev, DIOCSETIFFLAG, &pi))
1890 err(1, "DIOCSETIFFLAG");
1897 pfctl_debug(int dev, u_int32_t level, int opts)
1899 if (ioctl(dev, DIOCSETDEBUG, &level))
1900 err(1, "DIOCSETDEBUG");
1901 if ((opts & PF_OPT_QUIET) == 0) {
1902 fprintf(stderr, "debug level set to '");
1905 fprintf(stderr, "none");
1907 case PF_DEBUG_URGENT:
1908 fprintf(stderr, "urgent");
1911 fprintf(stderr, "misc");
1913 case PF_DEBUG_NOISY:
1914 fprintf(stderr, "loud");
1917 fprintf(stderr, "<invalid>");
1920 fprintf(stderr, "'\n");
1925 pfctl_test_altqsupport(int dev, int opts)
1927 struct pfioc_altq pa;
1929 if (ioctl(dev, DIOCGETALTQS, &pa)) {
1930 if (errno == ENODEV) {
1931 if (!(opts & PF_OPT_QUIET))
1932 fprintf(stderr, "No ALTQ support in kernel\n"
1933 "ALTQ related functions disabled\n");
1936 err(1, "DIOCGETALTQS");
1942 pfctl_show_anchors(int dev, int opts, char *anchorname)
1944 struct pfioc_ruleset pr;
1947 memset(&pr, 0, sizeof(pr));
1948 memcpy(pr.path, anchorname, sizeof(pr.path));
1949 if (ioctl(dev, DIOCGETRULESETS, &pr)) {
1950 if (errno == EINVAL)
1951 fprintf(stderr, "Anchor '%s' not found.\n",
1954 err(1, "DIOCGETRULESETS");
1958 for (nr = 0; nr < mnr; ++nr) {
1959 char sub[MAXPATHLEN];
1962 if (ioctl(dev, DIOCGETRULESET, &pr))
1963 err(1, "DIOCGETRULESET");
1964 if (!strcmp(pr.name, PF_RESERVED_ANCHOR))
1968 strlcat(sub, pr.path, sizeof(sub));
1969 strlcat(sub, "/", sizeof(sub));
1971 strlcat(sub, pr.name, sizeof(sub));
1972 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE))
1973 printf(" %s\n", sub);
1974 if ((opts & PF_OPT_VERBOSE) && pfctl_show_anchors(dev, opts, sub))
1981 pfctl_lookup_option(char *cmd, const char **list)
1983 if (cmd != NULL && *cmd)
1984 for (; *list; list++)
1985 if (!strncmp(cmd, *list, strlen(cmd)))
1991 main(int argc, char *argv[])
1995 int mode = O_RDONLY;
1997 int optimize = PF_OPTIMIZE_BASIC;
1998 char anchorname[MAXPATHLEN];
2004 while ((ch = getopt(argc, argv,
2005 "a:AdD:eqf:F:ghi:k:K:mnNOo:Pp:rRs:t:T:vx:z")) != -1) {
2011 opts |= PF_OPT_DISABLE;
2015 if (pfctl_cmdline_symset(optarg) < 0)
2016 warnx("could not parse macro definition %s",
2020 opts |= PF_OPT_ENABLE;
2024 opts |= PF_OPT_QUIET;
2027 clearopt = pfctl_lookup_option(optarg, clearopt_list);
2028 if (clearopt == NULL) {
2029 warnx("Unknown flush modifier '%s'", optarg);
2038 if (state_killers >= 2) {
2039 warnx("can only specify -k twice");
2043 state_kill[state_killers++] = optarg;
2047 if (src_node_killers >= 2) {
2048 warnx("can only specify -K twice");
2052 src_node_kill[src_node_killers++] = optarg;
2056 opts |= PF_OPT_MERGE;
2059 opts |= PF_OPT_NOACTION;
2062 loadopt |= PFCTL_FLAG_NAT;
2065 opts |= PF_OPT_USEDNS;
2072 opts |= PF_OPT_DEBUG;
2075 loadopt |= PFCTL_FLAG_ALTQ;
2078 loadopt |= PFCTL_FLAG_FILTER;
2081 optiopt = pfctl_lookup_option(optarg, optiopt_list);
2082 if (optiopt == NULL) {
2083 warnx("Unknown optimization '%s'", optarg);
2086 opts |= PF_OPT_OPTIMIZE;
2089 loadopt |= PFCTL_FLAG_OPTION;
2095 opts |= PF_OPT_NUMERIC;
2098 showopt = pfctl_lookup_option(optarg, showopt_list);
2099 if (showopt == NULL) {
2100 warnx("Unknown show modifier '%s'", optarg);
2108 tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
2109 if (tblcmdopt == NULL) {
2110 warnx("Unknown table command '%s'", optarg);
2115 if (opts & PF_OPT_VERBOSE)
2116 opts |= PF_OPT_VERBOSE2;
2117 opts |= PF_OPT_VERBOSE;
2120 debugopt = pfctl_lookup_option(optarg, debugopt_list);
2121 if (debugopt == NULL) {
2122 warnx("Unknown debug level '%s'", optarg);
2128 opts |= PF_OPT_CLRRULECTRS;
2139 if (tblcmdopt != NULL) {
2144 loadopt |= PFCTL_FLAG_TABLE;
2147 mode = strchr("acdefkrz", ch) ? O_RDWR : O_RDONLY;
2148 } else if (argc != optind) {
2149 warnx("unknown command line argument: %s ...", argv[optind]);
2156 if ((path = calloc(1, MAXPATHLEN)) == NULL)
2157 errx(1, "pfctl: calloc");
2158 memset(anchorname, 0, sizeof(anchorname));
2159 if (anchoropt != NULL) {
2160 int len = strlen(anchoropt);
2162 if (anchoropt[len - 1] == '*') {
2163 if (len >= 2 && anchoropt[len - 2] == '/')
2164 anchoropt[len - 2] = '\0';
2166 anchoropt[len - 1] = '\0';
2167 opts |= PF_OPT_RECURSE;
2169 if (strlcpy(anchorname, anchoropt,
2170 sizeof(anchorname)) >= sizeof(anchorname))
2171 errx(1, "anchor name '%s' too long",
2173 loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE;
2176 if ((opts & PF_OPT_NOACTION) == 0) {
2177 dev = open(pf_device, mode);
2179 err(1, "%s", pf_device);
2180 altqsupport = pfctl_test_altqsupport(dev, opts);
2182 dev = open(pf_device, O_RDONLY);
2184 opts |= PF_OPT_DUMMYACTION;
2185 /* turn off options */
2186 opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
2187 clearopt = showopt = debugopt = NULL;
2188 #if defined(__FreeBSD__) && !defined(ENABLE_ALTQ)
2195 if (opts & PF_OPT_DISABLE)
2196 if (pfctl_disable(dev, opts))
2199 if (showopt != NULL) {
2202 pfctl_show_anchors(dev, opts, anchorname);
2205 pfctl_load_fingerprints(dev, opts);
2206 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
2210 pfctl_load_fingerprints(dev, opts);
2211 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
2215 pfctl_load_fingerprints(dev, opts);
2216 pfctl_show_nat(dev, opts, anchorname);
2219 pfctl_show_altq(dev, ifaceopt, opts,
2220 opts & PF_OPT_VERBOSE2);
2223 pfctl_show_states(dev, ifaceopt, opts);
2226 pfctl_show_src_nodes(dev, opts);
2229 pfctl_show_status(dev, opts);
2232 pfctl_show_timeouts(dev, opts);
2235 pfctl_show_limits(dev, opts);
2238 opts |= PF_OPT_SHOWALL;
2239 pfctl_load_fingerprints(dev, opts);
2241 pfctl_show_nat(dev, opts, anchorname);
2242 pfctl_show_rules(dev, path, opts, 0, anchorname, 0);
2243 pfctl_show_altq(dev, ifaceopt, opts, 0);
2244 pfctl_show_states(dev, ifaceopt, opts);
2245 pfctl_show_src_nodes(dev, opts);
2246 pfctl_show_status(dev, opts);
2247 pfctl_show_rules(dev, path, opts, 1, anchorname, 0);
2248 pfctl_show_timeouts(dev, opts);
2249 pfctl_show_limits(dev, opts);
2250 pfctl_show_tables(anchorname, opts);
2251 pfctl_show_fingerprints(opts);
2254 pfctl_show_tables(anchorname, opts);
2257 pfctl_load_fingerprints(dev, opts);
2258 pfctl_show_fingerprints(opts);
2261 pfctl_show_ifaces(ifaceopt, opts);
2266 if ((opts & PF_OPT_CLRRULECTRS) && showopt == NULL)
2267 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
2270 if (clearopt != NULL) {
2271 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL)
2272 errx(1, "anchor names beginning with '_' cannot "
2273 "be modified from the command line");
2275 switch (*clearopt) {
2277 pfctl_clear_rules(dev, opts, anchorname);
2280 pfctl_clear_nat(dev, opts, anchorname);
2283 pfctl_clear_altq(dev, opts);
2286 pfctl_clear_states(dev, ifaceopt, opts);
2289 pfctl_clear_src_nodes(dev, opts);
2292 pfctl_clear_stats(dev, opts);
2295 pfctl_clear_rules(dev, opts, anchorname);
2296 pfctl_clear_nat(dev, opts, anchorname);
2297 pfctl_clear_tables(anchorname, opts);
2299 pfctl_clear_altq(dev, opts);
2300 pfctl_clear_states(dev, ifaceopt, opts);
2301 pfctl_clear_src_nodes(dev, opts);
2302 pfctl_clear_stats(dev, opts);
2303 pfctl_clear_fingerprints(dev, opts);
2304 pfctl_clear_interface_flags(dev, opts);
2308 pfctl_clear_fingerprints(dev, opts);
2311 pfctl_clear_tables(anchorname, opts);
2315 if (state_killers) {
2316 if (!strcmp(state_kill[0], "label"))
2317 pfctl_label_kill_states(dev, ifaceopt, opts);
2318 else if (!strcmp(state_kill[0], "id"))
2319 pfctl_id_kill_states(dev, ifaceopt, opts);
2321 pfctl_net_kill_states(dev, ifaceopt, opts);
2324 if (src_node_killers)
2325 pfctl_kill_src_nodes(dev, ifaceopt, opts);
2327 if (tblcmdopt != NULL) {
2328 error = pfctl_command_tables(argc, argv, tableopt,
2329 tblcmdopt, rulesopt, anchorname, opts);
2332 if (optiopt != NULL) {
2338 optimize |= PF_OPTIMIZE_BASIC;
2342 optimize |= PF_OPTIMIZE_PROFILE;
2347 if ((rulesopt != NULL) && (loadopt & PFCTL_FLAG_OPTION) &&
2349 if (pfctl_clear_interface_flags(dev, opts | PF_OPT_QUIET))
2352 if (rulesopt != NULL && !(opts & (PF_OPT_MERGE|PF_OPT_NOACTION)) &&
2353 !anchorname[0] && (loadopt & PFCTL_FLAG_OPTION))
2354 if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
2357 if (rulesopt != NULL) {
2358 if (anchorname[0] == '_' || strstr(anchorname, "/_") != NULL)
2359 errx(1, "anchor names beginning with '_' cannot "
2360 "be modified from the command line");
2361 if (pfctl_rules(dev, rulesopt, opts, optimize,
2364 else if (!(opts & PF_OPT_NOACTION) &&
2365 (loadopt & PFCTL_FLAG_TABLE))
2366 warn_namespace_collision(NULL);
2369 if (opts & PF_OPT_ENABLE)
2370 if (pfctl_enable(dev, opts))
2373 if (debugopt != NULL) {
2374 switch (*debugopt) {
2376 pfctl_debug(dev, PF_DEBUG_NONE, opts);
2379 pfctl_debug(dev, PF_DEBUG_URGENT, opts);
2382 pfctl_debug(dev, PF_DEBUG_MISC, opts);
2385 pfctl_debug(dev, PF_DEBUG_NOISY, opts);