if_tun.c revision 16604
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 if (ifp->if_flags & IFF_RUNNING) { 198 /* find internet addresses and delete routes */ 199 register struct ifaddr *ifa; 200 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { 201 if (ifa->ifa_addr->sa_family == AF_INET) { 202 rtinit(ifa, (int)RTM_DELETE, 203 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 204 } 205 } 206 } 207 splx(s); 208 } 209 tp->tun_pgrp = 0; 210 selwakeup(&tp->tun_rsel); 211 212 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit); 213 return (0); 214} 215 216static int 217tuninit(unit) 218 int unit; 219{ 220 struct tun_softc *tp = &tunctl[unit]; 221 struct ifnet *ifp = &tp->tun_if; 222 register struct ifaddr *ifa; 223 224 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit); 225 226 ifp->if_flags |= IFF_UP | IFF_RUNNING; 227 microtime(&ifp->if_lastchange); 228 229 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 230 if (ifa->ifa_addr->sa_family == AF_INET) { 231 struct sockaddr_in *si; 232 233 si = (struct sockaddr_in *)ifa->ifa_addr; 234 if (si && si->sin_addr.s_addr) 235 tp->tun_flags |= TUN_IASET; 236 237 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 238 if (si && si->sin_addr.s_addr) 239 tp->tun_flags |= TUN_DSTADDR; 240 } 241 242 return 0; 243} 244 245/* 246 * Process an ioctl request. 247 */ 248int 249tunifioctl(ifp, cmd, data) 250 struct ifnet *ifp; 251 int cmd; 252 caddr_t data; 253{ 254 register struct ifreq *ifr = (struct ifreq *)data; 255 int error = 0, s; 256 257 s = splimp(); 258 switch(cmd) { 259 case SIOCSIFADDR: 260 tuninit(ifp->if_unit); 261 TUNDEBUG("%s%d: address set\n", 262 ifp->if_name, ifp->if_unit); 263 break; 264 case SIOCSIFDSTADDR: 265 tuninit(ifp->if_unit); 266 TUNDEBUG("%s%d: destination address set\n", 267 ifp->if_name, ifp->if_unit); 268 break; 269 case SIOCADDMULTI: 270 case SIOCDELMULTI: 271 if (ifr == 0) { 272 error = EAFNOSUPPORT; /* XXX */ 273 break; 274 } 275 switch (ifr->ifr_addr.sa_family) { 276 277#ifdef INET 278 case AF_INET: 279 break; 280#endif 281 282 default: 283 error = EAFNOSUPPORT; 284 break; 285 } 286 break; 287 288 289 default: 290 error = EINVAL; 291 } 292 splx(s); 293 return (error); 294} 295 296/* 297 * tunoutput - queue packets from higher level ready to put out. 298 */ 299int 300tunoutput(ifp, m0, dst, rt) 301 struct ifnet *ifp; 302 struct mbuf *m0; 303 struct sockaddr *dst; 304 struct rtentry *rt; 305{ 306 struct tun_softc *tp = &tunctl[ifp->if_unit]; 307 struct proc *p; 308 int s; 309 310 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit); 311 312 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 313 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 314 ifp->if_unit, tp->tun_flags); 315 m_freem (m0); 316 return EHOSTDOWN; 317 } 318 319#if NBPFILTER > 0 320 /* BPF write needs to be handled specially */ 321 if (dst->sa_family == AF_UNSPEC) { 322 dst->sa_family = *(mtod(m0, int *)); 323 m0->m_len -= sizeof(int); 324 m0->m_pkthdr.len -= sizeof(int); 325 m0->m_data += sizeof(int); 326 } 327 328 if (ifp->if_bpf) { 329 /* 330 * We need to prepend the address family as 331 * a four byte field. Cons up a dummy header 332 * to pacify bpf. This is safe because bpf 333 * will only read from the mbuf (i.e., it won't 334 * try to free it or keep a pointer to it). 335 */ 336 struct mbuf m; 337 u_int af = dst->sa_family; 338 339 m.m_next = m0; 340 m.m_len = 4; 341 m.m_data = (char *)⁡ 342 343 bpf_mtap(ifp, &m); 344 } 345#endif 346 347 switch(dst->sa_family) { 348#ifdef INET 349 case AF_INET: 350 s = splimp(); 351 if (IF_QFULL(&ifp->if_snd)) { 352 IF_DROP(&ifp->if_snd); 353 m_freem(m0); 354 splx(s); 355 ifp->if_collisions++; 356 return (ENOBUFS); 357 } 358 ifp->if_obytes += m0->m_pkthdr.len; 359 IF_ENQUEUE(&ifp->if_snd, m0); 360 splx(s); 361 ifp->if_opackets++; 362 break; 363#endif 364 default: 365 m_freem(m0); 366 return EAFNOSUPPORT; 367 } 368 369 if (tp->tun_flags & TUN_RWAIT) { 370 tp->tun_flags &= ~TUN_RWAIT; 371 wakeup((caddr_t)tp); 372 } 373 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 374 if (tp->tun_pgrp > 0) 375 gsignal(tp->tun_pgrp, SIGIO); 376 else if ((p = pfind(-tp->tun_pgrp)) != 0) 377 psignal(p, SIGIO); 378 } 379 selwakeup(&tp->tun_rsel); 380 return 0; 381} 382 383/* 384 * the cdevsw interface is now pretty minimal. 385 */ 386static int 387tunioctl(dev, cmd, data, flag, p) 388 dev_t dev; 389 int cmd; 390 caddr_t data; 391 int flag; 392 struct proc *p; 393{ 394 int unit = minor(dev), s; 395 struct tun_softc *tp = &tunctl[unit]; 396 struct tuninfo *tunp; 397 398 switch (cmd) { 399 case TUNSIFINFO: 400 tunp = (struct tuninfo *)data; 401 tp->tun_if.if_mtu = tunp->mtu; 402 tp->tun_if.if_type = tunp->type; 403 tp->tun_if.if_baudrate = tunp->baudrate; 404 break; 405 case TUNGIFINFO: 406 tunp = (struct tuninfo *)data; 407 tunp->mtu = tp->tun_if.if_mtu; 408 tunp->type = tp->tun_if.if_type; 409 tunp->baudrate = tp->tun_if.if_baudrate; 410 break; 411 case TUNSDEBUG: 412 tundebug = *(int *)data; 413 break; 414 case TUNGDEBUG: 415 *(int *)data = tundebug; 416 break; 417 case FIONBIO: 418 if (*(int *)data) 419 tp->tun_flags |= TUN_NBIO; 420 else 421 tp->tun_flags &= ~TUN_NBIO; 422 break; 423 case FIOASYNC: 424 if (*(int *)data) 425 tp->tun_flags |= TUN_ASYNC; 426 else 427 tp->tun_flags &= ~TUN_ASYNC; 428 break; 429 case FIONREAD: 430 s = splimp(); 431 if (tp->tun_if.if_snd.ifq_head) { 432 struct mbuf *mb = tp->tun_if.if_snd.ifq_head; 433 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 434 *(int *)data += mb->m_len; 435 } else 436 *(int *)data = 0; 437 splx(s); 438 break; 439 case TIOCSPGRP: 440 tp->tun_pgrp = *(int *)data; 441 break; 442 case TIOCGPGRP: 443 *(int *)data = tp->tun_pgrp; 444 break; 445 default: 446 return (ENOTTY); 447 } 448 return (0); 449} 450 451/* 452 * The cdevsw read interface - reads a packet at a time, or at 453 * least as much of a packet as can be read. 454 */ 455static int 456tunread(dev_t dev, struct uio *uio, int flag) 457{ 458 int unit = minor(dev); 459 struct tun_softc *tp = &tunctl[unit]; 460 struct ifnet *ifp = &tp->tun_if; 461 struct mbuf *m, *m0; 462 int error=0, len, s; 463 464 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit); 465 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 466 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 467 ifp->if_unit, tp->tun_flags); 468 return EHOSTDOWN; 469 } 470 471 tp->tun_flags &= ~TUN_RWAIT; 472 473 s = splimp(); 474 do { 475 IF_DEQUEUE(&ifp->if_snd, m0); 476 if (m0 == 0) { 477 if (tp->tun_flags & TUN_NBIO) { 478 splx(s); 479 return EWOULDBLOCK; 480 } 481 tp->tun_flags |= TUN_RWAIT; 482 tsleep((caddr_t)tp, PZERO + 1, "tunread", 0); 483 } 484 } while (m0 == 0); 485 splx(s); 486 487 while (m0 && uio->uio_resid > 0 && error == 0) { 488 len = min(uio->uio_resid, m0->m_len); 489 if (len == 0) 490 break; 491 error = uiomove(mtod(m0, caddr_t), len, uio); 492 MFREE(m0, m); 493 m0 = m; 494 } 495 496 if (m0) { 497 TUNDEBUG("Dropping mbuf\n"); 498 m_freem(m0); 499 } 500 return error; 501} 502 503/* 504 * the cdevsw write interface - an atomic write is a packet - or else! 505 */ 506static int 507tunwrite(dev_t dev, struct uio *uio, int flag) 508{ 509 int unit = minor (dev); 510 struct ifnet *ifp = &tunctl[unit].tun_if; 511 struct mbuf *top, **mp, *m; 512 int error=0, s, tlen, mlen; 513 514 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit); 515 516 if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) { 517 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit, 518 uio->uio_resid); 519 return EIO; 520 } 521 tlen = uio->uio_resid; 522 523 /* get a header mbuf */ 524 MGETHDR(m, M_DONTWAIT, MT_DATA); 525 if (m == NULL) 526 return ENOBUFS; 527 mlen = MHLEN; 528 529 top = 0; 530 mp = ⊤ 531 while (error == 0 && uio->uio_resid > 0) { 532 m->m_len = min(mlen, uio->uio_resid); 533 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 534 *mp = m; 535 mp = &m->m_next; 536 if (uio->uio_resid > 0) { 537 MGET (m, M_DONTWAIT, MT_DATA); 538 if (m == 0) { 539 error = ENOBUFS; 540 break; 541 } 542 mlen = MLEN; 543 } 544 } 545 if (error) { 546 if (top) 547 m_freem (top); 548 return error; 549 } 550 551 top->m_pkthdr.len = tlen; 552 top->m_pkthdr.rcvif = ifp; 553 554#if NBPFILTER > 0 555 if (ifp->if_bpf) { 556 /* 557 * We need to prepend the address family as 558 * a four byte field. Cons up a dummy header 559 * to pacify bpf. This is safe because bpf 560 * will only read from the mbuf (i.e., it won't 561 * try to free it or keep a pointer to it). 562 */ 563 struct mbuf m; 564 u_int af = AF_INET; 565 566 m.m_next = top; 567 m.m_len = 4; 568 m.m_data = (char *)⁡ 569 570 bpf_mtap(ifp, &m); 571 } 572#endif 573 574 s = splimp(); 575 if (IF_QFULL (&ipintrq)) { 576 IF_DROP(&ipintrq); 577 splx(s); 578 ifp->if_collisions++; 579 m_freem(top); 580 return ENOBUFS; 581 } 582 IF_ENQUEUE(&ipintrq, top); 583 splx(s); 584 ifp->if_ibytes += tlen; 585 ifp->if_ipackets++; 586 schednetisr(NETISR_IP); 587 return error; 588} 589 590/* 591 * tunselect - the select interface, this is only useful on reads 592 * really. The write detect always returns true, write never blocks 593 * anyway, it either accepts the packet or drops it. 594 */ 595static int 596tunselect(dev_t dev, int rw, struct proc *p) 597{ 598 int unit = minor(dev), s; 599 struct tun_softc *tp = &tunctl[unit]; 600 struct ifnet *ifp = &tp->tun_if; 601 602 s = splimp(); 603 TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit); 604 605 switch (rw) { 606 case FREAD: 607 if (ifp->if_snd.ifq_len > 0) { 608 splx(s); 609 TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name, 610 ifp->if_unit, ifp->if_snd.ifq_len); 611 return 1; 612 } 613 selrecord(p, &tp->tun_rsel); 614 break; 615 case FWRITE: 616 splx(s); 617 return 1; 618 } 619 splx(s); 620 TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit); 621 return 0; 622} 623 624 625#endif /* NTUN */ 626