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