The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_tun.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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$
   17  */
   18 
   19 #include "opt_atalk.h"
   20 #include "opt_inet.h"
   21 #include "opt_inet6.h"
   22 #include "opt_ipx.h"
   23 
   24 #include <sys/param.h>
   25 #include <sys/priv.h>
   26 #include <sys/proc.h>
   27 #include <sys/systm.h>
   28 #include <sys/jail.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_clone.h>
   49 #include <net/if_types.h>
   50 #include <net/netisr.h>
   51 #include <net/route.h>
   52 #include <net/vnet.h>
   53 #ifdef INET
   54 #include <netinet/in.h>
   55 #endif
   56 #include <net/bpf.h>
   57 #include <net/if_tun.h>
   58 
   59 #include <sys/queue.h>
   60 #include <sys/condvar.h>
   61 
   62 #include <security/mac/mac_framework.h>
   63 
   64 /*
   65  * tun_list is protected by global tunmtx.  Other mutable fields are
   66  * protected by tun->tun_mtx, or by their owning subsystem.  tun_dev is
   67  * static for the duration of a tunnel interface.
   68  */
   69 struct tun_softc {
   70         TAILQ_ENTRY(tun_softc)  tun_list;
   71         struct cdev *tun_dev;
   72         u_short tun_flags;              /* misc flags */
   73 #define TUN_OPEN        0x0001
   74 #define TUN_INITED      0x0002
   75 #define TUN_RCOLL       0x0004
   76 #define TUN_IASET       0x0008
   77 #define TUN_DSTADDR     0x0010
   78 #define TUN_LMODE       0x0020
   79 #define TUN_RWAIT       0x0040
   80 #define TUN_ASYNC       0x0080
   81 #define TUN_IFHEAD      0x0100
   82 
   83 #define TUN_READY       (TUN_OPEN | TUN_INITED)
   84 
   85         /*
   86          * XXXRW: tun_pid is used to exclusively lock /dev/tun.  Is this
   87          * actually needed?  Can we just return EBUSY if already open?
   88          * Problem is that this involved inherent races when a tun device
   89          * is handed off from one process to another, as opposed to just
   90          * being slightly stale informationally.
   91          */
   92         pid_t   tun_pid;                /* owning pid */
   93         struct  ifnet *tun_ifp;         /* the interface */
   94         struct  sigio *tun_sigio;       /* information for async I/O */
   95         struct  selinfo tun_rsel;       /* read select */
   96         struct mtx      tun_mtx;        /* protect mutable softc fields */
   97         struct cv       tun_cv;         /* protect against ref'd dev destroy */
   98 };
   99 #define TUN2IFP(sc)     ((sc)->tun_ifp)
  100 
  101 #define TUNDEBUG        if (tundebug) if_printf
  102 #define TUNNAME         "tun"
  103 
  104 /*
  105  * All mutable global variables in if_tun are locked using tunmtx, with
  106  * the exception of tundebug, which is used unlocked, and tunclones,
  107  * which is static after setup.
  108  */
  109 static struct mtx tunmtx;
  110 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
  111 static int tundebug = 0;
  112 static int tundclone = 1;
  113 static struct clonedevs *tunclones;
  114 static TAILQ_HEAD(,tun_softc)   tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
  115 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
  116 
  117 SYSCTL_DECL(_net_link);
  118 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
  119     "IP tunnel software network interface.");
  120 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
  121     "Enable legacy devfs interface creation.");
  122 
  123 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
  124 
  125 static void     tunclone(void *arg, struct ucred *cred, char *name,
  126                     int namelen, struct cdev **dev);
  127 static void     tuncreate(const char *name, struct cdev *dev);
  128 static int      tunifioctl(struct ifnet *, u_long, caddr_t);
  129 static void     tuninit(struct ifnet *);
  130 static int      tunmodevent(module_t, int, void *);
  131 static int      tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
  132                     struct route *ro);
  133 static void     tunstart(struct ifnet *);
  134 
  135 static int      tun_clone_create(struct if_clone *, int, caddr_t);
  136 static void     tun_clone_destroy(struct ifnet *);
  137 
  138 IFC_SIMPLE_DECLARE(tun, 0);
  139 
  140 static d_open_t         tunopen;
  141 static d_close_t        tunclose;
  142 static d_read_t         tunread;
  143 static d_write_t        tunwrite;
  144 static d_ioctl_t        tunioctl;
  145 static d_poll_t         tunpoll;
  146 static d_kqfilter_t     tunkqfilter;
  147 
  148 static int              tunkqread(struct knote *, long);
  149 static int              tunkqwrite(struct knote *, long);
  150 static void             tunkqdetach(struct knote *);
  151 
  152 static struct filterops tun_read_filterops = {
  153         .f_isfd =       1,
  154         .f_attach =     NULL,
  155         .f_detach =     tunkqdetach,
  156         .f_event =      tunkqread,
  157 };
  158 
  159 static struct filterops tun_write_filterops = {
  160         .f_isfd =       1,
  161         .f_attach =     NULL,
  162         .f_detach =     tunkqdetach,
  163         .f_event =      tunkqwrite,
  164 };
  165 
  166 static struct cdevsw tun_cdevsw = {
  167         .d_version =    D_VERSION,
  168         .d_flags =      D_PSEUDO | D_NEEDMINOR,
  169         .d_open =       tunopen,
  170         .d_close =      tunclose,
  171         .d_read =       tunread,
  172         .d_write =      tunwrite,
  173         .d_ioctl =      tunioctl,
  174         .d_poll =       tunpoll,
  175         .d_kqfilter =   tunkqfilter,
  176         .d_name =       TUNNAME,
  177 };
  178 
  179 static int
  180 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
  181 {
  182         struct cdev *dev;
  183         int i;
  184 
  185         /* find any existing device, or allocate new unit number */
  186         i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
  187         if (i) {
  188                 /* No preexisting struct cdev *, create one */
  189                 dev = make_dev(&tun_cdevsw, unit,
  190                     UID_UUCP, GID_DIALER, 0600, "%s%d", ifc->ifc_name, unit);
  191         }
  192         tuncreate(ifc->ifc_name, dev);
  193 
  194         return (0);
  195 }
  196 
  197 static void
  198 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
  199     struct cdev **dev)
  200 {
  201         char devname[SPECNAMELEN + 1];
  202         int u, i, append_unit;
  203 
  204         if (*dev != NULL)
  205                 return;
  206 
  207         /*
  208          * If tun cloning is enabled, only the superuser can create an
  209          * interface.
  210          */
  211         if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
  212                 return;
  213 
  214         if (strcmp(name, TUNNAME) == 0) {
  215                 u = -1;
  216         } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
  217                 return; /* Don't recognise the name */
  218         if (u != -1 && u > IF_MAXUNIT)
  219                 return; /* Unit number too high */
  220 
  221         if (u == -1)
  222                 append_unit = 1;
  223         else
  224                 append_unit = 0;
  225 
  226         CURVNET_SET(CRED_TO_VNET(cred));
  227         /* find any existing device, or allocate new unit number */
  228         i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
  229         if (i) {
  230                 if (append_unit) {
  231                         namelen = snprintf(devname, sizeof(devname), "%s%d",
  232                             name, u);
  233                         name = devname;
  234                 }
  235                 /* No preexisting struct cdev *, create one */
  236                 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
  237                     UID_UUCP, GID_DIALER, 0600, "%s", name);
  238         }
  239 
  240         if_clone_create(name, namelen, NULL);
  241         CURVNET_RESTORE();
  242 }
  243 
  244 static void
  245 tun_destroy(struct tun_softc *tp)
  246 {
  247         struct cdev *dev;
  248 
  249         /* Unlocked read. */
  250         mtx_lock(&tp->tun_mtx);
  251         if ((tp->tun_flags & TUN_OPEN) != 0)
  252                 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
  253         else
  254                 mtx_unlock(&tp->tun_mtx);
  255 
  256         CURVNET_SET(TUN2IFP(tp)->if_vnet);
  257         dev = tp->tun_dev;
  258         bpfdetach(TUN2IFP(tp));
  259         if_detach(TUN2IFP(tp));
  260         if_free(TUN2IFP(tp));
  261         destroy_dev(dev);
  262         seldrain(&tp->tun_rsel);
  263         knlist_destroy(&tp->tun_rsel.si_note);
  264         mtx_destroy(&tp->tun_mtx);
  265         cv_destroy(&tp->tun_cv);
  266         free(tp, M_TUN);
  267         CURVNET_RESTORE();
  268 }
  269 
  270 static void
  271 tun_clone_destroy(struct ifnet *ifp)
  272 {
  273         struct tun_softc *tp = ifp->if_softc;
  274 
  275         mtx_lock(&tunmtx);
  276         TAILQ_REMOVE(&tunhead, tp, tun_list);
  277         mtx_unlock(&tunmtx);
  278         tun_destroy(tp);
  279 }
  280 
  281 static int
  282 tunmodevent(module_t mod, int type, void *data)
  283 {
  284         static eventhandler_tag tag;
  285         struct tun_softc *tp;
  286 
  287         switch (type) {
  288         case MOD_LOAD:
  289                 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
  290                 clone_setup(&tunclones);
  291                 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
  292                 if (tag == NULL)
  293                         return (ENOMEM);
  294                 if_clone_attach(&tun_cloner);
  295                 break;
  296         case MOD_UNLOAD:
  297                 if_clone_detach(&tun_cloner);
  298                 EVENTHANDLER_DEREGISTER(dev_clone, tag);
  299                 drain_dev_clone_events();
  300 
  301                 mtx_lock(&tunmtx);
  302                 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
  303                         TAILQ_REMOVE(&tunhead, tp, tun_list);
  304                         mtx_unlock(&tunmtx);
  305                         tun_destroy(tp);
  306                         mtx_lock(&tunmtx);
  307                 }
  308                 mtx_unlock(&tunmtx);
  309                 clone_cleanup(&tunclones);
  310                 mtx_destroy(&tunmtx);
  311                 break;
  312         default:
  313                 return EOPNOTSUPP;
  314         }
  315         return 0;
  316 }
  317 
  318 static moduledata_t tun_mod = {
  319         "if_tun",
  320         tunmodevent,
  321         0
  322 };
  323 
  324 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  325 MODULE_VERSION(if_tun, 1);
  326 
  327 static void
  328 tunstart(struct ifnet *ifp)
  329 {
  330         struct tun_softc *tp = ifp->if_softc;
  331         struct mbuf *m;
  332 
  333         TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
  334         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
  335                 IFQ_LOCK(&ifp->if_snd);
  336                 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
  337                 if (m == NULL) {
  338                         IFQ_UNLOCK(&ifp->if_snd);
  339                         return;
  340                 }
  341                 IFQ_UNLOCK(&ifp->if_snd);
  342         }
  343 
  344         mtx_lock(&tp->tun_mtx);
  345         if (tp->tun_flags & TUN_RWAIT) {
  346                 tp->tun_flags &= ~TUN_RWAIT;
  347                 wakeup(tp);
  348         }
  349         selwakeuppri(&tp->tun_rsel, PZERO + 1);
  350         KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
  351         if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
  352                 mtx_unlock(&tp->tun_mtx);
  353                 pgsigio(&tp->tun_sigio, SIGIO, 0);
  354         } else
  355                 mtx_unlock(&tp->tun_mtx);
  356 }
  357 
  358 /* XXX: should return an error code so it can fail. */
  359 static void
  360 tuncreate(const char *name, struct cdev *dev)
  361 {
  362         struct tun_softc *sc;
  363         struct ifnet *ifp;
  364 
  365         dev->si_flags &= ~SI_CHEAPCLONE;
  366 
  367         sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
  368         mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
  369         cv_init(&sc->tun_cv, "tun_condvar");
  370         sc->tun_flags = TUN_INITED;
  371         sc->tun_dev = dev;
  372         mtx_lock(&tunmtx);
  373         TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
  374         mtx_unlock(&tunmtx);
  375 
  376         ifp = sc->tun_ifp = if_alloc(IFT_PPP);
  377         if (ifp == NULL)
  378                 panic("%s%d: failed to if_alloc() interface.\n",
  379                     name, dev2unit(dev));
  380         if_initname(ifp, name, dev2unit(dev));
  381         ifp->if_mtu = TUNMTU;
  382         ifp->if_ioctl = tunifioctl;
  383         ifp->if_output = tunoutput;
  384         ifp->if_start = tunstart;
  385         ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
  386         ifp->if_softc = sc;
  387         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  388         ifp->if_snd.ifq_drv_maxlen = 0;
  389         IFQ_SET_READY(&ifp->if_snd);
  390         knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
  391         ifp->if_capabilities |= IFCAP_LINKSTATE;
  392         ifp->if_capenable |= IFCAP_LINKSTATE;
  393 
  394         if_attach(ifp);
  395         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  396         dev->si_drv1 = sc;
  397         TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
  398             ifp->if_xname, dev2unit(dev));
  399 }
  400 
  401 static int
  402 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
  403 {
  404         struct ifnet    *ifp;
  405         struct tun_softc *tp;
  406 
  407         /*
  408          * XXXRW: Non-atomic test and set of dev->si_drv1 requires
  409          * synchronization.
  410          */
  411         tp = dev->si_drv1;
  412         if (!tp) {
  413                 tuncreate(TUNNAME, dev);
  414                 tp = dev->si_drv1;
  415         }
  416 
  417         /*
  418          * XXXRW: This use of tun_pid is subject to error due to the
  419          * fact that a reference to the tunnel can live beyond the
  420          * death of the process that created it.  Can we replace this
  421          * with a simple busy flag?
  422          */
  423         mtx_lock(&tp->tun_mtx);
  424         if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
  425                 mtx_unlock(&tp->tun_mtx);
  426                 return (EBUSY);
  427         }
  428         tp->tun_pid = td->td_proc->p_pid;
  429 
  430         tp->tun_flags |= TUN_OPEN;
  431         ifp = TUN2IFP(tp);
  432         if_link_state_change(ifp, LINK_STATE_UP);
  433         TUNDEBUG(ifp, "open\n");
  434         mtx_unlock(&tp->tun_mtx);
  435 
  436         return (0);
  437 }
  438 
  439 /*
  440  * tunclose - close the device - mark i/f down & delete
  441  * routing info
  442  */
  443 static  int
  444 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
  445 {
  446         struct tun_softc *tp;
  447         struct ifnet *ifp;
  448 
  449         tp = dev->si_drv1;
  450         ifp = TUN2IFP(tp);
  451 
  452         mtx_lock(&tp->tun_mtx);
  453         tp->tun_flags &= ~TUN_OPEN;
  454         tp->tun_pid = 0;
  455 
  456         /*
  457          * junk all pending output
  458          */
  459         CURVNET_SET(ifp->if_vnet);
  460         IFQ_PURGE(&ifp->if_snd);
  461 
  462         if (ifp->if_flags & IFF_UP) {
  463                 mtx_unlock(&tp->tun_mtx);
  464                 if_down(ifp);
  465                 mtx_lock(&tp->tun_mtx);
  466         }
  467 
  468         /* Delete all addresses and routes which reference this interface. */
  469         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
  470                 struct ifaddr *ifa;
  471 
  472                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  473                 mtx_unlock(&tp->tun_mtx);
  474                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  475                         /* deal w/IPv4 PtP destination; unlocked read */
  476                         if (ifa->ifa_addr->sa_family == AF_INET) {
  477                                 rtinit(ifa, (int)RTM_DELETE,
  478                                     tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
  479                         } else {
  480                                 rtinit(ifa, (int)RTM_DELETE, 0);
  481                         }
  482                 }
  483                 if_purgeaddrs(ifp);
  484                 mtx_lock(&tp->tun_mtx);
  485         }
  486         if_link_state_change(ifp, LINK_STATE_DOWN);
  487         CURVNET_RESTORE();
  488 
  489         funsetown(&tp->tun_sigio);
  490         selwakeuppri(&tp->tun_rsel, PZERO + 1);
  491         KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
  492         TUNDEBUG (ifp, "closed\n");
  493 
  494         cv_broadcast(&tp->tun_cv);
  495         mtx_unlock(&tp->tun_mtx);
  496         return (0);
  497 }
  498 
  499 static void
  500 tuninit(struct ifnet *ifp)
  501 {
  502         struct tun_softc *tp = ifp->if_softc;
  503 #ifdef INET
  504         struct ifaddr *ifa;
  505 #endif
  506 
  507         TUNDEBUG(ifp, "tuninit\n");
  508 
  509         mtx_lock(&tp->tun_mtx);
  510         ifp->if_flags |= IFF_UP;
  511         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  512         getmicrotime(&ifp->if_lastchange);
  513 
  514 #ifdef INET
  515         if_addr_rlock(ifp);
  516         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  517                 if (ifa->ifa_addr->sa_family == AF_INET) {
  518                         struct sockaddr_in *si;
  519 
  520                         si = (struct sockaddr_in *)ifa->ifa_addr;
  521                         if (si->sin_addr.s_addr)
  522                                 tp->tun_flags |= TUN_IASET;
  523 
  524                         si = (struct sockaddr_in *)ifa->ifa_dstaddr;
  525                         if (si && si->sin_addr.s_addr)
  526                                 tp->tun_flags |= TUN_DSTADDR;
  527                 }
  528         }
  529         if_addr_runlock(ifp);
  530 #endif
  531         mtx_unlock(&tp->tun_mtx);
  532 }
  533 
  534 /*
  535  * Process an ioctl request.
  536  */
  537 static int
  538 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  539 {
  540         struct ifreq *ifr = (struct ifreq *)data;
  541         struct tun_softc *tp = ifp->if_softc;
  542         struct ifstat *ifs;
  543         int             error = 0;
  544 
  545         switch(cmd) {
  546         case SIOCGIFSTATUS:
  547                 ifs = (struct ifstat *)data;
  548                 mtx_lock(&tp->tun_mtx);
  549                 if (tp->tun_pid)
  550                         sprintf(ifs->ascii + strlen(ifs->ascii),
  551                             "\tOpened by PID %d\n", tp->tun_pid);
  552                 mtx_unlock(&tp->tun_mtx);
  553                 break;
  554         case SIOCSIFADDR:
  555                 tuninit(ifp);
  556                 TUNDEBUG(ifp, "address set\n");
  557                 break;
  558         case SIOCSIFDSTADDR:
  559                 tuninit(ifp);
  560                 TUNDEBUG(ifp, "destination address set\n");
  561                 break;
  562         case SIOCSIFMTU:
  563                 ifp->if_mtu = ifr->ifr_mtu;
  564                 TUNDEBUG(ifp, "mtu set\n");
  565                 break;
  566         case SIOCSIFFLAGS:
  567         case SIOCADDMULTI:
  568         case SIOCDELMULTI:
  569                 break;
  570         default:
  571                 error = EINVAL;
  572         }
  573         return (error);
  574 }
  575 
  576 /*
  577  * tunoutput - queue packets from higher level ready to put out.
  578  */
  579 static int
  580 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
  581     struct route *ro)
  582 {
  583         struct tun_softc *tp = ifp->if_softc;
  584         u_short cached_tun_flags;
  585         int error;
  586         u_int32_t af;
  587 
  588         TUNDEBUG (ifp, "tunoutput\n");
  589 
  590 #ifdef MAC
  591         error = mac_ifnet_check_transmit(ifp, m0);
  592         if (error) {
  593                 m_freem(m0);
  594                 return (error);
  595         }
  596 #endif
  597 
  598         /* Could be unlocked read? */
  599         mtx_lock(&tp->tun_mtx);
  600         cached_tun_flags = tp->tun_flags;
  601         mtx_unlock(&tp->tun_mtx);
  602         if ((cached_tun_flags & TUN_READY) != TUN_READY) {
  603                 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
  604                 m_freem (m0);
  605                 return (EHOSTDOWN);
  606         }
  607 
  608         if ((ifp->if_flags & IFF_UP) != IFF_UP) {
  609                 m_freem (m0);
  610                 return (EHOSTDOWN);
  611         }
  612 
  613         /* BPF writes need to be handled specially. */
  614         if (dst->sa_family == AF_UNSPEC) {
  615                 bcopy(dst->sa_data, &af, sizeof(af));
  616                 dst->sa_family = af; 
  617         }
  618 
  619         if (bpf_peers_present(ifp->if_bpf)) {
  620                 af = dst->sa_family;
  621                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
  622         }
  623 
  624         /* prepend sockaddr? this may abort if the mbuf allocation fails */
  625         if (cached_tun_flags & TUN_LMODE) {
  626                 /* allocate space for sockaddr */
  627                 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
  628 
  629                 /* if allocation failed drop packet */
  630                 if (m0 == NULL) {
  631                         ifp->if_iqdrops++;
  632                         ifp->if_oerrors++;
  633                         return (ENOBUFS);
  634                 } else {
  635                         bcopy(dst, m0->m_data, dst->sa_len);
  636                 }
  637         }
  638 
  639         if (cached_tun_flags & TUN_IFHEAD) {
  640                 /* Prepend the address family */
  641                 M_PREPEND(m0, 4, M_DONTWAIT);
  642 
  643                 /* if allocation failed drop packet */
  644                 if (m0 == NULL) {
  645                         ifp->if_iqdrops++;
  646                         ifp->if_oerrors++;
  647                         return (ENOBUFS);
  648                 } else
  649                         *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
  650         } else {
  651 #ifdef INET
  652                 if (dst->sa_family != AF_INET)
  653 #endif
  654                 {
  655                         m_freem(m0);
  656                         return (EAFNOSUPPORT);
  657                 }
  658         }
  659 
  660         error = (ifp->if_transmit)(ifp, m0);
  661         if (error)
  662                 return (ENOBUFS);
  663         ifp->if_opackets++;
  664         return (0);
  665 }
  666 
  667 /*
  668  * the cdevsw interface is now pretty minimal.
  669  */
  670 static  int
  671 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
  672     struct thread *td)
  673 {
  674         int             error;
  675         struct tun_softc *tp = dev->si_drv1;
  676         struct tuninfo *tunp;
  677 
  678         switch (cmd) {
  679         case TUNSIFINFO:
  680                 tunp = (struct tuninfo *)data;
  681                 if (tunp->mtu < IF_MINMTU)
  682                         return (EINVAL);
  683                 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
  684                         error = priv_check(td, PRIV_NET_SETIFMTU);
  685                         if (error)
  686                                 return (error);
  687                 }
  688                 if (TUN2IFP(tp)->if_type != tunp->type)
  689                         return (EPROTOTYPE);
  690                 mtx_lock(&tp->tun_mtx);
  691                 TUN2IFP(tp)->if_mtu = tunp->mtu;
  692                 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
  693                 mtx_unlock(&tp->tun_mtx);
  694                 break;
  695         case TUNGIFINFO:
  696                 tunp = (struct tuninfo *)data;
  697                 mtx_lock(&tp->tun_mtx);
  698                 tunp->mtu = TUN2IFP(tp)->if_mtu;
  699                 tunp->type = TUN2IFP(tp)->if_type;
  700                 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
  701                 mtx_unlock(&tp->tun_mtx);
  702                 break;
  703         case TUNSDEBUG:
  704                 tundebug = *(int *)data;
  705                 break;
  706         case TUNGDEBUG:
  707                 *(int *)data = tundebug;
  708                 break;
  709         case TUNSLMODE:
  710                 mtx_lock(&tp->tun_mtx);
  711                 if (*(int *)data) {
  712                         tp->tun_flags |= TUN_LMODE;
  713                         tp->tun_flags &= ~TUN_IFHEAD;
  714                 } else
  715                         tp->tun_flags &= ~TUN_LMODE;
  716                 mtx_unlock(&tp->tun_mtx);
  717                 break;
  718         case TUNSIFHEAD:
  719                 mtx_lock(&tp->tun_mtx);
  720                 if (*(int *)data) {
  721                         tp->tun_flags |= TUN_IFHEAD;
  722                         tp->tun_flags &= ~TUN_LMODE;
  723                 } else
  724                         tp->tun_flags &= ~TUN_IFHEAD;
  725                 mtx_unlock(&tp->tun_mtx);
  726                 break;
  727         case TUNGIFHEAD:
  728                 mtx_lock(&tp->tun_mtx);
  729                 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
  730                 mtx_unlock(&tp->tun_mtx);
  731                 break;
  732         case TUNSIFMODE:
  733                 /* deny this if UP */
  734                 if (TUN2IFP(tp)->if_flags & IFF_UP)
  735                         return(EBUSY);
  736 
  737                 switch (*(int *)data & ~IFF_MULTICAST) {
  738                 case IFF_POINTOPOINT:
  739                 case IFF_BROADCAST:
  740                         mtx_lock(&tp->tun_mtx);
  741                         TUN2IFP(tp)->if_flags &=
  742                             ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
  743                         TUN2IFP(tp)->if_flags |= *(int *)data;
  744                         mtx_unlock(&tp->tun_mtx);
  745                         break;
  746                 default:
  747                         return(EINVAL);
  748                 }
  749                 break;
  750         case TUNSIFPID:
  751                 mtx_lock(&tp->tun_mtx);
  752                 tp->tun_pid = curthread->td_proc->p_pid;
  753                 mtx_unlock(&tp->tun_mtx);
  754                 break;
  755         case FIONBIO:
  756                 break;
  757         case FIOASYNC:
  758                 mtx_lock(&tp->tun_mtx);
  759                 if (*(int *)data)
  760                         tp->tun_flags |= TUN_ASYNC;
  761                 else
  762                         tp->tun_flags &= ~TUN_ASYNC;
  763                 mtx_unlock(&tp->tun_mtx);
  764                 break;
  765         case FIONREAD:
  766                 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
  767                         struct mbuf *mb;
  768                         IFQ_LOCK(&TUN2IFP(tp)->if_snd);
  769                         IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
  770                         for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
  771                                 *(int *)data += mb->m_len;
  772                         IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
  773                 } else
  774                         *(int *)data = 0;
  775                 break;
  776         case FIOSETOWN:
  777                 return (fsetown(*(int *)data, &tp->tun_sigio));
  778 
  779         case FIOGETOWN:
  780                 *(int *)data = fgetown(&tp->tun_sigio);
  781                 return (0);
  782 
  783         /* This is deprecated, FIOSETOWN should be used instead. */
  784         case TIOCSPGRP:
  785                 return (fsetown(-(*(int *)data), &tp->tun_sigio));
  786 
  787         /* This is deprecated, FIOGETOWN should be used instead. */
  788         case TIOCGPGRP:
  789                 *(int *)data = -fgetown(&tp->tun_sigio);
  790                 return (0);
  791 
  792         default:
  793                 return (ENOTTY);
  794         }
  795         return (0);
  796 }
  797 
  798 /*
  799  * The cdevsw 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 cdev *dev, struct uio *uio, int flag)
  804 {
  805         struct tun_softc *tp = dev->si_drv1;
  806         struct ifnet    *ifp = TUN2IFP(tp);
  807         struct mbuf     *m;
  808         int             error=0, len;
  809 
  810         TUNDEBUG (ifp, "read\n");
  811         mtx_lock(&tp->tun_mtx);
  812         if ((tp->tun_flags & TUN_READY) != TUN_READY) {
  813                 mtx_unlock(&tp->tun_mtx);
  814                 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
  815                 return (EHOSTDOWN);
  816         }
  817 
  818         tp->tun_flags &= ~TUN_RWAIT;
  819 
  820         do {
  821                 IFQ_DEQUEUE(&ifp->if_snd, m);
  822                 if (m == NULL) {
  823                         if (flag & O_NONBLOCK) {
  824                                 mtx_unlock(&tp->tun_mtx);
  825                                 return (EWOULDBLOCK);
  826                         }
  827                         tp->tun_flags |= TUN_RWAIT;
  828                         error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
  829                             "tunread", 0);
  830                         if (error != 0) {
  831                                 mtx_unlock(&tp->tun_mtx);
  832                                 return (error);
  833                         }
  834                 }
  835         } while (m == NULL);
  836         mtx_unlock(&tp->tun_mtx);
  837 
  838         while (m && uio->uio_resid > 0 && error == 0) {
  839                 len = min(uio->uio_resid, m->m_len);
  840                 if (len != 0)
  841                         error = uiomove(mtod(m, void *), len, uio);
  842                 m = m_free(m);
  843         }
  844 
  845         if (m) {
  846                 TUNDEBUG(ifp, "Dropping mbuf\n");
  847                 m_freem(m);
  848         }
  849         return (error);
  850 }
  851 
  852 /*
  853  * the cdevsw write interface - an atomic write is a packet - or else!
  854  */
  855 static  int
  856 tunwrite(struct cdev *dev, struct uio *uio, int flag)
  857 {
  858         struct tun_softc *tp = dev->si_drv1;
  859         struct ifnet    *ifp = TUN2IFP(tp);
  860         struct mbuf     *m;
  861         uint32_t        family;
  862         int             isr;
  863 
  864         TUNDEBUG(ifp, "tunwrite\n");
  865 
  866         if ((ifp->if_flags & IFF_UP) != IFF_UP)
  867                 /* ignore silently */
  868                 return (0);
  869 
  870         if (uio->uio_resid == 0)
  871                 return (0);
  872 
  873         if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
  874                 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
  875                 return (EIO);
  876         }
  877 
  878         if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
  879                 ifp->if_ierrors++;
  880                 return (ENOBUFS);
  881         }
  882 
  883         m->m_pkthdr.rcvif = ifp;
  884 #ifdef MAC
  885         mac_ifnet_create_mbuf(ifp, m);
  886 #endif
  887 
  888         /* Could be unlocked read? */
  889         mtx_lock(&tp->tun_mtx);
  890         if (tp->tun_flags & TUN_IFHEAD) {
  891                 mtx_unlock(&tp->tun_mtx);
  892                 if (m->m_len < sizeof(family) &&
  893                     (m = m_pullup(m, sizeof(family))) == NULL)
  894                         return (ENOBUFS);
  895                 family = ntohl(*mtod(m, u_int32_t *));
  896                 m_adj(m, sizeof(family));
  897         } else {
  898                 mtx_unlock(&tp->tun_mtx);
  899                 family = AF_INET;
  900         }
  901 
  902         BPF_MTAP2(ifp, &family, sizeof(family), m);
  903 
  904         switch (family) {
  905 #ifdef INET
  906         case AF_INET:
  907                 isr = NETISR_IP;
  908                 break;
  909 #endif
  910 #ifdef INET6
  911         case AF_INET6:
  912                 isr = NETISR_IPV6;
  913                 break;
  914 #endif
  915 #ifdef IPX
  916         case AF_IPX:
  917                 isr = NETISR_IPX;
  918                 break;
  919 #endif
  920 #ifdef NETATALK
  921         case AF_APPLETALK:
  922                 isr = NETISR_ATALK2;
  923                 break;
  924 #endif
  925         default:
  926                 m_freem(m);
  927                 return (EAFNOSUPPORT);
  928         }
  929         /* First chunk of an mbuf contains good junk */
  930         if (harvest.point_to_point)
  931                 random_harvest(m, 16, 3, 0, RANDOM_NET);
  932         ifp->if_ibytes += m->m_pkthdr.len;
  933         ifp->if_ipackets++;
  934         CURVNET_SET(ifp->if_vnet);
  935         M_SETFIB(m, ifp->if_fib);
  936         netisr_dispatch(isr, m);
  937         CURVNET_RESTORE();
  938         return (0);
  939 }
  940 
  941 /*
  942  * tunpoll - the poll interface, this is only useful on reads
  943  * really. The write detect always returns true, write never blocks
  944  * anyway, it either accepts the packet or drops it.
  945  */
  946 static  int
  947 tunpoll(struct cdev *dev, int events, struct thread *td)
  948 {
  949         struct tun_softc *tp = dev->si_drv1;
  950         struct ifnet    *ifp = TUN2IFP(tp);
  951         int             revents = 0;
  952         struct mbuf     *m;
  953 
  954         TUNDEBUG(ifp, "tunpoll\n");
  955 
  956         if (events & (POLLIN | POLLRDNORM)) {
  957                 IFQ_LOCK(&ifp->if_snd);
  958                 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
  959                 if (m != NULL) {
  960                         TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
  961                         revents |= events & (POLLIN | POLLRDNORM);
  962                 } else {
  963                         TUNDEBUG(ifp, "tunpoll waiting\n");
  964                         selrecord(td, &tp->tun_rsel);
  965                 }
  966                 IFQ_UNLOCK(&ifp->if_snd);
  967         }
  968         if (events & (POLLOUT | POLLWRNORM))
  969                 revents |= events & (POLLOUT | POLLWRNORM);
  970 
  971         return (revents);
  972 }
  973 
  974 /*
  975  * tunkqfilter - support for the kevent() system call.
  976  */
  977 static int
  978 tunkqfilter(struct cdev *dev, struct knote *kn)
  979 {
  980         struct tun_softc        *tp = dev->si_drv1;
  981         struct ifnet    *ifp = TUN2IFP(tp);
  982 
  983         switch(kn->kn_filter) {
  984         case EVFILT_READ:
  985                 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
  986                     ifp->if_xname, dev2unit(dev));
  987                 kn->kn_fop = &tun_read_filterops;
  988                 break;
  989 
  990         case EVFILT_WRITE:
  991                 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
  992                     ifp->if_xname, dev2unit(dev));
  993                 kn->kn_fop = &tun_write_filterops;
  994                 break;
  995 
  996         default:
  997                 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
  998                     ifp->if_xname, dev2unit(dev));
  999                 return(EINVAL);
 1000         }
 1001 
 1002         kn->kn_hook = tp;
 1003         knlist_add(&tp->tun_rsel.si_note, kn, 0);
 1004 
 1005         return (0);
 1006 }
 1007 
 1008 /*
 1009  * Return true of there is data in the interface queue.
 1010  */
 1011 static int
 1012 tunkqread(struct knote *kn, long hint)
 1013 {
 1014         int                     ret;
 1015         struct tun_softc        *tp = kn->kn_hook;
 1016         struct cdev             *dev = tp->tun_dev;
 1017         struct ifnet    *ifp = TUN2IFP(tp);
 1018 
 1019         if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
 1020                 TUNDEBUG(ifp,
 1021                     "%s have data in the queue.  Len = %d, minor = %#x\n",
 1022                     ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
 1023                 ret = 1;
 1024         } else {
 1025                 TUNDEBUG(ifp,
 1026                     "%s waiting for data, minor = %#x\n", ifp->if_xname,
 1027                     dev2unit(dev));
 1028                 ret = 0;
 1029         }
 1030 
 1031         return (ret);
 1032 }
 1033 
 1034 /*
 1035  * Always can write, always return MTU in kn->data.
 1036  */
 1037 static int
 1038 tunkqwrite(struct knote *kn, long hint)
 1039 {
 1040         struct tun_softc        *tp = kn->kn_hook;
 1041         struct ifnet    *ifp = TUN2IFP(tp);
 1042 
 1043         kn->kn_data = ifp->if_mtu;
 1044 
 1045         return (1);
 1046 }
 1047 
 1048 static void
 1049 tunkqdetach(struct knote *kn)
 1050 {
 1051         struct tun_softc        *tp = kn->kn_hook;
 1052 
 1053         knlist_remove(&tp->tun_rsel.si_note, kn, 0);
 1054 }

Cache object: 1fa559e5992ea1a10b6d4cd6b4e58711


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.