Home | History | Annotate | Line # | Download | only in npf
npf.c revision 1.31.2.1
      1  1.31.2.1  pgoyette /*	$NetBSD: npf.c,v 1.31.2.1 2016/07/18 03:50:00 pgoyette Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*-
      4      1.15     rmind  * Copyright (c) 2009-2013 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.31.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.31.2.1 2016/07/18 03:50:00 pgoyette 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.11     rmind #include <sys/sysctl.h>
     52       1.1     rmind #include <sys/uio.h>
     53  1.31.2.1  pgoyette #include <sys/localcount.h>
     54       1.1     rmind 
     55       1.1     rmind #include "npf_impl.h"
     56      1.20     rmind #include "npf_conn.h"
     57       1.1     rmind 
     58      1.27  christos #ifndef _MODULE
     59      1.27  christos #include "opt_modular.h"
     60      1.27  christos #endif
     61      1.27  christos 
     62      1.23  christos #include "ioconf.h"
     63      1.23  christos 
     64       1.1     rmind /*
     65       1.1     rmind  * Module and device structures.
     66       1.1     rmind  */
     67      1.31  christos #ifndef _MODULE
     68      1.27  christos /*
     69      1.27  christos  * Modular kernels load drivers too early, and we need percpu to be inited
     70      1.27  christos  * So we make this misc; a better way would be to have early boot and late
     71      1.31  christos  * boot drivers.
     72      1.27  christos  */
     73      1.27  christos MODULE(MODULE_CLASS_MISC, npf, NULL);
     74      1.27  christos #else
     75      1.27  christos /* This module autoloads via /dev/npf so it needs to be a driver */
     76      1.25  christos MODULE(MODULE_CLASS_DRIVER, npf, NULL);
     77      1.27  christos #endif
     78       1.1     rmind 
     79       1.5      yamt static int	npf_fini(void);
     80       1.1     rmind static int	npf_dev_open(dev_t, int, int, lwp_t *);
     81       1.1     rmind static int	npf_dev_close(dev_t, int, int, lwp_t *);
     82       1.1     rmind static int	npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
     83       1.1     rmind static int	npf_dev_poll(dev_t, int, lwp_t *);
     84       1.1     rmind static int	npf_dev_read(dev_t, struct uio *, int);
     85       1.1     rmind 
     86       1.2     rmind static int	npfctl_stats(void *);
     87       1.2     rmind 
     88       1.2     rmind static percpu_t *		npf_stats_percpu	__read_mostly;
     89      1.11     rmind static struct sysctllog *	npf_sysctl		__read_mostly;
     90       1.2     rmind 
     91  1.31.2.1  pgoyette #ifdef _MODULE
     92  1.31.2.1  pgoyette struct localcount npf_localcount;
     93  1.31.2.1  pgoyette #endif
     94  1.31.2.1  pgoyette 
     95       1.1     rmind const struct cdevsw npf_cdevsw = {
     96      1.19  dholland 	.d_open = npf_dev_open,
     97      1.19  dholland 	.d_close = npf_dev_close,
     98      1.19  dholland 	.d_read = npf_dev_read,
     99      1.19  dholland 	.d_write = nowrite,
    100      1.19  dholland 	.d_ioctl = npf_dev_ioctl,
    101      1.19  dholland 	.d_stop = nostop,
    102      1.19  dholland 	.d_tty = notty,
    103      1.19  dholland 	.d_poll = npf_dev_poll,
    104      1.19  dholland 	.d_mmap = nommap,
    105      1.19  dholland 	.d_kqfilter = nokqfilter,
    106      1.22  dholland 	.d_discard = nodiscard,
    107  1.31.2.1  pgoyette #ifdef	_MODULE
    108  1.31.2.1  pgoyette 	.d_localcount = &npf_localcount,
    109  1.31.2.1  pgoyette #endif
    110      1.19  dholland 	.d_flag = D_OTHER | D_MPSAFE
    111       1.1     rmind };
    112       1.1     rmind 
    113       1.1     rmind static int
    114       1.1     rmind npf_init(void)
    115       1.1     rmind {
    116      1.27  christos 	KASSERT(npf_stats_percpu == NULL);
    117       1.2     rmind 
    118       1.2     rmind 	npf_stats_percpu = percpu_alloc(NPF_STATS_SIZE);
    119      1.11     rmind 	npf_sysctl = NULL;
    120      1.11     rmind 
    121      1.17     rmind 	npf_bpf_sysinit();
    122      1.16     rmind 	npf_worker_sysinit();
    123       1.2     rmind 	npf_tableset_sysinit();
    124      1.20     rmind 	npf_conn_sysinit();
    125       1.1     rmind 	npf_nat_sysinit();
    126       1.1     rmind 	npf_alg_sysinit();
    127      1.13     rmind 	npf_ext_sysinit();
    128       1.2     rmind 
    129       1.2     rmind 	/* Load empty configuration. */
    130      1.18     rmind 	npf_pfil_register(true);
    131      1.15     rmind 	npf_config_init();
    132       1.1     rmind 
    133       1.1     rmind #ifdef _MODULE
    134      1.27  christos 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
    135      1.27  christos 
    136       1.1     rmind 	/* Attach /dev/npf device. */
    137      1.27  christos 	int error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
    138       1.1     rmind 	if (error) {
    139       1.2     rmind 		/* It will call devsw_detach(), which is safe. */
    140       1.2     rmind 		(void)npf_fini();
    141       1.1     rmind 	}
    142      1.27  christos 	return error;
    143      1.27  christos #else
    144      1.27  christos 	return 0;
    145       1.1     rmind #endif
    146       1.1     rmind }
    147       1.1     rmind 
    148       1.1     rmind static int
    149       1.1     rmind npf_fini(void)
    150       1.1     rmind {
    151       1.9     rmind 	/* At first, detach device and remove pfil hooks. */
    152       1.1     rmind #ifdef _MODULE
    153       1.1     rmind 	devsw_detach(NULL, &npf_cdevsw);
    154       1.1     rmind #endif
    155      1.18     rmind 	npf_pfil_unregister(true);
    156      1.15     rmind 	npf_config_fini();
    157       1.2     rmind 
    158       1.9     rmind 	/* Finally, safe to destroy the subsystems. */
    159      1.13     rmind 	npf_ext_sysfini();
    160       1.9     rmind 	npf_alg_sysfini();
    161       1.1     rmind 	npf_nat_sysfini();
    162      1.20     rmind 	npf_conn_sysfini();
    163       1.1     rmind 	npf_tableset_sysfini();
    164      1.17     rmind 	npf_bpf_sysfini();
    165      1.11     rmind 
    166      1.16     rmind 	/* Note: worker is the last. */
    167      1.16     rmind 	npf_worker_sysfini();
    168      1.16     rmind 
    169      1.11     rmind 	if (npf_sysctl) {
    170      1.11     rmind 		sysctl_teardown(&npf_sysctl);
    171      1.11     rmind 	}
    172       1.2     rmind 	percpu_free(npf_stats_percpu, NPF_STATS_SIZE);
    173       1.1     rmind 
    174       1.1     rmind 	return 0;
    175       1.1     rmind }
    176       1.1     rmind 
    177       1.1     rmind /*
    178       1.1     rmind  * Module interface.
    179       1.1     rmind  */
    180       1.1     rmind static int
    181       1.1     rmind npf_modcmd(modcmd_t cmd, void *arg)
    182       1.1     rmind {
    183       1.1     rmind 
    184       1.1     rmind 	switch (cmd) {
    185       1.1     rmind 	case MODULE_CMD_INIT:
    186       1.1     rmind 		return npf_init();
    187       1.1     rmind 	case MODULE_CMD_FINI:
    188       1.1     rmind 		return npf_fini();
    189      1.12     rmind 	case MODULE_CMD_AUTOUNLOAD:
    190      1.13     rmind 		if (npf_autounload_p()) {
    191      1.12     rmind 			return EBUSY;
    192      1.12     rmind 		}
    193      1.12     rmind 		break;
    194       1.1     rmind 	default:
    195       1.1     rmind 		return ENOTTY;
    196       1.1     rmind 	}
    197       1.1     rmind 	return 0;
    198       1.1     rmind }
    199       1.1     rmind 
    200       1.1     rmind void
    201       1.1     rmind npfattach(int nunits)
    202       1.1     rmind {
    203      1.31  christos 	/* Nothing */
    204       1.1     rmind }
    205       1.1     rmind 
    206       1.1     rmind static int
    207       1.1     rmind npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
    208       1.1     rmind {
    209       1.1     rmind 
    210       1.1     rmind 	/* Available only for super-user. */
    211      1.10      elad 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
    212      1.10      elad 	    KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
    213       1.1     rmind 		return EPERM;
    214       1.1     rmind 	}
    215       1.1     rmind 	return 0;
    216       1.1     rmind }
    217       1.1     rmind 
    218       1.1     rmind static int
    219       1.1     rmind npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
    220       1.1     rmind {
    221       1.1     rmind 
    222       1.1     rmind 	return 0;
    223       1.1     rmind }
    224       1.1     rmind 
    225       1.1     rmind static int
    226       1.1     rmind npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    227       1.1     rmind {
    228       1.1     rmind 	int error;
    229       1.1     rmind 
    230       1.1     rmind 	/* Available only for super-user. */
    231      1.10      elad 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FIREWALL,
    232      1.10      elad 	    KAUTH_REQ_NETWORK_FIREWALL_FW, NULL, NULL, NULL)) {
    233       1.1     rmind 		return EPERM;
    234       1.1     rmind 	}
    235       1.1     rmind 
    236       1.1     rmind 	switch (cmd) {
    237      1.15     rmind 	case IOC_NPF_TABLE:
    238      1.15     rmind 		error = npfctl_table(data);
    239       1.1     rmind 		break;
    240      1.15     rmind 	case IOC_NPF_RULE:
    241      1.15     rmind 		error = npfctl_rule(cmd, data);
    242       1.1     rmind 		break;
    243       1.2     rmind 	case IOC_NPF_STATS:
    244       1.2     rmind 		error = npfctl_stats(data);
    245       1.2     rmind 		break;
    246      1.21     rmind 	case IOC_NPF_SAVE:
    247      1.21     rmind 		error = npfctl_save(cmd, data);
    248       1.2     rmind 		break;
    249      1.15     rmind 	case IOC_NPF_SWITCH:
    250      1.15     rmind 		error = npfctl_switch(data);
    251      1.15     rmind 		break;
    252      1.21     rmind 	case IOC_NPF_LOAD:
    253      1.21     rmind 		error = npfctl_load(cmd, data);
    254      1.15     rmind 		break;
    255      1.15     rmind 	case IOC_NPF_VERSION:
    256      1.15     rmind 		*(int *)data = NPF_VERSION;
    257      1.15     rmind 		error = 0;
    258       1.4     rmind 		break;
    259       1.1     rmind 	default:
    260       1.1     rmind 		error = ENOTTY;
    261       1.1     rmind 		break;
    262       1.1     rmind 	}
    263       1.1     rmind 	return error;
    264       1.1     rmind }
    265       1.1     rmind 
    266       1.1     rmind static int
    267       1.1     rmind npf_dev_poll(dev_t dev, int events, lwp_t *l)
    268       1.1     rmind {
    269       1.1     rmind 
    270       1.1     rmind 	return ENOTSUP;
    271       1.1     rmind }
    272       1.1     rmind 
    273       1.1     rmind static int
    274       1.1     rmind npf_dev_read(dev_t dev, struct uio *uio, int flag)
    275       1.1     rmind {
    276       1.1     rmind 
    277       1.1     rmind 	return ENOTSUP;
    278       1.1     rmind }
    279       1.2     rmind 
    280      1.13     rmind bool
    281      1.13     rmind npf_autounload_p(void)
    282      1.13     rmind {
    283      1.13     rmind 	return !npf_pfil_registered_p() && npf_default_pass();
    284      1.13     rmind }
    285      1.13     rmind 
    286       1.2     rmind /*
    287       1.2     rmind  * NPF statistics interface.
    288       1.2     rmind  */
    289       1.2     rmind 
    290       1.2     rmind void
    291       1.2     rmind npf_stats_inc(npf_stats_t st)
    292       1.2     rmind {
    293       1.2     rmind 	uint64_t *stats = percpu_getref(npf_stats_percpu);
    294       1.2     rmind 	stats[st]++;
    295       1.2     rmind 	percpu_putref(npf_stats_percpu);
    296       1.2     rmind }
    297       1.2     rmind 
    298       1.2     rmind void
    299       1.2     rmind npf_stats_dec(npf_stats_t st)
    300       1.2     rmind {
    301       1.2     rmind 	uint64_t *stats = percpu_getref(npf_stats_percpu);
    302       1.2     rmind 	stats[st]--;
    303       1.2     rmind 	percpu_putref(npf_stats_percpu);
    304       1.2     rmind }
    305       1.2     rmind 
    306       1.2     rmind static void
    307       1.2     rmind npf_stats_collect(void *mem, void *arg, struct cpu_info *ci)
    308       1.2     rmind {
    309       1.2     rmind 	uint64_t *percpu_stats = mem, *full_stats = arg;
    310       1.2     rmind 	int i;
    311       1.2     rmind 
    312       1.2     rmind 	for (i = 0; i < NPF_STATS_COUNT; i++) {
    313       1.2     rmind 		full_stats[i] += percpu_stats[i];
    314       1.2     rmind 	}
    315       1.2     rmind }
    316       1.2     rmind 
    317       1.2     rmind /*
    318       1.2     rmind  * npfctl_stats: export collected statistics.
    319       1.2     rmind  */
    320       1.2     rmind static int
    321       1.2     rmind npfctl_stats(void *data)
    322       1.2     rmind {
    323       1.2     rmind 	uint64_t *fullst, *uptr = *(uint64_t **)data;
    324       1.2     rmind 	int error;
    325       1.2     rmind 
    326       1.2     rmind 	fullst = kmem_zalloc(NPF_STATS_SIZE, KM_SLEEP);
    327       1.2     rmind 	percpu_foreach(npf_stats_percpu, npf_stats_collect, fullst);
    328       1.2     rmind 	error = copyout(fullst, uptr, NPF_STATS_SIZE);
    329       1.2     rmind 	kmem_free(fullst, NPF_STATS_SIZE);
    330       1.2     rmind 	return error;
    331       1.2     rmind }
    332