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