Home | History | Annotate | Line # | Download | only in npfctl
npfctl.c revision 1.6.2.1
      1  1.6.2.1   yamt /*	$NetBSD: npfctl.c,v 1.6.2.1 2011/11/10 14:31:55 yamt Exp $	*/
      2      1.1  rmind 
      3      1.1  rmind /*-
      4      1.4  rmind  * Copyright (c) 2009-2011 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.6.2.1   yamt __RCSID("$NetBSD: npfctl.c,v 1.6.2.1 2011/11/10 14:31:55 yamt 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.4  rmind 		err(EXIT_FAILURE, "zalloc");
     85      1.1  rmind 	}
     86      1.1  rmind 	memset(p, 0, sz);
     87      1.1  rmind 	return p;
     88      1.1  rmind }
     89      1.1  rmind 
     90      1.1  rmind char *
     91      1.1  rmind xstrdup(const char *s)
     92      1.1  rmind {
     93      1.1  rmind 	char *p;
     94      1.1  rmind 
     95      1.1  rmind 	p = strdup(s);
     96      1.1  rmind 	if (p == NULL) {
     97      1.4  rmind 		err(EXIT_FAILURE, "xstrdup");
     98      1.1  rmind 	}
     99      1.1  rmind 	return p;
    100      1.1  rmind }
    101      1.1  rmind 
    102      1.6  joerg __dead static void
    103      1.1  rmind usage(void)
    104      1.1  rmind {
    105      1.1  rmind 	const char *progname = getprogname();
    106      1.1  rmind 
    107      1.1  rmind 	fprintf(stderr,
    108      1.3  rmind 	    "usage:\t%s [ start | stop | reload | flush | stats ]\n",
    109      1.3  rmind 	    progname);
    110      1.3  rmind 	fprintf(stderr,
    111      1.3  rmind 	    "usage:\t%s [ sess-save | sess-load ]\n",
    112      1.1  rmind 	    progname);
    113      1.1  rmind 	fprintf(stderr,
    114      1.1  rmind 	    "\t%s table <tid> [ flush ]\n",
    115      1.1  rmind 	    progname);
    116      1.1  rmind 	fprintf(stderr,
    117      1.1  rmind 	    "\t%s table <tid> { add | rem } <address/mask>\n",
    118      1.1  rmind 	    progname);
    119      1.1  rmind 
    120      1.1  rmind 	exit(EXIT_FAILURE);
    121      1.1  rmind }
    122      1.1  rmind 
    123      1.1  rmind static void
    124      1.1  rmind npfctl_parsecfg(const char *cfg)
    125      1.1  rmind {
    126      1.1  rmind 	char *buf, *p;
    127      1.1  rmind 	FILE *fp;
    128      1.1  rmind 	size_t n;
    129      1.1  rmind 	int l;
    130      1.1  rmind 
    131      1.1  rmind 	fp = fopen(cfg, "r");
    132      1.1  rmind 	if (fp == NULL) {
    133      1.4  rmind 		err(EXIT_FAILURE, "open '%s'", cfg);
    134      1.1  rmind 	}
    135      1.1  rmind 	l = 0;
    136      1.1  rmind 	buf = NULL;
    137      1.1  rmind 	while (getline(&buf, &n, fp) != -1) {
    138      1.1  rmind 		l++;
    139      1.1  rmind 		p = strpbrk(buf, "#\n");
    140      1.1  rmind 		if (p != NULL) {
    141      1.1  rmind 			*p = '\0';
    142      1.1  rmind 		}
    143      1.1  rmind 		if (npf_parseline(buf)) {
    144      1.1  rmind 			fprintf(stderr, "invalid syntax at line %d\n", l);
    145      1.1  rmind 			exit(EXIT_FAILURE);
    146      1.1  rmind 		}
    147      1.1  rmind 	}
    148      1.1  rmind 	if (buf != NULL) {
    149      1.1  rmind 		free(buf);
    150      1.1  rmind 	}
    151      1.1  rmind }
    152      1.1  rmind 
    153      1.3  rmind static int
    154      1.3  rmind npfctl_print_stats(int fd)
    155      1.3  rmind {
    156      1.3  rmind 	uint64_t *st = malloc(NPF_STATS_SIZE);
    157      1.3  rmind 
    158      1.3  rmind 	if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
    159      1.3  rmind 		err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
    160      1.3  rmind 	}
    161      1.3  rmind 
    162      1.3  rmind 	printf("Packets passed:\n\t%"PRIu64" default pass\n\t"
    163      1.3  rmind 	    "%"PRIu64 " ruleset pass\n\t%"PRIu64" session pass\n\n",
    164      1.3  rmind 	    st[NPF_STAT_PASS_DEFAULT], st[NPF_STAT_PASS_RULESET],
    165      1.3  rmind 	    st[NPF_STAT_PASS_SESSION]);
    166      1.3  rmind 
    167      1.3  rmind 	printf("Packets blocked:\n\t%"PRIu64" default block\n\t"
    168      1.3  rmind 	    "%"PRIu64 " ruleset block\n\n", st[NPF_STAT_BLOCK_DEFAULT],
    169      1.3  rmind 	    st[NPF_STAT_BLOCK_RULESET]);
    170      1.3  rmind 
    171      1.3  rmind 	printf("Session and NAT entries:\n\t%"PRIu64" session allocations\n\t"
    172      1.3  rmind 	    "%"PRIu64" session destructions\n\t%"PRIu64" NAT entry allocations\n\t"
    173      1.3  rmind 	    "%"PRIu64" NAT entry destructions\n\n", st[NPF_STAT_SESSION_CREATE],
    174      1.3  rmind 	    st[NPF_STAT_SESSION_DESTROY], st[NPF_STAT_NAT_CREATE],
    175      1.3  rmind 	    st[NPF_STAT_NAT_DESTROY]);
    176      1.3  rmind 
    177      1.3  rmind 	printf("Invalid packet state cases:\n\t%"PRIu64" cases in total\n\t"
    178      1.3  rmind 	    "%"PRIu64" TCP case I\n\t%"PRIu64" TCP case II\n\t%"PRIu64
    179      1.3  rmind 	    " TCP case III\n\n", st[NPF_STAT_INVALID_STATE],
    180      1.3  rmind 	    st[NPF_STAT_INVALID_STATE_TCP1], st[NPF_STAT_INVALID_STATE_TCP2],
    181      1.3  rmind 	    st[NPF_STAT_INVALID_STATE_TCP3]);
    182      1.3  rmind 
    183      1.3  rmind 	printf("Packet race cases:\n\t%"PRIu64" NAT association race\n\t"
    184      1.4  rmind 	    "%"PRIu64" duplicate session race\n\n", st[NPF_STAT_RACE_NAT],
    185      1.3  rmind 	    st[NPF_STAT_RACE_SESSION]);
    186      1.3  rmind 
    187      1.4  rmind 	printf("Rule processing procedure cases:\n"
    188      1.4  rmind 	    "\t%"PRIu64" packets logged\n\t%"PRIu64" packets normalized\n\n",
    189      1.4  rmind 	    st[NPF_STAT_RPROC_LOG], st[NPF_STAT_RPROC_NORM]);
    190      1.4  rmind 
    191      1.4  rmind 	printf("Unexpected error cases:\n\t%"PRIu64"\n", st[NPF_STAT_ERROR]);
    192      1.4  rmind 
    193      1.3  rmind 	free(st);
    194      1.3  rmind 	return 0;
    195      1.3  rmind }
    196      1.3  rmind 
    197      1.1  rmind static void
    198      1.1  rmind npfctl(int action, int argc, char **argv)
    199      1.1  rmind {
    200      1.1  rmind 	int fd, ret, ver, boolval;
    201      1.1  rmind 	npf_ioctl_table_t tbl;
    202      1.1  rmind 	char *arg;
    203      1.1  rmind 
    204      1.1  rmind 	fd = open(NPF_DEV_PATH, O_RDONLY);
    205      1.1  rmind 	if (fd == -1) {
    206      1.4  rmind 		err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
    207      1.1  rmind 	}
    208      1.1  rmind 	ret = ioctl(fd, IOC_NPF_VERSION, &ver);
    209      1.1  rmind 	if (ver != NPF_VERSION) {
    210      1.4  rmind 		errx(EXIT_FAILURE,
    211      1.4  rmind 		    "incompatible NPF interface version (%d, kernel %d)",
    212      1.4  rmind 		    NPF_VERSION, ver);
    213      1.1  rmind 	}
    214      1.1  rmind 	switch (action) {
    215      1.1  rmind 	case NPFCTL_START:
    216      1.1  rmind 		boolval = true;
    217      1.1  rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    218      1.1  rmind 		break;
    219      1.1  rmind 	case NPFCTL_STOP:
    220      1.1  rmind 		boolval = false;
    221      1.1  rmind 		ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
    222      1.1  rmind 		break;
    223      1.1  rmind 	case NPFCTL_RELOAD:
    224      1.1  rmind 		npfctl_init_data();
    225      1.1  rmind 		npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
    226      1.1  rmind 		ret = npfctl_ioctl_send(fd);
    227      1.1  rmind 		break;
    228      1.1  rmind 	case NPFCTL_FLUSH:
    229      1.1  rmind 		/* Pass empty configuration to flush. */
    230      1.1  rmind 		npfctl_init_data();
    231      1.1  rmind 		ret = npfctl_ioctl_send(fd);
    232      1.4  rmind 		if (ret) {
    233      1.4  rmind 			break;
    234      1.4  rmind 		}
    235      1.5  rmind 		ret = npf_sessions_send(fd, NULL);
    236      1.1  rmind 		break;
    237      1.1  rmind 	case NPFCTL_TABLE:
    238      1.1  rmind 		if (argc < 5) {
    239      1.1  rmind 			usage();
    240      1.1  rmind 		}
    241      1.1  rmind 		tbl.nct_tid = atoi(argv[2]);
    242      1.1  rmind 		if (strcmp(argv[3], "add") == 0) {
    243      1.4  rmind 			/* Add table entry. */
    244      1.1  rmind 			tbl.nct_action = NPF_IOCTL_TBLENT_ADD;
    245      1.1  rmind 			arg = argv[4];
    246      1.1  rmind 		} else if (strcmp(argv[3], "rem") == 0) {
    247      1.4  rmind 			/* Remove entry. */
    248      1.1  rmind 			tbl.nct_action = NPF_IOCTL_TBLENT_REM;
    249      1.1  rmind 			arg = argv[4];
    250      1.1  rmind 		} else {
    251      1.4  rmind 			/* Default: lookup. */
    252      1.1  rmind 			tbl.nct_action = 0;
    253      1.1  rmind 			arg = argv[3];
    254      1.1  rmind 		}
    255  1.6.2.1   yamt 		if (!npfctl_parse_cidr(arg,
    256  1.6.2.1   yamt 		    npfctl_get_addrfamily(arg),
    257      1.1  rmind 		    &tbl.nct_addr, &tbl.nct_mask)) {
    258      1.1  rmind 			errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
    259      1.1  rmind 		}
    260      1.1  rmind 		ret = ioctl(fd, IOC_NPF_TABLE, &tbl);
    261      1.1  rmind 		break;
    262      1.3  rmind 	case NPFCTL_STATS:
    263      1.3  rmind 		ret = npfctl_print_stats(fd);
    264      1.3  rmind 		break;
    265      1.3  rmind 	case NPFCTL_SESSIONS_SAVE:
    266      1.5  rmind 		ret = npf_sessions_recv(fd, NPF_SESSDB_PATH);
    267      1.5  rmind 		if (ret) {
    268      1.5  rmind 			errx(EXIT_FAILURE, "could not save sessions to '%s'",
    269      1.5  rmind 			    NPF_SESSDB_PATH);
    270      1.5  rmind 		}
    271      1.3  rmind 		break;
    272      1.3  rmind 	case NPFCTL_SESSIONS_LOAD:
    273      1.5  rmind 		ret = npf_sessions_send(fd, NPF_SESSDB_PATH);
    274      1.5  rmind 		if (ret) {
    275      1.5  rmind 			errx(EXIT_FAILURE, "no sessions loaded from '%s'",
    276      1.5  rmind 			    NPF_SESSDB_PATH);
    277      1.5  rmind 		}
    278      1.3  rmind 		break;
    279      1.1  rmind 	}
    280  1.6.2.1   yamt 	if (ret) {
    281      1.1  rmind 		err(EXIT_FAILURE, "ioctl");
    282      1.1  rmind 	}
    283      1.1  rmind 	close(fd);
    284      1.1  rmind }
    285      1.1  rmind 
    286      1.1  rmind int
    287      1.1  rmind main(int argc, char **argv)
    288      1.1  rmind {
    289      1.1  rmind 	char *cmd;
    290      1.1  rmind 	int n;
    291      1.1  rmind 
    292      1.1  rmind 	if (argc < 2) {
    293      1.1  rmind 		usage();
    294      1.1  rmind 	}
    295      1.1  rmind 	cmd = argv[1];
    296      1.1  rmind 
    297      1.4  rmind #ifdef _NPF_TESTING
    298      1.4  rmind 	/* Special testing case. */
    299      1.4  rmind 	npfctl_init_data();
    300      1.4  rmind 	npfctl_parsecfg("npf.conf");
    301      1.4  rmind 	npfctl_ioctl_send(0);
    302      1.4  rmind 	return 0;
    303      1.4  rmind #endif
    304      1.4  rmind 
    305      1.1  rmind 	/* Find and call the subroutine */
    306      1.1  rmind 	for (n = 0; operations[n].cmd != NULL; n++) {
    307      1.1  rmind 		if (strcmp(cmd, operations[n].cmd) != 0)
    308      1.1  rmind 			continue;
    309      1.1  rmind 		npfctl(operations[n].action, argc, argv);
    310      1.3  rmind 		return 0;
    311      1.1  rmind 	}
    312      1.3  rmind 	usage();
    313      1.1  rmind 	return 0;
    314      1.1  rmind }
    315