bpf.c revision 167035
1107120Sjulian/*- 2107120Sjulian * Copyright (c) 1990, 1991, 1993 3139823Simp * The Regents of the University of California. All rights reserved. 4139823Simp * 5139823Simp * This code is derived from the Stanford/CMU enet packet filter, 6107120Sjulian * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7107120Sjulian * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8107120Sjulian * Berkeley Laboratory. 9107120Sjulian * 10107120Sjulian * Redistribution and use in source and binary forms, with or without 11107120Sjulian * modification, are permitted provided that the following conditions 12107120Sjulian * are met: 13107120Sjulian * 1. Redistributions of source code must retain the above copyright 14107120Sjulian * notice, this list of conditions and the following disclaimer. 15107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright 16107120Sjulian * notice, this list of conditions and the following disclaimer in the 17107120Sjulian * documentation and/or other materials provided with the distribution. 18107120Sjulian * 4. Neither the name of the University nor the names of its contributors 19107120Sjulian * may be used to endorse or promote products derived from this software 20107120Sjulian * without specific prior written permission. 21107120Sjulian * 22107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25107120Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30114878Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31107120Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32107120Sjulian * SUCH DAMAGE. 33107120Sjulian * 34107120Sjulian * @(#)bpf.c 8.4 (Berkeley) 1/9/95 35107120Sjulian * 36107120Sjulian * $FreeBSD: head/sys/net/bpf.c 167035 2007-02-26 22:24:14Z jkim $ 37107120Sjulian */ 38107120Sjulian 39107120Sjulian#include "opt_bpf.h" 40107120Sjulian#include "opt_mac.h" 41107120Sjulian#include "opt_netgraph.h" 42107120Sjulian 43107120Sjulian#include <sys/types.h> 44107120Sjulian#include <sys/param.h> 45107120Sjulian#include <sys/systm.h> 46107120Sjulian#include <sys/conf.h> 47107120Sjulian#include <sys/fcntl.h> 48122634Semax#include <sys/malloc.h> 49107120Sjulian#include <sys/mbuf.h> 50107120Sjulian#include <sys/time.h> 51107120Sjulian#include <sys/priv.h> 52107120Sjulian#include <sys/proc.h> 53107120Sjulian#include <sys/signalvar.h> 54107120Sjulian#include <sys/filio.h> 55107120Sjulian#include <sys/sockio.h> 56107120Sjulian#include <sys/ttycom.h> 57107120Sjulian#include <sys/uio.h> 58107120Sjulian 59107120Sjulian#include <sys/event.h> 60107120Sjulian#include <sys/file.h> 61107120Sjulian#include <sys/poll.h> 62107120Sjulian#include <sys/proc.h> 63107120Sjulian 64107120Sjulian#include <sys/socket.h> 65107120Sjulian 66107120Sjulian#include <net/if.h> 67107120Sjulian#include <net/bpf.h> 68107120Sjulian#ifdef BPF_JITTER 69107120Sjulian#include <net/bpf_jitter.h> 70107120Sjulian#endif 71107120Sjulian#include <net/bpfdesc.h> 72107120Sjulian 73107120Sjulian#include <netinet/in.h> 74107120Sjulian#include <netinet/if_ether.h> 75107120Sjulian#include <sys/kernel.h> 76107120Sjulian#include <sys/sysctl.h> 77107120Sjulian 78107120Sjulian#include <net80211/ieee80211_freebsd.h> 79107120Sjulian 80107120Sjulian#include <security/mac/mac_framework.h> 81107120Sjulian 82107120Sjulianstatic MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); 83107120Sjulian 84107120Sjulian#if defined(DEV_BPF) || defined(NETGRAPH_BPF) 85107120Sjulian 86107120Sjulian#define PRINET 26 /* interruptible */ 87107120Sjulian 88107120Sjulian#define M_SKIP_BPF M_SKIP_FIREWALL 89107120Sjulian 90107120Sjulian/* 91107120Sjulian * bpf_iflist is a list of BPF interface structures, each corresponding to a 92107120Sjulian * specific DLT. The same network interface might have several BPF interface 93107120Sjulian * structures registered by different layers in the stack (i.e., 802.11 94107120Sjulian * frames, ethernet frames, etc). 95107120Sjulian */ 96107120Sjulianstatic LIST_HEAD(, bpf_if) bpf_iflist; 97107120Sjulianstatic struct mtx bpf_mtx; /* bpf global lock */ 98107120Sjulianstatic int bpf_bpfd_cnt; 99107120Sjulian 100107120Sjulianstatic void bpf_allocbufs(struct bpf_d *); 101107120Sjulianstatic void bpf_attachd(struct bpf_d *, struct bpf_if *); 102107120Sjulianstatic void bpf_detachd(struct bpf_d *); 103107120Sjulianstatic void bpf_freed(struct bpf_d *); 104107120Sjulianstatic void bpf_mcopy(const void *, void *, size_t); 105107120Sjulianstatic int bpf_movein(struct uio *, int, int, struct mbuf **, 106107120Sjulian struct sockaddr *, int *, struct bpf_insn *); 107107120Sjulianstatic int bpf_setif(struct bpf_d *, struct ifreq *); 108107120Sjulianstatic void bpf_timed_out(void *); 109107120Sjulianstatic __inline void 110107120Sjulian bpf_wakeup(struct bpf_d *); 111107120Sjulianstatic void catchpacket(struct bpf_d *, u_char *, u_int, 112107120Sjulian u_int, void (*)(const void *, void *, size_t), 113107120Sjulian struct timeval *); 114107120Sjulianstatic void reset_d(struct bpf_d *); 115107120Sjulianstatic int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd); 116107120Sjulianstatic int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 117107120Sjulianstatic int bpf_setdlt(struct bpf_d *, u_int); 118107120Sjulianstatic void filt_bpfdetach(struct knote *); 119107120Sjulianstatic int filt_bpfread(struct knote *, long); 120107120Sjulianstatic void bpf_drvinit(void *); 121107120Sjulianstatic void bpf_clone(void *, struct ucred *, char *, int, struct cdev **); 122107120Sjulianstatic int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS); 123107120Sjulian 124107120SjulianSYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl"); 125107120Sjulianstatic int bpf_bufsize = 4096; 126107120SjulianSYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW, 127107120Sjulian &bpf_bufsize, 0, "Default bpf buffer size"); 128107120Sjulianstatic int bpf_maxbufsize = BPF_MAXBUFSIZE; 129107120SjulianSYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW, 130107120Sjulian &bpf_maxbufsize, 0, "Maximum bpf buffer size"); 131107120Sjulianstatic int bpf_maxinsns = BPF_MAXINSNS; 132107120SjulianSYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW, 133107120Sjulian &bpf_maxinsns, 0, "Maximum bpf program instructions"); 134107120SjulianSYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW, 135107120Sjulian bpf_stats_sysctl, "bpf statistics portal"); 136107120Sjulian 137107120Sjulianstatic d_open_t bpfopen; 138107120Sjulianstatic d_close_t bpfclose; 139107120Sjulianstatic d_read_t bpfread; 140107120Sjulianstatic d_write_t bpfwrite; 141107120Sjulianstatic d_ioctl_t bpfioctl; 142107120Sjulianstatic d_poll_t bpfpoll; 143107120Sjulianstatic d_kqfilter_t bpfkqfilter; 144107120Sjulian 145107120Sjulianstatic struct cdevsw bpf_cdevsw = { 146107120Sjulian .d_version = D_VERSION, 147107120Sjulian .d_flags = D_NEEDGIANT, 148107120Sjulian .d_open = bpfopen, 149107120Sjulian .d_close = bpfclose, 150107120Sjulian .d_read = bpfread, 151107120Sjulian .d_write = bpfwrite, 152107120Sjulian .d_ioctl = bpfioctl, 153107120Sjulian .d_poll = bpfpoll, 154107120Sjulian .d_name = "bpf", 155107120Sjulian .d_kqfilter = bpfkqfilter, 156107120Sjulian}; 157107120Sjulian 158107120Sjulianstatic struct filterops bpfread_filtops = 159107120Sjulian { 1, NULL, filt_bpfdetach, filt_bpfread }; 160107120Sjulian 161107120Sjulianstatic int 162107120Sjulianbpf_movein(struct uio *uio, int linktype, int mtu, struct mbuf **mp, 163107120Sjulian struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter) 164107120Sjulian{ 165107120Sjulian const struct ieee80211_bpf_params *p; 166107120Sjulian struct mbuf *m; 167107120Sjulian int error; 168107120Sjulian int len; 169107120Sjulian int hlen; 170107120Sjulian int slen; 171107120Sjulian 172107120Sjulian /* 173107120Sjulian * Build a sockaddr based on the data link layer type. 174107120Sjulian * We do this at this level because the ethernet header 175107120Sjulian * is copied directly into the data field of the sockaddr. 176107120Sjulian * In the case of SLIP, there is no header and the packet 177107120Sjulian * is forwarded as is. 178107120Sjulian * Also, we are careful to leave room at the front of the mbuf 179107120Sjulian * for the link level header. 180107120Sjulian */ 181107120Sjulian switch (linktype) { 182107120Sjulian 183107120Sjulian case DLT_SLIP: 184107120Sjulian sockp->sa_family = AF_INET; 185107120Sjulian hlen = 0; 186107120Sjulian break; 187107120Sjulian 188107120Sjulian case DLT_EN10MB: 189107120Sjulian sockp->sa_family = AF_UNSPEC; 190107120Sjulian /* XXX Would MAXLINKHDR be better? */ 191107120Sjulian hlen = ETHER_HDR_LEN; 192107120Sjulian break; 193107120Sjulian 194107120Sjulian case DLT_FDDI: 195107120Sjulian sockp->sa_family = AF_IMPLINK; 196107120Sjulian hlen = 0; 197107120Sjulian break; 198107120Sjulian 199107120Sjulian case DLT_RAW: 200107120Sjulian sockp->sa_family = AF_UNSPEC; 201107120Sjulian hlen = 0; 202107120Sjulian break; 203107120Sjulian 204107120Sjulian case DLT_NULL: 205107120Sjulian /* 206107120Sjulian * null interface types require a 4 byte pseudo header which 207107120Sjulian * corresponds to the address family of the packet. 208107120Sjulian */ 209107120Sjulian sockp->sa_family = AF_UNSPEC; 210107120Sjulian hlen = 4; 211107120Sjulian break; 212107120Sjulian 213107120Sjulian case DLT_ATM_RFC1483: 214107120Sjulian /* 215107120Sjulian * en atm driver requires 4-byte atm pseudo header. 216107120Sjulian * though it isn't standard, vpi:vci needs to be 217107120Sjulian * specified anyway. 218107120Sjulian */ 219107120Sjulian sockp->sa_family = AF_UNSPEC; 220107120Sjulian hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 221107120Sjulian break; 222107120Sjulian 223107120Sjulian case DLT_PPP: 224107120Sjulian sockp->sa_family = AF_UNSPEC; 225107120Sjulian hlen = 4; /* This should match PPP_HDRLEN */ 226107120Sjulian break; 227107120Sjulian 228107120Sjulian case DLT_IEEE802_11: /* IEEE 802.11 wireless */ 229107120Sjulian sockp->sa_family = AF_IEEE80211; 230107120Sjulian hlen = 0; 231107120Sjulian break; 232107120Sjulian 233107120Sjulian case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */ 234107120Sjulian sockp->sa_family = AF_IEEE80211; 235107120Sjulian sockp->sa_len = 12; /* XXX != 0 */ 236107120Sjulian hlen = sizeof(struct ieee80211_bpf_params); 237107120Sjulian break; 238107120Sjulian 239107120Sjulian default: 240107120Sjulian return (EIO); 241107120Sjulian } 242107120Sjulian 243107120Sjulian len = uio->uio_resid; 244107120Sjulian 245107120Sjulian if (len - hlen > mtu) 246107120Sjulian return (EMSGSIZE); 247107120Sjulian 248107120Sjulian if ((unsigned)len > MCLBYTES) 249107120Sjulian return (EIO); 250107120Sjulian 251107120Sjulian if (len > MHLEN) { 252107120Sjulian m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR); 253107120Sjulian } else { 254107120Sjulian MGETHDR(m, M_TRYWAIT, MT_DATA); 255107120Sjulian } 256107120Sjulian if (m == NULL) 257107120Sjulian return (ENOBUFS); 258107120Sjulian m->m_pkthdr.len = m->m_len = len; 259107120Sjulian m->m_pkthdr.rcvif = NULL; 260107120Sjulian *mp = m; 261107120Sjulian 262107120Sjulian if (m->m_len < hlen) { 263107120Sjulian error = EPERM; 264107120Sjulian goto bad; 265107120Sjulian } 266107120Sjulian 267107120Sjulian error = uiomove(mtod(m, u_char *), len, uio); 268107120Sjulian if (error) 269107120Sjulian goto bad; 270107120Sjulian 271107120Sjulian slen = bpf_filter(wfilter, mtod(m, u_char *), len, len); 272107120Sjulian if (slen == 0) { 273107120Sjulian error = EPERM; 274107120Sjulian goto bad; 275107120Sjulian } 276107120Sjulian 277107120Sjulian /* 278107120Sjulian * Make room for link header, and copy it to sockaddr 279107120Sjulian */ 280107120Sjulian if (hlen != 0) { 281107120Sjulian if (sockp->sa_family == AF_IEEE80211) { 282107120Sjulian /* 283107120Sjulian * Collect true length from the parameter header 284107120Sjulian * NB: sockp is known to be zero'd so if we do a 285107120Sjulian * short copy unspecified parameters will be 286107120Sjulian * zero. 287107120Sjulian * NB: packet may not be aligned after stripping 288107120Sjulian * bpf params 289107120Sjulian * XXX check ibp_vers 290107120Sjulian */ 291107120Sjulian p = mtod(m, const struct ieee80211_bpf_params *); 292107120Sjulian hlen = p->ibp_len; 293107120Sjulian if (hlen > sizeof(sockp->sa_data)) { 294107120Sjulian error = EINVAL; 295107120Sjulian goto bad; 296107120Sjulian } 297107120Sjulian } 298107120Sjulian bcopy(m->m_data, sockp->sa_data, hlen); 299107120Sjulian } 300107120Sjulian *hdrlen = hlen; 301107120Sjulian 302107120Sjulian return (0); 303107120Sjulianbad: 304107120Sjulian m_freem(m); 305107120Sjulian return (error); 306107120Sjulian} 307107120Sjulian 308107120Sjulian/* 309107120Sjulian * Attach file to the bpf interface, i.e. make d listen on bp. 310107120Sjulian */ 311107120Sjulianstatic void 312107120Sjulianbpf_attachd(struct bpf_d *d, struct bpf_if *bp) 313107120Sjulian{ 314107120Sjulian /* 315107120Sjulian * Point d at bp, and add d to the interface's list of listeners. 316107120Sjulian * Finally, point the driver's bpf cookie at the interface so 317107120Sjulian * it will divert packets to bpf. 318107120Sjulian */ 319107120Sjulian BPFIF_LOCK(bp); 320107120Sjulian d->bd_bif = bp; 321107120Sjulian LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 322107120Sjulian 323107120Sjulian bpf_bpfd_cnt++; 324107120Sjulian BPFIF_UNLOCK(bp); 325107120Sjulian} 326107120Sjulian 327107120Sjulian/* 328107120Sjulian * Detach a file from its interface. 329107120Sjulian */ 330107120Sjulianstatic void 331107120Sjulianbpf_detachd(struct bpf_d *d) 332107120Sjulian{ 333107120Sjulian int error; 334107120Sjulian struct bpf_if *bp; 335107120Sjulian struct ifnet *ifp; 336107120Sjulian 337107120Sjulian bp = d->bd_bif; 338107120Sjulian BPFIF_LOCK(bp); 339107120Sjulian BPFD_LOCK(d); 340107120Sjulian ifp = d->bd_bif->bif_ifp; 341107120Sjulian 342107120Sjulian /* 343107120Sjulian * Remove d from the interface's descriptor list. 344107120Sjulian */ 345107120Sjulian LIST_REMOVE(d, bd_next); 346107120Sjulian 347107120Sjulian bpf_bpfd_cnt--; 348107120Sjulian d->bd_bif = NULL; 349107120Sjulian BPFD_UNLOCK(d); 350107120Sjulian BPFIF_UNLOCK(bp); 351107120Sjulian 352107120Sjulian /* 353107120Sjulian * Check if this descriptor had requested promiscuous mode. 354107120Sjulian * If so, turn it off. 355107120Sjulian */ 356107120Sjulian if (d->bd_promisc) { 357107120Sjulian d->bd_promisc = 0; 358107120Sjulian error = ifpromisc(ifp, 0); 359107120Sjulian if (error != 0 && error != ENXIO) { 360107120Sjulian /* 361107120Sjulian * ENXIO can happen if a pccard is unplugged 362107120Sjulian * Something is really wrong if we were able to put 363107120Sjulian * the driver into promiscuous mode, but can't 364107120Sjulian * take it out. 365107120Sjulian */ 366107120Sjulian if_printf(bp->bif_ifp, 367107120Sjulian "bpf_detach: ifpromisc failed (%d)\n", error); 368107120Sjulian } 369107120Sjulian } 370107120Sjulian} 371107120Sjulian 372107120Sjulian/* 373107120Sjulian * Open ethernet device. Returns ENXIO for illegal minor device number, 374107120Sjulian * EBUSY if file is open by another process. 375107120Sjulian */ 376107120Sjulian/* ARGSUSED */ 377107120Sjulianstatic int 378107120Sjulianbpfopen(struct cdev *dev, int flags, int fmt, struct thread *td) 379107120Sjulian{ 380107120Sjulian struct bpf_d *d; 381107120Sjulian 382107120Sjulian mtx_lock(&bpf_mtx); 383107120Sjulian d = dev->si_drv1; 384107120Sjulian /* 385107120Sjulian * Each minor can be opened by only one process. If the requested 386107120Sjulian * minor is in use, return EBUSY. 387107120Sjulian */ 388107120Sjulian if (d != NULL) { 389107120Sjulian mtx_unlock(&bpf_mtx); 390107120Sjulian return (EBUSY); 391107120Sjulian } 392107120Sjulian dev->si_drv1 = (struct bpf_d *)~0; /* mark device in use */ 393107120Sjulian mtx_unlock(&bpf_mtx); 394107120Sjulian 395107120Sjulian if ((dev->si_flags & SI_NAMED) == 0) 396107120Sjulian make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600, 397107120Sjulian "bpf%d", dev2unit(dev)); 398107120Sjulian MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO); 399107120Sjulian dev->si_drv1 = d; 400107120Sjulian d->bd_bufsize = bpf_bufsize; 401107120Sjulian d->bd_sig = SIGIO; 402107120Sjulian d->bd_direction = BPF_D_INOUT; 403107120Sjulian d->bd_pid = td->td_proc->p_pid; 404107120Sjulian#ifdef MAC 405107120Sjulian mac_init_bpfdesc(d); 406107120Sjulian mac_create_bpfdesc(td->td_ucred, d); 407107120Sjulian#endif 408107120Sjulian mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF); 409107120Sjulian callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE); 410107120Sjulian knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL); 411107120Sjulian 412107120Sjulian return (0); 413107120Sjulian} 414107120Sjulian 415107120Sjulian/* 416107120Sjulian * Close the descriptor by detaching it from its interface, 417107120Sjulian * deallocating its buffers, and marking it free. 418107120Sjulian */ 419107120Sjulian/* ARGSUSED */ 420107120Sjulianstatic int 421107120Sjulianbpfclose(struct cdev *dev, int flags, int fmt, struct thread *td) 422107120Sjulian{ 423107120Sjulian struct bpf_d *d = dev->si_drv1; 424107120Sjulian 425107120Sjulian BPFD_LOCK(d); 426107120Sjulian if (d->bd_state == BPF_WAITING) 427107120Sjulian callout_stop(&d->bd_callout); 428107120Sjulian d->bd_state = BPF_IDLE; 429107120Sjulian BPFD_UNLOCK(d); 430107120Sjulian funsetown(&d->bd_sigio); 431107120Sjulian mtx_lock(&bpf_mtx); 432107120Sjulian if (d->bd_bif) 433107120Sjulian bpf_detachd(d); 434107120Sjulian mtx_unlock(&bpf_mtx); 435107120Sjulian selwakeuppri(&d->bd_sel, PRINET); 436107120Sjulian#ifdef MAC 437107120Sjulian mac_destroy_bpfdesc(d); 438107120Sjulian#endif /* MAC */ 439107120Sjulian knlist_destroy(&d->bd_sel.si_note); 440107120Sjulian bpf_freed(d); 441107120Sjulian dev->si_drv1 = NULL; 442107120Sjulian free(d, M_BPF); 443107120Sjulian 444107120Sjulian return (0); 445107120Sjulian} 446107120Sjulian 447107120Sjulian 448107120Sjulian/* 449107120Sjulian * Rotate the packet buffers in descriptor d. Move the store buffer 450107120Sjulian * into the hold slot, and the free buffer into the store slot. 451107120Sjulian * Zero the length of the new store buffer. 452107120Sjulian */ 453107120Sjulian#define ROTATE_BUFFERS(d) \ 454107120Sjulian (d)->bd_hbuf = (d)->bd_sbuf; \ 455107120Sjulian (d)->bd_hlen = (d)->bd_slen; \ 456107120Sjulian (d)->bd_sbuf = (d)->bd_fbuf; \ 457107120Sjulian (d)->bd_slen = 0; \ 458107120Sjulian (d)->bd_fbuf = NULL; 459107120Sjulian/* 460107120Sjulian * bpfread - read next chunk of packets from buffers 461107120Sjulian */ 462107120Sjulianstatic int 463107120Sjulianbpfread(struct cdev *dev, struct uio *uio, int ioflag) 464107120Sjulian{ 465107120Sjulian struct bpf_d *d = dev->si_drv1; 466107120Sjulian int timed_out; 467107120Sjulian int error; 468107120Sjulian 469107120Sjulian /* 470107120Sjulian * Restrict application to use a buffer the same size as 471107120Sjulian * as kernel buffers. 472107120Sjulian */ 473107120Sjulian if (uio->uio_resid != d->bd_bufsize) 474107120Sjulian return (EINVAL); 475107120Sjulian 476107120Sjulian BPFD_LOCK(d); 477107120Sjulian if (d->bd_state == BPF_WAITING) 478107120Sjulian callout_stop(&d->bd_callout); 479107120Sjulian timed_out = (d->bd_state == BPF_TIMED_OUT); 480107120Sjulian d->bd_state = BPF_IDLE; 481107120Sjulian /* 482107120Sjulian * If the hold buffer is empty, then do a timed sleep, which 483107120Sjulian * ends when the timeout expires or when enough packets 484107120Sjulian * have arrived to fill the store buffer. 485107120Sjulian */ 486107120Sjulian while (d->bd_hbuf == NULL) { 487107120Sjulian if ((d->bd_immediate || timed_out) && d->bd_slen != 0) { 488107120Sjulian /* 489107120Sjulian * A packet(s) either arrived since the previous 490107120Sjulian * read or arrived while we were asleep. 491107120Sjulian * Rotate the buffers and return what's here. 492107120Sjulian */ 493107120Sjulian ROTATE_BUFFERS(d); 494107120Sjulian break; 495107120Sjulian } 496107120Sjulian 497107120Sjulian /* 498107120Sjulian * No data is available, check to see if the bpf device 499107120Sjulian * is still pointed at a real interface. If not, return 500107120Sjulian * ENXIO so that the userland process knows to rebind 501107120Sjulian * it before using it again. 502107120Sjulian */ 503107120Sjulian if (d->bd_bif == NULL) { 504107120Sjulian BPFD_UNLOCK(d); 505107120Sjulian return (ENXIO); 506107120Sjulian } 507107120Sjulian 508107120Sjulian if (ioflag & O_NONBLOCK) { 509107120Sjulian BPFD_UNLOCK(d); 510107120Sjulian return (EWOULDBLOCK); 511107120Sjulian } 512107120Sjulian error = msleep(d, &d->bd_mtx, PRINET|PCATCH, 513107120Sjulian "bpf", d->bd_rtout); 514107120Sjulian if (error == EINTR || error == ERESTART) { 515107120Sjulian BPFD_UNLOCK(d); 516107120Sjulian return (error); 517107120Sjulian } 518107120Sjulian if (error == EWOULDBLOCK) { 519107120Sjulian /* 520107120Sjulian * On a timeout, return what's in the buffer, 521107120Sjulian * which may be nothing. If there is something 522107120Sjulian * in the store buffer, we can rotate the buffers. 523107120Sjulian */ 524107120Sjulian if (d->bd_hbuf) 525107120Sjulian /* 526107120Sjulian * We filled up the buffer in between 527107120Sjulian * getting the timeout and arriving 528107120Sjulian * here, so we don't need to rotate. 529107120Sjulian */ 530107120Sjulian break; 531107120Sjulian 532107120Sjulian if (d->bd_slen == 0) { 533107120Sjulian BPFD_UNLOCK(d); 534107120Sjulian return (0); 535107120Sjulian } 536107120Sjulian ROTATE_BUFFERS(d); 537107120Sjulian break; 538107120Sjulian } 539107120Sjulian } 540107120Sjulian /* 541107120Sjulian * At this point, we know we have something in the hold slot. 542107120Sjulian */ 543107120Sjulian BPFD_UNLOCK(d); 544107120Sjulian 545107120Sjulian /* 546107120Sjulian * Move data from hold buffer into user space. 547107120Sjulian * We know the entire buffer is transferred since 548107120Sjulian * we checked above that the read buffer is bpf_bufsize bytes. 549107120Sjulian */ 550107120Sjulian error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 551107120Sjulian 552107120Sjulian BPFD_LOCK(d); 553107120Sjulian d->bd_fbuf = d->bd_hbuf; 554107120Sjulian d->bd_hbuf = NULL; 555107120Sjulian d->bd_hlen = 0; 556107120Sjulian BPFD_UNLOCK(d); 557107120Sjulian 558107120Sjulian return (error); 559107120Sjulian} 560107120Sjulian 561107120Sjulian 562107120Sjulian/* 563107120Sjulian * If there are processes sleeping on this descriptor, wake them up. 564107120Sjulian */ 565107120Sjulianstatic __inline void 566107120Sjulianbpf_wakeup(struct bpf_d *d) 567107120Sjulian{ 568107120Sjulian 569107120Sjulian BPFD_LOCK_ASSERT(d); 570107120Sjulian if (d->bd_state == BPF_WAITING) { 571107120Sjulian callout_stop(&d->bd_callout); 572107120Sjulian d->bd_state = BPF_IDLE; 573107120Sjulian } 574107120Sjulian wakeup(d); 575107120Sjulian if (d->bd_async && d->bd_sig && d->bd_sigio) 576107120Sjulian pgsigio(&d->bd_sigio, d->bd_sig, 0); 577107120Sjulian 578107120Sjulian selwakeuppri(&d->bd_sel, PRINET); 579107120Sjulian KNOTE_LOCKED(&d->bd_sel.si_note, 0); 580107120Sjulian} 581107120Sjulian 582107120Sjulianstatic void 583107120Sjulianbpf_timed_out(void *arg) 584107120Sjulian{ 585107120Sjulian struct bpf_d *d = (struct bpf_d *)arg; 586107120Sjulian 587107120Sjulian BPFD_LOCK(d); 588107120Sjulian if (d->bd_state == BPF_WAITING) { 589107120Sjulian d->bd_state = BPF_TIMED_OUT; 590107120Sjulian if (d->bd_slen != 0) 591107120Sjulian bpf_wakeup(d); 592107120Sjulian } 593107120Sjulian BPFD_UNLOCK(d); 594107120Sjulian} 595107120Sjulian 596107120Sjulianstatic int 597107120Sjulianbpfwrite(struct cdev *dev, struct uio *uio, int ioflag) 598107120Sjulian{ 599107120Sjulian struct bpf_d *d = dev->si_drv1; 600107120Sjulian struct ifnet *ifp; 601107120Sjulian struct mbuf *m, *mc; 602107120Sjulian struct sockaddr dst; 603114878Sjulian int error, hlen; 604114878Sjulian 605114878Sjulian if (d->bd_bif == NULL) 606114878Sjulian return (ENXIO); 607158672Semax 608158672Semax ifp = d->bd_bif->bif_ifp; 609107120Sjulian 610107120Sjulian if ((ifp->if_flags & IFF_UP) == 0) 611107120Sjulian return (ENETDOWN); 612107120Sjulian 613107120Sjulian if (uio->uio_resid == 0) 614107120Sjulian return (0); 615107120Sjulian 616107120Sjulian bzero(&dst, sizeof(dst)); 617107120Sjulian error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, 618107120Sjulian &m, &dst, &hlen, d->bd_wfilter); 619107120Sjulian if (error) 620107120Sjulian return (error); 621107120Sjulian 622107120Sjulian if (d->bd_hdrcmplt) 623107120Sjulian dst.sa_family = pseudo_AF_HDRCMPLT; 624107120Sjulian 625107120Sjulian if (d->bd_feedback) { 626107120Sjulian mc = m_dup(m, M_DONTWAIT); 627107120Sjulian if (mc != NULL) 628107120Sjulian mc->m_pkthdr.rcvif = ifp; 629107120Sjulian /* XXX Do not return the same packet twice. */ 630107120Sjulian if (d->bd_direction == BPF_D_INOUT) 631107120Sjulian m->m_flags |= M_SKIP_BPF; 632107120Sjulian } else 633107120Sjulian mc = NULL; 634107120Sjulian 635107120Sjulian m->m_pkthdr.len -= hlen; 636107120Sjulian m->m_len -= hlen; 637107120Sjulian m->m_data += hlen; /* XXX */ 638107120Sjulian 639107120Sjulian#ifdef MAC 640107120Sjulian BPFD_LOCK(d); 641107120Sjulian mac_create_mbuf_from_bpfdesc(d, m); 642107120Sjulian if (mc != NULL) 643107120Sjulian mac_create_mbuf_from_bpfdesc(d, mc); 644107120Sjulian BPFD_UNLOCK(d); 645107120Sjulian#endif 646107120Sjulian 647107120Sjulian NET_LOCK_GIANT(); 648107120Sjulian error = (*ifp->if_output)(ifp, m, &dst, NULL); 649107120Sjulian NET_UNLOCK_GIANT(); 650107120Sjulian 651107120Sjulian if (mc != NULL) { 652107120Sjulian if (error == 0) { 653107120Sjulian NET_LOCK_GIANT(); 654107120Sjulian (*ifp->if_input)(ifp, mc); 655107120Sjulian NET_UNLOCK_GIANT(); 656107120Sjulian } else 657107120Sjulian m_freem(mc); 658107120Sjulian } 659107120Sjulian 660107120Sjulian return (error); 661107120Sjulian} 662107120Sjulian 663107120Sjulian/* 664107120Sjulian * Reset a descriptor by flushing its packet buffer and clearing the 665107120Sjulian * receive and drop counts. 666107120Sjulian */ 667107120Sjulianstatic void 668107120Sjulianreset_d(struct bpf_d *d) 669107120Sjulian{ 670107120Sjulian 671107120Sjulian mtx_assert(&d->bd_mtx, MA_OWNED); 672107120Sjulian if (d->bd_hbuf) { 673107120Sjulian /* Free the hold buffer. */ 674107120Sjulian d->bd_fbuf = d->bd_hbuf; 675107120Sjulian d->bd_hbuf = NULL; 676107120Sjulian } 677107120Sjulian d->bd_slen = 0; 678107120Sjulian d->bd_hlen = 0; 679107120Sjulian d->bd_rcount = 0; 680107120Sjulian d->bd_dcount = 0; 681107120Sjulian d->bd_fcount = 0; 682107120Sjulian} 683107120Sjulian 684107120Sjulian/* 685107120Sjulian * FIONREAD Check for read packet available. 686107120Sjulian * SIOCGIFADDR Get interface address - convenient hook to driver. 687107120Sjulian * BIOCGBLEN Get buffer len [for read()]. 688107120Sjulian * BIOCSETF Set ethernet read filter. 689107120Sjulian * BIOCSETWF Set ethernet write filter. 690107120Sjulian * BIOCFLUSH Flush read packet buffer. 691107120Sjulian * BIOCPROMISC Put interface into promiscuous mode. 692107120Sjulian * BIOCGDLT Get link layer type. 693107120Sjulian * BIOCGETIF Get interface name. 694107120Sjulian * BIOCSETIF Set interface. 695107120Sjulian * BIOCSRTIMEOUT Set read timeout. 696107120Sjulian * BIOCGRTIMEOUT Get read timeout. 697107120Sjulian * BIOCGSTATS Get packet stats. 698107120Sjulian * BIOCIMMEDIATE Set immediate mode. 699107120Sjulian * BIOCVERSION Get filter language version. 700107120Sjulian * BIOCGHDRCMPLT Get "header already complete" flag 701107120Sjulian * BIOCSHDRCMPLT Set "header already complete" flag 702107120Sjulian * BIOCGDIRECTION Get packet direction flag 703107120Sjulian * BIOCSDIRECTION Set packet direction flag 704107120Sjulian * BIOCLOCK Set "locked" flag 705107120Sjulian * BIOCFEEDBACK Set packet feedback mode. 706107120Sjulian */ 707107120Sjulian/* ARGSUSED */ 708107120Sjulianstatic int 709107120Sjulianbpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, 710107120Sjulian struct thread *td) 711107120Sjulian{ 712107120Sjulian struct bpf_d *d = dev->si_drv1; 713107120Sjulian int error = 0; 714107120Sjulian 715107120Sjulian /* 716107120Sjulian * Refresh PID associated with this descriptor. 717107120Sjulian */ 718107120Sjulian BPFD_LOCK(d); 719107120Sjulian d->bd_pid = td->td_proc->p_pid; 720107120Sjulian if (d->bd_state == BPF_WAITING) 721107120Sjulian callout_stop(&d->bd_callout); 722107120Sjulian d->bd_state = BPF_IDLE; 723107120Sjulian BPFD_UNLOCK(d); 724107120Sjulian 725107120Sjulian if (d->bd_locked == 1) { 726107120Sjulian switch (cmd) { 727107120Sjulian case BIOCGBLEN: 728107120Sjulian case BIOCFLUSH: 729107120Sjulian case BIOCGDLT: 730107120Sjulian case BIOCGDLTLIST: 731107120Sjulian case BIOCGETIF: 732107120Sjulian case BIOCGRTIMEOUT: 733107120Sjulian case BIOCGSTATS: 734107120Sjulian case BIOCVERSION: 735107120Sjulian case BIOCGRSIG: 736107120Sjulian case BIOCGHDRCMPLT: 737107120Sjulian case BIOCFEEDBACK: 738107120Sjulian case FIONREAD: 739107120Sjulian case BIOCLOCK: 740107120Sjulian case BIOCSRTIMEOUT: 741107120Sjulian case BIOCIMMEDIATE: 742107120Sjulian case TIOCGPGRP: 743107120Sjulian break; 744107120Sjulian default: 745107120Sjulian return (EPERM); 746107120Sjulian } 747107120Sjulian } 748107120Sjulian switch (cmd) { 749107120Sjulian 750107120Sjulian default: 751107120Sjulian error = EINVAL; 752107120Sjulian break; 753107120Sjulian 754107120Sjulian /* 755107120Sjulian * Check for read packet available. 756107120Sjulian */ 757107120Sjulian case FIONREAD: 758107120Sjulian { 759107120Sjulian int n; 760107120Sjulian 761107120Sjulian BPFD_LOCK(d); 762107120Sjulian n = d->bd_slen; 763107120Sjulian if (d->bd_hbuf) 764107120Sjulian n += d->bd_hlen; 765107120Sjulian BPFD_UNLOCK(d); 766107120Sjulian 767107120Sjulian *(int *)addr = n; 768107120Sjulian break; 769107120Sjulian } 770107120Sjulian 771107120Sjulian case SIOCGIFADDR: 772107120Sjulian { 773107120Sjulian struct ifnet *ifp; 774107120Sjulian 775107120Sjulian if (d->bd_bif == NULL) 776107120Sjulian error = EINVAL; 777107120Sjulian else { 778107120Sjulian ifp = d->bd_bif->bif_ifp; 779107120Sjulian error = (*ifp->if_ioctl)(ifp, cmd, addr); 780107120Sjulian } 781107120Sjulian break; 782107120Sjulian } 783107120Sjulian 784107120Sjulian /* 785107120Sjulian * Get buffer len [for read()]. 786107120Sjulian */ 787107120Sjulian case BIOCGBLEN: 788107120Sjulian *(u_int *)addr = d->bd_bufsize; 789107120Sjulian break; 790107120Sjulian 791107120Sjulian /* 792107120Sjulian * Set buffer length. 793107120Sjulian */ 794107120Sjulian case BIOCSBLEN: 795107120Sjulian if (d->bd_bif != NULL) 796107120Sjulian error = EINVAL; 797107120Sjulian else { 798107120Sjulian u_int size = *(u_int *)addr; 799107120Sjulian 800107120Sjulian if (size > bpf_maxbufsize) 801107120Sjulian *(u_int *)addr = size = bpf_maxbufsize; 802107120Sjulian else if (size < BPF_MINBUFSIZE) 803107120Sjulian *(u_int *)addr = size = BPF_MINBUFSIZE; 804107120Sjulian d->bd_bufsize = size; 805107120Sjulian } 806107120Sjulian break; 807107120Sjulian 808107120Sjulian /* 809107120Sjulian * Set link layer read filter. 810107120Sjulian */ 811107120Sjulian case BIOCSETF: 812107120Sjulian case BIOCSETWF: 813107120Sjulian error = bpf_setf(d, (struct bpf_program *)addr, cmd); 814107120Sjulian break; 815107120Sjulian 816107120Sjulian /* 817107120Sjulian * Flush read packet buffer. 818107120Sjulian */ 819107120Sjulian case BIOCFLUSH: 820107120Sjulian BPFD_LOCK(d); 821107120Sjulian reset_d(d); 822107120Sjulian BPFD_UNLOCK(d); 823107120Sjulian break; 824107120Sjulian 825107120Sjulian /* 826107120Sjulian * Put interface into promiscuous mode. 827107120Sjulian */ 828107120Sjulian case BIOCPROMISC: 829107120Sjulian if (d->bd_bif == NULL) { 830107120Sjulian /* 831107120Sjulian * No interface attached yet. 832107120Sjulian */ 833107120Sjulian error = EINVAL; 834107120Sjulian break; 835107120Sjulian } 836107120Sjulian if (d->bd_promisc == 0) { 837107120Sjulian mtx_lock(&Giant); 838107120Sjulian error = ifpromisc(d->bd_bif->bif_ifp, 1); 839107120Sjulian mtx_unlock(&Giant); 840107120Sjulian if (error == 0) 841107120Sjulian d->bd_promisc = 1; 842107120Sjulian } 843107120Sjulian break; 844107120Sjulian 845107120Sjulian /* 846107120Sjulian * Get current data link type. 847107120Sjulian */ 848107120Sjulian case BIOCGDLT: 849107120Sjulian if (d->bd_bif == NULL) 850107120Sjulian error = EINVAL; 851107120Sjulian else 852107120Sjulian *(u_int *)addr = d->bd_bif->bif_dlt; 853107120Sjulian break; 854107120Sjulian 855107120Sjulian /* 856107120Sjulian * Get a list of supported data link types. 857107120Sjulian */ 858107120Sjulian case BIOCGDLTLIST: 859107120Sjulian if (d->bd_bif == NULL) 860107120Sjulian error = EINVAL; 861107120Sjulian else 862107120Sjulian error = bpf_getdltlist(d, (struct bpf_dltlist *)addr); 863107120Sjulian break; 864107120Sjulian 865107120Sjulian /* 866107120Sjulian * Set data link type. 867107120Sjulian */ 868107120Sjulian case BIOCSDLT: 869107120Sjulian if (d->bd_bif == NULL) 870107120Sjulian error = EINVAL; 871107120Sjulian else 872107120Sjulian error = bpf_setdlt(d, *(u_int *)addr); 873107120Sjulian break; 874107120Sjulian 875107120Sjulian /* 876107120Sjulian * Get interface name. 877107120Sjulian */ 878107120Sjulian case BIOCGETIF: 879107120Sjulian if (d->bd_bif == NULL) 880107120Sjulian error = EINVAL; 881107120Sjulian else { 882107120Sjulian struct ifnet *const ifp = d->bd_bif->bif_ifp; 883107120Sjulian struct ifreq *const ifr = (struct ifreq *)addr; 884107120Sjulian 885107120Sjulian strlcpy(ifr->ifr_name, ifp->if_xname, 886107120Sjulian sizeof(ifr->ifr_name)); 887107120Sjulian } 888107120Sjulian break; 889107120Sjulian 890107120Sjulian /* 891107120Sjulian * Set interface. 892107120Sjulian */ 893107120Sjulian case BIOCSETIF: 894107120Sjulian error = bpf_setif(d, (struct ifreq *)addr); 895107120Sjulian break; 896107120Sjulian 897107120Sjulian /* 898107120Sjulian * Set read timeout. 899107120Sjulian */ 900107120Sjulian case BIOCSRTIMEOUT: 901107120Sjulian { 902107120Sjulian struct timeval *tv = (struct timeval *)addr; 903107120Sjulian 904107120Sjulian /* 905107120Sjulian * Subtract 1 tick from tvtohz() since this isn't 906107120Sjulian * a one-shot timer. 907107120Sjulian */ 908107120Sjulian if ((error = itimerfix(tv)) == 0) 909107120Sjulian d->bd_rtout = tvtohz(tv) - 1; 910107120Sjulian break; 911107120Sjulian } 912107120Sjulian 913107120Sjulian /* 914107120Sjulian * Get read timeout. 915107120Sjulian */ 916107120Sjulian case BIOCGRTIMEOUT: 917107120Sjulian { 918107120Sjulian struct timeval *tv = (struct timeval *)addr; 919107120Sjulian 920107120Sjulian tv->tv_sec = d->bd_rtout / hz; 921107120Sjulian tv->tv_usec = (d->bd_rtout % hz) * tick; 922107120Sjulian break; 923107120Sjulian } 924107120Sjulian 925107120Sjulian /* 926107120Sjulian * Get packet stats. 927107120Sjulian */ 928107120Sjulian case BIOCGSTATS: 929107120Sjulian { 930107120Sjulian struct bpf_stat *bs = (struct bpf_stat *)addr; 931107120Sjulian 932107120Sjulian bs->bs_recv = d->bd_rcount; 933107120Sjulian bs->bs_drop = d->bd_dcount; 934107120Sjulian break; 935107120Sjulian } 936107120Sjulian 937107120Sjulian /* 938107120Sjulian * Set immediate mode. 939107120Sjulian */ 940107120Sjulian case BIOCIMMEDIATE: 941107120Sjulian d->bd_immediate = *(u_int *)addr; 942107120Sjulian break; 943107120Sjulian 944107120Sjulian case BIOCVERSION: 945107120Sjulian { 946107120Sjulian struct bpf_version *bv = (struct bpf_version *)addr; 947107120Sjulian 948107120Sjulian bv->bv_major = BPF_MAJOR_VERSION; 949107120Sjulian bv->bv_minor = BPF_MINOR_VERSION; 950107120Sjulian break; 951107120Sjulian } 952107120Sjulian 953107120Sjulian /* 954107120Sjulian * Get "header already complete" flag 955107120Sjulian */ 956107120Sjulian case BIOCGHDRCMPLT: 957107120Sjulian *(u_int *)addr = d->bd_hdrcmplt; 958107120Sjulian break; 959107120Sjulian 960107120Sjulian /* 961107120Sjulian * Set "header already complete" flag 962107120Sjulian */ 963107120Sjulian case BIOCSHDRCMPLT: 964107120Sjulian d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 965107120Sjulian break; 966107120Sjulian 967107120Sjulian /* 968107120Sjulian * Get packet direction flag 969107120Sjulian */ 970107120Sjulian case BIOCGDIRECTION: 971107120Sjulian *(u_int *)addr = d->bd_direction; 972107120Sjulian break; 973107120Sjulian 974107120Sjulian /* 975107120Sjulian * Set packet direction flag 976107120Sjulian */ 977107120Sjulian case BIOCSDIRECTION: 978107120Sjulian { 979107120Sjulian u_int direction; 980107120Sjulian 981107120Sjulian direction = *(u_int *)addr; 982107120Sjulian switch (direction) { 983107120Sjulian case BPF_D_IN: 984107120Sjulian case BPF_D_INOUT: 985107120Sjulian case BPF_D_OUT: 986107120Sjulian d->bd_direction = direction; 987107120Sjulian break; 988107120Sjulian default: 989107120Sjulian error = EINVAL; 990107120Sjulian } 991107120Sjulian } 992107120Sjulian break; 993107120Sjulian 994107120Sjulian case BIOCFEEDBACK: 995107120Sjulian d->bd_feedback = *(u_int *)addr; 996107120Sjulian break; 997107120Sjulian 998107120Sjulian case BIOCLOCK: 999107120Sjulian d->bd_locked = 1; 1000107120Sjulian break; 1001107120Sjulian 1002107120Sjulian case FIONBIO: /* Non-blocking I/O */ 1003107120Sjulian break; 1004107120Sjulian 1005107120Sjulian case FIOASYNC: /* Send signal on receive packets */ 1006107120Sjulian d->bd_async = *(int *)addr; 1007107120Sjulian break; 1008107120Sjulian 1009107120Sjulian case FIOSETOWN: 1010107120Sjulian error = fsetown(*(int *)addr, &d->bd_sigio); 1011107120Sjulian break; 1012107120Sjulian 1013107120Sjulian case FIOGETOWN: 1014107120Sjulian *(int *)addr = fgetown(&d->bd_sigio); 1015107120Sjulian break; 1016107120Sjulian 1017107120Sjulian /* This is deprecated, FIOSETOWN should be used instead. */ 1018107120Sjulian case TIOCSPGRP: 1019107120Sjulian error = fsetown(-(*(int *)addr), &d->bd_sigio); 1020107120Sjulian break; 1021107120Sjulian 1022107120Sjulian /* This is deprecated, FIOGETOWN should be used instead. */ 1023107120Sjulian case TIOCGPGRP: 1024107120Sjulian *(int *)addr = -fgetown(&d->bd_sigio); 1025107120Sjulian break; 1026107120Sjulian 1027107120Sjulian case BIOCSRSIG: /* Set receive signal */ 1028107120Sjulian { 1029107120Sjulian u_int sig; 1030107120Sjulian 1031107120Sjulian sig = *(u_int *)addr; 1032107120Sjulian 1033107120Sjulian if (sig >= NSIG) 1034107120Sjulian error = EINVAL; 1035107120Sjulian else 1036107120Sjulian d->bd_sig = sig; 1037107120Sjulian break; 1038107120Sjulian } 1039107120Sjulian case BIOCGRSIG: 1040107120Sjulian *(u_int *)addr = d->bd_sig; 1041107120Sjulian break; 1042107120Sjulian } 1043107120Sjulian return (error); 1044107120Sjulian} 1045107120Sjulian 1046107120Sjulian/* 1047107120Sjulian * Set d's packet filter program to fp. If this file already has a filter, 1048107120Sjulian * free it and replace it. Returns EINVAL for bogus requests. 1049107120Sjulian */ 1050107120Sjulianstatic int 1051107120Sjulianbpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd) 1052107120Sjulian{ 1053107120Sjulian struct bpf_insn *fcode, *old; 1054107120Sjulian u_int wfilter, flen, size; 1055107120Sjulian#ifdef BPF_JITTER 1056107120Sjulian bpf_jit_filter *ofunc; 1057107120Sjulian#endif 1058107120Sjulian 1059107120Sjulian if (cmd == BIOCSETWF) { 1060107120Sjulian old = d->bd_wfilter; 1061107120Sjulian wfilter = 1; 1062107120Sjulian#ifdef BPF_JITTER 1063107120Sjulian ofunc = NULL; 1064107120Sjulian#endif 1065107120Sjulian } else { 1066107120Sjulian wfilter = 0; 1067107120Sjulian old = d->bd_rfilter; 1068107120Sjulian#ifdef BPF_JITTER 1069107120Sjulian ofunc = d->bd_bfilter; 1070107120Sjulian#endif 1071107120Sjulian } 1072107120Sjulian if (fp->bf_insns == NULL) { 1073107120Sjulian if (fp->bf_len != 0) 1074107120Sjulian return (EINVAL); 1075107120Sjulian BPFD_LOCK(d); 1076107120Sjulian if (wfilter) 1077107120Sjulian d->bd_wfilter = NULL; 1078107120Sjulian else { 1079107120Sjulian d->bd_rfilter = NULL; 1080107120Sjulian#ifdef BPF_JITTER 1081107120Sjulian d->bd_bfilter = NULL; 1082107120Sjulian#endif 1083107120Sjulian } 1084107120Sjulian reset_d(d); 1085107120Sjulian BPFD_UNLOCK(d); 1086107120Sjulian if (old != NULL) 1087107120Sjulian free((caddr_t)old, M_BPF); 1088107120Sjulian#ifdef BPF_JITTER 1089107120Sjulian if (ofunc != NULL) 1090107120Sjulian bpf_destroy_jit_filter(ofunc); 1091107120Sjulian#endif 1092107120Sjulian return (0); 1093107120Sjulian } 1094107120Sjulian flen = fp->bf_len; 1095107120Sjulian if (flen > bpf_maxinsns) 1096107120Sjulian return (EINVAL); 1097107120Sjulian 1098107120Sjulian size = flen * sizeof(*fp->bf_insns); 1099107120Sjulian fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK); 1100107120Sjulian if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 1101107120Sjulian bpf_validate(fcode, (int)flen)) { 1102107120Sjulian BPFD_LOCK(d); 1103107120Sjulian if (wfilter) 1104107120Sjulian d->bd_wfilter = fcode; 1105107120Sjulian else { 1106107120Sjulian d->bd_rfilter = fcode; 1107107120Sjulian#ifdef BPF_JITTER 1108107120Sjulian d->bd_bfilter = bpf_jitter(fcode, flen); 1109107120Sjulian#endif 1110107120Sjulian } 1111107120Sjulian reset_d(d); 1112107120Sjulian BPFD_UNLOCK(d); 1113107120Sjulian if (old != NULL) 1114107120Sjulian free((caddr_t)old, M_BPF); 1115107120Sjulian#ifdef BPF_JITTER 1116107120Sjulian if (ofunc != NULL) 1117107120Sjulian bpf_destroy_jit_filter(ofunc); 1118107120Sjulian#endif 1119107120Sjulian 1120107120Sjulian return (0); 1121107120Sjulian } 1122107120Sjulian free((caddr_t)fcode, M_BPF); 1123107120Sjulian return (EINVAL); 1124107120Sjulian} 1125107120Sjulian 1126107120Sjulian/* 1127107120Sjulian * Detach a file from its current interface (if attached at all) and attach 1128107120Sjulian * to the interface indicated by the name stored in ifr. 1129107120Sjulian * Return an errno or 0. 1130107120Sjulian */ 1131107120Sjulianstatic int 1132107120Sjulianbpf_setif(struct bpf_d *d, struct ifreq *ifr) 1133107120Sjulian{ 1134107120Sjulian struct bpf_if *bp; 1135107120Sjulian struct ifnet *theywant; 1136107120Sjulian 1137107120Sjulian theywant = ifunit(ifr->ifr_name); 1138107120Sjulian if (theywant == NULL || theywant->if_bpf == NULL) 1139107120Sjulian return (ENXIO); 1140107120Sjulian 1141107120Sjulian bp = theywant->if_bpf; 1142107120Sjulian /* 1143107120Sjulian * Allocate the packet buffers if we need to. 1144107120Sjulian * If we're already attached to requested interface, 1145107120Sjulian * just flush the buffer. 1146107120Sjulian */ 1147107120Sjulian if (d->bd_sbuf == NULL) 1148107120Sjulian bpf_allocbufs(d); 1149107120Sjulian if (bp != d->bd_bif) { 1150107120Sjulian if (d->bd_bif) 1151107120Sjulian /* 1152107120Sjulian * Detach if attached to something else. 1153107120Sjulian */ 1154107120Sjulian bpf_detachd(d); 1155107120Sjulian 1156107120Sjulian bpf_attachd(d, bp); 1157107120Sjulian } 1158107120Sjulian BPFD_LOCK(d); 1159107120Sjulian reset_d(d); 1160107120Sjulian BPFD_UNLOCK(d); 1161107120Sjulian return (0); 1162107120Sjulian} 1163107120Sjulian 1164107120Sjulian/* 1165107120Sjulian * Support for select() and poll() system calls 1166107120Sjulian * 1167107120Sjulian * Return true iff the specific operation will not block indefinitely. 1168107120Sjulian * Otherwise, return false but make a note that a selwakeup() must be done. 1169107120Sjulian */ 1170107120Sjulianstatic int 1171107120Sjulianbpfpoll(struct cdev *dev, int events, struct thread *td) 1172107120Sjulian{ 1173107120Sjulian struct bpf_d *d; 1174107120Sjulian int revents; 1175107120Sjulian 1176107120Sjulian d = dev->si_drv1; 1177107120Sjulian if (d->bd_bif == NULL) 1178107120Sjulian return (ENXIO); 1179107120Sjulian 1180107120Sjulian /* 1181107120Sjulian * Refresh PID associated with this descriptor. 1182107120Sjulian */ 1183107120Sjulian revents = events & (POLLOUT | POLLWRNORM); 1184107120Sjulian BPFD_LOCK(d); 1185107120Sjulian d->bd_pid = td->td_proc->p_pid; 1186107120Sjulian if (events & (POLLIN | POLLRDNORM)) { 1187107120Sjulian if (bpf_ready(d)) 1188107120Sjulian revents |= events & (POLLIN | POLLRDNORM); 1189107120Sjulian else { 1190107120Sjulian selrecord(td, &d->bd_sel); 1191107120Sjulian /* Start the read timeout if necessary. */ 1192107120Sjulian if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1193107120Sjulian callout_reset(&d->bd_callout, d->bd_rtout, 1194107120Sjulian bpf_timed_out, d); 1195107120Sjulian d->bd_state = BPF_WAITING; 1196107120Sjulian } 1197107120Sjulian } 1198107120Sjulian } 1199107120Sjulian BPFD_UNLOCK(d); 1200107120Sjulian return (revents); 1201107120Sjulian} 1202107120Sjulian 1203107120Sjulian/* 1204107120Sjulian * Support for kevent() system call. Register EVFILT_READ filters and 1205107120Sjulian * reject all others. 1206107120Sjulian */ 1207107120Sjulianint 1208107120Sjulianbpfkqfilter(struct cdev *dev, struct knote *kn) 1209107120Sjulian{ 1210107120Sjulian struct bpf_d *d = (struct bpf_d *)dev->si_drv1; 1211107120Sjulian 1212107120Sjulian if (kn->kn_filter != EVFILT_READ) 1213107120Sjulian return (1); 1214107120Sjulian 1215107120Sjulian /* 1216107120Sjulian * Refresh PID associated with this descriptor. 1217107120Sjulian */ 1218107120Sjulian BPFD_LOCK(d); 1219107120Sjulian d->bd_pid = curthread->td_proc->p_pid; 1220107120Sjulian kn->kn_fop = &bpfread_filtops; 1221107120Sjulian kn->kn_hook = d; 1222107120Sjulian knlist_add(&d->bd_sel.si_note, kn, 1); 1223107120Sjulian BPFD_UNLOCK(d); 1224107120Sjulian 1225107120Sjulian return (0); 1226107120Sjulian} 1227107120Sjulian 1228107120Sjulianstatic void 1229107120Sjulianfilt_bpfdetach(struct knote *kn) 1230107120Sjulian{ 1231107120Sjulian struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1232107120Sjulian 1233107120Sjulian knlist_remove(&d->bd_sel.si_note, kn, 0); 1234107120Sjulian} 1235107120Sjulian 1236107120Sjulianstatic int 1237107120Sjulianfilt_bpfread(struct knote *kn, long hint) 1238107120Sjulian{ 1239107120Sjulian struct bpf_d *d = (struct bpf_d *)kn->kn_hook; 1240107120Sjulian int ready; 1241107120Sjulian 1242107120Sjulian BPFD_LOCK_ASSERT(d); 1243107120Sjulian ready = bpf_ready(d); 1244107120Sjulian if (ready) { 1245107120Sjulian kn->kn_data = d->bd_slen; 1246107120Sjulian if (d->bd_hbuf) 1247107120Sjulian kn->kn_data += d->bd_hlen; 1248107120Sjulian } 1249107120Sjulian else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1250107120Sjulian callout_reset(&d->bd_callout, d->bd_rtout, 1251107120Sjulian bpf_timed_out, d); 1252107120Sjulian d->bd_state = BPF_WAITING; 1253107120Sjulian } 1254107120Sjulian 1255107120Sjulian return (ready); 1256107120Sjulian} 1257107120Sjulian 1258107120Sjulian/* 1259107120Sjulian * Incoming linkage from device drivers. Process the packet pkt, of length 1260107120Sjulian * pktlen, which is stored in a contiguous buffer. The packet is parsed 1261107120Sjulian * by each process' filter, and if accepted, stashed into the corresponding 1262107120Sjulian * buffer. 1263107120Sjulian */ 1264107120Sjulianvoid 1265107120Sjulianbpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1266107120Sjulian{ 1267107120Sjulian struct bpf_d *d; 1268107120Sjulian u_int slen; 1269107120Sjulian int gottime; 1270107120Sjulian struct timeval tv; 1271107120Sjulian 1272107120Sjulian gottime = 0; 1273107120Sjulian BPFIF_LOCK(bp); 1274107120Sjulian LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1275107120Sjulian BPFD_LOCK(d); 1276107120Sjulian ++d->bd_rcount; 1277107120Sjulian#ifdef BPF_JITTER 1278107120Sjulian if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL) 1279107120Sjulian slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen); 1280107120Sjulian else 1281107120Sjulian#endif 1282107120Sjulian slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen); 1283107120Sjulian if (slen != 0) { 1284107120Sjulian d->bd_fcount++; 1285107120Sjulian if (!gottime) { 1286107120Sjulian microtime(&tv); 1287107120Sjulian gottime = 1; 1288107120Sjulian } 1289107120Sjulian#ifdef MAC 1290107120Sjulian if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1291107120Sjulian#endif 1292107120Sjulian catchpacket(d, pkt, pktlen, slen, bcopy, &tv); 1293107120Sjulian } 1294107120Sjulian BPFD_UNLOCK(d); 1295107120Sjulian } 1296107120Sjulian BPFIF_UNLOCK(bp); 1297107120Sjulian} 1298107120Sjulian 1299107120Sjulian/* 1300107120Sjulian * Copy data from an mbuf chain into a buffer. This code is derived 1301107120Sjulian * from m_copydata in sys/uipc_mbuf.c. 1302107120Sjulian */ 1303107120Sjulianstatic void 1304107120Sjulianbpf_mcopy(const void *src_arg, void *dst_arg, size_t len) 1305107120Sjulian{ 1306107120Sjulian const struct mbuf *m; 1307107120Sjulian u_int count; 1308107120Sjulian u_char *dst; 1309107120Sjulian 1310107120Sjulian m = src_arg; 1311107120Sjulian dst = dst_arg; 1312107120Sjulian while (len > 0) { 1313107120Sjulian if (m == NULL) 1314107120Sjulian panic("bpf_mcopy"); 1315107120Sjulian count = min(m->m_len, len); 1316107120Sjulian bcopy(mtod(m, void *), dst, count); 1317107120Sjulian m = m->m_next; 1318107120Sjulian dst += count; 1319107120Sjulian len -= count; 1320107120Sjulian } 1321107120Sjulian} 1322107120Sjulian 1323107120Sjulian#define BPF_CHECK_DIRECTION(d, m) \ 1324107120Sjulian if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \ 1325107120Sjulian ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL)) 1326107120Sjulian 1327107120Sjulian/* 1328107120Sjulian * Incoming linkage from device drivers, when packet is in an mbuf chain. 1329107120Sjulian */ 1330107120Sjulianvoid 1331107120Sjulianbpf_mtap(struct bpf_if *bp, struct mbuf *m) 1332107120Sjulian{ 1333107120Sjulian struct bpf_d *d; 1334107120Sjulian u_int pktlen, slen; 1335107120Sjulian int gottime; 1336107120Sjulian struct timeval tv; 1337107120Sjulian 1338107120Sjulian if (m->m_flags & M_SKIP_BPF) { 1339107120Sjulian m->m_flags &= ~M_SKIP_BPF; 1340107120Sjulian return; 1341107120Sjulian } 1342107120Sjulian 1343107120Sjulian gottime = 0; 1344107120Sjulian 1345107120Sjulian pktlen = m_length(m, NULL); 1346107120Sjulian 1347107120Sjulian BPFIF_LOCK(bp); 1348107120Sjulian LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1349107120Sjulian BPF_CHECK_DIRECTION(d, m) 1350107120Sjulian continue; 1351107120Sjulian BPFD_LOCK(d); 1352107120Sjulian ++d->bd_rcount; 1353107120Sjulian#ifdef BPF_JITTER 1354107120Sjulian /* XXX We cannot handle multiple mbufs. */ 1355107120Sjulian if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL && 1356107120Sjulian m->m_next == NULL) 1357107120Sjulian slen = (*(d->bd_bfilter->func))(mtod(m, u_char *), 1358107120Sjulian pktlen, pktlen); 1359107120Sjulian else 1360107120Sjulian#endif 1361107120Sjulian slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0); 1362107120Sjulian if (slen != 0) { 1363107120Sjulian d->bd_fcount++; 1364107120Sjulian if (!gottime) { 1365107120Sjulian microtime(&tv); 1366107120Sjulian gottime = 1; 1367107120Sjulian } 1368107120Sjulian#ifdef MAC 1369107120Sjulian if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1370107120Sjulian#endif 1371107120Sjulian catchpacket(d, (u_char *)m, pktlen, slen, 1372107120Sjulian bpf_mcopy, &tv); 1373107120Sjulian } 1374107120Sjulian BPFD_UNLOCK(d); 1375107120Sjulian } 1376107120Sjulian BPFIF_UNLOCK(bp); 1377107120Sjulian} 1378107120Sjulian 1379107120Sjulian/* 1380107120Sjulian * Incoming linkage from device drivers, when packet is in 1381107120Sjulian * an mbuf chain and to be prepended by a contiguous header. 1382107120Sjulian */ 1383107120Sjulianvoid 1384107120Sjulianbpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m) 1385107120Sjulian{ 1386107120Sjulian struct mbuf mb; 1387107120Sjulian struct bpf_d *d; 1388107120Sjulian u_int pktlen, slen; 1389107120Sjulian int gottime; 1390107120Sjulian struct timeval tv; 1391107120Sjulian 1392107120Sjulian if (m->m_flags & M_SKIP_BPF) { 1393107120Sjulian m->m_flags &= ~M_SKIP_BPF; 1394107120Sjulian return; 1395107120Sjulian } 1396107120Sjulian 1397107120Sjulian gottime = 0; 1398107120Sjulian 1399107120Sjulian pktlen = m_length(m, NULL); 1400107120Sjulian /* 1401107120Sjulian * Craft on-stack mbuf suitable for passing to bpf_filter. 1402107120Sjulian * Note that we cut corners here; we only setup what's 1403107120Sjulian * absolutely needed--this mbuf should never go anywhere else. 1404107120Sjulian */ 1405107120Sjulian mb.m_next = m; 1406107120Sjulian mb.m_data = data; 1407107120Sjulian mb.m_len = dlen; 1408107120Sjulian pktlen += dlen; 1409107120Sjulian 1410107120Sjulian BPFIF_LOCK(bp); 1411107120Sjulian LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1412107120Sjulian BPF_CHECK_DIRECTION(d, m) 1413107120Sjulian continue; 1414107120Sjulian BPFD_LOCK(d); 1415107120Sjulian ++d->bd_rcount; 1416107120Sjulian slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0); 1417107120Sjulian if (slen != 0) { 1418107120Sjulian d->bd_fcount++; 1419107120Sjulian if (!gottime) { 1420107120Sjulian microtime(&tv); 1421107120Sjulian gottime = 1; 1422107120Sjulian } 1423107120Sjulian#ifdef MAC 1424107120Sjulian if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0) 1425107120Sjulian#endif 1426107120Sjulian catchpacket(d, (u_char *)&mb, pktlen, slen, 1427107120Sjulian bpf_mcopy, &tv); 1428107120Sjulian } 1429107120Sjulian BPFD_UNLOCK(d); 1430107120Sjulian } 1431107120Sjulian BPFIF_UNLOCK(bp); 1432107120Sjulian} 1433107120Sjulian 1434107120Sjulian#undef BPF_CHECK_DIRECTION 1435107120Sjulian 1436107120Sjulian/* 1437107120Sjulian * Move the packet data from interface memory (pkt) into the 1438107120Sjulian * store buffer. "cpfn" is the routine called to do the actual data 1439107120Sjulian * transfer. bcopy is passed in to copy contiguous chunks, while 1440107120Sjulian * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 1441107120Sjulian * pkt is really an mbuf. 1442107120Sjulian */ 1443133415Semaxstatic void 1444107120Sjuliancatchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, 1445107120Sjulian void (*cpfn)(const void *, void *, size_t), struct timeval *tv) 1446133415Semax{ 1447133415Semax struct bpf_hdr *hp; 1448133415Semax int totlen, curlen; 1449133415Semax int hdrlen = d->bd_bif->bif_hdrlen; 1450133415Semax int do_wakeup = 0; 1451139686Semax 1452133415Semax BPFD_LOCK_ASSERT(d); 1453133415Semax /* 1454133415Semax * Figure out how many bytes to move. If the packet is 1455107120Sjulian * greater or equal to the snapshot length, transfer that 1456107120Sjulian * much. Otherwise, transfer the whole packet (unless 1457107120Sjulian * we hit the buffer size limit). 1458107120Sjulian */ 1459107120Sjulian totlen = hdrlen + min(snaplen, pktlen); 1460107120Sjulian if (totlen > d->bd_bufsize) 1461107120Sjulian totlen = d->bd_bufsize; 1462107120Sjulian 1463107120Sjulian /* 1464107120Sjulian * Round up the end of the previous packet to the next longword. 1465107120Sjulian */ 1466107120Sjulian curlen = BPF_WORDALIGN(d->bd_slen); 1467107120Sjulian if (curlen + totlen > d->bd_bufsize) { 1468107120Sjulian /* 1469107120Sjulian * This packet will overflow the storage buffer. 1470107120Sjulian * Rotate the buffers if we can, then wakeup any 1471107120Sjulian * pending reads. 1472107120Sjulian */ 1473107120Sjulian if (d->bd_fbuf == NULL) { 1474107120Sjulian /* 1475107120Sjulian * We haven't completed the previous read yet, 1476107120Sjulian * so drop the packet. 1477107120Sjulian */ 1478107120Sjulian ++d->bd_dcount; 1479107120Sjulian return; 1480107120Sjulian } 1481107120Sjulian ROTATE_BUFFERS(d); 1482107120Sjulian do_wakeup = 1; 1483107120Sjulian curlen = 0; 1484107120Sjulian } 1485107120Sjulian else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) 1486107120Sjulian /* 1487107120Sjulian * Immediate mode is set, or the read timeout has 1488107120Sjulian * already expired during a select call. A packet 1489107120Sjulian * arrived, so the reader should be woken up. 1490107120Sjulian */ 1491107120Sjulian do_wakeup = 1; 1492107120Sjulian 1493107120Sjulian /* 1494107120Sjulian * Append the bpf header. 1495107120Sjulian */ 1496107120Sjulian hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1497107120Sjulian hp->bh_tstamp = *tv; 1498107120Sjulian hp->bh_datalen = pktlen; 1499107120Sjulian hp->bh_hdrlen = hdrlen; 1500107120Sjulian /* 1501107120Sjulian * Copy the packet data into the store buffer and update its length. 1502107120Sjulian */ 1503107120Sjulian (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1504107120Sjulian d->bd_slen = curlen + totlen; 1505107120Sjulian 1506107120Sjulian if (do_wakeup) 1507107120Sjulian bpf_wakeup(d); 1508107120Sjulian} 1509107120Sjulian 1510107120Sjulian/* 1511107120Sjulian * Initialize all nonzero fields of a descriptor. 1512107120Sjulian */ 1513107120Sjulianstatic void 1514107120Sjulianbpf_allocbufs(struct bpf_d *d) 1515107120Sjulian{ 1516107120Sjulian 1517107120Sjulian KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL")); 1518107120Sjulian KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL")); 1519107120Sjulian KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL")); 1520107120Sjulian 1521107120Sjulian d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1522107120Sjulian d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1523107120Sjulian d->bd_slen = 0; 1524107120Sjulian d->bd_hlen = 0; 1525107120Sjulian} 1526107120Sjulian 1527107120Sjulian/* 1528107120Sjulian * Free buffers currently in use by a descriptor. 1529107120Sjulian * Called on close. 1530107120Sjulian */ 1531107120Sjulianstatic void 1532107120Sjulianbpf_freed(struct bpf_d *d) 1533107120Sjulian{ 1534107120Sjulian /* 1535107120Sjulian * We don't need to lock out interrupts since this descriptor has 1536107120Sjulian * been detached from its interface and it yet hasn't been marked 1537107120Sjulian * free. 1538107120Sjulian */ 1539107120Sjulian if (d->bd_sbuf != NULL) { 1540107120Sjulian free(d->bd_sbuf, M_BPF); 1541107120Sjulian if (d->bd_hbuf != NULL) 1542107120Sjulian free(d->bd_hbuf, M_BPF); 1543107120Sjulian if (d->bd_fbuf != NULL) 1544107120Sjulian free(d->bd_fbuf, M_BPF); 1545107120Sjulian } 1546107120Sjulian if (d->bd_rfilter) { 1547107120Sjulian free((caddr_t)d->bd_rfilter, M_BPF); 1548107120Sjulian#ifdef BPF_JITTER 1549107120Sjulian bpf_destroy_jit_filter(d->bd_bfilter); 1550107120Sjulian#endif 1551107120Sjulian } 1552107120Sjulian if (d->bd_wfilter) 1553107120Sjulian free((caddr_t)d->bd_wfilter, M_BPF); 1554107120Sjulian mtx_destroy(&d->bd_mtx); 1555107120Sjulian} 1556107120Sjulian 1557107120Sjulian/* 1558107120Sjulian * Attach an interface to bpf. dlt is the link layer type; hdrlen is the 1559107120Sjulian * fixed size of the link header (variable length headers not yet supported). 1560107120Sjulian */ 1561107120Sjulianvoid 1562107120Sjulianbpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1563107120Sjulian{ 1564107120Sjulian 1565107120Sjulian bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 1566107120Sjulian} 1567107120Sjulian 1568107120Sjulian/* 1569107120Sjulian * Attach an interface to bpf. ifp is a pointer to the structure 1570107120Sjulian * defining the interface to be attached, dlt is the link layer type, 1571107120Sjulian * and hdrlen is the fixed size of the link header (variable length 1572107120Sjulian * headers are not yet supporrted). 1573107120Sjulian */ 1574107120Sjulianvoid 1575107120Sjulianbpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1576107120Sjulian{ 1577107120Sjulian struct bpf_if *bp; 1578107120Sjulian 1579107120Sjulian bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 1580107120Sjulian if (bp == NULL) 1581107120Sjulian panic("bpfattach"); 1582107120Sjulian 1583107120Sjulian LIST_INIT(&bp->bif_dlist); 1584107120Sjulian bp->bif_ifp = ifp; 1585107120Sjulian bp->bif_dlt = dlt; 1586107120Sjulian mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF); 1587107120Sjulian KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized")); 1588107120Sjulian *driverp = bp; 1589107120Sjulian 1590107120Sjulian mtx_lock(&bpf_mtx); 1591107120Sjulian LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next); 1592107120Sjulian mtx_unlock(&bpf_mtx); 1593107120Sjulian 1594107120Sjulian /* 1595107120Sjulian * Compute the length of the bpf header. This is not necessarily 1596107120Sjulian * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1597107120Sjulian * that the network layer header begins on a longword boundary (for 1598107120Sjulian * performance reasons and to alleviate alignment restrictions). 1599107120Sjulian */ 1600107120Sjulian bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1601107120Sjulian 1602107120Sjulian if (bootverbose) 1603107120Sjulian if_printf(ifp, "bpf attached\n"); 1604107120Sjulian} 1605107120Sjulian 1606107120Sjulian/* 1607107120Sjulian * Detach bpf from an interface. This involves detaching each descriptor 1608107120Sjulian * associated with the interface, and leaving bd_bif NULL. Notify each 1609107120Sjulian * descriptor as it's detached so that any sleepers wake up and get 1610107120Sjulian * ENXIO. 1611107120Sjulian */ 1612107120Sjulianvoid 1613107120Sjulianbpfdetach(struct ifnet *ifp) 1614107120Sjulian{ 1615107120Sjulian struct bpf_if *bp; 1616107120Sjulian struct bpf_d *d; 1617107120Sjulian 1618107120Sjulian /* Locate BPF interface information */ 1619107120Sjulian mtx_lock(&bpf_mtx); 1620107120Sjulian LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1621107120Sjulian if (ifp == bp->bif_ifp) 1622107120Sjulian break; 1623107120Sjulian } 1624107120Sjulian 1625107120Sjulian /* Interface wasn't attached */ 1626107120Sjulian if ((bp == NULL) || (bp->bif_ifp == NULL)) { 1627107120Sjulian mtx_unlock(&bpf_mtx); 1628107120Sjulian printf("bpfdetach: %s was not attached\n", ifp->if_xname); 1629107120Sjulian return; 1630107120Sjulian } 1631107120Sjulian 1632107120Sjulian LIST_REMOVE(bp, bif_next); 1633107120Sjulian mtx_unlock(&bpf_mtx); 1634107120Sjulian 1635107120Sjulian while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) { 1636107120Sjulian bpf_detachd(d); 1637107120Sjulian BPFD_LOCK(d); 1638107120Sjulian bpf_wakeup(d); 1639107120Sjulian BPFD_UNLOCK(d); 1640107120Sjulian } 1641107120Sjulian 1642107120Sjulian mtx_destroy(&bp->bif_mtx); 1643107120Sjulian free(bp, M_BPF); 1644107120Sjulian} 1645107120Sjulian 1646107120Sjulian/* 1647107120Sjulian * Get a list of available data link type of the interface. 1648107120Sjulian */ 1649107120Sjulianstatic int 1650107120Sjulianbpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 1651107120Sjulian{ 1652107120Sjulian int n, error; 1653107120Sjulian struct ifnet *ifp; 1654107120Sjulian struct bpf_if *bp; 1655107120Sjulian 1656107120Sjulian ifp = d->bd_bif->bif_ifp; 1657107120Sjulian n = 0; 1658107120Sjulian error = 0; 1659107120Sjulian mtx_lock(&bpf_mtx); 1660107120Sjulian LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1661107120Sjulian if (bp->bif_ifp != ifp) 1662107120Sjulian continue; 1663 if (bfl->bfl_list != NULL) { 1664 if (n >= bfl->bfl_len) { 1665 mtx_unlock(&bpf_mtx); 1666 return (ENOMEM); 1667 } 1668 error = copyout(&bp->bif_dlt, 1669 bfl->bfl_list + n, sizeof(u_int)); 1670 } 1671 n++; 1672 } 1673 mtx_unlock(&bpf_mtx); 1674 bfl->bfl_len = n; 1675 return (error); 1676} 1677 1678/* 1679 * Set the data link type of a BPF instance. 1680 */ 1681static int 1682bpf_setdlt(struct bpf_d *d, u_int dlt) 1683{ 1684 int error, opromisc; 1685 struct ifnet *ifp; 1686 struct bpf_if *bp; 1687 1688 if (d->bd_bif->bif_dlt == dlt) 1689 return (0); 1690 ifp = d->bd_bif->bif_ifp; 1691 mtx_lock(&bpf_mtx); 1692 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1693 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 1694 break; 1695 } 1696 mtx_unlock(&bpf_mtx); 1697 if (bp != NULL) { 1698 opromisc = d->bd_promisc; 1699 bpf_detachd(d); 1700 bpf_attachd(d, bp); 1701 BPFD_LOCK(d); 1702 reset_d(d); 1703 BPFD_UNLOCK(d); 1704 if (opromisc) { 1705 error = ifpromisc(bp->bif_ifp, 1); 1706 if (error) 1707 if_printf(bp->bif_ifp, 1708 "bpf_setdlt: ifpromisc failed (%d)\n", 1709 error); 1710 else 1711 d->bd_promisc = 1; 1712 } 1713 } 1714 return (bp == NULL ? EINVAL : 0); 1715} 1716 1717static void 1718bpf_clone(void *arg, struct ucred *cred, char *name, int namelen, 1719 struct cdev **dev) 1720{ 1721 int u; 1722 1723 if (*dev != NULL) 1724 return; 1725 if (dev_stdclone(name, NULL, "bpf", &u) != 1) 1726 return; 1727 *dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, 1728 "bpf%d", u); 1729 dev_ref(*dev); 1730 (*dev)->si_flags |= SI_CHEAPCLONE; 1731 return; 1732} 1733 1734static void 1735bpf_drvinit(void *unused) 1736{ 1737 1738 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF); 1739 LIST_INIT(&bpf_iflist); 1740 EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000); 1741} 1742 1743static void 1744bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd) 1745{ 1746 1747 bzero(d, sizeof(*d)); 1748 BPFD_LOCK_ASSERT(bd); 1749 d->bd_immediate = bd->bd_immediate; 1750 d->bd_promisc = bd->bd_promisc; 1751 d->bd_hdrcmplt = bd->bd_hdrcmplt; 1752 d->bd_direction = bd->bd_direction; 1753 d->bd_feedback = bd->bd_feedback; 1754 d->bd_async = bd->bd_async; 1755 d->bd_rcount = bd->bd_rcount; 1756 d->bd_dcount = bd->bd_dcount; 1757 d->bd_fcount = bd->bd_fcount; 1758 d->bd_sig = bd->bd_sig; 1759 d->bd_slen = bd->bd_slen; 1760 d->bd_hlen = bd->bd_hlen; 1761 d->bd_bufsize = bd->bd_bufsize; 1762 d->bd_pid = bd->bd_pid; 1763 strlcpy(d->bd_ifname, 1764 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ); 1765 d->bd_locked = bd->bd_locked; 1766} 1767 1768static int 1769bpf_stats_sysctl(SYSCTL_HANDLER_ARGS) 1770{ 1771 struct xbpf_d *xbdbuf, *xbd; 1772 int index, error; 1773 struct bpf_if *bp; 1774 struct bpf_d *bd; 1775 1776 /* 1777 * XXX This is not technically correct. It is possible for non 1778 * privileged users to open bpf devices. It would make sense 1779 * if the users who opened the devices were able to retrieve 1780 * the statistics for them, too. 1781 */ 1782 error = priv_check(req->td, PRIV_NET_BPF); 1783 if (error) 1784 return (error); 1785 if (req->oldptr == NULL) 1786 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd))); 1787 if (bpf_bpfd_cnt == 0) 1788 return (SYSCTL_OUT(req, 0, 0)); 1789 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK); 1790 mtx_lock(&bpf_mtx); 1791 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) { 1792 mtx_unlock(&bpf_mtx); 1793 free(xbdbuf, M_BPF); 1794 return (ENOMEM); 1795 } 1796 index = 0; 1797 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1798 BPFIF_LOCK(bp); 1799 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { 1800 xbd = &xbdbuf[index++]; 1801 BPFD_LOCK(bd); 1802 bpfstats_fill_xbpf(xbd, bd); 1803 BPFD_UNLOCK(bd); 1804 } 1805 BPFIF_UNLOCK(bp); 1806 } 1807 mtx_unlock(&bpf_mtx); 1808 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd)); 1809 free(xbdbuf, M_BPF); 1810 return (error); 1811} 1812 1813SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL) 1814 1815#else /* !DEV_BPF && !NETGRAPH_BPF */ 1816/* 1817 * NOP stubs to allow bpf-using drivers to load and function. 1818 * 1819 * A 'better' implementation would allow the core bpf functionality 1820 * to be loaded at runtime. 1821 */ 1822static struct bpf_if bp_null; 1823 1824void 1825bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) 1826{ 1827} 1828 1829void 1830bpf_mtap(struct bpf_if *bp, struct mbuf *m) 1831{ 1832} 1833 1834void 1835bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m) 1836{ 1837} 1838 1839void 1840bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) 1841{ 1842 1843 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); 1844} 1845 1846void 1847bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 1848{ 1849 1850 *driverp = &bp_null; 1851} 1852 1853void 1854bpfdetach(struct ifnet *ifp) 1855{ 1856} 1857 1858u_int 1859bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen) 1860{ 1861 return -1; /* "no filter" behaviour */ 1862} 1863 1864int 1865bpf_validate(const struct bpf_insn *f, int len) 1866{ 1867 return 0; /* false */ 1868} 1869 1870#endif /* !DEV_BPF && !NETGRAPH_BPF */ 1871