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