1 /* $NetBSD: if_tun.c,v 1.55 2002/09/23 05:51:11 simonb Exp $ */ 2 3 /* 4 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> 5 * Nottingham University 1987. 6 * 7 * This source may be freely distributed, however I would be interested 8 * in any changes that are made. 9 * 10 * This driver takes packets off the IP i/f and hands them up to a 11 * user process to have its wicked way with. This driver has its 12 * roots in a similar driver written by Phil Cockcroft (formerly) at 13 * UCL. This driver is based much more on read/write/poll mode of 14 * operation though. 15 */ 16 17 #include <sys/cdefs.h> 18 __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.55 2002/09/23 05:51:11 simonb Exp $"); 19 20 #include "tun.h" 21 22 #include "opt_inet.h" 23 #include "opt_ns.h" 24 25 #include <sys/param.h> 26 #include <sys/proc.h> 27 #include <sys/systm.h> 28 #include <sys/mbuf.h> 29 #include <sys/buf.h> 30 #include <sys/protosw.h> 31 #include <sys/socket.h> 32 #include <sys/ioctl.h> 33 #include <sys/errno.h> 34 #include <sys/syslog.h> 35 #include <sys/select.h> 36 #include <sys/poll.h> 37 #include <sys/file.h> 38 #include <sys/signalvar.h> 39 #include <sys/conf.h> 40 41 #include <machine/cpu.h> 42 43 #include <net/if.h> 44 #include <net/if_ether.h> 45 #include <net/netisr.h> 46 #include <net/route.h> 47 48 49 #ifdef INET 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> 52 #include <netinet/in_var.h> 53 #include <netinet/ip.h> 54 #include <netinet/if_inarp.h> 55 #endif 56 57 #ifdef NS 58 #include <netns/ns.h> 59 #include <netns/ns_if.h> 60 #endif 61 62 #include "bpfilter.h" 63 #if NBPFILTER > 0 64 #include <sys/time.h> 65 #include <net/bpf.h> 66 #endif 67 68 #include <net/if_tun.h> 69 70 #define TUNDEBUG if (tundebug) printf 71 int tundebug = 0; 72 73 extern int ifqmaxlen; 74 void tunattach __P((int)); 75 LIST_HEAD(, tun_softc) tun_softc_list; 76 static struct simplelock tun_softc_lock; 77 78 int tun_ioctl __P((struct ifnet *, u_long, caddr_t)); 79 int tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *, 80 struct rtentry *rt)); 81 int tun_clone_create __P((struct if_clone *, int)); 82 void tun_clone_destroy __P((struct ifnet *)); 83 84 struct if_clone tun_cloner = 85 IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy); 86 87 static void tunattach0 __P((struct tun_softc *)); 88 static void tuninit __P((struct tun_softc *)); 89 #ifdef ALTQ 90 static void tunstart __P((struct ifnet *)); 91 #endif 92 static struct tun_softc *tun_find_unit __P((dev_t)); 93 94 dev_type_open(tunopen); 95 dev_type_close(tunclose); 96 dev_type_read(tunread); 97 dev_type_write(tunwrite); 98 dev_type_ioctl(tunioctl); 99 dev_type_poll(tunpoll); 100 101 const struct cdevsw tun_cdevsw = { 102 tunopen, tunclose, tunread, tunwrite, tunioctl, 103 nostop, notty, tunpoll, nommap, 104 }; 105 106 void 107 tunattach(unused) 108 int unused; 109 { 110 111 simple_lock_init(&tun_softc_lock); 112 LIST_INIT(&tun_softc_list); 113 if_clone_attach(&tun_cloner); 114 } 115 116 int 117 tun_clone_create(ifc, unit) 118 struct if_clone *ifc; 119 int unit; 120 { 121 struct tun_softc *sc; 122 123 sc = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK); 124 (void)memset(sc, 0, sizeof(struct tun_softc)); 125 126 (void)snprintf(sc->tun_if.if_xname, sizeof(sc->tun_if.if_xname), 127 "%s%d", ifc->ifc_name, unit); 128 sc->tun_unit = unit; 129 simple_lock_init(&sc->tun_lock); 130 131 tunattach0(sc); 132 133 simple_lock(&tun_softc_lock); 134 LIST_INSERT_HEAD(&tun_softc_list, sc, tun_list); 135 simple_unlock(&tun_softc_lock); 136 137 return (0); 138 } 139 140 void 141 tunattach0(sc) 142 struct tun_softc *sc; 143 { 144 struct ifnet *ifp = (void *)sc; 145 146 sc->tun_flags = TUN_INITED; 147 148 ifp = &sc->tun_if; 149 ifp->if_softc = sc; 150 ifp->if_mtu = TUNMTU; 151 ifp->if_ioctl = tun_ioctl; 152 ifp->if_output = tun_output; 153 #ifdef ALTQ 154 ifp->if_start = tunstart; 155 #endif 156 ifp->if_flags = IFF_POINTOPOINT; 157 ifp->if_snd.ifq_maxlen = ifqmaxlen; 158 ifp->if_collisions = 0; 159 ifp->if_ierrors = 0; 160 ifp->if_oerrors = 0; 161 ifp->if_ipackets = 0; 162 ifp->if_opackets = 0; 163 ifp->if_dlt = DLT_NULL; 164 IFQ_SET_READY(&ifp->if_snd); 165 if_attach(ifp); 166 if_alloc_sadl(ifp); 167 #if NBPFILTER > 0 168 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 169 #endif 170 } 171 172 void 173 tun_clone_destroy(ifp) 174 struct ifnet *ifp; 175 { 176 struct tun_softc *tp = (void *)ifp; 177 struct proc *p; 178 179 simple_lock(&tun_softc_lock); 180 simple_lock(&tp->tun_lock); 181 LIST_REMOVE(tp, tun_list); 182 simple_unlock(&tp->tun_lock); 183 simple_unlock(&tun_softc_lock); 184 185 if (tp->tun_flags & TUN_RWAIT) { 186 tp->tun_flags &= ~TUN_RWAIT; 187 wakeup((caddr_t)tp); 188 } 189 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 190 if (tp->tun_pgrp > 0) 191 gsignal(tp->tun_pgrp, SIGIO); 192 else if ((p = pfind(-tp->tun_pgrp)) != NULL) 193 psignal(p, SIGIO); 194 } 195 selwakeup(&tp->tun_rsel); 196 197 #if NBPFILTER > 0 198 bpfdetach(ifp); 199 #endif 200 if_detach(ifp); 201 202 free(tp, M_DEVBUF); 203 } 204 205 static struct tun_softc * 206 tun_find_unit(dev) 207 dev_t dev; 208 { 209 struct tun_softc *tp; 210 int unit = minor(dev); 211 212 simple_lock(&tun_softc_lock); 213 LIST_FOREACH(tp, &tun_softc_list, tun_list) 214 if (unit == tp->tun_unit) 215 break; 216 if (tp) 217 simple_lock(&tp->tun_lock); 218 simple_unlock(&tun_softc_lock); 219 220 return (tp); 221 } 222 223 /* 224 * tunnel open - must be superuser & the device must be 225 * configured in 226 */ 227 int 228 tunopen(dev, flag, mode, p) 229 dev_t dev; 230 int flag, mode; 231 struct proc *p; 232 { 233 struct ifnet *ifp; 234 struct tun_softc *tp; 235 int error; 236 237 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 238 return (error); 239 240 if (NTUN < 1) 241 return (ENXIO); 242 243 tp = tun_find_unit(dev); 244 245 if (!tp) { 246 (void)tun_clone_create(&tun_cloner, minor(dev)); 247 tp = tun_find_unit(dev); 248 } 249 250 if (!tp) 251 return (ENXIO); 252 253 if (tp->tun_flags & TUN_OPEN) { 254 simple_unlock(&tp->tun_lock); 255 return (EBUSY); 256 } 257 258 ifp = &tp->tun_if; 259 tp->tun_flags |= TUN_OPEN; 260 TUNDEBUG("%s: open\n", ifp->if_xname); 261 simple_unlock(&tp->tun_lock); 262 return (0); 263 } 264 265 /* 266 * tunclose - close the device - mark i/f down & delete 267 * routing info 268 */ 269 int 270 tunclose(dev, flag, mode, p) 271 dev_t dev; 272 int flag; 273 int mode; 274 struct proc *p; 275 { 276 int s; 277 struct tun_softc *tp; 278 struct ifnet *ifp; 279 280 tp = tun_find_unit(dev); 281 282 /* interface was "destroyed" before the close */ 283 if (tp == NULL) 284 return (0); 285 286 ifp = &tp->tun_if; 287 288 tp->tun_flags &= ~TUN_OPEN; 289 290 /* 291 * junk all pending output 292 */ 293 s = splnet(); 294 IFQ_PURGE(&ifp->if_snd); 295 splx(s); 296 297 if (ifp->if_flags & IFF_UP) { 298 s = splnet(); 299 if_down(ifp); 300 if (ifp->if_flags & IFF_RUNNING) { 301 /* find internet addresses and delete routes */ 302 struct ifaddr *ifa; 303 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 304 #ifdef INET 305 if (ifa->ifa_addr->sa_family == AF_INET) { 306 rtinit(ifa, (int)RTM_DELETE, 307 tp->tun_flags & TUN_DSTADDR 308 ? RTF_HOST 309 : 0); 310 } 311 #endif 312 } 313 } 314 splx(s); 315 } 316 tp->tun_pgrp = 0; 317 selwakeup(&tp->tun_rsel); 318 319 TUNDEBUG ("%s: closed\n", ifp->if_xname); 320 simple_unlock(&tp->tun_lock); 321 return (0); 322 } 323 324 static void 325 tuninit(tp) 326 struct tun_softc *tp; 327 { 328 struct ifnet *ifp = &tp->tun_if; 329 struct ifaddr *ifa; 330 331 TUNDEBUG("%s: tuninit\n", ifp->if_xname); 332 333 ifp->if_flags |= IFF_UP | IFF_RUNNING; 334 335 tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR); 336 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 337 #ifdef INET 338 if (ifa->ifa_addr->sa_family == AF_INET) { 339 struct sockaddr_in *sin; 340 341 sin = satosin(ifa->ifa_addr); 342 if (sin && sin->sin_addr.s_addr) 343 tp->tun_flags |= TUN_IASET; 344 345 if (ifp->if_flags & IFF_POINTOPOINT) { 346 sin = satosin(ifa->ifa_dstaddr); 347 if (sin && sin->sin_addr.s_addr) 348 tp->tun_flags |= TUN_DSTADDR; 349 } 350 } 351 #endif 352 } 353 354 return; 355 } 356 357 /* 358 * Process an ioctl request. 359 */ 360 int 361 tun_ioctl(ifp, cmd, data) 362 struct ifnet *ifp; 363 u_long cmd; 364 caddr_t data; 365 { 366 int error = 0, s; 367 struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc); 368 369 simple_lock(&tp->tun_lock); 370 371 s = splnet(); 372 switch(cmd) { 373 case SIOCSIFADDR: 374 tuninit((struct tun_softc *)(ifp->if_softc)); 375 TUNDEBUG("%s: address set\n", ifp->if_xname); 376 break; 377 case SIOCSIFDSTADDR: 378 tuninit((struct tun_softc *)(ifp->if_softc)); 379 TUNDEBUG("%s: destination address set\n", ifp->if_xname); 380 break; 381 case SIOCSIFBRDADDR: 382 TUNDEBUG("%s: broadcast address set\n", ifp->if_xname); 383 break; 384 case SIOCSIFMTU: { 385 struct ifreq *ifr = (struct ifreq *) data; 386 if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) { 387 error = EINVAL; 388 break; 389 } 390 TUNDEBUG("%s: interface mtu set\n", ifp->if_xname); 391 ifp->if_mtu = ifr->ifr_mtu; 392 break; 393 } 394 case SIOCADDMULTI: 395 case SIOCDELMULTI: { 396 struct ifreq *ifr = (struct ifreq *) data; 397 if (ifr == 0) { 398 error = EAFNOSUPPORT; /* XXX */ 399 break; 400 } 401 switch (ifr->ifr_addr.sa_family) { 402 403 #ifdef INET 404 case AF_INET: 405 break; 406 #endif 407 408 default: 409 error = EAFNOSUPPORT; 410 break; 411 } 412 break; 413 } 414 case SIOCSIFFLAGS: 415 break; 416 default: 417 error = EINVAL; 418 } 419 splx(s); 420 simple_unlock(&tp->tun_lock); 421 return (error); 422 } 423 424 /* 425 * tun_output - queue packets from higher level ready to put out. 426 */ 427 int 428 tun_output(ifp, m0, dst, rt) 429 struct ifnet *ifp; 430 struct mbuf *m0; 431 struct sockaddr *dst; 432 struct rtentry *rt; 433 { 434 struct tun_softc *tp = ifp->if_softc; 435 struct proc *p; 436 #ifdef INET 437 int s; 438 int error; 439 #endif 440 ALTQ_DECL(struct altq_pktattr pktattr;) 441 442 simple_lock(&tp->tun_lock); 443 TUNDEBUG ("%s: tun_output\n", ifp->if_xname); 444 445 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 446 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, 447 tp->tun_flags); 448 m_freem (m0); 449 simple_unlock(&tp->tun_lock); 450 return (EHOSTDOWN); 451 } 452 453 /* 454 * if the queueing discipline needs packet classification, 455 * do it before prepending link headers. 456 */ 457 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr); 458 459 #if NBPFILTER > 0 460 if (ifp->if_bpf) { 461 /* 462 * We need to prepend the address family as 463 * a four byte field. Cons up a dummy header 464 * to pacify bpf. This is safe because bpf 465 * will only read from the mbuf (i.e., it won't 466 * try to free it or keep a pointer to it). 467 */ 468 struct mbuf m; 469 u_int32_t af = dst->sa_family; 470 471 m.m_next = m0; 472 m.m_len = sizeof(af); 473 m.m_data = (char *)⁡ 474 475 bpf_mtap(ifp->if_bpf, &m); 476 } 477 #endif 478 479 switch(dst->sa_family) { 480 #ifdef INET 481 case AF_INET: 482 if (tp->tun_flags & TUN_PREPADDR) { 483 /* Simple link-layer header */ 484 M_PREPEND(m0, dst->sa_len, M_DONTWAIT); 485 if (m0 == NULL) { 486 IF_DROP(&ifp->if_snd); 487 simple_unlock(&tp->tun_lock); 488 return (ENOBUFS); 489 } 490 bcopy(dst, mtod(m0, char *), dst->sa_len); 491 } 492 /* FALLTHROUGH */ 493 case AF_UNSPEC: 494 s = splnet(); 495 IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error); 496 if (error) { 497 splx(s); 498 ifp->if_collisions++; 499 return (error); 500 } 501 splx(s); 502 ifp->if_opackets++; 503 break; 504 #endif 505 default: 506 m_freem(m0); 507 simple_unlock(&tp->tun_lock); 508 return (EAFNOSUPPORT); 509 } 510 511 if (tp->tun_flags & TUN_RWAIT) { 512 tp->tun_flags &= ~TUN_RWAIT; 513 wakeup((caddr_t)tp); 514 } 515 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 516 if (tp->tun_pgrp > 0) 517 gsignal(tp->tun_pgrp, SIGIO); 518 else if ((p = pfind(-tp->tun_pgrp)) != NULL) 519 psignal(p, SIGIO); 520 } 521 selwakeup(&tp->tun_rsel); 522 simple_unlock(&tp->tun_lock); 523 return (0); 524 } 525 526 /* 527 * the cdevsw interface is now pretty minimal. 528 */ 529 int 530 tunioctl(dev, cmd, data, flag, p) 531 dev_t dev; 532 u_long cmd; 533 caddr_t data; 534 int flag; 535 struct proc *p; 536 { 537 int s; 538 struct tun_softc *tp; 539 540 tp = tun_find_unit(dev); 541 542 /* interface was "destroyed" already */ 543 if (tp == NULL) 544 return (ENXIO); 545 546 switch (cmd) { 547 case TUNSDEBUG: 548 tundebug = *(int *)data; 549 break; 550 551 case TUNGDEBUG: 552 *(int *)data = tundebug; 553 break; 554 555 case TUNSIFMODE: 556 switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) { 557 case IFF_POINTOPOINT: 558 case IFF_BROADCAST: 559 s = splnet(); 560 if (tp->tun_if.if_flags & IFF_UP) { 561 splx(s); 562 simple_unlock(&tp->tun_lock); 563 return (EBUSY); 564 } 565 tp->tun_if.if_flags &= 566 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); 567 tp->tun_if.if_flags |= *(int *)data; 568 splx(s); 569 break; 570 default: 571 simple_unlock(&tp->tun_lock); 572 return (EINVAL); 573 } 574 break; 575 576 case TUNSLMODE: 577 if (*(int *)data) 578 tp->tun_flags |= TUN_PREPADDR; 579 else 580 tp->tun_flags &= ~TUN_PREPADDR; 581 break; 582 583 case FIONBIO: 584 if (*(int *)data) 585 tp->tun_flags |= TUN_NBIO; 586 else 587 tp->tun_flags &= ~TUN_NBIO; 588 break; 589 590 case FIOASYNC: 591 if (*(int *)data) 592 tp->tun_flags |= TUN_ASYNC; 593 else 594 tp->tun_flags &= ~TUN_ASYNC; 595 break; 596 597 case FIONREAD: 598 s = splnet(); 599 if (tp->tun_if.if_snd.ifq_head) 600 *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len; 601 else 602 *(int *)data = 0; 603 splx(s); 604 break; 605 606 case TIOCSPGRP: 607 tp->tun_pgrp = *(int *)data; 608 break; 609 610 case TIOCGPGRP: 611 *(int *)data = tp->tun_pgrp; 612 break; 613 614 default: 615 simple_unlock(&tp->tun_lock); 616 return (ENOTTY); 617 } 618 simple_unlock(&tp->tun_lock); 619 return (0); 620 } 621 622 /* 623 * The cdevsw read interface - reads a packet at a time, or at 624 * least as much of a packet as can be read. 625 */ 626 int 627 tunread(dev, uio, ioflag) 628 dev_t dev; 629 struct uio *uio; 630 int ioflag; 631 { 632 struct tun_softc *tp; 633 struct ifnet *ifp; 634 struct mbuf *m, *m0; 635 int error=0, len, s, index; 636 637 tp = tun_find_unit(dev); 638 639 /* interface was "destroyed" already */ 640 if (tp == NULL) 641 return (ENXIO); 642 643 index = tp->tun_if.if_index; 644 ifp = &tp->tun_if; 645 646 TUNDEBUG ("%s: read\n", ifp->if_xname); 647 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 648 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags); 649 simple_unlock(&tp->tun_lock); 650 return EHOSTDOWN; 651 } 652 653 tp->tun_flags &= ~TUN_RWAIT; 654 655 s = splnet(); 656 do { 657 IFQ_DEQUEUE(&ifp->if_snd, m0); 658 if (m0 == 0) { 659 if (tp->tun_flags & TUN_NBIO) { 660 splx(s); 661 simple_unlock(&tp->tun_lock); 662 return (EWOULDBLOCK); 663 } 664 tp->tun_flags |= TUN_RWAIT; 665 simple_unlock(&tp->tun_lock); 666 if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) { 667 splx(s); 668 return (EINTR); 669 } else { 670 /* 671 * Maybe the interface was destroyed while 672 * we were sleeping, so let's ensure that 673 * we're looking at the same (valid) tun 674 * interface before looping. 675 */ 676 tp = tun_find_unit(dev); 677 if (tp == NULL || 678 tp->tun_if.if_index != index) { 679 splx(s); 680 if (tp) 681 simple_unlock(&tp->tun_lock); 682 return (ENXIO); 683 } 684 } 685 } 686 } while (m0 == 0); 687 splx(s); 688 689 while (m0 && uio->uio_resid > 0 && error == 0) { 690 len = min(uio->uio_resid, m0->m_len); 691 if (len != 0) 692 error = uiomove(mtod(m0, caddr_t), len, uio); 693 MFREE(m0, m); 694 m0 = m; 695 } 696 697 if (m0) { 698 TUNDEBUG("Dropping mbuf\n"); 699 m_freem(m0); 700 } 701 if (error) 702 ifp->if_ierrors++; 703 simple_unlock(&tp->tun_lock); 704 return (error); 705 } 706 707 /* 708 * the cdevsw write interface - an atomic write is a packet - or else! 709 */ 710 int 711 tunwrite(dev, uio, ioflag) 712 dev_t dev; 713 struct uio *uio; 714 int ioflag; 715 { 716 struct tun_softc *tp; 717 struct ifnet *ifp; 718 struct mbuf *top, **mp, *m; 719 struct ifqueue *ifq; 720 struct sockaddr dst; 721 int isr, error=0, s, tlen, mlen; 722 723 tp = tun_find_unit(dev); 724 725 /* interface was "destroyed" already */ 726 if (tp == NULL) 727 return (ENXIO); 728 729 ifp = &tp->tun_if; 730 731 TUNDEBUG("%s: tunwrite\n", ifp->if_xname); 732 733 if (tp->tun_flags & TUN_PREPADDR) { 734 if (uio->uio_resid < sizeof(dst)) { 735 simple_unlock(&tp->tun_lock); 736 return (EIO); 737 } 738 error = uiomove((caddr_t)&dst, sizeof(dst), uio); 739 if (dst.sa_len > sizeof(dst)) { 740 /* Duh.. */ 741 char discard; 742 int n = dst.sa_len - sizeof(dst); 743 while (n--) 744 if ((error = uiomove(&discard, 1, uio)) != 0) { 745 simple_unlock(&tp->tun_lock); 746 return (error); 747 } 748 } 749 } else { 750 #ifdef INET 751 dst.sa_family = AF_INET; 752 #endif 753 } 754 755 if (uio->uio_resid > TUNMTU) { 756 TUNDEBUG("%s: len=%lu!\n", ifp->if_xname, 757 (unsigned long)uio->uio_resid); 758 simple_unlock(&tp->tun_lock); 759 return (EIO); 760 } 761 762 switch (dst.sa_family) { 763 #ifdef INET 764 case AF_INET: 765 ifq = &ipintrq; 766 isr = NETISR_IP; 767 break; 768 #endif 769 default: 770 simple_unlock(&tp->tun_lock); 771 return (EAFNOSUPPORT); 772 } 773 774 tlen = uio->uio_resid; 775 776 /* get a header mbuf */ 777 MGETHDR(m, M_DONTWAIT, MT_DATA); 778 if (m == NULL) { 779 simple_unlock(&tp->tun_lock); 780 return (ENOBUFS); 781 } 782 mlen = MHLEN; 783 784 top = 0; 785 mp = ⊤ 786 while (error == 0 && uio->uio_resid > 0) { 787 m->m_len = min(mlen, uio->uio_resid); 788 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 789 *mp = m; 790 mp = &m->m_next; 791 if (uio->uio_resid > 0) { 792 MGET (m, M_DONTWAIT, MT_DATA); 793 if (m == 0) { 794 error = ENOBUFS; 795 break; 796 } 797 mlen = MLEN; 798 } 799 } 800 if (error) { 801 if (top) 802 m_freem (top); 803 ifp->if_ierrors++; 804 simple_unlock(&tp->tun_lock); 805 return (error); 806 } 807 808 top->m_pkthdr.len = tlen; 809 top->m_pkthdr.rcvif = ifp; 810 811 #if NBPFILTER > 0 812 if (ifp->if_bpf) { 813 /* 814 * We need to prepend the address family as 815 * a four byte field. Cons up a dummy header 816 * to pacify bpf. This is safe because bpf 817 * will only read from the mbuf (i.e., it won't 818 * try to free it or keep a pointer to it). 819 */ 820 struct mbuf m; 821 u_int32_t af = AF_INET; 822 823 m.m_next = top; 824 m.m_len = sizeof(af); 825 m.m_data = (char *)⁡ 826 827 bpf_mtap(ifp->if_bpf, &m); 828 } 829 #endif 830 831 s = splnet(); 832 if (IF_QFULL(ifq)) { 833 IF_DROP(ifq); 834 splx(s); 835 ifp->if_collisions++; 836 m_freem(top); 837 simple_unlock(&tp->tun_lock); 838 return (ENOBUFS); 839 } 840 IF_ENQUEUE(ifq, top); 841 splx(s); 842 ifp->if_ipackets++; 843 schednetisr(isr); 844 simple_unlock(&tp->tun_lock); 845 return (error); 846 } 847 848 #ifdef ALTQ 849 /* 850 * Start packet transmission on the interface. 851 * when the interface queue is rate-limited by ALTQ or TBR, 852 * if_start is needed to drain packets from the queue in order 853 * to notify readers when outgoing packets become ready. 854 */ 855 static void 856 tunstart(ifp) 857 struct ifnet *ifp; 858 { 859 struct tun_softc *tp = ifp->if_softc; 860 struct mbuf *m; 861 struct proc *p; 862 863 if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd)) 864 return; 865 866 IFQ_POLL(&ifp->if_snd, m); 867 if (m != NULL) { 868 if (tp->tun_flags & TUN_RWAIT) { 869 tp->tun_flags &= ~TUN_RWAIT; 870 wakeup((caddr_t)tp); 871 } 872 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 873 if (tp->tun_pgrp > 0) 874 gsignal(tp->tun_pgrp, SIGIO); 875 else if ((p = pfind(-tp->tun_pgrp)) != NULL) 876 psignal(p, SIGIO); 877 } 878 selwakeup(&tp->tun_rsel); 879 } 880 } 881 #endif /* ALTQ */ 882 /* 883 * tunpoll - the poll interface, this is only useful on reads 884 * really. The write detect always returns true, write never blocks 885 * anyway, it either accepts the packet or drops it. 886 */ 887 int 888 tunpoll(dev, events, p) 889 dev_t dev; 890 int events; 891 struct proc *p; 892 { 893 struct tun_softc *tp; 894 struct ifnet *ifp; 895 int s, revents = 0; 896 897 tp = tun_find_unit(dev); 898 899 /* interface was "destroyed" already */ 900 if (tp == NULL) 901 return (0); 902 903 ifp = &tp->tun_if; 904 905 s = splnet(); 906 TUNDEBUG("%s: tunpoll\n", ifp->if_xname); 907 908 if (events & (POLLIN | POLLRDNORM)) { 909 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) { 910 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname, 911 ifp->if_snd.ifq_len); 912 revents |= events & (POLLIN | POLLRDNORM); 913 } else { 914 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname); 915 selrecord(p, &tp->tun_rsel); 916 } 917 } 918 919 if (events & (POLLOUT | POLLWRNORM)) 920 revents |= events & (POLLOUT | POLLWRNORM); 921 922 splx(s); 923 simple_unlock(&tp->tun_lock); 924 return (revents); 925 } 926