1 /* $OpenBSD: pfctl_table.c,v 1.66 2007/03/01 17:20:54 deraadt Exp $ */
4 * Copyright (c) 2002 Cedric Berger
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
41 #include <net/pfvar.h>
42 #include <arpa/inet.h>
54 #include "pfctl_parser.h"
57 extern void usage(void);
58 static int pfctl_table(int, char *[], char *, const char *, char *,
60 static void print_table(struct pfr_table *, int, int);
61 static void print_tstats(struct pfr_tstats *, int);
62 static int load_addr(struct pfr_buffer *, int, char *[], char *, int);
63 static void print_addrx(struct pfr_addr *, struct pfr_addr *, int);
64 static void print_astats(struct pfr_astats *, int);
65 static void radix_perror(void);
66 static void xprintf(int, const char *, ...);
67 static void print_iface(struct pfi_kif *, int);
69 static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = {
70 { "In/Block:", "In/Pass:", "In/XPass:" },
71 { "Out/Block:", "Out/Pass:", "Out/XPass:" }
74 static const char *istats_text[2][2][2] = {
75 { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } },
76 { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } }
79 #define RVTEST(fct) do { \
80 if ((!(opts & PF_OPT_NOACTION) || \
81 (opts & PF_OPT_DUMMYACTION)) && \
88 #define CREATE_TABLE do { \
89 table.pfrt_flags |= PFR_TFLAG_PERSIST; \
90 if ((!(opts & PF_OPT_NOACTION) || \
91 (opts & PF_OPT_DUMMYACTION)) && \
92 (pfr_add_tables(&table, 1, &nadd, flags)) && \
98 warn_namespace_collision(table.pfrt_name); \
99 xprintf(opts, "%d table created", nadd); \
100 if (opts & PF_OPT_NOACTION) \
103 table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \
107 pfctl_clear_tables(const char *anchor, int opts)
109 return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts);
113 pfctl_show_tables(const char *anchor, int opts)
115 return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts);
119 pfctl_command_tables(int argc, char *argv[], char *tname,
120 const char *command, char *file, const char *anchor, int opts)
122 if (tname == NULL || command == NULL)
124 return pfctl_table(argc, argv, tname, command, file, anchor, opts);
128 pfctl_table(int argc, char *argv[], char *tname, const char *command,
129 char *file, const char *anchor, int opts)
131 struct pfr_table table;
132 struct pfr_buffer b, b2;
133 struct pfr_addr *a, *a2;
134 int nadd = 0, ndel = 0, nchange = 0, nzero = 0;
135 int rv = 0, flags = 0, nmatch = 0;
140 if (opts & PF_OPT_NOACTION)
141 flags |= PFR_FLAG_DUMMY;
143 bzero(&b, sizeof(b));
144 bzero(&b2, sizeof(b2));
145 bzero(&table, sizeof(table));
147 if (strlen(tname) >= PF_TABLE_NAME_SIZE)
149 if (strlcpy(table.pfrt_name, tname,
150 sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name))
151 errx(1, "pfctl_table: strlcpy");
153 if (strlcpy(table.pfrt_anchor, anchor,
154 sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor))
155 errx(1, "pfctl_table: strlcpy");
157 if (!strcmp(command, "-F")) {
158 if (argc || file != NULL)
160 RVTEST(pfr_clr_tables(&table, &ndel, flags));
161 xprintf(opts, "%d tables deleted", ndel);
162 } else if (!strcmp(command, "-s")) {
163 b.pfrb_type = (opts & PF_OPT_VERBOSE2) ?
164 PFRB_TSTATS : PFRB_TABLES;
165 if (argc || file != NULL)
168 pfr_buf_grow(&b, b.pfrb_size);
169 b.pfrb_size = b.pfrb_msize;
170 if (opts & PF_OPT_VERBOSE2)
171 RVTEST(pfr_get_tstats(&table,
172 b.pfrb_caddr, &b.pfrb_size, flags));
174 RVTEST(pfr_get_tables(&table,
175 b.pfrb_caddr, &b.pfrb_size, flags));
176 if (b.pfrb_size <= b.pfrb_msize)
180 if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0)
181 pfctl_print_title("TABLES:");
184 if (opts & PF_OPT_VERBOSE2)
185 print_tstats(p, opts & PF_OPT_DEBUG);
187 print_table(p, opts & PF_OPT_VERBOSE,
188 opts & PF_OPT_DEBUG);
189 } else if (!strcmp(command, "kill")) {
190 if (argc || file != NULL)
192 RVTEST(pfr_del_tables(&table, 1, &ndel, flags));
193 xprintf(opts, "%d table deleted", ndel);
194 } else if (!strcmp(command, "flush")) {
195 if (argc || file != NULL)
197 RVTEST(pfr_clr_addrs(&table, &ndel, flags));
198 xprintf(opts, "%d addresses deleted", ndel);
199 } else if (!strcmp(command, "add")) {
200 b.pfrb_type = PFRB_ADDRS;
201 if (load_addr(&b, argc, argv, file, 0))
204 if (opts & PF_OPT_VERBOSE)
205 flags |= PFR_FLAG_FEEDBACK;
206 RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size,
208 xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
209 if (opts & PF_OPT_VERBOSE)
211 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
213 opts & PF_OPT_USEDNS);
214 } else if (!strcmp(command, "delete")) {
215 b.pfrb_type = PFRB_ADDRS;
216 if (load_addr(&b, argc, argv, file, 0))
218 if (opts & PF_OPT_VERBOSE)
219 flags |= PFR_FLAG_FEEDBACK;
220 RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size,
222 xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size);
223 if (opts & PF_OPT_VERBOSE)
225 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
227 opts & PF_OPT_USEDNS);
228 } else if (!strcmp(command, "replace")) {
229 b.pfrb_type = PFRB_ADDRS;
230 if (load_addr(&b, argc, argv, file, 0))
233 if (opts & PF_OPT_VERBOSE)
234 flags |= PFR_FLAG_FEEDBACK;
236 int sz2 = b.pfrb_msize;
238 RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size,
239 &sz2, &nadd, &ndel, &nchange, flags));
240 if (sz2 <= b.pfrb_msize) {
244 pfr_buf_grow(&b, sz2);
247 xprintf(opts, "%d addresses added", nadd);
249 xprintf(opts, "%d addresses deleted", ndel);
251 xprintf(opts, "%d addresses changed", nchange);
252 if (!nadd && !ndel && !nchange)
253 xprintf(opts, "no changes");
254 if (opts & PF_OPT_VERBOSE)
256 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
258 opts & PF_OPT_USEDNS);
259 } else if (!strcmp(command, "expire")) {
263 b.pfrb_type = PFRB_ASTATS;
264 b2.pfrb_type = PFRB_ADDRS;
265 if (argc != 1 || file != NULL)
267 lifetime = strtonum(*argv, 0, UINT_MAX, &errstr);
269 errx(1, "expiry time: %s", errstr);
271 pfr_buf_grow(&b, b.pfrb_size);
272 b.pfrb_size = b.pfrb_msize;
273 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
274 &b.pfrb_size, flags));
275 if (b.pfrb_size <= b.pfrb_msize)
279 if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero >
282 &((struct pfr_astats *)p)->pfras_a))
283 err(1, "duplicate buffer");
285 if (opts & PF_OPT_VERBOSE)
286 flags |= PFR_FLAG_FEEDBACK;
287 RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size,
289 xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size);
290 if (opts & PF_OPT_VERBOSE)
292 if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
294 opts & PF_OPT_USEDNS);
295 } else if (!strcmp(command, "show")) {
296 b.pfrb_type = (opts & PF_OPT_VERBOSE) ?
297 PFRB_ASTATS : PFRB_ADDRS;
298 if (argc || file != NULL)
301 pfr_buf_grow(&b, b.pfrb_size);
302 b.pfrb_size = b.pfrb_msize;
303 if (opts & PF_OPT_VERBOSE)
304 RVTEST(pfr_get_astats(&table, b.pfrb_caddr,
305 &b.pfrb_size, flags));
307 RVTEST(pfr_get_addrs(&table, b.pfrb_caddr,
308 &b.pfrb_size, flags));
309 if (b.pfrb_size <= b.pfrb_msize)
313 if (opts & PF_OPT_VERBOSE)
314 print_astats(p, opts & PF_OPT_USEDNS);
316 print_addrx(p, NULL, opts & PF_OPT_USEDNS);
317 } else if (!strcmp(command, "test")) {
318 b.pfrb_type = PFRB_ADDRS;
319 b2.pfrb_type = PFRB_ADDRS;
321 if (load_addr(&b, argc, argv, file, 1))
323 if (opts & PF_OPT_VERBOSE2) {
324 flags |= PFR_FLAG_REPLACE;
326 if (pfr_buf_add(&b2, a))
327 err(1, "duplicate buffer");
329 RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size,
331 xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size);
332 if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2))
334 if (a->pfra_fback == PFR_FB_MATCH)
336 opts & PF_OPT_USEDNS);
337 if (opts & PF_OPT_VERBOSE2) {
339 PFRB_FOREACH(a, &b) {
340 a2 = pfr_buf_next(&b2, a2);
341 print_addrx(a2, a, opts & PF_OPT_USEDNS);
344 if (nmatch < b.pfrb_size)
346 } else if (!strcmp(command, "zero")) {
347 if (argc || file != NULL)
349 flags |= PFR_FLAG_ADDRSTOO;
350 RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags));
351 xprintf(opts, "%d table/stats cleared", nzero);
353 warnx("pfctl_table: unknown command '%s'", command);
365 print_table(struct pfr_table *ta, int verbose, int debug)
367 if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
370 printf("%c%c%c%c%c%c\t%s",
371 (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
372 (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
373 (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
374 (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
375 (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
376 (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
378 if (ta->pfrt_anchor[0])
379 printf("\t%s", ta->pfrt_anchor);
386 print_tstats(struct pfr_tstats *ts, int debug)
388 time_t time = ts->pfrts_tzero;
391 if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE))
393 print_table(&ts->pfrts_t, 1, debug);
394 printf("\tAddresses: %d\n", ts->pfrts_cnt);
395 printf("\tCleared: %s", ctime(&time));
396 printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n",
397 ts->pfrts_refcnt[PFR_REFCNT_ANCHOR],
398 ts->pfrts_refcnt[PFR_REFCNT_RULE]);
399 printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n",
400 (unsigned long long)ts->pfrts_nomatch,
401 (unsigned long long)ts->pfrts_match);
402 for (dir = 0; dir < PFR_DIR_MAX; dir++)
403 for (op = 0; op < PFR_OP_TABLE_MAX; op++)
404 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
406 (unsigned long long)ts->pfrts_packets[dir][op],
407 (unsigned long long)ts->pfrts_bytes[dir][op]);
411 load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
415 if (append_addr(b, *argv++, nonetwork)) {
417 warn("cannot decode %s", argv[-1]);
420 if (pfr_buf_load(b, file, nonetwork, append_addr)) {
421 warn("cannot load %s", file);
428 print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns)
430 char ch, buf[256] = "{error}";
431 char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y' };
432 unsigned int fback, hostnet;
434 fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback;
435 ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?';
436 hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32;
437 inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf));
438 printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf);
439 if (ad->pfra_net < hostnet)
440 printf("/%d", ad->pfra_net);
441 if (rad != NULL && fback != PFR_FB_NONE) {
442 if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf))
443 errx(1, "print_addrx: strlcpy");
444 inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf));
445 printf("\t%c%s", (rad->pfra_not?'!':' '), buf);
446 if (rad->pfra_net < hostnet)
447 printf("/%d", rad->pfra_net);
449 if (rad != NULL && fback == PFR_FB_NONE)
450 printf("\t nomatch");
451 if (dns && ad->pfra_net == hostnet) {
452 char host[NI_MAXHOST];
453 union sockaddr_union sa;
455 strlcpy(host, "?", sizeof(host));
456 bzero(&sa, sizeof(sa));
457 sa.sa.sa_family = ad->pfra_af;
458 if (sa.sa.sa_family == AF_INET) {
459 sa.sa.sa_len = sizeof(sa.sin);
460 sa.sin.sin_addr = ad->pfra_ip4addr;
462 sa.sa.sa_len = sizeof(sa.sin6);
463 sa.sin6.sin6_addr = ad->pfra_ip6addr;
465 if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host),
466 NULL, 0, NI_NAMEREQD) == 0)
467 printf("\t(%s)", host);
473 print_astats(struct pfr_astats *as, int dns)
475 time_t time = as->pfras_tzero;
478 print_addrx(&as->pfras_a, NULL, dns);
479 printf("\tCleared: %s", ctime(&time));
480 for (dir = 0; dir < PFR_DIR_MAX; dir++)
481 for (op = 0; op < PFR_OP_ADDR_MAX; op++)
482 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
484 (unsigned long long)as->pfras_packets[dir][op],
485 (unsigned long long)as->pfras_bytes[dir][op]);
491 extern char *__progname;
492 fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno));
496 pfctl_define_table(char *name, int flags, int addrs, const char *anchor,
497 struct pfr_buffer *ab, u_int32_t ticket)
499 struct pfr_table tbl;
501 bzero(&tbl, sizeof(tbl));
502 if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >=
503 sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor,
504 sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor))
505 errx(1, "pfctl_define_table: strlcpy");
506 tbl.pfrt_flags = flags;
508 return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL,
509 NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0);
513 warn_namespace_collision(const char *filter)
517 const char *name = NULL, *lastcoll;
520 bzero(&b, sizeof(b));
521 b.pfrb_type = PFRB_TABLES;
523 pfr_buf_grow(&b, b.pfrb_size);
524 b.pfrb_size = b.pfrb_msize;
525 if (pfr_get_tables(NULL, b.pfrb_caddr,
526 &b.pfrb_size, PFR_FLAG_ALLRSETS))
527 err(1, "pfr_get_tables");
528 if (b.pfrb_size <= b.pfrb_msize)
531 PFRB_FOREACH(t, &b) {
532 if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE))
534 if (filter != NULL && strcmp(filter, t->pfrt_name))
536 if (!t->pfrt_anchor[0])
538 else if (name != NULL && !strcmp(name, t->pfrt_name)) {
545 warnx("warning: namespace collision with <%s> global table.",
548 warnx("warning: namespace collisions with %d global tables.",
554 xprintf(int opts, const char *fmt, ...)
558 if (opts & PF_OPT_QUIET)
562 vfprintf(stderr, fmt, args);
565 if (opts & PF_OPT_DUMMYACTION)
566 fprintf(stderr, " (dummy).\n");
567 else if (opts & PF_OPT_NOACTION)
568 fprintf(stderr, " (syntax only).\n");
570 fprintf(stderr, ".\n");
574 /* interface stuff */
577 pfctl_show_ifaces(const char *filter, int opts)
583 bzero(&b, sizeof(b));
584 b.pfrb_type = PFRB_IFACES;
586 pfr_buf_grow(&b, b.pfrb_size);
587 b.pfrb_size = b.pfrb_msize;
588 if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) {
592 if (b.pfrb_size <= b.pfrb_msize)
596 if (opts & PF_OPT_SHOWALL)
597 pfctl_print_title("INTERFACES:");
599 print_iface(p, opts);
604 print_iface(struct pfi_kif *p, int opts)
606 time_t tzero = p->pfik_tzero;
609 printf("%s", p->pfik_name);
610 if (opts & PF_OPT_VERBOSE) {
611 if (p->pfik_flags & PFI_IFLAG_SKIP)
616 if (!(opts & PF_OPT_VERBOSE2))
618 printf("\tCleared: %s", ctime(&tzero));
619 printf("\tReferences: [ States: %-18d Rules: %-18d ]\n",
620 p->pfik_states, p->pfik_rules);
621 for (i = 0; i < 8; i++) {
625 printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n",
626 istats_text[af][dir][act],
627 (unsigned long long)p->pfik_packets[af][dir][act],
628 (unsigned long long)p->pfik_bytes[af][dir][act]);