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