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 * $DragonFly: src/sys/net/tap/if_tap.c,v 1.15 2004/07/23 07:16:31 joerg Exp $ 36 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $ 37 */ 38 39 #include "opt_inet.h" 40 41 #include <sys/param.h> 42 #include <sys/conf.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/poll.h> 49 #include <sys/proc.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 59 #include <net/bpf.h> 60 #include <net/ethernet.h> 61 #include <net/if.h> 62 #include <net/if_arp.h> 63 #include <net/route.h> 64 65 #include <netinet/in.h> 66 67 #include "if_tapvar.h" 68 #include "if_tap.h" 69 70 71 #define CDEV_NAME "tap" 72 #define CDEV_MAJOR 149 73 #define TAPDEBUG if (tapdebug) printf 74 75 #define TAP "tap" 76 #define VMNET "vmnet" 77 #define VMNET_DEV_MASK 0x00010000 78 79 /* module */ 80 static int tapmodevent (module_t, int, void *); 81 82 /* device */ 83 static void tapcreate (dev_t); 84 85 /* network interface */ 86 static void tapifstart (struct ifnet *); 87 static int tapifioctl (struct ifnet *, u_long, caddr_t, 88 struct ucred *); 89 static void tapifinit (void *); 90 91 /* character device */ 92 static d_open_t tapopen; 93 static d_close_t tapclose; 94 static d_read_t tapread; 95 static d_write_t tapwrite; 96 static d_ioctl_t tapioctl; 97 static d_poll_t tappoll; 98 99 static struct cdevsw tap_cdevsw = { 100 /* dev name */ CDEV_NAME, 101 /* dev major */ CDEV_MAJOR, 102 /* flags */ 0, 103 /* port */ NULL, 104 /* clone */ NULL, 105 106 /* open */ tapopen, 107 /* close */ tapclose, 108 /* read */ tapread, 109 /* write */ tapwrite, 110 /* ioctl */ tapioctl, 111 /* poll */ tappoll, 112 /* mmap */ nommap, 113 /* startegy */ nostrategy, 114 /* dump */ nodump, 115 /* psize */ nopsize 116 }; 117 118 static int taprefcnt = 0; /* module ref. counter */ 119 static int taplastunit = -1; /* max. open unit number */ 120 static int tapdebug = 0; /* debug flag */ 121 122 MALLOC_DECLARE(M_TAP); 123 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface"); 124 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, ""); 125 DEV_MODULE(if_tap, tapmodevent, NULL); 126 127 /* 128 * tapmodevent 129 * 130 * module event handler 131 */ 132 static int 133 tapmodevent(mod, type, data) 134 module_t mod; 135 int type; 136 void *data; 137 { 138 static int attached = 0; 139 struct ifnet *ifp = NULL; 140 int unit, s; 141 142 switch (type) { 143 case MOD_LOAD: 144 if (attached) 145 return (EEXIST); 146 147 cdevsw_add(&tap_cdevsw, 0, 0); 148 attached = 1; 149 break; 150 151 case MOD_UNLOAD: 152 if (taprefcnt > 0) 153 return (EBUSY); 154 155 cdevsw_remove(&tap_cdevsw, 0, 0); 156 157 /* XXX: maintain tap ifs in a local list */ 158 unit = 0; 159 while (unit <= taplastunit) { 160 s = splimp(); 161 TAILQ_FOREACH(ifp, &ifnet, if_link) 162 if ((strcmp(ifp->if_dname, TAP) == 0) || 163 (strcmp(ifp->if_dname, VMNET) == 0)) 164 if (ifp->if_dunit == unit) 165 break; 166 splx(s); 167 168 if (ifp != NULL) { 169 struct tap_softc *tp = ifp->if_softc; 170 171 TAPDEBUG("detaching %s. minor = %#x, " \ 172 "taplastunit = %d\n", 173 ifp->if_xname, minor(tp->tap_dev), 174 taplastunit); 175 176 s = splimp(); 177 ether_ifdetach(ifp); 178 splx(s); 179 destroy_dev(tp->tap_dev); 180 free(tp, M_TAP); 181 } 182 else 183 unit ++; 184 } 185 186 attached = 0; 187 break; 188 189 default: 190 return (EOPNOTSUPP); 191 } 192 193 return (0); 194 } /* tapmodevent */ 195 196 197 /* 198 * tapcreate 199 * 200 * to create interface 201 */ 202 static void 203 tapcreate(dev) 204 dev_t dev; 205 { 206 struct ifnet *ifp = NULL; 207 struct tap_softc *tp = NULL; 208 uint8_t ether_addr[ETHER_ADDR_LEN]; 209 int unit, s; 210 char *name = NULL; 211 212 /* allocate driver storage and create device */ 213 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK); 214 bzero(tp, sizeof(*tp)); 215 216 /* select device: tap or vmnet */ 217 if (minor(dev) & VMNET_DEV_MASK) { 218 name = VMNET; 219 unit = lminor(dev) & 0xff; 220 tp->tap_flags |= TAP_VMNET; 221 } 222 else { 223 name = TAP; 224 unit = lminor(dev); 225 } 226 227 tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 228 0600, "%s%d", name, unit); 229 tp->tap_dev->si_drv1 = dev->si_drv1 = tp; 230 reference_dev(tp->tap_dev); /* so we can destroy it later */ 231 232 /* generate fake MAC address: 00 bd xx xx xx unit_no */ 233 ether_addr[0] = 0x00; 234 ether_addr[1] = 0xbd; 235 bcopy(&ticks, ether_addr, 4); 236 ether_addr[5] = (u_char)unit; 237 238 /* fill the rest and attach interface */ 239 ifp = &tp->tap_if; 240 ifp->if_softc = tp; 241 242 if_initname(ifp, name, unit); 243 if (unit > taplastunit) 244 taplastunit = unit; 245 246 ifp->if_init = tapifinit; 247 ifp->if_start = tapifstart; 248 ifp->if_ioctl = tapifioctl; 249 ifp->if_mtu = ETHERMTU; 250 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST); 251 ifp->if_snd.ifq_maxlen = ifqmaxlen; 252 253 s = splimp(); 254 ether_ifattach(ifp, ether_addr); 255 splx(s); 256 257 tp->tap_flags |= TAP_INITED; 258 259 TAPDEBUG("interface %s created. minor = %#x\n", 260 ifp->if_xname, minor(tp->tap_dev)); 261 } /* tapcreate */ 262 263 264 /* 265 * tapopen 266 * 267 * to open tunnel. must be superuser 268 */ 269 static int 270 tapopen(dev_t dev, int flag, int mode, d_thread_t *td) 271 { 272 struct tap_softc *tp = NULL; 273 int error; 274 275 KKASSERT(p != NULL); 276 277 if ((error = suser(td)) != 0) 278 return (error); 279 280 tp = dev->si_drv1; 281 if (tp == NULL) { 282 tapcreate(dev); 283 tp = dev->si_drv1; 284 } 285 286 if (tp->tap_flags & TAP_OPEN) 287 return (EBUSY); 288 289 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr)); 290 291 tp->tap_td = td; 292 tp->tap_flags |= TAP_OPEN; 293 taprefcnt ++; 294 295 TAPDEBUG("%s is open. minor = %#x, refcnt = %d, taplastunit = %d\n", 296 tp->tap_if.if_xname, 297 minor(tp->tap_dev), taprefcnt, taplastunit); 298 299 return (0); 300 } /* tapopen */ 301 302 303 /* 304 * tapclose 305 * 306 * close the device - mark i/f down & delete routing info 307 */ 308 static int 309 tapclose(dev_t dev, int foo, int bar, d_thread_t *td) 310 { 311 int s; 312 struct tap_softc *tp = dev->si_drv1; 313 struct ifnet *ifp = &tp->tap_if; 314 struct mbuf *m = NULL; 315 316 /* junk all pending output */ 317 318 s = splimp(); 319 do { 320 IF_DEQUEUE(&ifp->if_snd, m); 321 if (m != NULL) 322 m_freem(m); 323 } while (m != NULL); 324 splx(s); 325 326 /* 327 * do not bring the interface down, and do not anything with 328 * interface, if we are in VMnet mode. just close the device. 329 */ 330 331 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) { 332 s = splimp(); 333 if_down(ifp); 334 if (ifp->if_flags & IFF_RUNNING) { 335 /* find internet addresses and delete routes */ 336 struct ifaddr *ifa = NULL; 337 338 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 339 if (ifa->ifa_addr->sa_family == AF_INET) { 340 rtinit(ifa, (int)RTM_DELETE, 0); 341 342 /* remove address from interface */ 343 bzero(ifa->ifa_addr, 344 sizeof(*(ifa->ifa_addr))); 345 bzero(ifa->ifa_dstaddr, 346 sizeof(*(ifa->ifa_dstaddr))); 347 bzero(ifa->ifa_netmask, 348 sizeof(*(ifa->ifa_netmask))); 349 } 350 } 351 352 ifp->if_flags &= ~IFF_RUNNING; 353 } 354 splx(s); 355 } 356 357 funsetown(tp->tap_sigio); 358 selwakeup(&tp->tap_rsel); 359 360 tp->tap_flags &= ~TAP_OPEN; 361 tp->tap_td = NULL; 362 363 taprefcnt --; 364 if (taprefcnt < 0) { 365 taprefcnt = 0; 366 printf("%s minor = %#x, refcnt = %d is out of sync. " \ 367 "set refcnt to 0\n", ifp->if_xname, 368 minor(tp->tap_dev), taprefcnt); 369 } 370 371 TAPDEBUG("%s is closed. minor = %#x, refcnt = %d, taplastunit = %d\n", 372 ifp->if_xname, minor(tp->tap_dev), 373 taprefcnt, taplastunit); 374 375 return (0); 376 } /* tapclose */ 377 378 379 /* 380 * tapifinit 381 * 382 * network interface initialization function 383 */ 384 static void 385 tapifinit(xtp) 386 void *xtp; 387 { 388 struct tap_softc *tp = (struct tap_softc *)xtp; 389 struct ifnet *ifp = &tp->tap_if; 390 391 TAPDEBUG("initializing %s, minor = %#x\n", 392 ifp->if_xname, minor(tp->tap_dev)); 393 394 ifp->if_flags |= IFF_RUNNING; 395 ifp->if_flags &= ~IFF_OACTIVE; 396 397 /* attempt to start output */ 398 tapifstart(ifp); 399 } /* tapifinit */ 400 401 402 /* 403 * tapifioctl 404 * 405 * Process an ioctl request on network interface 406 */ 407 int 408 tapifioctl(ifp, cmd, data, cr) 409 struct ifnet *ifp; 410 u_long cmd; 411 caddr_t data; 412 struct ucred *cr; 413 { 414 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc); 415 struct ifstat *ifs = NULL; 416 int s, dummy; 417 418 switch (cmd) { 419 case SIOCSIFADDR: 420 case SIOCGIFADDR: 421 case SIOCSIFMTU: 422 s = splimp(); 423 dummy = ether_ioctl(ifp, cmd, data); 424 splx(s); 425 return (dummy); 426 427 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */ 428 case SIOCADDMULTI: 429 case SIOCDELMULTI: 430 break; 431 432 case SIOCGIFSTATUS: 433 s = splimp(); 434 ifs = (struct ifstat *)data; 435 dummy = strlen(ifs->ascii); 436 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) { 437 if (tp->tap_td->td_proc) { 438 snprintf(ifs->ascii + dummy, 439 sizeof(ifs->ascii) - dummy, 440 "\tOpened by pid %d\n", 441 (int)tp->tap_td->td_proc->p_pid); 442 } else { 443 snprintf(ifs->ascii + dummy, 444 sizeof(ifs->ascii) - dummy, 445 "\tOpened by td %p\n", tp->tap_td); 446 } 447 } 448 splx(s); 449 break; 450 451 default: 452 return (EINVAL); 453 } 454 455 return (0); 456 } /* tapifioctl */ 457 458 459 /* 460 * tapifstart 461 * 462 * queue packets from higher level ready to put out 463 */ 464 static void 465 tapifstart(ifp) 466 struct ifnet *ifp; 467 { 468 struct tap_softc *tp = ifp->if_softc; 469 int s; 470 471 TAPDEBUG("%s starting, minor = %#x\n", 472 ifp->if_xname, minor(tp->tap_dev)); 473 474 /* 475 * do not junk pending output if we are in VMnet mode. 476 * XXX: can this do any harm because of queue overflow? 477 */ 478 479 if (((tp->tap_flags & TAP_VMNET) == 0) && 480 ((tp->tap_flags & TAP_READY) != TAP_READY)) { 481 struct mbuf *m = NULL; 482 483 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n", 484 ifp->if_xname, 485 minor(tp->tap_dev), tp->tap_flags); 486 487 s = splimp(); 488 do { 489 IF_DEQUEUE(&ifp->if_snd, m); 490 if (m != NULL) 491 m_freem(m); 492 ifp->if_oerrors ++; 493 } while (m != NULL); 494 splx(s); 495 496 return; 497 } 498 499 s = splimp(); 500 ifp->if_flags |= IFF_OACTIVE; 501 502 if (ifp->if_snd.ifq_len != 0) { 503 if (tp->tap_flags & TAP_RWAIT) { 504 tp->tap_flags &= ~TAP_RWAIT; 505 wakeup((caddr_t)tp); 506 } 507 508 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) 509 pgsigio(tp->tap_sigio, SIGIO, 0); 510 511 selwakeup(&tp->tap_rsel); 512 ifp->if_opackets ++; /* obytes are counted in ether_output */ 513 } 514 515 ifp->if_flags &= ~IFF_OACTIVE; 516 splx(s); 517 } /* tapifstart */ 518 519 520 /* 521 * tapioctl 522 * 523 * the cdevsw interface is now pretty minimal 524 */ 525 static int 526 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td) 527 { 528 struct tap_softc *tp = dev->si_drv1; 529 struct ifnet *ifp = &tp->tap_if; 530 struct tapinfo *tapp = NULL; 531 int s; 532 533 switch (cmd) { 534 case TAPSIFINFO: 535 s = splimp(); 536 tapp = (struct tapinfo *)data; 537 ifp->if_mtu = tapp->mtu; 538 ifp->if_type = tapp->type; 539 ifp->if_baudrate = tapp->baudrate; 540 splx(s); 541 break; 542 543 case TAPGIFINFO: 544 tapp = (struct tapinfo *)data; 545 tapp->mtu = ifp->if_mtu; 546 tapp->type = ifp->if_type; 547 tapp->baudrate = ifp->if_baudrate; 548 break; 549 550 case TAPSDEBUG: 551 tapdebug = *(int *)data; 552 break; 553 554 case TAPGDEBUG: 555 *(int *)data = tapdebug; 556 break; 557 558 case FIONBIO: 559 break; 560 561 case FIOASYNC: 562 s = splimp(); 563 if (*(int *)data) 564 tp->tap_flags |= TAP_ASYNC; 565 else 566 tp->tap_flags &= ~TAP_ASYNC; 567 splx(s); 568 break; 569 570 case FIONREAD: 571 s = splimp(); 572 if (ifp->if_snd.ifq_head) { 573 struct mbuf *mb = ifp->if_snd.ifq_head; 574 575 for(*(int *)data = 0;mb != NULL;mb = mb->m_next) 576 *(int *)data += mb->m_len; 577 } 578 else 579 *(int *)data = 0; 580 splx(s); 581 break; 582 583 case FIOSETOWN: 584 return (fsetown(*(int *)data, &tp->tap_sigio)); 585 586 case FIOGETOWN: 587 *(int *)data = fgetown(tp->tap_sigio); 588 return (0); 589 590 /* this is deprecated, FIOSETOWN should be used instead */ 591 case TIOCSPGRP: 592 return (fsetown(-(*(int *)data), &tp->tap_sigio)); 593 594 /* this is deprecated, FIOGETOWN should be used instead */ 595 case TIOCGPGRP: 596 *(int *)data = -fgetown(tp->tap_sigio); 597 return (0); 598 599 /* VMware/VMnet port ioctl's */ 600 601 case SIOCGIFFLAGS: /* get ifnet flags */ 602 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags)); 603 break; 604 605 case VMIO_SIOCSIFFLAGS: { /* VMware/VMnet SIOCSIFFLAGS */ 606 short f = *(short *)data; 607 608 f &= 0x0fff; 609 f &= ~IFF_CANTCHANGE; 610 f |= IFF_UP; 611 612 s = splimp(); 613 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE); 614 splx(s); 615 } break; 616 617 case OSIOCGIFADDR: /* get MAC address of the remote side */ 618 case SIOCGIFADDR: 619 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr)); 620 break; 621 622 case SIOCSIFADDR: /* set MAC address of the remote side */ 623 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr)); 624 break; 625 626 default: 627 return (ENOTTY); 628 } 629 return (0); 630 } /* tapioctl */ 631 632 633 /* 634 * tapread 635 * 636 * the cdevsw read interface - reads a packet at a time, or at 637 * least as much of a packet as can be read 638 */ 639 static int 640 tapread(dev, uio, flag) 641 dev_t dev; 642 struct uio *uio; 643 int flag; 644 { 645 struct tap_softc *tp = dev->si_drv1; 646 struct ifnet *ifp = &tp->tap_if; 647 struct mbuf *m0 = NULL; 648 int error = 0, len, s; 649 650 TAPDEBUG("%s reading, minor = %#x\n", 651 ifp->if_xname, minor(tp->tap_dev)); 652 653 if ((tp->tap_flags & TAP_READY) != TAP_READY) { 654 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n", 655 ifp->if_xname, 656 minor(tp->tap_dev), tp->tap_flags); 657 658 return (EHOSTDOWN); 659 } 660 661 tp->tap_flags &= ~TAP_RWAIT; 662 663 /* sleep until we get a packet */ 664 do { 665 s = splimp(); 666 IF_DEQUEUE(&ifp->if_snd, m0); 667 splx(s); 668 669 if (m0 == NULL) { 670 if (flag & IO_NDELAY) 671 return (EWOULDBLOCK); 672 673 tp->tap_flags |= TAP_RWAIT; 674 error = tsleep((caddr_t)tp, PCATCH, "taprd", 0); 675 if (error) 676 return (error); 677 } 678 } while (m0 == NULL); 679 680 /* feed packet to bpf */ 681 if (ifp->if_bpf != NULL) 682 bpf_mtap(ifp, m0); 683 684 /* xfer packet to user space */ 685 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) { 686 len = min(uio->uio_resid, m0->m_len); 687 if (len == 0) 688 break; 689 690 error = uiomove(mtod(m0, caddr_t), len, uio); 691 m0 = m_free(m0); 692 } 693 694 if (m0 != NULL) { 695 TAPDEBUG("%s dropping mbuf, minor = %#x\n", 696 ifp->if_xname, minor(tp->tap_dev)); 697 m_freem(m0); 698 } 699 700 return (error); 701 } /* tapread */ 702 703 704 /* 705 * tapwrite 706 * 707 * the cdevsw write interface - an atomic write is a packet - or else! 708 */ 709 static int 710 tapwrite(dev, uio, flag) 711 dev_t dev; 712 struct uio *uio; 713 int flag; 714 { 715 struct tap_softc *tp = dev->si_drv1; 716 struct ifnet *ifp = &tp->tap_if; 717 struct mbuf *top = NULL, **mp = NULL, *m = NULL; 718 int error = 0, tlen, mlen; 719 720 TAPDEBUG("%s writting, minor = %#x\n", 721 ifp->if_xname, minor(tp->tap_dev)); 722 723 if (uio->uio_resid == 0) 724 return (0); 725 726 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) { 727 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n", 728 ifp->if_xname, 729 uio->uio_resid, minor(tp->tap_dev)); 730 731 return (EIO); 732 } 733 tlen = uio->uio_resid; 734 735 /* get a header mbuf */ 736 MGETHDR(m, MB_DONTWAIT, MT_DATA); 737 if (m == NULL) 738 return (ENOBUFS); 739 mlen = MHLEN; 740 741 top = 0; 742 mp = ⊤ 743 while ((error == 0) && (uio->uio_resid > 0)) { 744 m->m_len = min(mlen, uio->uio_resid); 745 error = uiomove(mtod(m, caddr_t), m->m_len, uio); 746 *mp = m; 747 mp = &m->m_next; 748 if (uio->uio_resid > 0) { 749 MGET(m, MB_DONTWAIT, MT_DATA); 750 if (m == NULL) { 751 error = ENOBUFS; 752 break; 753 } 754 mlen = MLEN; 755 } 756 } 757 if (error) { 758 ifp->if_ierrors ++; 759 if (top) 760 m_freem(top); 761 return (error); 762 } 763 764 top->m_pkthdr.len = tlen; 765 top->m_pkthdr.rcvif = ifp; 766 767 /* 768 * Ethernet bridge and bpf are handled in ether_input 769 * 770 * adjust mbuf and give packet to the ether_input 771 */ 772 773 (*ifp->if_input)(ifp, top); 774 ifp->if_ipackets ++; /* ibytes are counted in ether_input */ 775 776 return (0); 777 } /* tapwrite */ 778 779 780 /* 781 * tappoll 782 * 783 * the poll interface, this is only useful on reads 784 * really. the write detect always returns true, write never blocks 785 * anyway, it either accepts the packet or drops it 786 */ 787 static int 788 tappoll(dev_t dev, int events, d_thread_t *td) 789 { 790 struct tap_softc *tp = dev->si_drv1; 791 struct ifnet *ifp = &tp->tap_if; 792 int s, revents = 0; 793 794 TAPDEBUG("%s polling, minor = %#x\n", 795 ifp->if_xname, minor(tp->tap_dev)); 796 797 s = splimp(); 798 if (events & (POLLIN | POLLRDNORM)) { 799 if (ifp->if_snd.ifq_len > 0) { 800 TAPDEBUG("%s have data in queue. len = %d, " \ 801 "minor = %#x\n", ifp->if_xname, 802 ifp->if_snd.ifq_len, minor(tp->tap_dev)); 803 804 revents |= (events & (POLLIN | POLLRDNORM)); 805 } 806 else { 807 TAPDEBUG("%s waiting for data, minor = %#x\n", 808 ifp->if_xname, minor(tp->tap_dev)); 809 810 selrecord(td, &tp->tap_rsel); 811 } 812 } 813 814 if (events & (POLLOUT | POLLWRNORM)) 815 revents |= (events & (POLLOUT | POLLWRNORM)); 816 817 splx(s); 818 return (revents); 819 } /* tappoll */ 820