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