2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
41 #include "opt_netgraph.h"
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/fcntl.h>
49 #include <sys/malloc.h>
53 #include <sys/signalvar.h>
54 #include <sys/filio.h>
55 #include <sys/sockio.h>
56 #include <sys/ttycom.h>
59 #include <sys/event.h>
64 #include <sys/socket.h>
68 #include <net/bpfdesc.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
75 static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
77 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
79 #define PRINET 26 /* interruptible */
82 * The default read buffer size is patchable.
84 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
85 static int bpf_bufsize = 4096;
86 SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
88 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
89 SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
90 &bpf_maxbufsize, 0, "");
91 static int bpf_maxinsns = BPF_MAXINSNS;
92 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
93 &bpf_maxinsns, 0, "Maximum bpf program instructions");
96 * bpf_iflist is a list of BPF interface structures, each corresponding to a
97 * specific DLT. The same network interface might have several BPF interface
98 * structures registered by different layers in the stack (i.e., 802.11
99 * frames, ethernet frames, etc).
101 static LIST_HEAD(, bpf_if) bpf_iflist;
102 static struct mtx bpf_mtx; /* bpf global lock */
104 static int bpf_allocbufs(struct bpf_d *);
105 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
106 static void bpf_detachd(struct bpf_d *d);
107 static void bpf_freed(struct bpf_d *);
108 static void bpf_mcopy(const void *, void *, size_t);
109 static int bpf_movein(struct uio *, int,
110 struct mbuf **, struct sockaddr *, int *);
111 static int bpf_setif(struct bpf_d *, struct ifreq *);
112 static void bpf_timed_out(void *);
114 bpf_wakeup(struct bpf_d *);
115 static void catchpacket(struct bpf_d *, u_char *, u_int,
116 u_int, void (*)(const void *, void *, size_t));
117 static void reset_d(struct bpf_d *);
118 static int bpf_setf(struct bpf_d *, struct bpf_program *);
119 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
120 static int bpf_setdlt(struct bpf_d *, u_int);
121 static void filt_bpfdetach(struct knote *);
122 static int filt_bpfread(struct knote *, long);
123 static void bpf_drvinit(void *);
124 static void bpf_clone(void *, char *, int, struct cdev **);
126 static d_open_t bpfopen;
127 static d_close_t bpfclose;
128 static d_read_t bpfread;
129 static d_write_t bpfwrite;
130 static d_ioctl_t bpfioctl;
131 static d_poll_t bpfpoll;
132 static d_kqfilter_t bpfkqfilter;
134 static struct cdevsw bpf_cdevsw = {
135 .d_version = D_VERSION,
136 .d_flags = D_NEEDGIANT,
144 .d_kqfilter = bpfkqfilter,
147 static struct filterops bpfread_filtops =
148 { 1, NULL, filt_bpfdetach, filt_bpfread };
151 bpf_movein(uio, linktype, mp, sockp, datlen)
153 int linktype, *datlen;
155 struct sockaddr *sockp;
163 * Build a sockaddr based on the data link layer type.
164 * We do this at this level because the ethernet header
165 * is copied directly into the data field of the sockaddr.
166 * In the case of SLIP, there is no header and the packet
167 * is forwarded as is.
168 * Also, we are careful to leave room at the front of the mbuf
169 * for the link level header.
174 sockp->sa_family = AF_INET;
179 sockp->sa_family = AF_UNSPEC;
180 /* XXX Would MAXLINKHDR be better? */
181 hlen = ETHER_HDR_LEN;
185 sockp->sa_family = AF_IMPLINK;
191 sockp->sa_family = AF_UNSPEC;
195 case DLT_ATM_RFC1483:
197 * en atm driver requires 4-byte atm pseudo header.
198 * though it isn't standard, vpi:vci needs to be
201 sockp->sa_family = AF_UNSPEC;
202 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
206 sockp->sa_family = AF_UNSPEC;
207 hlen = 4; /* This should match PPP_HDRLEN */
214 len = uio->uio_resid;
215 *datlen = len - hlen;
216 if ((unsigned)len > MCLBYTES)
220 m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
222 MGETHDR(m, M_TRYWAIT, MT_DATA);
226 m->m_pkthdr.len = m->m_len = len;
227 m->m_pkthdr.rcvif = NULL;
231 * Make room for link header.
234 m->m_pkthdr.len -= hlen;
237 m->m_data += hlen; /* XXX */
241 error = uiomove(sockp->sa_data, hlen, uio);
245 error = uiomove(mtod(m, void *), len - hlen, uio);
254 * Attach file to the bpf interface, i.e. make d listen on bp.
262 * Point d at bp, and add d to the interface's list of listeners.
263 * Finally, point the driver's bpf cookie at the interface so
264 * it will divert packets to bpf.
268 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
270 *bp->bif_driverp = bp;
275 * Detach a file from its interface.
288 ifp = d->bd_bif->bif_ifp;
291 * Remove d from the interface's descriptor list.
293 LIST_REMOVE(d, bd_next);
296 * Let the driver know that there are no more listeners.
298 if (LIST_EMPTY(&bp->bif_dlist))
299 *bp->bif_driverp = NULL;
306 * Check if this descriptor had requested promiscuous mode.
307 * If so, turn it off.
311 error = ifpromisc(ifp, 0);
312 if (error != 0 && error != ENXIO) {
314 * ENXIO can happen if a pccard is unplugged
315 * Something is really wrong if we were able to put
316 * the driver into promiscuous mode, but can't
319 if_printf(bp->bif_ifp,
320 "bpf_detach: ifpromisc failed (%d)\n", error);
326 * Open ethernet device. Returns ENXIO for illegal minor device number,
327 * EBUSY if file is open by another process.
331 bpfopen(dev, flags, fmt, td)
342 * Each minor can be opened by only one process. If the requested
343 * minor is in use, return EBUSY.
346 mtx_unlock(&bpf_mtx);
349 dev->si_drv1 = (struct bpf_d *)~0; /* mark device in use */
350 mtx_unlock(&bpf_mtx);
352 if ((dev->si_flags & SI_NAMED) == 0)
353 make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
354 "bpf%d", dev2unit(dev));
355 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
357 d->bd_bufsize = bpf_bufsize;
362 mac_create_bpfdesc(td->td_ucred, d);
364 mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
365 callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
366 knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
372 * Close the descriptor by detaching it from its interface,
373 * deallocating its buffers, and marking it free.
377 bpfclose(dev, flags, fmt, td)
383 struct bpf_d *d = dev->si_drv1;
386 if (d->bd_state == BPF_WAITING)
387 callout_stop(&d->bd_callout);
388 d->bd_state = BPF_IDLE;
390 funsetown(&d->bd_sigio);
394 mtx_unlock(&bpf_mtx);
395 selwakeuppri(&d->bd_sel, PRINET);
397 mac_destroy_bpfdesc(d);
399 knlist_destroy(&d->bd_sel.si_note);
409 * Rotate the packet buffers in descriptor d. Move the store buffer
410 * into the hold slot, and the free buffer into the store slot.
411 * Zero the length of the new store buffer.
413 #define ROTATE_BUFFERS(d) \
414 (d)->bd_hbuf = (d)->bd_sbuf; \
415 (d)->bd_hlen = (d)->bd_slen; \
416 (d)->bd_sbuf = (d)->bd_fbuf; \
420 * bpfread - read next chunk of packets from buffers
423 bpfread(dev, uio, ioflag)
428 struct bpf_d *d = dev->si_drv1;
433 * Restrict application to use a buffer the same size as
436 if (uio->uio_resid != d->bd_bufsize)
440 if (d->bd_state == BPF_WAITING)
441 callout_stop(&d->bd_callout);
442 timed_out = (d->bd_state == BPF_TIMED_OUT);
443 d->bd_state = BPF_IDLE;
445 * If the hold buffer is empty, then do a timed sleep, which
446 * ends when the timeout expires or when enough packets
447 * have arrived to fill the store buffer.
449 while (d->bd_hbuf == NULL) {
450 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
452 * A packet(s) either arrived since the previous
453 * read or arrived while we were asleep.
454 * Rotate the buffers and return what's here.
461 * No data is available, check to see if the bpf device
462 * is still pointed at a real interface. If not, return
463 * ENXIO so that the userland process knows to rebind
464 * it before using it again.
466 if (d->bd_bif == NULL) {
471 if (ioflag & O_NONBLOCK) {
473 return (EWOULDBLOCK);
475 error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
477 if (error == EINTR || error == ERESTART) {
481 if (error == EWOULDBLOCK) {
483 * On a timeout, return what's in the buffer,
484 * which may be nothing. If there is something
485 * in the store buffer, we can rotate the buffers.
489 * We filled up the buffer in between
490 * getting the timeout and arriving
491 * here, so we don't need to rotate.
495 if (d->bd_slen == 0) {
504 * At this point, we know we have something in the hold slot.
509 * Move data from hold buffer into user space.
510 * We know the entire buffer is transferred since
511 * we checked above that the read buffer is bpf_bufsize bytes.
513 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
516 d->bd_fbuf = d->bd_hbuf;
526 * If there are processes sleeping on this descriptor, wake them up.
534 if (d->bd_state == BPF_WAITING) {
535 callout_stop(&d->bd_callout);
536 d->bd_state = BPF_IDLE;
539 if (d->bd_async && d->bd_sig && d->bd_sigio)
540 pgsigio(&d->bd_sigio, d->bd_sig, 0);
542 selwakeuppri(&d->bd_sel, PRINET);
543 KNOTE_LOCKED(&d->bd_sel.si_note, 0);
550 struct bpf_d *d = (struct bpf_d *)arg;
553 if (d->bd_state == BPF_WAITING) {
554 d->bd_state = BPF_TIMED_OUT;
562 bpfwrite(dev, uio, ioflag)
567 struct bpf_d *d = dev->si_drv1;
574 if (d->bd_bif == NULL)
577 ifp = d->bd_bif->bif_ifp;
579 if ((ifp->if_flags & IFF_UP) == 0)
582 if (uio->uio_resid == 0)
585 bzero(&dst, sizeof(dst));
586 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
590 if (datlen > ifp->if_mtu) {
596 dst.sa_family = pseudo_AF_HDRCMPLT;
600 mac_create_mbuf_from_bpfdesc(d, m);
604 error = (*ifp->if_output)(ifp, m, &dst, NULL);
607 * The driver frees the mbuf.
613 * Reset a descriptor by flushing its packet buffer and clearing the
614 * receive and drop counts.
621 mtx_assert(&d->bd_mtx, MA_OWNED);
623 /* Free the hold buffer. */
624 d->bd_fbuf = d->bd_hbuf;
634 * FIONREAD Check for read packet available.
635 * SIOCGIFADDR Get interface address - convenient hook to driver.
636 * BIOCGBLEN Get buffer len [for read()].
637 * BIOCSETF Set ethernet read filter.
638 * BIOCFLUSH Flush read packet buffer.
639 * BIOCPROMISC Put interface into promiscuous mode.
640 * BIOCGDLT Get link layer type.
641 * BIOCGETIF Get interface name.
642 * BIOCSETIF Set interface.
643 * BIOCSRTIMEOUT Set read timeout.
644 * BIOCGRTIMEOUT Get read timeout.
645 * BIOCGSTATS Get packet stats.
646 * BIOCIMMEDIATE Set immediate mode.
647 * BIOCVERSION Get filter language version.
648 * BIOCGHDRCMPLT Get "header already complete" flag
649 * BIOCSHDRCMPLT Set "header already complete" flag
650 * BIOCGSEESENT Get "see packets sent" flag
651 * BIOCSSEESENT Set "see packets sent" flag
655 bpfioctl(dev, cmd, addr, flags, td)
662 struct bpf_d *d = dev->si_drv1;
666 if (d->bd_state == BPF_WAITING)
667 callout_stop(&d->bd_callout);
668 d->bd_state = BPF_IDLE;
678 * Check for read packet available.
698 if (d->bd_bif == NULL)
701 ifp = d->bd_bif->bif_ifp;
702 error = (*ifp->if_ioctl)(ifp, cmd, addr);
708 * Get buffer len [for read()].
711 *(u_int *)addr = d->bd_bufsize;
718 if (d->bd_bif != NULL)
721 u_int size = *(u_int *)addr;
723 if (size > bpf_maxbufsize)
724 *(u_int *)addr = size = bpf_maxbufsize;
725 else if (size < BPF_MINBUFSIZE)
726 *(u_int *)addr = size = BPF_MINBUFSIZE;
727 d->bd_bufsize = size;
732 * Set link layer read filter.
735 error = bpf_setf(d, (struct bpf_program *)addr);
739 * Flush read packet buffer.
748 * Put interface into promiscuous mode.
751 if (d->bd_bif == NULL) {
753 * No interface attached yet.
758 if (d->bd_promisc == 0) {
760 error = ifpromisc(d->bd_bif->bif_ifp, 1);
768 * Get current data link type.
771 if (d->bd_bif == NULL)
774 *(u_int *)addr = d->bd_bif->bif_dlt;
778 * Get a list of supported data link types.
781 if (d->bd_bif == NULL)
784 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
788 * Set data link type.
791 if (d->bd_bif == NULL)
794 error = bpf_setdlt(d, *(u_int *)addr);
798 * Get interface name.
801 if (d->bd_bif == NULL)
804 struct ifnet *const ifp = d->bd_bif->bif_ifp;
805 struct ifreq *const ifr = (struct ifreq *)addr;
807 strlcpy(ifr->ifr_name, ifp->if_xname,
808 sizeof(ifr->ifr_name));
816 error = bpf_setif(d, (struct ifreq *)addr);
824 struct timeval *tv = (struct timeval *)addr;
827 * Subtract 1 tick from tvtohz() since this isn't
830 if ((error = itimerfix(tv)) == 0)
831 d->bd_rtout = tvtohz(tv) - 1;
840 struct timeval *tv = (struct timeval *)addr;
842 tv->tv_sec = d->bd_rtout / hz;
843 tv->tv_usec = (d->bd_rtout % hz) * tick;
852 struct bpf_stat *bs = (struct bpf_stat *)addr;
854 bs->bs_recv = d->bd_rcount;
855 bs->bs_drop = d->bd_dcount;
860 * Set immediate mode.
863 d->bd_immediate = *(u_int *)addr;
868 struct bpf_version *bv = (struct bpf_version *)addr;
870 bv->bv_major = BPF_MAJOR_VERSION;
871 bv->bv_minor = BPF_MINOR_VERSION;
876 * Get "header already complete" flag
879 *(u_int *)addr = d->bd_hdrcmplt;
883 * Set "header already complete" flag
886 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
890 * Get "see sent packets" flag
893 *(u_int *)addr = d->bd_seesent;
897 * Set "see sent packets" flag
900 d->bd_seesent = *(u_int *)addr;
903 case FIONBIO: /* Non-blocking I/O */
906 case FIOASYNC: /* Send signal on receive packets */
907 d->bd_async = *(int *)addr;
911 error = fsetown(*(int *)addr, &d->bd_sigio);
915 *(int *)addr = fgetown(&d->bd_sigio);
918 /* This is deprecated, FIOSETOWN should be used instead. */
920 error = fsetown(-(*(int *)addr), &d->bd_sigio);
923 /* This is deprecated, FIOGETOWN should be used instead. */
925 *(int *)addr = -fgetown(&d->bd_sigio);
928 case BIOCSRSIG: /* Set receive signal */
932 sig = *(u_int *)addr;
941 *(u_int *)addr = d->bd_sig;
948 * Set d's packet filter program to fp. If this file already has a filter,
949 * free it and replace it. Returns EINVAL for bogus requests.
954 struct bpf_program *fp;
956 struct bpf_insn *fcode, *old;
959 if (fp->bf_insns == NULL) {
968 free((caddr_t)old, M_BPF);
972 if (flen > bpf_maxinsns)
975 size = flen * sizeof(*fp->bf_insns);
976 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
977 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
978 bpf_validate(fcode, (int)flen)) {
981 d->bd_filter = fcode;
985 free((caddr_t)old, M_BPF);
989 free((caddr_t)fcode, M_BPF);
994 * Detach a file from its current interface (if attached at all) and attach
995 * to the interface indicated by the name stored in ifr.
996 * Return an errno or 0.
1005 struct ifnet *theywant;
1007 theywant = ifunit(ifr->ifr_name);
1008 if (theywant == NULL)
1012 * Look through attached interfaces for the named one.
1015 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1016 struct ifnet *ifp = bp->bif_ifp;
1018 if (ifp == NULL || ifp != theywant)
1020 /* skip additional entry */
1021 if (bp->bif_driverp != &ifp->if_bpf)
1024 mtx_unlock(&bpf_mtx);
1026 * We found the requested interface.
1027 * Allocate the packet buffers if we need to.
1028 * If we're already attached to requested interface,
1029 * just flush the buffer.
1031 if (d->bd_sbuf == NULL) {
1032 error = bpf_allocbufs(d);
1036 if (bp != d->bd_bif) {
1039 * Detach if attached to something else.
1050 mtx_unlock(&bpf_mtx);
1056 * Support for select() and poll() system calls
1058 * Return true iff the specific operation will not block indefinitely.
1059 * Otherwise, return false but make a note that a selwakeup() must be done.
1062 bpfpoll(dev, events, td)
1071 if (d->bd_bif == NULL)
1074 revents = events & (POLLOUT | POLLWRNORM);
1076 if (events & (POLLIN | POLLRDNORM)) {
1078 revents |= events & (POLLIN | POLLRDNORM);
1080 selrecord(td, &d->bd_sel);
1081 /* Start the read timeout if necessary. */
1082 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1083 callout_reset(&d->bd_callout, d->bd_rtout,
1085 d->bd_state = BPF_WAITING;
1094 * Support for kevent() system call. Register EVFILT_READ filters and
1095 * reject all others.
1098 bpfkqfilter(dev, kn)
1102 struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1104 if (kn->kn_filter != EVFILT_READ)
1107 kn->kn_fop = &bpfread_filtops;
1109 knlist_add(&d->bd_sel.si_note, kn, 0);
1118 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1120 knlist_remove(&d->bd_sel.si_note, kn, 0);
1124 filt_bpfread(kn, hint)
1128 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1131 BPFD_LOCK_ASSERT(d);
1132 ready = bpf_ready(d);
1134 kn->kn_data = d->bd_slen;
1136 kn->kn_data += d->bd_hlen;
1138 else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1139 callout_reset(&d->bd_callout, d->bd_rtout,
1141 d->bd_state = BPF_WAITING;
1148 * Incoming linkage from device drivers. Process the packet pkt, of length
1149 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1150 * by each process' filter, and if accepted, stashed into the corresponding
1154 bpf_tap(bp, pkt, pktlen)
1163 * Lockless read to avoid cost of locking the interface if there are
1164 * no descriptors attached.
1166 if (LIST_EMPTY(&bp->bif_dlist))
1170 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1173 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1176 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1178 catchpacket(d, pkt, pktlen, slen, bcopy);
1186 * Copy data from an mbuf chain into a buffer. This code is derived
1187 * from m_copydata in sys/uipc_mbuf.c.
1190 bpf_mcopy(src_arg, dst_arg, len)
1191 const void *src_arg;
1195 const struct mbuf *m;
1204 count = min(m->m_len, len);
1205 bcopy(mtod(m, void *), dst, count);
1213 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1224 * Lockless read to avoid cost of locking the interface if there are
1225 * no descriptors attached.
1227 if (LIST_EMPTY(&bp->bif_dlist))
1230 pktlen = m_length(m, NULL);
1231 if (pktlen == m->m_len) {
1232 bpf_tap(bp, mtod(m, u_char *), pktlen);
1237 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1238 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1242 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1245 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1247 catchpacket(d, (u_char *)m, pktlen, slen,
1255 * Incoming linkage from device drivers, when packet is in
1256 * an mbuf chain and to be prepended by a contiguous header.
1259 bpf_mtap2(bp, data, dlen, m)
1270 * Lockless read to avoid cost of locking the interface if there are
1271 * no descriptors attached.
1273 if (LIST_EMPTY(&bp->bif_dlist))
1276 pktlen = m_length(m, NULL);
1278 * Craft on-stack mbuf suitable for passing to bpf_filter.
1279 * Note that we cut corners here; we only setup what's
1280 * absolutely needed--this mbuf should never go anywhere else.
1288 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1289 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1293 slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1296 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1298 catchpacket(d, (u_char *)&mb, pktlen, slen,
1306 * Move the packet data from interface memory (pkt) into the
1307 * store buffer. "cpfn" is the routine called to do the actual data
1308 * transfer. bcopy is passed in to copy contiguous chunks, while
1309 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1310 * pkt is really an mbuf.
1313 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1316 u_int pktlen, snaplen;
1317 void (*cpfn)(const void *, void *, size_t);
1321 int hdrlen = d->bd_bif->bif_hdrlen;
1324 BPFD_LOCK_ASSERT(d);
1326 * Figure out how many bytes to move. If the packet is
1327 * greater or equal to the snapshot length, transfer that
1328 * much. Otherwise, transfer the whole packet (unless
1329 * we hit the buffer size limit).
1331 totlen = hdrlen + min(snaplen, pktlen);
1332 if (totlen > d->bd_bufsize)
1333 totlen = d->bd_bufsize;
1336 * Round up the end of the previous packet to the next longword.
1338 curlen = BPF_WORDALIGN(d->bd_slen);
1339 if (curlen + totlen > d->bd_bufsize) {
1341 * This packet will overflow the storage buffer.
1342 * Rotate the buffers if we can, then wakeup any
1345 if (d->bd_fbuf == NULL) {
1347 * We haven't completed the previous read yet,
1348 * so drop the packet.
1357 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1359 * Immediate mode is set, or the read timeout has
1360 * already expired during a select call. A packet
1361 * arrived, so the reader should be woken up.
1366 * Append the bpf header.
1368 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1369 microtime(&hp->bh_tstamp);
1370 hp->bh_datalen = pktlen;
1371 hp->bh_hdrlen = hdrlen;
1373 * Copy the packet data into the store buffer and update its length.
1375 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1376 d->bd_slen = curlen + totlen;
1383 * Initialize all nonzero fields of a descriptor.
1389 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1390 if (d->bd_fbuf == NULL)
1393 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1394 if (d->bd_sbuf == NULL) {
1395 free(d->bd_fbuf, M_BPF);
1404 * Free buffers currently in use by a descriptor.
1412 * We don't need to lock out interrupts since this descriptor has
1413 * been detached from its interface and it yet hasn't been marked
1416 if (d->bd_sbuf != NULL) {
1417 free(d->bd_sbuf, M_BPF);
1418 if (d->bd_hbuf != NULL)
1419 free(d->bd_hbuf, M_BPF);
1420 if (d->bd_fbuf != NULL)
1421 free(d->bd_fbuf, M_BPF);
1424 free((caddr_t)d->bd_filter, M_BPF);
1425 mtx_destroy(&d->bd_mtx);
1429 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
1430 * fixed size of the link header (variable length headers not yet supported).
1433 bpfattach(ifp, dlt, hdrlen)
1438 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1442 * Attach an interface to bpf. ifp is a pointer to the structure
1443 * defining the interface to be attached, dlt is the link layer type,
1444 * and hdrlen is the fixed size of the link header (variable length
1445 * headers are not yet supporrted).
1448 bpfattach2(ifp, dlt, hdrlen, driverp)
1451 struct bpf_if **driverp;
1454 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1458 LIST_INIT(&bp->bif_dlist);
1459 bp->bif_driverp = driverp;
1462 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1465 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1466 mtx_unlock(&bpf_mtx);
1468 *bp->bif_driverp = NULL;
1471 * Compute the length of the bpf header. This is not necessarily
1472 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1473 * that the network layer header begins on a longword boundary (for
1474 * performance reasons and to alleviate alignment restrictions).
1476 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1479 if_printf(ifp, "bpf attached\n");
1483 * Detach bpf from an interface. This involves detaching each descriptor
1484 * associated with the interface, and leaving bd_bif NULL. Notify each
1485 * descriptor as it's detached so that any sleepers wake up and get
1495 /* Locate BPF interface information */
1497 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1498 if (ifp == bp->bif_ifp)
1502 /* Interface wasn't attached */
1503 if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1504 mtx_unlock(&bpf_mtx);
1505 printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1509 LIST_REMOVE(bp, bif_next);
1510 mtx_unlock(&bpf_mtx);
1512 while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1519 mtx_destroy(&bp->bif_mtx);
1524 * Get a list of available data link type of the interface.
1527 bpf_getdltlist(d, bfl)
1529 struct bpf_dltlist *bfl;
1535 ifp = d->bd_bif->bif_ifp;
1539 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1540 if (bp->bif_ifp != ifp)
1542 if (bfl->bfl_list != NULL) {
1543 if (n >= bfl->bfl_len) {
1544 mtx_unlock(&bpf_mtx);
1547 error = copyout(&bp->bif_dlt,
1548 bfl->bfl_list + n, sizeof(u_int));
1552 mtx_unlock(&bpf_mtx);
1558 * Set the data link type of a BPF instance.
1565 int error, opromisc;
1569 if (d->bd_bif->bif_dlt == dlt)
1571 ifp = d->bd_bif->bif_ifp;
1573 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1574 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1577 mtx_unlock(&bpf_mtx);
1579 opromisc = d->bd_promisc;
1586 error = ifpromisc(bp->bif_ifp, 1);
1588 if_printf(bp->bif_ifp,
1589 "bpf_setdlt: ifpromisc failed (%d)\n",
1595 return (bp == NULL ? EINVAL : 0);
1599 bpf_clone(arg, name, namelen, dev)
1609 if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1611 *dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1614 (*dev)->si_flags |= SI_CHEAPCLONE;
1623 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1624 LIST_INIT(&bpf_iflist);
1625 EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1628 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1630 #else /* !DEV_BPF && !NETGRAPH_BPF */
1632 * NOP stubs to allow bpf-using drivers to load and function.
1634 * A 'better' implementation would allow the core bpf functionality
1635 * to be loaded at runtime.
1639 bpf_tap(bp, pkt, pktlen)
1654 bpf_mtap2(bp, d, l, m)
1663 bpfattach(ifp, dlt, hdrlen)
1670 bpfattach2(ifp, dlt, hdrlen, driverp)
1673 struct bpf_if **driverp;
1684 bpf_filter(pc, p, wirelen, buflen)
1685 const struct bpf_insn *pc;
1690 return -1; /* "no filter" behaviour */
1694 bpf_validate(f, len)
1695 const struct bpf_insn *f;
1698 return 0; /* false */
1701 #endif /* !DEV_BPF && !NETGRAPH_BPF */