Home | History | Annotate | Line # | Download | only in net
if_tap.c revision 1.106
      1  1.106   msaitoh /*	$NetBSD: if_tap.c,v 1.106 2018/06/26 06:48:02 msaitoh Exp $	*/
      2    1.1      cube 
      3    1.1      cube /*
      4   1.55        ad  *  Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation.
      5    1.1      cube  *  All rights reserved.
      6    1.1      cube  *
      7    1.1      cube  *  Redistribution and use in source and binary forms, with or without
      8    1.1      cube  *  modification, are permitted provided that the following conditions
      9    1.1      cube  *  are met:
     10    1.1      cube  *  1. Redistributions of source code must retain the above copyright
     11    1.1      cube  *     notice, this list of conditions and the following disclaimer.
     12    1.1      cube  *  2. Redistributions in binary form must reproduce the above copyright
     13    1.1      cube  *     notice, this list of conditions and the following disclaimer in the
     14    1.1      cube  *     documentation and/or other materials provided with the distribution.
     15    1.6     perry  *
     16    1.1      cube  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17    1.1      cube  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18    1.1      cube  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19    1.1      cube  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20    1.1      cube  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21    1.1      cube  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22    1.1      cube  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23    1.1      cube  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24    1.1      cube  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25    1.1      cube  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26    1.1      cube  *  POSSIBILITY OF SUCH DAMAGE.
     27    1.1      cube  */
     28    1.1      cube 
     29    1.1      cube /*
     30    1.1      cube  * tap(4) is a virtual Ethernet interface.  It appears as a real Ethernet
     31    1.1      cube  * device to the system, but can also be accessed by userland through a
     32    1.1      cube  * character device interface, which allows reading and injecting frames.
     33    1.1      cube  */
     34    1.1      cube 
     35    1.1      cube #include <sys/cdefs.h>
     36  1.106   msaitoh __KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.106 2018/06/26 06:48:02 msaitoh Exp $");
     37    1.1      cube 
     38    1.2      cube #if defined(_KERNEL_OPT)
     39   1.63     pooka 
     40   1.54    plunky #include "opt_modular.h"
     41   1.54    plunky #include "opt_compat_netbsd.h"
     42    1.2      cube #endif
     43    1.1      cube 
     44    1.1      cube #include <sys/param.h>
     45   1.96     skrll #include <sys/atomic.h>
     46    1.1      cube #include <sys/conf.h>
     47   1.70      yamt #include <sys/cprng.h>
     48    1.1      cube #include <sys/device.h>
     49    1.1      cube #include <sys/file.h>
     50    1.1      cube #include <sys/filedesc.h>
     51   1.96     skrll #include <sys/intr.h>
     52   1.96     skrll #include <sys/kauth.h>
     53   1.96     skrll #include <sys/kernel.h>
     54   1.98     skrll #include <sys/kmem.h>
     55   1.96     skrll #include <sys/module.h>
     56   1.96     skrll #include <sys/mutex.h>
     57  1.102  jmcneill #include <sys/condvar.h>
     58    1.1      cube #include <sys/poll.h>
     59   1.54    plunky #include <sys/proc.h>
     60    1.1      cube #include <sys/select.h>
     61    1.1      cube #include <sys/sockio.h>
     62   1.96     skrll #include <sys/stat.h>
     63    1.1      cube #include <sys/sysctl.h>
     64   1.96     skrll #include <sys/systm.h>
     65    1.1      cube 
     66    1.1      cube #include <net/if.h>
     67    1.1      cube #include <net/if_dl.h>
     68    1.1      cube #include <net/if_ether.h>
     69    1.1      cube #include <net/if_media.h>
     70    1.1      cube #include <net/if_tap.h>
     71    1.1      cube #include <net/bpf.h>
     72    1.1      cube 
     73   1.29  christos #include <compat/sys/sockio.h>
     74   1.29  christos 
     75   1.82  christos #include "ioconf.h"
     76   1.82  christos 
     77    1.1      cube /*
     78    1.1      cube  * sysctl node management
     79    1.1      cube  *
     80    1.1      cube  * It's not really possible to use a SYSCTL_SETUP block with
     81   1.51        ad  * current module implementation, so it is easier to just define
     82    1.1      cube  * our own function.
     83    1.1      cube  *
     84    1.1      cube  * The handler function is a "helper" in Andrew Brown's sysctl
     85    1.1      cube  * framework terminology.  It is used as a gateway for sysctl
     86    1.1      cube  * requests over the nodes.
     87    1.1      cube  *
     88    1.1      cube  * tap_log allows the module to log creations of nodes and
     89    1.1      cube  * destroy them all at once using sysctl_teardown.
     90    1.1      cube  */
     91   1.91  christos static int	tap_node;
     92    1.1      cube static int	tap_sysctl_handler(SYSCTLFN_PROTO);
     93   1.91  christos static void	sysctl_tap_setup(struct sysctllog **);
     94    1.1      cube 
     95    1.1      cube /*
     96   1.68       chs  * Since we're an Ethernet device, we need the 2 following
     97   1.68       chs  * components: a struct ethercom and a struct ifmedia
     98   1.68       chs  * since we don't attach a PHY to ourselves.
     99   1.68       chs  * We could emulate one, but there's no real point.
    100    1.1      cube  */
    101    1.1      cube 
    102    1.1      cube struct tap_softc {
    103   1.40      cube 	device_t	sc_dev;
    104    1.1      cube 	struct ifmedia	sc_im;
    105    1.1      cube 	struct ethercom	sc_ec;
    106    1.1      cube 	int		sc_flags;
    107    1.1      cube #define	TAP_INUSE	0x00000001	/* tap device can only be opened once */
    108    1.1      cube #define TAP_ASYNCIO	0x00000002	/* user is using async I/O (SIGIO) on the device */
    109    1.1      cube #define TAP_NBIO	0x00000004	/* user wants calls to avoid blocking */
    110    1.1      cube #define TAP_GOING	0x00000008	/* interface is being destroyed */
    111    1.1      cube 	struct selinfo	sc_rsel;
    112    1.1      cube 	pid_t		sc_pgid; /* For async. IO */
    113  1.102  jmcneill 	kmutex_t	sc_lock;
    114  1.102  jmcneill 	kcondvar_t	sc_cv;
    115   1.42        ad 	void		*sc_sih;
    116   1.56  christos 	struct timespec sc_atime;
    117   1.56  christos 	struct timespec sc_mtime;
    118   1.56  christos 	struct timespec sc_btime;
    119    1.1      cube };
    120    1.1      cube 
    121    1.1      cube /* autoconf(9) glue */
    122    1.1      cube 
    123   1.40      cube static int	tap_match(device_t, cfdata_t, void *);
    124   1.40      cube static void	tap_attach(device_t, device_t, void *);
    125   1.40      cube static int	tap_detach(device_t, int);
    126    1.1      cube 
    127   1.40      cube CFATTACH_DECL_NEW(tap, sizeof(struct tap_softc),
    128    1.1      cube     tap_match, tap_attach, tap_detach, NULL);
    129    1.1      cube extern struct cfdriver tap_cd;
    130    1.1      cube 
    131    1.1      cube /* Real device access routines */
    132    1.1      cube static int	tap_dev_close(struct tap_softc *);
    133    1.1      cube static int	tap_dev_read(int, struct uio *, int);
    134    1.1      cube static int	tap_dev_write(int, struct uio *, int);
    135   1.26  christos static int	tap_dev_ioctl(int, u_long, void *, struct lwp *);
    136   1.11  christos static int	tap_dev_poll(int, int, struct lwp *);
    137    1.1      cube static int	tap_dev_kqfilter(int, struct knote *);
    138    1.1      cube 
    139    1.1      cube /* Fileops access routines */
    140   1.41        ad static int	tap_fops_close(file_t *);
    141   1.41        ad static int	tap_fops_read(file_t *, off_t *, struct uio *,
    142   1.17      elad     kauth_cred_t, int);
    143   1.41        ad static int	tap_fops_write(file_t *, off_t *, struct uio *,
    144   1.17      elad     kauth_cred_t, int);
    145   1.41        ad static int	tap_fops_ioctl(file_t *, u_long, void *);
    146   1.41        ad static int	tap_fops_poll(file_t *, int);
    147   1.56  christos static int	tap_fops_stat(file_t *, struct stat *);
    148   1.41        ad static int	tap_fops_kqfilter(file_t *, struct knote *);
    149    1.1      cube 
    150    1.1      cube static const struct fileops tap_fileops = {
    151  1.104  christos 	.fo_name = "tap",
    152   1.55        ad 	.fo_read = tap_fops_read,
    153   1.55        ad 	.fo_write = tap_fops_write,
    154   1.55        ad 	.fo_ioctl = tap_fops_ioctl,
    155   1.55        ad 	.fo_fcntl = fnullop_fcntl,
    156   1.55        ad 	.fo_poll = tap_fops_poll,
    157   1.56  christos 	.fo_stat = tap_fops_stat,
    158   1.55        ad 	.fo_close = tap_fops_close,
    159   1.55        ad 	.fo_kqfilter = tap_fops_kqfilter,
    160   1.62       dsl 	.fo_restart = fnullop_restart,
    161    1.1      cube };
    162    1.1      cube 
    163    1.1      cube /* Helper for cloning open() */
    164   1.11  christos static int	tap_dev_cloner(struct lwp *);
    165    1.1      cube 
    166    1.1      cube /* Character device routines */
    167   1.11  christos static int	tap_cdev_open(dev_t, int, int, struct lwp *);
    168   1.11  christos static int	tap_cdev_close(dev_t, int, int, struct lwp *);
    169    1.1      cube static int	tap_cdev_read(dev_t, struct uio *, int);
    170    1.1      cube static int	tap_cdev_write(dev_t, struct uio *, int);
    171   1.26  christos static int	tap_cdev_ioctl(dev_t, u_long, void *, int, struct lwp *);
    172   1.11  christos static int	tap_cdev_poll(dev_t, int, struct lwp *);
    173    1.1      cube static int	tap_cdev_kqfilter(dev_t, struct knote *);
    174    1.1      cube 
    175    1.1      cube const struct cdevsw tap_cdevsw = {
    176   1.73  dholland 	.d_open = tap_cdev_open,
    177   1.73  dholland 	.d_close = tap_cdev_close,
    178   1.73  dholland 	.d_read = tap_cdev_read,
    179   1.73  dholland 	.d_write = tap_cdev_write,
    180   1.73  dholland 	.d_ioctl = tap_cdev_ioctl,
    181   1.73  dholland 	.d_stop = nostop,
    182   1.73  dholland 	.d_tty = notty,
    183   1.73  dholland 	.d_poll = tap_cdev_poll,
    184   1.73  dholland 	.d_mmap = nommap,
    185   1.73  dholland 	.d_kqfilter = tap_cdev_kqfilter,
    186   1.77  dholland 	.d_discard = nodiscard,
    187  1.102  jmcneill 	.d_flag = D_OTHER | D_MPSAFE
    188    1.1      cube };
    189    1.1      cube 
    190    1.1      cube #define TAP_CLONER	0xfffff		/* Maximal minor value */
    191    1.1      cube 
    192    1.1      cube /* kqueue-related routines */
    193    1.1      cube static void	tap_kqdetach(struct knote *);
    194    1.1      cube static int	tap_kqread(struct knote *, long);
    195    1.1      cube 
    196    1.1      cube /*
    197    1.1      cube  * Those are needed by the if_media interface.
    198    1.1      cube  */
    199    1.1      cube 
    200    1.1      cube static int	tap_mediachange(struct ifnet *);
    201    1.1      cube static void	tap_mediastatus(struct ifnet *, struct ifmediareq *);
    202    1.1      cube 
    203    1.1      cube /*
    204    1.1      cube  * Those are needed by the ifnet interface, and would typically be
    205    1.1      cube  * there for any network interface driver.
    206    1.1      cube  * Some other routines are optional: watchdog and drain.
    207    1.1      cube  */
    208    1.1      cube 
    209    1.1      cube static void	tap_start(struct ifnet *);
    210    1.1      cube static void	tap_stop(struct ifnet *, int);
    211    1.1      cube static int	tap_init(struct ifnet *);
    212   1.26  christos static int	tap_ioctl(struct ifnet *, u_long, void *);
    213    1.1      cube 
    214   1.42        ad /* Internal functions */
    215    1.1      cube static int	tap_lifaddr(struct ifnet *, u_long, struct ifaliasreq *);
    216   1.42        ad static void	tap_softintr(void *);
    217    1.1      cube 
    218    1.1      cube /*
    219    1.1      cube  * tap is a clonable interface, although it is highly unrealistic for
    220    1.1      cube  * an Ethernet device.
    221    1.1      cube  *
    222    1.1      cube  * Here are the bits needed for a clonable interface.
    223    1.1      cube  */
    224    1.1      cube static int	tap_clone_create(struct if_clone *, int);
    225    1.1      cube static int	tap_clone_destroy(struct ifnet *);
    226    1.1      cube 
    227    1.1      cube struct if_clone tap_cloners = IF_CLONE_INITIALIZER("tap",
    228    1.1      cube 					tap_clone_create,
    229    1.1      cube 					tap_clone_destroy);
    230    1.1      cube 
    231   1.97     skrll /* Helper functions shared by the two cloning code paths */
    232    1.1      cube static struct tap_softc *	tap_clone_creator(int);
    233   1.40      cube int	tap_clone_destroyer(device_t);
    234    1.1      cube 
    235   1.86  pgoyette static struct sysctllog *tap_sysctl_clog;
    236   1.87  pgoyette 
    237   1.91  christos #ifdef _MODULE
    238   1.87  pgoyette devmajor_t tap_bmajor = -1, tap_cmajor = -1;
    239   1.86  pgoyette #endif
    240   1.86  pgoyette 
    241   1.85  christos static u_int tap_count;
    242   1.85  christos 
    243    1.1      cube void
    244   1.23  christos tapattach(int n)
    245    1.1      cube {
    246    1.1      cube 
    247   1.85  christos 	/*
    248   1.85  christos 	 * Nothing to do here, initialization is handled by the
    249   1.85  christos 	 * module initialization code in tapinit() below).
    250   1.85  christos 	 */
    251   1.85  christos }
    252    1.1      cube 
    253   1.85  christos static void
    254   1.85  christos tapinit(void)
    255   1.85  christos {
    256   1.99     skrll 	int error = config_cfattach_attach(tap_cd.cd_name, &tap_ca);
    257   1.99     skrll 	if (error) {
    258   1.99     skrll 		aprint_error("%s: unable to register cfattach\n",
    259   1.99     skrll 		    tap_cd.cd_name);
    260   1.99     skrll 		(void)config_cfdriver_detach(&tap_cd);
    261   1.99     skrll 		return;
    262   1.99     skrll 	}
    263   1.95     skrll 
    264    1.1      cube 	if_clone_attach(&tap_cloners);
    265   1.91  christos 	sysctl_tap_setup(&tap_sysctl_clog);
    266   1.86  pgoyette #ifdef _MODULE
    267   1.89  pgoyette 	devsw_attach("tap", NULL, &tap_bmajor, &tap_cdevsw, &tap_cmajor);
    268   1.86  pgoyette #endif
    269    1.1      cube }
    270    1.1      cube 
    271   1.85  christos static int
    272   1.85  christos tapdetach(void)
    273   1.85  christos {
    274   1.85  christos 	int error = 0;
    275   1.85  christos 
    276   1.85  christos 	if (tap_count != 0)
    277   1.88  pgoyette 		return EBUSY;
    278   1.85  christos 
    279   1.88  pgoyette #ifdef _MODULE
    280   1.91  christos 	if (error == 0)
    281   1.91  christos 		error = devsw_detach(NULL, &tap_cdevsw);
    282   1.91  christos #endif
    283   1.85  christos 	if (error == 0)
    284   1.88  pgoyette 		sysctl_teardown(&tap_sysctl_clog);
    285   1.87  pgoyette 	if (error == 0)
    286   1.85  christos 		if_clone_detach(&tap_cloners);
    287   1.85  christos 
    288   1.91  christos 	if (error == 0)
    289   1.91  christos 		error = config_cfattach_detach(tap_cd.cd_name, &tap_ca);
    290   1.91  christos 
    291   1.85  christos 	return error;
    292   1.85  christos }
    293   1.85  christos 
    294    1.1      cube /* Pretty much useless for a pseudo-device */
    295    1.1      cube static int
    296   1.40      cube tap_match(device_t parent, cfdata_t cfdata, void *arg)
    297    1.1      cube {
    298   1.40      cube 
    299   1.95     skrll 	return 1;
    300    1.1      cube }
    301    1.1      cube 
    302    1.1      cube void
    303   1.40      cube tap_attach(device_t parent, device_t self, void *aux)
    304    1.1      cube {
    305   1.40      cube 	struct tap_softc *sc = device_private(self);
    306    1.1      cube 	struct ifnet *ifp;
    307   1.18    kardel 	const struct sysctlnode *node;
    308   1.54    plunky 	int error;
    309   1.38      matt 	uint8_t enaddr[ETHER_ADDR_LEN] =
    310    1.7      cube 	    { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
    311   1.14  christos 	char enaddrstr[3 * ETHER_ADDR_LEN];
    312    1.1      cube 
    313   1.40      cube 	sc->sc_dev = self;
    314   1.71      yamt 	sc->sc_sih = NULL;
    315   1.56  christos 	getnanotime(&sc->sc_btime);
    316   1.56  christos 	sc->sc_atime = sc->sc_mtime = sc->sc_btime;
    317   1.80     ozaki 	sc->sc_flags = 0;
    318   1.80     ozaki 	selinit(&sc->sc_rsel);
    319   1.80     ozaki 
    320  1.102  jmcneill 	cv_init(&sc->sc_cv, "tapread");
    321  1.102  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
    322   1.40      cube 
    323   1.48      hans 	if (!pmf_device_register(self, NULL, NULL))
    324   1.48      hans 		aprint_error_dev(self, "couldn't establish power handler\n");
    325   1.48      hans 
    326    1.1      cube 	/*
    327    1.1      cube 	 * In order to obtain unique initial Ethernet address on a host,
    328   1.70      yamt 	 * do some randomisation.  It's not meant for anything but avoiding
    329   1.70      yamt 	 * hard-coding an address.
    330    1.1      cube 	 */
    331   1.70      yamt 	cprng_fast(&enaddr[3], 3);
    332    1.1      cube 
    333   1.40      cube 	aprint_verbose_dev(self, "Ethernet address %s\n",
    334   1.14  christos 	    ether_snprintf(enaddrstr, sizeof(enaddrstr), enaddr));
    335    1.1      cube 
    336    1.1      cube 	/*
    337    1.1      cube 	 * Why 1000baseT? Why not? You can add more.
    338    1.1      cube 	 *
    339    1.1      cube 	 * Note that there are 3 steps: init, one or several additions to
    340    1.1      cube 	 * list of supported media, and in the end, the selection of one
    341    1.1      cube 	 * of them.
    342    1.1      cube 	 */
    343    1.1      cube 	ifmedia_init(&sc->sc_im, 0, tap_mediachange, tap_mediastatus);
    344    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T, 0, NULL);
    345    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
    346    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX, 0, NULL);
    347    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
    348    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T, 0, NULL);
    349    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
    350    1.1      cube 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
    351    1.1      cube 	ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
    352    1.1      cube 
    353    1.1      cube 	/*
    354    1.1      cube 	 * One should note that an interface must do multicast in order
    355    1.1      cube 	 * to support IPv6.
    356    1.1      cube 	 */
    357    1.1      cube 	ifp = &sc->sc_ec.ec_if;
    358   1.40      cube 	strcpy(ifp->if_xname, device_xname(self));
    359    1.1      cube 	ifp->if_softc	= sc;
    360    1.1      cube 	ifp->if_flags	= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    361  1.105     ozaki 	ifp->if_extflags = IFEF_NO_LINK_STATE_CHANGE;
    362  1.105     ozaki #ifdef NET_MPSAFE
    363  1.105     ozaki 	ifp->if_extflags |= IFEF_MPSAFE;
    364  1.105     ozaki #endif
    365    1.1      cube 	ifp->if_ioctl	= tap_ioctl;
    366    1.1      cube 	ifp->if_start	= tap_start;
    367    1.1      cube 	ifp->if_stop	= tap_stop;
    368    1.1      cube 	ifp->if_init	= tap_init;
    369    1.1      cube 	IFQ_SET_READY(&ifp->if_snd);
    370    1.1      cube 
    371    1.1      cube 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
    372    1.1      cube 
    373   1.81     ozaki 	/* Those steps are mandatory for an Ethernet driver. */
    374  1.100   msaitoh 	error = if_initialize(ifp);
    375  1.100   msaitoh 	if (error != 0) {
    376  1.100   msaitoh 		aprint_error_dev(self, "if_initialize failed(%d)\n", error);
    377  1.100   msaitoh 		ifmedia_removeall(&sc->sc_im);
    378  1.100   msaitoh 		pmf_device_deregister(self);
    379  1.102  jmcneill 		mutex_destroy(&sc->sc_lock);
    380  1.100   msaitoh 		seldestroy(&sc->sc_rsel);
    381  1.100   msaitoh 
    382  1.100   msaitoh 		return; /* Error */
    383  1.100   msaitoh 	}
    384  1.102  jmcneill 	ifp->if_percpuq = if_percpuq_create(ifp);
    385    1.1      cube 	ether_ifattach(ifp, enaddr);
    386   1.81     ozaki 	if_register(ifp);
    387    1.1      cube 
    388    1.1      cube 	/*
    389    1.1      cube 	 * Add a sysctl node for that interface.
    390    1.1      cube 	 *
    391    1.1      cube 	 * The pointer transmitted is not a string, but instead a pointer to
    392    1.1      cube 	 * the softc structure, which we can use to build the string value on
    393    1.1      cube 	 * the fly in the helper function of the node.  See the comments for
    394    1.1      cube 	 * tap_sysctl_handler for details.
    395   1.21      cube 	 *
    396   1.21      cube 	 * Usually sysctl_createv is called with CTL_CREATE as the before-last
    397   1.21      cube 	 * component.  However, we can allocate a number ourselves, as we are
    398   1.21      cube 	 * the only consumer of the net.link.<iface> node.  In this case, the
    399   1.21      cube 	 * unit number is conveniently used to number the node.  CTL_CREATE
    400   1.21      cube 	 * would just work, too.
    401    1.1      cube 	 */
    402    1.1      cube 	if ((error = sysctl_createv(NULL, 0, NULL,
    403    1.1      cube 	    &node, CTLFLAG_READWRITE,
    404   1.40      cube 	    CTLTYPE_STRING, device_xname(self), NULL,
    405   1.67       dsl 	    tap_sysctl_handler, 0, (void *)sc, 18,
    406   1.40      cube 	    CTL_NET, AF_LINK, tap_node, device_unit(sc->sc_dev),
    407   1.15   thorpej 	    CTL_EOL)) != 0)
    408  1.100   msaitoh 		aprint_error_dev(self,
    409  1.100   msaitoh 		    "sysctl_createv returned %d, ignoring\n", error);
    410    1.1      cube }
    411    1.1      cube 
    412    1.1      cube /*
    413    1.1      cube  * When detaching, we do the inverse of what is done in the attach
    414    1.1      cube  * routine, in reversed order.
    415    1.1      cube  */
    416    1.1      cube static int
    417   1.40      cube tap_detach(device_t self, int flags)
    418    1.1      cube {
    419   1.40      cube 	struct tap_softc *sc = device_private(self);
    420    1.1      cube 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    421   1.54    plunky 	int error;
    422    1.1      cube 
    423    1.1      cube 	sc->sc_flags |= TAP_GOING;
    424    1.1      cube 	tap_stop(ifp, 1);
    425    1.1      cube 	if_down(ifp);
    426    1.1      cube 
    427   1.71      yamt 	if (sc->sc_sih != NULL) {
    428   1.71      yamt 		softint_disestablish(sc->sc_sih);
    429   1.71      yamt 		sc->sc_sih = NULL;
    430   1.71      yamt 	}
    431   1.42        ad 
    432    1.1      cube 	/*
    433    1.1      cube 	 * Destroying a single leaf is a very straightforward operation using
    434    1.1      cube 	 * sysctl_destroyv.  One should be sure to always end the path with
    435    1.1      cube 	 * CTL_EOL.
    436    1.1      cube 	 */
    437    1.3      cube 	if ((error = sysctl_destroyv(NULL, CTL_NET, AF_LINK, tap_node,
    438   1.40      cube 	    device_unit(sc->sc_dev), CTL_EOL)) != 0)
    439   1.40      cube 		aprint_error_dev(self,
    440   1.40      cube 		    "sysctl_destroyv returned %d, ignoring\n", error);
    441    1.1      cube 	ether_ifdetach(ifp);
    442    1.1      cube 	if_detach(ifp);
    443  1.100   msaitoh 	ifmedia_removeall(&sc->sc_im);
    444   1.47     rmind 	seldestroy(&sc->sc_rsel);
    445  1.102  jmcneill 	mutex_destroy(&sc->sc_lock);
    446  1.102  jmcneill 	cv_destroy(&sc->sc_cv);
    447    1.1      cube 
    448   1.49      hans 	pmf_device_deregister(self);
    449   1.49      hans 
    450   1.95     skrll 	return 0;
    451    1.1      cube }
    452    1.1      cube 
    453    1.1      cube /*
    454    1.1      cube  * This function is called by the ifmedia layer to notify the driver
    455    1.1      cube  * that the user requested a media change.  A real driver would
    456    1.1      cube  * reconfigure the hardware.
    457    1.1      cube  */
    458    1.1      cube static int
    459   1.23  christos tap_mediachange(struct ifnet *ifp)
    460    1.1      cube {
    461   1.95     skrll 	return 0;
    462    1.1      cube }
    463    1.1      cube 
    464    1.1      cube /*
    465    1.1      cube  * Here the user asks for the currently used media.
    466    1.1      cube  */
    467    1.1      cube static void
    468    1.1      cube tap_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
    469    1.1      cube {
    470    1.1      cube 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
    471    1.1      cube 	imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
    472    1.1      cube }
    473    1.1      cube 
    474    1.1      cube /*
    475    1.1      cube  * This is the function where we SEND packets.
    476    1.1      cube  *
    477    1.1      cube  * There is no 'receive' equivalent.  A typical driver will get
    478    1.1      cube  * interrupts from the hardware, and from there will inject new packets
    479    1.1      cube  * into the network stack.
    480    1.1      cube  *
    481    1.1      cube  * Once handled, a packet must be freed.  A real driver might not be able
    482    1.1      cube  * to fit all the pending packets into the hardware, and is allowed to
    483    1.1      cube  * return before having sent all the packets.  It should then use the
    484    1.1      cube  * if_flags flag IFF_OACTIVE to notify the upper layer.
    485    1.1      cube  *
    486    1.1      cube  * There are also other flags one should check, such as IFF_PAUSE.
    487    1.1      cube  *
    488    1.1      cube  * It is our duty to make packets available to BPF listeners.
    489    1.1      cube  *
    490    1.1      cube  * You should be aware that this function is called by the Ethernet layer
    491    1.1      cube  * at splnet().
    492    1.1      cube  *
    493    1.1      cube  * When the device is opened, we have to pass the packet(s) to the
    494    1.1      cube  * userland.  For that we stay in OACTIVE mode while the userland gets
    495    1.1      cube  * the packets, and we send a signal to the processes waiting to read.
    496    1.1      cube  *
    497    1.1      cube  * wakeup(sc) is the counterpart to the tsleep call in
    498    1.1      cube  * tap_dev_read, while selnotify() is used for kevent(2) and
    499    1.1      cube  * poll(2) (which includes select(2)) listeners.
    500    1.1      cube  */
    501    1.1      cube static void
    502    1.1      cube tap_start(struct ifnet *ifp)
    503    1.1      cube {
    504    1.1      cube 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
    505    1.1      cube 	struct mbuf *m0;
    506    1.1      cube 
    507  1.102  jmcneill 	mutex_enter(&sc->sc_lock);
    508    1.1      cube 	if ((sc->sc_flags & TAP_INUSE) == 0) {
    509    1.1      cube 		/* Simply drop packets */
    510    1.1      cube 		for(;;) {
    511    1.1      cube 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    512    1.1      cube 			if (m0 == NULL)
    513  1.102  jmcneill 				goto done;
    514    1.1      cube 
    515    1.1      cube 			ifp->if_opackets++;
    516  1.106   msaitoh 			bpf_mtap(ifp, m0, BPF_D_OUT);
    517    1.1      cube 
    518    1.1      cube 			m_freem(m0);
    519    1.1      cube 		}
    520    1.1      cube 	} else if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
    521    1.1      cube 		ifp->if_flags |= IFF_OACTIVE;
    522  1.102  jmcneill 		cv_broadcast(&sc->sc_cv);
    523   1.39     rmind 		selnotify(&sc->sc_rsel, 0, 1);
    524    1.1      cube 		if (sc->sc_flags & TAP_ASYNCIO)
    525   1.42        ad 			softint_schedule(sc->sc_sih);
    526   1.42        ad 	}
    527  1.102  jmcneill done:
    528  1.102  jmcneill 	mutex_exit(&sc->sc_lock);
    529   1.42        ad }
    530   1.42        ad 
    531   1.42        ad static void
    532   1.42        ad tap_softintr(void *cookie)
    533   1.42        ad {
    534   1.42        ad 	struct tap_softc *sc;
    535   1.42        ad 	struct ifnet *ifp;
    536   1.42        ad 	int a, b;
    537   1.42        ad 
    538   1.42        ad 	sc = cookie;
    539   1.42        ad 
    540   1.42        ad 	if (sc->sc_flags & TAP_ASYNCIO) {
    541   1.42        ad 		ifp = &sc->sc_ec.ec_if;
    542   1.42        ad 		if (ifp->if_flags & IFF_RUNNING) {
    543   1.42        ad 			a = POLL_IN;
    544   1.42        ad 			b = POLLIN|POLLRDNORM;
    545   1.42        ad 		} else {
    546   1.42        ad 			a = POLL_HUP;
    547   1.42        ad 			b = 0;
    548   1.42        ad 		}
    549   1.42        ad 		fownsignal(sc->sc_pgid, SIGIO, a, b, NULL);
    550    1.1      cube 	}
    551    1.1      cube }
    552    1.1      cube 
    553    1.1      cube /*
    554    1.1      cube  * A typical driver will only contain the following handlers for
    555    1.1      cube  * ioctl calls, except SIOCSIFPHYADDR.
    556    1.1      cube  * The latter is a hack I used to set the Ethernet address of the
    557    1.1      cube  * faked device.
    558    1.1      cube  *
    559    1.1      cube  * Note that both ifmedia_ioctl() and ether_ioctl() have to be
    560    1.1      cube  * called under splnet().
    561    1.1      cube  */
    562    1.1      cube static int
    563   1.26  christos tap_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    564    1.1      cube {
    565    1.1      cube 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
    566    1.1      cube 	struct ifreq *ifr = (struct ifreq *)data;
    567    1.1      cube 	int s, error;
    568    1.1      cube 
    569    1.1      cube 	s = splnet();
    570    1.1      cube 
    571    1.1      cube 	switch (cmd) {
    572   1.29  christos #ifdef OSIOCSIFMEDIA
    573   1.29  christos 	case OSIOCSIFMEDIA:
    574   1.29  christos #endif
    575    1.1      cube 	case SIOCSIFMEDIA:
    576    1.1      cube 	case SIOCGIFMEDIA:
    577    1.1      cube 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
    578    1.1      cube 		break;
    579    1.1      cube 	case SIOCSIFPHYADDR:
    580    1.1      cube 		error = tap_lifaddr(ifp, cmd, (struct ifaliasreq *)data);
    581    1.1      cube 		break;
    582    1.1      cube 	default:
    583    1.1      cube 		error = ether_ioctl(ifp, cmd, data);
    584    1.1      cube 		if (error == ENETRESET)
    585    1.1      cube 			error = 0;
    586    1.1      cube 		break;
    587    1.1      cube 	}
    588    1.1      cube 
    589    1.1      cube 	splx(s);
    590    1.1      cube 
    591   1.95     skrll 	return error;
    592    1.1      cube }
    593    1.1      cube 
    594    1.1      cube /*
    595   1.54    plunky  * Helper function to set Ethernet address.  This has been replaced by
    596   1.54    plunky  * the generic SIOCALIFADDR ioctl on a PF_LINK socket.
    597    1.1      cube  */
    598    1.1      cube static int
    599   1.23  christos tap_lifaddr(struct ifnet *ifp, u_long cmd, struct ifaliasreq *ifra)
    600    1.1      cube {
    601   1.53    plunky 	const struct sockaddr *sa = &ifra->ifra_addr;
    602    1.1      cube 
    603   1.53    plunky 	if (sa->sa_family != AF_LINK)
    604   1.95     skrll 		return EINVAL;
    605    1.1      cube 
    606   1.53    plunky 	if_set_sadl(ifp, sa->sa_data, ETHER_ADDR_LEN, false);
    607    1.1      cube 
    608   1.95     skrll 	return 0;
    609    1.1      cube }
    610    1.1      cube 
    611    1.1      cube /*
    612    1.1      cube  * _init() would typically be called when an interface goes up,
    613    1.1      cube  * meaning it should configure itself into the state in which it
    614    1.1      cube  * can send packets.
    615    1.1      cube  */
    616    1.1      cube static int
    617    1.1      cube tap_init(struct ifnet *ifp)
    618    1.1      cube {
    619    1.1      cube 	ifp->if_flags |= IFF_RUNNING;
    620    1.1      cube 
    621    1.1      cube 	tap_start(ifp);
    622    1.1      cube 
    623   1.95     skrll 	return 0;
    624    1.1      cube }
    625    1.1      cube 
    626    1.1      cube /*
    627    1.1      cube  * _stop() is called when an interface goes down.  It is our
    628    1.1      cube  * responsability to validate that state by clearing the
    629    1.1      cube  * IFF_RUNNING flag.
    630    1.1      cube  *
    631    1.1      cube  * We have to wake up all the sleeping processes to have the pending
    632    1.1      cube  * read requests cancelled.
    633    1.1      cube  */
    634    1.1      cube static void
    635   1.23  christos tap_stop(struct ifnet *ifp, int disable)
    636    1.1      cube {
    637    1.1      cube 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
    638    1.1      cube 
    639  1.102  jmcneill 	mutex_enter(&sc->sc_lock);
    640    1.1      cube 	ifp->if_flags &= ~IFF_RUNNING;
    641  1.102  jmcneill 	cv_broadcast(&sc->sc_cv);
    642   1.39     rmind 	selnotify(&sc->sc_rsel, 0, 1);
    643    1.1      cube 	if (sc->sc_flags & TAP_ASYNCIO)
    644   1.42        ad 		softint_schedule(sc->sc_sih);
    645  1.102  jmcneill 	mutex_exit(&sc->sc_lock);
    646    1.1      cube }
    647    1.1      cube 
    648    1.1      cube /*
    649    1.1      cube  * The 'create' command of ifconfig can be used to create
    650    1.1      cube  * any numbered instance of a given device.  Thus we have to
    651    1.1      cube  * make sure we have enough room in cd_devs to create the
    652    1.1      cube  * user-specified instance.  config_attach_pseudo will do this
    653    1.1      cube  * for us.
    654    1.1      cube  */
    655    1.1      cube static int
    656   1.23  christos tap_clone_create(struct if_clone *ifc, int unit)
    657    1.1      cube {
    658    1.1      cube 	if (tap_clone_creator(unit) == NULL) {
    659    1.1      cube 		aprint_error("%s%d: unable to attach an instance\n",
    660   1.99     skrll 		    tap_cd.cd_name, unit);
    661   1.95     skrll 		return ENXIO;
    662    1.1      cube 	}
    663   1.85  christos 	atomic_inc_uint(&tap_count);
    664   1.95     skrll 	return 0;
    665    1.1      cube }
    666    1.1      cube 
    667    1.1      cube /*
    668    1.1      cube  * tap(4) can be cloned by two ways:
    669    1.1      cube  *   using 'ifconfig tap0 create', which will use the network
    670    1.1      cube  *     interface cloning API, and call tap_clone_create above.
    671    1.1      cube  *   opening the cloning device node, whose minor number is TAP_CLONER.
    672    1.1      cube  *     See below for an explanation on how this part work.
    673    1.1      cube  */
    674    1.1      cube static struct tap_softc *
    675    1.1      cube tap_clone_creator(int unit)
    676    1.1      cube {
    677   1.98     skrll 	cfdata_t cf;
    678    1.1      cube 
    679   1.98     skrll 	cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
    680    1.1      cube 	cf->cf_name = tap_cd.cd_name;
    681    1.1      cube 	cf->cf_atname = tap_ca.ca_name;
    682   1.27  drochner 	if (unit == -1) {
    683   1.27  drochner 		/* let autoconf find the first free one */
    684   1.27  drochner 		cf->cf_unit = 0;
    685   1.27  drochner 		cf->cf_fstate = FSTATE_STAR;
    686   1.27  drochner 	} else {
    687   1.27  drochner 		cf->cf_unit = unit;
    688   1.58    plunky 		cf->cf_fstate = FSTATE_NOTFOUND;
    689   1.27  drochner 	}
    690    1.1      cube 
    691   1.40      cube 	return device_private(config_attach_pseudo(cf));
    692    1.1      cube }
    693    1.1      cube 
    694    1.1      cube /*
    695    1.1      cube  * The clean design of if_clone and autoconf(9) makes that part
    696    1.1      cube  * really straightforward.  The second argument of config_detach
    697    1.1      cube  * means neither QUIET nor FORCED.
    698    1.1      cube  */
    699    1.1      cube static int
    700    1.1      cube tap_clone_destroy(struct ifnet *ifp)
    701    1.1      cube {
    702   1.45    dyoung 	struct tap_softc *sc = ifp->if_softc;
    703   1.85  christos 	int error = tap_clone_destroyer(sc->sc_dev);
    704   1.45    dyoung 
    705   1.85  christos 	if (error == 0)
    706   1.90       kre 		atomic_dec_uint(&tap_count);
    707   1.85  christos 	return error;
    708    1.1      cube }
    709    1.1      cube 
    710   1.12      cube int
    711   1.40      cube tap_clone_destroyer(device_t dev)
    712    1.1      cube {
    713   1.40      cube 	cfdata_t cf = device_cfdata(dev);
    714    1.1      cube 	int error;
    715    1.1      cube 
    716    1.1      cube 	if ((error = config_detach(dev, 0)) != 0)
    717   1.40      cube 		aprint_error_dev(dev, "unable to detach instance\n");
    718   1.98     skrll 	kmem_free(cf, sizeof(*cf));
    719    1.1      cube 
    720   1.95     skrll 	return error;
    721    1.1      cube }
    722    1.1      cube 
    723    1.1      cube /*
    724    1.1      cube  * tap(4) is a bit of an hybrid device.  It can be used in two different
    725    1.1      cube  * ways:
    726    1.1      cube  *  1. ifconfig tapN create, then use /dev/tapN to read/write off it.
    727    1.1      cube  *  2. open /dev/tap, get a new interface created and read/write off it.
    728    1.1      cube  *     That interface is destroyed when the process that had it created exits.
    729    1.1      cube  *
    730    1.1      cube  * The first way is managed by the cdevsw structure, and you access interfaces
    731    1.1      cube  * through a (major, minor) mapping:  tap4 is obtained by the minor number
    732    1.1      cube  * 4.  The entry points for the cdevsw interface are prefixed by tap_cdev_.
    733    1.1      cube  *
    734    1.1      cube  * The second way is the so-called "cloning" device.  It's a special minor
    735    1.1      cube  * number (chosen as the maximal number, to allow as much tap devices as
    736    1.1      cube  * possible).  The user first opens the cloner (e.g., /dev/tap), and that
    737    1.1      cube  * call ends in tap_cdev_open.  The actual place where it is handled is
    738    1.1      cube  * tap_dev_cloner.
    739    1.1      cube  *
    740    1.1      cube  * An tap device cannot be opened more than once at a time, so the cdevsw
    741    1.1      cube  * part of open() does nothing but noting that the interface is being used and
    742    1.1      cube  * hence ready to actually handle packets.
    743    1.1      cube  */
    744    1.1      cube 
    745    1.1      cube static int
    746   1.23  christos tap_cdev_open(dev_t dev, int flags, int fmt, struct lwp *l)
    747    1.1      cube {
    748    1.1      cube 	struct tap_softc *sc;
    749    1.1      cube 
    750    1.1      cube 	if (minor(dev) == TAP_CLONER)
    751   1.11  christos 		return tap_dev_cloner(l);
    752    1.1      cube 
    753   1.46    cegger 	sc = device_lookup_private(&tap_cd, minor(dev));
    754    1.1      cube 	if (sc == NULL)
    755   1.95     skrll 		return ENXIO;
    756    1.1      cube 
    757    1.1      cube 	/* The device can only be opened once */
    758    1.1      cube 	if (sc->sc_flags & TAP_INUSE)
    759   1.95     skrll 		return EBUSY;
    760    1.1      cube 	sc->sc_flags |= TAP_INUSE;
    761   1.95     skrll 	return 0;
    762    1.1      cube }
    763    1.1      cube 
    764    1.1      cube /*
    765    1.1      cube  * There are several kinds of cloning devices, and the most simple is the one
    766    1.1      cube  * tap(4) uses.  What it does is change the file descriptor with a new one,
    767    1.1      cube  * with its own fileops structure (which maps to the various read, write,
    768    1.1      cube  * ioctl functions).  It starts allocating a new file descriptor with falloc,
    769    1.1      cube  * then actually creates the new tap devices.
    770    1.1      cube  *
    771    1.1      cube  * Once those two steps are successful, we can re-wire the existing file
    772    1.1      cube  * descriptor to its new self.  This is done with fdclone():  it fills the fp
    773   1.78      matt  * structure as needed (notably f_devunit gets filled with the fifth parameter
    774    1.1      cube  * passed, the unit of the tap device which will allows us identifying the
    775    1.1      cube  * device later), and returns EMOVEFD.
    776    1.1      cube  *
    777    1.1      cube  * That magic value is interpreted by sys_open() which then replaces the
    778    1.1      cube  * current file descriptor by the new one (through a magic member of struct
    779   1.13     pooka  * lwp, l_dupfd).
    780    1.1      cube  *
    781    1.1      cube  * The tap device is flagged as being busy since it otherwise could be
    782    1.1      cube  * externally accessed through the corresponding device node with the cdevsw
    783    1.1      cube  * interface.
    784    1.1      cube  */
    785    1.1      cube 
    786    1.1      cube static int
    787   1.11  christos tap_dev_cloner(struct lwp *l)
    788    1.1      cube {
    789    1.1      cube 	struct tap_softc *sc;
    790   1.41        ad 	file_t *fp;
    791    1.1      cube 	int error, fd;
    792    1.1      cube 
    793   1.41        ad 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    794   1.95     skrll 		return error;
    795    1.1      cube 
    796   1.27  drochner 	if ((sc = tap_clone_creator(-1)) == NULL) {
    797   1.41        ad 		fd_abort(curproc, fp, fd);
    798   1.95     skrll 		return ENXIO;
    799    1.1      cube 	}
    800    1.1      cube 
    801    1.1      cube 	sc->sc_flags |= TAP_INUSE;
    802    1.1      cube 
    803   1.41        ad 	return fd_clone(fp, fd, FREAD|FWRITE, &tap_fileops,
    804   1.40      cube 	    (void *)(intptr_t)device_unit(sc->sc_dev));
    805    1.1      cube }
    806    1.1      cube 
    807    1.1      cube /*
    808    1.1      cube  * While all other operations (read, write, ioctl, poll and kqfilter) are
    809    1.1      cube  * really the same whether we are in cdevsw or fileops mode, the close()
    810    1.1      cube  * function is slightly different in the two cases.
    811    1.1      cube  *
    812    1.1      cube  * As for the other, the core of it is shared in tap_dev_close.  What
    813    1.1      cube  * it does is sufficient for the cdevsw interface, but the cloning interface
    814    1.1      cube  * needs another thing:  the interface is destroyed when the processes that
    815    1.1      cube  * created it closes it.
    816    1.1      cube  */
    817    1.1      cube static int
    818   1.23  christos tap_cdev_close(dev_t dev, int flags, int fmt,
    819   1.23  christos     struct lwp *l)
    820    1.1      cube {
    821    1.1      cube 	struct tap_softc *sc =
    822   1.46    cegger 	    device_lookup_private(&tap_cd, minor(dev));
    823    1.1      cube 
    824    1.1      cube 	if (sc == NULL)
    825   1.95     skrll 		return ENXIO;
    826    1.1      cube 
    827    1.1      cube 	return tap_dev_close(sc);
    828    1.1      cube }
    829    1.1      cube 
    830    1.1      cube /*
    831    1.1      cube  * It might happen that the administrator used ifconfig to externally destroy
    832    1.1      cube  * the interface.  In that case, tap_fops_close will be called while
    833    1.1      cube  * tap_detach is already happening.  If we called it again from here, we
    834    1.1      cube  * would dead lock.  TAP_GOING ensures that this situation doesn't happen.
    835    1.1      cube  */
    836    1.1      cube static int
    837   1.41        ad tap_fops_close(file_t *fp)
    838    1.1      cube {
    839   1.78      matt 	int unit = fp->f_devunit;
    840    1.1      cube 	struct tap_softc *sc;
    841    1.1      cube 	int error;
    842    1.1      cube 
    843   1.46    cegger 	sc = device_lookup_private(&tap_cd, unit);
    844    1.1      cube 	if (sc == NULL)
    845   1.95     skrll 		return ENXIO;
    846    1.1      cube 
    847    1.1      cube 	/* tap_dev_close currently always succeeds, but it might not
    848    1.1      cube 	 * always be the case. */
    849   1.44        ad 	KERNEL_LOCK(1, NULL);
    850   1.44        ad 	if ((error = tap_dev_close(sc)) != 0) {
    851   1.44        ad 		KERNEL_UNLOCK_ONE(NULL);
    852   1.95     skrll 		return error;
    853   1.44        ad 	}
    854    1.1      cube 
    855    1.1      cube 	/* Destroy the device now that it is no longer useful,
    856    1.1      cube 	 * unless it's already being destroyed. */
    857   1.44        ad 	if ((sc->sc_flags & TAP_GOING) != 0) {
    858   1.44        ad 		KERNEL_UNLOCK_ONE(NULL);
    859   1.95     skrll 		return 0;
    860   1.44        ad 	}
    861    1.1      cube 
    862   1.44        ad 	error = tap_clone_destroyer(sc->sc_dev);
    863   1.44        ad 	KERNEL_UNLOCK_ONE(NULL);
    864   1.44        ad 	return error;
    865    1.1      cube }
    866    1.1      cube 
    867    1.1      cube static int
    868    1.1      cube tap_dev_close(struct tap_softc *sc)
    869    1.1      cube {
    870    1.1      cube 	struct ifnet *ifp;
    871    1.1      cube 	int s;
    872    1.1      cube 
    873    1.1      cube 	s = splnet();
    874    1.1      cube 	/* Let tap_start handle packets again */
    875    1.1      cube 	ifp = &sc->sc_ec.ec_if;
    876    1.1      cube 	ifp->if_flags &= ~IFF_OACTIVE;
    877    1.1      cube 
    878    1.1      cube 	/* Purge output queue */
    879    1.1      cube 	if (!(IFQ_IS_EMPTY(&ifp->if_snd))) {
    880    1.1      cube 		struct mbuf *m;
    881    1.1      cube 
    882    1.1      cube 		for (;;) {
    883    1.1      cube 			IFQ_DEQUEUE(&ifp->if_snd, m);
    884    1.1      cube 			if (m == NULL)
    885    1.1      cube 				break;
    886    1.1      cube 
    887    1.1      cube 			ifp->if_opackets++;
    888  1.106   msaitoh 			bpf_mtap(ifp, m, BPF_D_OUT);
    889   1.60    plunky 			m_freem(m);
    890    1.1      cube 		}
    891    1.1      cube 	}
    892    1.1      cube 	splx(s);
    893    1.1      cube 
    894   1.71      yamt 	if (sc->sc_sih != NULL) {
    895   1.71      yamt 		softint_disestablish(sc->sc_sih);
    896   1.71      yamt 		sc->sc_sih = NULL;
    897   1.71      yamt 	}
    898    1.1      cube 	sc->sc_flags &= ~(TAP_INUSE | TAP_ASYNCIO);
    899    1.1      cube 
    900   1.95     skrll 	return 0;
    901    1.1      cube }
    902    1.1      cube 
    903    1.1      cube static int
    904    1.1      cube tap_cdev_read(dev_t dev, struct uio *uio, int flags)
    905    1.1      cube {
    906    1.1      cube 	return tap_dev_read(minor(dev), uio, flags);
    907    1.1      cube }
    908    1.1      cube 
    909    1.1      cube static int
    910   1.41        ad tap_fops_read(file_t *fp, off_t *offp, struct uio *uio,
    911   1.23  christos     kauth_cred_t cred, int flags)
    912    1.1      cube {
    913   1.44        ad 	int error;
    914   1.44        ad 
    915   1.44        ad 	KERNEL_LOCK(1, NULL);
    916   1.78      matt 	error = tap_dev_read(fp->f_devunit, uio, flags);
    917   1.44        ad 	KERNEL_UNLOCK_ONE(NULL);
    918   1.44        ad 	return error;
    919    1.1      cube }
    920    1.1      cube 
    921    1.1      cube static int
    922   1.23  christos tap_dev_read(int unit, struct uio *uio, int flags)
    923    1.1      cube {
    924   1.78      matt 	struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
    925    1.1      cube 	struct ifnet *ifp;
    926    1.1      cube 	struct mbuf *m, *n;
    927  1.102  jmcneill 	int error = 0;
    928    1.1      cube 
    929    1.1      cube 	if (sc == NULL)
    930   1.95     skrll 		return ENXIO;
    931    1.1      cube 
    932   1.56  christos 	getnanotime(&sc->sc_atime);
    933   1.56  christos 
    934    1.1      cube 	ifp = &sc->sc_ec.ec_if;
    935    1.1      cube 	if ((ifp->if_flags & IFF_UP) == 0)
    936   1.95     skrll 		return EHOSTDOWN;
    937    1.1      cube 
    938    1.1      cube 	/*
    939    1.1      cube 	 * In the TAP_NBIO case, we have to make sure we won't be sleeping
    940    1.1      cube 	 */
    941   1.34        ad 	if ((sc->sc_flags & TAP_NBIO) != 0) {
    942  1.102  jmcneill 		if (!mutex_tryenter(&sc->sc_lock))
    943   1.95     skrll 			return EWOULDBLOCK;
    944   1.34        ad 	} else {
    945  1.102  jmcneill 		mutex_enter(&sc->sc_lock);
    946   1.34        ad 	}
    947    1.1      cube 
    948    1.1      cube 	if (IFQ_IS_EMPTY(&ifp->if_snd)) {
    949    1.1      cube 		ifp->if_flags &= ~IFF_OACTIVE;
    950    1.1      cube 		if (sc->sc_flags & TAP_NBIO)
    951    1.1      cube 			error = EWOULDBLOCK;
    952    1.1      cube 		else
    953  1.102  jmcneill 			error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
    954   1.52     pooka 
    955  1.102  jmcneill 		if (error != 0) {
    956  1.102  jmcneill 			mutex_exit(&sc->sc_lock);
    957   1.95     skrll 			return error;
    958  1.102  jmcneill 		}
    959    1.1      cube 		/* The device might have been downed */
    960  1.102  jmcneill 		if ((ifp->if_flags & IFF_UP) == 0) {
    961  1.102  jmcneill 			mutex_exit(&sc->sc_lock);
    962   1.95     skrll 			return EHOSTDOWN;
    963   1.34        ad 		}
    964    1.1      cube 	}
    965    1.1      cube 
    966    1.1      cube 	IFQ_DEQUEUE(&ifp->if_snd, m);
    967  1.102  jmcneill 	mutex_exit(&sc->sc_lock);
    968  1.102  jmcneill 
    969    1.1      cube 	ifp->if_flags &= ~IFF_OACTIVE;
    970    1.1      cube 	if (m == NULL) {
    971    1.1      cube 		error = 0;
    972    1.1      cube 		goto out;
    973    1.1      cube 	}
    974    1.1      cube 
    975    1.1      cube 	ifp->if_opackets++;
    976  1.106   msaitoh 	bpf_mtap(ifp, m, BPF_D_OUT);
    977    1.1      cube 
    978    1.1      cube 	/*
    979    1.1      cube 	 * One read is one packet.
    980    1.1      cube 	 */
    981    1.1      cube 	do {
    982   1.26  christos 		error = uiomove(mtod(m, void *),
    983    1.1      cube 		    min(m->m_len, uio->uio_resid), uio);
    984   1.93  christos 		m = n = m_free(m);
    985    1.1      cube 	} while (m != NULL && uio->uio_resid > 0 && error == 0);
    986    1.1      cube 
    987    1.1      cube 	if (m != NULL)
    988    1.1      cube 		m_freem(m);
    989    1.1      cube 
    990    1.1      cube out:
    991   1.95     skrll 	return error;
    992    1.1      cube }
    993    1.1      cube 
    994    1.1      cube static int
    995   1.56  christos tap_fops_stat(file_t *fp, struct stat *st)
    996   1.56  christos {
    997   1.59  drochner 	int error = 0;
    998   1.57  christos 	struct tap_softc *sc;
    999   1.78      matt 	int unit = fp->f_devunit;
   1000   1.57  christos 
   1001   1.57  christos 	(void)memset(st, 0, sizeof(*st));
   1002   1.57  christos 
   1003   1.56  christos 	KERNEL_LOCK(1, NULL);
   1004   1.57  christos 	sc = device_lookup_private(&tap_cd, unit);
   1005   1.57  christos 	if (sc == NULL) {
   1006   1.57  christos 		error = ENXIO;
   1007   1.57  christos 		goto out;
   1008   1.57  christos 	}
   1009   1.56  christos 
   1010   1.56  christos 	st->st_dev = makedev(cdevsw_lookup_major(&tap_cdevsw), unit);
   1011   1.56  christos 	st->st_atimespec = sc->sc_atime;
   1012   1.56  christos 	st->st_mtimespec = sc->sc_mtime;
   1013   1.56  christos 	st->st_ctimespec = st->st_birthtimespec = sc->sc_btime;
   1014   1.57  christos 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   1015   1.57  christos 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   1016   1.57  christos out:
   1017   1.57  christos 	KERNEL_UNLOCK_ONE(NULL);
   1018   1.57  christos 	return error;
   1019   1.56  christos }
   1020   1.56  christos 
   1021   1.56  christos static int
   1022    1.1      cube tap_cdev_write(dev_t dev, struct uio *uio, int flags)
   1023    1.1      cube {
   1024    1.1      cube 	return tap_dev_write(minor(dev), uio, flags);
   1025    1.1      cube }
   1026    1.1      cube 
   1027    1.1      cube static int
   1028   1.41        ad tap_fops_write(file_t *fp, off_t *offp, struct uio *uio,
   1029   1.23  christos     kauth_cred_t cred, int flags)
   1030    1.1      cube {
   1031   1.44        ad 	int error;
   1032   1.44        ad 
   1033   1.44        ad 	KERNEL_LOCK(1, NULL);
   1034   1.78      matt 	error = tap_dev_write(fp->f_devunit, uio, flags);
   1035   1.44        ad 	KERNEL_UNLOCK_ONE(NULL);
   1036   1.44        ad 	return error;
   1037    1.1      cube }
   1038    1.1      cube 
   1039    1.1      cube static int
   1040   1.23  christos tap_dev_write(int unit, struct uio *uio, int flags)
   1041    1.1      cube {
   1042    1.1      cube 	struct tap_softc *sc =
   1043   1.46    cegger 	    device_lookup_private(&tap_cd, unit);
   1044    1.1      cube 	struct ifnet *ifp;
   1045    1.1      cube 	struct mbuf *m, **mp;
   1046    1.1      cube 	int error = 0;
   1047    1.1      cube 
   1048    1.1      cube 	if (sc == NULL)
   1049   1.95     skrll 		return ENXIO;
   1050    1.1      cube 
   1051   1.56  christos 	getnanotime(&sc->sc_mtime);
   1052    1.1      cube 	ifp = &sc->sc_ec.ec_if;
   1053    1.1      cube 
   1054    1.1      cube 	/* One write, one packet, that's the rule */
   1055    1.1      cube 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1056    1.1      cube 	if (m == NULL) {
   1057    1.1      cube 		ifp->if_ierrors++;
   1058   1.95     skrll 		return ENOBUFS;
   1059    1.1      cube 	}
   1060    1.1      cube 	m->m_pkthdr.len = uio->uio_resid;
   1061    1.1      cube 
   1062    1.1      cube 	mp = &m;
   1063    1.1      cube 	while (error == 0 && uio->uio_resid > 0) {
   1064    1.1      cube 		if (*mp != m) {
   1065    1.1      cube 			MGET(*mp, M_DONTWAIT, MT_DATA);
   1066    1.1      cube 			if (*mp == NULL) {
   1067    1.1      cube 				error = ENOBUFS;
   1068    1.1      cube 				break;
   1069    1.1      cube 			}
   1070    1.1      cube 		}
   1071    1.1      cube 		(*mp)->m_len = min(MHLEN, uio->uio_resid);
   1072   1.26  christos 		error = uiomove(mtod(*mp, void *), (*mp)->m_len, uio);
   1073    1.1      cube 		mp = &(*mp)->m_next;
   1074    1.1      cube 	}
   1075    1.1      cube 	if (error) {
   1076    1.1      cube 		ifp->if_ierrors++;
   1077    1.1      cube 		m_freem(m);
   1078   1.95     skrll 		return error;
   1079    1.1      cube 	}
   1080    1.1      cube 
   1081   1.84     ozaki 	m_set_rcvif(m, ifp);
   1082    1.1      cube 
   1083  1.102  jmcneill 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1084    1.1      cube 
   1085   1.95     skrll 	return 0;
   1086    1.1      cube }
   1087    1.1      cube 
   1088    1.1      cube static int
   1089   1.26  christos tap_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flags,
   1090   1.11  christos     struct lwp *l)
   1091    1.1      cube {
   1092   1.11  christos 	return tap_dev_ioctl(minor(dev), cmd, data, l);
   1093    1.1      cube }
   1094    1.1      cube 
   1095    1.1      cube static int
   1096   1.41        ad tap_fops_ioctl(file_t *fp, u_long cmd, void *data)
   1097    1.1      cube {
   1098   1.78      matt 	return tap_dev_ioctl(fp->f_devunit, cmd, data, curlwp);
   1099    1.1      cube }
   1100    1.1      cube 
   1101    1.1      cube static int
   1102   1.26  christos tap_dev_ioctl(int unit, u_long cmd, void *data, struct lwp *l)
   1103    1.1      cube {
   1104   1.66  christos 	struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
   1105    1.1      cube 
   1106    1.1      cube 	if (sc == NULL)
   1107   1.66  christos 		return ENXIO;
   1108    1.1      cube 
   1109    1.1      cube 	switch (cmd) {
   1110    1.1      cube 	case FIONREAD:
   1111    1.1      cube 		{
   1112    1.1      cube 			struct ifnet *ifp = &sc->sc_ec.ec_if;
   1113    1.1      cube 			struct mbuf *m;
   1114    1.1      cube 			int s;
   1115    1.1      cube 
   1116    1.1      cube 			s = splnet();
   1117    1.1      cube 			IFQ_POLL(&ifp->if_snd, m);
   1118    1.1      cube 
   1119    1.1      cube 			if (m == NULL)
   1120    1.1      cube 				*(int *)data = 0;
   1121    1.1      cube 			else
   1122    1.1      cube 				*(int *)data = m->m_pkthdr.len;
   1123    1.1      cube 			splx(s);
   1124   1.66  christos 			return 0;
   1125   1.95     skrll 		}
   1126    1.1      cube 	case TIOCSPGRP:
   1127    1.1      cube 	case FIOSETOWN:
   1128   1.66  christos 		return fsetown(&sc->sc_pgid, cmd, data);
   1129    1.1      cube 	case TIOCGPGRP:
   1130    1.1      cube 	case FIOGETOWN:
   1131   1.66  christos 		return fgetown(sc->sc_pgid, cmd, data);
   1132    1.1      cube 	case FIOASYNC:
   1133   1.71      yamt 		if (*(int *)data) {
   1134   1.71      yamt 			if (sc->sc_sih == NULL) {
   1135   1.71      yamt 				sc->sc_sih = softint_establish(SOFTINT_CLOCK,
   1136   1.71      yamt 				    tap_softintr, sc);
   1137   1.71      yamt 				if (sc->sc_sih == NULL)
   1138   1.71      yamt 					return EBUSY; /* XXX */
   1139   1.71      yamt 			}
   1140    1.1      cube 			sc->sc_flags |= TAP_ASYNCIO;
   1141   1.71      yamt 		} else {
   1142    1.1      cube 			sc->sc_flags &= ~TAP_ASYNCIO;
   1143   1.71      yamt 			if (sc->sc_sih != NULL) {
   1144   1.71      yamt 				softint_disestablish(sc->sc_sih);
   1145   1.71      yamt 				sc->sc_sih = NULL;
   1146   1.71      yamt 			}
   1147   1.71      yamt 		}
   1148   1.66  christos 		return 0;
   1149    1.1      cube 	case FIONBIO:
   1150    1.1      cube 		if (*(int *)data)
   1151    1.1      cube 			sc->sc_flags |= TAP_NBIO;
   1152    1.1      cube 		else
   1153    1.1      cube 			sc->sc_flags &= ~TAP_NBIO;
   1154   1.66  christos 		return 0;
   1155   1.29  christos #ifdef OTAPGIFNAME
   1156   1.29  christos 	case OTAPGIFNAME:
   1157   1.29  christos #endif
   1158    1.1      cube 	case TAPGIFNAME:
   1159    1.1      cube 		{
   1160    1.1      cube 			struct ifreq *ifr = (struct ifreq *)data;
   1161    1.1      cube 			struct ifnet *ifp = &sc->sc_ec.ec_if;
   1162    1.1      cube 
   1163    1.1      cube 			strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
   1164   1.66  christos 			return 0;
   1165   1.66  christos 		}
   1166    1.1      cube 	default:
   1167   1.66  christos 		return ENOTTY;
   1168    1.1      cube 	}
   1169    1.1      cube }
   1170    1.1      cube 
   1171    1.1      cube static int
   1172   1.11  christos tap_cdev_poll(dev_t dev, int events, struct lwp *l)
   1173    1.1      cube {
   1174   1.11  christos 	return tap_dev_poll(minor(dev), events, l);
   1175    1.1      cube }
   1176    1.1      cube 
   1177    1.1      cube static int
   1178   1.41        ad tap_fops_poll(file_t *fp, int events)
   1179    1.1      cube {
   1180   1.78      matt 	return tap_dev_poll(fp->f_devunit, events, curlwp);
   1181    1.1      cube }
   1182    1.1      cube 
   1183    1.1      cube static int
   1184   1.11  christos tap_dev_poll(int unit, int events, struct lwp *l)
   1185    1.1      cube {
   1186    1.1      cube 	struct tap_softc *sc =
   1187   1.46    cegger 	    device_lookup_private(&tap_cd, unit);
   1188    1.1      cube 	int revents = 0;
   1189    1.1      cube 
   1190    1.1      cube 	if (sc == NULL)
   1191   1.28  christos 		return POLLERR;
   1192    1.1      cube 
   1193    1.1      cube 	if (events & (POLLIN|POLLRDNORM)) {
   1194    1.1      cube 		struct ifnet *ifp = &sc->sc_ec.ec_if;
   1195    1.1      cube 		struct mbuf *m;
   1196    1.1      cube 		int s;
   1197    1.1      cube 
   1198    1.1      cube 		s = splnet();
   1199    1.1      cube 		IFQ_POLL(&ifp->if_snd, m);
   1200    1.1      cube 
   1201    1.1      cube 		if (m != NULL)
   1202    1.1      cube 			revents |= events & (POLLIN|POLLRDNORM);
   1203    1.1      cube 		else {
   1204  1.102  jmcneill 			mutex_spin_enter(&sc->sc_lock);
   1205   1.11  christos 			selrecord(l, &sc->sc_rsel);
   1206  1.102  jmcneill 			mutex_spin_exit(&sc->sc_lock);
   1207    1.1      cube 		}
   1208   1.76      cube 		splx(s);
   1209    1.1      cube 	}
   1210    1.1      cube 	revents |= events & (POLLOUT|POLLWRNORM);
   1211    1.1      cube 
   1212   1.95     skrll 	return revents;
   1213    1.1      cube }
   1214    1.1      cube 
   1215    1.1      cube static struct filterops tap_read_filterops = { 1, NULL, tap_kqdetach,
   1216    1.1      cube 	tap_kqread };
   1217    1.1      cube static struct filterops tap_seltrue_filterops = { 1, NULL, tap_kqdetach,
   1218    1.1      cube 	filt_seltrue };
   1219    1.1      cube 
   1220    1.1      cube static int
   1221    1.1      cube tap_cdev_kqfilter(dev_t dev, struct knote *kn)
   1222    1.1      cube {
   1223    1.1      cube 	return tap_dev_kqfilter(minor(dev), kn);
   1224    1.1      cube }
   1225    1.1      cube 
   1226    1.1      cube static int
   1227   1.41        ad tap_fops_kqfilter(file_t *fp, struct knote *kn)
   1228    1.1      cube {
   1229   1.78      matt 	return tap_dev_kqfilter(fp->f_devunit, kn);
   1230    1.1      cube }
   1231    1.1      cube 
   1232    1.1      cube static int
   1233    1.1      cube tap_dev_kqfilter(int unit, struct knote *kn)
   1234    1.1      cube {
   1235    1.1      cube 	struct tap_softc *sc =
   1236   1.46    cegger 	    device_lookup_private(&tap_cd, unit);
   1237    1.1      cube 
   1238    1.1      cube 	if (sc == NULL)
   1239   1.95     skrll 		return ENXIO;
   1240    1.1      cube 
   1241   1.44        ad 	KERNEL_LOCK(1, NULL);
   1242    1.1      cube 	switch(kn->kn_filter) {
   1243    1.1      cube 	case EVFILT_READ:
   1244    1.1      cube 		kn->kn_fop = &tap_read_filterops;
   1245    1.1      cube 		break;
   1246    1.1      cube 	case EVFILT_WRITE:
   1247    1.1      cube 		kn->kn_fop = &tap_seltrue_filterops;
   1248    1.1      cube 		break;
   1249    1.1      cube 	default:
   1250   1.44        ad 		KERNEL_UNLOCK_ONE(NULL);
   1251   1.95     skrll 		return EINVAL;
   1252    1.1      cube 	}
   1253    1.1      cube 
   1254    1.1      cube 	kn->kn_hook = sc;
   1255  1.102  jmcneill 	mutex_spin_enter(&sc->sc_lock);
   1256    1.1      cube 	SLIST_INSERT_HEAD(&sc->sc_rsel.sel_klist, kn, kn_selnext);
   1257  1.102  jmcneill 	mutex_spin_exit(&sc->sc_lock);
   1258   1.44        ad 	KERNEL_UNLOCK_ONE(NULL);
   1259   1.95     skrll 	return 0;
   1260    1.1      cube }
   1261    1.1      cube 
   1262    1.1      cube static void
   1263    1.1      cube tap_kqdetach(struct knote *kn)
   1264    1.1      cube {
   1265    1.1      cube 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
   1266    1.1      cube 
   1267   1.44        ad 	KERNEL_LOCK(1, NULL);
   1268  1.102  jmcneill 	mutex_spin_enter(&sc->sc_lock);
   1269    1.1      cube 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
   1270  1.102  jmcneill 	mutex_spin_exit(&sc->sc_lock);
   1271   1.44        ad 	KERNEL_UNLOCK_ONE(NULL);
   1272    1.1      cube }
   1273    1.1      cube 
   1274    1.1      cube static int
   1275   1.23  christos tap_kqread(struct knote *kn, long hint)
   1276    1.1      cube {
   1277    1.1      cube 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
   1278    1.1      cube 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1279    1.1      cube 	struct mbuf *m;
   1280   1.44        ad 	int s, rv;
   1281    1.1      cube 
   1282   1.44        ad 	KERNEL_LOCK(1, NULL);
   1283    1.1      cube 	s = splnet();
   1284    1.1      cube 	IFQ_POLL(&ifp->if_snd, m);
   1285    1.1      cube 
   1286    1.1      cube 	if (m == NULL)
   1287    1.1      cube 		kn->kn_data = 0;
   1288    1.1      cube 	else
   1289    1.1      cube 		kn->kn_data = m->m_pkthdr.len;
   1290    1.1      cube 	splx(s);
   1291   1.44        ad 	rv = (kn->kn_data != 0 ? 1 : 0);
   1292   1.44        ad 	KERNEL_UNLOCK_ONE(NULL);
   1293   1.44        ad 	return rv;
   1294    1.1      cube }
   1295    1.1      cube 
   1296    1.1      cube /*
   1297    1.1      cube  * sysctl management routines
   1298    1.1      cube  * You can set the address of an interface through:
   1299    1.1      cube  * net.link.tap.tap<number>
   1300    1.1      cube  *
   1301    1.1      cube  * Note the consistent use of tap_log in order to use
   1302    1.1      cube  * sysctl_teardown at unload time.
   1303    1.1      cube  *
   1304    1.1      cube  * In the kernel you will find a lot of SYSCTL_SETUP blocks.  Those
   1305    1.1      cube  * blocks register a function in a special section of the kernel
   1306    1.1      cube  * (called a link set) which is used at init_sysctl() time to cycle
   1307    1.1      cube  * through all those functions to create the kernel's sysctl tree.
   1308    1.1      cube  *
   1309   1.51        ad  * It is not possible to use link sets in a module, so the
   1310    1.1      cube  * easiest is to simply call our own setup routine at load time.
   1311    1.1      cube  *
   1312    1.1      cube  * In the SYSCTL_SETUP blocks you find in the kernel, nodes have the
   1313    1.1      cube  * CTLFLAG_PERMANENT flag, meaning they cannot be removed.  Once the
   1314    1.1      cube  * whole kernel sysctl tree is built, it is not possible to add any
   1315    1.1      cube  * permanent node.
   1316    1.1      cube  *
   1317    1.1      cube  * It should be noted that we're not saving the sysctlnode pointer
   1318    1.1      cube  * we are returned when creating the "tap" node.  That structure
   1319    1.1      cube  * cannot be trusted once out of the calling function, as it might
   1320    1.1      cube  * get reused.  So we just save the MIB number, and always give the
   1321    1.1      cube  * full path starting from the root for later calls to sysctl_createv
   1322    1.1      cube  * and sysctl_destroyv.
   1323    1.1      cube  */
   1324   1.91  christos static void
   1325   1.91  christos sysctl_tap_setup(struct sysctllog **clog)
   1326    1.1      cube {
   1327   1.10    atatat 	const struct sysctlnode *node;
   1328    1.1      cube 	int error = 0;
   1329    1.1      cube 
   1330    1.1      cube 	if ((error = sysctl_createv(clog, 0, NULL, NULL,
   1331    1.1      cube 	    CTLFLAG_PERMANENT,
   1332    1.1      cube 	    CTLTYPE_NODE, "link", NULL,
   1333    1.1      cube 	    NULL, 0, NULL, 0,
   1334    1.3      cube 	    CTL_NET, AF_LINK, CTL_EOL)) != 0)
   1335    1.1      cube 		return;
   1336    1.1      cube 
   1337    1.1      cube 	/*
   1338    1.1      cube 	 * The first four parameters of sysctl_createv are for management.
   1339    1.1      cube 	 *
   1340    1.1      cube 	 * The four that follows, here starting with a '0' for the flags,
   1341    1.1      cube 	 * describe the node.
   1342    1.1      cube 	 *
   1343    1.1      cube 	 * The next series of four set its value, through various possible
   1344    1.1      cube 	 * means.
   1345    1.1      cube 	 *
   1346    1.1      cube 	 * Last but not least, the path to the node is described.  That path
   1347    1.1      cube 	 * is relative to the given root (third argument).  Here we're
   1348    1.1      cube 	 * starting from the root.
   1349    1.1      cube 	 */
   1350    1.1      cube 	if ((error = sysctl_createv(clog, 0, NULL, &node,
   1351    1.1      cube 	    CTLFLAG_PERMANENT,
   1352    1.1      cube 	    CTLTYPE_NODE, "tap", NULL,
   1353    1.1      cube 	    NULL, 0, NULL, 0,
   1354    1.3      cube 	    CTL_NET, AF_LINK, CTL_CREATE, CTL_EOL)) != 0)
   1355    1.1      cube 		return;
   1356    1.1      cube 	tap_node = node->sysctl_num;
   1357    1.1      cube }
   1358    1.1      cube 
   1359    1.1      cube /*
   1360    1.1      cube  * The helper functions make Andrew Brown's interface really
   1361    1.1      cube  * shine.  It makes possible to create value on the fly whether
   1362    1.1      cube  * the sysctl value is read or written.
   1363    1.1      cube  *
   1364    1.1      cube  * As shown as an example in the man page, the first step is to
   1365    1.1      cube  * create a copy of the node to have sysctl_lookup work on it.
   1366    1.1      cube  *
   1367    1.1      cube  * Here, we have more work to do than just a copy, since we have
   1368    1.1      cube  * to create the string.  The first step is to collect the actual
   1369    1.1      cube  * value of the node, which is a convenient pointer to the softc
   1370    1.1      cube  * of the interface.  From there we create the string and use it
   1371    1.1      cube  * as the value, but only for the *copy* of the node.
   1372    1.1      cube  *
   1373    1.1      cube  * Then we let sysctl_lookup do the magic, which consists in
   1374    1.1      cube  * setting oldp and newp as required by the operation.  When the
   1375    1.1      cube  * value is read, that means that the string will be copied to
   1376    1.1      cube  * the user, and when it is written, the new value will be copied
   1377    1.1      cube  * over in the addr array.
   1378    1.1      cube  *
   1379    1.1      cube  * If newp is NULL, the user was reading the value, so we don't
   1380    1.1      cube  * have anything else to do.  If a new value was written, we
   1381    1.1      cube  * have to check it.
   1382    1.1      cube  *
   1383    1.1      cube  * If it is incorrect, we can return an error and leave 'node' as
   1384    1.1      cube  * it is:  since it is a copy of the actual node, the change will
   1385    1.1      cube  * be forgotten.
   1386    1.1      cube  *
   1387    1.1      cube  * Upon a correct input, we commit the change to the ifnet
   1388    1.1      cube  * structure of our interface.
   1389    1.1      cube  */
   1390    1.1      cube static int
   1391    1.1      cube tap_sysctl_handler(SYSCTLFN_ARGS)
   1392    1.1      cube {
   1393    1.1      cube 	struct sysctlnode node;
   1394    1.1      cube 	struct tap_softc *sc;
   1395    1.1      cube 	struct ifnet *ifp;
   1396    1.1      cube 	int error;
   1397    1.1      cube 	size_t len;
   1398   1.14  christos 	char addr[3 * ETHER_ADDR_LEN];
   1399   1.32    dyoung 	uint8_t enaddr[ETHER_ADDR_LEN];
   1400    1.1      cube 
   1401    1.1      cube 	node = *rnode;
   1402    1.1      cube 	sc = node.sysctl_data;
   1403    1.1      cube 	ifp = &sc->sc_ec.ec_if;
   1404   1.31    dyoung 	(void)ether_snprintf(addr, sizeof(addr), CLLADDR(ifp->if_sadl));
   1405    1.1      cube 	node.sysctl_data = addr;
   1406    1.1      cube 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1407    1.1      cube 	if (error || newp == NULL)
   1408   1.95     skrll 		return error;
   1409    1.1      cube 
   1410    1.1      cube 	len = strlen(addr);
   1411    1.1      cube 	if (len < 11 || len > 17)
   1412   1.95     skrll 		return EINVAL;
   1413    1.1      cube 
   1414    1.1      cube 	/* Commit change */
   1415   1.65  christos 	if (ether_aton_r(enaddr, sizeof(enaddr), addr) != 0)
   1416   1.95     skrll 		return EINVAL;
   1417   1.50    dyoung 	if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
   1418   1.95     skrll 	return error;
   1419    1.1      cube }
   1420   1.85  christos 
   1421   1.85  christos /*
   1422   1.85  christos  * Module infrastructure
   1423   1.85  christos  */
   1424   1.85  christos #include "if_module.h"
   1425   1.85  christos 
   1426   1.85  christos IF_MODULE(MODULE_CLASS_DRIVER, tap, "")
   1427