pfctl.c revision 1.1 1 1.1 itojun /* $OpenBSD: pfctl.c,v 1.213 2004/03/20 09:31:42 david Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (c) 2001 Daniel Hartmeier
5 1.1 itojun * Copyright (c) 2002,2003 Henning Brauer
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun *
12 1.1 itojun * - Redistributions of source code must retain the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer.
14 1.1 itojun * - Redistributions in binary form must reproduce the above
15 1.1 itojun * copyright notice, this list of conditions and the following
16 1.1 itojun * disclaimer in the documentation and/or other materials provided
17 1.1 itojun * with the distribution.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.1 itojun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.1 itojun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 1.1 itojun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 1.1 itojun * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.1 itojun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1 itojun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 1.1 itojun * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 1.1 itojun * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 itojun * POSSIBILITY OF SUCH DAMAGE.
31 1.1 itojun *
32 1.1 itojun */
33 1.1 itojun
34 1.1 itojun #include <sys/types.h>
35 1.1 itojun #include <sys/ioctl.h>
36 1.1 itojun #include <sys/socket.h>
37 1.1 itojun
38 1.1 itojun #include <net/if.h>
39 1.1 itojun #include <netinet/in.h>
40 1.1 itojun #include <net/pfvar.h>
41 1.1 itojun #include <arpa/inet.h>
42 1.1 itojun #include <altq/altq.h>
43 1.1 itojun
44 1.1 itojun #include <err.h>
45 1.1 itojun #include <errno.h>
46 1.1 itojun #include <fcntl.h>
47 1.1 itojun #include <limits.h>
48 1.1 itojun #include <netdb.h>
49 1.1 itojun #include <stdio.h>
50 1.1 itojun #include <stdlib.h>
51 1.1 itojun #include <string.h>
52 1.1 itojun #include <unistd.h>
53 1.1 itojun
54 1.1 itojun #include "pfctl_parser.h"
55 1.1 itojun #include "pfctl.h"
56 1.1 itojun
57 1.1 itojun void usage(void);
58 1.1 itojun int pfctl_enable(int, int);
59 1.1 itojun int pfctl_disable(int, int);
60 1.1 itojun int pfctl_clear_stats(int, int);
61 1.1 itojun int pfctl_clear_rules(int, int, char *, char *);
62 1.1 itojun int pfctl_clear_nat(int, int, char *, char *);
63 1.1 itojun int pfctl_clear_altq(int, int);
64 1.1 itojun int pfctl_clear_src_nodes(int, int);
65 1.1 itojun int pfctl_clear_states(int, const char *, int);
66 1.1 itojun int pfctl_kill_states(int, const char *, int);
67 1.1 itojun int pfctl_get_pool(int, struct pf_pool *, u_int32_t, u_int32_t, int,
68 1.1 itojun char *, char *);
69 1.1 itojun void pfctl_print_rule_counters(struct pf_rule *, int);
70 1.1 itojun int pfctl_show_rules(int, int, int, char *, char *);
71 1.1 itojun int pfctl_show_nat(int, int, char *, char *);
72 1.1 itojun int pfctl_show_src_nodes(int, int);
73 1.1 itojun int pfctl_show_states(int, const char *, int);
74 1.1 itojun int pfctl_show_status(int, int);
75 1.1 itojun int pfctl_show_timeouts(int, int);
76 1.1 itojun int pfctl_show_limits(int, int);
77 1.1 itojun int pfctl_debug(int, u_int32_t, int);
78 1.1 itojun int pfctl_clear_rule_counters(int, int);
79 1.1 itojun int pfctl_test_altqsupport(int, int);
80 1.1 itojun int pfctl_show_anchors(int, int, char *);
81 1.1 itojun const char *pfctl_lookup_option(char *, const char **);
82 1.1 itojun
83 1.1 itojun const char *clearopt;
84 1.1 itojun char *rulesopt;
85 1.1 itojun const char *showopt;
86 1.1 itojun const char *debugopt;
87 1.1 itojun char *anchoropt;
88 1.1 itojun char *pf_device = "/dev/pf";
89 1.1 itojun char *ifaceopt;
90 1.1 itojun char *tableopt;
91 1.1 itojun const char *tblcmdopt;
92 1.1 itojun int state_killers;
93 1.1 itojun char *state_kill[2];
94 1.1 itojun int loadopt;
95 1.1 itojun int altqsupport;
96 1.1 itojun
97 1.1 itojun int dev = -1;
98 1.1 itojun int first_title = 1;
99 1.1 itojun int labels = 0;
100 1.1 itojun
101 1.1 itojun const char *infile;
102 1.1 itojun
103 1.1 itojun static const struct {
104 1.1 itojun const char *name;
105 1.1 itojun int index;
106 1.1 itojun } pf_limits[] = {
107 1.1 itojun { "states", PF_LIMIT_STATES },
108 1.1 itojun { "src-nodes", PF_LIMIT_SRC_NODES },
109 1.1 itojun { "frags", PF_LIMIT_FRAGS },
110 1.1 itojun { NULL, 0 }
111 1.1 itojun };
112 1.1 itojun
113 1.1 itojun struct pf_hint {
114 1.1 itojun const char *name;
115 1.1 itojun int timeout;
116 1.1 itojun };
117 1.1 itojun static const struct pf_hint pf_hint_normal[] = {
118 1.1 itojun { "tcp.first", 2 * 60 },
119 1.1 itojun { "tcp.opening", 30 },
120 1.1 itojun { "tcp.established", 24 * 60 * 60 },
121 1.1 itojun { "tcp.closing", 15 * 60 },
122 1.1 itojun { "tcp.finwait", 45 },
123 1.1 itojun { "tcp.closed", 90 },
124 1.1 itojun { NULL, 0 }
125 1.1 itojun };
126 1.1 itojun static const struct pf_hint pf_hint_satellite[] = {
127 1.1 itojun { "tcp.first", 3 * 60 },
128 1.1 itojun { "tcp.opening", 30 + 5 },
129 1.1 itojun { "tcp.established", 24 * 60 * 60 },
130 1.1 itojun { "tcp.closing", 15 * 60 + 5 },
131 1.1 itojun { "tcp.finwait", 45 + 5 },
132 1.1 itojun { "tcp.closed", 90 + 5 },
133 1.1 itojun { NULL, 0 }
134 1.1 itojun };
135 1.1 itojun static const struct pf_hint pf_hint_conservative[] = {
136 1.1 itojun { "tcp.first", 60 * 60 },
137 1.1 itojun { "tcp.opening", 15 * 60 },
138 1.1 itojun { "tcp.established", 5 * 24 * 60 * 60 },
139 1.1 itojun { "tcp.closing", 60 * 60 },
140 1.1 itojun { "tcp.finwait", 10 * 60 },
141 1.1 itojun { "tcp.closed", 3 * 60 },
142 1.1 itojun { NULL, 0 }
143 1.1 itojun };
144 1.1 itojun static const struct pf_hint pf_hint_aggressive[] = {
145 1.1 itojun { "tcp.first", 30 },
146 1.1 itojun { "tcp.opening", 5 },
147 1.1 itojun { "tcp.established", 5 * 60 * 60 },
148 1.1 itojun { "tcp.closing", 60 },
149 1.1 itojun { "tcp.finwait", 30 },
150 1.1 itojun { "tcp.closed", 30 },
151 1.1 itojun { NULL, 0 }
152 1.1 itojun };
153 1.1 itojun
154 1.1 itojun static const struct {
155 1.1 itojun const char *name;
156 1.1 itojun const struct pf_hint *hint;
157 1.1 itojun } pf_hints[] = {
158 1.1 itojun { "normal", pf_hint_normal },
159 1.1 itojun { "satellite", pf_hint_satellite },
160 1.1 itojun { "high-latency", pf_hint_satellite },
161 1.1 itojun { "conservative", pf_hint_conservative },
162 1.1 itojun { "aggressive", pf_hint_aggressive },
163 1.1 itojun { NULL, NULL }
164 1.1 itojun };
165 1.1 itojun
166 1.1 itojun static const char *clearopt_list[] = {
167 1.1 itojun "nat", "queue", "rules", "Sources",
168 1.1 itojun "state", "info", "Tables", "osfp", "all", NULL
169 1.1 itojun };
170 1.1 itojun
171 1.1 itojun static const char *showopt_list[] = {
172 1.1 itojun "nat", "queue", "rules", "Anchors", "Sources", "state", "info",
173 1.1 itojun "Interfaces", "labels", "timeouts", "memory", "Tables", "osfp",
174 1.1 itojun "all", NULL
175 1.1 itojun };
176 1.1 itojun
177 1.1 itojun static const char *tblcmdopt_list[] = {
178 1.1 itojun "kill", "flush", "add", "delete", "load", "replace", "show",
179 1.1 itojun "test", "zero", NULL
180 1.1 itojun };
181 1.1 itojun
182 1.1 itojun static const char *debugopt_list[] = {
183 1.1 itojun "none", "urgent", "misc", "loud", NULL
184 1.1 itojun };
185 1.1 itojun
186 1.1 itojun
187 1.1 itojun void
188 1.1 itojun usage(void)
189 1.1 itojun {
190 1.1 itojun extern char *__progname;
191 1.1 itojun
192 1.1 itojun fprintf(stderr, "usage: %s [-AdeghNnOqRrvz] ", __progname);
193 1.1 itojun fprintf(stderr, "[-a anchor[:ruleset]] [-D macro=value]\n");
194 1.1 itojun fprintf(stderr, " ");
195 1.1 itojun fprintf(stderr, "[-F modifier] [-f file] [-i interface] ");
196 1.1 itojun fprintf(stderr, "[-k host] [-p device]\n");
197 1.1 itojun fprintf(stderr, " ");
198 1.1 itojun fprintf(stderr, "[-s modifier] [-T command [address ...]] ");
199 1.1 itojun fprintf(stderr, "[-t table] [-x level]\n");
200 1.1 itojun exit(1);
201 1.1 itojun }
202 1.1 itojun
203 1.1 itojun int
204 1.1 itojun pfctl_enable(int dev, int opts)
205 1.1 itojun {
206 1.1 itojun if (ioctl(dev, DIOCSTART)) {
207 1.1 itojun if (errno == EEXIST)
208 1.1 itojun errx(1, "pf already enabled");
209 1.1 itojun else
210 1.1 itojun err(1, "DIOCSTART");
211 1.1 itojun }
212 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
213 1.1 itojun fprintf(stderr, "pf enabled\n");
214 1.1 itojun
215 1.1 itojun if (altqsupport && ioctl(dev, DIOCSTARTALTQ))
216 1.1 itojun if (errno != EEXIST)
217 1.1 itojun err(1, "DIOCSTARTALTQ");
218 1.1 itojun
219 1.1 itojun return (0);
220 1.1 itojun }
221 1.1 itojun
222 1.1 itojun int
223 1.1 itojun pfctl_disable(int dev, int opts)
224 1.1 itojun {
225 1.1 itojun if (ioctl(dev, DIOCSTOP)) {
226 1.1 itojun if (errno == ENOENT)
227 1.1 itojun errx(1, "pf not enabled");
228 1.1 itojun else
229 1.1 itojun err(1, "DIOCSTOP");
230 1.1 itojun }
231 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
232 1.1 itojun fprintf(stderr, "pf disabled\n");
233 1.1 itojun
234 1.1 itojun if (altqsupport && ioctl(dev, DIOCSTOPALTQ))
235 1.1 itojun if (errno != ENOENT)
236 1.1 itojun err(1, "DIOCSTOPALTQ");
237 1.1 itojun
238 1.1 itojun return (0);
239 1.1 itojun }
240 1.1 itojun
241 1.1 itojun int
242 1.1 itojun pfctl_clear_stats(int dev, int opts)
243 1.1 itojun {
244 1.1 itojun if (ioctl(dev, DIOCCLRSTATUS))
245 1.1 itojun err(1, "DIOCCLRSTATUS");
246 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
247 1.1 itojun fprintf(stderr, "pf: statistics cleared\n");
248 1.1 itojun return (0);
249 1.1 itojun }
250 1.1 itojun
251 1.1 itojun int
252 1.1 itojun pfctl_clear_rules(int dev, int opts, char *anchorname, char *rulesetname)
253 1.1 itojun {
254 1.1 itojun struct pfr_buffer t;
255 1.1 itojun
256 1.1 itojun if (*anchorname && !*rulesetname) {
257 1.1 itojun struct pfioc_ruleset pr;
258 1.1 itojun int mnr, nr, r;
259 1.1 itojun
260 1.1 itojun memset(&pr, 0, sizeof(pr));
261 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
262 1.1 itojun if (ioctl(dev, DIOCGETRULESETS, &pr)) {
263 1.1 itojun if (errno == EINVAL)
264 1.1 itojun fprintf(stderr, "No rulesets in anchor '%s'.\n",
265 1.1 itojun anchorname);
266 1.1 itojun else
267 1.1 itojun err(1, "DIOCGETRULESETS");
268 1.1 itojun return (-1);
269 1.1 itojun }
270 1.1 itojun mnr = pr.nr;
271 1.1 itojun for (nr = mnr - 1; nr >= 0; --nr) {
272 1.1 itojun pr.nr = nr;
273 1.1 itojun if (ioctl(dev, DIOCGETRULESET, &pr))
274 1.1 itojun err(1, "DIOCGETRULESET");
275 1.1 itojun r = pfctl_clear_rules(dev, opts | PF_OPT_QUIET,
276 1.1 itojun anchorname, pr.name);
277 1.1 itojun if (r)
278 1.1 itojun return (r);
279 1.1 itojun }
280 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
281 1.1 itojun fprintf(stderr, "rules cleared\n");
282 1.1 itojun return (0);
283 1.1 itojun }
284 1.1 itojun memset(&t, 0, sizeof(t));
285 1.1 itojun t.pfrb_type = PFRB_TRANS;
286 1.1 itojun if (pfctl_add_trans(&t, PF_RULESET_SCRUB, anchorname, rulesetname) ||
287 1.1 itojun pfctl_add_trans(&t, PF_RULESET_FILTER, anchorname, rulesetname) ||
288 1.1 itojun pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
289 1.1 itojun pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
290 1.1 itojun err(1, "pfctl_clear_rules");
291 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
292 1.1 itojun fprintf(stderr, "rules cleared\n");
293 1.1 itojun return (0);
294 1.1 itojun }
295 1.1 itojun
296 1.1 itojun int
297 1.1 itojun pfctl_clear_nat(int dev, int opts, char *anchorname, char *rulesetname)
298 1.1 itojun {
299 1.1 itojun struct pfr_buffer t;
300 1.1 itojun
301 1.1 itojun if (*anchorname && !*rulesetname) {
302 1.1 itojun struct pfioc_ruleset pr;
303 1.1 itojun int mnr, nr, r;
304 1.1 itojun
305 1.1 itojun memset(&pr, 0, sizeof(pr));
306 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
307 1.1 itojun if (ioctl(dev, DIOCGETRULESETS, &pr)) {
308 1.1 itojun if (errno == EINVAL)
309 1.1 itojun fprintf(stderr, "No rulesets in anchor '%s'.\n",
310 1.1 itojun anchorname);
311 1.1 itojun else
312 1.1 itojun err(1, "DIOCGETRULESETS");
313 1.1 itojun return (-1);
314 1.1 itojun }
315 1.1 itojun mnr = pr.nr;
316 1.1 itojun for (nr = mnr - 1; nr >= 0; --nr) {
317 1.1 itojun pr.nr = nr;
318 1.1 itojun if (ioctl(dev, DIOCGETRULESET, &pr))
319 1.1 itojun err(1, "DIOCGETRULESET");
320 1.1 itojun r = pfctl_clear_nat(dev, opts | PF_OPT_QUIET,
321 1.1 itojun anchorname, pr.name);
322 1.1 itojun if (r)
323 1.1 itojun return (r);
324 1.1 itojun }
325 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
326 1.1 itojun fprintf(stderr, "nat cleared\n");
327 1.1 itojun return (0);
328 1.1 itojun }
329 1.1 itojun memset(&t, 0, sizeof(t));
330 1.1 itojun t.pfrb_type = PFRB_TRANS;
331 1.1 itojun if (pfctl_add_trans(&t, PF_RULESET_NAT, anchorname, rulesetname) ||
332 1.1 itojun pfctl_add_trans(&t, PF_RULESET_BINAT, anchorname, rulesetname) ||
333 1.1 itojun pfctl_add_trans(&t, PF_RULESET_RDR, anchorname, rulesetname) ||
334 1.1 itojun pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
335 1.1 itojun pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
336 1.1 itojun err(1, "pfctl_clear_nat");
337 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
338 1.1 itojun fprintf(stderr, "nat cleared\n");
339 1.1 itojun return (0);
340 1.1 itojun }
341 1.1 itojun
342 1.1 itojun int
343 1.1 itojun pfctl_clear_altq(int dev, int opts)
344 1.1 itojun {
345 1.1 itojun struct pfr_buffer t;
346 1.1 itojun
347 1.1 itojun if (!altqsupport)
348 1.1 itojun return (-1);
349 1.1 itojun memset(&t, 0, sizeof(t));
350 1.1 itojun t.pfrb_type = PFRB_TRANS;
351 1.1 itojun if (pfctl_add_trans(&t, PF_RULESET_ALTQ, "", "") ||
352 1.1 itojun pfctl_trans(dev, &t, DIOCXBEGIN, 0) ||
353 1.1 itojun pfctl_trans(dev, &t, DIOCXCOMMIT, 0))
354 1.1 itojun err(1, "pfctl_clear_altq");
355 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
356 1.1 itojun fprintf(stderr, "altq cleared\n");
357 1.1 itojun return (0);
358 1.1 itojun }
359 1.1 itojun
360 1.1 itojun int
361 1.1 itojun pfctl_clear_src_nodes(int dev, int opts)
362 1.1 itojun {
363 1.1 itojun if (ioctl(dev, DIOCCLRSRCNODES))
364 1.1 itojun err(1, "DIOCCLRSRCNODES");
365 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
366 1.1 itojun fprintf(stderr, "source tracking entries cleared\n");
367 1.1 itojun return (0);
368 1.1 itojun }
369 1.1 itojun
370 1.1 itojun int
371 1.1 itojun pfctl_clear_states(int dev, const char *iface, int opts)
372 1.1 itojun {
373 1.1 itojun struct pfioc_state_kill psk;
374 1.1 itojun
375 1.1 itojun memset(&psk, 0, sizeof(psk));
376 1.1 itojun if (iface != NULL && strlcpy(psk.psk_ifname, iface,
377 1.1 itojun sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
378 1.1 itojun errx(1, "invalid interface: %s", iface);
379 1.1 itojun
380 1.1 itojun if (ioctl(dev, DIOCCLRSTATES, &psk))
381 1.1 itojun err(1, "DIOCCLRSTATES");
382 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
383 1.1 itojun fprintf(stderr, "%d states cleared\n", psk.psk_af);
384 1.1 itojun return (0);
385 1.1 itojun }
386 1.1 itojun
387 1.1 itojun int
388 1.1 itojun pfctl_kill_states(int dev, const char *iface, int opts)
389 1.1 itojun {
390 1.1 itojun struct pfioc_state_kill psk;
391 1.1 itojun struct addrinfo *res[2], *resp[2];
392 1.1 itojun struct sockaddr last_src, last_dst;
393 1.1 itojun int killed, sources, dests;
394 1.1 itojun int ret_ga;
395 1.1 itojun
396 1.1 itojun killed = sources = dests = 0;
397 1.1 itojun
398 1.1 itojun memset(&psk, 0, sizeof(psk));
399 1.1 itojun memset(&psk.psk_src.addr.v.a.mask, 0xff,
400 1.1 itojun sizeof(psk.psk_src.addr.v.a.mask));
401 1.1 itojun memset(&last_src, 0xff, sizeof(last_src));
402 1.1 itojun memset(&last_dst, 0xff, sizeof(last_dst));
403 1.1 itojun if (iface != NULL && strlcpy(psk.psk_ifname, iface,
404 1.1 itojun sizeof(psk.psk_ifname)) >= sizeof(psk.psk_ifname))
405 1.1 itojun errx(1, "invalid interface: %s", iface);
406 1.1 itojun
407 1.1 itojun if ((ret_ga = getaddrinfo(state_kill[0], NULL, NULL, &res[0]))) {
408 1.1 itojun errx(1, "getaddrinfo: %s", gai_strerror(ret_ga));
409 1.1 itojun /* NOTREACHED */
410 1.1 itojun }
411 1.1 itojun for (resp[0] = res[0]; resp[0]; resp[0] = resp[0]->ai_next) {
412 1.1 itojun if (resp[0]->ai_addr == NULL)
413 1.1 itojun continue;
414 1.1 itojun /* We get lots of duplicates. Catch the easy ones */
415 1.1 itojun if (memcmp(&last_src, resp[0]->ai_addr, sizeof(last_src)) == 0)
416 1.1 itojun continue;
417 1.1 itojun last_src = *(struct sockaddr *)resp[0]->ai_addr;
418 1.1 itojun
419 1.1 itojun psk.psk_af = resp[0]->ai_family;
420 1.1 itojun sources++;
421 1.1 itojun
422 1.1 itojun if (psk.psk_af == AF_INET)
423 1.1 itojun psk.psk_src.addr.v.a.addr.v4 =
424 1.1 itojun ((struct sockaddr_in *)resp[0]->ai_addr)->sin_addr;
425 1.1 itojun else if (psk.psk_af == AF_INET6)
426 1.1 itojun psk.psk_src.addr.v.a.addr.v6 =
427 1.1 itojun ((struct sockaddr_in6 *)resp[0]->ai_addr)->
428 1.1 itojun sin6_addr;
429 1.1 itojun else
430 1.1 itojun errx(1, "Unknown address family %d", psk.psk_af);
431 1.1 itojun
432 1.1 itojun if (state_killers > 1) {
433 1.1 itojun dests = 0;
434 1.1 itojun memset(&psk.psk_dst.addr.v.a.mask, 0xff,
435 1.1 itojun sizeof(psk.psk_dst.addr.v.a.mask));
436 1.1 itojun memset(&last_dst, 0xff, sizeof(last_dst));
437 1.1 itojun if ((ret_ga = getaddrinfo(state_kill[1], NULL, NULL,
438 1.1 itojun &res[1]))) {
439 1.1 itojun errx(1, "getaddrinfo: %s",
440 1.1 itojun gai_strerror(ret_ga));
441 1.1 itojun /* NOTREACHED */
442 1.1 itojun }
443 1.1 itojun for (resp[1] = res[1]; resp[1];
444 1.1 itojun resp[1] = resp[1]->ai_next) {
445 1.1 itojun if (resp[1]->ai_addr == NULL)
446 1.1 itojun continue;
447 1.1 itojun if (psk.psk_af != resp[1]->ai_family)
448 1.1 itojun continue;
449 1.1 itojun
450 1.1 itojun if (memcmp(&last_dst, resp[1]->ai_addr,
451 1.1 itojun sizeof(last_dst)) == 0)
452 1.1 itojun continue;
453 1.1 itojun last_dst = *(struct sockaddr *)resp[1]->ai_addr;
454 1.1 itojun
455 1.1 itojun dests++;
456 1.1 itojun
457 1.1 itojun if (psk.psk_af == AF_INET)
458 1.1 itojun psk.psk_dst.addr.v.a.addr.v4 =
459 1.1 itojun ((struct sockaddr_in *)resp[1]->
460 1.1 itojun ai_addr)->sin_addr;
461 1.1 itojun else if (psk.psk_af == AF_INET6)
462 1.1 itojun psk.psk_dst.addr.v.a.addr.v6 =
463 1.1 itojun ((struct sockaddr_in6 *)resp[1]->
464 1.1 itojun ai_addr)->sin6_addr;
465 1.1 itojun else
466 1.1 itojun errx(1, "Unknown address family %d",
467 1.1 itojun psk.psk_af);
468 1.1 itojun
469 1.1 itojun if (ioctl(dev, DIOCKILLSTATES, &psk))
470 1.1 itojun err(1, "DIOCKILLSTATES");
471 1.1 itojun killed += psk.psk_af;
472 1.1 itojun /* fixup psk.psk_af */
473 1.1 itojun psk.psk_af = resp[1]->ai_family;
474 1.1 itojun }
475 1.1 itojun freeaddrinfo(res[1]);
476 1.1 itojun } else {
477 1.1 itojun if (ioctl(dev, DIOCKILLSTATES, &psk))
478 1.1 itojun err(1, "DIOCKILLSTATES");
479 1.1 itojun killed += psk.psk_af;
480 1.1 itojun /* fixup psk.psk_af */
481 1.1 itojun psk.psk_af = res[0]->ai_family;
482 1.1 itojun }
483 1.1 itojun }
484 1.1 itojun
485 1.1 itojun freeaddrinfo(res[0]);
486 1.1 itojun
487 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
488 1.1 itojun fprintf(stderr, "killed %d states from %d sources and %d "
489 1.1 itojun "destinations\n", killed, sources, dests);
490 1.1 itojun return (0);
491 1.1 itojun }
492 1.1 itojun
493 1.1 itojun int
494 1.1 itojun pfctl_get_pool(int dev, struct pf_pool *pool, u_int32_t nr,
495 1.1 itojun u_int32_t ticket, int r_action, char *anchorname, char *rulesetname)
496 1.1 itojun {
497 1.1 itojun struct pfioc_pooladdr pp;
498 1.1 itojun struct pf_pooladdr *pa;
499 1.1 itojun u_int32_t pnr, mpnr;
500 1.1 itojun
501 1.1 itojun memset(&pp, 0, sizeof(pp));
502 1.1 itojun memcpy(pp.anchor, anchorname, sizeof(pp.anchor));
503 1.1 itojun memcpy(pp.ruleset, rulesetname, sizeof(pp.ruleset));
504 1.1 itojun pp.r_action = r_action;
505 1.1 itojun pp.r_num = nr;
506 1.1 itojun pp.ticket = ticket;
507 1.1 itojun if (ioctl(dev, DIOCGETADDRS, &pp)) {
508 1.1 itojun warn("DIOCGETADDRS");
509 1.1 itojun return (-1);
510 1.1 itojun }
511 1.1 itojun mpnr = pp.nr;
512 1.1 itojun TAILQ_INIT(&pool->list);
513 1.1 itojun for (pnr = 0; pnr < mpnr; ++pnr) {
514 1.1 itojun pp.nr = pnr;
515 1.1 itojun if (ioctl(dev, DIOCGETADDR, &pp)) {
516 1.1 itojun warn("DIOCGETADDR");
517 1.1 itojun return (-1);
518 1.1 itojun }
519 1.1 itojun pa = calloc(1, sizeof(struct pf_pooladdr));
520 1.1 itojun if (pa == NULL)
521 1.1 itojun err(1, "calloc");
522 1.1 itojun bcopy(&pp.addr, pa, sizeof(struct pf_pooladdr));
523 1.1 itojun TAILQ_INSERT_TAIL(&pool->list, pa, entries);
524 1.1 itojun }
525 1.1 itojun
526 1.1 itojun return (0);
527 1.1 itojun }
528 1.1 itojun
529 1.1 itojun void
530 1.1 itojun pfctl_clear_pool(struct pf_pool *pool)
531 1.1 itojun {
532 1.1 itojun struct pf_pooladdr *pa;
533 1.1 itojun
534 1.1 itojun while ((pa = TAILQ_FIRST(&pool->list)) != NULL) {
535 1.1 itojun TAILQ_REMOVE(&pool->list, pa, entries);
536 1.1 itojun free(pa);
537 1.1 itojun }
538 1.1 itojun }
539 1.1 itojun
540 1.1 itojun void
541 1.1 itojun pfctl_print_rule_counters(struct pf_rule *rule, int opts)
542 1.1 itojun {
543 1.1 itojun if (opts & PF_OPT_DEBUG) {
544 1.1 itojun const char *t[PF_SKIP_COUNT] = { "i", "d", "f",
545 1.1 itojun "p", "sa", "sp", "da", "dp" };
546 1.1 itojun int i;
547 1.1 itojun
548 1.1 itojun printf(" [ Skip steps: ");
549 1.1 itojun for (i = 0; i < PF_SKIP_COUNT; ++i) {
550 1.1 itojun if (rule->skip[i].nr == rule->nr + 1)
551 1.1 itojun continue;
552 1.1 itojun printf("%s=", t[i]);
553 1.1 itojun if (rule->skip[i].nr == -1)
554 1.1 itojun printf("end ");
555 1.1 itojun else
556 1.1 itojun printf("%u ", rule->skip[i].nr);
557 1.1 itojun }
558 1.1 itojun printf("]\n");
559 1.1 itojun
560 1.1 itojun printf(" [ queue: qname=%s qid=%u pqname=%s pqid=%u ]\n",
561 1.1 itojun rule->qname, rule->qid, rule->pqname, rule->pqid);
562 1.1 itojun }
563 1.1 itojun if (opts & PF_OPT_VERBOSE)
564 1.1 itojun printf(" [ Evaluations: %-8llu Packets: %-8llu "
565 1.1 itojun "Bytes: %-10llu States: %-6u]\n",
566 1.1 itojun (unsigned long long)rule->evaluations,
567 1.1 itojun (unsigned long long)rule->packets,
568 1.1 itojun (unsigned long long)rule->bytes, rule->states);
569 1.1 itojun }
570 1.1 itojun
571 1.1 itojun void
572 1.1 itojun pfctl_print_title(char *title)
573 1.1 itojun {
574 1.1 itojun if (!first_title)
575 1.1 itojun printf("\n");
576 1.1 itojun first_title = 0;
577 1.1 itojun printf("%s\n", title);
578 1.1 itojun }
579 1.1 itojun
580 1.1 itojun int
581 1.1 itojun pfctl_show_rules(int dev, int opts, int format, char *anchorname,
582 1.1 itojun char *rulesetname)
583 1.1 itojun {
584 1.1 itojun struct pfioc_rule pr;
585 1.1 itojun u_int32_t nr, mnr, header = 0;
586 1.1 itojun int rule_numbers = opts & (PF_OPT_VERBOSE2 | PF_OPT_DEBUG);
587 1.1 itojun
588 1.1 itojun if (*anchorname && !*rulesetname) {
589 1.1 itojun struct pfioc_ruleset pr;
590 1.1 itojun int r;
591 1.1 itojun
592 1.1 itojun memset(&pr, 0, sizeof(pr));
593 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
594 1.1 itojun if (ioctl(dev, DIOCGETRULESETS, &pr)) {
595 1.1 itojun if (errno == EINVAL)
596 1.1 itojun fprintf(stderr, "No rulesets in anchor '%s'.\n",
597 1.1 itojun anchorname);
598 1.1 itojun else
599 1.1 itojun err(1, "DIOCGETRULESETS");
600 1.1 itojun return (-1);
601 1.1 itojun }
602 1.1 itojun if (opts & PF_OPT_SHOWALL && pr.nr)
603 1.1 itojun pfctl_print_title("FILTER RULES:");
604 1.1 itojun mnr = pr.nr;
605 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
606 1.1 itojun pr.nr = nr;
607 1.1 itojun if (ioctl(dev, DIOCGETRULESET, &pr))
608 1.1 itojun err(1, "DIOCGETRULESET");
609 1.1 itojun r = pfctl_show_rules(dev, opts, format, anchorname,
610 1.1 itojun pr.name);
611 1.1 itojun if (r)
612 1.1 itojun return (r);
613 1.1 itojun }
614 1.1 itojun return (0);
615 1.1 itojun }
616 1.1 itojun
617 1.1 itojun memset(&pr, 0, sizeof(pr));
618 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
619 1.1 itojun memcpy(pr.ruleset, rulesetname, sizeof(pr.ruleset));
620 1.1 itojun if (opts & PF_OPT_SHOWALL) {
621 1.1 itojun pr.rule.action = PF_PASS;
622 1.1 itojun if (ioctl(dev, DIOCGETRULES, &pr)) {
623 1.1 itojun warn("DIOCGETRULES");
624 1.1 itojun return (-1);
625 1.1 itojun }
626 1.1 itojun header++;
627 1.1 itojun }
628 1.1 itojun pr.rule.action = PF_SCRUB;
629 1.1 itojun if (ioctl(dev, DIOCGETRULES, &pr)) {
630 1.1 itojun warn("DIOCGETRULES");
631 1.1 itojun return (-1);
632 1.1 itojun }
633 1.1 itojun if (opts & PF_OPT_SHOWALL) {
634 1.1 itojun if (format == 0 && (pr.nr > 0 || header))
635 1.1 itojun pfctl_print_title("FILTER RULES:");
636 1.1 itojun else if (format == 1 && labels)
637 1.1 itojun pfctl_print_title("LABEL COUNTERS:");
638 1.1 itojun }
639 1.1 itojun mnr = pr.nr;
640 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
641 1.1 itojun pr.nr = nr;
642 1.1 itojun if (ioctl(dev, DIOCGETRULE, &pr)) {
643 1.1 itojun warn("DIOCGETRULE");
644 1.1 itojun return (-1);
645 1.1 itojun }
646 1.1 itojun
647 1.1 itojun if (pfctl_get_pool(dev, &pr.rule.rpool,
648 1.1 itojun nr, pr.ticket, PF_SCRUB, anchorname, rulesetname) != 0)
649 1.1 itojun return (-1);
650 1.1 itojun
651 1.1 itojun switch (format) {
652 1.1 itojun case 1:
653 1.1 itojun if (pr.rule.label[0]) {
654 1.1 itojun printf("%s ", pr.rule.label);
655 1.1 itojun printf("%llu %llu %llu\n",
656 1.1 itojun (unsigned long long)pr.rule.evaluations,
657 1.1 itojun (unsigned long long)pr.rule.packets,
658 1.1 itojun (unsigned long long)pr.rule.bytes);
659 1.1 itojun }
660 1.1 itojun break;
661 1.1 itojun default:
662 1.1 itojun if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
663 1.1 itojun labels = 1;
664 1.1 itojun print_rule(&pr.rule, rule_numbers);
665 1.1 itojun pfctl_print_rule_counters(&pr.rule, opts);
666 1.1 itojun }
667 1.1 itojun pfctl_clear_pool(&pr.rule.rpool);
668 1.1 itojun }
669 1.1 itojun pr.rule.action = PF_PASS;
670 1.1 itojun if (ioctl(dev, DIOCGETRULES, &pr)) {
671 1.1 itojun warn("DIOCGETRULES");
672 1.1 itojun return (-1);
673 1.1 itojun }
674 1.1 itojun mnr = pr.nr;
675 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
676 1.1 itojun pr.nr = nr;
677 1.1 itojun if (ioctl(dev, DIOCGETRULE, &pr)) {
678 1.1 itojun warn("DIOCGETRULE");
679 1.1 itojun return (-1);
680 1.1 itojun }
681 1.1 itojun
682 1.1 itojun if (pfctl_get_pool(dev, &pr.rule.rpool,
683 1.1 itojun nr, pr.ticket, PF_PASS, anchorname, rulesetname) != 0)
684 1.1 itojun return (-1);
685 1.1 itojun
686 1.1 itojun switch (format) {
687 1.1 itojun case 1:
688 1.1 itojun if (pr.rule.label[0]) {
689 1.1 itojun printf("%s ", pr.rule.label);
690 1.1 itojun printf("%llu %llu %llu\n",
691 1.1 itojun (unsigned long long)pr.rule.evaluations,
692 1.1 itojun (unsigned long long)pr.rule.packets,
693 1.1 itojun (unsigned long long)pr.rule.bytes);
694 1.1 itojun }
695 1.1 itojun break;
696 1.1 itojun default:
697 1.1 itojun if (pr.rule.label[0] && (opts & PF_OPT_SHOWALL))
698 1.1 itojun labels = 1;
699 1.1 itojun print_rule(&pr.rule, rule_numbers);
700 1.1 itojun pfctl_print_rule_counters(&pr.rule, opts);
701 1.1 itojun }
702 1.1 itojun pfctl_clear_pool(&pr.rule.rpool);
703 1.1 itojun }
704 1.1 itojun return (0);
705 1.1 itojun }
706 1.1 itojun
707 1.1 itojun int
708 1.1 itojun pfctl_show_nat(int dev, int opts, char *anchorname, char *rulesetname)
709 1.1 itojun {
710 1.1 itojun struct pfioc_rule pr;
711 1.1 itojun u_int32_t mnr, nr;
712 1.1 itojun static int nattype[3] = { PF_NAT, PF_RDR, PF_BINAT };
713 1.1 itojun int i, dotitle = opts & PF_OPT_SHOWALL;
714 1.1 itojun
715 1.1 itojun if (*anchorname && !*rulesetname) {
716 1.1 itojun struct pfioc_ruleset pr;
717 1.1 itojun int r;
718 1.1 itojun
719 1.1 itojun memset(&pr, 0, sizeof(pr));
720 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
721 1.1 itojun if (ioctl(dev, DIOCGETRULESETS, &pr)) {
722 1.1 itojun if (errno == EINVAL)
723 1.1 itojun fprintf(stderr, "No rulesets in anchor '%s'.\n",
724 1.1 itojun anchorname);
725 1.1 itojun else
726 1.1 itojun err(1, "DIOCGETRULESETS");
727 1.1 itojun return (-1);
728 1.1 itojun }
729 1.1 itojun mnr = pr.nr;
730 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
731 1.1 itojun pr.nr = nr;
732 1.1 itojun if (ioctl(dev, DIOCGETRULESET, &pr))
733 1.1 itojun err(1, "DIOCGETRULESET");
734 1.1 itojun r = pfctl_show_nat(dev, opts, anchorname, pr.name);
735 1.1 itojun if (r)
736 1.1 itojun return (r);
737 1.1 itojun }
738 1.1 itojun return (0);
739 1.1 itojun }
740 1.1 itojun
741 1.1 itojun memset(&pr, 0, sizeof(pr));
742 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
743 1.1 itojun memcpy(pr.ruleset, rulesetname, sizeof(pr.ruleset));
744 1.1 itojun for (i = 0; i < 3; i++) {
745 1.1 itojun pr.rule.action = nattype[i];
746 1.1 itojun if (ioctl(dev, DIOCGETRULES, &pr)) {
747 1.1 itojun warn("DIOCGETRULES");
748 1.1 itojun return (-1);
749 1.1 itojun }
750 1.1 itojun mnr = pr.nr;
751 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
752 1.1 itojun pr.nr = nr;
753 1.1 itojun if (ioctl(dev, DIOCGETRULE, &pr)) {
754 1.1 itojun warn("DIOCGETRULE");
755 1.1 itojun return (-1);
756 1.1 itojun }
757 1.1 itojun if (pfctl_get_pool(dev, &pr.rule.rpool, nr,
758 1.1 itojun pr.ticket, nattype[i], anchorname,
759 1.1 itojun rulesetname) != 0)
760 1.1 itojun return (-1);
761 1.1 itojun if (dotitle) {
762 1.1 itojun pfctl_print_title("TRANSLATION RULES:");
763 1.1 itojun dotitle = 0;
764 1.1 itojun }
765 1.1 itojun print_rule(&pr.rule, opts & PF_OPT_VERBOSE2);
766 1.1 itojun pfctl_print_rule_counters(&pr.rule, opts);
767 1.1 itojun pfctl_clear_pool(&pr.rule.rpool);
768 1.1 itojun }
769 1.1 itojun }
770 1.1 itojun return (0);
771 1.1 itojun }
772 1.1 itojun
773 1.1 itojun int
774 1.1 itojun pfctl_show_src_nodes(int dev, int opts)
775 1.1 itojun {
776 1.1 itojun struct pfioc_src_nodes psn;
777 1.1 itojun struct pf_src_node *p;
778 1.1 itojun char *inbuf = NULL, *newinbuf = NULL;
779 1.1 itojun unsigned len = 0;
780 1.1 itojun int i;
781 1.1 itojun
782 1.1 itojun memset(&psn, 0, sizeof(psn));
783 1.1 itojun for (;;) {
784 1.1 itojun psn.psn_len = len;
785 1.1 itojun if (len) {
786 1.1 itojun newinbuf = realloc(inbuf, len);
787 1.1 itojun if (newinbuf == NULL)
788 1.1 itojun err(1, "realloc");
789 1.1 itojun psn.psn_buf = inbuf = newinbuf;
790 1.1 itojun }
791 1.1 itojun if (ioctl(dev, DIOCGETSRCNODES, &psn) < 0) {
792 1.1 itojun warn("DIOCGETSRCNODES");
793 1.1 itojun return (-1);
794 1.1 itojun }
795 1.1 itojun if (psn.psn_len + sizeof(struct pfioc_src_nodes) < len)
796 1.1 itojun break;
797 1.1 itojun if (len == 0 && psn.psn_len == 0)
798 1.1 itojun return (0);
799 1.1 itojun if (len == 0 && psn.psn_len != 0)
800 1.1 itojun len = psn.psn_len;
801 1.1 itojun if (psn.psn_len == 0)
802 1.1 itojun return (0); /* no src_nodes */
803 1.1 itojun len *= 2;
804 1.1 itojun }
805 1.1 itojun p = psn.psn_src_nodes;
806 1.1 itojun if (psn.psn_len > 0 && (opts & PF_OPT_SHOWALL))
807 1.1 itojun pfctl_print_title("SOURCE TRACKING NODES:");
808 1.1 itojun for (i = 0; i < psn.psn_len; i += sizeof(*p)) {
809 1.1 itojun print_src_node(p, opts);
810 1.1 itojun p++;
811 1.1 itojun }
812 1.1 itojun return (0);
813 1.1 itojun }
814 1.1 itojun
815 1.1 itojun int
816 1.1 itojun pfctl_show_states(int dev, const char *iface, int opts)
817 1.1 itojun {
818 1.1 itojun struct pfioc_states ps;
819 1.1 itojun struct pf_state *p;
820 1.1 itojun char *inbuf = NULL, *newinbuf = NULL;
821 1.1 itojun unsigned len = 0;
822 1.1 itojun int i, dotitle = (opts & PF_OPT_SHOWALL);
823 1.1 itojun
824 1.1 itojun memset(&ps, 0, sizeof(ps));
825 1.1 itojun for (;;) {
826 1.1 itojun ps.ps_len = len;
827 1.1 itojun if (len) {
828 1.1 itojun newinbuf = realloc(inbuf, len);
829 1.1 itojun if (newinbuf == NULL)
830 1.1 itojun err(1, "realloc");
831 1.1 itojun ps.ps_buf = inbuf = newinbuf;
832 1.1 itojun }
833 1.1 itojun if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
834 1.1 itojun warn("DIOCGETSTATES");
835 1.1 itojun return (-1);
836 1.1 itojun }
837 1.1 itojun if (ps.ps_len + sizeof(struct pfioc_states) < len)
838 1.1 itojun break;
839 1.1 itojun if (len == 0 && ps.ps_len == 0)
840 1.1 itojun return (0);
841 1.1 itojun if (len == 0 && ps.ps_len != 0)
842 1.1 itojun len = ps.ps_len;
843 1.1 itojun if (ps.ps_len == 0)
844 1.1 itojun return (0); /* no states */
845 1.1 itojun len *= 2;
846 1.1 itojun }
847 1.1 itojun p = ps.ps_states;
848 1.1 itojun for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
849 1.1 itojun if (iface != NULL && strcmp(p->u.ifname, iface))
850 1.1 itojun continue;
851 1.1 itojun if (dotitle) {
852 1.1 itojun pfctl_print_title("STATES:");
853 1.1 itojun dotitle = 0;
854 1.1 itojun }
855 1.1 itojun print_state(p, opts);
856 1.1 itojun }
857 1.1 itojun return (0);
858 1.1 itojun }
859 1.1 itojun
860 1.1 itojun int
861 1.1 itojun pfctl_show_status(int dev, int opts)
862 1.1 itojun {
863 1.1 itojun struct pf_status status;
864 1.1 itojun
865 1.1 itojun if (ioctl(dev, DIOCGETSTATUS, &status)) {
866 1.1 itojun warn("DIOCGETSTATUS");
867 1.1 itojun return (-1);
868 1.1 itojun }
869 1.1 itojun if (opts & PF_OPT_SHOWALL)
870 1.1 itojun pfctl_print_title("INFO:");
871 1.1 itojun print_status(&status, opts);
872 1.1 itojun return (0);
873 1.1 itojun }
874 1.1 itojun
875 1.1 itojun int
876 1.1 itojun pfctl_show_timeouts(int dev, int opts)
877 1.1 itojun {
878 1.1 itojun struct pfioc_tm pt;
879 1.1 itojun int i;
880 1.1 itojun
881 1.1 itojun if (opts & PF_OPT_SHOWALL)
882 1.1 itojun pfctl_print_title("TIMEOUTS:");
883 1.1 itojun memset(&pt, 0, sizeof(pt));
884 1.1 itojun for (i = 0; pf_timeouts[i].name; i++) {
885 1.1 itojun pt.timeout = pf_timeouts[i].timeout;
886 1.1 itojun if (ioctl(dev, DIOCGETTIMEOUT, &pt))
887 1.1 itojun err(1, "DIOCGETTIMEOUT");
888 1.1 itojun printf("%-20s %10d", pf_timeouts[i].name, pt.seconds);
889 1.1 itojun if (i >= PFTM_ADAPTIVE_START && i <= PFTM_ADAPTIVE_END)
890 1.1 itojun printf(" states");
891 1.1 itojun else
892 1.1 itojun printf("s");
893 1.1 itojun printf("\n");
894 1.1 itojun }
895 1.1 itojun return (0);
896 1.1 itojun
897 1.1 itojun }
898 1.1 itojun
899 1.1 itojun int
900 1.1 itojun pfctl_show_limits(int dev, int opts)
901 1.1 itojun {
902 1.1 itojun struct pfioc_limit pl;
903 1.1 itojun int i;
904 1.1 itojun
905 1.1 itojun if (opts & PF_OPT_SHOWALL)
906 1.1 itojun pfctl_print_title("LIMITS:");
907 1.1 itojun memset(&pl, 0, sizeof(pl));
908 1.1 itojun for (i = 0; pf_limits[i].name; i++) {
909 1.1 itojun pl.index = pf_limits[i].index;
910 1.1 itojun if (ioctl(dev, DIOCGETLIMIT, &pl))
911 1.1 itojun err(1, "DIOCGETLIMIT");
912 1.1 itojun printf("%-10s ", pf_limits[i].name);
913 1.1 itojun if (pl.limit == UINT_MAX)
914 1.1 itojun printf("unlimited\n");
915 1.1 itojun else
916 1.1 itojun printf("hard limit %6u\n", pl.limit);
917 1.1 itojun }
918 1.1 itojun return (0);
919 1.1 itojun }
920 1.1 itojun
921 1.1 itojun /* callbacks for rule/nat/rdr/addr */
922 1.1 itojun int
923 1.1 itojun pfctl_add_pool(struct pfctl *pf, struct pf_pool *p, sa_family_t af)
924 1.1 itojun {
925 1.1 itojun struct pf_pooladdr *pa;
926 1.1 itojun
927 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
928 1.1 itojun if (ioctl(pf->dev, DIOCBEGINADDRS, &pf->paddr))
929 1.1 itojun err(1, "DIOCBEGINADDRS");
930 1.1 itojun }
931 1.1 itojun
932 1.1 itojun pf->paddr.af = af;
933 1.1 itojun TAILQ_FOREACH(pa, &p->list, entries) {
934 1.1 itojun memcpy(&pf->paddr.addr, pa, sizeof(struct pf_pooladdr));
935 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
936 1.1 itojun if (ioctl(pf->dev, DIOCADDADDR, &pf->paddr))
937 1.1 itojun err(1, "DIOCADDADDR");
938 1.1 itojun }
939 1.1 itojun }
940 1.1 itojun return (0);
941 1.1 itojun }
942 1.1 itojun
943 1.1 itojun int
944 1.1 itojun pfctl_add_rule(struct pfctl *pf, struct pf_rule *r)
945 1.1 itojun {
946 1.1 itojun u_int8_t rs_num;
947 1.1 itojun struct pfioc_rule pr;
948 1.1 itojun
949 1.1 itojun switch (r->action) {
950 1.1 itojun case PF_SCRUB:
951 1.1 itojun if ((loadopt & PFCTL_FLAG_FILTER) == 0)
952 1.1 itojun return (0);
953 1.1 itojun rs_num = PF_RULESET_SCRUB;
954 1.1 itojun break;
955 1.1 itojun case PF_DROP:
956 1.1 itojun case PF_PASS:
957 1.1 itojun if ((loadopt & PFCTL_FLAG_FILTER) == 0)
958 1.1 itojun return (0);
959 1.1 itojun rs_num = PF_RULESET_FILTER;
960 1.1 itojun break;
961 1.1 itojun case PF_NAT:
962 1.1 itojun case PF_NONAT:
963 1.1 itojun if ((loadopt & PFCTL_FLAG_NAT) == 0)
964 1.1 itojun return (0);
965 1.1 itojun rs_num = PF_RULESET_NAT;
966 1.1 itojun break;
967 1.1 itojun case PF_RDR:
968 1.1 itojun case PF_NORDR:
969 1.1 itojun if ((loadopt & PFCTL_FLAG_NAT) == 0)
970 1.1 itojun return (0);
971 1.1 itojun rs_num = PF_RULESET_RDR;
972 1.1 itojun break;
973 1.1 itojun case PF_BINAT:
974 1.1 itojun case PF_NOBINAT:
975 1.1 itojun if ((loadopt & PFCTL_FLAG_NAT) == 0)
976 1.1 itojun return (0);
977 1.1 itojun rs_num = PF_RULESET_BINAT;
978 1.1 itojun break;
979 1.1 itojun default:
980 1.1 itojun errx(1, "Invalid rule type");
981 1.1 itojun break;
982 1.1 itojun }
983 1.1 itojun
984 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
985 1.1 itojun bzero(&pr, sizeof(pr));
986 1.1 itojun if (strlcpy(pr.anchor, pf->anchor, sizeof(pr.anchor)) >=
987 1.1 itojun sizeof(pr.anchor) ||
988 1.1 itojun strlcpy(pr.ruleset, pf->ruleset, sizeof(pr.ruleset)) >=
989 1.1 itojun sizeof(pr.ruleset))
990 1.1 itojun errx(1, "pfctl_add_rule: strlcpy");
991 1.1 itojun if (pfctl_add_pool(pf, &r->rpool, r->af))
992 1.1 itojun return (1);
993 1.1 itojun pr.ticket = pfctl_get_ticket(pf->trans, rs_num, pf->anchor,
994 1.1 itojun pf->ruleset);
995 1.1 itojun pr.pool_ticket = pf->paddr.ticket;
996 1.1 itojun memcpy(&pr.rule, r, sizeof(pr.rule));
997 1.1 itojun if (ioctl(pf->dev, DIOCADDRULE, &pr))
998 1.1 itojun err(1, "DIOCADDRULE");
999 1.1 itojun }
1000 1.1 itojun if (pf->opts & PF_OPT_VERBOSE)
1001 1.1 itojun print_rule(r, pf->opts & PF_OPT_VERBOSE2);
1002 1.1 itojun pfctl_clear_pool(&r->rpool);
1003 1.1 itojun return (0);
1004 1.1 itojun }
1005 1.1 itojun
1006 1.1 itojun int
1007 1.1 itojun pfctl_add_altq(struct pfctl *pf, struct pf_altq *a)
1008 1.1 itojun {
1009 1.1 itojun if (altqsupport &&
1010 1.1 itojun (loadopt & PFCTL_FLAG_ALTQ) != 0) {
1011 1.1 itojun memcpy(&pf->paltq->altq, a, sizeof(struct pf_altq));
1012 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
1013 1.1 itojun if (ioctl(pf->dev, DIOCADDALTQ, pf->paltq)) {
1014 1.1 itojun if (errno == ENXIO)
1015 1.1 itojun errx(1, "qtype not configured");
1016 1.1 itojun else if (errno == ENODEV)
1017 1.1 itojun errx(1, "%s: driver does not support "
1018 1.1 itojun "altq", a->ifname);
1019 1.1 itojun else
1020 1.1 itojun err(1, "DIOCADDALTQ");
1021 1.1 itojun }
1022 1.1 itojun }
1023 1.1 itojun pfaltq_store(&pf->paltq->altq);
1024 1.1 itojun }
1025 1.1 itojun return (0);
1026 1.1 itojun }
1027 1.1 itojun
1028 1.1 itojun int
1029 1.1 itojun pfctl_rules(int dev, char *filename, int opts, char *anchorname,
1030 1.1 itojun char *rulesetname, struct pfr_buffer *trans)
1031 1.1 itojun {
1032 1.1 itojun #define ERR(x) do { warn(x); goto _error; } while(0)
1033 1.1 itojun #define ERRX(x) do { warnx(x); goto _error; } while(0)
1034 1.1 itojun
1035 1.1 itojun FILE *fin;
1036 1.1 itojun struct pfr_buffer *t, buf;
1037 1.1 itojun struct pfioc_altq pa;
1038 1.1 itojun struct pfctl pf;
1039 1.1 itojun struct pfr_table trs;
1040 1.1 itojun int osize;
1041 1.1 itojun
1042 1.1 itojun if (trans == NULL) {
1043 1.1 itojun bzero(&buf, sizeof(buf));
1044 1.1 itojun buf.pfrb_type = PFRB_TRANS;
1045 1.1 itojun t = &buf;
1046 1.1 itojun osize = 0;
1047 1.1 itojun } else {
1048 1.1 itojun t = trans;
1049 1.1 itojun osize = t->pfrb_size;
1050 1.1 itojun }
1051 1.1 itojun
1052 1.1 itojun memset(&pa, 0, sizeof(pa));
1053 1.1 itojun memset(&pf, 0, sizeof(pf));
1054 1.1 itojun memset(&trs, 0, sizeof(trs));
1055 1.1 itojun if (strlcpy(trs.pfrt_anchor, anchorname,
1056 1.1 itojun sizeof(trs.pfrt_anchor)) >= sizeof(trs.pfrt_anchor) ||
1057 1.1 itojun strlcpy(trs.pfrt_ruleset, rulesetname,
1058 1.1 itojun sizeof(trs.pfrt_ruleset)) >= sizeof(trs.pfrt_ruleset))
1059 1.1 itojun ERRX("pfctl_rules: strlcpy");
1060 1.1 itojun if (strcmp(filename, "-") == 0) {
1061 1.1 itojun fin = stdin;
1062 1.1 itojun infile = "stdin";
1063 1.1 itojun } else {
1064 1.1 itojun if ((fin = fopen(filename, "r")) == NULL) {
1065 1.1 itojun warn("%s", filename);
1066 1.1 itojun return (1);
1067 1.1 itojun }
1068 1.1 itojun infile = filename;
1069 1.1 itojun }
1070 1.1 itojun pf.dev = dev;
1071 1.1 itojun pf.opts = opts;
1072 1.1 itojun pf.loadopt = loadopt;
1073 1.1 itojun if (anchorname[0])
1074 1.1 itojun pf.loadopt &= ~PFCTL_FLAG_ALTQ;
1075 1.1 itojun pf.paltq = &pa;
1076 1.1 itojun pf.trans = t;
1077 1.1 itojun pf.rule_nr = 0;
1078 1.1 itojun pf.anchor = anchorname;
1079 1.1 itojun pf.ruleset = rulesetname;
1080 1.1 itojun
1081 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0) {
1082 1.1 itojun if ((pf.loadopt & PFCTL_FLAG_NAT) != 0) {
1083 1.1 itojun if (pfctl_add_trans(t, PF_RULESET_NAT, anchorname,
1084 1.1 itojun rulesetname) ||
1085 1.1 itojun pfctl_add_trans(t, PF_RULESET_BINAT, anchorname,
1086 1.1 itojun rulesetname) ||
1087 1.1 itojun pfctl_add_trans(t, PF_RULESET_RDR, anchorname,
1088 1.1 itojun rulesetname))
1089 1.1 itojun ERR("pfctl_rules");
1090 1.1 itojun }
1091 1.1 itojun if (((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))) {
1092 1.1 itojun if (pfctl_add_trans(t, PF_RULESET_ALTQ, anchorname,
1093 1.1 itojun rulesetname))
1094 1.1 itojun ERR("pfctl_rules");
1095 1.1 itojun }
1096 1.1 itojun if ((pf.loadopt & PFCTL_FLAG_FILTER) != 0) {
1097 1.1 itojun if (pfctl_add_trans(t, PF_RULESET_SCRUB, anchorname,
1098 1.1 itojun rulesetname) ||
1099 1.1 itojun pfctl_add_trans(t, PF_RULESET_FILTER, anchorname,
1100 1.1 itojun rulesetname))
1101 1.1 itojun ERR("pfctl_rules");
1102 1.1 itojun }
1103 1.1 itojun if (pf.loadopt & PFCTL_FLAG_TABLE) {
1104 1.1 itojun if (pfctl_add_trans(t, PF_RULESET_TABLE, anchorname,
1105 1.1 itojun rulesetname))
1106 1.1 itojun ERR("pfctl_rules");
1107 1.1 itojun }
1108 1.1 itojun if (pfctl_trans(dev, t, DIOCXBEGIN, osize))
1109 1.1 itojun ERR("DIOCXBEGIN");
1110 1.1 itojun if (altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ))
1111 1.1 itojun pa.ticket = pfctl_get_ticket(t, PF_RULESET_ALTQ,
1112 1.1 itojun anchorname, rulesetname);
1113 1.1 itojun if (pf.loadopt & PFCTL_FLAG_TABLE)
1114 1.1 itojun pf.tticket = pfctl_get_ticket(t, PF_RULESET_TABLE,
1115 1.1 itojun anchorname, rulesetname);
1116 1.1 itojun }
1117 1.1 itojun if (parse_rules(fin, &pf) < 0) {
1118 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0)
1119 1.1 itojun ERRX("Syntax error in config file: "
1120 1.1 itojun "pf rules not loaded");
1121 1.1 itojun else
1122 1.1 itojun goto _error;
1123 1.1 itojun }
1124 1.1 itojun if ((altqsupport && (pf.loadopt & PFCTL_FLAG_ALTQ) != 0))
1125 1.1 itojun if (check_commit_altq(dev, opts) != 0)
1126 1.1 itojun ERRX("errors in altq config");
1127 1.1 itojun if (fin != stdin)
1128 1.1 itojun fclose(fin);
1129 1.1 itojun
1130 1.1 itojun /* process "load anchor" directives */
1131 1.1 itojun if (!anchorname[0] && !rulesetname[0])
1132 1.1 itojun if (pfctl_load_anchors(dev, opts, t) == -1)
1133 1.1 itojun ERRX("load anchors");
1134 1.1 itojun
1135 1.1 itojun if (trans == NULL && (opts & PF_OPT_NOACTION) == 0)
1136 1.1 itojun if (pfctl_trans(dev, t, DIOCXCOMMIT, 0))
1137 1.1 itojun ERR("DIOCXCOMMIT");
1138 1.1 itojun return (0);
1139 1.1 itojun
1140 1.1 itojun _error:
1141 1.1 itojun if (trans == NULL) { /* main ruleset */
1142 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0)
1143 1.1 itojun if (pfctl_trans(dev, t, DIOCXROLLBACK, 0))
1144 1.1 itojun err(1, "DIOCXROLLBACK");
1145 1.1 itojun exit(1);
1146 1.1 itojun } else /* sub ruleset */
1147 1.1 itojun return (-1);
1148 1.1 itojun
1149 1.1 itojun #undef ERR
1150 1.1 itojun #undef ERRX
1151 1.1 itojun }
1152 1.1 itojun
1153 1.1 itojun int
1154 1.1 itojun pfctl_set_limit(struct pfctl *pf, const char *opt, unsigned int limit)
1155 1.1 itojun {
1156 1.1 itojun struct pfioc_limit pl;
1157 1.1 itojun int i;
1158 1.1 itojun
1159 1.1 itojun if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1160 1.1 itojun return (0);
1161 1.1 itojun
1162 1.1 itojun memset(&pl, 0, sizeof(pl));
1163 1.1 itojun for (i = 0; pf_limits[i].name; i++) {
1164 1.1 itojun if (strcasecmp(opt, pf_limits[i].name) == 0) {
1165 1.1 itojun pl.index = pf_limits[i].index;
1166 1.1 itojun pl.limit = limit;
1167 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
1168 1.1 itojun if (ioctl(pf->dev, DIOCSETLIMIT, &pl)) {
1169 1.1 itojun if (errno == EBUSY) {
1170 1.1 itojun warnx("Current pool "
1171 1.1 itojun "size exceeds requested "
1172 1.1 itojun "hard limit");
1173 1.1 itojun return (1);
1174 1.1 itojun } else
1175 1.1 itojun err(1, "DIOCSETLIMIT");
1176 1.1 itojun }
1177 1.1 itojun }
1178 1.1 itojun break;
1179 1.1 itojun }
1180 1.1 itojun }
1181 1.1 itojun if (pf_limits[i].name == NULL) {
1182 1.1 itojun warnx("Bad pool name.");
1183 1.1 itojun return (1);
1184 1.1 itojun }
1185 1.1 itojun
1186 1.1 itojun if (pf->opts & PF_OPT_VERBOSE)
1187 1.1 itojun printf("set limit %s %d\n", opt, limit);
1188 1.1 itojun
1189 1.1 itojun return (0);
1190 1.1 itojun }
1191 1.1 itojun
1192 1.1 itojun int
1193 1.1 itojun pfctl_set_timeout(struct pfctl *pf, const char *opt, int seconds, int quiet)
1194 1.1 itojun {
1195 1.1 itojun struct pfioc_tm pt;
1196 1.1 itojun int i;
1197 1.1 itojun
1198 1.1 itojun if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1199 1.1 itojun return (0);
1200 1.1 itojun
1201 1.1 itojun memset(&pt, 0, sizeof(pt));
1202 1.1 itojun for (i = 0; pf_timeouts[i].name; i++) {
1203 1.1 itojun if (strcasecmp(opt, pf_timeouts[i].name) == 0) {
1204 1.1 itojun pt.timeout = pf_timeouts[i].timeout;
1205 1.1 itojun break;
1206 1.1 itojun }
1207 1.1 itojun }
1208 1.1 itojun
1209 1.1 itojun if (pf_timeouts[i].name == NULL) {
1210 1.1 itojun warnx("Bad timeout name.");
1211 1.1 itojun return (1);
1212 1.1 itojun }
1213 1.1 itojun
1214 1.1 itojun pt.seconds = seconds;
1215 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
1216 1.1 itojun if (ioctl(pf->dev, DIOCSETTIMEOUT, &pt))
1217 1.1 itojun err(1, "DIOCSETTIMEOUT");
1218 1.1 itojun }
1219 1.1 itojun
1220 1.1 itojun if (pf->opts & PF_OPT_VERBOSE && ! quiet)
1221 1.1 itojun printf("set timeout %s %d\n", opt, seconds);
1222 1.1 itojun
1223 1.1 itojun return (0);
1224 1.1 itojun }
1225 1.1 itojun
1226 1.1 itojun int
1227 1.1 itojun pfctl_set_optimization(struct pfctl *pf, const char *opt)
1228 1.1 itojun {
1229 1.1 itojun const struct pf_hint *hint;
1230 1.1 itojun int i, r;
1231 1.1 itojun
1232 1.1 itojun if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1233 1.1 itojun return (0);
1234 1.1 itojun
1235 1.1 itojun for (i = 0; pf_hints[i].name; i++)
1236 1.1 itojun if (strcasecmp(opt, pf_hints[i].name) == 0)
1237 1.1 itojun break;
1238 1.1 itojun
1239 1.1 itojun hint = pf_hints[i].hint;
1240 1.1 itojun if (hint == NULL) {
1241 1.1 itojun warnx("Bad hint name.");
1242 1.1 itojun return (1);
1243 1.1 itojun }
1244 1.1 itojun
1245 1.1 itojun for (i = 0; hint[i].name; i++)
1246 1.1 itojun if ((r = pfctl_set_timeout(pf, hint[i].name,
1247 1.1 itojun hint[i].timeout, 1)))
1248 1.1 itojun return (r);
1249 1.1 itojun
1250 1.1 itojun if (pf->opts & PF_OPT_VERBOSE)
1251 1.1 itojun printf("set optimization %s\n", opt);
1252 1.1 itojun
1253 1.1 itojun return (0);
1254 1.1 itojun }
1255 1.1 itojun
1256 1.1 itojun int
1257 1.1 itojun pfctl_set_logif(struct pfctl *pf, char *ifname)
1258 1.1 itojun {
1259 1.1 itojun struct pfioc_if pi;
1260 1.1 itojun
1261 1.1 itojun if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1262 1.1 itojun return (0);
1263 1.1 itojun
1264 1.1 itojun memset(&pi, 0, sizeof(pi));
1265 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0) {
1266 1.1 itojun if (!strcmp(ifname, "none"))
1267 1.1 itojun bzero(pi.ifname, sizeof(pi.ifname));
1268 1.1 itojun else {
1269 1.1 itojun if (strlcpy(pi.ifname, ifname,
1270 1.1 itojun sizeof(pi.ifname)) >= sizeof(pi.ifname))
1271 1.1 itojun errx(1, "pfctl_set_logif: strlcpy");
1272 1.1 itojun }
1273 1.1 itojun if (ioctl(pf->dev, DIOCSETSTATUSIF, &pi))
1274 1.1 itojun err(1, "DIOCSETSTATUSIF");
1275 1.1 itojun }
1276 1.1 itojun
1277 1.1 itojun if (pf->opts & PF_OPT_VERBOSE)
1278 1.1 itojun printf("set loginterface %s\n", ifname);
1279 1.1 itojun
1280 1.1 itojun return (0);
1281 1.1 itojun }
1282 1.1 itojun
1283 1.1 itojun int
1284 1.1 itojun pfctl_set_hostid(struct pfctl *pf, u_int32_t hostid)
1285 1.1 itojun {
1286 1.1 itojun if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1287 1.1 itojun return (0);
1288 1.1 itojun
1289 1.1 itojun HTONL(hostid);
1290 1.1 itojun
1291 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0)
1292 1.1 itojun if (ioctl(dev, DIOCSETHOSTID, &hostid))
1293 1.1 itojun err(1, "DIOCSETHOSTID");
1294 1.1 itojun
1295 1.1 itojun if (pf->opts & PF_OPT_VERBOSE)
1296 1.1 itojun printf("set hostid 0x%08x\n", ntohl(hostid));
1297 1.1 itojun
1298 1.1 itojun return (0);
1299 1.1 itojun }
1300 1.1 itojun
1301 1.1 itojun int
1302 1.1 itojun pfctl_set_debug(struct pfctl *pf, char *d)
1303 1.1 itojun {
1304 1.1 itojun u_int32_t level;
1305 1.1 itojun
1306 1.1 itojun if ((loadopt & PFCTL_FLAG_OPTION) == 0)
1307 1.1 itojun return (0);
1308 1.1 itojun
1309 1.1 itojun if (!strcmp(d, "none"))
1310 1.1 itojun level = PF_DEBUG_NONE;
1311 1.1 itojun else if (!strcmp(d, "urgent"))
1312 1.1 itojun level = PF_DEBUG_URGENT;
1313 1.1 itojun else if (!strcmp(d, "misc"))
1314 1.1 itojun level = PF_DEBUG_MISC;
1315 1.1 itojun else if (!strcmp(d, "loud"))
1316 1.1 itojun level = PF_DEBUG_NOISY;
1317 1.1 itojun else {
1318 1.1 itojun warnx("unknown debug level \"%s\"", d);
1319 1.1 itojun return (-1);
1320 1.1 itojun }
1321 1.1 itojun
1322 1.1 itojun if ((pf->opts & PF_OPT_NOACTION) == 0)
1323 1.1 itojun if (ioctl(dev, DIOCSETDEBUG, &level))
1324 1.1 itojun err(1, "DIOCSETDEBUG");
1325 1.1 itojun
1326 1.1 itojun if (pf->opts & PF_OPT_VERBOSE)
1327 1.1 itojun printf("set debug %s\n", d);
1328 1.1 itojun
1329 1.1 itojun return (0);
1330 1.1 itojun }
1331 1.1 itojun
1332 1.1 itojun int
1333 1.1 itojun pfctl_debug(int dev, u_int32_t level, int opts)
1334 1.1 itojun {
1335 1.1 itojun if (ioctl(dev, DIOCSETDEBUG, &level))
1336 1.1 itojun err(1, "DIOCSETDEBUG");
1337 1.1 itojun if ((opts & PF_OPT_QUIET) == 0) {
1338 1.1 itojun fprintf(stderr, "debug level set to '");
1339 1.1 itojun switch (level) {
1340 1.1 itojun case PF_DEBUG_NONE:
1341 1.1 itojun fprintf(stderr, "none");
1342 1.1 itojun break;
1343 1.1 itojun case PF_DEBUG_URGENT:
1344 1.1 itojun fprintf(stderr, "urgent");
1345 1.1 itojun break;
1346 1.1 itojun case PF_DEBUG_MISC:
1347 1.1 itojun fprintf(stderr, "misc");
1348 1.1 itojun break;
1349 1.1 itojun case PF_DEBUG_NOISY:
1350 1.1 itojun fprintf(stderr, "loud");
1351 1.1 itojun break;
1352 1.1 itojun default:
1353 1.1 itojun fprintf(stderr, "<invalid>");
1354 1.1 itojun break;
1355 1.1 itojun }
1356 1.1 itojun fprintf(stderr, "'\n");
1357 1.1 itojun }
1358 1.1 itojun return (0);
1359 1.1 itojun }
1360 1.1 itojun
1361 1.1 itojun int
1362 1.1 itojun pfctl_clear_rule_counters(int dev, int opts)
1363 1.1 itojun {
1364 1.1 itojun if (ioctl(dev, DIOCCLRRULECTRS))
1365 1.1 itojun err(1, "DIOCCLRRULECTRS");
1366 1.1 itojun if ((opts & PF_OPT_QUIET) == 0)
1367 1.1 itojun fprintf(stderr, "pf: rule counters cleared\n");
1368 1.1 itojun return (0);
1369 1.1 itojun }
1370 1.1 itojun
1371 1.1 itojun int
1372 1.1 itojun pfctl_test_altqsupport(int dev, int opts)
1373 1.1 itojun {
1374 1.1 itojun struct pfioc_altq pa;
1375 1.1 itojun
1376 1.1 itojun if (ioctl(dev, DIOCGETALTQS, &pa)) {
1377 1.1 itojun if (errno == ENODEV) {
1378 1.1 itojun if (!(opts & PF_OPT_QUIET))
1379 1.1 itojun fprintf(stderr, "No ALTQ support in kernel\n"
1380 1.1 itojun "ALTQ related functions disabled\n");
1381 1.1 itojun return (0);
1382 1.1 itojun } else
1383 1.1 itojun err(1, "DIOCGETALTQS");
1384 1.1 itojun }
1385 1.1 itojun return (1);
1386 1.1 itojun }
1387 1.1 itojun
1388 1.1 itojun int
1389 1.1 itojun pfctl_show_anchors(int dev, int opts, char *anchorname)
1390 1.1 itojun {
1391 1.1 itojun u_int32_t nr, mnr;
1392 1.1 itojun
1393 1.1 itojun if (!*anchorname) {
1394 1.1 itojun struct pfioc_anchor pa;
1395 1.1 itojun
1396 1.1 itojun memset(&pa, 0, sizeof(pa));
1397 1.1 itojun if (ioctl(dev, DIOCGETANCHORS, &pa)) {
1398 1.1 itojun warn("DIOCGETANCHORS");
1399 1.1 itojun return (-1);
1400 1.1 itojun }
1401 1.1 itojun mnr = pa.nr;
1402 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
1403 1.1 itojun pa.nr = nr;
1404 1.1 itojun if (ioctl(dev, DIOCGETANCHOR, &pa)) {
1405 1.1 itojun warn("DIOCGETANCHOR");
1406 1.1 itojun return (-1);
1407 1.1 itojun }
1408 1.1 itojun if (!(opts & PF_OPT_VERBOSE) &&
1409 1.1 itojun !strcmp(pa.name, PF_RESERVED_ANCHOR))
1410 1.1 itojun continue;
1411 1.1 itojun printf(" %s\n", pa.name);
1412 1.1 itojun }
1413 1.1 itojun } else {
1414 1.1 itojun struct pfioc_ruleset pr;
1415 1.1 itojun
1416 1.1 itojun memset(&pr, 0, sizeof(pr));
1417 1.1 itojun memcpy(pr.anchor, anchorname, sizeof(pr.anchor));
1418 1.1 itojun if (ioctl(dev, DIOCGETRULESETS, &pr)) {
1419 1.1 itojun if (errno == EINVAL)
1420 1.1 itojun fprintf(stderr, "No rulesets in anchor '%s'.\n",
1421 1.1 itojun anchorname);
1422 1.1 itojun else
1423 1.1 itojun err(1, "DIOCGETRULESETS");
1424 1.1 itojun return (-1);
1425 1.1 itojun }
1426 1.1 itojun mnr = pr.nr;
1427 1.1 itojun for (nr = 0; nr < mnr; ++nr) {
1428 1.1 itojun pr.nr = nr;
1429 1.1 itojun if (ioctl(dev, DIOCGETRULESET, &pr))
1430 1.1 itojun err(1, "DIOCGETRULESET");
1431 1.1 itojun printf(" %s:%s\n", pr.anchor, pr.name);
1432 1.1 itojun }
1433 1.1 itojun }
1434 1.1 itojun return (0);
1435 1.1 itojun }
1436 1.1 itojun
1437 1.1 itojun const char *
1438 1.1 itojun pfctl_lookup_option(char *cmd, const char **list)
1439 1.1 itojun {
1440 1.1 itojun if (cmd != NULL && *cmd)
1441 1.1 itojun for (; *list; list++)
1442 1.1 itojun if (!strncmp(cmd, *list, strlen(cmd)))
1443 1.1 itojun return (*list);
1444 1.1 itojun return (NULL);
1445 1.1 itojun }
1446 1.1 itojun
1447 1.1 itojun int
1448 1.1 itojun main(int argc, char *argv[])
1449 1.1 itojun {
1450 1.1 itojun int error = 0;
1451 1.1 itojun int ch;
1452 1.1 itojun int mode = O_RDONLY;
1453 1.1 itojun int opts = 0;
1454 1.1 itojun char anchorname[PF_ANCHOR_NAME_SIZE];
1455 1.1 itojun char rulesetname[PF_RULESET_NAME_SIZE];
1456 1.1 itojun
1457 1.1 itojun if (argc < 2)
1458 1.1 itojun usage();
1459 1.1 itojun
1460 1.1 itojun while ((ch = getopt(argc, argv,
1461 1.1 itojun "a:AdD:eqf:F:ghi:k:nNOp:rRs:t:T:vx:z")) != -1) {
1462 1.1 itojun switch (ch) {
1463 1.1 itojun case 'a':
1464 1.1 itojun anchoropt = optarg;
1465 1.1 itojun break;
1466 1.1 itojun case 'd':
1467 1.1 itojun opts |= PF_OPT_DISABLE;
1468 1.1 itojun mode = O_RDWR;
1469 1.1 itojun break;
1470 1.1 itojun case 'D':
1471 1.1 itojun if (pfctl_cmdline_symset(optarg) < 0)
1472 1.1 itojun warnx("could not parse macro definition %s",
1473 1.1 itojun optarg);
1474 1.1 itojun break;
1475 1.1 itojun case 'e':
1476 1.1 itojun opts |= PF_OPT_ENABLE;
1477 1.1 itojun mode = O_RDWR;
1478 1.1 itojun break;
1479 1.1 itojun case 'q':
1480 1.1 itojun opts |= PF_OPT_QUIET;
1481 1.1 itojun break;
1482 1.1 itojun case 'F':
1483 1.1 itojun clearopt = pfctl_lookup_option(optarg, clearopt_list);
1484 1.1 itojun if (clearopt == NULL) {
1485 1.1 itojun warnx("Unknown flush modifier '%s'", optarg);
1486 1.1 itojun usage();
1487 1.1 itojun }
1488 1.1 itojun mode = O_RDWR;
1489 1.1 itojun break;
1490 1.1 itojun case 'i':
1491 1.1 itojun ifaceopt = optarg;
1492 1.1 itojun break;
1493 1.1 itojun case 'k':
1494 1.1 itojun if (state_killers >= 2) {
1495 1.1 itojun warnx("can only specify -k twice");
1496 1.1 itojun usage();
1497 1.1 itojun /* NOTREACHED */
1498 1.1 itojun }
1499 1.1 itojun state_kill[state_killers++] = optarg;
1500 1.1 itojun mode = O_RDWR;
1501 1.1 itojun break;
1502 1.1 itojun case 'n':
1503 1.1 itojun opts |= PF_OPT_NOACTION;
1504 1.1 itojun break;
1505 1.1 itojun case 'N':
1506 1.1 itojun loadopt |= PFCTL_FLAG_NAT;
1507 1.1 itojun break;
1508 1.1 itojun case 'r':
1509 1.1 itojun opts |= PF_OPT_USEDNS;
1510 1.1 itojun break;
1511 1.1 itojun case 'f':
1512 1.1 itojun rulesopt = optarg;
1513 1.1 itojun mode = O_RDWR;
1514 1.1 itojun break;
1515 1.1 itojun case 'g':
1516 1.1 itojun opts |= PF_OPT_DEBUG;
1517 1.1 itojun break;
1518 1.1 itojun case 'A':
1519 1.1 itojun loadopt |= PFCTL_FLAG_ALTQ;
1520 1.1 itojun break;
1521 1.1 itojun case 'R':
1522 1.1 itojun loadopt |= PFCTL_FLAG_FILTER;
1523 1.1 itojun break;
1524 1.1 itojun case 'O':
1525 1.1 itojun loadopt |= PFCTL_FLAG_OPTION;
1526 1.1 itojun break;
1527 1.1 itojun case 'p':
1528 1.1 itojun pf_device = optarg;
1529 1.1 itojun break;
1530 1.1 itojun case 's':
1531 1.1 itojun showopt = pfctl_lookup_option(optarg, showopt_list);
1532 1.1 itojun if (showopt == NULL) {
1533 1.1 itojun warnx("Unknown show modifier '%s'", optarg);
1534 1.1 itojun usage();
1535 1.1 itojun }
1536 1.1 itojun break;
1537 1.1 itojun case 't':
1538 1.1 itojun tableopt = optarg;
1539 1.1 itojun break;
1540 1.1 itojun case 'T':
1541 1.1 itojun tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
1542 1.1 itojun if (tblcmdopt == NULL) {
1543 1.1 itojun warnx("Unknown table command '%s'", optarg);
1544 1.1 itojun usage();
1545 1.1 itojun }
1546 1.1 itojun break;
1547 1.1 itojun case 'v':
1548 1.1 itojun if (opts & PF_OPT_VERBOSE)
1549 1.1 itojun opts |= PF_OPT_VERBOSE2;
1550 1.1 itojun opts |= PF_OPT_VERBOSE;
1551 1.1 itojun break;
1552 1.1 itojun case 'x':
1553 1.1 itojun debugopt = pfctl_lookup_option(optarg, debugopt_list);
1554 1.1 itojun if (debugopt == NULL) {
1555 1.1 itojun warnx("Unknown debug level '%s'", optarg);
1556 1.1 itojun usage();
1557 1.1 itojun }
1558 1.1 itojun mode = O_RDWR;
1559 1.1 itojun break;
1560 1.1 itojun case 'z':
1561 1.1 itojun opts |= PF_OPT_CLRRULECTRS;
1562 1.1 itojun mode = O_RDWR;
1563 1.1 itojun break;
1564 1.1 itojun case 'h':
1565 1.1 itojun /* FALLTHROUGH */
1566 1.1 itojun default:
1567 1.1 itojun usage();
1568 1.1 itojun /* NOTREACHED */
1569 1.1 itojun }
1570 1.1 itojun }
1571 1.1 itojun
1572 1.1 itojun if (tblcmdopt != NULL) {
1573 1.1 itojun argc -= optind;
1574 1.1 itojun argv += optind;
1575 1.1 itojun ch = *tblcmdopt;
1576 1.1 itojun if (ch == 'l') {
1577 1.1 itojun loadopt |= PFCTL_FLAG_TABLE;
1578 1.1 itojun tblcmdopt = NULL;
1579 1.1 itojun } else
1580 1.1 itojun mode = strchr("acdfkrz", ch) ? O_RDWR : O_RDONLY;
1581 1.1 itojun } else if (argc != optind) {
1582 1.1 itojun warnx("unknown command line argument: %s ...", argv[optind]);
1583 1.1 itojun usage();
1584 1.1 itojun /* NOTREACHED */
1585 1.1 itojun }
1586 1.1 itojun if (loadopt == 0)
1587 1.1 itojun loadopt = ~0;
1588 1.1 itojun
1589 1.1 itojun memset(anchorname, 0, sizeof(anchorname));
1590 1.1 itojun memset(rulesetname, 0, sizeof(rulesetname));
1591 1.1 itojun if (anchoropt != NULL) {
1592 1.1 itojun char *t;
1593 1.1 itojun
1594 1.1 itojun if ((t = strchr(anchoropt, ':')) == NULL) {
1595 1.1 itojun if (strlcpy(anchorname, anchoropt,
1596 1.1 itojun sizeof(anchorname)) >= sizeof(anchorname))
1597 1.1 itojun errx(1, "anchor name '%s' too long",
1598 1.1 itojun anchoropt);
1599 1.1 itojun } else {
1600 1.1 itojun char *p;
1601 1.1 itojun
1602 1.1 itojun if ((p = strdup(anchoropt)) == NULL)
1603 1.1 itojun err(1, "anchoropt: strdup");
1604 1.1 itojun t = strsep(&p, ":");
1605 1.1 itojun if (*t == '\0' || *p == '\0')
1606 1.1 itojun errx(1, "anchor '%s' invalid", anchoropt);
1607 1.1 itojun if (strlcpy(anchorname, t, sizeof(anchorname)) >=
1608 1.1 itojun sizeof(anchorname))
1609 1.1 itojun errx(1, "anchor name '%s' too long", t);
1610 1.1 itojun if (strlcpy(rulesetname, p, sizeof(rulesetname)) >=
1611 1.1 itojun sizeof(rulesetname))
1612 1.1 itojun errx(1, "ruleset name '%s' too long", p);
1613 1.1 itojun free(t); /* not p */
1614 1.1 itojun }
1615 1.1 itojun loadopt &= PFCTL_FLAG_FILTER|PFCTL_FLAG_NAT|PFCTL_FLAG_TABLE;
1616 1.1 itojun }
1617 1.1 itojun
1618 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0) {
1619 1.1 itojun dev = open(pf_device, mode);
1620 1.1 itojun if (dev == -1)
1621 1.1 itojun err(1, "%s", pf_device);
1622 1.1 itojun altqsupport = pfctl_test_altqsupport(dev, opts);
1623 1.1 itojun } else {
1624 1.1 itojun dev = open(pf_device, O_RDONLY);
1625 1.1 itojun if (dev >= 0)
1626 1.1 itojun opts |= PF_OPT_DUMMYACTION;
1627 1.1 itojun /* turn off options */
1628 1.1 itojun opts &= ~ (PF_OPT_DISABLE | PF_OPT_ENABLE);
1629 1.1 itojun clearopt = showopt = debugopt = NULL;
1630 1.1 itojun altqsupport = 1;
1631 1.1 itojun }
1632 1.1 itojun
1633 1.1 itojun if (opts & PF_OPT_DISABLE)
1634 1.1 itojun if (pfctl_disable(dev, opts))
1635 1.1 itojun error = 1;
1636 1.1 itojun
1637 1.1 itojun if (showopt != NULL) {
1638 1.1 itojun switch (*showopt) {
1639 1.1 itojun case 'A':
1640 1.1 itojun pfctl_show_anchors(dev, opts, anchorname);
1641 1.1 itojun break;
1642 1.1 itojun case 'r':
1643 1.1 itojun pfctl_load_fingerprints(dev, opts);
1644 1.1 itojun pfctl_show_rules(dev, opts, 0, anchorname,
1645 1.1 itojun rulesetname);
1646 1.1 itojun break;
1647 1.1 itojun case 'l':
1648 1.1 itojun pfctl_load_fingerprints(dev, opts);
1649 1.1 itojun pfctl_show_rules(dev, opts, 1, anchorname,
1650 1.1 itojun rulesetname);
1651 1.1 itojun break;
1652 1.1 itojun case 'n':
1653 1.1 itojun pfctl_load_fingerprints(dev, opts);
1654 1.1 itojun pfctl_show_nat(dev, opts, anchorname, rulesetname);
1655 1.1 itojun break;
1656 1.1 itojun case 'q':
1657 1.1 itojun pfctl_show_altq(dev, ifaceopt, opts,
1658 1.1 itojun opts & PF_OPT_VERBOSE2);
1659 1.1 itojun break;
1660 1.1 itojun case 's':
1661 1.1 itojun pfctl_show_states(dev, ifaceopt, opts);
1662 1.1 itojun break;
1663 1.1 itojun case 'S':
1664 1.1 itojun pfctl_show_src_nodes(dev, opts);
1665 1.1 itojun break;
1666 1.1 itojun case 'i':
1667 1.1 itojun pfctl_show_status(dev, opts);
1668 1.1 itojun break;
1669 1.1 itojun case 't':
1670 1.1 itojun pfctl_show_timeouts(dev, opts);
1671 1.1 itojun break;
1672 1.1 itojun case 'm':
1673 1.1 itojun pfctl_show_limits(dev, opts);
1674 1.1 itojun break;
1675 1.1 itojun case 'a':
1676 1.1 itojun opts |= PF_OPT_SHOWALL;
1677 1.1 itojun pfctl_load_fingerprints(dev, opts);
1678 1.1 itojun
1679 1.1 itojun pfctl_show_nat(dev, opts, anchorname, rulesetname);
1680 1.1 itojun pfctl_show_rules(dev, opts, 0, anchorname,
1681 1.1 itojun rulesetname);
1682 1.1 itojun pfctl_show_altq(dev, ifaceopt, opts, 0);
1683 1.1 itojun pfctl_show_states(dev, ifaceopt, opts);
1684 1.1 itojun pfctl_show_src_nodes(dev, opts);
1685 1.1 itojun pfctl_show_status(dev, opts);
1686 1.1 itojun pfctl_show_rules(dev, opts, 1, anchorname, rulesetname);
1687 1.1 itojun pfctl_show_timeouts(dev, opts);
1688 1.1 itojun pfctl_show_limits(dev, opts);
1689 1.1 itojun pfctl_show_tables(anchorname, rulesetname, opts);
1690 1.1 itojun pfctl_show_fingerprints(opts);
1691 1.1 itojun break;
1692 1.1 itojun case 'T':
1693 1.1 itojun pfctl_show_tables(anchorname, rulesetname, opts);
1694 1.1 itojun break;
1695 1.1 itojun case 'o':
1696 1.1 itojun pfctl_load_fingerprints(dev, opts);
1697 1.1 itojun pfctl_show_fingerprints(opts);
1698 1.1 itojun break;
1699 1.1 itojun case 'I':
1700 1.1 itojun pfctl_show_ifaces(ifaceopt, opts);
1701 1.1 itojun break;
1702 1.1 itojun }
1703 1.1 itojun }
1704 1.1 itojun
1705 1.1 itojun if (clearopt != NULL) {
1706 1.1 itojun switch (*clearopt) {
1707 1.1 itojun case 'r':
1708 1.1 itojun pfctl_clear_rules(dev, opts, anchorname, rulesetname);
1709 1.1 itojun break;
1710 1.1 itojun case 'n':
1711 1.1 itojun pfctl_clear_nat(dev, opts, anchorname, rulesetname);
1712 1.1 itojun break;
1713 1.1 itojun case 'q':
1714 1.1 itojun pfctl_clear_altq(dev, opts);
1715 1.1 itojun break;
1716 1.1 itojun case 's':
1717 1.1 itojun pfctl_clear_states(dev, ifaceopt, opts);
1718 1.1 itojun break;
1719 1.1 itojun case 'S':
1720 1.1 itojun pfctl_clear_src_nodes(dev, opts);
1721 1.1 itojun break;
1722 1.1 itojun case 'i':
1723 1.1 itojun pfctl_clear_stats(dev, opts);
1724 1.1 itojun break;
1725 1.1 itojun case 'a':
1726 1.1 itojun pfctl_clear_rules(dev, opts, anchorname, rulesetname);
1727 1.1 itojun pfctl_clear_nat(dev, opts, anchorname, rulesetname);
1728 1.1 itojun pfctl_clear_tables(anchorname, rulesetname, opts);
1729 1.1 itojun if (!*anchorname && !*rulesetname) {
1730 1.1 itojun pfctl_clear_altq(dev, opts);
1731 1.1 itojun pfctl_clear_states(dev, ifaceopt, opts);
1732 1.1 itojun pfctl_clear_src_nodes(dev, opts);
1733 1.1 itojun pfctl_clear_stats(dev, opts);
1734 1.1 itojun pfctl_clear_fingerprints(dev, opts);
1735 1.1 itojun }
1736 1.1 itojun break;
1737 1.1 itojun case 'o':
1738 1.1 itojun pfctl_clear_fingerprints(dev, opts);
1739 1.1 itojun break;
1740 1.1 itojun case 'T':
1741 1.1 itojun pfctl_clear_tables(anchorname, rulesetname, opts);
1742 1.1 itojun break;
1743 1.1 itojun }
1744 1.1 itojun }
1745 1.1 itojun if (state_killers)
1746 1.1 itojun pfctl_kill_states(dev, ifaceopt, opts);
1747 1.1 itojun
1748 1.1 itojun if (tblcmdopt != NULL) {
1749 1.1 itojun error = pfctl_command_tables(argc, argv, tableopt,
1750 1.1 itojun tblcmdopt, rulesopt, anchorname, rulesetname, opts);
1751 1.1 itojun rulesopt = NULL;
1752 1.1 itojun }
1753 1.1 itojun
1754 1.1 itojun if (rulesopt != NULL)
1755 1.1 itojun if (pfctl_file_fingerprints(dev, opts, PF_OSFP_FILE))
1756 1.1 itojun error = 1;
1757 1.1 itojun
1758 1.1 itojun if (rulesopt != NULL) {
1759 1.1 itojun if (pfctl_rules(dev, rulesopt, opts, anchorname, rulesetname,
1760 1.1 itojun NULL))
1761 1.1 itojun error = 1;
1762 1.1 itojun else if (!(opts & PF_OPT_NOACTION) &&
1763 1.1 itojun (loadopt & PFCTL_FLAG_TABLE))
1764 1.1 itojun warn_namespace_collision(NULL);
1765 1.1 itojun }
1766 1.1 itojun
1767 1.1 itojun if (opts & PF_OPT_ENABLE)
1768 1.1 itojun if (pfctl_enable(dev, opts))
1769 1.1 itojun error = 1;
1770 1.1 itojun
1771 1.1 itojun if (debugopt != NULL) {
1772 1.1 itojun switch (*debugopt) {
1773 1.1 itojun case 'n':
1774 1.1 itojun pfctl_debug(dev, PF_DEBUG_NONE, opts);
1775 1.1 itojun break;
1776 1.1 itojun case 'u':
1777 1.1 itojun pfctl_debug(dev, PF_DEBUG_URGENT, opts);
1778 1.1 itojun break;
1779 1.1 itojun case 'm':
1780 1.1 itojun pfctl_debug(dev, PF_DEBUG_MISC, opts);
1781 1.1 itojun break;
1782 1.1 itojun case 'l':
1783 1.1 itojun pfctl_debug(dev, PF_DEBUG_NOISY, opts);
1784 1.1 itojun break;
1785 1.1 itojun }
1786 1.1 itojun }
1787 1.1 itojun
1788 1.1 itojun if (opts & PF_OPT_CLRRULECTRS) {
1789 1.1 itojun if (pfctl_clear_rule_counters(dev, opts))
1790 1.1 itojun error = 1;
1791 1.1 itojun }
1792 1.1 itojun exit(error);
1793 1.1 itojun }
1794