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 static int restore_limit_handler_armed = 0;
2622
2623 if (pfctl_set_limit(pf->h, index, limit)) {
2624 if (errno == EBUSY)
2625 warnx("Current pool size exceeds requested %s limit %u",
2626 pf_limits[index].name, limit);
2627 else
2628 warnx("Cannot set %s limit to %u",
2629 pf_limits[index].name, limit);
2630 return (1);
2631 } else if (restore_limit_handler_armed == 0) {
2632 atexit(pfctl_restore_limits);
2633 restore_limit_handler_armed = 1;
2634 }
2635 return (0);
2636 }
2637
2638 int
pfctl_apply_timeout(struct pfctl * pf,const char * opt,int seconds,int quiet)2639 pfctl_apply_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
2640 {
2641 int i;
2642
2643 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2644 return (0);
2645
2646 for (i = 0; pf_timeouts[i].name; i++) {
2647 if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
2648 pf->timeout[pf_timeouts[i].timeout] = seconds;
2649 pf->timeout_set[pf_timeouts[i].timeout] = 1;
2650 break;
2651 }
2652 }
2653
2654 if (pf_timeouts[i].name == NULL) {
2655 warnx("Bad timeout name.");
2656 return (1);
2657 }
2658
2659
2660 if (pf->opts & PF_OPT_VERBOSE && ! quiet)
2661 printf("set timeout %s %d\n", opt, seconds);
2662
2663 return (0);
2664 }
2665
2666 int
pfctl_load_timeout(struct pfctl * pf,unsigned int timeout,unsigned int seconds)2667 pfctl_load_timeout(struct pfctl *pf, unsigned int timeout, unsigned int seconds)
2668 {
2669 if (pfctl_set_timeout(pf->h, timeout, seconds)) {
2670 warnx("DIOCSETTIMEOUT");
2671 return (1);
2672 }
2673 return (0);
2674 }
2675
2676 int
pfctl_set_reassembly(struct pfctl * pf,int on,int nodf)2677 pfctl_set_reassembly(struct pfctl *pf, int on, int nodf)
2678 {
2679 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2680 return (0);
2681
2682 pf->reass_set = 1;
2683 if (on) {
2684 pf->reassemble = PF_REASS_ENABLED;
2685 if (nodf)
2686 pf->reassemble |= PF_REASS_NODF;
2687 } else {
2688 pf->reassemble = 0;
2689 }
2690
2691 if (pf->opts & PF_OPT_VERBOSE)
2692 printf("set reassemble %s %s\n", on ? "yes" : "no",
2693 nodf ? "no-df" : "");
2694
2695 return (0);
2696 }
2697
2698 int
pfctl_set_optimization(struct pfctl * pf,const char * opt)2699 pfctl_set_optimization(struct pfctl *pf, const char *opt)
2700 {
2701 const struct pf_hint *hint;
2702 int i, r;
2703
2704 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2705 return (0);
2706
2707 for (i = 0; pf_hints[i].name; i++)
2708 if (strcasecmp(opt, pf_hints[i].name) == 0)
2709 break;
2710
2711 hint = pf_hints[i].hint;
2712 if (hint == NULL) {
2713 warnx("invalid state timeouts optimization");
2714 return (1);
2715 }
2716
2717 for (i = 0; hint[i].name; i++)
2718 if ((r = pfctl_apply_timeout(pf, hint[i].name,
2719 hint[i].timeout, 1)))
2720 return (r);
2721
2722 if (pf->opts & PF_OPT_VERBOSE)
2723 printf("set optimization %s\n", opt);
2724
2725 return (0);
2726 }
2727
2728 int
pfctl_set_logif(struct pfctl * pf,char * ifname)2729 pfctl_set_logif(struct pfctl *pf, char *ifname)
2730 {
2731
2732 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2733 return (0);
2734
2735 if (!strcmp(ifname, "none")) {
2736 free(pf->ifname);
2737 pf->ifname = NULL;
2738 } else {
2739 pf->ifname = strdup(ifname);
2740 if (!pf->ifname)
2741 errx(1, "pfctl_set_logif: strdup");
2742 }
2743 pf->ifname_set = 1;
2744
2745 if (pf->opts & PF_OPT_VERBOSE)
2746 printf("set loginterface %s\n", ifname);
2747
2748 return (0);
2749 }
2750
2751 int
pfctl_load_logif(struct pfctl * pf,char * ifname)2752 pfctl_load_logif(struct pfctl *pf, char *ifname)
2753 {
2754 if (ifname != NULL && strlen(ifname) >= IFNAMSIZ) {
2755 warnx("pfctl_load_logif: strlcpy");
2756 return (1);
2757 }
2758 return (pfctl_set_statusif(pfh, ifname ? ifname : ""));
2759 }
2760
2761 void
pfctl_set_hostid(struct pfctl * pf,u_int32_t hostid)2762 pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
2763 {
2764 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2765 return;
2766
2767 HTONL(hostid);
2768
2769 pf->hostid = hostid;
2770 pf->hostid_set = 1;
2771
2772 if (pf->opts & PF_OPT_VERBOSE)
2773 printf("set hostid 0x%08x\n", ntohl(hostid));
2774 }
2775
2776 int
pfctl_load_hostid(struct pfctl * pf,u_int32_t hostid)2777 pfctl_load_hostid(struct pfctl *pf, u_int32_t hostid)
2778 {
2779 if (ioctl(dev, DIOCSETHOSTID, &hostid)) {
2780 warnx("DIOCSETHOSTID");
2781 return (1);
2782 }
2783 return (0);
2784 }
2785
2786 int
pfctl_load_reassembly(struct pfctl * pf,u_int32_t reassembly)2787 pfctl_load_reassembly(struct pfctl *pf, u_int32_t reassembly)
2788 {
2789 if (ioctl(dev, DIOCSETREASS, &reassembly)) {
2790 warnx("DIOCSETREASS");
2791 return (1);
2792 }
2793 return (0);
2794 }
2795
2796 int
pfctl_load_syncookies(struct pfctl * pf,u_int8_t val)2797 pfctl_load_syncookies(struct pfctl *pf, u_int8_t val)
2798 {
2799 struct pfctl_syncookies cookies;
2800
2801 bzero(&cookies, sizeof(cookies));
2802
2803 cookies.mode = val;
2804 cookies.lowwater = pf->syncookieswat[0];
2805 cookies.highwater = pf->syncookieswat[1];
2806
2807 if (pfctl_set_syncookies(dev, &cookies)) {
2808 warnx("DIOCSETSYNCOOKIES");
2809 return (1);
2810 }
2811 return (0);
2812 }
2813
2814 int
pfctl_cfg_syncookies(struct pfctl * pf,uint8_t val,struct pfctl_watermarks * w)2815 pfctl_cfg_syncookies(struct pfctl *pf, uint8_t val, struct pfctl_watermarks *w)
2816 {
2817 if (val != PF_SYNCOOKIES_ADAPTIVE && w != NULL) {
2818 warnx("syncookies start/end only apply to adaptive");
2819 return (1);
2820 }
2821 if (val == PF_SYNCOOKIES_ADAPTIVE && w != NULL) {
2822 if (!w->hi)
2823 w->hi = PF_SYNCOOKIES_HIWATPCT;
2824 if (!w->lo)
2825 w->lo = w->hi / 2;
2826 if (w->lo >= w->hi) {
2827 warnx("start must be higher than end");
2828 return (1);
2829 }
2830 pf->syncookieswat[0] = w->lo;
2831 pf->syncookieswat[1] = w->hi;
2832 pf->syncookieswat_set = 1;
2833 }
2834
2835 if (pf->opts & PF_OPT_VERBOSE) {
2836 if (val == PF_SYNCOOKIES_NEVER)
2837 printf("set syncookies never\n");
2838 else if (val == PF_SYNCOOKIES_ALWAYS)
2839 printf("set syncookies always\n");
2840 else if (val == PF_SYNCOOKIES_ADAPTIVE) {
2841 if (pf->syncookieswat_set)
2842 printf("set syncookies adaptive (start %u%%, "
2843 "end %u%%)\n", pf->syncookieswat[1],
2844 pf->syncookieswat[0]);
2845 else
2846 printf("set syncookies adaptive\n");
2847 } else { /* cannot happen */
2848 warnx("king bula ate all syncookies");
2849 return (1);
2850 }
2851 }
2852
2853 pf->syncookies = val;
2854 return (0);
2855 }
2856
2857 int
pfctl_do_set_debug(struct pfctl * pf,char * d)2858 pfctl_do_set_debug(struct pfctl *pf, char *d)
2859 {
2860 u_int32_t level;
2861 int ret;
2862
2863 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2864 return (0);
2865
2866 if (!strcmp(d, "none"))
2867 pf->debug = PF_DEBUG_NONE;
2868 else if (!strcmp(d, "urgent"))
2869 pf->debug = PF_DEBUG_URGENT;
2870 else if (!strcmp(d, "misc"))
2871 pf->debug = PF_DEBUG_MISC;
2872 else if (!strcmp(d, "loud"))
2873 pf->debug = PF_DEBUG_NOISY;
2874 else {
2875 warnx("unknown debug level \"%s\"", d);
2876 return (-1);
2877 }
2878
2879 pf->debug_set = 1;
2880 level = pf->debug;
2881
2882 if ((pf->opts & PF_OPT_NOACTION) == 0)
2883 if ((ret = pfctl_set_debug(pfh, level)) != 0)
2884 errc(1, ret, "DIOCSETDEBUG");
2885
2886 if (pf->opts & PF_OPT_VERBOSE)
2887 printf("set debug %s\n", d);
2888
2889 return (0);
2890 }
2891
2892 int
pfctl_load_debug(struct pfctl * pf,unsigned int level)2893 pfctl_load_debug(struct pfctl *pf, unsigned int level)
2894 {
2895 if (pfctl_set_debug(pf->h, level)) {
2896 warnx("DIOCSETDEBUG");
2897 return (1);
2898 }
2899 return (0);
2900 }
2901
2902 int
pfctl_set_interface_flags(struct pfctl * pf,char * ifname,int flags,int how)2903 pfctl_set_interface_flags(struct pfctl *pf, char *ifname, int flags, int how)
2904 {
2905 struct pfioc_iface pi;
2906 struct node_host *h = NULL, *n = NULL;
2907
2908 if ((loadopt & PFCTL_FLAG_OPTION) == 0)
2909 return (0);
2910
2911 bzero(&pi, sizeof(pi));
2912
2913 pi.pfiio_flags = flags;
2914
2915 /* Make sure our cache matches the kernel. If we set or clear the flag
2916 * for a group this applies to all members. */
2917 h = ifa_grouplookup(ifname, 0);
2918 for (n = h; n != NULL; n = n->next)
2919 pfctl_set_interface_flags(pf, n->ifname, flags, how);
2920
2921 if (strlcpy(pi.pfiio_name, ifname, sizeof(pi.pfiio_name)) >=
2922 sizeof(pi.pfiio_name))
2923 errx(1, "pfctl_set_interface_flags: strlcpy");
2924
2925 if ((pf->opts & PF_OPT_NOACTION) == 0) {
2926 if (how == 0) {
2927 if (ioctl(pf->dev, DIOCCLRIFFLAG, &pi))
2928 pfctl_err(pf->opts, 1, "DIOCCLRIFFLAG");
2929 } else {
2930 if (ioctl(pf->dev, DIOCSETIFFLAG, &pi))
2931 err(1, "DIOCSETIFFLAG");
2932 pfctl_check_skip_ifaces(ifname);
2933 }
2934 }
2935 return (0);
2936 }
2937
2938 void
pfctl_debug(int dev,u_int32_t level,int opts)2939 pfctl_debug(int dev, u_int32_t level, int opts)
2940 {
2941 int ret;
2942
2943 if ((ret = pfctl_set_debug(pfh, level)) != 0)
2944 errc(1, ret, "DIOCSETDEBUG");
2945 if ((opts & PF_OPT_QUIET) == 0) {
2946 fprintf(stderr, "debug level set to '");
2947 switch (level) {
2948 case PF_DEBUG_NONE:
2949 fprintf(stderr, "none");
2950 break;
2951 case PF_DEBUG_URGENT:
2952 fprintf(stderr, "urgent");
2953 break;
2954 case PF_DEBUG_MISC:
2955 fprintf(stderr, "misc");
2956 break;
2957 case PF_DEBUG_NOISY:
2958 fprintf(stderr, "loud");
2959 break;
2960 default:
2961 fprintf(stderr, "<invalid>");
2962 break;
2963 }
2964 fprintf(stderr, "'\n");
2965 }
2966 }
2967
2968 int
pfctl_test_altqsupport(int dev,int opts)2969 pfctl_test_altqsupport(int dev, int opts)
2970 {
2971 struct pfioc_altq pa;
2972
2973 pa.version = PFIOC_ALTQ_VERSION;
2974 if (ioctl(dev, DIOCGETALTQS, &pa)) {
2975 if (errno == ENODEV) {
2976 if (opts & PF_OPT_VERBOSE)
2977 fprintf(stderr, "No ALTQ support in kernel\n"
2978 "ALTQ related functions disabled\n");
2979 return (0);
2980 } else
2981 err(1, "DIOCGETALTQS");
2982 }
2983 return (1);
2984 }
2985
2986 int
pfctl_walk_show(int opts,struct pfioc_ruleset * pr,void * warg)2987 pfctl_walk_show(int opts, struct pfioc_ruleset *pr, void *warg)
2988 {
2989 if (pr->path[0]) {
2990 if (pr->path[0] != '_' || (opts & PF_OPT_VERBOSE))
2991 printf(" %s/%s\n", pr->path, pr->name);
2992 } else if (pr->name[0] != '_' || (opts & PF_OPT_VERBOSE))
2993 printf(" %s\n", pr->name);
2994
2995 return (0);
2996 }
2997
2998 int
pfctl_walk_get(int opts,struct pfioc_ruleset * pr,void * warg)2999 pfctl_walk_get(int opts, struct pfioc_ruleset *pr, void *warg)
3000 {
3001 struct pfr_anchoritem *pfra;
3002 struct pfr_anchors *anchors;
3003 int e;
3004
3005 anchors = (struct pfr_anchors *)warg;
3006
3007 pfra = malloc(sizeof(*pfra));
3008 if (pfra == NULL)
3009 err(1, "%s", __func__);
3010
3011 if (pr->path[0])
3012 e = asprintf(&pfra->pfra_anchorname, "%s/%s", pr->path,
3013 pr->name);
3014 else
3015 e = asprintf(&pfra->pfra_anchorname, "%s", pr->name);
3016
3017 if (e == -1)
3018 err(1, "%s", __func__);
3019
3020 SLIST_INSERT_HEAD(anchors, pfra, pfra_sle);
3021
3022 return (0);
3023 }
3024
3025 int
pfctl_walk_anchors(int dev,int opts,const char * anchor,int (walkf)(int,struct pfioc_ruleset *,void *),void * warg)3026 pfctl_walk_anchors(int dev, int opts, const char *anchor,
3027 int(walkf)(int, struct pfioc_ruleset *, void *), void *warg)
3028 {
3029 struct pfioc_ruleset pr;
3030 u_int32_t mnr, nr;
3031 int ret;
3032
3033 memset(&pr, 0, sizeof(pr));
3034 if ((ret = pfctl_get_rulesets(pfh, anchor, &mnr)) != 0)
3035 errx(1, "%s", pf_strerror(ret));
3036 for (nr = 0; nr < mnr; ++nr) {
3037 char sub[MAXPATHLEN];
3038
3039 if ((ret = pfctl_get_ruleset(pfh, anchor, nr, &pr)) != 0)
3040 errc(1, ret, "DIOCGETRULESET");
3041 if (!strcmp(pr.name, PF_RESERVED_ANCHOR))
3042 continue;
3043 sub[0] = '\0';
3044 if (walkf(opts, &pr, warg))
3045 return (-1);
3046
3047 if (pr.path[0])
3048 snprintf(sub, sizeof(sub), "%s/%s", pr.path, pr.name);
3049 else
3050 snprintf(sub, sizeof(sub), "%s", pr.name);
3051 if (pfctl_walk_anchors(dev, opts, sub, walkf, warg))
3052 return (-1);
3053 }
3054 return (0);
3055 }
3056
3057 int
pfctl_show_anchors(int dev,int opts,char * anchor)3058 pfctl_show_anchors(int dev, int opts, char *anchor)
3059 {
3060 return (
3061 pfctl_walk_anchors(dev, opts, anchor, pfctl_walk_show, NULL));
3062 }
3063
3064 struct pfr_anchors *
pfctl_get_anchors(int dev,const char * anchor,int opts)3065 pfctl_get_anchors(int dev, const char *anchor, int opts)
3066 {
3067 struct pfioc_ruleset pr;
3068 static struct pfr_anchors anchors;
3069 char anchorbuf[PATH_MAX];
3070 char *n;
3071
3072 SLIST_INIT(&anchors);
3073
3074 memset(&pr, 0, sizeof(pr));
3075 if (*anchor != '\0') {
3076 strlcpy(anchorbuf, anchor, sizeof(anchorbuf));
3077 n = dirname(anchorbuf);
3078 if (n[0] != '.' && n[1] != '\0')
3079 strlcpy(pr.path, n, sizeof(pr.path));
3080 strlcpy(anchorbuf, anchor, sizeof(anchorbuf));
3081 n = basename(anchorbuf);
3082 if (n != NULL)
3083 strlcpy(pr.name, n, sizeof(pr.name));
3084 }
3085
3086 /* insert a root anchor first. */
3087 pfctl_walk_get(opts, &pr, &anchors);
3088
3089 if (pfctl_walk_anchors(dev, opts, anchor, pfctl_walk_get, &anchors))
3090 errx(1, "%s failed to retrieve list of anchors, can't continue",
3091 __func__);
3092
3093 return (&anchors);
3094 }
3095
3096 int
pfctl_call_cleartables(int dev,int opts,struct pfr_anchoritem * pfra)3097 pfctl_call_cleartables(int dev, int opts, struct pfr_anchoritem *pfra)
3098 {
3099 /*
3100 * PF_OPT_QUIET makes pfctl_clear_tables() to stop printing number of
3101 * tables cleared for given anchor.
3102 */
3103 opts |= PF_OPT_QUIET;
3104 return ((pfctl_do_clear_tables(pfra->pfra_anchorname, opts) == -1) ?
3105 1 : 0);
3106 }
3107
3108 int
pfctl_call_clearrules(int dev,int opts,struct pfr_anchoritem * pfra)3109 pfctl_call_clearrules(int dev, int opts, struct pfr_anchoritem *pfra)
3110 {
3111 /*
3112 * PF_OPT_QUIET makes pfctl_clear_rules() to stop printing a 'rules
3113 * cleared' message for every anchor it deletes.
3114 */
3115 opts |= PF_OPT_QUIET;
3116 return (pfctl_flush_rules(dev, opts, pfra->pfra_anchorname));
3117 }
3118
3119 int
pfctl_call_clearanchors(int dev,int opts,struct pfr_anchoritem * pfra)3120 pfctl_call_clearanchors(int dev, int opts, struct pfr_anchoritem *pfra)
3121 {
3122 int rv = 0;
3123
3124 rv |= pfctl_call_cleartables(dev, opts, pfra);
3125 rv |= pfctl_call_clearrules(dev, opts, pfra);
3126
3127 return (rv);
3128 }
3129
3130 int
pfctl_call_showtables(int dev,int opts,struct pfr_anchoritem * pfra)3131 pfctl_call_showtables(int dev, int opts, struct pfr_anchoritem *pfra)
3132 {
3133 pfctl_show_tables(pfra->pfra_anchorname, opts);
3134 return (0);
3135 }
3136
3137 int
pfctl_recurse(int dev,int opts,const char * anchorname,int (* walkf)(int,int,struct pfr_anchoritem *))3138 pfctl_recurse(int dev, int opts, const char *anchorname,
3139 int(*walkf)(int, int, struct pfr_anchoritem *))
3140 {
3141 int rv = 0;
3142 struct pfr_anchors *anchors;
3143 struct pfr_anchoritem *pfra, *pfra_save;
3144
3145 anchors = pfctl_get_anchors(dev, anchorname, opts);
3146 /*
3147 * While traversing the list, pfctl_clear_*() must always return
3148 * so that failures on one anchor do not prevent clearing others.
3149 */
3150 opts |= PF_OPT_IGNFAIL;
3151 if ((opts & PF_OPT_CALLSHOW) == 0)
3152 printf("Removing:\n");
3153 SLIST_FOREACH_SAFE(pfra, anchors, pfra_sle, pfra_save) {
3154 if ((opts & PF_OPT_CALLSHOW) == 0)
3155 printf(" %s\n",
3156 (*pfra->pfra_anchorname == '\0') ? "/" :
3157 pfra->pfra_anchorname);
3158 rv |= walkf(dev, opts, pfra);
3159 SLIST_REMOVE(anchors, pfra, pfr_anchoritem, pfra_sle);
3160 free(pfra->pfra_anchorname);
3161 free(pfra);
3162 }
3163
3164 return (rv);
3165 }
3166
3167 int
pfctl_show_eth_anchors(int dev,int opts,char * anchorname)3168 pfctl_show_eth_anchors(int dev, int opts, char *anchorname)
3169 {
3170 struct pfctl_eth_rulesets_info ri;
3171 struct pfctl_eth_ruleset_info rs;
3172 int ret;
3173
3174 if ((ret = pfctl_get_eth_rulesets_info(dev, &ri, anchorname)) != 0) {
3175 if (ret != ENOENT)
3176 errc(1, ret, "DIOCGETETHRULESETS");
3177 return (-1);
3178 }
3179
3180 for (int nr = 0; nr < ri.nr; nr++) {
3181 char sub[MAXPATHLEN];
3182
3183 if ((ret = pfctl_get_eth_ruleset(dev, anchorname, nr, &rs)) != 0)
3184 errc(1, ret, "DIOCGETETHRULESET");
3185
3186 if (!strcmp(rs.name, PF_RESERVED_ANCHOR))
3187 continue;
3188 sub[0] = 0;
3189 if (rs.path[0]) {
3190 strlcat(sub, rs.path, sizeof(sub));
3191 strlcat(sub, "/", sizeof(sub));
3192 }
3193 strlcat(sub, rs.name, sizeof(sub));
3194 if (sub[0] != '_' || (opts & PF_OPT_VERBOSE))
3195 printf(" %s\n", sub);
3196 if ((opts & PF_OPT_VERBOSE) && pfctl_show_eth_anchors(dev, opts, sub))
3197 return (-1);
3198 }
3199 return (0);
3200 }
3201
3202 const char *
pfctl_lookup_option(char * cmd,const char * const * list)3203 pfctl_lookup_option(char *cmd, const char * const *list)
3204 {
3205 if (cmd != NULL && *cmd)
3206 for (; *list; list++)
3207 if (!strncmp(cmd, *list, strlen(cmd)))
3208 return (*list);
3209 return (NULL);
3210 }
3211
3212 void
pfctl_reset(int dev,int opts)3213 pfctl_reset(int dev, int opts)
3214 {
3215 struct pfctl pf;
3216 struct pfr_buffer t;
3217 int i;
3218
3219 memset(&pf, 0, sizeof(pf));
3220 pf.dev = dev;
3221 pf.h = pfh;
3222 pfctl_init_options(&pf);
3223
3224 /* Force reset upon pfctl_load_options() */
3225 pf.debug_set = 1;
3226 pf.reass_set = 1;
3227 pf.syncookieswat_set = 1;
3228 pf.ifname = strdup("none");
3229 if (pf.ifname == NULL)
3230 err(1, "%s: strdup", __func__);
3231 pf.ifname_set = 1;
3232
3233 memset(&t, 0, sizeof(t));
3234 t.pfrb_type = PFRB_TRANS;
3235 if (pfctl_trans(dev, &t, DIOCXBEGIN, 0))
3236 err(1, "%s: DIOCXBEGIN", __func__);
3237
3238 for (i = 0; pf_limits[i].name; i++)
3239 pf.limit_set[pf_limits[i].index] = 1;
3240
3241 for (i = 0; pf_timeouts[i].name; i++)
3242 pf.timeout_set[pf_timeouts[i].timeout] = 1;
3243
3244 pfctl_load_options(&pf);
3245
3246 if (pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
3247 err(1, "%s: DIOCXCOMMIT", __func__);
3248
3249 pfctl_clear_interface_flags(dev, opts);
3250 }
3251
3252 int
main(int argc,char * argv[])3253 main(int argc, char *argv[])
3254 {
3255 int ch;
3256 int mode = O_RDONLY;
3257 int opts = 0;
3258 int optimize = PF_OPTIMIZE_BASIC;
3259 char anchorname[MAXPATHLEN];
3260 char *path;
3261
3262 if (argc < 2)
3263 usage();
3264
3265 while ((ch = getopt(argc, argv,
3266 "a:AdD:eqf:F:ghi:k:K:mMnNOo:Pp:rRs:St:T:vx:z")) != -1) {
3267 switch (ch) {
3268 case 'a':
3269 anchoropt = optarg;
3270 break;
3271 case 'd':
3272 opts |= PF_OPT_DISABLE;
3273 mode = O_RDWR;
3274 break;
3275 case 'D':
3276 if (pfctl_cmdline_symset(optarg) < 0)
3277 warnx("could not parse macro definition %s",
3278 optarg);
3279 break;
3280 case 'e':
3281 opts |= PF_OPT_ENABLE;
3282 mode = O_RDWR;
3283 break;
3284 case 'q':
3285 opts |= PF_OPT_QUIET;
3286 break;
3287 case 'F':
3288 clearopt = pfctl_lookup_option(optarg, clearopt_list);
3289 if (clearopt == NULL) {
3290 warnx("Unknown flush modifier '%s'", optarg);
3291 usage();
3292 }
3293 mode = O_RDWR;
3294 break;
3295 case 'i':
3296 ifaceopt = optarg;
3297 break;
3298 case 'k':
3299 if (state_killers >= 2) {
3300 warnx("can only specify -k twice");
3301 usage();
3302 /* NOTREACHED */
3303 }
3304 state_kill[state_killers++] = optarg;
3305 mode = O_RDWR;
3306 break;
3307 case 'K':
3308 if (src_node_killers >= 2) {
3309 warnx("can only specify -K twice");
3310 usage();
3311 /* NOTREACHED */
3312 }
3313 src_node_kill[src_node_killers++] = optarg;
3314 mode = O_RDWR;
3315 break;
3316 case 'm':
3317 opts |= PF_OPT_MERGE;
3318 break;
3319 case 'M':
3320 opts |= PF_OPT_KILLMATCH;
3321 break;
3322 case 'n':
3323 opts |= PF_OPT_NOACTION;
3324 break;
3325 case 'N':
3326 loadopt |= PFCTL_FLAG_NAT;
3327 break;
3328 case 'r':
3329 opts |= PF_OPT_USEDNS;
3330 break;
3331 case 'f':
3332 rulesopt = optarg;
3333 mode = O_RDWR;
3334 break;
3335 case 'g':
3336 opts |= PF_OPT_DEBUG;
3337 break;
3338 case 'A':
3339 loadopt |= PFCTL_FLAG_ALTQ;
3340 break;
3341 case 'R':
3342 loadopt |= PFCTL_FLAG_FILTER;
3343 break;
3344 case 'o':
3345 optiopt = pfctl_lookup_option(optarg, optiopt_list);
3346 if (optiopt == NULL) {
3347 warnx("Unknown optimization '%s'", optarg);
3348 usage();
3349 }
3350 opts |= PF_OPT_OPTIMIZE;
3351 break;
3352 case 'O':
3353 loadopt |= PFCTL_FLAG_OPTION;
3354 break;
3355 case 'p':
3356 pf_device = optarg;
3357 break;
3358 case 'P':
3359 opts |= PF_OPT_NUMERIC;
3360 break;
3361 case 's':
3362 showopt = pfctl_lookup_option(optarg, showopt_list);
3363 if (showopt == NULL) {
3364 warnx("Unknown show modifier '%s'", optarg);
3365 usage();
3366 }
3367 break;
3368 case 'S':
3369 opts |= PF_OPT_NODNS;
3370 break;
3371 case 't':
3372 tableopt = optarg;
3373 break;
3374 case 'T':
3375 tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
3376 if (tblcmdopt == NULL) {
3377 warnx("Unknown table command '%s'", optarg);
3378 usage();
3379 }
3380 break;
3381 case 'v':
3382 if (opts & PF_OPT_VERBOSE)
3383 opts |= PF_OPT_VERBOSE2;
3384 opts |= PF_OPT_VERBOSE;
3385 break;
3386 case 'x':
3387 debugopt = pfctl_lookup_option(optarg, debugopt_list);
3388 if (debugopt == NULL) {
3389 warnx("Unknown debug level '%s'", optarg);
3390 usage();
3391 }
3392 mode = O_RDWR;
3393 break;
3394 case 'z':
3395 opts |= PF_OPT_CLRRULECTRS;
3396 mode = O_RDWR;
3397 break;
3398 case 'h':
3399 /* FALLTHROUGH */
3400 default:
3401 usage();
3402 /* NOTREACHED */
3403 }
3404 }
3405
3406 if ((opts & PF_OPT_NODNS) && (opts & PF_OPT_USEDNS))
3407 errx(1, "-N and -r are mutually exclusive");
3408
3409 if ((tblcmdopt == NULL) ^ (tableopt == NULL))
3410 usage();
3411
3412 if (tblcmdopt != NULL) {
3413 argc -= optind;
3414 argv += optind;
3415 ch = *tblcmdopt;
3416 if (ch == 'l') {
3417 loadopt |= PFCTL_FLAG_TABLE;
3418 tblcmdopt = NULL;
3419 } else
3420 mode = strchr("st", ch) ? O_RDONLY : O_RDWR;
3421 } else if (argc != optind) {
3422 warnx("unknown command line argument: %s ...", argv[optind]);
3423 usage();
3424 /* NOTREACHED */
3425 }
3426 if (loadopt == 0)
3427 loadopt = ~0;
3428
3429 memset(anchorname, 0, sizeof(anchorname));
3430 if (anchoropt != NULL) {
3431 int len = strlen(anchoropt);
3432
3433 if (anchoropt[0] == '\0')
3434 errx(1, "anchor name must not be empty");
3435 if (mode == O_RDONLY && showopt == NULL && tblcmdopt == NULL) {
3436 warnx("anchors apply to -f, -F, -s, and -T only");
3437 usage();
3438 }
3439 if (mode == O_RDWR && tblcmdopt == NULL &&
3440 (anchoropt[0] == '_' || strstr(anchoropt, "/_") != NULL))
3441 errx(1, "anchor names beginning with '_' cannot "
3442 "be modified from the command line");
3443
3444 if (len >= 1 && anchoropt[len - 1] == '*') {
3445 if (len >= 2 && anchoropt[len - 2] == '/')
3446 anchoropt[len - 2] = '\0';
3447 else
3448 anchoropt[len - 1] = '\0';
3449 opts |= PF_OPT_RECURSE;
3450 }
3451 if (strlcpy(anchorname, anchoropt,
3452 sizeof(anchorname)) >= sizeof(anchorname))
3453 errx(1, "anchor name '%s' too long",
3454 anchoropt);
3455 loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE|PFCTL_FLAG_ETH;
3456 }
3457
3458 if ((opts & PF_OPT_NOACTION) == 0) {
3459 dev = open(pf_device, mode);
3460 if (dev == -1)
3461 err(1, "%s", pf_device);
3462 altqsupport = pfctl_test_altqsupport(dev, opts);
3463 } else {
3464 dev = open(pf_device, O_RDONLY);
3465 if (dev >= 0)
3466 opts |= PF_OPT_DUMMYACTION;
3467 /* turn off options */
3468 opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
3469 clearopt = showopt = debugopt = NULL;
3470 #if !defined(ENABLE_ALTQ)
3471 altqsupport = 0;
3472 #else
3473 altqsupport = 1;
3474 #endif
3475 }
3476 pfh = pfctl_open(pf_device);
3477 if (pfh == NULL)
3478 err(1, "Failed to open netlink");
3479
3480 if ((opts & PF_OPT_NOACTION) == 0) {
3481 pfctl_read_limits(pfh);
3482 }
3483
3484 if (opts & PF_OPT_DISABLE)
3485 if (pfctl_disable(dev, opts))
3486 exit_val = 1;
3487
3488 if ((path = calloc(1, MAXPATHLEN)) == NULL)
3489 errx(1, "%s: calloc", __func__);
3490
3491 if (showopt != NULL) {
3492 switch (*showopt) {
3493 case 'A':
3494 pfctl_show_anchors(dev, opts, anchorname);
3495 if (opts & PF_OPT_VERBOSE2)
3496 printf("Ethernet:\n");
3497 pfctl_show_eth_anchors(dev, opts, anchorname);
3498 break;
3499 case 'r':
3500 pfctl_load_fingerprints(dev, opts);
3501 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
3502 anchorname, 0, 0);
3503 break;
3504 case 'l':
3505 pfctl_load_fingerprints(dev, opts);
3506 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
3507 anchorname, 0, 0);
3508 break;
3509 case 'n':
3510 pfctl_load_fingerprints(dev, opts);
3511 pfctl_show_nat(dev, path, opts, anchorname, 0, 0);
3512 break;
3513 case 'q':
3514 pfctl_show_altq(dev, ifaceopt, opts,
3515 opts & PF_OPT_VERBOSE2);
3516 break;
3517 case 's':
3518 pfctl_show_states(dev, ifaceopt, opts);
3519 break;
3520 case 'S':
3521 pfctl_show_src_nodes(dev, opts);
3522 break;
3523 case 'i':
3524 pfctl_show_status(dev, opts);
3525 break;
3526 case 'R':
3527 exit_val = pfctl_show_running(dev);
3528 break;
3529 case 't':
3530 pfctl_show_timeouts(dev, opts);
3531 break;
3532 case 'm':
3533 pfctl_show_limits(dev, opts);
3534 break;
3535 case 'e':
3536 pfctl_show_eth_rules(dev, path, opts, 0, anchorname, 0,
3537 0);
3538 break;
3539 case 'a':
3540 opts |= PF_OPT_SHOWALL;
3541 pfctl_load_fingerprints(dev, opts);
3542
3543 pfctl_show_eth_rules(dev, path, opts, 0, anchorname, 0,
3544 0);
3545
3546 pfctl_show_nat(dev, path, opts, anchorname, 0, 0);
3547 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_RULES,
3548 anchorname, 0, 0);
3549 pfctl_show_altq(dev, ifaceopt, opts, 0);
3550 pfctl_show_states(dev, ifaceopt, opts);
3551 pfctl_show_src_nodes(dev, opts);
3552 pfctl_show_status(dev, opts);
3553 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_LABELS,
3554 anchorname, 0, 0);
3555 pfctl_show_timeouts(dev, opts);
3556 pfctl_show_limits(dev, opts);
3557 pfctl_show_tables(anchorname, opts);
3558 pfctl_show_fingerprints(opts);
3559 break;
3560 case 'T':
3561 if (opts & PF_OPT_RECURSE) {
3562 opts |= PF_OPT_CALLSHOW;
3563 pfctl_recurse(dev, opts, anchorname,
3564 pfctl_call_showtables);
3565 } else
3566 pfctl_show_tables(anchorname, opts);
3567 break;
3568 case 'o':
3569 pfctl_load_fingerprints(dev, opts);
3570 pfctl_show_fingerprints(opts);
3571 break;
3572 case 'I':
3573 pfctl_show_ifaces(ifaceopt, opts);
3574 break;
3575 case 'c':
3576 pfctl_show_creators(opts);
3577 break;
3578 }
3579 }
3580
3581 if ((opts & PF_OPT_CLRRULECTRS) && showopt == NULL) {
3582 pfctl_show_eth_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
3583 anchorname, 0, 0);
3584 pfctl_show_rules(dev, path, opts, PFCTL_SHOW_NOTHING,
3585 anchorname, 0, 0);
3586 }
3587
3588 if (clearopt != NULL) {
3589 int mnr;
3590
3591 /* Check if anchor exists. */
3592 if ((pfctl_get_rulesets(pfh, anchorname, &mnr)) == ENOENT)
3593 errx(1, "No such anchor %s", anchorname);
3594
3595 switch (*clearopt) {
3596 case 'e':
3597 pfctl_flush_eth_rules(dev, opts, anchorname);
3598 break;
3599 case 'r':
3600 if (opts & PF_OPT_RECURSE)
3601 pfctl_recurse(dev, opts, anchorname,
3602 pfctl_call_clearrules);
3603 else
3604 pfctl_flush_rules(dev, opts, anchorname);
3605 break;
3606 case 'n':
3607 pfctl_flush_nat(dev, opts, anchorname);
3608 break;
3609 case 'q':
3610 pfctl_clear_altq(dev, opts);
3611 break;
3612 case 's':
3613 pfctl_clear_iface_states(dev, ifaceopt, opts);
3614 break;
3615 case 'S':
3616 pfctl_clear_src_nodes(dev, opts);
3617 break;
3618 case 'i':
3619 pfctl_clear_stats(pfh, opts);
3620 break;
3621 case 'a':
3622 if (ifaceopt) {
3623 warnx("don't specify an interface with -Fall");
3624 usage();
3625 /* NOTREACHED */
3626 }
3627 pfctl_flush_eth_rules(dev, opts, anchorname);
3628 pfctl_flush_rules(dev, opts, anchorname);
3629 pfctl_flush_nat(dev, opts, anchorname);
3630 if (opts & PF_OPT_RECURSE)
3631 pfctl_recurse(dev, opts, anchorname,
3632 pfctl_call_clearanchors);
3633 else {
3634 pfctl_do_clear_tables(anchorname, opts);
3635 pfctl_flush_rules(dev, opts, anchorname);
3636 }
3637 if (!*anchorname) {
3638 pfctl_clear_altq(dev, opts);
3639 pfctl_clear_iface_states(dev, ifaceopt, opts);
3640 pfctl_clear_src_nodes(dev, opts);
3641 pfctl_clear_stats(pfh, opts);
3642 pfctl_clear_fingerprints(dev, opts);
3643 pfctl_reset(dev, opts);
3644 }
3645 break;
3646 case 'o':
3647 pfctl_clear_fingerprints(dev, opts);
3648 break;
3649 case 'T':
3650 if ((opts & PF_OPT_RECURSE) == 0)
3651 pfctl_do_clear_tables(anchorname, opts);
3652 else
3653 pfctl_recurse(dev, opts, anchorname,
3654 pfctl_call_cleartables);
3655 break;
3656 case 'R':
3657 pfctl_reset(dev, opts);
3658 break;
3659 }
3660 }
3661 if (state_killers) {
3662 if (!strcmp(state_kill[0], "label"))
3663 pfctl_label_kill_states(dev, ifaceopt, opts);
3664 else if (!strcmp(state_kill[0], "id"))
3665 pfctl_id_kill_states(dev, ifaceopt, opts);
3666 else if (!strcmp(state_kill[0], "gateway"))
3667 pfctl_gateway_kill_states(dev, ifaceopt, opts);
3668 else if (!strcmp(state_kill[0], "key"))
3669 pfctl_key_kill_states(dev, ifaceopt, opts);
3670 else
3671 pfctl_net_kill_states(dev, ifaceopt, opts);
3672 }
3673
3674 if (src_node_killers)
3675 pfctl_kill_src_nodes(dev, opts);
3676
3677 if (tblcmdopt != NULL) {
3678 exit_val = pfctl_table(argc, argv, tableopt,
3679 tblcmdopt, rulesopt, anchorname, opts);
3680 rulesopt = NULL;
3681 }
3682 if (optiopt != NULL) {
3683 switch (*optiopt) {
3684 case 'n':
3685 optimize = 0;
3686 break;
3687 case 'b':
3688 optimize |= PF_OPTIMIZE_BASIC;
3689 break;
3690 case 'o':
3691 case 'p':
3692 optimize |= PF_OPTIMIZE_PROFILE;
3693 break;
3694 }
3695 }
3696
3697 if ((rulesopt != NULL) && (loadopt & PFCTL_FLAG_OPTION) &&
3698 !anchorname[0] && !(opts & PF_OPT_NOACTION))
3699 pfctl_get_skip_ifaces();
3700
3701 if (rulesopt != NULL && !(opts & PF_OPT_MERGE) &&
3702 !anchorname[0] && (loadopt & PFCTL_FLAG_OPTION))
3703 if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
3704 exit_val = 1;
3705
3706 if (rulesopt != NULL) {
3707 if (pfctl_rules(dev, rulesopt, opts, optimize,
3708 anchorname, NULL))
3709 exit_val = 1;
3710 }
3711
3712 if (opts & PF_OPT_ENABLE)
3713 if (pfctl_enable(dev, opts))
3714 exit_val = 1;
3715
3716 if (debugopt != NULL) {
3717 switch (*debugopt) {
3718 case 'n':
3719 pfctl_debug(dev, PF_DEBUG_NONE, opts);
3720 break;
3721 case 'u':
3722 pfctl_debug(dev, PF_DEBUG_URGENT, opts);
3723 break;
3724 case 'm':
3725 pfctl_debug(dev, PF_DEBUG_MISC, opts);
3726 break;
3727 case 'l':
3728 pfctl_debug(dev, PF_DEBUG_NOISY, opts);
3729 break;
3730 }
3731 }
3732
3733 /*
3734 * prevent pfctl_restore_limits() exit handler from restoring
3735 * pf(4) options settings on successful exit.
3736 */
3737 if (exit_val == 0) {
3738 close(dev);
3739 dev = -1;
3740 pfctl_close(pfh);
3741 pfh = NULL;
3742 }
3743
3744 return (exit_val);
3745 }
3746
3747 char *
pf_strerror(int errnum)3748 pf_strerror(int errnum)
3749 {
3750 switch (errnum) {
3751 case ESRCH:
3752 return "Table does not exist.";
3753 case EINVAL:
3754 case ENOENT:
3755 return "Anchor does not exist.";
3756 default:
3757 return strerror(errnum);
3758 }
3759 }
3760