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