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