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