13b3a8eb9SGleb Smirnoff /* $OpenBSD: pfctl_table.c,v 1.67 2008/06/10 20:55:02 mcbride Exp $ */ 23b3a8eb9SGleb Smirnoff 31de7b4b8SPedro F. Giffuni /*- 41de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause 51de7b4b8SPedro F. Giffuni * 63b3a8eb9SGleb Smirnoff * Copyright (c) 2002 Cedric Berger 73b3a8eb9SGleb Smirnoff * All rights reserved. 83b3a8eb9SGleb Smirnoff * 93b3a8eb9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 103b3a8eb9SGleb Smirnoff * modification, are permitted provided that the following conditions 113b3a8eb9SGleb Smirnoff * are met: 123b3a8eb9SGleb Smirnoff * 133b3a8eb9SGleb Smirnoff * - Redistributions of source code must retain the above copyright 143b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 153b3a8eb9SGleb Smirnoff * - Redistributions in binary form must reproduce the above 163b3a8eb9SGleb Smirnoff * copyright notice, this list of conditions and the following 173b3a8eb9SGleb Smirnoff * disclaimer in the documentation and/or other materials provided 183b3a8eb9SGleb Smirnoff * with the distribution. 193b3a8eb9SGleb Smirnoff * 203b3a8eb9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 213b3a8eb9SGleb Smirnoff * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223b3a8eb9SGleb Smirnoff * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233b3a8eb9SGleb Smirnoff * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 243b3a8eb9SGleb Smirnoff * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253b3a8eb9SGleb Smirnoff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263b3a8eb9SGleb Smirnoff * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 273b3a8eb9SGleb Smirnoff * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 283b3a8eb9SGleb Smirnoff * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 293b3a8eb9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 303b3a8eb9SGleb Smirnoff * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 313b3a8eb9SGleb Smirnoff * POSSIBILITY OF SUCH DAMAGE. 323b3a8eb9SGleb Smirnoff * 333b3a8eb9SGleb Smirnoff */ 343b3a8eb9SGleb Smirnoff 353b3a8eb9SGleb Smirnoff #include <sys/types.h> 363b3a8eb9SGleb Smirnoff #include <sys/ioctl.h> 373b3a8eb9SGleb Smirnoff #include <sys/socket.h> 383b3a8eb9SGleb Smirnoff 393b3a8eb9SGleb Smirnoff #include <net/if.h> 403b3a8eb9SGleb Smirnoff #include <net/pfvar.h> 413b3a8eb9SGleb Smirnoff #include <arpa/inet.h> 423b3a8eb9SGleb Smirnoff 433b3a8eb9SGleb Smirnoff #include <ctype.h> 443b3a8eb9SGleb Smirnoff #include <err.h> 453b3a8eb9SGleb Smirnoff #include <errno.h> 463b3a8eb9SGleb Smirnoff #include <netdb.h> 473b3a8eb9SGleb Smirnoff #include <stdarg.h> 483b3a8eb9SGleb Smirnoff #include <stdio.h> 493b3a8eb9SGleb Smirnoff #include <stdlib.h> 503b3a8eb9SGleb Smirnoff #include <string.h> 513b3a8eb9SGleb Smirnoff #include <time.h> 523b3a8eb9SGleb Smirnoff 533b3a8eb9SGleb Smirnoff #include "pfctl_parser.h" 543b3a8eb9SGleb Smirnoff #include "pfctl.h" 553b3a8eb9SGleb Smirnoff 563b3a8eb9SGleb Smirnoff extern void usage(void); 573b3a8eb9SGleb Smirnoff static int pfctl_table(int, char *[], char *, const char *, char *, 583b3a8eb9SGleb Smirnoff const char *, int); 593b3a8eb9SGleb Smirnoff static void print_table(struct pfr_table *, int, int); 603b3a8eb9SGleb Smirnoff static void print_tstats(struct pfr_tstats *, int); 613b3a8eb9SGleb Smirnoff static int load_addr(struct pfr_buffer *, int, char *[], char *, int); 623b3a8eb9SGleb Smirnoff static void print_addrx(struct pfr_addr *, struct pfr_addr *, int); 635b59b0c6SLeonid Evdokimov static int nonzero_astats(struct pfr_astats *); 643b3a8eb9SGleb Smirnoff static void print_astats(struct pfr_astats *, int); 653b3a8eb9SGleb Smirnoff static void radix_perror(void); 663b3a8eb9SGleb Smirnoff static void xprintf(int, const char *, ...); 673b3a8eb9SGleb Smirnoff static void print_iface(struct pfi_kif *, int); 683b3a8eb9SGleb Smirnoff 693b3a8eb9SGleb Smirnoff static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = { 703b3a8eb9SGleb Smirnoff { "In/Block:", "In/Pass:", "In/XPass:" }, 713b3a8eb9SGleb Smirnoff { "Out/Block:", "Out/Pass:", "Out/XPass:" } 723b3a8eb9SGleb Smirnoff }; 733b3a8eb9SGleb Smirnoff 743b3a8eb9SGleb Smirnoff static const char *istats_text[2][2][2] = { 753b3a8eb9SGleb Smirnoff { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } }, 763b3a8eb9SGleb Smirnoff { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } } 773b3a8eb9SGleb Smirnoff }; 783b3a8eb9SGleb Smirnoff 793b3a8eb9SGleb Smirnoff #define RVTEST(fct) do { \ 803b3a8eb9SGleb Smirnoff if ((!(opts & PF_OPT_NOACTION) || \ 813b3a8eb9SGleb Smirnoff (opts & PF_OPT_DUMMYACTION)) && \ 823b3a8eb9SGleb Smirnoff (fct)) { \ 833b3a8eb9SGleb Smirnoff radix_perror(); \ 843b3a8eb9SGleb Smirnoff goto _error; \ 853b3a8eb9SGleb Smirnoff } \ 863b3a8eb9SGleb Smirnoff } while (0) 873b3a8eb9SGleb Smirnoff 883b3a8eb9SGleb Smirnoff #define CREATE_TABLE do { \ 893b3a8eb9SGleb Smirnoff table.pfrt_flags |= PFR_TFLAG_PERSIST; \ 903b3a8eb9SGleb Smirnoff if ((!(opts & PF_OPT_NOACTION) || \ 913b3a8eb9SGleb Smirnoff (opts & PF_OPT_DUMMYACTION)) && \ 923b3a8eb9SGleb Smirnoff (pfr_add_tables(&table, 1, &nadd, flags)) && \ 933b3a8eb9SGleb Smirnoff (errno != EPERM)) { \ 943b3a8eb9SGleb Smirnoff radix_perror(); \ 953b3a8eb9SGleb Smirnoff goto _error; \ 963b3a8eb9SGleb Smirnoff } \ 973b3a8eb9SGleb Smirnoff if (nadd) { \ 983b3a8eb9SGleb Smirnoff warn_namespace_collision(table.pfrt_name); \ 993b3a8eb9SGleb Smirnoff xprintf(opts, "%d table created", nadd); \ 1003b3a8eb9SGleb Smirnoff if (opts & PF_OPT_NOACTION) \ 1013b3a8eb9SGleb Smirnoff return (0); \ 1023b3a8eb9SGleb Smirnoff } \ 1033b3a8eb9SGleb Smirnoff table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \ 1043b3a8eb9SGleb Smirnoff } while(0) 1053b3a8eb9SGleb Smirnoff 1063b3a8eb9SGleb Smirnoff int 107*441d4894SKristof Provost pfctl_do_clear_tables(const char *anchor, int opts) 1083b3a8eb9SGleb Smirnoff { 1093b3a8eb9SGleb Smirnoff return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts); 1103b3a8eb9SGleb Smirnoff } 1113b3a8eb9SGleb Smirnoff 1123b3a8eb9SGleb Smirnoff int 1133b3a8eb9SGleb Smirnoff pfctl_show_tables(const char *anchor, int opts) 1143b3a8eb9SGleb Smirnoff { 1153b3a8eb9SGleb Smirnoff return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts); 1163b3a8eb9SGleb Smirnoff } 1173b3a8eb9SGleb Smirnoff 1183b3a8eb9SGleb Smirnoff int 1193b3a8eb9SGleb Smirnoff pfctl_command_tables(int argc, char *argv[], char *tname, 1203b3a8eb9SGleb Smirnoff const char *command, char *file, const char *anchor, int opts) 1213b3a8eb9SGleb Smirnoff { 1223b3a8eb9SGleb Smirnoff if (tname == NULL || command == NULL) 1233b3a8eb9SGleb Smirnoff usage(); 1243b3a8eb9SGleb Smirnoff return pfctl_table(argc, argv, tname, command, file, anchor, opts); 1253b3a8eb9SGleb Smirnoff } 1263b3a8eb9SGleb Smirnoff 1273b3a8eb9SGleb Smirnoff int 1283b3a8eb9SGleb Smirnoff pfctl_table(int argc, char *argv[], char *tname, const char *command, 1293b3a8eb9SGleb Smirnoff char *file, const char *anchor, int opts) 1303b3a8eb9SGleb Smirnoff { 1313b3a8eb9SGleb Smirnoff struct pfr_table table; 1323b3a8eb9SGleb Smirnoff struct pfr_buffer b, b2; 1333b3a8eb9SGleb Smirnoff struct pfr_addr *a, *a2; 1343b3a8eb9SGleb Smirnoff int nadd = 0, ndel = 0, nchange = 0, nzero = 0; 1353b3a8eb9SGleb Smirnoff int rv = 0, flags = 0, nmatch = 0; 1363b3a8eb9SGleb Smirnoff void *p; 1373b3a8eb9SGleb Smirnoff 1383b3a8eb9SGleb Smirnoff if (command == NULL) 1393b3a8eb9SGleb Smirnoff usage(); 1403b3a8eb9SGleb Smirnoff if (opts & PF_OPT_NOACTION) 1413b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_DUMMY; 1423b3a8eb9SGleb Smirnoff 1433b3a8eb9SGleb Smirnoff bzero(&b, sizeof(b)); 1443b3a8eb9SGleb Smirnoff bzero(&b2, sizeof(b2)); 1453b3a8eb9SGleb Smirnoff bzero(&table, sizeof(table)); 1463b3a8eb9SGleb Smirnoff if (tname != NULL) { 1473b3a8eb9SGleb Smirnoff if (strlen(tname) >= PF_TABLE_NAME_SIZE) 1483b3a8eb9SGleb Smirnoff usage(); 1493b3a8eb9SGleb Smirnoff if (strlcpy(table.pfrt_name, tname, 1503b3a8eb9SGleb Smirnoff sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name)) 1513b3a8eb9SGleb Smirnoff errx(1, "pfctl_table: strlcpy"); 1523b3a8eb9SGleb Smirnoff } 1533b3a8eb9SGleb Smirnoff if (strlcpy(table.pfrt_anchor, anchor, 1543b3a8eb9SGleb Smirnoff sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor)) 1553b3a8eb9SGleb Smirnoff errx(1, "pfctl_table: strlcpy"); 1563b3a8eb9SGleb Smirnoff 1573b3a8eb9SGleb Smirnoff if (!strcmp(command, "-F")) { 1583b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1593b3a8eb9SGleb Smirnoff usage(); 160*441d4894SKristof Provost RVTEST(pfctl_clear_tables(pfh, &table, &ndel, flags)); 1613b3a8eb9SGleb Smirnoff xprintf(opts, "%d tables deleted", ndel); 1623b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "-s")) { 1633b3a8eb9SGleb Smirnoff b.pfrb_type = (opts & PF_OPT_VERBOSE2) ? 1643b3a8eb9SGleb Smirnoff PFRB_TSTATS : PFRB_TABLES; 1653b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1663b3a8eb9SGleb Smirnoff usage(); 1673b3a8eb9SGleb Smirnoff for (;;) { 1683b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 1693b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 1703b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) 1713b3a8eb9SGleb Smirnoff RVTEST(pfr_get_tstats(&table, 1723b3a8eb9SGleb Smirnoff b.pfrb_caddr, &b.pfrb_size, flags)); 1733b3a8eb9SGleb Smirnoff else 1743b3a8eb9SGleb Smirnoff RVTEST(pfr_get_tables(&table, 1753b3a8eb9SGleb Smirnoff b.pfrb_caddr, &b.pfrb_size, flags)); 1763b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 1773b3a8eb9SGleb Smirnoff break; 1783b3a8eb9SGleb Smirnoff } 1793b3a8eb9SGleb Smirnoff 1803b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0) 1813b3a8eb9SGleb Smirnoff pfctl_print_title("TABLES:"); 1823b3a8eb9SGleb Smirnoff 1833b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) 1843b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) 1853b3a8eb9SGleb Smirnoff print_tstats(p, opts & PF_OPT_DEBUG); 1863b3a8eb9SGleb Smirnoff else 1873b3a8eb9SGleb Smirnoff print_table(p, opts & PF_OPT_VERBOSE, 1883b3a8eb9SGleb Smirnoff opts & PF_OPT_DEBUG); 1893b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "kill")) { 1903b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1913b3a8eb9SGleb Smirnoff usage(); 1923b3a8eb9SGleb Smirnoff RVTEST(pfr_del_tables(&table, 1, &ndel, flags)); 1933b3a8eb9SGleb Smirnoff xprintf(opts, "%d table deleted", ndel); 1943b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "flush")) { 1953b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1963b3a8eb9SGleb Smirnoff usage(); 1973b3a8eb9SGleb Smirnoff RVTEST(pfr_clr_addrs(&table, &ndel, flags)); 1983b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses deleted", ndel); 1993b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "add")) { 2003b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 2013b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 0)) 2023b3a8eb9SGleb Smirnoff goto _error; 2033b3a8eb9SGleb Smirnoff CREATE_TABLE; 2043b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2053b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2063b3a8eb9SGleb Smirnoff RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size, 2073b3a8eb9SGleb Smirnoff &nadd, flags)); 2083b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size); 2093b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2103b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 2113b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2123b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2133b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2143b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "delete")) { 2153b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 2163b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 0)) 2173b3a8eb9SGleb Smirnoff goto _error; 2183b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2193b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2203b3a8eb9SGleb Smirnoff RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size, 2213b3a8eb9SGleb Smirnoff &ndel, flags)); 2223b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size); 2233b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2243b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 2253b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2263b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2273b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2283b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "replace")) { 2293b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 2303b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 0)) 2313b3a8eb9SGleb Smirnoff goto _error; 2323b3a8eb9SGleb Smirnoff CREATE_TABLE; 2333b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2343b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2353b3a8eb9SGleb Smirnoff for (;;) { 2363b3a8eb9SGleb Smirnoff int sz2 = b.pfrb_msize; 2373b3a8eb9SGleb Smirnoff 2383b3a8eb9SGleb Smirnoff RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size, 2393b3a8eb9SGleb Smirnoff &sz2, &nadd, &ndel, &nchange, flags)); 2403b3a8eb9SGleb Smirnoff if (sz2 <= b.pfrb_msize) { 2413b3a8eb9SGleb Smirnoff b.pfrb_size = sz2; 2423b3a8eb9SGleb Smirnoff break; 2433b3a8eb9SGleb Smirnoff } else 2443b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, sz2); 2453b3a8eb9SGleb Smirnoff } 2463b3a8eb9SGleb Smirnoff if (nadd) 2473b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses added", nadd); 2483b3a8eb9SGleb Smirnoff if (ndel) 2493b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses deleted", ndel); 2503b3a8eb9SGleb Smirnoff if (nchange) 2513b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses changed", nchange); 2523b3a8eb9SGleb Smirnoff if (!nadd && !ndel && !nchange) 2533b3a8eb9SGleb Smirnoff xprintf(opts, "no changes"); 2543b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2553b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 2563b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2573b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2583b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2593b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "expire")) { 2603b3a8eb9SGleb Smirnoff const char *errstr; 2613b3a8eb9SGleb Smirnoff u_int lifetime; 2623b3a8eb9SGleb Smirnoff 2633b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ASTATS; 2643b3a8eb9SGleb Smirnoff b2.pfrb_type = PFRB_ADDRS; 2653b3a8eb9SGleb Smirnoff if (argc != 1 || file != NULL) 2663b3a8eb9SGleb Smirnoff usage(); 2673b3a8eb9SGleb Smirnoff lifetime = strtonum(*argv, 0, UINT_MAX, &errstr); 2683b3a8eb9SGleb Smirnoff if (errstr) 2693b3a8eb9SGleb Smirnoff errx(1, "expiry time: %s", errstr); 2703b3a8eb9SGleb Smirnoff for (;;) { 2713b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 2723b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 2733b3a8eb9SGleb Smirnoff RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 2743b3a8eb9SGleb Smirnoff &b.pfrb_size, flags)); 2753b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 2763b3a8eb9SGleb Smirnoff break; 2773b3a8eb9SGleb Smirnoff } 2783b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) { 2793b3a8eb9SGleb Smirnoff ((struct pfr_astats *)p)->pfras_a.pfra_fback = 0; 2803b3a8eb9SGleb Smirnoff if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero > 2813b3a8eb9SGleb Smirnoff lifetime) 2823b3a8eb9SGleb Smirnoff if (pfr_buf_add(&b2, 2833b3a8eb9SGleb Smirnoff &((struct pfr_astats *)p)->pfras_a)) 2843b3a8eb9SGleb Smirnoff err(1, "duplicate buffer"); 2853b3a8eb9SGleb Smirnoff } 2863b3a8eb9SGleb Smirnoff 2873b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2883b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2893b3a8eb9SGleb Smirnoff RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size, 2903b3a8eb9SGleb Smirnoff &ndel, flags)); 2913b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size); 2923b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2933b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b2) 2943b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2953b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2963b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2975b59b0c6SLeonid Evdokimov } else if (!strcmp(command, "reset")) { 2985b59b0c6SLeonid Evdokimov struct pfr_astats *as; 2995b59b0c6SLeonid Evdokimov 3005b59b0c6SLeonid Evdokimov b.pfrb_type = PFRB_ASTATS; 3015b59b0c6SLeonid Evdokimov b2.pfrb_type = PFRB_ADDRS; 3025b59b0c6SLeonid Evdokimov if (argc || file != NULL) 3035b59b0c6SLeonid Evdokimov usage(); 3045b59b0c6SLeonid Evdokimov do { 3055b59b0c6SLeonid Evdokimov pfr_buf_grow(&b, b.pfrb_size); 3065b59b0c6SLeonid Evdokimov b.pfrb_size = b.pfrb_msize; 3075b59b0c6SLeonid Evdokimov RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 3085b59b0c6SLeonid Evdokimov &b.pfrb_size, flags)); 3095b59b0c6SLeonid Evdokimov } while (b.pfrb_size > b.pfrb_msize); 3105b59b0c6SLeonid Evdokimov PFRB_FOREACH(as, &b) { 3115b59b0c6SLeonid Evdokimov as->pfras_a.pfra_fback = 0; 3125b59b0c6SLeonid Evdokimov if (nonzero_astats(as)) 3135b59b0c6SLeonid Evdokimov if (pfr_buf_add(&b2, &as->pfras_a)) 3145b59b0c6SLeonid Evdokimov err(1, "duplicate buffer"); 3155b59b0c6SLeonid Evdokimov } 3165b59b0c6SLeonid Evdokimov 3175b59b0c6SLeonid Evdokimov if (opts & PF_OPT_VERBOSE) 3185b59b0c6SLeonid Evdokimov flags |= PFR_FLAG_FEEDBACK; 3195b59b0c6SLeonid Evdokimov RVTEST(pfr_clr_astats(&table, b2.pfrb_caddr, b2.pfrb_size, 3205b59b0c6SLeonid Evdokimov &nzero, flags)); 3215b59b0c6SLeonid Evdokimov xprintf(opts, "%d/%d stats cleared", nzero, b.pfrb_size); 3225b59b0c6SLeonid Evdokimov if (opts & PF_OPT_VERBOSE) 3235b59b0c6SLeonid Evdokimov PFRB_FOREACH(a, &b2) 3245b59b0c6SLeonid Evdokimov if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 3255b59b0c6SLeonid Evdokimov print_addrx(a, NULL, 3265b59b0c6SLeonid Evdokimov opts & PF_OPT_USEDNS); 3273b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "show")) { 3283b3a8eb9SGleb Smirnoff b.pfrb_type = (opts & PF_OPT_VERBOSE) ? 3293b3a8eb9SGleb Smirnoff PFRB_ASTATS : PFRB_ADDRS; 3303b3a8eb9SGleb Smirnoff if (argc || file != NULL) 3313b3a8eb9SGleb Smirnoff usage(); 3323b3a8eb9SGleb Smirnoff for (;;) { 3333b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 3343b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 3353b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 3363b3a8eb9SGleb Smirnoff RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 3373b3a8eb9SGleb Smirnoff &b.pfrb_size, flags)); 3383b3a8eb9SGleb Smirnoff else 3393b3a8eb9SGleb Smirnoff RVTEST(pfr_get_addrs(&table, b.pfrb_caddr, 3403b3a8eb9SGleb Smirnoff &b.pfrb_size, flags)); 3413b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 3423b3a8eb9SGleb Smirnoff break; 3433b3a8eb9SGleb Smirnoff } 3443b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) 3453b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 3463b3a8eb9SGleb Smirnoff print_astats(p, opts & PF_OPT_USEDNS); 3473b3a8eb9SGleb Smirnoff else 3483b3a8eb9SGleb Smirnoff print_addrx(p, NULL, opts & PF_OPT_USEDNS); 3493b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "test")) { 3503b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 3513b3a8eb9SGleb Smirnoff b2.pfrb_type = PFRB_ADDRS; 3523b3a8eb9SGleb Smirnoff 3533b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 1)) 3543b3a8eb9SGleb Smirnoff goto _error; 3553b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) { 3563b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_REPLACE; 3573b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 3583b3a8eb9SGleb Smirnoff if (pfr_buf_add(&b2, a)) 3593b3a8eb9SGleb Smirnoff err(1, "duplicate buffer"); 3603b3a8eb9SGleb Smirnoff } 3613b3a8eb9SGleb Smirnoff RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size, 3623b3a8eb9SGleb Smirnoff &nmatch, flags)); 3633b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size); 3643b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2)) 3653b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 3663b3a8eb9SGleb Smirnoff if (a->pfra_fback == PFR_FB_MATCH) 3673b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 3683b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 3693b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) { 3703b3a8eb9SGleb Smirnoff a2 = NULL; 3713b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) { 3723b3a8eb9SGleb Smirnoff a2 = pfr_buf_next(&b2, a2); 3733b3a8eb9SGleb Smirnoff print_addrx(a2, a, opts & PF_OPT_USEDNS); 3743b3a8eb9SGleb Smirnoff } 3753b3a8eb9SGleb Smirnoff } 3763b3a8eb9SGleb Smirnoff if (nmatch < b.pfrb_size) 3773b3a8eb9SGleb Smirnoff rv = 2; 3786463b6b5SKristof Provost } else if (!strcmp(command, "zero") && (argc || file != NULL)) { 3796463b6b5SKristof Provost b.pfrb_type = PFRB_ADDRS; 3806463b6b5SKristof Provost if (load_addr(&b, argc, argv, file, 0)) 3816463b6b5SKristof Provost goto _error; 3826463b6b5SKristof Provost if (opts & PF_OPT_VERBOSE) 3836463b6b5SKristof Provost flags |= PFR_FLAG_FEEDBACK; 3846463b6b5SKristof Provost RVTEST(pfr_clr_astats(&table, b.pfrb_caddr, b.pfrb_size, 3856463b6b5SKristof Provost &nzero, flags)); 3866463b6b5SKristof Provost xprintf(opts, "%d/%d addresses cleared", nzero, b.pfrb_size); 3876463b6b5SKristof Provost if (opts & PF_OPT_VERBOSE) 3886463b6b5SKristof Provost PFRB_FOREACH(a, &b) 3896463b6b5SKristof Provost if (opts & PF_OPT_VERBOSE2 || 3906463b6b5SKristof Provost a->pfra_fback != PFR_FB_NONE) 3916463b6b5SKristof Provost print_addrx(a, NULL, 3926463b6b5SKristof Provost opts & PF_OPT_USEDNS); 3933b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "zero")) { 3943b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_ADDRSTOO; 3953b3a8eb9SGleb Smirnoff RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags)); 3963b3a8eb9SGleb Smirnoff xprintf(opts, "%d table/stats cleared", nzero); 3973b3a8eb9SGleb Smirnoff } else 3983b3a8eb9SGleb Smirnoff warnx("pfctl_table: unknown command '%s'", command); 3993b3a8eb9SGleb Smirnoff goto _cleanup; 4003b3a8eb9SGleb Smirnoff 4013b3a8eb9SGleb Smirnoff _error: 4023b3a8eb9SGleb Smirnoff rv = -1; 4033b3a8eb9SGleb Smirnoff _cleanup: 4043b3a8eb9SGleb Smirnoff pfr_buf_clear(&b); 4053b3a8eb9SGleb Smirnoff pfr_buf_clear(&b2); 4063b3a8eb9SGleb Smirnoff return (rv); 4073b3a8eb9SGleb Smirnoff } 4083b3a8eb9SGleb Smirnoff 4093b3a8eb9SGleb Smirnoff void 4103b3a8eb9SGleb Smirnoff print_table(struct pfr_table *ta, int verbose, int debug) 4113b3a8eb9SGleb Smirnoff { 4123b3a8eb9SGleb Smirnoff if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE)) 4133b3a8eb9SGleb Smirnoff return; 4143b3a8eb9SGleb Smirnoff if (verbose) { 4153b3a8eb9SGleb Smirnoff printf("%c%c%c%c%c%c%c\t%s", 4163b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-', 4173b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-', 4183b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-', 4193b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-', 4203b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-', 4213b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-', 4223b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-', 4233b3a8eb9SGleb Smirnoff ta->pfrt_name); 4243b3a8eb9SGleb Smirnoff if (ta->pfrt_anchor[0]) 4253b3a8eb9SGleb Smirnoff printf("\t%s", ta->pfrt_anchor); 4263b3a8eb9SGleb Smirnoff puts(""); 4273b3a8eb9SGleb Smirnoff } else 4283b3a8eb9SGleb Smirnoff puts(ta->pfrt_name); 4293b3a8eb9SGleb Smirnoff } 4303b3a8eb9SGleb Smirnoff 4313b3a8eb9SGleb Smirnoff void 4323b3a8eb9SGleb Smirnoff print_tstats(struct pfr_tstats *ts, int debug) 4333b3a8eb9SGleb Smirnoff { 4343b3a8eb9SGleb Smirnoff time_t time = ts->pfrts_tzero; 4353b3a8eb9SGleb Smirnoff int dir, op; 4363b3a8eb9SGleb Smirnoff 4373b3a8eb9SGleb Smirnoff if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE)) 4383b3a8eb9SGleb Smirnoff return; 4393b3a8eb9SGleb Smirnoff print_table(&ts->pfrts_t, 1, debug); 4403b3a8eb9SGleb Smirnoff printf("\tAddresses: %d\n", ts->pfrts_cnt); 4413b3a8eb9SGleb Smirnoff printf("\tCleared: %s", ctime(&time)); 4423b3a8eb9SGleb Smirnoff printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n", 4433b3a8eb9SGleb Smirnoff ts->pfrts_refcnt[PFR_REFCNT_ANCHOR], 4443b3a8eb9SGleb Smirnoff ts->pfrts_refcnt[PFR_REFCNT_RULE]); 4453b3a8eb9SGleb Smirnoff printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n", 4463b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_nomatch, 4473b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_match); 4483b3a8eb9SGleb Smirnoff for (dir = 0; dir < PFR_DIR_MAX; dir++) 4493b3a8eb9SGleb Smirnoff for (op = 0; op < PFR_OP_TABLE_MAX; op++) 4503b3a8eb9SGleb Smirnoff printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 4513b3a8eb9SGleb Smirnoff stats_text[dir][op], 4523b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_packets[dir][op], 4533b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_bytes[dir][op]); 4543b3a8eb9SGleb Smirnoff } 4553b3a8eb9SGleb Smirnoff 4563b3a8eb9SGleb Smirnoff int 4573b3a8eb9SGleb Smirnoff load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file, 4583b3a8eb9SGleb Smirnoff int nonetwork) 4593b3a8eb9SGleb Smirnoff { 4603b3a8eb9SGleb Smirnoff while (argc--) 4613b3a8eb9SGleb Smirnoff if (append_addr(b, *argv++, nonetwork)) { 4623b3a8eb9SGleb Smirnoff if (errno) 4633b3a8eb9SGleb Smirnoff warn("cannot decode %s", argv[-1]); 4643b3a8eb9SGleb Smirnoff return (-1); 4653b3a8eb9SGleb Smirnoff } 4663b3a8eb9SGleb Smirnoff if (pfr_buf_load(b, file, nonetwork, append_addr)) { 4673b3a8eb9SGleb Smirnoff warn("cannot load %s", file); 4683b3a8eb9SGleb Smirnoff return (-1); 4693b3a8eb9SGleb Smirnoff } 4703b3a8eb9SGleb Smirnoff return (0); 4713b3a8eb9SGleb Smirnoff } 4723b3a8eb9SGleb Smirnoff 4733b3a8eb9SGleb Smirnoff void 4743b3a8eb9SGleb Smirnoff print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns) 4753b3a8eb9SGleb Smirnoff { 4763b3a8eb9SGleb Smirnoff char ch, buf[256] = "{error}"; 4773b3a8eb9SGleb Smirnoff char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' }; 4783b3a8eb9SGleb Smirnoff unsigned int fback, hostnet; 4793b3a8eb9SGleb Smirnoff 4803b3a8eb9SGleb Smirnoff fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback; 4813b3a8eb9SGleb Smirnoff ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?'; 4823b3a8eb9SGleb Smirnoff hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32; 4833b3a8eb9SGleb Smirnoff inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf)); 4843b3a8eb9SGleb Smirnoff printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf); 4853b3a8eb9SGleb Smirnoff if (ad->pfra_net < hostnet) 4863b3a8eb9SGleb Smirnoff printf("/%d", ad->pfra_net); 4873b3a8eb9SGleb Smirnoff if (rad != NULL && fback != PFR_FB_NONE) { 4883b3a8eb9SGleb Smirnoff if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf)) 4893b3a8eb9SGleb Smirnoff errx(1, "print_addrx: strlcpy"); 4903b3a8eb9SGleb Smirnoff inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf)); 4913b3a8eb9SGleb Smirnoff printf("\t%c%s", (rad->pfra_not?'!':' '), buf); 4923b3a8eb9SGleb Smirnoff if (rad->pfra_net < hostnet) 4933b3a8eb9SGleb Smirnoff printf("/%d", rad->pfra_net); 4943b3a8eb9SGleb Smirnoff } 4953b3a8eb9SGleb Smirnoff if (rad != NULL && fback == PFR_FB_NONE) 4963b3a8eb9SGleb Smirnoff printf("\t nomatch"); 4973b3a8eb9SGleb Smirnoff if (dns && ad->pfra_net == hostnet) { 4983b3a8eb9SGleb Smirnoff char host[NI_MAXHOST]; 4993b3a8eb9SGleb Smirnoff union sockaddr_union sa; 5003b3a8eb9SGleb Smirnoff 5013b3a8eb9SGleb Smirnoff strlcpy(host, "?", sizeof(host)); 5023b3a8eb9SGleb Smirnoff bzero(&sa, sizeof(sa)); 5033b3a8eb9SGleb Smirnoff sa.sa.sa_family = ad->pfra_af; 5043b3a8eb9SGleb Smirnoff if (sa.sa.sa_family == AF_INET) { 5053b3a8eb9SGleb Smirnoff sa.sa.sa_len = sizeof(sa.sin); 5063b3a8eb9SGleb Smirnoff sa.sin.sin_addr = ad->pfra_ip4addr; 5073b3a8eb9SGleb Smirnoff } else { 5083b3a8eb9SGleb Smirnoff sa.sa.sa_len = sizeof(sa.sin6); 5093b3a8eb9SGleb Smirnoff sa.sin6.sin6_addr = ad->pfra_ip6addr; 5103b3a8eb9SGleb Smirnoff } 5113b3a8eb9SGleb Smirnoff if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host), 5123b3a8eb9SGleb Smirnoff NULL, 0, NI_NAMEREQD) == 0) 5133b3a8eb9SGleb Smirnoff printf("\t(%s)", host); 5143b3a8eb9SGleb Smirnoff } 5153b3a8eb9SGleb Smirnoff printf("\n"); 5163b3a8eb9SGleb Smirnoff } 5173b3a8eb9SGleb Smirnoff 5185b59b0c6SLeonid Evdokimov int 5195b59b0c6SLeonid Evdokimov nonzero_astats(struct pfr_astats *as) 5205b59b0c6SLeonid Evdokimov { 5215b59b0c6SLeonid Evdokimov uint64_t s = 0; 5225b59b0c6SLeonid Evdokimov 5235b59b0c6SLeonid Evdokimov for (int dir = 0; dir < PFR_DIR_MAX; dir++) 5245b59b0c6SLeonid Evdokimov for (int op = 0; op < PFR_OP_ADDR_MAX; op++) 5255b59b0c6SLeonid Evdokimov s |= as->pfras_packets[dir][op] | 5265b59b0c6SLeonid Evdokimov as->pfras_bytes[dir][op]; 5275b59b0c6SLeonid Evdokimov 5285b59b0c6SLeonid Evdokimov return (!!s); 5295b59b0c6SLeonid Evdokimov } 5305b59b0c6SLeonid Evdokimov 5313b3a8eb9SGleb Smirnoff void 5323b3a8eb9SGleb Smirnoff print_astats(struct pfr_astats *as, int dns) 5333b3a8eb9SGleb Smirnoff { 5343b3a8eb9SGleb Smirnoff time_t time = as->pfras_tzero; 5353b3a8eb9SGleb Smirnoff int dir, op; 5363b3a8eb9SGleb Smirnoff 5373b3a8eb9SGleb Smirnoff print_addrx(&as->pfras_a, NULL, dns); 5383b3a8eb9SGleb Smirnoff printf("\tCleared: %s", ctime(&time)); 5393b3a8eb9SGleb Smirnoff if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT) 5403b3a8eb9SGleb Smirnoff return; 5413b3a8eb9SGleb Smirnoff for (dir = 0; dir < PFR_DIR_MAX; dir++) 5423b3a8eb9SGleb Smirnoff for (op = 0; op < PFR_OP_ADDR_MAX; op++) 5433b3a8eb9SGleb Smirnoff printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 5443b3a8eb9SGleb Smirnoff stats_text[dir][op], 5453b3a8eb9SGleb Smirnoff (unsigned long long)as->pfras_packets[dir][op], 5463b3a8eb9SGleb Smirnoff (unsigned long long)as->pfras_bytes[dir][op]); 5473b3a8eb9SGleb Smirnoff } 5483b3a8eb9SGleb Smirnoff 5493b3a8eb9SGleb Smirnoff void 5503b3a8eb9SGleb Smirnoff radix_perror(void) 5513b3a8eb9SGleb Smirnoff { 5523b3a8eb9SGleb Smirnoff extern char *__progname; 5533b3a8eb9SGleb Smirnoff fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno)); 5543b3a8eb9SGleb Smirnoff } 5553b3a8eb9SGleb Smirnoff 5563b3a8eb9SGleb Smirnoff int 5573b3a8eb9SGleb Smirnoff pfctl_define_table(char *name, int flags, int addrs, const char *anchor, 5583b3a8eb9SGleb Smirnoff struct pfr_buffer *ab, u_int32_t ticket) 5593b3a8eb9SGleb Smirnoff { 5603b3a8eb9SGleb Smirnoff struct pfr_table tbl; 5613b3a8eb9SGleb Smirnoff 5623b3a8eb9SGleb Smirnoff bzero(&tbl, sizeof(tbl)); 5633b3a8eb9SGleb Smirnoff if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >= 5643b3a8eb9SGleb Smirnoff sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor, 5653b3a8eb9SGleb Smirnoff sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor)) 5663b3a8eb9SGleb Smirnoff errx(1, "pfctl_define_table: strlcpy"); 5673b3a8eb9SGleb Smirnoff tbl.pfrt_flags = flags; 5683b3a8eb9SGleb Smirnoff 5693b3a8eb9SGleb Smirnoff return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL, 5703b3a8eb9SGleb Smirnoff NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0); 5713b3a8eb9SGleb Smirnoff } 5723b3a8eb9SGleb Smirnoff 5733b3a8eb9SGleb Smirnoff void 5743b3a8eb9SGleb Smirnoff warn_namespace_collision(const char *filter) 5753b3a8eb9SGleb Smirnoff { 5763b3a8eb9SGleb Smirnoff struct pfr_buffer b; 5773b3a8eb9SGleb Smirnoff struct pfr_table *t; 5783b3a8eb9SGleb Smirnoff const char *name = NULL, *lastcoll; 5793b3a8eb9SGleb Smirnoff int coll = 0; 5803b3a8eb9SGleb Smirnoff 5813b3a8eb9SGleb Smirnoff bzero(&b, sizeof(b)); 5823b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_TABLES; 5833b3a8eb9SGleb Smirnoff for (;;) { 5843b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 5853b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 5863b3a8eb9SGleb Smirnoff if (pfr_get_tables(NULL, b.pfrb_caddr, 5873b3a8eb9SGleb Smirnoff &b.pfrb_size, PFR_FLAG_ALLRSETS)) 5883b3a8eb9SGleb Smirnoff err(1, "pfr_get_tables"); 5893b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 5903b3a8eb9SGleb Smirnoff break; 5913b3a8eb9SGleb Smirnoff } 5923b3a8eb9SGleb Smirnoff PFRB_FOREACH(t, &b) { 5933b3a8eb9SGleb Smirnoff if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE)) 5943b3a8eb9SGleb Smirnoff continue; 5953b3a8eb9SGleb Smirnoff if (filter != NULL && strcmp(filter, t->pfrt_name)) 5963b3a8eb9SGleb Smirnoff continue; 5973b3a8eb9SGleb Smirnoff if (!t->pfrt_anchor[0]) 5983b3a8eb9SGleb Smirnoff name = t->pfrt_name; 5993b3a8eb9SGleb Smirnoff else if (name != NULL && !strcmp(name, t->pfrt_name)) { 6003b3a8eb9SGleb Smirnoff coll++; 6013b3a8eb9SGleb Smirnoff lastcoll = name; 6023b3a8eb9SGleb Smirnoff name = NULL; 6033b3a8eb9SGleb Smirnoff } 6043b3a8eb9SGleb Smirnoff } 6053b3a8eb9SGleb Smirnoff if (coll == 1) 6063b3a8eb9SGleb Smirnoff warnx("warning: namespace collision with <%s> global table.", 6073b3a8eb9SGleb Smirnoff lastcoll); 6083b3a8eb9SGleb Smirnoff else if (coll > 1) 6093b3a8eb9SGleb Smirnoff warnx("warning: namespace collisions with %d global tables.", 6103b3a8eb9SGleb Smirnoff coll); 6113b3a8eb9SGleb Smirnoff pfr_buf_clear(&b); 6123b3a8eb9SGleb Smirnoff } 6133b3a8eb9SGleb Smirnoff 6143b3a8eb9SGleb Smirnoff void 6153b3a8eb9SGleb Smirnoff xprintf(int opts, const char *fmt, ...) 6163b3a8eb9SGleb Smirnoff { 6173b3a8eb9SGleb Smirnoff va_list args; 6183b3a8eb9SGleb Smirnoff 6193b3a8eb9SGleb Smirnoff if (opts & PF_OPT_QUIET) 6203b3a8eb9SGleb Smirnoff return; 6213b3a8eb9SGleb Smirnoff 6223b3a8eb9SGleb Smirnoff va_start(args, fmt); 6233b3a8eb9SGleb Smirnoff vfprintf(stderr, fmt, args); 6243b3a8eb9SGleb Smirnoff va_end(args); 6253b3a8eb9SGleb Smirnoff 6263b3a8eb9SGleb Smirnoff if (opts & PF_OPT_DUMMYACTION) 6273b3a8eb9SGleb Smirnoff fprintf(stderr, " (dummy).\n"); 6283b3a8eb9SGleb Smirnoff else if (opts & PF_OPT_NOACTION) 6293b3a8eb9SGleb Smirnoff fprintf(stderr, " (syntax only).\n"); 6303b3a8eb9SGleb Smirnoff else 6313b3a8eb9SGleb Smirnoff fprintf(stderr, ".\n"); 6323b3a8eb9SGleb Smirnoff } 6333b3a8eb9SGleb Smirnoff 6343b3a8eb9SGleb Smirnoff 6353b3a8eb9SGleb Smirnoff /* interface stuff */ 6363b3a8eb9SGleb Smirnoff 6373b3a8eb9SGleb Smirnoff int 6383b3a8eb9SGleb Smirnoff pfctl_show_ifaces(const char *filter, int opts) 6393b3a8eb9SGleb Smirnoff { 6403b3a8eb9SGleb Smirnoff struct pfr_buffer b; 6413b3a8eb9SGleb Smirnoff struct pfi_kif *p; 6423b3a8eb9SGleb Smirnoff 6433b3a8eb9SGleb Smirnoff bzero(&b, sizeof(b)); 6443b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_IFACES; 6453b3a8eb9SGleb Smirnoff for (;;) { 6463b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 6473b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 6483b3a8eb9SGleb Smirnoff if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) { 6493b3a8eb9SGleb Smirnoff radix_perror(); 6503b3a8eb9SGleb Smirnoff return (1); 6513b3a8eb9SGleb Smirnoff } 6523b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 6533b3a8eb9SGleb Smirnoff break; 6543b3a8eb9SGleb Smirnoff } 6553b3a8eb9SGleb Smirnoff if (opts & PF_OPT_SHOWALL) 6563b3a8eb9SGleb Smirnoff pfctl_print_title("INTERFACES:"); 6573b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) 6583b3a8eb9SGleb Smirnoff print_iface(p, opts); 6593b3a8eb9SGleb Smirnoff return (0); 6603b3a8eb9SGleb Smirnoff } 6613b3a8eb9SGleb Smirnoff 6623b3a8eb9SGleb Smirnoff void 6633b3a8eb9SGleb Smirnoff print_iface(struct pfi_kif *p, int opts) 6643b3a8eb9SGleb Smirnoff { 6653b3a8eb9SGleb Smirnoff time_t tzero = p->pfik_tzero; 6663b3a8eb9SGleb Smirnoff int i, af, dir, act; 6673b3a8eb9SGleb Smirnoff 6683b3a8eb9SGleb Smirnoff printf("%s", p->pfik_name); 6693b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) { 6703b3a8eb9SGleb Smirnoff if (p->pfik_flags & PFI_IFLAG_SKIP) 6713b3a8eb9SGleb Smirnoff printf(" (skip)"); 6723b3a8eb9SGleb Smirnoff } 6733b3a8eb9SGleb Smirnoff printf("\n"); 6743b3a8eb9SGleb Smirnoff 6753b3a8eb9SGleb Smirnoff if (!(opts & PF_OPT_VERBOSE2)) 6763b3a8eb9SGleb Smirnoff return; 6773b3a8eb9SGleb Smirnoff printf("\tCleared: %s", ctime(&tzero)); 6783b3a8eb9SGleb Smirnoff printf("\tReferences: %-18d\n", p->pfik_rulerefs); 6793b3a8eb9SGleb Smirnoff for (i = 0; i < 8; i++) { 6803b3a8eb9SGleb Smirnoff af = (i>>2) & 1; 6813b3a8eb9SGleb Smirnoff dir = (i>>1) &1; 6823b3a8eb9SGleb Smirnoff act = i & 1; 6833b3a8eb9SGleb Smirnoff printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 6843b3a8eb9SGleb Smirnoff istats_text[af][dir][act], 6853b3a8eb9SGleb Smirnoff (unsigned long long)p->pfik_packets[af][dir][act], 6863b3a8eb9SGleb Smirnoff (unsigned long long)p->pfik_bytes[af][dir][act]); 6873b3a8eb9SGleb Smirnoff } 6883b3a8eb9SGleb Smirnoff } 689