if_tun.c revision 16258
1117610Sdes/* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */ 2255376Sdes 3141098Sdes/* 4255376Sdes * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> 5141098Sdes * Nottingham University 1987. 6255376Sdes * 7255376Sdes * This source may be freely distributed, however I would be interested 8117610Sdes * in any changes that are made. 9117610Sdes * 10117610Sdes * This driver takes packets off the IP i/f and hands them up to a 11117610Sdes * user process to have it's wicked way with. This driver has it's 12117610Sdes * roots in a similar driver written by Phil Cockcroft (formerly) at 13117610Sdes * UCL. This driver is based much more on read/write/select mode of 14117610Sdes * operation though. 15117610Sdes */ 16117610Sdes 17117610Sdes#include "tun.h" 18117610Sdes#if NTUN > 0 19117610Sdes 20228692Sdes#include <sys/param.h> 21117610Sdes#include <sys/proc.h> 22117610Sdes#include <sys/systm.h> 23117610Sdes#include <sys/mbuf.h> 24117610Sdes#include <sys/buf.h> 25117610Sdes#include <sys/protosw.h> 26117610Sdes#include <sys/socket.h> 27117610Sdes#include <sys/ioctl.h> 28255376Sdes#include <sys/errno.h> 29117610Sdes#include <sys/syslog.h> 30117610Sdes#include <sys/select.h> 31117610Sdes#include <sys/file.h> 32255376Sdes#include <sys/signalvar.h> 33117610Sdes#include <sys/kernel.h> 34255376Sdes#include <sys/sysctl.h> 35255376Sdes#ifdef DEVFS 36255376Sdes#include <sys/devfsext.h> 37255376Sdes#endif /*DEVFS*/ 38255376Sdes#include <sys/conf.h> 39117610Sdes 40255376Sdes#include <net/if.h> 41255376Sdes#include <net/netisr.h> 42255376Sdes#include <net/route.h> 43255376Sdes 44141098Sdes#ifdef INET 45117610Sdes#include <netinet/in.h> 46117610Sdes#include <netinet/in_systm.h> 47117610Sdes#include <netinet/in_var.h> 48117610Sdes#include <netinet/ip.h> 49255376Sdes#include <netinet/if_ether.h> 50255376Sdes#endif 51117610Sdes 52117610Sdes#ifdef NS 53117610Sdes#include <netns/ns.h> 54117610Sdes#include <netns/ns_if.h> 55117610Sdes#endif 56117610Sdes 57255376Sdes#include "bpfilter.h" 58255376Sdes#if NBPFILTER > 0 59141098Sdes#include <sys/time.h> 60255376Sdes#include <net/bpf.h> 61255376Sdes#endif 62228692Sdes 63141098Sdes#include <net/if_tun.h> 64174832Sdes 65117610Sdesstatic void tunattach __P((void *)); 66117610SdesPSEUDO_SET(tunattach, if_tun); 67117610Sdes 68141098Sdes#define TUNDEBUG if (tundebug) printf 69174832Sdesstatic int tundebug = 0; 70117610SdesSYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); 71117610Sdes 72117610Sdesstatic struct tun_softc tunctl[NTUN]; 73255376Sdes 74255376Sdesstatic int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *, 75117610Sdes struct rtentry *rt)); 76117610Sdesstatic int tunifioctl __P((struct ifnet *, int, caddr_t)); 77117610Sdesstatic int tuninit __P((int)); 78174832Sdes 79174832Sdesstatic d_open_t tunopen; 80255376Sdesstatic d_close_t tunclose; 81255376Sdesstatic d_read_t tunread; 82228692Sdesstatic d_write_t tunwrite; 83255376Sdesstatic d_ioctl_t tunioctl; 84255376Sdesstatic d_select_t tunselect; 85174832Sdes 86255376Sdes#define CDEV_MAJOR 52 87255376Sdesstatic struct cdevsw tun_cdevsw = { 88255376Sdes tunopen, tunclose, tunread, tunwrite, 89255376Sdes tunioctl, nullstop, noreset, nodevtotty, 90174832Sdes tunselect, nommap, nostrategy, "tun", NULL, -1 91255376Sdes}; 92255376Sdes 93255376Sdes 94255376Sdesstatic tun_devsw_installed = 0; 95255376Sdes#ifdef DEVFS 96255376Sdesstatic void *tun_devfs_token[NTUN]; 97255376Sdes#endif 98255376Sdes 99255376Sdesstatic void 100255376Sdestunattach(dummy) 101255376Sdes void *dummy; 102255376Sdes{ 103141098Sdes register int i; 104255376Sdes struct ifnet *ifp; 105255376Sdes dev_t dev; 106255376Sdes 107117610Sdes if( tun_devsw_installed ) return; 108255376Sdes dev = makedev(CDEV_MAJOR, 0); 109255376Sdes cdevsw_add(&dev,&tun_cdevsw, NULL); 110255376Sdes tun_devsw_installed = 1; 111255376Sdes for ( i = 0; i < NTUN; i++ ) { 112255376Sdes#ifdef DEVFS 113255376Sdes tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, i, DV_CHR, 114255376Sdes UID_UUCP, GID_DIALER, 115255376Sdes 0600, "tun%d", i); 116255376Sdes#endif 117255376Sdes tunctl[i].tun_flags = TUN_INITED; 118255376Sdes 119255376Sdes ifp = &tunctl[i].tun_if; 120255376Sdes ifp->if_unit = i; 121255376Sdes ifp->if_name = "tun"; 122255376Sdes ifp->if_mtu = TUNMTU; 123255376Sdes ifp->if_ioctl = tunifioctl; 124255376Sdes ifp->if_output = tunoutput; 125255376Sdes ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 126255376Sdes ifp->if_snd.ifq_maxlen = ifqmaxlen; 127255376Sdes ifp->if_collisions = 0; 128117610Sdes ifp->if_ierrors = 0; 129255376Sdes ifp->if_oerrors = 0; 130255376Sdes ifp->if_ipackets = 0; 131255376Sdes ifp->if_opackets = 0; 132255376Sdes if_attach(ifp); 133255376Sdes#if NBPFILTER > 0 134255376Sdes bpfattach(ifp, DLT_NULL, sizeof(u_int)); 135255376Sdes#endif 136117610Sdes } 137255376Sdes} 138117610Sdes 139255376Sdes/* 140255376Sdes * tunnel open - must be superuser & the device must be 141255376Sdes * configured in 142255376Sdes */ 143255376Sdesstatic int 144255376Sdestunopen(dev, flag, mode, p) 145255376Sdes dev_t dev; 146255376Sdes int flag, mode; 147255376Sdes struct proc *p; 148255376Sdes{ 149255376Sdes struct ifnet *ifp; 150255376Sdes struct tun_softc *tp; 151255376Sdes register int unit, error; 152255376Sdes 153255376Sdes error = suser(p->p_ucred, &p->p_acflag); 154255376Sdes if (error) 155255376Sdes return (error); 156255376Sdes 157255376Sdes if ((unit = minor(dev)) >= NTUN) 158255376Sdes return (ENXIO); 159255376Sdes tp = &tunctl[unit]; 160255376Sdes if (tp->tun_flags & TUN_OPEN) 161255376Sdes return EBUSY; 162255376Sdes ifp = &tp->tun_if; 163255376Sdes tp->tun_flags |= TUN_OPEN; 164255376Sdes TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit); 165255376Sdes return (0); 166255376Sdes} 167255376Sdes 168255376Sdes/* 169255376Sdes * tunclose - close the device - mark i/f down & delete 170255376Sdes * routing info 171255376Sdes */ 172255376Sdesstatic int 173255376Sdestunclose(dev_t dev, int foo, int bar, struct proc *p) 174255376Sdes{ 175255376Sdes register int unit = minor(dev), s; 176255376Sdes struct tun_softc *tp = &tunctl[unit]; 177255376Sdes struct ifnet *ifp = &tp->tun_if; 178255376Sdes struct mbuf *m; 179255376Sdes 180255376Sdes tp->tun_flags &= ~TUN_OPEN; 181141098Sdes 182255376Sdes /* 183255376Sdes * junk all pending output 184255376Sdes */ 185255376Sdes do { 186255376Sdes s = splimp(); 187255376Sdes IF_DEQUEUE(&ifp->if_snd, m); 188255376Sdes splx(s); 189255376Sdes if (m) 190255376Sdes m_freem(m); 191255376Sdes } while (m); 192255376Sdes 193255376Sdes if (ifp->if_flags & IFF_UP) { 194255376Sdes s = splimp(); 195255376Sdes if_down(ifp); 196255376Sdes if (ifp->if_flags & IFF_RUNNING) { 197255376Sdes /* find internet addresses and delete routes */ 198255376Sdes register struct ifaddr *ifa; 199255376Sdes for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { 200255376Sdes if (ifa->ifa_addr->sa_family == AF_INET) { 201141098Sdes rtinit(ifa, (int)RTM_DELETE, 202255376Sdes tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 203255376Sdes } 204117610Sdes } 205255376Sdes } 206255376Sdes splx(s); 207255376Sdes } 208117610Sdes tp->tun_pgrp = 0; 209141098Sdes selwakeup(&tp->tun_rsel); 210141098Sdes 211141098Sdes TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit); 212141098Sdes return (0); 213228692Sdes} 214228692Sdes 215141098Sdesstatic int 216tuninit(unit) 217 int unit; 218{ 219 struct tun_softc *tp = &tunctl[unit]; 220 struct ifnet *ifp = &tp->tun_if; 221 register struct ifaddr *ifa; 222 223 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit); 224 225 ifp->if_flags |= IFF_UP | IFF_RUNNING; 226 227 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 228 if (ifa->ifa_addr->sa_family == AF_INET) { 229 struct sockaddr_in *si; 230 231 si = (struct sockaddr_in *)ifa->ifa_addr; 232 if (si && si->sin_addr.s_addr) 233 tp->tun_flags |= TUN_IASET; 234 235 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 236 if (si && si->sin_addr.s_addr) 237 tp->tun_flags |= TUN_DSTADDR; 238 } 239 240 return 0; 241} 242 243/* 244 * Process an ioctl request. 245 */ 246int 247tunifioctl(ifp, cmd, data) 248 struct ifnet *ifp; 249 int cmd; 250 caddr_t data; 251{ 252 register struct ifreq *ifr = (struct ifreq *)data; 253 int error = 0, s; 254 255 s = splimp(); 256 switch(cmd) { 257 case SIOCSIFADDR: 258 tuninit(ifp->if_unit); 259 TUNDEBUG("%s%d: address set\n", 260 ifp->if_name, ifp->if_unit); 261 break; 262 case SIOCSIFDSTADDR: 263 tuninit(ifp->if_unit); 264 TUNDEBUG("%s%d: destination address set\n", 265 ifp->if_name, ifp->if_unit); 266 break; 267 case SIOCADDMULTI: 268 case SIOCDELMULTI: 269 if (ifr == 0) { 270 error = EAFNOSUPPORT; /* XXX */ 271 break; 272 } 273 switch (ifr->ifr_addr.sa_family) { 274 275#ifdef INET 276 case AF_INET: 277 break; 278#endif 279 280 default: 281 error = EAFNOSUPPORT; 282 break; 283 } 284 break; 285 286 287 default: 288 error = EINVAL; 289 } 290 splx(s); 291 return (error); 292} 293 294/* 295 * tunoutput - queue packets from higher level ready to put out. 296 */ 297int 298tunoutput(ifp, m0, dst, rt) 299 struct ifnet *ifp; 300 struct mbuf *m0; 301 struct sockaddr *dst; 302 struct rtentry *rt; 303{ 304 struct tun_softc *tp = &tunctl[ifp->if_unit]; 305 struct proc *p; 306 int s; 307 308 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit); 309 310 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 311 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 312 ifp->if_unit, tp->tun_flags); 313 m_freem (m0); 314 return EHOSTDOWN; 315 } 316 317#if NBPFILTER > 0 318 /* BPF write needs to be handled specially */ 319 if (dst->sa_family == AF_UNSPEC) { 320 dst->sa_family = *(mtod(m0, int *)); 321 m0->m_len -= sizeof(int); 322 m0->m_pkthdr.len -= sizeof(int); 323 m0->m_data += sizeof(int); 324 } 325 326 if (ifp->if_bpf) { 327 /* 328 * We need to prepend the address family as 329 * a four byte field. Cons up a dummy header 330 * to pacify bpf. This is safe because bpf 331 * will only read from the mbuf (i.e., it won't 332 * try to free it or keep a pointer to it). 333 */ 334 struct mbuf m; 335 u_int af = dst->sa_family; 336 337 m.m_next = m0; 338 m.m_len = 4; 339 m.m_data = (char *)⁡ 340 341 bpf_mtap(ifp, &m); 342 } 343#endif 344 345 switch(dst->sa_family) { 346#ifdef INET 347 case AF_INET: 348 s = splimp(); 349 if (IF_QFULL(&ifp->if_snd)) { 350 IF_DROP(&ifp->if_snd); 351 m_freem(m0); 352 splx(s); 353 ifp->if_collisions++; 354 return (ENOBUFS); 355 } 356 ifp->if_obytes += m0->m_pkthdr.len; 357 IF_ENQUEUE(&ifp->if_snd, m0); 358 splx(s); 359 ifp->if_opackets++; 360 break; 361#endif 362 default: 363 m_freem(m0); 364 return EAFNOSUPPORT; 365 } 366 367 if (tp->tun_flags & TUN_RWAIT) { 368 tp->tun_flags &= ~TUN_RWAIT; 369 wakeup((caddr_t)tp); 370 } 371 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 372 if (tp->tun_pgrp > 0) 373 gsignal(tp->tun_pgrp, SIGIO); 374 else if ((p = pfind(-tp->tun_pgrp)) != 0) 375 psignal(p, SIGIO); 376 } 377 selwakeup(&tp->tun_rsel); 378 return 0; 379} 380 381/* 382 * the cdevsw interface is now pretty minimal. 383 */ 384static int 385tunioctl(dev, cmd, data, flag, p) 386 dev_t dev; 387 int cmd; 388 caddr_t data; 389 int flag; 390 struct proc *p; 391{ 392 int unit = minor(dev), s; 393 struct tun_softc *tp = &tunctl[unit]; 394 struct tuninfo *tunp; 395 396 switch (cmd) { 397 case TUNSIFINFO: 398 tunp = (struct tuninfo *)data; 399 tp->tun_if.if_mtu = tunp->mtu; 400 tp->tun_if.if_type = tunp->type; 401 tp->tun_if.if_baudrate = tunp->baudrate; 402 break; 403 case TUNGIFINFO: 404 tunp = (struct tuninfo *)data; 405 tunp->mtu = tp->tun_if.if_mtu; 406 tunp->type = tp->tun_if.if_type; 407 tunp->baudrate = tp->tun_if.if_baudrate; 408 break; 409 case TUNSDEBUG: 410 tundebug = *(int *)data; 411 break; 412 case TUNGDEBUG: 413 *(int *)data = tundebug; 414 break; 415 case FIONBIO: 416 if (*(int *)data) 417 tp->tun_flags |= TUN_NBIO; 418 else 419 tp->tun_flags &= ~TUN_NBIO; 420 break; 421 case FIOASYNC: 422 if (*(int *)data) 423 tp->tun_flags |= TUN_ASYNC; 424 else 425 tp->tun_flags &= ~TUN_ASYNC; 426 break; 427 case FIONREAD: 428 s = splimp(); 429 if (tp->tun_if.if_snd.ifq_head) { 430 struct mbuf *mb = tp->tun_if.if_snd.ifq_head; 431 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 432 *(int *)data += mb->m_len; 433 } else 434 *(int *)data = 0; 435 splx(s); 436 break; 437 case TIOCSPGRP: 438 tp->tun_pgrp = *(int *)data; 439 break; 440 case TIOCGPGRP: 441 *(int *)data = tp->tun_pgrp; 442 break; 443 default: 444 return (ENOTTY); 445 } 446 return (0); 447} 448 449/* 450 * The cdevsw read interface - reads a packet at a time, or at 451 * least as much of a packet as can be read. 452 */ 453static int 454tunread(dev_t dev, struct uio *uio, int flag) 455{ 456 int unit = minor(dev); 457 struct tun_softc *tp = &tunctl[unit]; 458 struct ifnet *ifp = &tp->tun_if; 459 struct mbuf *m, *m0; 460 int error=0, len, s; 461 462 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit); 463 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 464 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 465 ifp->if_unit, tp->tun_flags); 466 return EHOSTDOWN; 467 } 468 469 tp->tun_flags &= ~TUN_RWAIT; 470 471 s = splimp(); 472 do { 473 IF_DEQUEUE(&ifp->if_snd, m0); 474 if (m0 == 0) { 475 if (tp->tun_flags & TUN_NBIO) { 476 splx(s); 477 return EWOULDBLOCK; 478 } 479 tp->tun_flags |= TUN_RWAIT; 480 tsleep((caddr_t)tp, PZERO + 1, "tunread", 0); 481 } 482 } while (m0 == 0); 483 splx(s); 484 485 while (m0 && uio->uio_resid > 0 && error == 0) { 486 len = min(uio->uio_resid, m0->m_len); 487 if (len == 0) 488 break; 489 error = uiomove(mtod(m0, caddr_t), len, uio); 490 MFREE(m0, m); 491 m0 = m; 492 } 493 494 if (m0) { 495 TUNDEBUG("Dropping mbuf\n"); 496 m_freem(m0); 497 } 498 return error; 499} 500 501/* 502 * the cdevsw write interface - an atomic write is a packet - or else! 503 */ 504static int 505tunwrite(dev_t dev, struct uio *uio, int flag) 506{ 507 int unit = minor (dev); 508 struct ifnet *ifp = &tunctl[unit].tun_if; 509 struct mbuf *top, **mp, *m; 510 int error=0, s, tlen, mlen; 511 512 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit); 513 514 if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) { 515 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit, 516 uio->uio_resid); 517 return EIO; 518 } 519 tlen = uio->uio_resid; 520 521 /* get a header mbuf */ 522 MGETHDR(m, M_DONTWAIT, MT_DATA); 523 if (m == NULL) 524 return ENOBUFS; 525 mlen = MHLEN; 526 527 top = 0; 528 mp = ⊤ 529 while (error == 0 && uio->uio_resid > 0) { 530 m->m_len = min(mlen, uio->uio_resid); 531 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 532 *mp = m; 533 mp = &m->m_next; 534 if (uio->uio_resid > 0) { 535 MGET (m, M_DONTWAIT, MT_DATA); 536 if (m == 0) { 537 error = ENOBUFS; 538 break; 539 } 540 mlen = MLEN; 541 } 542 } 543 if (error) { 544 if (top) 545 m_freem (top); 546 return error; 547 } 548 549 top->m_pkthdr.len = tlen; 550 top->m_pkthdr.rcvif = ifp; 551 552#if NBPFILTER > 0 553 if (ifp->if_bpf) { 554 /* 555 * We need to prepend the address family as 556 * a four byte field. Cons up a dummy header 557 * to pacify bpf. This is safe because bpf 558 * will only read from the mbuf (i.e., it won't 559 * try to free it or keep a pointer to it). 560 */ 561 struct mbuf m; 562 u_int af = AF_INET; 563 564 m.m_next = top; 565 m.m_len = 4; 566 m.m_data = (char *)⁡ 567 568 bpf_mtap(ifp, &m); 569 } 570#endif 571 572 s = splimp(); 573 if (IF_QFULL (&ipintrq)) { 574 IF_DROP(&ipintrq); 575 splx(s); 576 ifp->if_collisions++; 577 m_freem(top); 578 return ENOBUFS; 579 } 580 IF_ENQUEUE(&ipintrq, top); 581 splx(s); 582 ifp->if_ibytes += tlen; 583 ifp->if_ipackets++; 584 schednetisr(NETISR_IP); 585 return error; 586} 587 588/* 589 * tunselect - the select interface, this is only useful on reads 590 * really. The write detect always returns true, write never blocks 591 * anyway, it either accepts the packet or drops it. 592 */ 593static int 594tunselect(dev_t dev, int rw, struct proc *p) 595{ 596 int unit = minor(dev), s; 597 struct tun_softc *tp = &tunctl[unit]; 598 struct ifnet *ifp = &tp->tun_if; 599 600 s = splimp(); 601 TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit); 602 603 switch (rw) { 604 case FREAD: 605 if (ifp->if_snd.ifq_len > 0) { 606 splx(s); 607 TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name, 608 ifp->if_unit, ifp->if_snd.ifq_len); 609 return 1; 610 } 611 selrecord(p, &tp->tun_rsel); 612 break; 613 case FWRITE: 614 splx(s); 615 return 1; 616 } 617 splx(s); 618 TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit); 619 return 0; 620} 621 622 623#endif /* NTUN */ 624