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