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