xref: /netbsd-src/sys/net/if_tap.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: if_tap.c,v 1.51 2008/11/12 12:36:28 ad 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.51 2008/11/12 12:36:28 ad 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 module 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 	pmf_device_deregister(self);
394 
395 	return (0);
396 }
397 
398 /*
399  * This function is called by the ifmedia layer to notify the driver
400  * that the user requested a media change.  A real driver would
401  * reconfigure the hardware.
402  */
403 static int
404 tap_mediachange(struct ifnet *ifp)
405 {
406 	return (0);
407 }
408 
409 /*
410  * Here the user asks for the currently used media.
411  */
412 static void
413 tap_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
414 {
415 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
416 	imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
417 }
418 
419 /*
420  * This is the function where we SEND packets.
421  *
422  * There is no 'receive' equivalent.  A typical driver will get
423  * interrupts from the hardware, and from there will inject new packets
424  * into the network stack.
425  *
426  * Once handled, a packet must be freed.  A real driver might not be able
427  * to fit all the pending packets into the hardware, and is allowed to
428  * return before having sent all the packets.  It should then use the
429  * if_flags flag IFF_OACTIVE to notify the upper layer.
430  *
431  * There are also other flags one should check, such as IFF_PAUSE.
432  *
433  * It is our duty to make packets available to BPF listeners.
434  *
435  * You should be aware that this function is called by the Ethernet layer
436  * at splnet().
437  *
438  * When the device is opened, we have to pass the packet(s) to the
439  * userland.  For that we stay in OACTIVE mode while the userland gets
440  * the packets, and we send a signal to the processes waiting to read.
441  *
442  * wakeup(sc) is the counterpart to the tsleep call in
443  * tap_dev_read, while selnotify() is used for kevent(2) and
444  * poll(2) (which includes select(2)) listeners.
445  */
446 static void
447 tap_start(struct ifnet *ifp)
448 {
449 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
450 	struct mbuf *m0;
451 
452 	if ((sc->sc_flags & TAP_INUSE) == 0) {
453 		/* Simply drop packets */
454 		for(;;) {
455 			IFQ_DEQUEUE(&ifp->if_snd, m0);
456 			if (m0 == NULL)
457 				return;
458 
459 			ifp->if_opackets++;
460 #if NBPFILTER > 0
461 			if (ifp->if_bpf)
462 				bpf_mtap(ifp->if_bpf, m0);
463 #endif
464 
465 			m_freem(m0);
466 		}
467 	} else if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
468 		ifp->if_flags |= IFF_OACTIVE;
469 		wakeup(sc);
470 		selnotify(&sc->sc_rsel, 0, 1);
471 		if (sc->sc_flags & TAP_ASYNCIO)
472 			softint_schedule(sc->sc_sih);
473 	}
474 }
475 
476 static void
477 tap_softintr(void *cookie)
478 {
479 	struct tap_softc *sc;
480 	struct ifnet *ifp;
481 	int a, b;
482 
483 	sc = cookie;
484 
485 	if (sc->sc_flags & TAP_ASYNCIO) {
486 		ifp = &sc->sc_ec.ec_if;
487 		if (ifp->if_flags & IFF_RUNNING) {
488 			a = POLL_IN;
489 			b = POLLIN|POLLRDNORM;
490 		} else {
491 			a = POLL_HUP;
492 			b = 0;
493 		}
494 		fownsignal(sc->sc_pgid, SIGIO, a, b, NULL);
495 	}
496 }
497 
498 /*
499  * A typical driver will only contain the following handlers for
500  * ioctl calls, except SIOCSIFPHYADDR.
501  * The latter is a hack I used to set the Ethernet address of the
502  * faked device.
503  *
504  * Note that both ifmedia_ioctl() and ether_ioctl() have to be
505  * called under splnet().
506  */
507 static int
508 tap_ioctl(struct ifnet *ifp, u_long cmd, void *data)
509 {
510 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
511 	struct ifreq *ifr = (struct ifreq *)data;
512 	int s, error;
513 
514 	s = splnet();
515 
516 	switch (cmd) {
517 #ifdef OSIOCSIFMEDIA
518 	case OSIOCSIFMEDIA:
519 #endif
520 	case SIOCSIFMEDIA:
521 	case SIOCGIFMEDIA:
522 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
523 		break;
524 	case SIOCSIFPHYADDR:
525 		error = tap_lifaddr(ifp, cmd, (struct ifaliasreq *)data);
526 		break;
527 	default:
528 		error = ether_ioctl(ifp, cmd, data);
529 		if (error == ENETRESET)
530 			error = 0;
531 		break;
532 	}
533 
534 	splx(s);
535 
536 	return (error);
537 }
538 
539 /*
540  * Helper function to set Ethernet address.  This shouldn't be done there,
541  * and should actually be available to all Ethernet drivers, real or not.
542  */
543 static int
544 tap_lifaddr(struct ifnet *ifp, u_long cmd, struct ifaliasreq *ifra)
545 {
546 	const struct sockaddr_dl *sdl = satosdl(&ifra->ifra_addr);
547 
548 	if (sdl->sdl_family != AF_LINK)
549 		return (EINVAL);
550 
551 	if_set_sadl(ifp, CLLADDR(sdl), ETHER_ADDR_LEN, false);
552 
553 	return (0);
554 }
555 
556 /*
557  * _init() would typically be called when an interface goes up,
558  * meaning it should configure itself into the state in which it
559  * can send packets.
560  */
561 static int
562 tap_init(struct ifnet *ifp)
563 {
564 	ifp->if_flags |= IFF_RUNNING;
565 
566 	tap_start(ifp);
567 
568 	return (0);
569 }
570 
571 /*
572  * _stop() is called when an interface goes down.  It is our
573  * responsability to validate that state by clearing the
574  * IFF_RUNNING flag.
575  *
576  * We have to wake up all the sleeping processes to have the pending
577  * read requests cancelled.
578  */
579 static void
580 tap_stop(struct ifnet *ifp, int disable)
581 {
582 	struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
583 
584 	ifp->if_flags &= ~IFF_RUNNING;
585 	wakeup(sc);
586 	selnotify(&sc->sc_rsel, 0, 1);
587 	if (sc->sc_flags & TAP_ASYNCIO)
588 		softint_schedule(sc->sc_sih);
589 }
590 
591 /*
592  * The 'create' command of ifconfig can be used to create
593  * any numbered instance of a given device.  Thus we have to
594  * make sure we have enough room in cd_devs to create the
595  * user-specified instance.  config_attach_pseudo will do this
596  * for us.
597  */
598 static int
599 tap_clone_create(struct if_clone *ifc, int unit)
600 {
601 	if (tap_clone_creator(unit) == NULL) {
602 		aprint_error("%s%d: unable to attach an instance\n",
603                     tap_cd.cd_name, unit);
604 		return (ENXIO);
605 	}
606 
607 	return (0);
608 }
609 
610 /*
611  * tap(4) can be cloned by two ways:
612  *   using 'ifconfig tap0 create', which will use the network
613  *     interface cloning API, and call tap_clone_create above.
614  *   opening the cloning device node, whose minor number is TAP_CLONER.
615  *     See below for an explanation on how this part work.
616  */
617 static struct tap_softc *
618 tap_clone_creator(int unit)
619 {
620 	struct cfdata *cf;
621 
622 	cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
623 	cf->cf_name = tap_cd.cd_name;
624 	cf->cf_atname = tap_ca.ca_name;
625 	if (unit == -1) {
626 		/* let autoconf find the first free one */
627 		cf->cf_unit = 0;
628 		cf->cf_fstate = FSTATE_STAR;
629 	} else {
630 		cf->cf_unit = unit;
631 		cf->cf_fstate = FSTATE_FOUND;
632 	}
633 
634 	return device_private(config_attach_pseudo(cf));
635 }
636 
637 /*
638  * The clean design of if_clone and autoconf(9) makes that part
639  * really straightforward.  The second argument of config_detach
640  * means neither QUIET nor FORCED.
641  */
642 static int
643 tap_clone_destroy(struct ifnet *ifp)
644 {
645 	struct tap_softc *sc = ifp->if_softc;
646 
647 	return tap_clone_destroyer(sc->sc_dev);
648 }
649 
650 int
651 tap_clone_destroyer(device_t dev)
652 {
653 	cfdata_t cf = device_cfdata(dev);
654 	int error;
655 
656 	if ((error = config_detach(dev, 0)) != 0)
657 		aprint_error_dev(dev, "unable to detach instance\n");
658 	free(cf, M_DEVBUF);
659 
660 	return (error);
661 }
662 
663 /*
664  * tap(4) is a bit of an hybrid device.  It can be used in two different
665  * ways:
666  *  1. ifconfig tapN create, then use /dev/tapN to read/write off it.
667  *  2. open /dev/tap, get a new interface created and read/write off it.
668  *     That interface is destroyed when the process that had it created exits.
669  *
670  * The first way is managed by the cdevsw structure, and you access interfaces
671  * through a (major, minor) mapping:  tap4 is obtained by the minor number
672  * 4.  The entry points for the cdevsw interface are prefixed by tap_cdev_.
673  *
674  * The second way is the so-called "cloning" device.  It's a special minor
675  * number (chosen as the maximal number, to allow as much tap devices as
676  * possible).  The user first opens the cloner (e.g., /dev/tap), and that
677  * call ends in tap_cdev_open.  The actual place where it is handled is
678  * tap_dev_cloner.
679  *
680  * An tap device cannot be opened more than once at a time, so the cdevsw
681  * part of open() does nothing but noting that the interface is being used and
682  * hence ready to actually handle packets.
683  */
684 
685 static int
686 tap_cdev_open(dev_t dev, int flags, int fmt, struct lwp *l)
687 {
688 	struct tap_softc *sc;
689 
690 	if (minor(dev) == TAP_CLONER)
691 		return tap_dev_cloner(l);
692 
693 	sc = device_lookup_private(&tap_cd, minor(dev));
694 	if (sc == NULL)
695 		return (ENXIO);
696 
697 	/* The device can only be opened once */
698 	if (sc->sc_flags & TAP_INUSE)
699 		return (EBUSY);
700 	sc->sc_flags |= TAP_INUSE;
701 	return (0);
702 }
703 
704 /*
705  * There are several kinds of cloning devices, and the most simple is the one
706  * tap(4) uses.  What it does is change the file descriptor with a new one,
707  * with its own fileops structure (which maps to the various read, write,
708  * ioctl functions).  It starts allocating a new file descriptor with falloc,
709  * then actually creates the new tap devices.
710  *
711  * Once those two steps are successful, we can re-wire the existing file
712  * descriptor to its new self.  This is done with fdclone():  it fills the fp
713  * structure as needed (notably f_data gets filled with the fifth parameter
714  * passed, the unit of the tap device which will allows us identifying the
715  * device later), and returns EMOVEFD.
716  *
717  * That magic value is interpreted by sys_open() which then replaces the
718  * current file descriptor by the new one (through a magic member of struct
719  * lwp, l_dupfd).
720  *
721  * The tap device is flagged as being busy since it otherwise could be
722  * externally accessed through the corresponding device node with the cdevsw
723  * interface.
724  */
725 
726 static int
727 tap_dev_cloner(struct lwp *l)
728 {
729 	struct tap_softc *sc;
730 	file_t *fp;
731 	int error, fd;
732 
733 	if ((error = fd_allocfile(&fp, &fd)) != 0)
734 		return (error);
735 
736 	if ((sc = tap_clone_creator(-1)) == NULL) {
737 		fd_abort(curproc, fp, fd);
738 		return (ENXIO);
739 	}
740 
741 	sc->sc_flags |= TAP_INUSE;
742 
743 	return fd_clone(fp, fd, FREAD|FWRITE, &tap_fileops,
744 	    (void *)(intptr_t)device_unit(sc->sc_dev));
745 }
746 
747 /*
748  * While all other operations (read, write, ioctl, poll and kqfilter) are
749  * really the same whether we are in cdevsw or fileops mode, the close()
750  * function is slightly different in the two cases.
751  *
752  * As for the other, the core of it is shared in tap_dev_close.  What
753  * it does is sufficient for the cdevsw interface, but the cloning interface
754  * needs another thing:  the interface is destroyed when the processes that
755  * created it closes it.
756  */
757 static int
758 tap_cdev_close(dev_t dev, int flags, int fmt,
759     struct lwp *l)
760 {
761 	struct tap_softc *sc =
762 	    device_lookup_private(&tap_cd, minor(dev));
763 
764 	if (sc == NULL)
765 		return (ENXIO);
766 
767 	return tap_dev_close(sc);
768 }
769 
770 /*
771  * It might happen that the administrator used ifconfig to externally destroy
772  * the interface.  In that case, tap_fops_close will be called while
773  * tap_detach is already happening.  If we called it again from here, we
774  * would dead lock.  TAP_GOING ensures that this situation doesn't happen.
775  */
776 static int
777 tap_fops_close(file_t *fp)
778 {
779 	int unit = (intptr_t)fp->f_data;
780 	struct tap_softc *sc;
781 	int error;
782 
783 	sc = device_lookup_private(&tap_cd, unit);
784 	if (sc == NULL)
785 		return (ENXIO);
786 
787 	/* tap_dev_close currently always succeeds, but it might not
788 	 * always be the case. */
789 	KERNEL_LOCK(1, NULL);
790 	if ((error = tap_dev_close(sc)) != 0) {
791 		KERNEL_UNLOCK_ONE(NULL);
792 		return (error);
793 	}
794 
795 	/* Destroy the device now that it is no longer useful,
796 	 * unless it's already being destroyed. */
797 	if ((sc->sc_flags & TAP_GOING) != 0) {
798 		KERNEL_UNLOCK_ONE(NULL);
799 		return (0);
800 	}
801 
802 	error = tap_clone_destroyer(sc->sc_dev);
803 	KERNEL_UNLOCK_ONE(NULL);
804 	return error;
805 }
806 
807 static int
808 tap_dev_close(struct tap_softc *sc)
809 {
810 	struct ifnet *ifp;
811 	int s;
812 
813 	s = splnet();
814 	/* Let tap_start handle packets again */
815 	ifp = &sc->sc_ec.ec_if;
816 	ifp->if_flags &= ~IFF_OACTIVE;
817 
818 	/* Purge output queue */
819 	if (!(IFQ_IS_EMPTY(&ifp->if_snd))) {
820 		struct mbuf *m;
821 
822 		for (;;) {
823 			IFQ_DEQUEUE(&ifp->if_snd, m);
824 			if (m == NULL)
825 				break;
826 
827 			ifp->if_opackets++;
828 #if NBPFILTER > 0
829 			if (ifp->if_bpf)
830 				bpf_mtap(ifp->if_bpf, m);
831 #endif
832 		}
833 	}
834 	splx(s);
835 
836 	sc->sc_flags &= ~(TAP_INUSE | TAP_ASYNCIO);
837 
838 	return (0);
839 }
840 
841 static int
842 tap_cdev_read(dev_t dev, struct uio *uio, int flags)
843 {
844 	return tap_dev_read(minor(dev), uio, flags);
845 }
846 
847 static int
848 tap_fops_read(file_t *fp, off_t *offp, struct uio *uio,
849     kauth_cred_t cred, int flags)
850 {
851 	int error;
852 
853 	KERNEL_LOCK(1, NULL);
854 	error = tap_dev_read((intptr_t)fp->f_data, uio, flags);
855 	KERNEL_UNLOCK_ONE(NULL);
856 	return error;
857 }
858 
859 static int
860 tap_dev_read(int unit, struct uio *uio, int flags)
861 {
862 	struct tap_softc *sc =
863 	    device_lookup_private(&tap_cd, unit);
864 	struct ifnet *ifp;
865 	struct mbuf *m, *n;
866 	int error = 0, s;
867 
868 	if (sc == NULL)
869 		return (ENXIO);
870 
871 	ifp = &sc->sc_ec.ec_if;
872 	if ((ifp->if_flags & IFF_UP) == 0)
873 		return (EHOSTDOWN);
874 
875 	/*
876 	 * In the TAP_NBIO case, we have to make sure we won't be sleeping
877 	 */
878 	if ((sc->sc_flags & TAP_NBIO) != 0) {
879 		if (!mutex_tryenter(&sc->sc_rdlock))
880 			return (EWOULDBLOCK);
881 	} else {
882 		mutex_enter(&sc->sc_rdlock);
883 	}
884 
885 	s = splnet();
886 	if (IFQ_IS_EMPTY(&ifp->if_snd)) {
887 		ifp->if_flags &= ~IFF_OACTIVE;
888 		splx(s);
889 		/*
890 		 * We must release the lock before sleeping, and re-acquire it
891 		 * after.
892 		 */
893 		mutex_exit(&sc->sc_rdlock);
894 		if (sc->sc_flags & TAP_NBIO)
895 			error = EWOULDBLOCK;
896 		else
897 			error = tsleep(sc, PSOCK|PCATCH, "tap", 0);
898 		if (error != 0)
899 			return (error);
900 		/* The device might have been downed */
901 		if ((ifp->if_flags & IFF_UP) == 0)
902 			return (EHOSTDOWN);
903 		if ((sc->sc_flags & TAP_NBIO)) {
904 			if (!mutex_tryenter(&sc->sc_rdlock))
905 				return (EWOULDBLOCK);
906 		} else {
907 			mutex_enter(&sc->sc_rdlock);
908 		}
909 		s = splnet();
910 	}
911 
912 	IFQ_DEQUEUE(&ifp->if_snd, m);
913 	ifp->if_flags &= ~IFF_OACTIVE;
914 	splx(s);
915 	if (m == NULL) {
916 		error = 0;
917 		goto out;
918 	}
919 
920 	ifp->if_opackets++;
921 #if NBPFILTER > 0
922 	if (ifp->if_bpf)
923 		bpf_mtap(ifp->if_bpf, m);
924 #endif
925 
926 	/*
927 	 * One read is one packet.
928 	 */
929 	do {
930 		error = uiomove(mtod(m, void *),
931 		    min(m->m_len, uio->uio_resid), uio);
932 		MFREE(m, n);
933 		m = n;
934 	} while (m != NULL && uio->uio_resid > 0 && error == 0);
935 
936 	if (m != NULL)
937 		m_freem(m);
938 
939 out:
940 	mutex_exit(&sc->sc_rdlock);
941 	return (error);
942 }
943 
944 static int
945 tap_cdev_write(dev_t dev, struct uio *uio, int flags)
946 {
947 	return tap_dev_write(minor(dev), uio, flags);
948 }
949 
950 static int
951 tap_fops_write(file_t *fp, off_t *offp, struct uio *uio,
952     kauth_cred_t cred, int flags)
953 {
954 	int error;
955 
956 	KERNEL_LOCK(1, NULL);
957 	error = tap_dev_write((intptr_t)fp->f_data, uio, flags);
958 	KERNEL_UNLOCK_ONE(NULL);
959 	return error;
960 }
961 
962 static int
963 tap_dev_write(int unit, struct uio *uio, int flags)
964 {
965 	struct tap_softc *sc =
966 	    device_lookup_private(&tap_cd, unit);
967 	struct ifnet *ifp;
968 	struct mbuf *m, **mp;
969 	int error = 0;
970 	int s;
971 
972 	if (sc == NULL)
973 		return (ENXIO);
974 
975 	ifp = &sc->sc_ec.ec_if;
976 
977 	/* One write, one packet, that's the rule */
978 	MGETHDR(m, M_DONTWAIT, MT_DATA);
979 	if (m == NULL) {
980 		ifp->if_ierrors++;
981 		return (ENOBUFS);
982 	}
983 	m->m_pkthdr.len = uio->uio_resid;
984 
985 	mp = &m;
986 	while (error == 0 && uio->uio_resid > 0) {
987 		if (*mp != m) {
988 			MGET(*mp, M_DONTWAIT, MT_DATA);
989 			if (*mp == NULL) {
990 				error = ENOBUFS;
991 				break;
992 			}
993 		}
994 		(*mp)->m_len = min(MHLEN, uio->uio_resid);
995 		error = uiomove(mtod(*mp, void *), (*mp)->m_len, uio);
996 		mp = &(*mp)->m_next;
997 	}
998 	if (error) {
999 		ifp->if_ierrors++;
1000 		m_freem(m);
1001 		return (error);
1002 	}
1003 
1004 	ifp->if_ipackets++;
1005 	m->m_pkthdr.rcvif = ifp;
1006 
1007 #if NBPFILTER > 0
1008 	if (ifp->if_bpf)
1009 		bpf_mtap(ifp->if_bpf, m);
1010 #endif
1011 	s =splnet();
1012 	(*ifp->if_input)(ifp, m);
1013 	splx(s);
1014 
1015 	return (0);
1016 }
1017 
1018 static int
1019 tap_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flags,
1020     struct lwp *l)
1021 {
1022 	return tap_dev_ioctl(minor(dev), cmd, data, l);
1023 }
1024 
1025 static int
1026 tap_fops_ioctl(file_t *fp, u_long cmd, void *data)
1027 {
1028 	return tap_dev_ioctl((intptr_t)fp->f_data, cmd, data, curlwp);
1029 }
1030 
1031 static int
1032 tap_dev_ioctl(int unit, u_long cmd, void *data, struct lwp *l)
1033 {
1034 	struct tap_softc *sc =
1035 	    device_lookup_private(&tap_cd, unit);
1036 	int error = 0;
1037 
1038 	if (sc == NULL)
1039 		return (ENXIO);
1040 
1041 	switch (cmd) {
1042 	case FIONREAD:
1043 		{
1044 			struct ifnet *ifp = &sc->sc_ec.ec_if;
1045 			struct mbuf *m;
1046 			int s;
1047 
1048 			s = splnet();
1049 			IFQ_POLL(&ifp->if_snd, m);
1050 
1051 			if (m == NULL)
1052 				*(int *)data = 0;
1053 			else
1054 				*(int *)data = m->m_pkthdr.len;
1055 			splx(s);
1056 		} break;
1057 	case TIOCSPGRP:
1058 	case FIOSETOWN:
1059 		error = fsetown(&sc->sc_pgid, cmd, data);
1060 		break;
1061 	case TIOCGPGRP:
1062 	case FIOGETOWN:
1063 		error = fgetown(sc->sc_pgid, cmd, data);
1064 		break;
1065 	case FIOASYNC:
1066 		if (*(int *)data)
1067 			sc->sc_flags |= TAP_ASYNCIO;
1068 		else
1069 			sc->sc_flags &= ~TAP_ASYNCIO;
1070 		break;
1071 	case FIONBIO:
1072 		if (*(int *)data)
1073 			sc->sc_flags |= TAP_NBIO;
1074 		else
1075 			sc->sc_flags &= ~TAP_NBIO;
1076 		break;
1077 #ifdef OTAPGIFNAME
1078 	case OTAPGIFNAME:
1079 #endif
1080 	case TAPGIFNAME:
1081 		{
1082 			struct ifreq *ifr = (struct ifreq *)data;
1083 			struct ifnet *ifp = &sc->sc_ec.ec_if;
1084 
1085 			strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1086 		} break;
1087 	default:
1088 		error = ENOTTY;
1089 		break;
1090 	}
1091 
1092 	return (0);
1093 }
1094 
1095 static int
1096 tap_cdev_poll(dev_t dev, int events, struct lwp *l)
1097 {
1098 	return tap_dev_poll(minor(dev), events, l);
1099 }
1100 
1101 static int
1102 tap_fops_poll(file_t *fp, int events)
1103 {
1104 	return tap_dev_poll((intptr_t)fp->f_data, events, curlwp);
1105 }
1106 
1107 static int
1108 tap_dev_poll(int unit, int events, struct lwp *l)
1109 {
1110 	struct tap_softc *sc =
1111 	    device_lookup_private(&tap_cd, unit);
1112 	int revents = 0;
1113 
1114 	if (sc == NULL)
1115 		return POLLERR;
1116 
1117 	if (events & (POLLIN|POLLRDNORM)) {
1118 		struct ifnet *ifp = &sc->sc_ec.ec_if;
1119 		struct mbuf *m;
1120 		int s;
1121 
1122 		s = splnet();
1123 		IFQ_POLL(&ifp->if_snd, m);
1124 		splx(s);
1125 
1126 		if (m != NULL)
1127 			revents |= events & (POLLIN|POLLRDNORM);
1128 		else {
1129 			simple_lock(&sc->sc_kqlock);
1130 			selrecord(l, &sc->sc_rsel);
1131 			simple_unlock(&sc->sc_kqlock);
1132 		}
1133 	}
1134 	revents |= events & (POLLOUT|POLLWRNORM);
1135 
1136 	return (revents);
1137 }
1138 
1139 static struct filterops tap_read_filterops = { 1, NULL, tap_kqdetach,
1140 	tap_kqread };
1141 static struct filterops tap_seltrue_filterops = { 1, NULL, tap_kqdetach,
1142 	filt_seltrue };
1143 
1144 static int
1145 tap_cdev_kqfilter(dev_t dev, struct knote *kn)
1146 {
1147 	return tap_dev_kqfilter(minor(dev), kn);
1148 }
1149 
1150 static int
1151 tap_fops_kqfilter(file_t *fp, struct knote *kn)
1152 {
1153 	return tap_dev_kqfilter((intptr_t)fp->f_data, kn);
1154 }
1155 
1156 static int
1157 tap_dev_kqfilter(int unit, struct knote *kn)
1158 {
1159 	struct tap_softc *sc =
1160 	    device_lookup_private(&tap_cd, unit);
1161 
1162 	if (sc == NULL)
1163 		return (ENXIO);
1164 
1165 	KERNEL_LOCK(1, NULL);
1166 	switch(kn->kn_filter) {
1167 	case EVFILT_READ:
1168 		kn->kn_fop = &tap_read_filterops;
1169 		break;
1170 	case EVFILT_WRITE:
1171 		kn->kn_fop = &tap_seltrue_filterops;
1172 		break;
1173 	default:
1174 		KERNEL_UNLOCK_ONE(NULL);
1175 		return (EINVAL);
1176 	}
1177 
1178 	kn->kn_hook = sc;
1179 	simple_lock(&sc->sc_kqlock);
1180 	SLIST_INSERT_HEAD(&sc->sc_rsel.sel_klist, kn, kn_selnext);
1181 	simple_unlock(&sc->sc_kqlock);
1182 	KERNEL_UNLOCK_ONE(NULL);
1183 	return (0);
1184 }
1185 
1186 static void
1187 tap_kqdetach(struct knote *kn)
1188 {
1189 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1190 
1191 	KERNEL_LOCK(1, NULL);
1192 	simple_lock(&sc->sc_kqlock);
1193 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
1194 	simple_unlock(&sc->sc_kqlock);
1195 	KERNEL_UNLOCK_ONE(NULL);
1196 }
1197 
1198 static int
1199 tap_kqread(struct knote *kn, long hint)
1200 {
1201 	struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1202 	struct ifnet *ifp = &sc->sc_ec.ec_if;
1203 	struct mbuf *m;
1204 	int s, rv;
1205 
1206 	KERNEL_LOCK(1, NULL);
1207 	s = splnet();
1208 	IFQ_POLL(&ifp->if_snd, m);
1209 
1210 	if (m == NULL)
1211 		kn->kn_data = 0;
1212 	else
1213 		kn->kn_data = m->m_pkthdr.len;
1214 	splx(s);
1215 	rv = (kn->kn_data != 0 ? 1 : 0);
1216 	KERNEL_UNLOCK_ONE(NULL);
1217 	return rv;
1218 }
1219 
1220 /*
1221  * sysctl management routines
1222  * You can set the address of an interface through:
1223  * net.link.tap.tap<number>
1224  *
1225  * Note the consistent use of tap_log in order to use
1226  * sysctl_teardown at unload time.
1227  *
1228  * In the kernel you will find a lot of SYSCTL_SETUP blocks.  Those
1229  * blocks register a function in a special section of the kernel
1230  * (called a link set) which is used at init_sysctl() time to cycle
1231  * through all those functions to create the kernel's sysctl tree.
1232  *
1233  * It is not possible to use link sets in a module, so the
1234  * easiest is to simply call our own setup routine at load time.
1235  *
1236  * In the SYSCTL_SETUP blocks you find in the kernel, nodes have the
1237  * CTLFLAG_PERMANENT flag, meaning they cannot be removed.  Once the
1238  * whole kernel sysctl tree is built, it is not possible to add any
1239  * permanent node.
1240  *
1241  * It should be noted that we're not saving the sysctlnode pointer
1242  * we are returned when creating the "tap" node.  That structure
1243  * cannot be trusted once out of the calling function, as it might
1244  * get reused.  So we just save the MIB number, and always give the
1245  * full path starting from the root for later calls to sysctl_createv
1246  * and sysctl_destroyv.
1247  */
1248 SYSCTL_SETUP(sysctl_tap_setup, "sysctl net.link.tap subtree setup")
1249 {
1250 	const struct sysctlnode *node;
1251 	int error = 0;
1252 
1253 	if ((error = sysctl_createv(clog, 0, NULL, NULL,
1254 	    CTLFLAG_PERMANENT,
1255 	    CTLTYPE_NODE, "net", NULL,
1256 	    NULL, 0, NULL, 0,
1257 	    CTL_NET, CTL_EOL)) != 0)
1258 		return;
1259 
1260 	if ((error = sysctl_createv(clog, 0, NULL, NULL,
1261 	    CTLFLAG_PERMANENT,
1262 	    CTLTYPE_NODE, "link", NULL,
1263 	    NULL, 0, NULL, 0,
1264 	    CTL_NET, AF_LINK, CTL_EOL)) != 0)
1265 		return;
1266 
1267 	/*
1268 	 * The first four parameters of sysctl_createv are for management.
1269 	 *
1270 	 * The four that follows, here starting with a '0' for the flags,
1271 	 * describe the node.
1272 	 *
1273 	 * The next series of four set its value, through various possible
1274 	 * means.
1275 	 *
1276 	 * Last but not least, the path to the node is described.  That path
1277 	 * is relative to the given root (third argument).  Here we're
1278 	 * starting from the root.
1279 	 */
1280 	if ((error = sysctl_createv(clog, 0, NULL, &node,
1281 	    CTLFLAG_PERMANENT,
1282 	    CTLTYPE_NODE, "tap", NULL,
1283 	    NULL, 0, NULL, 0,
1284 	    CTL_NET, AF_LINK, CTL_CREATE, CTL_EOL)) != 0)
1285 		return;
1286 	tap_node = node->sysctl_num;
1287 }
1288 
1289 /*
1290  * The helper functions make Andrew Brown's interface really
1291  * shine.  It makes possible to create value on the fly whether
1292  * the sysctl value is read or written.
1293  *
1294  * As shown as an example in the man page, the first step is to
1295  * create a copy of the node to have sysctl_lookup work on it.
1296  *
1297  * Here, we have more work to do than just a copy, since we have
1298  * to create the string.  The first step is to collect the actual
1299  * value of the node, which is a convenient pointer to the softc
1300  * of the interface.  From there we create the string and use it
1301  * as the value, but only for the *copy* of the node.
1302  *
1303  * Then we let sysctl_lookup do the magic, which consists in
1304  * setting oldp and newp as required by the operation.  When the
1305  * value is read, that means that the string will be copied to
1306  * the user, and when it is written, the new value will be copied
1307  * over in the addr array.
1308  *
1309  * If newp is NULL, the user was reading the value, so we don't
1310  * have anything else to do.  If a new value was written, we
1311  * have to check it.
1312  *
1313  * If it is incorrect, we can return an error and leave 'node' as
1314  * it is:  since it is a copy of the actual node, the change will
1315  * be forgotten.
1316  *
1317  * Upon a correct input, we commit the change to the ifnet
1318  * structure of our interface.
1319  */
1320 static int
1321 tap_sysctl_handler(SYSCTLFN_ARGS)
1322 {
1323 	struct sysctlnode node;
1324 	struct tap_softc *sc;
1325 	struct ifnet *ifp;
1326 	int error;
1327 	size_t len;
1328 	char addr[3 * ETHER_ADDR_LEN];
1329 	uint8_t enaddr[ETHER_ADDR_LEN];
1330 
1331 	node = *rnode;
1332 	sc = node.sysctl_data;
1333 	ifp = &sc->sc_ec.ec_if;
1334 	(void)ether_snprintf(addr, sizeof(addr), CLLADDR(ifp->if_sadl));
1335 	node.sysctl_data = addr;
1336 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1337 	if (error || newp == NULL)
1338 		return (error);
1339 
1340 	len = strlen(addr);
1341 	if (len < 11 || len > 17)
1342 		return (EINVAL);
1343 
1344 	/* Commit change */
1345 	if (ether_nonstatic_aton(enaddr, addr) != 0)
1346 		return (EINVAL);
1347 	if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
1348 	return (error);
1349 }
1350