Home | History | Annotate | Line # | Download | only in npf
npf.c revision 1.9
      1  1.9  rmind /*	$NetBSD: npf.c,v 1.9 2012/03/11 18:27:59 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.1  rmind /*
     33  1.1  rmind  * NPF main: dynamic load/initialisation and unload routines.
     34  1.1  rmind  */
     35  1.1  rmind 
     36  1.1  rmind #include <sys/cdefs.h>
     37  1.9  rmind __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.9 2012/03/11 18:27:59 rmind Exp $");
     38  1.1  rmind 
     39  1.1  rmind #include <sys/param.h>
     40  1.1  rmind #include <sys/types.h>
     41  1.1  rmind 
     42  1.2  rmind #include <sys/atomic.h>
     43  1.1  rmind #include <sys/conf.h>
     44  1.1  rmind #include <sys/kauth.h>
     45  1.2  rmind #include <sys/kmem.h>
     46  1.1  rmind #include <sys/lwp.h>
     47  1.1  rmind #include <sys/module.h>
     48  1.2  rmind #include <sys/percpu.h>
     49  1.2  rmind #include <sys/rwlock.h>
     50  1.1  rmind #include <sys/socketvar.h>
     51  1.1  rmind #include <sys/uio.h>
     52  1.1  rmind 
     53  1.1  rmind #include "npf_impl.h"
     54  1.1  rmind 
     55  1.1  rmind /*
     56  1.1  rmind  * Module and device structures.
     57  1.1  rmind  */
     58  1.6   tron MODULE(MODULE_CLASS_DRIVER, npf, NULL);
     59  1.1  rmind 
     60  1.1  rmind void		npfattach(int);
     61  1.1  rmind 
     62  1.5   yamt static int	npf_fini(void);
     63  1.1  rmind static int	npf_dev_open(dev_t, int, int, lwp_t *);
     64  1.1  rmind static int	npf_dev_close(dev_t, int, int, lwp_t *);
     65  1.1  rmind static int	npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
     66  1.1  rmind static int	npf_dev_poll(dev_t, int, lwp_t *);
     67  1.1  rmind static int	npf_dev_read(dev_t, struct uio *, int);
     68  1.1  rmind 
     69  1.2  rmind typedef struct {
     70  1.2  rmind 	npf_ruleset_t *		n_rules;
     71  1.2  rmind 	npf_tableset_t *	n_tables;
     72  1.2  rmind 	npf_ruleset_t *		n_nat_rules;
     73  1.9  rmind 	prop_dictionary_t	n_dict;
     74  1.8  rmind 	bool			n_default_pass;
     75  1.2  rmind } npf_core_t;
     76  1.2  rmind 
     77  1.2  rmind static void	npf_core_destroy(npf_core_t *);
     78  1.2  rmind static int	npfctl_stats(void *);
     79  1.2  rmind 
     80  1.2  rmind static krwlock_t		npf_lock		__cacheline_aligned;
     81  1.2  rmind static npf_core_t *		npf_core		__cacheline_aligned;
     82  1.2  rmind static percpu_t *		npf_stats_percpu	__read_mostly;
     83  1.2  rmind 
     84  1.1  rmind const struct cdevsw npf_cdevsw = {
     85  1.1  rmind 	npf_dev_open, npf_dev_close, npf_dev_read, nowrite, npf_dev_ioctl,
     86  1.1  rmind 	nostop, notty, npf_dev_poll, nommap, nokqfilter, D_OTHER | D_MPSAFE
     87  1.1  rmind };
     88  1.1  rmind 
     89  1.1  rmind static int
     90  1.1  rmind npf_init(void)
     91  1.1  rmind {
     92  1.1  rmind #ifdef _MODULE
     93  1.1  rmind 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
     94  1.1  rmind #endif
     95  1.2  rmind 	npf_ruleset_t *rset, *nset;
     96  1.2  rmind 	npf_tableset_t *tset;
     97  1.9  rmind 	prop_dictionary_t dict;
     98  1.2  rmind 	int error = 0;
     99  1.2  rmind 
    100  1.2  rmind 	rw_init(&npf_lock);
    101  1.2  rmind 	npf_stats_percpu = percpu_alloc(NPF_STATS_SIZE);
    102  1.2  rmind 	npf_tableset_sysinit();
    103  1.2  rmind 	npf_session_sysinit();
    104  1.1  rmind 	npf_nat_sysinit();
    105  1.1  rmind 	npf_alg_sysinit();
    106  1.2  rmind 	npflogattach(1);
    107  1.2  rmind 
    108  1.2  rmind 	/* Load empty configuration. */
    109  1.9  rmind 	dict = prop_dictionary_create();
    110  1.2  rmind 	rset = npf_ruleset_create();
    111  1.2  rmind 	tset = npf_tableset_create();
    112  1.2  rmind 	nset = npf_ruleset_create();
    113  1.9  rmind 	npf_reload(dict, rset, tset, nset, true);
    114  1.2  rmind 	KASSERT(npf_core != NULL);
    115  1.1  rmind 
    116  1.1  rmind #ifdef _MODULE
    117  1.1  rmind 	/* Attach /dev/npf device. */
    118  1.1  rmind 	error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
    119  1.1  rmind 	if (error) {
    120  1.2  rmind 		/* It will call devsw_detach(), which is safe. */
    121  1.2  rmind 		(void)npf_fini();
    122  1.1  rmind 	}
    123  1.1  rmind #endif
    124  1.1  rmind 	return error;
    125  1.1  rmind }
    126  1.1  rmind 
    127  1.1  rmind static int
    128  1.1  rmind npf_fini(void)
    129  1.1  rmind {
    130  1.1  rmind 
    131  1.9  rmind 	/* At first, detach device and remove pfil hooks. */
    132  1.1  rmind #ifdef _MODULE
    133  1.1  rmind 	devsw_detach(NULL, &npf_cdevsw);
    134  1.1  rmind #endif
    135  1.9  rmind 	npflogdetach();
    136  1.9  rmind 	npf_pfil_unregister();
    137  1.9  rmind 
    138  1.9  rmind 	/* Flush all sessions, destroy configuration (ruleset, etc). */
    139  1.9  rmind 	npf_session_tracking(false);
    140  1.2  rmind 	npf_core_destroy(npf_core);
    141  1.2  rmind 
    142  1.9  rmind 	/* Finally, safe to destroy the subsystems. */
    143  1.9  rmind 	npf_alg_sysfini();
    144  1.1  rmind 	npf_nat_sysfini();
    145  1.1  rmind 	npf_session_sysfini();
    146  1.1  rmind 	npf_tableset_sysfini();
    147  1.2  rmind 	percpu_free(npf_stats_percpu, NPF_STATS_SIZE);
    148  1.2  rmind 	rw_destroy(&npf_lock);
    149  1.1  rmind 
    150  1.1  rmind 	return 0;
    151  1.1  rmind }
    152  1.1  rmind 
    153  1.1  rmind /*
    154  1.1  rmind  * Module interface.
    155  1.1  rmind  */
    156  1.1  rmind static int
    157  1.1  rmind npf_modcmd(modcmd_t cmd, void *arg)
    158  1.1  rmind {
    159  1.1  rmind 
    160  1.1  rmind 	switch (cmd) {
    161  1.1  rmind 	case MODULE_CMD_INIT:
    162  1.1  rmind 		return npf_init();
    163  1.1  rmind 	case MODULE_CMD_FINI:
    164  1.1  rmind 		return npf_fini();
    165  1.1  rmind 	default:
    166  1.1  rmind 		return ENOTTY;
    167  1.1  rmind 	}
    168  1.1  rmind 	return 0;
    169  1.1  rmind }
    170  1.1  rmind 
    171  1.1  rmind void
    172  1.1  rmind npfattach(int nunits)
    173  1.1  rmind {
    174  1.1  rmind 
    175  1.1  rmind 	/* Void. */
    176  1.1  rmind }
    177  1.1  rmind 
    178  1.1  rmind static int
    179  1.1  rmind npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
    180  1.1  rmind {
    181  1.1  rmind 
    182  1.1  rmind 	/* Available only for super-user. */
    183  1.1  rmind 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    184  1.1  rmind 		return EPERM;
    185  1.1  rmind 	}
    186  1.1  rmind 	return 0;
    187  1.1  rmind }
    188  1.1  rmind 
    189  1.1  rmind static int
    190  1.1  rmind npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
    191  1.1  rmind {
    192  1.1  rmind 
    193  1.1  rmind 	return 0;
    194  1.1  rmind }
    195  1.1  rmind 
    196  1.1  rmind static int
    197  1.1  rmind npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    198  1.1  rmind {
    199  1.1  rmind 	int error;
    200  1.1  rmind 
    201  1.1  rmind 	/* Available only for super-user. */
    202  1.1  rmind 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    203  1.1  rmind 		return EPERM;
    204  1.1  rmind 	}
    205  1.1  rmind 
    206  1.1  rmind 	switch (cmd) {
    207  1.1  rmind 	case IOC_NPF_VERSION:
    208  1.1  rmind 		*(int *)data = NPF_VERSION;
    209  1.1  rmind 		error = 0;
    210  1.1  rmind 		break;
    211  1.1  rmind 	case IOC_NPF_SWITCH:
    212  1.1  rmind 		error = npfctl_switch(data);
    213  1.1  rmind 		break;
    214  1.1  rmind 	case IOC_NPF_RELOAD:
    215  1.1  rmind 		error = npfctl_reload(cmd, data);
    216  1.1  rmind 		break;
    217  1.9  rmind 	case IOC_NPF_GETCONF:
    218  1.9  rmind 		error = npfctl_getconf(cmd, data);
    219  1.9  rmind 		break;
    220  1.1  rmind 	case IOC_NPF_TABLE:
    221  1.1  rmind 		error = npfctl_table(data);
    222  1.1  rmind 		break;
    223  1.2  rmind 	case IOC_NPF_STATS:
    224  1.2  rmind 		error = npfctl_stats(data);
    225  1.2  rmind 		break;
    226  1.2  rmind 	case IOC_NPF_SESSIONS_SAVE:
    227  1.2  rmind 		error = npfctl_sessions_save(cmd, data);
    228  1.2  rmind 		break;
    229  1.2  rmind 	case IOC_NPF_SESSIONS_LOAD:
    230  1.2  rmind 		error = npfctl_sessions_load(cmd, data);
    231  1.2  rmind 		break;
    232  1.4  rmind 	case IOC_NPF_UPDATE_RULE:
    233  1.4  rmind 		error = npfctl_update_rule(cmd, data);
    234  1.4  rmind 		break;
    235  1.1  rmind 	default:
    236  1.1  rmind 		error = ENOTTY;
    237  1.1  rmind 		break;
    238  1.1  rmind 	}
    239  1.1  rmind 	return error;
    240  1.1  rmind }
    241  1.1  rmind 
    242  1.1  rmind static int
    243  1.1  rmind npf_dev_poll(dev_t dev, int events, lwp_t *l)
    244  1.1  rmind {
    245  1.1  rmind 
    246  1.1  rmind 	return ENOTSUP;
    247  1.1  rmind }
    248  1.1  rmind 
    249  1.1  rmind static int
    250  1.1  rmind npf_dev_read(dev_t dev, struct uio *uio, int flag)
    251  1.1  rmind {
    252  1.1  rmind 
    253  1.1  rmind 	return ENOTSUP;
    254  1.1  rmind }
    255  1.2  rmind 
    256  1.2  rmind /*
    257  1.2  rmind  * NPF core loading/reloading/unloading mechanism.
    258  1.2  rmind  */
    259  1.2  rmind 
    260  1.2  rmind static void
    261  1.2  rmind npf_core_destroy(npf_core_t *nc)
    262  1.2  rmind {
    263  1.2  rmind 
    264  1.9  rmind 	prop_object_release(nc->n_dict);
    265  1.2  rmind 	npf_ruleset_destroy(nc->n_rules);
    266  1.2  rmind 	npf_ruleset_destroy(nc->n_nat_rules);
    267  1.3  rmind 	npf_tableset_destroy(nc->n_tables);
    268  1.2  rmind 	kmem_free(nc, sizeof(npf_core_t));
    269  1.2  rmind }
    270  1.2  rmind 
    271  1.2  rmind /*
    272  1.2  rmind  * npf_reload: atomically load new ruleset, tableset and NAT policies.
    273  1.2  rmind  * Then destroy old (unloaded) structures.
    274  1.2  rmind  */
    275  1.2  rmind void
    276  1.9  rmind npf_reload(prop_dictionary_t dict, npf_ruleset_t *rset,
    277  1.9  rmind     npf_tableset_t *tset, npf_ruleset_t *nset, bool flush)
    278  1.2  rmind {
    279  1.2  rmind 	npf_core_t *nc, *onc;
    280  1.2  rmind 
    281  1.2  rmind 	/* Setup a new core structure. */
    282  1.8  rmind 	nc = kmem_zalloc(sizeof(npf_core_t), KM_SLEEP);
    283  1.2  rmind 	nc->n_rules = rset;
    284  1.2  rmind 	nc->n_tables = tset;
    285  1.2  rmind 	nc->n_nat_rules = nset;
    286  1.9  rmind 	nc->n_dict = dict;
    287  1.9  rmind 	nc->n_default_pass = flush;
    288  1.2  rmind 
    289  1.2  rmind 	/* Lock and load the core structure. */
    290  1.2  rmind 	rw_enter(&npf_lock, RW_WRITER);
    291  1.2  rmind 	onc = atomic_swap_ptr(&npf_core, nc);
    292  1.2  rmind 	if (onc) {
    293  1.2  rmind 		/* Reload only necessary NAT policies. */
    294  1.2  rmind 		npf_ruleset_natreload(nset, onc->n_nat_rules);
    295  1.2  rmind 	}
    296  1.2  rmind 	/* Unlock.  Everything goes "live" now. */
    297  1.2  rmind 	rw_exit(&npf_lock);
    298  1.2  rmind 
    299  1.2  rmind 	if (onc) {
    300  1.2  rmind 		/* Destroy unloaded structures. */
    301  1.2  rmind 		npf_core_destroy(onc);
    302  1.2  rmind 	}
    303  1.2  rmind }
    304  1.2  rmind 
    305  1.2  rmind void
    306  1.2  rmind npf_core_enter(void)
    307  1.2  rmind {
    308  1.2  rmind 	rw_enter(&npf_lock, RW_READER);
    309  1.2  rmind }
    310  1.2  rmind 
    311  1.2  rmind npf_ruleset_t *
    312  1.2  rmind npf_core_ruleset(void)
    313  1.2  rmind {
    314  1.2  rmind 	KASSERT(rw_lock_held(&npf_lock));
    315  1.2  rmind 	return npf_core->n_rules;
    316  1.2  rmind }
    317  1.2  rmind 
    318  1.2  rmind npf_ruleset_t *
    319  1.2  rmind npf_core_natset(void)
    320  1.2  rmind {
    321  1.2  rmind 	KASSERT(rw_lock_held(&npf_lock));
    322  1.2  rmind 	return npf_core->n_nat_rules;
    323  1.2  rmind }
    324  1.2  rmind 
    325  1.2  rmind npf_tableset_t *
    326  1.2  rmind npf_core_tableset(void)
    327  1.2  rmind {
    328  1.2  rmind 	KASSERT(rw_lock_held(&npf_lock));
    329  1.2  rmind 	return npf_core->n_tables;
    330  1.2  rmind }
    331  1.2  rmind 
    332  1.2  rmind void
    333  1.2  rmind npf_core_exit(void)
    334  1.2  rmind {
    335  1.2  rmind 	rw_exit(&npf_lock);
    336  1.2  rmind }
    337  1.2  rmind 
    338  1.2  rmind bool
    339  1.2  rmind npf_core_locked(void)
    340  1.2  rmind {
    341  1.2  rmind 	return rw_lock_held(&npf_lock);
    342  1.2  rmind }
    343  1.2  rmind 
    344  1.9  rmind prop_dictionary_t
    345  1.9  rmind npf_core_dict(void)
    346  1.9  rmind {
    347  1.9  rmind 	KASSERT(rw_lock_held(&npf_lock));
    348  1.9  rmind 	return npf_core->n_dict;
    349  1.9  rmind }
    350  1.9  rmind 
    351  1.8  rmind bool
    352  1.8  rmind npf_default_pass(void)
    353  1.8  rmind {
    354  1.8  rmind 	KASSERT(rw_lock_held(&npf_lock));
    355  1.8  rmind 	return npf_core->n_default_pass;
    356  1.8  rmind }
    357  1.8  rmind 
    358  1.2  rmind /*
    359  1.2  rmind  * NPF statistics interface.
    360  1.2  rmind  */
    361  1.2  rmind 
    362  1.2  rmind void
    363  1.2  rmind npf_stats_inc(npf_stats_t st)
    364  1.2  rmind {
    365  1.2  rmind 	uint64_t *stats = percpu_getref(npf_stats_percpu);
    366  1.2  rmind 	stats[st]++;
    367  1.2  rmind 	percpu_putref(npf_stats_percpu);
    368  1.2  rmind }
    369  1.2  rmind 
    370  1.2  rmind void
    371  1.2  rmind npf_stats_dec(npf_stats_t st)
    372  1.2  rmind {
    373  1.2  rmind 	uint64_t *stats = percpu_getref(npf_stats_percpu);
    374  1.2  rmind 	stats[st]--;
    375  1.2  rmind 	percpu_putref(npf_stats_percpu);
    376  1.2  rmind }
    377  1.2  rmind 
    378  1.2  rmind static void
    379  1.2  rmind npf_stats_collect(void *mem, void *arg, struct cpu_info *ci)
    380  1.2  rmind {
    381  1.2  rmind 	uint64_t *percpu_stats = mem, *full_stats = arg;
    382  1.2  rmind 	int i;
    383  1.2  rmind 
    384  1.2  rmind 	for (i = 0; i < NPF_STATS_COUNT; i++) {
    385  1.2  rmind 		full_stats[i] += percpu_stats[i];
    386  1.2  rmind 	}
    387  1.2  rmind }
    388  1.2  rmind 
    389  1.2  rmind /*
    390  1.2  rmind  * npfctl_stats: export collected statistics.
    391  1.2  rmind  */
    392  1.2  rmind static int
    393  1.2  rmind npfctl_stats(void *data)
    394  1.2  rmind {
    395  1.2  rmind 	uint64_t *fullst, *uptr = *(uint64_t **)data;
    396  1.2  rmind 	int error;
    397  1.2  rmind 
    398  1.2  rmind 	fullst = kmem_zalloc(NPF_STATS_SIZE, KM_SLEEP);
    399  1.2  rmind 	percpu_foreach(npf_stats_percpu, npf_stats_collect, fullst);
    400  1.2  rmind 	error = copyout(fullst, uptr, NPF_STATS_SIZE);
    401  1.2  rmind 	kmem_free(fullst, NPF_STATS_SIZE);
    402  1.2  rmind 	return error;
    403  1.2  rmind }
    404