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

Cache object: 653038d16177133f13729612c9662813


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