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