1 /* 2 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * BASED ON: 27 * ------------------------------------------------------------------------- 28 * 29 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> 30 * Nottingham University 1987. 31 */ 32 33 /* 34 * $FreeBSD: src/sys/net/if_tap.c,v 1.3.2.3 2002/04/14 21:41:48 luigi Exp $ 35 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $ 36 */ 37 38 #include "opt_inet.h" 39 40 #include <sys/param.h> 41 #include <sys/conf.h> 42 #include <sys/device.h> 43 #include <sys/filedesc.h> 44 #include <sys/filio.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/proc.h> 49 #include <sys/priv.h> 50 #include <sys/signalvar.h> 51 #include <sys/socket.h> 52 #include <sys/sockio.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 #include <sys/ttycom.h> 56 #include <sys/uio.h> 57 #include <sys/vnode.h> 58 #include <sys/serialize.h> 59 60 #include <sys/thread2.h> 61 #include <sys/mplock2.h> 62 63 #include <net/bpf.h> 64 #include <net/ethernet.h> 65 #include <net/if.h> 66 #include <net/ifq_var.h> 67 #include <net/if_arp.h> 68 #include <net/if_clone.h> 69 #include <net/route.h> 70 #include <sys/devfs.h> 71 72 #include <netinet/in.h> 73 74 #include "if_tapvar.h" 75 #include "if_tap.h" 76 77 #define TAP_IFFLAGS (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST) 78 79 #define TAP_PREALLOCATED_UNITS 4 80 81 #define CDEV_NAME "tap" 82 #define TAPDEBUG if (tapdebug) if_printf 83 84 #define TAP "tap" 85 #define VMNET "vmnet" 86 #define VMNET_DEV_MASK 0x00010000 87 88 DEVFS_DECLARE_CLONE_BITMAP(tap); 89 90 /* module */ 91 static int tapmodevent (module_t, int, void *); 92 93 /* device */ 94 static struct tap_softc *tapcreate(int, cdev_t); 95 static void tapdestroy(struct tap_softc *); 96 97 /* clone */ 98 static int tap_clone_create(struct if_clone *, int, caddr_t); 99 static void tap_clone_destroy(struct ifnet *); 100 101 102 /* network interface */ 103 static void tapifstart (struct ifnet *); 104 static int tapifioctl (struct ifnet *, u_long, caddr_t, 105 struct ucred *); 106 static void tapifinit (void *); 107 static void tapifstop(struct tap_softc *, int); 108 static void tapifflags(struct tap_softc *); 109 110 /* character device */ 111 static d_open_t tapopen; 112 static d_clone_t tapclone; 113 static d_close_t tapclose; 114 static d_read_t tapread; 115 static d_write_t tapwrite; 116 static d_ioctl_t tapioctl; 117 static d_kqfilter_t tapkqfilter; 118 119 static struct dev_ops tap_ops = { 120 { CDEV_NAME, 0, 0 }, 121 .d_open = tapopen, 122 .d_close = tapclose, 123 .d_read = tapread, 124 .d_write = tapwrite, 125 .d_ioctl = tapioctl, 126 .d_kqfilter = tapkqfilter 127 }; 128 129 static int taprefcnt = 0; /* module ref. counter */ 130 static int taplastunit = -1; /* max. open unit number */ 131 static int tapdebug = 0; /* debug flag */ 132 static int tapuopen = 0; /* all user open() */ 133 static int tapuponopen = 0; /* IFF_UP */ 134 135 MALLOC_DECLARE(M_TAP); 136 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface"); 137 struct if_clone tap_cloner = IF_CLONE_INITIALIZER("tap", 138 tap_clone_create, tap_clone_destroy, 139 0, IF_MAXUNIT); 140 static SLIST_HEAD(,tap_softc) tap_listhead = 141 SLIST_HEAD_INITIALIZER(&tap_listhead); 142 143 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, ""); 144 SYSCTL_DECL(_net_link); 145 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0, 146 "Ethernet tunnel software network interface"); 147 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0, 148 "Allow user to open /dev/tap (based on node permissions)"); 149 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0, 150 "Bring interface up when /dev/tap is opened"); 151 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, ""); 152 153 DEV_MODULE(if_tap, tapmodevent, NULL); 154 155 /* 156 * tapmodevent 157 * 158 * module event handler 159 */ 160 static int 161 tapmodevent(module_t mod, int type, void *data) 162 { 163 static int attached = 0; 164 struct tap_softc *tp, *ntp; 165 int i; 166 167 switch (type) { 168 case MOD_LOAD: 169 if (attached) 170 return (EEXIST); 171 172 make_autoclone_dev(&tap_ops, &DEVFS_CLONE_BITMAP(tap), tapclone, 173 UID_ROOT, GID_WHEEL, 0600, "tap"); 174 SLIST_INIT(&tap_listhead); 175 if_clone_attach(&tap_cloner); 176 177 for (i = 0; i < TAP_PREALLOCATED_UNITS; ++i) { 178 make_dev(&tap_ops, i, UID_ROOT, GID_WHEEL, 179 0600, "tap%d", i); 180 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), i); 181 } 182 183 attached = 1; 184 break; 185 186 case MOD_UNLOAD: 187 if (taprefcnt > 0) 188 return (EBUSY); 189 190 if_clone_detach(&tap_cloner); 191 192 /* Maintain tap ifs in a local list */ 193 SLIST_FOREACH_MUTABLE(tp, &tap_listhead, tap_link, ntp) 194 tapdestroy(tp); 195 196 attached = 0; 197 198 devfs_clone_handler_del("tap"); 199 dev_ops_remove_all(&tap_ops); 200 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(tap)); 201 break; 202 203 default: 204 return (EOPNOTSUPP); 205 } 206 207 return (0); 208 } /* tapmodevent */ 209 210 211 /* 212 * tapcreate - create or clone an interface 213 */ 214 static struct tap_softc * 215 tapcreate(int unit, cdev_t dev) 216 { 217 const char *name = TAP; 218 struct ifnet *ifp; 219 struct tap_softc *tp; 220 uint8_t ether_addr[ETHER_ADDR_LEN]; 221 222 tp = kmalloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO); 223 dev->si_drv1 = tp; 224 tp->tap_dev = dev; 225 tp->tap_unit = unit; 226 227 reference_dev(dev); /* tp association */ 228 229 /* generate fake MAC address: 00 bd xx xx xx unit_no */ 230 ether_addr[0] = 0x00; 231 ether_addr[1] = 0xbd; 232 bcopy(&ticks, ðer_addr[2], 3); 233 ether_addr[5] = (u_char)unit; 234 235 /* fill the rest and attach interface */ 236 ifp = &tp->tap_if; 237 ifp->if_softc = tp; 238 239 if_initname(ifp, name, unit); 240 if (unit > taplastunit) 241 taplastunit = unit; 242 243 ifp->if_init = tapifinit; 244 ifp->if_start = tapifstart; 245 ifp->if_ioctl = tapifioctl; 246 ifp->if_mtu = ETHERMTU; 247 ifp->if_flags = TAP_IFFLAGS; 248 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 249 ifq_set_ready(&ifp->if_snd); 250 251 ether_ifattach(ifp, ether_addr, NULL); 252 253 tp->tap_flags |= TAP_INITED; 254 tp->tap_devq.ifq_maxlen = ifqmaxlen; 255 256 SLIST_INSERT_HEAD(&tap_listhead, tp, tap_link); 257 258 TAPDEBUG(ifp, "created. minor = %#x\n", minor(dev)); 259 return (tp); 260 } 261 262 static 263 struct tap_softc * 264 tapfind(int unit) 265 { 266 struct tap_softc *tp; 267 268 SLIST_FOREACH(tp, &tap_listhead, tap_link) { 269 if (tp->tap_unit == unit) 270 return(tp); 271 } 272 return (NULL); 273 } 274 275 /* 276 * tap_clone_create: 277 * 278 * Create a new tap instance via ifconfig. 279 */ 280 static int 281 tap_clone_create(struct if_clone *ifc __unused, int unit, 282 caddr_t param __unused) 283 { 284 struct tap_softc *tp; 285 cdev_t dev; 286 287 tp = tapfind(unit); 288 if (tp == NULL) { 289 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tap), unit)) { 290 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), unit); 291 dev = make_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL, 292 0600, "%s%d", TAP, unit); 293 } else { 294 dev = devfs_find_device_by_name("%s%d", TAP, unit); 295 } 296 297 KKASSERT(dev != NULL); 298 tp = tapcreate(unit, dev); 299 } 300 tp->tap_flags |= TAP_CLONE; 301 TAPDEBUG(&tp->tap_if, "clone created. minor = %#x tap_flags = 0x%x\n", 302 minor(tp->tap_dev), tp->tap_flags); 303 304 return (0); 305 } 306 307 /* 308 * tapopen 309 * 310 * to open tunnel. must be superuser 311 */ 312 static int 313 tapopen(struct dev_open_args *ap) 314 { 315 cdev_t dev = NULL; 316 struct tap_softc *tp = NULL; 317 struct ifnet *ifp = NULL; 318 int error; 319 320 if (tapuopen == 0 && 321 (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0) 322 return (error); 323 324 get_mplock(); 325 dev = ap->a_head.a_dev; 326 tp = dev->si_drv1; 327 if (tp == NULL) 328 tp = tapcreate(minor(dev), dev); 329 if (tp->tap_flags & TAP_OPEN) { 330 rel_mplock(); 331 return (EBUSY); 332 } 333 ifp = &tp->arpcom.ac_if; 334 335 if ((tp->tap_flags & TAP_CLONE) == 0) { 336 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp); 337 338 /* Announce the return of the interface. */ 339 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 340 } 341 342 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr)); 343 344 if (curthread->td_proc) 345 fsetown(curthread->td_proc->p_pid, &tp->tap_sigtd); 346 tp->tap_flags |= TAP_OPEN; 347 taprefcnt ++; 348 349 if (tapuponopen && (ifp->if_flags & IFF_UP) == 0) { 350 crit_enter(); 351 if_up(ifp); 352 crit_exit(); 353 354 ifnet_serialize_all(ifp); 355 tapifflags(tp); 356 ifnet_deserialize_all(ifp); 357 358 tp->tap_flags |= TAP_CLOSEDOWN; 359 } 360 361 TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n", 362 minor(tp->tap_dev), taprefcnt, taplastunit); 363 364 rel_mplock(); 365 return (0); 366 } 367 368 static int 369 tapclone(struct dev_clone_args *ap) 370 { 371 int unit; 372 373 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tap), 0); 374 ap->a_dev = make_only_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL, 375 0600, "%s%d", TAP, unit); 376 tapcreate(unit, ap->a_dev); 377 return (0); 378 } 379 380 /* 381 * tapclose 382 * 383 * close the device - mark i/f down & delete routing info 384 */ 385 static int 386 tapclose(struct dev_close_args *ap) 387 { 388 cdev_t dev = ap->a_head.a_dev; 389 struct tap_softc *tp = dev->si_drv1; 390 struct ifnet *ifp = &tp->tap_if; 391 int clear_flags = 0; 392 393 get_mplock(); 394 395 /* Junk all pending output */ 396 ifq_purge(&ifp->if_snd); 397 398 /* 399 * Do not bring the interface down, and do not anything with 400 * interface, if we are in VMnet mode. just close the device. 401 * 402 * If the interface is not cloned, we always bring it down. 403 * 404 * If the interface is cloned, then we bring it down during 405 * closing only if it was brought up during opening. 406 */ 407 if ((tp->tap_flags & TAP_VMNET) == 0 && 408 ((tp->tap_flags & TAP_CLONE) == 0 || 409 (tp->tap_flags & TAP_CLOSEDOWN))) { 410 if (ifp->if_flags & IFF_UP) 411 if_down(ifp); 412 clear_flags = 1; 413 } 414 ifnet_serialize_all(ifp); 415 tapifstop(tp, clear_flags); 416 ifnet_deserialize_all(ifp); 417 418 if ((tp->tap_flags & TAP_CLONE) == 0) { 419 if_purgeaddrs_nolink(ifp); 420 421 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp); 422 423 /* Announce the departure of the interface. */ 424 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 425 } 426 427 funsetown(tp->tap_sigio); 428 tp->tap_sigio = NULL; 429 KNOTE(&tp->tap_rkq.ki_note, 0); 430 431 tp->tap_flags &= ~TAP_OPEN; 432 funsetown(tp->tap_sigtd); 433 tp->tap_sigtd = NULL; 434 435 taprefcnt --; 436 if (taprefcnt < 0) { 437 taprefcnt = 0; 438 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. " 439 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt); 440 } 441 442 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n", 443 minor(tp->tap_dev), taprefcnt, taplastunit); 444 445 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS) 446 tapdestroy(tp); 447 448 rel_mplock(); 449 return (0); 450 } 451 452 /* 453 * tapdestroy: 454 * 455 * Destroy a tap instance. 456 */ 457 static void 458 tapdestroy(struct tap_softc *tp) 459 { 460 struct ifnet *ifp = &tp->arpcom.ac_if; 461 cdev_t dev; 462 463 TAPDEBUG(ifp, "destroyed. minor = %#x, refcnt = %d, taplastunit = %d\n", 464 minor(tp->tap_dev), taprefcnt, taplastunit); 465 466 ifnet_serialize_all(ifp); 467 tapifstop(tp, 1); 468 ifnet_deserialize_all(ifp); 469 470 ether_ifdetach(ifp); 471 SLIST_REMOVE(&tap_listhead, tp, tap_softc, tap_link); 472 473 dev = tp->tap_dev; 474 tp->tap_dev = NULL; 475 dev->si_drv1 = NULL; 476 477 release_dev(dev); /* tp association */ 478 479 /* 480 * Also destroy the cloned device 481 */ 482 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS) { 483 destroy_dev(dev); 484 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tap), tp->tap_unit); 485 } 486 487 kfree(tp, M_TAP); 488 489 taplastunit--; 490 } 491 492 /* 493 * tap_clone_destroy: 494 * 495 * Destroy a tap instance. 496 */ 497 static void 498 tap_clone_destroy(struct ifnet *ifp) 499 { 500 struct tap_softc *tp = ifp->if_softc; 501 502 TAPDEBUG(&tp->tap_if, "clone destroyed. minor = %#x tap_flags = 0x%x\n", 503 minor(tp->tap_dev), tp->tap_flags); 504 if (tp->tap_flags & TAP_CLONE) 505 tapdestroy(tp); 506 } 507 508 /* 509 * tapifinit 510 * 511 * Network interface initialization function (called with if serializer held) 512 * 513 * MPSAFE 514 */ 515 static void 516 tapifinit(void *xtp) 517 { 518 struct tap_softc *tp = xtp; 519 struct ifnet *ifp = &tp->tap_if; 520 521 TAPDEBUG(ifp, "initializing, minor = %#x tap_flags = 0x%x\n", 522 minor(tp->tap_dev), tp->tap_flags); 523 524 ASSERT_IFNET_SERIALIZED_ALL(ifp); 525 526 tapifstop(tp, 1); 527 528 ifp->if_flags |= IFF_RUNNING; 529 ifp->if_flags &= ~IFF_OACTIVE; 530 531 /* attempt to start output */ 532 tapifstart(ifp); 533 } 534 535 536 /* 537 * tapifioctl 538 * 539 * Process an ioctl request on network interface (called with if serializer 540 * held). 541 * 542 * MPSAFE 543 */ 544 static int 545 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 546 { 547 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc); 548 struct ifstat *ifs = NULL; 549 int dummy; 550 551 switch (cmd) { 552 case SIOCSIFADDR: 553 case SIOCGIFADDR: 554 case SIOCSIFMTU: 555 dummy = ether_ioctl(ifp, cmd, data); 556 return (dummy); 557 558 case SIOCSIFFLAGS: 559 tapifflags(tp); 560 break; 561 562 case SIOCADDMULTI: /* XXX -- just like vmnet does */ 563 case SIOCDELMULTI: 564 break; 565 566 case SIOCGIFSTATUS: 567 ifs = (struct ifstat *)data; 568 dummy = strlen(ifs->ascii); 569 if ((tp->tap_flags & TAP_OPEN) && 570 dummy < sizeof(ifs->ascii)) { 571 if (tp->tap_sigtd && tp->tap_sigtd->sio_proc) { 572 ksnprintf(ifs->ascii + dummy, 573 sizeof(ifs->ascii) - dummy, 574 "\tOpened by pid %d\n", 575 (int)tp->tap_sigtd->sio_proc->p_pid); 576 } else { 577 ksnprintf(ifs->ascii + dummy, 578 sizeof(ifs->ascii) - dummy, 579 "\tOpened by <unknown>\n"); 580 } 581 } 582 break; 583 584 default: 585 return (EINVAL); 586 } 587 588 return (0); 589 } 590 591 592 /* 593 * tapifstart 594 * 595 * Queue packets from higher level ready to put out (called with if serializer 596 * held) 597 * 598 * MPSAFE 599 */ 600 static void 601 tapifstart(struct ifnet *ifp) 602 { 603 struct tap_softc *tp = ifp->if_softc; 604 struct ifqueue *ifq; 605 struct mbuf *m; 606 int has_data = 0; 607 608 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev)); 609 610 /* 611 * do not junk pending output if we are in VMnet mode. 612 * XXX: can this do any harm because of queue overflow? 613 */ 614 615 if (((tp->tap_flags & TAP_VMNET) == 0) && 616 ((tp->tap_flags & TAP_READY) != TAP_READY)) { 617 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n", 618 minor(tp->tap_dev), tp->tap_flags); 619 ifq_purge(&ifp->if_snd); 620 return; 621 } 622 623 ifp->if_flags |= IFF_OACTIVE; 624 625 ifq = &tp->tap_devq; 626 while ((m = ifq_dequeue(&ifp->if_snd, NULL)) != NULL) { 627 if (IF_QFULL(ifq)) { 628 IF_DROP(ifq); 629 ifp->if_oerrors++; 630 m_freem(m); 631 } else { 632 IF_ENQUEUE(ifq, m); 633 ifp->if_opackets++; 634 has_data = 1; 635 } 636 } 637 638 if (has_data) { 639 if (tp->tap_flags & TAP_RWAIT) { 640 tp->tap_flags &= ~TAP_RWAIT; 641 wakeup((caddr_t)tp); 642 } 643 644 KNOTE(&tp->tap_rkq.ki_note, 0); 645 646 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) { 647 get_mplock(); 648 pgsigio(tp->tap_sigio, SIGIO, 0); 649 rel_mplock(); 650 } 651 } 652 653 ifp->if_flags &= ~IFF_OACTIVE; 654 } 655 656 657 /* 658 * tapioctl 659 * 660 * The ops interface is now pretty minimal. Called via fileops with nothing 661 * held. 662 * 663 * MPSAFE 664 */ 665 static int 666 tapioctl(struct dev_ioctl_args *ap) 667 { 668 cdev_t dev = ap->a_head.a_dev; 669 caddr_t data = ap->a_data; 670 struct tap_softc *tp = dev->si_drv1; 671 struct ifnet *ifp = &tp->tap_if; 672 struct tapinfo *tapp = NULL; 673 struct mbuf *mb; 674 short f; 675 int error; 676 677 ifnet_serialize_all(ifp); 678 error = 0; 679 680 switch (ap->a_cmd) { 681 case TAPSIFINFO: 682 tapp = (struct tapinfo *)data; 683 ifp->if_mtu = tapp->mtu; 684 ifp->if_type = tapp->type; 685 ifp->if_baudrate = tapp->baudrate; 686 break; 687 688 case TAPGIFINFO: 689 tapp = (struct tapinfo *)data; 690 tapp->mtu = ifp->if_mtu; 691 tapp->type = ifp->if_type; 692 tapp->baudrate = ifp->if_baudrate; 693 break; 694 695 case TAPSDEBUG: 696 tapdebug = *(int *)data; 697 break; 698 699 case TAPGDEBUG: 700 *(int *)data = tapdebug; 701 break; 702 703 case FIOASYNC: 704 if (*(int *)data) 705 tp->tap_flags |= TAP_ASYNC; 706 else 707 tp->tap_flags &= ~TAP_ASYNC; 708 break; 709 710 case FIONREAD: 711 *(int *)data = 0; 712 713 /* Take a look at devq first */ 714 IF_POLL(&tp->tap_devq, mb); 715 if (mb == NULL) 716 mb = ifq_poll(&ifp->if_snd); 717 718 if (mb != NULL) { 719 for(; mb != NULL; mb = mb->m_next) 720 *(int *)data += mb->m_len; 721 } 722 break; 723 724 case FIOSETOWN: 725 error = fsetown(*(int *)data, &tp->tap_sigio); 726 break; 727 728 case FIOGETOWN: 729 *(int *)data = fgetown(tp->tap_sigio); 730 break; 731 732 /* this is deprecated, FIOSETOWN should be used instead */ 733 case TIOCSPGRP: 734 error = fsetown(-(*(int *)data), &tp->tap_sigio); 735 break; 736 737 /* this is deprecated, FIOGETOWN should be used instead */ 738 case TIOCGPGRP: 739 *(int *)data = -fgetown(tp->tap_sigio); 740 break; 741 742 /* VMware/VMnet port ioctl's */ 743 744 case SIOCGIFFLAGS: /* get ifnet flags */ 745 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags)); 746 break; 747 748 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */ 749 f = *(short *)data; 750 f &= 0x0fff; 751 f &= ~IFF_CANTCHANGE; 752 f |= IFF_UP; 753 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE); 754 break; 755 756 case OSIOCGIFADDR: /* get MAC address of the remote side */ 757 case SIOCGIFADDR: 758 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr)); 759 break; 760 761 case SIOCSIFADDR: /* set MAC address of the remote side */ 762 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr)); 763 break; 764 765 default: 766 error = ENOTTY; 767 break; 768 } 769 ifnet_deserialize_all(ifp); 770 return (error); 771 } 772 773 774 /* 775 * tapread 776 * 777 * The ops read interface - reads a packet at a time, or at 778 * least as much of a packet as can be read. 779 * 780 * Called from the fileops interface with nothing held. 781 * 782 * MPSAFE 783 */ 784 static int 785 tapread(struct dev_read_args *ap) 786 { 787 cdev_t dev = ap->a_head.a_dev; 788 struct uio *uio = ap->a_uio; 789 struct tap_softc *tp = dev->si_drv1; 790 struct ifnet *ifp = &tp->tap_if; 791 struct mbuf *m0 = NULL; 792 int error = 0, len; 793 794 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev)); 795 796 if ((tp->tap_flags & TAP_READY) != TAP_READY) { 797 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n", 798 minor(tp->tap_dev), tp->tap_flags); 799 800 return (EHOSTDOWN); 801 } 802 803 tp->tap_flags &= ~TAP_RWAIT; 804 805 /* sleep until we get a packet */ 806 do { 807 ifnet_serialize_all(ifp); 808 IF_DEQUEUE(&tp->tap_devq, m0); 809 if (m0 == NULL) { 810 if (ap->a_ioflag & IO_NDELAY) { 811 ifnet_deserialize_all(ifp); 812 return (EWOULDBLOCK); 813 } 814 tp->tap_flags |= TAP_RWAIT; 815 tsleep_interlock(tp, PCATCH); 816 ifnet_deserialize_all(ifp); 817 error = tsleep(tp, PCATCH | PINTERLOCKED, "taprd", 0); 818 if (error) 819 return (error); 820 } else { 821 ifnet_deserialize_all(ifp); 822 } 823 } while (m0 == NULL); 824 825 BPF_MTAP(ifp, m0); 826 827 /* xfer packet to user space */ 828 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) { 829 len = (int)szmin(uio->uio_resid, m0->m_len); 830 if (len == 0) 831 break; 832 833 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio); 834 m0 = m_free(m0); 835 } 836 837 if (m0 != NULL) { 838 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n", 839 minor(tp->tap_dev)); 840 m_freem(m0); 841 } 842 843 return (error); 844 } 845 846 /* 847 * tapwrite 848 * 849 * The ops write interface - an atomic write is a packet - or else! 850 * 851 * Called from the fileops interface with nothing held. 852 * 853 * MPSAFE 854 */ 855 static int 856 tapwrite(struct dev_write_args *ap) 857 { 858 cdev_t dev = ap->a_head.a_dev; 859 struct uio *uio = ap->a_uio; 860 struct tap_softc *tp = dev->si_drv1; 861 struct ifnet *ifp = &tp->tap_if; 862 struct mbuf *top = NULL, **mp = NULL, *m = NULL; 863 int error = 0; 864 size_t tlen, mlen; 865 866 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev)); 867 868 if ((tp->tap_flags & TAP_READY) != TAP_READY) { 869 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n", 870 minor(tp->tap_dev), tp->tap_flags); 871 return (EHOSTDOWN); 872 } 873 874 if (uio->uio_resid == 0) 875 return (0); 876 877 if (uio->uio_resid > TAPMRU) { 878 TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n", 879 uio->uio_resid, minor(tp->tap_dev)); 880 881 return (EIO); 882 } 883 tlen = uio->uio_resid; 884 885 /* get a header mbuf */ 886 MGETHDR(m, MB_DONTWAIT, MT_DATA); 887 if (m == NULL) 888 return (ENOBUFS); 889 mlen = MHLEN; 890 891 top = 0; 892 mp = ⊤ 893 while ((error == 0) && (uio->uio_resid > 0)) { 894 m->m_len = (int)szmin(mlen, uio->uio_resid); 895 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio); 896 *mp = m; 897 mp = &m->m_next; 898 if (uio->uio_resid > 0) { 899 MGET(m, MB_DONTWAIT, MT_DATA); 900 if (m == NULL) { 901 error = ENOBUFS; 902 break; 903 } 904 mlen = MLEN; 905 } 906 } 907 if (error) { 908 ifp->if_ierrors ++; 909 if (top) 910 m_freem(top); 911 return (error); 912 } 913 914 top->m_pkthdr.len = (int)tlen; 915 top->m_pkthdr.rcvif = ifp; 916 917 /* 918 * Ethernet bridge and bpf are handled in ether_input 919 * 920 * adjust mbuf and give packet to the ether_input 921 */ 922 ifnet_serialize_all(ifp); 923 ifp->if_input(ifp, top); 924 ifp->if_ipackets ++; /* ibytes are counted in ether_input */ 925 ifnet_deserialize_all(ifp); 926 927 return (0); 928 } 929 930 /* 931 * tapkqfilter - called from the fileops interface with nothing held 932 * 933 * MPSAFE 934 */ 935 static int filt_tapread(struct knote *kn, long hint); 936 static int filt_tapwrite(struct knote *kn, long hint); 937 static void filt_tapdetach(struct knote *kn); 938 static struct filterops tapread_filtops = 939 { FILTEROP_ISFD, NULL, filt_tapdetach, filt_tapread }; 940 static struct filterops tapwrite_filtops = 941 { FILTEROP_ISFD, NULL, filt_tapdetach, filt_tapwrite }; 942 943 static int 944 tapkqfilter(struct dev_kqfilter_args *ap) 945 { 946 cdev_t dev = ap->a_head.a_dev; 947 struct knote *kn = ap->a_kn; 948 struct tap_softc *tp; 949 struct klist *list; 950 struct ifnet *ifp; 951 952 tp = dev->si_drv1; 953 list = &tp->tap_rkq.ki_note; 954 ifp = &tp->tap_if; 955 ap->a_result =0; 956 957 switch(kn->kn_filter) { 958 case EVFILT_READ: 959 kn->kn_fop = &tapread_filtops; 960 kn->kn_hook = (void *)tp; 961 break; 962 case EVFILT_WRITE: 963 kn->kn_fop = &tapwrite_filtops; 964 kn->kn_hook = (void *)tp; 965 break; 966 default: 967 ap->a_result = EOPNOTSUPP; 968 rel_mplock(); 969 return(0); 970 } 971 972 knote_insert(list, kn); 973 return(0); 974 } 975 976 static int 977 filt_tapread(struct knote *kn, long hint) 978 { 979 struct tap_softc *tp = (void *)kn->kn_hook; 980 981 if (IF_QEMPTY(&tp->tap_devq) == 0) /* XXX serializer */ 982 return(1); 983 else 984 return(0); 985 } 986 987 static int 988 filt_tapwrite(struct knote *kn, long hint) 989 { 990 /* Always ready for a write */ 991 return (1); 992 } 993 994 static void 995 filt_tapdetach(struct knote *kn) 996 { 997 struct tap_softc *tp = (void *)kn->kn_hook; 998 999 knote_remove(&tp->tap_rkq.ki_note, kn); 1000 } 1001 1002 static void 1003 tapifstop(struct tap_softc *tp, int clear_flags) 1004 { 1005 struct ifnet *ifp = &tp->tap_if; 1006 1007 ASSERT_IFNET_SERIALIZED_ALL(ifp); 1008 IF_DRAIN(&tp->tap_devq); 1009 tp->tap_flags &= ~TAP_CLOSEDOWN; 1010 if (clear_flags) 1011 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1012 } 1013 1014 static void 1015 tapifflags(struct tap_softc *tp) 1016 { 1017 struct ifnet *ifp = &tp->arpcom.ac_if; 1018 1019 ASSERT_IFNET_SERIALIZED_ALL(ifp); 1020 if ((tp->tap_flags & TAP_VMNET) == 0) { 1021 /* 1022 * Only for non-vmnet tap(4) 1023 */ 1024 if (ifp->if_flags & IFF_UP) { 1025 if ((ifp->if_flags & IFF_RUNNING) == 0) 1026 tapifinit(tp); 1027 } else { 1028 tapifstop(tp, 1); 1029 } 1030 } else { 1031 /* XXX */ 1032 } 1033 } 1034