if_tun.c revision 41086
1254721Semaste/* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */ 2254721Semaste 3254721Semaste/* 4254721Semaste * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> 5254721Semaste * Nottingham University 1987. 6254721Semaste * 7254721Semaste * This source may be freely distributed, however I would be interested 8254721Semaste * in any changes that are made. 9254721Semaste * 10254721Semaste * This driver takes packets off the IP i/f and hands them up to a 11254721Semaste * user process to have its wicked way with. This driver has it's 12254721Semaste * roots in a similar driver written by Phil Cockcroft (formerly) at 13254721Semaste * UCL. This driver is based much more on read/write/poll mode of 14254721Semaste * operation though. 15254721Semaste */ 16254721Semaste 17254721Semaste#include "tun.h" 18254721Semaste#if NTUN > 0 19254721Semaste 20254721Semaste#include "opt_devfs.h" 21254721Semaste#include "opt_inet.h" 22254721Semaste 23254721Semaste#include <sys/param.h> 24254721Semaste#include <sys/proc.h> 25254721Semaste#include <sys/systm.h> 26254721Semaste#include <sys/mbuf.h> 27254721Semaste#include <sys/socket.h> 28254721Semaste#include <sys/filio.h> 29254721Semaste#include <sys/sockio.h> 30254721Semaste#include <sys/ttycom.h> 31254721Semaste#include <sys/poll.h> 32254721Semaste#include <sys/signalvar.h> 33254721Semaste#include <sys/filedesc.h> 34254721Semaste#include <sys/kernel.h> 35254721Semaste#include <sys/sysctl.h> 36254721Semaste#ifdef DEVFS 37254721Semaste#include <sys/devfsext.h> 38254721Semaste#endif /*DEVFS*/ 39254721Semaste#include <sys/conf.h> 40254721Semaste#include <sys/uio.h> 41254721Semaste/* 42254721Semaste * XXX stop <sys/vnode.h> from including <vnode_if.h>. <vnode_if.h> doesn't 43254721Semaste * exist if we are an LKM. 44254721Semaste */ 45254721Semaste#undef KERNEL 46254721Semaste#include <sys/vnode.h> 47254721Semaste#define KERNEL 48254721Semaste 49254721Semaste#include <net/if.h> 50254721Semaste#include <net/netisr.h> 51254721Semaste#include <net/route.h> 52254721Semaste 53254721Semaste#ifdef INET 54254721Semaste#include <netinet/in.h> 55254721Semaste#include <netinet/in_var.h> 56254721Semaste#endif 57254721Semaste 58254721Semaste#ifdef NS 59254721Semaste#include <netns/ns.h> 60254721Semaste#include <netns/ns_if.h> 61254721Semaste#endif 62254721Semaste 63254721Semaste#include "bpfilter.h" 64254721Semaste#if NBPFILTER > 0 65254721Semaste#include <net/bpf.h> 66254721Semaste#endif 67254721Semaste 68254721Semaste#include <net/if_tunvar.h> 69254721Semaste#include <net/if_tun.h> 70254721Semaste 71254721Semastestatic void tunattach __P((void *)); 72254721SemastePSEUDO_SET(tunattach, if_tun); 73254721Semaste 74254721Semaste#define TUNDEBUG if (tundebug) printf 75254721Semastestatic int tundebug = 0; 76254721SemasteSYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); 77254721Semaste 78254721Semastestatic struct tun_softc tunctl[NTUN]; 79254721Semaste 80254721Semastestatic int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *, 81254721Semaste struct rtentry *rt)); 82254721Semastestatic int tunifioctl __P((struct ifnet *, u_long, caddr_t)); 83254721Semastestatic int tuninit __P((int)); 84254721Semaste 85254721Semastestatic d_open_t tunopen; 86254721Semastestatic d_close_t tunclose; 87254721Semastestatic d_read_t tunread; 88254721Semastestatic d_write_t tunwrite; 89254721Semastestatic d_ioctl_t tunioctl; 90254721Semastestatic d_poll_t tunpoll; 91254721Semaste 92254721Semaste#define CDEV_MAJOR 52 93254721Semastestatic struct cdevsw tun_cdevsw = { 94254721Semaste tunopen, tunclose, tunread, tunwrite, 95254721Semaste tunioctl, nullstop, noreset, nodevtotty, 96254721Semaste tunpoll, nommap, nostrategy, "tun", NULL, -1 97254721Semaste}; 98254721Semaste 99254721Semaste 100254721Semastestatic int tun_devsw_installed; 101254721Semaste#ifdef DEVFS 102254721Semastestatic void *tun_devfs_token[NTUN]; 103254721Semaste#endif 104254721Semaste 105254721Semaste#define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff)) 106254721Semaste#define dev_val(n) (((n) >> 8) | ((n) & 0xff)) 107254721Semaste 108254721Semastestatic void 109254721Semastetunattach(dummy) 110254721Semaste void *dummy; 111254721Semaste{ 112254721Semaste register int i; 113254721Semaste struct ifnet *ifp; 114254721Semaste dev_t dev; 115254721Semaste 116254721Semaste if ( tun_devsw_installed ) 117254721Semaste return; 118254721Semaste dev = makedev(CDEV_MAJOR, 0); 119254721Semaste cdevsw_add(&dev, &tun_cdevsw, NULL); 120254721Semaste tun_devsw_installed = 1; 121254721Semaste for ( i = 0; i < NTUN; i++ ) { 122254721Semaste#ifdef DEVFS 123254721Semaste tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i), 124254721Semaste DV_CHR, UID_UUCP, 125254721Semaste GID_DIALER, 0600, 126254721Semaste "tun%d", i); 127254721Semaste#endif 128254721Semaste tunctl[i].tun_flags = TUN_INITED; 129254721Semaste 130254721Semaste ifp = &tunctl[i].tun_if; 131254721Semaste ifp->if_unit = i; 132254721Semaste ifp->if_name = "tun"; 133254721Semaste ifp->if_mtu = TUNMTU; 134254721Semaste ifp->if_ioctl = tunifioctl; 135254721Semaste ifp->if_output = tunoutput; 136254721Semaste ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 137254721Semaste ifp->if_snd.ifq_maxlen = ifqmaxlen; 138254721Semaste if_attach(ifp); 139254721Semaste#if NBPFILTER > 0 140254721Semaste bpfattach(ifp, DLT_NULL, sizeof(u_int)); 141254721Semaste#endif 142254721Semaste } 143254721Semaste} 144254721Semaste 145254721Semaste/* 146254721Semaste * tunnel open - must be superuser & the device must be 147254721Semaste * configured in 148254721Semaste */ 149254721Semastestatic int 150254721Semastetunopen(dev, flag, mode, p) 151254721Semaste dev_t dev; 152254721Semaste int flag, mode; 153254721Semaste struct proc *p; 154254721Semaste{ 155254721Semaste struct ifnet *ifp; 156254721Semaste struct tun_softc *tp; 157254721Semaste register int unit, error; 158254721Semaste 159254721Semaste error = suser(p->p_ucred, &p->p_acflag); 160254721Semaste if (error) 161254721Semaste return (error); 162254721Semaste 163254721Semaste if ((unit = dev_val(minor(dev))) >= NTUN) 164254721Semaste return (ENXIO); 165254721Semaste tp = &tunctl[unit]; 166254721Semaste if (tp->tun_flags & TUN_OPEN) 167254721Semaste return EBUSY; 168254721Semaste ifp = &tp->tun_if; 169254721Semaste tp->tun_flags |= TUN_OPEN; 170254721Semaste TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit); 171254721Semaste return (0); 172254721Semaste} 173254721Semaste 174254721Semaste/* 175254721Semaste * tunclose - close the device - mark i/f down & delete 176 * routing info 177 */ 178static int 179tunclose(dev, foo, bar, p) 180 dev_t dev; 181 int foo; 182 int bar; 183 struct proc *p; 184{ 185 register int unit = dev_val(minor(dev)), s; 186 struct tun_softc *tp = &tunctl[unit]; 187 struct ifnet *ifp = &tp->tun_if; 188 struct mbuf *m; 189 190 tp->tun_flags &= ~TUN_OPEN; 191 192 /* 193 * junk all pending output 194 */ 195 do { 196 s = splimp(); 197 IF_DEQUEUE(&ifp->if_snd, m); 198 splx(s); 199 if (m) 200 m_freem(m); 201 } while (m); 202 203 if (ifp->if_flags & IFF_UP) { 204 s = splimp(); 205 if_down(ifp); 206 if (ifp->if_flags & IFF_RUNNING) { 207 /* find internet addresses and delete routes */ 208 register struct ifaddr *ifa; 209 for (ifa = ifp->if_addrhead.tqh_first; ifa; 210 ifa = ifa->ifa_link.tqe_next) { 211 if (ifa->ifa_addr->sa_family == AF_INET) { 212 rtinit(ifa, (int)RTM_DELETE, 213 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 214 } 215 } 216 } 217 splx(s); 218 } 219 funsetown(tp->tun_sigio); 220 selwakeup(&tp->tun_rsel); 221 222 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit); 223 return (0); 224} 225 226static int 227tuninit(unit) 228 int unit; 229{ 230 struct tun_softc *tp = &tunctl[unit]; 231 struct ifnet *ifp = &tp->tun_if; 232 register struct ifaddr *ifa; 233 234 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit); 235 236 ifp->if_flags |= IFF_UP | IFF_RUNNING; 237 getmicrotime(&ifp->if_lastchange); 238 239 for (ifa = ifp->if_addrhead.tqh_first; ifa; 240 ifa = ifa->ifa_link.tqe_next) { 241#ifdef INET 242 if (ifa->ifa_addr->sa_family == AF_INET) { 243 struct sockaddr_in *si; 244 245 si = (struct sockaddr_in *)ifa->ifa_addr; 246 if (si && si->sin_addr.s_addr) 247 tp->tun_flags |= TUN_IASET; 248 249 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 250 if (si && si->sin_addr.s_addr) 251 tp->tun_flags |= TUN_DSTADDR; 252 } 253#endif 254 } 255 return 0; 256} 257 258/* 259 * Process an ioctl request. 260 */ 261int 262tunifioctl(ifp, cmd, data) 263 struct ifnet *ifp; 264 u_long cmd; 265 caddr_t data; 266{ 267 register struct ifreq *ifr = (struct ifreq *)data; 268 int error = 0, s; 269 270 s = splimp(); 271 switch(cmd) { 272 case SIOCSIFADDR: 273 tuninit(ifp->if_unit); 274 TUNDEBUG("%s%d: address set\n", 275 ifp->if_name, ifp->if_unit); 276 break; 277 case SIOCSIFDSTADDR: 278 tuninit(ifp->if_unit); 279 TUNDEBUG("%s%d: destination address set\n", 280 ifp->if_name, ifp->if_unit); 281 break; 282 case SIOCSIFMTU: 283 ifp->if_mtu = ifr->ifr_mtu; 284 TUNDEBUG("%s%d: mtu set\n", 285 ifp->if_name, ifp->if_unit); 286 break; 287 case SIOCADDMULTI: 288 case SIOCDELMULTI: 289 break; 290 291 292 default: 293 error = EINVAL; 294 } 295 splx(s); 296 return (error); 297} 298 299/* 300 * tunoutput - queue packets from higher level ready to put out. 301 */ 302int 303tunoutput(ifp, m0, dst, rt) 304 struct ifnet *ifp; 305 struct mbuf *m0; 306 struct sockaddr *dst; 307 struct rtentry *rt; 308{ 309 struct tun_softc *tp = &tunctl[ifp->if_unit]; 310 struct proc *p; 311 int s; 312 313 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit); 314 315 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 316 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 317 ifp->if_unit, tp->tun_flags); 318 m_freem (m0); 319 return EHOSTDOWN; 320 } 321 322#if NBPFILTER > 0 323 /* BPF write needs to be handled specially */ 324 if (dst->sa_family == AF_UNSPEC) { 325 dst->sa_family = *(mtod(m0, int *)); 326 m0->m_len -= sizeof(int); 327 m0->m_pkthdr.len -= sizeof(int); 328 m0->m_data += sizeof(int); 329 } 330 331 if (ifp->if_bpf) { 332 /* 333 * We need to prepend the address family as 334 * a four byte field. Cons up a dummy header 335 * to pacify bpf. This is safe because bpf 336 * will only read from the mbuf (i.e., it won't 337 * try to free it or keep a pointer to it). 338 */ 339 struct mbuf m; 340 u_int af = dst->sa_family; 341 342 m.m_next = m0; 343 m.m_len = 4; 344 m.m_data = (char *)⁡ 345 346 bpf_mtap(ifp, &m); 347 } 348#endif 349 350 switch(dst->sa_family) { 351#ifdef INET 352 case AF_INET: 353 s = splimp(); 354 if (IF_QFULL(&ifp->if_snd)) { 355 IF_DROP(&ifp->if_snd); 356 m_freem(m0); 357 splx(s); 358 ifp->if_collisions++; 359 return (ENOBUFS); 360 } 361 ifp->if_obytes += m0->m_pkthdr.len; 362 IF_ENQUEUE(&ifp->if_snd, m0); 363 splx(s); 364 ifp->if_opackets++; 365 break; 366#endif 367 default: 368 m_freem(m0); 369 return EAFNOSUPPORT; 370 } 371 372 if (tp->tun_flags & TUN_RWAIT) { 373 tp->tun_flags &= ~TUN_RWAIT; 374 wakeup((caddr_t)tp); 375 } 376 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) 377 pgsigio(tp->tun_sigio, SIGIO, 0); 378 selwakeup(&tp->tun_rsel); 379 return 0; 380} 381 382/* 383 * the cdevsw interface is now pretty minimal. 384 */ 385static int 386tunioctl(dev, cmd, data, flag, p) 387 dev_t dev; 388 u_long cmd; 389 caddr_t data; 390 int flag; 391 struct proc *p; 392{ 393 int unit = dev_val(minor(dev)), s; 394 struct tun_softc *tp = &tunctl[unit]; 395 struct tuninfo *tunp; 396 397 switch (cmd) { 398 case TUNSIFINFO: 399 tunp = (struct tuninfo *)data; 400 tp->tun_if.if_mtu = tunp->mtu; 401 tp->tun_if.if_type = tunp->type; 402 tp->tun_if.if_baudrate = tunp->baudrate; 403 break; 404 case TUNGIFINFO: 405 tunp = (struct tuninfo *)data; 406 tunp->mtu = tp->tun_if.if_mtu; 407 tunp->type = tp->tun_if.if_type; 408 tunp->baudrate = tp->tun_if.if_baudrate; 409 break; 410 case TUNSDEBUG: 411 tundebug = *(int *)data; 412 break; 413 case TUNGDEBUG: 414 *(int *)data = tundebug; 415 break; 416 case FIONBIO: 417 break; 418 case FIOASYNC: 419 if (*(int *)data) 420 tp->tun_flags |= TUN_ASYNC; 421 else 422 tp->tun_flags &= ~TUN_ASYNC; 423 break; 424 case FIONREAD: 425 s = splimp(); 426 if (tp->tun_if.if_snd.ifq_head) { 427 struct mbuf *mb = tp->tun_if.if_snd.ifq_head; 428 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 429 *(int *)data += mb->m_len; 430 } else 431 *(int *)data = 0; 432 splx(s); 433 break; 434 case FIOSETOWN: 435 return (fsetown(*(int *)data, &tp->tun_sigio)); 436 437 case FIOGETOWN: 438 *(int *)data = fgetown(tp->tun_sigio); 439 return (0); 440 441 /* This is deprecated, FIOSETOWN should be used instead. */ 442 case TIOCSPGRP: 443 return (fsetown(-(*(int *)data), &tp->tun_sigio)); 444 445 /* This is deprecated, FIOGETOWN should be used instead. */ 446 case TIOCGPGRP: 447 *(int *)data = -fgetown(tp->tun_sigio); 448 return (0); 449 450 default: 451 return (ENOTTY); 452 } 453 return (0); 454} 455 456/* 457 * The cdevsw read interface - reads a packet at a time, or at 458 * least as much of a packet as can be read. 459 */ 460static int 461tunread(dev, uio, flag) 462 dev_t dev; 463 struct uio *uio; 464 int flag; 465{ 466 int unit = dev_val(minor(dev)); 467 struct tun_softc *tp = &tunctl[unit]; 468 struct ifnet *ifp = &tp->tun_if; 469 struct mbuf *m, *m0; 470 int error=0, len, s; 471 472 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit); 473 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 474 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 475 ifp->if_unit, tp->tun_flags); 476 return EHOSTDOWN; 477 } 478 479 tp->tun_flags &= ~TUN_RWAIT; 480 481 s = splimp(); 482 do { 483 IF_DEQUEUE(&ifp->if_snd, m0); 484 if (m0 == 0) { 485 if (flag & IO_NDELAY) { 486 splx(s); 487 return EWOULDBLOCK; 488 } 489 tp->tun_flags |= TUN_RWAIT; 490 if( error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1), 491 "tunread", 0)) { 492 splx(s); 493 return error; 494 } 495 } 496 } while (m0 == 0); 497 splx(s); 498 499 while (m0 && uio->uio_resid > 0 && error == 0) { 500 len = min(uio->uio_resid, m0->m_len); 501 if (len == 0) 502 break; 503 error = uiomove(mtod(m0, caddr_t), len, uio); 504 MFREE(m0, m); 505 m0 = m; 506 } 507 508 if (m0) { 509 TUNDEBUG("Dropping mbuf\n"); 510 m_freem(m0); 511 } 512 return error; 513} 514 515/* 516 * the cdevsw write interface - an atomic write is a packet - or else! 517 */ 518static int 519tunwrite(dev, uio, flag) 520 dev_t dev; 521 struct uio *uio; 522 int flag; 523{ 524 int unit = dev_val(minor(dev)); 525 struct ifnet *ifp = &tunctl[unit].tun_if; 526 struct mbuf *top, **mp, *m; 527 int error=0, s, tlen, mlen; 528 529 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit); 530 531 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { 532 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit, 533 uio->uio_resid); 534 return EIO; 535 } 536 tlen = uio->uio_resid; 537 538 /* get a header mbuf */ 539 MGETHDR(m, M_DONTWAIT, MT_DATA); 540 if (m == NULL) 541 return ENOBUFS; 542 mlen = MHLEN; 543 544 top = 0; 545 mp = ⊤ 546 while (error == 0 && uio->uio_resid > 0) { 547 m->m_len = min(mlen, uio->uio_resid); 548 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 549 *mp = m; 550 mp = &m->m_next; 551 if (uio->uio_resid > 0) { 552 MGET (m, M_DONTWAIT, MT_DATA); 553 if (m == 0) { 554 error = ENOBUFS; 555 break; 556 } 557 mlen = MLEN; 558 } 559 } 560 if (error) { 561 if (top) 562 m_freem (top); 563 return error; 564 } 565 566 top->m_pkthdr.len = tlen; 567 top->m_pkthdr.rcvif = ifp; 568 569#if NBPFILTER > 0 570 if (ifp->if_bpf) { 571 /* 572 * We need to prepend the address family as 573 * a four byte field. Cons up a dummy header 574 * to pacify bpf. This is safe because bpf 575 * will only read from the mbuf (i.e., it won't 576 * try to free it or keep a pointer to it). 577 */ 578 struct mbuf m; 579 u_int af = AF_INET; 580 581 m.m_next = top; 582 m.m_len = 4; 583 m.m_data = (char *)⁡ 584 585 bpf_mtap(ifp, &m); 586 } 587#endif 588 589#ifdef INET 590 s = splimp(); 591 if (IF_QFULL (&ipintrq)) { 592 IF_DROP(&ipintrq); 593 splx(s); 594 ifp->if_collisions++; 595 m_freem(top); 596 return ENOBUFS; 597 } 598 IF_ENQUEUE(&ipintrq, top); 599 splx(s); 600 ifp->if_ibytes += tlen; 601 ifp->if_ipackets++; 602 schednetisr(NETISR_IP); 603#endif 604 return error; 605} 606 607/* 608 * tunpoll - the poll interface, this is only useful on reads 609 * really. The write detect always returns true, write never blocks 610 * anyway, it either accepts the packet or drops it. 611 */ 612static int 613tunpoll(dev, events, p) 614 dev_t dev; 615 int events; 616 struct proc *p; 617{ 618 int unit = dev_val(minor(dev)), s; 619 struct tun_softc *tp = &tunctl[unit]; 620 struct ifnet *ifp = &tp->tun_if; 621 int revents = 0; 622 623 s = splimp(); 624 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit); 625 626 if (events & (POLLIN | POLLRDNORM)) 627 if (ifp->if_snd.ifq_len > 0) { 628 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name, 629 ifp->if_unit, ifp->if_snd.ifq_len); 630 revents |= events & (POLLIN | POLLRDNORM); 631 } else { 632 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name, 633 ifp->if_unit); 634 selrecord(p, &tp->tun_rsel); 635 } 636 637 if (events & (POLLOUT | POLLWRNORM)) 638 revents |= events & (POLLOUT | POLLWRNORM); 639 640 splx(s); 641 return (revents); 642} 643 644 645#endif /* NTUN */ 646