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_tap.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 /*-
    2  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * BASED ON:
   27  * -------------------------------------------------------------------------
   28  *
   29  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
   30  * Nottingham University 1987.
   31  */
   32 
   33 /*
   34  * $FreeBSD: releng/6.2/sys/net/if_tap.c 162998 2006-10-04 06:09:11Z ru $
   35  * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
   36  */
   37 
   38 #include "opt_inet.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/conf.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/filio.h>
   44 #include <sys/kernel.h>
   45 #include <sys/malloc.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/module.h>
   48 #include <sys/poll.h>
   49 #include <sys/proc.h>
   50 #include <sys/selinfo.h>
   51 #include <sys/signalvar.h>
   52 #include <sys/socket.h>
   53 #include <sys/sockio.h>
   54 #include <sys/sysctl.h>
   55 #include <sys/systm.h>
   56 #include <sys/ttycom.h>
   57 #include <sys/uio.h>
   58 #include <sys/queue.h>
   59 
   60 #include <net/bpf.h>
   61 #include <net/ethernet.h>
   62 #include <net/if.h>
   63 #include <net/if_arp.h>
   64 #include <net/route.h>
   65 #include <net/if_types.h>
   66 
   67 #include <netinet/in.h>
   68 
   69 #include <net/if_tapvar.h>
   70 #include <net/if_tap.h>
   71 
   72 
   73 #define CDEV_NAME       "tap"
   74 #define TAPDEBUG        if (tapdebug) printf
   75 
   76 #define TAP             "tap"
   77 #define VMNET           "vmnet"
   78 #define TAPMAXUNIT      0x7fff
   79 #define VMNET_DEV_MASK  CLONE_FLAG0
   80 
   81 /* module */
   82 static int              tapmodevent(module_t, int, void *);
   83 
   84 /* device */
   85 static void             tapclone(void *, struct ucred *, char *, int,
   86                             struct cdev **);
   87 static void             tapcreate(struct cdev *);
   88 
   89 /* network interface */
   90 static void             tapifstart(struct ifnet *);
   91 static int              tapifioctl(struct ifnet *, u_long, caddr_t);
   92 static void             tapifinit(void *);
   93 
   94 /* character device */
   95 static d_open_t         tapopen;
   96 static d_close_t        tapclose;
   97 static d_read_t         tapread;
   98 static d_write_t        tapwrite;
   99 static d_ioctl_t        tapioctl;
  100 static d_poll_t         tappoll;
  101 static d_kqfilter_t     tapkqfilter;
  102 
  103 /* kqueue(2) */
  104 static int              tapkqread(struct knote *, long);
  105 static int              tapkqwrite(struct knote *, long);
  106 static void             tapkqdetach(struct knote *);
  107 
  108 static struct filterops tap_read_filterops = {
  109         .f_isfd =       1,
  110         .f_attach =     NULL,
  111         .f_detach =     tapkqdetach,
  112         .f_event =      tapkqread,
  113 };
  114 
  115 static struct filterops tap_write_filterops = {
  116         .f_isfd =       1,
  117         .f_attach =     NULL,
  118         .f_detach =     tapkqdetach,
  119         .f_event =      tapkqwrite,
  120 };
  121 
  122 static struct cdevsw    tap_cdevsw = {
  123         .d_version =    D_VERSION,
  124         .d_flags =      D_PSEUDO | D_NEEDGIANT,
  125         .d_open =       tapopen,
  126         .d_close =      tapclose,
  127         .d_read =       tapread,
  128         .d_write =      tapwrite,
  129         .d_ioctl =      tapioctl,
  130         .d_poll =       tappoll,
  131         .d_name =       CDEV_NAME,
  132         .d_kqfilter =   tapkqfilter,
  133 };
  134 
  135 /*
  136  * All global variables in if_tap.c are locked with tapmtx, with the
  137  * exception of tapdebug, which is accessed unlocked; tapclones is
  138  * static at runtime.
  139  */
  140 static struct mtx               tapmtx;
  141 static int                      tapdebug = 0;        /* debug flag   */
  142 static int                      tapuopen = 0;        /* allow user open() */         
  143 static SLIST_HEAD(, tap_softc)  taphead;             /* first device */
  144 static struct clonedevs         *tapclones;
  145 
  146 MALLOC_DECLARE(M_TAP);
  147 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
  148 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
  149 
  150 SYSCTL_DECL(_net_link);
  151 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
  152     "Ethernet tunnel software network interface");
  153 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
  154         "Allow user to open /dev/tap (based on node permissions)");
  155 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
  156 
  157 DEV_MODULE(if_tap, tapmodevent, NULL);
  158 
  159 /*
  160  * tapmodevent
  161  *
  162  * module event handler
  163  */
  164 static int
  165 tapmodevent(module_t mod, int type, void *data)
  166 {
  167         static eventhandler_tag  eh_tag = NULL;
  168         struct tap_softc        *tp = NULL;
  169         struct ifnet            *ifp = NULL;
  170         int                      s;
  171 
  172         switch (type) {
  173         case MOD_LOAD:
  174 
  175                 /* intitialize device */
  176 
  177                 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
  178                 SLIST_INIT(&taphead);
  179 
  180                 clone_setup(&tapclones);
  181                 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
  182                 if (eh_tag == NULL) {
  183                         clone_cleanup(&tapclones);
  184                         mtx_destroy(&tapmtx);
  185                         return (ENOMEM);
  186                 }
  187                 return (0);
  188 
  189         case MOD_UNLOAD:
  190                 /*
  191                  * The EBUSY algorithm here can't quite atomically
  192                  * guarantee that this is race-free since we have to
  193                  * release the tap mtx to deregister the clone handler.
  194                  */
  195                 mtx_lock(&tapmtx);
  196                 SLIST_FOREACH(tp, &taphead, tap_next) {
  197                         mtx_lock(&tp->tap_mtx);
  198                         if (tp->tap_flags & TAP_OPEN) {
  199                                 mtx_unlock(&tp->tap_mtx);
  200                                 mtx_unlock(&tapmtx);
  201                                 return (EBUSY);
  202                         }
  203                         mtx_unlock(&tp->tap_mtx);
  204                 }
  205                 mtx_unlock(&tapmtx);
  206 
  207                 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
  208 
  209                 mtx_lock(&tapmtx);
  210                 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
  211                         SLIST_REMOVE_HEAD(&taphead, tap_next);
  212                         mtx_unlock(&tapmtx);
  213 
  214                         ifp = tp->tap_ifp;
  215 
  216                         TAPDEBUG("detaching %s\n", ifp->if_xname);
  217 
  218                         /* Unlocked read. */
  219                         KASSERT(!(tp->tap_flags & TAP_OPEN), 
  220                                 ("%s flags is out of sync", ifp->if_xname));
  221 
  222                         knlist_destroy(&tp->tap_rsel.si_note);
  223                         destroy_dev(tp->tap_dev);
  224                         s = splimp();
  225                         ether_ifdetach(ifp);
  226                         if_free_type(ifp, IFT_ETHER);
  227                         splx(s);
  228 
  229                         mtx_destroy(&tp->tap_mtx);
  230                         free(tp, M_TAP);
  231                         mtx_lock(&tapmtx);
  232                 }
  233                 mtx_unlock(&tapmtx);
  234                 clone_cleanup(&tapclones);
  235 
  236                 mtx_destroy(&tapmtx);
  237 
  238                 break;
  239 
  240         default:
  241                 return (EOPNOTSUPP);
  242         }
  243 
  244         return (0);
  245 } /* tapmodevent */
  246 
  247 
  248 /*
  249  * DEVFS handler
  250  *
  251  * We need to support two kind of devices - tap and vmnet
  252  */
  253 static void
  254 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
  255 {
  256         u_int           extra;
  257         int             i, unit;
  258         char            *device_name = name;
  259 
  260         if (*dev != NULL)
  261                 return;
  262 
  263         device_name = TAP;
  264         extra = 0;
  265         if (strcmp(name, TAP) == 0) {
  266                 unit = -1;
  267         } else if (strcmp(name, VMNET) == 0) {
  268                 device_name = VMNET;
  269                 extra = VMNET_DEV_MASK;
  270                 unit = -1;
  271         } else if (dev_stdclone(name, NULL, device_name, &unit) != 1) {
  272                 device_name = VMNET;
  273                 extra = VMNET_DEV_MASK;
  274                 if (dev_stdclone(name, NULL, device_name, &unit) != 1)
  275                         return;
  276         }
  277 
  278         /* find any existing device, or allocate new unit number */
  279         i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
  280         if (i) {
  281                 *dev = make_dev(&tap_cdevsw, unit2minor(unit | extra),
  282                      UID_ROOT, GID_WHEEL, 0600, "%s%d", device_name, unit);
  283                 if (*dev != NULL) {
  284                         dev_ref(*dev);
  285                         (*dev)->si_flags |= SI_CHEAPCLONE;
  286                 }
  287         }
  288 } /* tapclone */
  289 
  290 
  291 /*
  292  * tapcreate
  293  *
  294  * to create interface
  295  */
  296 static void
  297 tapcreate(struct cdev *dev)
  298 {
  299         struct ifnet            *ifp = NULL;
  300         struct tap_softc        *tp = NULL;
  301         unsigned short           macaddr_hi;
  302         int                      unit, s;
  303         char                    *name = NULL;
  304         u_char                  eaddr[6];
  305 
  306         dev->si_flags &= ~SI_CHEAPCLONE;
  307 
  308         /* allocate driver storage and create device */
  309         MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
  310         mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
  311         mtx_lock(&tapmtx);
  312         SLIST_INSERT_HEAD(&taphead, tp, tap_next);
  313         mtx_unlock(&tapmtx);
  314 
  315         unit = dev2unit(dev);
  316 
  317         /* select device: tap or vmnet */
  318         if (unit & VMNET_DEV_MASK) {
  319                 name = VMNET;
  320                 tp->tap_flags |= TAP_VMNET;
  321         } else
  322                 name = TAP;
  323 
  324         unit &= TAPMAXUNIT;
  325 
  326         TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, minor(dev));
  327 
  328         /* generate fake MAC address: 00 bd xx xx xx unit_no */
  329         macaddr_hi = htons(0x00bd);
  330         bcopy(&macaddr_hi, eaddr, sizeof(short));
  331         bcopy(&ticks, &eaddr[2], sizeof(long));
  332         eaddr[5] = (u_char)unit;
  333 
  334         /* fill the rest and attach interface */
  335         ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
  336         if (ifp == NULL)
  337                 panic("%s%d: can not if_alloc()", name, unit);
  338         ifp->if_softc = tp;
  339         if_initname(ifp, name, unit);
  340         ifp->if_init = tapifinit;
  341         ifp->if_start = tapifstart;
  342         ifp->if_ioctl = tapifioctl;
  343         ifp->if_mtu = ETHERMTU;
  344         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
  345         ifp->if_snd.ifq_maxlen = ifqmaxlen;
  346 
  347         dev->si_drv1 = tp;
  348         tp->tap_dev = dev;
  349 
  350         s = splimp();
  351         ether_ifattach(ifp, eaddr);
  352         splx(s);
  353 
  354         mtx_lock(&tp->tap_mtx);
  355         tp->tap_flags |= TAP_INITED;
  356         mtx_unlock(&tp->tap_mtx);
  357 
  358         knlist_init(&tp->tap_rsel.si_note, NULL, NULL, NULL, NULL);
  359 
  360         TAPDEBUG("interface %s is created. minor = %#x\n", 
  361                 ifp->if_xname, minor(dev));
  362 } /* tapcreate */
  363 
  364 
  365 /*
  366  * tapopen
  367  *
  368  * to open tunnel. must be superuser
  369  */
  370 static int
  371 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
  372 {
  373         struct tap_softc        *tp = NULL;
  374         struct ifnet            *ifp = NULL;
  375         int                      s;
  376 
  377         if (tapuopen == 0 && suser(td) != 0)
  378                 return (EPERM);
  379 
  380         if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
  381                 return (ENXIO);
  382 
  383         /*
  384          * XXXRW: Non-atomic test-and-set of si_drv1.  Currently protected
  385          * by Giant, but the race actually exists under memory pressure as
  386          * well even when running with Giant, as malloc() may sleep.
  387          */
  388         tp = dev->si_drv1;
  389         if (tp == NULL) {
  390                 tapcreate(dev);
  391                 tp = dev->si_drv1;
  392         }
  393 
  394         mtx_lock(&tp->tap_mtx);
  395         if (tp->tap_flags & TAP_OPEN) {
  396                 mtx_unlock(&tp->tap_mtx);
  397                 return (EBUSY);
  398         }
  399 
  400         bcopy(IFP2ENADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
  401         tp->tap_pid = td->td_proc->p_pid;
  402         tp->tap_flags |= TAP_OPEN;
  403         ifp = tp->tap_ifp;
  404         mtx_unlock(&tp->tap_mtx);
  405 
  406         s = splimp();
  407         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  408         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  409         splx(s);
  410 
  411         TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, minor(dev));
  412 
  413         return (0);
  414 } /* tapopen */
  415 
  416 
  417 /*
  418  * tapclose
  419  *
  420  * close the device - mark i/f down & delete routing info
  421  */
  422 static int
  423 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
  424 {
  425         struct ifaddr           *ifa;
  426         struct tap_softc        *tp = dev->si_drv1;
  427         struct ifnet            *ifp = tp->tap_ifp;
  428         int                     s;
  429 
  430         /* junk all pending output */
  431         IF_DRAIN(&ifp->if_snd);
  432 
  433         /*
  434          * do not bring the interface down, and do not anything with
  435          * interface, if we are in VMnet mode. just close the device.
  436          */
  437 
  438         mtx_lock(&tp->tap_mtx);
  439         if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
  440                 mtx_unlock(&tp->tap_mtx);
  441                 s = splimp();
  442                 if_down(ifp);
  443                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
  444                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  445                                 rtinit(ifa, (int)RTM_DELETE, 0);
  446                         }
  447                         if_purgeaddrs(ifp);
  448                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  449                 }
  450                 splx(s);
  451         } else
  452                 mtx_unlock(&tp->tap_mtx);
  453 
  454         funsetown(&tp->tap_sigio);
  455         selwakeuppri(&tp->tap_rsel, PZERO+1);
  456         KNOTE_UNLOCKED(&tp->tap_rsel.si_note, 0);
  457 
  458         mtx_lock(&tp->tap_mtx);
  459         tp->tap_flags &= ~TAP_OPEN;
  460         tp->tap_pid = 0;
  461         mtx_unlock(&tp->tap_mtx);
  462 
  463         TAPDEBUG("%s is closed. minor = %#x\n", 
  464                 ifp->if_xname, minor(dev));
  465 
  466         return (0);
  467 } /* tapclose */
  468 
  469 
  470 /*
  471  * tapifinit
  472  *
  473  * network interface initialization function
  474  */
  475 static void
  476 tapifinit(void *xtp)
  477 {
  478         struct tap_softc        *tp = (struct tap_softc *)xtp;
  479         struct ifnet            *ifp = tp->tap_ifp;
  480 
  481         TAPDEBUG("initializing %s\n", ifp->if_xname);
  482 
  483         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  484         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  485 
  486         /* attempt to start output */
  487         tapifstart(ifp);
  488 } /* tapifinit */
  489 
  490 
  491 /*
  492  * tapifioctl
  493  *
  494  * Process an ioctl request on network interface
  495  */
  496 static int
  497 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  498 {
  499         struct tap_softc        *tp = (struct tap_softc *)(ifp->if_softc);
  500         struct ifstat           *ifs = NULL;
  501         int                      s, dummy;
  502 
  503         switch (cmd) {
  504                 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
  505                 case SIOCADDMULTI:
  506                 case SIOCDELMULTI:
  507                         break;
  508 
  509                 case SIOCGIFSTATUS:
  510                         s = splimp();
  511                         ifs = (struct ifstat *)data;
  512                         dummy = strlen(ifs->ascii);
  513                         mtx_lock(&tp->tap_mtx);
  514                         if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
  515                                 snprintf(ifs->ascii + dummy,
  516                                         sizeof(ifs->ascii) - dummy,
  517                                         "\tOpened by PID %d\n", tp->tap_pid);
  518                         mtx_unlock(&tp->tap_mtx);
  519                         splx(s);
  520                         break;
  521 
  522                 default:
  523                         s = splimp();
  524                         dummy = ether_ioctl(ifp, cmd, data);
  525                         splx(s);
  526                         return (dummy);
  527                         /* NOT REACHED */
  528         }
  529 
  530         return (0);
  531 } /* tapifioctl */
  532 
  533 
  534 /*
  535  * tapifstart
  536  *
  537  * queue packets from higher level ready to put out
  538  */
  539 static void
  540 tapifstart(struct ifnet *ifp)
  541 {
  542         struct tap_softc        *tp = ifp->if_softc;
  543         int                      s;
  544 
  545         TAPDEBUG("%s starting\n", ifp->if_xname);
  546 
  547         /*
  548          * do not junk pending output if we are in VMnet mode.
  549          * XXX: can this do any harm because of queue overflow?
  550          */
  551 
  552         mtx_lock(&tp->tap_mtx);
  553         if (((tp->tap_flags & TAP_VMNET) == 0) &&
  554             ((tp->tap_flags & TAP_READY) != TAP_READY)) {
  555                 struct mbuf     *m = NULL;
  556 
  557                 mtx_unlock(&tp->tap_mtx);
  558 
  559                 /* Unlocked read. */
  560                 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname, 
  561                     tp->tap_flags);
  562 
  563                 s = splimp();
  564                 do {
  565                         IF_DEQUEUE(&ifp->if_snd, m);
  566                         if (m != NULL)
  567                                 m_freem(m);
  568                         ifp->if_oerrors ++;
  569                 } while (m != NULL);
  570                 splx(s);
  571 
  572                 return;
  573         }
  574         mtx_unlock(&tp->tap_mtx);
  575 
  576         s = splimp();
  577         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  578 
  579         if (ifp->if_snd.ifq_len != 0) {
  580                 mtx_lock(&tp->tap_mtx);
  581                 if (tp->tap_flags & TAP_RWAIT) {
  582                         tp->tap_flags &= ~TAP_RWAIT;
  583                         wakeup(tp);
  584                 }
  585 
  586                 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
  587                         mtx_unlock(&tp->tap_mtx);
  588                         pgsigio(&tp->tap_sigio, SIGIO, 0);
  589                 } else
  590                         mtx_unlock(&tp->tap_mtx);
  591 
  592                 selwakeuppri(&tp->tap_rsel, PZERO+1);
  593                 KNOTE_UNLOCKED(&tp->tap_rsel.si_note, 0);
  594                 ifp->if_opackets ++; /* obytes are counted in ether_output */
  595         }
  596 
  597         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  598         splx(s);
  599 } /* tapifstart */
  600 
  601 
  602 /*
  603  * tapioctl
  604  *
  605  * the cdevsw interface is now pretty minimal
  606  */
  607 static int
  608 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  609 {
  610         struct tap_softc        *tp = dev->si_drv1;
  611         struct ifnet            *ifp = tp->tap_ifp;
  612         struct tapinfo          *tapp = NULL;
  613         int                      s;
  614         int                      f;
  615         int                      ival;
  616 
  617         switch (cmd) {
  618                 case TAPSIFINFO:
  619                         s = splimp();
  620                         tapp = (struct tapinfo *)data;
  621                         ifp->if_mtu = tapp->mtu;
  622                         ifp->if_type = tapp->type;
  623                         ifp->if_baudrate = tapp->baudrate;
  624                         splx(s);
  625                         break;
  626 
  627                 case TAPGIFINFO:
  628                         tapp = (struct tapinfo *)data;
  629                         tapp->mtu = ifp->if_mtu;
  630                         tapp->type = ifp->if_type;
  631                         tapp->baudrate = ifp->if_baudrate;
  632                         break;
  633 
  634                 case TAPSDEBUG:
  635                         tapdebug = *(int *)data;
  636                         break;
  637 
  638                 case TAPGDEBUG:
  639                         *(int *)data = tapdebug;
  640                         break;
  641 
  642                 case FIONBIO:
  643                         break;
  644 
  645                 case FIOASYNC:
  646                         s = splimp();
  647                         mtx_lock(&tp->tap_mtx);
  648                         if (*(int *)data)
  649                                 tp->tap_flags |= TAP_ASYNC;
  650                         else
  651                                 tp->tap_flags &= ~TAP_ASYNC;
  652                         mtx_unlock(&tp->tap_mtx);
  653                         splx(s);
  654                         break;
  655 
  656                 case FIONREAD:
  657                         s = splimp();
  658                         if (ifp->if_snd.ifq_head) {
  659                                 struct mbuf     *mb = ifp->if_snd.ifq_head;
  660 
  661                                 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
  662                                         *(int *)data += mb->m_len;
  663                         } else
  664                                 *(int *)data = 0;
  665                         splx(s);
  666                         break;
  667 
  668                 case FIOSETOWN:
  669                         return (fsetown(*(int *)data, &tp->tap_sigio));
  670 
  671                 case FIOGETOWN:
  672                         *(int *)data = fgetown(&tp->tap_sigio);
  673                         return (0);
  674 
  675                 /* this is deprecated, FIOSETOWN should be used instead */
  676                 case TIOCSPGRP:
  677                         return (fsetown(-(*(int *)data), &tp->tap_sigio));
  678 
  679                 /* this is deprecated, FIOGETOWN should be used instead */
  680                 case TIOCGPGRP:
  681                         *(int *)data = -fgetown(&tp->tap_sigio);
  682                         return (0);
  683 
  684                 /* VMware/VMnet port ioctl's */
  685 
  686                 case SIOCGIFFLAGS:      /* get ifnet flags */
  687                         bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
  688                         break;
  689 
  690                 case _IO('V', 0):
  691                         ival = IOCPARM_IVAL(data);
  692                         data = (caddr_t)&ival;
  693                         /* FALLTHROUGH */
  694                 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
  695                         f = *(int *)data;
  696                         f &= 0x0fff;
  697                         f &= ~IFF_CANTCHANGE;
  698                         f |= IFF_UP;
  699 
  700                         s = splimp();
  701                         ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
  702                         splx(s);
  703                         break;
  704 
  705                 case OSIOCGIFADDR:      /* get MAC address of the remote side */
  706                 case SIOCGIFADDR:
  707                         mtx_lock(&tp->tap_mtx);
  708                         bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
  709                         mtx_unlock(&tp->tap_mtx);
  710                         break;
  711 
  712                 case SIOCSIFADDR:       /* set MAC address of the remote side */
  713                         mtx_lock(&tp->tap_mtx);
  714                         bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
  715                         mtx_unlock(&tp->tap_mtx);
  716                         break;
  717 
  718                 default:
  719                         return (ENOTTY);
  720         }
  721         return (0);
  722 } /* tapioctl */
  723 
  724 
  725 /*
  726  * tapread
  727  *
  728  * the cdevsw read interface - reads a packet at a time, or at
  729  * least as much of a packet as can be read
  730  */
  731 static int
  732 tapread(struct cdev *dev, struct uio *uio, int flag)
  733 {
  734         struct tap_softc        *tp = dev->si_drv1;
  735         struct ifnet            *ifp = tp->tap_ifp;
  736         struct mbuf             *m = NULL;
  737         int                      error = 0, len, s;
  738 
  739         TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, minor(dev));
  740 
  741         mtx_lock(&tp->tap_mtx);
  742         if ((tp->tap_flags & TAP_READY) != TAP_READY) {
  743                 mtx_unlock(&tp->tap_mtx);
  744 
  745                 /* Unlocked read. */
  746                 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
  747                         ifp->if_xname, minor(dev), tp->tap_flags);
  748 
  749                 return (EHOSTDOWN);
  750         }
  751 
  752         tp->tap_flags &= ~TAP_RWAIT;
  753         mtx_unlock(&tp->tap_mtx);
  754 
  755         /* sleep until we get a packet */
  756         do {
  757                 s = splimp();
  758                 IF_DEQUEUE(&ifp->if_snd, m);
  759                 splx(s);
  760 
  761                 if (m == NULL) {
  762                         if (flag & O_NONBLOCK)
  763                                 return (EWOULDBLOCK);
  764 
  765                         mtx_lock(&tp->tap_mtx);
  766                         tp->tap_flags |= TAP_RWAIT;
  767                         mtx_unlock(&tp->tap_mtx);
  768                         error = tsleep(tp,PCATCH|(PZERO+1),"taprd",0);
  769                         if (error)
  770                                 return (error);
  771                 }
  772         } while (m == NULL);
  773 
  774         /* feed packet to bpf */
  775         BPF_MTAP(ifp, m);
  776 
  777         /* xfer packet to user space */
  778         while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
  779                 len = min(uio->uio_resid, m->m_len);
  780                 if (len == 0)
  781                         break;
  782 
  783                 error = uiomove(mtod(m, void *), len, uio);
  784                 m = m_free(m);
  785         }
  786 
  787         if (m != NULL) {
  788                 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname, 
  789                         minor(dev));
  790                 m_freem(m);
  791         }
  792 
  793         return (error);
  794 } /* tapread */
  795 
  796 
  797 /*
  798  * tapwrite
  799  *
  800  * the cdevsw write interface - an atomic write is a packet - or else!
  801  */
  802 static int
  803 tapwrite(struct cdev *dev, struct uio *uio, int flag)
  804 {
  805         struct tap_softc        *tp = dev->si_drv1;
  806         struct ifnet            *ifp = tp->tap_ifp;
  807         struct mbuf             *m;
  808         int                      error = 0;
  809 
  810         TAPDEBUG("%s writting, minor = %#x\n", 
  811                 ifp->if_xname, minor(dev));
  812 
  813         if (uio->uio_resid == 0)
  814                 return (0);
  815 
  816         if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
  817                 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
  818                         ifp->if_xname, uio->uio_resid, minor(dev));
  819 
  820                 return (EIO);
  821         }
  822 
  823         if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN)) == NULL) {
  824                 ifp->if_ierrors ++;
  825                 return (error);
  826         }
  827 
  828         m->m_pkthdr.rcvif = ifp;
  829 
  830         /* Pass packet up to parent. */
  831         (*ifp->if_input)(ifp, m);
  832         ifp->if_ipackets ++; /* ibytes are counted in parent */
  833 
  834         return (0);
  835 } /* tapwrite */
  836 
  837 
  838 /*
  839  * tappoll
  840  *
  841  * the poll interface, this is only useful on reads
  842  * really. the write detect always returns true, write never blocks
  843  * anyway, it either accepts the packet or drops it
  844  */
  845 static int
  846 tappoll(struct cdev *dev, int events, struct thread *td)
  847 {
  848         struct tap_softc        *tp = dev->si_drv1;
  849         struct ifnet            *ifp = tp->tap_ifp;
  850         int                      s, revents = 0;
  851 
  852         TAPDEBUG("%s polling, minor = %#x\n", 
  853                 ifp->if_xname, minor(dev));
  854 
  855         s = splimp();
  856         if (events & (POLLIN | POLLRDNORM)) {
  857                 if (ifp->if_snd.ifq_len > 0) {
  858                         TAPDEBUG("%s have data in queue. len = %d, " \
  859                                 "minor = %#x\n", ifp->if_xname,
  860                                 ifp->if_snd.ifq_len, minor(dev));
  861 
  862                         revents |= (events & (POLLIN | POLLRDNORM));
  863                 } else {
  864                         TAPDEBUG("%s waiting for data, minor = %#x\n",
  865                                 ifp->if_xname, minor(dev));
  866 
  867                         selrecord(td, &tp->tap_rsel);
  868                 }
  869         }
  870 
  871         if (events & (POLLOUT | POLLWRNORM))
  872                 revents |= (events & (POLLOUT | POLLWRNORM));
  873 
  874         splx(s);
  875         return (revents);
  876 } /* tappoll */
  877 
  878 
  879 /*
  880  * tap_kqfilter
  881  *
  882  * support for kevent() system call
  883  */
  884 static int
  885 tapkqfilter(struct cdev *dev, struct knote *kn)
  886 {
  887         int                      s;
  888         struct tap_softc        *tp = dev->si_drv1;
  889         struct ifnet            *ifp = tp->tap_ifp;
  890 
  891         s = splimp();
  892         switch (kn->kn_filter) {
  893         case EVFILT_READ:
  894                 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
  895                         ifp->if_xname, minor(dev));
  896                 kn->kn_fop = &tap_read_filterops;
  897                 break;
  898 
  899         case EVFILT_WRITE:
  900                 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
  901                         ifp->if_xname, minor(dev));
  902                 kn->kn_fop = &tap_write_filterops;
  903                 break;
  904 
  905         default:
  906                 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
  907                         ifp->if_xname, minor(dev));
  908                 splx(s);
  909                 return (EINVAL);
  910                 /* NOT REACHED */
  911         }
  912         splx(s);
  913 
  914         kn->kn_hook = (caddr_t) dev;
  915         knlist_add(&tp->tap_rsel.si_note, kn, 0);
  916 
  917         return (0);
  918 } /* tapkqfilter */
  919 
  920 
  921 /*
  922  * tap_kqread
  923  * 
  924  * Return true if there is data in the interface queue
  925  */
  926 static int
  927 tapkqread(struct knote *kn, long hint)
  928 {
  929         int                      ret, s;
  930         struct cdev             *dev = (struct cdev *)(kn->kn_hook);
  931         struct tap_softc        *tp = dev->si_drv1;
  932         struct ifnet            *ifp = tp->tap_ifp;
  933 
  934         s = splimp();
  935         if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
  936                 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
  937                         ifp->if_xname, ifp->if_snd.ifq_len, minor(dev));
  938                 ret = 1;
  939         } else {
  940                 TAPDEBUG("%s waiting for data, minor = %#x\n",
  941                         ifp->if_xname, minor(dev));
  942                 ret = 0;
  943         }
  944         splx(s);
  945 
  946         return (ret);
  947 } /* tapkqread */
  948 
  949 
  950 /*
  951  * tap_kqwrite
  952  *
  953  * Always can write. Return the MTU in kn->data
  954  */
  955 static int
  956 tapkqwrite(struct knote *kn, long hint)
  957 {
  958         int                      s;
  959         struct tap_softc        *tp = ((struct cdev *) kn->kn_hook)->si_drv1;
  960         struct ifnet            *ifp = tp->tap_ifp;
  961 
  962         s = splimp();
  963         kn->kn_data = ifp->if_mtu;
  964         splx(s);
  965 
  966         return (1);
  967 } /* tapkqwrite */
  968 
  969 
  970 static void
  971 tapkqdetach(struct knote *kn)
  972 {
  973         struct tap_softc        *tp = ((struct cdev *) kn->kn_hook)->si_drv1;
  974 
  975         knlist_remove(&tp->tap_rsel.si_note, kn, 0);
  976 } /* tapkqdetach */
  977 

Cache object: 64a7115d226d72a336fb4b532513d5a5


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