if_tun.c revision 10080
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#define TUNDEBUG if (tundebug) printf 66int tundebug = 0; 67 68struct tun_softc tunctl[NTUN]; 69 70d_open_t tunopen; 71d_close_t tunclose; 72int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *, 73 struct rtentry *rt)); 74d_rdwr_t tunread; 75d_rdwr_t tunwrite; 76d_ioctl_t tunioctl; 77int tunifioctl __P((struct ifnet *, int, caddr_t)); 78d_select_t tunselect; 79void tunattach __P((void)); 80 81static struct cdevsw tuncdevsw = 82{ tunopen, tunclose, tunread, tunwrite, 83 tunioctl, (d_stop_t *)enodev, (d_reset_t *)nullop, (d_ttycv_t *)enodev, 84 tunselect, (d_mmap_t *)enodev, NULL }; 85extern dev_t tuncdev; 86 87static int tuninit __P((int)); 88 89void 90tunattach(void) 91{ 92 register int i; 93 struct ifnet *ifp; 94 struct sockaddr_in *sin; 95 96 /* 97 * In case we are an LKM, set up device switch. 98 */ 99 cdevsw[major(tuncdev)] = tuncdevsw; 100 101 for (i = 0; i < NTUN; i++) { 102 tunctl[i].tun_flags = TUN_INITED; 103 104 ifp = &tunctl[i].tun_if; 105 ifp->if_unit = i; 106 ifp->if_name = "tun"; 107 ifp->if_mtu = TUNMTU; 108 ifp->if_ioctl = tunifioctl; 109 ifp->if_output = tunoutput; 110 ifp->if_flags = IFF_POINTOPOINT; 111 ifp->if_snd.ifq_maxlen = ifqmaxlen; 112 ifp->if_collisions = 0; 113 ifp->if_ierrors = 0; 114 ifp->if_oerrors = 0; 115 ifp->if_ipackets = 0; 116 ifp->if_opackets = 0; 117 if_attach(ifp); 118#if NBPFILTER > 0 119 bpfattach(&tunctl[i].tun_bpf, ifp, DLT_NULL, sizeof(u_int)); 120#endif 121 } 122} 123 124#ifdef __FreeBSD__ 125PSEUDO_SET(tunattach, if_tun); 126#endif 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 struct tun_softc *tp = &tunctl[ifp->if_unit]; 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 default: 256 error = EINVAL; 257 } 258 splx(s); 259 return (error); 260} 261 262/* 263 * tunoutput - queue packets from higher level ready to put out. 264 */ 265int 266tunoutput(ifp, m0, dst, rt) 267 struct ifnet *ifp; 268 struct mbuf *m0; 269 struct sockaddr *dst; 270 struct rtentry *rt; 271{ 272 struct tun_softc *tp = &tunctl[ifp->if_unit]; 273 struct proc *p; 274 int s; 275 276 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit); 277 278 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 279 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name, 280 ifp->if_unit, tp->tun_flags); 281 m_freem (m0); 282 return EHOSTDOWN; 283 } 284 285#if NBPFILTER > 0 286 if (tp->tun_bpf) { 287 /* 288 * We need to prepend the address family as 289 * a four byte field. Cons up a dummy header 290 * to pacify bpf. This is safe because bpf 291 * will only read from the mbuf (i.e., it won't 292 * try to free it or keep a pointer to it). 293 */ 294 struct mbuf m; 295 u_int af = dst->sa_family; 296 297 m.m_next = m0; 298 m.m_len = 4; 299 m.m_data = (char *)⁡ 300 301 bpf_mtap(tp->tun_bpf, &m); 302 } 303#endif 304 305 switch(dst->sa_family) { 306#ifdef INET 307 case AF_INET: 308 s = splimp(); 309 if (IF_QFULL(&ifp->if_snd)) { 310 IF_DROP(&ifp->if_snd); 311 m_freem(m0); 312 splx(s); 313 ifp->if_collisions++; 314 return (ENOBUFS); 315 } 316 IF_ENQUEUE(&ifp->if_snd, m0); 317 splx(s); 318 ifp->if_opackets++; 319 break; 320#endif 321 default: 322 m_freem(m0); 323 return EAFNOSUPPORT; 324 } 325 326 if (tp->tun_flags & TUN_RWAIT) { 327 tp->tun_flags &= ~TUN_RWAIT; 328 wakeup((caddr_t)tp); 329 } 330 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) { 331 if (tp->tun_pgrp > 0) 332 gsignal(tp->tun_pgrp, SIGIO); 333 else if (p = pfind(-tp->tun_pgrp)) 334 psignal(p, SIGIO); 335 } 336 selwakeup(&tp->tun_rsel); 337 return 0; 338} 339 340/* 341 * the cdevsw interface is now pretty minimal. 342 */ 343int 344tunioctl(dev, cmd, data, flag, p) 345 dev_t dev; 346 int cmd; 347 caddr_t data; 348 int flag; 349 struct proc *p; 350{ 351 int unit = minor(dev), s; 352 struct tun_softc *tp = &tunctl[unit]; 353 struct tuninfo *tunp; 354 355 switch (cmd) { 356 case TUNSIFINFO: 357 tunp = (struct tuninfo *)data; 358 tp->tun_if.if_mtu = tunp->mtu; 359 tp->tun_if.if_type = tunp->type; 360 tp->tun_if.if_baudrate = tunp->baudrate; 361 break; 362 case TUNGIFINFO: 363 tunp = (struct tuninfo *)data; 364 tunp->mtu = tp->tun_if.if_mtu; 365 tunp->type = tp->tun_if.if_type; 366 tunp->baudrate = tp->tun_if.if_baudrate; 367 break; 368 case TUNSDEBUG: 369 tundebug = *(int *)data; 370 break; 371 case TUNGDEBUG: 372 *(int *)data = tundebug; 373 break; 374 case FIONBIO: 375 if (*(int *)data) 376 tp->tun_flags |= TUN_NBIO; 377 else 378 tp->tun_flags &= ~TUN_NBIO; 379 break; 380 case FIOASYNC: 381 if (*(int *)data) 382 tp->tun_flags |= TUN_ASYNC; 383 else 384 tp->tun_flags &= ~TUN_ASYNC; 385 break; 386 case FIONREAD: 387 s = splimp(); 388 if (tp->tun_if.if_snd.ifq_head) 389 *(int *)data = tp->tun_if.if_snd.ifq_head->m_len; 390 else 391 *(int *)data = 0; 392 splx(s); 393 break; 394 case TIOCSPGRP: 395 tp->tun_pgrp = *(int *)data; 396 break; 397 case TIOCGPGRP: 398 *(int *)data = tp->tun_pgrp; 399 break; 400 default: 401 return (ENOTTY); 402 } 403 return (0); 404} 405 406/* 407 * The cdevsw read interface - reads a packet at a time, or at 408 * least as much of a packet as can be read. 409 */ 410int 411tunread(dev_t dev, struct uio *uio, int flag) 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_t dev, struct uio *uio, int flag) 463{ 464 int unit = minor (dev); 465 struct ifnet *ifp = &tunctl[unit].tun_if; 466 struct mbuf *top, **mp, *m; 467 int error=0, s, tlen, mlen; 468 469 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit); 470 471 if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) { 472 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit, 473 uio->uio_resid); 474 return EIO; 475 } 476 tlen = uio->uio_resid; 477 478 /* get a header mbuf */ 479 MGETHDR(m, M_DONTWAIT, MT_DATA); 480 if (m == NULL) 481 return ENOBUFS; 482 mlen = MHLEN; 483 484 top = 0; 485 mp = ⊤ 486 while (error == 0 && uio->uio_resid > 0) { 487 m->m_len = min(mlen, uio->uio_resid); 488 error = uiomove(mtod (m, caddr_t), m->m_len, uio); 489 *mp = m; 490 mp = &m->m_next; 491 if (uio->uio_resid > 0) { 492 MGET (m, M_DONTWAIT, MT_DATA); 493 if (m == 0) { 494 error = ENOBUFS; 495 break; 496 } 497 mlen = MLEN; 498 } 499 } 500 if (error) { 501 if (top) 502 m_freem (top); 503 return error; 504 } 505 506 top->m_pkthdr.len = tlen; 507 top->m_pkthdr.rcvif = ifp; 508 509#if NBPFILTER > 0 510 if (tunctl[unit].tun_bpf) { 511 /* 512 * We need to prepend the address family as 513 * a four byte field. Cons up a dummy header 514 * to pacify bpf. This is safe because bpf 515 * will only read from the mbuf (i.e., it won't 516 * try to free it or keep a pointer to it). 517 */ 518 struct mbuf m; 519 u_int af = AF_INET; 520 521 m.m_next = top; 522 m.m_len = 4; 523 m.m_data = (char *)⁡ 524 525 bpf_mtap(tunctl[unit].tun_bpf, &m); 526 } 527#endif 528 529 s = splimp(); 530 if (IF_QFULL (&ipintrq)) { 531 IF_DROP(&ipintrq); 532 splx(s); 533 ifp->if_collisions++; 534 m_freem(top); 535 return ENOBUFS; 536 } 537 IF_ENQUEUE(&ipintrq, top); 538 splx(s); 539 ifp->if_ipackets++; 540 schednetisr(NETISR_IP); 541 return error; 542} 543 544/* 545 * tunselect - the select interface, this is only useful on reads 546 * really. The write detect always returns true, write never blocks 547 * anyway, it either accepts the packet or drops it. 548 */ 549int 550tunselect(dev_t dev, int rw, struct proc *p) 551{ 552 int unit = minor(dev), s; 553 struct tun_softc *tp = &tunctl[unit]; 554 struct ifnet *ifp = &tp->tun_if; 555 556 s = splimp(); 557 TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit); 558 559 switch (rw) { 560 case FREAD: 561 if (ifp->if_snd.ifq_len > 0) { 562 splx(s); 563 TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name, 564 ifp->if_unit, ifp->if_snd.ifq_len); 565 return 1; 566 } 567 selrecord(p, &tp->tun_rsel); 568 break; 569 case FWRITE: 570 splx(s); 571 return 1; 572 } 573 splx(s); 574 TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit); 575 return 0; 576} 577 578#endif /* NTUN */ 579