1 /* $NetBSD: if_tun.c,v 1.53 2002/09/06 13:18:43 gehenna 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.53 2002/09/06 13:18:43 gehenna 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 break; 574 } 575 break; 576 577 case TUNSLMODE: 578 if (*(int *)data) 579 tp->tun_flags |= TUN_PREPADDR; 580 else 581 tp->tun_flags &= ~TUN_PREPADDR; 582 break; 583 584 case FIONBIO: 585 if (*(int *)data) 586 tp->tun_flags |= TUN_NBIO; 587 else 588 tp->tun_flags &= ~TUN_NBIO; 589 break; 590 591 case FIOASYNC: 592 if (*(int *)data) 593 tp->tun_flags |= TUN_ASYNC; 594 else 595 tp->tun_flags &= ~TUN_ASYNC; 596 break; 597 598 case FIONREAD: 599 s = splnet(); 600 if (tp->tun_if.if_snd.ifq_head) 601 *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len; 602 else 603 *(int *)data = 0; 604 splx(s); 605 break; 606 607 case TIOCSPGRP: 608 tp->tun_pgrp = *(int *)data; 609 break; 610 611 case TIOCGPGRP: 612 *(int *)data = tp->tun_pgrp; 613 break; 614 615 default: 616 simple_unlock(&tp->tun_lock); 617 return (ENOTTY); 618 } 619 simple_unlock(&tp->tun_lock); 620 return (0); 621 } 622 623 /* 624 * The cdevsw read interface - reads a packet at a time, or at 625 * least as much of a packet as can be read. 626 */ 627 int 628 tunread(dev, uio, ioflag) 629 dev_t dev; 630 struct uio *uio; 631 int ioflag; 632 { 633 struct tun_softc *tp; 634 struct ifnet *ifp; 635 struct mbuf *m, *m0; 636 int error=0, len, s, index; 637 638 tp = tun_find_unit(dev); 639 640 /* interface was "destroyed" already */ 641 if (tp == NULL) 642 return (ENXIO); 643 644 index = tp->tun_if.if_index; 645 ifp = &tp->tun_if; 646 647 TUNDEBUG ("%s: read\n", ifp->if_xname); 648 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 649 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags); 650 simple_unlock(&tp->tun_lock); 651 return EHOSTDOWN; 652 } 653 654 tp->tun_flags &= ~TUN_RWAIT; 655 656 s = splnet(); 657 do { 658 IFQ_DEQUEUE(&ifp->if_snd, m0); 659 if (m0 == 0) { 660 if (tp->tun_flags & TUN_NBIO) { 661 splx(s); 662 simple_unlock(&tp->tun_lock); 663 return (EWOULDBLOCK); 664 } 665 tp->tun_flags |= TUN_RWAIT; 666 simple_unlock(&tp->tun_lock); 667 if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) { 668 splx(s); 669 return (EINTR); 670 } else { 671 /* 672 * Maybe the interface was destroyed while 673 * we were sleeping, so let's ensure that 674 * we're looking at the same (valid) tun 675 * interface before looping. 676 */ 677 tp = tun_find_unit(dev); 678 if (tp == NULL || 679 tp->tun_if.if_index != index) { 680 splx(s); 681 if (tp) 682 simple_unlock(&tp->tun_lock); 683 return (ENXIO); 684 } 685 } 686 } 687 } while (m0 == 0); 688 splx(s); 689 690 while (m0 && uio->uio_resid > 0 && error == 0) { 691 len = min(uio->uio_resid, m0->m_len); 692 if (len != 0) 693 error = uiomove(mtod(m0, caddr_t), len, uio); 694 MFREE(m0, m); 695 m0 = m; 696 } 697 698 if (m0) { 699 TUNDEBUG("Dropping mbuf\n"); 700 m_freem(m0); 701 } 702 if (error) 703 ifp->if_ierrors++; 704 simple_unlock(&tp->tun_lock); 705 return (error); 706 } 707 708 /* 709 * the cdevsw write interface - an atomic write is a packet - or else! 710 */ 711 int 712 tunwrite(dev, uio, ioflag) 713 dev_t dev; 714 struct uio *uio; 715 int ioflag; 716 { 717 struct tun_softc *tp; 718 struct ifnet *ifp; 719 struct mbuf *top, **mp, *m; 720 struct ifqueue *ifq; 721 struct sockaddr dst; 722 int isr, error=0, s, tlen, mlen; 723 724 tp = tun_find_unit(dev); 725 726 /* interface was "destroyed" already */ 727 if (tp == NULL) 728 return (ENXIO); 729 730 ifp = &tp->tun_if; 731 732 TUNDEBUG("%s: tunwrite\n", ifp->if_xname); 733 734 if (tp->tun_flags & TUN_PREPADDR) { 735 if (uio->uio_resid < sizeof(dst)) { 736 simple_unlock(&tp->tun_lock); 737 return (EIO); 738 } 739 error = uiomove((caddr_t)&dst, sizeof(dst), uio); 740 if (dst.sa_len > sizeof(dst)) { 741 /* Duh.. */ 742 char discard; 743 int n = dst.sa_len - sizeof(dst); 744 while (n--) 745 if ((error = uiomove(&discard, 1, uio)) != 0) { 746 simple_unlock(&tp->tun_lock); 747 return (error); 748 } 749 } 750 } else { 751 #ifdef INET 752 dst.sa_family = AF_INET; 753 #endif 754 } 755 756 if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) { 757 TUNDEBUG("%s: len=%lu!\n", ifp->if_xname, 758 (unsigned long)uio->uio_resid); 759 simple_unlock(&tp->tun_lock); 760 return (EIO); 761 } 762 763 switch (dst.sa_family) { 764 #ifdef INET 765 case AF_INET: 766 ifq = &ipintrq; 767 isr = NETISR_IP; 768 break; 769 #endif 770 default: 771 simple_unlock(&tp->tun_lock); 772 return (EAFNOSUPPORT); 773 } 774 775 tlen = uio->uio_resid; 776 777 /* get a header mbuf */ 778 MGETHDR(m, M_DONTWAIT, MT_DATA); 779 if (m == NULL) { 780 simple_unlock(&tp->tun_lock); 781 return (ENOBUFS); 782 } 783 mlen = MHLEN; 784 785 top = 0; 786 mp = ⊤ 787 while (error == 0 && uio->uio_resid > 0) { 788 m->m_len = min(mlen, uio->uio_resid); 789 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 790 *mp = m; 791 mp = &m->m_next; 792 if (uio->uio_resid > 0) { 793 MGET (m, M_DONTWAIT, MT_DATA); 794 if (m == 0) { 795 error = ENOBUFS; 796 break; 797 } 798 mlen = MLEN; 799 } 800 } 801 if (error) { 802 if (top) 803 m_freem (top); 804 ifp->if_ierrors++; 805 simple_unlock(&tp->tun_lock); 806 return (error); 807 } 808 809 top->m_pkthdr.len = tlen; 810 top->m_pkthdr.rcvif = ifp; 811 812 #if NBPFILTER > 0 813 if (ifp->if_bpf) { 814 /* 815 * We need to prepend the address family as 816 * a four byte field. Cons up a dummy header 817 * to pacify bpf. This is safe because bpf 818 * will only read from the mbuf (i.e., it won't 819 * try to free it or keep a pointer to it). 820 */ 821 struct mbuf m; 822 u_int32_t af = AF_INET; 823 824 m.m_next = top; 825 m.m_len = sizeof(af); 826 m.m_data = (char *)⁡ 827 828 bpf_mtap(ifp->if_bpf, &m); 829 } 830 #endif 831 832 s = splnet(); 833 if (IF_QFULL(ifq)) { 834 IF_DROP(ifq); 835 splx(s); 836 ifp->if_collisions++; 837 m_freem(top); 838 simple_unlock(&tp->tun_lock); 839 return (ENOBUFS); 840 } 841 IF_ENQUEUE(ifq, top); 842 splx(s); 843 ifp->if_ipackets++; 844 schednetisr(isr); 845 simple_unlock(&tp->tun_lock); 846 return (error); 847 } 848 849 #ifdef ALTQ 850 /* 851 * Start packet transmission on the interface. 852 * when the interface queue is rate-limited by ALTQ or TBR, 853 * if_start is needed to drain packets from the queue in order 854 * to notify readers when outgoing packets become ready. 855 */ 856 static void 857 tunstart(ifp) 858 struct ifnet *ifp; 859 { 860 struct tun_softc *tp = ifp->if_softc; 861 struct mbuf *m; 862 struct proc *p; 863 864 if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd)) 865 return; 866 867 IFQ_POLL(&ifp->if_snd, m); 868 if (m != NULL) { 869 if (tp->tun_flags & TUN_RWAIT) { 870 tp->tun_flags &= ~TUN_RWAIT; 871 wakeup((caddr_t)tp); 872 } 873 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 874 if (tp->tun_pgrp > 0) 875 gsignal(tp->tun_pgrp, SIGIO); 876 else if ((p = pfind(-tp->tun_pgrp)) != NULL) 877 psignal(p, SIGIO); 878 } 879 selwakeup(&tp->tun_rsel); 880 } 881 } 882 #endif /* ALTQ */ 883 /* 884 * tunpoll - the poll interface, this is only useful on reads 885 * really. The write detect always returns true, write never blocks 886 * anyway, it either accepts the packet or drops it. 887 */ 888 int 889 tunpoll(dev, events, p) 890 dev_t dev; 891 int events; 892 struct proc *p; 893 { 894 struct tun_softc *tp; 895 struct ifnet *ifp; 896 int s, revents = 0; 897 898 tp = tun_find_unit(dev); 899 900 /* interface was "destroyed" already */ 901 if (tp == NULL) 902 return (0); 903 904 ifp = &tp->tun_if; 905 906 s = splnet(); 907 TUNDEBUG("%s: tunpoll\n", ifp->if_xname); 908 909 if (events & (POLLIN | POLLRDNORM)) { 910 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) { 911 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname, 912 ifp->if_snd.ifq_len); 913 revents |= events & (POLLIN | POLLRDNORM); 914 } else { 915 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname); 916 selrecord(p, &tp->tun_rsel); 917 } 918 } 919 920 if (events & (POLLOUT | POLLWRNORM)) 921 revents |= events & (POLLOUT | POLLWRNORM); 922 923 splx(s); 924 simple_unlock(&tp->tun_lock); 925 return (revents); 926 } 927