Home | History | Annotate | Line # | Download | only in npf
npf.c revision 1.28
      1 /*	$NetBSD: npf.c,v 1.28 2015/10/19 09:28:24 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This material is based upon work partially supported by The
      8  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * NPF main: dynamic load/initialisation and unload routines.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.28 2015/10/19 09:28:24 martin Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/types.h>
     41 
     42 #include <sys/atomic.h>
     43 #include <sys/conf.h>
     44 #include <sys/kauth.h>
     45 #include <sys/kmem.h>
     46 #include <sys/lwp.h>
     47 #include <sys/module.h>
     48 #include <sys/percpu.h>
     49 #include <sys/rwlock.h>
     50 #include <sys/socketvar.h>
     51 #include <sys/sysctl.h>
     52 #include <sys/uio.h>
     53 
     54 #include "npf_impl.h"
     55 #include "npf_conn.h"
     56 
     57 #ifndef _MODULE
     58 #include "opt_modular.h"
     59 #endif
     60 
     61 #include "ioconf.h"
     62 
     63 /*
     64  * Module and device structures.
     65  */
     66 #ifdef MODULAR
     67 /*
     68  * Modular kernels load drivers too early, and we need percpu to be inited
     69  * So we make this misc; a better way would be to have early boot and late
     70  * boot drivers
     71  */
     72 MODULE(MODULE_CLASS_MISC, npf, NULL);
     73 #else
     74 /* This module autoloads via /dev/npf so it needs to be a driver */
     75 MODULE(MODULE_CLASS_DRIVER, npf, NULL);
     76 #endif
     77 
     78 static int	npf_fini(void);
     79 static int	npf_dev_open(dev_t, int, int, lwp_t *);
     80 static int	npf_dev_close(dev_t, int, int, lwp_t *);
     81 static int	npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
     82 static int	npf_dev_poll(dev_t, int, lwp_t *);
     83 static int	npf_dev_read(dev_t, struct uio *, int);
     84 
     85 static int	npfctl_stats(void *);
     86 
     87 static percpu_t *		npf_stats_percpu	__read_mostly;
     88 static struct sysctllog *	npf_sysctl		__read_mostly;
     89 
     90 const struct cdevsw npf_cdevsw = {
     91 	.d_open = npf_dev_open,
     92 	.d_close = npf_dev_close,
     93 	.d_read = npf_dev_read,
     94 	.d_write = nowrite,
     95 	.d_ioctl = npf_dev_ioctl,
     96 	.d_stop = nostop,
     97 	.d_tty = notty,
     98 	.d_poll = npf_dev_poll,
     99 	.d_mmap = nommap,
    100 	.d_kqfilter = nokqfilter,
    101 	.d_discard = nodiscard,
    102 	.d_flag = D_OTHER | D_MPSAFE
    103 };
    104 
    105 #if !defined(MODULAR) || defined(_MODULE)
    106 static int
    107 npf_init(void)
    108 {
    109 	KASSERT(npf_stats_percpu == NULL);
    110 
    111 	npf_stats_percpu = percpu_alloc(NPF_STATS_SIZE);
    112 	npf_sysctl = NULL;
    113 
    114 	npf_bpf_sysinit();
    115 	npf_worker_sysinit();
    116 	npf_tableset_sysinit();
    117 	npf_conn_sysinit();
    118 	npf_nat_sysinit();
    119 	npf_alg_sysinit();
    120 	npf_ext_sysinit();
    121 
    122 	/* Load empty configuration. */
    123 	npf_pfil_register(true);
    124 	npf_config_init();
    125 
    126 #ifdef _MODULE
    127 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
    128 
    129 	/* Attach /dev/npf device. */
    130 	int error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
    131 	if (error) {
    132 		/* It will call devsw_detach(), which is safe. */
    133 		(void)npf_fini();
    134 	}
    135 	return error;
    136 #else
    137 	return 0;
    138 #endif
    139 }
    140 #endif
    141 
    142 static int
    143 npf_fini(void)
    144 {
    145 	/* At first, detach device and remove pfil hooks. */
    146 #ifdef _MODULE
    147 	devsw_detach(NULL, &npf_cdevsw);
    148 #endif
    149 	npf_pfil_unregister(true);
    150 	npf_config_fini();
    151 
    152 	/* Finally, safe to destroy the subsystems. */
    153 	npf_ext_sysfini();
    154 	npf_alg_sysfini();
    155 	npf_nat_sysfini();
    156 	npf_conn_sysfini();
    157 	npf_tableset_sysfini();
    158 	npf_bpf_sysfini();
    159 
    160 	/* Note: worker is the last. */
    161 	npf_worker_sysfini();
    162 
    163 	if (npf_sysctl) {
    164 		sysctl_teardown(&npf_sysctl);
    165 	}
    166 	percpu_free(npf_stats_percpu, NPF_STATS_SIZE);
    167 
    168 	return 0;
    169 }
    170 
    171 /*
    172  * Module interface.
    173  */
    174 static int
    175 npf_modcmd(modcmd_t cmd, void *arg)
    176 {
    177 
    178 	switch (cmd) {
    179 	case MODULE_CMD_INIT:
    180 #if defined(_MODULE)
    181 		return npf_init();
    182 #else
    183 		/* initialized by npfattach */
    184 		return 0;
    185 #endif
    186 	case MODULE_CMD_FINI:
    187 		return npf_fini();
    188 	case MODULE_CMD_AUTOUNLOAD:
    189 		if (npf_autounload_p()) {
    190 			return EBUSY;
    191 		}
    192 		break;
    193 	default:
    194 		return ENOTTY;
    195 	}
    196 	return 0;
    197 }
    198 
    199 void
    200 npfattach(int nunits)
    201 {
    202 #ifdef MODULAR
    203 	/*
    204 	 * Modular kernels will automatically load any built-in modules
    205 	 * and call their modcmd() routine, so we don't need to do it
    206 	 * again as part of pseudo-device configuration.
    207 	 */
    208 	return;
    209 #else
    210 	(void)npf_init();
    211 #endif
    212 }
    213 
    214 static int
    215 npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
    216 {
    217 
    218 	/* Available only for super-user. */
    219 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
    220 	    KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
    221 		return EPERM;
    222 	}
    223 	return 0;
    224 }
    225 
    226 static int
    227 npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
    228 {
    229 
    230 	return 0;
    231 }
    232 
    233 static int
    234 npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    235 {
    236 	int error;
    237 
    238 	/* Available only for super-user. */
    239 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
    240 	    KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
    241 		return EPERM;
    242 	}
    243 
    244 	switch (cmd) {
    245 	case IOC_NPF_TABLE:
    246 		error = npfctl_table(data);
    247 		break;
    248 	case IOC_NPF_RULE:
    249 		error = npfctl_rule(cmd, data);
    250 		break;
    251 	case IOC_NPF_STATS:
    252 		error = npfctl_stats(data);
    253 		break;
    254 	case IOC_NPF_SAVE:
    255 		error = npfctl_save(cmd, data);
    256 		break;
    257 	case IOC_NPF_SWITCH:
    258 		error = npfctl_switch(data);
    259 		break;
    260 	case IOC_NPF_LOAD:
    261 		error = npfctl_load(cmd, data);
    262 		break;
    263 	case IOC_NPF_VERSION:
    264 		*(int *)data = NPF_VERSION;
    265 		error = 0;
    266 		break;
    267 	default:
    268 		error = ENOTTY;
    269 		break;
    270 	}
    271 	return error;
    272 }
    273 
    274 static int
    275 npf_dev_poll(dev_t dev, int events, lwp_t *l)
    276 {
    277 
    278 	return ENOTSUP;
    279 }
    280 
    281 static int
    282 npf_dev_read(dev_t dev, struct uio *uio, int flag)
    283 {
    284 
    285 	return ENOTSUP;
    286 }
    287 
    288 bool
    289 npf_autounload_p(void)
    290 {
    291 	return !npf_pfil_registered_p() && npf_default_pass();
    292 }
    293 
    294 /*
    295  * NPF statistics interface.
    296  */
    297 
    298 void
    299 npf_stats_inc(npf_stats_t st)
    300 {
    301 	uint64_t *stats = percpu_getref(npf_stats_percpu);
    302 	stats[st]++;
    303 	percpu_putref(npf_stats_percpu);
    304 }
    305 
    306 void
    307 npf_stats_dec(npf_stats_t st)
    308 {
    309 	uint64_t *stats = percpu_getref(npf_stats_percpu);
    310 	stats[st]--;
    311 	percpu_putref(npf_stats_percpu);
    312 }
    313 
    314 static void
    315 npf_stats_collect(void *mem, void *arg, struct cpu_info *ci)
    316 {
    317 	uint64_t *percpu_stats = mem, *full_stats = arg;
    318 	int i;
    319 
    320 	for (i = 0; i < NPF_STATS_COUNT; i++) {
    321 		full_stats[i] += percpu_stats[i];
    322 	}
    323 }
    324 
    325 /*
    326  * npfctl_stats: export collected statistics.
    327  */
    328 static int
    329 npfctl_stats(void *data)
    330 {
    331 	uint64_t *fullst, *uptr = *(uint64_t **)data;
    332 	int error;
    333 
    334 	fullst = kmem_zalloc(NPF_STATS_SIZE, KM_SLEEP);
    335 	percpu_foreach(npf_stats_percpu, npf_stats_collect, fullst);
    336 	error = copyout(fullst, uptr, NPF_STATS_SIZE);
    337 	kmem_free(fullst, NPF_STATS_SIZE);
    338 	return error;
    339 }
    340