Home | History | Annotate | Line # | Download | only in npfctl
npf_show.c revision 1.13.4.2
      1  1.13.4.2  yamt /*	$NetBSD: npf_show.c,v 1.13.4.2 2014/05/22 11:43:07 yamt Exp $	*/
      2  1.13.4.2  yamt 
      3  1.13.4.2  yamt /*-
      4  1.13.4.2  yamt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.13.4.2  yamt  * All rights reserved.
      6  1.13.4.2  yamt  *
      7  1.13.4.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13.4.2  yamt  * by Mindaugas Rasiukevicius.
      9  1.13.4.2  yamt  *
     10  1.13.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.13.4.2  yamt  * modification, are permitted provided that the following conditions
     12  1.13.4.2  yamt  * are met:
     13  1.13.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.13.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.13.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.13.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.13.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.13.4.2  yamt  *
     19  1.13.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.13.4.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.13.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.13.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.13.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.13.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.13.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.13.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.13.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.13.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.13.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.13.4.2  yamt  */
     31  1.13.4.2  yamt 
     32  1.13.4.2  yamt /*
     33  1.13.4.2  yamt  * NPF configuration printing.
     34  1.13.4.2  yamt  *
     35  1.13.4.2  yamt  * Each rule having BPF byte-code has a binary description.
     36  1.13.4.2  yamt  */
     37  1.13.4.2  yamt 
     38  1.13.4.2  yamt #include <sys/cdefs.h>
     39  1.13.4.2  yamt __RCSID("$NetBSD: npf_show.c,v 1.13.4.2 2014/05/22 11:43:07 yamt Exp $");
     40  1.13.4.2  yamt 
     41  1.13.4.2  yamt #include <sys/socket.h>
     42  1.13.4.2  yamt #include <netinet/in.h>
     43  1.13.4.2  yamt #include <netinet/tcp.h>
     44  1.13.4.2  yamt #include <net/if.h>
     45  1.13.4.2  yamt 
     46  1.13.4.2  yamt #include <stdio.h>
     47  1.13.4.2  yamt #include <stdlib.h>
     48  1.13.4.2  yamt #include <string.h>
     49  1.13.4.2  yamt #include <stdbool.h>
     50  1.13.4.2  yamt #include <inttypes.h>
     51  1.13.4.2  yamt #include <errno.h>
     52  1.13.4.2  yamt #include <err.h>
     53  1.13.4.2  yamt 
     54  1.13.4.2  yamt #include "npfctl.h"
     55  1.13.4.2  yamt 
     56  1.13.4.2  yamt typedef struct {
     57  1.13.4.2  yamt 	nl_config_t *	conf;
     58  1.13.4.2  yamt 	FILE *		fp;
     59  1.13.4.2  yamt 	long		fpos;
     60  1.13.4.2  yamt } npf_conf_info_t;
     61  1.13.4.2  yamt 
     62  1.13.4.2  yamt static npf_conf_info_t	stdout_ctx = { .fp = stdout, .fpos = 0 };
     63  1.13.4.2  yamt 
     64  1.13.4.2  yamt static void	print_indent(npf_conf_info_t *, u_int);
     65  1.13.4.2  yamt static void	print_linesep(npf_conf_info_t *);
     66  1.13.4.2  yamt 
     67  1.13.4.2  yamt /*
     68  1.13.4.2  yamt  * Helper routines to print various pieces of information.
     69  1.13.4.2  yamt  */
     70  1.13.4.2  yamt 
     71  1.13.4.2  yamt static void
     72  1.13.4.2  yamt print_indent(npf_conf_info_t *ctx, u_int level)
     73  1.13.4.2  yamt {
     74  1.13.4.2  yamt 	if (level == 0) { /* XXX */
     75  1.13.4.2  yamt 		print_linesep(ctx);
     76  1.13.4.2  yamt 	}
     77  1.13.4.2  yamt 	while (level--)
     78  1.13.4.2  yamt 		fprintf(ctx->fp, "\t");
     79  1.13.4.2  yamt }
     80  1.13.4.2  yamt 
     81  1.13.4.2  yamt static void
     82  1.13.4.2  yamt print_linesep(npf_conf_info_t *ctx)
     83  1.13.4.2  yamt {
     84  1.13.4.2  yamt 	if (ftell(ctx->fp) != ctx->fpos) {
     85  1.13.4.2  yamt 		fputs("\n", ctx->fp);
     86  1.13.4.2  yamt 		ctx->fpos = ftell(ctx->fp);
     87  1.13.4.2  yamt 	}
     88  1.13.4.2  yamt }
     89  1.13.4.2  yamt 
     90  1.13.4.2  yamt static size_t
     91  1.13.4.2  yamt tcpflags2string(char *buf, u_int tfl)
     92  1.13.4.2  yamt {
     93  1.13.4.2  yamt 	u_int i = 0;
     94  1.13.4.2  yamt 
     95  1.13.4.2  yamt 	if (tfl & TH_FIN)	buf[i++] = 'F';
     96  1.13.4.2  yamt 	if (tfl & TH_SYN)	buf[i++] = 'S';
     97  1.13.4.2  yamt 	if (tfl & TH_RST)	buf[i++] = 'R';
     98  1.13.4.2  yamt 	if (tfl & TH_PUSH)	buf[i++] = 'P';
     99  1.13.4.2  yamt 	if (tfl & TH_ACK)	buf[i++] = 'A';
    100  1.13.4.2  yamt 	if (tfl & TH_URG)	buf[i++] = 'U';
    101  1.13.4.2  yamt 	if (tfl & TH_ECE)	buf[i++] = 'E';
    102  1.13.4.2  yamt 	if (tfl & TH_CWR)	buf[i++] = 'C';
    103  1.13.4.2  yamt 	buf[i] = '\0';
    104  1.13.4.2  yamt 	return i;
    105  1.13.4.2  yamt }
    106  1.13.4.2  yamt 
    107  1.13.4.2  yamt static char *
    108  1.13.4.2  yamt print_family(npf_conf_info_t *ctx, const uint32_t *words)
    109  1.13.4.2  yamt {
    110  1.13.4.2  yamt 	const int af = words[0];
    111  1.13.4.2  yamt 
    112  1.13.4.2  yamt 	switch (af) {
    113  1.13.4.2  yamt 	case AF_INET:
    114  1.13.4.2  yamt 		return estrdup("inet4");
    115  1.13.4.2  yamt 	case AF_INET6:
    116  1.13.4.2  yamt 		return estrdup("inet6");
    117  1.13.4.2  yamt 	default:
    118  1.13.4.2  yamt 		errx(EXIT_FAILURE, "invalid byte-code mark (family)");
    119  1.13.4.2  yamt 	}
    120  1.13.4.2  yamt 	return NULL;
    121  1.13.4.2  yamt }
    122  1.13.4.2  yamt 
    123  1.13.4.2  yamt static char *
    124  1.13.4.2  yamt print_address(npf_conf_info_t *ctx, const uint32_t *words)
    125  1.13.4.2  yamt {
    126  1.13.4.2  yamt 	const int af = *words++;
    127  1.13.4.2  yamt 	const u_int mask = *words++;
    128  1.13.4.2  yamt 	const npf_addr_t *addr;
    129  1.13.4.2  yamt 	int alen = 0;
    130  1.13.4.2  yamt 
    131  1.13.4.2  yamt 	switch (af) {
    132  1.13.4.2  yamt 	case AF_INET:
    133  1.13.4.2  yamt 		alen = 4;
    134  1.13.4.2  yamt 		break;
    135  1.13.4.2  yamt 	case AF_INET6:
    136  1.13.4.2  yamt 		alen = 16;
    137  1.13.4.2  yamt 		break;
    138  1.13.4.2  yamt 	default:
    139  1.13.4.2  yamt 		errx(EXIT_FAILURE, "invalid byte-code mark (address)");
    140  1.13.4.2  yamt 	}
    141  1.13.4.2  yamt 	addr = (const npf_addr_t *)words;
    142  1.13.4.2  yamt 	return npfctl_print_addrmask(alen, addr, mask);
    143  1.13.4.2  yamt }
    144  1.13.4.2  yamt 
    145  1.13.4.2  yamt static char *
    146  1.13.4.2  yamt print_number(npf_conf_info_t *ctx, const uint32_t *words)
    147  1.13.4.2  yamt {
    148  1.13.4.2  yamt 	char *p;
    149  1.13.4.2  yamt 	easprintf(&p, "%u", words[0]);
    150  1.13.4.2  yamt 	return p;
    151  1.13.4.2  yamt }
    152  1.13.4.2  yamt 
    153  1.13.4.2  yamt static char *
    154  1.13.4.2  yamt print_table(npf_conf_info_t *ctx, const uint32_t *words)
    155  1.13.4.2  yamt {
    156  1.13.4.2  yamt 	unsigned tid = words[0];
    157  1.13.4.2  yamt 	nl_table_t *tl;
    158  1.13.4.2  yamt 	char *p = NULL;
    159  1.13.4.2  yamt 
    160  1.13.4.2  yamt 	/* XXX: Iterating all as we need to rewind for the next call. */
    161  1.13.4.2  yamt 	while ((tl = npf_table_iterate(ctx->conf)) != NULL) {
    162  1.13.4.2  yamt 		if (!p && npf_table_getid(tl) == tid) {
    163  1.13.4.2  yamt 			easprintf(&p, "%s", npf_table_getname(tl));
    164  1.13.4.2  yamt 		}
    165  1.13.4.2  yamt 	}
    166  1.13.4.2  yamt 	assert(p != NULL);
    167  1.13.4.2  yamt 	return p;
    168  1.13.4.2  yamt }
    169  1.13.4.2  yamt 
    170  1.13.4.2  yamt static char *
    171  1.13.4.2  yamt print_proto(npf_conf_info_t *ctx, const uint32_t *words)
    172  1.13.4.2  yamt {
    173  1.13.4.2  yamt 	switch (words[0]) {
    174  1.13.4.2  yamt 	case IPPROTO_TCP:
    175  1.13.4.2  yamt 		return estrdup("tcp");
    176  1.13.4.2  yamt 	case IPPROTO_UDP:
    177  1.13.4.2  yamt 		return estrdup("udp");
    178  1.13.4.2  yamt 	case IPPROTO_ICMP:
    179  1.13.4.2  yamt 		return estrdup("icmp");
    180  1.13.4.2  yamt 	case IPPROTO_ICMPV6:
    181  1.13.4.2  yamt 		return estrdup("ipv6-icmp");
    182  1.13.4.2  yamt 	}
    183  1.13.4.2  yamt 	return print_number(ctx, words);
    184  1.13.4.2  yamt }
    185  1.13.4.2  yamt 
    186  1.13.4.2  yamt static char *
    187  1.13.4.2  yamt print_tcpflags(npf_conf_info_t *ctx, const uint32_t *words)
    188  1.13.4.2  yamt {
    189  1.13.4.2  yamt 	const u_int tf = words[0], tf_mask = words[1];
    190  1.13.4.2  yamt 	char buf[16];
    191  1.13.4.2  yamt 
    192  1.13.4.2  yamt 	size_t n = tcpflags2string(buf, tf);
    193  1.13.4.2  yamt 	if (tf != tf_mask) {
    194  1.13.4.2  yamt 		buf[n++] = '/';
    195  1.13.4.2  yamt 		tcpflags2string(buf + n, tf_mask);
    196  1.13.4.2  yamt 	}
    197  1.13.4.2  yamt 	return estrdup(buf);
    198  1.13.4.2  yamt }
    199  1.13.4.2  yamt 
    200  1.13.4.2  yamt static char *
    201  1.13.4.2  yamt print_portrange(npf_conf_info_t *ctx, const uint32_t *words)
    202  1.13.4.2  yamt {
    203  1.13.4.2  yamt 	u_int fport = words[0], tport = words[1];
    204  1.13.4.2  yamt 	char *p;
    205  1.13.4.2  yamt 
    206  1.13.4.2  yamt 	if (fport != tport) {
    207  1.13.4.2  yamt 		easprintf(&p, "%u:%u", fport, tport);
    208  1.13.4.2  yamt 	} else {
    209  1.13.4.2  yamt 		easprintf(&p, "%u", fport);
    210  1.13.4.2  yamt 	}
    211  1.13.4.2  yamt 	return p;
    212  1.13.4.2  yamt }
    213  1.13.4.2  yamt 
    214  1.13.4.2  yamt /*
    215  1.13.4.2  yamt  * The main keyword mapping tables defining the syntax:
    216  1.13.4.2  yamt  * - Mapping of rule attributes (flags) to the keywords.
    217  1.13.4.2  yamt  * - Mapping of the byte-code marks to the keywords.
    218  1.13.4.2  yamt  */
    219  1.13.4.2  yamt 
    220  1.13.4.2  yamt #define	F(name)		__CONCAT(NPF_RULE_, name)
    221  1.13.4.2  yamt #define	STATEFUL_ENDS	(NPF_RULE_STATEFUL | NPF_RULE_MULTIENDS)
    222  1.13.4.2  yamt #define	NAME_AT		2
    223  1.13.4.2  yamt 
    224  1.13.4.2  yamt static const struct attr_keyword_mapent {
    225  1.13.4.2  yamt 	uint32_t	mask;
    226  1.13.4.2  yamt 	uint32_t	flags;
    227  1.13.4.2  yamt 	const char *	val;
    228  1.13.4.2  yamt } attr_keyword_map[] = {
    229  1.13.4.2  yamt 	{ F(GROUP)|F(DYNAMIC),	F(GROUP),		"group"		},
    230  1.13.4.2  yamt 	{ F(DYNAMIC),		F(DYNAMIC),		"ruleset"	},
    231  1.13.4.2  yamt 	{ F(GROUP)|F(PASS),	0,			"block"		},
    232  1.13.4.2  yamt 	{ F(GROUP)|F(PASS),	F(PASS),		"pass"		},
    233  1.13.4.2  yamt 	{ F(RETRST)|F(RETICMP),	F(RETRST)|F(RETICMP),	"return"	},
    234  1.13.4.2  yamt 	{ F(RETRST)|F(RETICMP),	F(RETRST),		"return-rst"	},
    235  1.13.4.2  yamt 	{ F(RETRST)|F(RETICMP),	F(RETICMP),		"return-icmp"	},
    236  1.13.4.2  yamt 	{ STATEFUL_ENDS,	F(STATEFUL),		"stateful"	},
    237  1.13.4.2  yamt 	{ STATEFUL_ENDS,	STATEFUL_ENDS,		"stateful-ends"	},
    238  1.13.4.2  yamt 	{ F(DIMASK),		F(IN),			"in"		},
    239  1.13.4.2  yamt 	{ F(DIMASK),		F(OUT),			"out"		},
    240  1.13.4.2  yamt 	{ F(FINAL),		F(FINAL),		"final"		},
    241  1.13.4.2  yamt };
    242  1.13.4.2  yamt 
    243  1.13.4.2  yamt static const struct mark_keyword_mapent {
    244  1.13.4.2  yamt 	u_int		mark;
    245  1.13.4.2  yamt 	const char *	token;
    246  1.13.4.2  yamt 	const char *	sep;
    247  1.13.4.2  yamt 	char *		(*printfn)(npf_conf_info_t *, const uint32_t *);
    248  1.13.4.2  yamt 	u_int		fwords;
    249  1.13.4.2  yamt } mark_keyword_map[] = {
    250  1.13.4.2  yamt 	{ BM_IPVER,	"family %s",	NULL,		print_family,	1 },
    251  1.13.4.2  yamt 	{ BM_PROTO,	"proto %s",	NULL,		print_proto,	1 },
    252  1.13.4.2  yamt 	{ BM_TCPFL,	"flags %s",	NULL,		print_tcpflags,	2 },
    253  1.13.4.2  yamt 	{ BM_ICMP_TYPE,	"icmp-type %s",	NULL,		print_number,	1 },
    254  1.13.4.2  yamt 	{ BM_ICMP_CODE,	"code %s",	NULL,		print_number,	1 },
    255  1.13.4.2  yamt 
    256  1.13.4.2  yamt 	{ BM_SRC_CIDR,	"from %s",	", ",		print_address,	6 },
    257  1.13.4.2  yamt 	{ BM_SRC_TABLE,	"from <%s>",	NULL,		print_table,	1 },
    258  1.13.4.2  yamt 	{ BM_SRC_PORTS,	"port %s",	", ",		print_portrange,2 },
    259  1.13.4.2  yamt 
    260  1.13.4.2  yamt 	{ BM_DST_CIDR,	"to %s",	", ",		print_address,	6 },
    261  1.13.4.2  yamt 	{ BM_DST_TABLE,	"to <%s>",	NULL,		print_table,	1 },
    262  1.13.4.2  yamt 	{ BM_DST_PORTS,	"port %s",	", ",		print_portrange,2 },
    263  1.13.4.2  yamt };
    264  1.13.4.2  yamt 
    265  1.13.4.2  yamt static const char * __attribute__((format_arg(2)))
    266  1.13.4.2  yamt verified_fmt(const char *fmt, const char *t __unused)
    267  1.13.4.2  yamt {
    268  1.13.4.2  yamt 	return fmt;
    269  1.13.4.2  yamt }
    270  1.13.4.2  yamt 
    271  1.13.4.2  yamt static char *
    272  1.13.4.2  yamt scan_marks(npf_conf_info_t *ctx, const struct mark_keyword_mapent *mk,
    273  1.13.4.2  yamt     const uint32_t *marks, size_t mlen)
    274  1.13.4.2  yamt {
    275  1.13.4.2  yamt 	char buf[2048], *vals[256], *p;
    276  1.13.4.2  yamt 	size_t nvals = 0;
    277  1.13.4.2  yamt 
    278  1.13.4.2  yamt 	/* Scan for the marks and extract the values. */
    279  1.13.4.2  yamt 	mlen /= sizeof(uint32_t);
    280  1.13.4.2  yamt 	while (mlen > 2) {
    281  1.13.4.2  yamt 		const uint32_t m = *marks++;
    282  1.13.4.2  yamt 		const u_int nwords = *marks++;
    283  1.13.4.2  yamt 
    284  1.13.4.2  yamt 		if ((mlen -= 2) < nwords) {
    285  1.13.4.2  yamt 			errx(EXIT_FAILURE, "byte-code marking inconsistency");
    286  1.13.4.2  yamt 		}
    287  1.13.4.2  yamt 		if (m == mk->mark) {
    288  1.13.4.2  yamt 			/* Value is processed by the print function. */
    289  1.13.4.2  yamt 			assert(mk->fwords == nwords);
    290  1.13.4.2  yamt 			vals[nvals++] = mk->printfn(ctx, marks);
    291  1.13.4.2  yamt 		}
    292  1.13.4.2  yamt 		marks += nwords;
    293  1.13.4.2  yamt 		mlen -= nwords;
    294  1.13.4.2  yamt 	}
    295  1.13.4.2  yamt 	if (nvals == 0) {
    296  1.13.4.2  yamt 		return NULL;
    297  1.13.4.2  yamt 	}
    298  1.13.4.2  yamt 	assert(nvals == 1 || mk->sep != NULL);
    299  1.13.4.2  yamt 
    300  1.13.4.2  yamt 	/*
    301  1.13.4.2  yamt 	 * Join all the values and print.  Add curly brackets if there
    302  1.13.4.2  yamt 	 * is more than value and it can be a set.
    303  1.13.4.2  yamt 	 */
    304  1.13.4.2  yamt 	if (!join(buf, sizeof(buf), nvals, vals, mk->sep ? mk->sep : "")) {
    305  1.13.4.2  yamt 		errx(EXIT_FAILURE, "out of memory while parsing the rule");
    306  1.13.4.2  yamt 	}
    307  1.13.4.2  yamt 	easprintf(&p, nvals > 1 ? "{ %s }" : "%s", buf);
    308  1.13.4.2  yamt 
    309  1.13.4.2  yamt 	for (u_int i = 0; i < nvals; i++) {
    310  1.13.4.2  yamt 		free(vals[i]);
    311  1.13.4.2  yamt 	}
    312  1.13.4.2  yamt 	return p;
    313  1.13.4.2  yamt }
    314  1.13.4.2  yamt 
    315  1.13.4.2  yamt static void
    316  1.13.4.2  yamt npfctl_print_filter(npf_conf_info_t *ctx, nl_rule_t *rl)
    317  1.13.4.2  yamt {
    318  1.13.4.2  yamt 	const void *marks;
    319  1.13.4.2  yamt 	size_t mlen;
    320  1.13.4.2  yamt 
    321  1.13.4.2  yamt 	/* BPF filter criteria described by the byte-code marks. */
    322  1.13.4.2  yamt 	marks = npf_rule_getinfo(rl, &mlen);
    323  1.13.4.2  yamt 	for (u_int i = 0; i < __arraycount(mark_keyword_map); i++) {
    324  1.13.4.2  yamt 		const struct mark_keyword_mapent *mk = &mark_keyword_map[i];
    325  1.13.4.2  yamt 		char *val;
    326  1.13.4.2  yamt 
    327  1.13.4.2  yamt 		if ((val = scan_marks(ctx, mk, marks, mlen)) != NULL) {
    328  1.13.4.2  yamt 			fprintf(ctx->fp, verified_fmt(mk->token, "%s"), val);
    329  1.13.4.2  yamt 			fputs(" ", ctx->fp);
    330  1.13.4.2  yamt 			free(val);
    331  1.13.4.2  yamt 		}
    332  1.13.4.2  yamt 	}
    333  1.13.4.2  yamt 	if (!mlen) {
    334  1.13.4.2  yamt 		fputs("all ", ctx->fp);
    335  1.13.4.2  yamt 	}
    336  1.13.4.2  yamt }
    337  1.13.4.2  yamt 
    338  1.13.4.2  yamt static void
    339  1.13.4.2  yamt npfctl_print_rule(npf_conf_info_t *ctx, nl_rule_t *rl)
    340  1.13.4.2  yamt {
    341  1.13.4.2  yamt 	const uint32_t attr = npf_rule_getattr(rl);
    342  1.13.4.2  yamt 	const char *rproc, *ifname, *name;
    343  1.13.4.2  yamt 
    344  1.13.4.2  yamt 	/* Rule attributes/flags. */
    345  1.13.4.2  yamt 	for (u_int i = 0; i < __arraycount(attr_keyword_map); i++) {
    346  1.13.4.2  yamt 		const struct attr_keyword_mapent *ak = &attr_keyword_map[i];
    347  1.13.4.2  yamt 
    348  1.13.4.2  yamt 		if (i == NAME_AT && (name = npf_rule_getname(rl)) != NULL) {
    349  1.13.4.2  yamt 			fprintf(ctx->fp, "\"%s\" ", name);
    350  1.13.4.2  yamt 		}
    351  1.13.4.2  yamt 		if ((attr & ak->mask) == ak->flags) {
    352  1.13.4.2  yamt 			fprintf(ctx->fp, "%s ", ak->val);
    353  1.13.4.2  yamt 		}
    354  1.13.4.2  yamt 	}
    355  1.13.4.2  yamt 	if ((ifname = npf_rule_getinterface(rl)) != NULL) {
    356  1.13.4.2  yamt 		fprintf(ctx->fp, "on %s ", ifname);
    357  1.13.4.2  yamt 	}
    358  1.13.4.2  yamt 
    359  1.13.4.2  yamt 	if ((attr & (NPF_RULE_GROUP | NPF_RULE_DYNAMIC)) == NPF_RULE_GROUP) {
    360  1.13.4.2  yamt 		/* Group; done. */
    361  1.13.4.2  yamt 		fputs("\n", ctx->fp);
    362  1.13.4.2  yamt 		return;
    363  1.13.4.2  yamt 	}
    364  1.13.4.2  yamt 
    365  1.13.4.2  yamt 	/* Print filter criteria. */
    366  1.13.4.2  yamt 	npfctl_print_filter(ctx, rl);
    367  1.13.4.2  yamt 
    368  1.13.4.2  yamt 	/* Rule procedure. */
    369  1.13.4.2  yamt 	if ((rproc = npf_rule_getproc(rl)) != NULL) {
    370  1.13.4.2  yamt 		fprintf(ctx->fp, "apply \"%s\"", rproc);
    371  1.13.4.2  yamt 	}
    372  1.13.4.2  yamt 	fputs("\n", ctx->fp);
    373  1.13.4.2  yamt }
    374  1.13.4.2  yamt 
    375  1.13.4.2  yamt static void
    376  1.13.4.2  yamt npfctl_print_nat(npf_conf_info_t *ctx, nl_nat_t *nt)
    377  1.13.4.2  yamt {
    378  1.13.4.2  yamt 	nl_rule_t *rl = (nl_nat_t *)nt;
    379  1.13.4.2  yamt 	const char *ifname, *seg1, *seg2, *arrow;
    380  1.13.4.2  yamt 	npf_addr_t addr;
    381  1.13.4.2  yamt 	in_port_t port;
    382  1.13.4.2  yamt 	size_t alen;
    383  1.13.4.2  yamt 	u_int flags;
    384  1.13.4.2  yamt 	char *seg;
    385  1.13.4.2  yamt 
    386  1.13.4.2  yamt 	/* Get the interface. */
    387  1.13.4.2  yamt 	ifname = npf_rule_getinterface(rl);
    388  1.13.4.2  yamt 	assert(ifname != NULL);
    389  1.13.4.2  yamt 
    390  1.13.4.2  yamt 	/* Get the translation address (and port, if used). */
    391  1.13.4.2  yamt 	npf_nat_getmap(nt, &addr, &alen, &port);
    392  1.13.4.2  yamt 	seg = npfctl_print_addrmask(alen, &addr, NPF_NO_NETMASK);
    393  1.13.4.2  yamt 	if (port) {
    394  1.13.4.2  yamt 		char *p;
    395  1.13.4.2  yamt 		easprintf(&p, "%s port %u", seg, ntohs(port));
    396  1.13.4.2  yamt 		free(seg), seg = p;
    397  1.13.4.2  yamt 	}
    398  1.13.4.2  yamt 	seg1 = seg2 = "any";
    399  1.13.4.2  yamt 
    400  1.13.4.2  yamt 	/* Get the NAT type and determine the translation segment. */
    401  1.13.4.2  yamt 	switch (npf_nat_gettype(nt)) {
    402  1.13.4.2  yamt 	case NPF_NATIN:
    403  1.13.4.2  yamt 		arrow = "<-";
    404  1.13.4.2  yamt 		seg1 = seg;
    405  1.13.4.2  yamt 		break;
    406  1.13.4.2  yamt 	case NPF_NATOUT:
    407  1.13.4.2  yamt 		arrow = "->";
    408  1.13.4.2  yamt 		seg2 = seg;
    409  1.13.4.2  yamt 		break;
    410  1.13.4.2  yamt 	default:
    411  1.13.4.2  yamt 		abort();
    412  1.13.4.2  yamt 	}
    413  1.13.4.2  yamt 	flags = npf_nat_getflags(nt);
    414  1.13.4.2  yamt 
    415  1.13.4.2  yamt 	/* Print out the NAT policy with the filter criteria. */
    416  1.13.4.2  yamt 	fprintf(ctx->fp, "map %s %s %s %s %s pass ",
    417  1.13.4.2  yamt 	    ifname, (flags & NPF_NAT_STATIC) ? "static" : "dynamic",
    418  1.13.4.2  yamt 	    seg1, arrow, seg2);
    419  1.13.4.2  yamt 	npfctl_print_filter(ctx, rl);
    420  1.13.4.2  yamt 	fputs("\n", ctx->fp);
    421  1.13.4.2  yamt 	free(seg);
    422  1.13.4.2  yamt }
    423  1.13.4.2  yamt 
    424  1.13.4.2  yamt static void
    425  1.13.4.2  yamt npfctl_print_table(npf_conf_info_t *ctx, nl_table_t *tl)
    426  1.13.4.2  yamt {
    427  1.13.4.2  yamt 	const char *name = npf_table_getname(tl);
    428  1.13.4.2  yamt 	const unsigned type = npf_table_gettype(tl);
    429  1.13.4.2  yamt 	const char *table_types[] = {
    430  1.13.4.2  yamt 		[NPF_TABLE_HASH] = "hash",
    431  1.13.4.2  yamt 		[NPF_TABLE_TREE] = "tree",
    432  1.13.4.2  yamt 		[NPF_TABLE_CDB]  = "cdb",
    433  1.13.4.2  yamt 	};
    434  1.13.4.2  yamt 
    435  1.13.4.2  yamt 	if (name[0] == '.') {
    436  1.13.4.2  yamt 		/* Internal tables use dot and are hidden. */
    437  1.13.4.2  yamt 		return;
    438  1.13.4.2  yamt 	}
    439  1.13.4.2  yamt 	assert(type < __arraycount(table_types));
    440  1.13.4.2  yamt 	fprintf(ctx->fp, "table <%s> type %s\n", name, table_types[type]);
    441  1.13.4.2  yamt }
    442  1.13.4.2  yamt 
    443  1.13.4.2  yamt int
    444  1.13.4.2  yamt npfctl_config_show(int fd)
    445  1.13.4.2  yamt {
    446  1.13.4.2  yamt 	npf_conf_info_t *ctx = &stdout_ctx;
    447  1.13.4.2  yamt 	nl_config_t *ncf;
    448  1.13.4.2  yamt 	bool active, loaded;
    449  1.13.4.2  yamt 
    450  1.13.4.2  yamt 	if (fd) {
    451  1.13.4.2  yamt 		ncf = npf_config_retrieve(fd, &active, &loaded);
    452  1.13.4.2  yamt 		if (ncf == NULL) {
    453  1.13.4.2  yamt 			return errno;
    454  1.13.4.2  yamt 		}
    455  1.13.4.2  yamt 		fprintf(ctx->fp, "Filtering:\t%s\nConfiguration:\t%s\n",
    456  1.13.4.2  yamt 		    active ? "active" : "inactive",
    457  1.13.4.2  yamt 		    loaded ? "loaded" : "empty");
    458  1.13.4.2  yamt 		print_linesep(ctx);
    459  1.13.4.2  yamt 	} else {
    460  1.13.4.2  yamt 		npfctl_config_send(0, NULL);
    461  1.13.4.2  yamt 		ncf = npfctl_config_ref();
    462  1.13.4.2  yamt 		loaded = true;
    463  1.13.4.2  yamt 	}
    464  1.13.4.2  yamt 	ctx->conf = ncf;
    465  1.13.4.2  yamt 
    466  1.13.4.2  yamt 	if (loaded) {
    467  1.13.4.2  yamt 		nl_rule_t *rl;
    468  1.13.4.2  yamt 		nl_rproc_t *rp;
    469  1.13.4.2  yamt 		nl_nat_t *nt;
    470  1.13.4.2  yamt 		nl_table_t *tl;
    471  1.13.4.2  yamt 		u_int level;
    472  1.13.4.2  yamt 
    473  1.13.4.2  yamt 		while ((tl = npf_table_iterate(ncf)) != NULL) {
    474  1.13.4.2  yamt 			npfctl_print_table(ctx, tl);
    475  1.13.4.2  yamt 		}
    476  1.13.4.2  yamt 		print_linesep(ctx);
    477  1.13.4.2  yamt 
    478  1.13.4.2  yamt 		while ((rp = npf_rproc_iterate(ncf)) != NULL) {
    479  1.13.4.2  yamt 			const char *rpname = npf_rproc_getname(rp);
    480  1.13.4.2  yamt 			fprintf(ctx->fp, "procedure \"%s\"\n", rpname);
    481  1.13.4.2  yamt 		}
    482  1.13.4.2  yamt 		print_linesep(ctx);
    483  1.13.4.2  yamt 
    484  1.13.4.2  yamt 		while ((nt = npf_nat_iterate(ncf)) != NULL) {
    485  1.13.4.2  yamt 			npfctl_print_nat(ctx, nt);
    486  1.13.4.2  yamt 		}
    487  1.13.4.2  yamt 		print_linesep(ctx);
    488  1.13.4.2  yamt 
    489  1.13.4.2  yamt 		while ((rl = npf_rule_iterate(ncf, &level)) != NULL) {
    490  1.13.4.2  yamt 			print_indent(ctx, level);
    491  1.13.4.2  yamt 			npfctl_print_rule(ctx, rl);
    492  1.13.4.2  yamt 		}
    493  1.13.4.2  yamt 		print_linesep(ctx);
    494  1.13.4.2  yamt 	}
    495  1.13.4.2  yamt 	npf_config_destroy(ncf);
    496  1.13.4.2  yamt 	return 0;
    497  1.13.4.2  yamt }
    498  1.13.4.2  yamt 
    499  1.13.4.2  yamt int
    500  1.13.4.2  yamt npfctl_ruleset_show(int fd, const char *ruleset_name)
    501  1.13.4.2  yamt {
    502  1.13.4.2  yamt 	npf_conf_info_t *ctx = &stdout_ctx;
    503  1.13.4.2  yamt 	nl_config_t *ncf;
    504  1.13.4.2  yamt 	nl_rule_t *rl;
    505  1.13.4.2  yamt 	u_int level;
    506  1.13.4.2  yamt 	int error;
    507  1.13.4.2  yamt 
    508  1.13.4.2  yamt 	ncf = npf_config_create();
    509  1.13.4.2  yamt 	ctx->conf = ncf;
    510  1.13.4.2  yamt 
    511  1.13.4.2  yamt 	if ((error = _npf_ruleset_list(fd, ruleset_name, ncf)) != 0) {
    512  1.13.4.2  yamt 		return error;
    513  1.13.4.2  yamt 	}
    514  1.13.4.2  yamt 	while ((rl = npf_rule_iterate(ncf, &level)) != NULL) {
    515  1.13.4.2  yamt 		npfctl_print_rule(ctx, rl);
    516  1.13.4.2  yamt 	}
    517  1.13.4.2  yamt 	npf_config_destroy(ncf);
    518  1.13.4.2  yamt 	return error;
    519  1.13.4.2  yamt }
    520