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 /*      $NetBSD: if_tap.c,v 1.47.4.8 2010/12/09 04:11:39 riz Exp $      */
    2 
    3 /*
    4  *  Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation.
    5  *  All rights reserved.
    6  *
    7  *  Redistribution and use in source and binary forms, with or without
    8  *  modification, are permitted provided that the following conditions
    9  *  are met:
   10  *  1. Redistributions of source code must retain the above copyright
   11  *     notice, this list of conditions and the following disclaimer.
   12  *  2. Redistributions in binary form must reproduce the above copyright
   13  *     notice, this list of conditions and the following disclaimer in the
   14  *     documentation and/or other materials provided with the distribution.
   15  *
   16  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   17  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   18  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   19  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   20  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   26  *  POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 /*
   30  * tap(4) is a virtual Ethernet interface.  It appears as a real Ethernet
   31  * device to the system, but can also be accessed by userland through a
   32  * character device interface, which allows reading and injecting frames.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.47.4.8 2010/12/09 04:11:39 riz Exp $");
   37 
   38 #if defined(_KERNEL_OPT)
   39 #include "bpfilter.h"
   40 #include "opt_modular.h"
   41 #include "opt_compat_netbsd.h"
   42 #endif
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/kernel.h>
   47 #include <sys/malloc.h>
   48 #include <sys/conf.h>
   49 #include <sys/device.h>
   50 #include <sys/file.h>
   51 #include <sys/filedesc.h>
   52 #include <sys/ksyms.h>
   53 #include <sys/poll.h>
   54 #include <sys/proc.h>
   55 #include <sys/select.h>
   56 #include <sys/sockio.h>
   57 #if defined(COMPAT_40) || defined(MODULAR)
   58 #include <sys/sysctl.h>
   59 #endif
   60 #include <sys/kauth.h>
   61 #include <sys/mutex.h>
   62 #include <sys/simplelock.h>
   63 #include <sys/intr.h>
   64 
   65 #include <net/if.h>
   66 #include <net/if_dl.h>
   67 #include <net/if_ether.h>
   68 #include <net/if_media.h>
   69 #include <net/if_tap.h>
   70 #if NBPFILTER > 0
   71 #include <net/bpf.h>
   72 #endif
   73 
   74 #include <compat/sys/sockio.h>
   75 
   76 #if defined(COMPAT_40) || defined(MODULAR)
   77 /*
   78  * sysctl node management
   79  *
   80  * It's not really possible to use a SYSCTL_SETUP block with
   81  * current LKM implementation, so it is easier to just define
   82  * our own function.
   83  *
   84  * The handler function is a "helper" in Andrew Brown's sysctl
   85  * framework terminology.  It is used as a gateway for sysctl
   86  * requests over the nodes.
   87  *
   88  * tap_log allows the module to log creations of nodes and
   89  * destroy them all at once using sysctl_teardown.
   90  */
   91 static int tap_node;
   92 static int      tap_sysctl_handler(SYSCTLFN_PROTO);
   93 SYSCTL_SETUP_PROTO(sysctl_tap_setup);
   94 #endif
   95 
   96 /*
   97  * Since we're an Ethernet device, we need the 3 following
   98  * components: a leading struct device, a struct ethercom,
   99  * and also a struct ifmedia since we don't attach a PHY to
  100  * ourselves. We could emulate one, but there's no real
  101  * point.
  102  */
  103 
  104 struct tap_softc {
  105         device_t        sc_dev;
  106         struct ifmedia  sc_im;
  107         struct ethercom sc_ec;
  108         int             sc_flags;
  109 #define TAP_INUSE       0x00000001      /* tap device can only be opened once */
  110 #define TAP_ASYNCIO     0x00000002      /* user is using async I/O (SIGIO) on the device */
  111 #define TAP_NBIO        0x00000004      /* user wants calls to avoid blocking */
  112 #define TAP_GOING       0x00000008      /* interface is being destroyed */
  113         struct selinfo  sc_rsel;
  114         pid_t           sc_pgid; /* For async. IO */
  115         kmutex_t        sc_rdlock;
  116         struct simplelock       sc_kqlock;
  117         void            *sc_sih;
  118 };
  119 
  120 /* autoconf(9) glue */
  121 
  122 void    tapattach(int);
  123 
  124 static int      tap_match(device_t, cfdata_t, void *);
  125 static void     tap_attach(device_t, device_t, void *);
  126 static int      tap_detach(device_t, int);
  127 
  128 CFATTACH_DECL_NEW(tap, sizeof(struct tap_softc),
  129     tap_match, tap_attach, tap_detach, NULL);
  130 extern struct cfdriver tap_cd;
  131 
  132 /* Real device access routines */
  133 static int      tap_dev_close(struct tap_softc *);
  134 static int      tap_dev_read(int, struct uio *, int);
  135 static int      tap_dev_write(int, struct uio *, int);
  136 static int      tap_dev_ioctl(int, u_long, void *, struct lwp *);
  137 static int      tap_dev_poll(int, int, struct lwp *);
  138 static int      tap_dev_kqfilter(int, struct knote *);
  139 
  140 /* Fileops access routines */
  141 static int      tap_fops_close(file_t *);
  142 static int      tap_fops_read(file_t *, off_t *, struct uio *,
  143     kauth_cred_t, int);
  144 static int      tap_fops_write(file_t *, off_t *, struct uio *,
  145     kauth_cred_t, int);
  146 static int      tap_fops_ioctl(file_t *, u_long, void *);
  147 static int      tap_fops_poll(file_t *, int);
  148 static int      tap_fops_kqfilter(file_t *, struct knote *);
  149 
  150 static const struct fileops tap_fileops = {
  151         .fo_read = tap_fops_read,
  152         .fo_write = tap_fops_write,
  153         .fo_ioctl = tap_fops_ioctl,
  154         .fo_fcntl = fnullop_fcntl,
  155         .fo_poll = tap_fops_poll,
  156         .fo_stat = fbadop_stat,
  157         .fo_close = tap_fops_close,
  158         .fo_kqfilter = tap_fops_kqfilter,
  159         .fo_drain = fnullop_drain,
  160 };
  161 
  162 /* Helper for cloning open() */
  163 static int      tap_dev_cloner(struct lwp *);
  164 
  165 /* Character device routines */
  166 static int      tap_cdev_open(dev_t, int, int, struct lwp *);
  167 static int      tap_cdev_close(dev_t, int, int, struct lwp *);
  168 static int      tap_cdev_read(dev_t, struct uio *, int);
  169 static int      tap_cdev_write(dev_t, struct uio *, int);
  170 static int      tap_cdev_ioctl(dev_t, u_long, void *, int, struct lwp *);
  171 static int      tap_cdev_poll(dev_t, int, struct lwp *);
  172 static int      tap_cdev_kqfilter(dev_t, struct knote *);
  173 
  174 const struct cdevsw tap_cdevsw = {
  175         tap_cdev_open, tap_cdev_close,
  176         tap_cdev_read, tap_cdev_write,
  177         tap_cdev_ioctl, nostop, notty,
  178         tap_cdev_poll, nommap,
  179         tap_cdev_kqfilter,
  180         D_OTHER,
  181 };
  182 
  183 #define TAP_CLONER      0xfffff         /* Maximal minor value */
  184 
  185 /* kqueue-related routines */
  186 static void     tap_kqdetach(struct knote *);
  187 static int      tap_kqread(struct knote *, long);
  188 
  189 /*
  190  * Those are needed by the if_media interface.
  191  */
  192 
  193 static int      tap_mediachange(struct ifnet *);
  194 static void     tap_mediastatus(struct ifnet *, struct ifmediareq *);
  195 
  196 /*
  197  * Those are needed by the ifnet interface, and would typically be
  198  * there for any network interface driver.
  199  * Some other routines are optional: watchdog and drain.
  200  */
  201 
  202 static void     tap_start(struct ifnet *);
  203 static void     tap_stop(struct ifnet *, int);
  204 static int      tap_init(struct ifnet *);
  205 static int      tap_ioctl(struct ifnet *, u_long, void *);
  206 
  207 /* Internal functions */
  208 #if defined(COMPAT_40) || defined(MODULAR)
  209 static int      tap_lifaddr(struct ifnet *, u_long, struct ifaliasreq *);
  210 #endif
  211 static void     tap_softintr(void *);
  212 
  213 /*
  214  * tap is a clonable interface, although it is highly unrealistic for
  215  * an Ethernet device.
  216  *
  217  * Here are the bits needed for a clonable interface.
  218  */
  219 static int      tap_clone_create(struct if_clone *, int);
  220 static int      tap_clone_destroy(struct ifnet *);
  221 
  222 struct if_clone tap_cloners = IF_CLONE_INITIALIZER("tap",
  223                                         tap_clone_create,
  224                                         tap_clone_destroy);
  225 
  226 /* Helper functionis shared by the two cloning code paths */
  227 static struct tap_softc *       tap_clone_creator(int);
  228 int     tap_clone_destroyer(device_t);
  229 
  230 void
  231 tapattach(int n)
  232 {
  233         int error;
  234 
  235         error = config_cfattach_attach(tap_cd.cd_name, &tap_ca);
  236         if (error) {
  237                 aprint_error("%s: unable to register cfattach\n",
  238                     tap_cd.cd_name);
  239                 (void)config_cfdriver_detach(&tap_cd);
  240                 return;
  241         }
  242 
  243         if_clone_attach(&tap_cloners);
  244 }
  245 
  246 /* Pretty much useless for a pseudo-device */
  247 static int
  248 tap_match(device_t parent, cfdata_t cfdata, void *arg)
  249 {
  250 
  251         return (1);
  252 }
  253 
  254 void
  255 tap_attach(device_t parent, device_t self, void *aux)
  256 {
  257         struct tap_softc *sc = device_private(self);
  258         struct ifnet *ifp;
  259 #if defined(COMPAT_40) || defined(MODULAR)
  260         const struct sysctlnode *node;
  261         int error;
  262 #endif
  263         uint8_t enaddr[ETHER_ADDR_LEN] =
  264             { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
  265         char enaddrstr[3 * ETHER_ADDR_LEN];
  266         struct timeval tv;
  267         uint32_t ui;
  268 
  269         sc->sc_dev = self;
  270         sc->sc_sih = softint_establish(SOFTINT_CLOCK, tap_softintr, sc);
  271 
  272         if (!pmf_device_register(self, NULL, NULL))
  273                 aprint_error_dev(self, "couldn't establish power handler\n");
  274 
  275         /*
  276          * In order to obtain unique initial Ethernet address on a host,
  277          * do some randomisation using the current uptime.  It's not meant
  278          * for anything but avoiding hard-coding an address.
  279          */
  280         getmicrouptime(&tv);
  281         ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
  282         memcpy(enaddr+3, (uint8_t *)&ui, 3);
  283 
  284         aprint_verbose_dev(self, "Ethernet address %s\n",
  285             ether_snprintf(enaddrstr, sizeof(enaddrstr), enaddr));
  286 
  287         /*
  288          * Why 1000baseT? Why not? You can add more.
  289          *
  290          * Note that there are 3 steps: init, one or several additions to
  291          * list of supported media, and in the end, the selection of one
  292          * of them.
  293          */
  294         ifmedia_init(&sc->sc_im, 0, tap_mediachange, tap_mediastatus);
  295         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T, 0, NULL);
  296         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
  297         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX, 0, NULL);
  298         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
  299         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T, 0, NULL);
  300         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
  301         ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
  302         ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
  303 
  304         /*
  305          * One should note that an interface must do multicast in order
  306          * to support IPv6.
  307          */
  308         ifp = &sc->sc_ec.ec_if;
  309         strcpy(ifp->if_xname, device_xname(self));
  310         ifp->if_softc   = sc;
  311         ifp->if_flags   = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  312         ifp->if_ioctl   = tap_ioctl;
  313         ifp->if_start   = tap_start;
  314         ifp->if_stop    = tap_stop;
  315         ifp->if_init    = tap_init;
  316         IFQ_SET_READY(&ifp->if_snd);
  317 
  318         sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
  319 
  320         /* Those steps are mandatory for an Ethernet driver, the fisrt call
  321          * being common to all network interface drivers. */
  322         if_attach(ifp);
  323         ether_ifattach(ifp, enaddr);
  324 
  325         sc->sc_flags = 0;
  326 
  327 #if defined(COMPAT_40) || defined(MODULAR)
  328         /*
  329          * Add a sysctl node for that interface.
  330          *
  331          * The pointer transmitted is not a string, but instead a pointer to
  332          * the softc structure, which we can use to build the string value on
  333          * the fly in the helper function of the node.  See the comments for
  334          * tap_sysctl_handler for details.
  335          *
  336          * Usually sysctl_createv is called with CTL_CREATE as the before-last
  337          * component.  However, we can allocate a number ourselves, as we are
  338          * the only consumer of the net.link.<iface> node.  In this case, the
  339          * unit number is conveniently used to number the node.  CTL_CREATE
  340          * would just work, too.
  341          */
  342         if ((error = sysctl_createv(NULL, 0, NULL,
  343             &node, CTLFLAG_READWRITE,
  344             CTLTYPE_STRING, device_xname(self), NULL,
  345             tap_sysctl_handler, 0, sc, 18,
  346             CTL_NET, AF_LINK, tap_node, device_unit(sc->sc_dev),
  347             CTL_EOL)) != 0)
  348                 aprint_error_dev(self, "sysctl_createv returned %d, ignoring\n",
  349                     error);
  350 #endif
  351 
  352         /*
  353          * Initialize the two locks for the device.
  354          *
  355          * We need a lock here because even though the tap device can be
  356          * opened only once, the file descriptor might be passed to another
  357          * process, say a fork(2)ed child.
  358          *
  359          * The Giant saves us from most of the hassle, but since the read
  360          * operation can sleep, we don't want two processes to wake up at
  361          * the same moment and both try and dequeue a single packet.
  362          *
  363          * The queue for event listeners (used by kqueue(9), see below) has
  364          * to be protected, too, but we don't need the same level of
  365          * complexity for that lock, so a simple spinning lock is fine.
  366          */
  367         mutex_init(&sc->sc_rdlock, MUTEX_DEFAULT, IPL_NONE);
  368         simple_lock_init(&sc->sc_kqlock);
  369 
  370         selinit(&sc->sc_rsel);
  371 }
  372 
  373 /*
  374  * When detaching, we do the inverse of what is done in the attach
  375  * routine, in reversed order.
  376  */
  377 static int
  378 tap_detach(device_t self, int flags)
  379 {
  380         struct tap_softc *sc = device_private(self);
  381         struct ifnet *ifp = &sc->sc_ec.ec_if;
  382 #if defined(COMPAT_40) || defined(MODULAR)
  383         int error;
  384 #endif
  385         int s;
  386 
  387         sc->sc_flags |= TAP_GOING;
  388         s = splnet();
  389         tap_stop(ifp, 1);
  390         if_down(ifp);
  391         splx(s);
  392 
  393         softint_disestablish(sc->sc_sih);
  394 
  395 #if defined(COMPAT_40) || defined(MODULAR)
  396         /*
  397          * Destroying a single leaf is a very straightforward operation using
  398          * sysctl_destroyv.  One should be sure to always end the path with
  399          * CTL_EOL.
  400          */
  401         if ((error = sysctl_destroyv(NULL, CTL_NET, AF_LINK, tap_node,
  402             device_unit(sc->sc_dev), CTL_EOL)) != 0)
  403                 aprint_error_dev(self,
  404                     "sysctl_destroyv returned %d, ignoring\n", error);
  405 #endif
  406         ether_ifdetach(ifp);
  407         if_detach(ifp);
  408         ifmedia_delete_instance(&sc->sc_im, IFM_INST_ANY);
  409         seldestroy(&sc->sc_rsel);
  410         mutex_destroy(&sc->sc_rdlock);
  411 
  412         pmf_device_deregister(self);
  413 
  414         return (0);
  415 }
  416 
  417 /*
  418  * This function is called by the ifmedia layer to notify the driver
  419  * that the user requested a media change.  A real driver would
  420  * reconfigure the hardware.
  421  */
  422 static int
  423 tap_mediachange(struct ifnet *ifp)
  424 {
  425         return (0);
  426 }
  427 
  428 /*
  429  * Here the user asks for the currently used media.
  430  */
  431 static void
  432 tap_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
  433 {
  434         struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
  435         imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
  436 }
  437 
  438 /*
  439  * This is the function where we SEND packets.
  440  *
  441  * There is no 'receive' equivalent.  A typical driver will get
  442  * interrupts from the hardware, and from there will inject new packets
  443  * into the network stack.
  444  *
  445  * Once handled, a packet must be freed.  A real driver might not be able
  446  * to fit all the pending packets into the hardware, and is allowed to
  447  * return before having sent all the packets.  It should then use the
  448  * if_flags flag IFF_OACTIVE to notify the upper layer.
  449  *
  450  * There are also other flags one should check, such as IFF_PAUSE.
  451  *
  452  * It is our duty to make packets available to BPF listeners.
  453  *
  454  * You should be aware that this function is called by the Ethernet layer
  455  * at splnet().
  456  *
  457  * When the device is opened, we have to pass the packet(s) to the
  458  * userland.  For that we stay in OACTIVE mode while the userland gets
  459  * the packets, and we send a signal to the processes waiting to read.
  460  *
  461  * wakeup(sc) is the counterpart to the tsleep call in
  462  * tap_dev_read, while selnotify() is used for kevent(2) and
  463  * poll(2) (which includes select(2)) listeners.
  464  */
  465 static void
  466 tap_start(struct ifnet *ifp)
  467 {
  468         struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
  469         struct mbuf *m0;
  470 
  471         if ((sc->sc_flags & TAP_INUSE) == 0) {
  472                 /* Simply drop packets */
  473                 for(;;) {
  474                         IFQ_DEQUEUE(&ifp->if_snd, m0);
  475                         if (m0 == NULL)
  476                                 return;
  477 
  478                         ifp->if_opackets++;
  479 #if NBPFILTER > 0
  480                         if (ifp->if_bpf)
  481                                 bpf_mtap(ifp->if_bpf, m0);
  482 #endif
  483 
  484                         m_freem(m0);
  485                 }
  486         } else if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
  487                 ifp->if_flags |= IFF_OACTIVE;
  488                 wakeup(sc);
  489                 selnotify(&sc->sc_rsel, 0, 1);
  490                 if (sc->sc_flags & TAP_ASYNCIO)
  491                         softint_schedule(sc->sc_sih);
  492         }
  493 }
  494 
  495 static void
  496 tap_softintr(void *cookie)
  497 {
  498         struct tap_softc *sc;
  499         struct ifnet *ifp;
  500         int a, b;
  501 
  502         sc = cookie;
  503 
  504         if (sc->sc_flags & TAP_ASYNCIO) {
  505                 ifp = &sc->sc_ec.ec_if;
  506                 if (ifp->if_flags & IFF_RUNNING) {
  507                         a = POLL_IN;
  508                         b = POLLIN|POLLRDNORM;
  509                 } else {
  510                         a = POLL_HUP;
  511                         b = 0;
  512                 }
  513                 fownsignal(sc->sc_pgid, SIGIO, a, b, NULL);
  514         }
  515 }
  516 
  517 /*
  518  * A typical driver will only contain the following handlers for
  519  * ioctl calls, except SIOCSIFPHYADDR.
  520  * The latter is a hack I used to set the Ethernet address of the
  521  * faked device.
  522  *
  523  * Note that both ifmedia_ioctl() and ether_ioctl() have to be
  524  * called under splnet().
  525  */
  526 static int
  527 tap_ioctl(struct ifnet *ifp, u_long cmd, void *data)
  528 {
  529         struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
  530         struct ifreq *ifr = (struct ifreq *)data;
  531         int s, error;
  532 
  533         s = splnet();
  534 
  535         switch (cmd) {
  536 #ifdef OSIOCSIFMEDIA
  537         case OSIOCSIFMEDIA:
  538 #endif
  539         case SIOCSIFMEDIA:
  540         case SIOCGIFMEDIA:
  541                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
  542                 break;
  543 #if defined(COMPAT_40) || defined(MODULAR)
  544         case SIOCSIFPHYADDR:
  545                 error = tap_lifaddr(ifp, cmd, (struct ifaliasreq *)data);
  546                 break;
  547 #endif
  548         default:
  549                 error = ether_ioctl(ifp, cmd, data);
  550                 if (error == ENETRESET)
  551                         error = 0;
  552                 break;
  553         }
  554 
  555         splx(s);
  556 
  557         return (error);
  558 }
  559 
  560 #if defined(COMPAT_40) || defined(MODULAR)
  561 /*
  562  * Helper function to set Ethernet address.  This has been replaced by
  563  * the generic SIOCALIFADDR ioctl on a PF_LINK socket.
  564  */
  565 static int
  566 tap_lifaddr(struct ifnet *ifp, u_long cmd, struct ifaliasreq *ifra)
  567 {
  568         const struct sockaddr *sa = &ifra->ifra_addr;
  569 
  570         if (sa->sa_family != AF_LINK)
  571                 return (EINVAL);
  572 
  573         if_set_sadl(ifp, sa->sa_data, ETHER_ADDR_LEN);
  574 
  575         return (0);
  576 }
  577 #endif
  578 
  579 /*
  580  * _init() would typically be called when an interface goes up,
  581  * meaning it should configure itself into the state in which it
  582  * can send packets.
  583  */
  584 static int
  585 tap_init(struct ifnet *ifp)
  586 {
  587         ifp->if_flags |= IFF_RUNNING;
  588 
  589         tap_start(ifp);
  590 
  591         return (0);
  592 }
  593 
  594 /*
  595  * _stop() is called when an interface goes down.  It is our
  596  * responsability to validate that state by clearing the
  597  * IFF_RUNNING flag.
  598  *
  599  * We have to wake up all the sleeping processes to have the pending
  600  * read requests cancelled.
  601  */
  602 static void
  603 tap_stop(struct ifnet *ifp, int disable)
  604 {
  605         struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
  606 
  607         ifp->if_flags &= ~IFF_RUNNING;
  608         wakeup(sc);
  609         selnotify(&sc->sc_rsel, 0, 1);
  610         if (sc->sc_flags & TAP_ASYNCIO)
  611                 softint_schedule(sc->sc_sih);
  612 }
  613 
  614 /*
  615  * The 'create' command of ifconfig can be used to create
  616  * any numbered instance of a given device.  Thus we have to
  617  * make sure we have enough room in cd_devs to create the
  618  * user-specified instance.  config_attach_pseudo will do this
  619  * for us.
  620  */
  621 static int
  622 tap_clone_create(struct if_clone *ifc, int unit)
  623 {
  624         if (tap_clone_creator(unit) == NULL) {
  625                 aprint_error("%s%d: unable to attach an instance\n",
  626                     tap_cd.cd_name, unit);
  627                 return (ENXIO);
  628         }
  629 
  630         return (0);
  631 }
  632 
  633 /*
  634  * tap(4) can be cloned by two ways:
  635  *   using 'ifconfig tap0 create', which will use the network
  636  *     interface cloning API, and call tap_clone_create above.
  637  *   opening the cloning device node, whose minor number is TAP_CLONER.
  638  *     See below for an explanation on how this part work.
  639  */
  640 static struct tap_softc *
  641 tap_clone_creator(int unit)
  642 {
  643         struct cfdata *cf;
  644 
  645         cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
  646         cf->cf_name = tap_cd.cd_name;
  647         cf->cf_atname = tap_ca.ca_name;
  648         if (unit == -1) {
  649                 /* let autoconf find the first free one */
  650                 cf->cf_unit = 0;
  651                 cf->cf_fstate = FSTATE_STAR;
  652         } else {
  653                 cf->cf_unit = unit;
  654                 cf->cf_fstate = FSTATE_FOUND;
  655         }
  656 
  657         return device_private(config_attach_pseudo(cf));
  658 }
  659 
  660 /*
  661  * The clean design of if_clone and autoconf(9) makes that part
  662  * really straightforward.  The second argument of config_detach
  663  * means neither QUIET nor FORCED.
  664  */
  665 static int
  666 tap_clone_destroy(struct ifnet *ifp)
  667 {
  668         struct tap_softc *sc = ifp->if_softc;
  669 
  670         return tap_clone_destroyer(sc->sc_dev);
  671 }
  672 
  673 int
  674 tap_clone_destroyer(device_t dev)
  675 {
  676         cfdata_t cf = device_cfdata(dev);
  677         int error;
  678 
  679         if ((error = config_detach(dev, 0)) != 0)
  680                 aprint_error_dev(dev, "unable to detach instance\n");
  681         free(cf, M_DEVBUF);
  682 
  683         return (error);
  684 }
  685 
  686 /*
  687  * tap(4) is a bit of an hybrid device.  It can be used in two different
  688  * ways:
  689  *  1. ifconfig tapN create, then use /dev/tapN to read/write off it.
  690  *  2. open /dev/tap, get a new interface created and read/write off it.
  691  *     That interface is destroyed when the process that had it created exits.
  692  *
  693  * The first way is managed by the cdevsw structure, and you access interfaces
  694  * through a (major, minor) mapping:  tap4 is obtained by the minor number
  695  * 4.  The entry points for the cdevsw interface are prefixed by tap_cdev_.
  696  *
  697  * The second way is the so-called "cloning" device.  It's a special minor
  698  * number (chosen as the maximal number, to allow as much tap devices as
  699  * possible).  The user first opens the cloner (e.g., /dev/tap), and that
  700  * call ends in tap_cdev_open.  The actual place where it is handled is
  701  * tap_dev_cloner.
  702  *
  703  * An tap device cannot be opened more than once at a time, so the cdevsw
  704  * part of open() does nothing but noting that the interface is being used and
  705  * hence ready to actually handle packets.
  706  */
  707 
  708 static int
  709 tap_cdev_open(dev_t dev, int flags, int fmt, struct lwp *l)
  710 {
  711         struct tap_softc *sc;
  712 
  713         if (minor(dev) == TAP_CLONER)
  714                 return tap_dev_cloner(l);
  715 
  716         sc = device_lookup_private(&tap_cd, minor(dev));
  717         if (sc == NULL)
  718                 return (ENXIO);
  719 
  720         /* The device can only be opened once */
  721         if (sc->sc_flags & TAP_INUSE)
  722                 return (EBUSY);
  723         sc->sc_flags |= TAP_INUSE;
  724         return (0);
  725 }
  726 
  727 /*
  728  * There are several kinds of cloning devices, and the most simple is the one
  729  * tap(4) uses.  What it does is change the file descriptor with a new one,
  730  * with its own fileops structure (which maps to the various read, write,
  731  * ioctl functions).  It starts allocating a new file descriptor with falloc,
  732  * then actually creates the new tap devices.
  733  *
  734  * Once those two steps are successful, we can re-wire the existing file
  735  * descriptor to its new self.  This is done with fdclone():  it fills the fp
  736  * structure as needed (notably f_data gets filled with the fifth parameter
  737  * passed, the unit of the tap device which will allows us identifying the
  738  * device later), and returns EMOVEFD.
  739  *
  740  * That magic value is interpreted by sys_open() which then replaces the
  741  * current file descriptor by the new one (through a magic member of struct
  742  * lwp, l_dupfd).
  743  *
  744  * The tap device is flagged as being busy since it otherwise could be
  745  * externally accessed through the corresponding device node with the cdevsw
  746  * interface.
  747  */
  748 
  749 static int
  750 tap_dev_cloner(struct lwp *l)
  751 {
  752         struct tap_softc *sc;
  753         file_t *fp;
  754         int error, fd;
  755 
  756         if ((error = fd_allocfile(&fp, &fd)) != 0)
  757                 return (error);
  758 
  759         if ((sc = tap_clone_creator(-1)) == NULL) {
  760                 fd_abort(curproc, fp, fd);
  761                 return (ENXIO);
  762         }
  763 
  764         sc->sc_flags |= TAP_INUSE;
  765 
  766         return fd_clone(fp, fd, FREAD|FWRITE, &tap_fileops,
  767             (void *)(intptr_t)device_unit(sc->sc_dev));
  768 }
  769 
  770 /*
  771  * While all other operations (read, write, ioctl, poll and kqfilter) are
  772  * really the same whether we are in cdevsw or fileops mode, the close()
  773  * function is slightly different in the two cases.
  774  *
  775  * As for the other, the core of it is shared in tap_dev_close.  What
  776  * it does is sufficient for the cdevsw interface, but the cloning interface
  777  * needs another thing:  the interface is destroyed when the processes that
  778  * created it closes it.
  779  */
  780 static int
  781 tap_cdev_close(dev_t dev, int flags, int fmt,
  782     struct lwp *l)
  783 {
  784         struct tap_softc *sc =
  785             device_lookup_private(&tap_cd, minor(dev));
  786 
  787         if (sc == NULL)
  788                 return (ENXIO);
  789 
  790         return tap_dev_close(sc);
  791 }
  792 
  793 /*
  794  * It might happen that the administrator used ifconfig to externally destroy
  795  * the interface.  In that case, tap_fops_close will be called while
  796  * tap_detach is already happening.  If we called it again from here, we
  797  * would dead lock.  TAP_GOING ensures that this situation doesn't happen.
  798  */
  799 static int
  800 tap_fops_close(file_t *fp)
  801 {
  802         int unit = (intptr_t)fp->f_data;
  803         struct tap_softc *sc;
  804         int error;
  805 
  806         sc = device_lookup_private(&tap_cd, unit);
  807         if (sc == NULL)
  808                 return (ENXIO);
  809 
  810         /* tap_dev_close currently always succeeds, but it might not
  811          * always be the case. */
  812         KERNEL_LOCK(1, NULL);
  813         if ((error = tap_dev_close(sc)) != 0) {
  814                 KERNEL_UNLOCK_ONE(NULL);
  815                 return (error);
  816         }
  817 
  818         /* Destroy the device now that it is no longer useful,
  819          * unless it's already being destroyed. */
  820         if ((sc->sc_flags & TAP_GOING) != 0) {
  821                 KERNEL_UNLOCK_ONE(NULL);
  822                 return (0);
  823         }
  824 
  825         error = tap_clone_destroyer(sc->sc_dev);
  826         KERNEL_UNLOCK_ONE(NULL);
  827         return error;
  828 }
  829 
  830 static int
  831 tap_dev_close(struct tap_softc *sc)
  832 {
  833         struct ifnet *ifp;
  834         int s;
  835 
  836         s = splnet();
  837         /* Let tap_start handle packets again */
  838         ifp = &sc->sc_ec.ec_if;
  839         ifp->if_flags &= ~IFF_OACTIVE;
  840 
  841         /* Purge output queue */
  842         if (!(IFQ_IS_EMPTY(&ifp->if_snd))) {
  843                 struct mbuf *m;
  844 
  845                 for (;;) {
  846                         IFQ_DEQUEUE(&ifp->if_snd, m);
  847                         if (m == NULL)
  848                                 break;
  849 
  850                         ifp->if_opackets++;
  851 #if NBPFILTER > 0
  852                         if (ifp->if_bpf)
  853                                 bpf_mtap(ifp->if_bpf, m);
  854 #endif
  855                         m_freem(m);
  856                 }
  857         }
  858         splx(s);
  859 
  860         sc->sc_flags &= ~(TAP_INUSE | TAP_ASYNCIO);
  861 
  862         return (0);
  863 }
  864 
  865 static int
  866 tap_cdev_read(dev_t dev, struct uio *uio, int flags)
  867 {
  868         return tap_dev_read(minor(dev), uio, flags);
  869 }
  870 
  871 static int
  872 tap_fops_read(file_t *fp, off_t *offp, struct uio *uio,
  873     kauth_cred_t cred, int flags)
  874 {
  875         int error;
  876 
  877         KERNEL_LOCK(1, NULL);
  878         error = tap_dev_read((intptr_t)fp->f_data, uio, flags);
  879         KERNEL_UNLOCK_ONE(NULL);
  880         return error;
  881 }
  882 
  883 static int
  884 tap_dev_read(int unit, struct uio *uio, int flags)
  885 {
  886         struct tap_softc *sc =
  887             device_lookup_private(&tap_cd, unit);
  888         struct ifnet *ifp;
  889         struct mbuf *m, *n;
  890         int error = 0, s;
  891 
  892         if (sc == NULL)
  893                 return (ENXIO);
  894 
  895         ifp = &sc->sc_ec.ec_if;
  896         if ((ifp->if_flags & IFF_UP) == 0)
  897                 return (EHOSTDOWN);
  898 
  899         /*
  900          * In the TAP_NBIO case, we have to make sure we won't be sleeping
  901          */
  902         if ((sc->sc_flags & TAP_NBIO) != 0) {
  903                 if (!mutex_tryenter(&sc->sc_rdlock))
  904                         return (EWOULDBLOCK);
  905         } else {
  906                 mutex_enter(&sc->sc_rdlock);
  907         }
  908 
  909         s = splnet();
  910         if (IFQ_IS_EMPTY(&ifp->if_snd)) {
  911                 ifp->if_flags &= ~IFF_OACTIVE;
  912                 /*
  913                  * We must release the lock before sleeping, and re-acquire it
  914                  * after.
  915                  */
  916                 mutex_exit(&sc->sc_rdlock);
  917                 if (sc->sc_flags & TAP_NBIO)
  918                         error = EWOULDBLOCK;
  919                 else
  920                         error = tsleep(sc, PSOCK|PCATCH, "tap", 0);
  921                 splx(s);
  922 
  923                 if (error != 0)
  924                         return (error);
  925                 /* The device might have been downed */
  926                 if ((ifp->if_flags & IFF_UP) == 0)
  927                         return (EHOSTDOWN);
  928                 if ((sc->sc_flags & TAP_NBIO)) {
  929                         if (!mutex_tryenter(&sc->sc_rdlock))
  930                                 return (EWOULDBLOCK);
  931                 } else {
  932                         mutex_enter(&sc->sc_rdlock);
  933                 }
  934                 s = splnet();
  935         }
  936 
  937         IFQ_DEQUEUE(&ifp->if_snd, m);
  938         ifp->if_flags &= ~IFF_OACTIVE;
  939         splx(s);
  940         if (m == NULL) {
  941                 error = 0;
  942                 goto out;
  943         }
  944 
  945         ifp->if_opackets++;
  946 #if NBPFILTER > 0
  947         if (ifp->if_bpf)
  948                 bpf_mtap(ifp->if_bpf, m);
  949 #endif
  950 
  951         /*
  952          * One read is one packet.
  953          */
  954         do {
  955                 error = uiomove(mtod(m, void *),
  956                     min(m->m_len, uio->uio_resid), uio);
  957                 MFREE(m, n);
  958                 m = n;
  959         } while (m != NULL && uio->uio_resid > 0 && error == 0);
  960 
  961         if (m != NULL)
  962                 m_freem(m);
  963 
  964 out:
  965         mutex_exit(&sc->sc_rdlock);
  966         return (error);
  967 }
  968 
  969 static int
  970 tap_cdev_write(dev_t dev, struct uio *uio, int flags)
  971 {
  972         return tap_dev_write(minor(dev), uio, flags);
  973 }
  974 
  975 static int
  976 tap_fops_write(file_t *fp, off_t *offp, struct uio *uio,
  977     kauth_cred_t cred, int flags)
  978 {
  979         int error;
  980 
  981         KERNEL_LOCK(1, NULL);
  982         error = tap_dev_write((intptr_t)fp->f_data, uio, flags);
  983         KERNEL_UNLOCK_ONE(NULL);
  984         return error;
  985 }
  986 
  987 static int
  988 tap_dev_write(int unit, struct uio *uio, int flags)
  989 {
  990         struct tap_softc *sc =
  991             device_lookup_private(&tap_cd, unit);
  992         struct ifnet *ifp;
  993         struct mbuf *m, **mp;
  994         int error = 0;
  995         int s;
  996 
  997         if (sc == NULL)
  998                 return (ENXIO);
  999 
 1000         ifp = &sc->sc_ec.ec_if;
 1001 
 1002         /* One write, one packet, that's the rule */
 1003         MGETHDR(m, M_DONTWAIT, MT_DATA);
 1004         if (m == NULL) {
 1005                 ifp->if_ierrors++;
 1006                 return (ENOBUFS);
 1007         }
 1008         m->m_pkthdr.len = uio->uio_resid;
 1009 
 1010         mp = &m;
 1011         while (error == 0 && uio->uio_resid > 0) {
 1012                 if (*mp != m) {
 1013                         MGET(*mp, M_DONTWAIT, MT_DATA);
 1014                         if (*mp == NULL) {
 1015                                 error = ENOBUFS;
 1016                                 break;
 1017                         }
 1018                 }
 1019                 (*mp)->m_len = min(MHLEN, uio->uio_resid);
 1020                 error = uiomove(mtod(*mp, void *), (*mp)->m_len, uio);
 1021                 mp = &(*mp)->m_next;
 1022         }
 1023         if (error) {
 1024                 ifp->if_ierrors++;
 1025                 m_freem(m);
 1026                 return (error);
 1027         }
 1028 
 1029         ifp->if_ipackets++;
 1030         m->m_pkthdr.rcvif = ifp;
 1031 
 1032 #if NBPFILTER > 0
 1033         if (ifp->if_bpf)
 1034                 bpf_mtap(ifp->if_bpf, m);
 1035 #endif
 1036         s =splnet();
 1037         (*ifp->if_input)(ifp, m);
 1038         splx(s);
 1039 
 1040         return (0);
 1041 }
 1042 
 1043 static int
 1044 tap_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flags,
 1045     struct lwp *l)
 1046 {
 1047         return tap_dev_ioctl(minor(dev), cmd, data, l);
 1048 }
 1049 
 1050 static int
 1051 tap_fops_ioctl(file_t *fp, u_long cmd, void *data)
 1052 {
 1053         return tap_dev_ioctl((intptr_t)fp->f_data, cmd, data, curlwp);
 1054 }
 1055 
 1056 static int
 1057 tap_dev_ioctl(int unit, u_long cmd, void *data, struct lwp *l)
 1058 {
 1059         struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
 1060 
 1061         if (sc == NULL)
 1062                 return ENXIO;
 1063 
 1064         switch (cmd) {
 1065         case FIONREAD:
 1066                 {
 1067                         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1068                         struct mbuf *m;
 1069                         int s;
 1070 
 1071                         s = splnet();
 1072                         IFQ_POLL(&ifp->if_snd, m);
 1073 
 1074                         if (m == NULL)
 1075                                 *(int *)data = 0;
 1076                         else
 1077                                 *(int *)data = m->m_pkthdr.len;
 1078                         splx(s);
 1079                         return 0;
 1080                 } 
 1081         case TIOCSPGRP:
 1082         case FIOSETOWN:
 1083                 return fsetown(&sc->sc_pgid, cmd, data);
 1084         case TIOCGPGRP:
 1085         case FIOGETOWN:
 1086                 return fgetown(sc->sc_pgid, cmd, data);
 1087         case FIOASYNC:
 1088                 if (*(int *)data)
 1089                         sc->sc_flags |= TAP_ASYNCIO;
 1090                 else
 1091                         sc->sc_flags &= ~TAP_ASYNCIO;
 1092                 return 0;
 1093         case FIONBIO:
 1094                 if (*(int *)data)
 1095                         sc->sc_flags |= TAP_NBIO;
 1096                 else
 1097                         sc->sc_flags &= ~TAP_NBIO;
 1098                 return 0;
 1099 #ifdef OTAPGIFNAME
 1100         case OTAPGIFNAME:
 1101 #endif
 1102         case TAPGIFNAME:
 1103                 {
 1104                         struct ifreq *ifr = (struct ifreq *)data;
 1105                         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1106 
 1107                         strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
 1108                         return 0;
 1109                 }
 1110         default:
 1111                 return ENOTTY;
 1112         }
 1113 }
 1114 
 1115 static int
 1116 tap_cdev_poll(dev_t dev, int events, struct lwp *l)
 1117 {
 1118         return tap_dev_poll(minor(dev), events, l);
 1119 }
 1120 
 1121 static int
 1122 tap_fops_poll(file_t *fp, int events)
 1123 {
 1124         return tap_dev_poll((intptr_t)fp->f_data, events, curlwp);
 1125 }
 1126 
 1127 static int
 1128 tap_dev_poll(int unit, int events, struct lwp *l)
 1129 {
 1130         struct tap_softc *sc =
 1131             device_lookup_private(&tap_cd, unit);
 1132         int revents = 0;
 1133 
 1134         if (sc == NULL)
 1135                 return POLLERR;
 1136 
 1137         if (events & (POLLIN|POLLRDNORM)) {
 1138                 struct ifnet *ifp = &sc->sc_ec.ec_if;
 1139                 struct mbuf *m;
 1140                 int s;
 1141 
 1142                 s = splnet();
 1143                 IFQ_POLL(&ifp->if_snd, m);
 1144                 splx(s);
 1145 
 1146                 if (m != NULL)
 1147                         revents |= events & (POLLIN|POLLRDNORM);
 1148                 else {
 1149                         simple_lock(&sc->sc_kqlock);
 1150                         selrecord(l, &sc->sc_rsel);
 1151                         simple_unlock(&sc->sc_kqlock);
 1152                 }
 1153         }
 1154         revents |= events & (POLLOUT|POLLWRNORM);
 1155 
 1156         return (revents);
 1157 }
 1158 
 1159 static struct filterops tap_read_filterops = { 1, NULL, tap_kqdetach,
 1160         tap_kqread };
 1161 static struct filterops tap_seltrue_filterops = { 1, NULL, tap_kqdetach,
 1162         filt_seltrue };
 1163 
 1164 static int
 1165 tap_cdev_kqfilter(dev_t dev, struct knote *kn)
 1166 {
 1167         return tap_dev_kqfilter(minor(dev), kn);
 1168 }
 1169 
 1170 static int
 1171 tap_fops_kqfilter(file_t *fp, struct knote *kn)
 1172 {
 1173         return tap_dev_kqfilter((intptr_t)fp->f_data, kn);
 1174 }
 1175 
 1176 static int
 1177 tap_dev_kqfilter(int unit, struct knote *kn)
 1178 {
 1179         struct tap_softc *sc =
 1180             device_lookup_private(&tap_cd, unit);
 1181 
 1182         if (sc == NULL)
 1183                 return (ENXIO);
 1184 
 1185         KERNEL_LOCK(1, NULL);
 1186         switch(kn->kn_filter) {
 1187         case EVFILT_READ:
 1188                 kn->kn_fop = &tap_read_filterops;
 1189                 break;
 1190         case EVFILT_WRITE:
 1191                 kn->kn_fop = &tap_seltrue_filterops;
 1192                 break;
 1193         default:
 1194                 KERNEL_UNLOCK_ONE(NULL);
 1195                 return (EINVAL);
 1196         }
 1197 
 1198         kn->kn_hook = sc;
 1199         simple_lock(&sc->sc_kqlock);
 1200         SLIST_INSERT_HEAD(&sc->sc_rsel.sel_klist, kn, kn_selnext);
 1201         simple_unlock(&sc->sc_kqlock);
 1202         KERNEL_UNLOCK_ONE(NULL);
 1203         return (0);
 1204 }
 1205 
 1206 static void
 1207 tap_kqdetach(struct knote *kn)
 1208 {
 1209         struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
 1210 
 1211         KERNEL_LOCK(1, NULL);
 1212         simple_lock(&sc->sc_kqlock);
 1213         SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
 1214         simple_unlock(&sc->sc_kqlock);
 1215         KERNEL_UNLOCK_ONE(NULL);
 1216 }
 1217 
 1218 static int
 1219 tap_kqread(struct knote *kn, long hint)
 1220 {
 1221         struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
 1222         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1223         struct mbuf *m;
 1224         int s, rv;
 1225 
 1226         KERNEL_LOCK(1, NULL);
 1227         s = splnet();
 1228         IFQ_POLL(&ifp->if_snd, m);
 1229 
 1230         if (m == NULL)
 1231                 kn->kn_data = 0;
 1232         else
 1233                 kn->kn_data = m->m_pkthdr.len;
 1234         splx(s);
 1235         rv = (kn->kn_data != 0 ? 1 : 0);
 1236         KERNEL_UNLOCK_ONE(NULL);
 1237         return rv;
 1238 }
 1239 
 1240 #if defined(COMPAT_40) || defined(MODULAR)
 1241 /*
 1242  * sysctl management routines
 1243  * You can set the address of an interface through:
 1244  * net.link.tap.tap<number>
 1245  *
 1246  * Note the consistent use of tap_log in order to use
 1247  * sysctl_teardown at unload time.
 1248  *
 1249  * In the kernel you will find a lot of SYSCTL_SETUP blocks.  Those
 1250  * blocks register a function in a special section of the kernel
 1251  * (called a link set) which is used at init_sysctl() time to cycle
 1252  * through all those functions to create the kernel's sysctl tree.
 1253  *
 1254  * It is not (currently) possible to use link sets in a LKM, so the
 1255  * easiest is to simply call our own setup routine at load time.
 1256  *
 1257  * In the SYSCTL_SETUP blocks you find in the kernel, nodes have the
 1258  * CTLFLAG_PERMANENT flag, meaning they cannot be removed.  Once the
 1259  * whole kernel sysctl tree is built, it is not possible to add any
 1260  * permanent node.
 1261  *
 1262  * It should be noted that we're not saving the sysctlnode pointer
 1263  * we are returned when creating the "tap" node.  That structure
 1264  * cannot be trusted once out of the calling function, as it might
 1265  * get reused.  So we just save the MIB number, and always give the
 1266  * full path starting from the root for later calls to sysctl_createv
 1267  * and sysctl_destroyv.
 1268  */
 1269 SYSCTL_SETUP(sysctl_tap_setup, "sysctl net.link.tap subtree setup")
 1270 {
 1271         const struct sysctlnode *node;
 1272         int error = 0;
 1273 
 1274         if ((error = sysctl_createv(clog, 0, NULL, NULL,
 1275             CTLFLAG_PERMANENT,
 1276             CTLTYPE_NODE, "net", NULL,
 1277             NULL, 0, NULL, 0,
 1278             CTL_NET, CTL_EOL)) != 0)
 1279                 return;
 1280 
 1281         if ((error = sysctl_createv(clog, 0, NULL, NULL,
 1282             CTLFLAG_PERMANENT,
 1283             CTLTYPE_NODE, "link", NULL,
 1284             NULL, 0, NULL, 0,
 1285             CTL_NET, AF_LINK, CTL_EOL)) != 0)
 1286                 return;
 1287 
 1288         /*
 1289          * The first four parameters of sysctl_createv are for management.
 1290          *
 1291          * The four that follows, here starting with a '' for the flags,
 1292          * describe the node.
 1293          *
 1294          * The next series of four set its value, through various possible
 1295          * means.
 1296          *
 1297          * Last but not least, the path to the node is described.  That path
 1298          * is relative to the given root (third argument).  Here we're
 1299          * starting from the root.
 1300          */
 1301         if ((error = sysctl_createv(clog, 0, NULL, &node,
 1302             CTLFLAG_PERMANENT,
 1303             CTLTYPE_NODE, "tap", NULL,
 1304             NULL, 0, NULL, 0,
 1305             CTL_NET, AF_LINK, CTL_CREATE, CTL_EOL)) != 0)
 1306                 return;
 1307         tap_node = node->sysctl_num;
 1308 }
 1309 
 1310 /*
 1311  * The helper functions make Andrew Brown's interface really
 1312  * shine.  It makes possible to create value on the fly whether
 1313  * the sysctl value is read or written.
 1314  *
 1315  * As shown as an example in the man page, the first step is to
 1316  * create a copy of the node to have sysctl_lookup work on it.
 1317  *
 1318  * Here, we have more work to do than just a copy, since we have
 1319  * to create the string.  The first step is to collect the actual
 1320  * value of the node, which is a convenient pointer to the softc
 1321  * of the interface.  From there we create the string and use it
 1322  * as the value, but only for the *copy* of the node.
 1323  *
 1324  * Then we let sysctl_lookup do the magic, which consists in
 1325  * setting oldp and newp as required by the operation.  When the
 1326  * value is read, that means that the string will be copied to
 1327  * the user, and when it is written, the new value will be copied
 1328  * over in the addr array.
 1329  *
 1330  * If newp is NULL, the user was reading the value, so we don't
 1331  * have anything else to do.  If a new value was written, we
 1332  * have to check it.
 1333  *
 1334  * If it is incorrect, we can return an error and leave 'node' as
 1335  * it is:  since it is a copy of the actual node, the change will
 1336  * be forgotten.
 1337  *
 1338  * Upon a correct input, we commit the change to the ifnet
 1339  * structure of our interface.
 1340  */
 1341 static int
 1342 tap_sysctl_handler(SYSCTLFN_ARGS)
 1343 {
 1344         struct sysctlnode node;
 1345         struct tap_softc *sc;
 1346         struct ifnet *ifp;
 1347         int error;
 1348         size_t len;
 1349         char addr[3 * ETHER_ADDR_LEN];
 1350         uint8_t enaddr[ETHER_ADDR_LEN];
 1351 
 1352         node = *rnode;
 1353         sc = node.sysctl_data;
 1354         ifp = &sc->sc_ec.ec_if;
 1355         (void)ether_snprintf(addr, sizeof(addr), CLLADDR(ifp->if_sadl));
 1356         node.sysctl_data = addr;
 1357         error = sysctl_lookup(SYSCTLFN_CALL(&node));
 1358         if (error || newp == NULL)
 1359                 return (error);
 1360 
 1361         len = strlen(addr);
 1362         if (len < 11 || len > 17)
 1363                 return (EINVAL);
 1364 
 1365         /* Commit change */
 1366         if (ether_nonstatic_aton(enaddr, addr) != 0)
 1367                 return (EINVAL);
 1368         if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN);
 1369         return (error);
 1370 }
 1371 #endif

Cache object: 52b5bf0502918974a9c64769f6c44c57


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