if_tun.c revision 16332
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 it's 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/select mode of 14 * operation though. 15 */ 16 17#include "tun.h" 18#if NTUN > 0 19 20#include <sys/param.h> 21#include <sys/proc.h> 22#include <sys/systm.h> 23#include <sys/mbuf.h> 24#include <sys/buf.h> 25#include <sys/protosw.h> 26#include <sys/socket.h> 27#include <sys/ioctl.h> 28#include <sys/errno.h> 29#include <sys/syslog.h> 30#include <sys/select.h> 31#include <sys/file.h> 32#include <sys/signalvar.h> 33#include <sys/kernel.h> 34#include <sys/sysctl.h> 35#ifdef DEVFS 36#include <sys/devfsext.h> 37#endif /*DEVFS*/ 38#include <sys/conf.h> 39 40#include <net/if.h> 41#include <net/netisr.h> 42#include <net/route.h> 43 44#ifdef INET 45#include <netinet/in.h> 46#include <netinet/in_systm.h> 47#include <netinet/in_var.h> 48#include <netinet/ip.h> 49#include <netinet/if_ether.h> 50#endif 51 52#ifdef NS 53#include <netns/ns.h> 54#include <netns/ns_if.h> 55#endif 56 57#include "bpfilter.h" 58#if NBPFILTER > 0 59#include <sys/time.h> 60#include <net/bpf.h> 61#endif 62 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 *, int, 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_select_t tunselect; 85 86#define CDEV_MAJOR 52 87static struct cdevsw tun_cdevsw = { 88 tunopen, tunclose, tunread, tunwrite, 89 tunioctl, nullstop, noreset, nodevtotty, 90 tunselect, nommap, nostrategy, "tun", NULL, -1 91}; 92 93 94static tun_devsw_installed = 0; 95#ifdef DEVFS 96static void *tun_devfs_token[NTUN]; 97#endif 98 99static void 100tunattach(dummy) 101 void *dummy; 102{ 103 register int i; 104 struct ifnet *ifp; 105 dev_t dev; 106 107 if ( tun_devsw_installed ) 108 return; 109 dev = makedev(CDEV_MAJOR, 0); 110 cdevsw_add(&dev, &tun_cdevsw, NULL); 111 tun_devsw_installed = 1; 112 for ( i = 0; i < NTUN; i++ ) { 113#ifdef DEVFS 114 tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, i, DV_CHR, 115 UID_UUCP, GID_DIALER, 116 0600, "tun%d", i); 117#endif 118 tunctl[i].tun_flags = TUN_INITED; 119 120 ifp = &tunctl[i].tun_if; 121 ifp->if_unit = i; 122 ifp->if_name = "tun"; 123 ifp->if_mtu = TUNMTU; 124 ifp->if_ioctl = tunifioctl; 125 ifp->if_output = tunoutput; 126 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 127 ifp->if_snd.ifq_maxlen = ifqmaxlen; 128 ifp->if_collisions = 0; 129 ifp->if_ierrors = 0; 130 ifp->if_oerrors = 0; 131 ifp->if_ipackets = 0; 132 ifp->if_opackets = 0; 133 if_attach(ifp); 134#if NBPFILTER > 0 135 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 136#endif 137 } 138} 139 140/* 141 * tunnel open - must be superuser & the device must be 142 * configured in 143 */ 144static int 145tunopen(dev, flag, mode, p) 146 dev_t dev; 147 int flag, mode; 148 struct proc *p; 149{ 150 struct ifnet *ifp; 151 struct tun_softc *tp; 152 register int unit, error; 153 154 error = suser(p->p_ucred, &p->p_acflag); 155 if (error) 156 return (error); 157 158 if ((unit = minor(dev)) >= NTUN) 159 return (ENXIO); 160 tp = &tunctl[unit]; 161 if (tp->tun_flags & TUN_OPEN) 162 return EBUSY; 163 ifp = &tp->tun_if; 164 tp->tun_flags |= TUN_OPEN; 165 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit); 166 return (0); 167} 168 169/* 170 * tunclose - close the device - mark i/f down & delete 171 * routing info 172 */ 173static int 174tunclose(dev_t dev, int foo, int bar, struct proc *p) 175{ 176 register int unit = minor(dev), s; 177 struct tun_softc *tp = &tunctl[unit]; 178 struct ifnet *ifp = &tp->tun_if; 179 struct mbuf *m; 180 181 tp->tun_flags &= ~TUN_OPEN; 182 183 /* 184 * junk all pending output 185 */ 186 do { 187 s = splimp(); 188 IF_DEQUEUE(&ifp->if_snd, m); 189 splx(s); 190 if (m) 191 m_freem(m); 192 } while (m); 193 194 if (ifp->if_flags & IFF_UP) { 195 s = splimp(); 196 if_down(ifp); 197 microtime(&ifp->if_lastchange); 198 if (ifp->if_flags & IFF_RUNNING) { 199 /* find internet addresses and delete routes */ 200 register struct ifaddr *ifa; 201 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { 202 if (ifa->ifa_addr->sa_family == AF_INET) { 203 rtinit(ifa, (int)RTM_DELETE, 204 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 205 } 206 } 207 } 208 splx(s); 209 } 210 tp->tun_pgrp = 0; 211 selwakeup(&tp->tun_rsel); 212 213 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit); 214 return (0); 215} 216 217static int 218tuninit(unit) 219 int unit; 220{ 221 struct tun_softc *tp = &tunctl[unit]; 222 struct ifnet *ifp = &tp->tun_if; 223 register struct ifaddr *ifa; 224 225 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit); 226 227 ifp->if_flags |= IFF_UP | IFF_RUNNING; 228 microtime(&ifp->if_lastchange); 229 230 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 231 if (ifa->ifa_addr->sa_family == AF_INET) { 232 struct sockaddr_in *si; 233 234 si = (struct sockaddr_in *)ifa->ifa_addr; 235 if (si && si->sin_addr.s_addr) 236 tp->tun_flags |= TUN_IASET; 237 238 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 239 if (si && si->sin_addr.s_addr) 240 tp->tun_flags |= TUN_DSTADDR; 241 } 242 243 return 0; 244} 245 246/* 247 * Process an ioctl request. 248 */ 249int 250tunifioctl(ifp, cmd, data) 251 struct ifnet *ifp; 252 int cmd; 253 caddr_t data; 254{ 255 register struct ifreq *ifr = (struct ifreq *)data; 256 int error = 0, s; 257 258 s = splimp(); 259 switch(cmd) { 260 case SIOCSIFADDR: 261 tuninit(ifp->if_unit); 262 TUNDEBUG("%s%d: address set\n", 263 ifp->if_name, ifp->if_unit); 264 break; 265 case SIOCSIFDSTADDR: 266 tuninit(ifp->if_unit); 267 TUNDEBUG("%s%d: destination address set\n", 268 ifp->if_name, ifp->if_unit); 269 break; 270 case SIOCADDMULTI: 271 case SIOCDELMULTI: 272 if (ifr == 0) { 273 error = EAFNOSUPPORT; /* XXX */ 274 break; 275 } 276 switch (ifr->ifr_addr.sa_family) { 277 278#ifdef INET 279 case AF_INET: 280 break; 281#endif 282 283 default: 284 error = EAFNOSUPPORT; 285 break; 286 } 287 break; 288 289 290 default: 291 error = EINVAL; 292 } 293 splx(s); 294 return (error); 295} 296 297/* 298 * tunoutput - queue packets from higher level ready to put out. 299 */ 300int 301tunoutput(ifp, m0, dst, rt) 302 struct ifnet *ifp; 303 struct mbuf *m0; 304 struct sockaddr *dst; 305 struct rtentry *rt; 306{ 307 struct tun_softc *tp = &tunctl[ifp->if_unit]; 308 struct proc *p; 309 int s; 310 311 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit); 312 313 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 314 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 315 ifp->if_unit, tp->tun_flags); 316 m_freem (m0); 317 return EHOSTDOWN; 318 } 319 320#if NBPFILTER > 0 321 /* BPF write needs to be handled specially */ 322 if (dst->sa_family == AF_UNSPEC) { 323 dst->sa_family = *(mtod(m0, int *)); 324 m0->m_len -= sizeof(int); 325 m0->m_pkthdr.len -= sizeof(int); 326 m0->m_data += sizeof(int); 327 } 328 329 if (ifp->if_bpf) { 330 /* 331 * We need to prepend the address family as 332 * a four byte field. Cons up a dummy header 333 * to pacify bpf. This is safe because bpf 334 * will only read from the mbuf (i.e., it won't 335 * try to free it or keep a pointer to it). 336 */ 337 struct mbuf m; 338 u_int af = dst->sa_family; 339 340 m.m_next = m0; 341 m.m_len = 4; 342 m.m_data = (char *)⁡ 343 344 bpf_mtap(ifp, &m); 345 } 346#endif 347 348 switch(dst->sa_family) { 349#ifdef INET 350 case AF_INET: 351 s = splimp(); 352 if (IF_QFULL(&ifp->if_snd)) { 353 IF_DROP(&ifp->if_snd); 354 m_freem(m0); 355 splx(s); 356 ifp->if_collisions++; 357 return (ENOBUFS); 358 } 359 ifp->if_obytes += m0->m_pkthdr.len; 360 IF_ENQUEUE(&ifp->if_snd, m0); 361 splx(s); 362 ifp->if_opackets++; 363 break; 364#endif 365 default: 366 m_freem(m0); 367 return EAFNOSUPPORT; 368 } 369 370 if (tp->tun_flags & TUN_RWAIT) { 371 tp->tun_flags &= ~TUN_RWAIT; 372 wakeup((caddr_t)tp); 373 } 374 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 375 if (tp->tun_pgrp > 0) 376 gsignal(tp->tun_pgrp, SIGIO); 377 else if ((p = pfind(-tp->tun_pgrp)) != 0) 378 psignal(p, SIGIO); 379 } 380 selwakeup(&tp->tun_rsel); 381 return 0; 382} 383 384/* 385 * the cdevsw interface is now pretty minimal. 386 */ 387static int 388tunioctl(dev, cmd, data, flag, p) 389 dev_t dev; 390 int cmd; 391 caddr_t data; 392 int flag; 393 struct proc *p; 394{ 395 int unit = minor(dev), s; 396 struct tun_softc *tp = &tunctl[unit]; 397 struct tuninfo *tunp; 398 399 switch (cmd) { 400 case TUNSIFINFO: 401 tunp = (struct tuninfo *)data; 402 tp->tun_if.if_mtu = tunp->mtu; 403 tp->tun_if.if_type = tunp->type; 404 tp->tun_if.if_baudrate = tunp->baudrate; 405 break; 406 case TUNGIFINFO: 407 tunp = (struct tuninfo *)data; 408 tunp->mtu = tp->tun_if.if_mtu; 409 tunp->type = tp->tun_if.if_type; 410 tunp->baudrate = tp->tun_if.if_baudrate; 411 break; 412 case TUNSDEBUG: 413 tundebug = *(int *)data; 414 break; 415 case TUNGDEBUG: 416 *(int *)data = tundebug; 417 break; 418 case FIONBIO: 419 if (*(int *)data) 420 tp->tun_flags |= TUN_NBIO; 421 else 422 tp->tun_flags &= ~TUN_NBIO; 423 break; 424 case FIOASYNC: 425 if (*(int *)data) 426 tp->tun_flags |= TUN_ASYNC; 427 else 428 tp->tun_flags &= ~TUN_ASYNC; 429 break; 430 case FIONREAD: 431 s = splimp(); 432 if (tp->tun_if.if_snd.ifq_head) { 433 struct mbuf *mb = tp->tun_if.if_snd.ifq_head; 434 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 435 *(int *)data += mb->m_len; 436 } else 437 *(int *)data = 0; 438 splx(s); 439 break; 440 case TIOCSPGRP: 441 tp->tun_pgrp = *(int *)data; 442 break; 443 case TIOCGPGRP: 444 *(int *)data = tp->tun_pgrp; 445 break; 446 default: 447 return (ENOTTY); 448 } 449 return (0); 450} 451 452/* 453 * The cdevsw read interface - reads a packet at a time, or at 454 * least as much of a packet as can be read. 455 */ 456static int 457tunread(dev_t dev, struct uio *uio, int flag) 458{ 459 int unit = minor(dev); 460 struct tun_softc *tp = &tunctl[unit]; 461 struct ifnet *ifp = &tp->tun_if; 462 struct mbuf *m, *m0; 463 int error=0, len, s; 464 465 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit); 466 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 467 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 468 ifp->if_unit, tp->tun_flags); 469 return EHOSTDOWN; 470 } 471 472 tp->tun_flags &= ~TUN_RWAIT; 473 474 s = splimp(); 475 do { 476 IF_DEQUEUE(&ifp->if_snd, m0); 477 if (m0 == 0) { 478 if (tp->tun_flags & TUN_NBIO) { 479 splx(s); 480 return EWOULDBLOCK; 481 } 482 tp->tun_flags |= TUN_RWAIT; 483 tsleep((caddr_t)tp, PZERO + 1, "tunread", 0); 484 } 485 } while (m0 == 0); 486 splx(s); 487 488 while (m0 && uio->uio_resid > 0 && error == 0) { 489 len = min(uio->uio_resid, m0->m_len); 490 if (len == 0) 491 break; 492 error = uiomove(mtod(m0, caddr_t), len, uio); 493 MFREE(m0, m); 494 m0 = m; 495 } 496 497 if (m0) { 498 TUNDEBUG("Dropping mbuf\n"); 499 m_freem(m0); 500 } 501 return error; 502} 503 504/* 505 * the cdevsw write interface - an atomic write is a packet - or else! 506 */ 507static int 508tunwrite(dev_t dev, struct uio *uio, int flag) 509{ 510 int unit = minor (dev); 511 struct ifnet *ifp = &tunctl[unit].tun_if; 512 struct mbuf *top, **mp, *m; 513 int error=0, s, tlen, mlen; 514 515 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit); 516 517 if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) { 518 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit, 519 uio->uio_resid); 520 return EIO; 521 } 522 tlen = uio->uio_resid; 523 524 /* get a header mbuf */ 525 MGETHDR(m, M_DONTWAIT, MT_DATA); 526 if (m == NULL) 527 return ENOBUFS; 528 mlen = MHLEN; 529 530 top = 0; 531 mp = ⊤ 532 while (error == 0 && uio->uio_resid > 0) { 533 m->m_len = min(mlen, uio->uio_resid); 534 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 535 *mp = m; 536 mp = &m->m_next; 537 if (uio->uio_resid > 0) { 538 MGET (m, M_DONTWAIT, MT_DATA); 539 if (m == 0) { 540 error = ENOBUFS; 541 break; 542 } 543 mlen = MLEN; 544 } 545 } 546 if (error) { 547 if (top) 548 m_freem (top); 549 return error; 550 } 551 552 top->m_pkthdr.len = tlen; 553 top->m_pkthdr.rcvif = ifp; 554 555#if NBPFILTER > 0 556 if (ifp->if_bpf) { 557 /* 558 * We need to prepend the address family as 559 * a four byte field. Cons up a dummy header 560 * to pacify bpf. This is safe because bpf 561 * will only read from the mbuf (i.e., it won't 562 * try to free it or keep a pointer to it). 563 */ 564 struct mbuf m; 565 u_int af = AF_INET; 566 567 m.m_next = top; 568 m.m_len = 4; 569 m.m_data = (char *)⁡ 570 571 bpf_mtap(ifp, &m); 572 } 573#endif 574 575 s = splimp(); 576 if (IF_QFULL (&ipintrq)) { 577 IF_DROP(&ipintrq); 578 splx(s); 579 ifp->if_collisions++; 580 m_freem(top); 581 return ENOBUFS; 582 } 583 IF_ENQUEUE(&ipintrq, top); 584 splx(s); 585 ifp->if_ibytes += tlen; 586 ifp->if_ipackets++; 587 schednetisr(NETISR_IP); 588 return error; 589} 590 591/* 592 * tunselect - the select interface, this is only useful on reads 593 * really. The write detect always returns true, write never blocks 594 * anyway, it either accepts the packet or drops it. 595 */ 596static int 597tunselect(dev_t dev, int rw, struct proc *p) 598{ 599 int unit = minor(dev), s; 600 struct tun_softc *tp = &tunctl[unit]; 601 struct ifnet *ifp = &tp->tun_if; 602 603 s = splimp(); 604 TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit); 605 606 switch (rw) { 607 case FREAD: 608 if (ifp->if_snd.ifq_len > 0) { 609 splx(s); 610 TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name, 611 ifp->if_unit, ifp->if_snd.ifq_len); 612 return 1; 613 } 614 selrecord(p, &tp->tun_rsel); 615 break; 616 case FWRITE: 617 splx(s); 618 return 1; 619 } 620 splx(s); 621 TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit); 622 return 0; 623} 624 625 626#endif /* NTUN */ 627