bpf.c revision 101074
154263Sshin/* 2175061Sobrien * Copyright (c) 1990, 1991, 1993 354263Sshin * The Regents of the University of California. All rights reserved. 454263Sshin * 554263Sshin * This code is derived from the Stanford/CMU enet packet filter, 654263Sshin * (net/enet.c) distributed as part of 4.3BSD, and code contributed 754263Sshin * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 854263Sshin * Berkeley Laboratory. 954263Sshin * 1054263Sshin * Redistribution and use in source and binary forms, with or without 1154263Sshin * modification, are permitted provided that the following conditions 1254263Sshin * are met: 1354263Sshin * 1. Redistributions of source code must retain the above copyright 1454263Sshin * notice, this list of conditions and the following disclaimer. 1554263Sshin * 2. Redistributions in binary form must reproduce the above copyright 1654263Sshin * notice, this list of conditions and the following disclaimer in the 1754263Sshin * documentation and/or other materials provided with the distribution. 1854263Sshin * 3. All advertising materials mentioning features or use of this software 1954263Sshin * must display the following acknowledgement: 2054263Sshin * This product includes software developed by the University of 2154263Sshin * California, Berkeley and its contributors. 2254263Sshin * 4. Neither the name of the University nor the names of its contributors 2354263Sshin * may be used to endorse or promote products derived from this software 2454263Sshin * without specific prior written permission. 2554263Sshin * 2654263Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2754263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2854263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2954263Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3054263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31132671Scharnier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3254263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3354263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3454263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35132671Scharnier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3654263Sshin * SUCH DAMAGE. 37132671Scharnier * 38132671Scharnier * @(#)bpf.c 8.4 (Berkeley) 1/9/95 39132671Scharnier * 4064342Sume * $FreeBSD: head/sys/net/bpf.c 101074 2002-07-31 16:09:38Z rwatson $ 4154263Sshin */ 4254263Sshin 4354263Sshin#include "opt_bpf.h" 4454263Sshin#include "opt_mac.h" 4554263Sshin#include "opt_netgraph.h" 4654263Sshin 4778064Sume#include <sys/param.h> 4854263Sshin#include <sys/systm.h> 4954263Sshin#include <sys/conf.h> 5054263Sshin#include <sys/mac.h> 5154263Sshin#include <sys/malloc.h> 5254263Sshin#include <sys/mbuf.h> 5354263Sshin#include <sys/time.h> 5454263Sshin#include <sys/proc.h> 5554263Sshin#include <sys/signalvar.h> 5654263Sshin#include <sys/filio.h> 5754263Sshin#include <sys/sockio.h> 5854263Sshin#include <sys/ttycom.h> 5954263Sshin#include <sys/filedesc.h> 6078064Sume 6154263Sshin#include <sys/poll.h> 6254263Sshin 6354263Sshin#include <sys/socket.h> 6454263Sshin#include <sys/vnode.h> 65166952Sbms 66160787Syar#include <net/if.h> 6754263Sshin#include <net/bpf.h> 68160373Sjulian#include <net/bpfdesc.h> 6954263Sshin 7054263Sshin#include <netinet/in.h> 7154263Sshin#include <netinet/if_ether.h> 7254263Sshin#include <sys/kernel.h> 7354263Sshin#include <sys/sysctl.h> 7454263Sshin 75132671Scharnierstatic MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); 7654263Sshin 7754263Sshin#if defined(DEV_BPF) || defined(NETGRAPH_BPF) 7854263Sshin 79102975Sdwmalone#define PRINET 26 /* interruptible */ 8054263Sshin 8154263Sshin/* 8254263Sshin * The default read buffer size is patchable. 8354263Sshin */ 8454263Sshinstatic int bpf_bufsize = 4096; 8554263SshinSYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW, 8654263Sshin &bpf_bufsize, 0, ""); 8754263Sshinstatic int bpf_maxbufsize = BPF_MAXBUFSIZE; 8854263SshinSYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW, 8954263Sshin &bpf_maxbufsize, 0, ""); 9054263Sshin 9154263Sshin/* 9254263Sshin * bpf_iflist is the list of interfaces; each corresponds to an ifnet 9354263Sshin */ 9454263Sshinstatic struct bpf_if *bpf_iflist; 9554263Sshinstatic struct mtx bpf_mtx; /* bpf global lock */ 9654263Sshin 9754263Sshinstatic int bpf_allocbufs(struct bpf_d *); 9854263Sshinstatic void bpf_attachd(struct bpf_d *d, struct bpf_if *bp); 99175061Sobrienstatic void bpf_detachd(struct bpf_d *d); 10054263Sshinstatic void bpf_freed(struct bpf_d *); 10154263Sshinstatic void bpf_mcopy(const void *, void *, size_t); 10254263Sshinstatic int bpf_movein(struct uio *, int, 10354263Sshin struct mbuf **, struct sockaddr *, int *); 10454263Sshinstatic int bpf_setif(struct bpf_d *, struct ifreq *); 10554263Sshinstatic void bpf_timed_out(void *); 10654263Sshinstatic __inline void 10754263Sshin bpf_wakeup(struct bpf_d *); 10854263Sshinstatic void catchpacket(struct bpf_d *, u_char *, u_int, 109175061Sobrien u_int, void (*)(const void *, void *, size_t)); 11054263Sshinstatic void reset_d(struct bpf_d *); 11154263Sshinstatic int bpf_setf(struct bpf_d *, struct bpf_program *); 11254263Sshin 11354263Sshinstatic d_open_t bpfopen; 11454263Sshinstatic d_close_t bpfclose; 11554263Sshinstatic d_read_t bpfread; 11654263Sshinstatic d_write_t bpfwrite; 11754263Sshinstatic d_ioctl_t bpfioctl; 11854263Sshinstatic d_poll_t bpfpoll; 119175061Sobrien 12054263Sshin#define CDEV_MAJOR 23 12154263Sshinstatic struct cdevsw bpf_cdevsw = { 12254263Sshin /* open */ bpfopen, 12354263Sshin /* close */ bpfclose, 12454263Sshin /* read */ bpfread, 12554263Sshin /* write */ bpfwrite, 12654263Sshin /* ioctl */ bpfioctl, 12754263Sshin /* poll */ bpfpoll, 12854263Sshin /* mmap */ nommap, 129175061Sobrien /* strategy */ nostrategy, 13054263Sshin /* name */ "bpf", 13154263Sshin /* maj */ CDEV_MAJOR, 13254263Sshin /* dump */ nodump, 13354263Sshin /* psize */ nopsize, 13454263Sshin /* flags */ 0, 13554263Sshin}; 13654263Sshin 13754263Sshin 13854263Sshinstatic int 139175061Sobrienbpf_movein(uio, linktype, mp, sockp, datlen) 14054263Sshin register struct uio *uio; 14154263Sshin int linktype, *datlen; 142125482Sume register struct mbuf **mp; 14354263Sshin register struct sockaddr *sockp; 14454263Sshin{ 14554263Sshin struct mbuf *m; 14654263Sshin int error; 14754263Sshin int len; 14854263Sshin int hlen; 149175061Sobrien 15054263Sshin /* 15154263Sshin * Build a sockaddr based on the data link layer type. 15254263Sshin * We do this at this level because the ethernet header 15354263Sshin * is copied directly into the data field of the sockaddr. 15454263Sshin * In the case of SLIP, there is no header and the packet 15554263Sshin * is forwarded as is. 15654263Sshin * Also, we are careful to leave room at the front of the mbuf 15754263Sshin * for the link level header. 15854263Sshin */ 159175061Sobrien switch (linktype) { 16054263Sshin 16154263Sshin case DLT_SLIP: 16254263Sshin sockp->sa_family = AF_INET; 16354263Sshin hlen = 0; 16454263Sshin break; 16554263Sshin 16654263Sshin case DLT_EN10MB: 16754263Sshin sockp->sa_family = AF_UNSPEC; 16854263Sshin /* XXX Would MAXLINKHDR be better? */ 169175061Sobrien hlen = sizeof(struct ether_header); 17054263Sshin break; 17154263Sshin 17254263Sshin case DLT_FDDI: 17354263Sshin sockp->sa_family = AF_IMPLINK; 17454263Sshin hlen = 0; 17554263Sshin break; 17654263Sshin 17754263Sshin case DLT_RAW: 17854263Sshin case DLT_NULL: 179175061Sobrien sockp->sa_family = AF_UNSPEC; 18054263Sshin hlen = 0; 18154263Sshin break; 18254263Sshin 18354263Sshin case DLT_ATM_RFC1483: 18454263Sshin /* 18554263Sshin * en atm driver requires 4-byte atm pseudo header. 18654263Sshin * though it isn't standard, vpi:vci needs to be 18754263Sshin * specified anyway. 18854263Sshin */ 189175061Sobrien sockp->sa_family = AF_UNSPEC; 19054263Sshin hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 19154263Sshin break; 19254263Sshin 19354263Sshin case DLT_PPP: 19454263Sshin sockp->sa_family = AF_UNSPEC; 19554263Sshin hlen = 4; /* This should match PPP_HDRLEN */ 19654263Sshin break; 19754263Sshin 19854263Sshin default: 199175061Sobrien return (EIO); 20054263Sshin } 20154263Sshin 20254263Sshin len = uio->uio_resid; 20354263Sshin *datlen = len - hlen; 20454263Sshin if ((unsigned)len > MCLBYTES) 20554263Sshin return (EIO); 20654263Sshin 20754263Sshin MGETHDR(m, M_TRYWAIT, MT_DATA); 20854263Sshin if (m == 0) 209175061Sobrien return (ENOBUFS); 21054263Sshin if (len > MHLEN) { 21154263Sshin MCLGET(m, M_TRYWAIT); 21254263Sshin if ((m->m_flags & M_EXT) == 0) { 21354263Sshin error = ENOBUFS; 21454263Sshin goto bad; 21554263Sshin } 21654263Sshin } 21754263Sshin m->m_pkthdr.len = m->m_len = len; 21854263Sshin m->m_pkthdr.rcvif = NULL; 219175061Sobrien *mp = m; 22054263Sshin /* 22154263Sshin * Make room for link header. 22254263Sshin */ 22354263Sshin if (hlen != 0) { 22454263Sshin m->m_pkthdr.len -= hlen; 22554263Sshin m->m_len -= hlen; 22654263Sshin#if BSD >= 199103 22754263Sshin m->m_data += hlen; /* XXX */ 22854263Sshin#else 229175061Sobrien m->m_off += hlen; 23054263Sshin#endif 23154263Sshin error = uiomove((caddr_t)sockp->sa_data, hlen, uio); 23254263Sshin if (error) 23354263Sshin goto bad; 23454263Sshin } 23554263Sshin error = uiomove(mtod(m, caddr_t), len - hlen, uio); 23654263Sshin if (!error) 23754263Sshin return (0); 23854263Sshin bad: 239175061Sobrien m_freem(m); 24054263Sshin return (error); 24154263Sshin} 24254263Sshin 24354263Sshin/* 24454263Sshin * Attach file to the bpf interface, i.e. make d listen on bp. 24554263Sshin */ 24654263Sshinstatic void 24754263Sshinbpf_attachd(d, bp) 24854263Sshin struct bpf_d *d; 249175061Sobrien struct bpf_if *bp; 25054263Sshin{ 25154263Sshin /* 25254263Sshin * Point d at bp, and add d to the interface's list of listeners. 25354263Sshin * Finally, point the driver's bpf cookie at the interface so 25454263Sshin * it will divert packets to bpf. 25554263Sshin */ 25654263Sshin BPFIF_LOCK(bp); 25754263Sshin d->bd_bif = bp; 25854263Sshin d->bd_next = bp->bif_dlist; 259175061Sobrien bp->bif_dlist = d; 26054263Sshin 26154263Sshin bp->bif_ifp->if_bpf = bp; 26254263Sshin BPFIF_UNLOCK(bp); 26354263Sshin} 26454263Sshin 26554263Sshin/* 26654263Sshin * Detach a file from its interface. 26754263Sshin */ 26854263Sshinstatic void 269175061Sobrienbpf_detachd(d) 27054263Sshin struct bpf_d *d; 27154263Sshin{ 27254263Sshin int error; 27354263Sshin struct bpf_d **p; 27454263Sshin struct bpf_if *bp; 27554263Sshin 27654263Sshin bp = d->bd_bif; 27754263Sshin /* 27854263Sshin * Check if this descriptor had requested promiscuous mode. 279175061Sobrien * If so, turn it off. 28054263Sshin */ 28154263Sshin if (d->bd_promisc) { 28254263Sshin d->bd_promisc = 0; 28354263Sshin error = ifpromisc(bp->bif_ifp, 0); 28454263Sshin if (error != 0 && error != ENXIO) { 28554263Sshin /* 28654263Sshin * ENXIO can happen if a pccard is unplugged 28754263Sshin * Something is really wrong if we were able to put 28854263Sshin * the driver into promiscuous mode, but can't 289175061Sobrien * take it out. 29054263Sshin */ 29154263Sshin printf("%s%d: ifpromisc failed %d\n", 29254263Sshin bp->bif_ifp->if_name, bp->bif_ifp->if_unit, error); 29354263Sshin } 29454263Sshin } 29554263Sshin /* Remove d from the interface's descriptor list. */ 29654263Sshin BPFIF_LOCK(bp); 29754263Sshin p = &bp->bif_dlist; 29854263Sshin while (*p != d) { 299175061Sobrien p = &(*p)->bd_next; 30054263Sshin if (*p == 0) 30154263Sshin panic("bpf_detachd: descriptor not in list"); 30254263Sshin } 30354263Sshin *p = (*p)->bd_next; 30454263Sshin if (bp->bif_dlist == 0) 30554263Sshin /* 30654263Sshin * Let the driver know that there are no more listeners. 30754263Sshin */ 30854263Sshin d->bd_bif->bif_ifp->if_bpf = 0; 309175061Sobrien BPFIF_UNLOCK(bp); 31054263Sshin d->bd_bif = 0; 31154263Sshin} 31254263Sshin 31354263Sshin/* 31454263Sshin * Open ethernet device. Returns ENXIO for illegal minor device number, 31554263Sshin * EBUSY if file is open by another process. 31654263Sshin */ 31754263Sshin/* ARGSUSED */ 31854263Sshinstatic int 319175061Sobrienbpfopen(dev, flags, fmt, td) 32054263Sshin dev_t dev; 32154263Sshin int flags; 32254263Sshin int fmt; 32354263Sshin struct thread *td; 32454263Sshin{ 32554263Sshin struct bpf_d *d; 32654263Sshin 32754263Sshin mtx_lock(&bpf_mtx); 32854263Sshin d = dev->si_drv1; 329175061Sobrien /* 33054263Sshin * Each minor can be opened by only one process. If the requested 33154263Sshin * minor is in use, return EBUSY. 33254263Sshin */ 33354263Sshin if (d) { 33454263Sshin mtx_unlock(&bpf_mtx); 33554263Sshin return (EBUSY); 33654263Sshin } 33754263Sshin dev->si_drv1 = (struct bpf_d *)~0; /* mark device in use */ 338125483Sume mtx_unlock(&bpf_mtx); 339125483Sume 340125483Sume if ((dev->si_flags & SI_NAMED) == 0) 341125483Sume make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600, 342125483Sume "bpf%d", dev2unit(dev)); 343125483Sume MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO); 344125483Sume dev->si_drv1 = d; 345125483Sume d->bd_bufsize = bpf_bufsize; 346125483Sume d->bd_sig = SIGIO; 347125483Sume d->bd_seesent = 1; 348125483Sume#ifdef MAC 349125483Sume mac_init_bpfdesc(d); 350125483Sume mac_create_bpfdesc(td->td_ucred, d); 351125483Sume#endif 352125483Sume mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF); 353125483Sume callout_init(&d->bd_callout, 1); 354125483Sume 355125483Sume return (0); 356125483Sume} 35754263Sshin 35854263Sshin/* 35954263Sshin * Close the descriptor by detaching it from its interface, 36054263Sshin * deallocating its buffers, and marking it free. 361171465Sjhb */ 36254263Sshin/* ARGSUSED */ 363228700Smaximstatic int 36454263Sshinbpfclose(dev, flags, fmt, td) 36578931Sume dev_t dev; 36654263Sshin int flags; 367171465Sjhb int fmt; 368171465Sjhb struct thread *td; 369171465Sjhb{ 370228700Smaxim struct bpf_d *d = dev->si_drv1; 371228700Smaxim 372228700Smaxim BPFD_LOCK(d); 373228700Smaxim if (d->bd_state == BPF_WAITING) 374171465Sjhb callout_stop(&d->bd_callout); 375171465Sjhb d->bd_state = BPF_IDLE; 376171465Sjhb BPFD_UNLOCK(d); 377171465Sjhb funsetown(&d->bd_sigio); 378171465Sjhb mtx_lock(&bpf_mtx); 379171465Sjhb if (d->bd_bif) 38054263Sshin bpf_detachd(d); 38154263Sshin mtx_unlock(&bpf_mtx); 38254263Sshin#ifdef MAC 38354263Sshin mac_destroy_bpfdesc(d); 384160787Syar#endif /* MAC */ 38554263Sshin bpf_freed(d); 386160787Syar dev->si_drv1 = 0; 38754263Sshin free(d, M_BPF); 388160787Syar 389160787Syar return (0); 390160787Syar} 391160787Syar 392160787Syar 393160787Syar/* 394160787Syar * Rotate the packet buffers in descriptor d. Move the store buffer 395160787Syar * into the hold slot, and the free buffer into the store slot. 396160787Syar * Zero the length of the new store buffer. 397160787Syar */ 398160787Syar#define ROTATE_BUFFERS(d) \ 399160787Syar (d)->bd_hbuf = (d)->bd_sbuf; \ 400160787Syar (d)->bd_hlen = (d)->bd_slen; \ 401160787Syar (d)->bd_sbuf = (d)->bd_fbuf; \ 402160787Syar (d)->bd_slen = 0; \ 403160787Syar (d)->bd_fbuf = 0; 404160787Syar/* 405160787Syar * bpfread - read next chunk of packets from buffers 406160787Syar */ 407160787Syarstatic int 408160787Syarbpfread(dev, uio, ioflag) 409160787Syar dev_t dev; 410160787Syar register struct uio *uio; 41154263Sshin int ioflag; 41254263Sshin{ 41354263Sshin struct bpf_d *d = dev->si_drv1; 41454263Sshin int timed_out; 41554263Sshin int error; 41654263Sshin 417160787Syar /* 418160787Syar * Restrict application to use a buffer the same size as 41954263Sshin * as kernel buffers. 42054263Sshin */ 421160787Syar if (uio->uio_resid != d->bd_bufsize) 42254263Sshin return (EINVAL); 42355163Sshin 424175061Sobrien BPFD_LOCK(d); 42554263Sshin if (d->bd_state == BPF_WAITING) 42654263Sshin callout_stop(&d->bd_callout); 42754263Sshin timed_out = (d->bd_state == BPF_TIMED_OUT); 42854263Sshin d->bd_state = BPF_IDLE; 429160787Syar /* 43062584Sitojun * If the hold buffer is empty, then do a timed sleep, which 431160787Syar * ends when the timeout expires or when enough packets 43254263Sshin * have arrived to fill the store buffer. 43354263Sshin */ 434160787Syar while (d->bd_hbuf == 0) { 435160787Syar if ((d->bd_immediate || timed_out) && d->bd_slen != 0) { 436160787Syar /* 437175061Sobrien * A packet(s) either arrived since the previous 43862584Sitojun * read or arrived while we were asleep. 439215955Sbrucec * Rotate the buffers and return what's here. 440160787Syar */ 44162584Sitojun ROTATE_BUFFERS(d); 442160787Syar break; 44362584Sitojun } 44462584Sitojun 445175061Sobrien /* 44662584Sitojun * No data is available, check to see if the bpf device 44762584Sitojun * is still pointed at a real interface. If not, return 448160787Syar * ENXIO so that the userland process knows to rebind 44962584Sitojun * it before using it again. 45062584Sitojun */ 451160787Syar if (d->bd_bif == NULL) { 45262584Sitojun BPFD_UNLOCK(d); 45362584Sitojun return (ENXIO); 454160787Syar } 45562584Sitojun 45662584Sitojun if (ioflag & IO_NDELAY) { 457160787Syar BPFD_UNLOCK(d); 45862584Sitojun return (EWOULDBLOCK); 45962584Sitojun } 460160787Syar error = msleep((caddr_t)d, &d->bd_mtx, PRINET|PCATCH, 461160787Syar "bpf", d->bd_rtout); 46262584Sitojun if (error == EINTR || error == ERESTART) { 46362584Sitojun BPFD_UNLOCK(d); 46462584Sitojun return (error); 46562584Sitojun } 466160787Syar if (error == EWOULDBLOCK) { 46762584Sitojun /* 46862584Sitojun * On a timeout, return what's in the buffer, 46962584Sitojun * which may be nothing. If there is something 47062584Sitojun * in the store buffer, we can rotate the buffers. 47162584Sitojun */ 47262584Sitojun if (d->bd_hbuf) 47362584Sitojun /* 47462584Sitojun * We filled up the buffer in between 47562584Sitojun * getting the timeout and arriving 47662584Sitojun * here, so we don't need to rotate. 47762584Sitojun */ 47862584Sitojun break; 47962584Sitojun 48062584Sitojun if (d->bd_slen == 0) { 48162584Sitojun BPFD_UNLOCK(d); 48262584Sitojun return (0); 48362584Sitojun } 48462584Sitojun ROTATE_BUFFERS(d); 48562584Sitojun break; 48662584Sitojun } 48762584Sitojun } 48862584Sitojun /* 48962584Sitojun * At this point, we know we have something in the hold slot. 49062584Sitojun */ 49162584Sitojun BPFD_UNLOCK(d); 49262584Sitojun 49362584Sitojun /* 49462584Sitojun * Move data from hold buffer into user space. 49562584Sitojun * We know the entire buffer is transferred since 49662584Sitojun * we checked above that the read buffer is bpf_bufsize bytes. 49762584Sitojun */ 49862584Sitojun error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 49962584Sitojun 50062584Sitojun BPFD_LOCK(d); 50162584Sitojun d->bd_fbuf = d->bd_hbuf; 50262584Sitojun d->bd_hbuf = 0; 50362584Sitojun d->bd_hlen = 0; 50462584Sitojun BPFD_UNLOCK(d); 50562584Sitojun 50662584Sitojun return (error); 50762584Sitojun} 50862584Sitojun 50962584Sitojun 51062584Sitojun/* 51162584Sitojun * If there are processes sleeping on this descriptor, wake them up. 51262584Sitojun */ 513125483Sumestatic __inline void 514125483Sumebpf_wakeup(d) 515125483Sume register struct bpf_d *d; 516160787Syar{ 517160787Syar if (d->bd_state == BPF_WAITING) { 518125483Sume callout_stop(&d->bd_callout); 519125483Sume d->bd_state = BPF_IDLE; 52054263Sshin } 52162584Sitojun wakeup((caddr_t)d); 52254263Sshin if (d->bd_async && d->bd_sig && d->bd_sigio) 52354263Sshin pgsigio(&d->bd_sigio, d->bd_sig, 0); 52454263Sshin 52554263Sshin selwakeup(&d->bd_sel); 52654263Sshin} 52754263Sshin 52878314Sassarstatic void 52954263Sshinbpf_timed_out(arg) 53054263Sshin void *arg; 53154263Sshin{ 53254263Sshin struct bpf_d *d = (struct bpf_d *)arg; 533160787Syar 53454263Sshin BPFD_LOCK(d); 535160787Syar if (d->bd_state == BPF_WAITING) { 53654263Sshin d->bd_state = BPF_TIMED_OUT; 53754263Sshin if (d->bd_slen != 0) 53854263Sshin bpf_wakeup(d); 53954263Sshin } 54054263Sshin BPFD_UNLOCK(d); 54154263Sshin} 54254263Sshin 54354263Sshinstatic int 54454263Sshinbpfwrite(dev, uio, ioflag) 54554263Sshin dev_t dev; 54654263Sshin struct uio *uio; 54754263Sshin int ioflag; 54854263Sshin{ 54954263Sshin struct bpf_d *d = dev->si_drv1; 550160787Syar struct ifnet *ifp; 551160787Syar struct mbuf *m; 552160787Syar int error; 553160787Syar static struct sockaddr dst; 554160787Syar int datlen; 555160787Syar 556160787Syar if (d->bd_bif == 0) 557160787Syar return (ENXIO); 55854263Sshin 559160787Syar ifp = d->bd_bif->bif_ifp; 560160787Syar 56154263Sshin if (uio->uio_resid == 0) 562160787Syar return (0); 563160787Syar 564160787Syar error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen); 565160787Syar if (error) 566160787Syar return (error); 567160787Syar 568160787Syar if (datlen > ifp->if_mtu) 569160787Syar return (EMSGSIZE); 570160787Syar 571160787Syar if (d->bd_hdrcmplt) 57254263Sshin dst.sa_family = pseudo_AF_HDRCMPLT; 57354263Sshin 57454263Sshin mtx_lock(&Giant); 57554263Sshin#ifdef MAC 57654263Sshin mac_create_mbuf_from_bpfdesc(d, m); 57754263Sshin#endif 57854263Sshin error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0); 57954263Sshin mtx_unlock(&Giant); 580102975Sdwmalone /* 58154263Sshin * The driver frees the mbuf. 58254263Sshin */ 58354263Sshin return (error); 58454263Sshin} 58554263Sshin 58654263Sshin/* 58754263Sshin * Reset a descriptor by flushing its packet buffer and clearing the 58854263Sshin * receive and drop counts. 58954263Sshin */ 59054263Sshinstatic void 59154263Sshinreset_d(d) 59254263Sshin struct bpf_d *d; 59354263Sshin{ 59454263Sshin 59554263Sshin mtx_assert(&d->bd_mtx, MA_OWNED); 59654263Sshin if (d->bd_hbuf) { 59754263Sshin /* Free the hold buffer. */ 59854263Sshin d->bd_fbuf = d->bd_hbuf; 59954263Sshin d->bd_hbuf = 0; 600175061Sobrien } 60154263Sshin d->bd_slen = 0; 60254263Sshin d->bd_hlen = 0; 60354263Sshin d->bd_rcount = 0; 60454263Sshin d->bd_dcount = 0; 60554263Sshin} 60654263Sshin 60754263Sshin/* 60854263Sshin * FIONREAD Check for read packet available. 60954263Sshin * SIOCGIFADDR Get interface address - convenient hook to driver. 610175061Sobrien * BIOCGBLEN Get buffer len [for read()]. 61154263Sshin * BIOCSETF Set ethernet read filter. 61254263Sshin * BIOCFLUSH Flush read packet buffer. 61354263Sshin * BIOCPROMISC Put interface into promiscuous mode. 61454263Sshin * BIOCGDLT Get link layer type. 61554263Sshin * BIOCGETIF Get interface name. 61654263Sshin * BIOCSETIF Set interface. 61754263Sshin * BIOCSRTIMEOUT Set read timeout. 61854263Sshin * BIOCGRTIMEOUT Get read timeout. 61954263Sshin * BIOCGSTATS Get packet stats. 620175061Sobrien * BIOCIMMEDIATE Set immediate mode. 62154263Sshin * BIOCVERSION Get filter language version. 62254263Sshin * BIOCGHDRCMPLT Get "header already complete" flag 62354263Sshin * BIOCSHDRCMPLT Set "header already complete" flag 62454263Sshin * BIOCGSEESENT Get "see packets sent" flag 62554263Sshin * BIOCSSEESENT Set "see packets sent" flag 62654263Sshin */ 62754263Sshin/* ARGSUSED */ 62854263Sshinstatic int 62954263Sshinbpfioctl(dev, cmd, addr, flags, td) 630175061Sobrien dev_t dev; 63154263Sshin u_long cmd; 63254263Sshin caddr_t addr; 63354263Sshin int flags; 63454263Sshin struct thread *td; 63554263Sshin{ 63654263Sshin struct bpf_d *d = dev->si_drv1; 63754263Sshin int error = 0; 63854263Sshin 63954263Sshin BPFD_LOCK(d); 640175061Sobrien if (d->bd_state == BPF_WAITING) 64154263Sshin callout_stop(&d->bd_callout); 64254263Sshin d->bd_state = BPF_IDLE; 64354263Sshin BPFD_UNLOCK(d); 64454263Sshin 64554263Sshin switch (cmd) { 64654263Sshin 64754263Sshin default: 64854263Sshin error = EINVAL; 64954263Sshin break; 650175061Sobrien 65154263Sshin /* 65254263Sshin * Check for read packet available. 65354263Sshin */ 65454263Sshin case FIONREAD: 65554263Sshin { 65654263Sshin int n; 65754263Sshin 65854263Sshin BPFD_LOCK(d); 65954263Sshin n = d->bd_slen; 660175061Sobrien if (d->bd_hbuf) 66154263Sshin n += d->bd_hlen; 66254263Sshin BPFD_UNLOCK(d); 66354263Sshin 66454263Sshin *(int *)addr = n; 66554263Sshin break; 66654263Sshin } 66754263Sshin 66854263Sshin case SIOCGIFADDR: 66954263Sshin { 670175061Sobrien struct ifnet *ifp; 67154263Sshin 67254263Sshin if (d->bd_bif == 0) 67354263Sshin error = EINVAL; 67454263Sshin else { 67554263Sshin ifp = d->bd_bif->bif_ifp; 67654263Sshin error = (*ifp->if_ioctl)(ifp, cmd, addr); 67754263Sshin } 67854263Sshin break; 67954263Sshin } 680175061Sobrien 68154263Sshin /* 68254263Sshin * Get buffer len [for read()]. 68354263Sshin */ 68454263Sshin case BIOCGBLEN: 68554263Sshin *(u_int *)addr = d->bd_bufsize; 68654263Sshin break; 68754263Sshin 68854263Sshin /* 68954263Sshin * Set buffer length. 690175061Sobrien */ 69154263Sshin case BIOCSBLEN: 69254263Sshin if (d->bd_bif != 0) 69354263Sshin error = EINVAL; 69454263Sshin else { 69554263Sshin register u_int size = *(u_int *)addr; 69654263Sshin 69754263Sshin if (size > bpf_maxbufsize) 69854263Sshin *(u_int *)addr = size = bpf_maxbufsize; 69954263Sshin else if (size < BPF_MINBUFSIZE) 700175061Sobrien *(u_int *)addr = size = BPF_MINBUFSIZE; 70154263Sshin d->bd_bufsize = size; 70254263Sshin } 70354263Sshin break; 70454263Sshin 70554263Sshin /* 70654263Sshin * Set link layer read filter. 70754263Sshin */ 70854263Sshin case BIOCSETF: 70954263Sshin error = bpf_setf(d, (struct bpf_program *)addr); 710175061Sobrien break; 71154263Sshin 712191652Sbms /* 713191652Sbms * Flush read packet buffer. 71454263Sshin */ 71577565Sdd case BIOCFLUSH: 71654263Sshin BPFD_LOCK(d); 71777565Sdd reset_d(d); 71854263Sshin BPFD_UNLOCK(d); 71954263Sshin break; 72054263Sshin 72154263Sshin /* 72278540Ssumikawa * Put interface into promiscuous mode. 72378540Ssumikawa */ 724191652Sbms case BIOCPROMISC: 72554263Sshin if (d->bd_bif == 0) { 72654263Sshin /* 72754263Sshin * No interface attached yet. 72854263Sshin */ 72954263Sshin error = EINVAL; 730175061Sobrien break; 73154263Sshin } 73254263Sshin if (d->bd_promisc == 0) { 73354263Sshin mtx_lock(&Giant); 73454263Sshin error = ifpromisc(d->bd_bif->bif_ifp, 1); 73554263Sshin mtx_unlock(&Giant); 73654263Sshin if (error == 0) 73754263Sshin d->bd_promisc = 1; 73854263Sshin } 73954263Sshin break; 740175061Sobrien 74154263Sshin /* 74254263Sshin * Get device parameters. 74354263Sshin */ 74454263Sshin case BIOCGDLT: 74554263Sshin if (d->bd_bif == 0) 74654263Sshin error = EINVAL; 74754263Sshin else 74854263Sshin *(u_int *)addr = d->bd_bif->bif_dlt; 74954263Sshin break; 750175061Sobrien 75154263Sshin /* 75254263Sshin * Get interface name. 75354263Sshin */ 75454263Sshin case BIOCGETIF: 75554263Sshin if (d->bd_bif == 0) 75654263Sshin error = EINVAL; 75754263Sshin else { 75854263Sshin struct ifnet *const ifp = d->bd_bif->bif_ifp; 75954263Sshin struct ifreq *const ifr = (struct ifreq *)addr; 760175061Sobrien 76154263Sshin snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), 76254263Sshin "%s%d", ifp->if_name, ifp->if_unit); 76354263Sshin } 76454263Sshin break; 76554263Sshin 76654263Sshin /* 76754263Sshin * Set interface. 76854263Sshin */ 76954263Sshin case BIOCSETIF: 770175061Sobrien error = bpf_setif(d, (struct ifreq *)addr); 77154263Sshin break; 77254263Sshin 77354263Sshin /* 77454263Sshin * Set read timeout. 77554263Sshin */ 77654263Sshin case BIOCSRTIMEOUT: 77754263Sshin { 77854263Sshin struct timeval *tv = (struct timeval *)addr; 77954263Sshin 780175061Sobrien /* 78154263Sshin * Subtract 1 tick from tvtohz() since this isn't 78254263Sshin * a one-shot timer. 78354263Sshin */ 78454263Sshin if ((error = itimerfix(tv)) == 0) 78554263Sshin d->bd_rtout = tvtohz(tv) - 1; 78654263Sshin break; 78754263Sshin } 78854263Sshin 78954263Sshin /* 790175061Sobrien * Get read timeout. 79154263Sshin */ 79254263Sshin case BIOCGRTIMEOUT: 79354263Sshin { 79454263Sshin struct timeval *tv = (struct timeval *)addr; 79554263Sshin 79654263Sshin tv->tv_sec = d->bd_rtout / hz; 79754263Sshin tv->tv_usec = (d->bd_rtout % hz) * tick; 79854263Sshin break; 79954263Sshin } 800175061Sobrien 80154263Sshin /* 80254263Sshin * Get packet stats. 80354263Sshin */ 80454263Sshin case BIOCGSTATS: 80554263Sshin { 80654263Sshin struct bpf_stat *bs = (struct bpf_stat *)addr; 80754263Sshin 80854263Sshin bs->bs_recv = d->bd_rcount; 80954263Sshin bs->bs_drop = d->bd_dcount; 810175061Sobrien break; 81154263Sshin } 81254263Sshin 81354263Sshin /* 81454263Sshin * Set immediate mode. 81554263Sshin */ 81654263Sshin case BIOCIMMEDIATE: 81754263Sshin d->bd_immediate = *(u_int *)addr; 81854263Sshin break; 81954263Sshin 820175061Sobrien case BIOCVERSION: 82154263Sshin { 82254263Sshin struct bpf_version *bv = (struct bpf_version *)addr; 82354263Sshin 82454263Sshin bv->bv_major = BPF_MAJOR_VERSION; 82554263Sshin bv->bv_minor = BPF_MINOR_VERSION; 82654263Sshin break; 82754263Sshin } 82854263Sshin 82954263Sshin /* 830175061Sobrien * Get "header already complete" flag 83154263Sshin */ 83254263Sshin case BIOCGHDRCMPLT: 83354263Sshin *(u_int *)addr = d->bd_hdrcmplt; 83454263Sshin break; 83554263Sshin 83654263Sshin /* 83754263Sshin * Set "header already complete" flag 83854263Sshin */ 83954263Sshin case BIOCSHDRCMPLT: 84054263Sshin d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 84154263Sshin break; 84254263Sshin 843171465Sjhb /* 84454263Sshin * Get "see sent packets" flag 845228700Smaxim */ 84695637Smarkm case BIOCGSEESENT: 84778931Sume *(u_int *)addr = d->bd_seesent; 84854263Sshin break; 849171465Sjhb 850171465Sjhb /* 851171465Sjhb * Set "see sent packets" flag 852228700Smaxim */ 853228700Smaxim case BIOCSSEESENT: 854171465Sjhb d->bd_seesent = *(u_int *)addr; 855228700Smaxim break; 856171465Sjhb 857171465Sjhb case FIONBIO: /* Non-blocking I/O */ 858171465Sjhb break; 859171465Sjhb 860171465Sjhb case FIOASYNC: /* Send signal on receive packets */ 861171465Sjhb d->bd_async = *(int *)addr; 86278931Sume break; 86354263Sshin 86454263Sshin case FIOSETOWN: 86554263Sshin error = fsetown(*(int *)addr, &d->bd_sigio); 866160787Syar break; 867187134Smaxim 868187134Smaxim case FIOGETOWN: 86954263Sshin *(int *)addr = fgetown(d->bd_sigio); 870160787Syar break; 87154263Sshin 872160787Syar /* This is deprecated, FIOSETOWN should be used instead. */ 87354263Sshin case TIOCSPGRP: 874160787Syar error = fsetown(-(*(int *)addr), &d->bd_sigio); 875175061Sobrien break; 87678540Ssumikawa 87754263Sshin /* This is deprecated, FIOGETOWN should be used instead. */ 87854263Sshin case TIOCGPGRP: 87954263Sshin *(int *)addr = -fgetown(d->bd_sigio); 88054263Sshin break; 88154263Sshin 882160787Syar case BIOCSRSIG: /* Set receive signal */ 883160787Syar { 88454263Sshin u_int sig; 88578540Ssumikawa 886160787Syar sig = *(u_int *)addr; 887160787Syar 888160787Syar if (sig >= NSIG) 889160787Syar error = EINVAL; 890175061Sobrien else 89178540Ssumikawa d->bd_sig = sig; 89254263Sshin break; 89354263Sshin } 89454263Sshin case BIOCGRSIG: 89554263Sshin *(u_int *)addr = d->bd_sig; 89654263Sshin break; 897160787Syar } 898160787Syar return (error); 89954263Sshin} 90078540Ssumikawa 90177565Sdd/* 902160787Syar * Set d's packet filter program to fp. If this file already has a filter, 903160787Syar * free it and replace it. Returns EINVAL for bogus requests. 904160787Syar */ 905160787Syarstatic int 906160787Syarbpf_setf(d, fp) 907160787Syar struct bpf_d *d; 908160787Syar struct bpf_program *fp; 909160787Syar{ 910160787Syar struct bpf_insn *fcode, *old; 911160787Syar u_int flen, size; 912160787Syar 913160787Syar old = d->bd_filter; 914160787Syar if (fp->bf_insns == 0) { 91562584Sitojun if (fp->bf_len != 0) 916160787Syar return (EINVAL); 917160787Syar BPFD_LOCK(d); 918160787Syar d->bd_filter = 0; 919160787Syar reset_d(d); 920160787Syar BPFD_UNLOCK(d); 921160787Syar if (old != 0) 922160787Syar free((caddr_t)old, M_BPF); 923160787Syar return (0); 924160787Syar } 92554263Sshin flen = fp->bf_len; 92654263Sshin if (flen > BPF_MAXINSNS) 92754263Sshin return (EINVAL); 92854263Sshin 92954263Sshin size = flen * sizeof(*fp->bf_insns); 93054263Sshin fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK); 93154263Sshin if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && 93254263Sshin bpf_validate(fcode, (int)flen)) { 93378314Sassar BPFD_LOCK(d); 93454263Sshin d->bd_filter = fcode; 93554263Sshin reset_d(d); 93654263Sshin BPFD_UNLOCK(d); 93754263Sshin if (old != 0) 938160787Syar free((caddr_t)old, M_BPF); 939109234Smtm 940160787Syar return (0); 94154263Sshin } 94254263Sshin free((caddr_t)fcode, M_BPF); 94354263Sshin return (EINVAL); 94454263Sshin} 94554263Sshin 94654263Sshin/* 94754263Sshin * Detach a file from its current interface (if attached at all) and attach 94854263Sshin * to the interface indicated by the name stored in ifr. 94954263Sshin * Return an errno or 0. 95054263Sshin */ 95154263Sshinstatic int 95254263Sshinbpf_setif(d, ifr) 95354263Sshin struct bpf_d *d; 95454263Sshin struct ifreq *ifr; 955160787Syar{ 956175061Sobrien struct bpf_if *bp; 957160787Syar int error; 958160787Syar struct ifnet *theywant; 959160787Syar 960160787Syar theywant = ifunit(ifr->ifr_name); 961160787Syar if (theywant == 0) 962160787Syar return ENXIO; 963160787Syar 964160787Syar /* 965160787Syar * Look through attached interfaces for the named one. 966160787Syar */ 967160787Syar mtx_lock(&bpf_mtx); 968160787Syar for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) { 969160787Syar struct ifnet *ifp = bp->bif_ifp; 970160787Syar 971160787Syar if (ifp == 0 || ifp != theywant) 97254263Sshin continue; 973160787Syar 974160787Syar mtx_unlock(&bpf_mtx); 975160787Syar /* 976160787Syar * We found the requested interface. 977160787Syar * If it's not up, return an error. 978160787Syar * Allocate the packet buffers if we need to. 979160787Syar * If we're already attached to requested interface, 980160787Syar * just flush the buffer. 981160787Syar */ 982160787Syar if ((ifp->if_flags & IFF_UP) == 0) 983160787Syar return (ENETDOWN); 984160787Syar 985160787Syar if (d->bd_sbuf == 0) { 986160787Syar error = bpf_allocbufs(d); 987160787Syar if (error != 0) 988160787Syar return (error); 989160787Syar } 99054263Sshin if (bp != d->bd_bif) { 99154263Sshin if (d->bd_bif) 99254263Sshin /* 99354263Sshin * Detach if attached to something else. 99454263Sshin */ 99554263Sshin bpf_detachd(d); 99654263Sshin 99754263Sshin bpf_attachd(d, bp); 99854263Sshin } 99954263Sshin BPFD_LOCK(d); 1000171465Sjhb reset_d(d); 100154263Sshin BPFD_UNLOCK(d); 1002166952Sbms return (0); 1003166952Sbms } 100454263Sshin mtx_unlock(&bpf_mtx); 1005171465Sjhb /* Not found. */ 1006171465Sjhb return (ENXIO); 1007171465Sjhb} 1008171465Sjhb 1009171465Sjhb/* 1010171465Sjhb * Support for select() and poll() system calls 1011171465Sjhb * 1012171465Sjhb * Return true iff the specific operation will not block indefinitely. 1013171465Sjhb * Otherwise, return false but make a note that a selwakeup() must be done. 1014171465Sjhb */ 1015171465Sjhbint 1016171465Sjhbbpfpoll(dev, events, td) 1017171465Sjhb register dev_t dev; 1018166952Sbms int events; 1019171465Sjhb struct thread *td; 102054263Sshin{ 102154263Sshin struct bpf_d *d; 102254263Sshin int revents; 1023160787Syar 1024160787Syar d = dev->si_drv1; 1025160787Syar if (d->bd_bif == NULL) 1026160787Syar return (ENXIO); 1027160787Syar 1028160787Syar revents = events & (POLLOUT | POLLWRNORM); 1029160787Syar BPFD_LOCK(d); 1030160787Syar if (events & (POLLIN | POLLRDNORM)) { 103154263Sshin /* 103254263Sshin * An imitation of the FIONREAD ioctl code. 103354263Sshin * XXX not quite. An exact imitation: 103454263Sshin * if (d->b_slen != 0 || 103578064Sume * (d->bd_hbuf != NULL && d->bd_hlen != 0) 103678064Sume */ 103778064Sume if (d->bd_hlen != 0 || 1038171465Sjhb ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) && 103978064Sume d->bd_slen != 0)) 1040228700Smaxim revents |= events & (POLLIN | POLLRDNORM); 104178064Sume else { 1042171465Sjhb selrecord(td, &d->bd_sel); 104378064Sume /* Start the read timeout if necessary. */ 1044171465Sjhb if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1045171465Sjhb callout_reset(&d->bd_callout, d->bd_rtout, 1046228700Smaxim bpf_timed_out, d); 1047228700Smaxim d->bd_state = BPF_WAITING; 1048171465Sjhb } 1049228700Smaxim } 1050171465Sjhb } 1051171465Sjhb BPFD_UNLOCK(d); 1052171465Sjhb return (revents); 1053171465Sjhb} 1054171465Sjhb 1055171465Sjhb/* 105678064Sume * Incoming linkage from device drivers. Process the packet pkt, of length 105778064Sume * pktlen, which is stored in a contiguous buffer. The packet is parsed 105878064Sume * by each process' filter, and if accepted, stashed into the corresponding 105978064Sume * buffer. 1060160787Syar */ 1061160787Syarvoid 1062186498Smaximbpf_tap(ifp, pkt, pktlen) 1063160787Syar struct ifnet *ifp; 1064160787Syar register u_char *pkt; 106578064Sume register u_int pktlen; 1066160787Syar{ 106778064Sume struct bpf_if *bp; 1068160787Syar register struct bpf_d *d; 106978064Sume register u_int slen; 107078064Sume 107178064Sume bp = ifp->if_bpf; 107278064Sume BPFIF_LOCK(bp); 107378064Sume for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 107478064Sume BPFD_LOCK(d); 1075160787Syar ++d->bd_rcount; 1076160787Syar slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen); 107778064Sume if (slen != 0) 107878064Sume catchpacket(d, pkt, pktlen, slen, bcopy); 107978064Sume BPFD_UNLOCK(d); 108078064Sume } 108154263Sshin BPFIF_UNLOCK(bp); 108278238Sassar} 108354263Sshin 1084175061Sobrien/* 108554263Sshin * Copy data from an mbuf chain into a buffer. This code is derived 108654263Sshin * from m_copydata in sys/uipc_mbuf.c. 108754263Sshin */ 108854263Sshinstatic void 108954263Sshinbpf_mcopy(src_arg, dst_arg, len) 109054263Sshin const void *src_arg; 109154263Sshin void *dst_arg; 109254263Sshin register size_t len; 109354263Sshin{ 109454263Sshin register const struct mbuf *m; 1095102975Sdwmalone register u_int count; 109654263Sshin u_char *dst; 109754263Sshin 109854263Sshin m = src_arg; 109954263Sshin dst = dst_arg; 110054263Sshin while (len > 0) { 110183200Sru if (m == 0) 110254263Sshin panic("bpf_mcopy"); 110354263Sshin count = min(m->m_len, len); 110454263Sshin bcopy(mtod(m, void *), dst, count); 110554263Sshin m = m->m_next; 110654263Sshin dst += count; 1107177352Sume len -= count; 110854263Sshin } 110954263Sshin} 111083200Sru 111155533Sshin/* 111254263Sshin * Incoming linkage from device drivers, when packet is in an mbuf chain. 111354263Sshin */ 111454263Sshinvoid 111554263Sshinbpf_mtap(ifp, m) 111678238Sassar struct ifnet *ifp; 111754263Sshin struct mbuf *m; 111854263Sshin{ 111954263Sshin struct bpf_if *bp = ifp->if_bpf; 112054263Sshin struct bpf_d *d; 112178314Sassar u_int pktlen, slen; 112254263Sshin struct mbuf *m0; 1123102975Sdwmalone 112454263Sshin pktlen = 0; 112554263Sshin for (m0 = m; m0 != 0; m0 = m0->m_next) 112674262Sbrian pktlen += m0->m_len; 112754263Sshin 112854263Sshin BPFIF_LOCK(bp); 112978238Sassar for (d = bp->bif_dlist; d != 0; d = d->bd_next) { 113054263Sshin if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL)) 113154263Sshin continue; 113254263Sshin BPFD_LOCK(d); 113354263Sshin ++d->bd_rcount; 113454263Sshin slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0); 113554263Sshin if (slen != 0) 113654263Sshin catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy); 113754263Sshin BPFD_UNLOCK(d); 113878238Sassar } 113954263Sshin BPFIF_UNLOCK(bp); 114054263Sshin} 114154263Sshin 114254263Sshin/* 114354263Sshin * Move the packet data from interface memory (pkt) into the 114454263Sshin * store buffer. Return 1 if it's time to wakeup a listener (buffer full), 114554263Sshin * otherwise 0. "copy" is the routine called to do the actual data 114654263Sshin * transfer. bcopy is passed in to copy contiguous chunks, while 114754263Sshin * bpf_mcopy is passed in to copy mbuf chains. In the latter case, 114854263Sshin * pkt is really an mbuf. 114954263Sshin */ 115054263Sshinstatic void 1151175061Sobriencatchpacket(d, pkt, pktlen, snaplen, cpfn) 115254263Sshin register struct bpf_d *d; 115354263Sshin register u_char *pkt; 115454263Sshin register u_int pktlen, snaplen; 115554263Sshin register void (*cpfn)(const void *, void *, size_t); 115654263Sshin{ 115764342Sume register struct bpf_hdr *hp; 1158 register int totlen, curlen; 1159 register int hdrlen = d->bd_bif->bif_hdrlen; 1160 /* 1161 * Figure out how many bytes to move. If the packet is 1162 * greater or equal to the snapshot length, transfer that 1163 * much. Otherwise, transfer the whole packet (unless 1164 * we hit the buffer size limit). 1165 */ 1166 totlen = hdrlen + min(snaplen, pktlen); 1167 if (totlen > d->bd_bufsize) 1168 totlen = d->bd_bufsize; 1169 1170 /* 1171 * Round up the end of the previous packet to the next longword. 1172 */ 1173 curlen = BPF_WORDALIGN(d->bd_slen); 1174 if (curlen + totlen > d->bd_bufsize) { 1175 /* 1176 * This packet will overflow the storage buffer. 1177 * Rotate the buffers if we can, then wakeup any 1178 * pending reads. 1179 */ 1180 if (d->bd_fbuf == 0) { 1181 /* 1182 * We haven't completed the previous read yet, 1183 * so drop the packet. 1184 */ 1185 ++d->bd_dcount; 1186 return; 1187 } 1188 ROTATE_BUFFERS(d); 1189 bpf_wakeup(d); 1190 curlen = 0; 1191 } 1192 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) 1193 /* 1194 * Immediate mode is set, or the read timeout has 1195 * already expired during a select call. A packet 1196 * arrived, so the reader should be woken up. 1197 */ 1198 bpf_wakeup(d); 1199 1200 /* 1201 * Append the bpf header. 1202 */ 1203 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); 1204 microtime(&hp->bh_tstamp); 1205 hp->bh_datalen = pktlen; 1206 hp->bh_hdrlen = hdrlen; 1207 /* 1208 * Copy the packet data into the store buffer and update its length. 1209 */ 1210 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); 1211 d->bd_slen = curlen + totlen; 1212} 1213 1214/* 1215 * Initialize all nonzero fields of a descriptor. 1216 */ 1217static int 1218bpf_allocbufs(d) 1219 register struct bpf_d *d; 1220{ 1221 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1222 if (d->bd_fbuf == 0) 1223 return (ENOBUFS); 1224 1225 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK); 1226 if (d->bd_sbuf == 0) { 1227 free(d->bd_fbuf, M_BPF); 1228 return (ENOBUFS); 1229 } 1230 d->bd_slen = 0; 1231 d->bd_hlen = 0; 1232 return (0); 1233} 1234 1235/* 1236 * Free buffers currently in use by a descriptor. 1237 * Called on close. 1238 */ 1239static void 1240bpf_freed(d) 1241 register struct bpf_d *d; 1242{ 1243 /* 1244 * We don't need to lock out interrupts since this descriptor has 1245 * been detached from its interface and it yet hasn't been marked 1246 * free. 1247 */ 1248 if (d->bd_sbuf != 0) { 1249 free(d->bd_sbuf, M_BPF); 1250 if (d->bd_hbuf != 0) 1251 free(d->bd_hbuf, M_BPF); 1252 if (d->bd_fbuf != 0) 1253 free(d->bd_fbuf, M_BPF); 1254 } 1255 if (d->bd_filter) 1256 free((caddr_t)d->bd_filter, M_BPF); 1257 mtx_destroy(&d->bd_mtx); 1258} 1259 1260/* 1261 * Attach an interface to bpf. ifp is a pointer to the structure 1262 * defining the interface to be attached, dlt is the link layer type, 1263 * and hdrlen is the fixed size of the link header (variable length 1264 * headers are not yet supporrted). 1265 */ 1266void 1267bpfattach(ifp, dlt, hdrlen) 1268 struct ifnet *ifp; 1269 u_int dlt, hdrlen; 1270{ 1271 struct bpf_if *bp; 1272 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 1273 if (bp == 0) 1274 panic("bpfattach"); 1275 1276 bp->bif_ifp = ifp; 1277 bp->bif_dlt = dlt; 1278 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF); 1279 1280 mtx_lock(&bpf_mtx); 1281 bp->bif_next = bpf_iflist; 1282 bpf_iflist = bp; 1283 mtx_unlock(&bpf_mtx); 1284 1285 bp->bif_ifp->if_bpf = 0; 1286 1287 /* 1288 * Compute the length of the bpf header. This is not necessarily 1289 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1290 * that the network layer header begins on a longword boundary (for 1291 * performance reasons and to alleviate alignment restrictions). 1292 */ 1293 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1294 1295 if (bootverbose) 1296 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit); 1297} 1298 1299/* 1300 * Detach bpf from an interface. This involves detaching each descriptor 1301 * associated with the interface, and leaving bd_bif NULL. Notify each 1302 * descriptor as it's detached so that any sleepers wake up and get 1303 * ENXIO. 1304 */ 1305void 1306bpfdetach(ifp) 1307 struct ifnet *ifp; 1308{ 1309 struct bpf_if *bp, *bp_prev; 1310 struct bpf_d *d; 1311 1312 mtx_lock(&bpf_mtx); 1313 1314 /* Locate BPF interface information */ 1315 bp_prev = NULL; 1316 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) { 1317 if (ifp == bp->bif_ifp) 1318 break; 1319 bp_prev = bp; 1320 } 1321 1322 /* Interface wasn't attached */ 1323 if (bp->bif_ifp == NULL) { 1324 mtx_unlock(&bpf_mtx); 1325 printf("bpfdetach: %s%d was not attached\n", ifp->if_name, 1326 ifp->if_unit); 1327 return; 1328 } 1329 1330 if (bp_prev) { 1331 bp_prev->bif_next = bp->bif_next; 1332 } else { 1333 bpf_iflist = bp->bif_next; 1334 } 1335 1336 while ((d = bp->bif_dlist) != NULL) { 1337 bpf_detachd(d); 1338 BPFD_LOCK(d); 1339 bpf_wakeup(d); 1340 BPFD_UNLOCK(d); 1341 } 1342 1343 mtx_destroy(&bp->bif_mtx); 1344 free(bp, M_BPF); 1345 1346 mtx_unlock(&bpf_mtx); 1347} 1348 1349static void bpf_drvinit(void *unused); 1350 1351static void bpf_clone(void *arg, char *name, int namelen, dev_t *dev); 1352 1353static void 1354bpf_clone(arg, name, namelen, dev) 1355 void *arg; 1356 char *name; 1357 int namelen; 1358 dev_t *dev; 1359{ 1360 int u; 1361 1362 if (*dev != NODEV) 1363 return; 1364 if (dev_stdclone(name, NULL, "bpf", &u) != 1) 1365 return; 1366 *dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600, 1367 "bpf%d", u); 1368 (*dev)->si_flags |= SI_CHEAPCLONE; 1369 return; 1370} 1371 1372static void 1373bpf_drvinit(unused) 1374 void *unused; 1375{ 1376 1377 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF); 1378 EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000); 1379 cdevsw_add(&bpf_cdevsw); 1380} 1381 1382SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL) 1383 1384#else /* !DEV_BPF && !NETGRAPH_BPF */ 1385/* 1386 * NOP stubs to allow bpf-using drivers to load and function. 1387 * 1388 * A 'better' implementation would allow the core bpf functionality 1389 * to be loaded at runtime. 1390 */ 1391 1392void 1393bpf_tap(ifp, pkt, pktlen) 1394 struct ifnet *ifp; 1395 register u_char *pkt; 1396 register u_int pktlen; 1397{ 1398} 1399 1400void 1401bpf_mtap(ifp, m) 1402 struct ifnet *ifp; 1403 struct mbuf *m; 1404{ 1405} 1406 1407void 1408bpfattach(ifp, dlt, hdrlen) 1409 struct ifnet *ifp; 1410 u_int dlt, hdrlen; 1411{ 1412} 1413 1414void 1415bpfdetach(ifp) 1416 struct ifnet *ifp; 1417{ 1418} 1419 1420u_int 1421bpf_filter(pc, p, wirelen, buflen) 1422 register const struct bpf_insn *pc; 1423 register u_char *p; 1424 u_int wirelen; 1425 register u_int buflen; 1426{ 1427 return -1; /* "no filter" behaviour */ 1428} 1429 1430int 1431bpf_validate(f, len) 1432 const struct bpf_insn *f; 1433 int len; 1434{ 1435 return 0; /* false */ 1436} 1437 1438#endif /* !DEV_BPF && !NETGRAPH_BPF */ 1439