summaryrefslogtreecommitdiff
path: root/usr.sbin/npf/npfctl/npf_show.c
blob: 8e889459cad3f39bc674b97f9a75a29358eb1bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*-
 * Copyright (c) 2013-2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Mindaugas Rasiukevicius.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * NPF configuration printing.
 *
 * Each rule having BPF byte-code has a binary description.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD");

#include <sys/socket.h>
#define	__FAVOR_BSD
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <net/if.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>
#include <errno.h>
#include <err.h>

#include "npfctl.h"

#define	SEEN_PROTO	0x01

typedef struct {
	char **		values;
	unsigned	count;
} elem_list_t;

enum {
	LIST_PROTO = 0, LIST_SADDR, LIST_DADDR, LIST_SPORT, LIST_DPORT,
	LIST_COUNT,
};

typedef struct {
	nl_config_t *	conf;
	bool		validating;

	FILE *		fp;
	long		fpos;
	long		fposln;
	int		glevel;

	unsigned	flags;
	uint32_t	curmark;
	uint64_t	seen_marks;
	elem_list_t	list[LIST_COUNT];

} npf_conf_info_t;

static void	print_linesep(npf_conf_info_t *);

static npf_conf_info_t *
npfctl_show_init(void)
{
	static npf_conf_info_t stdout_ctx;
	memset(&stdout_ctx, 0, sizeof(npf_conf_info_t));
	stdout_ctx.glevel = -1;
	stdout_ctx.fp = stdout;
	return &stdout_ctx;
}

static void
list_push(elem_list_t *list, char *val)
{
	const unsigned n = list->count;
	char **values;

	if ((values = calloc(n + 1, sizeof(char *))) == NULL) {
		err(EXIT_FAILURE, "calloc");
	}
	for (unsigned i = 0; i < n; i++) {
		values[i] = list->values[i];
	}
	values[n] = val;
	free(list->values);
	list->values = values;
	list->count++;
}

static char *
list_join_free(elem_list_t *list, const bool use_br, const char *sep)
{
	char *s, buf[2048];

	if (!join(buf, sizeof(buf), list->count, list->values, sep)) {
		errx(EXIT_FAILURE, "out of memory while parsing the rule");
	}
	easprintf(&s, (use_br && list->count > 1) ? "{ %s }" : "%s", buf);
	for (unsigned i = 0; i < list->count; i++) {
		free(list->values[i]);
	}
	free(list->values);
	list->values = NULL;
	list->count = 0;
	return s;
}

/*
 * Helper routines to print various pieces of information.
 */

static void
print_indent(npf_conf_info_t *ctx, unsigned level)
{
	if (ctx->glevel >= 0 && level <= (unsigned)ctx->glevel) {
		/*
		 * Level decrease -- end of the group.
		 * Print the group closing curly bracket.
		 */
		ctx->fpos += fprintf(ctx->fp, "}\n\n");
		ctx->glevel = -1;
	}
	while (level--) {
		ctx->fpos += fprintf(ctx->fp, "\t");
	}
}

static void
print_linesep(npf_conf_info_t *ctx)
{
	if (ctx->fpos != ctx->fposln) {
		ctx->fpos += fprintf(ctx->fp, "\n");
		ctx->fposln = ctx->fpos;
	}
}

static size_t
tcpflags2string(char *buf, unsigned tfl)
{
	unsigned i = 0;

	if (tfl & TH_FIN)	buf[i++] = 'F';
	if (tfl & TH_SYN)	buf[i++] = 'S';
	if (tfl & TH_RST)	buf[i++] = 'R';
	if (tfl & TH_PUSH)	buf[i++] = 'P';
	if (tfl & TH_ACK)	buf[i++] = 'A';
	if (tfl & TH_URG)	buf[i++] = 'U';
	if (tfl & TH_ECE)	buf[i++] = 'E';
	if (tfl & TH_CWR)	buf[i++] = 'W';
	buf[i] = '\0';
	return i;
}

static char *
print_family(npf_conf_info_t *ctx __unused, const uint32_t *words)
{
	const int af = words[0];

	switch (af) {
	case AF_INET:
		return estrdup("inet4");
	case AF_INET6:
		return estrdup("inet6");
	default:
		errx(EXIT_FAILURE, "invalid byte-code mark (family)");
	}
	return NULL;
}

static char *
print_address(npf_conf_info_t *ctx __unused, const uint32_t *words)
{
	const int af = *words++;
	const unsigned mask = *words++;
	const npf_addr_t *addr;
	int alen = 0;

	switch (af) {
	case AF_INET:
		alen = 4;
		break;
	case AF_INET6:
		alen = 16;
		break;
	default:
		errx(EXIT_FAILURE, "invalid byte-code mark (address)");
	}
	addr = (const npf_addr_t *)words;
	return npfctl_print_addrmask(alen, "%a", addr, mask);
}

static char *
print_number(npf_conf_info_t *ctx __unused, const uint32_t *words)
{
	char *p;
	easprintf(&p, "%u", words[0]);
	return p;
}

static char *
print_table(npf_conf_info_t *ctx, const uint32_t *words)
{
	const unsigned tid = words[0];
	const char *tname;
	char *s = NULL;
	bool ifaddr;

	tname = npfctl_table_getname(ctx->conf, tid, &ifaddr);
	easprintf(&s, ifaddr ? "ifaddrs(%s)" : "<%s>", tname);
	return s;
}

static char *
print_proto(npf_conf_info_t *ctx, const uint32_t *words)
{
	ctx->flags |= SEEN_PROTO;
	switch (words[0]) {
	case IPPROTO_TCP:
		return estrdup("tcp");
	case IPPROTO_UDP:
		return estrdup("udp");
	case IPPROTO_ICMP:
		return estrdup("icmp");
	case IPPROTO_ICMPV6:
		return estrdup("ipv6-icmp");
	}
	return print_number(ctx, words);
}

static char *
print_tcpflags(npf_conf_info_t *ctx __unused, const uint32_t *words)
{
	const unsigned tf = words[0], tf_mask = words[1];
	char buf[32];
	size_t n;

	if ((ctx->flags & SEEN_PROTO) == 0) {
		/*
		 * Note: the TCP flag matching might be without 'proto tcp'
		 * when using a plain 'stateful' rule.  In such case, just
		 * skip showing of the flags as they are implicit.
		 */
		return NULL;
	}
	n = tcpflags2string(buf, tf);
	if (tf != tf_mask) {
		buf[n++] = '/';
		tcpflags2string(buf + n, tf_mask);
	}
	return estrdup(buf);
}

static char *
print_portrange(npf_conf_info_t *ctx __unused, const uint32_t *words)
{
	unsigned fport = words[0], tport = words[1];
	char *p;

	if (fport != tport) {
		easprintf(&p, "%u-%u", fport, tport);
	} else {
		easprintf(&p, "%u", fport);
	}
	return p;
}

/*
 * The main keyword mapping tables defining the syntax:
 * - Mapping of rule attributes (flags) to the keywords.
 * - Mapping of the byte-code marks to the keywords.
 */

#define	F(name)		__CONCAT(NPF_RULE_, name)
#define	STATEFUL_ALL	(NPF_RULE_STATEFUL | NPF_RULE_GSTATEFUL)
#define	NAME_AT		2

static const struct attr_keyword_mapent {
	uint32_t	mask;
	uint32_t	flags;
	const char *	val;
} attr_keyword_map[] = {
	{ F(GROUP)|F(DYNAMIC),	F(GROUP),		"group"		},
	{ F(GROUP)|F(DYNAMIC),	F(GROUP)|F(DYNAMIC),	"ruleset"	},
	{ F(GROUP)|F(PASS),	0,			"block"		},
	{ F(GROUP)|F(PASS),	F(PASS),		"pass"		},
	{ F(RETRST)|F(RETICMP),	F(RETRST)|F(RETICMP),	"return"	},
	{ F(RETRST)|F(RETICMP),	F(RETRST),		"return-rst"	},
	{ F(RETRST)|F(RETICMP),	F(RETICMP),		"return-icmp"	},
	{ STATEFUL_ALL,		F(STATEFUL),		"stateful"	},
	{ STATEFUL_ALL,		STATEFUL_ALL,		"stateful-all"	},
	{ F(DIMASK),		F(IN),			"in"		},
	{ F(DIMASK),		F(OUT),			"out"		},
	{ F(FINAL),		F(FINAL),		"final"		},
};

static const struct mark_keyword_mapent {
	unsigned	mark;
	const char *	format;
	int		list_id;
	char *		(*printfn)(npf_conf_info_t *, const uint32_t *);
	unsigned	fwords;
} mark_keyword_map[] = {
	{ BM_IPVER,	"family %s",	LIST_PROTO,	print_family,	1 },
	{ BM_PROTO,	"proto %s",	LIST_PROTO,	print_proto,	1 },
	{ BM_TCPFL,	"flags %s",	LIST_PROTO,	print_tcpflags,	2 },
	{ BM_ICMP_TYPE,	"icmp-type %s",	LIST_PROTO,	print_number,	1 },
	{ BM_ICMP_CODE,	"code %s",	LIST_PROTO,	print_number,	1 },

	{ BM_SRC_NEG,	NULL,		-1,		NULL,		0 },
	{ BM_SRC_CIDR,	NULL,		LIST_SADDR,	print_address,	6 },
	{ BM_SRC_TABLE,	NULL,		LIST_SADDR,	print_table,	1 },
	{ BM_SRC_PORTS,	NULL,		LIST_SPORT,	print_portrange,2 },

	{ BM_DST_NEG,	NULL,		-1,		NULL,		0 },
	{ BM_DST_CIDR,	NULL,		LIST_DADDR,	print_address,	6 },
	{ BM_DST_TABLE,	NULL,		LIST_DADDR,	print_table,	1 },
	{ BM_DST_PORTS,	NULL,		LIST_DPORT,	print_portrange,2 },
};

static const char * __attribute__((format_arg(2)))
verified_fmt(const char *fmt, const char *t __unused)
{
	return fmt;
}

static void
scan_marks(npf_conf_info_t *ctx, const struct mark_keyword_mapent *mk,
    const uint32_t *marks, size_t mlen)
{
	elem_list_t sublist, *target_list;

	/*
	 * If format is used for this mark, then collect multiple elements
	 * in into the list, merge and re-push the set into the target list.
	 *
	 * Currently, this is applicable only for 'proto { tcp, udp }'.
	 */
	memset(&sublist, 0, sizeof(elem_list_t));
	target_list = mk->format ? &sublist : &ctx->list[mk->list_id];

	/* Scan for the marks and extract the values. */
	mlen /= sizeof(uint32_t);
	while (mlen > 2) {
		const uint32_t m = *marks++;
		const unsigned nwords = *marks++;

		if ((mlen -= 2) < nwords) {
			errx(EXIT_FAILURE, "byte-code marking inconsistency");
		}
		if (m == mk->mark) {
			/*
			 * Set the current mark and note it as seen.
			 * Value is processed by the print function,
			 * otherwise we just need to note the mark.
			 */
			ctx->curmark = m;
			assert(BM_COUNT < (sizeof(uint64_t) * CHAR_BIT));
			ctx->seen_marks = UINT64_C(1) << m;
			assert(mk->fwords == nwords);

			if (mk->printfn) {
				char *val;

				if ((val = mk->printfn(ctx, marks)) != NULL) {
					list_push(target_list, val);
				}
			}
		}
		marks += nwords;
		mlen -= nwords;
	}

	if (sublist.count) {
		char *val, *elements;

		elements = list_join_free(&sublist, true, ", ");
		easprintf(&val, verified_fmt(mk->format, "%s"), elements );
		list_push(&ctx->list[mk->list_id], val);
		free(elements);
	}
}

static void
npfctl_print_id(npf_conf_info_t *ctx, nl_rule_t *rl)
{
	const uint64_t id = npf_rule_getid(rl);

	if (id) {
		ctx->fpos += fprintf(ctx->fp, "# id=\"%" PRIx64 "\" ", id);
	}
}

static void
npfctl_print_filter_generic(npf_conf_info_t *ctx)
{
	elem_list_t *list = &ctx->list[LIST_PROTO];

	if (list->count) {
		char *elements = list_join_free(list, false, " ");
		ctx->fpos += fprintf(ctx->fp, "%s ", elements);
		free(elements);
	}
}

static bool
npfctl_print_filter_seg(npf_conf_info_t *ctx, unsigned which)
{
	static const struct {
		const char *	keyword;
		unsigned	alist;
		unsigned	plist;
		unsigned	negbm;
	} refs[] = {
		[NPF_SRC] = {
			.keyword	= "from",
			.alist		= LIST_SADDR,
			.plist		= LIST_SPORT,
			.negbm		= UINT64_C(1) << BM_SRC_NEG,
		},
		[NPF_DST] = {
			.keyword	= "to",
			.alist		= LIST_DADDR,
			.plist		= LIST_DPORT,
			.negbm		= UINT64_C(1) << BM_DST_NEG,
		}
	};
	const char *neg = !!(ctx->seen_marks & refs[which].negbm) ? "! " : "";
	const char *kwd = refs[which].keyword;
	bool seen_filter = false;
	elem_list_t *list;
	char *elements;

	list = &ctx->list[refs[which].alist];
	if (list->count != 0) {
		seen_filter = true;
		elements = list_join_free(list, true, ", ");
		ctx->fpos += fprintf(ctx->fp, "%s %s%s ", kwd, neg, elements);
		free(elements);
	}

	list = &ctx->list[refs[which].plist];
	if (list->count != 0) {
		if (!seen_filter) {
			ctx->fpos += fprintf(ctx->fp, "%s any ", kwd);
			seen_filter = true;
		}
		elements = list_join_free(list, true, ", ");
		ctx->fpos += fprintf(ctx->fp, "port %s ", elements);
		free(elements);
	}
	return seen_filter;
}

static bool
npfctl_print_filter(npf_conf_info_t *ctx, nl_rule_t *rl)
{
	const void *marks;
	size_t mlen, len;
	const void *code;
	bool seenf = false;
	int type;

	marks = npf_rule_getinfo(rl, &mlen);
	if (!marks && (code = npf_rule_getcode(rl, &type, &len)) != NULL) {
		/*
		 * No marks, but the byte-code is present.  This must
		 * have been filled by libpcap(3) or possibly an unknown
		 * to us byte-code.
		 */
		ctx->fpos += fprintf(ctx->fp, "%s ", type == NPF_CODE_BPF ?
		    "pcap-filter \"...\"" : "unrecognized-bytecode");
		return true;
	}
	ctx->flags = 0;

	/*
	 * BPF filter criteria described by the byte-code marks.
	 */
	for (unsigned i = 0; i < __arraycount(mark_keyword_map); i++) {
		const struct mark_keyword_mapent *mk = &mark_keyword_map[i];
		scan_marks(ctx, mk, marks, mlen);
	}
	npfctl_print_filter_generic(ctx);
	seenf |= npfctl_print_filter_seg(ctx, NPF_SRC);
	seenf |= npfctl_print_filter_seg(ctx, NPF_DST);
	return seenf;
}

static void
npfctl_print_rule(npf_conf_info_t *ctx, nl_rule_t *rl, unsigned level)
{
	const uint32_t attr = npf_rule_getattr(rl);
	const char *rproc, *ifname, *name;
	bool dyn_ruleset;

	/* Rule attributes/flags. */
	for (unsigned i = 0; i < __arraycount(attr_keyword_map); i++) {
		const struct attr_keyword_mapent *ak = &attr_keyword_map[i];

		if (i == NAME_AT && (name = npf_rule_getname(rl)) != NULL) {
			ctx->fpos += fprintf(ctx->fp, "\"%s\" ", name);
		}
		if ((attr & ak->mask) == ak->flags) {
			ctx->fpos += fprintf(ctx->fp, "%s ", ak->val);
		}
	}
	if ((ifname = npf_rule_getinterface(rl)) != NULL) {
		ctx->fpos += fprintf(ctx->fp, "on %s ", ifname);
	}
	if (attr == (NPF_RULE_GROUP | NPF_RULE_IN | NPF_RULE_OUT) && !ifname) {
		/* The default group is a special case. */
		ctx->fpos += fprintf(ctx->fp, "default ");
	}
	if ((attr & NPF_DYNAMIC_GROUP) == NPF_RULE_GROUP) {
		/* Group; done. */
		ctx->fpos += fprintf(ctx->fp, "{ ");
		ctx->glevel = level;
		goto out;
	}

	/* Print filter criteria. */
	dyn_ruleset = (attr & NPF_DYNAMIC_GROUP) == NPF_DYNAMIC_GROUP;
	if (!npfctl_print_filter(ctx, rl) && !dyn_ruleset) {
		ctx->fpos += fprintf(ctx->fp, "all ");
	}

	/* Rule procedure. */
	if ((rproc = npf_rule_getproc(rl)) != NULL) {
		ctx->fpos += fprintf(ctx->fp, "apply \"%s\" ", rproc);
	}
out:
	npfctl_print_id(ctx, rl);
	ctx->fpos += fprintf(ctx->fp, "\n");
}

static void
npfctl_print_nat(npf_conf_info_t *ctx, nl_nat_t *nt)
{
	const unsigned dynamic_natset = NPF_RULE_GROUP | NPF_RULE_DYNAMIC;
	nl_rule_t *rl = (nl_nat_t *)nt;
	const char *ifname, *algo, *seg1, *seg2, *arrow;
	const npf_addr_t *addr;
	npf_netmask_t mask;
	in_port_t port;
	size_t alen;
	unsigned flags;
	char *seg;

	/* Get flags and the interface. */
	flags = npf_nat_getflags(nt);
	ifname = npf_rule_getinterface(rl);
	assert(ifname != NULL);

	if ((npf_rule_getattr(rl) & dynamic_natset) == dynamic_natset) {
		const char *name = npf_rule_getname(rl);
		ctx->fpos += fprintf(ctx->fp,
		    "map ruleset \"%s\" on %s\n", name, ifname);
		return;
	}

	/* Get the translation address or table (and port, if used). */
	addr = npf_nat_getaddr(nt, &alen, &mask);
	if (addr) {
		seg = npfctl_print_addrmask(alen, "%a", addr, mask);
	} else {
		const unsigned tid = npf_nat_gettable(nt);
		const char *tname;
		bool ifaddr;

		tname = npfctl_table_getname(ctx->conf, tid, &ifaddr);
		easprintf(&seg, ifaddr ? "ifaddrs(%s)" : "<%s>", tname);
	}

	if ((port = npf_nat_getport(nt)) != 0) {
		char *p;
		easprintf(&p, "%s port %u", seg, ntohs(port));
		free(seg), seg = p;
	}
	seg1 = seg2 = "any";

	/* Get the NAT type and determine the translation segment. */
	switch (npf_nat_gettype(nt)) {
	case NPF_NATIN:
		arrow = "<-";
		seg1 = seg;
		break;
	case NPF_NATOUT:
		arrow = "->";
		seg2 = seg;
		break;
	default:
		abort();
	}

	/* NAT algorithm. */
	switch (npf_nat_getalgo(nt)) {
	case NPF_ALGO_NETMAP:
		algo = "algo netmap ";
		break;
	case NPF_ALGO_IPHASH:
		algo = "algo ip-hash ";
		break;
	case NPF_ALGO_RR:
		algo = "algo round-robin ";
		break;
	case NPF_ALGO_NPT66:
		algo = "algo npt66 ";
		break;
	default:
		algo = "";
		break;
	}

	/* XXX also handle "any" */

	/* Print out the NAT policy with the filter criteria. */
	ctx->fpos += fprintf(ctx->fp, "map %s %s %s%s%s %s %s pass ",
	    ifname, (flags & NPF_NAT_STATIC) ? "static" : "dynamic",
	    algo, (flags & NPF_NAT_PORTS) ? "" : "no-ports ",
	    seg1, arrow, seg2);
	npfctl_print_filter(ctx, rl);
	npfctl_print_id(ctx, rl);
	ctx->fpos += fprintf(ctx->fp, "\n");
	free(seg);
}

static void
npfctl_print_table(npf_conf_info_t *ctx, nl_table_t *tl)
{
	const char *name = npf_table_getname(tl);
	const unsigned type = npf_table_gettype(tl);
	const char *table_types[] = {
		[NPF_TABLE_IPSET]	= "ipset",
		[NPF_TABLE_LPM]		= "lpm",
		[NPF_TABLE_CONST]	= "const",
	};

	if (name[0] == '.') {
		/* Internal tables use dot and are hidden. */
		return;
	}
	assert(type < __arraycount(table_types));
	ctx->fpos += fprintf(ctx->fp,
	    "table <%s> type %s\n", name, table_types[type]);
}

static void
npfctl_print_params(npf_conf_info_t *ctx, nl_config_t *ncf)
{
	nl_iter_t i = NPF_ITER_BEGIN;
	int val, defval, *dval;
	const char *name;

	dval = ctx->validating ? NULL : &defval;
	while ((name = npf_param_iterate(ncf, &i, &val, dval)) != NULL) {
		if (dval && val == *dval) {
			continue;
		}
		ctx->fpos += fprintf(ctx->fp, "set %s %d\n", name, val);
	}
	print_linesep(ctx);
}

int
npfctl_config_show(int fd)
{
	npf_conf_info_t *ctx = npfctl_show_init();
	nl_config_t *ncf;
	bool loaded;

	if (fd) {
		ncf = npf_config_retrieve(fd);
		if (ncf == NULL) {
			return errno;
		}
		loaded = npf_config_loaded_p(ncf);
		ctx->validating = false;
		ctx->fpos += fprintf(ctx->fp,
		    "# filtering:\t%s\n# config:\t%s\n",
		    npf_config_active_p(ncf) ? "active" : "inactive",
		    loaded ? "loaded" : "empty");
		print_linesep(ctx);
	} else {
		ncf = npfctl_config_ref();
		npfctl_config_build();
		ctx->validating = true;
		loaded = true;
	}
	ctx->conf = ncf;

	if (loaded) {
		nl_rule_t *rl;
		nl_rproc_t *rp;
		nl_nat_t *nt;
		nl_table_t *tl;
		nl_iter_t i;
		unsigned level;

		npfctl_print_params(ctx, ncf);

		i = NPF_ITER_BEGIN;
		while ((tl = npf_table_iterate(ncf, &i)) != NULL) {
			npfctl_print_table(ctx, tl);
		}
		print_linesep(ctx);

		i = NPF_ITER_BEGIN;
		while ((rp = npf_rproc_iterate(ncf, &i)) != NULL) {
			const char *rpname = npf_rproc_getname(rp);
			ctx->fpos += fprintf(ctx->fp,
			    "procedure \"%s\"\n", rpname);
		}
		print_linesep(ctx);

		i = NPF_ITER_BEGIN;
		while ((nt = npf_nat_iterate(ncf, &i)) != NULL) {
			npfctl_print_nat(ctx, nt);
		}
		print_linesep(ctx);

		i = NPF_ITER_BEGIN;
		while ((rl = npf_rule_iterate(ncf, &i, &level)) != NULL) {
			print_indent(ctx, level);
			npfctl_print_rule(ctx, rl, level);
		}
		print_indent(ctx, 0);
	}
	npf_config_destroy(ncf);
	return 0;
}

int
npfctl_ruleset_show(int fd, const char *ruleset_name)
{
	npf_conf_info_t *ctx = npfctl_show_init();
	nl_config_t *ncf;
	nl_rule_t *rl;
	unsigned level;
	nl_iter_t i;
	int error;

	ncf = npf_config_create();
	ctx->conf = ncf;

	if ((error = _npf_ruleset_list(fd, ruleset_name, ncf)) != 0) {
		return error;
	}
	i = NPF_ITER_BEGIN;
	while ((rl = npf_rule_iterate(ncf, &i, &level)) != NULL) {
		npfctl_print_rule(ctx, rl, 0);
	}
	npf_config_destroy(ncf);
	return error;
}