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