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