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