Home | History | Annotate | Line # | Download | only in npfctl
npfctl.c revision 1.22
      1  1.22  martin /*	$NetBSD: npfctl.c,v 1.22 2012/10/31 08:54:39 martin 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.22  martin __RCSID("$NetBSD: npfctl.c,v 1.22 2012/10/31 08:54:39 martin 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.15   rmind #include <errno.h>
     46   1.1   rmind 
     47  1.21   rmind #include <util.h>
     48  1.21   rmind 
     49   1.1   rmind #include "npfctl.h"
     50   1.1   rmind 
     51   1.8   rmind extern int		yylineno, yycolumn;
     52   1.8   rmind extern const char *	yyfilename;
     53   1.8   rmind extern int		yyparse(void);
     54   1.8   rmind extern void		yyrestart(FILE *);
     55   1.8   rmind 
     56  1.11   rmind enum {
     57  1.11   rmind 	NPFCTL_START,
     58  1.11   rmind 	NPFCTL_STOP,
     59  1.11   rmind 	NPFCTL_RELOAD,
     60  1.11   rmind 	NPFCTL_SHOWCONF,
     61  1.11   rmind 	NPFCTL_FLUSH,
     62  1.11   rmind 	NPFCTL_TABLE,
     63  1.11   rmind 	NPFCTL_STATS,
     64  1.11   rmind 	NPFCTL_SESSIONS_SAVE,
     65  1.11   rmind 	NPFCTL_SESSIONS_LOAD,
     66  1.11   rmind };
     67   1.1   rmind 
     68  1.15   rmind static const struct operations_s {
     69   1.1   rmind 	const char *		cmd;
     70   1.1   rmind 	int			action;
     71   1.1   rmind } operations[] = {
     72   1.1   rmind 	/* Start, stop, reload */
     73   1.3   rmind 	{	"start",		NPFCTL_START		},
     74   1.3   rmind 	{	"stop",			NPFCTL_STOP		},
     75   1.3   rmind 	{	"reload",		NPFCTL_RELOAD		},
     76  1.11   rmind 	{	"show",			NPFCTL_SHOWCONF,	},
     77   1.3   rmind 	{	"flush",		NPFCTL_FLUSH		},
     78   1.1   rmind 	/* Table */
     79   1.3   rmind 	{	"table",		NPFCTL_TABLE		},
     80   1.3   rmind 	/* Stats */
     81   1.3   rmind 	{	"stats",		NPFCTL_STATS		},
     82   1.3   rmind 	/* Sessions */
     83   1.3   rmind 	{	"sess-save",		NPFCTL_SESSIONS_SAVE	},
     84   1.3   rmind 	{	"sess-load",		NPFCTL_SESSIONS_LOAD	},
     85   1.1   rmind 	/* --- */
     86   1.3   rmind 	{	NULL,			0			}
     87   1.1   rmind };
     88   1.1   rmind 
     89   1.1   rmind void *
     90   1.1   rmind zalloc(size_t sz)
     91   1.1   rmind {
     92   1.8   rmind 	void *p = malloc(sz);
     93   1.1   rmind 
     94   1.1   rmind 	if (p == NULL) {
     95   1.4   rmind 		err(EXIT_FAILURE, "zalloc");
     96   1.1   rmind 	}
     97   1.1   rmind 	memset(p, 0, sz);
     98   1.1   rmind 	return p;
     99   1.1   rmind }
    100   1.1   rmind 
    101   1.8   rmind void *
    102   1.8   rmind xrealloc(void *ptr, size_t size)
    103   1.8   rmind {
    104   1.8   rmind 	void *p = realloc(ptr, size);
    105   1.8   rmind 
    106   1.8   rmind 	if (p == NULL) {
    107   1.8   rmind 		err(EXIT_FAILURE, "xrealloc");
    108   1.8   rmind 	}
    109   1.8   rmind 	return p;
    110   1.8   rmind }
    111   1.8   rmind 
    112   1.1   rmind char *
    113   1.1   rmind xstrdup(const char *s)
    114   1.1   rmind {
    115   1.8   rmind 	char *p = strdup(s);
    116   1.8   rmind 
    117   1.8   rmind 	if (p == NULL) {
    118   1.8   rmind 		err(EXIT_FAILURE, "xstrdup");
    119   1.8   rmind 	}
    120   1.8   rmind 	return p;
    121   1.8   rmind }
    122   1.8   rmind 
    123   1.8   rmind char *
    124   1.8   rmind xstrndup(const char *s, size_t len)
    125   1.8   rmind {
    126   1.1   rmind 	char *p;
    127   1.1   rmind 
    128   1.8   rmind 	p = strndup(s, len);
    129   1.1   rmind 	if (p == NULL) {
    130   1.8   rmind 		err(EXIT_FAILURE, "xstrndup");
    131   1.1   rmind 	}
    132   1.1   rmind 	return p;
    133   1.1   rmind }
    134   1.1   rmind 
    135   1.6   joerg __dead static void
    136   1.1   rmind usage(void)
    137   1.1   rmind {
    138   1.1   rmind 	const char *progname = getprogname();
    139   1.1   rmind 
    140   1.1   rmind 	fprintf(stderr,
    141  1.13   rmind 	    "usage:\t%s [ start | stop | reload | flush | show | stats ]\n",
    142   1.3   rmind 	    progname);
    143   1.3   rmind 	fprintf(stderr,
    144  1.19   rmind 	    "\t%s ( sess-save | sess-load )\n",
    145   1.1   rmind 	    progname);
    146   1.1   rmind 	fprintf(stderr,
    147  1.21   rmind 	    "\t%s table <tid> { add | rem | test } <address/mask>\n",
    148   1.1   rmind 	    progname);
    149   1.1   rmind 	fprintf(stderr,
    150  1.21   rmind 	    "\t%s table <tid> { list | flush }\n",
    151   1.1   rmind 	    progname);
    152   1.1   rmind 
    153   1.1   rmind 	exit(EXIT_FAILURE);
    154   1.1   rmind }
    155   1.1   rmind 
    156   1.1   rmind static void
    157   1.1   rmind npfctl_parsecfg(const char *cfg)
    158   1.1   rmind {
    159   1.1   rmind 	FILE *fp;
    160   1.1   rmind 
    161   1.1   rmind 	fp = fopen(cfg, "r");
    162   1.1   rmind 	if (fp == NULL) {
    163   1.4   rmind 		err(EXIT_FAILURE, "open '%s'", cfg);
    164   1.1   rmind 	}
    165   1.8   rmind 	yyrestart(fp);
    166   1.8   rmind 	yylineno = 1;
    167   1.8   rmind 	yycolumn = 0;
    168   1.8   rmind 	yyfilename = cfg;
    169   1.8   rmind 	yyparse();
    170   1.8   rmind 	fclose(fp);
    171   1.1   rmind }
    172   1.1   rmind 
    173   1.3   rmind static int
    174   1.3   rmind npfctl_print_stats(int fd)
    175   1.3   rmind {
    176  1.17   rmind 	static const struct stats_s {
    177  1.17   rmind 		/* Note: -1 indicates a new section. */
    178  1.17   rmind 		int		index;
    179  1.17   rmind 		const char *	name;
    180  1.17   rmind 	} stats[] = {
    181  1.17   rmind 		{ -1, "Packets passed"					},
    182  1.17   rmind 		{ NPF_STAT_PASS_DEFAULT,	"default pass"		},
    183  1.17   rmind 		{ NPF_STAT_PASS_RULESET,	"ruleset pass"		},
    184  1.17   rmind 		{ NPF_STAT_PASS_SESSION,	"session pass"		},
    185  1.17   rmind 
    186  1.17   rmind 		{ -1, "Packets blocked"					},
    187  1.17   rmind 		{ NPF_STAT_BLOCK_DEFAULT,	"default block"		},
    188  1.17   rmind 		{ NPF_STAT_BLOCK_RULESET,	"ruleset block"		},
    189  1.17   rmind 
    190  1.17   rmind 		{ -1, "Session and NAT entries"				},
    191  1.17   rmind 		{ NPF_STAT_SESSION_CREATE,	"session allocations"	},
    192  1.17   rmind 		{ NPF_STAT_SESSION_DESTROY,	"session destructions"	},
    193  1.17   rmind 		{ NPF_STAT_NAT_CREATE,		"NAT entry allocations"	},
    194  1.17   rmind 		{ NPF_STAT_NAT_DESTROY,		"NAT entry destructions"},
    195  1.17   rmind 
    196  1.17   rmind 		{ -1, "Invalid packet state cases"			},
    197  1.17   rmind 		{ NPF_STAT_INVALID_STATE,	"cases in total"	},
    198  1.17   rmind 		{ NPF_STAT_INVALID_STATE_TCP1,	"TCP case I"		},
    199  1.17   rmind 		{ NPF_STAT_INVALID_STATE_TCP2,	"TCP case II"		},
    200  1.17   rmind 		{ NPF_STAT_INVALID_STATE_TCP3,	"TCP case III"		},
    201  1.17   rmind 
    202  1.17   rmind 		{ -1, "Packet race cases"				},
    203  1.17   rmind 		{ NPF_STAT_RACE_NAT,		"NAT association race"	},
    204  1.17   rmind 		{ NPF_STAT_RACE_SESSION,	"duplicate session race"},
    205  1.17   rmind 
    206  1.17   rmind 		{ -1, "Fragmentation"					},
    207  1.17   rmind 		{ NPF_STAT_FRAGMENTS,		"fragments"		},
    208  1.17   rmind 		{ NPF_STAT_REASSEMBLY,		"reassembled"		},
    209  1.17   rmind 		{ NPF_STAT_REASSFAIL,		"failed reassembly"	},
    210  1.17   rmind 
    211  1.17   rmind 		{ -1, "Other"						},
    212  1.17   rmind 		{ NPF_STAT_ERROR,		"unexpected errors"	},
    213  1.17   rmind 	};
    214   1.8   rmind 	uint64_t *st = zalloc(NPF_STATS_SIZE);
    215   1.3   rmind 
    216   1.3   rmind 	if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
    217   1.3   rmind 		err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
    218   1.3   rmind 	}
    219   1.3   rmind 
    220  1.17   rmind 	for (unsigned i = 0; i < __arraycount(stats); i++) {
    221  1.17   rmind 		const char *sname = stats[i].name;
    222  1.17   rmind 		int sidx = stats[i].index;
    223  1.17   rmind 
    224  1.17   rmind 		if (sidx == -1) {
    225  1.17   rmind 			printf("%s:\n", sname);
    226  1.17   rmind 		} else {
    227  1.17   rmind 			printf("\t%"PRIu64" %s\n", st[sidx], sname);
    228  1.17   rmind 		}
    229  1.17   rmind 	}
    230   1.4   rmind 
    231   1.3   rmind 	free(st);
    232   1.3   rmind 	return 0;
    233   1.3   rmind }
    234   1.3   rmind 
    235  1.10   rmind void
    236  1.10   rmind npfctl_print_error(const nl_error_t *ne)
    237  1.10   rmind {
    238  1.10   rmind 	static const char *ncode_errors[] = {
    239  1.10   rmind 		[-NPF_ERR_OPCODE]	= "invalid instruction",
    240  1.10   rmind 		[-NPF_ERR_JUMP]		= "invalid jump",
    241  1.10   rmind 		[-NPF_ERR_REG]		= "invalid register",
    242  1.10   rmind 		[-NPF_ERR_INVAL]	= "invalid argument value",
    243  1.10   rmind 		[-NPF_ERR_RANGE]	= "processing out of range"
    244  1.10   rmind 	};
    245  1.10   rmind 	const int nc_err = ne->ne_ncode_error;
    246  1.10   rmind 	const char *srcfile = ne->ne_source_file;
    247  1.10   rmind 
    248  1.10   rmind 	if (srcfile) {
    249  1.10   rmind 		warnx("source %s line %d", srcfile, ne->ne_source_line);
    250  1.10   rmind 	}
    251  1.10   rmind 	if (nc_err) {
    252  1.10   rmind 		warnx("n-code error (%d): %s at offset 0x%x",
    253  1.10   rmind 		    nc_err, ncode_errors[-nc_err], ne->ne_ncode_errat);
    254  1.10   rmind 	}
    255  1.10   rmind 	if (ne->ne_id) {
    256  1.10   rmind 		warnx("object: %d", ne->ne_id);
    257  1.10   rmind 	}
    258  1.10   rmind }
    259  1.10   rmind 
    260  1.21   rmind char *
    261  1.21   rmind npfctl_print_addrmask(int alen, npf_addr_t *addr, npf_netmask_t mask)
    262  1.21   rmind {
    263  1.21   rmind 	struct sockaddr_storage ss;
    264  1.21   rmind 	char *buf = zalloc(64);
    265  1.21   rmind 	int len;
    266  1.21   rmind 
    267  1.21   rmind 	switch (alen) {
    268  1.21   rmind 	case 4: {
    269  1.21   rmind 		struct sockaddr_in *sin = (void *)&ss;
    270  1.21   rmind 		sin->sin_len = sizeof(*sin);
    271  1.21   rmind 		sin->sin_family = AF_INET;
    272  1.21   rmind 		sin->sin_port = 0;
    273  1.21   rmind 		memcpy(&sin->sin_addr, addr, sizeof(sin->sin_addr));
    274  1.21   rmind 		break;
    275  1.21   rmind 	}
    276  1.21   rmind 	case 16: {
    277  1.21   rmind 		struct sockaddr_in6 *sin6 = (void *)&ss;
    278  1.21   rmind 		sin6->sin6_len = sizeof(*sin6);
    279  1.21   rmind 		sin6->sin6_family = AF_INET6;
    280  1.21   rmind 		sin6->sin6_port = 0;
    281  1.21   rmind 		memcpy(&sin6->sin6_addr, addr, sizeof(sin6->sin6_addr));
    282  1.21   rmind 		break;
    283  1.21   rmind 	}
    284  1.21   rmind 	default:
    285  1.21   rmind 		assert(false);
    286  1.21   rmind 	}
    287  1.21   rmind 	len = sockaddr_snprintf(buf, 64, "%a", (struct sockaddr *)&ss);
    288  1.21   rmind 	if (mask) {
    289  1.21   rmind 		snprintf(&buf[len], 64 - len, "/%u", mask);
    290  1.21   rmind 	}
    291  1.21   rmind 	return buf;
    292  1.21   rmind }
    293  1.21   rmind 
    294  1.16   joerg __dead static void
    295  1.21   rmind npfctl_table(int fd, int argc, char **argv)
    296  1.15   rmind {
    297  1.15   rmind 	static const struct tblops_s {
    298  1.15   rmind 		const char *	cmd;
    299  1.15   rmind 		int		action;
    300  1.15   rmind 	} tblops[] = {
    301  1.21   rmind 		{ "add",	NPF_IOCTL_TBLENT_ADD		},
    302  1.21   rmind 		{ "rem",	NPF_IOCTL_TBLENT_REM		},
    303  1.21   rmind 		{ "test",	NPF_IOCTL_TBLENT_LOOKUP		},
    304  1.21   rmind 		{ "list",	NPF_IOCTL_TBLENT_LIST		},
    305  1.21   rmind 		{ NULL,		0				}
    306  1.15   rmind 	};
    307  1.15   rmind 	npf_ioctl_table_t nct;
    308  1.15   rmind 	fam_addr_mask_t fam;
    309  1.21   rmind 	size_t buflen = 512;
    310  1.22  martin 	char *cmd, *arg = NULL; /* XXX gcc */
    311  1.15   rmind 	int n, alen;
    312  1.15   rmind 
    313  1.21   rmind 	/* Default action is list. */
    314  1.15   rmind 	memset(&nct, 0, sizeof(npf_ioctl_table_t));
    315  1.21   rmind 	nct.nct_tid = atoi(argv[0]);
    316  1.21   rmind 	cmd = argv[1];
    317  1.15   rmind 
    318  1.15   rmind 	for (n = 0; tblops[n].cmd != NULL; n++) {
    319  1.21   rmind 		if (strcmp(cmd, tblops[n].cmd) != 0) {
    320  1.21   rmind 			continue;
    321  1.21   rmind 		}
    322  1.21   rmind 		nct.nct_action = tblops[n].action;
    323  1.21   rmind 		break;
    324  1.21   rmind 	}
    325  1.21   rmind 	if (tblops[n].cmd == NULL) {
    326  1.21   rmind 		errx(EXIT_FAILURE, "invalid command '%s'", cmd);
    327  1.21   rmind 	}
    328  1.21   rmind 	if (nct.nct_action != NPF_IOCTL_TBLENT_LIST) {
    329  1.21   rmind 		if (argc < 3) {
    330  1.21   rmind 			usage();
    331  1.15   rmind 		}
    332  1.21   rmind 		arg = argv[2];
    333  1.15   rmind 	}
    334  1.21   rmind again:
    335  1.21   rmind 	if (nct.nct_action == NPF_IOCTL_TBLENT_LIST) {
    336  1.21   rmind 		nct.nct_data.buf.buf = zalloc(buflen);
    337  1.21   rmind 		nct.nct_data.buf.len = buflen;
    338  1.21   rmind 	} else {
    339  1.21   rmind 		if (!npfctl_parse_cidr(arg, &fam, &alen)) {
    340  1.21   rmind 			errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
    341  1.21   rmind 		}
    342  1.21   rmind 		nct.nct_data.ent.alen = alen;
    343  1.21   rmind 		memcpy(&nct.nct_data.ent.addr, &fam.fam_addr, sizeof(npf_addr_t));
    344  1.21   rmind 		nct.nct_data.ent.mask = fam.fam_mask;
    345  1.15   rmind 	}
    346  1.15   rmind 
    347  1.15   rmind 	if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
    348  1.15   rmind 		errno = 0;
    349  1.15   rmind 	}
    350  1.15   rmind 	switch (errno) {
    351  1.21   rmind 	case 0:
    352  1.21   rmind 		break;
    353  1.15   rmind 	case EEXIST:
    354  1.21   rmind 		errx(EXIT_FAILURE, "entry already exists or is conflicting");
    355  1.15   rmind 	case ENOENT:
    356  1.21   rmind 		errx(EXIT_FAILURE, "no matching entry was not found");
    357  1.15   rmind 	case EINVAL:
    358  1.21   rmind 		errx(EXIT_FAILURE, "invalid address, mask or table ID");
    359  1.21   rmind 	case ENOMEM:
    360  1.21   rmind 		if (nct.nct_action == NPF_IOCTL_TBLENT_LIST) {
    361  1.21   rmind 			/* XXX */
    362  1.21   rmind 			free(nct.nct_data.buf.buf);
    363  1.21   rmind 			buflen <<= 1;
    364  1.21   rmind 			goto again;
    365  1.21   rmind 		}
    366  1.21   rmind 		/* FALLTHROUGH */
    367  1.21   rmind 	default:
    368  1.21   rmind 		err(EXIT_FAILURE, "ioctl");
    369  1.21   rmind 	}
    370  1.21   rmind 
    371  1.21   rmind 	if (nct.nct_action == NPF_IOCTL_TBLENT_LIST) {
    372  1.21   rmind 		npf_ioctl_ent_t *ent = nct.nct_data.buf.buf;
    373  1.21   rmind 		char *buf;
    374  1.21   rmind 
    375  1.21   rmind 		while (nct.nct_data.buf.len--) {
    376  1.21   rmind 			if (!ent->alen)
    377  1.21   rmind 				break;
    378  1.21   rmind 			buf = npfctl_print_addrmask(ent->alen,
    379  1.21   rmind 			    &ent->addr, ent->mask);
    380  1.21   rmind 			puts(buf);
    381  1.21   rmind 			ent++;
    382  1.21   rmind 		}
    383  1.21   rmind 		free(nct.nct_data.buf.buf);
    384  1.21   rmind 	} else {
    385  1.21   rmind 		printf("%s: %s\n", getprogname(),
    386  1.21   rmind 		    nct.nct_action == NPF_IOCTL_TBLENT_LOOKUP ?
    387  1.15   rmind 		    "matching entry found" : "success");
    388  1.15   rmind 	}
    389  1.21   rmind 	exit(EXIT_SUCCESS);
    390  1.15   rmind }
    391  1.15   rmind 
    392  1.15   rmind static void
    393   1.1   rmind npfctl(int action, int argc, char **argv)
    394   1.1   rmind {
    395   1.1   rmind 	int fd, ret, ver, boolval;
    396   1.1   rmind 
    397   1.1   rmind 	fd = open(NPF_DEV_PATH, O_RDONLY);
    398   1.1   rmind 	if (fd == -1) {
    399   1.4   rmind 		err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
    400   1.1   rmind 	}
    401   1.1   rmind 	ret = ioctl(fd, IOC_NPF_VERSION, &ver);
    402  1.15   rmind 	if (ret == -1) {
    403  1.15   rmind 		err(EXIT_FAILURE, "ioctl");
    404  1.15   rmind 	}
    405   1.1   rmind 	if (ver != NPF_VERSION) {
    406   1.4   rmind 		errx(EXIT_FAILURE,
    407  1.14   rmind 		    "incompatible NPF interface version (%d, kernel %d)\n"
    408  1.14   rmind 		    "Hint: update userland?", NPF_VERSION, ver);
    409   1.1   rmind 	}
    410   1.1   rmind 	switch (action) {
    411   1.1   rmind 	case NPFCTL_START:
    412   1.1   rmind 		boolval = true;
    413   1.1   rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    414   1.1   rmind 		break;
    415   1.1   rmind 	case NPFCTL_STOP:
    416   1.1   rmind 		boolval = false;
    417   1.1   rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    418   1.1   rmind 		break;
    419   1.1   rmind 	case NPFCTL_RELOAD:
    420   1.8   rmind 		npfctl_config_init(false);
    421   1.1   rmind 		npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
    422  1.18   rmind 		ret = npfctl_config_send(fd, NULL);
    423  1.15   rmind 		if (ret) {
    424  1.15   rmind 			errx(EXIT_FAILURE, "ioctl: %s", strerror(ret));
    425  1.15   rmind 		}
    426   1.1   rmind 		break;
    427  1.11   rmind 	case NPFCTL_SHOWCONF:
    428  1.11   rmind 		ret = npfctl_config_show(fd);
    429  1.11   rmind 		break;
    430   1.1   rmind 	case NPFCTL_FLUSH:
    431   1.9   rmind 		ret = npf_config_flush(fd);
    432   1.1   rmind 		break;
    433   1.1   rmind 	case NPFCTL_TABLE:
    434  1.21   rmind 		if ((argc -= 2) < 2) {
    435   1.1   rmind 			usage();
    436   1.1   rmind 		}
    437  1.21   rmind 		argv += 2;
    438  1.21   rmind 		npfctl_table(fd, argc, argv);
    439   1.1   rmind 		break;
    440   1.3   rmind 	case NPFCTL_STATS:
    441   1.3   rmind 		ret = npfctl_print_stats(fd);
    442   1.3   rmind 		break;
    443   1.3   rmind 	case NPFCTL_SESSIONS_SAVE:
    444  1.15   rmind 		if (npf_sessions_recv(fd, NPF_SESSDB_PATH) != 0) {
    445   1.5   rmind 			errx(EXIT_FAILURE, "could not save sessions to '%s'",
    446   1.5   rmind 			    NPF_SESSDB_PATH);
    447   1.5   rmind 		}
    448   1.3   rmind 		break;
    449   1.3   rmind 	case NPFCTL_SESSIONS_LOAD:
    450  1.15   rmind 		if (npf_sessions_send(fd, NPF_SESSDB_PATH) != 0) {
    451   1.5   rmind 			errx(EXIT_FAILURE, "no sessions loaded from '%s'",
    452   1.5   rmind 			    NPF_SESSDB_PATH);
    453   1.5   rmind 		}
    454   1.3   rmind 		break;
    455   1.1   rmind 	}
    456   1.7  zoltan 	if (ret) {
    457   1.1   rmind 		err(EXIT_FAILURE, "ioctl");
    458   1.1   rmind 	}
    459   1.1   rmind 	close(fd);
    460   1.1   rmind }
    461   1.1   rmind 
    462   1.1   rmind int
    463   1.1   rmind main(int argc, char **argv)
    464   1.1   rmind {
    465   1.1   rmind 	char *cmd;
    466   1.1   rmind 
    467   1.1   rmind 	if (argc < 2) {
    468   1.1   rmind 		usage();
    469   1.1   rmind 	}
    470   1.1   rmind 	cmd = argv[1];
    471   1.1   rmind 
    472   1.8   rmind 	if (strcmp(cmd, "debug") == 0) {
    473  1.14   rmind 		const char *cfg = argc > 2 ? argv[2] : "/etc/npf.conf";
    474  1.18   rmind 		const char *out = argc > 3 ? argv[3] : "/tmp/npf.plist";
    475  1.18   rmind 
    476   1.8   rmind 		npfctl_config_init(true);
    477   1.8   rmind 		npfctl_parsecfg(cfg);
    478  1.18   rmind 		npfctl_config_send(0, out);
    479   1.8   rmind 		return EXIT_SUCCESS;
    480   1.8   rmind 	}
    481   1.4   rmind 
    482  1.12   rmind 	/* Find and call the subroutine. */
    483   1.8   rmind 	for (int n = 0; operations[n].cmd != NULL; n++) {
    484   1.1   rmind 		if (strcmp(cmd, operations[n].cmd) != 0)
    485   1.1   rmind 			continue;
    486   1.1   rmind 		npfctl(operations[n].action, argc, argv);
    487   1.8   rmind 		return EXIT_SUCCESS;
    488   1.1   rmind 	}
    489   1.3   rmind 	usage();
    490   1.1   rmind }
    491