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