Home | History | Annotate | Line # | Download | only in npfctl
npfctl.c revision 1.10.2.5.2.1
      1  1.10.2.5.2.1    matt /*	$NetBSD: npfctl.c,v 1.10.2.5.2.1 2012/11/01 16:45:07 matt Exp $	*/
      2           1.1   rmind 
      3           1.1   rmind /*-
      4           1.8   rmind  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
      5           1.1   rmind  * All rights reserved.
      6           1.1   rmind  *
      7           1.1   rmind  * This material is based upon work partially supported by The
      8           1.1   rmind  * NetBSD Foundation under a contract with 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.2   rmind #include <sys/cdefs.h>
     33  1.10.2.5.2.1    matt __RCSID("$NetBSD: npfctl.c,v 1.10.2.5.2.1 2012/11/01 16:45:07 matt Exp $");
     34           1.2   rmind 
     35           1.1   rmind #include <sys/ioctl.h>
     36           1.1   rmind #include <sys/stat.h>
     37           1.1   rmind #include <sys/types.h>
     38           1.1   rmind 
     39           1.1   rmind #include <stdio.h>
     40           1.1   rmind #include <stdlib.h>
     41           1.1   rmind #include <string.h>
     42           1.1   rmind #include <err.h>
     43           1.1   rmind #include <fcntl.h>
     44           1.1   rmind #include <unistd.h>
     45      1.10.2.3     riz #include <errno.h>
     46           1.1   rmind 
     47           1.1   rmind #include "npfctl.h"
     48           1.1   rmind 
     49           1.8   rmind extern int		yylineno, yycolumn;
     50           1.8   rmind extern const char *	yyfilename;
     51           1.8   rmind extern int		yyparse(void);
     52           1.8   rmind extern void		yyrestart(FILE *);
     53           1.8   rmind 
     54      1.10.2.1     riz enum {
     55      1.10.2.1     riz 	NPFCTL_START,
     56      1.10.2.1     riz 	NPFCTL_STOP,
     57      1.10.2.1     riz 	NPFCTL_RELOAD,
     58      1.10.2.1     riz 	NPFCTL_SHOWCONF,
     59      1.10.2.1     riz 	NPFCTL_FLUSH,
     60      1.10.2.1     riz 	NPFCTL_TABLE,
     61      1.10.2.1     riz 	NPFCTL_STATS,
     62      1.10.2.1     riz 	NPFCTL_SESSIONS_SAVE,
     63      1.10.2.1     riz 	NPFCTL_SESSIONS_LOAD,
     64      1.10.2.1     riz };
     65           1.1   rmind 
     66      1.10.2.3     riz static const struct operations_s {
     67           1.1   rmind 	const char *		cmd;
     68           1.1   rmind 	int			action;
     69           1.1   rmind } operations[] = {
     70           1.1   rmind 	/* Start, stop, reload */
     71           1.3   rmind 	{	"start",		NPFCTL_START		},
     72           1.3   rmind 	{	"stop",			NPFCTL_STOP		},
     73           1.3   rmind 	{	"reload",		NPFCTL_RELOAD		},
     74      1.10.2.1     riz 	{	"show",			NPFCTL_SHOWCONF,	},
     75           1.3   rmind 	{	"flush",		NPFCTL_FLUSH		},
     76           1.1   rmind 	/* Table */
     77           1.3   rmind 	{	"table",		NPFCTL_TABLE		},
     78           1.3   rmind 	/* Stats */
     79           1.3   rmind 	{	"stats",		NPFCTL_STATS		},
     80           1.3   rmind 	/* Sessions */
     81           1.3   rmind 	{	"sess-save",		NPFCTL_SESSIONS_SAVE	},
     82           1.3   rmind 	{	"sess-load",		NPFCTL_SESSIONS_LOAD	},
     83           1.1   rmind 	/* --- */
     84           1.3   rmind 	{	NULL,			0			}
     85           1.1   rmind };
     86           1.1   rmind 
     87           1.1   rmind void *
     88           1.1   rmind zalloc(size_t sz)
     89           1.1   rmind {
     90           1.8   rmind 	void *p = malloc(sz);
     91           1.1   rmind 
     92           1.1   rmind 	if (p == NULL) {
     93           1.4   rmind 		err(EXIT_FAILURE, "zalloc");
     94           1.1   rmind 	}
     95           1.1   rmind 	memset(p, 0, sz);
     96           1.1   rmind 	return p;
     97           1.1   rmind }
     98           1.1   rmind 
     99           1.8   rmind void *
    100           1.8   rmind xrealloc(void *ptr, size_t size)
    101           1.8   rmind {
    102           1.8   rmind 	void *p = realloc(ptr, size);
    103           1.8   rmind 
    104           1.8   rmind 	if (p == NULL) {
    105           1.8   rmind 		err(EXIT_FAILURE, "xrealloc");
    106           1.8   rmind 	}
    107           1.8   rmind 	return p;
    108           1.8   rmind }
    109           1.8   rmind 
    110           1.1   rmind char *
    111           1.1   rmind xstrdup(const char *s)
    112           1.1   rmind {
    113           1.8   rmind 	char *p = strdup(s);
    114           1.8   rmind 
    115           1.8   rmind 	if (p == NULL) {
    116           1.8   rmind 		err(EXIT_FAILURE, "xstrdup");
    117           1.8   rmind 	}
    118           1.8   rmind 	return p;
    119           1.8   rmind }
    120           1.8   rmind 
    121           1.8   rmind char *
    122           1.8   rmind xstrndup(const char *s, size_t len)
    123           1.8   rmind {
    124           1.1   rmind 	char *p;
    125           1.1   rmind 
    126           1.8   rmind 	p = strndup(s, len);
    127           1.1   rmind 	if (p == NULL) {
    128           1.8   rmind 		err(EXIT_FAILURE, "xstrndup");
    129           1.1   rmind 	}
    130           1.1   rmind 	return p;
    131           1.1   rmind }
    132           1.1   rmind 
    133           1.6   joerg __dead static void
    134           1.1   rmind usage(void)
    135           1.1   rmind {
    136           1.1   rmind 	const char *progname = getprogname();
    137           1.1   rmind 
    138           1.1   rmind 	fprintf(stderr,
    139      1.10.2.2     riz 	    "usage:\t%s [ start | stop | reload | flush | show | stats ]\n",
    140           1.3   rmind 	    progname);
    141           1.3   rmind 	fprintf(stderr,
    142  1.10.2.5.2.1    matt 	    "\t%s ( sess-save | sess-load )\n",
    143           1.1   rmind 	    progname);
    144           1.1   rmind 	fprintf(stderr,
    145           1.1   rmind 	    "\t%s table <tid> [ flush ]\n",
    146           1.1   rmind 	    progname);
    147           1.1   rmind 	fprintf(stderr,
    148      1.10.2.3     riz 	    "\t%s table <tid> { add | rem | test } <address/mask>\n",
    149           1.1   rmind 	    progname);
    150           1.1   rmind 
    151           1.1   rmind 	exit(EXIT_FAILURE);
    152           1.1   rmind }
    153           1.1   rmind 
    154           1.1   rmind static void
    155           1.1   rmind npfctl_parsecfg(const char *cfg)
    156           1.1   rmind {
    157           1.1   rmind 	FILE *fp;
    158           1.1   rmind 
    159           1.1   rmind 	fp = fopen(cfg, "r");
    160           1.1   rmind 	if (fp == NULL) {
    161           1.4   rmind 		err(EXIT_FAILURE, "open '%s'", cfg);
    162           1.1   rmind 	}
    163           1.8   rmind 	yyrestart(fp);
    164           1.8   rmind 	yylineno = 1;
    165           1.8   rmind 	yycolumn = 0;
    166           1.8   rmind 	yyfilename = cfg;
    167           1.8   rmind 	yyparse();
    168           1.8   rmind 	fclose(fp);
    169           1.1   rmind }
    170           1.1   rmind 
    171           1.3   rmind static int
    172           1.3   rmind npfctl_print_stats(int fd)
    173           1.3   rmind {
    174      1.10.2.4     jdc 	static const struct stats_s {
    175      1.10.2.4     jdc 		/* Note: -1 indicates a new section. */
    176      1.10.2.4     jdc 		int		index;
    177      1.10.2.4     jdc 		const char *	name;
    178      1.10.2.4     jdc 	} stats[] = {
    179      1.10.2.4     jdc 		{ -1, "Packets passed"					},
    180      1.10.2.4     jdc 		{ NPF_STAT_PASS_DEFAULT,	"default pass"		},
    181      1.10.2.4     jdc 		{ NPF_STAT_PASS_RULESET,	"ruleset pass"		},
    182      1.10.2.4     jdc 		{ NPF_STAT_PASS_SESSION,	"session pass"		},
    183      1.10.2.4     jdc 
    184      1.10.2.4     jdc 		{ -1, "Packets blocked"					},
    185      1.10.2.4     jdc 		{ NPF_STAT_BLOCK_DEFAULT,	"default block"		},
    186      1.10.2.4     jdc 		{ NPF_STAT_BLOCK_RULESET,	"ruleset block"		},
    187      1.10.2.4     jdc 
    188      1.10.2.4     jdc 		{ -1, "Session and NAT entries"				},
    189      1.10.2.4     jdc 		{ NPF_STAT_SESSION_CREATE,	"session allocations"	},
    190      1.10.2.4     jdc 		{ NPF_STAT_SESSION_DESTROY,	"session destructions"	},
    191      1.10.2.4     jdc 		{ NPF_STAT_NAT_CREATE,		"NAT entry allocations"	},
    192      1.10.2.4     jdc 		{ NPF_STAT_NAT_DESTROY,		"NAT entry destructions"},
    193      1.10.2.4     jdc 
    194      1.10.2.4     jdc 		{ -1, "Invalid packet state cases"			},
    195      1.10.2.4     jdc 		{ NPF_STAT_INVALID_STATE,	"cases in total"	},
    196      1.10.2.4     jdc 		{ NPF_STAT_INVALID_STATE_TCP1,	"TCP case I"		},
    197      1.10.2.4     jdc 		{ NPF_STAT_INVALID_STATE_TCP2,	"TCP case II"		},
    198      1.10.2.4     jdc 		{ NPF_STAT_INVALID_STATE_TCP3,	"TCP case III"		},
    199      1.10.2.4     jdc 
    200      1.10.2.4     jdc 		{ -1, "Packet race cases"				},
    201      1.10.2.4     jdc 		{ NPF_STAT_RACE_NAT,		"NAT association race"	},
    202      1.10.2.4     jdc 		{ NPF_STAT_RACE_SESSION,	"duplicate session race"},
    203      1.10.2.4     jdc 
    204      1.10.2.4     jdc 		{ -1, "Rule procedure cases"				},
    205      1.10.2.4     jdc 		{ NPF_STAT_RPROC_LOG,		"packets logged"	},
    206      1.10.2.4     jdc 		{ NPF_STAT_RPROC_NORM,		"packets normalised"	},
    207      1.10.2.4     jdc 
    208      1.10.2.4     jdc 		{ -1, "Fragmentation"					},
    209      1.10.2.4     jdc 		{ NPF_STAT_FRAGMENTS,		"fragments"		},
    210      1.10.2.4     jdc 		{ NPF_STAT_REASSEMBLY,		"reassembled"		},
    211      1.10.2.4     jdc 		{ NPF_STAT_REASSFAIL,		"failed reassembly"	},
    212      1.10.2.4     jdc 
    213      1.10.2.4     jdc 		{ -1, "Other"						},
    214      1.10.2.4     jdc 		{ NPF_STAT_ERROR,		"unexpected errors"	},
    215      1.10.2.4     jdc 	};
    216           1.8   rmind 	uint64_t *st = zalloc(NPF_STATS_SIZE);
    217           1.3   rmind 
    218           1.3   rmind 	if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
    219           1.3   rmind 		err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
    220           1.3   rmind 	}
    221           1.3   rmind 
    222      1.10.2.4     jdc 	for (unsigned i = 0; i < __arraycount(stats); i++) {
    223      1.10.2.4     jdc 		const char *sname = stats[i].name;
    224      1.10.2.4     jdc 		int sidx = stats[i].index;
    225      1.10.2.4     jdc 
    226      1.10.2.4     jdc 		if (sidx == -1) {
    227      1.10.2.4     jdc 			printf("%s:\n", sname);
    228      1.10.2.4     jdc 		} else {
    229      1.10.2.4     jdc 			printf("\t%"PRIu64" %s\n", st[sidx], sname);
    230      1.10.2.4     jdc 		}
    231      1.10.2.4     jdc 	}
    232           1.4   rmind 
    233           1.3   rmind 	free(st);
    234           1.3   rmind 	return 0;
    235           1.3   rmind }
    236           1.3   rmind 
    237          1.10   rmind void
    238          1.10   rmind npfctl_print_error(const nl_error_t *ne)
    239          1.10   rmind {
    240          1.10   rmind 	static const char *ncode_errors[] = {
    241          1.10   rmind 		[-NPF_ERR_OPCODE]	= "invalid instruction",
    242          1.10   rmind 		[-NPF_ERR_JUMP]		= "invalid jump",
    243          1.10   rmind 		[-NPF_ERR_REG]		= "invalid register",
    244          1.10   rmind 		[-NPF_ERR_INVAL]	= "invalid argument value",
    245          1.10   rmind 		[-NPF_ERR_RANGE]	= "processing out of range"
    246          1.10   rmind 	};
    247          1.10   rmind 	const int nc_err = ne->ne_ncode_error;
    248          1.10   rmind 	const char *srcfile = ne->ne_source_file;
    249          1.10   rmind 
    250          1.10   rmind 	if (srcfile) {
    251          1.10   rmind 		warnx("source %s line %d", srcfile, ne->ne_source_line);
    252          1.10   rmind 	}
    253          1.10   rmind 	if (nc_err) {
    254          1.10   rmind 		warnx("n-code error (%d): %s at offset 0x%x",
    255          1.10   rmind 		    nc_err, ncode_errors[-nc_err], ne->ne_ncode_errat);
    256          1.10   rmind 	}
    257          1.10   rmind 	if (ne->ne_id) {
    258          1.10   rmind 		warnx("object: %d", ne->ne_id);
    259          1.10   rmind 	}
    260          1.10   rmind }
    261          1.10   rmind 
    262      1.10.2.4     jdc __dead static void
    263      1.10.2.3     riz npfctl_table(int fd, char **argv)
    264      1.10.2.3     riz {
    265      1.10.2.3     riz 	static const struct tblops_s {
    266      1.10.2.3     riz 		const char *	cmd;
    267      1.10.2.3     riz 		int		action;
    268      1.10.2.3     riz 	} tblops[] = {
    269      1.10.2.3     riz 		{ "add",	NPF_IOCTL_TBLENT_ADD },
    270      1.10.2.3     riz 		{ "rem",	NPF_IOCTL_TBLENT_REM },
    271      1.10.2.3     riz 		{ "test",	0 },
    272      1.10.2.3     riz 		{ NULL,		0 }
    273      1.10.2.3     riz 	};
    274      1.10.2.3     riz 	npf_ioctl_table_t nct;
    275      1.10.2.3     riz 	fam_addr_mask_t fam;
    276      1.10.2.3     riz 	char *cmd = argv[3];
    277      1.10.2.3     riz 	char *arg = argv[3];
    278      1.10.2.3     riz 	int n, alen;
    279      1.10.2.3     riz 
    280      1.10.2.3     riz 	memset(&nct, 0, sizeof(npf_ioctl_table_t));
    281      1.10.2.3     riz 	nct.nct_tid = atoi(argv[2]);
    282      1.10.2.3     riz 
    283      1.10.2.3     riz 	for (n = 0; tblops[n].cmd != NULL; n++) {
    284      1.10.2.3     riz 		if (strcmp(cmd, tblops[n].cmd) == 0) {
    285      1.10.2.3     riz 			nct.nct_action = tblops[n].action;
    286      1.10.2.3     riz 			arg = argv[4];
    287      1.10.2.3     riz 			break;
    288      1.10.2.3     riz 		}
    289      1.10.2.3     riz 	}
    290      1.10.2.3     riz 	if (!npfctl_parse_cidr(arg, &fam, &alen)) {
    291      1.10.2.3     riz 		errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
    292      1.10.2.3     riz 	}
    293      1.10.2.3     riz 	memcpy(&nct.nct_addr, &fam.fam_addr, sizeof(npf_addr_t));
    294      1.10.2.3     riz 	nct.nct_mask = fam.fam_mask;
    295      1.10.2.3     riz 	nct.nct_alen = alen;
    296      1.10.2.3     riz 
    297      1.10.2.3     riz 	if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
    298      1.10.2.3     riz 		errno = 0;
    299      1.10.2.3     riz 	}
    300      1.10.2.3     riz 	switch (errno) {
    301      1.10.2.3     riz 	case EEXIST:
    302      1.10.2.3     riz 		warnx("entry already exists or is conflicting");
    303      1.10.2.3     riz 		break;
    304      1.10.2.3     riz 	case ENOENT:
    305      1.10.2.3     riz 		warnx("no matching entry was not found");
    306      1.10.2.3     riz 		break;
    307      1.10.2.3     riz 	case EINVAL:
    308      1.10.2.3     riz 		warnx("invalid address, mask or table ID");
    309      1.10.2.3     riz 		break;
    310      1.10.2.3     riz 	case 0:
    311      1.10.2.3     riz 		printf("%s: %s\n", getprogname(), nct.nct_action == 0 ?
    312      1.10.2.3     riz 		    "matching entry found" : "success");
    313      1.10.2.3     riz 		break;
    314      1.10.2.3     riz 	default:
    315      1.10.2.3     riz 		warn("error");
    316      1.10.2.3     riz 	}
    317      1.10.2.3     riz 	exit(errno ? EXIT_FAILURE : EXIT_SUCCESS);
    318      1.10.2.3     riz }
    319      1.10.2.3     riz 
    320      1.10.2.3     riz static void
    321           1.1   rmind npfctl(int action, int argc, char **argv)
    322           1.1   rmind {
    323           1.1   rmind 	int fd, ret, ver, boolval;
    324           1.1   rmind 
    325           1.1   rmind 	fd = open(NPF_DEV_PATH, O_RDONLY);
    326           1.1   rmind 	if (fd == -1) {
    327           1.4   rmind 		err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
    328           1.1   rmind 	}
    329           1.1   rmind 	ret = ioctl(fd, IOC_NPF_VERSION, &ver);
    330      1.10.2.3     riz 	if (ret == -1) {
    331      1.10.2.3     riz 		err(EXIT_FAILURE, "ioctl");
    332      1.10.2.3     riz 	}
    333           1.1   rmind 	if (ver != NPF_VERSION) {
    334           1.4   rmind 		errx(EXIT_FAILURE,
    335      1.10.2.2     riz 		    "incompatible NPF interface version (%d, kernel %d)\n"
    336      1.10.2.2     riz 		    "Hint: update userland?", NPF_VERSION, ver);
    337           1.1   rmind 	}
    338           1.1   rmind 	switch (action) {
    339           1.1   rmind 	case NPFCTL_START:
    340           1.1   rmind 		boolval = true;
    341           1.1   rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    342           1.1   rmind 		break;
    343           1.1   rmind 	case NPFCTL_STOP:
    344           1.1   rmind 		boolval = false;
    345           1.1   rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    346           1.1   rmind 		break;
    347           1.1   rmind 	case NPFCTL_RELOAD:
    348           1.8   rmind 		npfctl_config_init(false);
    349           1.1   rmind 		npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
    350      1.10.2.5     riz 		ret = npfctl_config_send(fd, NULL);
    351      1.10.2.3     riz 		if (ret) {
    352      1.10.2.3     riz 			errx(EXIT_FAILURE, "ioctl: %s", strerror(ret));
    353      1.10.2.3     riz 		}
    354           1.1   rmind 		break;
    355      1.10.2.1     riz 	case NPFCTL_SHOWCONF:
    356      1.10.2.1     riz 		ret = npfctl_config_show(fd);
    357      1.10.2.1     riz 		break;
    358           1.1   rmind 	case NPFCTL_FLUSH:
    359           1.9   rmind 		ret = npf_config_flush(fd);
    360           1.1   rmind 		break;
    361           1.1   rmind 	case NPFCTL_TABLE:
    362           1.1   rmind 		if (argc < 5) {
    363           1.1   rmind 			usage();
    364           1.1   rmind 		}
    365      1.10.2.3     riz 		npfctl_table(fd, argv);
    366           1.1   rmind 		break;
    367           1.3   rmind 	case NPFCTL_STATS:
    368           1.3   rmind 		ret = npfctl_print_stats(fd);
    369           1.3   rmind 		break;
    370           1.3   rmind 	case NPFCTL_SESSIONS_SAVE:
    371      1.10.2.3     riz 		if (npf_sessions_recv(fd, NPF_SESSDB_PATH) != 0) {
    372           1.5   rmind 			errx(EXIT_FAILURE, "could not save sessions to '%s'",
    373           1.5   rmind 			    NPF_SESSDB_PATH);
    374           1.5   rmind 		}
    375           1.3   rmind 		break;
    376           1.3   rmind 	case NPFCTL_SESSIONS_LOAD:
    377      1.10.2.3     riz 		if (npf_sessions_send(fd, NPF_SESSDB_PATH) != 0) {
    378           1.5   rmind 			errx(EXIT_FAILURE, "no sessions loaded from '%s'",
    379           1.5   rmind 			    NPF_SESSDB_PATH);
    380           1.5   rmind 		}
    381           1.3   rmind 		break;
    382           1.1   rmind 	}
    383           1.7  zoltan 	if (ret) {
    384           1.1   rmind 		err(EXIT_FAILURE, "ioctl");
    385           1.1   rmind 	}
    386           1.1   rmind 	close(fd);
    387           1.1   rmind }
    388           1.1   rmind 
    389           1.1   rmind int
    390           1.1   rmind main(int argc, char **argv)
    391           1.1   rmind {
    392           1.1   rmind 	char *cmd;
    393           1.1   rmind 
    394           1.1   rmind 	if (argc < 2) {
    395           1.1   rmind 		usage();
    396           1.1   rmind 	}
    397           1.1   rmind 	cmd = argv[1];
    398           1.1   rmind 
    399           1.8   rmind 	if (strcmp(cmd, "debug") == 0) {
    400      1.10.2.2     riz 		const char *cfg = argc > 2 ? argv[2] : "/etc/npf.conf";
    401      1.10.2.5     riz 		const char *out = argc > 3 ? argv[3] : "/tmp/npf.plist";
    402      1.10.2.5     riz 
    403           1.8   rmind 		npfctl_config_init(true);
    404           1.8   rmind 		npfctl_parsecfg(cfg);
    405      1.10.2.5     riz 		npfctl_config_send(0, out);
    406           1.8   rmind 		return EXIT_SUCCESS;
    407           1.8   rmind 	}
    408           1.4   rmind 
    409      1.10.2.1     riz 	/* Find and call the subroutine. */
    410           1.8   rmind 	for (int n = 0; operations[n].cmd != NULL; n++) {
    411           1.1   rmind 		if (strcmp(cmd, operations[n].cmd) != 0)
    412           1.1   rmind 			continue;
    413           1.1   rmind 		npfctl(operations[n].action, argc, argv);
    414           1.8   rmind 		return EXIT_SUCCESS;
    415           1.1   rmind 	}
    416           1.3   rmind 	usage();
    417           1.1   rmind }
    418