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