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