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