bpf.c revision 171637
1185029Spjd/*- 2185029Spjd * Copyright (c) 1990, 1991, 1993 3185029Spjd * The Regents of the University of California. All rights reserved. 4185029Spjd * 5185029Spjd * This code is derived from the Stanford/CMU enet packet filter, 6185029Spjd * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7185029Spjd * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8185029Spjd * Berkeley Laboratory. 9185029Spjd * 10185029Spjd * Redistribution and use in source and binary forms, with or without 11185029Spjd * modification, are permitted provided that the following conditions 12185029Spjd * are met: 13185029Spjd * 1. Redistributions of source code must retain the above copyright 14185029Spjd * notice, this list of conditions and the following disclaimer. 15185029Spjd * 2. Redistributions in binary form must reproduce the above copyright 16185029Spjd * notice, this list of conditions and the following disclaimer in the 17185029Spjd * documentation and/or other materials provided with the distribution. 18185029Spjd * 4. Neither the name of the University nor the names of its contributors 19329175Skevans * may be used to endorse or promote products derived from this software 20329175Skevans * without specific prior written permission. 21185029Spjd * 22185029Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23185029Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24185096Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25185096Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26185096Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27185029Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28185029Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29185029Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30185029Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31185029Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32200309Sjhb * SUCH DAMAGE. 33185029Spjd * 34185029Spjd * @(#)bpf.c 8.4 (Berkeley) 1/9/95 35185029Spjd * 36185029Spjd * $FreeBSD: head/sys/net/bpf.c 171637 2007-07-28 07:31:30Z rwatson $ 37185029Spjd */ 38185029Spjd 39185029Spjd#include "opt_bpf.h" 40185029Spjd#include "opt_mac.h" 41185029Spjd#include "opt_netgraph.h" 42213136Spjd 43213136Spjd#include <sys/types.h> 44329100Skevans#include <sys/param.h> 45213136Spjd#include <sys/systm.h> 46235154Savg#include <sys/conf.h> 47294765Simp#include <sys/fcntl.h> 48185029Spjd#include <sys/malloc.h> 49235329Savg#include <sys/mbuf.h> 50235329Savg#include <sys/time.h> 51297629Sallanjude#include <sys/priv.h> 52297629Sallanjude#include <sys/proc.h> 53297629Sallanjude#include <sys/signalvar.h> 54185029Spjd#include <sys/filio.h> 55297629Sallanjude#include <sys/sockio.h> 56297629Sallanjude#include <sys/ttycom.h> 57297629Sallanjude#include <sys/uio.h> 58185029Spjd 59297629Sallanjude#include <sys/event.h> 60297629Sallanjude#include <sys/file.h> 61297629Sallanjude#include <sys/poll.h> 62297629Sallanjude#include <sys/proc.h> 63185029Spjd 64297629Sallanjude#include <sys/socket.h> 65297629Sallanjude 66185029Spjd#include <net/if.h> 67185029Spjd#include <net/bpf.h> 68185096Sdfr#ifdef BPF_JITTER 69185096Sdfr#include <net/bpf_jitter.h> 70185096Sdfr#endif 71185029Spjd#include <net/bpfdesc.h> 72185029Spjd 73185029Spjd#include <netinet/in.h> 74185029Spjd#include <netinet/if_ether.h> 75185029Spjd#include <sys/kernel.h> 76185029Spjd#include <sys/sysctl.h> 77185029Spjd 78185029Spjd#include <net80211/ieee80211_freebsd.h> 79185029Spjd 80185029Spjd#include <security/mac/mac_framework.h> 81185029Spjd 82185029Spjdstatic MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); 83185029Spjd 84185029Spjd#if defined(DEV_BPF) || defined(NETGRAPH_BPF) 85185029Spjd 86185029Spjd#define PRINET 26 /* interruptible */ 87185029Spjd 88213136Spjd#define M_SKIP_BPF M_SKIP_FIREWALL 89185029Spjd 90348866Skevans/* 91348866Skevans * bpf_iflist is a list of BPF interface structures, each corresponding to a 92348866Skevans * specific DLT. The same network interface might have several BPF interface 93348866Skevans * structures registered by different layers in the stack (i.e., 802.11 94348866Skevans * frames, ethernet frames, etc). 95348866Skevans */ 96348866Skevansstatic LIST_HEAD(, bpf_if) bpf_iflist; 97348866Skevansstatic struct mtx bpf_mtx; /* bpf global lock */ 98348866Skevansstatic int bpf_bpfd_cnt; 99348866Skevans 100348866Skevansstatic void bpf_allocbufs(struct bpf_d *); 101348866Skevansstatic void bpf_attachd(struct bpf_d *, struct bpf_if *); 102348866Skevansstatic void bpf_detachd(struct bpf_d *); 103348866Skevansstatic void bpf_freed(struct bpf_d *); 104348866Skevansstatic void bpf_mcopy(const void *, void *, size_t); 105348866Skevansstatic int bpf_movein(struct uio *, int, int, struct mbuf **, 106348866Skevans struct sockaddr *, int *, struct bpf_insn *); 107348866Skevansstatic int bpf_setif(struct bpf_d *, struct ifreq *); 108185029Spjdstatic void bpf_timed_out(void *); 109185029Spjdstatic __inline void 110185029Spjd bpf_wakeup(struct bpf_d *); 111234339Savgstatic void catchpacket(struct bpf_d *, u_char *, u_int, 112185029Spjd u_int, void (*)(const void *, void *, size_t), 113235329Savg struct timeval *); 114185029Spjdstatic void reset_d(struct bpf_d *); 115185029Spjdstatic int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd); 116185029Spjdstatic int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 117235329Savgstatic int bpf_setdlt(struct bpf_d *, u_int); 118185029Spjdstatic void filt_bpfdetach(struct knote *); 119200309Sjhbstatic int filt_bpfread(struct knote *, long); 120200309Sjhbstatic void bpf_drvinit(void *); 121200309Sjhbstatic void bpf_clone(void *, struct ucred *, char *, int, struct cdev **); 122200309Sjhbstatic int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS); 123200309Sjhb 124200309SjhbSYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl"); 125200309Sjhbstatic int bpf_bufsize = 4096; 126200309SjhbSYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW, 127328866Skevans &bpf_bufsize, 0, "Default bpf buffer size"); 128200309Sjhbstatic int bpf_maxbufsize = BPF_MAXBUFSIZE; 129200309SjhbSYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW, 130200309Sjhb &bpf_maxbufsize, 0, "Maximum bpf buffer size"); 131200309Sjhbstatic int bpf_maxinsns = BPF_MAXINSNS; 132185029SpjdSYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW, 133297629Sallanjude &bpf_maxinsns, 0, "Maximum bpf program instructions"); 134185029SpjdSYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW, 135185029Spjd bpf_stats_sysctl, "bpf statistics portal"); 136185029Spjd 137185029Spjdstatic d_open_t bpfopen; 138185029Spjdstatic d_close_t bpfclose; 139185029Spjdstatic d_read_t bpfread; 140185029Spjdstatic d_write_t bpfwrite; 141308914Savgstatic d_ioctl_t bpfioctl; 142185029Spjdstatic d_poll_t bpfpoll; 143329099Skevansstatic d_kqfilter_t bpfkqfilter; 144200309Sjhb 145329100Skevansstatic struct cdevsw bpf_cdevsw = { 146185029Spjd .d_version = D_VERSION, 147296963Sallanjude .d_open = bpfopen, 148344399Skevans .d_close = bpfclose, 149296963Sallanjude .d_read = bpfread, 150296963Sallanjude .d_write = bpfwrite, 151296963Sallanjude .d_ioctl = bpfioctl, 152344399Skevans .d_poll = bpfpoll, 153344399Skevans .d_name = "bpf", 154344399Skevans .d_kqfilter = bpfkqfilter, 155344399Skevans}; 156344399Skevans 157344399Skevansstatic struct filterops bpfread_filtops = 158344399Skevans { 1, NULL, filt_bpfdetach, filt_bpfread }; 159185029Spjd 160185029Spjdstatic int 161185029Spjdbpf_movein(struct uio *uio, int linktype, int mtu, struct mbuf **mp, 162185029Spjd struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter) 163185029Spjd{ 164185029Spjd const struct ieee80211_bpf_params *p; 165185029Spjd struct mbuf *m; 166185029Spjd int error; 167185029Spjd int len; 168185029Spjd int hlen; 169185029Spjd int slen; 170185029Spjd 171185029Spjd /* 172185029Spjd * Build a sockaddr based on the data link layer type. 173185029Spjd * We do this at this level because the ethernet header 174308914Savg * is copied directly into the data field of the sockaddr. 175185029Spjd * In the case of SLIP, there is no header and the packet 176185029Spjd * is forwarded as is. 177185029Spjd * Also, we are careful to leave room at the front of the mbuf 178185029Spjd * for the link level header. 179185029Spjd */ 180185029Spjd switch (linktype) { 181185029Spjd 182185029Spjd case DLT_SLIP: 183185029Spjd sockp->sa_family = AF_INET; 184185029Spjd hlen = 0; 185185029Spjd break; 186235329Savg 187241293Savg case DLT_EN10MB: 188241293Savg sockp->sa_family = AF_UNSPEC; 189185029Spjd /* XXX Would MAXLINKHDR be better? */ 190185029Spjd hlen = ETHER_HDR_LEN; 191185029Spjd break; 192185029Spjd 193185029Spjd case DLT_FDDI: 194185029Spjd sockp->sa_family = AF_IMPLINK; 195329175Skevans hlen = 0; 196185029Spjd break; 197185029Spjd 198297629Sallanjude case DLT_RAW: 199300257Sallanjude sockp->sa_family = AF_UNSPEC; 200297629Sallanjude hlen = 0; 201344399Skevans break; 202185029Spjd 203185029Spjd case DLT_NULL: 204185029Spjd /* 205185029Spjd * null interface types require a 4 byte pseudo header which 206185029Spjd * corresponds to the address family of the packet. 207185029Spjd */ 208344399Skevans sockp->sa_family = AF_UNSPEC; 209297629Sallanjude hlen = 4; 210300257Sallanjude break; 211300257Sallanjude 212300257Sallanjude case DLT_ATM_RFC1483: 213300257Sallanjude /* 214300257Sallanjude * en atm driver requires 4-byte atm pseudo header. 215297629Sallanjude * though it isn't standard, vpi:vci needs to be 216297629Sallanjude * specified anyway. 217297629Sallanjude */ 218344399Skevans sockp->sa_family = AF_UNSPEC; 219297629Sallanjude hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 220297629Sallanjude break; 221185029Spjd 222185029Spjd case DLT_PPP: 223297629Sallanjude sockp->sa_family = AF_UNSPEC; 224297629Sallanjude hlen = 4; /* This should match PPP_HDRLEN */ 225297629Sallanjude break; 226297629Sallanjude 227298949Speter case DLT_IEEE802_11: /* IEEE 802.11 wireless */ 228298949Speter sockp->sa_family = AF_IEEE80211; 229297629Sallanjude hlen = 0; 230297629Sallanjude break; 231297629Sallanjude 232297629Sallanjude case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */ 233298949Speter sockp->sa_family = AF_IEEE80211; 234298949Speter sockp->sa_len = 12; /* XXX != 0 */ 235297629Sallanjude hlen = sizeof(struct ieee80211_bpf_params); 236344399Skevans break; 237344399Skevans 238344399Skevans default: 239344399Skevans return (EIO); 240344399Skevans } 241344399Skevans 242344295Skevans len = uio->uio_resid; 243344295Skevans 244344399Skevans if (len - hlen > mtu) 245185029Spjd return (EMSGSIZE); 246296963Sallanjude 247296963Sallanjude if ((unsigned)len > MCLBYTES) 248344399Skevans return (EIO); 249344399Skevans 250297629Sallanjude if (len > MHLEN) { 251297629Sallanjude m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR); 252296963Sallanjude } else { 253296963Sallanjude MGETHDR(m, M_TRYWAIT, MT_DATA); 254297629Sallanjude } 255185029Spjd if (m == NULL) 256185029Spjd return (ENOBUFS); 257297629Sallanjude m->m_pkthdr.len = m->m_len = len; 258185029Spjd m->m_pkthdr.rcvif = NULL; 259297629Sallanjude *mp = m; 260297629Sallanjude 261185029Spjd if (m->m_len < hlen) { 262185029Spjd error = EPERM; 263185029Spjd goto bad; 264185029Spjd } 265329175Skevans 266329175Skevans error = uiomove(mtod(m, u_char *), len, uio); 267329175Skevans if (error) 268329175Skevans goto bad; 269329175Skevans 270329175Skevans slen = bpf_filter(wfilter, mtod(m, u_char *), len, len); 271185029Spjd if (slen == 0) { 272329175Skevans error = EPERM; 273185029Spjd goto bad; 274308914Savg } 275308914Savg 276308914Savg /* 277308914Savg * Make room for link header, and copy it to sockaddr 278308914Savg */ 279344399Skevans if (hlen != 0) { 280308914Savg if (sockp->sa_family == AF_IEEE80211) { 281308914Savg /* 282308914Savg * Collect true length from the parameter header 283308914Savg * NB: sockp is known to be zero'd so if we do a 284308914Savg * short copy unspecified parameters will be 285308914Savg * zero. 286344399Skevans * NB: packet may not be aligned after stripping 287308914Savg * bpf params 288308914Savg * XXX check ibp_vers 289308914Savg */ 290308914Savg p = mtod(m, const struct ieee80211_bpf_params *); 291308914Savg hlen = p->ibp_len; 292344399Skevans if (hlen > sizeof(sockp->sa_data)) { 293308914Savg error = EINVAL; 294308914Savg goto bad; 295308914Savg } 296308914Savg } 297308914Savg bcopy(m->m_data, sockp->sa_data, hlen); 298308914Savg } 299308914Savg *hdrlen = hlen; 300308914Savg 301308914Savg return (0); 302308914Savgbad: 303185029Spjd m_freem(m); 304185029Spjd return (error); 305185029Spjd} 306235329Savg 307185029Spjd/* 308185029Spjd * Attach file to the bpf interface, i.e. make d listen on bp. 309185029Spjd */ 310185029Spjdstatic void 311185029Spjdbpf_attachd(struct bpf_d *d, struct bpf_if *bp) 312308914Savg{ 313308914Savg /* 314308914Savg * Point d at bp, and add d to the interface's list of listeners. 315308914Savg * Finally, point the driver's bpf cookie at the interface so 316308914Savg * it will divert packets to bpf. 317308914Savg */ 318308914Savg BPFIF_LOCK(bp); 319308914Savg d->bd_bif = bp; 320308914Savg LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 321308914Savg 322308914Savg bpf_bpfd_cnt++; 323308914Savg BPFIF_UNLOCK(bp); 324308914Savg} 325308914Savg 326308914Savg/* 327308914Savg * Detach a file from its interface. 328308914Savg */ 329308914Savgstatic void 330308914Savgbpf_detachd(struct bpf_d *d) 331308914Savg{ 332308914Savg int error; 333308914Savg struct bpf_if *bp; 334308914Savg struct ifnet *ifp; 335308914Savg 336308914Savg bp = d->bd_bif; 337308914Savg BPFIF_LOCK(bp); 338308914Savg BPFD_LOCK(d); 339308914Savg ifp = d->bd_bif->bif_ifp; 340308914Savg 341308914Savg /* 342308914Savg * Remove d from the interface's descriptor list. 343308914Savg */ 344308914Savg LIST_REMOVE(d, bd_next); 345308914Savg 346308914Savg bpf_bpfd_cnt--; 347308914Savg d->bd_bif = NULL; 348308914Savg BPFD_UNLOCK(d); 349308914Savg BPFIF_UNLOCK(bp); 350308914Savg 351308914Savg /* 352308914Savg * Check if this descriptor had requested promiscuous mode. 353308914Savg * If so, turn it off. 354308914Savg */ 355308914Savg if (d->bd_promisc) { 356308914Savg d->bd_promisc = 0; 357308914Savg error = ifpromisc(ifp, 0); 358200309Sjhb if (error != 0 && error != ENXIO) { 359200309Sjhb /* 360185029Spjd * ENXIO can happen if a pccard is unplugged 361200309Sjhb * Something is really wrong if we were able to put 362185029Spjd * the driver into promiscuous mode, but can't 363200309Sjhb * take it out. 364200309Sjhb */ 365200309Sjhb if_printf(bp->bif_ifp, 366200309Sjhb "bpf_detach: ifpromisc failed (%d)\n", error); 367200309Sjhb } 368200309Sjhb } 369200309Sjhb} 370200309Sjhb 371200309Sjhb/* 372200309Sjhb * Open ethernet device. Returns ENXIO for illegal minor device number, 373200309Sjhb * EBUSY if file is open by another process. 374292682Sjhb */ 375200309Sjhb/* ARGSUSED */ 376200309Sjhbstatic int 377200309Sjhbbpfopen(struct cdev *dev, int flags, int fmt, struct thread *td) 378200309Sjhb{ 379200309Sjhb struct bpf_d *d; 380200309Sjhb 381200309Sjhb mtx_lock(&bpf_mtx); 382200309Sjhb d = dev->si_drv1; 383200309Sjhb /* 384200309Sjhb * Each minor can be opened by only one process. If the requested 385200309Sjhb * minor is in use, return EBUSY. 386200309Sjhb */ 387200309Sjhb if (d != NULL) { 388200309Sjhb mtx_unlock(&bpf_mtx); 389200309Sjhb return (EBUSY); 390200309Sjhb } 391200309Sjhb dev->si_drv1 = (struct bpf_d *)~0; /* mark device in use */ 392200309Sjhb mtx_unlock(&bpf_mtx); 393200309Sjhb 394200309Sjhb if ((dev->si_flags & SI_NAMED) == 0) 395200309Sjhb make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600, 396200309Sjhb "bpf%d", dev2unit(dev)); 397200309Sjhb MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO); 398200309Sjhb dev->si_drv1 = d; 399200309Sjhb d->bd_bufsize = bpf_bufsize; 400200309Sjhb d->bd_sig = SIGIO; 401200309Sjhb d->bd_direction = BPF_D_INOUT; 402200309Sjhb d->bd_pid = td->td_proc->p_pid; 403200309Sjhb#ifdef MAC 404200309Sjhb mac_init_bpfdesc(d); 405200309Sjhb mac_create_bpfdesc(td->td_ucred, d); 406200309Sjhb#endif 407200309Sjhb mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF); 408200309Sjhb callout_init(&d->bd_callout, CALLOUT_MPSAFE); 409200309Sjhb knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL); 410200309Sjhb 411200309Sjhb return (0); 412200309Sjhb} 413200309Sjhb 414200309Sjhb/* 415200309Sjhb * Close the descriptor by detaching it from its interface, 416200309Sjhb * deallocating its buffers, and marking it free. 417200309Sjhb */ 418200309Sjhb/* ARGSUSED */ 419200309Sjhbstatic int 420200309Sjhbbpfclose(struct cdev *dev, int flags, int fmt, struct thread *td) 421292682Sjhb{ 422200309Sjhb struct bpf_d *d = dev->si_drv1; 423200309Sjhb 424200309Sjhb BPFD_LOCK(d); 425200309Sjhb if (d->bd_state == BPF_WAITING) 426200309Sjhb callout_stop(&d->bd_callout); 427200309Sjhb d->bd_state = BPF_IDLE; 428200309Sjhb BPFD_UNLOCK(d); 429200309Sjhb funsetown(&d->bd_sigio); 430200309Sjhb mtx_lock(&bpf_mtx); 431200309Sjhb if (d->bd_bif) 432200309Sjhb bpf_detachd(d); 433200309Sjhb mtx_unlock(&bpf_mtx); 434200309Sjhb selwakeuppri(&d->bd_sel, PRINET); 435200309Sjhb#ifdef MAC 436200309Sjhb mac_destroy_bpfdesc(d); 437200309Sjhb#endif /* MAC */ 438200309Sjhb knlist_destroy(&d->bd_sel.si_note); 439200309Sjhb bpf_freed(d); 440200309Sjhb dev->si_drv1 = NULL; 441200309Sjhb free(d, M_BPF); 442296963Sallanjude 443200309Sjhb return (0); 444185029Spjd} 445185029Spjd 446185029Spjd 447185029Spjd/* 448185029Spjd * Rotate the packet buffers in descriptor d. Move the store buffer 449185029Spjd * into the hold slot, and the free buffer into the store slot. 450185029Spjd * Zero the length of the new store buffer. 451185029Spjd */ 452185029Spjd#define ROTATE_BUFFERS(d) \ 453185029Spjd (d)->bd_hbuf = (d)->bd_sbuf; \ 454185029Spjd (d)->bd_hlen = (d)->bd_slen; \ 455185029Spjd (d)->bd_sbuf = (d)->bd_fbuf; \ 456292682Sjhb (d)->bd_slen = 0; \ 457185029Spjd (d)->bd_fbuf = NULL; 458185029Spjd/* 459185029Spjd * bpfread - read next chunk of packets from buffers 460185029Spjd */ 461185029Spjdstatic int 462185029Spjdbpfread(struct cdev *dev, struct uio *uio, int ioflag) 463185029Spjd{ 464185029Spjd struct bpf_d *d = dev->si_drv1; 465185029Spjd int timed_out; 466192194Sdfr int error; 467192194Sdfr 468192194Sdfr /* 469192194Sdfr * Restrict application to use a buffer the same size as 470344399Skevans * as kernel buffers. 471344399Skevans */ 472192194Sdfr if (uio->uio_resid != d->bd_bufsize) 473344399Skevans return (EINVAL); 474192194Sdfr 475344399Skevans BPFD_LOCK(d); 476344399Skevans if (d->bd_state == BPF_WAITING) 477192194Sdfr callout_stop(&d->bd_callout); 478192194Sdfr timed_out = (d->bd_state == BPF_TIMED_OUT); 479192194Sdfr d->bd_state = BPF_IDLE; 480329100Skevans /* 481329100Skevans * If the hold buffer is empty, then do a timed sleep, which 482329100Skevans * ends when the timeout expires or when enough packets 483329100Skevans * have arrived to fill the store buffer. 484329100Skevans */ 485329100Skevans while (d->bd_hbuf == NULL) { 486329100Skevans if ((d->bd_immediate || timed_out) && d->bd_slen != 0) { 487329100Skevans /* 488344399Skevans * A packet(s) either arrived since the previous 489329100Skevans * read or arrived while we were asleep. 490344399Skevans * Rotate the buffers and return what's here. 491329100Skevans */ 492329100Skevans ROTATE_BUFFERS(d); 493329100Skevans break; 494344399Skevans } 495344399Skevans 496329100Skevans /* 497329100Skevans * No data is available, check to see if the bpf device 498329100Skevans * is still pointed at a real interface. If not, return 499329100Skevans * ENXIO so that the userland process knows to rebind 500329100Skevans * it before using it again. 501329100Skevans */ 502329100Skevans if (d->bd_bif == NULL) { 503329100Skevans BPFD_UNLOCK(d); 504329100Skevans return (ENXIO); 505329100Skevans } 506329100Skevans 507329100Skevans if (ioflag & O_NONBLOCK) { 508329100Skevans BPFD_UNLOCK(d); 509329100Skevans return (EWOULDBLOCK); 510329100Skevans } 511329100Skevans error = msleep(d, &d->bd_mtx, PRINET|PCATCH, 512329100Skevans "bpf", d->bd_rtout); 513329100Skevans if (error == EINTR || error == ERESTART) { 514329100Skevans BPFD_UNLOCK(d); 515329100Skevans return (error); 516329100Skevans } 517329100Skevans if (error == EWOULDBLOCK) { 518329100Skevans /* 519329100Skevans * On a timeout, return what's in the buffer, 520329100Skevans * which may be nothing. If there is something 521329100Skevans * in the store buffer, we can rotate the buffers. 522329100Skevans */ 523329100Skevans if (d->bd_hbuf) 524329100Skevans /* 525329100Skevans * We filled up the buffer in between 526329100Skevans * getting the timeout and arriving 527329100Skevans * here, so we don't need to rotate. 528329100Skevans */ 529329100Skevans break; 530329100Skevans 531329100Skevans if (d->bd_slen == 0) { 532329100Skevans BPFD_UNLOCK(d); 533329100Skevans return (0); 534329100Skevans } 535329100Skevans ROTATE_BUFFERS(d); 536329100Skevans break; 537329100Skevans } 538329100Skevans } 539329100Skevans /* 540344399Skevans * At this point, we know we have something in the hold slot. 541344399Skevans */ 542329100Skevans BPFD_UNLOCK(d); 543344399Skevans 544344399Skevans /* 545329100Skevans * Move data from hold buffer into user space. 546329100Skevans * We know the entire buffer is transferred since 547329100Skevans * we checked above that the read buffer is bpf_bufsize bytes. 548329100Skevans */ 549185029Spjd error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 550344399Skevans 551185029Spjd BPFD_LOCK(d); 552185096Sdfr d->bd_fbuf = d->bd_hbuf; 553185096Sdfr d->bd_hbuf = NULL; 554185096Sdfr d->bd_hlen = 0; 555185096Sdfr BPFD_UNLOCK(d); 556299997Speter 557185096Sdfr return (error); 558299997Speter} 559299997Speter 560299660Sngie 561299997Speter/* 562185029Spjd * If there are processes sleeping on this descriptor, wake them up. 563185029Spjd */ 564185029Spjdstatic __inline void 565185029Spjdbpf_wakeup(struct bpf_d *d) 566185029Spjd{ 567296963Sallanjude 568185029Spjd BPFD_LOCK_ASSERT(d); 569344399Skevans if (d->bd_state == BPF_WAITING) { 570185029Spjd callout_stop(&d->bd_callout); 571185029Spjd d->bd_state = BPF_IDLE; 572296963Sallanjude } 573296963Sallanjude wakeup(d); 574296963Sallanjude if (d->bd_async && d->bd_sig && d->bd_sigio) 575296963Sallanjude pgsigio(&d->bd_sigio, d->bd_sig, 0); 576296963Sallanjude 577296963Sallanjude selwakeuppri(&d->bd_sel, PRINET); 578296963Sallanjude KNOTE_LOCKED(&d->bd_sel.si_note, 0); 579344399Skevans} 580296963Sallanjude 581296963Sallanjudestatic void 582296963Sallanjudebpf_timed_out(void *arg) 583344399Skevans{ 584344399Skevans struct bpf_d *d = (struct bpf_d *)arg; 585344399Skevans 586344399Skevans BPFD_LOCK(d); 587344399Skevans if (d->bd_state == BPF_WAITING) { 588296963Sallanjude d->bd_state = BPF_TIMED_OUT; 589296963Sallanjude if (d->bd_slen != 0) 590296963Sallanjude bpf_wakeup(d); 591296963Sallanjude } 592296963Sallanjude BPFD_UNLOCK(d); 593296963Sallanjude} 594185029Spjd 595344399Skevansstatic int 596185096Sdfrbpfwrite(struct cdev *dev, struct uio *uio, int ioflag) 597185096Sdfr{ 598185096Sdfr struct bpf_d *d = dev->si_drv1; 599185096Sdfr struct ifnet *ifp; 600185096Sdfr struct mbuf *m, *mc; 601344399Skevans struct sockaddr dst; 602185096Sdfr int error, hlen; 603185096Sdfr 604185096Sdfr if (d->bd_bif == NULL) 605185096Sdfr return (ENXIO); 606185096Sdfr 607185096Sdfr ifp = d->bd_bif->bif_ifp; 608185096Sdfr 609185096Sdfr if ((ifp->if_flags & IFF_UP) == 0) 610185096Sdfr return (ENETDOWN); 611185096Sdfr 612298826Spfg if (uio->uio_resid == 0) 613185096Sdfr return (0); 614185096Sdfr 615185096Sdfr bzero(&dst, sizeof(dst)); 616296963Sallanjude m = NULL; 617296963Sallanjude hlen = 0; 618185096Sdfr error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, 619185096Sdfr &m, &dst, &hlen, d->bd_wfilter); 620185096Sdfr if (error) 621185096Sdfr return (error); 622185096Sdfr 623344399Skevans if (d->bd_hdrcmplt) 624344399Skevans dst.sa_family = pseudo_AF_HDRCMPLT; 625185096Sdfr 626185096Sdfr if (d->bd_feedback) { 627185096Sdfr mc = m_dup(m, M_DONTWAIT); 628185096Sdfr if (mc != NULL) 629185096Sdfr mc->m_pkthdr.rcvif = ifp; 630344399Skevans /* XXX Do not return the same packet twice. */ 631344399Skevans if (d->bd_direction == BPF_D_INOUT) 632344399Skevans m->m_flags |= M_SKIP_BPF; 633344399Skevans } else 634344399Skevans mc = NULL; 635185096Sdfr 636185096Sdfr m->m_pkthdr.len -= hlen; 637185096Sdfr m->m_len -= hlen; 638185096Sdfr m->m_data += hlen; /* XXX */ 639344399Skevans 640185096Sdfr#ifdef MAC 641296963Sallanjude BPFD_LOCK(d); 642344399Skevans mac_create_mbuf_from_bpfdesc(d, m); 643344399Skevans if (mc != NULL) 644344399Skevans mac_create_mbuf_from_bpfdesc(d, mc); 645344399Skevans BPFD_UNLOCK(d); 646344399Skevans#endif 647296963Sallanjude 648296963Sallanjude NET_LOCK_GIANT(); 649296963Sallanjude error = (*ifp->if_output)(ifp, m, &dst, NULL); 650344399Skevans NET_UNLOCK_GIANT(); 651296963Sallanjude 652296963Sallanjude if (mc != NULL) { 653296963Sallanjude if (error == 0) { 654296963Sallanjude NET_LOCK_GIANT(); 655344399Skevans (*ifp->if_input)(ifp, mc); 656296963Sallanjude NET_UNLOCK_GIANT(); 657296963Sallanjude } else 658296963Sallanjude m_freem(mc); 659296963Sallanjude } 660296963Sallanjude 661185096Sdfr return (error); 662185096Sdfr} 663185096Sdfr 664185096Sdfr/* 665185096Sdfr * Reset a descriptor by flushing its packet buffer and clearing the 666185096Sdfr * receive and drop counts. 667296963Sallanjude */ 668185096Sdfrstatic void 669344399Skevansreset_d(struct bpf_d *d) 670185029Spjd{ 671185029Spjd 672185029Spjd mtx_assert(&d->bd_mtx, MA_OWNED); 673185029Spjd if (d->bd_hbuf) { 674185029Spjd /* Free the hold buffer. */ 675185029Spjd d->bd_fbuf = d->bd_hbuf; 676344399Skevans d->bd_hbuf = NULL; 677344399Skevans } 678344399Skevans d->bd_slen = 0; 679344399Skevans d->bd_hlen = 0; 680344399Skevans d->bd_rcount = 0; 681185029Spjd d->bd_dcount = 0; 682296963Sallanjude d->bd_fcount = 0; 683344399Skevans} 684344399Skevans 685344399Skevans/* 686344399Skevans * FIONREAD Check for read packet available. 687296963Sallanjude * SIOCGIFADDR Get interface address - convenient hook to driver. 688296963Sallanjude * BIOCGBLEN Get buffer len [for read()]. 689296963Sallanjude * BIOCSETF Set ethernet read filter. 690344399Skevans * BIOCSETWF Set ethernet write filter. 691296963Sallanjude * BIOCFLUSH Flush read packet buffer. 692296963Sallanjude * BIOCPROMISC Put interface into promiscuous mode. 693296963Sallanjude * BIOCGDLT Get link layer type. 694296963Sallanjude * BIOCGETIF Get interface name. 695344399Skevans * BIOCSETIF Set interface. 696296963Sallanjude * BIOCSRTIMEOUT Set read timeout. 697296963Sallanjude * BIOCGRTIMEOUT Get read timeout. 698296963Sallanjude * BIOCGSTATS Get packet stats. 699296963Sallanjude * BIOCIMMEDIATE Set immediate mode. 700296963Sallanjude * BIOCVERSION Get filter language version. 701185029Spjd * BIOCGHDRCMPLT Get "header already complete" flag 702185029Spjd * BIOCSHDRCMPLT Set "header already complete" flag 703185029Spjd * BIOCGDIRECTION Get packet direction flag 704185029Spjd * BIOCSDIRECTION Set packet direction flag 705185029Spjd * BIOCLOCK Set "locked" flag 706185029Spjd * BIOCFEEDBACK Set packet feedback mode. 707185029Spjd */ 708185029Spjd/* ARGSUSED */ 709344399Skevansstatic int 710308914Savgbpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, 711308914Savg struct thread *td) 712308914Savg{ 713185029Spjd struct bpf_d *d = dev->si_drv1; 714208388Sjhb int error = 0; 715208388Sjhb 716200309Sjhb /* 717200309Sjhb * Refresh PID associated with this descriptor. 718200309Sjhb */ 719200309Sjhb BPFD_LOCK(d); 720200309Sjhb d->bd_pid = td->td_proc->p_pid; 721200309Sjhb if (d->bd_state == BPF_WAITING) 722296963Sallanjude callout_stop(&d->bd_callout); 723296963Sallanjude d->bd_state = BPF_IDLE; 724200309Sjhb BPFD_UNLOCK(d); 725329175Skevans 726200309Sjhb if (d->bd_locked == 1) { 727344399Skevans switch (cmd) { 728344399Skevans case BIOCGBLEN: 729344399Skevans case BIOCFLUSH: 730344399Skevans case BIOCGDLT: 731344399Skevans case BIOCGDLTLIST: 732344399Skevans case BIOCGETIF: 733344399Skevans case BIOCGRTIMEOUT: 734344399Skevans case BIOCGSTATS: 735185029Spjd case BIOCVERSION: 736185029Spjd case BIOCGRSIG: 737185029Spjd case BIOCGHDRCMPLT: 738200309Sjhb case BIOCFEEDBACK: 739200309Sjhb case FIONREAD: 740185029Spjd case BIOCLOCK: 741344399Skevans case BIOCSRTIMEOUT: 742185029Spjd case BIOCIMMEDIATE: 743344399Skevans case TIOCGPGRP: 744344399Skevans break; 745185029Spjd default: 746185029Spjd return (EPERM); 747185029Spjd } 748185029Spjd } 749185029Spjd switch (cmd) { 750185029Spjd 751185029Spjd default: 752185029Spjd error = EINVAL; 753185029Spjd break; 754185029Spjd 755185029Spjd /* 756344399Skevans * Check for read packet available. 757185029Spjd */ 758185029Spjd case FIONREAD: 759185029Spjd { 760185029Spjd int n; 761185029Spjd 762185029Spjd BPFD_LOCK(d); 763212805Spjd n = d->bd_slen; 764212805Spjd if (d->bd_hbuf) 765212805Spjd n += d->bd_hlen; 766212805Spjd BPFD_UNLOCK(d); 767212805Spjd 768212805Spjd *(int *)addr = n; 769185029Spjd break; 770185029Spjd } 771185029Spjd 772192194Sdfr case SIOCGIFADDR: 773192194Sdfr { 774192194Sdfr struct ifnet *ifp; 775344399Skevans 776344399Skevans if (d->bd_bif == NULL) 777344399Skevans error = EINVAL; 778344399Skevans else { 779344399Skevans NET_LOCK_GIANT(); 780344399Skevans ifp = d->bd_bif->bif_ifp; 781344399Skevans error = (*ifp->if_ioctl)(ifp, cmd, addr); 782344399Skevans NET_UNLOCK_GIANT(); 783344399Skevans } 784185029Spjd break; 785185029Spjd } 786185029Spjd 787241294Savg /* 788185029Spjd * Get buffer len [for read()]. 789241294Savg */ 790185029Spjd case BIOCGBLEN: 791241294Savg *(u_int *)addr = d->bd_bufsize; 792241294Savg break; 793241294Savg 794185029Spjd /* 795185029Spjd * Set buffer length. 796241293Savg */ 797241293Savg case BIOCSBLEN: 798241293Savg if (d->bd_bif != NULL) 799308914Savg error = EINVAL; 800308914Savg else { 801308914Savg u_int size = *(u_int *)addr; 802308914Savg 803308914Savg if (size > bpf_maxbufsize) 804308914Savg *(u_int *)addr = size = bpf_maxbufsize; 805308914Savg else if (size < BPF_MINBUFSIZE) 806308914Savg *(u_int *)addr = size = BPF_MINBUFSIZE; 807308914Savg d->bd_bufsize = size; 808308914Savg } 809308914Savg break; 810308914Savg 811308914Savg /* 812308914Savg * Set link layer read filter. 813308914Savg */ 814308914Savg case BIOCSETF: 815308914Savg case BIOCSETWF: 816329099Skevans error = bpf_setf(d, (struct bpf_program *)addr, cmd); 817308914Savg break; 818308914Savg 819308914Savg /* 820308914Savg * Flush read packet buffer. 821308914Savg */ 822308914Savg case BIOCFLUSH: 823308914Savg BPFD_LOCK(d); 824308914Savg reset_d(d); 825308914Savg BPFD_UNLOCK(d); 826308914Savg break; 827308914Savg 828308914Savg /* 829308914Savg * Put interface into promiscuous mode. 830308914Savg */ 831308914Savg case BIOCPROMISC: 832235329Savg if (d->bd_bif == NULL) { 833235329Savg /* 834235329Savg * No interface attached yet. 835235329Savg */ 836235329Savg error = EINVAL; 837185029Spjd break; 838198079Sjhb } 839185029Spjd if (d->bd_promisc == 0) { 840185029Spjd NET_LOCK_GIANT(); 841185029Spjd error = ifpromisc(d->bd_bif->bif_ifp, 1); 842234339Savg NET_UNLOCK_GIANT(); 843329099Skevans if (error == 0) 844234339Savg d->bd_promisc = 1; 845234339Savg } 846234339Savg break; 847329099Skevans 848185029Spjd /* 849234339Savg * Get current data link type. 850241288Savg */ 851185029Spjd case BIOCGDLT: 852185029Spjd if (d->bd_bif == NULL) 853185029Spjd error = EINVAL; 854185029Spjd else 855308914Savg *(u_int *)addr = d->bd_bif->bif_dlt; 856308914Savg break; 857308914Savg 858308914Savg /* 859185029Spjd * Get a list of supported data link types. 860348866Skevans */ 861348866Skevans case BIOCGDLTLIST: 862348866Skevans if (d->bd_bif == NULL) 863348866Skevans error = EINVAL; 864348866Skevans else 865348866Skevans error = bpf_getdltlist(d, (struct bpf_dltlist *)addr); 866348866Skevans break; 867348866Skevans 868348866Skevans /* 869185029Spjd * Set data link type. 870185029Spjd */ 871185029Spjd case BIOCSDLT: 872185029Spjd if (d->bd_bif == NULL) 873185029Spjd error = EINVAL; 874185029Spjd else 875185029Spjd error = bpf_setdlt(d, *(u_int *)addr); 876235329Savg break; 877235329Savg 878235329Savg /* 879241288Savg * Get interface name. 880235329Savg */ 881235329Savg case BIOCGETIF: 882241288Savg if (d->bd_bif == NULL) 883241288Savg error = EINVAL; 884241288Savg else { 885241288Savg struct ifnet *const ifp = d->bd_bif->bif_ifp; 886235329Savg struct ifreq *const ifr = (struct ifreq *)addr; 887241288Savg 888235329Savg strlcpy(ifr->ifr_name, ifp->if_xname, 889241288Savg sizeof(ifr->ifr_name)); 890235329Savg } 891185029Spjd break; 892185029Spjd 893213136Spjd /* 894213136Spjd * Set interface. 895185029Spjd */ 896185029Spjd case BIOCSETIF: 897185029Spjd error = bpf_setif(d, (struct ifreq *)addr); 898329099Skevans break; 899185029Spjd 900185029Spjd /* 901185029Spjd * Set read timeout. 902185029Spjd */ 903185029Spjd case BIOCSRTIMEOUT: 904185029Spjd { 905185029Spjd struct timeval *tv = (struct timeval *)addr; 906185029Spjd 907185029Spjd /* 908185029Spjd * Subtract 1 tick from tvtohz() since this isn't 909308914Savg * a one-shot timer. 910185029Spjd */ 911185029Spjd if ((error = itimerfix(tv)) == 0) 912308914Savg d->bd_rtout = tvtohz(tv) - 1; 913308914Savg break; 914308914Savg } 915308914Savg 916308914Savg /* 917308914Savg * Get read timeout. 918185029Spjd */ 919185029Spjd case BIOCGRTIMEOUT: 920185029Spjd { 921185029Spjd struct timeval *tv = (struct timeval *)addr; 922185029Spjd 923185029Spjd tv->tv_sec = d->bd_rtout / hz; 924185029Spjd tv->tv_usec = (d->bd_rtout % hz) * tick; 925185029Spjd break; 926185029Spjd } 927185029Spjd 928185029Spjd /* 929185029Spjd * Get packet stats. 930185029Spjd */ 931185029Spjd case BIOCGSTATS: 932185029Spjd { 933235329Savg struct bpf_stat *bs = (struct bpf_stat *)addr; 934235329Savg 935185029Spjd bs->bs_recv = d->bd_rcount; 936185029Spjd bs->bs_drop = d->bd_dcount; 937185029Spjd break; 938185029Spjd } 939185029Spjd 940185029Spjd /* 941185029Spjd * Set immediate mode. 942185029Spjd */ 943185029Spjd case BIOCIMMEDIATE: 944185029Spjd d->bd_immediate = *(u_int *)addr; 945185029Spjd break; 946185029Spjd 947185029Spjd case BIOCVERSION: 948185029Spjd { 949185029Spjd struct bpf_version *bv = (struct bpf_version *)addr; 950185029Spjd 951185029Spjd bv->bv_major = BPF_MAJOR_VERSION; 952185029Spjd bv->bv_minor = BPF_MINOR_VERSION; 953185029Spjd break; 954185029Spjd } 955185029Spjd 956185029Spjd /* 957185029Spjd * Get "header already complete" flag 958185029Spjd */ 959185029Spjd case BIOCGHDRCMPLT: 960185029Spjd *(u_int *)addr = d->bd_hdrcmplt; 961185029Spjd break; 962185029Spjd 963185029Spjd /* 964185029Spjd * Set "header already complete" flag 965185029Spjd */ 966185029Spjd case BIOCSHDRCMPLT: 967185029Spjd d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 968185029Spjd break; 969185029Spjd 970185029Spjd /* 971185029Spjd * Get packet direction flag 972185029Spjd */ 973185029Spjd case BIOCGDIRECTION: 974185029Spjd *(u_int *)addr = d->bd_direction; 975185029Spjd break; 976185029Spjd 977185029Spjd /* 978185029Spjd * Set packet direction flag 979185029Spjd */ 980185029Spjd case BIOCSDIRECTION: 981185029Spjd { 982185029Spjd u_int direction; 983185029Spjd 984185029Spjd direction = *(u_int *)addr; 985185029Spjd switch (direction) { 986185029Spjd case BPF_D_IN: 987185029Spjd case BPF_D_INOUT: 988185029Spjd case BPF_D_OUT: 989185029Spjd d->bd_direction = direction; 990185029Spjd break; 991185029Spjd default: 992185029Spjd error = EINVAL; 993185029Spjd } 994185029Spjd } 995185029Spjd break; 996185029Spjd 997185029Spjd case BIOCFEEDBACK: 998185029Spjd d->bd_feedback = *(u_int *)addr; 999185029Spjd break; 1000185029Spjd 1001185029Spjd case BIOCLOCK: 1002185029Spjd d->bd_locked = 1; 1003185029Spjd break; 1004185029Spjd 1005185029Spjd case FIONBIO: /* Non-blocking I/O */ 1006185029Spjd break; 1007185029Spjd 1008235329Savg case FIOASYNC: /* Send signal on receive packets */ 1009235329Savg d->bd_async = *(int *)addr; 1010235329Savg break; 1011241293Savg 1012296963Sallanjude case FIOSETOWN: 1013329099Skevans error = fsetown(*(int *)addr, &d->bd_sigio); 1014344399Skevans break; 1015296963Sallanjude 1016241293Savg case FIOGETOWN: 1017241293Savg *(int *)addr = fgetown(&d->bd_sigio); 1018241293Savg break; 1019241293Savg 1020344399Skevans /* This is deprecated, FIOSETOWN should be used instead. */ 1021344399Skevans case TIOCSPGRP: 1022344399Skevans error = fsetown(-(*(int *)addr), &d->bd_sigio); 1023344399Skevans break; 1024344399Skevans 1025185029Spjd /* This is deprecated, FIOGETOWN should be used instead. */ 1026185029Spjd case TIOCGPGRP: 1027235329Savg *(int *)addr = -fgetown(&d->bd_sigio); 1028185029Spjd break; 1029185029Spjd 1030235329Savg case BIOCSRSIG: /* Set receive signal */ 1031235329Savg { 1032185029Spjd u_int sig; 1033185029Spjd 1034185029Spjd sig = *(u_int *)addr; 1035241288Savg 1036241288Savg if (sig >= NSIG) 1037241288Savg error = EINVAL; 1038241288Savg else 1039241288Savg d->bd_sig = sig; 1040241288Savg break; 1041241288Savg } 1042241288Savg case BIOCGRSIG: 1043241288Savg *(u_int *)addr = d->bd_sig; 1044241288Savg break; 1045241288Savg } 1046241288Savg return (error); 1047241288Savg} 1048241288Savg 1049241288Savg/* 1050241288Savg * Set d's packet filter program to fp. If this file already has a filter, 1051241288Savg * free it and replace it. Returns EINVAL for bogus requests. 1052241288Savg */ 1053241288Savgstatic int 1054241288Savgbpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd) 1055241288Savg{ 1056241288Savg struct bpf_insn *fcode, *old; 1057241288Savg u_int wfilter, flen, size; 1058241288Savg#ifdef BPF_JITTER 1059241288Savg bpf_jit_filter *ofunc; 1060241288Savg#endif 1061241288Savg 1062241288Savg if (cmd == BIOCSETWF) { 1063241288Savg old = d->bd_wfilter; 1064241288Savg wfilter = 1; 1065241288Savg#ifdef BPF_JITTER 1066241288Savg ofunc = NULL; 1067241288Savg#endif 1068241288Savg } else { 1069241288Savg wfilter = 0; 1070329099Skevans old = d->bd_rfilter; 1071185029Spjd#ifdef BPF_JITTER 1072185029Spjd ofunc = d->bd_bfilter; 1073185029Spjd#endif 1074185029Spjd } 1075185029Spjd if (fp->bf_insns == NULL) { 1076185029Spjd if (fp->bf_len != 0) 1077185029Spjd return (EINVAL); 1078185029Spjd BPFD_LOCK(d); 1079185029Spjd if (wfilter) 1080185029Spjd d->bd_wfilter = NULL; 1081185029Spjd else { 1082185029Spjd d->bd_rfilter = NULL; 1083185029Spjd#ifdef BPF_JITTER 1084185029Spjd d->bd_bfilter = NULL; 1085185029Spjd#endif 1086185029Spjd } 1087185029Spjd reset_d(d); 1088185029Spjd BPFD_UNLOCK(d); 1089185029Spjd if (old != NULL) 1090185029Spjd free((caddr_t)old, M_BPF); 1091185029Spjd#ifdef BPF_JITTER 1092185029Spjd if (ofunc != NULL) 1093185029Spjd bpf_destroy_jit_filter(ofunc); 1094185029Spjd#endif 1095185029Spjd return (0); 1096185029Spjd } 1097185029Spjd flen = fp->bf_len; 1098185029Spjd if (flen > bpf_maxinsns) 1099185029Spjd return (EINVAL); 1100185029Spjd 1101185029Spjd size = flen * sizeof(*fp->bf_insns); 1102185029Spjd fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK); 1103185029Spjd if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 1104185029Spjd bpf_validate(fcode, (int)flen)) { 1105185029Spjd BPFD_LOCK(d); 1106185029Spjd if (wfilter) 1107185029Spjd d->bd_wfilter = fcode; 1108185029Spjd else { 1109185029Spjd d->bd_rfilter = fcode; 1110185029Spjd#ifdef BPF_JITTER 1111185029Spjd d->bd_bfilter = bpf_jitter(fcode, flen); 1112241301Savg#endif 1113241301Savg } 1114241301Savg reset_d(d); 1115241301Savg BPFD_UNLOCK(d); 1116185029Spjd if (old != NULL) 1117185029Spjd free((caddr_t)old, M_BPF); 1118185029Spjd#ifdef BPF_JITTER 1119235329Savg if (ofunc != NULL) 1120185029Spjd bpf_destroy_jit_filter(ofunc); 1121185029Spjd#endif 1122185029Spjd 1123185029Spjd return (0); 1124185029Spjd } 1125185029Spjd free((caddr_t)fcode, M_BPF); 1126185029Spjd return (EINVAL); 1127185029Spjd} 1128185029Spjd 1129185029Spjd/* 1130185029Spjd * Detach a file from its current interface (if attached at all) and attach 1131185029Spjd * to the interface indicated by the name stored in ifr. 1132185029Spjd * Return an errno or 0. 1133185029Spjd */ 1134185029Spjdstatic int 1135185029Spjdbpf_setif(struct bpf_d *d, struct ifreq *ifr) 1136241288Savg{ 1137241288Savg struct bpf_if *bp; 1138241288Savg struct ifnet *theywant; 1139241288Savg 1140241288Savg theywant = ifunit(ifr->ifr_name); 1141241288Savg if (theywant == NULL || theywant->if_bpf == NULL) 1142185029Spjd return (ENXIO); 1143185029Spjd 1144241288Savg bp = theywant->if_bpf; 1145185029Spjd /* 1146241288Savg * Allocate the packet buffers if we need to. 1147241288Savg * If we're already attached to requested interface, 1148185029Spjd * just flush the buffer. 1149241288Savg */ 1150185029Spjd if (d->bd_sbuf == NULL) 1151185029Spjd bpf_allocbufs(d); 1152185029Spjd if (bp != d->bd_bif) { 1153185029Spjd if (d->bd_bif) 1154185029Spjd /* 1155185029Spjd * Detach if attached to something else. 1156185029Spjd */ 1157185029Spjd bpf_detachd(d); 1158185029Spjd 1159185029Spjd bpf_attachd(d, bp); 1160185029Spjd } 1161 BPFD_LOCK(d); 1162 reset_d(d); 1163 BPFD_UNLOCK(d); 1164 return (0); 1165} 1166 1167/* 1168 * Support for select() and poll() system calls 1169 * 1170 * Return true iff the specific operation will not block indefinitely. 1171 * Otherwise, return false but make a note that a selwakeup() must be done. 1172 */ 1173static int 1174bpfpoll(struct cdev *dev, int events, struct thread *td) 1175{ 1176 struct bpf_d *d; 1177 int revents; 1178 1179 d = dev->si_drv1; 1180 if (d->bd_bif == NULL) 1181 return (ENXIO); 1182 1183 /* 1184 * Refresh PID associated with this descriptor. 1185 */ 1186 revents = events & (POLLOUT | POLLWRNORM); 1187 BPFD_LOCK(d); 1188 d->bd_pid = td->td_proc->p_pid; 1189 if (events & (POLLIN | POLLRDNORM)) { 1190 if (bpf_ready(d)) 1191 revents |= events & (POLLIN | POLLRDNORM); 1192 else { 1193 selrecord(td, &d->bd_sel); 1194 /* Start the read timeout if necessary. */ 1195 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1196 callout_reset(&d->bd_callout, d->bd_rtout, 1197 bpf_timed_out, d); 1198 d->bd_state = BPF_WAITING; 1199 } 1200 } 1201 } 1202 BPFD_UNLOCK(d); 1203 return (revents); 1204} 1205 1206/* 1207 * Support for kevent() system call. Register EVFILT_READ filters and 1208 * reject all others. 1209 */ 1210int 1211bpfkqfilter(struct cdev *dev, struct knote *kn) 1212{ 1213 struct bpf_d *d = (struct bpf_d *)dev->si_drv1; 1214 1215 if (kn->kn_filter != EVFILT_READ) 1216 return (1); 1217 1218 /* 1219 * Refresh PID associated with this descriptor. 1220 */ 1221 BPFD_LOCK(d); 1222 d->bd_pid = curthread->td_proc->p_pid; 1223 kn->kn_fop = &bpfread_filtops; 1224 kn->kn_hook = d; 1225 knlist_add(&d->bd_sel.si_note, kn, 1); 1226 BPFD_UNLOCK(d); 1227 1228 return (0); 1229} 1230 1231static void 1232filt_bpfdetach(struct knote *kn) 1233{ 1234 struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1235 1236 knlist_remove(&d->bd_sel.si_note, kn, 0); 1237} 1238 1239static int 1240filt_bpfread(struct knote *kn, long hint) 1241{ 1242 struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1243 int ready; 1244 1245 BPFD_LOCK_ASSERT(d); 1246 ready = bpf_ready(d); 1247 if (ready) { 1248 kn->kn_data = d->bd_slen; 1249 if (d->bd_hbuf) 1250 kn->kn_data += d->bd_hlen; 1251 } 1252 else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1253 callout_reset(&d->bd_callout, d->bd_rtout, 1254 bpf_timed_out, d); 1255 d->bd_state = BPF_WAITING; 1256 } 1257 1258 return (ready); 1259} 1260 1261/* 1262 * Incoming linkage from device drivers. Process the packet pkt, of length 1263 * pktlen, which is stored in a contiguous buffer. The packet is parsed 1264 * by each process' filter, and if accepted, stashed into the corresponding 1265 * buffer. 1266 */ 1267void 1268bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1269{ 1270 struct bpf_d *d; 1271 u_int slen; 1272 int gottime; 1273 struct timeval tv; 1274 1275 gottime = 0; 1276 BPFIF_LOCK(bp); 1277 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1278 BPFD_LOCK(d); 1279 ++d->bd_rcount; 1280#ifdef BPF_JITTER 1281 if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL) 1282 slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen); 1283 else 1284#endif 1285 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen); 1286 if (slen != 0) { 1287 d->bd_fcount++; 1288 if (!gottime) { 1289 microtime(&tv); 1290 gottime = 1; 1291 } 1292#ifdef MAC 1293 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1294#endif 1295 catchpacket(d, pkt, pktlen, slen, bcopy, &tv); 1296 } 1297 BPFD_UNLOCK(d); 1298 } 1299 BPFIF_UNLOCK(bp); 1300} 1301 1302/* 1303 * Copy data from an mbuf chain into a buffer. This code is derived 1304 * from m_copydata in sys/uipc_mbuf.c. 1305 */ 1306static void 1307bpf_mcopy(const void *src_arg, void *dst_arg, size_t len) 1308{ 1309 const struct mbuf *m; 1310 u_int count; 1311 u_char *dst; 1312 1313 m = src_arg; 1314 dst = dst_arg; 1315 while (len > 0) { 1316 if (m == NULL) 1317 panic("bpf_mcopy"); 1318 count = min(m->m_len, len); 1319 bcopy(mtod(m, void *), dst, count); 1320 m = m->m_next; 1321 dst += count; 1322 len -= count; 1323 } 1324} 1325 1326#define BPF_CHECK_DIRECTION(d, m) \ 1327 if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \ 1328 ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL)) 1329 1330/* 1331 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1332 */ 1333void 1334bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1335{ 1336 struct bpf_d *d; 1337 u_int pktlen, slen; 1338 int gottime; 1339 struct timeval tv; 1340 1341 if (m->m_flags & M_SKIP_BPF) { 1342 m->m_flags &= ~M_SKIP_BPF; 1343 return; 1344 } 1345 1346 gottime = 0; 1347 1348 pktlen = m_length(m, NULL); 1349 1350 BPFIF_LOCK(bp); 1351 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1352 BPF_CHECK_DIRECTION(d, m) 1353 continue; 1354 BPFD_LOCK(d); 1355 ++d->bd_rcount; 1356#ifdef BPF_JITTER 1357 /* XXX We cannot handle multiple mbufs. */ 1358 if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL && 1359 m->m_next == NULL) 1360 slen = (*(d->bd_bfilter->func))(mtod(m, u_char *), 1361 pktlen, pktlen); 1362 else 1363#endif 1364 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0); 1365 if (slen != 0) { 1366 d->bd_fcount++; 1367 if (!gottime) { 1368 microtime(&tv); 1369 gottime = 1; 1370 } 1371#ifdef MAC 1372 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1373#endif 1374 catchpacket(d, (u_char *)m, pktlen, slen, 1375 bpf_mcopy, &tv); 1376 } 1377 BPFD_UNLOCK(d); 1378 } 1379 BPFIF_UNLOCK(bp); 1380} 1381 1382/* 1383 * Incoming linkage from device drivers, when packet is in 1384 * an mbuf chain and to be prepended by a contiguous header. 1385 */ 1386void 1387bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m) 1388{ 1389 struct mbuf mb; 1390 struct bpf_d *d; 1391 u_int pktlen, slen; 1392 int gottime; 1393 struct timeval tv; 1394 1395 if (m->m_flags & M_SKIP_BPF) { 1396 m->m_flags &= ~M_SKIP_BPF; 1397 return; 1398 } 1399 1400 gottime = 0; 1401 1402 pktlen = m_length(m, NULL); 1403 /* 1404 * Craft on-stack mbuf suitable for passing to bpf_filter. 1405 * Note that we cut corners here; we only setup what's 1406 * absolutely needed--this mbuf should never go anywhere else. 1407 */ 1408 mb.m_next = m; 1409 mb.m_data = data; 1410 mb.m_len = dlen; 1411 pktlen += dlen; 1412 1413 BPFIF_LOCK(bp); 1414 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1415 BPF_CHECK_DIRECTION(d, m) 1416 continue; 1417 BPFD_LOCK(d); 1418 ++d->bd_rcount; 1419 slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0); 1420 if (slen != 0) { 1421 d->bd_fcount++; 1422 if (!gottime) { 1423 microtime(&tv); 1424 gottime = 1; 1425 } 1426#ifdef MAC 1427 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1428#endif 1429 catchpacket(d, (u_char *)&mb, pktlen, slen, 1430 bpf_mcopy, &tv); 1431 } 1432 BPFD_UNLOCK(d); 1433 } 1434 BPFIF_UNLOCK(bp); 1435} 1436 1437#undef BPF_CHECK_DIRECTION 1438 1439/* 1440 * Move the packet data from interface memory (pkt) into the 1441 * store buffer. "cpfn" is the routine called to do the actual data 1442 * transfer. bcopy is passed in to copy contiguous chunks, while 1443 * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 1444 * pkt is really an mbuf. 1445 */ 1446static void 1447catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, 1448 void (*cpfn)(const void *, void *, size_t), struct timeval *tv) 1449{ 1450 struct bpf_hdr *hp; 1451 int totlen, curlen; 1452 int hdrlen = d->bd_bif->bif_hdrlen; 1453 int do_wakeup = 0; 1454 1455 BPFD_LOCK_ASSERT(d); 1456 /* 1457 * Figure out how many bytes to move. If the packet is 1458 * greater or equal to the snapshot length, transfer that 1459 * much. Otherwise, transfer the whole packet (unless 1460 * we hit the buffer size limit). 1461 */ 1462 totlen = hdrlen + min(snaplen, pktlen); 1463 if (totlen > d->bd_bufsize) 1464 totlen = d->bd_bufsize; 1465 1466 /* 1467 * Round up the end of the previous packet to the next longword. 1468 */ 1469 curlen = BPF_WORDALIGN(d->bd_slen); 1470 if (curlen + totlen > d->bd_bufsize) { 1471 /* 1472 * This packet will overflow the storage buffer. 1473 * Rotate the buffers if we can, then wakeup any 1474 * pending reads. 1475 */ 1476 if (d->bd_fbuf == NULL) { 1477 /* 1478 * We haven't completed the previous read yet, 1479 * so drop the packet. 1480 */ 1481 ++d->bd_dcount; 1482 return; 1483 } 1484 ROTATE_BUFFERS(d); 1485 do_wakeup = 1; 1486 curlen = 0; 1487 } 1488 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) 1489 /* 1490 * Immediate mode is set, or the read timeout has 1491 * already expired during a select call. A packet 1492 * arrived, so the reader should be woken up. 1493 */ 1494 do_wakeup = 1; 1495 1496 /* 1497 * Append the bpf header. 1498 */ 1499 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1500 hp->bh_tstamp = *tv; 1501 hp->bh_datalen = pktlen; 1502 hp->bh_hdrlen = hdrlen; 1503 /* 1504 * Copy the packet data into the store buffer and update its length. 1505 */ 1506 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1507 d->bd_slen = curlen + totlen; 1508 1509 if (do_wakeup) 1510 bpf_wakeup(d); 1511} 1512 1513/* 1514 * Initialize all nonzero fields of a descriptor. 1515 */ 1516static void 1517bpf_allocbufs(struct bpf_d *d) 1518{ 1519 1520 KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL")); 1521 KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL")); 1522 KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL")); 1523 1524 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1525 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1526 d->bd_slen = 0; 1527 d->bd_hlen = 0; 1528} 1529 1530/* 1531 * Free buffers currently in use by a descriptor. 1532 * Called on close. 1533 */ 1534static void 1535bpf_freed(struct bpf_d *d) 1536{ 1537 /* 1538 * We don't need to lock out interrupts since this descriptor has 1539 * been detached from its interface and it yet hasn't been marked 1540 * free. 1541 */ 1542 if (d->bd_sbuf != NULL) { 1543 free(d->bd_sbuf, M_BPF); 1544 if (d->bd_hbuf != NULL) 1545 free(d->bd_hbuf, M_BPF); 1546 if (d->bd_fbuf != NULL) 1547 free(d->bd_fbuf, M_BPF); 1548 } 1549 if (d->bd_rfilter) { 1550 free((caddr_t)d->bd_rfilter, M_BPF); 1551#ifdef BPF_JITTER 1552 bpf_destroy_jit_filter(d->bd_bfilter); 1553#endif 1554 } 1555 if (d->bd_wfilter) 1556 free((caddr_t)d->bd_wfilter, M_BPF); 1557 mtx_destroy(&d->bd_mtx); 1558} 1559 1560/* 1561 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the 1562 * fixed size of the link header (variable length headers not yet supported). 1563 */ 1564void 1565bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1566{ 1567 1568 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 1569} 1570 1571/* 1572 * Attach an interface to bpf. ifp is a pointer to the structure 1573 * defining the interface to be attached, dlt is the link layer type, 1574 * and hdrlen is the fixed size of the link header (variable length 1575 * headers are not yet supporrted). 1576 */ 1577void 1578bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1579{ 1580 struct bpf_if *bp; 1581 1582 bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 1583 if (bp == NULL) 1584 panic("bpfattach"); 1585 1586 LIST_INIT(&bp->bif_dlist); 1587 bp->bif_ifp = ifp; 1588 bp->bif_dlt = dlt; 1589 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF); 1590 KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized")); 1591 *driverp = bp; 1592 1593 mtx_lock(&bpf_mtx); 1594 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next); 1595 mtx_unlock(&bpf_mtx); 1596 1597 /* 1598 * Compute the length of the bpf header. This is not necessarily 1599 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1600 * that the network layer header begins on a longword boundary (for 1601 * performance reasons and to alleviate alignment restrictions). 1602 */ 1603 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1604 1605 if (bootverbose) 1606 if_printf(ifp, "bpf attached\n"); 1607} 1608 1609/* 1610 * Detach bpf from an interface. This involves detaching each descriptor 1611 * associated with the interface, and leaving bd_bif NULL. Notify each 1612 * descriptor as it's detached so that any sleepers wake up and get 1613 * ENXIO. 1614 */ 1615void 1616bpfdetach(struct ifnet *ifp) 1617{ 1618 struct bpf_if *bp; 1619 struct bpf_d *d; 1620 1621 /* Locate BPF interface information */ 1622 mtx_lock(&bpf_mtx); 1623 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1624 if (ifp == bp->bif_ifp) 1625 break; 1626 } 1627 1628 /* Interface wasn't attached */ 1629 if ((bp == NULL) || (bp->bif_ifp == NULL)) { 1630 mtx_unlock(&bpf_mtx); 1631 printf("bpfdetach: %s was not attached\n", ifp->if_xname); 1632 return; 1633 } 1634 1635 LIST_REMOVE(bp, bif_next); 1636 mtx_unlock(&bpf_mtx); 1637 1638 while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) { 1639 bpf_detachd(d); 1640 BPFD_LOCK(d); 1641 bpf_wakeup(d); 1642 BPFD_UNLOCK(d); 1643 } 1644 1645 mtx_destroy(&bp->bif_mtx); 1646 free(bp, M_BPF); 1647} 1648 1649/* 1650 * Get a list of available data link type of the interface. 1651 */ 1652static int 1653bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 1654{ 1655 int n, error; 1656 struct ifnet *ifp; 1657 struct bpf_if *bp; 1658 1659 ifp = d->bd_bif->bif_ifp; 1660 n = 0; 1661 error = 0; 1662 mtx_lock(&bpf_mtx); 1663 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1664 if (bp->bif_ifp != ifp) 1665 continue; 1666 if (bfl->bfl_list != NULL) { 1667 if (n >= bfl->bfl_len) { 1668 mtx_unlock(&bpf_mtx); 1669 return (ENOMEM); 1670 } 1671 error = copyout(&bp->bif_dlt, 1672 bfl->bfl_list + n, sizeof(u_int)); 1673 } 1674 n++; 1675 } 1676 mtx_unlock(&bpf_mtx); 1677 bfl->bfl_len = n; 1678 return (error); 1679} 1680 1681/* 1682 * Set the data link type of a BPF instance. 1683 */ 1684static int 1685bpf_setdlt(struct bpf_d *d, u_int dlt) 1686{ 1687 int error, opromisc; 1688 struct ifnet *ifp; 1689 struct bpf_if *bp; 1690 1691 if (d->bd_bif->bif_dlt == dlt) 1692 return (0); 1693 ifp = d->bd_bif->bif_ifp; 1694 mtx_lock(&bpf_mtx); 1695 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1696 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 1697 break; 1698 } 1699 mtx_unlock(&bpf_mtx); 1700 if (bp != NULL) { 1701 opromisc = d->bd_promisc; 1702 bpf_detachd(d); 1703 bpf_attachd(d, bp); 1704 BPFD_LOCK(d); 1705 reset_d(d); 1706 BPFD_UNLOCK(d); 1707 if (opromisc) { 1708 error = ifpromisc(bp->bif_ifp, 1); 1709 if (error) 1710 if_printf(bp->bif_ifp, 1711 "bpf_setdlt: ifpromisc failed (%d)\n", 1712 error); 1713 else 1714 d->bd_promisc = 1; 1715 } 1716 } 1717 return (bp == NULL ? EINVAL : 0); 1718} 1719 1720static void 1721bpf_clone(void *arg, struct ucred *cred, char *name, int namelen, 1722 struct cdev **dev) 1723{ 1724 int u; 1725 1726 if (*dev != NULL) 1727 return; 1728 if (dev_stdclone(name, NULL, "bpf", &u) != 1) 1729 return; 1730 *dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, 1731 "bpf%d", u); 1732 dev_ref(*dev); 1733 (*dev)->si_flags |= SI_CHEAPCLONE; 1734 return; 1735} 1736 1737static void 1738bpf_drvinit(void *unused) 1739{ 1740 1741 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF); 1742 LIST_INIT(&bpf_iflist); 1743 EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000); 1744} 1745 1746static void 1747bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd) 1748{ 1749 1750 bzero(d, sizeof(*d)); 1751 BPFD_LOCK_ASSERT(bd); 1752 d->bd_immediate = bd->bd_immediate; 1753 d->bd_promisc = bd->bd_promisc; 1754 d->bd_hdrcmplt = bd->bd_hdrcmplt; 1755 d->bd_direction = bd->bd_direction; 1756 d->bd_feedback = bd->bd_feedback; 1757 d->bd_async = bd->bd_async; 1758 d->bd_rcount = bd->bd_rcount; 1759 d->bd_dcount = bd->bd_dcount; 1760 d->bd_fcount = bd->bd_fcount; 1761 d->bd_sig = bd->bd_sig; 1762 d->bd_slen = bd->bd_slen; 1763 d->bd_hlen = bd->bd_hlen; 1764 d->bd_bufsize = bd->bd_bufsize; 1765 d->bd_pid = bd->bd_pid; 1766 strlcpy(d->bd_ifname, 1767 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ); 1768 d->bd_locked = bd->bd_locked; 1769} 1770 1771static int 1772bpf_stats_sysctl(SYSCTL_HANDLER_ARGS) 1773{ 1774 struct xbpf_d *xbdbuf, *xbd; 1775 int index, error; 1776 struct bpf_if *bp; 1777 struct bpf_d *bd; 1778 1779 /* 1780 * XXX This is not technically correct. It is possible for non 1781 * privileged users to open bpf devices. It would make sense 1782 * if the users who opened the devices were able to retrieve 1783 * the statistics for them, too. 1784 */ 1785 error = priv_check(req->td, PRIV_NET_BPF); 1786 if (error) 1787 return (error); 1788 if (req->oldptr == NULL) 1789 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd))); 1790 if (bpf_bpfd_cnt == 0) 1791 return (SYSCTL_OUT(req, 0, 0)); 1792 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK); 1793 mtx_lock(&bpf_mtx); 1794 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) { 1795 mtx_unlock(&bpf_mtx); 1796 free(xbdbuf, M_BPF); 1797 return (ENOMEM); 1798 } 1799 index = 0; 1800 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1801 BPFIF_LOCK(bp); 1802 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { 1803 xbd = &xbdbuf[index++]; 1804 BPFD_LOCK(bd); 1805 bpfstats_fill_xbpf(xbd, bd); 1806 BPFD_UNLOCK(bd); 1807 } 1808 BPFIF_UNLOCK(bp); 1809 } 1810 mtx_unlock(&bpf_mtx); 1811 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd)); 1812 free(xbdbuf, M_BPF); 1813 return (error); 1814} 1815 1816SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL) 1817 1818#else /* !DEV_BPF && !NETGRAPH_BPF */ 1819/* 1820 * NOP stubs to allow bpf-using drivers to load and function. 1821 * 1822 * A 'better' implementation would allow the core bpf functionality 1823 * to be loaded at runtime. 1824 */ 1825static struct bpf_if bp_null; 1826 1827void 1828bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1829{ 1830} 1831 1832void 1833bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1834{ 1835} 1836 1837void 1838bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m) 1839{ 1840} 1841 1842void 1843bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1844{ 1845 1846 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 1847} 1848 1849void 1850bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1851{ 1852 1853 *driverp = &bp_null; 1854} 1855 1856void 1857bpfdetach(struct ifnet *ifp) 1858{ 1859} 1860 1861u_int 1862bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen) 1863{ 1864 return -1; /* "no filter" behaviour */ 1865} 1866 1867int 1868bpf_validate(const struct bpf_insn *f, int len) 1869{ 1870 return 0; /* false */ 1871} 1872 1873#endif /* !DEV_BPF && !NETGRAPH_BPF */ 1874