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