if_tun.c revision 72012
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 72012 2001-02-04 16:08:18Z phk $ 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 /* bmaj */ -1 91}; 92 93static void tun_clone __P((void *arg, char *name, int namelen, dev_t *dev)); 94 95static void 96tun_clone(arg, name, namelen, dev) 97 void *arg; 98 char *name; 99 int namelen; 100 dev_t *dev; 101{ 102 int u; 103 104 if (*dev != NODEV) 105 return; 106 if (dev_stdclone(name, NULL, "tun", &u) != 1) 107 return; 108 *dev = make_dev(&tun_cdevsw, unit2minor(u), 109 UID_ROOT, GID_WHEEL, 0600, "tun%d", u); 110 111} 112 113static int 114tun_modevent(module_t mod, int type, void *data) 115{ 116 switch (type) { 117 case MOD_LOAD: 118 EVENTHANDLER_REGISTER(dev_clone, tun_clone, 0, 1000); 119 cdevsw_add(&tun_cdevsw); 120 break; 121 case MOD_UNLOAD: 122 printf("if_tun module unload - not possible for this module type\n"); 123 return EINVAL; 124 } 125 return 0; 126} 127 128static moduledata_t tun_mod = { 129 "if_tun", 130 tun_modevent, 131 0 132}; 133 134DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 135 136static void 137tunstart(ifp) 138 struct ifnet *ifp; 139{ 140 struct tun_softc *tp = ifp->if_softc; 141 142 if (tp->tun_flags & TUN_RWAIT) { 143 tp->tun_flags &= ~TUN_RWAIT; 144 wakeup((caddr_t)tp); 145 } 146 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) 147 pgsigio(tp->tun_sigio, SIGIO, 0); 148 selwakeup(&tp->tun_rsel); 149} 150 151static void 152tuncreate(dev) 153 dev_t dev; 154{ 155 struct tun_softc *sc; 156 struct ifnet *ifp; 157 158 dev = make_dev(&tun_cdevsw, minor(dev), 159 UID_UUCP, GID_DIALER, 0600, "tun%d", dev2unit(dev)); 160 161 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO); 162 sc->tun_flags = TUN_INITED; 163 164 ifp = &sc->tun_if; 165 ifp->if_unit = dev2unit(dev); 166 ifp->if_name = "tun"; 167 ifp->if_mtu = TUNMTU; 168 ifp->if_ioctl = tunifioctl; 169 ifp->if_output = tunoutput; 170 ifp->if_start = tunstart; 171 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 172 ifp->if_type = IFT_PPP; 173 ifp->if_snd.ifq_maxlen = ifqmaxlen; 174 ifp->if_softc = sc; 175 if_attach(ifp); 176 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 177 dev->si_drv1 = sc; 178} 179 180/* 181 * tunnel open - must be superuser & the device must be 182 * configured in 183 */ 184static int 185tunopen(dev, flag, mode, p) 186 dev_t dev; 187 int flag, mode; 188 struct proc *p; 189{ 190 struct ifnet *ifp; 191 struct tun_softc *tp; 192 193 tp = dev->si_drv1; 194 if (!tp) { 195 tuncreate(dev); 196 tp = dev->si_drv1; 197 } 198 if (tp->tun_flags & TUN_OPEN) 199 return EBUSY; 200 tp->tun_pid = p->p_pid; 201 ifp = &tp->tun_if; 202 tp->tun_flags |= TUN_OPEN; 203 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit); 204 return (0); 205} 206 207/* 208 * tunclose - close the device - mark i/f down & delete 209 * routing info 210 */ 211static int 212tunclose(dev, foo, bar, p) 213 dev_t dev; 214 int foo; 215 int bar; 216 struct proc *p; 217{ 218 register int s; 219 struct tun_softc *tp; 220 struct ifnet *ifp; 221 222 tp = dev->si_drv1; 223 ifp = &tp->tun_if; 224 225 tp->tun_flags &= ~TUN_OPEN; 226 tp->tun_pid = 0; 227 228 /* 229 * junk all pending output 230 */ 231 IF_DRAIN(&ifp->if_snd); 232 233 if (ifp->if_flags & IFF_UP) { 234 s = splimp(); 235 if_down(ifp); 236 splx(s); 237 } 238 239 if (ifp->if_flags & IFF_RUNNING) { 240 register struct ifaddr *ifa; 241 242 s = splimp(); 243 /* find internet addresses and delete routes */ 244 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 245 if (ifa->ifa_addr->sa_family == AF_INET) 246 rtinit(ifa, (int)RTM_DELETE, 247 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 248 ifp->if_flags &= ~IFF_RUNNING; 249 splx(s); 250 } 251 252 funsetown(tp->tun_sigio); 253 selwakeup(&tp->tun_rsel); 254 255 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit); 256 return (0); 257} 258 259static int 260tuninit(ifp) 261 struct ifnet *ifp; 262{ 263 struct tun_softc *tp = ifp->if_softc; 264 register struct ifaddr *ifa; 265 int error = 0; 266 267 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit); 268 269 ifp->if_flags |= IFF_UP | IFF_RUNNING; 270 getmicrotime(&ifp->if_lastchange); 271 272 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 273 ifa = TAILQ_NEXT(ifa, ifa_link)) { 274 if (ifa->ifa_addr == NULL) 275 error = EFAULT; 276 /* XXX: Should maybe return straight off? */ 277 else { 278#ifdef INET 279 if (ifa->ifa_addr->sa_family == AF_INET) { 280 struct sockaddr_in *si; 281 282 si = (struct sockaddr_in *)ifa->ifa_addr; 283 if (si->sin_addr.s_addr) 284 tp->tun_flags |= TUN_IASET; 285 286 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 287 if (si && si->sin_addr.s_addr) 288 tp->tun_flags |= TUN_DSTADDR; 289 } 290#endif 291 } 292 } 293 return (error); 294} 295 296/* 297 * Process an ioctl request. 298 */ 299int 300tunifioctl(ifp, cmd, data) 301 struct ifnet *ifp; 302 u_long cmd; 303 caddr_t data; 304{ 305 struct ifreq *ifr = (struct ifreq *)data; 306 struct tun_softc *tp = ifp->if_softc; 307 struct ifstat *ifs; 308 int error = 0, s; 309 310 s = splimp(); 311 switch(cmd) { 312 case SIOCGIFSTATUS: 313 ifs = (struct ifstat *)data; 314 if (tp->tun_pid) 315 sprintf(ifs->ascii + strlen(ifs->ascii), 316 "\tOpened by PID %d\n", tp->tun_pid); 317 break; 318 case SIOCSIFADDR: 319 error = tuninit(ifp); 320 TUNDEBUG("%s%d: address set, error=%d\n", 321 ifp->if_name, ifp->if_unit, error); 322 break; 323 case SIOCSIFDSTADDR: 324 error = tuninit(ifp); 325 TUNDEBUG("%s%d: destination address set, error=%d\n", 326 ifp->if_name, ifp->if_unit, error); 327 break; 328 case SIOCSIFMTU: 329 ifp->if_mtu = ifr->ifr_mtu; 330 TUNDEBUG("%s%d: mtu set\n", ifp->if_name, ifp->if_unit); 331 break; 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