1 /* $OpenBSD: if_tun.c,v 1.51 2003/12/02 06:00:18 mickey Exp $ */ 2 /* $NetBSD: if_tun.c,v 1.24 1996/05/07 02:40:48 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1988, Julian Onions <Julian.Onions@nexor.co.uk> 6 * Nottingham University 1987. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * This driver takes packets off the IP i/f and hands them up to a 32 * user process to have its wicked way with. This driver has its 33 * roots in a similar driver written by Phil Cockcroft (formerly) at 34 * UCL. This driver is based much more on read/write/select mode of 35 * operation though. 36 */ 37 38 /* #define TUN_DEBUG 9 */ 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/proc.h> 43 #include <sys/systm.h> 44 #include <sys/mbuf.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/ioctl.h> 48 #include <sys/errno.h> 49 #include <sys/syslog.h> 50 #include <sys/select.h> 51 #include <sys/file.h> 52 #include <sys/time.h> 53 #include <sys/device.h> 54 #include <sys/vnode.h> 55 #include <sys/signalvar.h> 56 #include <sys/poll.h> 57 #include <sys/conf.h> 58 59 #include <machine/cpu.h> 60 61 #include <net/if.h> 62 #include <net/if_types.h> 63 #include <net/netisr.h> 64 #include <net/route.h> 65 66 #ifdef INET 67 #include <netinet/in.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/in_var.h> 70 #include <netinet/ip.h> 71 /* #include <netinet/if_ether.h> */ 72 #endif 73 74 #ifdef NS 75 #include <netns/ns.h> 76 #include <netns/ns_if.h> 77 #endif 78 79 #ifdef IPX 80 #include <netipx/ipx.h> 81 #include <netipx/ipx_if.h> 82 #endif 83 84 #ifdef NETATALK 85 #include <netatalk/at.h> 86 #include <netatalk/at_var.h> 87 #endif 88 89 #ifdef ISO 90 #include <netiso/iso.h> 91 #include <netiso/iso_var.h> 92 #endif 93 94 #include "bpfilter.h" 95 #if NBPFILTER > 0 96 #include <net/bpf.h> 97 #endif 98 99 #include <net/if_tun.h> 100 101 struct tun_softc { 102 struct ifnet tun_if; /* the interface */ 103 u_short tun_flags; /* misc flags */ 104 pid_t tun_pgid; /* the process group - if any */ 105 uid_t tun_siguid; /* uid for process that set tun_pgid */ 106 uid_t tun_sigeuid; /* euid for process that set tun_pgid */ 107 struct selinfo tun_rsel; /* read select */ 108 struct selinfo tun_wsel; /* write select (not used) */ 109 }; 110 111 #ifdef TUN_DEBUG 112 int tundebug = TUN_DEBUG; 113 #define TUNDEBUG(a) (tundebug? printf a : 0) 114 #else 115 #define TUNDEBUG(a) /* (tundebug? printf a : 0) */ 116 #endif 117 118 struct tun_softc *tunctl; 119 int ntun; 120 121 extern int ifqmaxlen; 122 123 void tunattach(int); 124 int tunopen(dev_t, int, int, struct proc *); 125 int tunclose(dev_t, int, int, struct proc *); 126 int tun_ioctl(struct ifnet *, u_long, caddr_t); 127 int tun_output(struct ifnet *, struct mbuf *, struct sockaddr *, 128 struct rtentry *rt); 129 int tunioctl(dev_t, u_long, caddr_t, int, struct proc *); 130 int tunread(dev_t, struct uio *, int); 131 int tunwrite(dev_t, struct uio *, int); 132 int tunpoll(dev_t, int, struct proc *); 133 int tunkqfilter(dev_t, struct knote *); 134 135 136 static int tuninit(struct tun_softc *); 137 #ifdef ALTQ 138 static void tunstart(struct ifnet *); 139 #endif 140 int filt_tunread(struct knote *, long); 141 int filt_tunwrite(struct knote *, long); 142 void filt_tunrdetach(struct knote *); 143 void filt_tunwdetach(struct knote *); 144 145 struct filterops tunread_filtops = 146 { 1, NULL, filt_tunrdetach, filt_tunread}; 147 148 struct filterops tunwrite_filtops = 149 { 1, NULL, filt_tunwdetach, filt_tunwrite}; 150 151 void 152 tunattach(n) 153 int n; 154 { 155 register int i; 156 struct ifnet *ifp; 157 158 ntun = n; 159 tunctl = malloc(ntun * sizeof(*tunctl), M_DEVBUF, M_WAITOK); 160 bzero(tunctl, ntun * sizeof(*tunctl)); 161 for (i = 0; i < ntun; i++) { 162 tunctl[i].tun_flags = TUN_INITED; 163 164 ifp = &tunctl[i].tun_if; 165 snprintf(ifp->if_xname, sizeof ifp->if_xname, "tun%d", i); 166 ifp->if_softc = &tunctl[i]; 167 ifp->if_mtu = TUNMTU; 168 ifp->if_ioctl = tun_ioctl; 169 ifp->if_output = tun_output; 170 #ifdef ALTQ 171 ifp->if_start = tunstart; 172 #endif 173 ifp->if_flags = IFF_POINTOPOINT; 174 ifp->if_type = IFT_PROPVIRTUAL; 175 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 176 IFQ_SET_READY(&ifp->if_snd); 177 ifp->if_hdrlen = sizeof(u_int32_t); 178 ifp->if_collisions = 0; 179 ifp->if_ierrors = 0; 180 ifp->if_oerrors = 0; 181 ifp->if_ipackets = 0; 182 ifp->if_opackets = 0; 183 ifp->if_ibytes = 0; 184 ifp->if_obytes = 0; 185 if_attach(ifp); 186 if_alloc_sadl(ifp); 187 #if NBPFILTER > 0 188 bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t)); 189 #endif 190 } 191 } 192 193 /* 194 * tunnel open - must be superuser & the device must be 195 * configured in 196 */ 197 int 198 tunopen(dev, flag, mode, p) 199 dev_t dev; 200 int flag, mode; 201 struct proc *p; 202 { 203 struct tun_softc *tp; 204 struct ifnet *ifp; 205 register int unit, error; 206 207 if ((error = suser(p, 0)) != 0) 208 return (error); 209 210 if ((unit = minor(dev)) >= ntun) 211 return (ENXIO); 212 213 tp = &tunctl[unit]; 214 if (tp->tun_flags & TUN_OPEN) 215 return EBUSY; 216 217 ifp = &tp->tun_if; 218 tp->tun_flags |= TUN_OPEN; 219 TUNDEBUG(("%s: open\n", ifp->if_xname)); 220 return (0); 221 } 222 223 /* 224 * tunclose - close the device; if closing the real device, flush pending 225 * output and (unless set STAYUP) bring down the interface. 226 */ 227 int 228 tunclose(dev, flag, mode, p) 229 dev_t dev; 230 int flag; 231 int mode; 232 struct proc *p; 233 { 234 register int unit, s; 235 struct tun_softc *tp; 236 struct ifnet *ifp; 237 238 if ((unit = minor(dev)) >= ntun) 239 return (ENXIO); 240 241 tp = &tunctl[unit]; 242 ifp = &tp->tun_if; 243 tp->tun_flags &= ~TUN_OPEN; 244 245 /* 246 * junk all pending output 247 */ 248 s = splimp(); 249 IFQ_PURGE(&ifp->if_snd); 250 splx(s); 251 252 if ((ifp->if_flags & IFF_UP) && !(tp->tun_flags & TUN_STAYUP)) { 253 s = splimp(); 254 if_down(ifp); 255 if (ifp->if_flags & IFF_RUNNING) { 256 /* find internet addresses and delete routes */ 257 register struct ifaddr *ifa; 258 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 259 #ifdef INET 260 if (ifa->ifa_addr->sa_family == AF_INET) { 261 rtinit(ifa, (int)RTM_DELETE, 262 (tp->tun_flags & TUN_DSTADDR)? 263 RTF_HOST : 0); 264 } 265 #endif 266 } 267 } 268 splx(s); 269 } 270 tp->tun_pgid = 0; 271 selwakeup(&tp->tun_rsel); 272 KNOTE(&tp->tun_rsel.si_note, 0); 273 274 TUNDEBUG(("%s: closed\n", ifp->if_xname)); 275 return (0); 276 } 277 278 static int 279 tuninit(tp) 280 struct tun_softc *tp; 281 { 282 struct ifnet *ifp = &tp->tun_if; 283 register struct ifaddr *ifa; 284 285 TUNDEBUG(("%s: tuninit\n", ifp->if_xname)); 286 287 ifp->if_flags |= IFF_UP | IFF_RUNNING; 288 289 tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR|TUN_BRDADDR); 290 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 291 #ifdef INET 292 if (ifa->ifa_addr->sa_family == AF_INET) { 293 struct sockaddr_in *sin; 294 295 sin = satosin(ifa->ifa_addr); 296 if (sin && sin->sin_addr.s_addr) 297 tp->tun_flags |= TUN_IASET; 298 299 if (ifp->if_flags & IFF_POINTOPOINT) { 300 sin = satosin(ifa->ifa_dstaddr); 301 if (sin && sin->sin_addr.s_addr) 302 tp->tun_flags |= TUN_DSTADDR; 303 } else 304 tp->tun_flags &= ~TUN_DSTADDR; 305 306 if (ifp->if_flags & IFF_BROADCAST) { 307 sin = satosin(ifa->ifa_broadaddr); 308 if (sin && sin->sin_addr.s_addr) 309 tp->tun_flags |= TUN_BRDADDR; 310 } else 311 tp->tun_flags &= ~TUN_BRDADDR; 312 } 313 #endif 314 #ifdef INET6 315 if (ifa->ifa_addr->sa_family == AF_INET6) { 316 struct sockaddr_in6 *sin; 317 318 sin = (struct sockaddr_in6 *)ifa->ifa_addr; 319 if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)) 320 tp->tun_flags |= TUN_IASET; 321 322 if (ifp->if_flags & IFF_POINTOPOINT) { 323 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr; 324 if (sin && 325 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)) 326 tp->tun_flags |= TUN_DSTADDR; 327 } else 328 tp->tun_flags &= ~TUN_DSTADDR; 329 } 330 #endif /* INET6 */ 331 } 332 333 return 0; 334 } 335 336 /* 337 * Process an ioctl request. 338 */ 339 int 340 tun_ioctl(ifp, cmd, data) 341 struct ifnet *ifp; 342 u_long cmd; 343 caddr_t data; 344 { 345 int error = 0, s; 346 347 s = splimp(); 348 switch(cmd) { 349 case SIOCSIFADDR: 350 tuninit((struct tun_softc *)(ifp->if_softc)); 351 TUNDEBUG(("%s: address set\n", ifp->if_xname)); 352 break; 353 case SIOCSIFDSTADDR: 354 tuninit((struct tun_softc *)(ifp->if_softc)); 355 TUNDEBUG(("%s: destination address set\n", ifp->if_xname)); 356 break; 357 case SIOCSIFBRDADDR: 358 tuninit((struct tun_softc *)(ifp->if_softc)); 359 TUNDEBUG(("%s: broadcast address set\n", ifp->if_xname)); 360 break; 361 case SIOCSIFMTU: 362 ifp->if_mtu = ((struct ifreq *)data)->ifr_mtu; 363 break; 364 case SIOCADDMULTI: 365 case SIOCDELMULTI: { 366 struct ifreq *ifr = (struct ifreq *)data; 367 if (ifr == 0) { 368 error = EAFNOSUPPORT; /* XXX */ 369 break; 370 } 371 switch (ifr->ifr_addr.sa_family) { 372 #ifdef INET 373 case AF_INET: 374 break; 375 #endif 376 #ifdef INET6 377 case AF_INET6: 378 break; 379 #endif 380 default: 381 error = EAFNOSUPPORT; 382 break; 383 } 384 break; 385 } 386 387 case SIOCSIFFLAGS: 388 break; 389 default: 390 error = EINVAL; 391 } 392 splx(s); 393 return (error); 394 } 395 396 /* 397 * tun_output - queue packets from higher level ready to put out. 398 */ 399 int 400 tun_output(ifp, m0, dst, rt) 401 struct ifnet *ifp; 402 struct mbuf *m0; 403 struct sockaddr *dst; 404 struct rtentry *rt; 405 { 406 struct tun_softc *tp = ifp->if_softc; 407 int s, len, error; 408 u_int32_t *af; 409 410 TUNDEBUG(("%s: tun_output\n", ifp->if_xname)); 411 412 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 413 TUNDEBUG(("%s: not ready 0%o\n", ifp->if_xname, 414 tp->tun_flags)); 415 m_freem (m0); 416 return EHOSTDOWN; 417 } 418 419 M_PREPEND(m0, sizeof(*af), M_DONTWAIT); 420 af = mtod(m0, u_int32_t *); 421 *af = htonl(dst->sa_family); 422 423 #if NBPFILTER > 0 424 if (ifp->if_bpf) 425 bpf_mtap(ifp->if_bpf, m0); 426 #endif 427 428 len = m0->m_pkthdr.len + sizeof(*af); 429 s = splimp(); 430 IFQ_ENQUEUE(&ifp->if_snd, m0, NULL, error); 431 if (error) { 432 splx(s); 433 ifp->if_collisions++; 434 return (error); 435 } 436 splx(s); 437 ifp->if_opackets++; 438 ifp->if_obytes += len; 439 440 if (tp->tun_flags & TUN_RWAIT) { 441 tp->tun_flags &= ~TUN_RWAIT; 442 wakeup((caddr_t)tp); 443 } 444 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) 445 csignal(tp->tun_pgid, SIGIO, 446 tp->tun_siguid, tp->tun_sigeuid); 447 selwakeup(&tp->tun_rsel); 448 KNOTE(&tp->tun_rsel.si_note, 0); 449 return 0; 450 } 451 452 /* 453 * the cdevsw interface is now pretty minimal. 454 */ 455 int 456 tunioctl(dev, cmd, data, flag, p) 457 dev_t dev; 458 u_long cmd; 459 caddr_t data; 460 int flag; 461 struct proc *p; 462 { 463 int unit, s; 464 struct tun_softc *tp; 465 struct tuninfo *tunp; 466 struct mbuf *m; 467 468 if ((unit = minor(dev)) >= ntun) 469 return (ENXIO); 470 471 tp = &tunctl[unit]; 472 473 s = splimp(); 474 switch (cmd) { 475 case TUNSIFINFO: 476 tunp = (struct tuninfo *)data; 477 tp->tun_if.if_mtu = tunp->mtu; 478 tp->tun_if.if_type = tunp->type; 479 tp->tun_if.if_flags = tunp->flags; 480 tp->tun_if.if_baudrate = tunp->baudrate; 481 break; 482 case TUNGIFINFO: 483 tunp = (struct tuninfo *)data; 484 tunp->mtu = tp->tun_if.if_mtu; 485 tunp->type = tp->tun_if.if_type; 486 tunp->flags = tp->tun_if.if_flags; 487 tunp->baudrate = tp->tun_if.if_baudrate; 488 break; 489 #ifdef TUN_DEBUG 490 case TUNSDEBUG: 491 tundebug = *(int *)data; 492 break; 493 case TUNGDEBUG: 494 *(int *)data = tundebug; 495 break; 496 #endif 497 case TUNSIFMODE: 498 switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) { 499 case IFF_POINTOPOINT: 500 case IFF_BROADCAST: 501 if (tp->tun_if.if_flags & IFF_UP) { 502 splx(s); 503 return (EBUSY); 504 } 505 tp->tun_if.if_flags &= 506 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); 507 tp->tun_if.if_flags |= *(int *)data; 508 break; 509 default: 510 splx(s); 511 return (EINVAL); 512 } 513 break; 514 515 case FIONBIO: 516 if (*(int *)data) 517 tp->tun_flags |= TUN_NBIO; 518 else 519 tp->tun_flags &= ~TUN_NBIO; 520 break; 521 case FIOASYNC: 522 if (*(int *)data) 523 tp->tun_flags |= TUN_ASYNC; 524 else 525 tp->tun_flags &= ~TUN_ASYNC; 526 break; 527 case FIONREAD: 528 IFQ_POLL(&tp->tun_if.if_snd, m); 529 if (m != NULL) 530 *(int *)data = m->m_pkthdr.len; 531 else 532 *(int *)data = 0; 533 break; 534 case TIOCSPGRP: 535 tp->tun_pgid = *(int *)data; 536 tp->tun_siguid = p->p_cred->p_ruid; 537 tp->tun_sigeuid = p->p_ucred->cr_uid; 538 break; 539 case TIOCGPGRP: 540 *(int *)data = tp->tun_pgid; 541 break; 542 default: 543 splx(s); 544 return (ENOTTY); 545 } 546 splx(s); 547 return (0); 548 } 549 550 /* 551 * The cdevsw read interface - reads a packet at a time, or at 552 * least as much of a packet as can be read. 553 */ 554 int 555 tunread(dev, uio, ioflag) 556 dev_t dev; 557 struct uio *uio; 558 int ioflag; 559 { 560 int unit; 561 struct tun_softc *tp; 562 struct ifnet *ifp; 563 struct mbuf *m, *m0; 564 int error = 0, len, s; 565 566 if ((unit = minor(dev)) >= ntun) 567 return (ENXIO); 568 569 tp = &tunctl[unit]; 570 ifp = &tp->tun_if; 571 TUNDEBUG(("%s: read\n", ifp->if_xname)); 572 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 573 TUNDEBUG(("%s: not ready 0%o\n", ifp->if_xname, 574 tp->tun_flags)); 575 return EHOSTDOWN; 576 } 577 578 tp->tun_flags &= ~TUN_RWAIT; 579 580 s = splimp(); 581 do { 582 while ((tp->tun_flags & TUN_READY) != TUN_READY) 583 if ((error = tsleep((caddr_t)tp, 584 (PZERO+1)|PCATCH, "tunread", 0)) != 0) { 585 splx(s); 586 return (error); 587 } 588 IFQ_DEQUEUE(&ifp->if_snd, m0); 589 if (m0 == 0) { 590 if (tp->tun_flags & TUN_NBIO && ioflag & IO_NDELAY) { 591 splx(s); 592 return EWOULDBLOCK; 593 } 594 tp->tun_flags |= TUN_RWAIT; 595 if ((error = tsleep((caddr_t)tp, 596 (PZERO + 1)|PCATCH, "tunread", 0)) != 0) { 597 splx(s); 598 return (error); 599 } 600 } 601 } while (m0 == 0); 602 splx(s); 603 604 while (m0 && uio->uio_resid > 0 && error == 0) { 605 len = min(uio->uio_resid, m0->m_len); 606 if (len != 0) 607 error = uiomove(mtod(m0, caddr_t), len, uio); 608 MFREE(m0, m); 609 m0 = m; 610 } 611 612 if (m0) { 613 TUNDEBUG(("Dropping mbuf\n")); 614 m_freem(m0); 615 } 616 if (error) 617 ifp->if_ierrors++; 618 619 return error; 620 } 621 622 /* 623 * the cdevsw write interface - an atomic write is a packet - or else! 624 */ 625 int 626 tunwrite(dev, uio, ioflag) 627 dev_t dev; 628 struct uio *uio; 629 int ioflag; 630 { 631 int unit; 632 struct ifnet *ifp; 633 struct ifqueue *ifq; 634 u_int32_t *th; 635 struct mbuf *top, **mp, *m; 636 int isr; 637 int error=0, s, tlen, mlen; 638 639 if ((unit = minor(dev)) >= ntun) 640 return (ENXIO); 641 642 ifp = &tunctl[unit].tun_if; 643 TUNDEBUG(("%s: tunwrite\n", ifp->if_xname)); 644 645 if (uio->uio_resid == 0 || uio->uio_resid > TUNMRU) { 646 TUNDEBUG(("%s: len=%d!\n", ifp->if_xname, uio->uio_resid)); 647 return EMSGSIZE; 648 } 649 tlen = uio->uio_resid; 650 651 /* get a header mbuf */ 652 MGETHDR(m, M_DONTWAIT, MT_DATA); 653 if (m == NULL) 654 return ENOBUFS; 655 mlen = MHLEN; 656 657 top = 0; 658 mp = ⊤ 659 while (error == 0 && uio->uio_resid > 0) { 660 m->m_len = min(mlen, uio->uio_resid); 661 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 662 *mp = m; 663 mp = &m->m_next; 664 if (uio->uio_resid > 0) { 665 MGET (m, M_DONTWAIT, MT_DATA); 666 if (m == 0) { 667 error = ENOBUFS; 668 break; 669 } 670 mlen = MLEN; 671 } 672 } 673 if (error) { 674 if (top) 675 m_freem (top); 676 ifp->if_ierrors++; 677 return error; 678 } 679 680 top->m_pkthdr.len = tlen; 681 top->m_pkthdr.rcvif = ifp; 682 683 #if NBPFILTER > 0 684 if (ifp->if_bpf) 685 bpf_mtap(ifp->if_bpf, top); 686 #endif 687 688 th = mtod(top, u_int32_t *); 689 /* strip the tunnel header */ 690 top->m_data += sizeof(*th); 691 top->m_len -= sizeof(*th); 692 top->m_pkthdr.len -= sizeof(*th); 693 694 switch (ntohl(*th)) { 695 #ifdef INET 696 case AF_INET: 697 ifq = &ipintrq; 698 isr = NETISR_IP; 699 break; 700 #endif 701 #ifdef INET6 702 case AF_INET6: 703 ifq = &ip6intrq; 704 isr = NETISR_IPV6; 705 break; 706 #endif 707 #ifdef NS 708 case AF_NS: 709 ifq = &nsintrq; 710 isr = NETISR_NS; 711 break; 712 #endif 713 #ifdef IPX 714 case AF_IPX: 715 ifq = &ipxintrq; 716 isr = NETISR_IPX; 717 break; 718 #endif 719 #ifdef NETATALK 720 case AF_APPLETALK: 721 ifq = &atintrq2; 722 isr = NETISR_ATALK; 723 break; 724 #endif 725 #ifdef ISO 726 case AF_ISO: 727 ifq = &clnlintrq; 728 isr = NETISR_ISO; 729 break; 730 #endif 731 default: 732 m_freem(top); 733 return EAFNOSUPPORT; 734 } 735 736 s = splimp(); 737 if (IF_QFULL(ifq)) { 738 IF_DROP(ifq); 739 splx(s); 740 ifp->if_collisions++; 741 m_freem(top); 742 return ENOBUFS; 743 } 744 IF_ENQUEUE(ifq, top); 745 schednetisr(isr); 746 ifp->if_ipackets++; 747 ifp->if_ibytes += top->m_pkthdr.len; 748 splx(s); 749 return error; 750 } 751 752 /* 753 * tunselect - the select interface, this is only useful on reads 754 * really. The write detect always returns true, write never blocks 755 * anyway, it either accepts the packet or drops it. 756 */ 757 int 758 tunpoll(dev, events, p) 759 dev_t dev; 760 int events; 761 struct proc *p; 762 { 763 int unit, revents, s; 764 struct tun_softc *tp; 765 struct ifnet *ifp; 766 struct mbuf *m; 767 768 if ((unit = minor(dev)) >= ntun) 769 return (ENXIO); 770 771 tp = &tunctl[unit]; 772 ifp = &tp->tun_if; 773 revents = 0; 774 s = splimp(); 775 TUNDEBUG(("%s: tunpoll\n", ifp->if_xname)); 776 777 if (events & (POLLIN | POLLRDNORM)) { 778 IFQ_POLL(&ifp->if_snd, m); 779 if (m != NULL) { 780 TUNDEBUG(("%s: tunselect q=%d\n", ifp->if_xname, 781 ifp->if_snd.ifq_len)); 782 revents |= events & (POLLIN | POLLRDNORM); 783 } else { 784 TUNDEBUG(("%s: tunpoll waiting\n", ifp->if_xname)); 785 selrecord(p, &tp->tun_rsel); 786 } 787 } 788 if (events & (POLLOUT | POLLWRNORM)) 789 revents |= events & (POLLOUT | POLLWRNORM); 790 splx(s); 791 return (revents); 792 } 793 794 /* 795 * kqueue(2) support. 796 * 797 * The tun driver uses an array of tun_softc's based on the minor number 798 * of the device. kn->kn_hook gets set to the specific tun_softc. 799 * 800 * filt_tunread() sets kn->kn_data to the iface qsize 801 * filt_tunwrite() sets kn->kn_data to the MTU size 802 */ 803 int 804 tunkqfilter(dev_t dev,struct knote *kn) 805 { 806 int unit, s; 807 struct klist *klist; 808 struct tun_softc *tp; 809 struct ifnet *ifp; 810 811 if ((unit = minor(dev)) >= ntun) 812 return ENXIO; 813 814 tp = &tunctl[unit]; 815 ifp = &tp->tun_if; 816 817 s = splimp(); 818 TUNDEBUG(("%s: tunselect\n", ifp->if_xname)); 819 splx(s); 820 821 switch (kn->kn_filter) { 822 case EVFILT_READ: 823 klist = &tp->tun_rsel.si_note; 824 kn->kn_fop = &tunread_filtops; 825 break; 826 case EVFILT_WRITE: 827 klist = &tp->tun_wsel.si_note; 828 kn->kn_fop = &tunwrite_filtops; 829 break; 830 default: 831 return EPERM; /* 1 */ 832 } 833 834 kn->kn_hook = (caddr_t)tp; 835 836 s = splhigh(); 837 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 838 splx(s); 839 840 return 0; 841 } 842 843 void 844 filt_tunrdetach(struct knote *kn) 845 { 846 int s; 847 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook; 848 849 s = splhigh(); 850 SLIST_REMOVE(&tp->tun_rsel.si_note, kn, knote, kn_selnext); 851 splx(s); 852 } 853 854 int 855 filt_tunread(struct knote *kn, long hint) 856 { 857 int s; 858 struct tun_softc *tp; 859 struct ifnet *ifp; 860 struct mbuf *m; 861 862 tp = (struct tun_softc *)kn->kn_hook; 863 ifp = &tp->tun_if; 864 865 s = splnet(); 866 IFQ_POLL(&ifp->if_snd, m); 867 if (m != NULL) { 868 splx(s); 869 kn->kn_data = ifp->if_snd.ifq_len; 870 871 TUNDEBUG(("%s: tunkqread q=%d\n", ifp->if_xname, 872 ifp->if_snd.ifq_len)); 873 return 1; 874 } 875 splx(s); 876 TUNDEBUG(("%s: tunkqread waiting\n", ifp->if_xname)); 877 return 0; 878 } 879 880 void 881 filt_tunwdetach(struct knote *kn) 882 { 883 int s; 884 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook; 885 886 s = splhigh(); 887 SLIST_REMOVE(&tp->tun_wsel.si_note, kn, knote, kn_selnext); 888 splx(s); 889 } 890 891 int 892 filt_tunwrite(struct knote *kn, long hint) 893 { 894 struct tun_softc *tp; 895 struct ifnet *ifp; 896 897 tp = (struct tun_softc *)kn->kn_hook; 898 ifp = &tp->tun_if; 899 900 kn->kn_data = ifp->if_mtu; 901 902 return 1; 903 } 904 905 #ifdef ALTQ 906 /* 907 * Start packet transmission on the interface. 908 * when the interface queue is rate-limited by ALTQ or TBR, 909 * if_start is needed to drain packets from the queue in order 910 * to notify readers when outgoing packets become ready. 911 */ 912 static void 913 tunstart(ifp) 914 struct ifnet *ifp; 915 { 916 struct tun_softc *tp = ifp->if_softc; 917 struct mbuf *m; 918 919 if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd)) 920 return; 921 922 IFQ_POLL(&ifp->if_snd, m); 923 if (m != NULL) { 924 if (tp->tun_flags & TUN_RWAIT) { 925 tp->tun_flags &= ~TUN_RWAIT; 926 wakeup((caddr_t)tp); 927 } 928 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) 929 csignal(tp->tun_pgid, SIGIO, 930 tp->tun_siguid, tp->tun_sigeuid); 931 selwakeup(&tp->tun_rsel); 932 KNOTE(&tp->tun_rsel.si_note, 0); 933 } 934 } 935 #endif 936