#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.136 2024/11/10 10:57:52 mlelstv Exp $");
#if defined(_KERNEL_OPT)
#include "opt_modular.h"
#include "opt_net_mpsafe.h"
#endif
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/conf.h>
#include <sys/cprng.h>
#include <sys/device.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/intr.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/select.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_tap.h>
#include <net/bpf.h>
#include "ioconf.h"
static int tap_node;
static int tap_sysctl_handler(SYSCTLFN_PROTO);
static void sysctl_tap_setup(struct sysctllog **);
struct tap_softc {
device_t sc_dev;
struct ethercom sc_ec;
int sc_flags;
#define TAP_INUSE 0x00000001
#define TAP_ASYNCIO 0x00000002
#define TAP_NBIO 0x00000004
#define TAP_GOING 0x00000008
struct selinfo sc_rsel;
pid_t sc_pgid;
kmutex_t sc_lock;
kcondvar_t sc_cv;
void *sc_sih;
struct timespec sc_atime;
struct timespec sc_mtime;
struct timespec sc_btime;
};
static int tap_match(device_t, cfdata_t, void *);
static void tap_attach(device_t, device_t, void *);
static int tap_detach(device_t, int);
CFATTACH_DECL_NEW(tap, sizeof(struct tap_softc),
tap_match, tap_attach, tap_detach, NULL);
extern struct cfdriver tap_cd;
static void tap_dev_close(struct tap_softc *);
static int tap_dev_read(int, struct uio *, int);
static int tap_dev_write(int, struct uio *, int);
static int tap_dev_ioctl(int, u_long, void *, struct lwp *);
static int tap_dev_poll(int, int, struct lwp *);
static int tap_dev_kqfilter(int, struct knote *);
static int tap_fops_close(file_t *);
static int tap_fops_read(file_t *, off_t *, struct uio *,
kauth_cred_t, int);
static int tap_fops_write(file_t *, off_t *, struct uio *,
kauth_cred_t, int);
static int tap_fops_ioctl(file_t *, u_long, void *);
static int tap_fops_poll(file_t *, int);
static int tap_fops_stat(file_t *, struct stat *);
static int tap_fops_kqfilter(file_t *, struct knote *);
static const struct fileops tap_fileops = {
.fo_name = "tap",
.fo_read = tap_fops_read,
.fo_write = tap_fops_write,
.fo_ioctl = tap_fops_ioctl,
.fo_fcntl = fnullop_fcntl,
.fo_poll = tap_fops_poll,
.fo_stat = tap_fops_stat,
.fo_close = tap_fops_close,
.fo_kqfilter = tap_fops_kqfilter,
.fo_restart = fnullop_restart,
};
static int tap_dev_cloner(struct lwp *);
static int tap_cdev_open(dev_t, int, int, struct lwp *);
static int tap_cdev_close(dev_t, int, int, struct lwp *);
static int tap_cdev_read(dev_t, struct uio *, int);
static int tap_cdev_write(dev_t, struct uio *, int);
static int tap_cdev_ioctl(dev_t, u_long, void *, int, struct lwp *);
static int tap_cdev_poll(dev_t, int, struct lwp *);
static int tap_cdev_kqfilter(dev_t, struct knote *);
const struct cdevsw tap_cdevsw = {
.d_open = tap_cdev_open,
.d_close = tap_cdev_close,
.d_read = tap_cdev_read,
.d_write = tap_cdev_write,
.d_ioctl = tap_cdev_ioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = tap_cdev_poll,
.d_mmap = nommap,
.d_kqfilter = tap_cdev_kqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER | D_MPSAFE
};
#define TAP_CLONER 0xfffff
static void tap_kqdetach(struct knote *);
static int tap_kqread(struct knote *, long);
static void tap_start(struct ifnet *);
static void tap_stop(struct ifnet *, int);
static int tap_init(struct ifnet *);
static int tap_ioctl(struct ifnet *, u_long, void *);
static int tap_lifaddr(struct ifnet *, u_long, struct ifaliasreq *);
static void tap_softintr(void *);
static int tap_clone_create(struct if_clone *, int);
static int tap_clone_destroy(struct ifnet *);
struct if_clone tap_cloners = IF_CLONE_INITIALIZER("tap",
tap_clone_create,
tap_clone_destroy);
static struct tap_softc * tap_clone_creator(int);
static void tap_clone_destroyer(device_t);
static struct sysctllog *tap_sysctl_clog;
#ifdef _MODULE
devmajor_t tap_bmajor = -1, tap_cmajor = -1;
#endif
static u_int tap_count;
void
tapattach(int n)
{
}
static void
tapinit(void)
{
int error;
#ifdef _MODULE
devsw_attach("tap", NULL, &tap_bmajor, &tap_cdevsw, &tap_cmajor);
#endif
error = config_cfattach_attach(tap_cd.cd_name, &tap_ca);
if (error) {
aprint_error("%s: unable to register cfattach\n",
tap_cd.cd_name);
(void)config_cfdriver_detach(&tap_cd);
return;
}
if_clone_attach(&tap_cloners);
sysctl_tap_setup(&tap_sysctl_clog);
}
static int
tapdetach(void)
{
int error = 0;
if_clone_detach(&tap_cloners);
if (tap_count != 0) {
if_clone_attach(&tap_cloners);
return EBUSY;
}
error = config_cfattach_detach(tap_cd.cd_name, &tap_ca);
if (error == 0) {
#ifdef _MODULE
devsw_detach(NULL, &tap_cdevsw);
#endif
sysctl_teardown(&tap_sysctl_clog);
} else
if_clone_attach(&tap_cloners);
return error;
}
static int
tap_match(device_t parent, cfdata_t cfdata, void *arg)
{
return 1;
}
void
tap_attach(device_t parent, device_t self, void *aux)
{
struct tap_softc *sc = device_private(self);
struct ifnet *ifp;
const struct sysctlnode *node;
int error;
uint8_t enaddr[ETHER_ADDR_LEN] =
{ 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
char enaddrstr[3 * ETHER_ADDR_LEN];
sc->sc_dev = self;
sc->sc_sih = NULL;
getnanotime(&sc->sc_btime);
sc->sc_atime = sc->sc_mtime = sc->sc_btime;
sc->sc_flags = 0;
selinit(&sc->sc_rsel);
cv_init(&sc->sc_cv, "tapread");
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
cprng_fast(&enaddr[3], 3);
aprint_verbose_dev(self, "Ethernet address %s\n",
ether_snprintf(enaddrstr, sizeof(enaddrstr), enaddr));
ifp = &sc->sc_ec.ec_if;
strcpy(ifp->if_xname, device_xname(self));
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
#ifdef NET_MPSAFE
ifp->if_extflags = IFEF_MPSAFE;
#endif
ifp->if_ioctl = tap_ioctl;
ifp->if_start = tap_start;
ifp->if_stop = tap_stop;
ifp->if_init = tap_init;
IFQ_SET_READY(&ifp->if_snd);
sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
if_initialize(ifp);
ifp->if_percpuq = if_percpuq_create(ifp);
ether_ifattach(ifp, enaddr);
ifp->if_link_state = LINK_STATE_DOWN;
if_register(ifp);
if ((error = sysctl_createv(NULL, 0, NULL,
&node, CTLFLAG_READWRITE,
CTLTYPE_STRING, device_xname(self), NULL,
tap_sysctl_handler, 0, (void *)sc, 18,
CTL_NET, AF_LINK, tap_node, device_unit(sc->sc_dev),
CTL_EOL)) != 0)
aprint_error_dev(self,
"sysctl_createv returned %d, ignoring\n", error);
}
static int
tap_detach(device_t self, int flags)
{
struct tap_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_ec.ec_if;
int error;
sc->sc_flags |= TAP_GOING;
tap_stop(ifp, 1);
if_down(ifp);
if (sc->sc_sih != NULL) {
softint_disestablish(sc->sc_sih);
sc->sc_sih = NULL;
}
if ((error = sysctl_destroyv(NULL, CTL_NET, AF_LINK, tap_node,
device_unit(sc->sc_dev), CTL_EOL)) != 0)
aprint_error_dev(self,
"sysctl_destroyv returned %d, ignoring\n", error);
ether_ifdetach(ifp);
if_detach(ifp);
seldestroy(&sc->sc_rsel);
mutex_destroy(&sc->sc_lock);
cv_destroy(&sc->sc_cv);
pmf_device_deregister(self);
return 0;
}
static void
tap_start(struct ifnet *ifp)
{
struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
struct mbuf *m0;
mutex_enter(&sc->sc_lock);
if ((sc->sc_flags & TAP_INUSE) == 0) {
for (;;) {
IFQ_DEQUEUE(&ifp->if_snd, m0);
if (m0 == NULL)
goto done;
if_statadd2(ifp, if_opackets, 1, if_obytes, m0->m_len);
bpf_mtap(ifp, m0, BPF_D_OUT);
m_freem(m0);
}
} else if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
ifp->if_flags |= IFF_OACTIVE;
cv_broadcast(&sc->sc_cv);
selnotify(&sc->sc_rsel, 0, 1);
if (sc->sc_flags & TAP_ASYNCIO) {
kpreempt_disable();
softint_schedule(sc->sc_sih);
kpreempt_enable();
}
}
done:
mutex_exit(&sc->sc_lock);
}
static void
tap_softintr(void *cookie)
{
struct tap_softc *sc;
struct ifnet *ifp;
int a, b;
sc = cookie;
if (sc->sc_flags & TAP_ASYNCIO) {
ifp = &sc->sc_ec.ec_if;
if (ifp->if_flags & IFF_RUNNING) {
a = POLL_IN;
b = POLLIN | POLLRDNORM;
} else {
a = POLL_HUP;
b = 0;
}
fownsignal(sc->sc_pgid, SIGIO, a, b, NULL);
}
}
static int
tap_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
int s, error;
s = splnet();
switch (cmd) {
case SIOCSIFPHYADDR:
error = tap_lifaddr(ifp, cmd, (struct ifaliasreq *)data);
break;
default:
error = ether_ioctl(ifp, cmd, data);
if (error == ENETRESET)
error = 0;
break;
}
splx(s);
return error;
}
static int
tap_lifaddr(struct ifnet *ifp, u_long cmd, struct ifaliasreq *ifra)
{
const struct sockaddr *sa = &ifra->ifra_addr;
if (sa->sa_family != AF_LINK)
return EINVAL;
if_set_sadl(ifp, sa->sa_data, ETHER_ADDR_LEN, false);
return 0;
}
static int
tap_init(struct ifnet *ifp)
{
ifp->if_flags |= IFF_RUNNING;
tap_start(ifp);
return 0;
}
static void
tap_stop(struct ifnet *ifp, int disable)
{
struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
mutex_enter(&sc->sc_lock);
ifp->if_flags &= ~IFF_RUNNING;
cv_broadcast(&sc->sc_cv);
selnotify(&sc->sc_rsel, 0, 1);
if (sc->sc_flags & TAP_ASYNCIO) {
kpreempt_disable();
softint_schedule(sc->sc_sih);
kpreempt_enable();
}
mutex_exit(&sc->sc_lock);
}
static int
tap_clone_create(struct if_clone *ifc, int unit)
{
if (tap_clone_creator(unit) == NULL) {
aprint_error("%s%d: unable to attach an instance\n",
tap_cd.cd_name, unit);
return ENXIO;
}
atomic_inc_uint(&tap_count);
return 0;
}
static struct tap_softc *
tap_clone_creator(int unit)
{
cfdata_t cf;
cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
cf->cf_name = tap_cd.cd_name;
cf->cf_atname = tap_ca.ca_name;
if (unit == -1) {
cf->cf_unit = 0;
cf->cf_fstate = FSTATE_STAR;
} else {
cf->cf_unit = unit;
cf->cf_fstate = FSTATE_NOTFOUND;
}
return device_private(config_attach_pseudo(cf));
}
static int
tap_clone_destroy(struct ifnet *ifp)
{
struct tap_softc *sc = ifp->if_softc;
tap_clone_destroyer(sc->sc_dev);
atomic_dec_uint(&tap_count);
return 0;
}
static void
tap_clone_destroyer(device_t dev)
{
cfdata_t cf = device_cfdata(dev);
int error;
error = config_detach(dev, DETACH_FORCE);
KASSERTMSG(error == 0, "error=%d", error);
kmem_free(cf, sizeof(*cf));
}
static int
tap_cdev_open(dev_t dev, int flags, int fmt, struct lwp *l)
{
struct tap_softc *sc;
if (minor(dev) == TAP_CLONER)
return tap_dev_cloner(l);
sc = device_lookup_private(&tap_cd, minor(dev));
if (sc == NULL)
return ENXIO;
if (sc->sc_flags & TAP_INUSE)
return EBUSY;
sc->sc_flags |= TAP_INUSE;
if_link_state_change(&sc->sc_ec.ec_if, LINK_STATE_UP);
return 0;
}
static int
tap_dev_cloner(struct lwp *l)
{
struct tap_softc *sc;
file_t *fp;
int error, fd;
if ((error = fd_allocfile(&fp, &fd)) != 0)
return error;
if ((sc = tap_clone_creator(-1)) == NULL) {
fd_abort(curproc, fp, fd);
return ENXIO;
}
sc->sc_flags |= TAP_INUSE;
if_link_state_change(&sc->sc_ec.ec_if, LINK_STATE_UP);
return fd_clone(fp, fd, FREAD | FWRITE, &tap_fileops,
(void *)(intptr_t)device_unit(sc->sc_dev));
}
static int
tap_cdev_close(dev_t dev, int flags, int fmt, struct lwp *l)
{
struct tap_softc *sc = device_lookup_private(&tap_cd, minor(dev));
if (sc == NULL)
return ENXIO;
tap_dev_close(sc);
return 0;
}
static int
tap_fops_close(file_t *fp)
{
struct tap_softc *sc;
int unit = fp->f_devunit;
sc = device_lookup_private(&tap_cd, unit);
if (sc == NULL)
return ENXIO;
KERNEL_LOCK(1, NULL);
tap_dev_close(sc);
if ((sc->sc_flags & TAP_GOING) != 0)
goto out;
tap_clone_destroyer(sc->sc_dev);
out: KERNEL_UNLOCK_ONE(NULL);
return 0;
}
static void
tap_dev_close(struct tap_softc *sc)
{
struct ifnet *ifp;
int s;
s = splnet();
ifp = &sc->sc_ec.ec_if;
ifp->if_flags &= ~IFF_OACTIVE;
if (!(IFQ_IS_EMPTY(&ifp->if_snd))) {
struct mbuf *m;
for (;;) {
IFQ_DEQUEUE(&ifp->if_snd, m);
if (m == NULL)
break;
if_statadd2(ifp, if_opackets, 1, if_obytes, m->m_len);
bpf_mtap(ifp, m, BPF_D_OUT);
m_freem(m);
}
}
splx(s);
if (sc->sc_sih != NULL) {
softint_disestablish(sc->sc_sih);
sc->sc_sih = NULL;
}
sc->sc_flags &= ~(TAP_INUSE | TAP_ASYNCIO);
if_link_state_change(ifp, LINK_STATE_DOWN);
}
static int
tap_cdev_read(dev_t dev, struct uio *uio, int flags)
{
return tap_dev_read(minor(dev), uio, flags);
}
static int
tap_fops_read(file_t *fp, off_t *offp, struct uio *uio,
kauth_cred_t cred, int flags)
{
int error;
KERNEL_LOCK(1, NULL);
error = tap_dev_read(fp->f_devunit, uio, flags);
KERNEL_UNLOCK_ONE(NULL);
return error;
}
static int
tap_dev_read(int unit, struct uio *uio, int flags)
{
struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
struct ifnet *ifp;
struct mbuf *m, *n;
int error = 0;
if (sc == NULL)
return ENXIO;
getnanotime(&sc->sc_atime);
ifp = &sc->sc_ec.ec_if;
if ((ifp->if_flags & IFF_UP) == 0)
return EHOSTDOWN;
mutex_enter(&sc->sc_lock);
if (IFQ_IS_EMPTY(&ifp->if_snd)) {
ifp->if_flags &= ~IFF_OACTIVE;
if (sc->sc_flags & TAP_NBIO)
error = EWOULDBLOCK;
else
error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
if (error != 0) {
mutex_exit(&sc->sc_lock);
return error;
}
if ((ifp->if_flags & IFF_UP) == 0) {
mutex_exit(&sc->sc_lock);
return EHOSTDOWN;
}
}
IFQ_DEQUEUE(&ifp->if_snd, m);
mutex_exit(&sc->sc_lock);
ifp->if_flags &= ~IFF_OACTIVE;
if (m == NULL) {
error = 0;
goto out;
}
if_statadd2(ifp, if_opackets, 1,
if_obytes, m->m_len);
bpf_mtap(ifp, m, BPF_D_OUT);
if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
goto out;
if (m == NULL)
goto out;
do {
error = uiomove(mtod(m, void *),
uimin(m->m_len, uio->uio_resid), uio);
m = n = m_free(m);
} while (m != NULL && uio->uio_resid > 0 && error == 0);
m_freem(m);
out:
return error;
}
static int
tap_fops_stat(file_t *fp, struct stat *st)
{
int error = 0;
struct tap_softc *sc;
int unit = fp->f_devunit;
(void)memset(st, 0, sizeof(*st));
KERNEL_LOCK(1, NULL);
sc = device_lookup_private(&tap_cd, unit);
if (sc == NULL) {
error = ENXIO;
goto out;
}
st->st_dev = makedev(cdevsw_lookup_major(&tap_cdevsw), unit);
st->st_atimespec = sc->sc_atime;
st->st_mtimespec = sc->sc_mtime;
st->st_ctimespec = st->st_birthtimespec = sc->sc_btime;
st->st_uid = kauth_cred_geteuid(fp->f_cred);
st->st_gid = kauth_cred_getegid(fp->f_cred);
out:
KERNEL_UNLOCK_ONE(NULL);
return error;
}
static int
tap_cdev_write(dev_t dev, struct uio *uio, int flags)
{
return tap_dev_write(minor(dev), uio, flags);
}
static int
tap_fops_write(file_t *fp, off_t *offp, struct uio *uio,
kauth_cred_t cred, int flags)
{
int error;
KERNEL_LOCK(1, NULL);
error = tap_dev_write(fp->f_devunit, uio, flags);
KERNEL_UNLOCK_ONE(NULL);
return error;
}
static int
tap_dev_write(int unit, struct uio *uio, int flags)
{
struct tap_softc *sc =
device_lookup_private(&tap_cd, unit);
struct ifnet *ifp;
struct mbuf *m, **mp;
size_t len = 0;
int error = 0;
if (sc == NULL)
return ENXIO;
getnanotime(&sc->sc_mtime);
ifp = &sc->sc_ec.ec_if;
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
if_statinc(ifp, if_ierrors);
return ENOBUFS;
}
MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
m->m_pkthdr.len = uio->uio_resid;
mp = &m;
while (error == 0 && uio->uio_resid > 0) {
if (*mp != m) {
MGET(*mp, M_DONTWAIT, MT_DATA);
if (*mp == NULL) {
error = ENOBUFS;
break;
}
MCLAIM(*mp, &sc->sc_ec.ec_rx_mowner);
}
(*mp)->m_len = uimin(MHLEN, uio->uio_resid);
len += (*mp)->m_len;
error = uiomove(mtod(*mp, void *), (*mp)->m_len, uio);
mp = &(*mp)->m_next;
}
if (error) {
if_statinc(ifp, if_ierrors);
m_freem(m);
return error;
}
m_set_rcvif(m, ifp);
if_statadd2(ifp, if_ipackets, 1, if_ibytes, len);
bpf_mtap(ifp, m, BPF_D_IN);
if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN)) != 0)
return error;
if (m == NULL)
return 0;
if_percpuq_enqueue(ifp->if_percpuq, m);
return 0;
}
static int
tap_cdev_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
{
return tap_dev_ioctl(minor(dev), cmd, data, l);
}
static int
tap_fops_ioctl(file_t *fp, u_long cmd, void *data)
{
return tap_dev_ioctl(fp->f_devunit, cmd, data, curlwp);
}
static int
tap_dev_ioctl(int unit, u_long cmd, void *data, struct lwp *l)
{
struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
if (sc == NULL)
return ENXIO;
switch (cmd) {
case FIONREAD:
{
struct ifnet *ifp = &sc->sc_ec.ec_if;
struct mbuf *m;
int s;
s = splnet();
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL)
*(int *)data = 0;
else
*(int *)data = m->m_pkthdr.len;
splx(s);
return 0;
}
case TIOCSPGRP:
case FIOSETOWN:
return fsetown(&sc->sc_pgid, cmd, data);
case TIOCGPGRP:
case FIOGETOWN:
return fgetown(sc->sc_pgid, cmd, data);
case FIOASYNC:
if (*(int *)data) {
if (sc->sc_sih == NULL) {
sc->sc_sih = softint_establish(SOFTINT_CLOCK,
tap_softintr, sc);
if (sc->sc_sih == NULL)
return EBUSY;
}
sc->sc_flags |= TAP_ASYNCIO;
} else {
sc->sc_flags &= ~TAP_ASYNCIO;
if (sc->sc_sih != NULL) {
softint_disestablish(sc->sc_sih);
sc->sc_sih = NULL;
}
}
return 0;
case FIONBIO:
if (*(int *)data)
sc->sc_flags |= TAP_NBIO;
else
sc->sc_flags &= ~TAP_NBIO;
return 0;
case TAPGIFNAME:
{
struct ifreq *ifr = (struct ifreq *)data;
struct ifnet *ifp = &sc->sc_ec.ec_if;
strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
return 0;
}
default:
return ENOTTY;
}
}
static int
tap_cdev_poll(dev_t dev, int events, struct lwp *l)
{
return tap_dev_poll(minor(dev), events, l);
}
static int
tap_fops_poll(file_t *fp, int events)
{
return tap_dev_poll(fp->f_devunit, events, curlwp);
}
static int
tap_dev_poll(int unit, int events, struct lwp *l)
{
struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
int revents = 0;
if (sc == NULL)
return POLLERR;
if (events & (POLLIN | POLLRDNORM)) {
struct ifnet *ifp = &sc->sc_ec.ec_if;
struct mbuf *m;
int s;
s = splnet();
IFQ_POLL(&ifp->if_snd, m);
if (m != NULL)
revents |= events & (POLLIN | POLLRDNORM);
else {
mutex_spin_enter(&sc->sc_lock);
selrecord(l, &sc->sc_rsel);
mutex_spin_exit(&sc->sc_lock);
}
splx(s);
}
revents |= events & (POLLOUT | POLLWRNORM);
return revents;
}
static struct filterops tap_read_filterops = {
.f_flags = FILTEROP_ISFD,
.f_attach = NULL,
.f_detach = tap_kqdetach,
.f_event = tap_kqread,
};
static int
tap_cdev_kqfilter(dev_t dev, struct knote *kn)
{
return tap_dev_kqfilter(minor(dev), kn);
}
static int
tap_fops_kqfilter(file_t *fp, struct knote *kn)
{
return tap_dev_kqfilter(fp->f_devunit, kn);
}
static int
tap_dev_kqfilter(int unit, struct knote *kn)
{
struct tap_softc *sc = device_lookup_private(&tap_cd, unit);
if (sc == NULL)
return ENXIO;
switch(kn->kn_filter) {
case EVFILT_READ:
kn->kn_fop = &tap_read_filterops;
kn->kn_hook = sc;
KERNEL_LOCK(1, NULL);
mutex_spin_enter(&sc->sc_lock);
selrecord_knote(&sc->sc_rsel, kn);
mutex_spin_exit(&sc->sc_lock);
KERNEL_UNLOCK_ONE(NULL);
break;
case EVFILT_WRITE:
kn->kn_fop = &seltrue_filtops;
break;
default:
return EINVAL;
}
return 0;
}
static void
tap_kqdetach(struct knote *kn)
{
struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
KERNEL_LOCK(1, NULL);
mutex_spin_enter(&sc->sc_lock);
selremove_knote(&sc->sc_rsel, kn);
mutex_spin_exit(&sc->sc_lock);
KERNEL_UNLOCK_ONE(NULL);
}
static int
tap_kqread(struct knote *kn, long hint)
{
struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
struct ifnet *ifp = &sc->sc_ec.ec_if;
struct mbuf *m;
int s, rv;
KERNEL_LOCK(1, NULL);
s = splnet();
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL)
kn->kn_data = 0;
else
kn->kn_data = m->m_pkthdr.len;
splx(s);
rv = (kn->kn_data != 0 ? 1 : 0);
KERNEL_UNLOCK_ONE(NULL);
return rv;
}
static void
sysctl_tap_setup(struct sysctllog **clog)
{
const struct sysctlnode *node;
int error = 0;
if ((error = sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "link", NULL,
NULL, 0, NULL, 0,
CTL_NET, AF_LINK, CTL_EOL)) != 0)
return;
if ((error = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "tap", NULL,
NULL, 0, NULL, 0,
CTL_NET, AF_LINK, CTL_CREATE, CTL_EOL)) != 0)
return;
tap_node = node->sysctl_num;
}
static int
tap_sysctl_handler(SYSCTLFN_ARGS)
{
struct sysctlnode node;
struct tap_softc *sc;
struct ifnet *ifp;
int error;
size_t len;
char addr[3 * ETHER_ADDR_LEN];
uint8_t enaddr[ETHER_ADDR_LEN];
node = *rnode;
sc = node.sysctl_data;
ifp = &sc->sc_ec.ec_if;
(void)ether_snprintf(addr, sizeof(addr), CLLADDR(ifp->if_sadl));
node.sysctl_data = addr;
error = sysctl_lookup(SYSCTLFN_CALL(&node));
if (error || newp == NULL)
return error;
len = strlen(addr);
if (len < 11 || len > 17)
return EINVAL;
if (ether_aton_r(enaddr, sizeof(enaddr), addr) != 0)
return EINVAL;
if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
return error;
}
#include "if_module.h"
IF_MODULE(MODULE_CLASS_DRIVER, tap, NULL)