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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
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>
54 #include <sys/signalvar.h>
55 #include <sys/filio.h>
56 #include <sys/sockio.h>
57 #include <sys/ttycom.h>
60 #include <sys/event.h>
65 #include <sys/socket.h>
69 #include <net/bpf_buffer.h>
71 #include <net/bpf_jitter.h>
73 #include <net/bpf_zerocopy.h>
74 #include <net/bpfdesc.h>
77 #include <netinet/in.h>
78 #include <netinet/if_ether.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
82 #include <net80211/ieee80211_freebsd.h>
84 #include <security/mac/mac_framework.h>
86 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
88 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
90 #define PRINET 26 /* interruptible */
93 * bpf_iflist is a list of BPF interface structures, each corresponding to a
94 * specific DLT. The same network interface might have several BPF interface
95 * structures registered by different layers in the stack (i.e., 802.11
96 * frames, ethernet frames, etc).
98 static LIST_HEAD(, bpf_if) bpf_iflist;
99 static struct mtx bpf_mtx; /* bpf global lock */
100 static int bpf_bpfd_cnt;
102 static void bpf_attachd(struct bpf_d *, struct bpf_if *);
103 static void bpf_detachd(struct bpf_d *);
104 static void bpf_freed(struct bpf_d *);
105 static int bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
106 struct sockaddr *, int *, struct bpf_insn *);
107 static int bpf_setif(struct bpf_d *, struct ifreq *);
108 static void bpf_timed_out(void *);
110 bpf_wakeup(struct bpf_d *);
111 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
112 void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
114 static void reset_d(struct bpf_d *);
115 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
116 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
117 static int bpf_setdlt(struct bpf_d *, u_int);
118 static void filt_bpfdetach(struct knote *);
119 static int filt_bpfread(struct knote *, long);
120 static void bpf_drvinit(void *);
121 static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
123 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
124 int bpf_maxinsns = BPF_MAXINSNS;
125 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
126 &bpf_maxinsns, 0, "Maximum bpf program instructions");
127 static int bpf_zerocopy_enable = 0;
128 SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
129 &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
130 SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
131 bpf_stats_sysctl, "bpf statistics portal");
133 static d_open_t bpfopen;
134 static d_read_t bpfread;
135 static d_write_t bpfwrite;
136 static d_ioctl_t bpfioctl;
137 static d_poll_t bpfpoll;
138 static d_kqfilter_t bpfkqfilter;
140 static struct cdevsw bpf_cdevsw = {
141 .d_version = D_VERSION,
148 .d_kqfilter = bpfkqfilter,
151 static struct filterops bpfread_filtops =
152 { 1, NULL, filt_bpfdetach, filt_bpfread };
155 * Wrapper functions for various buffering methods. If the set of buffer
156 * modes expands, we will probably want to introduce a switch data structure
157 * similar to protosw, et.
160 bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
166 switch (d->bd_bufmode) {
167 case BPF_BUFMODE_BUFFER:
168 return (bpf_buffer_append_bytes(d, buf, offset, src, len));
170 case BPF_BUFMODE_ZBUF:
172 return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
175 panic("bpf_buf_append_bytes");
180 bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
186 switch (d->bd_bufmode) {
187 case BPF_BUFMODE_BUFFER:
188 return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
190 case BPF_BUFMODE_ZBUF:
192 return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
195 panic("bpf_buf_append_mbuf");
200 * This function gets called when the free buffer is re-assigned.
203 bpf_buf_reclaimed(struct bpf_d *d)
208 switch (d->bd_bufmode) {
209 case BPF_BUFMODE_BUFFER:
212 case BPF_BUFMODE_ZBUF:
213 bpf_zerocopy_buf_reclaimed(d);
217 panic("bpf_buf_reclaimed");
222 * If the buffer mechanism has a way to decide that a held buffer can be made
223 * free, then it is exposed via the bpf_canfreebuf() interface. (1) is
224 * returned if the buffer can be discarded, (0) is returned if it cannot.
227 bpf_canfreebuf(struct bpf_d *d)
232 switch (d->bd_bufmode) {
233 case BPF_BUFMODE_ZBUF:
234 return (bpf_zerocopy_canfreebuf(d));
240 * Allow the buffer model to indicate that the current store buffer is
241 * immutable, regardless of the appearance of space. Return (1) if the
242 * buffer is writable, and (0) if not.
245 bpf_canwritebuf(struct bpf_d *d)
250 switch (d->bd_bufmode) {
251 case BPF_BUFMODE_ZBUF:
252 return (bpf_zerocopy_canwritebuf(d));
258 * Notify buffer model that an attempt to write to the store buffer has
259 * resulted in a dropped packet, in which case the buffer may be considered
263 bpf_buffull(struct bpf_d *d)
268 switch (d->bd_bufmode) {
269 case BPF_BUFMODE_ZBUF:
270 bpf_zerocopy_buffull(d);
276 * Notify the buffer model that a buffer has moved into the hold position.
279 bpf_bufheld(struct bpf_d *d)
284 switch (d->bd_bufmode) {
285 case BPF_BUFMODE_ZBUF:
286 bpf_zerocopy_bufheld(d);
292 bpf_free(struct bpf_d *d)
295 switch (d->bd_bufmode) {
296 case BPF_BUFMODE_BUFFER:
297 return (bpf_buffer_free(d));
299 case BPF_BUFMODE_ZBUF:
300 return (bpf_zerocopy_free(d));
303 panic("bpf_buf_free");
308 bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
311 if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
313 return (bpf_buffer_uiomove(d, buf, len, uio));
317 bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
320 if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
322 return (bpf_buffer_ioctl_sblen(d, i));
326 bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
329 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
331 return (bpf_zerocopy_ioctl_getzmax(td, d, i));
335 bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
338 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
340 return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
344 bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
347 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
349 return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
353 * General BPF functions.
356 bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
357 struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
359 const struct ieee80211_bpf_params *p;
360 struct ether_header *eh;
368 * Build a sockaddr based on the data link layer type.
369 * We do this at this level because the ethernet header
370 * is copied directly into the data field of the sockaddr.
371 * In the case of SLIP, there is no header and the packet
372 * is forwarded as is.
373 * Also, we are careful to leave room at the front of the mbuf
374 * for the link level header.
379 sockp->sa_family = AF_INET;
384 sockp->sa_family = AF_UNSPEC;
385 /* XXX Would MAXLINKHDR be better? */
386 hlen = ETHER_HDR_LEN;
390 sockp->sa_family = AF_IMPLINK;
395 sockp->sa_family = AF_UNSPEC;
401 * null interface types require a 4 byte pseudo header which
402 * corresponds to the address family of the packet.
404 sockp->sa_family = AF_UNSPEC;
408 case DLT_ATM_RFC1483:
410 * en atm driver requires 4-byte atm pseudo header.
411 * though it isn't standard, vpi:vci needs to be
414 sockp->sa_family = AF_UNSPEC;
415 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
419 sockp->sa_family = AF_UNSPEC;
420 hlen = 4; /* This should match PPP_HDRLEN */
423 case DLT_IEEE802_11: /* IEEE 802.11 wireless */
424 sockp->sa_family = AF_IEEE80211;
428 case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */
429 sockp->sa_family = AF_IEEE80211;
430 sockp->sa_len = 12; /* XXX != 0 */
431 hlen = sizeof(struct ieee80211_bpf_params);
438 len = uio->uio_resid;
440 if (len - hlen > ifp->if_mtu)
443 if ((unsigned)len > MJUM16BYTES)
447 MGETHDR(m, M_WAIT, MT_DATA);
448 else if (len <= MCLBYTES)
449 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
451 m = m_getjcl(M_WAIT, MT_DATA, M_PKTHDR,
452 #if (MJUMPAGESIZE > MCLBYTES)
453 len <= MJUMPAGESIZE ? MJUMPAGESIZE :
455 (len <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES));
456 m->m_pkthdr.len = m->m_len = len;
457 m->m_pkthdr.rcvif = NULL;
460 if (m->m_len < hlen) {
465 error = uiomove(mtod(m, u_char *), len, uio);
469 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
475 /* Check for multicast destination */
478 eh = mtod(m, struct ether_header *);
479 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
480 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
481 ETHER_ADDR_LEN) == 0)
482 m->m_flags |= M_BCAST;
484 m->m_flags |= M_MCAST;
490 * Make room for link header, and copy it to sockaddr
493 if (sockp->sa_family == AF_IEEE80211) {
495 * Collect true length from the parameter header
496 * NB: sockp is known to be zero'd so if we do a
497 * short copy unspecified parameters will be
499 * NB: packet may not be aligned after stripping
503 p = mtod(m, const struct ieee80211_bpf_params *);
505 if (hlen > sizeof(sockp->sa_data)) {
510 bcopy(m->m_data, sockp->sa_data, hlen);
521 * Attach file to the bpf interface, i.e. make d listen on bp.
524 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
527 * Point d at bp, and add d to the interface's list of listeners.
528 * Finally, point the driver's bpf cookie at the interface so
529 * it will divert packets to bpf.
533 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
538 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
542 * Detach a file from its interface.
545 bpf_detachd(struct bpf_d *d)
554 ifp = d->bd_bif->bif_ifp;
557 * Remove d from the interface's descriptor list.
559 LIST_REMOVE(d, bd_next);
566 EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0);
569 * Check if this descriptor had requested promiscuous mode.
570 * If so, turn it off.
574 CURVNET_SET(ifp->if_vnet);
575 error = ifpromisc(ifp, 0);
577 if (error != 0 && error != ENXIO) {
579 * ENXIO can happen if a pccard is unplugged
580 * Something is really wrong if we were able to put
581 * the driver into promiscuous mode, but can't
584 if_printf(bp->bif_ifp,
585 "bpf_detach: ifpromisc failed (%d)\n", error);
591 * Close the descriptor by detaching it from its interface,
592 * deallocating its buffers, and marking it free.
597 struct bpf_d *d = data;
600 if (d->bd_state == BPF_WAITING)
601 callout_stop(&d->bd_callout);
602 d->bd_state = BPF_IDLE;
604 funsetown(&d->bd_sigio);
608 mtx_unlock(&bpf_mtx);
609 selwakeuppri(&d->bd_sel, PRINET);
611 mac_bpfdesc_destroy(d);
613 knlist_destroy(&d->bd_sel.si_note);
619 * Open ethernet device. Returns ENXIO for illegal minor device number,
620 * EBUSY if file is open by another process.
624 bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
629 d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
630 error = devfs_set_cdevpriv(d, bpf_dtor);
637 * For historical reasons, perform a one-time initialization call to
638 * the buffer routines, even though we're not yet committed to a
639 * particular buffer method.
642 d->bd_bufmode = BPF_BUFMODE_BUFFER;
644 d->bd_direction = BPF_D_INOUT;
645 d->bd_pid = td->td_proc->p_pid;
648 mac_bpfdesc_create(td->td_ucred, d);
650 mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
651 callout_init(&d->bd_callout, CALLOUT_MPSAFE);
652 knlist_init_mtx(&d->bd_sel.si_note, &d->bd_mtx);
658 * bpfread - read next chunk of packets from buffers
661 bpfread(struct cdev *dev, struct uio *uio, int ioflag)
668 error = devfs_get_cdevpriv((void **)&d);
673 * Restrict application to use a buffer the same size as
676 if (uio->uio_resid != d->bd_bufsize)
679 non_block = ((ioflag & O_NONBLOCK) != 0);
682 d->bd_pid = curthread->td_proc->p_pid;
683 if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
687 if (d->bd_state == BPF_WAITING)
688 callout_stop(&d->bd_callout);
689 timed_out = (d->bd_state == BPF_TIMED_OUT);
690 d->bd_state = BPF_IDLE;
692 * If the hold buffer is empty, then do a timed sleep, which
693 * ends when the timeout expires or when enough packets
694 * have arrived to fill the store buffer.
696 while (d->bd_hbuf == NULL) {
697 if (d->bd_slen != 0) {
699 * A packet(s) either arrived since the previous
700 * read or arrived while we were asleep.
702 if (d->bd_immediate || non_block || timed_out) {
704 * Rotate the buffers and return what's here
705 * if we are in immediate mode, non-blocking
706 * flag is set, or this descriptor timed out.
714 * No data is available, check to see if the bpf device
715 * is still pointed at a real interface. If not, return
716 * ENXIO so that the userland process knows to rebind
717 * it before using it again.
719 if (d->bd_bif == NULL) {
726 return (EWOULDBLOCK);
728 error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
730 if (error == EINTR || error == ERESTART) {
734 if (error == EWOULDBLOCK) {
736 * On a timeout, return what's in the buffer,
737 * which may be nothing. If there is something
738 * in the store buffer, we can rotate the buffers.
742 * We filled up the buffer in between
743 * getting the timeout and arriving
744 * here, so we don't need to rotate.
748 if (d->bd_slen == 0) {
757 * At this point, we know we have something in the hold slot.
762 * Move data from hold buffer into user space.
763 * We know the entire buffer is transferred since
764 * we checked above that the read buffer is bpf_bufsize bytes.
766 * XXXRW: More synchronization needed here: what if a second thread
767 * issues a read on the same fd at the same time? Don't want this
768 * getting invalidated.
770 error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
773 d->bd_fbuf = d->bd_hbuf;
776 bpf_buf_reclaimed(d);
783 * If there are processes sleeping on this descriptor, wake them up.
786 bpf_wakeup(struct bpf_d *d)
790 if (d->bd_state == BPF_WAITING) {
791 callout_stop(&d->bd_callout);
792 d->bd_state = BPF_IDLE;
795 if (d->bd_async && d->bd_sig && d->bd_sigio)
796 pgsigio(&d->bd_sigio, d->bd_sig, 0);
798 selwakeuppri(&d->bd_sel, PRINET);
799 KNOTE_LOCKED(&d->bd_sel.si_note, 0);
803 bpf_timed_out(void *arg)
805 struct bpf_d *d = (struct bpf_d *)arg;
808 if (d->bd_state == BPF_WAITING) {
809 d->bd_state = BPF_TIMED_OUT;
817 bpf_ready(struct bpf_d *d)
822 if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
824 if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
831 bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
839 error = devfs_get_cdevpriv((void **)&d);
843 d->bd_pid = curthread->td_proc->p_pid;
845 if (d->bd_bif == NULL) {
850 ifp = d->bd_bif->bif_ifp;
852 if ((ifp->if_flags & IFF_UP) == 0) {
857 if (uio->uio_resid == 0) {
862 bzero(&dst, sizeof(dst));
865 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
866 &m, &dst, &hlen, d->bd_wfilter);
873 dst.sa_family = pseudo_AF_HDRCMPLT;
875 if (d->bd_feedback) {
876 mc = m_dup(m, M_DONTWAIT);
878 mc->m_pkthdr.rcvif = ifp;
879 /* Set M_PROMISC for outgoing packets to be discarded. */
880 if (d->bd_direction == BPF_D_INOUT)
881 m->m_flags |= M_PROMISC;
885 m->m_pkthdr.len -= hlen;
887 m->m_data += hlen; /* XXX */
889 CURVNET_SET(ifp->if_vnet);
892 mac_bpfdesc_create_mbuf(d, m);
894 mac_bpfdesc_create_mbuf(d, mc);
898 error = (*ifp->if_output)(ifp, m, &dst, NULL);
904 (*ifp->if_input)(ifp, mc);
914 * Reset a descriptor by flushing its packet buffer and clearing the receive
915 * and drop counts. This is doable for kernel-only buffers, but with
916 * zero-copy buffers, we can't write to (or rotate) buffers that are
917 * currently owned by userspace. It would be nice if we could encapsulate
918 * this logic in the buffer code rather than here.
921 reset_d(struct bpf_d *d)
924 mtx_assert(&d->bd_mtx, MA_OWNED);
926 if ((d->bd_hbuf != NULL) &&
927 (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) {
928 /* Free the hold buffer. */
929 d->bd_fbuf = d->bd_hbuf;
932 bpf_buf_reclaimed(d);
934 if (bpf_canwritebuf(d))
946 * FIONREAD Check for read packet available.
947 * SIOCGIFADDR Get interface address - convenient hook to driver.
948 * BIOCGBLEN Get buffer len [for read()].
949 * BIOCSETF Set read filter.
950 * BIOCSETFNR Set read filter without resetting descriptor.
951 * BIOCSETWF Set write filter.
952 * BIOCFLUSH Flush read packet buffer.
953 * BIOCPROMISC Put interface into promiscuous mode.
954 * BIOCGDLT Get link layer type.
955 * BIOCGETIF Get interface name.
956 * BIOCSETIF Set interface.
957 * BIOCSRTIMEOUT Set read timeout.
958 * BIOCGRTIMEOUT Get read timeout.
959 * BIOCGSTATS Get packet stats.
960 * BIOCIMMEDIATE Set immediate mode.
961 * BIOCVERSION Get filter language version.
962 * BIOCGHDRCMPLT Get "header already complete" flag
963 * BIOCSHDRCMPLT Set "header already complete" flag
964 * BIOCGDIRECTION Get packet direction flag
965 * BIOCSDIRECTION Set packet direction flag
966 * BIOCLOCK Set "locked" flag
967 * BIOCFEEDBACK Set packet feedback mode.
968 * BIOCSETZBUF Set current zero-copy buffer locations.
969 * BIOCGETZMAX Get maximum zero-copy buffer size.
970 * BIOCROTZBUF Force rotation of zero-copy buffer
971 * BIOCSETBUFMODE Set buffer mode.
972 * BIOCGETBUFMODE Get current buffer mode.
976 bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
982 error = devfs_get_cdevpriv((void **)&d);
987 * Refresh PID associated with this descriptor.
990 d->bd_pid = td->td_proc->p_pid;
991 if (d->bd_state == BPF_WAITING)
992 callout_stop(&d->bd_callout);
993 d->bd_state = BPF_IDLE;
996 if (d->bd_locked == 1) {
1020 CURVNET_SET(TD_TO_VNET(td));
1028 * Check for read packet available.
1048 if (d->bd_bif == NULL)
1051 ifp = d->bd_bif->bif_ifp;
1052 error = (*ifp->if_ioctl)(ifp, cmd, addr);
1058 * Get buffer len [for read()].
1061 *(u_int *)addr = d->bd_bufsize;
1065 * Set buffer length.
1068 error = bpf_ioctl_sblen(d, (u_int *)addr);
1072 * Set link layer read filter.
1077 error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1081 * Flush read packet buffer.
1090 * Put interface into promiscuous mode.
1093 if (d->bd_bif == NULL) {
1095 * No interface attached yet.
1100 if (d->bd_promisc == 0) {
1101 error = ifpromisc(d->bd_bif->bif_ifp, 1);
1108 * Get current data link type.
1111 if (d->bd_bif == NULL)
1114 *(u_int *)addr = d->bd_bif->bif_dlt;
1118 * Get a list of supported data link types.
1121 if (d->bd_bif == NULL)
1124 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
1128 * Set data link type.
1131 if (d->bd_bif == NULL)
1134 error = bpf_setdlt(d, *(u_int *)addr);
1138 * Get interface name.
1141 if (d->bd_bif == NULL)
1144 struct ifnet *const ifp = d->bd_bif->bif_ifp;
1145 struct ifreq *const ifr = (struct ifreq *)addr;
1147 strlcpy(ifr->ifr_name, ifp->if_xname,
1148 sizeof(ifr->ifr_name));
1156 error = bpf_setif(d, (struct ifreq *)addr);
1164 struct timeval *tv = (struct timeval *)addr;
1167 * Subtract 1 tick from tvtohz() since this isn't
1170 if ((error = itimerfix(tv)) == 0)
1171 d->bd_rtout = tvtohz(tv) - 1;
1180 struct timeval *tv = (struct timeval *)addr;
1182 tv->tv_sec = d->bd_rtout / hz;
1183 tv->tv_usec = (d->bd_rtout % hz) * tick;
1192 struct bpf_stat *bs = (struct bpf_stat *)addr;
1194 /* XXXCSJP overflow */
1195 bs->bs_recv = d->bd_rcount;
1196 bs->bs_drop = d->bd_dcount;
1201 * Set immediate mode.
1204 d->bd_immediate = *(u_int *)addr;
1209 struct bpf_version *bv = (struct bpf_version *)addr;
1211 bv->bv_major = BPF_MAJOR_VERSION;
1212 bv->bv_minor = BPF_MINOR_VERSION;
1217 * Get "header already complete" flag
1220 *(u_int *)addr = d->bd_hdrcmplt;
1224 * Set "header already complete" flag
1227 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1231 * Get packet direction flag
1233 case BIOCGDIRECTION:
1234 *(u_int *)addr = d->bd_direction;
1238 * Set packet direction flag
1240 case BIOCSDIRECTION:
1244 direction = *(u_int *)addr;
1245 switch (direction) {
1249 d->bd_direction = direction;
1258 d->bd_feedback = *(u_int *)addr;
1265 case FIONBIO: /* Non-blocking I/O */
1268 case FIOASYNC: /* Send signal on receive packets */
1269 d->bd_async = *(int *)addr;
1273 error = fsetown(*(int *)addr, &d->bd_sigio);
1277 *(int *)addr = fgetown(&d->bd_sigio);
1280 /* This is deprecated, FIOSETOWN should be used instead. */
1282 error = fsetown(-(*(int *)addr), &d->bd_sigio);
1285 /* This is deprecated, FIOGETOWN should be used instead. */
1287 *(int *)addr = -fgetown(&d->bd_sigio);
1290 case BIOCSRSIG: /* Set receive signal */
1294 sig = *(u_int *)addr;
1303 *(u_int *)addr = d->bd_sig;
1306 case BIOCGETBUFMODE:
1307 *(u_int *)addr = d->bd_bufmode;
1310 case BIOCSETBUFMODE:
1312 * Allow the buffering mode to be changed as long as we
1313 * haven't yet committed to a particular mode. Our
1314 * definition of commitment, for now, is whether or not a
1315 * buffer has been allocated or an interface attached, since
1316 * that's the point where things get tricky.
1318 switch (*(u_int *)addr) {
1319 case BPF_BUFMODE_BUFFER:
1322 case BPF_BUFMODE_ZBUF:
1323 if (bpf_zerocopy_enable)
1332 if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
1333 d->bd_fbuf != NULL || d->bd_bif != NULL) {
1337 d->bd_bufmode = *(u_int *)addr;
1342 return (bpf_ioctl_getzmax(td, d, (size_t *)addr));
1345 return (bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr));
1348 return (bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr));
1355 * Set d's packet filter program to fp. If this file already has a filter,
1356 * free it and replace it. Returns EINVAL for bogus requests.
1359 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1361 struct bpf_insn *fcode, *old;
1362 u_int wfilter, flen, size;
1364 bpf_jit_filter *ofunc;
1367 if (cmd == BIOCSETWF) {
1368 old = d->bd_wfilter;
1375 old = d->bd_rfilter;
1377 ofunc = d->bd_bfilter;
1380 if (fp->bf_insns == NULL) {
1381 if (fp->bf_len != 0)
1385 d->bd_wfilter = NULL;
1387 d->bd_rfilter = NULL;
1389 d->bd_bfilter = NULL;
1391 if (cmd == BIOCSETF)
1396 free((caddr_t)old, M_BPF);
1399 bpf_destroy_jit_filter(ofunc);
1404 if (flen > bpf_maxinsns)
1407 size = flen * sizeof(*fp->bf_insns);
1408 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1409 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1410 bpf_validate(fcode, (int)flen)) {
1413 d->bd_wfilter = fcode;
1415 d->bd_rfilter = fcode;
1417 d->bd_bfilter = bpf_jitter(fcode, flen);
1419 if (cmd == BIOCSETF)
1424 free((caddr_t)old, M_BPF);
1427 bpf_destroy_jit_filter(ofunc);
1432 free((caddr_t)fcode, M_BPF);
1437 * Detach a file from its current interface (if attached at all) and attach
1438 * to the interface indicated by the name stored in ifr.
1439 * Return an errno or 0.
1442 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1445 struct ifnet *theywant;
1447 theywant = ifunit(ifr->ifr_name);
1448 if (theywant == NULL || theywant->if_bpf == NULL)
1451 bp = theywant->if_bpf;
1454 * Behavior here depends on the buffering model. If we're using
1455 * kernel memory buffers, then we can allocate them here. If we're
1456 * using zero-copy, then the user process must have registered
1457 * buffers by the time we get here. If not, return an error.
1459 * XXXRW: There are locking issues here with multi-threaded use: what
1460 * if two threads try to set the interface at once?
1462 switch (d->bd_bufmode) {
1463 case BPF_BUFMODE_BUFFER:
1464 if (d->bd_sbuf == NULL)
1465 bpf_buffer_alloc(d);
1466 KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL"));
1469 case BPF_BUFMODE_ZBUF:
1470 if (d->bd_sbuf == NULL)
1475 panic("bpf_setif: bufmode %d", d->bd_bufmode);
1477 if (bp != d->bd_bif) {
1480 * Detach if attached to something else.
1493 * Support for select() and poll() system calls
1495 * Return true iff the specific operation will not block indefinitely.
1496 * Otherwise, return false but make a note that a selwakeup() must be done.
1499 bpfpoll(struct cdev *dev, int events, struct thread *td)
1504 if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL)
1506 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1509 * Refresh PID associated with this descriptor.
1511 revents = events & (POLLOUT | POLLWRNORM);
1513 d->bd_pid = td->td_proc->p_pid;
1514 if (events & (POLLIN | POLLRDNORM)) {
1516 revents |= events & (POLLIN | POLLRDNORM);
1518 selrecord(td, &d->bd_sel);
1519 /* Start the read timeout if necessary. */
1520 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1521 callout_reset(&d->bd_callout, d->bd_rtout,
1523 d->bd_state = BPF_WAITING;
1532 * Support for kevent() system call. Register EVFILT_READ filters and
1533 * reject all others.
1536 bpfkqfilter(struct cdev *dev, struct knote *kn)
1540 if (devfs_get_cdevpriv((void **)&d) != 0 ||
1541 kn->kn_filter != EVFILT_READ)
1545 * Refresh PID associated with this descriptor.
1548 d->bd_pid = curthread->td_proc->p_pid;
1549 kn->kn_fop = &bpfread_filtops;
1551 knlist_add(&d->bd_sel.si_note, kn, 1);
1558 filt_bpfdetach(struct knote *kn)
1560 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1562 knlist_remove(&d->bd_sel.si_note, kn, 0);
1566 filt_bpfread(struct knote *kn, long hint)
1568 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1571 BPFD_LOCK_ASSERT(d);
1572 ready = bpf_ready(d);
1574 kn->kn_data = d->bd_slen;
1576 kn->kn_data += d->bd_hlen;
1578 else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1579 callout_reset(&d->bd_callout, d->bd_rtout,
1581 d->bd_state = BPF_WAITING;
1588 * Incoming linkage from device drivers. Process the packet pkt, of length
1589 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1590 * by each process' filter, and if accepted, stashed into the corresponding
1594 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1606 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1610 * NB: We dont call BPF_CHECK_DIRECTION() here since there is no
1611 * way for the caller to indiciate to us whether this packet
1612 * is inbound or outbound. In the bpf_mtap() routines, we use
1613 * the interface pointers on the mbuf to figure it out.
1616 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1618 slen = (*(bf->func))(pkt, pktlen, pktlen);
1621 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1629 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1631 catchpacket(d, pkt, pktlen, slen,
1632 bpf_append_bytes, &tv);
1639 #define BPF_CHECK_DIRECTION(d, r, i) \
1640 (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \
1641 ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
1644 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1647 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1657 /* Skip outgoing duplicate packets. */
1658 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1659 m->m_flags &= ~M_PROMISC;
1665 pktlen = m_length(m, NULL);
1668 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1669 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1674 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1675 /* XXX We cannot handle multiple mbufs. */
1676 if (bf != NULL && m->m_next == NULL)
1677 slen = (*(bf->func))(mtod(m, u_char *), pktlen, pktlen);
1680 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1688 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1690 catchpacket(d, (u_char *)m, pktlen, slen,
1691 bpf_append_mbuf, &tv);
1699 * Incoming linkage from device drivers, when packet is in
1700 * an mbuf chain and to be prepended by a contiguous header.
1703 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1711 /* Skip outgoing duplicate packets. */
1712 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1713 m->m_flags &= ~M_PROMISC;
1719 pktlen = m_length(m, NULL);
1721 * Craft on-stack mbuf suitable for passing to bpf_filter.
1722 * Note that we cut corners here; we only setup what's
1723 * absolutely needed--this mbuf should never go anywhere else.
1731 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1732 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1736 slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1744 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1746 catchpacket(d, (u_char *)&mb, pktlen, slen,
1747 bpf_append_mbuf, &tv);
1754 #undef BPF_CHECK_DIRECTION
1757 * Move the packet data from interface memory (pkt) into the
1758 * store buffer. "cpfn" is the routine called to do the actual data
1759 * transfer. bcopy is passed in to copy contiguous chunks, while
1760 * bpf_append_mbuf is passed in to copy mbuf chains. In the latter case,
1761 * pkt is really an mbuf.
1764 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1765 void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
1770 int hdrlen = d->bd_bif->bif_hdrlen;
1773 BPFD_LOCK_ASSERT(d);
1776 * Detect whether user space has released a buffer back to us, and if
1777 * so, move it from being a hold buffer to a free buffer. This may
1778 * not be the best place to do it (for example, we might only want to
1779 * run this check if we need the space), but for now it's a reliable
1782 if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
1783 d->bd_fbuf = d->bd_hbuf;
1786 bpf_buf_reclaimed(d);
1790 * Figure out how many bytes to move. If the packet is
1791 * greater or equal to the snapshot length, transfer that
1792 * much. Otherwise, transfer the whole packet (unless
1793 * we hit the buffer size limit).
1795 totlen = hdrlen + min(snaplen, pktlen);
1796 if (totlen > d->bd_bufsize)
1797 totlen = d->bd_bufsize;
1800 * Round up the end of the previous packet to the next longword.
1802 * Drop the packet if there's no room and no hope of room
1803 * If the packet would overflow the storage buffer or the storage
1804 * buffer is considered immutable by the buffer model, try to rotate
1805 * the buffer and wakeup pending processes.
1807 curlen = BPF_WORDALIGN(d->bd_slen);
1808 if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
1809 if (d->bd_fbuf == NULL) {
1811 * There's no room in the store buffer, and no
1812 * prospect of room, so drop the packet. Notify the
1822 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1824 * Immediate mode is set, or the read timeout has already
1825 * expired during a select call. A packet arrived, so the
1826 * reader should be woken up.
1831 * Append the bpf header. Note we append the actual header size, but
1832 * move forward the length of the header plus padding.
1834 bzero(&hdr, sizeof(hdr));
1835 hdr.bh_tstamp = *tv;
1836 hdr.bh_datalen = pktlen;
1837 hdr.bh_hdrlen = hdrlen;
1838 hdr.bh_caplen = totlen - hdrlen;
1839 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
1842 * Copy the packet data into the store buffer and update its length.
1844 (*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, hdr.bh_caplen);
1845 d->bd_slen = curlen + totlen;
1852 * Free buffers currently in use by a descriptor.
1856 bpf_freed(struct bpf_d *d)
1860 * We don't need to lock out interrupts since this descriptor has
1861 * been detached from its interface and it yet hasn't been marked
1865 if (d->bd_rfilter) {
1866 free((caddr_t)d->bd_rfilter, M_BPF);
1868 bpf_destroy_jit_filter(d->bd_bfilter);
1872 free((caddr_t)d->bd_wfilter, M_BPF);
1873 mtx_destroy(&d->bd_mtx);
1877 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
1878 * fixed size of the link header (variable length headers not yet supported).
1881 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1884 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1888 * Attach an interface to bpf. ifp is a pointer to the structure
1889 * defining the interface to be attached, dlt is the link layer type,
1890 * and hdrlen is the fixed size of the link header (variable length
1891 * headers are not yet supporrted).
1894 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1898 bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1902 LIST_INIT(&bp->bif_dlist);
1905 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1906 KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
1910 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1911 mtx_unlock(&bpf_mtx);
1914 * Compute the length of the bpf header. This is not necessarily
1915 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1916 * that the network layer header begins on a longword boundary (for
1917 * performance reasons and to alleviate alignment restrictions).
1919 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1922 if_printf(ifp, "bpf attached\n");
1926 * Detach bpf from an interface. This involves detaching each descriptor
1927 * associated with the interface, and leaving bd_bif NULL. Notify each
1928 * descriptor as it's detached so that any sleepers wake up and get
1932 bpfdetach(struct ifnet *ifp)
1937 /* Locate BPF interface information */
1939 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1940 if (ifp == bp->bif_ifp)
1944 /* Interface wasn't attached */
1945 if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1946 mtx_unlock(&bpf_mtx);
1947 printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1951 LIST_REMOVE(bp, bif_next);
1952 mtx_unlock(&bpf_mtx);
1954 while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1961 mtx_destroy(&bp->bif_mtx);
1966 * Get a list of available data link type of the interface.
1969 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1975 ifp = d->bd_bif->bif_ifp;
1979 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1980 if (bp->bif_ifp != ifp)
1982 if (bfl->bfl_list != NULL) {
1983 if (n >= bfl->bfl_len) {
1984 mtx_unlock(&bpf_mtx);
1987 error = copyout(&bp->bif_dlt,
1988 bfl->bfl_list + n, sizeof(u_int));
1992 mtx_unlock(&bpf_mtx);
1998 * Set the data link type of a BPF instance.
2001 bpf_setdlt(struct bpf_d *d, u_int dlt)
2003 int error, opromisc;
2007 if (d->bd_bif->bif_dlt == dlt)
2009 ifp = d->bd_bif->bif_ifp;
2011 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2012 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2015 mtx_unlock(&bpf_mtx);
2017 opromisc = d->bd_promisc;
2024 error = ifpromisc(bp->bif_ifp, 1);
2026 if_printf(bp->bif_ifp,
2027 "bpf_setdlt: ifpromisc failed (%d)\n",
2033 return (bp == NULL ? EINVAL : 0);
2037 bpf_drvinit(void *unused)
2041 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
2042 LIST_INIT(&bpf_iflist);
2044 dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf");
2045 /* For compatibility */
2046 make_dev_alias(dev, "bpf0");
2050 * Zero out the various packet counters associated with all of the bpf
2051 * descriptors. At some point, we will probably want to get a bit more
2052 * granular and allow the user to specify descriptors to be zeroed.
2055 bpf_zero_counters(void)
2061 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2063 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2075 mtx_unlock(&bpf_mtx);
2079 bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
2082 bzero(d, sizeof(*d));
2083 BPFD_LOCK_ASSERT(bd);
2084 d->bd_structsize = sizeof(*d);
2085 d->bd_immediate = bd->bd_immediate;
2086 d->bd_promisc = bd->bd_promisc;
2087 d->bd_hdrcmplt = bd->bd_hdrcmplt;
2088 d->bd_direction = bd->bd_direction;
2089 d->bd_feedback = bd->bd_feedback;
2090 d->bd_async = bd->bd_async;
2091 d->bd_rcount = bd->bd_rcount;
2092 d->bd_dcount = bd->bd_dcount;
2093 d->bd_fcount = bd->bd_fcount;
2094 d->bd_sig = bd->bd_sig;
2095 d->bd_slen = bd->bd_slen;
2096 d->bd_hlen = bd->bd_hlen;
2097 d->bd_bufsize = bd->bd_bufsize;
2098 d->bd_pid = bd->bd_pid;
2099 strlcpy(d->bd_ifname,
2100 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
2101 d->bd_locked = bd->bd_locked;
2102 d->bd_wcount = bd->bd_wcount;
2103 d->bd_wdcount = bd->bd_wdcount;
2104 d->bd_wfcount = bd->bd_wfcount;
2105 d->bd_zcopy = bd->bd_zcopy;
2106 d->bd_bufmode = bd->bd_bufmode;
2110 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
2112 struct xbpf_d *xbdbuf, *xbd, zerostats;
2118 * XXX This is not technically correct. It is possible for non
2119 * privileged users to open bpf devices. It would make sense
2120 * if the users who opened the devices were able to retrieve
2121 * the statistics for them, too.
2123 error = priv_check(req->td, PRIV_NET_BPF);
2127 * Check to see if the user is requesting that the counters be
2128 * zeroed out. Explicitly check that the supplied data is zeroed,
2129 * as we aren't allowing the user to set the counters currently.
2131 if (req->newptr != NULL) {
2132 if (req->newlen != sizeof(zerostats))
2134 bzero(&zerostats, sizeof(zerostats));
2136 if (bcmp(xbd, &zerostats, sizeof(*xbd)) != 0)
2138 bpf_zero_counters();
2141 if (req->oldptr == NULL)
2142 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
2143 if (bpf_bpfd_cnt == 0)
2144 return (SYSCTL_OUT(req, 0, 0));
2145 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
2147 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2148 mtx_unlock(&bpf_mtx);
2149 free(xbdbuf, M_BPF);
2153 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2155 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2156 xbd = &xbdbuf[index++];
2158 bpfstats_fill_xbpf(xbd, bd);
2163 mtx_unlock(&bpf_mtx);
2164 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2165 free(xbdbuf, M_BPF);
2169 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
2171 #else /* !DEV_BPF && !NETGRAPH_BPF */
2173 * NOP stubs to allow bpf-using drivers to load and function.
2175 * A 'better' implementation would allow the core bpf functionality
2176 * to be loaded at runtime.
2178 static struct bpf_if bp_null;
2181 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2186 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2191 bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
2196 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2199 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2203 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
2206 *driverp = &bp_null;
2210 bpfdetach(struct ifnet *ifp)
2215 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
2217 return -1; /* "no filter" behaviour */
2221 bpf_validate(const struct bpf_insn *f, int len)
2223 return 0; /* false */
2226 #endif /* !DEV_BPF && !NETGRAPH_BPF */