Home | History | Annotate | Line # | Download | only in npf
npf_os.c revision 1.15
      1 /*-
      2  * Copyright (c) 2009-2016 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This material is based upon work partially supported by The
      6  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * NPF main: dynamic load/initialisation and unload routines.
     32  */
     33 
     34 #ifdef _KERNEL
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: npf_os.c,v 1.15 2019/08/21 21:45:47 rmind Exp $");
     37 
     38 #ifdef _KERNEL_OPT
     39 #include "pf.h"
     40 #if NPF > 0
     41 #error "NPF and PF are mutually exclusive; please select one"
     42 #endif
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/types.h>
     47 
     48 #include <sys/conf.h>
     49 #include <sys/kauth.h>
     50 #include <sys/kmem.h>
     51 #include <sys/lwp.h>
     52 #include <sys/module.h>
     53 #include <sys/socketvar.h>
     54 #include <sys/uio.h>
     55 
     56 #include <netinet/in.h>
     57 #include <netinet6/in6_var.h>
     58 #endif
     59 
     60 #include "npf_impl.h"
     61 #include "npfkern.h"
     62 
     63 #ifdef _KERNEL
     64 #ifndef _MODULE
     65 #include "opt_modular.h"
     66 #include "opt_net_mpsafe.h"
     67 #endif
     68 #include "ioconf.h"
     69 #endif
     70 
     71 /*
     72  * Module and device structures.
     73  */
     74 #ifndef _MODULE
     75 /*
     76  * Modular kernels load drivers too early, and we need percpu to be inited
     77  * So we make this misc; a better way would be to have early boot and late
     78  * boot drivers.
     79  */
     80 MODULE(MODULE_CLASS_MISC, npf, "bpf");
     81 #else
     82 /* This module autoloads via /dev/npf so it needs to be a driver */
     83 MODULE(MODULE_CLASS_DRIVER, npf, "bpf");
     84 #endif
     85 
     86 static int	npf_dev_open(dev_t, int, int, lwp_t *);
     87 static int	npf_dev_close(dev_t, int, int, lwp_t *);
     88 static int	npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
     89 static int	npf_dev_poll(dev_t, int, lwp_t *);
     90 static int	npf_dev_read(dev_t, struct uio *, int);
     91 
     92 const struct cdevsw npf_cdevsw = {
     93 	.d_open = npf_dev_open,
     94 	.d_close = npf_dev_close,
     95 	.d_read = npf_dev_read,
     96 	.d_write = nowrite,
     97 	.d_ioctl = npf_dev_ioctl,
     98 	.d_stop = nostop,
     99 	.d_tty = notty,
    100 	.d_poll = npf_dev_poll,
    101 	.d_mmap = nommap,
    102 	.d_kqfilter = nokqfilter,
    103 	.d_discard = nodiscard,
    104 	.d_flag = D_OTHER | D_MPSAFE
    105 };
    106 
    107 static const char *	npf_ifop_getname(ifnet_t *);
    108 static ifnet_t *	npf_ifop_lookup(const char *);
    109 static void		npf_ifop_flush(void *);
    110 static void *		npf_ifop_getmeta(const ifnet_t *);
    111 static void		npf_ifop_setmeta(ifnet_t *, void *);
    112 
    113 static const unsigned	nworkers = 1;
    114 
    115 static bool		pfil_registered = false;
    116 static pfil_head_t *	npf_ph_if = NULL;
    117 static pfil_head_t *	npf_ph_inet = NULL;
    118 static pfil_head_t *	npf_ph_inet6 = NULL;
    119 
    120 static const npf_ifops_t kern_ifops = {
    121 	.getname	= npf_ifop_getname,
    122 	.lookup		= npf_ifop_lookup,
    123 	.flush		= npf_ifop_flush,
    124 	.getmeta	= npf_ifop_getmeta,
    125 	.setmeta	= npf_ifop_setmeta,
    126 };
    127 
    128 static int
    129 npf_fini(void)
    130 {
    131 	npf_t *npf = npf_getkernctx();
    132 
    133 	/* At first, detach device and remove pfil hooks. */
    134 #ifdef _MODULE
    135 	devsw_detach(NULL, &npf_cdevsw);
    136 #endif
    137 	npf_pfil_unregister(true);
    138 	npfk_destroy(npf);
    139 	npfk_sysfini();
    140 	return 0;
    141 }
    142 
    143 static int
    144 npf_init(void)
    145 {
    146 	npf_t *npf;
    147 	int error = 0;
    148 
    149 	error = npfk_sysinit(nworkers);
    150 	if (error)
    151 		return error;
    152 	npf = npfk_create(0, NULL, &kern_ifops);
    153 	npf_setkernctx(npf);
    154 	npf_pfil_register(true);
    155 
    156 #ifdef _MODULE
    157 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
    158 
    159 	/* Attach /dev/npf device. */
    160 	error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
    161 	if (error) {
    162 		/* It will call devsw_detach(), which is safe. */
    163 		(void)npf_fini();
    164 	}
    165 #endif
    166 	return error;
    167 }
    168 
    169 
    170 /*
    171  * Module interface.
    172  */
    173 static int
    174 npf_modcmd(modcmd_t cmd, void *arg)
    175 {
    176 	switch (cmd) {
    177 	case MODULE_CMD_INIT:
    178 		return npf_init();
    179 	case MODULE_CMD_FINI:
    180 		return npf_fini();
    181 	case MODULE_CMD_AUTOUNLOAD:
    182 		if (npf_autounload_p()) {
    183 			return EBUSY;
    184 		}
    185 		break;
    186 	default:
    187 		return ENOTTY;
    188 	}
    189 	return 0;
    190 }
    191 
    192 void
    193 npfattach(int nunits)
    194 {
    195 	/* Nothing */
    196 }
    197 
    198 static int
    199 npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
    200 {
    201 	/* Available only for super-user. */
    202 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
    203 	    KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
    204 		return EPERM;
    205 	}
    206 	return 0;
    207 }
    208 
    209 static int
    210 npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
    211 {
    212 	return 0;
    213 }
    214 
    215 static int
    216 npf_stats_export(npf_t *npf, void *data)
    217 {
    218 	uint64_t *fullst, *uptr = *(uint64_t **)data;
    219 	int error;
    220 
    221 	fullst = kmem_alloc(NPF_STATS_SIZE, KM_SLEEP);
    222 	npfk_stats(npf, fullst); /* will zero the buffer */
    223 	error = copyout(fullst, uptr, NPF_STATS_SIZE);
    224 	kmem_free(fullst, NPF_STATS_SIZE);
    225 	return error;
    226 }
    227 
    228 static int
    229 npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    230 {
    231 	npf_t *npf = npf_getkernctx();
    232 	int error;
    233 
    234 	/* Available only for super-user. */
    235 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
    236 	    KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
    237 		return EPERM;
    238 	}
    239 
    240 	switch (cmd) {
    241 	case IOC_NPF_TABLE:
    242 		error = npfctl_table(npf, data);
    243 		break;
    244 	case IOC_NPF_RULE:
    245 		error = npfctl_rule(npf, cmd, data);
    246 		break;
    247 	case IOC_NPF_STATS:
    248 		error = npf_stats_export(npf, data);
    249 		break;
    250 	case IOC_NPF_SAVE:
    251 		error = npfctl_save(npf, cmd, data);
    252 		break;
    253 	case IOC_NPF_SWITCH:
    254 		error = npfctl_switch(data);
    255 		break;
    256 	case IOC_NPF_LOAD:
    257 		error = npfctl_load(npf, cmd, data);
    258 		break;
    259 	case IOC_NPF_CONN_LOOKUP:
    260 		error = npfctl_conn_lookup(npf, cmd, data);
    261 		break;
    262 	case IOC_NPF_TABLE_REPLACE:
    263 		error = npfctl_table_replace(npf, cmd, data);
    264 		break;
    265 	case IOC_NPF_VERSION:
    266 		*(int *)data = NPF_VERSION;
    267 		error = 0;
    268 		break;
    269 	default:
    270 		error = ENOTTY;
    271 		break;
    272 	}
    273 	return error;
    274 }
    275 
    276 static int
    277 npf_dev_poll(dev_t dev, int events, lwp_t *l)
    278 {
    279 	return ENOTSUP;
    280 }
    281 
    282 static int
    283 npf_dev_read(dev_t dev, struct uio *uio, int flag)
    284 {
    285 	return ENOTSUP;
    286 }
    287 
    288 bool
    289 npf_autounload_p(void)
    290 {
    291 	npf_t *npf = npf_getkernctx();
    292 	return !npf_pfil_registered_p() && npf_default_pass(npf);
    293 }
    294 
    295 /*
    296  * Interface operations.
    297  */
    298 
    299 static const char *
    300 npf_ifop_getname(ifnet_t *ifp)
    301 {
    302 	return ifp->if_xname;
    303 }
    304 
    305 static ifnet_t *
    306 npf_ifop_lookup(const char *name)
    307 {
    308 	return ifunit(name);
    309 }
    310 
    311 static void
    312 npf_ifop_flush(void *arg)
    313 {
    314 	ifnet_t *ifp;
    315 
    316 	KERNEL_LOCK(1, NULL);
    317 	IFNET_GLOBAL_LOCK();
    318 	IFNET_WRITER_FOREACH(ifp) {
    319 		ifp->if_npf_private = arg;
    320 	}
    321 	IFNET_GLOBAL_UNLOCK();
    322 	KERNEL_UNLOCK_ONE(NULL);
    323 }
    324 
    325 static void *
    326 npf_ifop_getmeta(const ifnet_t *ifp)
    327 {
    328 	return ifp->if_npf_private;
    329 }
    330 
    331 static void
    332 npf_ifop_setmeta(ifnet_t *ifp, void *arg)
    333 {
    334 	ifp->if_npf_private = arg;
    335 }
    336 
    337 #ifdef _KERNEL
    338 
    339 /*
    340  * Wrapper of the main packet handler to pass the kernel NPF context.
    341  */
    342 static int
    343 npfos_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
    344 {
    345 	npf_t *npf = npf_getkernctx();
    346 	return npfk_packet_handler(npf, mp, ifp, di);
    347 }
    348 
    349 /*
    350  * npf_ifhook: hook handling interface changes.
    351  */
    352 static void
    353 npf_ifhook(void *arg, unsigned long cmd, void *arg2)
    354 {
    355 	npf_t *npf = npf_getkernctx();
    356 	ifnet_t *ifp = arg2;
    357 
    358 	switch (cmd) {
    359 	case PFIL_IFNET_ATTACH:
    360 		npfk_ifmap_attach(npf, ifp);
    361 		npf_ifaddr_sync(npf, ifp);
    362 		break;
    363 	case PFIL_IFNET_DETACH:
    364 		npfk_ifmap_detach(npf, ifp);
    365 		npf_ifaddr_flush(npf, ifp);
    366 		break;
    367 	}
    368 }
    369 
    370 static void
    371 npf_ifaddrhook(void *arg, u_long cmd, void *arg2)
    372 {
    373 	npf_t *npf = npf_getkernctx();
    374 	struct ifaddr *ifa = arg2;
    375 
    376 	switch (cmd) {
    377 	case SIOCSIFADDR:
    378 	case SIOCAIFADDR:
    379 	case SIOCDIFADDR:
    380 #ifdef INET6
    381 	case SIOCSIFADDR_IN6:
    382 	case SIOCAIFADDR_IN6:
    383 	case SIOCDIFADDR_IN6:
    384 #endif
    385 		KASSERT(ifa != NULL);
    386 		break;
    387 	default:
    388 		return;
    389 	}
    390 	npf_ifaddr_sync(npf, ifa->ifa_ifp);
    391 }
    392 
    393 /*
    394  * npf_pfil_register: register pfil(9) hooks.
    395  */
    396 int
    397 npf_pfil_register(bool init)
    398 {
    399 	npf_t *npf = npf_getkernctx();
    400 	int error = 0;
    401 
    402 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    403 
    404 	/* Init: interface re-config and attach/detach hook. */
    405 	if (!npf_ph_if) {
    406 		npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
    407 		if (!npf_ph_if) {
    408 			error = ENOENT;
    409 			goto out;
    410 		}
    411 
    412 		error = pfil_add_ihook(npf_ifhook, NULL,
    413 		    PFIL_IFNET, npf_ph_if);
    414 		KASSERT(error == 0);
    415 
    416 		error = pfil_add_ihook(npf_ifaddrhook, NULL,
    417 		    PFIL_IFADDR, npf_ph_if);
    418 		KASSERT(error == 0);
    419 	}
    420 	if (init) {
    421 		goto out;
    422 	}
    423 
    424 	/* Check if pfil hooks are not already registered. */
    425 	if (pfil_registered) {
    426 		error = EEXIST;
    427 		goto out;
    428 	}
    429 
    430 	/* Capture points of the activity in the IP layer. */
    431 	npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET);
    432 	npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET6);
    433 	if (!npf_ph_inet && !npf_ph_inet6) {
    434 		error = ENOENT;
    435 		goto out;
    436 	}
    437 
    438 	/* Packet IN/OUT handlers for IP layer. */
    439 	if (npf_ph_inet) {
    440 		error = pfil_add_hook(npfos_packet_handler, npf,
    441 		    PFIL_ALL, npf_ph_inet);
    442 		KASSERT(error == 0);
    443 	}
    444 	if (npf_ph_inet6) {
    445 		error = pfil_add_hook(npfos_packet_handler, npf,
    446 		    PFIL_ALL, npf_ph_inet6);
    447 		KASSERT(error == 0);
    448 	}
    449 
    450 	/*
    451 	 * It is necessary to re-sync all/any interface address tables,
    452 	 * since we did not listen for any changes.
    453 	 */
    454 	npf_ifaddr_syncall(npf);
    455 	pfil_registered = true;
    456 out:
    457 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    458 
    459 	return error;
    460 }
    461 
    462 /*
    463  * npf_pfil_unregister: unregister pfil(9) hooks.
    464  */
    465 void
    466 npf_pfil_unregister(bool fini)
    467 {
    468 	npf_t *npf = npf_getkernctx();
    469 
    470 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    471 
    472 	if (fini && npf_ph_if) {
    473 		(void)pfil_remove_ihook(npf_ifhook, NULL,
    474 		    PFIL_IFNET, npf_ph_if);
    475 		(void)pfil_remove_ihook(npf_ifaddrhook, NULL,
    476 		    PFIL_IFADDR, npf_ph_if);
    477 	}
    478 	if (npf_ph_inet) {
    479 		(void)pfil_remove_hook(npfos_packet_handler, npf,
    480 		    PFIL_ALL, npf_ph_inet);
    481 	}
    482 	if (npf_ph_inet6) {
    483 		(void)pfil_remove_hook(npfos_packet_handler, npf,
    484 		    PFIL_ALL, npf_ph_inet6);
    485 	}
    486 	pfil_registered = false;
    487 
    488 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    489 }
    490 
    491 bool
    492 npf_pfil_registered_p(void)
    493 {
    494 	return pfil_registered;
    495 }
    496 #endif
    497