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