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 its 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/poll mode of 14 * operation though. 15 * 16 * $FreeBSD: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $ 17 */ 18 19 #include "use_tun.h" 20 #include "opt_inet.h" 21 #include "opt_inet6.h" 22 23 #include <sys/param.h> 24 #include <sys/proc.h> 25 #include <sys/priv.h> 26 #include <sys/systm.h> 27 #include <sys/mbuf.h> 28 #include <sys/socket.h> 29 #include <sys/conf.h> 30 #include <sys/device.h> 31 #include <sys/filio.h> 32 #include <sys/sockio.h> 33 #include <sys/thread2.h> 34 #include <sys/ttycom.h> 35 #include <sys/signalvar.h> 36 #include <sys/filedesc.h> 37 #include <sys/kernel.h> 38 #include <sys/sysctl.h> 39 #include <sys/uio.h> 40 #include <sys/vnode.h> 41 #include <sys/malloc.h> 42 #include <sys/mplock2.h> 43 #include <sys/devfs.h> 44 #include <sys/queue.h> 45 46 #include <net/bpf.h> 47 #include <net/if.h> 48 #include <net/if_types.h> 49 #include <net/if_clone.h> 50 #include <net/ifq_var.h> 51 #include <net/netisr.h> 52 #include <net/route.h> 53 54 #ifdef INET 55 #include <netinet/in.h> 56 #endif 57 58 #include "if_tunvar.h" 59 #include "if_tun.h" 60 61 #if NTUN <= 1 62 #define TUN_PREALLOCATED_UNITS 4 63 #else 64 #define TUN_PREALLOCATED_UNITS NTUN 65 #endif 66 67 #define TUN "tun" 68 #define TUNDEBUG if (tundebug) if_printf 69 70 /* module */ 71 static int tunmodevent(module_t, int, void *); 72 73 /* device */ 74 static struct tun_softc *tuncreate(cdev_t, int); 75 static void tundestroy(struct tun_softc *sc); 76 77 /* clone */ 78 static int tun_clone_create(struct if_clone *, int, caddr_t); 79 static int tun_clone_destroy(struct ifnet *); 80 81 /* network interface */ 82 static int tunifinit(struct ifnet *); 83 static void tunifstart(struct ifnet *, struct ifaltq_subque *); 84 static int tunifoutput(struct ifnet *, struct mbuf *, 85 struct sockaddr *, struct rtentry *rt); 86 static int tunifioctl(struct ifnet *, u_long, 87 caddr_t, struct ucred *); 88 89 /* character device */ 90 static d_open_t tunopen; 91 static d_close_t tunclose; 92 static d_read_t tunread; 93 static d_write_t tunwrite; 94 static d_ioctl_t tunioctl; 95 static d_kqfilter_t tunkqfilter; 96 static d_clone_t tunclone; 97 98 static struct dev_ops tun_ops = { 99 { TUN, 0, 0 }, 100 .d_open = tunopen, 101 .d_close = tunclose, 102 .d_read = tunread, 103 .d_write = tunwrite, 104 .d_ioctl = tunioctl, 105 .d_kqfilter = tunkqfilter 106 }; 107 108 /* kqueue support */ 109 static void tun_filter_detach(struct knote *); 110 static int tun_filter_read(struct knote *, long); 111 static int tun_filter_write(struct knote *, long); 112 113 static struct filterops tun_read_filtops = { 114 FILTEROP_ISFD, 115 NULL, 116 tun_filter_detach, 117 tun_filter_read 118 }; 119 static struct filterops tun_write_filtops = { 120 FILTEROP_ISFD, 121 NULL, 122 tun_filter_detach, 123 tun_filter_write 124 }; 125 126 static int tundebug = 0; /* debug flag */ 127 static int tunrefcnt = 0; /* module reference counter */ 128 129 static MALLOC_DEFINE(M_TUN, TUN, "Tunnel Interface"); 130 131 static DEVFS_DEFINE_CLONE_BITMAP(tun); 132 133 struct if_clone tun_cloner = IF_CLONE_INITIALIZER( 134 TUN, tun_clone_create, tun_clone_destroy, 0, IF_MAXUNIT); 135 136 static SLIST_HEAD(,tun_softc) tun_listhead = 137 SLIST_HEAD_INITIALIZER(&tun_listhead); 138 139 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, 140 "Enable debug output"); 141 SYSCTL_DECL(_net_link); 142 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0, 143 "IP tunnel software network interface"); 144 SYSCTL_INT(_net_link_tun, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, 145 "Enable debug output"); 146 SYSCTL_INT(_net_link_tun, OID_AUTO, refcnt, CTLFLAG_RD, &tunrefcnt, 0, 147 "Number of opened devices"); 148 149 DEV_MODULE(if_tun, tunmodevent, NULL); 150 151 /* 152 * tunmodevent - module event handler 153 */ 154 static int 155 tunmodevent(module_t mod, int type, void *data) 156 { 157 static cdev_t dev = NULL; 158 struct tun_softc *sc, *sc_tmp; 159 int i; 160 161 switch (type) { 162 case MOD_LOAD: 163 dev = make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun), 164 tunclone, UID_UUCP, GID_DIALER, 165 0600, TUN); 166 167 SLIST_INIT(&tun_listhead); 168 if_clone_attach(&tun_cloner); 169 170 for (i = 0; i < TUN_PREALLOCATED_UNITS; ++i) { 171 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 172 0600, "%s%d", TUN, i); 173 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i); 174 } 175 break; 176 177 case MOD_UNLOAD: 178 if (tunrefcnt > 0) 179 return (EBUSY); 180 181 if_clone_detach(&tun_cloner); 182 183 SLIST_FOREACH_MUTABLE(sc, &tun_listhead, tun_link, sc_tmp) 184 tundestroy(sc); 185 186 dev_ops_remove_all(&tun_ops); 187 destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(tun)); 188 break; 189 190 default: 191 return (EOPNOTSUPP); 192 } 193 194 return (0); 195 } 196 197 static int 198 tunclone(struct dev_clone_args *ap) 199 { 200 int unit; 201 202 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0); 203 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 204 0600, "%s%d", TUN, unit); 205 206 if (tuncreate(ap->a_dev, 0) == NULL) 207 return (ENOMEM); 208 else 209 return (0); 210 } 211 212 static struct tun_softc * 213 tuncreate(cdev_t dev, int flags) 214 { 215 struct tun_softc *sc; 216 struct ifnet *ifp; 217 int unit = minor(dev); 218 219 sc = kmalloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO); 220 dev->si_drv1 = sc; 221 sc->tun_dev = dev; 222 sc->tun_flags = TUN_INITED; 223 sc->tun_flags |= flags; 224 225 reference_dev(dev); /* device association */ 226 227 ifp = sc->tun_ifp = if_alloc(IFT_PPP); 228 if (ifp == NULL) { 229 kprintf("%s: failed to if_alloc() interface for %s%d", 230 __func__, TUN, unit); 231 return (NULL); 232 } 233 234 if_initname(ifp, TUN, unit); 235 ifp->if_mtu = TUNMTU; 236 ifp->if_ioctl = tunifioctl; 237 ifp->if_output = tunifoutput; 238 ifp->if_start = tunifstart; 239 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 240 ifp->if_type = IFT_PPP; 241 ifp->if_softc = sc; 242 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 243 ifq_set_ready(&ifp->if_snd); 244 245 if_attach(ifp, NULL); 246 bpfattach(ifp, DLT_NULL, sizeof(u_int)); 247 248 SLIST_INSERT_HEAD(&tun_listhead, sc, tun_link); 249 TUNDEBUG(ifp, "created, minor = %#x, flags = 0x%x\n", 250 unit, sc->tun_flags); 251 return (sc); 252 } 253 254 static void 255 tundestroy(struct tun_softc *sc) 256 { 257 cdev_t dev = sc->tun_dev; 258 struct ifnet *ifp = sc->tun_ifp; 259 int unit = minor(dev); 260 261 TUNDEBUG(ifp, "destroyed, minor = %#x. Module refcnt = %d\n", 262 unit, tunrefcnt); 263 264 bpfdetach(ifp); 265 if_detach(ifp); 266 if_free(ifp); 267 268 sc->tun_dev = NULL; 269 dev->si_drv1 = NULL; 270 release_dev(dev); /* device disassociation */ 271 272 /* Also destroy the cloned device */ 273 if (unit >= TUN_PREALLOCATED_UNITS) { 274 destroy_dev(dev); 275 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), unit); 276 } 277 278 SLIST_REMOVE(&tun_listhead, sc, tun_softc, tun_link); 279 kfree(sc, M_TUN); 280 } 281 282 /* 283 * tunnel open - must be superuser & the device must be configured in 284 */ 285 static int 286 tunopen(struct dev_open_args *ap) 287 { 288 cdev_t dev = ap->a_head.a_dev; 289 struct ifnet *ifp; 290 struct tun_softc *sc; 291 int error; 292 293 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0) 294 return (error); 295 296 sc = dev->si_drv1; 297 if (sc == NULL && (sc = tuncreate(dev, TUN_MANUALMAKE)) == NULL) 298 return (ENOMEM); 299 if (sc->tun_flags & TUN_OPEN) 300 return (EBUSY); 301 302 ifp = sc->tun_ifp; 303 if ((sc->tun_flags & TUN_CLONE) == 0) { 304 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp); 305 306 /* Announce the return of the interface. */ 307 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 308 } 309 310 sc->tun_pid = curproc->p_pid; 311 sc->tun_flags |= TUN_OPEN; 312 tunrefcnt++; 313 314 TUNDEBUG(ifp, "opened, minor = %#x. Module refcnt = %d\n", 315 minor(dev), tunrefcnt); 316 return (0); 317 } 318 319 /* 320 * close the device - mark interface down & delete routing info 321 */ 322 static int 323 tunclose(struct dev_close_args *ap) 324 { 325 cdev_t dev = ap->a_head.a_dev; 326 struct tun_softc *sc = dev->si_drv1; 327 struct ifnet *ifp; 328 int unit = minor(dev); 329 330 KASSERT(sc != NULL, 331 ("try closing the already destroyed %s%d", TUN, unit)); 332 ifp = sc->tun_ifp; 333 334 sc->tun_flags &= ~TUN_OPEN; 335 sc->tun_pid = 0; 336 337 /* Junk all pending output. */ 338 ifq_purge_all(&ifp->if_snd); 339 340 if (ifp->if_flags & IFF_UP) 341 if_down(ifp); 342 ifp->if_flags &= ~IFF_RUNNING; 343 344 if ((sc->tun_flags & TUN_CLONE) == 0) { 345 if_purgeaddrs_nolink(ifp); 346 347 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp); 348 349 /* Announce the departure of the interface. */ 350 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 351 } 352 353 funsetown(&sc->tun_sigio); 354 KNOTE(&sc->tun_rkq.ki_note, 0); 355 356 tunrefcnt--; 357 if (tunrefcnt < 0) { 358 tunrefcnt = 0; 359 if_printf(ifp, ". Module refcnt = %d is out of sync! " 360 "Force refcnt to be 0.\n", tunrefcnt); 361 } 362 363 TUNDEBUG(ifp, "closed, minor = %#x. Module refcnt = %d\n", 364 unit, tunrefcnt); 365 366 /* Only auto-destroy if the interface was not manually created. */ 367 if ((sc->tun_flags & TUN_MANUALMAKE) == 0 && 368 unit >= TUN_PREALLOCATED_UNITS) { 369 tundestroy(sc); 370 dev->si_drv1 = NULL; 371 } 372 373 return (0); 374 } 375 376 377 /* 378 * Interface clone support 379 * 380 * Create and destroy tun device/interface via ifconfig(8). 381 */ 382 383 static struct tun_softc * 384 tunfind(int unit) 385 { 386 struct tun_softc *sc; 387 388 SLIST_FOREACH(sc, &tun_listhead, tun_link) { 389 if (minor(sc->tun_dev) == unit) 390 return (sc); 391 } 392 return (NULL); 393 } 394 395 static int 396 tun_clone_create(struct if_clone *ifc __unused, int unit, 397 caddr_t param __unused) 398 { 399 struct tun_softc *sc; 400 cdev_t dev; 401 402 sc = tunfind(unit); 403 if (sc == NULL) { 404 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tun), unit)) { 405 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), unit); 406 dev = make_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 407 0600, "%s%d", TUN, unit); 408 } else { 409 dev = devfs_find_device_by_name("%s%d", TUN, unit); 410 } 411 412 if (dev == NULL) 413 return (ENODEV); 414 if ((sc = tuncreate(dev, TUN_MANUALMAKE)) == NULL) 415 return (ENOMEM); 416 } 417 418 sc->tun_flags |= TUN_CLONE; 419 TUNDEBUG(sc->tun_ifp, "clone created, minor = %#x, flags = 0x%x\n", 420 minor(sc->tun_dev), sc->tun_flags); 421 422 return (0); 423 } 424 425 static int 426 tun_clone_destroy(struct ifnet * ifp) 427 { 428 struct tun_softc *sc = ifp->if_softc; 429 430 if (sc->tun_flags & TUN_OPEN) 431 return (EBUSY); 432 if ((sc->tun_flags & TUN_CLONE) == 0) 433 return (EINVAL); 434 435 TUNDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n", 436 minor(sc->tun_dev), sc->tun_flags); 437 tundestroy(sc); 438 439 return (0); 440 } 441 442 443 /* 444 * Network interface functions 445 */ 446 447 static int 448 tunifinit(struct ifnet *ifp) 449 { 450 #ifdef INET 451 struct tun_softc *sc = ifp->if_softc; 452 #endif 453 struct ifaddr_container *ifac; 454 int error = 0; 455 456 TUNDEBUG(ifp, "initialize\n"); 457 458 ifp->if_flags |= IFF_UP | IFF_RUNNING; 459 getmicrotime(&ifp->if_lastchange); 460 461 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { 462 struct ifaddr *ifa = ifac->ifa; 463 464 if (ifa->ifa_addr == NULL) { 465 error = EFAULT; 466 /* XXX: Should maybe return straight off? */ 467 } else { 468 #ifdef INET 469 if (ifa->ifa_addr->sa_family == AF_INET) { 470 struct sockaddr_in *si; 471 472 si = (struct sockaddr_in *)ifa->ifa_addr; 473 if (si->sin_addr.s_addr) 474 sc->tun_flags |= TUN_IASET; 475 } 476 #endif 477 } 478 } 479 return (error); 480 } 481 482 /* 483 * Process an ioctl request. 484 * 485 * MPSAFE 486 */ 487 static int 488 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 489 { 490 struct ifreq *ifr = (struct ifreq *)data; 491 struct tun_softc *sc = ifp->if_softc; 492 struct ifstat *ifs; 493 int error = 0; 494 495 switch (cmd) { 496 case SIOCGIFSTATUS: 497 ifs = (struct ifstat *)data; 498 if (sc->tun_pid) 499 ksprintf(ifs->ascii + strlen(ifs->ascii), 500 "\tOpened by PID %d\n", sc->tun_pid); 501 break; 502 case SIOCSIFADDR: 503 error = tunifinit(ifp); 504 TUNDEBUG(ifp, "address set, error=%d\n", error); 505 break; 506 case SIOCSIFDSTADDR: 507 error = tunifinit(ifp); 508 TUNDEBUG(ifp, "destination address set, error=%d\n", error); 509 break; 510 case SIOCSIFMTU: 511 ifp->if_mtu = ifr->ifr_mtu; 512 TUNDEBUG(ifp, "mtu set\n"); 513 break; 514 case SIOCSIFFLAGS: 515 case SIOCADDMULTI: 516 case SIOCDELMULTI: 517 break; 518 default: 519 error = EINVAL; 520 } 521 return (error); 522 } 523 524 /* 525 * Start packet transmission on the interface. 526 * when the interface queue is rate-limited by ALTQ, 527 * if_start is needed to drain packets from the queue in order 528 * to notify readers when outgoing packets become ready. 529 */ 530 static void 531 tunifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq) 532 { 533 struct tun_softc *sc = ifp->if_softc; 534 struct mbuf *m; 535 536 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq); 537 538 if (!ifq_is_enabled(&ifp->if_snd)) 539 return; 540 541 m = ifsq_poll(ifsq); 542 if (m != NULL) { 543 if (sc->tun_flags & TUN_RWAIT) { 544 sc->tun_flags &= ~TUN_RWAIT; 545 wakeup((caddr_t)sc); 546 } 547 if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio) 548 pgsigio(sc->tun_sigio, SIGIO, 0); 549 ifsq_deserialize_hw(ifsq); 550 KNOTE(&sc->tun_rkq.ki_note, 0); 551 ifsq_serialize_hw(ifsq); 552 } 553 } 554 555 /* 556 * tunifoutput - queue packets from higher level ready to put out. 557 * 558 * MPSAFE 559 */ 560 static int 561 tunifoutput_serialized(struct ifnet *ifp, struct mbuf *m0, 562 struct sockaddr *dst, struct rtentry *rt) 563 { 564 struct tun_softc *sc = ifp->if_softc; 565 int error; 566 struct altq_pktattr pktattr; 567 568 TUNDEBUG(ifp, "output\n"); 569 570 if ((sc->tun_flags & TUN_READY) != TUN_READY) { 571 TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags); 572 m_freem (m0); 573 return (EHOSTDOWN); 574 } 575 576 /* 577 * if the queueing discipline needs packet classification, 578 * do it before prepending link headers. 579 */ 580 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr); 581 582 /* BPF write needs to be handled specially */ 583 if (dst->sa_family == AF_UNSPEC) { 584 dst->sa_family = *(mtod(m0, int *)); 585 m0->m_len -= sizeof(int); 586 m0->m_pkthdr.len -= sizeof(int); 587 m0->m_data += sizeof(int); 588 } 589 590 if (ifp->if_bpf) { 591 bpf_gettoken(); 592 if (ifp->if_bpf) { 593 /* 594 * We need to prepend the address family as 595 * a four byte field. 596 */ 597 uint32_t af = dst->sa_family; 598 599 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af)); 600 } 601 bpf_reltoken(); 602 } 603 604 /* prepend sockaddr? this may abort if the mbuf allocation fails */ 605 if (sc->tun_flags & TUN_LMODE) { 606 /* allocate space for sockaddr */ 607 M_PREPEND(m0, dst->sa_len, M_NOWAIT); 608 609 /* if allocation failed drop packet */ 610 if (m0 == NULL){ 611 IFNET_STAT_INC(ifp, oerrors, 1); 612 return (ENOBUFS); 613 } else { 614 bcopy(dst, m0->m_data, dst->sa_len); 615 } 616 } 617 618 if (sc->tun_flags & TUN_IFHEAD) { 619 /* Prepend the address family */ 620 M_PREPEND(m0, 4, M_NOWAIT); 621 622 /* if allocation failed drop packet */ 623 if (m0 == NULL){ 624 IFNET_STAT_INC(ifp, oerrors, 1); 625 return (ENOBUFS); 626 } else { 627 *(u_int32_t *)m0->m_data = htonl(dst->sa_family); 628 } 629 } else { 630 #ifdef INET 631 if (dst->sa_family != AF_INET) 632 #endif 633 { 634 m_freem(m0); 635 return (EAFNOSUPPORT); 636 } 637 } 638 639 error = ifq_handoff(ifp, m0, &pktattr); 640 if (error) { 641 IFNET_STAT_INC(ifp, collisions, 1); 642 } else { 643 IFNET_STAT_INC(ifp, opackets, 1); 644 if (sc->tun_flags & TUN_RWAIT) { 645 sc->tun_flags &= ~TUN_RWAIT; 646 wakeup((caddr_t)sc); 647 } 648 get_mplock(); 649 if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio) 650 pgsigio(sc->tun_sigio, SIGIO, 0); 651 rel_mplock(); 652 ifnet_deserialize_all(ifp); 653 KNOTE(&sc->tun_rkq.ki_note, 0); 654 ifnet_serialize_all(ifp); 655 } 656 return (error); 657 } 658 659 static int 660 tunifoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst, 661 struct rtentry *rt) 662 { 663 int error; 664 665 ifnet_serialize_all(ifp); 666 error = tunifoutput_serialized(ifp, m0, dst, rt); 667 ifnet_deserialize_all(ifp); 668 669 return (error); 670 } 671 672 673 /* 674 * the ops interface is now pretty minimal. 675 */ 676 static int 677 tunioctl(struct dev_ioctl_args *ap) 678 { 679 cdev_t dev = ap->a_head.a_dev; 680 caddr_t data = ap->a_data; 681 struct tun_softc *sc = dev->si_drv1; 682 struct ifnet *ifp = sc->tun_ifp; 683 struct ifreq *ifr; 684 struct tuninfo *tunp; 685 int error = 0; 686 687 switch (ap->a_cmd) { 688 case TUNSIFINFO: 689 tunp = (struct tuninfo *)data; 690 if (ifp->if_type != tunp->type) 691 return (EPROTOTYPE); 692 if (tunp->mtu < IF_MINMTU) 693 return (EINVAL); 694 ifp->if_mtu = tunp->mtu; 695 ifp->if_baudrate = tunp->baudrate; 696 break; 697 698 case TUNGIFINFO: 699 tunp = (struct tuninfo *)data; 700 tunp->mtu = ifp->if_mtu; 701 tunp->type = ifp->if_type; 702 tunp->baudrate = ifp->if_baudrate; 703 break; 704 705 case TUNGIFNAME: 706 ifr = (struct ifreq *)data; 707 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ); 708 break; 709 710 case TUNSDEBUG: 711 tundebug = *(int *)data; 712 break; 713 714 case TUNGDEBUG: 715 *(int *)data = tundebug; 716 break; 717 718 case TUNSLMODE: 719 if (*(int *)data) { 720 sc->tun_flags |= TUN_LMODE; 721 sc->tun_flags &= ~TUN_IFHEAD; 722 } else { 723 sc->tun_flags &= ~TUN_LMODE; 724 } 725 break; 726 727 case TUNSIFHEAD: 728 if (*(int *)data) { 729 sc->tun_flags |= TUN_IFHEAD; 730 sc->tun_flags &= ~TUN_LMODE; 731 } else { 732 sc->tun_flags &= ~TUN_IFHEAD; 733 } 734 break; 735 736 case TUNGIFHEAD: 737 *(int *)data = (sc->tun_flags & TUN_IFHEAD) ? 1 : 0; 738 break; 739 740 case TUNSIFMODE: 741 /* deny this if UP */ 742 if (ifp->if_flags & IFF_UP) 743 return (EBUSY); 744 745 switch (*(int *)data & ~IFF_MULTICAST) { 746 case IFF_POINTOPOINT: 747 case IFF_BROADCAST: 748 ifp->if_flags &= ~(IFF_BROADCAST | IFF_POINTOPOINT); 749 ifp->if_flags |= *(int *)data; 750 break; 751 default: 752 return (EINVAL); 753 } 754 break; 755 756 case TUNSIFPID: 757 sc->tun_pid = curproc->p_pid; 758 break; 759 760 case FIOASYNC: 761 if (*(int *)data) 762 sc->tun_flags |= TUN_ASYNC; 763 else 764 sc->tun_flags &= ~TUN_ASYNC; 765 break; 766 767 case FIONREAD: 768 *(int *)data = ifsq_poll_pktlen( 769 ifq_get_subq_default(&ifp->if_snd)); 770 break; 771 772 case FIOSETOWN: 773 error = fsetown(*(int *)data, &sc->tun_sigio); 774 break; 775 776 case FIOGETOWN: 777 *(int *)data = fgetown(&sc->tun_sigio); 778 break; 779 780 /* This is deprecated, FIOSETOWN should be used instead. */ 781 case TIOCSPGRP: 782 error = fsetown(-(*(int *)data), &sc->tun_sigio); 783 break; 784 785 /* This is deprecated, FIOGETOWN should be used instead. */ 786 case TIOCGPGRP: 787 *(int *)data = -fgetown(&sc->tun_sigio); 788 break; 789 790 default: 791 error = ENOTTY; 792 break; 793 } 794 795 return (error); 796 } 797 798 /* 799 * The ops read interface - reads a packet at a time, or at 800 * least as much of a packet as can be read. 801 */ 802 static int 803 tunread(struct dev_read_args *ap) 804 { 805 cdev_t dev = ap->a_head.a_dev; 806 struct uio *uio = ap->a_uio; 807 struct tun_softc *sc = dev->si_drv1; 808 struct ifnet *ifp = sc->tun_ifp; 809 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd); 810 struct mbuf *m0; 811 int error=0, len; 812 813 TUNDEBUG(ifp, "read\n"); 814 if ((sc->tun_flags & TUN_READY) != TUN_READY) { 815 TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags); 816 return (EHOSTDOWN); 817 } 818 819 sc->tun_flags &= ~TUN_RWAIT; 820 821 ifnet_serialize_all(ifp); 822 823 while ((m0 = ifsq_dequeue(ifsq)) == NULL) { 824 if (ap->a_ioflag & IO_NDELAY) { 825 ifnet_deserialize_all(ifp); 826 return (EWOULDBLOCK); 827 } 828 sc->tun_flags |= TUN_RWAIT; 829 ifnet_deserialize_all(ifp); 830 if ((error = tsleep(sc, PCATCH, "tunread", 0)) != 0) 831 return (error); 832 ifnet_serialize_all(ifp); 833 } 834 835 ifnet_deserialize_all(ifp); 836 837 while (m0 && uio->uio_resid > 0 && error == 0) { 838 len = (int)szmin(uio->uio_resid, m0->m_len); 839 if (len != 0) 840 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio); 841 m0 = m_free(m0); 842 } 843 844 if (m0) { 845 TUNDEBUG(ifp, "dropping mbuf\n"); 846 m_freem(m0); 847 } 848 return (error); 849 } 850 851 /* 852 * the ops write interface - an atomic write is a packet - or else! 853 */ 854 static int 855 tunwrite(struct dev_write_args *ap) 856 { 857 cdev_t dev = ap->a_head.a_dev; 858 struct uio *uio = ap->a_uio; 859 struct tun_softc *sc = dev->si_drv1; 860 struct ifnet *ifp = sc->tun_ifp; 861 struct mbuf *top, **mp, *m; 862 size_t tlen; 863 uint32_t family, mru; 864 int error = 0; 865 int isr; 866 867 TUNDEBUG(ifp, "tunwrite\n"); 868 869 if (uio->uio_resid == 0) 870 return (0); 871 872 mru = TUNMRU; 873 if (sc->tun_flags & TUN_IFHEAD) 874 mru += sizeof(family); 875 if (uio->uio_resid > mru) { 876 TUNDEBUG(ifp, "len = %zd!\n", uio->uio_resid); 877 return (EIO); 878 } 879 880 /* get a header mbuf */ 881 MGETHDR(m, M_WAITOK, MT_DATA); 882 if (m == NULL) 883 return (ENOBUFS); 884 885 tlen = uio->uio_resid; 886 top = NULL; 887 mp = ⊤ 888 while (error == 0 && uio->uio_resid > 0) { 889 m->m_len = (int)szmin(MHLEN, uio->uio_resid); 890 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio); 891 *mp = m; 892 mp = &m->m_next; 893 if (uio->uio_resid > 0) { 894 MGET(m, M_WAITOK, MT_DATA); 895 if (m == NULL) { 896 error = ENOBUFS; 897 break; 898 } 899 } 900 } 901 if (error) { 902 if (top) 903 m_freem(top); 904 IFNET_STAT_INC(ifp, ierrors, 1); 905 return (error); 906 } 907 908 top->m_pkthdr.len = (int)tlen; 909 top->m_pkthdr.rcvif = ifp; 910 911 if (ifp->if_bpf) { 912 bpf_gettoken(); 913 914 if (ifp->if_bpf) { 915 if (sc->tun_flags & TUN_IFHEAD) { 916 /* 917 * Conveniently, we already have a 4-byte 918 * address family prepended to our packet ! 919 * Inconveniently, it's in the wrong byte 920 * order ! 921 */ 922 if ((top = m_pullup(top, sizeof(family))) 923 == NULL) { 924 bpf_reltoken(); 925 return (ENOBUFS); 926 } 927 *mtod(top, u_int32_t *) = 928 ntohl(*mtod(top, u_int32_t *)); 929 bpf_mtap(ifp->if_bpf, top); 930 *mtod(top, u_int32_t *) = 931 htonl(*mtod(top, u_int32_t *)); 932 } else { 933 /* 934 * We need to prepend the address family as 935 * a four byte field. 936 */ 937 static const uint32_t af = AF_INET; 938 939 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af)); 940 } 941 } 942 943 bpf_reltoken(); 944 } 945 946 if (sc->tun_flags & TUN_IFHEAD) { 947 if (top->m_len < sizeof(family) && 948 (top = m_pullup(top, sizeof(family))) == NULL) 949 return (ENOBUFS); 950 family = ntohl(*mtod(top, u_int32_t *)); 951 m_adj(top, sizeof(family)); 952 } else { 953 family = AF_INET; 954 } 955 956 IFNET_STAT_INC(ifp, ibytes, top->m_pkthdr.len); 957 IFNET_STAT_INC(ifp, ipackets, 1); 958 959 switch (family) { 960 #ifdef INET 961 case AF_INET: 962 isr = NETISR_IP; 963 break; 964 #endif 965 #ifdef INET6 966 case AF_INET6: 967 isr = NETISR_IPV6; 968 break; 969 #endif 970 default: 971 m_freem(m); 972 return (EAFNOSUPPORT); 973 } 974 975 netisr_queue(isr, top); 976 return (0); 977 } 978 979 980 /* 981 * tunkqfilter - support for the kevent() system call. 982 */ 983 static int 984 tunkqfilter(struct dev_kqfilter_args *ap) 985 { 986 cdev_t dev = ap->a_head.a_dev; 987 struct tun_softc *sc = dev->si_drv1; 988 struct ifnet *ifp = sc->tun_ifp; 989 struct knote *kn = ap->a_kn; 990 struct klist *klist; 991 992 ap->a_result = 0; 993 ifnet_serialize_all(ifp); 994 995 switch (kn->kn_filter) { 996 case EVFILT_READ: 997 kn->kn_fop = &tun_read_filtops; 998 kn->kn_hook = (caddr_t)sc; 999 break; 1000 case EVFILT_WRITE: 1001 kn->kn_fop = &tun_write_filtops; 1002 kn->kn_hook = (caddr_t)sc; 1003 break; 1004 default: 1005 ifnet_deserialize_all(ifp); 1006 ap->a_result = EOPNOTSUPP; 1007 return (0); 1008 } 1009 1010 klist = &sc->tun_rkq.ki_note; 1011 knote_insert(klist, kn); 1012 ifnet_deserialize_all(ifp); 1013 1014 return (0); 1015 } 1016 1017 static void 1018 tun_filter_detach(struct knote *kn) 1019 { 1020 struct tun_softc *sc = (struct tun_softc *)kn->kn_hook; 1021 struct klist *klist = &sc->tun_rkq.ki_note; 1022 1023 knote_remove(klist, kn); 1024 } 1025 1026 static int 1027 tun_filter_write(struct knote *kn, long hint) 1028 { 1029 /* Always ready for a write */ 1030 return (1); 1031 } 1032 1033 static int 1034 tun_filter_read(struct knote *kn, long hint) 1035 { 1036 struct tun_softc *sc = (struct tun_softc *)kn->kn_hook; 1037 struct ifnet *ifp = sc->tun_ifp; 1038 int ready = 0; 1039 1040 ifnet_serialize_all(ifp); 1041 if (!ifsq_is_empty(ifq_get_subq_default(&ifp->if_snd))) 1042 ready = 1; 1043 ifnet_deserialize_all(ifp); 1044 1045 return (ready); 1046 } 1047