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