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