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 #define PFIOC_USE_LATEST
38
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <sys/endian.h>
44
45 #include <net/if.h>
46 #include <netinet/in.h>
47 #include <net/pfvar.h>
48 #include <arpa/inet.h>
49 #include <net/altq/altq.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <libpfctl.h>
55 #include <limits.h>
56 #include <netdb.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <stdarg.h>
63 #include <libgen.h>
64
65 #include "pfctl_parser.h"
66 #include "pfctl.h"
67
68 void usage(void);
69 int pfctl_enable(int, int);
70 int pfctl_disable(int, int);
71 void pfctl_clear_stats(struct pfctl_handle *, int);
72 void pfctl_get_skip_ifaces(void);
73 void pfctl_check_skip_ifaces(char *);
74 void pfctl_adjust_skip_ifaces(struct pfctl *);
75 void pfctl_clear_interface_flags(int, int);
76 void pfctl_flush_eth_rules(int, int, char *);
77 int pfctl_flush_rules(int, int, char *);
78 void pfctl_flush_nat(int, int, char *);
79 int pfctl_clear_altq(int, int);
80 void pfctl_clear_src_nodes(int, int);
81 void pfctl_clear_iface_states(int, const char *, int);
82 struct addrinfo *
83 pfctl_addrprefix(char *, struct pf_addr *, int);
84 void pfctl_kill_src_nodes(int, int);
85 void pfctl_net_kill_states(int, const char *, int);
86 void pfctl_gateway_kill_states(int, const char *, int);
87 void pfctl_label_kill_states(int, const char *, int);
88 void pfctl_id_kill_states(int, const char *, int);
89 void pfctl_key_kill_states(int, const char *, int);
90 int pfctl_parse_host(char *, struct pf_rule_addr *);
91 void pfctl_init_options(struct pfctl *);
92 int pfctl_load_options(struct pfctl *);
93 int pfctl_load_limit(struct pfctl *, unsigned int, unsigned int);
94 int pfctl_load_timeout(struct pfctl *, unsigned int, unsigned int);
95 int pfctl_load_debug(struct pfctl *, unsigned int);
96 int pfctl_load_logif(struct pfctl *, char *);
97 int pfctl_load_hostid(struct pfctl *, u_int32_t);
98 int pfctl_load_reassembly(struct pfctl *, u_int32_t);
99 int pfctl_load_syncookies(struct pfctl *, u_int8_t);
100 int pfctl_get_pool(int, struct pfctl_pool *, u_int32_t, u_int32_t, int,
101 const char *, int);
102 void pfctl_print_eth_rule_counters(struct pfctl_eth_rule *, int);
103 void pfctl_print_rule_counters(struct pfctl_rule *, int);
104 int pfctl_show_eth_rules(int, char *, int, enum pfctl_show, char *, int, int);
105 int pfctl_show_rules(int, char *, int, enum pfctl_show, char *, int, int);
106 int pfctl_show_nat(int, const char *, int, char *, int, int);
107 int pfctl_show_src_nodes(int, int);
108 int pfctl_show_states(int, const char *, int);
109 int pfctl_show_status(int, int);
110 int pfctl_show_running(int);
111 int pfctl_show_timeouts(int, int);
112 int pfctl_show_limits(int, int);
113 void pfctl_read_limits(struct pfctl_handle *);
114 void pfctl_restore_limits(void);
115 void pfctl_debug(int, u_int32_t, int);
116 int pfctl_test_altqsupport(int, int);
117 int pfctl_show_anchors(int, int, char *);
118 int pfctl_show_eth_anchors(int, int, char *);
119 int pfctl_ruleset_trans(struct pfctl *, char *, struct pfctl_anchor *, bool);
120 int pfctl_eth_ruleset_trans(struct pfctl *, char *,
121 struct pfctl_eth_anchor *);
122 int pfctl_load_eth_ruleset(struct pfctl *, char *,
123 struct pfctl_eth_ruleset *, int);
124 int pfctl_load_eth_rule(struct pfctl *, char *, struct pfctl_eth_rule *,
125 int);
126 int pfctl_load_ruleset(struct pfctl *, char *,
127 struct pfctl_ruleset *, int, int);
128 int pfctl_load_rule(struct pfctl *, char *, struct pfctl_rule *, int);
129 const char *pfctl_lookup_option(char *, const char * const *);
130 void pfctl_reset(int, int);
131 int pfctl_walk_show(int, struct pfioc_ruleset *, void *);
132 int pfctl_walk_get(int, struct pfioc_ruleset *, void *);
133 int pfctl_walk_anchors(int, int, const char *,
134 int(*)(int, struct pfioc_ruleset *, void *), void *);
135 struct pfr_anchors *
136 pfctl_get_anchors(int, const char *, int);
137 int pfctl_recurse(int, int, const char *,
138 int(*)(int, int, struct pfr_anchoritem *));
139 int pfctl_call_clearrules(int, int, struct pfr_anchoritem *);
140 int pfctl_call_cleartables(int, int, struct pfr_anchoritem *);
141 int pfctl_call_clearanchors(int, int, struct pfr_anchoritem *);
142 int pfctl_call_showtables(int, int, struct pfr_anchoritem *);
143
144 static struct pfctl_anchor_global pf_anchors;
145 struct pfctl_anchor pf_main_anchor;
146 struct pfctl_eth_anchor pf_eth_main_anchor;
147 static struct pfr_buffer skip_b;
148
149 static const char *clearopt;
150 static char *rulesopt;
151 static const char *showopt;
152 static const char *debugopt;
153 static char *anchoropt;
154 static const char *optiopt = NULL;
155 static const char *pf_device = PF_DEVICE;
156 static char *ifaceopt;
157 static char *tableopt;
158 static const char *tblcmdopt;
159 static int src_node_killers;
160 static char *src_node_kill[2];
161 static int state_killers;
162 static char *state_kill[2];
163 int loadopt;
164 int altqsupport;
165
166 int dev = -1;
167 struct pfctl_handle *pfh = NULL;
168 static int first_title = 1;
169 static int labels = 0;
170 static int exit_val = 0;
171
172 #define INDENT(d, o) do { \
173 if (o) { \
174 int i; \
175 for (i=0; i < d; i++) \
176 printf(" "); \
177 } \
178 } while (0); \
179
180
181 static const struct {
182 const char *name;
183 int index;
184 } pf_limits[] = {
185 { "states", PF_LIMIT_STATES },
186 { "src-nodes", PF_LIMIT_SRC_NODES },
187 { "frags", PF_LIMIT_FRAGS },
188 { "table-entries", PF_LIMIT_TABLE_ENTRIES },
189 { "anchors", PF_LIMIT_ANCHORS },
190 { "eth-anchors", PF_LIMIT_ETH_ANCHORS },
191 { NULL, 0 }
192 };
193
194 static unsigned int limit_curr[PF_LIMIT_MAX];
195
196 struct pf_hint {
197 const char *name;
198 int timeout;
199 };
200 static const struct pf_hint pf_hint_normal[] = {
201 { "tcp.first", 2 * 60 },
202 { "tcp.opening", 30 },
203 { "tcp.established", 24 * 60 * 60 },
204 { "tcp.closing", 15 * 60 },
205 { "tcp.finwait", 45 },
206 { "tcp.closed", 90 },
207 { "tcp.tsdiff", 30 },
208 { NULL, 0 }
209 };
210 static const struct pf_hint pf_hint_satellite[] = {
211 { "tcp.first", 3 * 60 },
212 { "tcp.opening", 30 + 5 },
213 { "tcp.established", 24 * 60 * 60 },
214 { "tcp.closing", 15 * 60 + 5 },
215 { "tcp.finwait", 45 + 5 },
216 { "tcp.closed", 90 + 5 },
217 { "tcp.tsdiff", 60 },
218 { NULL, 0 }
219 };
220 static const struct pf_hint pf_hint_conservative[] = {
221 { "tcp.first", 60 * 60 },
222 { "tcp.opening", 15 * 60 },
223 { "tcp.established", 5 * 24 * 60 * 60 },
224 { "tcp.closing", 60 * 60 },
225 { "tcp.finwait", 10 * 60 },
226 { "tcp.closed", 3 * 60 },
227 { "tcp.tsdiff", 60 },
228 { NULL, 0 }
229 };
230 static const struct pf_hint pf_hint_aggressive[] = {
231 { "tcp.first", 30 },
232 { "tcp.opening", 5 },
233 { "tcp.established", 5 * 60 * 60 },
234 { "tcp.closing", 60 },
235 { "tcp.finwait", 30 },
236 { "tcp.closed", 30 },
237 { "tcp.tsdiff", 10 },
238 { NULL, 0 }
239 };
240
241 static const struct {
242 const char *name;
243 const struct pf_hint *hint;
244 } pf_hints[] = {
245 { "normal", pf_hint_normal },
246 { "satellite", pf_hint_satellite },
247 { "high-latency", pf_hint_satellite },
248 { "conservative", pf_hint_conservative },
249 { "aggressive", pf_hint_aggressive },
250 { NULL, NULL }
251 };
252
253 static const char * const clearopt_list[] = {
254 "nat", "queue", "rules", "Sources",
255 "states", "info", "Tables", "osfp", "all",
256 "ethernet", "Reset", NULL
257 };
258
259 static const char * const showopt_list[] = {
260 "ether", "nat", "queue", "rules", "Anchors", "Sources", "states",
261 "info", "Interfaces", "labels", "timeouts", "memory", "Tables",
262 "osfp", "Running", "all", "creatorids", NULL
263 };
264
265 static const char * const tblcmdopt_list[] = {
266 "kill", "flush", "add", "delete", "load", "replace", "show",
267 "test", "zero", "expire", "reset", NULL
268 };
269
270 static const char * const debugopt_list[] = {
271 "none", "urgent", "misc", "loud", NULL
272 };
273
274 static const char * const optiopt_list[] = {
275 "none", "basic", "profile", NULL
276 };
277
278 void
usage(void)279 usage(void)
280 {
281 extern char *__progname;
282
283 fprintf(stderr,
284 "usage: %s [-AdeghMmNnOPqRSrvz] [-a anchor] [-D macro=value] [-F modifier]\n"
285 "\t[-f file] [-i interface] [-K host | network]\n"
286 "\t[-k host | network | gateway | label | id] [-o level] [-p device]\n"
287 "\t[-s modifier] [-t table -T command [address ...]] [-x level]\n",
288 __progname);
289
290 exit(1);
291 }
292
293 void
pfctl_err(int opts,int eval,const char * fmt,...)294 pfctl_err(int opts, int eval, const char *fmt, ...)
295 {
296 va_list ap;
297
298 va_start(ap, fmt);
299
300 if ((opts & PF_OPT_IGNFAIL) == 0)
301 verr(eval, fmt, ap);
302 else
303 vwarn(fmt, ap);
304
305 va_end(ap);
306
307 exit_val = eval;
308 }
309
310 void
pfctl_errx(int opts,int eval,const char * fmt,...)311 pfctl_errx(int opts, int eval, const char *fmt, ...)
312 {
313 va_list ap;
314
315 va_start(ap, fmt);
316
317 if ((opts & PF_OPT_IGNFAIL) == 0)
318 verrx(eval, fmt, ap);
319 else
320 vwarnx(fmt, ap);
321
322 va_end(ap);
323
324 exit_val = eval;
325 }
326
327 /*
328 * Cache protocol number to name translations.
329 *
330 * Translation is performed a lot e.g., when dumping states and
331 * getprotobynumber is incredibly expensive.
332 *
333 * Note from the getprotobynumber(3) manpage:
334 * <quote>
335 * These functions use a thread-specific data space; if the data is needed
336 * for future use, it should be copied before any subsequent calls overwrite
337 * it. Only the Internet protocols are currently understood.
338 * </quote>
339 *
340 * Consequently we only cache the name and strdup it for safety.
341 *
342 * At the time of writing this comment the last entry in /etc/protocols is:
343 * divert 258 DIVERT # Divert pseudo-protocol [non IANA]
344 */
345 const char *
pfctl_proto2name(int proto)346 pfctl_proto2name(int proto)
347 {
348 static const char *pfctl_proto_cache[259];
349 struct protoent *p;
350
351 if (proto >= nitems(pfctl_proto_cache)) {
352 p = getprotobynumber(proto);
353 if (p == NULL) {
354 return (NULL);
355 }
356 return (p->p_name);
357 }
358
359 if (pfctl_proto_cache[proto] == NULL) {
360 p = getprotobynumber(proto);
361 if (p == NULL) {
362 return (NULL);
363 }
364 pfctl_proto_cache[proto] = strdup(p->p_name);
365 }
366
367 return (pfctl_proto_cache[proto]);
368 }
369
370 int
pfctl_enable(int dev,int opts)371 pfctl_enable(int dev, int opts)
372 {
373 int ret;
374
375 if ((ret = pfctl_startstop(pfh, 1)) != 0) {
376 if (ret == EEXIST)
377 errx(1, "pf already enabled");
378 else if (ret == ESRCH)
379 errx(1, "pfil registeration failed");
380 else
381 errc(1, ret, "DIOCSTART");
382 }
383 if ((opts & PF_OPT_QUIET) == 0)
384 fprintf(stderr, "pf enabled\n");
385
386 if (altqsupport && ioctl(dev, DIOCSTARTALTQ))
387 if (errno != EEXIST)
388 err(1, "DIOCSTARTALTQ");
389
390 return (0);
391 }
392
393 int
pfctl_disable(int dev,int opts)394 pfctl_disable(int dev, int opts)
395 {
396 int ret;
397
398 if ((ret = pfctl_startstop(pfh, 0)) != 0) {
399 if (ret == ENOENT)
400 errx(1, "pf not enabled");
401 else
402 errc(1, ret, "DIOCSTOP");
403 }
404 if ((opts & PF_OPT_QUIET) == 0)
405 fprintf(stderr, "pf disabled\n");
406
407 if (altqsupport && ioctl(dev, DIOCSTOPALTQ))
408 if (errno != ENOENT)
409 err(1, "DIOCSTOPALTQ");
410
411 return (0);
412 }
413
414 void
pfctl_clear_stats(struct pfctl_handle * h,int opts)415 pfctl_clear_stats(struct pfctl_handle *h, int opts)
416 {
417 int ret;
418 if ((ret = pfctl_clear_status(h)) != 0)
419 pfctl_err(opts, 1, "DIOCCLRSTATUS");
420 if ((opts & PF_OPT_QUIET) == 0)
421 fprintf(stderr, "pf: statistics cleared\n");
422 }
423
424 void
pfctl_get_skip_ifaces(void)425 pfctl_get_skip_ifaces(void)
426 {
427 bzero(&skip_b, sizeof(skip_b));
428 skip_b.pfrb_type = PFRB_IFACES;
429 for (;;) {
430 pfr_buf_grow(&skip_b, skip_b.pfrb_size);
431 skip_b.pfrb_size = skip_b.pfrb_msize;
432 if (pfi_get_ifaces(NULL, skip_b.pfrb_caddr, &skip_b.pfrb_size))
433 err(1, "pfi_get_ifaces");
434 if (skip_b.pfrb_size <= skip_b.pfrb_msize)
435 break;
436 }
437 }
438
439 void
pfctl_check_skip_ifaces(char * ifname)440 pfctl_check_skip_ifaces(char *ifname)
441 {
442 struct pfi_kif *p;
443 struct node_host *h = NULL, *n = NULL;
444
445 PFRB_FOREACH(p, &skip_b) {
446 if (!strcmp(ifname, p->pfik_name) &&
447 (p->pfik_flags & PFI_IFLAG_SKIP))
448 p->pfik_flags &= ~PFI_IFLAG_SKIP;
449 if (!strcmp(ifname, p->pfik_name) && p->pfik_group != NULL) {
450 if ((h = ifa_grouplookup(p->pfik_name, 0)) == NULL)
451 continue;
452
453 for (n = h; n != NULL; n = n->next) {
454 if (strncmp(p->pfik_name, ifname, IFNAMSIZ))
455 continue;
456
457 p->pfik_flags &= ~PFI_IFLAG_SKIP;
458 }
459 }
460 }
461 }
462
463 void
pfctl_adjust_skip_ifaces(struct pfctl * pf)464 pfctl_adjust_skip_ifaces(struct pfctl *pf)
465 {
466 struct pfi_kif *p, *pp;
467 struct node_host *h = NULL, *n = NULL;
468
469 PFRB_FOREACH(p, &skip_b) {
470 if (p->pfik_group == NULL || !(p->pfik_flags & PFI_IFLAG_SKIP))
471 continue;
472
473 pfctl_set_interface_flags(pf, p->pfik_name, PFI_IFLAG_SKIP, 0);
474 if ((h = ifa_grouplookup(p->pfik_name, 0)) == NULL)
475 continue;
476
477 for (n = h; n != NULL; n = n->next)
478 PFRB_FOREACH(pp, &skip_b) {
479 if (strncmp(pp->pfik_name, n->ifname, IFNAMSIZ))
480 continue;
481
482 if (!(pp->pfik_flags & PFI_IFLAG_SKIP))
483 pfctl_set_interface_flags(pf,
484 pp->pfik_name, PFI_IFLAG_SKIP, 1);
485 if (pp->pfik_flags & PFI_IFLAG_SKIP)
486 pp->pfik_flags &= ~PFI_IFLAG_SKIP;
487 }
488 }
489
490 PFRB_FOREACH(p, &skip_b) {
491 if (! (p->pfik_flags & PFI_IFLAG_SKIP))
492 continue;
493
494 pfctl_set_interface_flags(pf, p->pfik_name, PFI_IFLAG_SKIP, 0);
495 }
496 }
497
498 void
pfctl_clear_interface_flags(int dev,int opts)499 pfctl_clear_interface_flags(int dev, int opts)
500 {
501 struct pfioc_iface pi;
502
503 if ((opts & PF_OPT_NOACTION) == 0) {
504 bzero(&pi, sizeof(pi));
505 pi.pfiio_flags = PFI_IFLAG_SKIP;
506
507 if (ioctl(dev, DIOCCLRIFFLAG, &pi))
508 err(1, "DIOCCLRIFFLAG");
509 if ((opts & PF_OPT_QUIET) == 0)
510 fprintf(stderr, "pf: interface flags reset\n");
511 }
512 }
513
514 void
pfctl_flush_eth_rules(int dev,int opts,char * anchorname)515 pfctl_flush_eth_rules(int dev, int opts, char *anchorname)
516 {
517 int ret;
518
519 ret = pfctl_clear_eth_rules(dev, anchorname);
520 if (ret != 0)
521 err(1, "pfctl_clear_eth_rules");
522
523 if ((opts & PF_OPT_QUIET) == 0)
524 fprintf(stderr, "Ethernet rules cleared\n");
525 }
526
527 int
pfctl_flush_rules(int dev,int opts,char * anchorname)528 pfctl_flush_rules(int dev, int opts, char *anchorname)
529 {
530 int ret;
531
532 ret = pfctl_clear_rules(dev, anchorname);
533 if (ret != 0) {
534 pfctl_err(opts, 1, "%s", __func__);
535 return (1);
536 } else if ((opts & PF_OPT_QUIET) == 0)
537 fprintf(stderr, "rules cleared\n");
538
539 return (0);
540 }
541
542 void
pfctl_flush_nat(int dev,int opts,char * anchorname)543 pfctl_flush_nat(int dev, int opts, char *anchorname)
544 {
545 int ret;
546
547 ret = pfctl_clear_nat(dev, anchorname);
548 if (ret != 0)
549 err(1, "pfctl_clear_nat");
550 if ((opts & PF_OPT_QUIET) == 0)
551 fprintf(stderr, "nat cleared\n");
552 }
553
554 int
pfctl_clear_altq(int dev,int opts)555 pfctl_clear_altq(int dev, int opts)
556 {
557 struct pfr_buffer t;
558
559 if (!altqsupport)
560 return (-1);
561 memset(&t, 0, sizeof(t));
562 t.pfrb_type = PFRB_TRANS;
563 if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "") ||
564 pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
565 pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
566 err(1, "pfctl_clear_altq");
567 if ((opts & PF_OPT_QUIET) == 0)
568 fprintf(stderr, "altq cleared\n");
569 return (0);
570 }
571
572 void
pfctl_clear_src_nodes(int dev,int opts)573 pfctl_clear_src_nodes(int dev, int opts)
574 {
575 if (ioctl(dev, DIOCCLRSRCNODES))
576 pfctl_err(opts, 1, "DIOCCLRSRCNODES");
577 if ((opts & PF_OPT_QUIET) == 0)
578 fprintf(stderr, "source tracking entries cleared\n");
579 }
580
581 void
pfctl_clear_iface_states(int dev,const char * iface,int opts)582 pfctl_clear_iface_states(int dev, const char *iface, int opts)
583 {
584 struct pfctl_kill kill;
585 unsigned int killed;
586 int ret;
587
588 memset(&kill, 0, sizeof(kill));
589 if (iface != NULL && strlcpy(kill.ifname, iface,
590 sizeof(kill.ifname)) >= sizeof(kill.ifname))
591 pfctl_errx(opts, 1, "invalid interface: %s", iface);
592
593 if (opts & PF_OPT_KILLMATCH)
594 kill.kill_match = true;
595
596 if ((ret = pfctl_clear_states_h(pfh, &kill, &killed)) != 0)
597 pfctl_err(opts, 1, "DIOCCLRSTATUS");
598 if ((opts & PF_OPT_QUIET) == 0)
599 fprintf(stderr, "%d states cleared\n", killed);
600 }
601
602 struct addrinfo *
pfctl_addrprefix(char * addr,struct pf_addr * mask,int numeric)603 pfctl_addrprefix(char *addr, struct pf_addr *mask, int numeric)
604 {
605 char *p;
606 const char *errstr;
607 int prefix, ret_ga, q, r;
608 struct addrinfo hints, *res;
609
610 bzero(&hints, sizeof(hints));
611 hints.ai_socktype = SOCK_DGRAM; /* dummy */
612 if (numeric)
613 hints.ai_flags = AI_NUMERICHOST;
614
615 if ((p = strchr(addr, '/')) != NULL) {
616 *p++ = '\0';
617 /* prefix only with numeric addresses */
618 hints.ai_flags |= AI_NUMERICHOST;
619 }
620
621 if ((ret_ga = getaddrinfo(addr, NULL, &hints, &res))) {
622 errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
623 /* NOTREACHED */
624 }
625
626 if (p == NULL)
627 return (res);
628
629 prefix = strtonum(p, 0, res->ai_family == AF_INET6 ? 128 : 32, &errstr);
630 if (errstr)
631 errx(1, "prefix is %s: %s", errstr, p);
632
633 q = prefix >> 3;
634 r = prefix & 7;
635 switch (res->ai_family) {
636 case AF_INET:
637 bzero(&mask->v4, sizeof(mask->v4));
638 mask->v4.s_addr = htonl((u_int32_t)
639 (0xffffffffffULL << (32 - prefix)));
640 break;
641 case AF_INET6:
642 bzero(&mask->v6, sizeof(mask->v6));
643 if (q > 0)
644 memset((void *)&mask->v6, 0xff, q);
645 if (r > 0)
646 *((u_char *)&mask->v6 + q) =
647 (0xff00 >> r) & 0xff;
648 break;
649 }
650
651 return (res);
652 }
653
654 void
pfctl_kill_src_nodes(int dev,int opts)655 pfctl_kill_src_nodes(int dev, int opts)
656 {
657 struct pfioc_src_node_kill psnk;
658 struct addrinfo *res[2], *resp[2];
659 struct sockaddr last_src, last_dst;
660 int killed, sources, dests;
661
662 killed = sources = dests = 0;
663
664 memset(&psnk, 0, sizeof(psnk));
665 memset(&psnk.psnk_src.addr.v.a.mask, 0xff,
666 sizeof(psnk.psnk_src.addr.v.a.mask));
667 memset(&last_src, 0xff, sizeof(last_src));
668 memset(&last_dst, 0xff, sizeof(last_dst));
669
670 res[0] = pfctl_addrprefix(src_node_kill[0],
671 &psnk.psnk_src.addr.v.a.mask, (opts & PF_OPT_NODNS));
672
673 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
674 if (resp[0]->ai_addr == NULL)
675 continue;
676 /* We get lots of duplicates. Catch the easy ones */
677 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
678 continue;
679 last_src = *(struct sockaddr *)resp[0]->ai_addr;
680
681 psnk.psnk_af = resp[0]->ai_family;
682 sources++;
683
684 copy_satopfaddr(&psnk.psnk_src.addr.v.a.addr, resp[0]->ai_addr);
685
686 if (src_node_killers > 1) {
687 dests = 0;
688 memset(&psnk.psnk_dst.addr.v.a.mask, 0xff,
689 sizeof(psnk.psnk_dst.addr.v.a.mask));
690 memset(&last_dst, 0xff, sizeof(last_dst));
691 res[1] = pfctl_addrprefix(src_node_kill[1],
692 &psnk.psnk_dst.addr.v.a.mask,
693 (opts & PF_OPT_NODNS));
694 for (resp[1] = res[1]; resp[1];
695 resp[1] = resp[1]->ai_next) {
696 if (resp[1]->ai_addr == NULL)
697 continue;
698 if (psnk.psnk_af != resp[1]->ai_family)
699 continue;
700
701 if (memcmp(&last_dst, resp[1]->ai_addr,
702 sizeof(last_dst)) == 0)
703 continue;
704 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
705
706 dests++;
707
708 copy_satopfaddr(&psnk.psnk_dst.addr.v.a.addr,
709 resp[1]->ai_addr);
710 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
711 err(1, "DIOCKILLSRCNODES");
712 killed += psnk.psnk_killed;
713 }
714 freeaddrinfo(res[1]);
715 } else {
716 if (ioctl(dev, DIOCKILLSRCNODES, &psnk))
717 err(1, "DIOCKILLSRCNODES");
718 killed += psnk.psnk_killed;
719 }
720 }
721
722 freeaddrinfo(res[0]);
723
724 if ((opts & PF_OPT_QUIET) == 0)
725 fprintf(stderr, "killed %d src nodes from %d sources and %d "
726 "destinations\n", killed, sources, dests);
727 }
728
729 void
pfctl_net_kill_states(int dev,const char * iface,int opts)730 pfctl_net_kill_states(int dev, const char *iface, int opts)
731 {
732 struct pfctl_kill kill;
733 struct addrinfo *res[2], *resp[2];
734 struct sockaddr last_src, last_dst;
735 unsigned int newkilled;
736 int killed, sources, dests;
737 int ret;
738
739 killed = sources = dests = 0;
740
741 memset(&kill, 0, sizeof(kill));
742 memset(&kill.src.addr.v.a.mask, 0xff,
743 sizeof(kill.src.addr.v.a.mask));
744 memset(&last_src, 0xff, sizeof(last_src));
745 memset(&last_dst, 0xff, sizeof(last_dst));
746 if (iface != NULL && strlcpy(kill.ifname, iface,
747 sizeof(kill.ifname)) >= sizeof(kill.ifname))
748 pfctl_errx(opts, 1, "invalid interface: %s", iface);
749
750 if (state_killers == 2 && (strcmp(state_kill[0], "nat") == 0)) {
751 kill.nat = true;
752 state_kill[0] = state_kill[1];
753 state_killers = 1;
754 }
755
756 res[0] = pfctl_addrprefix(state_kill[0],
757 &kill.src.addr.v.a.mask, (opts & PF_OPT_NODNS));
758
759 if (opts & PF_OPT_KILLMATCH)
760 kill.kill_match = true;
761
762 for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
763 if (resp[0]->ai_addr == NULL)
764 continue;
765 /* We get lots of duplicates. Catch the easy ones */
766 if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
767 continue;
768 last_src = *(struct sockaddr *)resp[0]->ai_addr;
769
770 kill.af = resp[0]->ai_family;
771 sources++;
772
773 copy_satopfaddr(&kill.src.addr.v.a.addr, resp[0]->ai_addr);
774
775 if (state_killers > 1) {
776 dests = 0;
777 memset(&kill.dst.addr.v.a.mask, 0xff,
778 sizeof(kill.dst.addr.v.a.mask));
779 memset(&last_dst, 0xff, sizeof(last_dst));
780 res[1] = pfctl_addrprefix(state_kill[1],
781 &kill.dst.addr.v.a.mask,
782 (opts & PF_OPT_NODNS));
783 for (resp[1] = res[1]; resp[1];
784 resp[1] = resp[1]->ai_next) {
785 if (resp[1]->ai_addr == NULL)
786 continue;
787 if (kill.af != resp[1]->ai_family)
788 continue;
789
790 if (memcmp(&last_dst, resp[1]->ai_addr,
791 sizeof(last_dst)) == 0)
792 continue;
793 last_dst = *(struct sockaddr *)resp[1]->ai_addr;
794
795 dests++;
796
797 copy_satopfaddr(&kill.dst.addr.v.a.addr,
798 resp[1]->ai_addr);
799
800 if ((ret = pfctl_kill_states_h(pfh, &kill, &newkilled)) != 0)
801 pfctl_errx(opts, 1, "DIOCKILLSTATES");
802 killed += newkilled;
803 }
804 freeaddrinfo(res[1]);
805 } else {
806 if ((ret = pfctl_kill_states_h(pfh, &kill, &newkilled)) != 0)
807 pfctl_errx(opts, 1, "DIOCKILLSTATES");
808 killed += newkilled;
809 }
810 }
811
812 freeaddrinfo(res[0]);
813
814 if ((opts & PF_OPT_QUIET) == 0)
815 fprintf(stderr, "killed %d states from %d sources and %d "
816 "destinations\n", killed, sources, dests);
817 }
818
819 void
pfctl_gateway_kill_states(int dev,const char * iface,int opts)820 pfctl_gateway_kill_states(int dev, const char *iface, int opts)
821 {
822 struct pfctl_kill kill;
823 struct addrinfo *res, *resp;
824 struct sockaddr last_src;
825 unsigned int newkilled;
826 int killed = 0;
827
828 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
829 warnx("no gateway specified");
830 usage();
831 }
832
833 memset(&kill, 0, sizeof(kill));
834 memset(&kill.rt_addr.addr.v.a.mask, 0xff,
835 sizeof(kill.rt_addr.addr.v.a.mask));
836 memset(&last_src, 0xff, sizeof(last_src));
837 if (iface != NULL && strlcpy(kill.ifname, iface,
838 sizeof(kill.ifname)) >= sizeof(kill.ifname))
839 pfctl_errx(opts, 1, "invalid interface: %s", iface);
840
841 if (opts & PF_OPT_KILLMATCH)
842 kill.kill_match = true;
843
844 res = pfctl_addrprefix(state_kill[1], &kill.rt_addr.addr.v.a.mask,
845 (opts & PF_OPT_NODNS));
846
847 for (resp = res; resp; resp = resp->ai_next) {
848 if (resp->ai_addr == NULL)
849 continue;
850 /* We get lots of duplicates. Catch the easy ones */
851 if (memcmp(&last_src, resp->ai_addr, sizeof(last_src)) == 0)
852 continue;
853 last_src = *(struct sockaddr *)resp->ai_addr;
854
855 kill.af = resp->ai_family;
856
857 copy_satopfaddr(&kill.rt_addr.addr.v.a.addr,
858 resp->ai_addr);
859 if (pfctl_kill_states_h(pfh, &kill, &newkilled))
860 pfctl_errx(opts, 1, "DIOCKILLSTATES");
861 killed += newkilled;
862 }
863
864 freeaddrinfo(res);
865
866 if ((opts & PF_OPT_QUIET) == 0)
867 fprintf(stderr, "killed %d states\n", killed);
868 }
869
870 void
pfctl_label_kill_states(int dev,const char * iface,int opts)871 pfctl_label_kill_states(int dev, const char *iface, int opts)
872 {
873 struct pfctl_kill kill;
874 unsigned int killed;
875 int ret;
876
877 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
878 warnx("no label specified");
879 usage();
880 }
881 memset(&kill, 0, sizeof(kill));
882 if (iface != NULL && strlcpy(kill.ifname, iface,
883 sizeof(kill.ifname)) >= sizeof(kill.ifname))
884 pfctl_errx(opts, 1, "invalid interface: %s", iface);
885
886 if (opts & PF_OPT_KILLMATCH)
887 kill.kill_match = true;
888
889 if (strlcpy(kill.label, state_kill[1], sizeof(kill.label)) >=
890 sizeof(kill.label))
891 errx(1, "label too long: %s", state_kill[1]);
892
893 if ((ret = pfctl_kill_states_h(pfh, &kill, &killed)) != 0)
894 pfctl_errx(opts, 1, "DIOCKILLSTATES");
895
896 if ((opts & PF_OPT_QUIET) == 0)
897 fprintf(stderr, "killed %d states\n", killed);
898 }
899
900 void
pfctl_id_kill_states(int dev,const char * iface,int opts)901 pfctl_id_kill_states(int dev, const char *iface, int opts)
902 {
903 struct pfctl_kill kill;
904 unsigned int killed;
905 int ret;
906
907 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
908 warnx("no id specified");
909 usage();
910 }
911
912 memset(&kill, 0, sizeof(kill));
913
914 if (opts & PF_OPT_KILLMATCH)
915 kill.kill_match = true;
916
917 if ((sscanf(state_kill[1], "%jx/%x",
918 &kill.cmp.id, &kill.cmp.creatorid)) == 2) {
919 }
920 else if ((sscanf(state_kill[1], "%jx", &kill.cmp.id)) == 1) {
921 kill.cmp.creatorid = 0;
922 } else {
923 warnx("wrong id format specified");
924 usage();
925 }
926 if (kill.cmp.id == 0) {
927 warnx("cannot kill id 0");
928 usage();
929 }
930
931 if ((ret = pfctl_kill_states_h(pfh, &kill, &killed)) != 0)
932 pfctl_errx(opts, 1, "DIOCKILLSTATES");
933
934 if ((opts & PF_OPT_QUIET) == 0)
935 fprintf(stderr, "killed %d states\n", killed);
936 }
937
938 void
pfctl_key_kill_states(int dev,const char * iface,int opts)939 pfctl_key_kill_states(int dev, const char *iface, int opts)
940 {
941 struct pfctl_kill kill;
942 char *s, *token, *tokens[4];
943 struct protoent *p;
944 u_int i, sidx, didx;
945 int ret, killed;
946
947 if (state_killers != 2 || (strlen(state_kill[1]) == 0)) {
948 warnx("no key specified");
949 usage();
950 }
951 memset(&kill, 0, sizeof(kill));
952
953 if (iface != NULL &&
954 strlcpy(kill.ifname, iface, sizeof(kill.ifname)) >=
955 sizeof(kill.ifname))
956 pfctl_errx(opts, 1, "invalid interface: %s", iface);
957
958 s = strdup(state_kill[1]);
959 if (!s)
960 errx(1, "%s: strdup", __func__);
961 i = 0;
962 while ((token = strsep(&s, " \t")) != NULL)
963 if (*token != '\0') {
964 if (i < 4)
965 tokens[i] = token;
966 i++;
967 }
968 if (i != 4)
969 errx(1, "%s: key must be "
970 "\"protocol host1:port1 direction host2:port2\" format",
971 __func__);
972
973 if ((p = getprotobyname(tokens[0])) == NULL)
974 errx(1, "invalid protocol: %s", tokens[0]);
975 kill.proto = p->p_proto;
976
977 if (strcmp(tokens[2], "->") == 0) {
978 sidx = 1;
979 didx = 3;
980 } else if (strcmp(tokens[2], "<-") == 0) {
981 sidx = 3;
982 didx = 1;
983 } else
984 errx(1, "invalid direction: %s", tokens[2]);
985
986 if (pfctl_parse_host(tokens[sidx], &kill.src) == -1)
987 errx(1, "invalid host: %s", tokens[sidx]);
988 if (pfctl_parse_host(tokens[didx], &kill.dst) == -1)
989 errx(1, "invalid host: %s", tokens[didx]);
990
991 if ((ret = pfctl_kill_states_h(pfh, &kill, &killed)) != 0)
992 pfctl_errx(opts, 1, "DIOCKILLSTATES");
993
994 if ((opts & PF_OPT_QUIET) == 0)
995 fprintf(stderr, "killed %d states\n", killed);
996 }
997
998 int
pfctl_parse_host(char * str,struct pf_rule_addr * addr)999 pfctl_parse_host(char *str, struct pf_rule_addr *addr)
1000 {
1001 char *s = NULL, *sbs, *sbe;
1002 struct addrinfo hints, *ai;
1003
1004 s = strdup(str);
1005 if (!s)
1006 errx(1, "pfctl_parse_host: strdup");
1007
1008 memset(&hints, 0, sizeof(hints));
1009 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1010 hints.ai_flags = AI_NUMERICHOST;
1011
1012 if ((sbs = strchr(s, '[')) != NULL && (sbe = strrchr(s, ']')) != NULL) {
1013 hints.ai_family = AF_INET6;
1014 *(sbs++) = *sbe = '\0';
1015 } else if ((sbs = strchr(s, ':')) != NULL) {
1016 hints.ai_family = AF_INET;
1017 *(sbs++) = '\0';
1018 } else {
1019 /* Assume that no ':<number>' means port 0 */
1020 }
1021
1022 if (getaddrinfo(s, sbs, &hints, &ai) != 0)
1023 goto error;
1024
1025 copy_satopfaddr(&addr->addr.v.a.addr, ai->ai_addr);
1026 addr->port[0] = ai->ai_family == AF_INET6 ?
1027 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port :
1028 ((struct sockaddr_in *)ai->ai_addr)->sin_port;
1029 freeaddrinfo(ai);
1030 free(s);
1031
1032 memset(&addr->addr.v.a.mask, 0xff, sizeof(struct pf_addr));
1033 addr->port_op = PF_OP_EQ;
1034 addr->addr.type = PF_ADDR_ADDRMASK;
1035
1036 return (0);
1037
1038 error:
1039 free(s);
1040 return (-1);
1041 }
1042
1043 int
pfctl_get_pool(int dev,struct pfctl_pool * pool,u_int32_t nr,u_int32_t ticket,int r_action,const char * anchorname,int which)1044 pfctl_get_pool(int dev, struct pfctl_pool *pool, u_int32_t nr,
1045 u_int32_t ticket, int r_action, const char *anchorname, int which)
1046 {
1047 struct pfioc_pooladdr pp;
1048 struct pfctl_pooladdr *pa;
1049 u_int32_t pnr, mpnr;
1050 int ret;
1051
1052 memset(&pp, 0, sizeof(pp));
1053 if ((ret = pfctl_get_addrs(pfh, ticket, nr, r_action, anchorname, &mpnr, which)) != 0) {
1054 warnc(ret, "DIOCGETADDRS");
1055 return (-1);
1056 }
1057
1058 TAILQ_INIT(&pool->list);
1059 for (pnr = 0; pnr < mpnr; ++pnr) {
1060 if ((ret = pfctl_get_addr(pfh, ticket, nr, r_action, anchorname, pnr, &pp, which)) != 0) {
1061 warnc(ret, "DIOCGETADDR");
1062 return (-1);
1063 }
1064 pa = calloc(1, sizeof(struct pfctl_pooladdr));
1065 if (pa == NULL)
1066 err(1, "calloc");
1067 bcopy(&pp.addr, pa, sizeof(struct pfctl_pooladdr));
1068 pa->af = pp.af;
1069 TAILQ_INSERT_TAIL(&pool->list, pa, entries);
1070 }
1071
1072 return (0);
1073 }
1074
1075 void
pfctl_move_pool(struct pfctl_pool * src,struct pfctl_pool * dst)1076 pfctl_move_pool(struct pfctl_pool *src, struct pfctl_pool *dst)
1077 {
1078 struct pfctl_pooladdr *pa;
1079
1080 while ((pa = TAILQ_FIRST(&src->list)) != NULL) {
1081 TAILQ_REMOVE(&src->list, pa, entries);
1082 TAILQ_INSERT_TAIL(&dst->list, pa, entries);
1083 }
1084 }
1085
1086 void
pfctl_clear_pool(struct pfctl_pool * pool)1087 pfctl_clear_pool(struct pfctl_pool *pool)
1088 {
1089 struct pfctl_pooladdr *pa;
1090
1091 while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
1092 TAILQ_REMOVE(&pool->list, pa, entries);
1093 free(pa);
1094 }
1095 }
1096
1097 void
pfctl_print_eth_rule_counters(struct pfctl_eth_rule * rule,int opts)1098 pfctl_print_eth_rule_counters(struct pfctl_eth_rule *rule, int opts)
1099 {
1100 if (opts & PF_OPT_VERBOSE) {
1101 printf(" [ Evaluations: %-8llu Packets: %-8llu "
1102 "Bytes: %-10llu]\n",
1103 (unsigned long long)rule->evaluations,
1104 (unsigned long long)(rule->packets[0] +
1105 rule->packets[1]),
1106 (unsigned long long)(rule->bytes[0] +
1107 rule->bytes[1]));
1108 }
1109 if (opts & PF_OPT_VERBOSE2) {
1110 char timestr[30];
1111
1112 if (rule->last_active_timestamp != 0) {
1113 bcopy(ctime(&rule->last_active_timestamp), timestr,
1114 sizeof(timestr));
1115 *strchr(timestr, '\n') = '\0';
1116 } else {
1117 snprintf(timestr, sizeof(timestr), "N/A");
1118 }
1119 printf(" [ Last Active Time: %s ]\n", timestr);
1120 }
1121 }
1122
1123 void
pfctl_print_rule_counters(struct pfctl_rule * rule,int opts)1124 pfctl_print_rule_counters(struct pfctl_rule *rule, int opts)
1125 {
1126 if (opts & PF_OPT_DEBUG) {
1127 const char *t[PF_SKIP_COUNT] = { "i", "d", "f",
1128 "p", "sa", "da", "sp", "dp" };
1129 int i;
1130
1131 printf(" [ Skip steps: ");
1132 for (i = 0; i < PF_SKIP_COUNT; ++i) {
1133 if (rule->skip[i].nr == rule->nr + 1)
1134 continue;
1135 printf("%s=", t[i]);
1136 if (rule->skip[i].nr == -1)
1137 printf("end ");
1138 else
1139 printf("%u ", rule->skip[i].nr);
1140 }
1141 printf("]\n");
1142
1143 printf(" [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n",
1144 rule->qname, rule->qid, rule->pqname, rule->pqid);
1145 if (rule->rule_flag & PFRULE_EXPIRED)
1146 printf(" [ Expired: %lld secs ago ]\n",
1147 (long long)(time(NULL) - rule->exptime));
1148 }
1149 if (opts & PF_OPT_VERBOSE) {
1150 printf(" [ Evaluations: %-8llu Packets: %-8llu "
1151 "Bytes: %-10llu States: %-6ju]\n",
1152 (unsigned long long)rule->evaluations,
1153 (unsigned long long)(rule->packets[0] +
1154 rule->packets[1]),
1155 (unsigned long long)(rule->bytes[0] +
1156 rule->bytes[1]), (uintmax_t)rule->states_cur);
1157 printf(" [ Source Nodes: %-6ju "
1158 "Limit: %-6ju "
1159 "NAT/RDR: %-6ju "
1160 "Route: %-6ju "
1161 "]\n",
1162 (uintmax_t)rule->src_nodes,
1163 (uintmax_t)rule->src_nodes_type[PF_SN_LIMIT],
1164 (uintmax_t)rule->src_nodes_type[PF_SN_NAT],
1165 (uintmax_t)rule->src_nodes_type[PF_SN_ROUTE]);
1166 if (!(opts & PF_OPT_DEBUG))
1167 printf(" [ Inserted: uid %u pid %u "
1168 "State Creations: %-6ju]\n",
1169 (unsigned)rule->cuid, (unsigned)rule->cpid,
1170 (uintmax_t)rule->states_tot);
1171 }
1172 if (opts & PF_OPT_VERBOSE2) {
1173 char timestr[30];
1174 if (rule->last_active_timestamp != 0) {
1175 bcopy(ctime(&rule->last_active_timestamp), timestr,
1176 sizeof(timestr));
1177 *strchr(timestr, '\n') = '\0';
1178 } else {
1179 snprintf(timestr, sizeof(timestr), "N/A");
1180 }
1181 printf(" [ Last Active Time: %s ]\n", timestr);
1182 }
1183 }
1184
1185 void
pfctl_print_title(char * title)1186 pfctl_print_title(char *title)
1187 {
1188 if (!first_title)
1189 printf("\n");
1190 first_title = 0;
1191 printf("%s\n", title);
1192 }
1193
1194 int
pfctl_show_eth_rules(int dev,char * path,int opts,enum pfctl_show format,char * anchorname,int depth,int wildcard)1195 pfctl_show_eth_rules(int dev, char *path, int opts, enum pfctl_show format,
1196 char *anchorname, int depth, int wildcard)
1197 {
1198 char anchor_call[MAXPATHLEN];
1199 struct pfctl_eth_rules_info info;
1200 struct pfctl_eth_rule rule;
1201 int brace;
1202 int dotitle = opts & PF_OPT_SHOWALL;
1203 int len = strlen(path);
1204 int ret;
1205 char *npath, *p;
1206
1207 /*
1208 * Truncate a trailing / and * on an anchorname before searching for
1209 * the ruleset, this is syntactic sugar that doesn't actually make it
1210 * to the kernel.
1211 */
1212 if ((p = strrchr(anchorname, '/')) != NULL &&
1213 p[1] == '*' && p[2] == '\0') {
1214 p[0] = '\0';
1215 }
1216
1217 if (anchorname[0] == '/') {
1218 if ((npath = calloc(1, MAXPATHLEN)) == NULL)
1219 errx(1, "calloc");
1220 snprintf(npath, MAXPATHLEN, "%s", anchorname);
1221 } else {
1222 if (path[0])
1223 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname);
1224 else
1225 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname);
1226 npath = path;
1227 }
1228
1229 /*
1230 * If this anchor was called with a wildcard path, go through
1231 * the rulesets in the anchor rather than the rules.
1232 */
1233 if (wildcard && (opts & PF_OPT_RECURSE)) {
1234 struct pfctl_eth_rulesets_info ri;
1235 u_int32_t mnr, nr;
1236
1237 if ((ret = pfctl_get_eth_rulesets_info(dev, &ri, npath)) != 0) {
1238 if (ret == EINVAL) {
1239 fprintf(stderr, "Anchor '%s' "
1240 "not found.\n", anchorname);
1241 } else {
1242 warnc(ret, "DIOCGETETHRULESETS");
1243 return (-1);
1244 }
1245 }
1246 mnr = ri.nr;
1247
1248 pfctl_print_eth_rule_counters(&rule, opts);
1249 for (nr = 0; nr < mnr; ++nr) {
1250 struct pfctl_eth_ruleset_info rs;
1251
1252 if ((ret = pfctl_get_eth_ruleset(dev, npath, nr, &rs)) != 0)
1253 errc(1, ret, "DIOCGETETHRULESET");
1254 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1255 printf("anchor \"%s\" all {\n", rs.name);
1256 pfctl_show_eth_rules(dev, npath, opts,
1257 format, rs.name, depth + 1, 0);
1258 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1259 printf("}\n");
1260 }
1261 path[len] = '\0';
1262 return (0);
1263 }
1264
1265 if ((ret = pfctl_get_eth_rules_info(dev, &info, path)) != 0) {
1266 warnc(ret, "DIOCGETETHRULES");
1267 return (-1);
1268 }
1269 for (int nr = 0; nr < info.nr; nr++) {
1270 brace = 0;
1271 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1272 if ((ret = pfctl_get_eth_rule(dev, nr, info.ticket, path, &rule,
1273 opts & PF_OPT_CLRRULECTRS, anchor_call)) != 0) {
1274 warnc(ret, "DIOCGETETHRULE");
1275 return (-1);
1276 }
1277 if (anchor_call[0] &&
1278 ((((p = strrchr(anchor_call, '_')) != NULL) &&
1279 (p == anchor_call ||
1280 *(--p) == '/')) || (opts & PF_OPT_RECURSE))) {
1281 brace++;
1282 int aclen = strlen(anchor_call);
1283 if (anchor_call[aclen - 1] == '*')
1284 anchor_call[aclen - 2] = '\0';
1285 }
1286 p = &anchor_call[0];
1287 if (dotitle) {
1288 pfctl_print_title("ETH RULES:");
1289 dotitle = 0;
1290 }
1291 print_eth_rule(&rule, anchor_call,
1292 opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG));
1293 if (brace)
1294 printf(" {\n");
1295 else
1296 printf("\n");
1297 pfctl_print_eth_rule_counters(&rule, opts);
1298 if (brace) {
1299 pfctl_show_eth_rules(dev, path, opts, format,
1300 p, depth + 1, rule.anchor_wildcard);
1301 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1302 printf("}\n");
1303 }
1304 }
1305
1306 path[len] = '\0';
1307 return (0);
1308 }
1309
1310 int
pfctl_show_rules(int dev,char * path,int opts,enum pfctl_show format,char * anchorname,int depth,int wildcard)1311 pfctl_show_rules(int dev, char *path, int opts, enum pfctl_show format,
1312 char *anchorname, int depth, int wildcard)
1313 {
1314 struct pfctl_rules_info ri;
1315 struct pfctl_rule rule;
1316 char anchor_call[MAXPATHLEN];
1317 u_int32_t nr, header = 0;
1318 int numeric = opts & PF_OPT_NUMERIC;
1319 int len = strlen(path), ret = 0;
1320 char *npath, *p;
1321
1322 /*
1323 * Truncate a trailing / and * on an anchorname before searching for
1324 * the ruleset, this is syntactic sugar that doesn't actually make it
1325 * to the kernel.
1326 */
1327 if ((p = strrchr(anchorname, '/')) != NULL &&
1328 p[1] == '*' && p[2] == '\0') {
1329 p[0] = '\0';
1330 }
1331
1332 if (anchorname[0] == '/') {
1333 if ((npath = calloc(1, MAXPATHLEN)) == NULL)
1334 errx(1, "calloc");
1335 strlcpy(npath, anchorname, MAXPATHLEN);
1336 } else {
1337 if (path[0])
1338 snprintf(&path[len], MAXPATHLEN - len, "/%s", anchorname);
1339 else
1340 snprintf(&path[len], MAXPATHLEN - len, "%s", anchorname);
1341 npath = path;
1342 }
1343
1344 /*
1345 * If this anchor was called with a wildcard path, go through
1346 * the rulesets in the anchor rather than the rules.
1347 */
1348 if (wildcard && (opts & PF_OPT_RECURSE)) {
1349 struct pfioc_ruleset prs;
1350 u_int32_t mnr, nr;
1351
1352 memset(&prs, 0, sizeof(prs));
1353 if ((ret = pfctl_get_rulesets(pfh, npath, &mnr)) != 0)
1354 errx(1, "%s", pf_strerror(ret));
1355
1356 for (nr = 0; nr < mnr; ++nr) {
1357 if ((ret = pfctl_get_ruleset(pfh, npath, nr, &prs)) != 0)
1358 errx(1, "%s", pf_strerror(ret));
1359 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1360 printf("anchor \"%s\" all {\n", prs.name);
1361 pfctl_show_rules(dev, npath, opts,
1362 format, prs.name, depth + 1, 0);
1363 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1364 printf("}\n");
1365 }
1366 path[len] = '\0';
1367 return (0);
1368 }
1369
1370 if (opts & PF_OPT_SHOWALL) {
1371 ret = pfctl_get_rules_info_h(pfh, &ri, PF_PASS, path);
1372 if (ret != 0) {
1373 warnx("%s", pf_strerror(ret));
1374 goto error;
1375 }
1376 header++;
1377 }
1378 ret = pfctl_get_rules_info_h(pfh, &ri, PF_SCRUB, path);
1379 if (ret != 0) {
1380 warnx("%s", pf_strerror(ret));
1381 goto error;
1382 }
1383 if (opts & PF_OPT_SHOWALL) {
1384 if (format == PFCTL_SHOW_RULES && (ri.nr > 0 || header))
1385 pfctl_print_title("FILTER RULES:");
1386 else if (format == PFCTL_SHOW_LABELS && labels)
1387 pfctl_print_title("LABEL COUNTERS:");
1388 }
1389
1390 for (nr = 0; nr < ri.nr; ++nr) {
1391 if ((ret = pfctl_get_clear_rule_h(pfh, nr, ri.ticket, path, PF_SCRUB,
1392 &rule, anchor_call, opts & PF_OPT_CLRRULECTRS)) != 0) {
1393 warnc(ret, "DIOCGETRULENV");
1394 goto error;
1395 }
1396
1397 if (pfctl_get_pool(dev, &rule.rdr,
1398 nr, ri.ticket, PF_SCRUB, path, PF_RDR) != 0)
1399 goto error;
1400
1401 if (pfctl_get_pool(dev, &rule.nat,
1402 nr, ri.ticket, PF_SCRUB, path, PF_NAT) != 0)
1403 goto error;
1404
1405 if (pfctl_get_pool(dev, &rule.route,
1406 nr, ri.ticket, PF_SCRUB, path, PF_RT) != 0)
1407 goto error;
1408
1409 switch (format) {
1410 case PFCTL_SHOW_LABELS:
1411 break;
1412 case PFCTL_SHOW_RULES:
1413 if (rule.label[0][0] && (opts & PF_OPT_SHOWALL))
1414 labels = 1;
1415 print_rule(&rule, anchor_call, opts, numeric);
1416 /*
1417 * Do not print newline, when we have not
1418 * printed expired rule.
1419 */
1420 if (!(rule.rule_flag & PFRULE_EXPIRED) ||
1421 (opts & (PF_OPT_VERBOSE2|PF_OPT_DEBUG)))
1422 printf("\n");
1423 pfctl_print_rule_counters(&rule, opts);
1424 break;
1425 case PFCTL_SHOW_NOTHING:
1426 break;
1427 }
1428 pfctl_clear_pool(&rule.rdr);
1429 pfctl_clear_pool(&rule.nat);
1430 pfctl_clear_pool(&rule.route);
1431 }
1432 ret = pfctl_get_rules_info_h(pfh, &ri, PF_PASS, path);
1433 if (ret != 0) {
1434 warnc(ret, "DIOCGETRULES");
1435 goto error;
1436 }
1437 for (nr = 0; nr < ri.nr; ++nr) {
1438 if ((ret = pfctl_get_clear_rule_h(pfh, nr, ri.ticket, path, PF_PASS,
1439 &rule, anchor_call, opts & PF_OPT_CLRRULECTRS)) != 0) {
1440 warnc(ret, "DIOCGETRULE");
1441 goto error;
1442 }
1443
1444 if (pfctl_get_pool(dev, &rule.rdr,
1445 nr, ri.ticket, PF_PASS, path, PF_RDR) != 0)
1446 goto error;
1447
1448 if (pfctl_get_pool(dev, &rule.nat,
1449 nr, ri.ticket, PF_PASS, path, PF_NAT) != 0)
1450 goto error;
1451
1452 if (pfctl_get_pool(dev, &rule.route,
1453 nr, ri.ticket, PF_PASS, path, PF_RT) != 0)
1454 goto error;
1455
1456 switch (format) {
1457 case PFCTL_SHOW_LABELS: {
1458 bool show = false;
1459 int i = 0;
1460
1461 while (rule.label[i][0]) {
1462 printf("%s ", rule.label[i++]);
1463 show = true;
1464 }
1465
1466 if (show) {
1467 printf("%llu %llu %llu %llu"
1468 " %llu %llu %llu %ju\n",
1469 (unsigned long long)rule.evaluations,
1470 (unsigned long long)(rule.packets[0] +
1471 rule.packets[1]),
1472 (unsigned long long)(rule.bytes[0] +
1473 rule.bytes[1]),
1474 (unsigned long long)rule.packets[0],
1475 (unsigned long long)rule.bytes[0],
1476 (unsigned long long)rule.packets[1],
1477 (unsigned long long)rule.bytes[1],
1478 (uintmax_t)rule.states_tot);
1479 }
1480
1481 if (anchor_call[0] &&
1482 (((p = strrchr(anchor_call, '/')) ?
1483 p[1] == '_' : anchor_call[0] == '_') ||
1484 opts & PF_OPT_RECURSE)) {
1485 pfctl_show_rules(dev, npath, opts, format,
1486 anchor_call, depth, rule.anchor_wildcard);
1487 }
1488 break;
1489 }
1490 case PFCTL_SHOW_RULES:
1491 if (rule.label[0][0] && (opts & PF_OPT_SHOWALL))
1492 labels = 1;
1493 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1494 print_rule(&rule, anchor_call, opts, numeric);
1495
1496 /*
1497 * If this is a 'unnamed' brace notation
1498 * anchor, OR the user has explicitly requested
1499 * recursion, print it recursively.
1500 */
1501 if (anchor_call[0] &&
1502 (((p = strrchr(anchor_call, '/')) ?
1503 p[1] == '_' : anchor_call[0] == '_') ||
1504 opts & PF_OPT_RECURSE)) {
1505 printf(" {\n");
1506 pfctl_print_rule_counters(&rule, opts);
1507 pfctl_show_rules(dev, npath, opts, format,
1508 anchor_call, depth + 1,
1509 rule.anchor_wildcard);
1510 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1511 printf("}\n");
1512 } else {
1513 printf("\n");
1514 pfctl_print_rule_counters(&rule, opts);
1515 }
1516 break;
1517 case PFCTL_SHOW_NOTHING:
1518 break;
1519 }
1520 pfctl_clear_pool(&rule.rdr);
1521 pfctl_clear_pool(&rule.nat);
1522 }
1523
1524 error:
1525 path[len] = '\0';
1526 return (ret);
1527 }
1528
1529 int
pfctl_show_nat(int dev,const char * path,int opts,char * anchorname,int depth,int wildcard)1530 pfctl_show_nat(int dev, const char *path, int opts, char *anchorname, int depth,
1531 int wildcard)
1532 {
1533 struct pfctl_rules_info ri;
1534 struct pfctl_rule rule;
1535 char anchor_call[MAXPATHLEN];
1536 u_int32_t nr;
1537 static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT };
1538 int i, dotitle = opts & PF_OPT_SHOWALL;
1539 int ret;
1540 int len = strlen(path);
1541 char *npath, *p;
1542
1543 /*
1544 * Truncate a trailing / and * on an anchorname before searching for
1545 * the ruleset, this is syntactic sugar that doesn't actually make it
1546 * to the kernel.
1547 */
1548 if ((p = strrchr(anchorname, '/')) != NULL &&
1549 p[1] == '*' && p[2] == '\0') {
1550 p[0] = '\0';
1551 }
1552
1553 if ((npath = calloc(1, MAXPATHLEN)) == NULL)
1554 errx(1, "calloc");
1555
1556 if (anchorname[0] == '/') {
1557 snprintf(npath, MAXPATHLEN, "%s", anchorname);
1558 } else {
1559 snprintf(npath, MAXPATHLEN, "%s", path);
1560 if (npath[0])
1561 snprintf(&npath[len], MAXPATHLEN - len, "/%s", anchorname);
1562 else
1563 snprintf(&npath[len], MAXPATHLEN - len, "%s", anchorname);
1564 }
1565
1566 /*
1567 * If this anchor was called with a wildcard path, go through
1568 * the rulesets in the anchor rather than the rules.
1569 */
1570 if (wildcard && (opts & PF_OPT_RECURSE)) {
1571 struct pfioc_ruleset prs;
1572 u_int32_t mnr, nr;
1573 memset(&prs, 0, sizeof(prs));
1574 if ((ret = pfctl_get_rulesets(pfh, npath, &mnr)) != 0) {
1575 if (ret == EINVAL)
1576 fprintf(stderr, "NAT anchor '%s' "
1577 "not found.\n", anchorname);
1578 else
1579 errx(1, "%s", pf_strerror(ret));
1580 }
1581
1582 for (nr = 0; nr < mnr; ++nr) {
1583 if ((ret = pfctl_get_ruleset(pfh, npath, nr, &prs)) != 0)
1584 errx(1, "%s", pf_strerror(ret));
1585 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1586 printf("nat-anchor \"%s\" all {\n", prs.name);
1587 pfctl_show_nat(dev, npath, opts,
1588 prs.name, depth + 1, 0);
1589 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1590 printf("}\n");
1591 }
1592 npath[len] = '\0';
1593 return (0);
1594 }
1595
1596 for (i = 0; i < 3; i++) {
1597 ret = pfctl_get_rules_info_h(pfh, &ri, nattype[i], npath);
1598 if (ret != 0) {
1599 warnc(ret, "DIOCGETRULES");
1600 return (-1);
1601 }
1602 for (nr = 0; nr < ri.nr; ++nr) {
1603 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1604
1605 if ((ret = pfctl_get_rule_h(pfh, nr, ri.ticket, npath,
1606 nattype[i], &rule, anchor_call)) != 0) {
1607 warnc(ret, "DIOCGETRULE");
1608 return (-1);
1609 }
1610 if (pfctl_get_pool(dev, &rule.rdr, nr,
1611 ri.ticket, nattype[i], npath, PF_RDR) != 0)
1612 return (-1);
1613 if (pfctl_get_pool(dev, &rule.nat, nr,
1614 ri.ticket, nattype[i], npath, PF_NAT) != 0)
1615 return (-1);
1616 if (pfctl_get_pool(dev, &rule.route, nr,
1617 ri.ticket, nattype[i], npath, PF_RT) != 0)
1618 return (-1);
1619
1620 if (dotitle) {
1621 pfctl_print_title("TRANSLATION RULES:");
1622 dotitle = 0;
1623 }
1624 print_rule(&rule, anchor_call,
1625 opts & PF_OPT_VERBOSE2, opts & PF_OPT_NUMERIC);
1626 if (anchor_call[0] &&
1627 (((p = strrchr(anchor_call, '/')) ?
1628 p[1] == '_' : anchor_call[0] == '_') ||
1629 opts & PF_OPT_RECURSE)) {
1630 printf(" {\n");
1631 pfctl_print_rule_counters(&rule, opts);
1632 pfctl_show_nat(dev, npath, opts, anchor_call,
1633 depth + 1, rule.anchor_wildcard);
1634 INDENT(depth, !(opts & PF_OPT_VERBOSE));
1635 printf("}\n");
1636 } else {
1637 printf("\n");
1638 pfctl_print_rule_counters(&rule, opts);
1639 }
1640 }
1641 }
1642 return (0);
1643 }
1644
1645 static int
pfctl_print_src_node(struct pfctl_src_node * sn,void * arg)1646 pfctl_print_src_node(struct pfctl_src_node *sn, void *arg)
1647 {
1648 int *opts = (int *)arg;
1649
1650 if (*opts & PF_OPT_SHOWALL) {
1651 pfctl_print_title("SOURCE TRACKING NODES:");
1652 *opts &= ~PF_OPT_SHOWALL;
1653 }
1654
1655 print_src_node(sn, *opts);
1656
1657 return (0);
1658 }
1659
1660 int
pfctl_show_src_nodes(int dev,int opts)1661 pfctl_show_src_nodes(int dev, int opts)
1662 {
1663 int error;
1664
1665 error = pfctl_get_srcnodes(pfh, pfctl_print_src_node, &opts);
1666
1667 return (error);
1668 }
1669
1670 struct pfctl_show_state_arg {
1671 int opts;
1672 int dotitle;
1673 const char *iface;
1674 };
1675
1676 static int
pfctl_show_state(struct pfctl_state * s,void * arg)1677 pfctl_show_state(struct pfctl_state *s, void *arg)
1678 {
1679 struct pfctl_show_state_arg *a = (struct pfctl_show_state_arg *)arg;
1680
1681 if (a->dotitle) {
1682 pfctl_print_title("STATES:");
1683 a->dotitle = 0;
1684 }
1685 print_state(s, a->opts);
1686
1687 return (0);
1688 }
1689
1690 int
pfctl_show_states(int dev,const char * iface,int opts)1691 pfctl_show_states(int dev, const char *iface, int opts)
1692 {
1693 struct pfctl_show_state_arg arg;
1694 struct pfctl_state_filter filter = {};
1695
1696 if (iface != NULL)
1697 strlcpy(filter.ifname, iface, IFNAMSIZ);
1698
1699 arg.opts = opts;
1700 arg.dotitle = opts & PF_OPT_SHOWALL;
1701 arg.iface = iface;
1702
1703 if (pfctl_get_filtered_states_iter(&filter, pfctl_show_state, &arg))
1704 return (-1);
1705
1706 return (0);
1707 }
1708
1709 int
pfctl_show_status(int dev,int opts)1710 pfctl_show_status(int dev, int opts)
1711 {
1712 struct pfctl_status *status;
1713 struct pfctl_syncookies cookies;
1714 int ret;
1715
1716 if ((status = pfctl_get_status_h(pfh)) == NULL) {
1717 warn("DIOCGETSTATUS");
1718 return (-1);
1719 }
1720 if ((ret = pfctl_get_syncookies(dev, &cookies)) != 0) {
1721 pfctl_free_status(status);
1722 warnc(ret, "DIOCGETSYNCOOKIES");
1723 return (-1);
1724 }
1725 if (opts & PF_OPT_SHOWALL)
1726 pfctl_print_title("INFO:");
1727 print_status(status, &cookies, opts);
1728 pfctl_free_status(status);
1729 return (0);
1730 }
1731
1732 int
pfctl_show_running(int dev)1733 pfctl_show_running(int dev)
1734 {
1735 struct pfctl_status *status;
1736 int running;
1737
1738 if ((status = pfctl_get_status_h(pfh)) == NULL) {
1739 warn("DIOCGETSTATUS");
1740 return (-1);
1741 }
1742
1743 running = status->running;
1744
1745 print_running(status);
1746 pfctl_free_status(status);
1747 return (!running);
1748 }
1749
1750 int
pfctl_show_timeouts(int dev,int opts)1751 pfctl_show_timeouts(int dev, int opts)
1752 {
1753 uint32_t seconds;
1754 int i;
1755 int ret;
1756
1757 if (opts & PF_OPT_SHOWALL)
1758 pfctl_print_title("TIMEOUTS:");
1759 for (i = 0; pf_timeouts[i].name; i++) {
1760 if ((ret = pfctl_get_timeout(pfh, pf_timeouts[i].timeout, &seconds)) != 0)
1761 errc(1, ret, "DIOCGETTIMEOUT");
1762 printf("%-20s %10d", pf_timeouts[i].name, seconds);
1763 if (pf_timeouts[i].timeout >= PFTM_ADAPTIVE_START &&
1764 pf_timeouts[i].timeout <= PFTM_ADAPTIVE_END)
1765 printf(" states");
1766 else
1767 printf("s");
1768 printf("\n");
1769 }
1770 return (0);
1771
1772 }
1773
1774 int
pfctl_show_limits(int dev,int opts)1775 pfctl_show_limits(int dev, int opts)
1776 {
1777 unsigned int limit;
1778 int i;
1779 int ret;
1780
1781 if (opts & PF_OPT_SHOWALL)
1782 pfctl_print_title("LIMITS:");
1783 for (i = 0; pf_limits[i].name; i++) {
1784 if ((ret = pfctl_get_limit(pfh, pf_limits[i].index, &limit)) != 0)
1785 errc(1, ret, "DIOCGETLIMIT");
1786 printf("%-13s ", pf_limits[i].name);
1787 if (limit == UINT_MAX)
1788 printf("unlimited\n");
1789 else
1790 printf("hard limit %8u\n", limit);
1791 }
1792 return (0);
1793 }
1794
1795 void
pfctl_read_limits(struct pfctl_handle * h)1796 pfctl_read_limits(struct pfctl_handle *h)
1797 {
1798 int i;
1799
1800 for (i = 0; pf_limits[i].name; i++) {
1801 if (pfctl_get_limit(h, i, &limit_curr[i]))
1802 err(1, "DIOCGETLIMIT");
1803 }
1804 }
1805
1806 void
pfctl_restore_limits(void)1807 pfctl_restore_limits(void)
1808 {
1809 int i;
1810
1811 if (pfh == NULL)
1812 return;
1813
1814 for (i = 0; pf_limits[i].name; i++) {
1815 if (pfctl_set_limit(pfh, i, limit_curr[i]))
1816 warn("DIOCSETLIMIT (%s)", pf_limits[i].name);
1817 }
1818 }
1819
1820 void
pfctl_show_creators(int opts)1821 pfctl_show_creators(int opts)
1822 {
1823 int ret;
1824 uint32_t creators[16];
1825 size_t count = nitems(creators);
1826
1827 ret = pfctl_get_creatorids(pfh, creators, &count);
1828 if (ret != 0)
1829 errx(ret, "Failed to retrieve creators");
1830
1831 printf("Creator IDs:\n");
1832 for (size_t i = 0; i < count; i++)
1833 printf("%08x\n", creators[i]);
1834 }
1835
1836 /* callbacks for rule/nat/rdr/addr */
1837 int
pfctl_add_pool(struct pfctl * pf,struct pfctl_pool * p,int which)1838 pfctl_add_pool(struct pfctl *pf, struct pfctl_pool *p, int which)
1839 {
1840 struct pfctl_pooladdr *pa;
1841 int ret;
1842
1843 TAILQ_FOREACH(pa, &p->list, entries) {
1844 memcpy(&pf->paddr.addr, pa, sizeof(struct pfctl_pooladdr));
1845 pf->paddr.af = pa->af;
1846 if ((pf->opts & PF_OPT_NOACTION) == 0) {
1847 if ((ret = pfctl_add_addr(pf->h, &pf->paddr, which)) != 0)
1848 errc(1, ret, "DIOCADDADDR");
1849 }
1850 }
1851 return (0);
1852 }
1853
1854 void
pfctl_init_rule(struct pfctl_rule * r)1855 pfctl_init_rule(struct pfctl_rule *r)
1856 {
1857 memset(r, 0, sizeof(struct pfctl_rule));
1858 TAILQ_INIT(&(r->rdr.list));
1859 TAILQ_INIT(&(r->nat.list));
1860 TAILQ_INIT(&(r->route.list));
1861 }
1862
1863 void
pfctl_append_rule(struct pfctl * pf,struct pfctl_rule * r)1864 pfctl_append_rule(struct pfctl *pf, struct pfctl_rule *r)
1865 {
1866 u_int8_t rs_num;
1867 struct pfctl_rule *rule;
1868 struct pfctl_ruleset *rs;
1869
1870 rs_num = pf_get_ruleset_number(r->action);
1871 if (rs_num == PF_RULESET_MAX)
1872 errx(1, "Invalid rule type %d", r->action);
1873
1874 rs = &pf->anchor->ruleset;
1875
1876 if ((rule = calloc(1, sizeof(*rule))) == NULL)
1877 err(1, "calloc");
1878 bcopy(r, rule, sizeof(*rule));
1879 TAILQ_INIT(&rule->rdr.list);
1880 pfctl_move_pool(&r->rdr, &rule->rdr);
1881 TAILQ_INIT(&rule->nat.list);
1882 pfctl_move_pool(&r->nat, &rule->nat);
1883 TAILQ_INIT(&rule->route.list);
1884 pfctl_move_pool(&r->route, &rule->route);
1885
1886 TAILQ_INSERT_TAIL(rs->rules[rs_num].active.ptr, rule, entries);
1887 }
1888
1889 int
pfctl_append_eth_rule(struct pfctl * pf,struct pfctl_eth_rule * r,const char * anchor_call)1890 pfctl_append_eth_rule(struct pfctl *pf, struct pfctl_eth_rule *r,
1891 const char *anchor_call)
1892 {
1893 struct pfctl_eth_rule *rule;
1894 struct pfctl_eth_ruleset *rs;
1895 char *p;
1896
1897 rs = &pf->eanchor->ruleset;
1898
1899 if (anchor_call[0] && r->anchor == NULL) {
1900 /*
1901 * Don't make non-brace anchors part of the main anchor pool.
1902 */
1903 if ((r->anchor = calloc(1, sizeof(*r->anchor))) == NULL)
1904 err(1, "pfctl_append_rule: calloc");
1905
1906 pf_init_eth_ruleset(&r->anchor->ruleset);
1907 r->anchor->ruleset.anchor = r->anchor;
1908 if (strlcpy(r->anchor->path, anchor_call,
1909 sizeof(rule->anchor->path)) >= sizeof(rule->anchor->path))
1910 errx(1, "pfctl_append_rule: strlcpy");
1911 if ((p = strrchr(anchor_call, '/')) != NULL) {
1912 if (!strlen(p))
1913 err(1, "pfctl_append_eth_rule: bad anchor name %s",
1914 anchor_call);
1915 } else
1916 p = (char *)anchor_call;
1917 if (strlcpy(r->anchor->name, p,
1918 sizeof(rule->anchor->name)) >= sizeof(rule->anchor->name))
1919 errx(1, "pfctl_append_eth_rule: strlcpy");
1920 }
1921
1922 if ((rule = calloc(1, sizeof(*rule))) == NULL)
1923 err(1, "calloc");
1924 bcopy(r, rule, sizeof(*rule));
1925
1926 TAILQ_INSERT_TAIL(&rs->rules, rule, entries);
1927 return (0);
1928 }
1929
1930 int
pfctl_eth_ruleset_trans(struct pfctl * pf,char * path,struct pfctl_eth_anchor * a)1931 pfctl_eth_ruleset_trans(struct pfctl *pf, char *path,
1932 struct pfctl_eth_anchor *a)
1933 {
1934 int osize = pf->trans->pfrb_size;
1935
1936 if ((pf->loadopt & PFCTL_FLAG_ETH) != 0) {
1937 if (pfctl_add_trans(pf->trans, PF_RULESET_ETH, path))
1938 return (1);
1939 }
1940 if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize))
1941 return (5);
1942
1943 return (0);
1944 }
1945
1946 int
pfctl_ruleset_trans(struct pfctl * pf,char * path,struct pfctl_anchor * a,bool do_eth)1947 pfctl_ruleset_trans(struct pfctl *pf, char *path, struct pfctl_anchor *a, bool do_eth)
1948 {
1949 int osize = pf->trans->pfrb_size;
1950
1951 if ((pf->loadopt & PFCTL_FLAG_ETH) != 0 && do_eth) {
1952 if (pfctl_add_trans(pf->trans, PF_RULESET_ETH, path))
1953 return (1);
1954 }
1955 if ((pf->loadopt & PFCTL_FLAG_NAT) != 0) {
1956 if (pfctl_add_trans(pf->trans, PF_RULESET_NAT, path) ||
1957 pfctl_add_trans(pf->trans, PF_RULESET_BINAT, path) ||
1958 pfctl_add_trans(pf->trans, PF_RULESET_RDR, path))
1959 return (1);
1960 }
1961 if (a == pf->astack[0] && ((altqsupport &&
1962 (pf->loadopt & PFCTL_FLAG_ALTQ) != 0))) {
1963 if (pfctl_add_trans(pf->trans, PF_RULESET_ALTQ, path))
1964 return (2);
1965 }
1966 if ((pf->loadopt & PFCTL_FLAG_FILTER) != 0) {
1967 if (pfctl_add_trans(pf->trans, PF_RULESET_SCRUB, path) ||
1968 pfctl_add_trans(pf->trans, PF_RULESET_FILTER, path))
1969 return (3);
1970 }
1971 if (pf->loadopt & PFCTL_FLAG_TABLE)
1972 if (pfctl_add_trans(pf->trans, PF_RULESET_TABLE, path))
1973 return (4);
1974 if (pfctl_trans(pf->dev, pf->trans, DIOCXBEGIN, osize))
1975 return (5);
1976
1977 return (0);
1978 }
1979
1980 int
pfctl_load_eth_ruleset(struct pfctl * pf,char * path,struct pfctl_eth_ruleset * rs,int depth)1981 pfctl_load_eth_ruleset(struct pfctl *pf, char *path,
1982 struct pfctl_eth_ruleset *rs, int depth)
1983 {
1984 struct pfctl_eth_rule *r;
1985 int error, len = strlen(path);
1986 int brace = 0;
1987
1988 pf->eanchor = rs->anchor;
1989 if (path[0])
1990 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->eanchor->name);
1991 else
1992 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->eanchor->name);
1993
1994 if (depth) {
1995 if (TAILQ_FIRST(&rs->rules) != NULL) {
1996 brace++;
1997 if (pf->opts & PF_OPT_VERBOSE)
1998 printf(" {\n");
1999 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
2000 (error = pfctl_eth_ruleset_trans(pf,
2001 path, rs->anchor))) {
2002 printf("pfctl_load_eth_rulesets: "
2003 "pfctl_eth_ruleset_trans %d\n", error);
2004 goto error;
2005 }
2006 } else if (pf->opts & PF_OPT_VERBOSE)
2007 printf("\n");
2008 }
2009
2010 while ((r = TAILQ_FIRST(&rs->rules)) != NULL) {
2011 TAILQ_REMOVE(&rs->rules, r, entries);
2012
2013 error = pfctl_load_eth_rule(pf, path, r, depth);
2014 if (error)
2015 return (error);
2016
2017 if (r->anchor) {
2018 if ((error = pfctl_load_eth_ruleset(pf, path,
2019 &r->anchor->ruleset, depth + 1)))
2020 return (error);
2021 } else if (pf->opts & PF_OPT_VERBOSE)
2022 printf("\n");
2023 free(r);
2024 }
2025 if (brace && pf->opts & PF_OPT_VERBOSE) {
2026 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE));
2027 printf("}\n");
2028 }
2029 path[len] = '\0';
2030
2031 return (0);
2032 error:
2033 path[len] = '\0';
2034 return (error);
2035 }
2036
2037 int
pfctl_load_eth_rule(struct pfctl * pf,char * path,struct pfctl_eth_rule * r,int depth)2038 pfctl_load_eth_rule(struct pfctl *pf, char *path, struct pfctl_eth_rule *r,
2039 int depth)
2040 {
2041 char *name;
2042 char anchor[PF_ANCHOR_NAME_SIZE];
2043 int len = strlen(path);
2044 int ret;
2045
2046 if (strlcpy(anchor, path, sizeof(anchor)) >= sizeof(anchor))
2047 errx(1, "pfctl_load_eth_rule: strlcpy");
2048
2049 if (r->anchor) {
2050 if (r->anchor->match) {
2051 if (path[0])
2052 snprintf(&path[len], MAXPATHLEN - len,
2053 "/%s", r->anchor->name);
2054 else
2055 snprintf(&path[len], MAXPATHLEN - len,
2056 "%s", r->anchor->name);
2057 name = r->anchor->name;
2058 } else
2059 name = r->anchor->path;
2060 } else
2061 name = "";
2062
2063 if ((pf->opts & PF_OPT_NOACTION) == 0)
2064 if ((ret = pfctl_add_eth_rule(pf->dev, r, anchor, name,
2065 pf->eth_ticket)) != 0)
2066 errc(1, ret, "DIOCADDETHRULENV");
2067
2068 if (pf->opts & PF_OPT_VERBOSE) {
2069 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2));
2070 print_eth_rule(r, r->anchor ? r->anchor->name : "",
2071 pf->opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG));
2072 }
2073
2074 path[len] = '\0';
2075
2076 return (0);
2077 }
2078
2079 static int
pfctl_load_tables(struct pfctl * pf,char * path,struct pfctl_anchor * a,int rs_num)2080 pfctl_load_tables(struct pfctl *pf, char *path, struct pfctl_anchor *a,
2081 int rs_num)
2082 {
2083 struct pfr_ktable *kt, *ktw;
2084 struct pfr_uktable *ukt;
2085 char anchor_path[PF_ANCHOR_MAXPATH];
2086 int e;
2087
2088 RB_FOREACH_SAFE(kt, pfr_ktablehead, &pfr_ktables, ktw) {
2089 if (strcmp(kt->pfrkt_anchor, a->path) != 0)
2090 continue;
2091
2092 if (path != NULL && *path) {
2093 strlcpy(anchor_path, kt->pfrkt_anchor,
2094 sizeof(anchor_path));
2095 snprintf(kt->pfrkt_anchor, PF_ANCHOR_MAXPATH, "%s/%s",
2096 path, anchor_path);
2097 }
2098 ukt = (struct pfr_uktable *)kt;
2099 e = pfr_ina_define(&ukt->pfrukt_t, ukt->pfrukt_addrs.pfrb_caddr,
2100 ukt->pfrukt_addrs.pfrb_size, NULL, NULL,
2101 pf->anchor->ruleset.tticket,
2102 ukt->pfrukt_init_addr ? PFR_FLAG_ADDRSTOO : 0);
2103 if (e != 0)
2104 err(1, "%s pfr_ina_define() %s@%s", __func__,
2105 kt->pfrkt_name, kt->pfrkt_anchor);
2106 RB_REMOVE(pfr_ktablehead, &pfr_ktables, kt);
2107 pfr_buf_clear(&ukt->pfrukt_addrs);
2108 free(ukt);
2109 }
2110
2111 return (0);
2112 }
2113
2114 int
pfctl_load_ruleset(struct pfctl * pf,char * path,struct pfctl_ruleset * rs,int rs_num,int depth)2115 pfctl_load_ruleset(struct pfctl *pf, char *path, struct pfctl_ruleset *rs,
2116 int rs_num, int depth)
2117 {
2118 struct pfctl_rule *r;
2119 int error, len = strlen(path);
2120 int brace = 0;
2121
2122 pf->anchor = rs->anchor;
2123
2124 if (path[0])
2125 snprintf(&path[len], MAXPATHLEN - len, "/%s", pf->anchor->name);
2126 else
2127 snprintf(&path[len], MAXPATHLEN - len, "%s", pf->anchor->name);
2128
2129 if (depth) {
2130 if (TAILQ_FIRST(rs->rules[rs_num].active.ptr) != NULL) {
2131 brace++;
2132 if (pf->opts & PF_OPT_VERBOSE)
2133 printf(" {\n");
2134 if ((pf->opts & PF_OPT_NOACTION) == 0 &&
2135 (error = pfctl_ruleset_trans(pf,
2136 path, rs->anchor, false))) {
2137 printf("%s: "
2138 "pfctl_ruleset_trans %d\n", __func__, error);
2139 goto error;
2140 }
2141 } else if (pf->opts & PF_OPT_VERBOSE)
2142 printf("\n");
2143 }
2144
2145 if (pf->optimize && rs_num == PF_RULESET_FILTER)
2146 pfctl_optimize_ruleset(pf, rs);
2147
2148 while ((r = TAILQ_FIRST(rs->rules[rs_num].active.ptr)) != NULL) {
2149 TAILQ_REMOVE(rs->rules[rs_num].active.ptr, r, entries);
2150
2151 for (int i = 0; i < PF_RULE_MAX_LABEL_COUNT; i++)
2152 expand_label(r->label[i], PF_RULE_LABEL_SIZE, r);
2153 expand_label(r->tagname, PF_TAG_NAME_SIZE, r);
2154 expand_label(r->match_tagname, PF_TAG_NAME_SIZE, r);
2155
2156 if ((error = pfctl_load_rule(pf, path, r, depth)))
2157 goto error;
2158 if (r->anchor) {
2159 if ((error = pfctl_load_ruleset(pf, path,
2160 &r->anchor->ruleset, rs_num, depth + 1)))
2161 goto error;
2162 if ((error = pfctl_load_tables(pf, path, r->anchor, rs_num)))
2163 goto error;
2164 } else if (pf->opts & PF_OPT_VERBOSE)
2165 printf("\n");
2166 free(r);
2167 }
2168 if (brace && pf->opts & PF_OPT_VERBOSE) {
2169 INDENT(depth - 1, (pf->opts & PF_OPT_VERBOSE));
2170 printf("}\n");
2171 }
2172 path[len] = '\0';
2173 return (0);
2174
2175 error:
2176 path[len] = '\0';
2177 return (error);
2178
2179 }
2180
2181 int
pfctl_load_rule(struct pfctl * pf,char * path,struct pfctl_rule * r,int depth)2182 pfctl_load_rule(struct pfctl *pf, char *path, struct pfctl_rule *r, int depth)
2183 {
2184 u_int8_t rs_num = pf_get_ruleset_number(r->action);
2185 char *name;
2186 uint32_t ticket;
2187 char anchor[PF_ANCHOR_NAME_SIZE];
2188 int len = strlen(path);
2189 int error;
2190 bool was_present;
2191
2192 /* set up anchor before adding to path for anchor_call */
2193 if ((pf->opts & PF_OPT_NOACTION) == 0) {
2194 if (pf->trans == NULL)
2195 errx(1, "pfctl_load_rule: no transaction");
2196 ticket = pfctl_get_ticket(pf->trans, rs_num, path);
2197 if (rs_num == PF_RULESET_FILTER)
2198 pf->anchor->ruleset.tticket = ticket;
2199 }
2200 if (strlcpy(anchor, path, sizeof(anchor)) >= sizeof(anchor))
2201 errx(1, "pfctl_load_rule: strlcpy");
2202
2203 if (r->anchor) {
2204 if (r->anchor->match) {
2205 if (path[0])
2206 snprintf(&path[len], MAXPATHLEN - len,
2207 "/%s", r->anchor->name);
2208 else
2209 snprintf(&path[len], MAXPATHLEN - len,
2210 "%s", r->anchor->name);
2211 name = r->anchor->name;
2212 } else
2213 name = r->anchor->path;
2214 } else
2215 name = "";
2216
2217 was_present = false;
2218 if ((pf->opts & PF_OPT_NOACTION) == 0) {
2219 if ((pf->opts & PF_OPT_NOACTION) == 0) {
2220 if ((error = pfctl_begin_addrs(pf->h,
2221 &pf->paddr.ticket)) != 0)
2222 errc(1, error, "DIOCBEGINADDRS");
2223 }
2224
2225 if (pfctl_add_pool(pf, &r->rdr, PF_RDR))
2226 return (1);
2227 if (pfctl_add_pool(pf, &r->nat, PF_NAT))
2228 return (1);
2229 if (pfctl_add_pool(pf, &r->route, PF_RT))
2230 return (1);
2231 error = pfctl_add_rule_h(pf->h, r, anchor, name, ticket,
2232 pf->paddr.ticket);
2233 switch (error) {
2234 case 0:
2235 /* things worked, do nothing */
2236 break;
2237 case EEXIST:
2238 /* an identical rule is already present */
2239 was_present = true;
2240 break;
2241 default:
2242 errc(1, error, "DIOCADDRULE");
2243 }
2244 }
2245
2246 if (pf->opts & PF_OPT_VERBOSE) {
2247 INDENT(depth, !(pf->opts & PF_OPT_VERBOSE2));
2248 print_rule(r, name,
2249 pf->opts & PF_OPT_VERBOSE2,
2250 pf->opts & PF_OPT_NUMERIC);
2251 if (was_present)
2252 printf(" -- rule was already present");
2253 }
2254 path[len] = '\0';
2255 pfctl_clear_pool(&r->rdr);
2256 pfctl_clear_pool(&r->nat);
2257 return (0);
2258 }
2259
2260 int
pfctl_add_altq(struct pfctl * pf,struct pf_altq * a)2261 pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
2262 {
2263 if (altqsupport &&
2264 (loadopt & PFCTL_FLAG_ALTQ) != 0) {
2265 memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq));
2266 if ((pf->opts & PF_OPT_NOACTION) == 0) {
2267 if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) {
2268 if (errno == ENXIO)
2269 errx(1, "qtype not configured");
2270 else if (errno == ENODEV)
2271 errx(1, "%s: driver does not support "
2272 "altq", a->ifname);
2273 else
2274 err(1, "DIOCADDALTQ");
2275 }
2276 }
2277 pfaltq_store(&pf->paltq->altq);
2278 }
2279 return (0);
2280 }
2281
2282 int
pfctl_rules(int dev,char * filename,int opts,int optimize,char * anchorname,struct pfr_buffer * trans)2283 pfctl_rules(int dev, char *filename, int opts, int optimize,
2284 char *anchorname, struct pfr_buffer *trans)
2285 {
2286 #define ERR(...) do { warn(__VA_ARGS__); goto _error; } while(0)
2287 #define ERRX(...) do { warnx(__VA_ARGS__); goto _error; } while(0)
2288
2289 struct pfr_buffer *t, buf;
2290 struct pfioc_altq pa;
2291 struct pfctl pf;
2292 struct pfctl_ruleset *rs;
2293 struct pfctl_eth_ruleset *ethrs;
2294 struct pfr_table trs;
2295 char *path = NULL;
2296 int osize;
2297
2298 RB_INIT(&pf_anchors);
2299 memset(&pf_main_anchor, 0, sizeof(pf_main_anchor));
2300 pf_init_ruleset(&pf_main_anchor.ruleset);
2301 memset(&pf, 0, sizeof(pf));
2302 memset(&trs, 0, sizeof(trs));
2303 pf_main_anchor.ruleset.anchor = &pf_main_anchor;
2304
2305 memset(&pf_eth_main_anchor, 0, sizeof(pf_eth_main_anchor));
2306 pf_init_eth_ruleset(&pf_eth_main_anchor.ruleset);
2307 pf_eth_main_anchor.ruleset.anchor = &pf_eth_main_anchor;
2308
2309 if (trans == NULL) {
2310 bzero(&buf, sizeof(buf));
2311 buf.pfrb_type = PFRB_TRANS;
2312 pf.trans = &buf;
2313 t = &buf;
2314 osize = 0;
2315 } else {
2316 t = trans;
2317 osize = t->pfrb_size;
2318 }
2319
2320 memset(&pa, 0, sizeof(pa));
2321 pa.version = PFIOC_ALTQ_VERSION;
2322 memset(&pf, 0, sizeof(pf));
2323 memset(&trs, 0, sizeof(trs));
2324 if ((path = calloc(1, MAXPATHLEN)) == NULL)
2325 ERRX("%s: calloc", __func__);
2326 if (strlcpy(trs.pfrt_anchor, anchorname,
2327 sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor))
2328 ERRX("%s: strlcpy", __func__);
2329 pf.dev = dev;
2330 pf.h = pfh;
2331 pf.opts = opts;
2332 pf.optimize = optimize;
2333 pf.loadopt = loadopt;
2334
2335 /* non-brace anchor, create without resolving the path */
2336 if ((pf.anchor = calloc(1, sizeof(*pf.anchor))) == NULL)
2337 ERRX("%s: calloc", __func__);
2338 rs = &pf.anchor->ruleset;
2339 pf_init_ruleset(rs);
2340 rs->anchor = pf.anchor;
2341 if (strlcpy(pf.anchor->path, anchorname,
2342 sizeof(pf.anchor->path)) >= sizeof(pf.anchor->path))
2343 errx(1, "%s: strlcpy", __func__);
2344 if (strlcpy(pf.anchor->name, anchorname,
2345 sizeof(pf.anchor->name)) >= sizeof(pf.anchor->name))
2346 errx(1, "%s: strlcpy", __func__);
2347
2348
2349 pf.astack[0] = pf.anchor;
2350 pf.asd = 0;
2351 if (anchorname[0])
2352 pf.loadopt &= ~PFCTL_FLAG_ALTQ;
2353 pf.paltq = &pa;
2354 pf.trans = t;
2355 pfctl_init_options(&pf);
2356
2357 /* Set up ethernet anchor */
2358 if ((pf.eanchor = calloc(1, sizeof(*pf.eanchor))) == NULL)
2359 ERRX("%s: calloc", __func__);
2360
2361 if (strlcpy(pf.eanchor->path, anchorname,
2362 sizeof(pf.eanchor->path)) >= sizeof(pf.eanchor->path))
2363 errx(1, "%s: strlcpy", __func__);
2364 if (strlcpy(pf.eanchor->name, anchorname,
2365 sizeof(pf.eanchor->name)) >= sizeof(pf.eanchor->name))
2366 errx(1, "%s: strlcpy", __func__);
2367
2368 ethrs = &pf.eanchor->ruleset;
2369 pf_init_eth_ruleset(ethrs);
2370 ethrs->anchor = pf.eanchor;
2371 pf.eastack[0] = pf.eanchor;
2372
2373 if ((opts & PF_OPT_NOACTION) == 0) {
2374 /*
2375 * XXX For the time being we need to open transactions for
2376 * the main ruleset before parsing, because tables are still
2377 * loaded at parse time.
2378 */
2379 if (pfctl_ruleset_trans(&pf, anchorname, pf.anchor, true))
2380 ERRX("%s", __func__);
2381 if (pf.loadopt & PFCTL_FLAG_ETH)
2382 pf.eth_ticket = pfctl_get_ticket(t, PF_RULESET_ETH, anchorname);
2383 if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ))
2384 pa.ticket =
2385 pfctl_get_ticket(t, PF_RULESET_ALTQ, anchorname);
2386 if (pf.loadopt & PFCTL_FLAG_TABLE)
2387 pf.astack[0]->ruleset.tticket =
2388 pfctl_get_ticket(t, PF_RULESET_TABLE, anchorname);
2389 }
2390
2391 if (parse_config(filename, &pf) < 0) {
2392 if ((opts & PF_OPT_NOACTION) == 0)
2393 ERRX("Syntax error in config file: "
2394 "pf rules not loaded");
2395 else
2396 goto _error;
2397 }
2398 if (loadopt & PFCTL_FLAG_OPTION)
2399 pfctl_adjust_skip_ifaces(&pf);
2400
2401 if ((pf.loadopt & PFCTL_FLAG_FILTER &&
2402 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_SCRUB, 0))) ||
2403 (pf.loadopt & PFCTL_FLAG_ETH &&
2404 (pfctl_load_eth_ruleset(&pf, path, ethrs, 0))) ||
2405 (pf.loadopt & PFCTL_FLAG_NAT &&
2406 (pfctl_load_ruleset(&pf, path, rs, PF_RULESET_NAT, 0) ||
2407 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_RDR, 0) ||
2408 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_BINAT, 0))) ||
2409 (pf.loadopt & PFCTL_FLAG_FILTER &&
2410 pfctl_load_ruleset(&pf, path, rs, PF_RULESET_FILTER, 0))) {
2411 if ((opts & PF_OPT_NOACTION) == 0)
2412 ERRX("Unable to load rules into kernel");
2413 else
2414 goto _error;
2415 }
2416
2417 if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))
2418 if (check_commit_altq(dev, opts) != 0)
2419 ERRX("errors in altq config");
2420
2421 if (trans == NULL) {
2422 /* process "load anchor" directives */
2423 if (pfctl_load_anchors(dev, &pf) == -1)
2424 ERRX("load anchors");
2425
2426 if ((opts & PF_OPT_NOACTION) == 0) {
2427 if (!anchorname[0] && pfctl_load_options(&pf))
2428 goto _error;
2429 if (pfctl_trans(dev, t, DIOCXCOMMIT, osize))
2430 ERR("DIOCXCOMMIT");
2431 }
2432 }
2433 free(path);
2434 return (0);
2435
2436 _error:
2437 if (trans == NULL) { /* main ruleset */
2438 if ((opts & PF_OPT_NOACTION) == 0)
2439 if (pfctl_trans(dev, t, DIOCXROLLBACK, osize))
2440 err(1, "DIOCXROLLBACK");
2441 exit(1);
2442 } else { /* sub ruleset */
2443 free(path);
2444 return (-1);
2445 }
2446
2447 #undef ERR
2448 #undef ERRX
2449 }
2450
2451 FILE *
pfctl_fopen(const char * name,const char * mode)2452 pfctl_fopen(const char *name, const char *mode)
2453 {
2454 struct stat st;
2455 FILE *fp;
2456
2457 fp = fopen(name, mode);
2458 if (fp == NULL)
2459 return (NULL);
2460 if (fstat(fileno(fp), &st)) {
2461 fclose(fp);
2462 return (NULL);
2463 }
2464 if (S_ISDIR(st.st_mode)) {
2465 fclose(fp);
2466 errno = EISDIR;
2467 return (NULL);
2468 }
2469 return (fp);
2470 }
2471
2472 void
pfctl_init_options(struct pfctl * pf)2473 pfctl_init_options(struct pfctl *pf)
2474 {
2475
2476 pf->timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
2477 pf->timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
2478 pf->timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
2479 pf->timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
2480 pf->timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
2481 pf->timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
2482 pf->timeout[PFTM_SCTP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
2483 pf->timeout[PFTM_SCTP_OPENING] = PFTM_TCP_OPENING_VAL;
2484 pf->timeout[PFTM_SCTP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
2485 pf->timeout[PFTM_SCTP_CLOSING] = PFTM_TCP_CLOSING_VAL;
2486 pf->timeout[PFTM_SCTP_CLOSED] = PFTM_TCP_CLOSED_VAL;
2487 pf->timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
2488 pf->timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
2489 pf->timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
2490 pf->timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
2491 pf->timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
2492 pf->timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
2493 pf->timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
2494 pf->timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
2495 pf->timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
2496 pf->timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
2497 pf->timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
2498 pf->timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
2499 pf->timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
2500 pf->timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
2501
2502 pf->limit[PF_LIMIT_STATES] = PFSTATE_HIWAT;
2503 pf->limit[PF_LIMIT_FRAGS] = PFFRAG_FRENT_HIWAT;
2504
2505 pf->limit[PF_LIMIT_SRC_NODES] = (limit_curr[PF_LIMIT_SRC_NODES] == 0) ?
2506 PFSNODE_HIWAT : limit_curr[PF_LIMIT_SRC_NODES];
2507 pf->limit[PF_LIMIT_TABLE_ENTRIES] =
2508 (limit_curr[PF_LIMIT_TABLE_ENTRIES] == 0) ?
2509 PFR_KENTRY_HIWAT : limit_curr[PF_LIMIT_TABLE_ENTRIES];
2510 pf->limit[PF_LIMIT_ANCHORS] = (limit_curr[PF_LIMIT_ANCHORS] == 0) ?
2511 PF_ANCHOR_HIWAT : limit_curr[PF_LIMIT_ANCHORS];
2512
2513 pf->debug = PF_DEBUG_URGENT;
2514 pf->reassemble = 0;
2515
2516 pf->syncookies = false;
2517 pf->syncookieswat[0] = PF_SYNCOOKIES_LOWATPCT;
2518 pf->syncookieswat[1] = PF_SYNCOOKIES_HIWATPCT;
2519 }
2520
2521 int
pfctl_load_options(struct pfctl * pf)2522 pfctl_load_options(struct pfctl *pf)
2523 {
2524 int i, error = 0;
2525
2526 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2527 return (0);
2528
2529 /* load limits */
2530 for (i = 0; i < PF_LIMIT_MAX; i++) {
2531 if ((pf->opts & PF_OPT_MERGE) && !pf->limit_set[i])
2532 continue;
2533 if (pfctl_load_limit(pf, i, pf->limit[i]))
2534 error = 1;
2535 }
2536
2537 /*
2538 * If we've set the states limit, but haven't explicitly set adaptive
2539 * timeouts, do it now with a start of 60% and end of 120%.
2540 */
2541 if (pf->limit_set[PF_LIMIT_STATES] &&
2542 !pf->timeout_set[PFTM_ADAPTIVE_START] &&
2543 !pf->timeout_set[PFTM_ADAPTIVE_END]) {
2544 pf->timeout[PFTM_ADAPTIVE_START] =
2545 (pf->limit[PF_LIMIT_STATES] / 10) * 6;
2546 pf->timeout_set[PFTM_ADAPTIVE_START] = 1;
2547 pf->timeout[PFTM_ADAPTIVE_END] =
2548 (pf->limit[PF_LIMIT_STATES] / 10) * 12;
2549 pf->timeout_set[PFTM_ADAPTIVE_END] = 1;
2550 }
2551
2552 /* load timeouts */
2553 for (i = 0; i < PFTM_MAX; i++) {
2554 if ((pf->opts & PF_OPT_MERGE) && !pf->timeout_set[i])
2555 continue;
2556 if (pfctl_load_timeout(pf, i, pf->timeout[i]))
2557 error = 1;
2558 }
2559
2560 /* load debug */
2561 if (!(pf->opts & PF_OPT_MERGE) || pf->debug_set)
2562 if (pfctl_load_debug(pf, pf->debug))
2563 error = 1;
2564
2565 /* load logif */
2566 if (!(pf->opts & PF_OPT_MERGE) || pf->ifname_set)
2567 if (pfctl_load_logif(pf, pf->ifname))
2568 error = 1;
2569
2570 /* load hostid */
2571 if (!(pf->opts & PF_OPT_MERGE) || pf->hostid_set)
2572 if (pfctl_load_hostid(pf, pf->hostid))
2573 error = 1;
2574
2575 /* load reassembly settings */
2576 if (!(pf->opts & PF_OPT_MERGE) || pf->reass_set)
2577 if (pfctl_load_reassembly(pf, pf->reassemble))
2578 error = 1;
2579
2580 /* load keepcounters */
2581 if (pfctl_set_keepcounters(pf->dev, pf->keep_counters))
2582 error = 1;
2583
2584 /* load syncookies settings */
2585 if (pfctl_load_syncookies(pf, pf->syncookies))
2586 error = 1;
2587
2588 return (error);
2589 }
2590
2591 int
pfctl_apply_limit(struct pfctl * pf,const char * opt,unsigned int limit)2592 pfctl_apply_limit(struct pfctl *pf, const char *opt, unsigned int limit)
2593 {
2594 int i;
2595
2596
2597 for (i = 0; pf_limits[i].name; i++) {
2598 if (strcasecmp(opt, pf_limits[i].name) == 0) {
2599 pf->limit[pf_limits[i].index] = limit;
2600 pf->limit_set[pf_limits[i].index] = 1;
2601 break;
2602 }
2603 }
2604 if (pf_limits[i].name == NULL) {
2605 warnx("Bad pool name.");
2606 return (1);
2607 }
2608
2609 if (pf->opts & PF_OPT_VERBOSE)
2610 printf("set limit %s %d\n", opt, limit);
2611
2612 if ((pf->opts & PF_OPT_NOACTION) == 0)
2613 pfctl_load_options(pf);
2614
2615 return (0);
2616 }
2617
2618 int
pfctl_load_limit(struct pfctl * pf,unsigned int index,unsigned int limit)2619 pfctl_load_limit(struct pfctl *pf, unsigned int index, unsigned int limit)
2620 {
2621 if (pfctl_set_limit(pf->h, index, limit)) {
2622 if (errno == EBUSY)
2623 warnx("Current pool size exceeds requested %s limit %u",
2624 pf_limits[index].name, limit);
2625 else
2626 warnx("Cannot set %s limit to %u",
2627 pf_limits[index].name, limit);
2628 return (1);
2629 }
2630 return (0);
2631 }
2632
2633 int
pfctl_apply_timeout(struct pfctl * pf,const char * opt,int seconds,int quiet)2634 pfctl_apply_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
2635 {
2636 int i;
2637
2638 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2639 return (0);
2640
2641 for (i = 0; pf_timeouts[i].name; i++) {
2642 if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
2643 pf->timeout[pf_timeouts[i].timeout] = seconds;
2644 pf->timeout_set[pf_timeouts[i].timeout] = 1;
2645 break;
2646 }
2647 }
2648
2649 if (pf_timeouts[i].name == NULL) {
2650 warnx("Bad timeout name.");
2651 return (1);
2652 }
2653
2654
2655 if (pf->opts & PF_OPT_VERBOSE && ! quiet)
2656 printf("set timeout %s %d\n", opt, seconds);
2657
2658 return (0);
2659 }
2660
2661 int
pfctl_load_timeout(struct pfctl * pf,unsigned int timeout,unsigned int seconds)2662 pfctl_load_timeout(struct pfctl *pf, unsigned int timeout, unsigned int seconds)
2663 {
2664 if (pfctl_set_timeout(pf->h, timeout, seconds)) {
2665 warnx("DIOCSETTIMEOUT");
2666 return (1);
2667 }
2668 return (0);
2669 }
2670
2671 int
pfctl_set_reassembly(struct pfctl * pf,int on,int nodf)2672 pfctl_set_reassembly(struct pfctl *pf, int on, int nodf)
2673 {
2674 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2675 return (0);
2676
2677 pf->reass_set = 1;
2678 if (on) {
2679 pf->reassemble = PF_REASS_ENABLED;
2680 if (nodf)
2681 pf->reassemble |= PF_REASS_NODF;
2682 } else {
2683 pf->reassemble = 0;
2684 }
2685
2686 if (pf->opts & PF_OPT_VERBOSE)
2687 printf("set reassemble %s %s\n", on ? "yes" : "no",
2688 nodf ? "no-df" : "");
2689
2690 return (0);
2691 }
2692
2693 int
pfctl_set_optimization(struct pfctl * pf,const char * opt)2694 pfctl_set_optimization(struct pfctl *pf, const char *opt)
2695 {
2696 const struct pf_hint *hint;
2697 int i, r;
2698
2699 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2700 return (0);
2701
2702 for (i = 0; pf_hints[i].name; i++)
2703 if (strcasecmp(opt, pf_hints[i].name) == 0)
2704 break;
2705
2706 hint = pf_hints[i].hint;
2707 if (hint == NULL) {
2708 warnx("invalid state timeouts optimization");
2709 return (1);
2710 }
2711
2712 for (i = 0; hint[i].name; i++)
2713 if ((r = pfctl_apply_timeout(pf, hint[i].name,
2714 hint[i].timeout, 1)))
2715 return (r);
2716
2717 if (pf->opts & PF_OPT_VERBOSE)
2718 printf("set optimization %s\n", opt);
2719
2720 return (0);
2721 }
2722
2723 int
pfctl_set_logif(struct pfctl * pf,char * ifname)2724 pfctl_set_logif(struct pfctl *pf, char *ifname)
2725 {
2726
2727 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2728 return (0);
2729
2730 if (!strcmp(ifname, "none")) {
2731 free(pf->ifname);
2732 pf->ifname = NULL;
2733 } else {
2734 pf->ifname = strdup(ifname);
2735 if (!pf->ifname)
2736 errx(1, "pfctl_set_logif: strdup");
2737 }
2738 pf->ifname_set = 1;
2739
2740 if (pf->opts & PF_OPT_VERBOSE)
2741 printf("set loginterface %s\n", ifname);
2742
2743 return (0);
2744 }
2745
2746 int
pfctl_load_logif(struct pfctl * pf,char * ifname)2747 pfctl_load_logif(struct pfctl *pf, char *ifname)
2748 {
2749 if (ifname != NULL && strlen(ifname) >= IFNAMSIZ) {
2750 warnx("pfctl_load_logif: strlcpy");
2751 return (1);
2752 }
2753 return (pfctl_set_statusif(pfh, ifname ? ifname : ""));
2754 }
2755
2756 void
pfctl_set_hostid(struct pfctl * pf,u_int32_t hostid)2757 pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
2758 {
2759 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2760 return;
2761
2762 HTONL(hostid);
2763
2764 pf->hostid = hostid;
2765 pf->hostid_set = 1;
2766
2767 if (pf->opts & PF_OPT_VERBOSE)
2768 printf("set hostid 0x%08x\n", ntohl(hostid));
2769 }
2770
2771 int
pfctl_load_hostid(struct pfctl * pf,u_int32_t hostid)2772 pfctl_load_hostid(struct pfctl *pf, u_int32_t hostid)
2773 {
2774 if (ioctl(dev, DIOCSETHOSTID, &hostid)) {
2775 warnx("DIOCSETHOSTID");
2776 return (1);
2777 }
2778 return (0);
2779 }
2780
2781 int
pfctl_load_reassembly(struct pfctl * pf,u_int32_t reassembly)2782 pfctl_load_reassembly(struct pfctl *pf, u_int32_t reassembly)
2783 {
2784 if (ioctl(dev, DIOCSETREASS, &reassembly)) {
2785 warnx("DIOCSETREASS");
2786 return (1);
2787 }
2788 return (0);
2789 }
2790
2791 int
pfctl_load_syncookies(struct pfctl * pf,u_int8_t val)2792 pfctl_load_syncookies(struct pfctl *pf, u_int8_t val)
2793 {
2794 struct pfctl_syncookies cookies;
2795
2796 bzero(&cookies, sizeof(cookies));
2797
2798 cookies.mode = val;
2799 cookies.lowwater = pf->syncookieswat[0];
2800 cookies.highwater = pf->syncookieswat[1];
2801
2802 if (pfctl_set_syncookies(dev, &cookies)) {
2803 warnx("DIOCSETSYNCOOKIES");
2804 return (1);
2805 }
2806 return (0);
2807 }
2808
2809 int
pfctl_cfg_syncookies(struct pfctl * pf,uint8_t val,struct pfctl_watermarks * w)2810 pfctl_cfg_syncookies(struct pfctl *pf, uint8_t val, struct pfctl_watermarks *w)
2811 {
2812 if (val != PF_SYNCOOKIES_ADAPTIVE && w != NULL) {
2813 warnx("syncookies start/end only apply to adaptive");
2814 return (1);
2815 }
2816 if (val == PF_SYNCOOKIES_ADAPTIVE && w != NULL) {
2817 if (!w->hi)
2818 w->hi = PF_SYNCOOKIES_HIWATPCT;
2819 if (!w->lo)
2820 w->lo = w->hi / 2;
2821 if (w->lo >= w->hi) {
2822 warnx("start must be higher than end");
2823 return (1);
2824 }
2825 pf->syncookieswat[0] = w->lo;
2826 pf->syncookieswat[1] = w->hi;
2827 pf->syncookieswat_set = 1;
2828 }
2829
2830 if (pf->opts & PF_OPT_VERBOSE) {
2831 if (val == PF_SYNCOOKIES_NEVER)
2832 printf("set syncookies never\n");
2833 else if (val == PF_SYNCOOKIES_ALWAYS)
2834 printf("set syncookies always\n");
2835 else if (val == PF_SYNCOOKIES_ADAPTIVE) {
2836 if (pf->syncookieswat_set)
2837 printf("set syncookies adaptive (start %u%%, "
2838 "end %u%%)\n", pf->syncookieswat[1],
2839 pf->syncookieswat[0]);
2840 else
2841 printf("set syncookies adaptive\n");
2842 } else { /* cannot happen */
2843 warnx("king bula ate all syncookies");
2844 return (1);
2845 }
2846 }
2847
2848 pf->syncookies = val;
2849 return (0);
2850 }
2851
2852 int
pfctl_do_set_debug(struct pfctl * pf,char * d)2853 pfctl_do_set_debug(struct pfctl *pf, char *d)
2854 {
2855 u_int32_t level;
2856 int ret;
2857
2858 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2859 return (0);
2860
2861 if (!strcmp(d, "none"))
2862 pf->debug = PF_DEBUG_NONE;
2863 else if (!strcmp(d, "urgent"))
2864 pf->debug = PF_DEBUG_URGENT;
2865 else if (!strcmp(d, "misc"))
2866 pf->debug = PF_DEBUG_MISC;
2867 else if (!strcmp(d, "loud"))
2868 pf->debug = PF_DEBUG_NOISY;
2869 else {
2870 warnx("unknown debug level \"%s\"", d);
2871 return (-1);
2872 }
2873
2874 pf->debug_set = 1;
2875 level = pf->debug;
2876
2877 if ((pf->opts & PF_OPT_NOACTION) == 0)
2878 if ((ret = pfctl_set_debug(pfh, level)) != 0)
2879 errc(1, ret, "DIOCSETDEBUG");
2880
2881 if (pf->opts & PF_OPT_VERBOSE)
2882 printf("set debug %s\n", d);
2883
2884 return (0);
2885 }
2886
2887 int
pfctl_load_debug(struct pfctl * pf,unsigned int level)2888 pfctl_load_debug(struct pfctl *pf, unsigned int level)
2889 {
2890 if (pfctl_set_debug(pf->h, level)) {
2891 warnx("DIOCSETDEBUG");
2892 return (1);
2893 }
2894 return (0);
2895 }
2896
2897 int
pfctl_set_interface_flags(struct pfctl * pf,char * ifname,int flags,int how)2898 pfctl_set_interface_flags(struct pfctl *pf, char *ifname, int flags, int how)
2899 {
2900 struct pfioc_iface pi;
2901 struct node_host *h = NULL, *n = NULL;
2902
2903 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2904 return (0);
2905
2906 bzero(&pi, sizeof(pi));
2907
2908 pi.pfiio_flags = flags;
2909
2910 /* Make sure our cache matches the kernel. If we set or clear the flag
2911 * for a group this applies to all members. */
2912 h = ifa_grouplookup(ifname, 0);
2913 for (n = h; n != NULL; n = n->next)
2914 pfctl_set_interface_flags(pf, n->ifname, flags, how);
2915
2916 if (strlcpy(pi.pfiio_name, ifname, sizeof(pi.pfiio_name)) >=
2917 sizeof(pi.pfiio_name))
2918 errx(1, "pfctl_set_interface_flags: strlcpy");
2919
2920 if ((pf->opts & PF_OPT_NOACTION) == 0) {
2921 if (how == 0) {
2922 if (ioctl(pf->dev, DIOCCLRIFFLAG, &pi))
2923 pfctl_err(pf->opts, 1, "DIOCCLRIFFLAG");
2924 } else {
2925 if (ioctl(pf->dev, DIOCSETIFFLAG, &pi))
2926 err(1, "DIOCSETIFFLAG");
2927 pfctl_check_skip_ifaces(ifname);
2928 }
2929 }
2930 return (0);
2931 }
2932
2933 void
pfctl_debug(int dev,u_int32_t level,int opts)2934 pfctl_debug(int dev, u_int32_t level, int opts)
2935 {
2936 int ret;
2937
2938 if ((ret = pfctl_set_debug(pfh, level)) != 0)
2939 errc(1, ret, "DIOCSETDEBUG");
2940 if ((opts & PF_OPT_QUIET) == 0) {
2941 fprintf(stderr, "debug level set to '");
2942 switch (level) {
2943 case PF_DEBUG_NONE:
2944 fprintf(stderr, "none");
2945 break;
2946 case PF_DEBUG_URGENT:
2947 fprintf(stderr, "urgent");
2948 break;
2949 case PF_DEBUG_MISC:
2950 fprintf(stderr, "misc");
2951 break;
2952 case PF_DEBUG_NOISY:
2953 fprintf(stderr, "loud");
2954 break;
2955 default:
2956 fprintf(stderr, "<invalid>");
2957 break;
2958 }
2959 fprintf(stderr, "'\n");
2960 }
2961 }
2962
2963 int
pfctl_test_altqsupport(int dev,int opts)2964 pfctl_test_altqsupport(int dev, int opts)
2965 {
2966 struct pfioc_altq pa;
2967
2968 pa.version = PFIOC_ALTQ_VERSION;
2969 if (ioctl(dev, DIOCGETALTQS, &pa)) {
2970 if (errno == ENODEV) {
2971 if (opts & PF_OPT_VERBOSE)
2972 fprintf(stderr, "No ALTQ support in kernel\n"
2973 "ALTQ related functions disabled\n");
2974 return (0);
2975 } else
2976 err(1, "DIOCGETALTQS");
2977 }
2978 return (1);
2979 }
2980
2981 int
pfctl_walk_show(int opts,struct pfioc_ruleset * pr,void * warg)2982 pfctl_walk_show(int opts, struct pfioc_ruleset *pr, void *warg)
2983 {
2984 if (pr->path[0]) {
2985 if (pr->path[0] != '_' || (opts & PF_OPT_VERBOSE))
2986 printf(" %s/%s\n", pr->path, pr->name);
2987 } else if (pr->name[0] != '_' || (opts & PF_OPT_VERBOSE))
2988 printf(" %s\n", pr->name);
2989
2990 return (0);
2991 }
2992
2993 int
pfctl_walk_get(int opts,struct pfioc_ruleset * pr,void * warg)2994 pfctl_walk_get(int opts, struct pfioc_ruleset *pr, void *warg)
2995 {
2996 struct pfr_anchoritem *pfra;
2997 struct pfr_anchors *anchors;
2998 int e;
2999
3000 anchors = (struct pfr_anchors *)warg;
3001
3002 pfra = malloc(sizeof(*pfra));
3003 if (pfra == NULL)
3004 err(1, "%s", __func__);
3005
3006 if (pr->path[0])
3007 e = asprintf(&pfra->pfra_anchorname, "%s/%s", pr->path,
3008 pr->name);
3009 else
3010 e = asprintf(&pfra->pfra_anchorname, "%s", pr->name);
3011
3012 if (e == -1)
3013 err(1, "%s", __func__);
3014
3015 SLIST_INSERT_HEAD(anchors, pfra, pfra_sle);
3016
3017 return (0);
3018 }
3019
3020 int
pfctl_walk_anchors(int dev,int opts,const char * anchor,int (walkf)(int,struct pfioc_ruleset *,void *),void * warg)3021 pfctl_walk_anchors(int dev, int opts, const char *anchor,
3022 int(walkf)(int, struct pfioc_ruleset *, void *), void *warg)
3023 {
3024 struct pfioc_ruleset pr;
3025 u_int32_t mnr, nr;
3026 int ret;
3027
3028 memset(&pr, 0, sizeof(pr));
3029 if ((ret = pfctl_get_rulesets(pfh, anchor, &mnr)) != 0)
3030 errx(1, "%s", pf_strerror(ret));
3031 for (nr = 0; nr < mnr; ++nr) {
3032 char sub[MAXPATHLEN];
3033
3034 if ((ret = pfctl_get_ruleset(pfh, anchor, nr, &pr)) != 0)
3035 errc(1, ret, "DIOCGETRULESET");
3036 if (!strcmp(pr.name, PF_RESERVED_ANCHOR))
3037 continue;
3038 sub[0] = '\0';
3039 if (walkf(opts, &pr, warg))
3040 return (-1);
3041
3042 if (pr.path[0])
3043 snprintf(sub, sizeof(sub), "%s/%s", pr.path, pr.name);
3044 else
3045 snprintf(sub, sizeof(sub), "%s", pr.name);
3046 if (pfctl_walk_anchors(dev, opts, sub, walkf, warg))
3047 return (-1);
3048 }
3049 return (0);
3050 }
3051
3052 int
pfctl_show_anchors(int dev,int opts,char * anchor)3053 pfctl_show_anchors(int dev, int opts, char *anchor)
3054 {
3055 return (
3056 pfctl_walk_anchors(dev, opts, anchor, pfctl_walk_show, NULL));
3057 }
3058
3059 struct pfr_anchors *
pfctl_get_anchors(int dev,const char * anchor,int opts)3060 pfctl_get_anchors(int dev, const char *anchor, int opts)
3061 {
3062 struct pfioc_ruleset pr;
3063 static struct pfr_anchors anchors;
3064 char anchorbuf[PATH_MAX];
3065 char *n;
3066
3067 SLIST_INIT(&anchors);
3068
3069 memset(&pr, 0, sizeof(pr));
3070 if (*anchor != '\0') {
3071 strlcpy(anchorbuf, anchor, sizeof(anchorbuf));
3072 n = dirname(anchorbuf);
3073 if (n[0] != '.' && n[1] != '\0')
3074 strlcpy(pr.path, n, sizeof(pr.path));
3075 strlcpy(anchorbuf, anchor, sizeof(anchorbuf));
3076 n = basename(anchorbuf);
3077 if (n != NULL)
3078 strlcpy(pr.name, n, sizeof(pr.name));
3079 }
3080
3081 /* insert a root anchor first. */
3082 pfctl_walk_get(opts, &pr, &anchors);
3083
3084 if (pfctl_walk_anchors(dev, opts, anchor, pfctl_walk_get, &anchors))
3085 errx(1, "%s failed to retrieve list of anchors, can't continue",
3086 __func__);
3087
3088 return (&anchors);
3089 }
3090
3091 int
pfctl_call_cleartables(int dev,int opts,struct pfr_anchoritem * pfra)3092 pfctl_call_cleartables(int dev, int opts, struct pfr_anchoritem *pfra)
3093 {
3094 /*
3095 * PF_OPT_QUIET makes pfctl_clear_tables() to stop printing number of
3096 * tables cleared for given anchor.
3097 */
3098 opts |= PF_OPT_QUIET;
3099 return ((pfctl_do_clear_tables(pfra->pfra_anchorname, opts) == -1) ?
3100 1 : 0);
3101 }
3102
3103 int
pfctl_call_clearrules(int dev,int opts,struct pfr_anchoritem * pfra)3104 pfctl_call_clearrules(int dev, int opts, struct pfr_anchoritem *pfra)
3105 {
3106 /*
3107 * PF_OPT_QUIET makes pfctl_clear_rules() to stop printing a 'rules
3108 * cleared' message for every anchor it deletes.
3109 */
3110 opts |= PF_OPT_QUIET;
3111 return (pfctl_flush_rules(dev, opts, pfra->pfra_anchorname));
3112 }
3113
3114 int
pfctl_call_clearanchors(int dev,int opts,struct pfr_anchoritem * pfra)3115 pfctl_call_clearanchors(int dev, int opts, struct pfr_anchoritem *pfra)
3116 {
3117 int rv = 0;
3118
3119 rv |= pfctl_call_cleartables(dev, opts, pfra);
3120 rv |= pfctl_call_clearrules(dev, opts, pfra);
3121
3122 return (rv);
3123 }
3124
3125 int
pfctl_call_showtables(int dev,int opts,struct pfr_anchoritem * pfra)3126 pfctl_call_showtables(int dev, int opts, struct pfr_anchoritem *pfra)
3127 {
3128 pfctl_show_tables(pfra->pfra_anchorname, opts);
3129 return (0);
3130 }
3131
3132 int
pfctl_recurse(int dev,int opts,const char * anchorname,int (* walkf)(int,int,struct pfr_anchoritem *))3133 pfctl_recurse(int dev, int opts, const char *anchorname,
3134 int(*walkf)(int, int, struct pfr_anchoritem *))
3135 {
3136 int rv = 0;
3137 struct pfr_anchors *anchors;
3138 struct pfr_anchoritem *pfra, *pfra_save;
3139
3140 anchors = pfctl_get_anchors(dev, anchorname, opts);
3141 /*
3142 * While traversing the list, pfctl_clear_*() must always return
3143 * so that failures on one anchor do not prevent clearing others.
3144 */
3145 opts |= PF_OPT_IGNFAIL;
3146 if ((opts & PF_OPT_CALLSHOW) == 0)
3147 printf("Removing:\n");
3148 SLIST_FOREACH_SAFE(pfra, anchors, pfra_sle, pfra_save) {
3149 if ((opts & PF_OPT_CALLSHOW) == 0)
3150 printf(" %s\n",
3151 (*pfra->pfra_anchorname == '\0') ? "/" :
3152 pfra->pfra_anchorname);
3153 rv |= walkf(dev, opts, pfra);
3154 SLIST_REMOVE(anchors, pfra, pfr_anchoritem, pfra_sle);
3155 free(pfra->pfra_anchorname);
3156 free(pfra);
3157 }
3158
3159 return (rv);
3160 }
3161
3162 int
pfctl_show_eth_anchors(int dev,int opts,char * anchorname)3163 pfctl_show_eth_anchors(int dev, int opts, char *anchorname)
3164 {
3165 struct pfctl_eth_rulesets_info ri;
3166 struct pfctl_eth_ruleset_info rs;
3167 int ret;
3168
3169 if ((ret = pfctl_get_eth_rulesets_info(dev, &ri, anchorname)) != 0) {
3170 if (ret == ENOENT)
3171 fprintf(stderr, "Anchor '%s' not found.\n",
3172 anchorname);
3173 else
3174 errc(1, ret, "DIOCGETETHRULESETS");
3175 return (-1);
3176 }
3177
3178 for (int nr = 0; nr < ri.nr; nr++) {
3179 char sub[MAXPATHLEN];
3180
3181 if ((ret = pfctl_get_eth_ruleset(dev, anchorname, nr, &rs)) != 0)
3182 errc(1, ret, "DIOCGETETHRULESET");
3183
3184 if (!strcmp(rs.name, PF_RESERVED_ANCHOR))
3185 continue;
3186 sub[0] = 0;
3187 if (rs.path[0]) {
3188 strlcat(sub, rs.path, sizeof(sub));
3189 strlcat(sub, "/", sizeof(sub));
3190 }
3191 strlcat(sub, rs.name, sizeof(sub));
3192 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE))
3193 printf(" %s\n", sub);
3194 if ((opts & PF_OPT_VERBOSE) && pfctl_show_eth_anchors(dev, opts, sub))
3195 return (-1);
3196 }
3197 return (0);
3198 }
3199
3200 const char *
pfctl_lookup_option(char * cmd,const char * const * list)3201 pfctl_lookup_option(char *cmd, const char * const *list)
3202 {
3203 if (cmd != NULL && *cmd)
3204 for (; *list; list++)
3205 if (!strncmp(cmd, *list, strlen(cmd)))
3206 return (*list);
3207 return (NULL);
3208 }
3209
3210 void
pfctl_reset(int dev,int opts)3211 pfctl_reset(int dev, int opts)
3212 {
3213 struct pfctl pf;
3214 struct pfr_buffer t;
3215 int i;
3216
3217 memset(&pf, 0, sizeof(pf));
3218 pf.dev = dev;
3219 pf.h = pfh;
3220 pfctl_init_options(&pf);
3221
3222 /* Force reset upon pfctl_load_options() */
3223 pf.debug_set = 1;
3224 pf.reass_set = 1;
3225 pf.syncookieswat_set = 1;
3226 pf.ifname = strdup("none");
3227 if (pf.ifname == NULL)
3228 err(1, "%s: strdup", __func__);
3229 pf.ifname_set = 1;
3230
3231 memset(&t, 0, sizeof(t));
3232 t.pfrb_type = PFRB_TRANS;
3233 if (pfctl_trans(dev, &t, DIOCXBEGIN, 0))
3234 err(1, "%s: DIOCXBEGIN", __func__);
3235
3236 for (i = 0; pf_limits[i].name; i++)
3237 pf.limit_set[pf_limits[i].index] = 1;
3238
3239 for (i = 0; pf_timeouts[i].name; i++)
3240 pf.timeout_set[pf_timeouts[i].timeout] = 1;
3241
3242 pfctl_load_options(&pf);
3243
3244 if (pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
3245 err(1, "%s: DIOCXCOMMIT", __func__);
3246
3247 pfctl_clear_interface_flags(dev, opts);
3248 }
3249
3250 int
main(int argc,char * argv[])3251 main(int argc, char *argv[])
3252 {
3253 int ch;
3254 int mode = O_RDONLY;
3255 int opts = 0;
3256 int optimize = PF_OPTIMIZE_BASIC;
3257 char anchorname[MAXPATHLEN];
3258 char *path;
3259
3260 if (argc < 2)
3261 usage();
3262
3263 while ((ch = getopt(argc, argv,
3264 "a:AdD:eqf:F:ghi:k:K:mMnNOo:Pp:rRs:St:T:vx:z")) != -1) {
3265 switch (ch) {
3266 case 'a':
3267 anchoropt = optarg;
3268 break;
3269 case 'd':
3270 opts |= PF_OPT_DISABLE;
3271 mode = O_RDWR;
3272 break;
3273 case 'D':
3274 if (pfctl_cmdline_symset(optarg) < 0)
3275 warnx("could not parse macro definition %s",
3276 optarg);
3277 break;
3278 case 'e':
3279 opts |= PF_OPT_ENABLE;
3280 mode = O_RDWR;
3281 break;
3282 case 'q':
3283 opts |= PF_OPT_QUIET;
3284 break;
3285 case 'F':
3286 clearopt = pfctl_lookup_option(optarg, clearopt_list);
3287 if (clearopt == NULL) {
3288 warnx("Unknown flush modifier '%s'", optarg);
3289 usage();
3290 }
3291 mode = O_RDWR;
3292 break;
3293 case 'i':
3294 ifaceopt = optarg;
3295 break;
3296 case 'k':
3297 if (state_killers >= 2) {
3298 warnx("can only specify -k twice");
3299 usage();
3300 /* NOTREACHED */
3301 }
3302 state_kill[state_killers++] = optarg;
3303 mode = O_RDWR;
3304 break;
3305 case 'K':
3306 if (src_node_killers >= 2) {
3307 warnx("can only specify -K twice");
3308 usage();
3309 /* NOTREACHED */
3310 }
3311 src_node_kill[src_node_killers++] = optarg;
3312 mode = O_RDWR;
3313 break;
3314 case 'm':
3315 opts |= PF_OPT_MERGE;
3316 break;
3317 case 'M':
3318 opts |= PF_OPT_KILLMATCH;
3319 break;
3320 case 'n':
3321 opts |= PF_OPT_NOACTION;
3322 break;
3323 case 'N':
3324 loadopt |= PFCTL_FLAG_NAT;
3325 break;
3326 case 'r':
3327 opts |= PF_OPT_USEDNS;
3328 break;
3329 case 'f':
3330 rulesopt = optarg;
3331 mode = O_RDWR;
3332 break;
3333 case 'g':
3334 opts |= PF_OPT_DEBUG;
3335 break;
3336 case 'A':
3337 loadopt |= PFCTL_FLAG_ALTQ;
3338 break;
3339 case 'R':
3340 loadopt |= PFCTL_FLAG_FILTER;
3341 break;
3342 case 'o':
3343 optiopt = pfctl_lookup_option(optarg, optiopt_list);
3344 if (optiopt == NULL) {
3345 warnx("Unknown optimization '%s'", optarg);
3346 usage();
3347 }
3348 opts |= PF_OPT_OPTIMIZE;
3349 break;
3350 case 'O':
3351 loadopt |= PFCTL_FLAG_OPTION;
3352 break;
3353 case 'p':
3354 pf_device = optarg;
3355 break;
3356 case 'P':
3357 opts |= PF_OPT_NUMERIC;
3358 break;
3359 case 's':
3360 showopt = pfctl_lookup_option(optarg, showopt_list);
3361 if (showopt == NULL) {
3362 warnx("Unknown show modifier '%s'", optarg);
3363 usage();
3364 }
3365 break;
3366 case 'S':
3367 opts |= PF_OPT_NODNS;
3368 break;
3369 case 't':
3370 tableopt = optarg;
3371 break;
3372 case 'T':
3373 tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
3374 if (tblcmdopt == NULL) {
3375 warnx("Unknown table command '%s'", optarg);
3376 usage();
3377 }
3378 break;
3379 case 'v':
3380 if (opts & PF_OPT_VERBOSE)
3381 opts |= PF_OPT_VERBOSE2;
3382 opts |= PF_OPT_VERBOSE;
3383 break;
3384 case 'x':
3385 debugopt = pfctl_lookup_option(optarg, debugopt_list);
3386 if (debugopt == NULL) {
3387 warnx("Unknown debug level '%s'", optarg);
3388 usage();
3389 }
3390 mode = O_RDWR;
3391 break;
3392 case 'z':
3393 opts |= PF_OPT_CLRRULECTRS;
3394 mode = O_RDWR;
3395 break;
3396 case 'h':
3397 /* FALLTHROUGH */
3398 default:
3399 usage();
3400 /* NOTREACHED */
3401 }
3402 }
3403
3404 if ((opts & PF_OPT_NODNS) && (opts & PF_OPT_USEDNS))
3405 errx(1, "-N and -r are mutually exclusive");
3406
3407 if ((tblcmdopt == NULL) ^ (tableopt == NULL))
3408 usage();
3409
3410 if (tblcmdopt != NULL) {
3411 argc -= optind;
3412 argv += optind;
3413 ch = *tblcmdopt;
3414 if (ch == 'l') {
3415 loadopt |= PFCTL_FLAG_TABLE;
3416 tblcmdopt = NULL;
3417 } else
3418 mode = strchr("st", ch) ? O_RDONLY : O_RDWR;
3419 } else if (argc != optind) {
3420 warnx("unknown command line argument: %s ...", argv[optind]);
3421 usage();
3422 /* NOTREACHED */
3423 }
3424 if (loadopt == 0)
3425 loadopt = ~0;
3426
3427 memset(anchorname, 0, sizeof(anchorname));
3428 if (anchoropt != NULL) {
3429 int len = strlen(anchoropt);
3430
3431 if (anchoropt[0] == '\0')
3432 errx(1, "anchor name must not be empty");
3433 if (mode == O_RDONLY && showopt == NULL && tblcmdopt == NULL) {
3434 warnx("anchors apply to -f, -F, -s, and -T only");
3435 usage();
3436 }
3437 if (mode == O_RDWR && tblcmdopt == NULL &&
3438 (anchoropt[0] == '_' || strstr(anchoropt, "/_") != NULL))
3439 errx(1, "anchor names beginning with '_' cannot "
3440 "be modified from the command line");
3441
3442 if (len >= 1 && anchoropt[len - 1] == '*') {
3443 if (len >= 2 && anchoropt[len - 2] == '/')
3444 anchoropt[len - 2] = '\0';
3445 else
3446 anchoropt[len - 1] = '\0';
3447 opts |= PF_OPT_RECURSE;
3448 }
3449 if (strlcpy(anchorname, anchoropt,
3450 sizeof(anchorname)) >= sizeof(anchorname))
3451 errx(1, "anchor name '%s' too long",
3452 anchoropt);
3453 loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE|PFCTL_FLAG_ETH;
3454 }
3455
3456 if ((opts & PF_OPT_NOACTION) == 0) {
3457 dev = open(pf_device, mode);
3458 if (dev == -1)
3459 err(1, "%s", pf_device);
3460 altqsupport = pfctl_test_altqsupport(dev, opts);
3461 } else {
3462 dev = open(pf_device, O_RDONLY);
3463 if (dev >= 0)
3464 opts |= PF_OPT_DUMMYACTION;
3465 /* turn off options */
3466 opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
3467 clearopt = showopt = debugopt = NULL;
3468 #if !defined(ENABLE_ALTQ)
3469 altqsupport = 0;
3470 #else
3471 altqsupport = 1;
3472 #endif
3473 }
3474 pfh = pfctl_open(pf_device);
3475 if (pfh == NULL)
3476 err(1, "Failed to open netlink");
3477
3478 if ((opts & PF_OPT_NOACTION) == 0) {
3479 pfctl_read_limits(pfh);
3480 atexit(pfctl_restore_limits);
3481 }
3482
3483 if (opts & PF_OPT_DISABLE)
3484 if (pfctl_disable(dev, opts))
3485 exit_val = 1;
3486
3487 if ((path = calloc(1, MAXPATHLEN)) == NULL)
3488 errx(1, "%s: calloc", __func__);
3489
3490 if (showopt != NULL) {
3491 switch (*showopt) {
3492 case 'A':
3493 pfctl_show_anchors(dev, opts, anchorname);
3494 if (opts & PF_OPT_VERBOSE2)
3495 printf("Ethernet:\n");
3496 pfctl_show_eth_anchors(dev, opts, anchorname);
3497 break;
3498 case 'r':
3499 pfctl_load_fingerprints(dev, opts);
3500 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
3501 anchorname, 0, 0);
3502 break;
3503 case 'l':
3504 pfctl_load_fingerprints(dev, opts);
3505 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
3506 anchorname, 0, 0);
3507 break;
3508 case 'n':
3509 pfctl_load_fingerprints(dev, opts);
3510 pfctl_show_nat(dev, path, opts, anchorname, 0, 0);
3511 break;
3512 case 'q':
3513 pfctl_show_altq(dev, ifaceopt, opts,
3514 opts & PF_OPT_VERBOSE2);
3515 break;
3516 case 's':
3517 pfctl_show_states(dev, ifaceopt, opts);
3518 break;
3519 case 'S':
3520 pfctl_show_src_nodes(dev, opts);
3521 break;
3522 case 'i':
3523 pfctl_show_status(dev, opts);
3524 break;
3525 case 'R':
3526 exit_val = pfctl_show_running(dev);
3527 break;
3528 case 't':
3529 pfctl_show_timeouts(dev, opts);
3530 break;
3531 case 'm':
3532 pfctl_show_limits(dev, opts);
3533 break;
3534 case 'e':
3535 pfctl_show_eth_rules(dev, path, opts, 0, anchorname, 0,
3536 0);
3537 break;
3538 case 'a':
3539 opts |= PF_OPT_SHOWALL;
3540 pfctl_load_fingerprints(dev, opts);
3541
3542 pfctl_show_eth_rules(dev, path, opts, 0, anchorname, 0,
3543 0);
3544
3545 pfctl_show_nat(dev, path, opts, anchorname, 0, 0);
3546 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
3547 anchorname, 0, 0);
3548 pfctl_show_altq(dev, ifaceopt, opts, 0);
3549 pfctl_show_states(dev, ifaceopt, opts);
3550 pfctl_show_src_nodes(dev, opts);
3551 pfctl_show_status(dev, opts);
3552 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
3553 anchorname, 0, 0);
3554 pfctl_show_timeouts(dev, opts);
3555 pfctl_show_limits(dev, opts);
3556 pfctl_show_tables(anchorname, opts);
3557 pfctl_show_fingerprints(opts);
3558 break;
3559 case 'T':
3560 if (opts & PF_OPT_RECURSE) {
3561 opts |= PF_OPT_CALLSHOW;
3562 pfctl_recurse(dev, opts, anchorname,
3563 pfctl_call_showtables);
3564 } else
3565 pfctl_show_tables(anchorname, opts);
3566 break;
3567 case 'o':
3568 pfctl_load_fingerprints(dev, opts);
3569 pfctl_show_fingerprints(opts);
3570 break;
3571 case 'I':
3572 pfctl_show_ifaces(ifaceopt, opts);
3573 break;
3574 case 'c':
3575 pfctl_show_creators(opts);
3576 break;
3577 }
3578 }
3579
3580 if ((opts & PF_OPT_CLRRULECTRS) && showopt == NULL) {
3581 pfctl_show_eth_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
3582 anchorname, 0, 0);
3583 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
3584 anchorname, 0, 0);
3585 }
3586
3587 if (clearopt != NULL) {
3588 int mnr;
3589
3590 /* Check if anchor exists. */
3591 if ((pfctl_get_rulesets(pfh, anchorname, &mnr)) == ENOENT)
3592 errx(1, "No such anchor %s", anchorname);
3593
3594 switch (*clearopt) {
3595 case 'e':
3596 pfctl_flush_eth_rules(dev, opts, anchorname);
3597 break;
3598 case 'r':
3599 if (opts & PF_OPT_RECURSE)
3600 pfctl_recurse(dev, opts, anchorname,
3601 pfctl_call_clearrules);
3602 else
3603 pfctl_flush_rules(dev, opts, anchorname);
3604 break;
3605 case 'n':
3606 pfctl_flush_nat(dev, opts, anchorname);
3607 break;
3608 case 'q':
3609 pfctl_clear_altq(dev, opts);
3610 break;
3611 case 's':
3612 pfctl_clear_iface_states(dev, ifaceopt, opts);
3613 break;
3614 case 'S':
3615 pfctl_clear_src_nodes(dev, opts);
3616 break;
3617 case 'i':
3618 pfctl_clear_stats(pfh, opts);
3619 break;
3620 case 'a':
3621 if (ifaceopt) {
3622 warnx("don't specify an interface with -Fall");
3623 usage();
3624 /* NOTREACHED */
3625 }
3626 pfctl_flush_eth_rules(dev, opts, anchorname);
3627 pfctl_flush_rules(dev, opts, anchorname);
3628 pfctl_flush_nat(dev, opts, anchorname);
3629 if (opts & PF_OPT_RECURSE)
3630 pfctl_recurse(dev, opts, anchorname,
3631 pfctl_call_clearanchors);
3632 else {
3633 pfctl_do_clear_tables(anchorname, opts);
3634 pfctl_flush_rules(dev, opts, anchorname);
3635 }
3636 if (!*anchorname) {
3637 pfctl_clear_altq(dev, opts);
3638 pfctl_clear_iface_states(dev, ifaceopt, opts);
3639 pfctl_clear_src_nodes(dev, opts);
3640 pfctl_clear_stats(pfh, opts);
3641 pfctl_clear_fingerprints(dev, opts);
3642 pfctl_reset(dev, opts);
3643 }
3644 break;
3645 case 'o':
3646 pfctl_clear_fingerprints(dev, opts);
3647 break;
3648 case 'T':
3649 if ((opts & PF_OPT_RECURSE) == 0)
3650 pfctl_do_clear_tables(anchorname, opts);
3651 else
3652 pfctl_recurse(dev, opts, anchorname,
3653 pfctl_call_cleartables);
3654 break;
3655 case 'R':
3656 pfctl_reset(dev, opts);
3657 break;
3658 }
3659 }
3660 if (state_killers) {
3661 if (!strcmp(state_kill[0], "label"))
3662 pfctl_label_kill_states(dev, ifaceopt, opts);
3663 else if (!strcmp(state_kill[0], "id"))
3664 pfctl_id_kill_states(dev, ifaceopt, opts);
3665 else if (!strcmp(state_kill[0], "gateway"))
3666 pfctl_gateway_kill_states(dev, ifaceopt, opts);
3667 else if (!strcmp(state_kill[0], "key"))
3668 pfctl_key_kill_states(dev, ifaceopt, opts);
3669 else
3670 pfctl_net_kill_states(dev, ifaceopt, opts);
3671 }
3672
3673 if (src_node_killers)
3674 pfctl_kill_src_nodes(dev, opts);
3675
3676 if (tblcmdopt != NULL) {
3677 exit_val = pfctl_table(argc, argv, tableopt,
3678 tblcmdopt, rulesopt, anchorname, opts);
3679 rulesopt = NULL;
3680 }
3681 if (optiopt != NULL) {
3682 switch (*optiopt) {
3683 case 'n':
3684 optimize = 0;
3685 break;
3686 case 'b':
3687 optimize |= PF_OPTIMIZE_BASIC;
3688 break;
3689 case 'o':
3690 case 'p':
3691 optimize |= PF_OPTIMIZE_PROFILE;
3692 break;
3693 }
3694 }
3695
3696 if ((rulesopt != NULL) && (loadopt & PFCTL_FLAG_OPTION) &&
3697 !anchorname[0] && !(opts & PF_OPT_NOACTION))
3698 pfctl_get_skip_ifaces();
3699
3700 if (rulesopt != NULL && !(opts & PF_OPT_MERGE) &&
3701 !anchorname[0] && (loadopt & PFCTL_FLAG_OPTION))
3702 if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
3703 exit_val = 1;
3704
3705 if (rulesopt != NULL) {
3706 if (pfctl_rules(dev, rulesopt, opts, optimize,
3707 anchorname, NULL))
3708 exit_val = 1;
3709 }
3710
3711 if (opts & PF_OPT_ENABLE)
3712 if (pfctl_enable(dev, opts))
3713 exit_val = 1;
3714
3715 if (debugopt != NULL) {
3716 switch (*debugopt) {
3717 case 'n':
3718 pfctl_debug(dev, PF_DEBUG_NONE, opts);
3719 break;
3720 case 'u':
3721 pfctl_debug(dev, PF_DEBUG_URGENT, opts);
3722 break;
3723 case 'm':
3724 pfctl_debug(dev, PF_DEBUG_MISC, opts);
3725 break;
3726 case 'l':
3727 pfctl_debug(dev, PF_DEBUG_NOISY, opts);
3728 break;
3729 }
3730 }
3731
3732 /*
3733 * prevent pfctl_restore_limits() exit handler from restoring
3734 * pf(4) options settings on successful exit.
3735 */
3736 if (exit_val == 0) {
3737 close(dev);
3738 dev = -1;
3739 pfctl_close(pfh);
3740 pfh = NULL;
3741 }
3742
3743 return (exit_val);
3744 }
3745
3746 char *
pf_strerror(int errnum)3747 pf_strerror(int errnum)
3748 {
3749 switch (errnum) {
3750 case ESRCH:
3751 return "Table does not exist.";
3752 case EINVAL:
3753 case ENOENT:
3754 return "Anchor does not exist.";
3755 default:
3756 return strerror(errnum);
3757 }
3758 }
3759