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