#include "bpf.h"
#ifndef __GNUC__
#define inline
#else
#define inline __inline
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/filio.h>
#include <sys/sockio.h>
#include <sys/ttycom.h>
#include <sys/filedesc.h>
#include <sys/uio_internal.h>
#if defined(sparc) && BSD < 199103
#include <sys/stream.h>
#endif
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/vnode.h>
#include <net/if.h>
#include <net/bpf.h>
#include <net/bpfdesc.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <net/firewire.h>
#include <miscfs/devfs/devfs.h>
#include <net/dlil.h>
#include <kern/locks.h>
#if CONFIG_MACF_NET
#include <security/mac_framework.h>
#endif
extern int tvtohz(struct timeval *);
#if BSD < 199103
extern bcopy();
static caddr_t bpf_alloc();
#include <net/bpf_compat.h>
#define BPF_BUFSIZE (MCLBYTES-8)
#define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
#else
#define BPF_BUFSIZE 4096
#define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
#endif
#define PRINET 26
static unsigned int bpf_bufsize = BPF_BUFSIZE;
SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
&bpf_bufsize, 0, "");
static unsigned int bpf_maxbufsize = BPF_MAXBUFSIZE;
SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
&bpf_maxbufsize, 0, "");
static unsigned int bpf_maxdevices = 256;
SYSCTL_UINT(_debug, OID_AUTO, bpf_maxdevices, CTLFLAG_RW,
&bpf_maxdevices, 0, "");
static struct bpf_if *bpf_iflist;
#ifdef __APPLE__
static struct bpf_d **bpf_dtab = NULL;
static unsigned int bpf_dtab_size = 0;
static unsigned int nbpfilter = 0;
static lck_mtx_t *bpf_mlock;
static lck_grp_t *bpf_mlock_grp;
static lck_grp_attr_t *bpf_mlock_grp_attr;
static lck_attr_t *bpf_mlock_attr;
#endif
static int bpf_allocbufs(struct bpf_d *);
static errno_t bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
static void bpf_detachd(struct bpf_d *d);
static void bpf_freed(struct bpf_d *);
static void bpf_mcopy(const void *, void *, size_t);
static int bpf_movein(struct uio *, int,
struct mbuf **, struct sockaddr *, int *);
static int bpf_setif(struct bpf_d *, ifnet_t ifp, u_int32_t dlt);
static void bpf_wakeup(struct bpf_d *);
static void catchpacket(struct bpf_d *, u_char *, u_int,
u_int, void (*)(const void *, void *, size_t));
static void reset_d(struct bpf_d *);
static int bpf_setf(struct bpf_d *, u_int bf_len, user_addr_t bf_insns);
static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
static int bpf_setdlt(struct bpf_d *, u_int);
static int bpf_devsw_installed;
void bpf_init(void *unused);
static int bpf_tap_callback(struct ifnet *ifp, struct mbuf *m);
d_open_t bpfopen;
d_close_t bpfclose;
d_read_t bpfread;
d_write_t bpfwrite;
ioctl_fcn_t bpfioctl;
select_fcn_t bpfpoll;
#define CDEV_MAJOR 23
static struct cdevsw bpf_cdevsw = {
bpfopen,
bpfclose,
bpfread,
bpfwrite,
bpfioctl,
eno_stop,
eno_reset,
NULL,
bpfpoll,
eno_mmap,
eno_strat,
eno_getc,
eno_putc,
0
};
#define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
static int
bpf_movein(struct uio *uio, int linktype, struct mbuf **mp, struct sockaddr *sockp, int *datlen)
{
struct mbuf *m;
int error;
int len;
uint8_t sa_family;
int hlen;
switch (linktype) {
#if SLIP
case DLT_SLIP:
sa_family = AF_INET;
hlen = 0;
break;
#endif
case DLT_EN10MB:
sa_family = AF_UNSPEC;
hlen = sizeof(struct ether_header);
break;
#if FDDI
case DLT_FDDI:
#if defined(__FreeBSD__) || defined(__bsdi__)
sa_family = AF_IMPLINK;
hlen = 0;
#else
sa_family = AF_UNSPEC;
hlen = 24;
#endif
break;
#endif
case DLT_RAW:
case DLT_NULL:
sa_family = AF_UNSPEC;
hlen = 0;
break;
#ifdef __FreeBSD__
case DLT_ATM_RFC1483:
sa_family = AF_UNSPEC;
hlen = 12;
break;
#endif
case DLT_PPP:
sa_family = AF_UNSPEC;
hlen = 4;
break;
case DLT_APPLE_IP_OVER_IEEE1394:
sa_family = AF_UNSPEC;
hlen = sizeof(struct firewire_header);
break;
default:
return (EIO);
}
len = uio_resid(uio);
*datlen = len - hlen;
if ((unsigned)len > MCLBYTES)
return (EIO);
if (sockp) {
if ((hlen + SOCKADDR_HDR_LEN) > sockp->sa_len) {
return (EIO);
}
sockp->sa_family = sa_family;
} else {
hlen = 0;
}
MGETHDR(m, M_WAIT, MT_DATA);
if (m == 0)
return (ENOBUFS);
if ((unsigned)len > MHLEN) {
#if BSD >= 199103
MCLGET(m, M_WAIT);
if ((m->m_flags & M_EXT) == 0) {
#else
MCLGET(m);
if (m->m_len != MCLBYTES) {
#endif
error = ENOBUFS;
goto bad;
}
}
m->m_pkthdr.len = m->m_len = len;
m->m_pkthdr.rcvif = NULL;
*mp = m;
if (hlen != 0) {
m->m_pkthdr.len -= hlen;
m->m_len -= hlen;
#if BSD >= 199103
m->m_data += hlen;
#else
m->m_off += hlen;
#endif
error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
if (error)
goto bad;
}
error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
if (!error)
return (0);
bad:
m_freem(m);
return (error);
}
#ifdef __APPLE__
static void
bpf_make_dev_t(int maj)
{
static int bpf_growing = 0;
unsigned int cur_size = nbpfilter, i;
if (nbpfilter >= bpf_maxdevices)
return;
while (bpf_growing) {
(void)tsleep((caddr_t)&bpf_growing, PZERO, "bpf_growing", 0);
}
if (nbpfilter > cur_size) {
return;
}
bpf_growing = 1;
if (nbpfilter == bpf_dtab_size) {
int new_dtab_size;
struct bpf_d **new_dtab = NULL;
struct bpf_d **old_dtab = NULL;
new_dtab_size = bpf_dtab_size + NBPFILTER;
new_dtab = (struct bpf_d **)_MALLOC(sizeof(struct bpf_d *) * new_dtab_size, M_DEVBUF, M_WAIT);
if (new_dtab == 0) {
printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
goto done;
}
if (bpf_dtab) {
bcopy(bpf_dtab, new_dtab,
sizeof(struct bpf_d *) * bpf_dtab_size);
}
bzero(new_dtab + bpf_dtab_size,
sizeof(struct bpf_d *) * NBPFILTER);
old_dtab = bpf_dtab;
bpf_dtab = new_dtab;
bpf_dtab_size = new_dtab_size;
if (old_dtab != NULL)
_FREE(old_dtab, M_DEVBUF);
}
i = nbpfilter++;
(void) devfs_make_node(makedev(maj, i),
DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0600,
"bpf%d", i);
done:
bpf_growing = 0;
wakeup((caddr_t)&bpf_growing);
}
#endif
static errno_t
bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
{
int first = bp->bif_dlist == NULL;
int error = 0;
d->bd_bif = bp;
d->bd_next = bp->bif_dlist;
bp->bif_dlist = d;
if (first) {
if (bp->bif_ifp->if_bpf == NULL) {
struct bpf_if *primary;
for (primary = bpf_iflist; primary && primary->bif_ifp != bp->bif_ifp;
primary = primary->bif_next)
;
bp->bif_ifp->if_bpf = primary;
}
if (bp->bif_ifp->if_bpf == bp)
dlil_set_bpf_tap(bp->bif_ifp, BPF_TAP_INPUT_OUTPUT, bpf_tap_callback);
if (bp->bif_tap)
error = bp->bif_tap(bp->bif_ifp, bp->bif_dlt, BPF_TAP_INPUT_OUTPUT);
}
return error;
}
static void
bpf_detachd(struct bpf_d *d)
{
struct bpf_d **p;
struct bpf_if *bp;
struct ifnet *ifp;
ifp = d->bd_bif->bif_ifp;
bp = d->bd_bif;
p = &bp->bif_dlist;
while (*p != d) {
p = &(*p)->bd_next;
if (*p == 0)
panic("bpf_detachd: descriptor not in list");
}
*p = (*p)->bd_next;
if (bp->bif_dlist == 0) {
if (bp->bif_ifp->if_bpf == bp)
dlil_set_bpf_tap(ifp, BPF_TAP_DISABLE, NULL);
if (bp->bif_tap)
bp->bif_tap(ifp, bp->bif_dlt, BPF_TAP_DISABLE);
for (bp = bpf_iflist; bp; bp = bp->bif_next)
if (bp->bif_ifp == ifp && bp->bif_dlist != 0)
break;
if (bp == NULL)
ifp->if_bpf = NULL;
}
d->bd_bif = NULL;
if (d->bd_promisc) {
d->bd_promisc = 0;
lck_mtx_unlock(bpf_mlock);
if (ifnet_set_promiscuous(ifp, 0)) {
printf("bpf: ifnet_set_promiscuous failed");
}
lck_mtx_lock(bpf_mlock);
}
}
int
bpfopen(dev_t dev, __unused int flags, __unused int fmt,
__unused struct proc *p)
{
struct bpf_d *d;
lck_mtx_lock(bpf_mlock);
if ((unsigned int) minor(dev) >= nbpfilter) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if ((unsigned int) minor(dev) == (nbpfilter - 1))
bpf_make_dev_t(major(dev));
if (bpf_dtab[minor(dev)] == 0) {
bpf_dtab[minor(dev)] = (void *)1;
} else {
lck_mtx_unlock(bpf_mlock);
return (EBUSY);
}
d = (struct bpf_d *)_MALLOC(sizeof(struct bpf_d), M_DEVBUF, M_WAIT);
if (d == NULL) {
printf("bpfopen: malloc bpf_d failed\n");
bpf_dtab[minor(dev)] = NULL;
lck_mtx_unlock(bpf_mlock);
return ENOMEM;
}
bzero(d, sizeof(struct bpf_d));
d->bd_bufsize = bpf_bufsize;
d->bd_sig = SIGIO;
d->bd_seesent = 1;
#if CONFIG_MACF_NET
mac_bpfdesc_label_init(d);
mac_bpfdesc_label_associate(kauth_cred_get(), d);
#endif
bpf_dtab[minor(dev)] = d;
lck_mtx_unlock(bpf_mlock);
return (0);
}
int
bpfclose(dev_t dev, __unused int flags, __unused int fmt,
__unused struct proc *p)
{
struct bpf_d *d;
lck_mtx_lock(bpf_mlock);
d = bpf_dtab[minor(dev)];
if (d == 0 || d == (void *)1) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
bpf_dtab[minor(dev)] = (void *)1;
if (d->bd_bif)
bpf_detachd(d);
selthreadclear(&d->bd_sel);
#if CONFIG_MACF_NET
mac_bpfdesc_label_destroy(d);
#endif
bpf_freed(d);
bpf_dtab[minor(dev)] = NULL;
lck_mtx_unlock(bpf_mlock);
_FREE(d, M_DEVBUF);
return (0);
}
#define BPF_SLEEP bpf_sleep
static int
bpf_sleep(struct bpf_d *d, int pri, const char *wmesg, int timo)
{
int st;
lck_mtx_unlock(bpf_mlock);
st = tsleep((caddr_t)d, pri, wmesg, timo);
lck_mtx_lock(bpf_mlock);
return st;
}
#define ROTATE_BUFFERS(d) \
(d)->bd_hbuf = (d)->bd_sbuf; \
(d)->bd_hlen = (d)->bd_slen; \
(d)->bd_sbuf = (d)->bd_fbuf; \
(d)->bd_slen = 0; \
(d)->bd_fbuf = NULL;
int
bpfread(dev_t dev, struct uio *uio, int ioflag)
{
struct bpf_d *d;
int error;
lck_mtx_lock(bpf_mlock);
d = bpf_dtab[minor(dev)];
if (d == 0 || d == (void *)1) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if (uio->uio_resid != d->bd_bufsize) {
lck_mtx_unlock(bpf_mlock);
return (EINVAL);
}
while (d->bd_hbuf == 0) {
if (d->bd_immediate && d->bd_slen != 0) {
ROTATE_BUFFERS(d);
break;
}
if (d->bd_bif == NULL) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if (ioflag & IO_NDELAY)
error = EWOULDBLOCK;
else
error = BPF_SLEEP(d, PRINET|PCATCH, "bpf",
d->bd_rtout);
d = bpf_dtab[minor(dev)];
if (d == 0 || d == (void *)1) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if (error == EINTR || error == ERESTART) {
lck_mtx_unlock(bpf_mlock);
return (error);
}
if (error == EWOULDBLOCK) {
if (d->bd_hbuf)
break;
if (d->bd_slen == 0) {
lck_mtx_unlock(bpf_mlock);
return (0);
}
ROTATE_BUFFERS(d);
break;
}
}
error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
d->bd_fbuf = d->bd_hbuf;
d->bd_hbuf = NULL;
d->bd_hlen = 0;
lck_mtx_unlock(bpf_mlock);
return (error);
}
static void
bpf_wakeup(struct bpf_d *d)
{
wakeup((caddr_t)d);
if (d->bd_async && d->bd_sig && d->bd_sigio)
pgsigio(d->bd_sigio, d->bd_sig);
#if BSD >= 199103
selwakeup(&d->bd_sel);
#ifndef __APPLE__
d->bd_sel.si_pid = 0;
#endif
#else
if (d->bd_selproc) {
selwakeup(d->bd_selproc, (int)d->bd_selcoll);
d->bd_selcoll = 0;
d->bd_selproc = 0;
}
#endif
}
#define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
int
bpfwrite(dev_t dev, struct uio *uio, __unused int ioflag)
{
struct bpf_d *d;
struct ifnet *ifp;
struct mbuf *m = NULL;
int error;
char dst_buf[SOCKADDR_HDR_LEN + MAX_DATALINK_HDR_LEN];
int datlen;
lck_mtx_lock(bpf_mlock);
d = bpf_dtab[minor(dev)];
if (d == 0 || d == (void *)1) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if (d->bd_bif == 0) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
ifp = d->bd_bif->bif_ifp;
if (uio->uio_resid == 0) {
lck_mtx_unlock(bpf_mlock);
return (0);
}
((struct sockaddr *)dst_buf)->sa_len = sizeof(dst_buf);
error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m,
d->bd_hdrcmplt ? NULL : (struct sockaddr *)dst_buf,
&datlen);
if (error) {
lck_mtx_unlock(bpf_mlock);
return (error);
}
if ((unsigned)datlen > ifp->if_mtu) {
lck_mtx_unlock(bpf_mlock);
m_freem(m);
return (EMSGSIZE);
}
if ((error = ifp_use(ifp, kIfNetUseCount_MustNotBeZero)) != 0) {
lck_mtx_unlock(bpf_mlock);
m_freem(m);
return (error);
}
#if CONFIG_MACF_NET
mac_mbuf_label_associate_bpfdesc(d, m);
#endif
lck_mtx_unlock(bpf_mlock);
if (d->bd_hdrcmplt) {
if (d->bd_bif->bif_send)
error = d->bd_bif->bif_send(ifp, d->bd_bif->bif_dlt, m);
else
error = dlil_output(ifp, 0, m, NULL, NULL, 1);
}
else {
error = dlil_output(ifp, PF_INET, m, NULL, (struct sockaddr *)dst_buf, 0);
}
if (ifp_unuse(ifp) != 0)
ifp_use_reached_zero(ifp);
return (error);
}
static void
reset_d(struct bpf_d *d)
{
if (d->bd_hbuf) {
d->bd_fbuf = d->bd_hbuf;
d->bd_hbuf = NULL;
}
d->bd_slen = 0;
d->bd_hlen = 0;
d->bd_rcount = 0;
d->bd_dcount = 0;
}
int
bpfioctl(dev_t dev, u_long cmd, caddr_t addr, __unused int flags,
__unused struct proc *p)
{
struct bpf_d *d;
int error = 0;
lck_mtx_lock(bpf_mlock);
d = bpf_dtab[minor(dev)];
if (d == 0 || d == (void *)1) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
switch (cmd) {
default:
error = EINVAL;
break;
case FIONREAD:
{
int n;
n = d->bd_slen;
if (d->bd_hbuf)
n += d->bd_hlen;
*(int *)addr = n;
break;
}
case SIOCGIFADDR:
{
struct ifnet *ifp;
if (d->bd_bif == 0)
error = EINVAL;
else {
ifp = d->bd_bif->bif_ifp;
error = ifnet_ioctl(ifp, 0, cmd, addr);
}
break;
}
case BIOCGBLEN:
*(u_int *)addr = d->bd_bufsize;
break;
case BIOCSBLEN:
#if BSD < 199103
error = EINVAL;
#else
if (d->bd_bif != 0)
error = EINVAL;
else {
u_int size = *(u_int *)addr;
if (size > bpf_maxbufsize)
*(u_int *)addr = size = bpf_maxbufsize;
else if (size < BPF_MINBUFSIZE)
*(u_int *)addr = size = BPF_MINBUFSIZE;
d->bd_bufsize = size;
}
#endif
break;
case BIOCSETF64:
case BIOCSETF: {
if (proc_is64bit(current_proc())) {
struct bpf_program64 * prg64;
prg64 = (struct bpf_program64 *)addr;
error = bpf_setf(d, prg64->bf_len,
prg64->bf_insns);
}
else {
struct bpf_program * prg;
prg = (struct bpf_program *)addr;
error = bpf_setf(d, prg->bf_len,
CAST_USER_ADDR_T(prg->bf_insns));
}
break;
}
case BIOCFLUSH:
reset_d(d);
break;
case BIOCPROMISC:
if (d->bd_bif == 0) {
error = EINVAL;
break;
}
if (d->bd_promisc == 0) {
lck_mtx_unlock(bpf_mlock);
error = ifnet_set_promiscuous(d->bd_bif->bif_ifp, 1);
lck_mtx_lock(bpf_mlock);
if (error == 0)
d->bd_promisc = 1;
}
break;
case BIOCGDLT:
if (d->bd_bif == 0)
error = EINVAL;
else
*(u_int *)addr = d->bd_bif->bif_dlt;
break;
case BIOCGDLTLIST:
if (d->bd_bif == NULL)
error = EINVAL;
else
error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
break;
case BIOCSDLT:
if (d->bd_bif == NULL)
error = EINVAL;
else
error = bpf_setdlt(d, *(u_int *)addr);
break;
case BIOCGETIF:
if (d->bd_bif == 0)
error = EINVAL;
else {
struct ifnet *const ifp = d->bd_bif->bif_ifp;
struct ifreq *const ifr = (struct ifreq *)addr;
snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
"%s%d", ifp->if_name, ifp->if_unit);
}
break;
case BIOCSETIF: {
ifnet_t ifp;
ifp = ifunit(((struct ifreq *)addr)->ifr_name);
if (ifp == NULL)
error = ENXIO;
else
error = bpf_setif(d, ifp, 0);
break;
}
case BIOCSRTIMEOUT:
{
struct timeval *tv = (struct timeval *)addr;
if ((error = itimerfix(tv)) == 0)
d->bd_rtout = tvtohz(tv) - 1;
break;
}
case BIOCGRTIMEOUT:
{
struct timeval *tv = (struct timeval *)addr;
tv->tv_sec = d->bd_rtout / hz;
tv->tv_usec = (d->bd_rtout % hz) * tick;
break;
}
case BIOCGSTATS:
{
struct bpf_stat *bs = (struct bpf_stat *)addr;
bs->bs_recv = d->bd_rcount;
bs->bs_drop = d->bd_dcount;
break;
}
case BIOCIMMEDIATE:
d->bd_immediate = *(u_int *)addr;
break;
case BIOCVERSION:
{
struct bpf_version *bv = (struct bpf_version *)addr;
bv->bv_major = BPF_MAJOR_VERSION;
bv->bv_minor = BPF_MINOR_VERSION;
break;
}
case BIOCGHDRCMPLT:
*(u_int *)addr = d->bd_hdrcmplt;
break;
case BIOCSHDRCMPLT:
d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
break;
case BIOCGSEESENT:
*(u_int *)addr = d->bd_seesent;
break;
case BIOCSSEESENT:
d->bd_seesent = *(u_int *)addr;
break;
case FIONBIO:
break;
case FIOASYNC:
d->bd_async = *(int *)addr;
break;
#ifndef __APPLE__
case FIOSETOWN:
error = fsetown(*(int *)addr, &d->bd_sigio);
break;
case FIOGETOWN:
*(int *)addr = fgetown(d->bd_sigio);
break;
case TIOCSPGRP:
error = fsetown(-(*(int *)addr), &d->bd_sigio);
break;
case TIOCGPGRP:
*(int *)addr = -fgetown(d->bd_sigio);
break;
#endif
case BIOCSRSIG:
{
u_int sig;
sig = *(u_int *)addr;
if (sig >= NSIG)
error = EINVAL;
else
d->bd_sig = sig;
break;
}
case BIOCGRSIG:
*(u_int *)addr = d->bd_sig;
break;
}
lck_mtx_unlock(bpf_mlock);
return (error);
}
static int
bpf_setf(struct bpf_d *d, u_int bf_len, user_addr_t bf_insns)
{
struct bpf_insn *fcode, *old;
u_int flen, size;
old = d->bd_filter;
if (bf_insns == USER_ADDR_NULL) {
if (bf_len != 0)
return (EINVAL);
d->bd_filter = NULL;
reset_d(d);
if (old != 0)
FREE((caddr_t)old, M_DEVBUF);
return (0);
}
flen = bf_len;
if (flen > BPF_MAXINSNS)
return (EINVAL);
size = flen * sizeof(struct bpf_insn);
fcode = (struct bpf_insn *) _MALLOC(size, M_DEVBUF, M_WAIT);
#ifdef __APPLE__
if (fcode == NULL)
return (ENOBUFS);
#endif
if (copyin(bf_insns, (caddr_t)fcode, size) == 0 &&
bpf_validate(fcode, (int)flen)) {
d->bd_filter = fcode;
reset_d(d);
if (old != 0)
FREE((caddr_t)old, M_DEVBUF);
return (0);
}
FREE((caddr_t)fcode, M_DEVBUF);
return (EINVAL);
}
static int
bpf_setif(struct bpf_d *d, ifnet_t theywant, u_int32_t dlt)
{
struct bpf_if *bp;
int error;
for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
struct ifnet *ifp = bp->bif_ifp;
if (ifp == 0 || ifp != theywant || (dlt != 0 && dlt != bp->bif_dlt))
continue;
if ((ifp->if_flags & IFF_UP) == 0)
return (ENETDOWN);
if (d->bd_sbuf == 0) {
error = bpf_allocbufs(d);
if (error != 0)
return (error);
}
if (bp != d->bd_bif) {
if (d->bd_bif)
bpf_detachd(d);
if (bpf_attachd(d, bp) != 0) {
return ENXIO;
}
}
reset_d(d);
return (0);
}
return (ENXIO);
}
static int
bpf_getdltlist(
struct bpf_d *d,
struct bpf_dltlist *bfl)
{
u_int n;
int error;
struct ifnet *ifp;
struct bpf_if *bp;
user_addr_t dlist;
if (IS_64BIT_PROCESS(current_proc())) {
dlist = CAST_USER_ADDR_T(bfl->bfl_u.bflu_pad);
}
else {
dlist = CAST_USER_ADDR_T(bfl->bfl_u.bflu_list);
}
ifp = d->bd_bif->bif_ifp;
n = 0;
error = 0;
for (bp = bpf_iflist; bp; bp = bp->bif_next) {
if (bp->bif_ifp != ifp)
continue;
if (dlist != 0) {
if (n >= bfl->bfl_len) {
return (ENOMEM);
}
error = copyout(&bp->bif_dlt, dlist, sizeof(bp->bif_dlt));
dlist += sizeof(bp->bif_dlt);
}
n++;
}
bfl->bfl_len = n;
return (error);
}
static int
bpf_setdlt(struct bpf_d *d, uint32_t dlt)
{
int error, opromisc;
struct ifnet *ifp;
struct bpf_if *bp;
if (d->bd_bif->bif_dlt == dlt)
return (0);
ifp = d->bd_bif->bif_ifp;
for (bp = bpf_iflist; bp; bp = bp->bif_next) {
if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
break;
}
if (bp != NULL) {
opromisc = d->bd_promisc;
bpf_detachd(d);
error = bpf_attachd(d, bp);
if (error) {
printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
ifnet_name(bp->bif_ifp), ifnet_unit(bp->bif_ifp), error);
return error;
}
reset_d(d);
if (opromisc) {
lck_mtx_unlock(bpf_mlock);
error = ifnet_set_promiscuous(bp->bif_ifp, 1);
lck_mtx_lock(bpf_mlock);
if (error)
printf("bpf_setdlt: ifpromisc %s%d failed (%d)\n",
ifnet_name(bp->bif_ifp), ifnet_unit(bp->bif_ifp), error);
else
d->bd_promisc = 1;
}
}
return (bp == NULL ? EINVAL : 0);
}
int
bpfpoll(dev_t dev, int events, void * wql, struct proc *p)
{
struct bpf_d *d;
int revents = 0;
lck_mtx_lock(bpf_mlock);
d = bpf_dtab[minor(dev)];
if (d == 0 || d == (void *)1) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if (d->bd_bif == NULL) {
lck_mtx_unlock(bpf_mlock);
return (ENXIO);
}
if (events & (POLLIN | POLLRDNORM)) {
if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
revents |= events & (POLLIN | POLLRDNORM);
else
selrecord(p, &d->bd_sel, wql);
}
lck_mtx_unlock(bpf_mlock);
return (revents);
}
static inline void*
_cast_non_const(const void * ptr) {
union {
const void* cval;
void* val;
} ret;
ret.cval = ptr;
return (ret.val);
}
static void
bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
{
struct mbuf *m = _cast_non_const(src_arg);
u_int count;
u_char *dst;
dst = dst_arg;
while (len > 0) {
if (m == 0)
panic("bpf_mcopy");
count = min(m->m_len, len);
bcopy(mbuf_data(m), dst, count);
m = m->m_next;
dst += count;
len -= count;
}
}
static inline void
bpf_tap_imp(
ifnet_t ifp,
u_int32_t dlt,
mbuf_t m,
void* hdr,
size_t hlen,
int outbound)
{
struct bpf_if *bp;
lck_mtx_lock(bpf_mlock);
if (ifp->if_bpf == NULL) {
lck_mtx_unlock(bpf_mlock);
return;
}
bp = ifp->if_bpf;
for (bp = ifp->if_bpf; bp && bp->bif_ifp == ifp &&
(dlt != 0 && bp->bif_dlt != dlt); bp = bp->bif_next)
;
if (bp && bp->bif_ifp == ifp && bp->bif_dlist != NULL) {
struct bpf_d *d;
struct m_hdr hack_hdr;
u_int pktlen = 0;
u_int slen = 0;
struct mbuf *m0;
if (hdr) {
hack_hdr.mh_next = m;
hack_hdr.mh_nextpkt = NULL;
hack_hdr.mh_len = hlen;
hack_hdr.mh_data = hdr;
hack_hdr.mh_type = m->m_type;
hack_hdr.mh_flags = 0;
m = (mbuf_t)&hack_hdr;
}
for (m0 = m; m0 != 0; m0 = m0->m_next)
pktlen += m0->m_len;
for (d = bp->bif_dlist; d; d = d->bd_next) {
if (outbound && !d->bd_seesent)
continue;
++d->bd_rcount;
slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
if (slen != 0) {
#if CONFIG_MACF_NET
if (mac_bpfdesc_check_receive(d, bp->bif_ifp) != 0)
continue;
#endif
catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
}
}
}
lck_mtx_unlock(bpf_mlock);
}
void
bpf_tap_out(
ifnet_t ifp,
u_int32_t dlt,
mbuf_t m,
void* hdr,
size_t hlen)
{
bpf_tap_imp(ifp, dlt, m, hdr, hlen, 1);
}
void
bpf_tap_in(
ifnet_t ifp,
u_int32_t dlt,
mbuf_t m,
void* hdr,
size_t hlen)
{
bpf_tap_imp(ifp, dlt, m, hdr, hlen, 0);
}
static int bpf_tap_callback(struct ifnet *ifp, struct mbuf *m)
{
bpf_tap_imp(ifp, 0, m, NULL, 0, mbuf_pkthdr_rcvif(m) == NULL);
return 0;
}
static void
catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
void (*cpfn)(const void *, void *, size_t))
{
struct bpf_hdr *hp;
int totlen, curlen;
int hdrlen = d->bd_bif->bif_hdrlen;
totlen = hdrlen + min(snaplen, pktlen);
if (totlen > d->bd_bufsize)
totlen = d->bd_bufsize;
curlen = BPF_WORDALIGN(d->bd_slen);
if (curlen + totlen > d->bd_bufsize) {
if (d->bd_fbuf == 0) {
++d->bd_dcount;
return;
}
ROTATE_BUFFERS(d);
bpf_wakeup(d);
curlen = 0;
}
else if (d->bd_immediate)
bpf_wakeup(d);
hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
microtime(&hp->bh_tstamp);
hp->bh_datalen = pktlen;
hp->bh_hdrlen = hdrlen;
(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
d->bd_slen = curlen + totlen;
}
static int
bpf_allocbufs(struct bpf_d *d)
{
d->bd_fbuf = (caddr_t) _MALLOC(d->bd_bufsize, M_DEVBUF, M_WAIT);
if (d->bd_fbuf == 0)
return (ENOBUFS);
d->bd_sbuf = (caddr_t) _MALLOC(d->bd_bufsize, M_DEVBUF, M_WAIT);
if (d->bd_sbuf == 0) {
FREE(d->bd_fbuf, M_DEVBUF);
return (ENOBUFS);
}
d->bd_slen = 0;
d->bd_hlen = 0;
return (0);
}
static void
bpf_freed(struct bpf_d *d)
{
if (d->bd_sbuf != 0) {
FREE(d->bd_sbuf, M_DEVBUF);
if (d->bd_hbuf != 0)
FREE(d->bd_hbuf, M_DEVBUF);
if (d->bd_fbuf != 0)
FREE(d->bd_fbuf, M_DEVBUF);
}
if (d->bd_filter)
FREE((caddr_t)d->bd_filter, M_DEVBUF);
}
void
bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
{
bpf_attach(ifp, dlt, hdrlen, NULL, NULL);
}
errno_t
bpf_attach(
ifnet_t ifp,
u_int32_t dlt,
u_int32_t hdrlen,
bpf_send_func send,
bpf_tap_func tap)
{
struct bpf_if *bp_new;
struct bpf_if *bp_temp;
struct bpf_if *bp_first = NULL;
bp_new = (struct bpf_if *) _MALLOC(sizeof(*bp_new), M_DEVBUF, M_WAIT);
if (bp_new == 0)
panic("bpfattach");
lck_mtx_lock(bpf_mlock);
for (bp_temp = bpf_iflist; bp_temp && (bp_temp->bif_ifp != ifp ||
bp_temp->bif_dlt != dlt); bp_temp = bp_temp->bif_next) {
if (bp_temp->bif_ifp == ifp && bp_first == NULL)
bp_first = bp_temp;
}
if (bp_temp != NULL) {
printf("bpfattach - %s%d with dlt %d is already attached\n",
ifp->if_name, ifp->if_unit, dlt);
FREE(bp_new, M_DEVBUF);
lck_mtx_unlock(bpf_mlock);
return EEXIST;
}
bzero(bp_new, sizeof(*bp_new));
bp_new->bif_ifp = ifp;
bp_new->bif_dlt = dlt;
bp_new->bif_send = send;
bp_new->bif_tap = tap;
if (bp_first == NULL) {
bp_new->bif_next = bpf_iflist;
bpf_iflist = bp_new;
}
else {
bp_new->bif_next = bp_first->bif_next;
bp_first->bif_next = bp_new;
}
bp_new->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
ifnet_reference(ifp);
lck_mtx_unlock(bpf_mlock);
#ifndef __APPLE__
if (bootverbose)
printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
#endif
return 0;
}
void
bpfdetach(struct ifnet *ifp)
{
struct bpf_if *bp, *bp_prev, *bp_next;
struct bpf_if *bp_free = NULL;
struct bpf_d *d;
lck_mtx_lock(bpf_mlock);
bp_prev = NULL;
for (bp = bpf_iflist; bp != NULL; bp = bp_next) {
bp_next = bp->bif_next;
if (ifp != bp->bif_ifp) {
bp_prev = bp;
continue;
}
while ((d = bp->bif_dlist) != NULL) {
bpf_detachd(d);
bpf_wakeup(d);
}
if (bp_prev) {
bp_prev->bif_next = bp->bif_next;
} else {
bpf_iflist = bp->bif_next;
}
bp->bif_next = bp_free;
bp_free = bp;
ifnet_release(ifp);
}
lck_mtx_unlock(bpf_mlock);
FREE(bp, M_DEVBUF);
}
void
bpf_init(__unused void *unused)
{
#ifdef __APPLE__
int i;
int maj;
if (bpf_devsw_installed == 0) {
bpf_devsw_installed = 1;
bpf_mlock_grp_attr = lck_grp_attr_alloc_init();
bpf_mlock_grp = lck_grp_alloc_init("bpf", bpf_mlock_grp_attr);
bpf_mlock_attr = lck_attr_alloc_init();
bpf_mlock = lck_mtx_alloc_init(bpf_mlock_grp, bpf_mlock_attr);
if (bpf_mlock == 0) {
printf("bpf_init: failed to allocate bpf_mlock\n");
bpf_devsw_installed = 0;
return;
}
maj = cdevsw_add(CDEV_MAJOR, &bpf_cdevsw);
if (maj == -1) {
if (bpf_mlock)
lck_mtx_free(bpf_mlock, bpf_mlock_grp);
if (bpf_mlock_attr)
lck_attr_free(bpf_mlock_attr);
if (bpf_mlock_grp)
lck_grp_free(bpf_mlock_grp);
if (bpf_mlock_grp_attr)
lck_grp_attr_free(bpf_mlock_grp_attr);
bpf_mlock = NULL;
bpf_mlock_attr = NULL;
bpf_mlock_grp = NULL;
bpf_mlock_grp_attr = NULL;
bpf_devsw_installed = 0;
printf("bpf_init: failed to allocate a major number!\n");
return;
}
for (i = 0 ; i < NBPFILTER; i++)
bpf_make_dev_t(maj);
}
#else
cdevsw_add(&bpf_cdevsw);
#endif
}
#ifndef __APPLE__
SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
#endif
#if CONFIG_MACF_NET
struct label *
mac_bpfdesc_label_get(struct bpf_d *d)
{
return (d->bd_label);
}
void
mac_bpfdesc_label_set(struct bpf_d *d, struct label *label)
{
d->bd_label = label;
}
#endif