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