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