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