1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd 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 it's 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 * $FreeBSD: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $ 17 * $DragonFly: src/sys/net/tun/if_tun.c,v 1.11 2004/01/06 01:40:51 dillon Exp $ 18 */ 19 20 #include "opt_atalk.h" 21 #include "opt_inet.h" 22 #include "opt_inet6.h" 23 #include "opt_ipx.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/socket.h> 30 #include <sys/filio.h> 31 #include <sys/sockio.h> 32 #include <sys/ttycom.h> 33 #include <sys/poll.h> 34 #include <sys/signalvar.h> 35 #include <sys/filedesc.h> 36 #include <sys/kernel.h> 37 #include <sys/sysctl.h> 38 #include <sys/conf.h> 39 #include <sys/uio.h> 40 #include <sys/vnode.h> 41 #include <sys/malloc.h> 42 43 #include <net/if.h> 44 #include <net/if_types.h> 45 #include <net/netisr.h> 46 #include <net/route.h> 47 48 #ifdef INET 49 #include <netinet/in.h> 50 #endif 51 52 #include <net/bpf.h> 53 54 #include "if_tunvar.h" 55 #include "if_tun.h" 56 57 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface"); 58 59 static void tunattach (void *); 60 PSEUDO_SET(tunattach, if_tun); 61 62 static void tuncreate (dev_t dev); 63 64 #define TUNDEBUG if (tundebug) printf 65 static int tundebug = 0; 66 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); 67 68 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *, 69 struct rtentry *rt); 70 static int tunifioctl (struct ifnet *, u_long, caddr_t); 71 static int tuninit (struct ifnet *); 72 73 static d_open_t tunopen; 74 static d_close_t tunclose; 75 static d_read_t tunread; 76 static d_write_t tunwrite; 77 static d_ioctl_t tunioctl; 78 static d_poll_t tunpoll; 79 80 #define CDEV_MAJOR 52 81 static struct cdevsw tun_cdevsw = { 82 /* name */ "tun", 83 /* maj */ CDEV_MAJOR, 84 /* flags */ 0, 85 /* port */ NULL, 86 /* autoq */ 0, 87 88 /* open */ tunopen, 89 /* close */ tunclose, 90 /* read */ tunread, 91 /* write */ tunwrite, 92 /* ioctl */ tunioctl, 93 /* poll */ tunpoll, 94 /* mmap */ nommap, 95 /* strategy */ nostrategy, 96 /* dump */ nodump, 97 /* psize */ nopsize 98 }; 99 100 static void 101 tunattach(dummy) 102 void *dummy; 103 { 104 105 cdevsw_add(&tun_cdevsw); 106 } 107 108 static void 109 tuncreate(dev) 110 dev_t dev; 111 { 112 struct tun_softc *sc; 113 struct ifnet *ifp; 114 115 dev = make_dev(&tun_cdevsw, minor(dev), 116 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev)); 117 118 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK); 119 bzero(sc, sizeof *sc); 120 sc->tun_flags = TUN_INITED; 121 122 ifp = &sc->tun_if; 123 if_initname(ifp, "tun", lminor(dev)); 124 ifp->if_mtu = TUNMTU; 125 ifp->if_ioctl = tunifioctl; 126 ifp->if_output = tunoutput; 127 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 128 ifp->if_type = IFT_PPP; 129 ifp->if_snd.ifq_maxlen = ifqmaxlen; 130 ifp->if_softc = sc; 131 if_attach(ifp); 132 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 133 dev->si_drv1 = sc; 134 } 135 136 /* 137 * tunnel open - must be superuser & the device must be 138 * configured in 139 */ 140 static int 141 tunopen(dev_t dev, int flag, int mode, struct thread *td) 142 { 143 struct ifnet *ifp; 144 struct tun_softc *tp; 145 int error; 146 147 KKASSERT(td->td_proc); 148 if ((error = suser(td)) != NULL) 149 return (error); 150 151 tp = dev->si_drv1; 152 if (!tp) { 153 tuncreate(dev); 154 tp = dev->si_drv1; 155 } 156 if (tp->tun_flags & TUN_OPEN) 157 return EBUSY; 158 tp->tun_pid = td->td_proc->p_pid; 159 ifp = &tp->tun_if; 160 tp->tun_flags |= TUN_OPEN; 161 TUNDEBUG("%s: open\n", ifp->if_xname); 162 return (0); 163 } 164 165 /* 166 * tunclose - close the device - mark i/f down & delete 167 * routing info 168 */ 169 static int 170 tunclose(dev_t dev, int foo, int bar, struct thread *td) 171 { 172 int s; 173 struct tun_softc *tp; 174 struct ifnet *ifp; 175 struct mbuf *m; 176 177 tp = dev->si_drv1; 178 ifp = &tp->tun_if; 179 180 tp->tun_flags &= ~TUN_OPEN; 181 tp->tun_pid = 0; 182 183 /* 184 * junk all pending output 185 */ 186 do { 187 s = splimp(); 188 IF_DEQUEUE(&ifp->if_snd, m); 189 splx(s); 190 if (m) 191 m_freem(m); 192 } while (m); 193 194 if (ifp->if_flags & IFF_UP) { 195 s = splimp(); 196 if_down(ifp); 197 splx(s); 198 } 199 200 if (ifp->if_flags & IFF_RUNNING) { 201 struct ifaddr *ifa; 202 203 s = splimp(); 204 /* find internet addresses and delete routes */ 205 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 206 if (ifa->ifa_addr->sa_family == AF_INET) 207 rtinit(ifa, (int)RTM_DELETE, 208 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 209 ifp->if_flags &= ~IFF_RUNNING; 210 splx(s); 211 } 212 213 funsetown(tp->tun_sigio); 214 selwakeup(&tp->tun_rsel); 215 216 TUNDEBUG ("%s: closed\n", ifp->if_xname); 217 return (0); 218 } 219 220 static int 221 tuninit(ifp) 222 struct ifnet *ifp; 223 { 224 struct tun_softc *tp = ifp->if_softc; 225 struct ifaddr *ifa; 226 int error = 0; 227 228 TUNDEBUG("%s: tuninit\n", ifp->if_xname); 229 230 ifp->if_flags |= IFF_UP | IFF_RUNNING; 231 getmicrotime(&ifp->if_lastchange); 232 233 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 234 ifa = TAILQ_NEXT(ifa, ifa_link)) { 235 if (ifa->ifa_addr == NULL) 236 error = EFAULT; 237 /* XXX: Should maybe return straight off? */ 238 else { 239 #ifdef INET 240 if (ifa->ifa_addr->sa_family == AF_INET) { 241 struct sockaddr_in *si; 242 243 si = (struct sockaddr_in *)ifa->ifa_addr; 244 if (si->sin_addr.s_addr) 245 tp->tun_flags |= TUN_IASET; 246 247 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 248 if (si && si->sin_addr.s_addr) 249 tp->tun_flags |= TUN_DSTADDR; 250 } 251 #endif 252 } 253 } 254 return (error); 255 } 256 257 /* 258 * Process an ioctl request. 259 */ 260 int 261 tunifioctl(ifp, cmd, data) 262 struct ifnet *ifp; 263 u_long cmd; 264 caddr_t data; 265 { 266 struct ifreq *ifr = (struct ifreq *)data; 267 struct tun_softc *tp = ifp->if_softc; 268 struct ifstat *ifs; 269 int error = 0, s; 270 271 s = splimp(); 272 switch(cmd) { 273 case SIOCGIFSTATUS: 274 ifs = (struct ifstat *)data; 275 if (tp->tun_pid) 276 sprintf(ifs->ascii + strlen(ifs->ascii), 277 "\tOpened by PID %d\n", tp->tun_pid); 278 break; 279 case SIOCSIFADDR: 280 error = tuninit(ifp); 281 TUNDEBUG("%s: address set, error=%d\n", 282 ifp->if_xname, error); 283 break; 284 case SIOCSIFDSTADDR: 285 error = tuninit(ifp); 286 TUNDEBUG("%s destination address set, error=%d\n", 287 ifp->if_xname, error); 288 break; 289 case SIOCSIFMTU: 290 ifp->if_mtu = ifr->ifr_mtu; 291 TUNDEBUG("%s: mtu set\n", 292 ifp->if_xname); 293 break; 294 case SIOCSIFFLAGS: 295 case SIOCADDMULTI: 296 case SIOCDELMULTI: 297 break; 298 default: 299 error = EINVAL; 300 } 301 splx(s); 302 return (error); 303 } 304 305 /* 306 * tunoutput - queue packets from higher level ready to put out. 307 */ 308 int 309 tunoutput(ifp, m0, dst, rt) 310 struct ifnet *ifp; 311 struct mbuf *m0; 312 struct sockaddr *dst; 313 struct rtentry *rt; 314 { 315 struct tun_softc *tp = ifp->if_softc; 316 int s; 317 318 TUNDEBUG ("%s: tunoutput\n", ifp->if_xname); 319 320 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 321 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, 322 tp->tun_flags); 323 m_freem (m0); 324 return EHOSTDOWN; 325 } 326 327 /* BPF write needs to be handled specially */ 328 if (dst->sa_family == AF_UNSPEC) { 329 dst->sa_family = *(mtod(m0, int *)); 330 m0->m_len -= sizeof(int); 331 m0->m_pkthdr.len -= sizeof(int); 332 m0->m_data += sizeof(int); 333 } 334 335 if (ifp->if_bpf) { 336 /* 337 * We need to prepend the address family as 338 * a four byte field. Cons up a dummy header 339 * to pacify bpf. This is safe because bpf 340 * will only read from the mbuf (i.e., it won't 341 * try to free it or keep a pointer to it). 342 */ 343 struct mbuf m; 344 uint32_t af = dst->sa_family; 345 346 m.m_next = m0; 347 m.m_len = 4; 348 m.m_data = (char *)⁡ 349 350 bpf_mtap(ifp, &m); 351 } 352 353 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 354 if (tp->tun_flags & TUN_LMODE) { 355 /* allocate space for sockaddr */ 356 M_PREPEND(m0, dst->sa_len, M_DONTWAIT); 357 358 /* if allocation failed drop packet */ 359 if (m0 == NULL){ 360 s = splimp(); /* spl on queue manipulation */ 361 IF_DROP(&ifp->if_snd); 362 splx(s); 363 ifp->if_oerrors++; 364 return (ENOBUFS); 365 } else { 366 bcopy(dst, m0->m_data, dst->sa_len); 367 } 368 } 369 370 if (tp->tun_flags & TUN_IFHEAD) { 371 /* Prepend the address family */ 372 M_PREPEND(m0, 4, M_DONTWAIT); 373 374 /* if allocation failed drop packet */ 375 if (m0 == NULL){ 376 s = splimp(); /* spl on queue manipulation */ 377 IF_DROP(&ifp->if_snd); 378 splx(s); 379 ifp->if_oerrors++; 380 return ENOBUFS; 381 } else 382 *(u_int32_t *)m0->m_data = htonl(dst->sa_family); 383 } else { 384 #ifdef INET 385 if (dst->sa_family != AF_INET) 386 #endif 387 { 388 m_freem(m0); 389 return EAFNOSUPPORT; 390 } 391 } 392 393 s = splimp(); 394 if (IF_QFULL(&ifp->if_snd)) { 395 IF_DROP(&ifp->if_snd); 396 m_freem(m0); 397 splx(s); 398 ifp->if_collisions++; 399 return ENOBUFS; 400 } 401 ifp->if_obytes += m0->m_pkthdr.len; 402 IF_ENQUEUE(&ifp->if_snd, m0); 403 splx(s); 404 ifp->if_opackets++; 405 406 if (tp->tun_flags & TUN_RWAIT) { 407 tp->tun_flags &= ~TUN_RWAIT; 408 wakeup((caddr_t)tp); 409 } 410 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) 411 pgsigio(tp->tun_sigio, SIGIO, 0); 412 selwakeup(&tp->tun_rsel); 413 return 0; 414 } 415 416 /* 417 * the cdevsw interface is now pretty minimal. 418 */ 419 static int 420 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 421 { 422 int s; 423 struct tun_softc *tp = dev->si_drv1; 424 struct tuninfo *tunp; 425 426 switch (cmd) { 427 case TUNSIFINFO: 428 tunp = (struct tuninfo *)data; 429 if (tunp->mtu < IF_MINMTU) 430 return (EINVAL); 431 tp->tun_if.if_mtu = tunp->mtu; 432 tp->tun_if.if_type = tunp->type; 433 tp->tun_if.if_baudrate = tunp->baudrate; 434 break; 435 case TUNGIFINFO: 436 tunp = (struct tuninfo *)data; 437 tunp->mtu = tp->tun_if.if_mtu; 438 tunp->type = tp->tun_if.if_type; 439 tunp->baudrate = tp->tun_if.if_baudrate; 440 break; 441 case TUNSDEBUG: 442 tundebug = *(int *)data; 443 break; 444 case TUNGDEBUG: 445 *(int *)data = tundebug; 446 break; 447 case TUNSLMODE: 448 if (*(int *)data) { 449 tp->tun_flags |= TUN_LMODE; 450 tp->tun_flags &= ~TUN_IFHEAD; 451 } else 452 tp->tun_flags &= ~TUN_LMODE; 453 break; 454 case TUNSIFHEAD: 455 if (*(int *)data) { 456 tp->tun_flags |= TUN_IFHEAD; 457 tp->tun_flags &= ~TUN_LMODE; 458 } else 459 tp->tun_flags &= ~TUN_IFHEAD; 460 break; 461 case TUNGIFHEAD: 462 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0; 463 break; 464 case TUNSIFMODE: 465 /* deny this if UP */ 466 if (tp->tun_if.if_flags & IFF_UP) 467 return(EBUSY); 468 469 switch (*(int *)data & ~IFF_MULTICAST) { 470 case IFF_POINTOPOINT: 471 case IFF_BROADCAST: 472 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT); 473 tp->tun_if.if_flags |= *(int *)data; 474 break; 475 default: 476 return(EINVAL); 477 } 478 break; 479 case TUNSIFPID: 480 tp->tun_pid = curproc->p_pid; 481 break; 482 case FIONBIO: 483 break; 484 case FIOASYNC: 485 if (*(int *)data) 486 tp->tun_flags |= TUN_ASYNC; 487 else 488 tp->tun_flags &= ~TUN_ASYNC; 489 break; 490 case FIONREAD: 491 s = splimp(); 492 if (tp->tun_if.if_snd.ifq_head) { 493 struct mbuf *mb = tp->tun_if.if_snd.ifq_head; 494 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 495 *(int *)data += mb->m_len; 496 } else 497 *(int *)data = 0; 498 splx(s); 499 break; 500 case FIOSETOWN: 501 return (fsetown(*(int *)data, &tp->tun_sigio)); 502 503 case FIOGETOWN: 504 *(int *)data = fgetown(tp->tun_sigio); 505 return (0); 506 507 /* This is deprecated, FIOSETOWN should be used instead. */ 508 case TIOCSPGRP: 509 return (fsetown(-(*(int *)data), &tp->tun_sigio)); 510 511 /* This is deprecated, FIOGETOWN should be used instead. */ 512 case TIOCGPGRP: 513 *(int *)data = -fgetown(tp->tun_sigio); 514 return (0); 515 516 default: 517 return (ENOTTY); 518 } 519 return (0); 520 } 521 522 /* 523 * The cdevsw read interface - reads a packet at a time, or at 524 * least as much of a packet as can be read. 525 */ 526 static int 527 tunread(dev, uio, flag) 528 dev_t dev; 529 struct uio *uio; 530 int flag; 531 { 532 struct tun_softc *tp = dev->si_drv1; 533 struct ifnet *ifp = &tp->tun_if; 534 struct mbuf *m0; 535 int error=0, len, s; 536 537 TUNDEBUG ("%s: read\n", ifp->if_xname); 538 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 539 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, 540 tp->tun_flags); 541 return EHOSTDOWN; 542 } 543 544 tp->tun_flags &= ~TUN_RWAIT; 545 546 s = splimp(); 547 do { 548 IF_DEQUEUE(&ifp->if_snd, m0); 549 if (m0 == 0) { 550 if (flag & IO_NDELAY) { 551 splx(s); 552 return EWOULDBLOCK; 553 } 554 tp->tun_flags |= TUN_RWAIT; 555 if((error = tsleep((caddr_t)tp, PCATCH, 556 "tunread", 0)) != 0) { 557 splx(s); 558 return error; 559 } 560 } 561 } while (m0 == 0); 562 splx(s); 563 564 while (m0 && uio->uio_resid > 0 && error == 0) { 565 len = min(uio->uio_resid, m0->m_len); 566 if (len != 0) 567 error = uiomove(mtod(m0, caddr_t), len, uio); 568 m0 = m_free(m0); 569 } 570 571 if (m0) { 572 TUNDEBUG("%s: Dropping mbuf\n", ifp->if_xname); 573 m_freem(m0); 574 } 575 return error; 576 } 577 578 /* 579 * the cdevsw write interface - an atomic write is a packet - or else! 580 */ 581 static int 582 tunwrite(dev, uio, flag) 583 dev_t dev; 584 struct uio *uio; 585 int flag; 586 { 587 struct tun_softc *tp = dev->si_drv1; 588 struct ifnet *ifp = &tp->tun_if; 589 struct mbuf *top, **mp, *m; 590 int error=0, tlen, mlen; 591 uint32_t family; 592 int isr; 593 594 TUNDEBUG("%s: tunwrite\n", ifp->if_xname); 595 596 if (uio->uio_resid == 0) 597 return 0; 598 599 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { 600 TUNDEBUG("%s: len=%d!\n", ifp->if_xname, 601 uio->uio_resid); 602 return EIO; 603 } 604 tlen = uio->uio_resid; 605 606 /* get a header mbuf */ 607 MGETHDR(m, M_DONTWAIT, MT_DATA); 608 if (m == NULL) 609 return ENOBUFS; 610 mlen = MHLEN; 611 612 top = 0; 613 mp = ⊤ 614 while (error == 0 && uio->uio_resid > 0) { 615 m->m_len = min(mlen, uio->uio_resid); 616 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 617 *mp = m; 618 mp = &m->m_next; 619 if (uio->uio_resid > 0) { 620 MGET (m, M_DONTWAIT, MT_DATA); 621 if (m == 0) { 622 error = ENOBUFS; 623 break; 624 } 625 mlen = MLEN; 626 } 627 } 628 if (error) { 629 if (top) 630 m_freem (top); 631 ifp->if_ierrors++; 632 return error; 633 } 634 635 top->m_pkthdr.len = tlen; 636 top->m_pkthdr.rcvif = ifp; 637 638 if (ifp->if_bpf) { 639 if (tp->tun_flags & TUN_IFHEAD) { 640 /* 641 * Conveniently, we already have a 4-byte address 642 * family prepended to our packet ! 643 * Inconveniently, it's in the wrong byte order ! 644 */ 645 if ((top = m_pullup(top, sizeof(family))) == NULL) 646 return ENOBUFS; 647 *mtod(top, u_int32_t *) = 648 ntohl(*mtod(top, u_int32_t *)); 649 bpf_mtap(ifp, top); 650 *mtod(top, u_int32_t *) = 651 htonl(*mtod(top, u_int32_t *)); 652 } else { 653 /* 654 * We need to prepend the address family as 655 * a four byte field. Cons up a dummy header 656 * to pacify bpf. This is safe because bpf 657 * will only read from the mbuf (i.e., it won't 658 * try to free it or keep a pointer to it). 659 */ 660 struct mbuf m; 661 uint32_t af = AF_INET; 662 663 m.m_next = top; 664 m.m_len = 4; 665 m.m_data = (char *)⁡ 666 667 bpf_mtap(ifp, &m); 668 } 669 } 670 671 if (tp->tun_flags & TUN_IFHEAD) { 672 if (top->m_len < sizeof(family) && 673 (top = m_pullup(top, sizeof(family))) == NULL) 674 return ENOBUFS; 675 family = ntohl(*mtod(top, u_int32_t *)); 676 m_adj(top, sizeof(family)); 677 } else 678 family = AF_INET; 679 680 ifp->if_ibytes += top->m_pkthdr.len; 681 ifp->if_ipackets++; 682 683 switch (family) { 684 #ifdef INET 685 case AF_INET: 686 isr = NETISR_IP; 687 break; 688 #endif 689 #ifdef INET6 690 case AF_INET6: 691 isr = NETISR_IPV6; 692 break; 693 #endif 694 #ifdef IPX 695 case AF_IPX: 696 isr = NETISR_IPX; 697 break; 698 #endif 699 #ifdef NETATALK 700 case AF_APPLETALK: 701 isr = NETISR_ATALK2; 702 break; 703 #endif 704 default: 705 m_freem(m); 706 return (EAFNOSUPPORT); 707 } 708 709 netisr_dispatch(isr, top); 710 return (0); 711 } 712 713 /* 714 * tunpoll - the poll interface, this is only useful on reads 715 * really. The write detect always returns true, write never blocks 716 * anyway, it either accepts the packet or drops it. 717 */ 718 static int 719 tunpoll(dev_t dev, int events, struct thread *td) 720 { 721 int s; 722 struct tun_softc *tp = dev->si_drv1; 723 struct ifnet *ifp = &tp->tun_if; 724 int revents = 0; 725 726 s = splimp(); 727 TUNDEBUG("%s: tunpoll\n", ifp->if_xname); 728 729 if (events & (POLLIN | POLLRDNORM)) { 730 if (ifp->if_snd.ifq_len > 0) { 731 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname, 732 ifp->if_snd.ifq_len); 733 revents |= events & (POLLIN | POLLRDNORM); 734 } else { 735 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname); 736 selrecord(td, &tp->tun_rsel); 737 } 738 } 739 if (events & (POLLOUT | POLLWRNORM)) 740 revents |= events & (POLLOUT | POLLWRNORM); 741 742 splx(s); 743 return (revents); 744 } 745