Home | History | Annotate | Line # | Download | only in npfctl
npfctl.c revision 1.47.2.2
      1  1.47.2.2  pgoyette /*	$NetBSD: npfctl.c,v 1.47.2.2 2017/03/20 06:58:08 pgoyette Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*-
      4      1.41     rmind  * Copyright (c) 2009-2014 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.47.2.2  pgoyette __RCSID("$NetBSD: npfctl.c,v 1.47.2.2 2017/03/20 06:58:08 pgoyette Exp $");
     34       1.2     rmind 
     35       1.1     rmind #include <sys/stat.h>
     36       1.1     rmind #include <sys/types.h>
     37  1.47.2.1  pgoyette #include <sys/mman.h>
     38  1.47.2.1  pgoyette #ifdef __NetBSD__
     39  1.47.2.1  pgoyette #include <sha1.h>
     40  1.47.2.1  pgoyette #include <sys/ioctl.h>
     41      1.44     rmind #include <sys/module.h>
     42  1.47.2.1  pgoyette #define SHA_DIGEST_LENGTH SHA1_DIGEST_LENGTH
     43  1.47.2.1  pgoyette #else
     44  1.47.2.1  pgoyette #include <openssl/sha.h>
     45  1.47.2.1  pgoyette #endif
     46       1.1     rmind 
     47       1.1     rmind #include <stdio.h>
     48       1.1     rmind #include <stdlib.h>
     49       1.1     rmind #include <string.h>
     50       1.1     rmind #include <err.h>
     51       1.1     rmind #include <fcntl.h>
     52       1.1     rmind #include <unistd.h>
     53      1.15     rmind #include <errno.h>
     54  1.47.2.1  pgoyette 
     55  1.47.2.1  pgoyette #include <arpa/inet.h>
     56      1.29     rmind 
     57       1.1     rmind #include "npfctl.h"
     58       1.1     rmind 
     59      1.29     rmind extern void		npf_yyparse_string(const char *);
     60       1.8     rmind 
     61      1.11     rmind enum {
     62      1.11     rmind 	NPFCTL_START,
     63      1.11     rmind 	NPFCTL_STOP,
     64      1.11     rmind 	NPFCTL_RELOAD,
     65      1.11     rmind 	NPFCTL_SHOWCONF,
     66      1.11     rmind 	NPFCTL_FLUSH,
     67      1.25     rmind 	NPFCTL_VALIDATE,
     68      1.11     rmind 	NPFCTL_TABLE,
     69      1.29     rmind 	NPFCTL_RULE,
     70      1.11     rmind 	NPFCTL_STATS,
     71      1.41     rmind 	NPFCTL_SAVE,
     72      1.41     rmind 	NPFCTL_LOAD,
     73  1.47.2.1  pgoyette 	NPFCTL_DEBUG,
     74  1.47.2.1  pgoyette 	NPFCTL_CONN_LIST,
     75      1.11     rmind };
     76       1.1     rmind 
     77      1.15     rmind static const struct operations_s {
     78       1.1     rmind 	const char *		cmd;
     79       1.1     rmind 	int			action;
     80       1.1     rmind } operations[] = {
     81       1.1     rmind 	/* Start, stop, reload */
     82      1.41     rmind 	{	"start",	NPFCTL_START		},
     83      1.41     rmind 	{	"stop",		NPFCTL_STOP		},
     84      1.41     rmind 	{	"reload",	NPFCTL_RELOAD		},
     85      1.41     rmind 	{	"show",		NPFCTL_SHOWCONF,	},
     86      1.41     rmind 	{	"flush",	NPFCTL_FLUSH		},
     87       1.1     rmind 	/* Table */
     88      1.41     rmind 	{	"table",	NPFCTL_TABLE		},
     89      1.29     rmind 	/* Rule */
     90      1.41     rmind 	{	"rule",		NPFCTL_RULE		},
     91       1.3     rmind 	/* Stats */
     92      1.41     rmind 	{	"stats",	NPFCTL_STATS		},
     93      1.41     rmind 	/* Full state save/load */
     94      1.41     rmind 	{	"save",		NPFCTL_SAVE		},
     95      1.41     rmind 	{	"load",		NPFCTL_LOAD		},
     96  1.47.2.1  pgoyette 	{	"list",		NPFCTL_CONN_LIST	},
     97  1.47.2.1  pgoyette 	/* Misc. */
     98  1.47.2.1  pgoyette 	{	"valid",	NPFCTL_VALIDATE		},
     99  1.47.2.1  pgoyette 	{	"debug",	NPFCTL_DEBUG		},
    100       1.1     rmind 	/* --- */
    101      1.41     rmind 	{	NULL,		0			}
    102       1.1     rmind };
    103       1.1     rmind 
    104      1.38     rmind bool
    105      1.38     rmind join(char *buf, size_t buflen, int count, char **args, const char *sep)
    106      1.29     rmind {
    107      1.38     rmind 	const u_int seplen = strlen(sep);
    108      1.29     rmind 	char *s = buf, *p = NULL;
    109      1.29     rmind 
    110      1.29     rmind 	for (int i = 0; i < count; i++) {
    111      1.29     rmind 		size_t len;
    112      1.29     rmind 
    113      1.29     rmind 		p = stpncpy(s, args[i], buflen);
    114      1.38     rmind 		len = p - s + seplen;
    115      1.29     rmind 		if (len >= buflen) {
    116      1.29     rmind 			return false;
    117      1.29     rmind 		}
    118      1.29     rmind 		buflen -= len;
    119      1.38     rmind 		strcpy(p, sep);
    120      1.38     rmind 		s = p + seplen;
    121      1.29     rmind 	}
    122      1.29     rmind 	*p = '\0';
    123      1.29     rmind 	return true;
    124      1.29     rmind }
    125      1.29     rmind 
    126       1.6     joerg __dead static void
    127       1.1     rmind usage(void)
    128       1.1     rmind {
    129       1.1     rmind 	const char *progname = getprogname();
    130       1.1     rmind 
    131       1.1     rmind 	fprintf(stderr,
    132      1.37     rmind 	    "Usage:\t%s start | stop | flush | show | stats\n",
    133      1.33  christos 	    progname);
    134      1.33  christos 	fprintf(stderr,
    135      1.33  christos 	    "\t%s validate | reload [<rule-file>]\n",
    136       1.3     rmind 	    progname);
    137       1.3     rmind 	fprintf(stderr,
    138      1.29     rmind 	    "\t%s rule \"rule-name\" { add | rem } <rule-syntax>\n",
    139      1.29     rmind 	    progname);
    140      1.29     rmind 	fprintf(stderr,
    141      1.29     rmind 	    "\t%s rule \"rule-name\" rem-id <rule-id>\n",
    142       1.1     rmind 	    progname);
    143       1.1     rmind 	fprintf(stderr,
    144      1.31     rmind 	    "\t%s rule \"rule-name\" { list | flush }\n",
    145      1.31     rmind 	    progname);
    146      1.31     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.37     rmind 	fprintf(stderr,
    153      1.41     rmind 	    "\t%s save | load\n",
    154      1.37     rmind 	    progname);
    155  1.47.2.1  pgoyette 	fprintf(stderr,
    156  1.47.2.1  pgoyette 	    "\t%s list [-46hNnw] [-i <ifname>]\n",
    157  1.47.2.1  pgoyette 	    progname);
    158       1.1     rmind 	exit(EXIT_FAILURE);
    159       1.1     rmind }
    160       1.1     rmind 
    161       1.3     rmind static int
    162       1.3     rmind npfctl_print_stats(int fd)
    163       1.3     rmind {
    164      1.17     rmind 	static const struct stats_s {
    165      1.17     rmind 		/* Note: -1 indicates a new section. */
    166      1.17     rmind 		int		index;
    167      1.17     rmind 		const char *	name;
    168      1.17     rmind 	} stats[] = {
    169      1.17     rmind 		{ -1, "Packets passed"					},
    170      1.17     rmind 		{ NPF_STAT_PASS_DEFAULT,	"default pass"		},
    171      1.17     rmind 		{ NPF_STAT_PASS_RULESET,	"ruleset pass"		},
    172      1.41     rmind 		{ NPF_STAT_PASS_CONN,		"state pass"		},
    173      1.17     rmind 
    174      1.17     rmind 		{ -1, "Packets blocked"					},
    175      1.17     rmind 		{ NPF_STAT_BLOCK_DEFAULT,	"default block"		},
    176      1.17     rmind 		{ NPF_STAT_BLOCK_RULESET,	"ruleset block"		},
    177      1.17     rmind 
    178      1.41     rmind 		{ -1, "State and NAT entries"				},
    179      1.41     rmind 		{ NPF_STAT_CONN_CREATE,		"state allocations"},
    180      1.41     rmind 		{ NPF_STAT_CONN_DESTROY,	"state destructions"},
    181      1.17     rmind 		{ NPF_STAT_NAT_CREATE,		"NAT entry allocations"	},
    182      1.17     rmind 		{ NPF_STAT_NAT_DESTROY,		"NAT entry destructions"},
    183      1.17     rmind 
    184      1.27     rmind 		{ -1, "Network buffers"					},
    185      1.27     rmind 		{ NPF_STAT_NBUF_NONCONTIG,	"non-contiguous cases"	},
    186      1.27     rmind 		{ NPF_STAT_NBUF_CONTIG_FAIL,	"contig alloc failures"	},
    187      1.27     rmind 
    188      1.17     rmind 		{ -1, "Invalid packet state cases"			},
    189      1.17     rmind 		{ NPF_STAT_INVALID_STATE,	"cases in total"	},
    190      1.17     rmind 		{ NPF_STAT_INVALID_STATE_TCP1,	"TCP case I"		},
    191      1.17     rmind 		{ NPF_STAT_INVALID_STATE_TCP2,	"TCP case II"		},
    192      1.17     rmind 		{ NPF_STAT_INVALID_STATE_TCP3,	"TCP case III"		},
    193      1.17     rmind 
    194      1.17     rmind 		{ -1, "Packet race cases"				},
    195      1.17     rmind 		{ NPF_STAT_RACE_NAT,		"NAT association race"	},
    196      1.41     rmind 		{ NPF_STAT_RACE_CONN,		"duplicate state race"	},
    197      1.17     rmind 
    198      1.17     rmind 		{ -1, "Fragmentation"					},
    199      1.17     rmind 		{ NPF_STAT_FRAGMENTS,		"fragments"		},
    200      1.17     rmind 		{ NPF_STAT_REASSEMBLY,		"reassembled"		},
    201      1.17     rmind 		{ NPF_STAT_REASSFAIL,		"failed reassembly"	},
    202      1.17     rmind 
    203      1.17     rmind 		{ -1, "Other"						},
    204      1.17     rmind 		{ NPF_STAT_ERROR,		"unexpected errors"	},
    205      1.17     rmind 	};
    206      1.24     rmind 	uint64_t *st = ecalloc(1, NPF_STATS_SIZE);
    207       1.3     rmind 
    208       1.3     rmind 	if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
    209       1.3     rmind 		err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
    210       1.3     rmind 	}
    211       1.3     rmind 
    212      1.17     rmind 	for (unsigned i = 0; i < __arraycount(stats); i++) {
    213      1.17     rmind 		const char *sname = stats[i].name;
    214      1.17     rmind 		int sidx = stats[i].index;
    215      1.17     rmind 
    216      1.17     rmind 		if (sidx == -1) {
    217      1.17     rmind 			printf("%s:\n", sname);
    218      1.17     rmind 		} else {
    219      1.17     rmind 			printf("\t%"PRIu64" %s\n", st[sidx], sname);
    220      1.17     rmind 		}
    221      1.17     rmind 	}
    222       1.4     rmind 
    223       1.3     rmind 	free(st);
    224       1.3     rmind 	return 0;
    225       1.3     rmind }
    226       1.3     rmind 
    227      1.10     rmind void
    228  1.47.2.1  pgoyette npfctl_print_error(const npf_error_t *ne)
    229      1.10     rmind {
    230  1.47.2.1  pgoyette 	const char *srcfile = ne->source_file;
    231      1.10     rmind 
    232      1.10     rmind 	if (srcfile) {
    233  1.47.2.1  pgoyette 		warnx("source %s line %d", srcfile, ne->source_line);
    234      1.10     rmind 	}
    235  1.47.2.1  pgoyette 	if (ne->id) {
    236  1.47.2.1  pgoyette 		warnx("object: %" PRIi64, ne->id);
    237      1.10     rmind 	}
    238      1.10     rmind }
    239      1.10     rmind 
    240      1.21     rmind char *
    241  1.47.2.1  pgoyette npfctl_print_addrmask(int alen, const char *fmt, const npf_addr_t *addr,
    242  1.47.2.1  pgoyette     npf_netmask_t mask)
    243      1.21     rmind {
    244  1.47.2.1  pgoyette 	const unsigned buflen = 256;
    245  1.47.2.1  pgoyette 	char *buf = ecalloc(1, buflen);
    246      1.21     rmind 	struct sockaddr_storage ss;
    247  1.47.2.1  pgoyette 
    248  1.47.2.1  pgoyette 	memset(&ss, 0, sizeof(ss));
    249      1.21     rmind 
    250      1.21     rmind 	switch (alen) {
    251      1.21     rmind 	case 4: {
    252      1.21     rmind 		struct sockaddr_in *sin = (void *)&ss;
    253      1.21     rmind 		sin->sin_family = AF_INET;
    254      1.21     rmind 		memcpy(&sin->sin_addr, addr, sizeof(sin->sin_addr));
    255      1.21     rmind 		break;
    256      1.21     rmind 	}
    257      1.21     rmind 	case 16: {
    258      1.21     rmind 		struct sockaddr_in6 *sin6 = (void *)&ss;
    259      1.21     rmind 		sin6->sin6_family = AF_INET6;
    260      1.21     rmind 		memcpy(&sin6->sin6_addr, addr, sizeof(sin6->sin6_addr));
    261      1.21     rmind 		break;
    262      1.21     rmind 	}
    263      1.21     rmind 	default:
    264      1.21     rmind 		assert(false);
    265      1.21     rmind 	}
    266  1.47.2.1  pgoyette 	sockaddr_snprintf(buf, buflen, fmt, (const void *)&ss);
    267      1.38     rmind 	if (mask && mask != NPF_NO_NETMASK) {
    268  1.47.2.1  pgoyette 		const unsigned len = strlen(buf);
    269  1.47.2.1  pgoyette 		snprintf(&buf[len], buflen - len, "/%u", mask);
    270      1.21     rmind 	}
    271      1.21     rmind 	return buf;
    272      1.21     rmind }
    273      1.21     rmind 
    274      1.16     joerg __dead static void
    275      1.21     rmind npfctl_table(int fd, int argc, char **argv)
    276      1.15     rmind {
    277      1.15     rmind 	static const struct tblops_s {
    278      1.15     rmind 		const char *	cmd;
    279      1.15     rmind 		int		action;
    280      1.15     rmind 	} tblops[] = {
    281      1.29     rmind 		{ "add",	NPF_CMD_TABLE_ADD		},
    282      1.29     rmind 		{ "rem",	NPF_CMD_TABLE_REMOVE		},
    283      1.29     rmind 		{ "del",	NPF_CMD_TABLE_REMOVE		},
    284      1.29     rmind 		{ "test",	NPF_CMD_TABLE_LOOKUP		},
    285      1.29     rmind 		{ "list",	NPF_CMD_TABLE_LIST		},
    286      1.37     rmind 		{ "flush",	NPF_CMD_TABLE_FLUSH		},
    287      1.21     rmind 		{ NULL,		0				}
    288      1.15     rmind 	};
    289      1.15     rmind 	npf_ioctl_table_t nct;
    290      1.15     rmind 	fam_addr_mask_t fam;
    291      1.21     rmind 	size_t buflen = 512;
    292      1.40     rmind 	char *cmd, *arg;
    293      1.15     rmind 	int n, alen;
    294      1.15     rmind 
    295      1.21     rmind 	/* Default action is list. */
    296      1.15     rmind 	memset(&nct, 0, sizeof(npf_ioctl_table_t));
    297      1.40     rmind 	nct.nct_name = argv[0];
    298      1.21     rmind 	cmd = argv[1];
    299      1.15     rmind 
    300      1.15     rmind 	for (n = 0; tblops[n].cmd != NULL; n++) {
    301      1.21     rmind 		if (strcmp(cmd, tblops[n].cmd) != 0) {
    302      1.21     rmind 			continue;
    303      1.21     rmind 		}
    304      1.29     rmind 		nct.nct_cmd = tblops[n].action;
    305      1.21     rmind 		break;
    306      1.21     rmind 	}
    307      1.21     rmind 	if (tblops[n].cmd == NULL) {
    308      1.21     rmind 		errx(EXIT_FAILURE, "invalid command '%s'", cmd);
    309      1.21     rmind 	}
    310      1.37     rmind 
    311      1.37     rmind 	switch (nct.nct_cmd) {
    312      1.37     rmind 	case NPF_CMD_TABLE_LIST:
    313      1.37     rmind 	case NPF_CMD_TABLE_FLUSH:
    314      1.40     rmind 		arg = NULL;
    315      1.37     rmind 		break;
    316      1.37     rmind 	default:
    317      1.21     rmind 		if (argc < 3) {
    318      1.21     rmind 			usage();
    319      1.15     rmind 		}
    320      1.21     rmind 		arg = argv[2];
    321      1.15     rmind 	}
    322      1.37     rmind 
    323      1.21     rmind again:
    324      1.37     rmind 	switch (nct.nct_cmd) {
    325      1.37     rmind 	case NPF_CMD_TABLE_LIST:
    326      1.24     rmind 		nct.nct_data.buf.buf = ecalloc(1, buflen);
    327      1.21     rmind 		nct.nct_data.buf.len = buflen;
    328      1.37     rmind 		break;
    329      1.37     rmind 	case NPF_CMD_TABLE_FLUSH:
    330      1.37     rmind 		break;
    331      1.37     rmind 	default:
    332      1.21     rmind 		if (!npfctl_parse_cidr(arg, &fam, &alen)) {
    333      1.21     rmind 			errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
    334      1.21     rmind 		}
    335      1.21     rmind 		nct.nct_data.ent.alen = alen;
    336      1.26     rmind 		memcpy(&nct.nct_data.ent.addr, &fam.fam_addr, alen);
    337      1.21     rmind 		nct.nct_data.ent.mask = fam.fam_mask;
    338      1.15     rmind 	}
    339      1.15     rmind 
    340      1.15     rmind 	if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
    341      1.15     rmind 		errno = 0;
    342      1.15     rmind 	}
    343      1.15     rmind 	switch (errno) {
    344      1.21     rmind 	case 0:
    345      1.21     rmind 		break;
    346      1.15     rmind 	case EEXIST:
    347      1.21     rmind 		errx(EXIT_FAILURE, "entry already exists or is conflicting");
    348      1.15     rmind 	case ENOENT:
    349      1.21     rmind 		errx(EXIT_FAILURE, "no matching entry was not found");
    350      1.15     rmind 	case EINVAL:
    351      1.21     rmind 		errx(EXIT_FAILURE, "invalid address, mask or table ID");
    352      1.21     rmind 	case ENOMEM:
    353      1.29     rmind 		if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
    354      1.21     rmind 			/* XXX */
    355      1.21     rmind 			free(nct.nct_data.buf.buf);
    356      1.21     rmind 			buflen <<= 1;
    357      1.21     rmind 			goto again;
    358      1.21     rmind 		}
    359      1.21     rmind 		/* FALLTHROUGH */
    360      1.21     rmind 	default:
    361      1.32  christos 		err(EXIT_FAILURE, "ioctl(IOC_NPF_TABLE)");
    362      1.21     rmind 	}
    363      1.21     rmind 
    364      1.29     rmind 	if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
    365      1.21     rmind 		npf_ioctl_ent_t *ent = nct.nct_data.buf.buf;
    366      1.21     rmind 		char *buf;
    367      1.21     rmind 
    368      1.21     rmind 		while (nct.nct_data.buf.len--) {
    369      1.21     rmind 			if (!ent->alen)
    370      1.21     rmind 				break;
    371  1.47.2.1  pgoyette 			buf = npfctl_print_addrmask(ent->alen, "%a",
    372      1.21     rmind 			    &ent->addr, ent->mask);
    373      1.21     rmind 			puts(buf);
    374      1.21     rmind 			ent++;
    375      1.21     rmind 		}
    376      1.21     rmind 		free(nct.nct_data.buf.buf);
    377      1.21     rmind 	} else {
    378      1.21     rmind 		printf("%s: %s\n", getprogname(),
    379      1.29     rmind 		    nct.nct_cmd == NPF_CMD_TABLE_LOOKUP ?
    380      1.15     rmind 		    "matching entry found" : "success");
    381      1.15     rmind 	}
    382      1.21     rmind 	exit(EXIT_SUCCESS);
    383      1.15     rmind }
    384      1.15     rmind 
    385      1.29     rmind static nl_rule_t *
    386      1.29     rmind npfctl_parse_rule(int argc, char **argv)
    387      1.29     rmind {
    388      1.29     rmind 	char rule_string[1024];
    389      1.29     rmind 	nl_rule_t *rl;
    390      1.29     rmind 
    391      1.29     rmind 	/* Get the rule string and parse it. */
    392      1.38     rmind 	if (!join(rule_string, sizeof(rule_string), argc, argv, " ")) {
    393      1.29     rmind 		errx(EXIT_FAILURE, "command too long");
    394      1.29     rmind 	}
    395      1.29     rmind 	npfctl_parse_string(rule_string);
    396      1.29     rmind 	if ((rl = npfctl_rule_ref()) == NULL) {
    397      1.29     rmind 		errx(EXIT_FAILURE, "could not parse the rule");
    398      1.29     rmind 	}
    399      1.29     rmind 	return rl;
    400      1.29     rmind }
    401      1.29     rmind 
    402  1.47.2.1  pgoyette #ifdef __NetBSD__
    403  1.47.2.1  pgoyette static unsigned char *
    404  1.47.2.1  pgoyette SHA1(const unsigned char *d, size_t l, unsigned char *md)
    405  1.47.2.1  pgoyette {
    406  1.47.2.1  pgoyette 	SHA1_CTX c;
    407  1.47.2.1  pgoyette 
    408  1.47.2.1  pgoyette 	SHA1Init(&c);
    409  1.47.2.1  pgoyette 	SHA1Update(&c, d, l);
    410  1.47.2.1  pgoyette 	SHA1Final(md, &c);
    411  1.47.2.1  pgoyette 	return md;
    412      1.47  christos }
    413  1.47.2.1  pgoyette #endif
    414      1.47  christos 
    415      1.47  christos static void
    416      1.29     rmind npfctl_generate_key(nl_rule_t *rl, void *key)
    417      1.29     rmind {
    418      1.29     rmind 	void *meta;
    419      1.29     rmind 	size_t len;
    420      1.29     rmind 
    421      1.29     rmind 	if ((meta = npf_rule_export(rl, &len)) == NULL) {
    422      1.29     rmind 		errx(EXIT_FAILURE, "error generating rule key");
    423      1.29     rmind 	}
    424  1.47.2.1  pgoyette 	__CTASSERT(NPF_RULE_MAXKEYLEN >= SHA_DIGEST_LENGTH);
    425      1.29     rmind 	memset(key, 0, NPF_RULE_MAXKEYLEN);
    426  1.47.2.1  pgoyette 	SHA1(meta, len, key);
    427      1.29     rmind 	free(meta);
    428      1.29     rmind }
    429      1.29     rmind 
    430      1.29     rmind __dead static void
    431      1.29     rmind npfctl_rule(int fd, int argc, char **argv)
    432      1.29     rmind {
    433      1.29     rmind 	static const struct ruleops_s {
    434      1.29     rmind 		const char *	cmd;
    435      1.29     rmind 		int		action;
    436      1.36     rmind 		bool		extra_arg;
    437      1.29     rmind 	} ruleops[] = {
    438      1.36     rmind 		{ "add",	NPF_CMD_RULE_ADD,	true	},
    439      1.36     rmind 		{ "rem",	NPF_CMD_RULE_REMKEY,	true	},
    440      1.36     rmind 		{ "del",	NPF_CMD_RULE_REMKEY,	true	},
    441      1.36     rmind 		{ "rem-id",	NPF_CMD_RULE_REMOVE,	true	},
    442      1.36     rmind 		{ "list",	NPF_CMD_RULE_LIST,	false	},
    443      1.36     rmind 		{ "flush",	NPF_CMD_RULE_FLUSH,	false	},
    444      1.36     rmind 		{ NULL,		0,			0	}
    445      1.29     rmind 	};
    446      1.29     rmind 	uint8_t key[NPF_RULE_MAXKEYLEN];
    447      1.29     rmind 	const char *ruleset_name = argv[0];
    448      1.29     rmind 	const char *cmd = argv[1];
    449      1.29     rmind 	int error, action = 0;
    450      1.31     rmind 	uint64_t rule_id;
    451      1.36     rmind 	bool extra_arg;
    452      1.29     rmind 	nl_rule_t *rl;
    453      1.29     rmind 
    454      1.29     rmind 	for (int n = 0; ruleops[n].cmd != NULL; n++) {
    455      1.29     rmind 		if (strcmp(cmd, ruleops[n].cmd) == 0) {
    456      1.29     rmind 			action = ruleops[n].action;
    457      1.36     rmind 			extra_arg = ruleops[n].extra_arg;
    458      1.29     rmind 			break;
    459      1.29     rmind 		}
    460      1.29     rmind 	}
    461      1.36     rmind 	argc -= 2;
    462      1.36     rmind 	argv += 2;
    463      1.29     rmind 
    464      1.36     rmind 	if (!action || (extra_arg && argc == 0)) {
    465      1.29     rmind 		usage();
    466      1.29     rmind 	}
    467      1.29     rmind 
    468      1.29     rmind 	switch (action) {
    469      1.29     rmind 	case NPF_CMD_RULE_ADD:
    470      1.29     rmind 		rl = npfctl_parse_rule(argc, argv);
    471      1.29     rmind 		npfctl_generate_key(rl, key);
    472      1.29     rmind 		npf_rule_setkey(rl, key, sizeof(key));
    473      1.29     rmind 		error = npf_ruleset_add(fd, ruleset_name, rl, &rule_id);
    474      1.29     rmind 		break;
    475      1.29     rmind 	case NPF_CMD_RULE_REMKEY:
    476      1.29     rmind 		rl = npfctl_parse_rule(argc, argv);
    477      1.29     rmind 		npfctl_generate_key(rl, key);
    478      1.29     rmind 		error = npf_ruleset_remkey(fd, ruleset_name, key, sizeof(key));
    479      1.29     rmind 		break;
    480      1.29     rmind 	case NPF_CMD_RULE_REMOVE:
    481      1.31     rmind 		rule_id = strtoull(argv[0], NULL, 16);
    482      1.29     rmind 		error = npf_ruleset_remove(fd, ruleset_name, rule_id);
    483      1.29     rmind 		break;
    484      1.30     rmind 	case NPF_CMD_RULE_LIST:
    485      1.30     rmind 		error = npfctl_ruleset_show(fd, ruleset_name);
    486      1.30     rmind 		break;
    487      1.30     rmind 	case NPF_CMD_RULE_FLUSH:
    488      1.30     rmind 		error = npf_ruleset_flush(fd, ruleset_name);
    489      1.30     rmind 		break;
    490      1.29     rmind 	default:
    491  1.47.2.1  pgoyette 		abort();
    492      1.29     rmind 	}
    493      1.29     rmind 
    494      1.29     rmind 	switch (error) {
    495      1.29     rmind 	case 0:
    496      1.29     rmind 		/* Success. */
    497      1.29     rmind 		break;
    498      1.31     rmind 	case ESRCH:
    499      1.31     rmind 		errx(EXIT_FAILURE, "ruleset \"%s\" not found", ruleset_name);
    500      1.29     rmind 	case ENOENT:
    501      1.31     rmind 		errx(EXIT_FAILURE, "rule was not found");
    502      1.29     rmind 	default:
    503      1.29     rmind 		errx(EXIT_FAILURE, "rule operation: %s", strerror(error));
    504      1.29     rmind 	}
    505      1.29     rmind 	if (action == NPF_CMD_RULE_ADD) {
    506      1.31     rmind 		printf("OK %" PRIx64 "\n", rule_id);
    507      1.29     rmind 	}
    508      1.29     rmind 	exit(EXIT_SUCCESS);
    509      1.29     rmind }
    510      1.29     rmind 
    511      1.45  christos static bool bpfjit = true;
    512      1.45  christos 
    513      1.45  christos void
    514      1.45  christos npfctl_bpfjit(bool onoff)
    515      1.45  christos {
    516      1.45  christos 	bpfjit = onoff;
    517      1.45  christos }
    518      1.45  christos 
    519      1.44     rmind static void
    520      1.44     rmind npfctl_preload_bpfjit(void)
    521      1.44     rmind {
    522  1.47.2.1  pgoyette #ifdef __NetBSD__
    523      1.44     rmind 	modctl_load_t args = {
    524      1.44     rmind 		.ml_filename = "bpfjit",
    525      1.44     rmind 		.ml_flags = MODCTL_NO_PROP,
    526      1.44     rmind 		.ml_props = NULL,
    527      1.44     rmind 		.ml_propslen = 0
    528      1.44     rmind 	};
    529      1.44     rmind 
    530      1.45  christos 	if (!bpfjit)
    531      1.45  christos 		return;
    532      1.45  christos 
    533      1.44     rmind 	if (modctl(MODCTL_LOAD, &args) != 0 && errno != EEXIST) {
    534      1.45  christos 		static const char *p = "; performance will be degraded";
    535      1.45  christos 		if (errno == ENOENT)
    536      1.45  christos 			warnx("the bpfjit module seems to be missing%s", p);
    537      1.45  christos 		else
    538      1.45  christos 			warn("error loading the bpfjit module%s", p);
    539      1.45  christos 		warnx("To disable this warning `set bpf.jit off' in "
    540      1.45  christos 		    "/etc/npf.conf");
    541      1.44     rmind 	}
    542  1.47.2.1  pgoyette #endif
    543      1.44     rmind }
    544      1.44     rmind 
    545      1.41     rmind static int
    546  1.47.2.1  pgoyette npfctl_load(int fd)
    547      1.41     rmind {
    548      1.41     rmind 	nl_config_t *ncf;
    549  1.47.2.1  pgoyette 	npf_error_t errinfo;
    550  1.47.2.1  pgoyette 	struct stat sb;
    551  1.47.2.1  pgoyette 	size_t blen;
    552  1.47.2.1  pgoyette 	void *blob;
    553      1.41     rmind 	int error;
    554      1.41     rmind 
    555  1.47.2.1  pgoyette 	/*
    556  1.47.2.1  pgoyette 	 * The file may change while reading - we are not handling this,
    557  1.47.2.1  pgoyette 	 * leaving this responsibility for the caller.
    558  1.47.2.1  pgoyette 	 */
    559  1.47.2.1  pgoyette 	if (stat(NPF_DB_PATH, &sb) == -1) {
    560  1.47.2.1  pgoyette 		err(EXIT_FAILURE, "stat");
    561  1.47.2.1  pgoyette 	}
    562  1.47.2.1  pgoyette 	if ((blen = sb.st_size) == 0) {
    563  1.47.2.1  pgoyette 		err(EXIT_FAILURE, "saved configuration file is empty");
    564  1.47.2.1  pgoyette 	}
    565  1.47.2.1  pgoyette 	if ((blob = mmap(NULL, blen, PROT_READ,
    566  1.47.2.1  pgoyette 	    MAP_FILE | MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
    567  1.47.2.1  pgoyette 		err(EXIT_FAILURE, "mmap");
    568  1.47.2.1  pgoyette 	}
    569  1.47.2.1  pgoyette 	ncf = npf_config_import(blob, blen);
    570  1.47.2.1  pgoyette 	munmap(blob, blen);
    571      1.41     rmind 	if (ncf == NULL) {
    572      1.41     rmind 		return errno;
    573      1.41     rmind 	}
    574  1.47.2.1  pgoyette 
    575  1.47.2.1  pgoyette 	/*
    576  1.47.2.1  pgoyette 	 * Configuration imported - submit it now.
    577  1.47.2.1  pgoyette 	 **/
    578  1.47.2.1  pgoyette 	errno = error = npf_config_submit(ncf, fd, &errinfo);
    579  1.47.2.1  pgoyette 	if (error) {
    580  1.47.2.1  pgoyette 		npfctl_print_error(&errinfo);
    581  1.47.2.1  pgoyette 	}
    582      1.41     rmind 	npf_config_destroy(ncf);
    583      1.42     htodd 	return error;
    584      1.41     rmind }
    585      1.41     rmind 
    586  1.47.2.1  pgoyette struct npf_conn_filter {
    587  1.47.2.1  pgoyette 	uint16_t alen;
    588  1.47.2.1  pgoyette 	const char *ifname;
    589  1.47.2.1  pgoyette 	bool nat;
    590  1.47.2.1  pgoyette 	bool wide;
    591  1.47.2.1  pgoyette 	bool name;
    592  1.47.2.1  pgoyette 	int width;
    593  1.47.2.1  pgoyette 	FILE *fp;
    594  1.47.2.1  pgoyette };
    595  1.47.2.1  pgoyette 
    596      1.41     rmind static int
    597  1.47.2.1  pgoyette npfctl_conn_print(unsigned alen, const npf_addr_t *a, const in_port_t *p,
    598  1.47.2.1  pgoyette     const char *ifname, void *v)
    599      1.41     rmind {
    600  1.47.2.1  pgoyette 	struct npf_conn_filter *fil = v;
    601  1.47.2.1  pgoyette 	FILE *fp = fil->fp;
    602  1.47.2.1  pgoyette 	char *src, *dst;
    603  1.47.2.1  pgoyette 
    604  1.47.2.1  pgoyette 	if (fil->ifname && strcmp(ifname, fil->ifname) != 0)
    605  1.47.2.1  pgoyette 		return 0;
    606  1.47.2.1  pgoyette 	if (fil->alen && alen != fil->alen)
    607  1.47.2.1  pgoyette 		return 0;
    608  1.47.2.1  pgoyette 	if (fil->nat && !p[2])
    609  1.47.2.1  pgoyette 		return 0;
    610  1.47.2.1  pgoyette 
    611  1.47.2.1  pgoyette 	int w = fil->width;
    612  1.47.2.1  pgoyette 	const char *fmt = fil->name ? "%A" :
    613  1.47.2.1  pgoyette 	    (alen == sizeof(struct in_addr) ? "%a" : "[%a]");
    614  1.47.2.1  pgoyette 	src = npfctl_print_addrmask(alen, fmt, &a[0], NPF_NO_NETMASK);
    615  1.47.2.1  pgoyette 	dst = npfctl_print_addrmask(alen, fmt, &a[1], NPF_NO_NETMASK);
    616  1.47.2.1  pgoyette 	if (fil->wide)
    617  1.47.2.1  pgoyette 		fprintf(fp, "%s:%d %s:%d", src, p[0], dst, p[1]);
    618  1.47.2.1  pgoyette 	else
    619  1.47.2.1  pgoyette 		fprintf(fp, "%*.*s:%-5d %*.*s:%-5d", w, w, src, p[0],
    620  1.47.2.1  pgoyette 		    w, w, dst, p[1]);
    621  1.47.2.1  pgoyette 	free(src);
    622  1.47.2.1  pgoyette 	free(dst);
    623  1.47.2.1  pgoyette 	if (!p[2]) {
    624  1.47.2.1  pgoyette 		fputc('\n', fp);
    625  1.47.2.1  pgoyette 		return 1;
    626      1.41     rmind 	}
    627  1.47.2.1  pgoyette 	fprintf(fp, " via %s:%d\n", ifname, ntohs(p[2]));
    628  1.47.2.1  pgoyette 	return 1;
    629  1.47.2.1  pgoyette }
    630  1.47.2.1  pgoyette 
    631  1.47.2.1  pgoyette 
    632  1.47.2.1  pgoyette static int
    633  1.47.2.1  pgoyette npfctl_conn_list(int fd, int argc, char **argv)
    634  1.47.2.1  pgoyette {
    635  1.47.2.1  pgoyette 	struct npf_conn_filter f;
    636  1.47.2.1  pgoyette 	int c;
    637  1.47.2.1  pgoyette 	int header = true;
    638  1.47.2.1  pgoyette 	memset(&f, 0, sizeof(f));
    639  1.47.2.1  pgoyette 
    640  1.47.2.1  pgoyette 	argc--;
    641  1.47.2.1  pgoyette 	argv++;
    642  1.47.2.1  pgoyette 
    643  1.47.2.1  pgoyette 	while ((c = getopt(argc, argv, "46hi:nNw")) != -1) {
    644  1.47.2.1  pgoyette 		switch (c) {
    645  1.47.2.1  pgoyette 		case '4':
    646  1.47.2.1  pgoyette 			f.alen = sizeof(struct in_addr);
    647  1.47.2.1  pgoyette 			break;
    648  1.47.2.1  pgoyette 		case '6':
    649  1.47.2.1  pgoyette 			f.alen = sizeof(struct in6_addr);
    650  1.47.2.1  pgoyette 			break;
    651  1.47.2.1  pgoyette 		case 'h':
    652  1.47.2.1  pgoyette 			header = false;
    653  1.47.2.1  pgoyette 		case 'i':
    654  1.47.2.1  pgoyette 			f.ifname = optarg;
    655  1.47.2.1  pgoyette 			break;
    656  1.47.2.1  pgoyette 		case 'n':
    657  1.47.2.1  pgoyette 			f.nat = true;
    658  1.47.2.1  pgoyette 			break;
    659  1.47.2.1  pgoyette 		case 'N':
    660  1.47.2.1  pgoyette 			f.name = true;
    661  1.47.2.1  pgoyette 			break;
    662  1.47.2.1  pgoyette 		case 'w':
    663  1.47.2.1  pgoyette 			f.wide = true;
    664  1.47.2.1  pgoyette 			break;
    665  1.47.2.1  pgoyette 		default:
    666  1.47.2.1  pgoyette 			fprintf(stderr,
    667  1.47.2.1  pgoyette 			    "Usage: %s list [-46hnNw] [-i <ifname>]\n",
    668  1.47.2.1  pgoyette 			    getprogname());
    669  1.47.2.1  pgoyette 			exit(EXIT_FAILURE);
    670  1.47.2.1  pgoyette 		}
    671      1.43     rmind 	}
    672  1.47.2.1  pgoyette 	f.width = f.alen == sizeof(struct in_addr) ? 25 : 41;
    673  1.47.2.1  pgoyette 	int w = f.width + 6;
    674  1.47.2.1  pgoyette 	f.fp = stdout;
    675  1.47.2.1  pgoyette 	if (header)
    676  1.47.2.1  pgoyette 		fprintf(f.fp, "%*.*s %*.*s\n",
    677  1.47.2.1  pgoyette 		    w, w, "From address:port ", w, w, "To address:port ");
    678  1.47.2.1  pgoyette 
    679  1.47.2.1  pgoyette 	npf_conn_list(fd, npfctl_conn_print, &f);
    680  1.47.2.1  pgoyette 	return 0;
    681      1.41     rmind }
    682      1.41     rmind 
    683  1.47.2.1  pgoyette static int
    684  1.47.2.1  pgoyette npfctl_open_dev(const char *path)
    685       1.1     rmind {
    686  1.47.2.1  pgoyette 	int fd, ver;
    687       1.1     rmind 
    688  1.47.2.1  pgoyette 	fd = open(path, O_RDONLY);
    689       1.1     rmind 	if (fd == -1) {
    690  1.47.2.1  pgoyette 		err(EXIT_FAILURE, "cannot open '%s'", path);
    691       1.1     rmind 	}
    692      1.29     rmind 	if (ioctl(fd, IOC_NPF_VERSION, &ver) == -1) {
    693      1.32  christos 		err(EXIT_FAILURE, "ioctl(IOC_NPF_VERSION)");
    694      1.15     rmind 	}
    695       1.1     rmind 	if (ver != NPF_VERSION) {
    696       1.4     rmind 		errx(EXIT_FAILURE,
    697      1.14     rmind 		    "incompatible NPF interface version (%d, kernel %d)\n"
    698  1.47.2.2  pgoyette 		    "Hint: update %s?", NPF_VERSION, ver,
    699  1.47.2.2  pgoyette 		    NPF_VERSION > ver ? "userland" : "kernel");
    700       1.1     rmind 	}
    701  1.47.2.1  pgoyette 	return fd;
    702  1.47.2.1  pgoyette }
    703      1.29     rmind 
    704  1.47.2.1  pgoyette static void
    705  1.47.2.1  pgoyette npfctl(int action, int argc, char **argv)
    706  1.47.2.1  pgoyette {
    707  1.47.2.1  pgoyette 	int fd, boolval, ret = 0;
    708      1.32  christos 	const char *fun = "";
    709  1.47.2.1  pgoyette 	nl_config_t *ncf;
    710  1.47.2.1  pgoyette 
    711  1.47.2.1  pgoyette 	switch (action) {
    712  1.47.2.1  pgoyette 	case NPFCTL_VALIDATE:
    713  1.47.2.1  pgoyette 	case NPFCTL_DEBUG:
    714  1.47.2.1  pgoyette 		fd = 0;
    715  1.47.2.1  pgoyette 		break;
    716  1.47.2.1  pgoyette 	default:
    717  1.47.2.1  pgoyette 		fd = npfctl_open_dev(NPF_DEV_PATH);
    718  1.47.2.1  pgoyette 	}
    719  1.47.2.1  pgoyette 
    720       1.1     rmind 	switch (action) {
    721       1.1     rmind 	case NPFCTL_START:
    722       1.1     rmind 		boolval = true;
    723       1.1     rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    724      1.32  christos 		fun = "ioctl(IOC_NPF_SWITCH)";
    725       1.1     rmind 		break;
    726       1.1     rmind 	case NPFCTL_STOP:
    727       1.1     rmind 		boolval = false;
    728       1.1     rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    729      1.32  christos 		fun = "ioctl(IOC_NPF_SWITCH)";
    730       1.1     rmind 		break;
    731       1.1     rmind 	case NPFCTL_RELOAD:
    732       1.8     rmind 		npfctl_config_init(false);
    733      1.29     rmind 		npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
    734      1.46  christos 		npfctl_preload_bpfjit();
    735      1.32  christos 		errno = ret = npfctl_config_send(fd, NULL);
    736      1.32  christos 		fun = "npfctl_config_send";
    737       1.1     rmind 		break;
    738      1.11     rmind 	case NPFCTL_SHOWCONF:
    739      1.11     rmind 		ret = npfctl_config_show(fd);
    740      1.32  christos 		fun = "npfctl_config_show";
    741      1.11     rmind 		break;
    742       1.1     rmind 	case NPFCTL_FLUSH:
    743       1.9     rmind 		ret = npf_config_flush(fd);
    744      1.32  christos 		fun = "npf_config_flush";
    745       1.1     rmind 		break;
    746       1.1     rmind 	case NPFCTL_TABLE:
    747      1.21     rmind 		if ((argc -= 2) < 2) {
    748       1.1     rmind 			usage();
    749       1.1     rmind 		}
    750      1.21     rmind 		argv += 2;
    751      1.21     rmind 		npfctl_table(fd, argc, argv);
    752       1.1     rmind 		break;
    753      1.29     rmind 	case NPFCTL_RULE:
    754      1.29     rmind 		if ((argc -= 2) < 2) {
    755      1.29     rmind 			usage();
    756      1.29     rmind 		}
    757      1.29     rmind 		argv += 2;
    758      1.29     rmind 		npfctl_rule(fd, argc, argv);
    759      1.29     rmind 		break;
    760      1.41     rmind 	case NPFCTL_LOAD:
    761      1.44     rmind 		npfctl_preload_bpfjit();
    762      1.41     rmind 		ret = npfctl_load(fd);
    763      1.41     rmind 		fun = "npfctl_config_load";
    764      1.41     rmind 		break;
    765      1.41     rmind 	case NPFCTL_SAVE:
    766  1.47.2.1  pgoyette 		ncf = npf_config_retrieve(fd);
    767  1.47.2.1  pgoyette 		if (ncf) {
    768  1.47.2.1  pgoyette 			npfctl_config_save(ncf, NPF_DB_PATH);
    769  1.47.2.1  pgoyette 			npf_config_destroy(ncf);
    770  1.47.2.1  pgoyette 		} else {
    771  1.47.2.1  pgoyette 			ret = errno;
    772  1.47.2.1  pgoyette 		}
    773      1.41     rmind 		fun = "npfctl_config_save";
    774      1.41     rmind 		break;
    775       1.3     rmind 	case NPFCTL_STATS:
    776       1.3     rmind 		ret = npfctl_print_stats(fd);
    777      1.32  christos 		fun = "npfctl_print_stats";
    778       1.3     rmind 		break;
    779  1.47.2.1  pgoyette 	case NPFCTL_CONN_LIST:
    780  1.47.2.1  pgoyette 		ret = npfctl_conn_list(fd, argc, argv);
    781  1.47.2.1  pgoyette 		fun = "npfctl_conn_list";
    782  1.47.2.1  pgoyette 		break;
    783  1.47.2.1  pgoyette 	case NPFCTL_VALIDATE:
    784  1.47.2.1  pgoyette 		npfctl_config_init(false);
    785  1.47.2.1  pgoyette 		npfctl_parse_file(argc > 2 ? argv[2] : NPF_CONF_PATH);
    786  1.47.2.1  pgoyette 		ret = npfctl_config_show(0);
    787  1.47.2.1  pgoyette 		fun = "npfctl_config_show";
    788  1.47.2.1  pgoyette 		break;
    789  1.47.2.1  pgoyette 	case NPFCTL_DEBUG:
    790  1.47.2.1  pgoyette 		npfctl_config_init(true);
    791  1.47.2.1  pgoyette 		npfctl_parse_file(argc > 2 ? argv[2] : NPF_CONF_PATH);
    792  1.47.2.1  pgoyette 		npfctl_config_send(0, argc > 3 ? argv[3] : "/tmp/npf.plist");
    793  1.47.2.1  pgoyette 		break;
    794       1.1     rmind 	}
    795       1.7    zoltan 	if (ret) {
    796      1.32  christos 		err(EXIT_FAILURE, "%s", fun);
    797       1.1     rmind 	}
    798  1.47.2.1  pgoyette 	if (fd) {
    799  1.47.2.1  pgoyette 		close(fd);
    800  1.47.2.1  pgoyette 	}
    801       1.1     rmind }
    802       1.1     rmind 
    803       1.1     rmind int
    804       1.1     rmind main(int argc, char **argv)
    805       1.1     rmind {
    806       1.1     rmind 	char *cmd;
    807       1.1     rmind 
    808       1.1     rmind 	if (argc < 2) {
    809       1.1     rmind 		usage();
    810       1.1     rmind 	}
    811  1.47.2.1  pgoyette 	npfctl_show_init();
    812       1.1     rmind 	cmd = argv[1];
    813       1.1     rmind 
    814      1.12     rmind 	/* Find and call the subroutine. */
    815       1.8     rmind 	for (int n = 0; operations[n].cmd != NULL; n++) {
    816      1.25     rmind 		const char *opcmd = operations[n].cmd;
    817      1.25     rmind 		if (strncmp(cmd, opcmd, strlen(opcmd)) != 0)
    818       1.1     rmind 			continue;
    819       1.1     rmind 		npfctl(operations[n].action, argc, argv);
    820       1.8     rmind 		return EXIT_SUCCESS;
    821       1.1     rmind 	}
    822       1.3     rmind 	usage();
    823       1.1     rmind }
    824