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

Cache object: a092ea6e60df6e48754a0b63120ecf8c


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