pfctl_table.c revision 1.3 1 1.3 dsl /* $NetBSD: pfctl_table.c,v 1.3 2004/10/29 19:46:27 dsl Exp $ */
2 1.1 itojun /* $OpenBSD: pfctl_table.c,v 1.59 2004/03/15 15:25:44 dhartmei Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2002 Cedric Berger
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.2 itojun #ifdef __NetBSD__
39 1.2 itojun #include <netinet/in.h>
40 1.2 itojun #endif
41 1.2 itojun
42 1.1 itojun #include <net/if.h>
43 1.1 itojun #include <net/pfvar.h>
44 1.1 itojun #include <arpa/inet.h>
45 1.1 itojun
46 1.1 itojun #include <ctype.h>
47 1.1 itojun #include <err.h>
48 1.1 itojun #include <errno.h>
49 1.1 itojun #include <netdb.h>
50 1.1 itojun #include <stdarg.h>
51 1.1 itojun #include <stdio.h>
52 1.1 itojun #include <stdlib.h>
53 1.1 itojun #include <string.h>
54 1.1 itojun #include <time.h>
55 1.1 itojun
56 1.1 itojun #include "pfctl_parser.h"
57 1.1 itojun #include "pfctl.h"
58 1.1 itojun
59 1.1 itojun extern void usage(void);
60 1.1 itojun static int pfctl_table(int, char *[], char *, const char *, char *,
61 1.1 itojun const char *, const char *, int);
62 1.1 itojun static void print_table(struct pfr_table *, int, int);
63 1.1 itojun static void print_tstats(struct pfr_tstats *, int);
64 1.1 itojun static int load_addr(struct pfr_buffer *, int, char *[], char *, int);
65 1.1 itojun static void print_addrx(struct pfr_addr *, struct pfr_addr *, int);
66 1.1 itojun static void print_astats(struct pfr_astats *, int);
67 1.1 itojun static void radix_perror(void);
68 1.1 itojun static void xprintf(int, const char *, ...);
69 1.1 itojun static void print_iface(struct pfi_if *, int);
70 1.1 itojun static void oprintf(int, int, const char *, int *, int);
71 1.1 itojun
72 1.1 itojun static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = {
73 1.1 itojun { "In/Block:", "In/Pass:", "In/XPass:" },
74 1.1 itojun { "Out/Block:", "Out/Pass:", "Out/XPass:" }
75 1.1 itojun };
76 1.1 itojun
77 1.1 itojun static const char *istats_text[2][2][2] = {
78 1.1 itojun { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } },
79 1.1 itojun { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } }
80 1.1 itojun };
81 1.1 itojun
82 1.1 itojun #define RVTEST(fct) do { \
83 1.1 itojun if ((!(opts & PF_OPT_NOACTION) || \
84 1.1 itojun (opts & PF_OPT_DUMMYACTION)) && \
85 1.1 itojun (fct)) { \
86 1.1 itojun radix_perror(); \
87 1.1 itojun goto _error; \
88 1.1 itojun } \
89 1.1 itojun } while (0)
90 1.1 itojun
91 1.1 itojun #define CREATE_TABLE do { \
92 1.1 itojun table.pfrt_flags |= PFR_TFLAG_PERSIST; \
93 1.1 itojun RVTEST(pfr_add_tables(&table, 1, &nadd, flags)); \
94 1.1 itojun if (nadd) { \
95 1.1 itojun warn_namespace_collision(table.pfrt_name); \
96 1.1 itojun xprintf(opts, "%d table created", nadd); \
97 1.1 itojun if (opts & PF_OPT_NOACTION) \
98 1.1 itojun return (0); \
99 1.1 itojun } \
100 1.1 itojun table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \
101 1.1 itojun } while(0)
102 1.1 itojun
103 1.1 itojun int
104 1.1 itojun pfctl_clear_tables(const char *anchor, const char *ruleset, int opts)
105 1.1 itojun {
106 1.1 itojun return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, ruleset, opts);
107 1.1 itojun }
108 1.1 itojun
109 1.1 itojun int
110 1.1 itojun pfctl_show_tables(const char *anchor, const char *ruleset, int opts)
111 1.1 itojun {
112 1.1 itojun return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, ruleset, opts);
113 1.1 itojun }
114 1.1 itojun
115 1.1 itojun int
116 1.1 itojun pfctl_command_tables(int argc, char *argv[], char *tname,
117 1.1 itojun const char *command, char *file, const char *anchor, const char *ruleset,
118 1.1 itojun int opts)
119 1.1 itojun {
120 1.1 itojun if (tname == NULL || command == NULL)
121 1.1 itojun usage();
122 1.1 itojun return pfctl_table(argc, argv, tname, command, file, anchor, ruleset,
123 1.1 itojun opts);
124 1.1 itojun }
125 1.1 itojun
126 1.1 itojun int
127 1.1 itojun pfctl_table(int argc, char *argv[], char *tname, const char *command,
128 1.1 itojun char *file, const char *anchor, const char *ruleset, int opts)
129 1.1 itojun {
130 1.1 itojun struct pfr_table table;
131 1.1 itojun struct pfr_buffer b, b2;
132 1.1 itojun struct pfr_addr *a, *a2;
133 1.1 itojun int nadd = 0, ndel = 0, nchange = 0, nzero = 0;
134 1.1 itojun int rv = 0, flags = 0, nmatch = 0;
135 1.1 itojun void *p;
136 1.1 itojun
137 1.1 itojun if (command == NULL)
138 1.1 itojun usage();
139 1.1 itojun if (opts & PF_OPT_NOACTION)
140 1.1 itojun flags |= PFR_FLAG_DUMMY;
141 1.1 itojun
142 1.1 itojun bzero(&b, sizeof(b));
143 1.1 itojun bzero(&b2, sizeof(b2));
144 1.1 itojun bzero(&table, sizeof(table));
145 1.1 itojun if (tname != NULL) {
146 1.1 itojun if (strlen(tname) >= PF_TABLE_NAME_SIZE)
147 1.1 itojun usage();
148 1.1 itojun if (strlcpy(table.pfrt_name, tname,
149 1.1 itojun sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
150 1.1 itojun errx(1, "pfctl_table: strlcpy");
151 1.1 itojun }
152 1.1 itojun if (strlcpy(table.pfrt_anchor, anchor,
153 1.1 itojun sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor) ||
154 1.1 itojun strlcpy(table.pfrt_ruleset, ruleset,
155 1.1 itojun sizeof(table.pfrt_ruleset)) >= sizeof(table.pfrt_ruleset))
156 1.1 itojun errx(1, "pfctl_table: strlcpy");
157 1.1 itojun
158 1.1 itojun if (!strcmp(command, "-F")) {
159 1.1 itojun if (argc || file != NULL)
160 1.1 itojun usage();
161 1.1 itojun RVTEST(pfr_clr_tables(&table, &ndel, flags));
162 1.1 itojun xprintf(opts, "%d tables deleted", ndel);
163 1.1 itojun } else if (!strcmp(command, "-s")) {
164 1.1 itojun b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
165 1.1 itojun PFRB_TSTATS : PFRB_TABLES;
166 1.1 itojun if (argc || file != NULL)
167 1.1 itojun usage();
168 1.1 itojun for (;;) {
169 1.1 itojun pfr_buf_grow(&b, b.pfrb_size);
170 1.1 itojun b.pfrb_size = b.pfrb_msize;
171 1.1 itojun if (opts & PF_OPT_VERBOSE2)
172 1.1 itojun RVTEST(pfr_get_tstats(&table,
173 1.1 itojun b.pfrb_caddr, &b.pfrb_size, flags));
174 1.1 itojun else
175 1.1 itojun RVTEST(pfr_get_tables(&table,
176 1.1 itojun b.pfrb_caddr, &b.pfrb_size, flags));
177 1.1 itojun if (b.pfrb_size <= b.pfrb_msize)
178 1.1 itojun break;
179 1.1 itojun }
180 1.1 itojun
181 1.1 itojun if (opts & PF_OPT_SHOWALL && b.pfrb_size > 0)
182 1.1 itojun pfctl_print_title("TABLES:");
183 1.1 itojun
184 1.1 itojun PFRB_FOREACH(p, &b)
185 1.1 itojun if (opts & PF_OPT_VERBOSE2)
186 1.1 itojun print_tstats(p, opts & PF_OPT_DEBUG);
187 1.1 itojun else
188 1.1 itojun print_table(p, opts & PF_OPT_VERBOSE,
189 1.1 itojun opts & PF_OPT_DEBUG);
190 1.1 itojun } else if (!strcmp(command, "kill")) {
191 1.1 itojun if (argc || file != NULL)
192 1.1 itojun usage();
193 1.1 itojun RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
194 1.1 itojun xprintf(opts, "%d table deleted", ndel);
195 1.1 itojun } else if (!strcmp(command, "flush")) {
196 1.1 itojun if (argc || file != NULL)
197 1.1 itojun usage();
198 1.1 itojun RVTEST(pfr_clr_addrs(&table, &ndel, flags));
199 1.1 itojun xprintf(opts, "%d addresses deleted", ndel);
200 1.1 itojun } else if (!strcmp(command, "add")) {
201 1.1 itojun b.pfrb_type = PFRB_ADDRS;
202 1.1 itojun if (load_addr(&b, argc, argv, file, 0))
203 1.1 itojun goto _error;
204 1.1 itojun CREATE_TABLE;
205 1.1 itojun if (opts & PF_OPT_VERBOSE)
206 1.1 itojun flags |= PFR_FLAG_FEEDBACK;
207 1.1 itojun RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
208 1.1 itojun &nadd, flags));
209 1.1 itojun xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
210 1.1 itojun if (opts & PF_OPT_VERBOSE)
211 1.1 itojun PFRB_FOREACH(a, &b)
212 1.1 itojun if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
213 1.1 itojun print_addrx(a, NULL,
214 1.1 itojun opts & PF_OPT_USEDNS);
215 1.1 itojun } else if (!strcmp(command, "delete")) {
216 1.1 itojun b.pfrb_type = PFRB_ADDRS;
217 1.1 itojun if (load_addr(&b, argc, argv, file, 0))
218 1.1 itojun goto _error;
219 1.1 itojun if (opts & PF_OPT_VERBOSE)
220 1.1 itojun flags |= PFR_FLAG_FEEDBACK;
221 1.1 itojun RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
222 1.1 itojun &ndel, flags));
223 1.1 itojun xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
224 1.1 itojun if (opts & PF_OPT_VERBOSE)
225 1.1 itojun PFRB_FOREACH(a, &b)
226 1.1 itojun if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
227 1.1 itojun print_addrx(a, NULL,
228 1.1 itojun opts & PF_OPT_USEDNS);
229 1.1 itojun } else if (!strcmp(command, "replace")) {
230 1.1 itojun b.pfrb_type = PFRB_ADDRS;
231 1.1 itojun if (load_addr(&b, argc, argv, file, 0))
232 1.1 itojun goto _error;
233 1.1 itojun CREATE_TABLE;
234 1.1 itojun if (opts & PF_OPT_VERBOSE)
235 1.1 itojun flags |= PFR_FLAG_FEEDBACK;
236 1.1 itojun for (;;) {
237 1.1 itojun int sz2 = b.pfrb_msize;
238 1.1 itojun
239 1.1 itojun RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
240 1.1 itojun &sz2, &nadd, &ndel, &nchange, flags));
241 1.1 itojun if (sz2 <= b.pfrb_msize) {
242 1.1 itojun b.pfrb_size = sz2;
243 1.1 itojun break;
244 1.1 itojun } else
245 1.1 itojun pfr_buf_grow(&b, sz2);
246 1.1 itojun }
247 1.1 itojun if (nadd)
248 1.1 itojun xprintf(opts, "%d addresses added", nadd);
249 1.1 itojun if (ndel)
250 1.1 itojun xprintf(opts, "%d addresses deleted", ndel);
251 1.1 itojun if (nchange)
252 1.1 itojun xprintf(opts, "%d addresses changed", nchange);
253 1.1 itojun if (!nadd && !ndel && !nchange)
254 1.1 itojun xprintf(opts, "no changes");
255 1.1 itojun if (opts & PF_OPT_VERBOSE)
256 1.1 itojun PFRB_FOREACH(a, &b)
257 1.1 itojun if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
258 1.1 itojun print_addrx(a, NULL,
259 1.1 itojun opts & PF_OPT_USEDNS);
260 1.1 itojun } else if (!strcmp(command, "show")) {
261 1.1 itojun b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
262 1.1 itojun PFRB_ASTATS : PFRB_ADDRS;
263 1.1 itojun if (argc || file != NULL)
264 1.1 itojun usage();
265 1.1 itojun for (;;) {
266 1.1 itojun pfr_buf_grow(&b, b.pfrb_size);
267 1.1 itojun b.pfrb_size = b.pfrb_msize;
268 1.1 itojun if (opts & PF_OPT_VERBOSE)
269 1.1 itojun RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
270 1.1 itojun &b.pfrb_size, flags));
271 1.1 itojun else
272 1.1 itojun RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
273 1.1 itojun &b.pfrb_size, flags));
274 1.1 itojun if (b.pfrb_size <= b.pfrb_msize)
275 1.1 itojun break;
276 1.1 itojun }
277 1.1 itojun PFRB_FOREACH(p, &b)
278 1.1 itojun if (opts & PF_OPT_VERBOSE)
279 1.1 itojun print_astats(p, opts & PF_OPT_USEDNS);
280 1.1 itojun else
281 1.1 itojun print_addrx(p, NULL, opts & PF_OPT_USEDNS);
282 1.1 itojun } else if (!strcmp(command, "test")) {
283 1.1 itojun b.pfrb_type = PFRB_ADDRS;
284 1.1 itojun b2.pfrb_type = PFRB_ADDRS;
285 1.1 itojun
286 1.1 itojun if (load_addr(&b, argc, argv, file, 1))
287 1.1 itojun goto _error;
288 1.1 itojun if (opts & PF_OPT_VERBOSE2) {
289 1.1 itojun flags |= PFR_FLAG_REPLACE;
290 1.1 itojun PFRB_FOREACH(a, &b)
291 1.1 itojun if (pfr_buf_add(&b2, a))
292 1.1 itojun err(1, "duplicate buffer");
293 1.1 itojun }
294 1.1 itojun RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
295 1.1 itojun &nmatch, flags));
296 1.1 itojun xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
297 1.1 itojun if (opts & PF_OPT_VERBOSE && !(opts & PF_OPT_VERBOSE2))
298 1.1 itojun PFRB_FOREACH(a, &b)
299 1.1 itojun if (a->pfra_fback == PFR_FB_MATCH)
300 1.1 itojun print_addrx(a, NULL,
301 1.1 itojun opts & PF_OPT_USEDNS);
302 1.1 itojun if (opts & PF_OPT_VERBOSE2) {
303 1.1 itojun a2 = NULL;
304 1.1 itojun PFRB_FOREACH(a, &b) {
305 1.1 itojun a2 = pfr_buf_next(&b2, a2);
306 1.1 itojun print_addrx(a2, a, opts & PF_OPT_USEDNS);
307 1.1 itojun }
308 1.1 itojun }
309 1.1 itojun if (nmatch < b.pfrb_size)
310 1.1 itojun rv = 2;
311 1.1 itojun } else if (!strcmp(command, "zero")) {
312 1.1 itojun if (argc || file != NULL)
313 1.1 itojun usage();
314 1.1 itojun flags |= PFR_FLAG_ADDRSTOO;
315 1.1 itojun RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
316 1.1 itojun xprintf(opts, "%d table/stats cleared", nzero);
317 1.1 itojun } else
318 1.1 itojun warnx("pfctl_table: unknown command '%s'", command);
319 1.1 itojun goto _cleanup;
320 1.1 itojun
321 1.1 itojun _error:
322 1.1 itojun rv = -1;
323 1.1 itojun _cleanup:
324 1.1 itojun pfr_buf_clear(&b);
325 1.1 itojun pfr_buf_clear(&b2);
326 1.1 itojun return (rv);
327 1.1 itojun }
328 1.1 itojun
329 1.1 itojun void
330 1.1 itojun print_table(struct pfr_table *ta, int verbose, int debug)
331 1.1 itojun {
332 1.1 itojun if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
333 1.1 itojun return;
334 1.1 itojun if (verbose) {
335 1.1 itojun printf("%c%c%c%c%c%c\t%s",
336 1.1 itojun (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
337 1.1 itojun (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
338 1.1 itojun (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
339 1.1 itojun (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
340 1.1 itojun (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
341 1.1 itojun (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
342 1.1 itojun ta->pfrt_name);
343 1.1 itojun if (ta->pfrt_anchor[0])
344 1.1 itojun printf("\t%s", ta->pfrt_anchor);
345 1.1 itojun if (ta->pfrt_ruleset[0])
346 1.1 itojun printf(":%s", ta->pfrt_ruleset);
347 1.1 itojun puts("");
348 1.1 itojun } else
349 1.1 itojun puts(ta->pfrt_name);
350 1.1 itojun }
351 1.1 itojun
352 1.1 itojun void
353 1.1 itojun print_tstats(struct pfr_tstats *ts, int debug)
354 1.1 itojun {
355 1.1 itojun time_t time = ts->pfrts_tzero;
356 1.1 itojun int dir, op;
357 1.1 itojun
358 1.1 itojun if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
359 1.1 itojun return;
360 1.1 itojun print_table(&ts->pfrts_t, 1, debug);
361 1.1 itojun printf("\tAddresses: %d\n", ts->pfrts_cnt);
362 1.1 itojun printf("\tCleared: %s", ctime(&time));
363 1.1 itojun printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
364 1.1 itojun ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
365 1.1 itojun ts->pfrts_refcnt[PFR_REFCNT_RULE]);
366 1.1 itojun printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
367 1.1 itojun (unsigned long long)ts->pfrts_nomatch,
368 1.1 itojun (unsigned long long)ts->pfrts_match);
369 1.1 itojun for (dir = 0; dir < PFR_DIR_MAX; dir++)
370 1.1 itojun for (op = 0; op < PFR_OP_TABLE_MAX; op++)
371 1.1 itojun printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
372 1.1 itojun stats_text[dir][op],
373 1.1 itojun (unsigned long long)ts->pfrts_packets[dir][op],
374 1.1 itojun (unsigned long long)ts->pfrts_bytes[dir][op]);
375 1.1 itojun }
376 1.1 itojun
377 1.1 itojun int
378 1.1 itojun load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
379 1.1 itojun int nonetwork)
380 1.1 itojun {
381 1.1 itojun while (argc--)
382 1.1 itojun if (append_addr(b, *argv++, nonetwork)) {
383 1.1 itojun if (errno)
384 1.1 itojun warn("cannot decode %s", argv[-1]);
385 1.1 itojun return (-1);
386 1.1 itojun }
387 1.1 itojun if (pfr_buf_load(b, file, nonetwork, append_addr)) {
388 1.1 itojun warn("cannot load %s", file);
389 1.1 itojun return (-1);
390 1.1 itojun }
391 1.1 itojun return (0);
392 1.1 itojun }
393 1.1 itojun
394 1.1 itojun void
395 1.1 itojun print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
396 1.1 itojun {
397 1.1 itojun char ch, buf[256] = "{error}";
398 1.1 itojun char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y' };
399 1.1 itojun unsigned int fback, hostnet;
400 1.1 itojun
401 1.1 itojun fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
402 1.1 itojun ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
403 1.1 itojun hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
404 1.1 itojun inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
405 1.1 itojun printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
406 1.1 itojun if (ad->pfra_net < hostnet)
407 1.1 itojun printf("/%d", ad->pfra_net);
408 1.1 itojun if (rad != NULL && fback != PFR_FB_NONE) {
409 1.1 itojun if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
410 1.1 itojun errx(1, "print_addrx: strlcpy");
411 1.1 itojun inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
412 1.1 itojun printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
413 1.1 itojun if (rad->pfra_net < hostnet)
414 1.1 itojun printf("/%d", rad->pfra_net);
415 1.1 itojun }
416 1.1 itojun if (rad != NULL && fback == PFR_FB_NONE)
417 1.1 itojun printf("\t nomatch");
418 1.1 itojun if (dns && ad->pfra_net == hostnet) {
419 1.1 itojun char host[NI_MAXHOST];
420 1.1 itojun union sockaddr_union sa;
421 1.1 itojun
422 1.1 itojun strlcpy(host, "?", sizeof(host));
423 1.1 itojun bzero(&sa, sizeof(sa));
424 1.1 itojun sa.sa.sa_family = ad->pfra_af;
425 1.1 itojun if (sa.sa.sa_family == AF_INET) {
426 1.1 itojun sa.sa.sa_len = sizeof(sa.sin);
427 1.1 itojun sa.sin.sin_addr = ad->pfra_ip4addr;
428 1.1 itojun } else {
429 1.1 itojun sa.sa.sa_len = sizeof(sa.sin6);
430 1.1 itojun sa.sin6.sin6_addr = ad->pfra_ip6addr;
431 1.1 itojun }
432 1.1 itojun if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host),
433 1.1 itojun NULL, 0, NI_NAMEREQD) == 0)
434 1.1 itojun printf("\t(%s)", host);
435 1.1 itojun }
436 1.1 itojun printf("\n");
437 1.1 itojun }
438 1.1 itojun
439 1.1 itojun void
440 1.1 itojun print_astats(struct pfr_astats *as, int dns)
441 1.1 itojun {
442 1.1 itojun time_t time = as->pfras_tzero;
443 1.1 itojun int dir, op;
444 1.1 itojun
445 1.1 itojun print_addrx(&as->pfras_a, NULL, dns);
446 1.1 itojun printf("\tCleared: %s", ctime(&time));
447 1.1 itojun for (dir = 0; dir < PFR_DIR_MAX; dir++)
448 1.1 itojun for (op = 0; op < PFR_OP_ADDR_MAX; op++)
449 1.1 itojun printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
450 1.1 itojun stats_text[dir][op],
451 1.1 itojun (unsigned long long)as->pfras_packets[dir][op],
452 1.1 itojun (unsigned long long)as->pfras_bytes[dir][op]);
453 1.1 itojun }
454 1.1 itojun
455 1.1 itojun void
456 1.1 itojun radix_perror(void)
457 1.1 itojun {
458 1.1 itojun extern char *__progname;
459 1.1 itojun fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
460 1.1 itojun }
461 1.1 itojun
462 1.1 itojun int
463 1.1 itojun pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
464 1.1 itojun const char *ruleset, struct pfr_buffer *ab, u_int32_t ticket)
465 1.1 itojun {
466 1.1 itojun struct pfr_table tbl;
467 1.1 itojun
468 1.1 itojun bzero(&tbl, sizeof(tbl));
469 1.1 itojun if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
470 1.1 itojun sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
471 1.1 itojun sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor) ||
472 1.1 itojun strlcpy(tbl.pfrt_ruleset, ruleset, sizeof(tbl.pfrt_ruleset)) >=
473 1.1 itojun sizeof(tbl.pfrt_ruleset))
474 1.1 itojun errx(1, "pfctl_define_table: strlcpy");
475 1.1 itojun tbl.pfrt_flags = flags;
476 1.1 itojun
477 1.1 itojun return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
478 1.1 itojun NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
479 1.1 itojun }
480 1.1 itojun
481 1.1 itojun void
482 1.1 itojun warn_namespace_collision(const char *filter)
483 1.1 itojun {
484 1.1 itojun struct pfr_buffer b;
485 1.1 itojun struct pfr_table *t;
486 1.1 itojun const char *name = NULL, *lastcoll;
487 1.1 itojun int coll = 0;
488 1.1 itojun
489 1.1 itojun bzero(&b, sizeof(b));
490 1.1 itojun b.pfrb_type = PFRB_TABLES;
491 1.1 itojun for (;;) {
492 1.1 itojun pfr_buf_grow(&b, b.pfrb_size);
493 1.1 itojun b.pfrb_size = b.pfrb_msize;
494 1.1 itojun if (pfr_get_tables(NULL, b.pfrb_caddr,
495 1.1 itojun &b.pfrb_size, PFR_FLAG_ALLRSETS))
496 1.1 itojun err(1, "pfr_get_tables");
497 1.1 itojun if (b.pfrb_size <= b.pfrb_msize)
498 1.1 itojun break;
499 1.1 itojun }
500 1.1 itojun PFRB_FOREACH(t, &b) {
501 1.1 itojun if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
502 1.1 itojun continue;
503 1.1 itojun if (filter != NULL && strcmp(filter, t->pfrt_name))
504 1.1 itojun continue;
505 1.1 itojun if (!t->pfrt_anchor[0])
506 1.1 itojun name = t->pfrt_name;
507 1.1 itojun else if (name != NULL && !strcmp(name, t->pfrt_name)) {
508 1.1 itojun coll++;
509 1.1 itojun lastcoll = name;
510 1.1 itojun name = NULL;
511 1.1 itojun }
512 1.1 itojun }
513 1.1 itojun if (coll == 1)
514 1.1 itojun warnx("warning: namespace collision with <%s> global table.",
515 1.1 itojun lastcoll);
516 1.1 itojun else if (coll > 1)
517 1.1 itojun warnx("warning: namespace collisions with %d global tables.",
518 1.1 itojun coll);
519 1.1 itojun pfr_buf_clear(&b);
520 1.1 itojun }
521 1.1 itojun
522 1.1 itojun void
523 1.1 itojun xprintf(int opts, const char *fmt, ...)
524 1.1 itojun {
525 1.1 itojun va_list args;
526 1.1 itojun
527 1.1 itojun if (opts & PF_OPT_QUIET)
528 1.1 itojun return;
529 1.1 itojun
530 1.1 itojun va_start(args, fmt);
531 1.1 itojun vfprintf(stderr, fmt, args);
532 1.1 itojun va_end(args);
533 1.1 itojun
534 1.1 itojun if (opts & PF_OPT_DUMMYACTION)
535 1.1 itojun fprintf(stderr, " (dummy).\n");
536 1.1 itojun else if (opts & PF_OPT_NOACTION)
537 1.1 itojun fprintf(stderr, " (syntax only).\n");
538 1.1 itojun else
539 1.1 itojun fprintf(stderr, ".\n");
540 1.1 itojun }
541 1.1 itojun
542 1.1 itojun
543 1.1 itojun /* interface stuff */
544 1.1 itojun
545 1.1 itojun int
546 1.1 itojun pfctl_show_ifaces(const char *filter, int opts)
547 1.1 itojun {
548 1.1 itojun struct pfr_buffer b;
549 1.1 itojun struct pfi_if *p;
550 1.1 itojun int i = 0, f = PFI_FLAG_GROUP|PFI_FLAG_INSTANCE;
551 1.1 itojun
552 1.3 dsl if (filter != NULL && *filter && !isdigit((unsigned char)filter[strlen(filter)-1]))
553 1.1 itojun f &= ~PFI_FLAG_INSTANCE;
554 1.1 itojun bzero(&b, sizeof(b));
555 1.1 itojun b.pfrb_type = PFRB_IFACES;
556 1.1 itojun for (;;) {
557 1.1 itojun pfr_buf_grow(&b, b.pfrb_size);
558 1.1 itojun b.pfrb_size = b.pfrb_msize;
559 1.1 itojun if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size, f)) {
560 1.1 itojun radix_perror();
561 1.1 itojun return (1);
562 1.1 itojun }
563 1.1 itojun if (b.pfrb_size <= b.pfrb_msize)
564 1.1 itojun break;
565 1.1 itojun i++;
566 1.1 itojun }
567 1.1 itojun if (opts & PF_OPT_SHOWALL)
568 1.1 itojun pfctl_print_title("INTERFACES:");
569 1.1 itojun PFRB_FOREACH(p, &b)
570 1.1 itojun print_iface(p, opts);
571 1.1 itojun return (0);
572 1.1 itojun }
573 1.1 itojun
574 1.1 itojun void
575 1.1 itojun print_iface(struct pfi_if *p, int opts)
576 1.1 itojun {
577 1.1 itojun time_t tzero = p->pfif_tzero;
578 1.1 itojun int flags = (opts & PF_OPT_VERBOSE) ? p->pfif_flags : 0;
579 1.1 itojun int first = 1;
580 1.1 itojun int i, af, dir, act;
581 1.1 itojun
582 1.1 itojun printf("%s", p->pfif_name);
583 1.1 itojun oprintf(flags, PFI_IFLAG_INSTANCE, "instance", &first, 0);
584 1.1 itojun oprintf(flags, PFI_IFLAG_GROUP, "group", &first, 0);
585 1.1 itojun oprintf(flags, PFI_IFLAG_CLONABLE, "clonable", &first, 0);
586 1.1 itojun oprintf(flags, PFI_IFLAG_DYNAMIC, "dynamic", &first, 0);
587 1.1 itojun oprintf(flags, PFI_IFLAG_ATTACHED, "attached", &first, 1);
588 1.1 itojun printf("\n");
589 1.1 itojun
590 1.1 itojun if (!(opts & PF_OPT_VERBOSE2))
591 1.1 itojun return;
592 1.1 itojun printf("\tCleared: %s", ctime(&tzero));
593 1.1 itojun printf("\tReferences: [ States: %-18d Rules: %-18d ]\n",
594 1.1 itojun p->pfif_states, p->pfif_rules);
595 1.1 itojun for (i = 0; i < 8; i++) {
596 1.1 itojun af = (i>>2) & 1;
597 1.1 itojun dir = (i>>1) &1;
598 1.1 itojun act = i & 1;
599 1.1 itojun printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
600 1.1 itojun istats_text[af][dir][act],
601 1.1 itojun (unsigned long long)p->pfif_packets[af][dir][act],
602 1.1 itojun (unsigned long long)p->pfif_bytes[af][dir][act]);
603 1.1 itojun }
604 1.1 itojun }
605 1.1 itojun
606 1.1 itojun void
607 1.1 itojun oprintf(int flags, int flag, const char *s, int *first, int last)
608 1.1 itojun {
609 1.1 itojun if (flags & flag) {
610 1.1 itojun printf(*first ? "\t(%s" : ", %s", s);
611 1.1 itojun *first = 0;
612 1.1 itojun }
613 1.1 itojun if (last && !*first)
614 1.1 itojun printf(")");
615 1.1 itojun }
616 1.1 itojun
617