npfctl.c revision 1.19.2.2       1  1.19.2.2     tls /*	$NetBSD: npfctl.c,v 1.19.2.2 2013/02/25 00:30:46 tls Exp $	*/
      2       1.1   rmind 
      3       1.1   rmind /*-
      4  1.19.2.2     tls  * Copyright (c) 2009-2013 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.19.2.2     tls __RCSID("$NetBSD: npfctl.c,v 1.19.2.2 2013/02/25 00:30:46 tls Exp $");
     34       1.2   rmind 
     35       1.1   rmind #include <sys/ioctl.h>
     36       1.1   rmind #include <sys/stat.h>
     37       1.1   rmind #include <sys/types.h>
     38       1.1   rmind 
     39       1.1   rmind #include <stdio.h>
     40       1.1   rmind #include <stdlib.h>
     41       1.1   rmind #include <string.h>
     42       1.1   rmind #include <err.h>
     43       1.1   rmind #include <fcntl.h>
     44       1.1   rmind #include <unistd.h>
     45      1.15   rmind #include <errno.h>
     46       1.1   rmind 
     47  1.19.2.2     tls #include <openssl/sha.h>
     48  1.19.2.2     tls 
     49       1.1   rmind #include "npfctl.h"
     50       1.1   rmind 
     51  1.19.2.2     tls 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.19.2.2     tls 	NPFCTL_VALIDATE,
     60      1.11   rmind 	NPFCTL_TABLE,
     61  1.19.2.2     tls 	NPFCTL_RULE,
     62      1.11   rmind 	NPFCTL_STATS,
     63      1.11   rmind 	NPFCTL_SESSIONS_SAVE,
     64      1.11   rmind 	NPFCTL_SESSIONS_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.3   rmind 	{	"start",		NPFCTL_START		},
     73       1.3   rmind 	{	"stop",			NPFCTL_STOP		},
     74       1.3   rmind 	{	"reload",		NPFCTL_RELOAD		},
     75      1.11   rmind 	{	"show",			NPFCTL_SHOWCONF,	},
     76       1.3   rmind 	{	"flush",		NPFCTL_FLUSH		},
     77  1.19.2.2     tls 	{	"valid",		NPFCTL_VALIDATE		},
     78       1.1   rmind 	/* Table */
     79       1.3   rmind 	{	"table",		NPFCTL_TABLE		},
     80  1.19.2.2     tls 	/* Rule */
     81  1.19.2.2     tls 	{	"rule",			NPFCTL_RULE		},
     82       1.3   rmind 	/* Stats */
     83       1.3   rmind 	{	"stats",		NPFCTL_STATS		},
     84       1.3   rmind 	/* Sessions */
     85       1.3   rmind 	{	"sess-save",		NPFCTL_SESSIONS_SAVE	},
     86       1.3   rmind 	{	"sess-load",		NPFCTL_SESSIONS_LOAD	},
     87       1.1   rmind 	/* --- */
     88       1.3   rmind 	{	NULL,			0			}
     89       1.1   rmind };
     90       1.1   rmind 
     91  1.19.2.2     tls static bool
     92  1.19.2.2     tls join(char *buf, size_t buflen, int count, char **args)
     93  1.19.2.2     tls {
     94  1.19.2.2     tls 	char *s = buf, *p = NULL;
     95  1.19.2.2     tls 
     96  1.19.2.2     tls 	for (int i = 0; i < count; i++) {
     97  1.19.2.2     tls 		size_t len;
     98  1.19.2.2     tls 
     99  1.19.2.2     tls 		p = stpncpy(s, args[i], buflen);
    100  1.19.2.2     tls 		len = p - s + 1;
    101  1.19.2.2     tls 		if (len >= buflen) {
    102  1.19.2.2     tls 			return false;
    103  1.19.2.2     tls 		}
    104  1.19.2.2     tls 		buflen -= len;
    105  1.19.2.2     tls 		*p = ' ';
    106  1.19.2.2     tls 		s = p + 1;
    107  1.19.2.2     tls 	}
    108  1.19.2.2     tls 	*p = '\0';
    109  1.19.2.2     tls 	return true;
    110  1.19.2.2     tls }
    111  1.19.2.2     tls 
    112       1.6   joerg __dead static void
    113       1.1   rmind usage(void)
    114       1.1   rmind {
    115       1.1   rmind 	const char *progname = getprogname();
    116       1.1   rmind 
    117       1.1   rmind 	fprintf(stderr,
    118      1.13   rmind 	    "usage:\t%s [ start | stop | reload | flush | show | stats ]\n",
    119       1.3   rmind 	    progname);
    120       1.3   rmind 	fprintf(stderr,
    121  1.19.2.2     tls 	    "\t%s rule \"rule-name\" { add | rem } <rule-syntax>\n",
    122  1.19.2.2     tls 	    progname);
    123  1.19.2.2     tls 	fprintf(stderr,
    124  1.19.2.2     tls 	    "\t%s rule \"rule-name\" rem-id <rule-id>\n",
    125  1.19.2.2     tls 	    progname);
    126  1.19.2.2     tls 	fprintf(stderr,
    127  1.19.2.2     tls 	    "\t%s rule \"rule-name\" { list | flush }\n",
    128       1.1   rmind 	    progname);
    129       1.1   rmind 	fprintf(stderr,
    130  1.19.2.1     tls 	    "\t%s table <tid> { add | rem | test } <address/mask>\n",
    131       1.1   rmind 	    progname);
    132       1.1   rmind 	fprintf(stderr,
    133  1.19.2.1     tls 	    "\t%s table <tid> { list | flush }\n",
    134       1.1   rmind 	    progname);
    135  1.19.2.2     tls 	fprintf(stderr,
    136  1.19.2.2     tls 	    "\t%s ( sess-save | sess-load )\n",
    137  1.19.2.2     tls 	    progname);
    138       1.1   rmind 	exit(EXIT_FAILURE);
    139       1.1   rmind }
    140       1.1   rmind 
    141       1.3   rmind static int
    142       1.3   rmind npfctl_print_stats(int fd)
    143       1.3   rmind {
    144      1.17   rmind 	static const struct stats_s {
    145      1.17   rmind 		/* Note: -1 indicates a new section. */
    146      1.17   rmind 		int		index;
    147      1.17   rmind 		const char *	name;
    148      1.17   rmind 	} stats[] = {
    149      1.17   rmind 		{ -1, "Packets passed"					},
    150      1.17   rmind 		{ NPF_STAT_PASS_DEFAULT,	"default pass"		},
    151      1.17   rmind 		{ NPF_STAT_PASS_RULESET,	"ruleset pass"		},
    152      1.17   rmind 		{ NPF_STAT_PASS_SESSION,	"session pass"		},
    153      1.17   rmind 
    154      1.17   rmind 		{ -1, "Packets blocked"					},
    155      1.17   rmind 		{ NPF_STAT_BLOCK_DEFAULT,	"default block"		},
    156      1.17   rmind 		{ NPF_STAT_BLOCK_RULESET,	"ruleset block"		},
    157      1.17   rmind 
    158      1.17   rmind 		{ -1, "Session and NAT entries"				},
    159      1.17   rmind 		{ NPF_STAT_SESSION_CREATE,	"session allocations"	},
    160      1.17   rmind 		{ NPF_STAT_SESSION_DESTROY,	"session destructions"	},
    161      1.17   rmind 		{ NPF_STAT_NAT_CREATE,		"NAT entry allocations"	},
    162      1.17   rmind 		{ NPF_STAT_NAT_DESTROY,		"NAT entry destructions"},
    163      1.17   rmind 
    164  1.19.2.2     tls 		{ -1, "Network buffers"					},
    165  1.19.2.2     tls 		{ NPF_STAT_NBUF_NONCONTIG,	"non-contiguous cases"	},
    166  1.19.2.2     tls 		{ NPF_STAT_NBUF_CONTIG_FAIL,	"contig alloc failures"	},
    167  1.19.2.2     tls 
    168      1.17   rmind 		{ -1, "Invalid packet state cases"			},
    169      1.17   rmind 		{ NPF_STAT_INVALID_STATE,	"cases in total"	},
    170      1.17   rmind 		{ NPF_STAT_INVALID_STATE_TCP1,	"TCP case I"		},
    171      1.17   rmind 		{ NPF_STAT_INVALID_STATE_TCP2,	"TCP case II"		},
    172      1.17   rmind 		{ NPF_STAT_INVALID_STATE_TCP3,	"TCP case III"		},
    173      1.17   rmind 
    174      1.17   rmind 		{ -1, "Packet race cases"				},
    175      1.17   rmind 		{ NPF_STAT_RACE_NAT,		"NAT association race"	},
    176      1.17   rmind 		{ NPF_STAT_RACE_SESSION,	"duplicate session race"},
    177      1.17   rmind 
    178      1.17   rmind 		{ -1, "Fragmentation"					},
    179      1.17   rmind 		{ NPF_STAT_FRAGMENTS,		"fragments"		},
    180      1.17   rmind 		{ NPF_STAT_REASSEMBLY,		"reassembled"		},
    181      1.17   rmind 		{ NPF_STAT_REASSFAIL,		"failed reassembly"	},
    182      1.17   rmind 
    183      1.17   rmind 		{ -1, "Other"						},
    184      1.17   rmind 		{ NPF_STAT_ERROR,		"unexpected errors"	},
    185      1.17   rmind 	};
    186  1.19.2.1     tls 	uint64_t *st = ecalloc(1, NPF_STATS_SIZE);
    187       1.3   rmind 
    188       1.3   rmind 	if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
    189       1.3   rmind 		err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
    190       1.3   rmind 	}
    191       1.3   rmind 
    192      1.17   rmind 	for (unsigned i = 0; i < __arraycount(stats); i++) {
    193      1.17   rmind 		const char *sname = stats[i].name;
    194      1.17   rmind 		int sidx = stats[i].index;
    195      1.17   rmind 
    196      1.17   rmind 		if (sidx == -1) {
    197      1.17   rmind 			printf("%s:\n", sname);
    198      1.17   rmind 		} else {
    199      1.17   rmind 			printf("\t%"PRIu64" %s\n", st[sidx], sname);
    200      1.17   rmind 		}
    201      1.17   rmind 	}
    202       1.4   rmind 
    203       1.3   rmind 	free(st);
    204       1.3   rmind 	return 0;
    205       1.3   rmind }
    206       1.3   rmind 
    207      1.10   rmind void
    208      1.10   rmind npfctl_print_error(const nl_error_t *ne)
    209      1.10   rmind {
    210      1.10   rmind 	static const char *ncode_errors[] = {
    211      1.10   rmind 		[-NPF_ERR_OPCODE]	= "invalid instruction",
    212      1.10   rmind 		[-NPF_ERR_JUMP]		= "invalid jump",
    213      1.10   rmind 		[-NPF_ERR_REG]		= "invalid register",
    214      1.10   rmind 		[-NPF_ERR_INVAL]	= "invalid argument value",
    215      1.10   rmind 		[-NPF_ERR_RANGE]	= "processing out of range"
    216      1.10   rmind 	};
    217      1.10   rmind 	const int nc_err = ne->ne_ncode_error;
    218      1.10   rmind 	const char *srcfile = ne->ne_source_file;
    219      1.10   rmind 
    220      1.10   rmind 	if (srcfile) {
    221      1.10   rmind 		warnx("source %s line %d", srcfile, ne->ne_source_line);
    222      1.10   rmind 	}
    223      1.10   rmind 	if (nc_err) {
    224      1.10   rmind 		warnx("n-code error (%d): %s at offset 0x%x",
    225      1.10   rmind 		    nc_err, ncode_errors[-nc_err], ne->ne_ncode_errat);
    226      1.10   rmind 	}
    227      1.10   rmind 	if (ne->ne_id) {
    228      1.10   rmind 		warnx("object: %d", ne->ne_id);
    229      1.10   rmind 	}
    230      1.10   rmind }
    231      1.10   rmind 
    232  1.19.2.1     tls char *
    233  1.19.2.1     tls npfctl_print_addrmask(int alen, npf_addr_t *addr, npf_netmask_t mask)
    234  1.19.2.1     tls {
    235  1.19.2.1     tls 	struct sockaddr_storage ss;
    236  1.19.2.1     tls 	char *buf = ecalloc(1, 64);
    237  1.19.2.1     tls 	int len;
    238  1.19.2.1     tls 
    239  1.19.2.1     tls 	switch (alen) {
    240  1.19.2.1     tls 	case 4: {
    241  1.19.2.1     tls 		struct sockaddr_in *sin = (void *)&ss;
    242  1.19.2.1     tls 		sin->sin_len = sizeof(*sin);
    243  1.19.2.1     tls 		sin->sin_family = AF_INET;
    244  1.19.2.1     tls 		sin->sin_port = 0;
    245  1.19.2.1     tls 		memcpy(&sin->sin_addr, addr, sizeof(sin->sin_addr));
    246  1.19.2.1     tls 		break;
    247  1.19.2.1     tls 	}
    248  1.19.2.1     tls 	case 16: {
    249  1.19.2.1     tls 		struct sockaddr_in6 *sin6 = (void *)&ss;
    250  1.19.2.1     tls 		sin6->sin6_len = sizeof(*sin6);
    251  1.19.2.1     tls 		sin6->sin6_family = AF_INET6;
    252  1.19.2.1     tls 		sin6->sin6_port = 0;
    253  1.19.2.2     tls 		sin6->sin6_scope_id = 0;
    254  1.19.2.1     tls 		memcpy(&sin6->sin6_addr, addr, sizeof(sin6->sin6_addr));
    255  1.19.2.1     tls 		break;
    256  1.19.2.1     tls 	}
    257  1.19.2.1     tls 	default:
    258  1.19.2.1     tls 		assert(false);
    259  1.19.2.1     tls 	}
    260  1.19.2.1     tls 	len = sockaddr_snprintf(buf, 64, "%a", (struct sockaddr *)&ss);
    261  1.19.2.1     tls 	if (mask) {
    262  1.19.2.1     tls 		snprintf(&buf[len], 64 - len, "/%u", mask);
    263  1.19.2.1     tls 	}
    264  1.19.2.1     tls 	return buf;
    265  1.19.2.1     tls }
    266  1.19.2.1     tls 
    267      1.16   joerg __dead static void
    268  1.19.2.1     tls npfctl_table(int fd, int argc, char **argv)
    269      1.15   rmind {
    270      1.15   rmind 	static const struct tblops_s {
    271      1.15   rmind 		const char *	cmd;
    272      1.15   rmind 		int		action;
    273      1.15   rmind 	} tblops[] = {
    274  1.19.2.2     tls 		{ "add",	NPF_CMD_TABLE_ADD		},
    275  1.19.2.2     tls 		{ "rem",	NPF_CMD_TABLE_REMOVE		},
    276  1.19.2.2     tls 		{ "del",	NPF_CMD_TABLE_REMOVE		},
    277  1.19.2.2     tls 		{ "test",	NPF_CMD_TABLE_LOOKUP		},
    278  1.19.2.2     tls 		{ "list",	NPF_CMD_TABLE_LIST		},
    279  1.19.2.1     tls 		{ NULL,		0				}
    280      1.15   rmind 	};
    281      1.15   rmind 	npf_ioctl_table_t nct;
    282      1.15   rmind 	fam_addr_mask_t fam;
    283  1.19.2.1     tls 	size_t buflen = 512;
    284  1.19.2.1     tls 	char *cmd, *arg = NULL; /* XXX gcc */
    285      1.15   rmind 	int n, alen;
    286      1.15   rmind 
    287  1.19.2.1     tls 	/* Default action is list. */
    288      1.15   rmind 	memset(&nct, 0, sizeof(npf_ioctl_table_t));
    289  1.19.2.1     tls 	nct.nct_tid = atoi(argv[0]);
    290  1.19.2.1     tls 	cmd = argv[1];
    291      1.15   rmind 
    292      1.15   rmind 	for (n = 0; tblops[n].cmd != NULL; n++) {
    293  1.19.2.1     tls 		if (strcmp(cmd, tblops[n].cmd) != 0) {
    294  1.19.2.1     tls 			continue;
    295      1.15   rmind 		}
    296  1.19.2.2     tls 		nct.nct_cmd = tblops[n].action;
    297  1.19.2.1     tls 		break;
    298      1.15   rmind 	}
    299  1.19.2.1     tls 	if (tblops[n].cmd == NULL) {
    300  1.19.2.1     tls 		errx(EXIT_FAILURE, "invalid command '%s'", cmd);
    301  1.19.2.1     tls 	}
    302  1.19.2.2     tls 	if (nct.nct_cmd != NPF_CMD_TABLE_LIST) {
    303  1.19.2.1     tls 		if (argc < 3) {
    304  1.19.2.1     tls 			usage();
    305  1.19.2.1     tls 		}
    306  1.19.2.1     tls 		arg = argv[2];
    307  1.19.2.1     tls 	}
    308  1.19.2.1     tls again:
    309  1.19.2.2     tls 	if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
    310  1.19.2.1     tls 		nct.nct_data.buf.buf = ecalloc(1, buflen);
    311  1.19.2.1     tls 		nct.nct_data.buf.len = buflen;
    312  1.19.2.1     tls 	} else {
    313  1.19.2.1     tls 		if (!npfctl_parse_cidr(arg, &fam, &alen)) {
    314  1.19.2.1     tls 			errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
    315  1.19.2.1     tls 		}
    316  1.19.2.1     tls 		nct.nct_data.ent.alen = alen;
    317  1.19.2.2     tls 		memcpy(&nct.nct_data.ent.addr, &fam.fam_addr, alen);
    318  1.19.2.1     tls 		nct.nct_data.ent.mask = fam.fam_mask;
    319      1.15   rmind 	}
    320      1.15   rmind 
    321      1.15   rmind 	if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
    322      1.15   rmind 		errno = 0;
    323      1.15   rmind 	}
    324      1.15   rmind 	switch (errno) {
    325  1.19.2.1     tls 	case 0:
    326      1.15   rmind 		break;
    327  1.19.2.1     tls 	case EEXIST:
    328  1.19.2.1     tls 		errx(EXIT_FAILURE, "entry already exists or is conflicting");
    329      1.15   rmind 	case ENOENT:
    330  1.19.2.1     tls 		errx(EXIT_FAILURE, "no matching entry was not found");
    331      1.15   rmind 	case EINVAL:
    332  1.19.2.1     tls 		errx(EXIT_FAILURE, "invalid address, mask or table ID");
    333  1.19.2.1     tls 	case ENOMEM:
    334  1.19.2.2     tls 		if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
    335  1.19.2.1     tls 			/* XXX */
    336  1.19.2.1     tls 			free(nct.nct_data.buf.buf);
    337  1.19.2.1     tls 			buflen <<= 1;
    338  1.19.2.1     tls 			goto again;
    339  1.19.2.1     tls 		}
    340  1.19.2.1     tls 		/* FALLTHROUGH */
    341      1.15   rmind 	default:
    342  1.19.2.1     tls 		err(EXIT_FAILURE, "ioctl");
    343  1.19.2.1     tls 	}
    344  1.19.2.1     tls 
    345  1.19.2.2     tls 	if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
    346  1.19.2.1     tls 		npf_ioctl_ent_t *ent = nct.nct_data.buf.buf;
    347  1.19.2.1     tls 		char *buf;
    348  1.19.2.1     tls 
    349  1.19.2.1     tls 		while (nct.nct_data.buf.len--) {
    350  1.19.2.1     tls 			if (!ent->alen)
    351  1.19.2.1     tls 				break;
    352  1.19.2.1     tls 			buf = npfctl_print_addrmask(ent->alen,
    353  1.19.2.1     tls 			    &ent->addr, ent->mask);
    354  1.19.2.1     tls 			puts(buf);
    355  1.19.2.1     tls 			ent++;
    356  1.19.2.1     tls 		}
    357  1.19.2.1     tls 		free(nct.nct_data.buf.buf);
    358  1.19.2.1     tls 	} else {
    359  1.19.2.1     tls 		printf("%s: %s\n", getprogname(),
    360  1.19.2.2     tls 		    nct.nct_cmd == NPF_CMD_TABLE_LOOKUP ?
    361  1.19.2.1     tls 		    "matching entry found" : "success");
    362      1.15   rmind 	}
    363  1.19.2.1     tls 	exit(EXIT_SUCCESS);
    364      1.15   rmind }
    365      1.15   rmind 
    366  1.19.2.2     tls static nl_rule_t *
    367  1.19.2.2     tls npfctl_parse_rule(int argc, char **argv)
    368  1.19.2.2     tls {
    369  1.19.2.2     tls 	char rule_string[1024];
    370  1.19.2.2     tls 	nl_rule_t *rl;
    371  1.19.2.2     tls 
    372  1.19.2.2     tls 	/* Get the rule string and parse it. */
    373  1.19.2.2     tls 	if (!join(rule_string, sizeof(rule_string), argc, argv)) {
    374  1.19.2.2     tls 		errx(EXIT_FAILURE, "command too long");
    375  1.19.2.2     tls 	}
    376  1.19.2.2     tls 	npfctl_parse_string(rule_string);
    377  1.19.2.2     tls 	if ((rl = npfctl_rule_ref()) == NULL) {
    378  1.19.2.2     tls 		errx(EXIT_FAILURE, "could not parse the rule");
    379  1.19.2.2     tls 	}
    380  1.19.2.2     tls 	return rl;
    381  1.19.2.2     tls }
    382  1.19.2.2     tls 
    383  1.19.2.2     tls static void
    384  1.19.2.2     tls npfctl_generate_key(nl_rule_t *rl, void *key)
    385  1.19.2.2     tls {
    386  1.19.2.2     tls 	void *meta;
    387  1.19.2.2     tls 	size_t len;
    388  1.19.2.2     tls 
    389  1.19.2.2     tls 	if ((meta = npf_rule_export(rl, &len)) == NULL) {
    390  1.19.2.2     tls 		errx(EXIT_FAILURE, "error generating rule key");
    391  1.19.2.2     tls 	}
    392  1.19.2.2     tls 	__CTASSERT(NPF_RULE_MAXKEYLEN >= SHA_DIGEST_LENGTH);
    393  1.19.2.2     tls 	memset(key, 0, NPF_RULE_MAXKEYLEN);
    394  1.19.2.2     tls 	SHA1(meta, len, key);
    395  1.19.2.2     tls 	free(meta);
    396  1.19.2.2     tls }
    397  1.19.2.2     tls 
    398  1.19.2.2     tls __dead static void
    399  1.19.2.2     tls npfctl_rule(int fd, int argc, char **argv)
    400  1.19.2.2     tls {
    401  1.19.2.2     tls 	static const struct ruleops_s {
    402  1.19.2.2     tls 		const char *	cmd;
    403  1.19.2.2     tls 		int		action;
    404  1.19.2.2     tls 	} ruleops[] = {
    405  1.19.2.2     tls 		{ "add",	NPF_CMD_RULE_ADD		},
    406  1.19.2.2     tls 		{ "rem",	NPF_CMD_RULE_REMKEY		},
    407  1.19.2.2     tls 		{ "del",	NPF_CMD_RULE_REMKEY		},
    408  1.19.2.2     tls 		{ "rem-id",	NPF_CMD_RULE_REMOVE		},
    409  1.19.2.2     tls 		{ "list",	NPF_CMD_RULE_LIST		},
    410  1.19.2.2     tls 		{ "flush",	NPF_CMD_RULE_FLUSH		},
    411  1.19.2.2     tls 		{ NULL,		0				}
    412  1.19.2.2     tls 	};
    413  1.19.2.2     tls 	uint8_t key[NPF_RULE_MAXKEYLEN];
    414  1.19.2.2     tls 	const char *ruleset_name = argv[0];
    415  1.19.2.2     tls 	const char *cmd = argv[1];
    416  1.19.2.2     tls 	int error, action = 0;
    417  1.19.2.2     tls 	uint64_t rule_id;
    418  1.19.2.2     tls 	nl_rule_t *rl;
    419  1.19.2.2     tls 
    420  1.19.2.2     tls 	for (int n = 0; ruleops[n].cmd != NULL; n++) {
    421  1.19.2.2     tls 		if (strcmp(cmd, ruleops[n].cmd) == 0) {
    422  1.19.2.2     tls 			action = ruleops[n].action;
    423  1.19.2.2     tls 			break;
    424  1.19.2.2     tls 		}
    425  1.19.2.2     tls 	}
    426  1.19.2.2     tls 
    427  1.19.2.2     tls 	bool narg = action == NPF_CMD_RULE_LIST || action == NPF_CMD_RULE_FLUSH;
    428  1.19.2.2     tls 	if (!action || (argc < 3 && !narg)) {
    429  1.19.2.2     tls 		usage();
    430  1.19.2.2     tls 	}
    431  1.19.2.2     tls 	argc -= 2;
    432  1.19.2.2     tls 	argv += 2;
    433  1.19.2.2     tls 
    434  1.19.2.2     tls 	switch (action) {
    435  1.19.2.2     tls 	case NPF_CMD_RULE_ADD:
    436  1.19.2.2     tls 		rl = npfctl_parse_rule(argc, argv);
    437  1.19.2.2     tls 		npfctl_generate_key(rl, key);
    438  1.19.2.2     tls 		npf_rule_setkey(rl, key, sizeof(key));
    439  1.19.2.2     tls 		error = npf_ruleset_add(fd, ruleset_name, rl, &rule_id);
    440  1.19.2.2     tls 		break;
    441  1.19.2.2     tls 	case NPF_CMD_RULE_REMKEY:
    442  1.19.2.2     tls 		rl = npfctl_parse_rule(argc, argv);
    443  1.19.2.2     tls 		npfctl_generate_key(rl, key);
    444  1.19.2.2     tls 		error = npf_ruleset_remkey(fd, ruleset_name, key, sizeof(key));
    445  1.19.2.2     tls 		break;
    446  1.19.2.2     tls 	case NPF_CMD_RULE_REMOVE:
    447  1.19.2.2     tls 		rule_id = strtoull(argv[0], NULL, 16);
    448  1.19.2.2     tls 		error = npf_ruleset_remove(fd, ruleset_name, rule_id);
    449  1.19.2.2     tls 		break;
    450  1.19.2.2     tls 	case NPF_CMD_RULE_LIST:
    451  1.19.2.2     tls 		error = npfctl_ruleset_show(fd, ruleset_name);
    452  1.19.2.2     tls 		break;
    453  1.19.2.2     tls 	case NPF_CMD_RULE_FLUSH:
    454  1.19.2.2     tls 		error = npf_ruleset_flush(fd, ruleset_name);
    455  1.19.2.2     tls 		break;
    456  1.19.2.2     tls 	default:
    457  1.19.2.2     tls 		assert(false);
    458  1.19.2.2     tls 	}
    459  1.19.2.2     tls 
    460  1.19.2.2     tls 	switch (error) {
    461  1.19.2.2     tls 	case 0:
    462  1.19.2.2     tls 		/* Success. */
    463  1.19.2.2     tls 		break;
    464  1.19.2.2     tls 	case ESRCH:
    465  1.19.2.2     tls 		errx(EXIT_FAILURE, "ruleset \"%s\" not found", ruleset_name);
    466  1.19.2.2     tls 	case ENOENT:
    467  1.19.2.2     tls 		errx(EXIT_FAILURE, "rule was not found");
    468  1.19.2.2     tls 	default:
    469  1.19.2.2     tls 		errx(EXIT_FAILURE, "rule operation: %s", strerror(error));
    470  1.19.2.2     tls 	}
    471  1.19.2.2     tls 	if (action == NPF_CMD_RULE_ADD) {
    472  1.19.2.2     tls 		printf("OK %" PRIx64 "\n", rule_id);
    473  1.19.2.2     tls 	}
    474  1.19.2.2     tls 	exit(EXIT_SUCCESS);
    475  1.19.2.2     tls }
    476  1.19.2.2     tls 
    477      1.15   rmind static void
    478       1.1   rmind npfctl(int action, int argc, char **argv)
    479       1.1   rmind {
    480  1.19.2.2     tls 	int fd, ver, boolval, ret = 0;
    481       1.1   rmind 
    482       1.1   rmind 	fd = open(NPF_DEV_PATH, O_RDONLY);
    483       1.1   rmind 	if (fd == -1) {
    484       1.4   rmind 		err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
    485       1.1   rmind 	}
    486  1.19.2.2     tls 	if (ioctl(fd, IOC_NPF_VERSION, &ver) == -1) {
    487      1.15   rmind 		err(EXIT_FAILURE, "ioctl");
    488      1.15   rmind 	}
    489       1.1   rmind 	if (ver != NPF_VERSION) {
    490       1.4   rmind 		errx(EXIT_FAILURE,
    491      1.14   rmind 		    "incompatible NPF interface version (%d, kernel %d)\n"
    492      1.14   rmind 		    "Hint: update userland?", NPF_VERSION, ver);
    493       1.1   rmind 	}
    494  1.19.2.2     tls 
    495       1.1   rmind 	switch (action) {
    496       1.1   rmind 	case NPFCTL_START:
    497       1.1   rmind 		boolval = true;
    498       1.1   rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    499       1.1   rmind 		break;
    500       1.1   rmind 	case NPFCTL_STOP:
    501       1.1   rmind 		boolval = false;
    502       1.1   rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    503       1.1   rmind 		break;
    504       1.1   rmind 	case NPFCTL_RELOAD:
    505       1.8   rmind 		npfctl_config_init(false);
    506  1.19.2.2     tls 		npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
    507      1.18   rmind 		ret = npfctl_config_send(fd, NULL);
    508      1.15   rmind 		if (ret) {
    509      1.15   rmind 			errx(EXIT_FAILURE, "ioctl: %s", strerror(ret));
    510      1.15   rmind 		}
    511       1.1   rmind 		break;
    512      1.11   rmind 	case NPFCTL_SHOWCONF:
    513      1.11   rmind 		ret = npfctl_config_show(fd);
    514      1.11   rmind 		break;
    515       1.1   rmind 	case NPFCTL_FLUSH:
    516       1.9   rmind 		ret = npf_config_flush(fd);
    517       1.1   rmind 		break;
    518  1.19.2.2     tls 	case NPFCTL_VALIDATE:
    519  1.19.2.2     tls 		npfctl_config_init(false);
    520  1.19.2.2     tls 		npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
    521  1.19.2.2     tls 		ret = npfctl_config_show(0);
    522  1.19.2.2     tls 		break;
    523       1.1   rmind 	case NPFCTL_TABLE:
    524  1.19.2.1     tls 		if ((argc -= 2) < 2) {
    525       1.1   rmind 			usage();
    526       1.1   rmind 		}
    527  1.19.2.1     tls 		argv += 2;
    528  1.19.2.1     tls 		npfctl_table(fd, argc, argv);
    529       1.1   rmind 		break;
    530  1.19.2.2     tls 	case NPFCTL_RULE:
    531  1.19.2.2     tls 		if ((argc -= 2) < 2) {
    532  1.19.2.2     tls 			usage();
    533  1.19.2.2     tls 		}
    534  1.19.2.2     tls 		argv += 2;
    535  1.19.2.2     tls 		npfctl_rule(fd, argc, argv);
    536  1.19.2.2     tls 		break;
    537       1.3   rmind 	case NPFCTL_STATS:
    538       1.3   rmind 		ret = npfctl_print_stats(fd);
    539       1.3   rmind 		break;
    540       1.3   rmind 	case NPFCTL_SESSIONS_SAVE:
    541      1.15   rmind 		if (npf_sessions_recv(fd, NPF_SESSDB_PATH) != 0) {
    542       1.5   rmind 			errx(EXIT_FAILURE, "could not save sessions to '%s'",
    543       1.5   rmind 			    NPF_SESSDB_PATH);
    544       1.5   rmind 		}
    545       1.3   rmind 		break;
    546       1.3   rmind 	case NPFCTL_SESSIONS_LOAD:
    547      1.15   rmind 		if (npf_sessions_send(fd, NPF_SESSDB_PATH) != 0) {
    548       1.5   rmind 			errx(EXIT_FAILURE, "no sessions loaded from '%s'",
    549       1.5   rmind 			    NPF_SESSDB_PATH);
    550       1.5   rmind 		}
    551       1.3   rmind 		break;
    552       1.1   rmind 	}
    553       1.7  zoltan 	if (ret) {
    554       1.1   rmind 		err(EXIT_FAILURE, "ioctl");
    555       1.1   rmind 	}
    556       1.1   rmind 	close(fd);
    557       1.1   rmind }
    558       1.1   rmind 
    559       1.1   rmind int
    560       1.1   rmind main(int argc, char **argv)
    561       1.1   rmind {
    562       1.1   rmind 	char *cmd;
    563       1.1   rmind 
    564       1.1   rmind 	if (argc < 2) {
    565       1.1   rmind 		usage();
    566       1.1   rmind 	}
    567       1.1   rmind 	cmd = argv[1];
    568       1.1   rmind 
    569       1.8   rmind 	if (strcmp(cmd, "debug") == 0) {
    570      1.14   rmind 		const char *cfg = argc > 2 ? argv[2] : "/etc/npf.conf";
    571      1.18   rmind 		const char *out = argc > 3 ? argv[3] : "/tmp/npf.plist";
    572      1.18   rmind 
    573       1.8   rmind 		npfctl_config_init(true);
    574  1.19.2.2     tls 		npfctl_parse_file(cfg);
    575      1.18   rmind 		npfctl_config_send(0, out);
    576       1.8   rmind 		return EXIT_SUCCESS;
    577       1.8   rmind 	}
    578       1.4   rmind 
    579      1.12   rmind 	/* Find and call the subroutine. */
    580       1.8   rmind 	for (int n = 0; operations[n].cmd != NULL; n++) {
    581  1.19.2.2     tls 		const char *opcmd = operations[n].cmd;
    582  1.19.2.2     tls 		if (strncmp(cmd, opcmd, strlen(opcmd)) != 0)
    583       1.1   rmind 			continue;
    584       1.1   rmind 		npfctl(operations[n].action, argc, argv);
    585       1.8   rmind 		return EXIT_SUCCESS;
    586       1.1   rmind 	}
    587       1.3   rmind 	usage();
    588       1.1   rmind }
    589