17 */ 18 19#include "opt_atalk.h" 20#include "opt_inet.h" 21#include "opt_inet6.h" 22#include "opt_ipx.h" 23#include "opt_mac.h" 24 25#include <sys/param.h> 26#include <sys/proc.h> 27#include <sys/systm.h> 28#include <sys/mac.h> 29#include <sys/mbuf.h> 30#include <sys/module.h> 31#include <sys/socket.h> 32#include <sys/fcntl.h> 33#include <sys/filio.h> 34#include <sys/sockio.h> 35#include <sys/ttycom.h> 36#include <sys/poll.h> 37#include <sys/selinfo.h> 38#include <sys/signalvar.h> 39#include <sys/filedesc.h> 40#include <sys/kernel.h> 41#include <sys/sysctl.h> 42#include <sys/conf.h> 43#include <sys/uio.h> 44#include <sys/malloc.h> 45#include <sys/random.h> 46 47#include <net/if.h> 48#include <net/if_types.h> 49#include <net/netisr.h> 50#include <net/route.h> 51#ifdef INET 52#include <netinet/in.h> 53#endif 54#include <net/bpf.h> 55#include <net/if_tun.h> 56 57#include <sys/queue.h> 58 59/* 60 * tun_list is protected by global tunmtx. Other mutable fields are 61 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is 62 * static for the duration of a tunnel interface. 63 */ 64struct tun_softc { 65 TAILQ_ENTRY(tun_softc) tun_list; 66 struct cdev *tun_dev; 67 u_short tun_flags; /* misc flags */ 68#define TUN_OPEN 0x0001 69#define TUN_INITED 0x0002 70#define TUN_RCOLL 0x0004 71#define TUN_IASET 0x0008 72#define TUN_DSTADDR 0x0010 73#define TUN_LMODE 0x0020 74#define TUN_RWAIT 0x0040 75#define TUN_ASYNC 0x0080 76#define TUN_IFHEAD 0x0100 77 78#define TUN_READY (TUN_OPEN | TUN_INITED) 79 80 /* 81 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this 82 * actually needed? Can we just return EBUSY if already open? 83 * Problem is that this involved inherent races when a tun device 84 * is handed off from one process to another, as opposed to just 85 * being slightly stale informationally. 86 */ 87 pid_t tun_pid; /* owning pid */ 88 struct ifnet tun_if; /* the interface */ 89 struct sigio *tun_sigio; /* information for async I/O */ 90 struct selinfo tun_rsel; /* read select */ 91 struct mtx tun_mtx; /* protect mutable softc fields */ 92}; 93 94#define TUNDEBUG if (tundebug) if_printf 95#define TUNNAME "tun" 96 97/* 98 * All mutable global variables in if_tun are locked using tunmtx, with 99 * the exception of tundebug, which is used unlocked, and tunclones, 100 * which is static after setup. 101 */ 102static struct mtx tunmtx; 103static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface"); 104static int tundebug = 0; 105static struct clonedevs *tunclones; 106static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead); 107SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); 108 109static void tunclone(void *arg, char *name, int namelen, struct cdev **dev); 110static void tuncreate(struct cdev *dev); 111static int tunifioctl(struct ifnet *, u_long, caddr_t); 112static int tuninit(struct ifnet *); 113static int tunmodevent(module_t, int, void *); 114static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *, 115 struct rtentry *rt); 116static void tunstart(struct ifnet *); 117 118static d_open_t tunopen; 119static d_close_t tunclose; 120static d_read_t tunread; 121static d_write_t tunwrite; 122static d_ioctl_t tunioctl; 123static d_poll_t tunpoll; 124 125static struct cdevsw tun_cdevsw = { 126 .d_version = D_VERSION, 127 .d_flags = D_PSEUDO | D_NEEDGIANT, 128 .d_open = tunopen, 129 .d_close = tunclose, 130 .d_read = tunread, 131 .d_write = tunwrite, 132 .d_ioctl = tunioctl, 133 .d_poll = tunpoll, 134 .d_name = TUNNAME, 135}; 136 137static void 138tunclone(void *arg, char *name, int namelen, struct cdev **dev) 139{ 140 int u, i; 141 142 if (*dev != NULL) 143 return; 144 145 if (strcmp(name, TUNNAME) == 0) { 146 u = -1; 147 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1) 148 return; /* Don't recognise the name */ 149 if (u != -1 && u > IF_MAXUNIT) 150 return; /* Unit number too high */ 151 152 /* find any existing device, or allocate new unit number */ 153 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0); 154 if (i) { 155 /* No preexisting struct cdev *, create one */ 156 *dev = make_dev(&tun_cdevsw, unit2minor(u), 157 UID_UUCP, GID_DIALER, 0600, "tun%d", u);
| 17 */ 18 19#include "opt_atalk.h" 20#include "opt_inet.h" 21#include "opt_inet6.h" 22#include "opt_ipx.h" 23#include "opt_mac.h" 24 25#include <sys/param.h> 26#include <sys/proc.h> 27#include <sys/systm.h> 28#include <sys/mac.h> 29#include <sys/mbuf.h> 30#include <sys/module.h> 31#include <sys/socket.h> 32#include <sys/fcntl.h> 33#include <sys/filio.h> 34#include <sys/sockio.h> 35#include <sys/ttycom.h> 36#include <sys/poll.h> 37#include <sys/selinfo.h> 38#include <sys/signalvar.h> 39#include <sys/filedesc.h> 40#include <sys/kernel.h> 41#include <sys/sysctl.h> 42#include <sys/conf.h> 43#include <sys/uio.h> 44#include <sys/malloc.h> 45#include <sys/random.h> 46 47#include <net/if.h> 48#include <net/if_types.h> 49#include <net/netisr.h> 50#include <net/route.h> 51#ifdef INET 52#include <netinet/in.h> 53#endif 54#include <net/bpf.h> 55#include <net/if_tun.h> 56 57#include <sys/queue.h> 58 59/* 60 * tun_list is protected by global tunmtx. Other mutable fields are 61 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is 62 * static for the duration of a tunnel interface. 63 */ 64struct tun_softc { 65 TAILQ_ENTRY(tun_softc) tun_list; 66 struct cdev *tun_dev; 67 u_short tun_flags; /* misc flags */ 68#define TUN_OPEN 0x0001 69#define TUN_INITED 0x0002 70#define TUN_RCOLL 0x0004 71#define TUN_IASET 0x0008 72#define TUN_DSTADDR 0x0010 73#define TUN_LMODE 0x0020 74#define TUN_RWAIT 0x0040 75#define TUN_ASYNC 0x0080 76#define TUN_IFHEAD 0x0100 77 78#define TUN_READY (TUN_OPEN | TUN_INITED) 79 80 /* 81 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this 82 * actually needed? Can we just return EBUSY if already open? 83 * Problem is that this involved inherent races when a tun device 84 * is handed off from one process to another, as opposed to just 85 * being slightly stale informationally. 86 */ 87 pid_t tun_pid; /* owning pid */ 88 struct ifnet tun_if; /* the interface */ 89 struct sigio *tun_sigio; /* information for async I/O */ 90 struct selinfo tun_rsel; /* read select */ 91 struct mtx tun_mtx; /* protect mutable softc fields */ 92}; 93 94#define TUNDEBUG if (tundebug) if_printf 95#define TUNNAME "tun" 96 97/* 98 * All mutable global variables in if_tun are locked using tunmtx, with 99 * the exception of tundebug, which is used unlocked, and tunclones, 100 * which is static after setup. 101 */ 102static struct mtx tunmtx; 103static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface"); 104static int tundebug = 0; 105static struct clonedevs *tunclones; 106static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead); 107SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, ""); 108 109static void tunclone(void *arg, char *name, int namelen, struct cdev **dev); 110static void tuncreate(struct cdev *dev); 111static int tunifioctl(struct ifnet *, u_long, caddr_t); 112static int tuninit(struct ifnet *); 113static int tunmodevent(module_t, int, void *); 114static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *, 115 struct rtentry *rt); 116static void tunstart(struct ifnet *); 117 118static d_open_t tunopen; 119static d_close_t tunclose; 120static d_read_t tunread; 121static d_write_t tunwrite; 122static d_ioctl_t tunioctl; 123static d_poll_t tunpoll; 124 125static struct cdevsw tun_cdevsw = { 126 .d_version = D_VERSION, 127 .d_flags = D_PSEUDO | D_NEEDGIANT, 128 .d_open = tunopen, 129 .d_close = tunclose, 130 .d_read = tunread, 131 .d_write = tunwrite, 132 .d_ioctl = tunioctl, 133 .d_poll = tunpoll, 134 .d_name = TUNNAME, 135}; 136 137static void 138tunclone(void *arg, char *name, int namelen, struct cdev **dev) 139{ 140 int u, i; 141 142 if (*dev != NULL) 143 return; 144 145 if (strcmp(name, TUNNAME) == 0) { 146 u = -1; 147 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1) 148 return; /* Don't recognise the name */ 149 if (u != -1 && u > IF_MAXUNIT) 150 return; /* Unit number too high */ 151 152 /* find any existing device, or allocate new unit number */ 153 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0); 154 if (i) { 155 /* No preexisting struct cdev *, create one */ 156 *dev = make_dev(&tun_cdevsw, unit2minor(u), 157 UID_UUCP, GID_DIALER, 0600, "tun%d", u);
|
160 } 161} 162 163static void 164tun_destroy(struct tun_softc *tp) 165{ 166 struct cdev *dev; 167 168 /* Unlocked read. */ 169 KASSERT((tp->tun_flags & TUN_OPEN) == 0, 170 ("tununits is out of sync - unit %d", tp->tun_if.if_dunit)); 171 172 dev = tp->tun_dev; 173 bpfdetach(&tp->tun_if); 174 if_detach(&tp->tun_if); 175 destroy_dev(dev); 176 mtx_destroy(&tp->tun_mtx); 177 free(tp, M_TUN); 178} 179 180static int 181tunmodevent(module_t mod, int type, void *data) 182{ 183 static eventhandler_tag tag; 184 struct tun_softc *tp; 185 186 switch (type) { 187 case MOD_LOAD: 188 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF); 189 clone_setup(&tunclones); 190 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000); 191 if (tag == NULL) 192 return (ENOMEM); 193 break; 194 case MOD_UNLOAD: 195 EVENTHANDLER_DEREGISTER(dev_clone, tag); 196 197 mtx_lock(&tunmtx); 198 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) { 199 TAILQ_REMOVE(&tunhead, tp, tun_list); 200 mtx_unlock(&tunmtx); 201 tun_destroy(tp); 202 mtx_lock(&tunmtx); 203 } 204 mtx_unlock(&tunmtx); 205 clone_cleanup(&tunclones); 206 mtx_destroy(&tunmtx); 207 break; 208 default: 209 return EOPNOTSUPP; 210 } 211 return 0; 212} 213 214static moduledata_t tun_mod = { 215 "if_tun", 216 tunmodevent, 217 0 218}; 219 220DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 221 222static void 223tunstart(struct ifnet *ifp) 224{ 225 struct tun_softc *tp = ifp->if_softc; 226 struct mbuf *m; 227 228 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 229 IFQ_LOCK(&ifp->if_snd); 230 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 231 if (m == NULL) { 232 IFQ_UNLOCK(&ifp->if_snd); 233 return; 234 } 235 IFQ_UNLOCK(&ifp->if_snd); 236 } 237 238 mtx_lock(&tp->tun_mtx); 239 if (tp->tun_flags & TUN_RWAIT) { 240 tp->tun_flags &= ~TUN_RWAIT; 241 wakeup(tp); 242 } 243 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) { 244 mtx_unlock(&tp->tun_mtx); 245 pgsigio(&tp->tun_sigio, SIGIO, 0); 246 } else 247 mtx_unlock(&tp->tun_mtx); 248 selwakeuppri(&tp->tun_rsel, PZERO + 1); 249} 250 251static void 252tuncreate(struct cdev *dev) 253{ 254 struct tun_softc *sc; 255 struct ifnet *ifp; 256 257 dev->si_flags &= ~SI_CHEAPCLONE; 258 259 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO); 260 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF); 261 sc->tun_flags = TUN_INITED; 262 sc->tun_dev = dev; 263 mtx_lock(&tunmtx); 264 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list); 265 mtx_unlock(&tunmtx); 266 267 ifp = &sc->tun_if; 268 if_initname(ifp, TUNNAME, dev2unit(dev)); 269 ifp->if_mtu = TUNMTU; 270 ifp->if_ioctl = tunifioctl; 271 ifp->if_output = tunoutput; 272 ifp->if_start = tunstart; 273 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 274 ifp->if_type = IFT_PPP; 275 ifp->if_softc = sc; 276 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 277 ifp->if_snd.ifq_drv_maxlen = 0; 278 IFQ_SET_READY(&ifp->if_snd); 279 280 if_attach(ifp); 281 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 282 dev->si_drv1 = sc; 283} 284 285static int 286tunopen(struct cdev *dev, int flag, int mode, struct thread *td) 287{ 288 struct ifnet *ifp; 289 struct tun_softc *tp; 290 291 /* 292 * XXXRW: Non-atomic test and set of dev->si_drv1 requires 293 * synchronization. 294 */ 295 tp = dev->si_drv1; 296 if (!tp) { 297 tuncreate(dev); 298 tp = dev->si_drv1; 299 } 300 301 /* 302 * XXXRW: This use of tun_pid is subject to error due to the 303 * fact that a reference to the tunnel can live beyond the 304 * death of the process that created it. Can we replace this 305 * with a simple busy flag? 306 */ 307 mtx_lock(&tp->tun_mtx); 308 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) { 309 mtx_unlock(&tp->tun_mtx); 310 return (EBUSY); 311 } 312 tp->tun_pid = td->td_proc->p_pid; 313 314 tp->tun_flags |= TUN_OPEN; 315 mtx_unlock(&tp->tun_mtx); 316 ifp = &tp->tun_if; 317 TUNDEBUG(ifp, "open\n"); 318 319 return (0); 320} 321 322/* 323 * tunclose - close the device - mark i/f down & delete 324 * routing info 325 */ 326static int 327tunclose(struct cdev *dev, int foo, int bar, struct thread *td) 328{ 329 struct tun_softc *tp; 330 struct ifnet *ifp; 331 int s; 332 333 tp = dev->si_drv1; 334 ifp = &tp->tun_if; 335 336 mtx_lock(&tp->tun_mtx); 337 tp->tun_flags &= ~TUN_OPEN; 338 tp->tun_pid = 0; 339 340 /* 341 * junk all pending output 342 */ 343 s = splimp(); 344 IFQ_PURGE(&ifp->if_snd); 345 splx(s); 346 mtx_unlock(&tp->tun_mtx); 347 348 if (ifp->if_flags & IFF_UP) { 349 s = splimp(); 350 if_down(ifp); 351 splx(s); 352 } 353 354 if (ifp->if_flags & IFF_RUNNING) { 355 struct ifaddr *ifa; 356 357 s = splimp(); 358 /* find internet addresses and delete routes */ 359 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 360 if (ifa->ifa_addr->sa_family == AF_INET) 361 /* Unlocked read. */ 362 rtinit(ifa, (int)RTM_DELETE, 363 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 364 ifp->if_flags &= ~IFF_RUNNING; 365 splx(s); 366 } 367 368 funsetown(&tp->tun_sigio); 369 selwakeuppri(&tp->tun_rsel, PZERO + 1); 370 TUNDEBUG (ifp, "closed\n"); 371 return (0); 372} 373 374static int 375tuninit(struct ifnet *ifp) 376{ 377 struct tun_softc *tp = ifp->if_softc; 378 struct ifaddr *ifa; 379 int error = 0; 380 381 TUNDEBUG(ifp, "tuninit\n"); 382 383 ifp->if_flags |= IFF_UP | IFF_RUNNING; 384 getmicrotime(&ifp->if_lastchange); 385 386 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 387 ifa = TAILQ_NEXT(ifa, ifa_link)) { 388 if (ifa->ifa_addr == NULL) 389 error = EFAULT; 390 /* XXX: Should maybe return straight off? */ 391 else { 392#ifdef INET 393 if (ifa->ifa_addr->sa_family == AF_INET) { 394 struct sockaddr_in *si; 395 396 si = (struct sockaddr_in *)ifa->ifa_addr; 397 mtx_lock(&tp->tun_mtx); 398 if (si->sin_addr.s_addr) 399 tp->tun_flags |= TUN_IASET; 400 401 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 402 if (si && si->sin_addr.s_addr) 403 tp->tun_flags |= TUN_DSTADDR; 404 mtx_unlock(&tp->tun_mtx); 405 } 406#endif 407 } 408 } 409 return (error); 410} 411 412/* 413 * Process an ioctl request. 414 */ 415static int 416tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 417{ 418 struct ifreq *ifr = (struct ifreq *)data; 419 struct tun_softc *tp = ifp->if_softc; 420 struct ifstat *ifs; 421 int error = 0, s; 422 423 s = splimp(); 424 switch(cmd) { 425 case SIOCGIFSTATUS: 426 ifs = (struct ifstat *)data; 427 mtx_lock(&tp->tun_mtx); 428 if (tp->tun_pid) 429 sprintf(ifs->ascii + strlen(ifs->ascii), 430 "\tOpened by PID %d\n", tp->tun_pid); 431 mtx_unlock(&tp->tun_mtx); 432 break; 433 case SIOCSIFADDR: 434 error = tuninit(ifp); 435 TUNDEBUG(ifp, "address set, error=%d\n", error); 436 break; 437 case SIOCSIFDSTADDR: 438 error = tuninit(ifp); 439 TUNDEBUG(ifp, "destination address set, error=%d\n", error); 440 break; 441 case SIOCSIFMTU: 442 ifp->if_mtu = ifr->ifr_mtu; 443 TUNDEBUG(ifp, "mtu set\n"); 444 break; 445 case SIOCSIFFLAGS: 446 case SIOCADDMULTI: 447 case SIOCDELMULTI: 448 break; 449 default: 450 error = EINVAL; 451 } 452 splx(s); 453 return (error); 454} 455 456/* 457 * tunoutput - queue packets from higher level ready to put out. 458 */ 459static int 460tunoutput( 461 struct ifnet *ifp, 462 struct mbuf *m0, 463 struct sockaddr *dst, 464 struct rtentry *rt) 465{ 466 struct tun_softc *tp = ifp->if_softc; 467 u_short cached_tun_flags; 468 int error; 469 470 TUNDEBUG (ifp, "tunoutput\n"); 471 472#ifdef MAC 473 error = mac_check_ifnet_transmit(ifp, m0); 474 if (error) { 475 m_freem(m0); 476 return (error); 477 } 478#endif 479 480 /* Could be unlocked read? */ 481 mtx_lock(&tp->tun_mtx); 482 cached_tun_flags = tp->tun_flags; 483 mtx_unlock(&tp->tun_mtx); 484 if ((cached_tun_flags & TUN_READY) != TUN_READY) { 485 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 486 m_freem (m0); 487 return (EHOSTDOWN); 488 } 489 490 if ((ifp->if_flags & IFF_UP) != IFF_UP) { 491 m_freem (m0); 492 return (EHOSTDOWN); 493 } 494 495 /* BPF write needs to be handled specially */ 496 if (dst->sa_family == AF_UNSPEC) { 497 dst->sa_family = *(mtod(m0, int *)); 498 m0->m_len -= sizeof(int); 499 m0->m_pkthdr.len -= sizeof(int); 500 m0->m_data += sizeof(int); 501 } 502 503 if (ifp->if_bpf) { 504 uint32_t af = dst->sa_family; 505 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0); 506 } 507 508 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 509 if (cached_tun_flags & TUN_LMODE) { 510 /* allocate space for sockaddr */ 511 M_PREPEND(m0, dst->sa_len, M_DONTWAIT); 512 513 /* if allocation failed drop packet */ 514 if (m0 == NULL) { 515 ifp->if_iqdrops++; 516 ifp->if_oerrors++; 517 return (ENOBUFS); 518 } else { 519 bcopy(dst, m0->m_data, dst->sa_len); 520 } 521 } 522 523 if (cached_tun_flags & TUN_IFHEAD) { 524 /* Prepend the address family */ 525 M_PREPEND(m0, 4, M_DONTWAIT); 526 527 /* if allocation failed drop packet */ 528 if (m0 == NULL) { 529 ifp->if_iqdrops++; 530 ifp->if_oerrors++; 531 return (ENOBUFS); 532 } else 533 *(u_int32_t *)m0->m_data = htonl(dst->sa_family); 534 } else { 535#ifdef INET 536 if (dst->sa_family != AF_INET) 537#endif 538 { 539 m_freem(m0); 540 return (EAFNOSUPPORT); 541 } 542 } 543 544 IFQ_HANDOFF(ifp, m0, error); 545 if (error) { 546 ifp->if_collisions++; 547 return (ENOBUFS); 548 } 549 ifp->if_opackets++; 550 return (0); 551} 552 553/* 554 * the cdevsw interface is now pretty minimal. 555 */ 556static int 557tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 558{ 559 int s; 560 int error; 561 struct tun_softc *tp = dev->si_drv1; 562 struct tuninfo *tunp; 563 564 switch (cmd) { 565 case TUNSIFINFO: 566 tunp = (struct tuninfo *)data; 567 if (tunp->mtu < IF_MINMTU) 568 return (EINVAL); 569 if (tp->tun_if.if_mtu != tunp->mtu 570 && (error = suser(td)) != 0) 571 return (error); 572 tp->tun_if.if_mtu = tunp->mtu; 573 tp->tun_if.if_type = tunp->type; 574 tp->tun_if.if_baudrate = tunp->baudrate; 575 break; 576 case TUNGIFINFO: 577 tunp = (struct tuninfo *)data; 578 tunp->mtu = tp->tun_if.if_mtu; 579 tunp->type = tp->tun_if.if_type; 580 tunp->baudrate = tp->tun_if.if_baudrate; 581 break; 582 case TUNSDEBUG: 583 tundebug = *(int *)data; 584 break; 585 case TUNGDEBUG: 586 *(int *)data = tundebug; 587 break; 588 case TUNSLMODE: 589 mtx_lock(&tp->tun_mtx); 590 if (*(int *)data) { 591 tp->tun_flags |= TUN_LMODE; 592 tp->tun_flags &= ~TUN_IFHEAD; 593 } else 594 tp->tun_flags &= ~TUN_LMODE; 595 mtx_unlock(&tp->tun_mtx); 596 break; 597 case TUNSIFHEAD: 598 mtx_lock(&tp->tun_mtx); 599 if (*(int *)data) { 600 tp->tun_flags |= TUN_IFHEAD; 601 tp->tun_flags &= ~TUN_LMODE; 602 } else 603 tp->tun_flags &= ~TUN_IFHEAD; 604 mtx_unlock(&tp->tun_mtx); 605 break; 606 case TUNGIFHEAD: 607 /* Could be unlocked read? */ 608 mtx_lock(&tp->tun_mtx); 609 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0; 610 mtx_unlock(&tp->tun_mtx); 611 break; 612 case TUNSIFMODE: 613 /* deny this if UP */ 614 if (tp->tun_if.if_flags & IFF_UP) 615 return(EBUSY); 616 617 switch (*(int *)data & ~IFF_MULTICAST) { 618 case IFF_POINTOPOINT: 619 case IFF_BROADCAST: 620 tp->tun_if.if_flags &= 621 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); 622 tp->tun_if.if_flags |= *(int *)data; 623 break; 624 default: 625 return(EINVAL); 626 } 627 break; 628 case TUNSIFPID: 629 mtx_lock(&tp->tun_mtx); 630 tp->tun_pid = curthread->td_proc->p_pid; 631 mtx_unlock(&tp->tun_mtx); 632 break; 633 case FIONBIO: 634 break; 635 case FIOASYNC: 636 mtx_lock(&tp->tun_mtx); 637 if (*(int *)data) 638 tp->tun_flags |= TUN_ASYNC; 639 else 640 tp->tun_flags &= ~TUN_ASYNC; 641 mtx_unlock(&tp->tun_mtx); 642 break; 643 case FIONREAD: 644 s = splimp(); 645 if (!IFQ_IS_EMPTY(&tp->tun_if.if_snd)) { 646 struct mbuf *mb; 647 IFQ_LOCK(&tp->tun_if.if_snd); 648 IFQ_POLL_NOLOCK(&tp->tun_if.if_snd, mb); 649 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 650 *(int *)data += mb->m_len; 651 IFQ_UNLOCK(&tp->tun_if.if_snd); 652 } else 653 *(int *)data = 0; 654 splx(s); 655 break; 656 case FIOSETOWN: 657 return (fsetown(*(int *)data, &tp->tun_sigio)); 658 659 case FIOGETOWN: 660 *(int *)data = fgetown(&tp->tun_sigio); 661 return (0); 662 663 /* This is deprecated, FIOSETOWN should be used instead. */ 664 case TIOCSPGRP: 665 return (fsetown(-(*(int *)data), &tp->tun_sigio)); 666 667 /* This is deprecated, FIOGETOWN should be used instead. */ 668 case TIOCGPGRP: 669 *(int *)data = -fgetown(&tp->tun_sigio); 670 return (0); 671 672 default: 673 return (ENOTTY); 674 } 675 return (0); 676} 677 678/* 679 * The cdevsw read interface - reads a packet at a time, or at 680 * least as much of a packet as can be read. 681 */ 682static int 683tunread(struct cdev *dev, struct uio *uio, int flag) 684{ 685 struct tun_softc *tp = dev->si_drv1; 686 struct ifnet *ifp = &tp->tun_if; 687 struct mbuf *m; 688 int error=0, len, s; 689 690 TUNDEBUG (ifp, "read\n"); 691 mtx_lock(&tp->tun_mtx); 692 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 693 mtx_unlock(&tp->tun_mtx); 694 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 695 return (EHOSTDOWN); 696 } 697 698 tp->tun_flags &= ~TUN_RWAIT; 699 mtx_unlock(&tp->tun_mtx); 700 701 s = splimp(); 702 do { 703 IFQ_DEQUEUE(&ifp->if_snd, m); 704 if (m == NULL) { 705 if (flag & O_NONBLOCK) { 706 splx(s); 707 return (EWOULDBLOCK); 708 } 709 mtx_lock(&tp->tun_mtx); 710 tp->tun_flags |= TUN_RWAIT; 711 mtx_unlock(&tp->tun_mtx); 712 if((error = tsleep(tp, PCATCH | (PZERO + 1), 713 "tunread", 0)) != 0) { 714 splx(s); 715 return (error); 716 } 717 } 718 } while (m == NULL); 719 splx(s); 720 721 while (m && uio->uio_resid > 0 && error == 0) { 722 len = min(uio->uio_resid, m->m_len); 723 if (len != 0) 724 error = uiomove(mtod(m, void *), len, uio); 725 m = m_free(m); 726 } 727 728 if (m) { 729 TUNDEBUG(ifp, "Dropping mbuf\n"); 730 m_freem(m); 731 } 732 return (error); 733} 734 735/* 736 * the cdevsw write interface - an atomic write is a packet - or else! 737 */ 738static int 739tunwrite(struct cdev *dev, struct uio *uio, int flag) 740{ 741 struct tun_softc *tp = dev->si_drv1; 742 struct ifnet *ifp = &tp->tun_if; 743 struct mbuf *m; 744 int error = 0; 745 uint32_t family; 746 int isr; 747 748 TUNDEBUG(ifp, "tunwrite\n"); 749 750 if ((ifp->if_flags & IFF_UP) != IFF_UP) 751 /* ignore silently */ 752 return (0); 753 754 if (uio->uio_resid == 0) 755 return (0); 756 757 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { 758 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid); 759 return (EIO); 760 } 761 762 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0)) == NULL) { 763 ifp->if_ierrors++; 764 return (error); 765 } 766 767 m->m_pkthdr.rcvif = ifp; 768#ifdef MAC 769 mac_create_mbuf_from_ifnet(ifp, m); 770#endif 771 772 /* Could be unlocked read? */ 773 mtx_lock(&tp->tun_mtx); 774 if (tp->tun_flags & TUN_IFHEAD) { 775 mtx_unlock(&tp->tun_mtx); 776 if (m->m_len < sizeof(family) && 777 (m = m_pullup(m, sizeof(family))) == NULL) 778 return (ENOBUFS); 779 family = ntohl(*mtod(m, u_int32_t *)); 780 m_adj(m, sizeof(family)); 781 } else { 782 mtx_unlock(&tp->tun_mtx); 783 family = AF_INET; 784 } 785 786 BPF_MTAP2(ifp, &family, sizeof(family), m); 787 788 switch (family) { 789#ifdef INET 790 case AF_INET: 791 isr = NETISR_IP; 792 break; 793#endif 794#ifdef INET6 795 case AF_INET6: 796 isr = NETISR_IPV6; 797 break; 798#endif 799#ifdef IPX 800 case AF_IPX: 801 isr = NETISR_IPX; 802 break; 803#endif 804#ifdef NETATALK 805 case AF_APPLETALK: 806 isr = NETISR_ATALK2; 807 break; 808#endif 809 default: 810 m_freem(m); 811 return (EAFNOSUPPORT); 812 } 813 /* First chunk of an mbuf contains good junk */ 814 if (harvest.point_to_point) 815 random_harvest(m, 16, 3, 0, RANDOM_NET); 816 ifp->if_ibytes += m->m_pkthdr.len; 817 ifp->if_ipackets++; 818 netisr_dispatch(isr, m); 819 return (0); 820} 821 822/* 823 * tunpoll - the poll interface, this is only useful on reads 824 * really. The write detect always returns true, write never blocks 825 * anyway, it either accepts the packet or drops it. 826 */ 827static int 828tunpoll(struct cdev *dev, int events, struct thread *td) 829{ 830 int s; 831 struct tun_softc *tp = dev->si_drv1; 832 struct ifnet *ifp = &tp->tun_if; 833 int revents = 0; 834 struct mbuf *m; 835 836 s = splimp(); 837 TUNDEBUG(ifp, "tunpoll\n"); 838 839 if (events & (POLLIN | POLLRDNORM)) { 840 IFQ_LOCK(&ifp->if_snd); 841 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 842 if (m != NULL) { 843 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len); 844 revents |= events & (POLLIN | POLLRDNORM); 845 } else { 846 TUNDEBUG(ifp, "tunpoll waiting\n"); 847 selrecord(td, &tp->tun_rsel); 848 } 849 IFQ_UNLOCK(&ifp->if_snd); 850 } 851 if (events & (POLLOUT | POLLWRNORM)) 852 revents |= events & (POLLOUT | POLLWRNORM); 853 854 splx(s); 855 return (revents); 856}
| 162 } 163} 164 165static void 166tun_destroy(struct tun_softc *tp) 167{ 168 struct cdev *dev; 169 170 /* Unlocked read. */ 171 KASSERT((tp->tun_flags & TUN_OPEN) == 0, 172 ("tununits is out of sync - unit %d", tp->tun_if.if_dunit)); 173 174 dev = tp->tun_dev; 175 bpfdetach(&tp->tun_if); 176 if_detach(&tp->tun_if); 177 destroy_dev(dev); 178 mtx_destroy(&tp->tun_mtx); 179 free(tp, M_TUN); 180} 181 182static int 183tunmodevent(module_t mod, int type, void *data) 184{ 185 static eventhandler_tag tag; 186 struct tun_softc *tp; 187 188 switch (type) { 189 case MOD_LOAD: 190 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF); 191 clone_setup(&tunclones); 192 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000); 193 if (tag == NULL) 194 return (ENOMEM); 195 break; 196 case MOD_UNLOAD: 197 EVENTHANDLER_DEREGISTER(dev_clone, tag); 198 199 mtx_lock(&tunmtx); 200 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) { 201 TAILQ_REMOVE(&tunhead, tp, tun_list); 202 mtx_unlock(&tunmtx); 203 tun_destroy(tp); 204 mtx_lock(&tunmtx); 205 } 206 mtx_unlock(&tunmtx); 207 clone_cleanup(&tunclones); 208 mtx_destroy(&tunmtx); 209 break; 210 default: 211 return EOPNOTSUPP; 212 } 213 return 0; 214} 215 216static moduledata_t tun_mod = { 217 "if_tun", 218 tunmodevent, 219 0 220}; 221 222DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 223 224static void 225tunstart(struct ifnet *ifp) 226{ 227 struct tun_softc *tp = ifp->if_softc; 228 struct mbuf *m; 229 230 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 231 IFQ_LOCK(&ifp->if_snd); 232 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 233 if (m == NULL) { 234 IFQ_UNLOCK(&ifp->if_snd); 235 return; 236 } 237 IFQ_UNLOCK(&ifp->if_snd); 238 } 239 240 mtx_lock(&tp->tun_mtx); 241 if (tp->tun_flags & TUN_RWAIT) { 242 tp->tun_flags &= ~TUN_RWAIT; 243 wakeup(tp); 244 } 245 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) { 246 mtx_unlock(&tp->tun_mtx); 247 pgsigio(&tp->tun_sigio, SIGIO, 0); 248 } else 249 mtx_unlock(&tp->tun_mtx); 250 selwakeuppri(&tp->tun_rsel, PZERO + 1); 251} 252 253static void 254tuncreate(struct cdev *dev) 255{ 256 struct tun_softc *sc; 257 struct ifnet *ifp; 258 259 dev->si_flags &= ~SI_CHEAPCLONE; 260 261 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO); 262 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF); 263 sc->tun_flags = TUN_INITED; 264 sc->tun_dev = dev; 265 mtx_lock(&tunmtx); 266 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list); 267 mtx_unlock(&tunmtx); 268 269 ifp = &sc->tun_if; 270 if_initname(ifp, TUNNAME, dev2unit(dev)); 271 ifp->if_mtu = TUNMTU; 272 ifp->if_ioctl = tunifioctl; 273 ifp->if_output = tunoutput; 274 ifp->if_start = tunstart; 275 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 276 ifp->if_type = IFT_PPP; 277 ifp->if_softc = sc; 278 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 279 ifp->if_snd.ifq_drv_maxlen = 0; 280 IFQ_SET_READY(&ifp->if_snd); 281 282 if_attach(ifp); 283 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 284 dev->si_drv1 = sc; 285} 286 287static int 288tunopen(struct cdev *dev, int flag, int mode, struct thread *td) 289{ 290 struct ifnet *ifp; 291 struct tun_softc *tp; 292 293 /* 294 * XXXRW: Non-atomic test and set of dev->si_drv1 requires 295 * synchronization. 296 */ 297 tp = dev->si_drv1; 298 if (!tp) { 299 tuncreate(dev); 300 tp = dev->si_drv1; 301 } 302 303 /* 304 * XXXRW: This use of tun_pid is subject to error due to the 305 * fact that a reference to the tunnel can live beyond the 306 * death of the process that created it. Can we replace this 307 * with a simple busy flag? 308 */ 309 mtx_lock(&tp->tun_mtx); 310 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) { 311 mtx_unlock(&tp->tun_mtx); 312 return (EBUSY); 313 } 314 tp->tun_pid = td->td_proc->p_pid; 315 316 tp->tun_flags |= TUN_OPEN; 317 mtx_unlock(&tp->tun_mtx); 318 ifp = &tp->tun_if; 319 TUNDEBUG(ifp, "open\n"); 320 321 return (0); 322} 323 324/* 325 * tunclose - close the device - mark i/f down & delete 326 * routing info 327 */ 328static int 329tunclose(struct cdev *dev, int foo, int bar, struct thread *td) 330{ 331 struct tun_softc *tp; 332 struct ifnet *ifp; 333 int s; 334 335 tp = dev->si_drv1; 336 ifp = &tp->tun_if; 337 338 mtx_lock(&tp->tun_mtx); 339 tp->tun_flags &= ~TUN_OPEN; 340 tp->tun_pid = 0; 341 342 /* 343 * junk all pending output 344 */ 345 s = splimp(); 346 IFQ_PURGE(&ifp->if_snd); 347 splx(s); 348 mtx_unlock(&tp->tun_mtx); 349 350 if (ifp->if_flags & IFF_UP) { 351 s = splimp(); 352 if_down(ifp); 353 splx(s); 354 } 355 356 if (ifp->if_flags & IFF_RUNNING) { 357 struct ifaddr *ifa; 358 359 s = splimp(); 360 /* find internet addresses and delete routes */ 361 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 362 if (ifa->ifa_addr->sa_family == AF_INET) 363 /* Unlocked read. */ 364 rtinit(ifa, (int)RTM_DELETE, 365 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0); 366 ifp->if_flags &= ~IFF_RUNNING; 367 splx(s); 368 } 369 370 funsetown(&tp->tun_sigio); 371 selwakeuppri(&tp->tun_rsel, PZERO + 1); 372 TUNDEBUG (ifp, "closed\n"); 373 return (0); 374} 375 376static int 377tuninit(struct ifnet *ifp) 378{ 379 struct tun_softc *tp = ifp->if_softc; 380 struct ifaddr *ifa; 381 int error = 0; 382 383 TUNDEBUG(ifp, "tuninit\n"); 384 385 ifp->if_flags |= IFF_UP | IFF_RUNNING; 386 getmicrotime(&ifp->if_lastchange); 387 388 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 389 ifa = TAILQ_NEXT(ifa, ifa_link)) { 390 if (ifa->ifa_addr == NULL) 391 error = EFAULT; 392 /* XXX: Should maybe return straight off? */ 393 else { 394#ifdef INET 395 if (ifa->ifa_addr->sa_family == AF_INET) { 396 struct sockaddr_in *si; 397 398 si = (struct sockaddr_in *)ifa->ifa_addr; 399 mtx_lock(&tp->tun_mtx); 400 if (si->sin_addr.s_addr) 401 tp->tun_flags |= TUN_IASET; 402 403 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 404 if (si && si->sin_addr.s_addr) 405 tp->tun_flags |= TUN_DSTADDR; 406 mtx_unlock(&tp->tun_mtx); 407 } 408#endif 409 } 410 } 411 return (error); 412} 413 414/* 415 * Process an ioctl request. 416 */ 417static int 418tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 419{ 420 struct ifreq *ifr = (struct ifreq *)data; 421 struct tun_softc *tp = ifp->if_softc; 422 struct ifstat *ifs; 423 int error = 0, s; 424 425 s = splimp(); 426 switch(cmd) { 427 case SIOCGIFSTATUS: 428 ifs = (struct ifstat *)data; 429 mtx_lock(&tp->tun_mtx); 430 if (tp->tun_pid) 431 sprintf(ifs->ascii + strlen(ifs->ascii), 432 "\tOpened by PID %d\n", tp->tun_pid); 433 mtx_unlock(&tp->tun_mtx); 434 break; 435 case SIOCSIFADDR: 436 error = tuninit(ifp); 437 TUNDEBUG(ifp, "address set, error=%d\n", error); 438 break; 439 case SIOCSIFDSTADDR: 440 error = tuninit(ifp); 441 TUNDEBUG(ifp, "destination address set, error=%d\n", error); 442 break; 443 case SIOCSIFMTU: 444 ifp->if_mtu = ifr->ifr_mtu; 445 TUNDEBUG(ifp, "mtu set\n"); 446 break; 447 case SIOCSIFFLAGS: 448 case SIOCADDMULTI: 449 case SIOCDELMULTI: 450 break; 451 default: 452 error = EINVAL; 453 } 454 splx(s); 455 return (error); 456} 457 458/* 459 * tunoutput - queue packets from higher level ready to put out. 460 */ 461static int 462tunoutput( 463 struct ifnet *ifp, 464 struct mbuf *m0, 465 struct sockaddr *dst, 466 struct rtentry *rt) 467{ 468 struct tun_softc *tp = ifp->if_softc; 469 u_short cached_tun_flags; 470 int error; 471 472 TUNDEBUG (ifp, "tunoutput\n"); 473 474#ifdef MAC 475 error = mac_check_ifnet_transmit(ifp, m0); 476 if (error) { 477 m_freem(m0); 478 return (error); 479 } 480#endif 481 482 /* Could be unlocked read? */ 483 mtx_lock(&tp->tun_mtx); 484 cached_tun_flags = tp->tun_flags; 485 mtx_unlock(&tp->tun_mtx); 486 if ((cached_tun_flags & TUN_READY) != TUN_READY) { 487 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 488 m_freem (m0); 489 return (EHOSTDOWN); 490 } 491 492 if ((ifp->if_flags & IFF_UP) != IFF_UP) { 493 m_freem (m0); 494 return (EHOSTDOWN); 495 } 496 497 /* BPF write needs to be handled specially */ 498 if (dst->sa_family == AF_UNSPEC) { 499 dst->sa_family = *(mtod(m0, int *)); 500 m0->m_len -= sizeof(int); 501 m0->m_pkthdr.len -= sizeof(int); 502 m0->m_data += sizeof(int); 503 } 504 505 if (ifp->if_bpf) { 506 uint32_t af = dst->sa_family; 507 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0); 508 } 509 510 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 511 if (cached_tun_flags & TUN_LMODE) { 512 /* allocate space for sockaddr */ 513 M_PREPEND(m0, dst->sa_len, M_DONTWAIT); 514 515 /* if allocation failed drop packet */ 516 if (m0 == NULL) { 517 ifp->if_iqdrops++; 518 ifp->if_oerrors++; 519 return (ENOBUFS); 520 } else { 521 bcopy(dst, m0->m_data, dst->sa_len); 522 } 523 } 524 525 if (cached_tun_flags & TUN_IFHEAD) { 526 /* Prepend the address family */ 527 M_PREPEND(m0, 4, M_DONTWAIT); 528 529 /* if allocation failed drop packet */ 530 if (m0 == NULL) { 531 ifp->if_iqdrops++; 532 ifp->if_oerrors++; 533 return (ENOBUFS); 534 } else 535 *(u_int32_t *)m0->m_data = htonl(dst->sa_family); 536 } else { 537#ifdef INET 538 if (dst->sa_family != AF_INET) 539#endif 540 { 541 m_freem(m0); 542 return (EAFNOSUPPORT); 543 } 544 } 545 546 IFQ_HANDOFF(ifp, m0, error); 547 if (error) { 548 ifp->if_collisions++; 549 return (ENOBUFS); 550 } 551 ifp->if_opackets++; 552 return (0); 553} 554 555/* 556 * the cdevsw interface is now pretty minimal. 557 */ 558static int 559tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 560{ 561 int s; 562 int error; 563 struct tun_softc *tp = dev->si_drv1; 564 struct tuninfo *tunp; 565 566 switch (cmd) { 567 case TUNSIFINFO: 568 tunp = (struct tuninfo *)data; 569 if (tunp->mtu < IF_MINMTU) 570 return (EINVAL); 571 if (tp->tun_if.if_mtu != tunp->mtu 572 && (error = suser(td)) != 0) 573 return (error); 574 tp->tun_if.if_mtu = tunp->mtu; 575 tp->tun_if.if_type = tunp->type; 576 tp->tun_if.if_baudrate = tunp->baudrate; 577 break; 578 case TUNGIFINFO: 579 tunp = (struct tuninfo *)data; 580 tunp->mtu = tp->tun_if.if_mtu; 581 tunp->type = tp->tun_if.if_type; 582 tunp->baudrate = tp->tun_if.if_baudrate; 583 break; 584 case TUNSDEBUG: 585 tundebug = *(int *)data; 586 break; 587 case TUNGDEBUG: 588 *(int *)data = tundebug; 589 break; 590 case TUNSLMODE: 591 mtx_lock(&tp->tun_mtx); 592 if (*(int *)data) { 593 tp->tun_flags |= TUN_LMODE; 594 tp->tun_flags &= ~TUN_IFHEAD; 595 } else 596 tp->tun_flags &= ~TUN_LMODE; 597 mtx_unlock(&tp->tun_mtx); 598 break; 599 case TUNSIFHEAD: 600 mtx_lock(&tp->tun_mtx); 601 if (*(int *)data) { 602 tp->tun_flags |= TUN_IFHEAD; 603 tp->tun_flags &= ~TUN_LMODE; 604 } else 605 tp->tun_flags &= ~TUN_IFHEAD; 606 mtx_unlock(&tp->tun_mtx); 607 break; 608 case TUNGIFHEAD: 609 /* Could be unlocked read? */ 610 mtx_lock(&tp->tun_mtx); 611 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0; 612 mtx_unlock(&tp->tun_mtx); 613 break; 614 case TUNSIFMODE: 615 /* deny this if UP */ 616 if (tp->tun_if.if_flags & IFF_UP) 617 return(EBUSY); 618 619 switch (*(int *)data & ~IFF_MULTICAST) { 620 case IFF_POINTOPOINT: 621 case IFF_BROADCAST: 622 tp->tun_if.if_flags &= 623 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); 624 tp->tun_if.if_flags |= *(int *)data; 625 break; 626 default: 627 return(EINVAL); 628 } 629 break; 630 case TUNSIFPID: 631 mtx_lock(&tp->tun_mtx); 632 tp->tun_pid = curthread->td_proc->p_pid; 633 mtx_unlock(&tp->tun_mtx); 634 break; 635 case FIONBIO: 636 break; 637 case FIOASYNC: 638 mtx_lock(&tp->tun_mtx); 639 if (*(int *)data) 640 tp->tun_flags |= TUN_ASYNC; 641 else 642 tp->tun_flags &= ~TUN_ASYNC; 643 mtx_unlock(&tp->tun_mtx); 644 break; 645 case FIONREAD: 646 s = splimp(); 647 if (!IFQ_IS_EMPTY(&tp->tun_if.if_snd)) { 648 struct mbuf *mb; 649 IFQ_LOCK(&tp->tun_if.if_snd); 650 IFQ_POLL_NOLOCK(&tp->tun_if.if_snd, mb); 651 for( *(int *)data = 0; mb != 0; mb = mb->m_next) 652 *(int *)data += mb->m_len; 653 IFQ_UNLOCK(&tp->tun_if.if_snd); 654 } else 655 *(int *)data = 0; 656 splx(s); 657 break; 658 case FIOSETOWN: 659 return (fsetown(*(int *)data, &tp->tun_sigio)); 660 661 case FIOGETOWN: 662 *(int *)data = fgetown(&tp->tun_sigio); 663 return (0); 664 665 /* This is deprecated, FIOSETOWN should be used instead. */ 666 case TIOCSPGRP: 667 return (fsetown(-(*(int *)data), &tp->tun_sigio)); 668 669 /* This is deprecated, FIOGETOWN should be used instead. */ 670 case TIOCGPGRP: 671 *(int *)data = -fgetown(&tp->tun_sigio); 672 return (0); 673 674 default: 675 return (ENOTTY); 676 } 677 return (0); 678} 679 680/* 681 * The cdevsw read interface - reads a packet at a time, or at 682 * least as much of a packet as can be read. 683 */ 684static int 685tunread(struct cdev *dev, struct uio *uio, int flag) 686{ 687 struct tun_softc *tp = dev->si_drv1; 688 struct ifnet *ifp = &tp->tun_if; 689 struct mbuf *m; 690 int error=0, len, s; 691 692 TUNDEBUG (ifp, "read\n"); 693 mtx_lock(&tp->tun_mtx); 694 if ((tp->tun_flags & TUN_READY) != TUN_READY) { 695 mtx_unlock(&tp->tun_mtx); 696 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags); 697 return (EHOSTDOWN); 698 } 699 700 tp->tun_flags &= ~TUN_RWAIT; 701 mtx_unlock(&tp->tun_mtx); 702 703 s = splimp(); 704 do { 705 IFQ_DEQUEUE(&ifp->if_snd, m); 706 if (m == NULL) { 707 if (flag & O_NONBLOCK) { 708 splx(s); 709 return (EWOULDBLOCK); 710 } 711 mtx_lock(&tp->tun_mtx); 712 tp->tun_flags |= TUN_RWAIT; 713 mtx_unlock(&tp->tun_mtx); 714 if((error = tsleep(tp, PCATCH | (PZERO + 1), 715 "tunread", 0)) != 0) { 716 splx(s); 717 return (error); 718 } 719 } 720 } while (m == NULL); 721 splx(s); 722 723 while (m && uio->uio_resid > 0 && error == 0) { 724 len = min(uio->uio_resid, m->m_len); 725 if (len != 0) 726 error = uiomove(mtod(m, void *), len, uio); 727 m = m_free(m); 728 } 729 730 if (m) { 731 TUNDEBUG(ifp, "Dropping mbuf\n"); 732 m_freem(m); 733 } 734 return (error); 735} 736 737/* 738 * the cdevsw write interface - an atomic write is a packet - or else! 739 */ 740static int 741tunwrite(struct cdev *dev, struct uio *uio, int flag) 742{ 743 struct tun_softc *tp = dev->si_drv1; 744 struct ifnet *ifp = &tp->tun_if; 745 struct mbuf *m; 746 int error = 0; 747 uint32_t family; 748 int isr; 749 750 TUNDEBUG(ifp, "tunwrite\n"); 751 752 if ((ifp->if_flags & IFF_UP) != IFF_UP) 753 /* ignore silently */ 754 return (0); 755 756 if (uio->uio_resid == 0) 757 return (0); 758 759 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) { 760 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid); 761 return (EIO); 762 } 763 764 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0)) == NULL) { 765 ifp->if_ierrors++; 766 return (error); 767 } 768 769 m->m_pkthdr.rcvif = ifp; 770#ifdef MAC 771 mac_create_mbuf_from_ifnet(ifp, m); 772#endif 773 774 /* Could be unlocked read? */ 775 mtx_lock(&tp->tun_mtx); 776 if (tp->tun_flags & TUN_IFHEAD) { 777 mtx_unlock(&tp->tun_mtx); 778 if (m->m_len < sizeof(family) && 779 (m = m_pullup(m, sizeof(family))) == NULL) 780 return (ENOBUFS); 781 family = ntohl(*mtod(m, u_int32_t *)); 782 m_adj(m, sizeof(family)); 783 } else { 784 mtx_unlock(&tp->tun_mtx); 785 family = AF_INET; 786 } 787 788 BPF_MTAP2(ifp, &family, sizeof(family), m); 789 790 switch (family) { 791#ifdef INET 792 case AF_INET: 793 isr = NETISR_IP; 794 break; 795#endif 796#ifdef INET6 797 case AF_INET6: 798 isr = NETISR_IPV6; 799 break; 800#endif 801#ifdef IPX 802 case AF_IPX: 803 isr = NETISR_IPX; 804 break; 805#endif 806#ifdef NETATALK 807 case AF_APPLETALK: 808 isr = NETISR_ATALK2; 809 break; 810#endif 811 default: 812 m_freem(m); 813 return (EAFNOSUPPORT); 814 } 815 /* First chunk of an mbuf contains good junk */ 816 if (harvest.point_to_point) 817 random_harvest(m, 16, 3, 0, RANDOM_NET); 818 ifp->if_ibytes += m->m_pkthdr.len; 819 ifp->if_ipackets++; 820 netisr_dispatch(isr, m); 821 return (0); 822} 823 824/* 825 * tunpoll - the poll interface, this is only useful on reads 826 * really. The write detect always returns true, write never blocks 827 * anyway, it either accepts the packet or drops it. 828 */ 829static int 830tunpoll(struct cdev *dev, int events, struct thread *td) 831{ 832 int s; 833 struct tun_softc *tp = dev->si_drv1; 834 struct ifnet *ifp = &tp->tun_if; 835 int revents = 0; 836 struct mbuf *m; 837 838 s = splimp(); 839 TUNDEBUG(ifp, "tunpoll\n"); 840 841 if (events & (POLLIN | POLLRDNORM)) { 842 IFQ_LOCK(&ifp->if_snd); 843 IFQ_POLL_NOLOCK(&ifp->if_snd, m); 844 if (m != NULL) { 845 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len); 846 revents |= events & (POLLIN | POLLRDNORM); 847 } else { 848 TUNDEBUG(ifp, "tunpoll waiting\n"); 849 selrecord(td, &tp->tun_rsel); 850 } 851 IFQ_UNLOCK(&ifp->if_snd); 852 } 853 if (events & (POLLOUT | POLLWRNORM)) 854 revents |= events & (POLLOUT | POLLWRNORM); 855 856 splx(s); 857 return (revents); 858}
|