npf_show.c revision 1.4 1 1.4 rmind /* $NetBSD: npf_show.c,v 1.4 2013/11/12 00:46:34 rmind 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.4 rmind __RCSID("$NetBSD: npf_show.c,v 1.4 2013/11/12 00:46:34 rmind Exp $");
40 1.1 rmind
41 1.1 rmind #include <sys/socket.h>
42 1.1 rmind #include <netinet/in.h>
43 1.1 rmind #include <netinet/tcp.h>
44 1.1 rmind #include <net/if.h>
45 1.1 rmind
46 1.1 rmind #include <stdio.h>
47 1.1 rmind #include <stdlib.h>
48 1.1 rmind #include <string.h>
49 1.1 rmind #include <stdbool.h>
50 1.1 rmind #include <inttypes.h>
51 1.1 rmind #include <errno.h>
52 1.1 rmind #include <err.h>
53 1.1 rmind
54 1.1 rmind #include "npfctl.h"
55 1.1 rmind
56 1.1 rmind typedef struct {
57 1.4 rmind nl_config_t * conf;
58 1.1 rmind FILE * fp;
59 1.1 rmind long fpos;
60 1.1 rmind } npf_conf_info_t;
61 1.1 rmind
62 1.1 rmind static npf_conf_info_t stdout_ctx = { .fp = stdout, .fpos = 0 };
63 1.1 rmind
64 1.1 rmind static void print_indent(npf_conf_info_t *, u_int);
65 1.1 rmind static void print_linesep(npf_conf_info_t *);
66 1.1 rmind
67 1.1 rmind /*
68 1.1 rmind * Helper routines to print various pieces of information.
69 1.1 rmind */
70 1.1 rmind
71 1.1 rmind static void
72 1.1 rmind print_indent(npf_conf_info_t *ctx, u_int level)
73 1.1 rmind {
74 1.1 rmind if (level == 0) { /* XXX */
75 1.1 rmind print_linesep(ctx);
76 1.1 rmind }
77 1.1 rmind while (level--)
78 1.1 rmind fprintf(ctx->fp, "\t");
79 1.1 rmind }
80 1.1 rmind
81 1.1 rmind static void
82 1.1 rmind print_linesep(npf_conf_info_t *ctx)
83 1.1 rmind {
84 1.1 rmind if (ftell(ctx->fp) != ctx->fpos) {
85 1.1 rmind fputs("\n", ctx->fp);
86 1.1 rmind ctx->fpos = ftell(ctx->fp);
87 1.1 rmind }
88 1.1 rmind }
89 1.1 rmind
90 1.1 rmind static size_t
91 1.1 rmind tcpflags2string(char *buf, u_int tfl)
92 1.1 rmind {
93 1.1 rmind u_int i = 0;
94 1.1 rmind
95 1.1 rmind if (tfl & TH_FIN) buf[i++] = 'F';
96 1.1 rmind if (tfl & TH_SYN) buf[i++] = 'S';
97 1.1 rmind if (tfl & TH_RST) buf[i++] = 'R';
98 1.1 rmind if (tfl & TH_PUSH) buf[i++] = 'P';
99 1.1 rmind if (tfl & TH_ACK) buf[i++] = 'A';
100 1.1 rmind if (tfl & TH_URG) buf[i++] = 'U';
101 1.1 rmind if (tfl & TH_ECE) buf[i++] = 'E';
102 1.1 rmind if (tfl & TH_CWR) buf[i++] = 'C';
103 1.1 rmind buf[i] = '\0';
104 1.1 rmind return i;
105 1.1 rmind }
106 1.1 rmind
107 1.1 rmind static char *
108 1.4 rmind print_family(npf_conf_info_t *ctx, const uint32_t *words)
109 1.1 rmind {
110 1.1 rmind const int af = words[0];
111 1.1 rmind
112 1.1 rmind switch (af) {
113 1.1 rmind case AF_INET:
114 1.1 rmind return estrdup("inet");
115 1.1 rmind case AF_INET6:
116 1.1 rmind return estrdup("inet6");
117 1.1 rmind default:
118 1.1 rmind errx(EXIT_FAILURE, "invalid byte-code mark (family)");
119 1.1 rmind }
120 1.1 rmind return NULL;
121 1.1 rmind }
122 1.1 rmind
123 1.1 rmind static char *
124 1.4 rmind print_address(npf_conf_info_t *ctx, const uint32_t *words)
125 1.1 rmind {
126 1.1 rmind const int af = *words++;
127 1.1 rmind const u_int mask = *words++;
128 1.1 rmind const npf_addr_t *addr;
129 1.1 rmind int alen = 0;
130 1.1 rmind
131 1.1 rmind switch (af) {
132 1.1 rmind case AF_INET:
133 1.1 rmind alen = 4;
134 1.1 rmind break;
135 1.1 rmind case AF_INET6:
136 1.1 rmind alen = 16;
137 1.1 rmind break;
138 1.1 rmind default:
139 1.1 rmind errx(EXIT_FAILURE, "invalid byte-code mark (address)");
140 1.1 rmind }
141 1.1 rmind addr = (const npf_addr_t *)words;
142 1.1 rmind return npfctl_print_addrmask(alen, addr, mask);
143 1.1 rmind }
144 1.1 rmind
145 1.1 rmind static char *
146 1.4 rmind print_number(npf_conf_info_t *ctx, const uint32_t *words)
147 1.1 rmind {
148 1.1 rmind char *p;
149 1.1 rmind easprintf(&p, "%u", words[0]);
150 1.1 rmind return p;
151 1.1 rmind }
152 1.1 rmind
153 1.1 rmind static char *
154 1.4 rmind print_table(npf_conf_info_t *ctx, const uint32_t *words)
155 1.4 rmind {
156 1.4 rmind unsigned tid = words[0];
157 1.4 rmind nl_table_t *tl;
158 1.4 rmind char *p;
159 1.4 rmind
160 1.4 rmind while ((tl = npf_table_iterate(ctx->conf)) != NULL) {
161 1.4 rmind if (npf_table_getid(tl) == tid)
162 1.4 rmind break;
163 1.4 rmind }
164 1.4 rmind easprintf(&p, "%s", npf_table_getname(tl));
165 1.4 rmind return p;
166 1.4 rmind }
167 1.4 rmind
168 1.4 rmind static char *
169 1.4 rmind print_proto(npf_conf_info_t *ctx, const uint32_t *words)
170 1.1 rmind {
171 1.1 rmind switch (words[0]) {
172 1.1 rmind case IPPROTO_TCP:
173 1.1 rmind return estrdup("tcp");
174 1.1 rmind case IPPROTO_UDP:
175 1.1 rmind return estrdup("udp");
176 1.1 rmind case IPPROTO_ICMP:
177 1.1 rmind return estrdup("icmp");
178 1.1 rmind case IPPROTO_ICMPV6:
179 1.1 rmind return estrdup("ipv6-icmp");
180 1.1 rmind }
181 1.4 rmind return print_number(ctx, words);
182 1.1 rmind }
183 1.1 rmind
184 1.1 rmind static char *
185 1.4 rmind print_tcpflags(npf_conf_info_t *ctx, const uint32_t *words)
186 1.1 rmind {
187 1.1 rmind const u_int tf = words[0], tf_mask = words[1];
188 1.1 rmind char buf[16];
189 1.1 rmind
190 1.1 rmind size_t n = tcpflags2string(buf, tf);
191 1.1 rmind if (tf != tf_mask) {
192 1.1 rmind buf[n++] = '/';
193 1.1 rmind tcpflags2string(buf + n, tf_mask);
194 1.1 rmind }
195 1.1 rmind return estrdup(buf);
196 1.1 rmind }
197 1.1 rmind
198 1.1 rmind static char *
199 1.4 rmind print_portrange(npf_conf_info_t *ctx, const uint32_t *words)
200 1.1 rmind {
201 1.1 rmind u_int fport = words[0], tport = words[1];
202 1.1 rmind char *p;
203 1.1 rmind
204 1.1 rmind if (fport != tport) {
205 1.1 rmind easprintf(&p, "%u:%u", fport, tport);
206 1.1 rmind } else {
207 1.1 rmind easprintf(&p, "%u", fport);
208 1.1 rmind }
209 1.1 rmind return p;
210 1.1 rmind }
211 1.1 rmind
212 1.1 rmind /*
213 1.1 rmind * The main keyword mapping tables defining the syntax:
214 1.1 rmind * - Mapping of rule attributes (flags) to the keywords.
215 1.1 rmind * - Mapping of the byte-code marks to the keywords.
216 1.1 rmind */
217 1.1 rmind
218 1.1 rmind #define F(name) __CONCAT(NPF_RULE_, name)
219 1.1 rmind #define NAME_AT 2
220 1.1 rmind
221 1.1 rmind static const struct attr_keyword_mapent {
222 1.1 rmind uint32_t mask;
223 1.1 rmind uint32_t flags;
224 1.1 rmind const char * val;
225 1.1 rmind } attr_keyword_map[] = {
226 1.1 rmind { F(GROUP)|F(DYNAMIC), F(GROUP), "group" },
227 1.1 rmind { F(DYNAMIC), F(DYNAMIC), "ruleset" },
228 1.1 rmind { F(GROUP)|F(PASS), 0, "block" },
229 1.1 rmind { F(GROUP)|F(PASS), F(PASS), "pass" },
230 1.1 rmind { F(RETRST)|F(RETICMP), F(RETRST)|F(RETICMP), "return" },
231 1.1 rmind { F(RETRST)|F(RETICMP), F(RETRST), "return-rst" },
232 1.1 rmind { F(RETRST)|F(RETICMP), F(RETICMP), "return-icmp" },
233 1.1 rmind { F(STATEFUL), F(STATEFUL), "stateful" },
234 1.1 rmind { F(DIMASK), F(IN), "in" },
235 1.1 rmind { F(DIMASK), F(OUT), "out" },
236 1.1 rmind { F(FINAL), F(FINAL), "final" },
237 1.1 rmind };
238 1.1 rmind
239 1.1 rmind static const struct mark_keyword_mapent {
240 1.1 rmind u_int mark;
241 1.1 rmind const char * token;
242 1.1 rmind const char * sep;
243 1.4 rmind char * (*printfn)(npf_conf_info_t *, const uint32_t *);
244 1.1 rmind u_int fwords;
245 1.1 rmind } mark_keyword_map[] = {
246 1.1 rmind { BM_IPVER, "family %s", NULL, print_family, 1 },
247 1.1 rmind { BM_PROTO, "proto %s", NULL, print_proto, 1 },
248 1.1 rmind { BM_TCPFL, "flags %s", NULL, print_tcpflags, 2 },
249 1.1 rmind { BM_ICMP_TYPE, "icmp-type %s", NULL, print_number, 1 },
250 1.1 rmind { BM_ICMP_CODE, "code %s", NULL, print_number, 1 },
251 1.1 rmind
252 1.1 rmind { BM_SRC_CIDR, "from %s", ", ", print_address, 6 },
253 1.4 rmind { BM_SRC_TABLE, "from <%s>", NULL, print_table, 1 },
254 1.1 rmind { BM_SRC_PORTS, "port %s", ", ", print_portrange,2 },
255 1.1 rmind
256 1.1 rmind { BM_DST_CIDR, "to %s", ", ", print_address, 6 },
257 1.4 rmind { BM_DST_TABLE, "to <%s>", NULL, print_table, 1 },
258 1.1 rmind { BM_DST_PORTS, "port %s", ", ", print_portrange,2 },
259 1.1 rmind };
260 1.1 rmind
261 1.1 rmind static const char * __attribute__((format_arg(2)))
262 1.1 rmind verified_fmt(const char *fmt, const char *t __unused)
263 1.1 rmind {
264 1.1 rmind return fmt;
265 1.1 rmind }
266 1.1 rmind
267 1.1 rmind static char *
268 1.1 rmind scan_marks(npf_conf_info_t *ctx, const struct mark_keyword_mapent *mk,
269 1.1 rmind const uint32_t *marks, size_t mlen)
270 1.1 rmind {
271 1.1 rmind char buf[2048], *vals[256], *p;
272 1.1 rmind size_t nvals = 0;
273 1.1 rmind
274 1.1 rmind /* Scan for the marks and extract the values. */
275 1.1 rmind mlen /= sizeof(uint32_t);
276 1.1 rmind while (mlen > 2) {
277 1.1 rmind const uint32_t m = *marks++;
278 1.1 rmind const u_int nwords = *marks++;
279 1.1 rmind
280 1.1 rmind if ((mlen -= 2) < nwords) {
281 1.1 rmind errx(EXIT_FAILURE, "byte-code marking inconsistency");
282 1.1 rmind }
283 1.1 rmind if (m == mk->mark) {
284 1.1 rmind /* Value is processed by the print function. */
285 1.1 rmind assert(mk->fwords == nwords);
286 1.4 rmind vals[nvals++] = mk->printfn(ctx, marks);
287 1.1 rmind }
288 1.1 rmind marks += nwords;
289 1.1 rmind mlen -= nwords;
290 1.1 rmind }
291 1.1 rmind if (nvals == 0) {
292 1.1 rmind return NULL;
293 1.1 rmind }
294 1.1 rmind assert(nvals == 1 || mk->sep != NULL);
295 1.1 rmind
296 1.1 rmind /*
297 1.1 rmind * Join all the values and print. Add curly brackets if there
298 1.1 rmind * is more than value and it can be a set.
299 1.1 rmind */
300 1.1 rmind if (!join(buf, sizeof(buf), nvals, vals, mk->sep ? mk->sep : "")) {
301 1.1 rmind errx(EXIT_FAILURE, "out of memory while parsing the rule");
302 1.1 rmind }
303 1.1 rmind easprintf(&p, nvals > 1 ? "{ %s }" : "%s", buf);
304 1.1 rmind
305 1.1 rmind for (u_int i = 0; i < nvals; i++) {
306 1.1 rmind free(vals[i]);
307 1.1 rmind }
308 1.1 rmind return p;
309 1.1 rmind }
310 1.1 rmind
311 1.1 rmind static void
312 1.1 rmind npfctl_print_filter(npf_conf_info_t *ctx, nl_rule_t *rl)
313 1.1 rmind {
314 1.1 rmind const void *marks;
315 1.1 rmind size_t mlen;
316 1.1 rmind
317 1.1 rmind /* BPF filter criteria described by the byte-code marks. */
318 1.1 rmind marks = npf_rule_getinfo(rl, &mlen);
319 1.1 rmind for (u_int i = 0; i < __arraycount(mark_keyword_map); i++) {
320 1.1 rmind const struct mark_keyword_mapent *mk = &mark_keyword_map[i];
321 1.1 rmind char *val;
322 1.1 rmind
323 1.1 rmind if ((val = scan_marks(ctx, mk, marks, mlen)) != NULL) {
324 1.1 rmind fprintf(ctx->fp, verified_fmt(mk->token, "%s"), val);
325 1.1 rmind fputs(" ", ctx->fp);
326 1.1 rmind free(val);
327 1.1 rmind }
328 1.1 rmind }
329 1.1 rmind if (!mlen) {
330 1.1 rmind fputs("all ", ctx->fp);
331 1.1 rmind }
332 1.1 rmind }
333 1.1 rmind
334 1.1 rmind static void
335 1.1 rmind npfctl_print_rule(npf_conf_info_t *ctx, nl_rule_t *rl)
336 1.1 rmind {
337 1.1 rmind const uint32_t attr = npf_rule_getattr(rl);
338 1.3 rmind const char *rproc, *ifname, *name;
339 1.1 rmind
340 1.1 rmind /* Rule attributes/flags. */
341 1.1 rmind for (u_int i = 0; i < __arraycount(attr_keyword_map); i++) {
342 1.1 rmind const struct attr_keyword_mapent *ak = &attr_keyword_map[i];
343 1.1 rmind
344 1.1 rmind if (i == NAME_AT && (name = npf_rule_getname(rl)) != NULL) {
345 1.1 rmind fprintf(ctx->fp, "\"%s\" ", name);
346 1.1 rmind }
347 1.1 rmind if ((attr & ak->mask) == ak->flags) {
348 1.1 rmind fprintf(ctx->fp, "%s ", ak->val);
349 1.1 rmind }
350 1.1 rmind }
351 1.3 rmind if ((ifname = npf_rule_getinterface(rl)) != NULL) {
352 1.1 rmind fprintf(ctx->fp, "on %s ", ifname);
353 1.1 rmind }
354 1.1 rmind
355 1.1 rmind if ((attr & (NPF_RULE_GROUP | NPF_RULE_DYNAMIC)) == NPF_RULE_GROUP) {
356 1.1 rmind /* Group; done. */
357 1.1 rmind fputs("\n", ctx->fp);
358 1.1 rmind return;
359 1.1 rmind }
360 1.1 rmind
361 1.1 rmind /* Print filter criteria. */
362 1.1 rmind npfctl_print_filter(ctx, rl);
363 1.1 rmind
364 1.1 rmind /* Rule procedure. */
365 1.1 rmind if ((rproc = npf_rule_getproc(rl)) != NULL) {
366 1.1 rmind fprintf(ctx->fp, "apply \"%s\"", rproc);
367 1.1 rmind }
368 1.1 rmind fputs("\n", ctx->fp);
369 1.1 rmind }
370 1.1 rmind
371 1.1 rmind static void
372 1.1 rmind npfctl_print_nat(npf_conf_info_t *ctx, nl_nat_t *nt)
373 1.1 rmind {
374 1.1 rmind nl_rule_t *rl = (nl_nat_t *)nt;
375 1.1 rmind const char *ifname, *seg1, *seg2, *arrow;
376 1.1 rmind npf_addr_t addr;
377 1.1 rmind in_port_t port;
378 1.3 rmind size_t alen;
379 1.3 rmind char *seg;
380 1.1 rmind
381 1.1 rmind /* Get the interface. */
382 1.3 rmind ifname = npf_rule_getinterface(rl);
383 1.3 rmind assert(ifname != NULL);
384 1.1 rmind
385 1.1 rmind /* Get the translation address (and port, if used). */
386 1.1 rmind npf_nat_getmap(nt, &addr, &alen, &port);
387 1.1 rmind seg = npfctl_print_addrmask(alen, &addr, NPF_NO_NETMASK);
388 1.1 rmind if (port) {
389 1.1 rmind char *p;
390 1.1 rmind easprintf(&p, "%s port %u", seg, port);
391 1.1 rmind free(seg), seg = p;
392 1.1 rmind }
393 1.1 rmind seg1 = seg2 = "any";
394 1.1 rmind
395 1.1 rmind /* Get the NAT type and determine the translation segment. */
396 1.1 rmind switch (npf_nat_gettype(nt)) {
397 1.1 rmind case NPF_NATIN:
398 1.1 rmind arrow = "<-";
399 1.1 rmind seg1 = seg;
400 1.1 rmind break;
401 1.1 rmind case NPF_NATOUT:
402 1.1 rmind arrow = "->";
403 1.1 rmind seg2 = seg;
404 1.1 rmind break;
405 1.1 rmind default:
406 1.1 rmind assert(false);
407 1.1 rmind }
408 1.1 rmind
409 1.1 rmind /* Print out the NAT policy with the filter criteria. */
410 1.1 rmind fprintf(ctx->fp, "map %s dynamic %s %s %s pass ",
411 1.1 rmind ifname, seg1, arrow, seg2);
412 1.1 rmind npfctl_print_filter(ctx, rl);
413 1.1 rmind fputs("\n", ctx->fp);
414 1.1 rmind free(seg);
415 1.1 rmind }
416 1.1 rmind
417 1.1 rmind static void
418 1.1 rmind npfctl_print_table(npf_conf_info_t *ctx, nl_table_t *tl)
419 1.1 rmind {
420 1.4 rmind const char *name = npf_table_getname(tl);
421 1.1 rmind const int type = npf_table_gettype(tl);
422 1.1 rmind
423 1.4 rmind fprintf(ctx->fp, "table <%s> type %s\n", name,
424 1.1 rmind (type == NPF_TABLE_HASH) ? "hash" :
425 1.1 rmind (type == NPF_TABLE_TREE) ? "tree" :
426 1.1 rmind "unknown");
427 1.1 rmind }
428 1.1 rmind
429 1.1 rmind int
430 1.1 rmind npfctl_config_show(int fd)
431 1.1 rmind {
432 1.1 rmind npf_conf_info_t *ctx = &stdout_ctx;
433 1.1 rmind nl_config_t *ncf;
434 1.1 rmind bool active, loaded;
435 1.1 rmind
436 1.1 rmind if (fd) {
437 1.1 rmind ncf = npf_config_retrieve(fd, &active, &loaded);
438 1.1 rmind if (ncf == NULL) {
439 1.1 rmind return errno;
440 1.1 rmind }
441 1.1 rmind fprintf(ctx->fp, "Filtering:\t%s\nConfiguration:\t%s\n",
442 1.1 rmind active ? "active" : "inactive",
443 1.1 rmind loaded ? "loaded" : "empty");
444 1.1 rmind print_linesep(ctx);
445 1.1 rmind } else {
446 1.1 rmind npfctl_config_send(0, NULL);
447 1.1 rmind ncf = npfctl_config_ref();
448 1.1 rmind loaded = true;
449 1.1 rmind }
450 1.4 rmind ctx->conf = ncf;
451 1.1 rmind
452 1.1 rmind if (loaded) {
453 1.1 rmind nl_rule_t *rl;
454 1.1 rmind nl_rproc_t *rp;
455 1.1 rmind nl_nat_t *nt;
456 1.1 rmind nl_table_t *tl;
457 1.1 rmind u_int level;
458 1.1 rmind
459 1.1 rmind while ((tl = npf_table_iterate(ncf)) != NULL) {
460 1.1 rmind npfctl_print_table(ctx, tl);
461 1.1 rmind }
462 1.1 rmind print_linesep(ctx);
463 1.1 rmind
464 1.1 rmind while ((rp = npf_rproc_iterate(ncf)) != NULL) {
465 1.1 rmind const char *rpname = npf_rproc_getname(rp);
466 1.1 rmind fprintf(ctx->fp, "procedure \"%s\"\n", rpname);
467 1.1 rmind }
468 1.1 rmind print_linesep(ctx);
469 1.1 rmind
470 1.1 rmind while ((nt = npf_nat_iterate(ncf)) != NULL) {
471 1.1 rmind npfctl_print_nat(ctx, nt);
472 1.1 rmind }
473 1.1 rmind print_linesep(ctx);
474 1.1 rmind
475 1.1 rmind while ((rl = npf_rule_iterate(ncf, &level)) != NULL) {
476 1.1 rmind print_indent(ctx, level);
477 1.1 rmind npfctl_print_rule(ctx, rl);
478 1.1 rmind }
479 1.1 rmind print_linesep(ctx);
480 1.1 rmind }
481 1.1 rmind npf_config_destroy(ncf);
482 1.1 rmind return 0;
483 1.1 rmind }
484 1.1 rmind
485 1.1 rmind int
486 1.1 rmind npfctl_ruleset_show(int fd, const char *ruleset_name)
487 1.1 rmind {
488 1.1 rmind npf_conf_info_t *ctx = &stdout_ctx;
489 1.1 rmind nl_config_t *ncf;
490 1.1 rmind nl_rule_t *rl;
491 1.1 rmind u_int level;
492 1.1 rmind int error;
493 1.1 rmind
494 1.1 rmind ncf = npf_config_create();
495 1.4 rmind ctx->conf = ncf;
496 1.4 rmind
497 1.1 rmind if ((error = _npf_ruleset_list(fd, ruleset_name, ncf)) != 0) {
498 1.1 rmind return error;
499 1.1 rmind }
500 1.1 rmind while ((rl = npf_rule_iterate(ncf, &level)) != NULL) {
501 1.1 rmind npfctl_print_rule(ctx, rl);
502 1.1 rmind }
503 1.1 rmind npf_config_destroy(ncf);
504 1.1 rmind return error;
505 1.1 rmind }
506