Home | History | Annotate | Line # | Download | only in npf
npf.c revision 1.1.2.2
      1  1.1.2.2  yamt /*	$NetBSD: npf.c,v 1.1.2.2 2010/10/09 03:32:37 yamt Exp $	*/
      2  1.1.2.2  yamt 
      3  1.1.2.2  yamt /*-
      4  1.1.2.2  yamt  * Copyright (c) 2009-2010 The NetBSD Foundation, Inc.
      5  1.1.2.2  yamt  * All rights reserved.
      6  1.1.2.2  yamt  *
      7  1.1.2.2  yamt  * This material is based upon work partially supported by The
      8  1.1.2.2  yamt  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  1.1.2.2  yamt  *
     10  1.1.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.2  yamt  * modification, are permitted provided that the following conditions
     12  1.1.2.2  yamt  * are met:
     13  1.1.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.2  yamt  *
     19  1.1.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1.2.2  yamt  */
     31  1.1.2.2  yamt 
     32  1.1.2.2  yamt /*
     33  1.1.2.2  yamt  * NPF main: dynamic load/initialisation and unload routines.
     34  1.1.2.2  yamt  */
     35  1.1.2.2  yamt 
     36  1.1.2.2  yamt #include <sys/cdefs.h>
     37  1.1.2.2  yamt __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.1.2.2 2010/10/09 03:32:37 yamt Exp $");
     38  1.1.2.2  yamt 
     39  1.1.2.2  yamt #include <sys/param.h>
     40  1.1.2.2  yamt #include <sys/types.h>
     41  1.1.2.2  yamt 
     42  1.1.2.2  yamt #include <sys/conf.h>
     43  1.1.2.2  yamt #include <sys/kauth.h>
     44  1.1.2.2  yamt #include <sys/lwp.h>
     45  1.1.2.2  yamt #include <sys/module.h>
     46  1.1.2.2  yamt #include <sys/socketvar.h>
     47  1.1.2.2  yamt #include <sys/uio.h>
     48  1.1.2.2  yamt 
     49  1.1.2.2  yamt #include "npf_impl.h"
     50  1.1.2.2  yamt 
     51  1.1.2.2  yamt /*
     52  1.1.2.2  yamt  * Module and device structures.
     53  1.1.2.2  yamt  */
     54  1.1.2.2  yamt MODULE(MODULE_CLASS_MISC, npf, NULL);
     55  1.1.2.2  yamt 
     56  1.1.2.2  yamt void		npfattach(int);
     57  1.1.2.2  yamt 
     58  1.1.2.2  yamt static int	npf_dev_open(dev_t, int, int, lwp_t *);
     59  1.1.2.2  yamt static int	npf_dev_close(dev_t, int, int, lwp_t *);
     60  1.1.2.2  yamt static int	npf_dev_ioctl(dev_t, u_long, void *, int, lwp_t *);
     61  1.1.2.2  yamt static int	npf_dev_poll(dev_t, int, lwp_t *);
     62  1.1.2.2  yamt static int	npf_dev_read(dev_t, struct uio *, int);
     63  1.1.2.2  yamt 
     64  1.1.2.2  yamt const struct cdevsw npf_cdevsw = {
     65  1.1.2.2  yamt 	npf_dev_open, npf_dev_close, npf_dev_read, nowrite, npf_dev_ioctl,
     66  1.1.2.2  yamt 	nostop, notty, npf_dev_poll, nommap, nokqfilter, D_OTHER | D_MPSAFE
     67  1.1.2.2  yamt };
     68  1.1.2.2  yamt 
     69  1.1.2.2  yamt static int
     70  1.1.2.2  yamt npf_init(void)
     71  1.1.2.2  yamt {
     72  1.1.2.2  yamt #ifdef _MODULE
     73  1.1.2.2  yamt 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
     74  1.1.2.2  yamt #endif
     75  1.1.2.2  yamt 	int error;
     76  1.1.2.2  yamt 
     77  1.1.2.2  yamt 	/*
     78  1.1.2.2  yamt 	 * Initialise ruleset, tables and session structures.
     79  1.1.2.2  yamt 	 */
     80  1.1.2.2  yamt 
     81  1.1.2.2  yamt 	error = npf_ruleset_sysinit();
     82  1.1.2.2  yamt 	if (error)
     83  1.1.2.2  yamt 		return error;
     84  1.1.2.2  yamt 
     85  1.1.2.2  yamt 	error = npf_tableset_sysinit();
     86  1.1.2.2  yamt 	if (error) {
     87  1.1.2.2  yamt 		npf_ruleset_sysfini();
     88  1.1.2.2  yamt 		return error;
     89  1.1.2.2  yamt 	}
     90  1.1.2.2  yamt 
     91  1.1.2.2  yamt 	error = npf_session_sysinit();
     92  1.1.2.2  yamt 	if (error) {
     93  1.1.2.2  yamt 		npf_tableset_sysfini();
     94  1.1.2.2  yamt 		npf_ruleset_sysfini();
     95  1.1.2.2  yamt 		return error;
     96  1.1.2.2  yamt 	}
     97  1.1.2.2  yamt 	npf_nat_sysinit();
     98  1.1.2.2  yamt 	npf_alg_sysinit();
     99  1.1.2.2  yamt 
    100  1.1.2.2  yamt #ifdef _MODULE
    101  1.1.2.2  yamt 	/* Attach /dev/npf device. */
    102  1.1.2.2  yamt 	error = devsw_attach("npf", NULL, &bmajor, &npf_cdevsw, &cmajor);
    103  1.1.2.2  yamt 	if (error) {
    104  1.1.2.2  yamt 		npf_nat_sysfini();
    105  1.1.2.2  yamt 		npf_session_sysfini();
    106  1.1.2.2  yamt 		npf_tableset_sysfini();
    107  1.1.2.2  yamt 		npf_ruleset_sysfini();
    108  1.1.2.2  yamt 	}
    109  1.1.2.2  yamt #endif
    110  1.1.2.2  yamt 	return error;
    111  1.1.2.2  yamt }
    112  1.1.2.2  yamt 
    113  1.1.2.2  yamt static int
    114  1.1.2.2  yamt npf_fini(void)
    115  1.1.2.2  yamt {
    116  1.1.2.2  yamt 
    117  1.1.2.2  yamt #ifdef _MODULE
    118  1.1.2.2  yamt 	/* At first, detach device and remove pfil hooks. */
    119  1.1.2.2  yamt 	devsw_detach(NULL, &npf_cdevsw);
    120  1.1.2.2  yamt #endif
    121  1.1.2.2  yamt 	npf_nat_sysfini();
    122  1.1.2.2  yamt 	npf_alg_sysfini();
    123  1.1.2.2  yamt 	npf_session_sysfini();
    124  1.1.2.2  yamt 	npf_tableset_sysfini();
    125  1.1.2.2  yamt 	npf_ruleset_sysfini();
    126  1.1.2.2  yamt 
    127  1.1.2.2  yamt 	return 0;
    128  1.1.2.2  yamt }
    129  1.1.2.2  yamt 
    130  1.1.2.2  yamt /*
    131  1.1.2.2  yamt  * Module interface.
    132  1.1.2.2  yamt  */
    133  1.1.2.2  yamt static int
    134  1.1.2.2  yamt npf_modcmd(modcmd_t cmd, void *arg)
    135  1.1.2.2  yamt {
    136  1.1.2.2  yamt 
    137  1.1.2.2  yamt 	switch (cmd) {
    138  1.1.2.2  yamt 	case MODULE_CMD_INIT:
    139  1.1.2.2  yamt 		return npf_init();
    140  1.1.2.2  yamt 	case MODULE_CMD_FINI:
    141  1.1.2.2  yamt 		return npf_fini();
    142  1.1.2.2  yamt 	default:
    143  1.1.2.2  yamt 		return ENOTTY;
    144  1.1.2.2  yamt 	}
    145  1.1.2.2  yamt 	return 0;
    146  1.1.2.2  yamt }
    147  1.1.2.2  yamt 
    148  1.1.2.2  yamt void
    149  1.1.2.2  yamt npfattach(int nunits)
    150  1.1.2.2  yamt {
    151  1.1.2.2  yamt 
    152  1.1.2.2  yamt 	/* Void. */
    153  1.1.2.2  yamt }
    154  1.1.2.2  yamt 
    155  1.1.2.2  yamt static int
    156  1.1.2.2  yamt npf_dev_open(dev_t dev, int flag, int mode, lwp_t *l)
    157  1.1.2.2  yamt {
    158  1.1.2.2  yamt 
    159  1.1.2.2  yamt 	/* Available only for super-user. */
    160  1.1.2.2  yamt 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    161  1.1.2.2  yamt 		return EPERM;
    162  1.1.2.2  yamt 	}
    163  1.1.2.2  yamt 	return 0;
    164  1.1.2.2  yamt }
    165  1.1.2.2  yamt 
    166  1.1.2.2  yamt static int
    167  1.1.2.2  yamt npf_dev_close(dev_t dev, int flag, int mode, lwp_t *l)
    168  1.1.2.2  yamt {
    169  1.1.2.2  yamt 
    170  1.1.2.2  yamt 	return 0;
    171  1.1.2.2  yamt }
    172  1.1.2.2  yamt 
    173  1.1.2.2  yamt static int
    174  1.1.2.2  yamt npf_dev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    175  1.1.2.2  yamt {
    176  1.1.2.2  yamt 	int error;
    177  1.1.2.2  yamt 
    178  1.1.2.2  yamt 	/* Available only for super-user. */
    179  1.1.2.2  yamt 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    180  1.1.2.2  yamt 		return EPERM;
    181  1.1.2.2  yamt 	}
    182  1.1.2.2  yamt 
    183  1.1.2.2  yamt 	switch (cmd) {
    184  1.1.2.2  yamt 	case IOC_NPF_VERSION:
    185  1.1.2.2  yamt 		*(int *)data = NPF_VERSION;
    186  1.1.2.2  yamt 		error = 0;
    187  1.1.2.2  yamt 		break;
    188  1.1.2.2  yamt 	case IOC_NPF_SWITCH:
    189  1.1.2.2  yamt 		error = npfctl_switch(data);
    190  1.1.2.2  yamt 		break;
    191  1.1.2.2  yamt 	case IOC_NPF_RELOAD:
    192  1.1.2.2  yamt 		error = npfctl_reload(cmd, data);
    193  1.1.2.2  yamt 		break;
    194  1.1.2.2  yamt 	case IOC_NPF_TABLE:
    195  1.1.2.2  yamt 		error = npfctl_table(data);
    196  1.1.2.2  yamt 		break;
    197  1.1.2.2  yamt 	default:
    198  1.1.2.2  yamt 		error = ENOTTY;
    199  1.1.2.2  yamt 		break;
    200  1.1.2.2  yamt 	}
    201  1.1.2.2  yamt 	return error;
    202  1.1.2.2  yamt }
    203  1.1.2.2  yamt 
    204  1.1.2.2  yamt static int
    205  1.1.2.2  yamt npf_dev_poll(dev_t dev, int events, lwp_t *l)
    206  1.1.2.2  yamt {
    207  1.1.2.2  yamt 
    208  1.1.2.2  yamt 	return ENOTSUP;
    209  1.1.2.2  yamt }
    210  1.1.2.2  yamt 
    211  1.1.2.2  yamt static int
    212  1.1.2.2  yamt npf_dev_read(dev_t dev, struct uio *uio, int flag)
    213  1.1.2.2  yamt {
    214  1.1.2.2  yamt 
    215  1.1.2.2  yamt 	return ENOTSUP;
    216  1.1.2.2  yamt }
    217