Home | History | Annotate | Line # | Download | only in npfctl
npfctl.c revision 1.3
      1  1.3  rmind /*	$NetBSD: npfctl.c,v 1.3 2010/12/18 01:07:26 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2009-2010 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.3  rmind __RCSID("$NetBSD: npfctl.c,v 1.3 2010/12/18 01:07:26 rmind 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.1  rmind 
     46  1.1  rmind #include "npfctl.h"
     47  1.1  rmind 
     48  1.1  rmind #define	NPFCTL_START		1
     49  1.1  rmind #define	NPFCTL_STOP		2
     50  1.1  rmind #define	NPFCTL_RELOAD		3
     51  1.1  rmind #define	NPFCTL_FLUSH		4
     52  1.1  rmind #define	NPFCTL_TABLE		5
     53  1.3  rmind #define	NPFCTL_STATS		6
     54  1.3  rmind #define	NPFCTL_SESSIONS_SAVE	7
     55  1.3  rmind #define	NPFCTL_SESSIONS_LOAD	8
     56  1.1  rmind 
     57  1.1  rmind static struct operations_s {
     58  1.1  rmind 	const char *		cmd;
     59  1.1  rmind 	int			action;
     60  1.1  rmind } operations[] = {
     61  1.1  rmind 	/* Start, stop, reload */
     62  1.3  rmind 	{	"start",		NPFCTL_START		},
     63  1.3  rmind 	{	"stop",			NPFCTL_STOP		},
     64  1.3  rmind 	{	"reload",		NPFCTL_RELOAD		},
     65  1.3  rmind 	{	"flush",		NPFCTL_FLUSH		},
     66  1.1  rmind 	/* Table */
     67  1.3  rmind 	{	"table",		NPFCTL_TABLE		},
     68  1.3  rmind 	/* Stats */
     69  1.3  rmind 	{	"stats",		NPFCTL_STATS		},
     70  1.3  rmind 	/* Sessions */
     71  1.3  rmind 	{	"sess-save",		NPFCTL_SESSIONS_SAVE	},
     72  1.3  rmind 	{	"sess-load",		NPFCTL_SESSIONS_LOAD	},
     73  1.1  rmind 	/* --- */
     74  1.3  rmind 	{	NULL,			0			}
     75  1.1  rmind };
     76  1.1  rmind 
     77  1.1  rmind void *
     78  1.1  rmind zalloc(size_t sz)
     79  1.1  rmind {
     80  1.1  rmind 	void *p;
     81  1.1  rmind 
     82  1.1  rmind 	p = malloc(sz);
     83  1.1  rmind 	if (p == NULL) {
     84  1.1  rmind 		perror("zalloc");
     85  1.1  rmind 		exit(EXIT_FAILURE);
     86  1.1  rmind 	}
     87  1.1  rmind 	memset(p, 0, sz);
     88  1.1  rmind 	return p;
     89  1.1  rmind }
     90  1.1  rmind 
     91  1.1  rmind char *
     92  1.1  rmind xstrdup(const char *s)
     93  1.1  rmind {
     94  1.1  rmind 	char *p;
     95  1.1  rmind 
     96  1.1  rmind 	p = strdup(s);
     97  1.1  rmind 	if (p == NULL) {
     98  1.1  rmind 		perror("xstrdup");
     99  1.1  rmind 		exit(EXIT_FAILURE);
    100  1.1  rmind 	}
    101  1.1  rmind 	return p;
    102  1.1  rmind }
    103  1.1  rmind 
    104  1.1  rmind static void
    105  1.1  rmind usage(void)
    106  1.1  rmind {
    107  1.1  rmind 	const char *progname = getprogname();
    108  1.1  rmind 
    109  1.1  rmind 	fprintf(stderr,
    110  1.3  rmind 	    "usage:\t%s [ start | stop | reload | flush | stats ]\n",
    111  1.3  rmind 	    progname);
    112  1.3  rmind 	fprintf(stderr,
    113  1.3  rmind 	    "usage:\t%s [ sess-save | sess-load ]\n",
    114  1.1  rmind 	    progname);
    115  1.1  rmind 	fprintf(stderr,
    116  1.1  rmind 	    "\t%s table <tid> [ flush ]\n",
    117  1.1  rmind 	    progname);
    118  1.1  rmind 	fprintf(stderr,
    119  1.1  rmind 	    "\t%s table <tid> { add | rem } <address/mask>\n",
    120  1.1  rmind 	    progname);
    121  1.1  rmind 
    122  1.1  rmind 	exit(EXIT_FAILURE);
    123  1.1  rmind }
    124  1.1  rmind 
    125  1.1  rmind static void
    126  1.1  rmind npfctl_parsecfg(const char *cfg)
    127  1.1  rmind {
    128  1.1  rmind 	char *buf, *p;
    129  1.1  rmind 	FILE *fp;
    130  1.1  rmind 	size_t n;
    131  1.1  rmind 	int l;
    132  1.1  rmind 
    133  1.1  rmind 	fp = fopen(cfg, "r");
    134  1.1  rmind 	if (fp == NULL) {
    135  1.1  rmind 		err(EXIT_FAILURE, "fopen");
    136  1.1  rmind 	}
    137  1.1  rmind 	l = 0;
    138  1.1  rmind 	buf = NULL;
    139  1.1  rmind 	while (getline(&buf, &n, fp) != -1) {
    140  1.1  rmind 		l++;
    141  1.1  rmind 		p = strpbrk(buf, "#\n");
    142  1.1  rmind 		if (p != NULL) {
    143  1.1  rmind 			*p = '\0';
    144  1.1  rmind 		}
    145  1.1  rmind 		if (npf_parseline(buf)) {
    146  1.1  rmind 			fprintf(stderr, "invalid syntax at line %d\n", l);
    147  1.1  rmind 			exit(EXIT_FAILURE);
    148  1.1  rmind 		}
    149  1.1  rmind 	}
    150  1.1  rmind 	if (buf != NULL) {
    151  1.1  rmind 		free(buf);
    152  1.1  rmind 	}
    153  1.1  rmind }
    154  1.1  rmind 
    155  1.3  rmind static int
    156  1.3  rmind npfctl_print_stats(int fd)
    157  1.3  rmind {
    158  1.3  rmind 	uint64_t *st = malloc(NPF_STATS_SIZE);
    159  1.3  rmind 
    160  1.3  rmind 	if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
    161  1.3  rmind 		err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
    162  1.3  rmind 	}
    163  1.3  rmind 
    164  1.3  rmind 	printf("Packets passed:\n\t%"PRIu64" default pass\n\t"
    165  1.3  rmind 	    "%"PRIu64 " ruleset pass\n\t%"PRIu64" session pass\n\n",
    166  1.3  rmind 	    st[NPF_STAT_PASS_DEFAULT], st[NPF_STAT_PASS_RULESET],
    167  1.3  rmind 	    st[NPF_STAT_PASS_SESSION]);
    168  1.3  rmind 
    169  1.3  rmind 	printf("Packets blocked:\n\t%"PRIu64" default block\n\t"
    170  1.3  rmind 	    "%"PRIu64 " ruleset block\n\n", st[NPF_STAT_BLOCK_DEFAULT],
    171  1.3  rmind 	    st[NPF_STAT_BLOCK_RULESET]);
    172  1.3  rmind 
    173  1.3  rmind 	printf("Session and NAT entries:\n\t%"PRIu64" session allocations\n\t"
    174  1.3  rmind 	    "%"PRIu64" session destructions\n\t%"PRIu64" NAT entry allocations\n\t"
    175  1.3  rmind 	    "%"PRIu64" NAT entry destructions\n\n", st[NPF_STAT_SESSION_CREATE],
    176  1.3  rmind 	    st[NPF_STAT_SESSION_DESTROY], st[NPF_STAT_NAT_CREATE],
    177  1.3  rmind 	    st[NPF_STAT_NAT_DESTROY]);
    178  1.3  rmind 
    179  1.3  rmind 	printf("Invalid packet state cases:\n\t%"PRIu64" cases in total\n\t"
    180  1.3  rmind 	    "%"PRIu64" TCP case I\n\t%"PRIu64" TCP case II\n\t%"PRIu64
    181  1.3  rmind 	    " TCP case III\n\n", st[NPF_STAT_INVALID_STATE],
    182  1.3  rmind 	    st[NPF_STAT_INVALID_STATE_TCP1], st[NPF_STAT_INVALID_STATE_TCP2],
    183  1.3  rmind 	    st[NPF_STAT_INVALID_STATE_TCP3]);
    184  1.3  rmind 
    185  1.3  rmind 	printf("Packet race cases:\n\t%"PRIu64" NAT association race\n\t"
    186  1.3  rmind 	    "%"PRIu64" duplicate session race\n", st[NPF_STAT_RACE_NAT],
    187  1.3  rmind 	    st[NPF_STAT_RACE_SESSION]);
    188  1.3  rmind 
    189  1.3  rmind 	free(st);
    190  1.3  rmind 	return 0;
    191  1.3  rmind }
    192  1.3  rmind 
    193  1.1  rmind static void
    194  1.1  rmind npfctl(int action, int argc, char **argv)
    195  1.1  rmind {
    196  1.1  rmind 	int fd, ret, ver, boolval;
    197  1.1  rmind 	npf_ioctl_table_t tbl;
    198  1.1  rmind 	char *arg;
    199  1.1  rmind 
    200  1.3  rmind #ifndef DEBUG
    201  1.1  rmind 	fd = open(NPF_DEV_PATH, O_RDONLY);
    202  1.1  rmind 	if (fd == -1) {
    203  1.1  rmind 		err(EXIT_FAILURE, "cannot open " NPF_DEV_PATH);
    204  1.1  rmind 	}
    205  1.1  rmind 	ret = ioctl(fd, IOC_NPF_VERSION, &ver);
    206  1.1  rmind 	if (ver != NPF_VERSION) {
    207  1.1  rmind 		errx(EXIT_FAILURE, "incompatible npf interface version "
    208  1.1  rmind 		    "(%d, kernel %d)", NPF_VERSION, ver);
    209  1.1  rmind 	}
    210  1.3  rmind #endif
    211  1.1  rmind 	switch (action) {
    212  1.1  rmind 	case NPFCTL_START:
    213  1.1  rmind 		boolval = true;
    214  1.1  rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    215  1.1  rmind 		break;
    216  1.1  rmind 	case NPFCTL_STOP:
    217  1.1  rmind 		boolval = false;
    218  1.1  rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    219  1.1  rmind 		break;
    220  1.1  rmind 	case NPFCTL_RELOAD:
    221  1.1  rmind 		npfctl_init_data();
    222  1.3  rmind #ifdef DEBUG
    223  1.3  rmind 		npfctl_parsecfg("npf.conf");
    224  1.3  rmind 		return npfctl_ioctl_send(0);
    225  1.3  rmind #endif
    226  1.1  rmind 		npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
    227  1.1  rmind 		ret = npfctl_ioctl_send(fd);
    228  1.1  rmind 		break;
    229  1.1  rmind 	case NPFCTL_FLUSH:
    230  1.1  rmind 		/* Pass empty configuration to flush. */
    231  1.1  rmind 		npfctl_init_data();
    232  1.1  rmind 		ret = npfctl_ioctl_send(fd);
    233  1.1  rmind 		break;
    234  1.1  rmind 	case NPFCTL_TABLE:
    235  1.1  rmind 		if (argc < 5) {
    236  1.1  rmind 			usage();
    237  1.1  rmind 		}
    238  1.1  rmind 		tbl.nct_tid = atoi(argv[2]);
    239  1.1  rmind 		if (strcmp(argv[3], "add") == 0) {
    240  1.1  rmind 			tbl.nct_action = NPF_IOCTL_TBLENT_ADD;
    241  1.1  rmind 			arg = argv[4];
    242  1.1  rmind 		} else if (strcmp(argv[3], "rem") == 0) {
    243  1.1  rmind 			tbl.nct_action = NPF_IOCTL_TBLENT_REM;
    244  1.1  rmind 			arg = argv[4];
    245  1.1  rmind 		} else {
    246  1.1  rmind 			tbl.nct_action = 0;
    247  1.1  rmind 			arg = argv[3];
    248  1.1  rmind 		}
    249  1.1  rmind 		if (!npfctl_parse_v4mask(arg,
    250  1.1  rmind 		    &tbl.nct_addr, &tbl.nct_mask)) {
    251  1.1  rmind 			errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
    252  1.1  rmind 		}
    253  1.1  rmind 		ret = ioctl(fd, IOC_NPF_TABLE, &tbl);
    254  1.1  rmind 		break;
    255  1.3  rmind 	case NPFCTL_STATS:
    256  1.3  rmind 		ret = npfctl_print_stats(fd);
    257  1.3  rmind 		break;
    258  1.3  rmind 	case NPFCTL_SESSIONS_SAVE:
    259  1.3  rmind 		ret = npfctl_ioctl_recvse(fd);
    260  1.3  rmind 		break;
    261  1.3  rmind 	case NPFCTL_SESSIONS_LOAD:
    262  1.3  rmind 		ret = npfctl_ioctl_sendse(fd);
    263  1.3  rmind 		break;
    264  1.1  rmind 	}
    265  1.1  rmind 	if (ret == -1) {
    266  1.1  rmind 		err(EXIT_FAILURE, "ioctl");
    267  1.1  rmind 	}
    268  1.1  rmind 	close(fd);
    269  1.1  rmind }
    270  1.1  rmind 
    271  1.1  rmind int
    272  1.1  rmind main(int argc, char **argv)
    273  1.1  rmind {
    274  1.1  rmind 	char *cmd;
    275  1.1  rmind 	int n;
    276  1.1  rmind 
    277  1.1  rmind 	if (argc < 2) {
    278  1.1  rmind 		usage();
    279  1.1  rmind 	}
    280  1.1  rmind 	cmd = argv[1];
    281  1.1  rmind 
    282  1.1  rmind 	/* Find and call the subroutine */
    283  1.1  rmind 	for (n = 0; operations[n].cmd != NULL; n++) {
    284  1.1  rmind 		if (strcmp(cmd, operations[n].cmd) != 0)
    285  1.1  rmind 			continue;
    286  1.1  rmind 		npfctl(operations[n].action, argc, argv);
    287  1.3  rmind 		return 0;
    288  1.1  rmind 	}
    289  1.3  rmind 	usage();
    290  1.1  rmind 	return 0;
    291  1.1  rmind }
    292