if_tun.c revision 71921
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 71921 2001-02-02 03:32:25Z 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 /* 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_UUCP, GID_DIALER, 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 register int error; 193 194 error = suser(p); 195 if (error) 196 return (error); 197 198 tp = dev->si_drv1; 199 if (!tp) { 200 tuncreate(dev); 201 tp = dev->si_drv1; 202 } 203 if (tp->tun_flags & TUN_OPEN) 204 return EBUSY; 205 tp->tun_pid = p->p_pid; 206 ifp = &tp->tun_if; 207 tp->tun_flags |= TUN_OPEN; 208 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit); 209 return (0); 210} 211 212/* 213 * tunclose - close the device - mark i/f down & delete 214 * routing info 215 */ 216static int 217tunclose(dev, foo, bar, p) 218 dev_t dev; 219 int foo; 220 int bar; 221 struct proc *p; 222{ 223 register int s; 224 struct tun_softc *tp; 225 struct ifnet *ifp; 226 227 tp = dev->si_drv1; 228 ifp = &tp->tun_if; 229 230 tp->tun_flags &= ~TUN_OPEN; 231 tp->tun_pid = 0; 232 233 /* 234 * junk all pending output 235 */ 236 IF_DRAIN(&ifp->if_snd); 237 238 if (ifp->if_flags & IFF_UP) { 239 s = splimp(); 240 if_down(ifp); 241 splx(s); 242 } 243 244 if (ifp->if_flags & IFF_RUNNING) { 245 register struct ifaddr *ifa; 246 247 s = splimp(); 248 /* find internet addresses and delete routes */ 249 for (ifa = ifp->if_addrhead.tqh_first; ifa; 250 ifa = ifa->ifa_link.tqe_next) 251 if (ifa->ifa_addr->sa_family == AF_INET) 252 rtinit(ifa, (int)RTM_DELETE, 253 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 254 ifp->if_flags &= ~IFF_RUNNING; 255 splx(s); 256 } 257 258 funsetown(tp->tun_sigio); 259 selwakeup(&tp->tun_rsel); 260 261 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit); 262 return (0); 263} 264 265static int 266tuninit(ifp) 267 struct ifnet *ifp; 268{ 269 struct tun_softc *tp = ifp->if_softc; 270 register struct ifaddr *ifa; 271 int error = 0; 272 273 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit); 274 275 ifp->if_flags |= IFF_UP | IFF_RUNNING; 276 getmicrotime(&ifp->if_lastchange); 277 278 for (ifa = ifp->if_addrhead.tqh_first; ifa; 279 ifa = ifa->ifa_link.tqe_next) { 280 if (ifa->ifa_addr == NULL) 281 error = EFAULT; 282 /* XXX: Should maybe return straight off? */ 283 else { 284#ifdef INET 285 if (ifa->ifa_addr->sa_family == AF_INET) { 286 struct sockaddr_in *si; 287 288 si = (struct sockaddr_in *)ifa->ifa_addr; 289 if (si->sin_addr.s_addr) 290 tp->tun_flags |= TUN_IASET; 291 292 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 293 if (si && si->sin_addr.s_addr) 294 tp->tun_flags |= TUN_DSTADDR; 295 } 296#endif 297 } 298 } 299 return (error); 300} 301 302/* 303 * Process an ioctl request. 304 */ 305int 306tunifioctl(ifp, cmd, data) 307 struct ifnet *ifp; 308 u_long cmd; 309 caddr_t data; 310{ 311 struct ifreq *ifr = (struct ifreq *)data; 312 struct tun_softc *tp = ifp->if_softc; 313 struct ifstat *ifs; 314 int error = 0, s; 315 316 s = splimp(); 317 switch(cmd) { 318 case SIOCGIFSTATUS: 319 ifs = (struct ifstat *)data; 320 if (tp->tun_pid) 321 sprintf(ifs->ascii + strlen(ifs->ascii), 322 "\tOpened by PID %d\n", tp->tun_pid); 323 break; 324 case SIOCSIFADDR: 325 error = tuninit(ifp); 326 TUNDEBUG("%s%d: address set, error=%d\n", 327 ifp->if_name, ifp->if_unit, error); 328 break; 329 case SIOCSIFDSTADDR: 330 error = tuninit(ifp); 331 TUNDEBUG("%s%d: destination address set, error=%d\n", 332 ifp->if_name, ifp->if_unit, error); 333 break; 334 case SIOCSIFMTU: 335 ifp->if_mtu = ifr->ifr_mtu; 336 TUNDEBUG("%s%d: mtu set\n", 337 ifp->if_name, ifp->if_unit); 338 break; 339 case SIOCADDMULTI: 340 case SIOCDELMULTI: 341 break; 342 default: 343 error = EINVAL; 344 } 345 splx(s); 346 return (error); 347} 348 349/* 350 * tunoutput - queue packets from higher level ready to put out. 351 */ 352int 353tunoutput(ifp, m0, dst, rt) 354 struct ifnet *ifp; 355 struct mbuf *m0; 356 struct sockaddr *dst; 357 struct rtentry *rt; 358{ 359 struct tun_softc *tp = ifp->if_softc; 360 361 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit); 362 363 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 364 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 365 ifp->if_unit, tp->tun_flags); 366 m_freem (m0); 367 return EHOSTDOWN; 368 } 369 370 /* BPF write needs to be handled specially */ 371 if (dst->sa_family == AF_UNSPEC) { 372 dst->sa_family = *(mtod(m0, int *)); 373 m0->m_len -= sizeof(int); 374 m0->m_pkthdr.len -= sizeof(int); 375 m0->m_data += sizeof(int); 376 } 377 378 if (ifp->if_bpf) { 379 /* 380 * We need to prepend the address family as 381 * a four byte field. Cons up a dummy header 382 * to pacify bpf. This is safe because bpf 383 * will only read from the mbuf (i.e., it won't 384 * try to free it or keep a pointer to it). 385 */ 386 struct mbuf m; 387 uint32_t af = dst->sa_family; 388 389 m.m_next = m0; 390 m.m_len = 4; 391 m.m_data = (char *)⁡ 392 393 bpf_mtap(ifp, &m); 394 } 395 396 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 397 if (tp->tun_flags & TUN_LMODE) { 398 /* allocate space for sockaddr */ 399 M_PREPEND(m0, dst->sa_len, M_DONTWAIT); 400 401 /* if allocation failed drop packet */ 402 if (m0 == NULL) { 403 ifp->if_iqdrops++; 404 ifp->if_oerrors++; 405 return (ENOBUFS); 406 } else { 407 bcopy(dst, m0->m_data, dst->sa_len); 408 } 409 } 410 411 if (tp->tun_flags & TUN_IFHEAD) { 412 /* Prepend the address family */ 413 M_PREPEND(m0, 4, M_DONTWAIT); 414 415 /* if allocation failed drop packet */ 416 if (m0 == NULL) { 417 ifp->if_iqdrops++; 418 ifp->if_oerrors++; 419 return ENOBUFS; 420 } else 421 *(u_int32_t *)m0->m_data = htonl(dst->sa_family); 422 } else { 423#ifdef INET 424 if (dst->sa_family != AF_INET) 425#endif 426 { 427 m_freem(m0); 428 return EAFNOSUPPORT; 429 } 430 } 431 432 if (! IF_HANDOFF(&ifp->if_snd, m0, ifp)) { 433 ifp->if_collisions++; 434 return ENOBUFS; 435 } 436 ifp->if_opackets++; 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