13b3a8eb9SGleb Smirnoff /* $OpenBSD: pfctl_table.c,v 1.67 2008/06/10 20:55:02 mcbride Exp $ */ 23b3a8eb9SGleb Smirnoff 3*1de7b4b8SPedro F. Giffuni /*- 4*1de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause 5*1de7b4b8SPedro 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/cdefs.h> 363b3a8eb9SGleb Smirnoff __FBSDID("$FreeBSD$"); 373b3a8eb9SGleb Smirnoff 383b3a8eb9SGleb Smirnoff #include <sys/types.h> 393b3a8eb9SGleb Smirnoff #include <sys/ioctl.h> 403b3a8eb9SGleb Smirnoff #include <sys/socket.h> 413b3a8eb9SGleb Smirnoff 423b3a8eb9SGleb Smirnoff #include <net/if.h> 433b3a8eb9SGleb Smirnoff #include <net/pfvar.h> 443b3a8eb9SGleb Smirnoff #include <arpa/inet.h> 453b3a8eb9SGleb Smirnoff 463b3a8eb9SGleb Smirnoff #include <ctype.h> 473b3a8eb9SGleb Smirnoff #include <err.h> 483b3a8eb9SGleb Smirnoff #include <errno.h> 493b3a8eb9SGleb Smirnoff #include <netdb.h> 503b3a8eb9SGleb Smirnoff #include <stdarg.h> 513b3a8eb9SGleb Smirnoff #include <stdio.h> 523b3a8eb9SGleb Smirnoff #include <stdlib.h> 533b3a8eb9SGleb Smirnoff #include <string.h> 543b3a8eb9SGleb Smirnoff #include <time.h> 553b3a8eb9SGleb Smirnoff 563b3a8eb9SGleb Smirnoff #include "pfctl_parser.h" 573b3a8eb9SGleb Smirnoff #include "pfctl.h" 583b3a8eb9SGleb Smirnoff 593b3a8eb9SGleb Smirnoff extern void usage(void); 603b3a8eb9SGleb Smirnoff static int pfctl_table(int, char *[], char *, const char *, char *, 613b3a8eb9SGleb Smirnoff const char *, int); 623b3a8eb9SGleb Smirnoff static void print_table(struct pfr_table *, int, int); 633b3a8eb9SGleb Smirnoff static void print_tstats(struct pfr_tstats *, int); 643b3a8eb9SGleb Smirnoff static int load_addr(struct pfr_buffer *, int, char *[], char *, int); 653b3a8eb9SGleb Smirnoff static void print_addrx(struct pfr_addr *, struct pfr_addr *, int); 663b3a8eb9SGleb Smirnoff static void print_astats(struct pfr_astats *, int); 673b3a8eb9SGleb Smirnoff static void radix_perror(void); 683b3a8eb9SGleb Smirnoff static void xprintf(int, const char *, ...); 693b3a8eb9SGleb Smirnoff static void print_iface(struct pfi_kif *, int); 703b3a8eb9SGleb Smirnoff 713b3a8eb9SGleb Smirnoff static const char *stats_text[PFR_DIR_MAX][PFR_OP_TABLE_MAX] = { 723b3a8eb9SGleb Smirnoff { "In/Block:", "In/Pass:", "In/XPass:" }, 733b3a8eb9SGleb Smirnoff { "Out/Block:", "Out/Pass:", "Out/XPass:" } 743b3a8eb9SGleb Smirnoff }; 753b3a8eb9SGleb Smirnoff 763b3a8eb9SGleb Smirnoff static const char *istats_text[2][2][2] = { 773b3a8eb9SGleb Smirnoff { { "In4/Pass:", "In4/Block:" }, { "Out4/Pass:", "Out4/Block:" } }, 783b3a8eb9SGleb Smirnoff { { "In6/Pass:", "In6/Block:" }, { "Out6/Pass:", "Out6/Block:" } } 793b3a8eb9SGleb Smirnoff }; 803b3a8eb9SGleb Smirnoff 813b3a8eb9SGleb Smirnoff #define RVTEST(fct) do { \ 823b3a8eb9SGleb Smirnoff if ((!(opts & PF_OPT_NOACTION) || \ 833b3a8eb9SGleb Smirnoff (opts & PF_OPT_DUMMYACTION)) && \ 843b3a8eb9SGleb Smirnoff (fct)) { \ 853b3a8eb9SGleb Smirnoff radix_perror(); \ 863b3a8eb9SGleb Smirnoff goto _error; \ 873b3a8eb9SGleb Smirnoff } \ 883b3a8eb9SGleb Smirnoff } while (0) 893b3a8eb9SGleb Smirnoff 903b3a8eb9SGleb Smirnoff #define CREATE_TABLE do { \ 913b3a8eb9SGleb Smirnoff table.pfrt_flags |= PFR_TFLAG_PERSIST; \ 923b3a8eb9SGleb Smirnoff if ((!(opts & PF_OPT_NOACTION) || \ 933b3a8eb9SGleb Smirnoff (opts & PF_OPT_DUMMYACTION)) && \ 943b3a8eb9SGleb Smirnoff (pfr_add_tables(&table, 1, &nadd, flags)) && \ 953b3a8eb9SGleb Smirnoff (errno != EPERM)) { \ 963b3a8eb9SGleb Smirnoff radix_perror(); \ 973b3a8eb9SGleb Smirnoff goto _error; \ 983b3a8eb9SGleb Smirnoff } \ 993b3a8eb9SGleb Smirnoff if (nadd) { \ 1003b3a8eb9SGleb Smirnoff warn_namespace_collision(table.pfrt_name); \ 1013b3a8eb9SGleb Smirnoff xprintf(opts, "%d table created", nadd); \ 1023b3a8eb9SGleb Smirnoff if (opts & PF_OPT_NOACTION) \ 1033b3a8eb9SGleb Smirnoff return (0); \ 1043b3a8eb9SGleb Smirnoff } \ 1053b3a8eb9SGleb Smirnoff table.pfrt_flags &= ~PFR_TFLAG_PERSIST; \ 1063b3a8eb9SGleb Smirnoff } while(0) 1073b3a8eb9SGleb Smirnoff 1083b3a8eb9SGleb Smirnoff int 1093b3a8eb9SGleb Smirnoff pfctl_clear_tables(const char *anchor, int opts) 1103b3a8eb9SGleb Smirnoff { 1113b3a8eb9SGleb Smirnoff return pfctl_table(0, NULL, NULL, "-F", NULL, anchor, opts); 1123b3a8eb9SGleb Smirnoff } 1133b3a8eb9SGleb Smirnoff 1143b3a8eb9SGleb Smirnoff int 1153b3a8eb9SGleb Smirnoff pfctl_show_tables(const char *anchor, int opts) 1163b3a8eb9SGleb Smirnoff { 1173b3a8eb9SGleb Smirnoff return pfctl_table(0, NULL, NULL, "-s", NULL, anchor, opts); 1183b3a8eb9SGleb Smirnoff } 1193b3a8eb9SGleb Smirnoff 1203b3a8eb9SGleb Smirnoff int 1213b3a8eb9SGleb Smirnoff pfctl_command_tables(int argc, char *argv[], char *tname, 1223b3a8eb9SGleb Smirnoff const char *command, char *file, const char *anchor, int opts) 1233b3a8eb9SGleb Smirnoff { 1243b3a8eb9SGleb Smirnoff if (tname == NULL || command == NULL) 1253b3a8eb9SGleb Smirnoff usage(); 1263b3a8eb9SGleb Smirnoff return pfctl_table(argc, argv, tname, command, file, anchor, opts); 1273b3a8eb9SGleb Smirnoff } 1283b3a8eb9SGleb Smirnoff 1293b3a8eb9SGleb Smirnoff int 1303b3a8eb9SGleb Smirnoff pfctl_table(int argc, char *argv[], char *tname, const char *command, 1313b3a8eb9SGleb Smirnoff char *file, const char *anchor, int opts) 1323b3a8eb9SGleb Smirnoff { 1333b3a8eb9SGleb Smirnoff struct pfr_table table; 1343b3a8eb9SGleb Smirnoff struct pfr_buffer b, b2; 1353b3a8eb9SGleb Smirnoff struct pfr_addr *a, *a2; 1363b3a8eb9SGleb Smirnoff int nadd = 0, ndel = 0, nchange = 0, nzero = 0; 1373b3a8eb9SGleb Smirnoff int rv = 0, flags = 0, nmatch = 0; 1383b3a8eb9SGleb Smirnoff void *p; 1393b3a8eb9SGleb Smirnoff 1403b3a8eb9SGleb Smirnoff if (command == NULL) 1413b3a8eb9SGleb Smirnoff usage(); 1423b3a8eb9SGleb Smirnoff if (opts & PF_OPT_NOACTION) 1433b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_DUMMY; 1443b3a8eb9SGleb Smirnoff 1453b3a8eb9SGleb Smirnoff bzero(&b, sizeof(b)); 1463b3a8eb9SGleb Smirnoff bzero(&b2, sizeof(b2)); 1473b3a8eb9SGleb Smirnoff bzero(&table, sizeof(table)); 1483b3a8eb9SGleb Smirnoff if (tname != NULL) { 1493b3a8eb9SGleb Smirnoff if (strlen(tname) >= PF_TABLE_NAME_SIZE) 1503b3a8eb9SGleb Smirnoff usage(); 1513b3a8eb9SGleb Smirnoff if (strlcpy(table.pfrt_name, tname, 1523b3a8eb9SGleb Smirnoff sizeof(table.pfrt_name)) >= sizeof(table.pfrt_name)) 1533b3a8eb9SGleb Smirnoff errx(1, "pfctl_table: strlcpy"); 1543b3a8eb9SGleb Smirnoff } 1553b3a8eb9SGleb Smirnoff if (strlcpy(table.pfrt_anchor, anchor, 1563b3a8eb9SGleb Smirnoff sizeof(table.pfrt_anchor)) >= sizeof(table.pfrt_anchor)) 1573b3a8eb9SGleb Smirnoff errx(1, "pfctl_table: strlcpy"); 1583b3a8eb9SGleb Smirnoff 1593b3a8eb9SGleb Smirnoff if (!strcmp(command, "-F")) { 1603b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1613b3a8eb9SGleb Smirnoff usage(); 1623b3a8eb9SGleb Smirnoff RVTEST(pfr_clr_tables(&table, &ndel, flags)); 1633b3a8eb9SGleb Smirnoff xprintf(opts, "%d tables deleted", ndel); 1643b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "-s")) { 1653b3a8eb9SGleb Smirnoff b.pfrb_type = (opts & PF_OPT_VERBOSE2) ? 1663b3a8eb9SGleb Smirnoff PFRB_TSTATS : PFRB_TABLES; 1673b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1683b3a8eb9SGleb Smirnoff usage(); 1693b3a8eb9SGleb Smirnoff for (;;) { 1703b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 1713b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 1723b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) 1733b3a8eb9SGleb Smirnoff RVTEST(pfr_get_tstats(&table, 1743b3a8eb9SGleb Smirnoff b.pfrb_caddr, &b.pfrb_size, flags)); 1753b3a8eb9SGleb Smirnoff else 1763b3a8eb9SGleb Smirnoff RVTEST(pfr_get_tables(&table, 1773b3a8eb9SGleb Smirnoff b.pfrb_caddr, &b.pfrb_size, flags)); 1783b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 1793b3a8eb9SGleb Smirnoff break; 1803b3a8eb9SGleb Smirnoff } 1813b3a8eb9SGleb Smirnoff 1823b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_SHOWALL) && b.pfrb_size > 0) 1833b3a8eb9SGleb Smirnoff pfctl_print_title("TABLES:"); 1843b3a8eb9SGleb Smirnoff 1853b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) 1863b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) 1873b3a8eb9SGleb Smirnoff print_tstats(p, opts & PF_OPT_DEBUG); 1883b3a8eb9SGleb Smirnoff else 1893b3a8eb9SGleb Smirnoff print_table(p, opts & PF_OPT_VERBOSE, 1903b3a8eb9SGleb Smirnoff opts & PF_OPT_DEBUG); 1913b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "kill")) { 1923b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1933b3a8eb9SGleb Smirnoff usage(); 1943b3a8eb9SGleb Smirnoff RVTEST(pfr_del_tables(&table, 1, &ndel, flags)); 1953b3a8eb9SGleb Smirnoff xprintf(opts, "%d table deleted", ndel); 1963b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "flush")) { 1973b3a8eb9SGleb Smirnoff if (argc || file != NULL) 1983b3a8eb9SGleb Smirnoff usage(); 1993b3a8eb9SGleb Smirnoff RVTEST(pfr_clr_addrs(&table, &ndel, flags)); 2003b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses deleted", ndel); 2013b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "add")) { 2023b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 2033b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 0)) 2043b3a8eb9SGleb Smirnoff goto _error; 2053b3a8eb9SGleb Smirnoff CREATE_TABLE; 2063b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2073b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2083b3a8eb9SGleb Smirnoff RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size, 2093b3a8eb9SGleb Smirnoff &nadd, flags)); 2103b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size); 2113b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2123b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 2133b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2143b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2153b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2163b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "delete")) { 2173b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 2183b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 0)) 2193b3a8eb9SGleb Smirnoff goto _error; 2203b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2213b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2223b3a8eb9SGleb Smirnoff RVTEST(pfr_del_addrs(&table, b.pfrb_caddr, b.pfrb_size, 2233b3a8eb9SGleb Smirnoff &ndel, flags)); 2243b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses deleted", ndel, b.pfrb_size); 2253b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2263b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 2273b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2283b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2293b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2303b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "replace")) { 2313b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 2323b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 0)) 2333b3a8eb9SGleb Smirnoff goto _error; 2343b3a8eb9SGleb Smirnoff CREATE_TABLE; 2353b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2363b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2373b3a8eb9SGleb Smirnoff for (;;) { 2383b3a8eb9SGleb Smirnoff int sz2 = b.pfrb_msize; 2393b3a8eb9SGleb Smirnoff 2403b3a8eb9SGleb Smirnoff RVTEST(pfr_set_addrs(&table, b.pfrb_caddr, b.pfrb_size, 2413b3a8eb9SGleb Smirnoff &sz2, &nadd, &ndel, &nchange, flags)); 2423b3a8eb9SGleb Smirnoff if (sz2 <= b.pfrb_msize) { 2433b3a8eb9SGleb Smirnoff b.pfrb_size = sz2; 2443b3a8eb9SGleb Smirnoff break; 2453b3a8eb9SGleb Smirnoff } else 2463b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, sz2); 2473b3a8eb9SGleb Smirnoff } 2483b3a8eb9SGleb Smirnoff if (nadd) 2493b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses added", nadd); 2503b3a8eb9SGleb Smirnoff if (ndel) 2513b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses deleted", ndel); 2523b3a8eb9SGleb Smirnoff if (nchange) 2533b3a8eb9SGleb Smirnoff xprintf(opts, "%d addresses changed", nchange); 2543b3a8eb9SGleb Smirnoff if (!nadd && !ndel && !nchange) 2553b3a8eb9SGleb Smirnoff xprintf(opts, "no changes"); 2563b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2573b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 2583b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2593b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2603b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2613b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "expire")) { 2623b3a8eb9SGleb Smirnoff const char *errstr; 2633b3a8eb9SGleb Smirnoff u_int lifetime; 2643b3a8eb9SGleb Smirnoff 2653b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ASTATS; 2663b3a8eb9SGleb Smirnoff b2.pfrb_type = PFRB_ADDRS; 2673b3a8eb9SGleb Smirnoff if (argc != 1 || file != NULL) 2683b3a8eb9SGleb Smirnoff usage(); 2693b3a8eb9SGleb Smirnoff lifetime = strtonum(*argv, 0, UINT_MAX, &errstr); 2703b3a8eb9SGleb Smirnoff if (errstr) 2713b3a8eb9SGleb Smirnoff errx(1, "expiry time: %s", errstr); 2723b3a8eb9SGleb Smirnoff for (;;) { 2733b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 2743b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 2753b3a8eb9SGleb Smirnoff RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 2763b3a8eb9SGleb Smirnoff &b.pfrb_size, flags)); 2773b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 2783b3a8eb9SGleb Smirnoff break; 2793b3a8eb9SGleb Smirnoff } 2803b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) { 2813b3a8eb9SGleb Smirnoff ((struct pfr_astats *)p)->pfras_a.pfra_fback = 0; 2823b3a8eb9SGleb Smirnoff if (time(NULL) - ((struct pfr_astats *)p)->pfras_tzero > 2833b3a8eb9SGleb Smirnoff lifetime) 2843b3a8eb9SGleb Smirnoff if (pfr_buf_add(&b2, 2853b3a8eb9SGleb Smirnoff &((struct pfr_astats *)p)->pfras_a)) 2863b3a8eb9SGleb Smirnoff err(1, "duplicate buffer"); 2873b3a8eb9SGleb Smirnoff } 2883b3a8eb9SGleb Smirnoff 2893b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2903b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_FEEDBACK; 2913b3a8eb9SGleb Smirnoff RVTEST(pfr_del_addrs(&table, b2.pfrb_caddr, b2.pfrb_size, 2923b3a8eb9SGleb Smirnoff &ndel, flags)); 2933b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses expired", ndel, b2.pfrb_size); 2943b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 2953b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b2) 2963b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback) 2973b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 2983b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 2993b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "show")) { 3003b3a8eb9SGleb Smirnoff b.pfrb_type = (opts & PF_OPT_VERBOSE) ? 3013b3a8eb9SGleb Smirnoff PFRB_ASTATS : PFRB_ADDRS; 3023b3a8eb9SGleb Smirnoff if (argc || file != NULL) 3033b3a8eb9SGleb Smirnoff usage(); 3043b3a8eb9SGleb Smirnoff for (;;) { 3053b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 3063b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 3073b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 3083b3a8eb9SGleb Smirnoff RVTEST(pfr_get_astats(&table, b.pfrb_caddr, 3093b3a8eb9SGleb Smirnoff &b.pfrb_size, flags)); 3103b3a8eb9SGleb Smirnoff else 3113b3a8eb9SGleb Smirnoff RVTEST(pfr_get_addrs(&table, b.pfrb_caddr, 3123b3a8eb9SGleb Smirnoff &b.pfrb_size, flags)); 3133b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 3143b3a8eb9SGleb Smirnoff break; 3153b3a8eb9SGleb Smirnoff } 3163b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) 3173b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) 3183b3a8eb9SGleb Smirnoff print_astats(p, opts & PF_OPT_USEDNS); 3193b3a8eb9SGleb Smirnoff else 3203b3a8eb9SGleb Smirnoff print_addrx(p, NULL, opts & PF_OPT_USEDNS); 3213b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "test")) { 3223b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_ADDRS; 3233b3a8eb9SGleb Smirnoff b2.pfrb_type = PFRB_ADDRS; 3243b3a8eb9SGleb Smirnoff 3253b3a8eb9SGleb Smirnoff if (load_addr(&b, argc, argv, file, 1)) 3263b3a8eb9SGleb Smirnoff goto _error; 3273b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) { 3283b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_REPLACE; 3293b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 3303b3a8eb9SGleb Smirnoff if (pfr_buf_add(&b2, a)) 3313b3a8eb9SGleb Smirnoff err(1, "duplicate buffer"); 3323b3a8eb9SGleb Smirnoff } 3333b3a8eb9SGleb Smirnoff RVTEST(pfr_tst_addrs(&table, b.pfrb_caddr, b.pfrb_size, 3343b3a8eb9SGleb Smirnoff &nmatch, flags)); 3353b3a8eb9SGleb Smirnoff xprintf(opts, "%d/%d addresses match", nmatch, b.pfrb_size); 3363b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_VERBOSE) && !(opts & PF_OPT_VERBOSE2)) 3373b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) 3383b3a8eb9SGleb Smirnoff if (a->pfra_fback == PFR_FB_MATCH) 3393b3a8eb9SGleb Smirnoff print_addrx(a, NULL, 3403b3a8eb9SGleb Smirnoff opts & PF_OPT_USEDNS); 3413b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2) { 3423b3a8eb9SGleb Smirnoff a2 = NULL; 3433b3a8eb9SGleb Smirnoff PFRB_FOREACH(a, &b) { 3443b3a8eb9SGleb Smirnoff a2 = pfr_buf_next(&b2, a2); 3453b3a8eb9SGleb Smirnoff print_addrx(a2, a, opts & PF_OPT_USEDNS); 3463b3a8eb9SGleb Smirnoff } 3473b3a8eb9SGleb Smirnoff } 3483b3a8eb9SGleb Smirnoff if (nmatch < b.pfrb_size) 3493b3a8eb9SGleb Smirnoff rv = 2; 3503b3a8eb9SGleb Smirnoff } else if (!strcmp(command, "zero")) { 3513b3a8eb9SGleb Smirnoff if (argc || file != NULL) 3523b3a8eb9SGleb Smirnoff usage(); 3533b3a8eb9SGleb Smirnoff flags |= PFR_FLAG_ADDRSTOO; 3543b3a8eb9SGleb Smirnoff RVTEST(pfr_clr_tstats(&table, 1, &nzero, flags)); 3553b3a8eb9SGleb Smirnoff xprintf(opts, "%d table/stats cleared", nzero); 3563b3a8eb9SGleb Smirnoff } else 3573b3a8eb9SGleb Smirnoff warnx("pfctl_table: unknown command '%s'", command); 3583b3a8eb9SGleb Smirnoff goto _cleanup; 3593b3a8eb9SGleb Smirnoff 3603b3a8eb9SGleb Smirnoff _error: 3613b3a8eb9SGleb Smirnoff rv = -1; 3623b3a8eb9SGleb Smirnoff _cleanup: 3633b3a8eb9SGleb Smirnoff pfr_buf_clear(&b); 3643b3a8eb9SGleb Smirnoff pfr_buf_clear(&b2); 3653b3a8eb9SGleb Smirnoff return (rv); 3663b3a8eb9SGleb Smirnoff } 3673b3a8eb9SGleb Smirnoff 3683b3a8eb9SGleb Smirnoff void 3693b3a8eb9SGleb Smirnoff print_table(struct pfr_table *ta, int verbose, int debug) 3703b3a8eb9SGleb Smirnoff { 3713b3a8eb9SGleb Smirnoff if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE)) 3723b3a8eb9SGleb Smirnoff return; 3733b3a8eb9SGleb Smirnoff if (verbose) { 3743b3a8eb9SGleb Smirnoff printf("%c%c%c%c%c%c%c\t%s", 3753b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-', 3763b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-', 3773b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-', 3783b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-', 3793b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-', 3803b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-', 3813b3a8eb9SGleb Smirnoff (ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-', 3823b3a8eb9SGleb Smirnoff ta->pfrt_name); 3833b3a8eb9SGleb Smirnoff if (ta->pfrt_anchor[0]) 3843b3a8eb9SGleb Smirnoff printf("\t%s", ta->pfrt_anchor); 3853b3a8eb9SGleb Smirnoff puts(""); 3863b3a8eb9SGleb Smirnoff } else 3873b3a8eb9SGleb Smirnoff puts(ta->pfrt_name); 3883b3a8eb9SGleb Smirnoff } 3893b3a8eb9SGleb Smirnoff 3903b3a8eb9SGleb Smirnoff void 3913b3a8eb9SGleb Smirnoff print_tstats(struct pfr_tstats *ts, int debug) 3923b3a8eb9SGleb Smirnoff { 3933b3a8eb9SGleb Smirnoff time_t time = ts->pfrts_tzero; 3943b3a8eb9SGleb Smirnoff int dir, op; 3953b3a8eb9SGleb Smirnoff 3963b3a8eb9SGleb Smirnoff if (!debug && !(ts->pfrts_flags & PFR_TFLAG_ACTIVE)) 3973b3a8eb9SGleb Smirnoff return; 3983b3a8eb9SGleb Smirnoff print_table(&ts->pfrts_t, 1, debug); 3993b3a8eb9SGleb Smirnoff printf("\tAddresses: %d\n", ts->pfrts_cnt); 4003b3a8eb9SGleb Smirnoff printf("\tCleared: %s", ctime(&time)); 4013b3a8eb9SGleb Smirnoff printf("\tReferences: [ Anchors: %-18d Rules: %-18d ]\n", 4023b3a8eb9SGleb Smirnoff ts->pfrts_refcnt[PFR_REFCNT_ANCHOR], 4033b3a8eb9SGleb Smirnoff ts->pfrts_refcnt[PFR_REFCNT_RULE]); 4043b3a8eb9SGleb Smirnoff printf("\tEvaluations: [ NoMatch: %-18llu Match: %-18llu ]\n", 4053b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_nomatch, 4063b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_match); 4073b3a8eb9SGleb Smirnoff for (dir = 0; dir < PFR_DIR_MAX; dir++) 4083b3a8eb9SGleb Smirnoff for (op = 0; op < PFR_OP_TABLE_MAX; op++) 4093b3a8eb9SGleb Smirnoff printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 4103b3a8eb9SGleb Smirnoff stats_text[dir][op], 4113b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_packets[dir][op], 4123b3a8eb9SGleb Smirnoff (unsigned long long)ts->pfrts_bytes[dir][op]); 4133b3a8eb9SGleb Smirnoff } 4143b3a8eb9SGleb Smirnoff 4153b3a8eb9SGleb Smirnoff int 4163b3a8eb9SGleb Smirnoff load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file, 4173b3a8eb9SGleb Smirnoff int nonetwork) 4183b3a8eb9SGleb Smirnoff { 4193b3a8eb9SGleb Smirnoff while (argc--) 4203b3a8eb9SGleb Smirnoff if (append_addr(b, *argv++, nonetwork)) { 4213b3a8eb9SGleb Smirnoff if (errno) 4223b3a8eb9SGleb Smirnoff warn("cannot decode %s", argv[-1]); 4233b3a8eb9SGleb Smirnoff return (-1); 4243b3a8eb9SGleb Smirnoff } 4253b3a8eb9SGleb Smirnoff if (pfr_buf_load(b, file, nonetwork, append_addr)) { 4263b3a8eb9SGleb Smirnoff warn("cannot load %s", file); 4273b3a8eb9SGleb Smirnoff return (-1); 4283b3a8eb9SGleb Smirnoff } 4293b3a8eb9SGleb Smirnoff return (0); 4303b3a8eb9SGleb Smirnoff } 4313b3a8eb9SGleb Smirnoff 4323b3a8eb9SGleb Smirnoff void 4333b3a8eb9SGleb Smirnoff print_addrx(struct pfr_addr *ad, struct pfr_addr *rad, int dns) 4343b3a8eb9SGleb Smirnoff { 4353b3a8eb9SGleb Smirnoff char ch, buf[256] = "{error}"; 4363b3a8eb9SGleb Smirnoff char fb[] = { ' ', 'M', 'A', 'D', 'C', 'Z', 'X', ' ', 'Y', ' ' }; 4373b3a8eb9SGleb Smirnoff unsigned int fback, hostnet; 4383b3a8eb9SGleb Smirnoff 4393b3a8eb9SGleb Smirnoff fback = (rad != NULL) ? rad->pfra_fback : ad->pfra_fback; 4403b3a8eb9SGleb Smirnoff ch = (fback < sizeof(fb)/sizeof(*fb)) ? fb[fback] : '?'; 4413b3a8eb9SGleb Smirnoff hostnet = (ad->pfra_af == AF_INET6) ? 128 : 32; 4423b3a8eb9SGleb Smirnoff inet_ntop(ad->pfra_af, &ad->pfra_u, buf, sizeof(buf)); 4433b3a8eb9SGleb Smirnoff printf("%c %c%s", ch, (ad->pfra_not?'!':' '), buf); 4443b3a8eb9SGleb Smirnoff if (ad->pfra_net < hostnet) 4453b3a8eb9SGleb Smirnoff printf("/%d", ad->pfra_net); 4463b3a8eb9SGleb Smirnoff if (rad != NULL && fback != PFR_FB_NONE) { 4473b3a8eb9SGleb Smirnoff if (strlcpy(buf, "{error}", sizeof(buf)) >= sizeof(buf)) 4483b3a8eb9SGleb Smirnoff errx(1, "print_addrx: strlcpy"); 4493b3a8eb9SGleb Smirnoff inet_ntop(rad->pfra_af, &rad->pfra_u, buf, sizeof(buf)); 4503b3a8eb9SGleb Smirnoff printf("\t%c%s", (rad->pfra_not?'!':' '), buf); 4513b3a8eb9SGleb Smirnoff if (rad->pfra_net < hostnet) 4523b3a8eb9SGleb Smirnoff printf("/%d", rad->pfra_net); 4533b3a8eb9SGleb Smirnoff } 4543b3a8eb9SGleb Smirnoff if (rad != NULL && fback == PFR_FB_NONE) 4553b3a8eb9SGleb Smirnoff printf("\t nomatch"); 4563b3a8eb9SGleb Smirnoff if (dns && ad->pfra_net == hostnet) { 4573b3a8eb9SGleb Smirnoff char host[NI_MAXHOST]; 4583b3a8eb9SGleb Smirnoff union sockaddr_union sa; 4593b3a8eb9SGleb Smirnoff 4603b3a8eb9SGleb Smirnoff strlcpy(host, "?", sizeof(host)); 4613b3a8eb9SGleb Smirnoff bzero(&sa, sizeof(sa)); 4623b3a8eb9SGleb Smirnoff sa.sa.sa_family = ad->pfra_af; 4633b3a8eb9SGleb Smirnoff if (sa.sa.sa_family == AF_INET) { 4643b3a8eb9SGleb Smirnoff sa.sa.sa_len = sizeof(sa.sin); 4653b3a8eb9SGleb Smirnoff sa.sin.sin_addr = ad->pfra_ip4addr; 4663b3a8eb9SGleb Smirnoff } else { 4673b3a8eb9SGleb Smirnoff sa.sa.sa_len = sizeof(sa.sin6); 4683b3a8eb9SGleb Smirnoff sa.sin6.sin6_addr = ad->pfra_ip6addr; 4693b3a8eb9SGleb Smirnoff } 4703b3a8eb9SGleb Smirnoff if (getnameinfo(&sa.sa, sa.sa.sa_len, host, sizeof(host), 4713b3a8eb9SGleb Smirnoff NULL, 0, NI_NAMEREQD) == 0) 4723b3a8eb9SGleb Smirnoff printf("\t(%s)", host); 4733b3a8eb9SGleb Smirnoff } 4743b3a8eb9SGleb Smirnoff printf("\n"); 4753b3a8eb9SGleb Smirnoff } 4763b3a8eb9SGleb Smirnoff 4773b3a8eb9SGleb Smirnoff void 4783b3a8eb9SGleb Smirnoff print_astats(struct pfr_astats *as, int dns) 4793b3a8eb9SGleb Smirnoff { 4803b3a8eb9SGleb Smirnoff time_t time = as->pfras_tzero; 4813b3a8eb9SGleb Smirnoff int dir, op; 4823b3a8eb9SGleb Smirnoff 4833b3a8eb9SGleb Smirnoff print_addrx(&as->pfras_a, NULL, dns); 4843b3a8eb9SGleb Smirnoff printf("\tCleared: %s", ctime(&time)); 4853b3a8eb9SGleb Smirnoff if (as->pfras_a.pfra_fback == PFR_FB_NOCOUNT) 4863b3a8eb9SGleb Smirnoff return; 4873b3a8eb9SGleb Smirnoff for (dir = 0; dir < PFR_DIR_MAX; dir++) 4883b3a8eb9SGleb Smirnoff for (op = 0; op < PFR_OP_ADDR_MAX; op++) 4893b3a8eb9SGleb Smirnoff printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 4903b3a8eb9SGleb Smirnoff stats_text[dir][op], 4913b3a8eb9SGleb Smirnoff (unsigned long long)as->pfras_packets[dir][op], 4923b3a8eb9SGleb Smirnoff (unsigned long long)as->pfras_bytes[dir][op]); 4933b3a8eb9SGleb Smirnoff } 4943b3a8eb9SGleb Smirnoff 4953b3a8eb9SGleb Smirnoff void 4963b3a8eb9SGleb Smirnoff radix_perror(void) 4973b3a8eb9SGleb Smirnoff { 4983b3a8eb9SGleb Smirnoff extern char *__progname; 4993b3a8eb9SGleb Smirnoff fprintf(stderr, "%s: %s.\n", __progname, pfr_strerror(errno)); 5003b3a8eb9SGleb Smirnoff } 5013b3a8eb9SGleb Smirnoff 5023b3a8eb9SGleb Smirnoff int 5033b3a8eb9SGleb Smirnoff pfctl_define_table(char *name, int flags, int addrs, const char *anchor, 5043b3a8eb9SGleb Smirnoff struct pfr_buffer *ab, u_int32_t ticket) 5053b3a8eb9SGleb Smirnoff { 5063b3a8eb9SGleb Smirnoff struct pfr_table tbl; 5073b3a8eb9SGleb Smirnoff 5083b3a8eb9SGleb Smirnoff bzero(&tbl, sizeof(tbl)); 5093b3a8eb9SGleb Smirnoff if (strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)) >= 5103b3a8eb9SGleb Smirnoff sizeof(tbl.pfrt_name) || strlcpy(tbl.pfrt_anchor, anchor, 5113b3a8eb9SGleb Smirnoff sizeof(tbl.pfrt_anchor)) >= sizeof(tbl.pfrt_anchor)) 5123b3a8eb9SGleb Smirnoff errx(1, "pfctl_define_table: strlcpy"); 5133b3a8eb9SGleb Smirnoff tbl.pfrt_flags = flags; 5143b3a8eb9SGleb Smirnoff 5153b3a8eb9SGleb Smirnoff return pfr_ina_define(&tbl, ab->pfrb_caddr, ab->pfrb_size, NULL, 5163b3a8eb9SGleb Smirnoff NULL, ticket, addrs ? PFR_FLAG_ADDRSTOO : 0); 5173b3a8eb9SGleb Smirnoff } 5183b3a8eb9SGleb Smirnoff 5193b3a8eb9SGleb Smirnoff void 5203b3a8eb9SGleb Smirnoff warn_namespace_collision(const char *filter) 5213b3a8eb9SGleb Smirnoff { 5223b3a8eb9SGleb Smirnoff struct pfr_buffer b; 5233b3a8eb9SGleb Smirnoff struct pfr_table *t; 5243b3a8eb9SGleb Smirnoff const char *name = NULL, *lastcoll; 5253b3a8eb9SGleb Smirnoff int coll = 0; 5263b3a8eb9SGleb Smirnoff 5273b3a8eb9SGleb Smirnoff bzero(&b, sizeof(b)); 5283b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_TABLES; 5293b3a8eb9SGleb Smirnoff for (;;) { 5303b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 5313b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 5323b3a8eb9SGleb Smirnoff if (pfr_get_tables(NULL, b.pfrb_caddr, 5333b3a8eb9SGleb Smirnoff &b.pfrb_size, PFR_FLAG_ALLRSETS)) 5343b3a8eb9SGleb Smirnoff err(1, "pfr_get_tables"); 5353b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 5363b3a8eb9SGleb Smirnoff break; 5373b3a8eb9SGleb Smirnoff } 5383b3a8eb9SGleb Smirnoff PFRB_FOREACH(t, &b) { 5393b3a8eb9SGleb Smirnoff if (!(t->pfrt_flags & PFR_TFLAG_ACTIVE)) 5403b3a8eb9SGleb Smirnoff continue; 5413b3a8eb9SGleb Smirnoff if (filter != NULL && strcmp(filter, t->pfrt_name)) 5423b3a8eb9SGleb Smirnoff continue; 5433b3a8eb9SGleb Smirnoff if (!t->pfrt_anchor[0]) 5443b3a8eb9SGleb Smirnoff name = t->pfrt_name; 5453b3a8eb9SGleb Smirnoff else if (name != NULL && !strcmp(name, t->pfrt_name)) { 5463b3a8eb9SGleb Smirnoff coll++; 5473b3a8eb9SGleb Smirnoff lastcoll = name; 5483b3a8eb9SGleb Smirnoff name = NULL; 5493b3a8eb9SGleb Smirnoff } 5503b3a8eb9SGleb Smirnoff } 5513b3a8eb9SGleb Smirnoff if (coll == 1) 5523b3a8eb9SGleb Smirnoff warnx("warning: namespace collision with <%s> global table.", 5533b3a8eb9SGleb Smirnoff lastcoll); 5543b3a8eb9SGleb Smirnoff else if (coll > 1) 5553b3a8eb9SGleb Smirnoff warnx("warning: namespace collisions with %d global tables.", 5563b3a8eb9SGleb Smirnoff coll); 5573b3a8eb9SGleb Smirnoff pfr_buf_clear(&b); 5583b3a8eb9SGleb Smirnoff } 5593b3a8eb9SGleb Smirnoff 5603b3a8eb9SGleb Smirnoff void 5613b3a8eb9SGleb Smirnoff xprintf(int opts, const char *fmt, ...) 5623b3a8eb9SGleb Smirnoff { 5633b3a8eb9SGleb Smirnoff va_list args; 5643b3a8eb9SGleb Smirnoff 5653b3a8eb9SGleb Smirnoff if (opts & PF_OPT_QUIET) 5663b3a8eb9SGleb Smirnoff return; 5673b3a8eb9SGleb Smirnoff 5683b3a8eb9SGleb Smirnoff va_start(args, fmt); 5693b3a8eb9SGleb Smirnoff vfprintf(stderr, fmt, args); 5703b3a8eb9SGleb Smirnoff va_end(args); 5713b3a8eb9SGleb Smirnoff 5723b3a8eb9SGleb Smirnoff if (opts & PF_OPT_DUMMYACTION) 5733b3a8eb9SGleb Smirnoff fprintf(stderr, " (dummy).\n"); 5743b3a8eb9SGleb Smirnoff else if (opts & PF_OPT_NOACTION) 5753b3a8eb9SGleb Smirnoff fprintf(stderr, " (syntax only).\n"); 5763b3a8eb9SGleb Smirnoff else 5773b3a8eb9SGleb Smirnoff fprintf(stderr, ".\n"); 5783b3a8eb9SGleb Smirnoff } 5793b3a8eb9SGleb Smirnoff 5803b3a8eb9SGleb Smirnoff 5813b3a8eb9SGleb Smirnoff /* interface stuff */ 5823b3a8eb9SGleb Smirnoff 5833b3a8eb9SGleb Smirnoff int 5843b3a8eb9SGleb Smirnoff pfctl_show_ifaces(const char *filter, int opts) 5853b3a8eb9SGleb Smirnoff { 5863b3a8eb9SGleb Smirnoff struct pfr_buffer b; 5873b3a8eb9SGleb Smirnoff struct pfi_kif *p; 5883b3a8eb9SGleb Smirnoff int i = 0; 5893b3a8eb9SGleb Smirnoff 5903b3a8eb9SGleb Smirnoff bzero(&b, sizeof(b)); 5913b3a8eb9SGleb Smirnoff b.pfrb_type = PFRB_IFACES; 5923b3a8eb9SGleb Smirnoff for (;;) { 5933b3a8eb9SGleb Smirnoff pfr_buf_grow(&b, b.pfrb_size); 5943b3a8eb9SGleb Smirnoff b.pfrb_size = b.pfrb_msize; 5953b3a8eb9SGleb Smirnoff if (pfi_get_ifaces(filter, b.pfrb_caddr, &b.pfrb_size)) { 5963b3a8eb9SGleb Smirnoff radix_perror(); 5973b3a8eb9SGleb Smirnoff return (1); 5983b3a8eb9SGleb Smirnoff } 5993b3a8eb9SGleb Smirnoff if (b.pfrb_size <= b.pfrb_msize) 6003b3a8eb9SGleb Smirnoff break; 6013b3a8eb9SGleb Smirnoff i++; 6023b3a8eb9SGleb Smirnoff } 6033b3a8eb9SGleb Smirnoff if (opts & PF_OPT_SHOWALL) 6043b3a8eb9SGleb Smirnoff pfctl_print_title("INTERFACES:"); 6053b3a8eb9SGleb Smirnoff PFRB_FOREACH(p, &b) 6063b3a8eb9SGleb Smirnoff print_iface(p, opts); 6073b3a8eb9SGleb Smirnoff return (0); 6083b3a8eb9SGleb Smirnoff } 6093b3a8eb9SGleb Smirnoff 6103b3a8eb9SGleb Smirnoff void 6113b3a8eb9SGleb Smirnoff print_iface(struct pfi_kif *p, int opts) 6123b3a8eb9SGleb Smirnoff { 6133b3a8eb9SGleb Smirnoff time_t tzero = p->pfik_tzero; 6143b3a8eb9SGleb Smirnoff int i, af, dir, act; 6153b3a8eb9SGleb Smirnoff 6163b3a8eb9SGleb Smirnoff printf("%s", p->pfik_name); 6173b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE) { 6183b3a8eb9SGleb Smirnoff if (p->pfik_flags & PFI_IFLAG_SKIP) 6193b3a8eb9SGleb Smirnoff printf(" (skip)"); 6203b3a8eb9SGleb Smirnoff } 6213b3a8eb9SGleb Smirnoff printf("\n"); 6223b3a8eb9SGleb Smirnoff 6233b3a8eb9SGleb Smirnoff if (!(opts & PF_OPT_VERBOSE2)) 6243b3a8eb9SGleb Smirnoff return; 6253b3a8eb9SGleb Smirnoff printf("\tCleared: %s", ctime(&tzero)); 6263b3a8eb9SGleb Smirnoff printf("\tReferences: %-18d\n", p->pfik_rulerefs); 6273b3a8eb9SGleb Smirnoff for (i = 0; i < 8; i++) { 6283b3a8eb9SGleb Smirnoff af = (i>>2) & 1; 6293b3a8eb9SGleb Smirnoff dir = (i>>1) &1; 6303b3a8eb9SGleb Smirnoff act = i & 1; 6313b3a8eb9SGleb Smirnoff printf("\t%-12s [ Packets: %-18llu Bytes: %-18llu ]\n", 6323b3a8eb9SGleb Smirnoff istats_text[af][dir][act], 6333b3a8eb9SGleb Smirnoff (unsigned long long)p->pfik_packets[af][dir][act], 6343b3a8eb9SGleb Smirnoff (unsigned long long)p->pfik_bytes[af][dir][act]); 6353b3a8eb9SGleb Smirnoff } 6363b3a8eb9SGleb Smirnoff } 637