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