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