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