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