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