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

Cache object: 123889f588a61ba1035a90d62e3fad23


[ 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.