2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2019 Andrey V. Elsukov <ae@FreeBSD.org>
8 * This code is derived from the Stanford/CMU enet packet filter,
9 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
10 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
11 * Berkeley Laboratory.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 #include "opt_netgraph.h"
47 #include <sys/types.h>
48 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
59 #include <sys/signalvar.h>
60 #include <sys/filio.h>
61 #include <sys/sockio.h>
62 #include <sys/ttycom.h>
64 #include <sys/sysent.h>
66 #include <sys/event.h>
71 #include <sys/socket.h>
78 #include <net/if_var.h>
79 #include <net/if_dl.h>
81 #include <net/bpf_buffer.h>
83 #include <net/bpf_jitter.h>
85 #include <net/bpf_zerocopy.h>
86 #include <net/bpfdesc.h>
87 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/if_ether.h>
92 #include <sys/kernel.h>
93 #include <sys/sysctl.h>
95 #include <net80211/ieee80211_freebsd.h>
97 #include <security/mac/mac_framework.h>
99 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
101 static struct bpf_if_ext dead_bpf_if = {
102 .bif_dlist = CK_LIST_HEAD_INITIALIZER()
106 #define bif_next bif_ext.bif_next
107 #define bif_dlist bif_ext.bif_dlist
108 struct bpf_if_ext bif_ext; /* public members */
109 u_int bif_dlt; /* link layer type */
110 u_int bif_hdrlen; /* length of link header */
111 struct bpfd_list bif_wlist; /* writer-only list */
112 struct ifnet *bif_ifp; /* corresponding interface */
113 struct bpf_if **bif_bpf; /* Pointer to pointer to us */
114 volatile u_int bif_refcnt;
115 struct epoch_context epoch_ctx;
118 CTASSERT(offsetof(struct bpf_if, bif_ext) == 0);
120 struct bpf_program_buffer {
121 struct epoch_context epoch_ctx;
123 bpf_jit_filter *func;
128 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
130 #define PRINET 26 /* interruptible */
132 #define SIZEOF_BPF_HDR(type) \
133 (offsetof(type, bh_hdrlen) + sizeof(((type *)0)->bh_hdrlen))
135 #ifdef COMPAT_FREEBSD32
136 #include <sys/mount.h>
137 #include <compat/freebsd32/freebsd32.h>
138 #define BPF_ALIGNMENT32 sizeof(int32_t)
139 #define BPF_WORDALIGN32(x) roundup2(x, BPF_ALIGNMENT32)
143 * 32-bit version of structure prepended to each packet. We use this header
144 * instead of the standard one for 32-bit streams. We mark the a stream as
145 * 32-bit the first time we see a 32-bit compat ioctl request.
148 struct timeval32 bh_tstamp; /* time stamp */
149 uint32_t bh_caplen; /* length of captured portion */
150 uint32_t bh_datalen; /* original length of packet */
151 uint16_t bh_hdrlen; /* length of bpf header (this struct
152 plus alignment padding) */
156 struct bpf_program32 {
161 struct bpf_dltlist32 {
166 #define BIOCSETF32 _IOW('B', 103, struct bpf_program32)
167 #define BIOCSRTIMEOUT32 _IOW('B', 109, struct timeval32)
168 #define BIOCGRTIMEOUT32 _IOR('B', 110, struct timeval32)
169 #define BIOCGDLTLIST32 _IOWR('B', 121, struct bpf_dltlist32)
170 #define BIOCSETWF32 _IOW('B', 123, struct bpf_program32)
171 #define BIOCSETFNR32 _IOW('B', 130, struct bpf_program32)
174 #define BPF_LOCK() sx_xlock(&bpf_sx)
175 #define BPF_UNLOCK() sx_xunlock(&bpf_sx)
176 #define BPF_LOCK_ASSERT() sx_assert(&bpf_sx, SA_XLOCKED)
178 * bpf_iflist is a list of BPF interface structures, each corresponding to a
179 * specific DLT. The same network interface might have several BPF interface
180 * structures registered by different layers in the stack (i.e., 802.11
181 * frames, ethernet frames, etc).
183 CK_LIST_HEAD(bpf_iflist, bpf_if);
184 static struct bpf_iflist bpf_iflist;
185 static struct sx bpf_sx; /* bpf global lock */
186 static int bpf_bpfd_cnt;
188 static void bpfif_ref(struct bpf_if *);
189 static void bpfif_rele(struct bpf_if *);
191 static void bpfd_ref(struct bpf_d *);
192 static void bpfd_rele(struct bpf_d *);
193 static void bpf_attachd(struct bpf_d *, struct bpf_if *);
194 static void bpf_detachd(struct bpf_d *);
195 static void bpf_detachd_locked(struct bpf_d *, bool);
196 static void bpfd_free(epoch_context_t);
197 static int bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
198 struct sockaddr *, int *, struct bpf_d *);
199 static int bpf_setif(struct bpf_d *, struct ifreq *);
200 static void bpf_timed_out(void *);
202 bpf_wakeup(struct bpf_d *);
203 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
204 void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
206 static void reset_d(struct bpf_d *);
207 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
208 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
209 static int bpf_setdlt(struct bpf_d *, u_int);
210 static void filt_bpfdetach(struct knote *);
211 static int filt_bpfread(struct knote *, long);
212 static void bpf_drvinit(void *);
213 static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
215 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
216 int bpf_maxinsns = BPF_MAXINSNS;
217 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
218 &bpf_maxinsns, 0, "Maximum bpf program instructions");
219 static int bpf_zerocopy_enable = 0;
220 SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
221 &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
222 static SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
223 bpf_stats_sysctl, "bpf statistics portal");
225 VNET_DEFINE_STATIC(int, bpf_optimize_writers) = 0;
226 #define V_bpf_optimize_writers VNET(bpf_optimize_writers)
227 SYSCTL_INT(_net_bpf, OID_AUTO, optimize_writers, CTLFLAG_VNET | CTLFLAG_RW,
228 &VNET_NAME(bpf_optimize_writers), 0,
229 "Do not send packets until BPF program is set");
231 static d_open_t bpfopen;
232 static d_read_t bpfread;
233 static d_write_t bpfwrite;
234 static d_ioctl_t bpfioctl;
235 static d_poll_t bpfpoll;
236 static d_kqfilter_t bpfkqfilter;
238 static struct cdevsw bpf_cdevsw = {
239 .d_version = D_VERSION,
246 .d_kqfilter = bpfkqfilter,
249 static struct filterops bpfread_filtops = {
251 .f_detach = filt_bpfdetach,
252 .f_event = filt_bpfread,
256 * LOCKING MODEL USED BY BPF
259 * 1) global lock (BPF_LOCK). Sx, used to protect some global counters,
260 * every bpf_iflist changes, serializes ioctl access to bpf descriptors.
261 * 2) Descriptor lock. Mutex, used to protect BPF buffers and various
262 * structure fields used by bpf_*tap* code.
264 * Lock order: global lock, then descriptor lock.
266 * There are several possible consumers:
268 * 1. The kernel registers interface pointer with bpfattach().
269 * Each call allocates new bpf_if structure, references ifnet pointer
270 * and links bpf_if into bpf_iflist chain. This is protected with global
273 * 2. An userland application uses ioctl() call to bpf_d descriptor.
274 * All such call are serialized with global lock. BPF filters can be
275 * changed, but pointer to old filter will be freed using epoch_call().
276 * Thus it should be safe for bpf_tap/bpf_mtap* code to do access to
277 * filter pointers, even if change will happen during bpf_tap execution.
278 * Destroying of bpf_d descriptor also is doing using epoch_call().
280 * 3. An userland application can write packets into bpf_d descriptor.
281 * There we need to be sure, that ifnet won't disappear during bpfwrite().
283 * 4. The kernel invokes bpf_tap/bpf_mtap* functions. The access to
284 * bif_dlist is protected with net_epoch_preempt section. So, it should
285 * be safe to make access to bpf_d descriptor inside the section.
287 * 5. The kernel invokes bpfdetach() on interface destroying. All lists
288 * are modified with global lock held and actual free() is done using
293 bpfif_free(epoch_context_t ctx)
297 bp = __containerof(ctx, struct bpf_if, epoch_ctx);
298 if_rele(bp->bif_ifp);
303 bpfif_ref(struct bpf_if *bp)
306 refcount_acquire(&bp->bif_refcnt);
310 bpfif_rele(struct bpf_if *bp)
313 if (!refcount_release(&bp->bif_refcnt))
315 epoch_call(net_epoch_preempt, &bp->epoch_ctx, bpfif_free);
319 bpfd_ref(struct bpf_d *d)
322 refcount_acquire(&d->bd_refcnt);
326 bpfd_rele(struct bpf_d *d)
329 if (!refcount_release(&d->bd_refcnt))
331 epoch_call(net_epoch_preempt, &d->epoch_ctx, bpfd_free);
334 static struct bpf_program_buffer*
335 bpf_program_buffer_alloc(size_t size, int flags)
338 return (malloc(sizeof(struct bpf_program_buffer) + size,
343 bpf_program_buffer_free(epoch_context_t ctx)
345 struct bpf_program_buffer *ptr;
347 ptr = __containerof(ctx, struct bpf_program_buffer, epoch_ctx);
349 if (ptr->func != NULL)
350 bpf_destroy_jit_filter(ptr->func);
356 * Wrapper functions for various buffering methods. If the set of buffer
357 * modes expands, we will probably want to introduce a switch data structure
358 * similar to protosw, et.
361 bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
367 switch (d->bd_bufmode) {
368 case BPF_BUFMODE_BUFFER:
369 return (bpf_buffer_append_bytes(d, buf, offset, src, len));
371 case BPF_BUFMODE_ZBUF:
372 counter_u64_add(d->bd_zcopy, 1);
373 return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
376 panic("bpf_buf_append_bytes");
381 bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
387 switch (d->bd_bufmode) {
388 case BPF_BUFMODE_BUFFER:
389 return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
391 case BPF_BUFMODE_ZBUF:
392 counter_u64_add(d->bd_zcopy, 1);
393 return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
396 panic("bpf_buf_append_mbuf");
401 * This function gets called when the free buffer is re-assigned.
404 bpf_buf_reclaimed(struct bpf_d *d)
409 switch (d->bd_bufmode) {
410 case BPF_BUFMODE_BUFFER:
413 case BPF_BUFMODE_ZBUF:
414 bpf_zerocopy_buf_reclaimed(d);
418 panic("bpf_buf_reclaimed");
423 * If the buffer mechanism has a way to decide that a held buffer can be made
424 * free, then it is exposed via the bpf_canfreebuf() interface. (1) is
425 * returned if the buffer can be discarded, (0) is returned if it cannot.
428 bpf_canfreebuf(struct bpf_d *d)
433 switch (d->bd_bufmode) {
434 case BPF_BUFMODE_ZBUF:
435 return (bpf_zerocopy_canfreebuf(d));
441 * Allow the buffer model to indicate that the current store buffer is
442 * immutable, regardless of the appearance of space. Return (1) if the
443 * buffer is writable, and (0) if not.
446 bpf_canwritebuf(struct bpf_d *d)
450 switch (d->bd_bufmode) {
451 case BPF_BUFMODE_ZBUF:
452 return (bpf_zerocopy_canwritebuf(d));
458 * Notify buffer model that an attempt to write to the store buffer has
459 * resulted in a dropped packet, in which case the buffer may be considered
463 bpf_buffull(struct bpf_d *d)
468 switch (d->bd_bufmode) {
469 case BPF_BUFMODE_ZBUF:
470 bpf_zerocopy_buffull(d);
476 * Notify the buffer model that a buffer has moved into the hold position.
479 bpf_bufheld(struct bpf_d *d)
484 switch (d->bd_bufmode) {
485 case BPF_BUFMODE_ZBUF:
486 bpf_zerocopy_bufheld(d);
492 bpf_free(struct bpf_d *d)
495 switch (d->bd_bufmode) {
496 case BPF_BUFMODE_BUFFER:
497 return (bpf_buffer_free(d));
499 case BPF_BUFMODE_ZBUF:
500 return (bpf_zerocopy_free(d));
503 panic("bpf_buf_free");
508 bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
511 if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
513 return (bpf_buffer_uiomove(d, buf, len, uio));
517 bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
520 if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
522 return (bpf_buffer_ioctl_sblen(d, i));
526 bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
529 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
531 return (bpf_zerocopy_ioctl_getzmax(td, d, i));
535 bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
538 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
540 return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
544 bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
547 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
549 return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
553 * General BPF functions.
556 bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
557 struct sockaddr *sockp, int *hdrlen, struct bpf_d *d)
559 const struct ieee80211_bpf_params *p;
560 struct ether_header *eh;
568 * Build a sockaddr based on the data link layer type.
569 * We do this at this level because the ethernet header
570 * is copied directly into the data field of the sockaddr.
571 * In the case of SLIP, there is no header and the packet
572 * is forwarded as is.
573 * Also, we are careful to leave room at the front of the mbuf
574 * for the link level header.
579 sockp->sa_family = AF_INET;
584 sockp->sa_family = AF_UNSPEC;
585 /* XXX Would MAXLINKHDR be better? */
586 hlen = ETHER_HDR_LEN;
590 sockp->sa_family = AF_IMPLINK;
595 sockp->sa_family = AF_UNSPEC;
601 * null interface types require a 4 byte pseudo header which
602 * corresponds to the address family of the packet.
604 sockp->sa_family = AF_UNSPEC;
608 case DLT_ATM_RFC1483:
610 * en atm driver requires 4-byte atm pseudo header.
611 * though it isn't standard, vpi:vci needs to be
614 sockp->sa_family = AF_UNSPEC;
615 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
619 sockp->sa_family = AF_UNSPEC;
620 hlen = 4; /* This should match PPP_HDRLEN */
623 case DLT_IEEE802_11: /* IEEE 802.11 wireless */
624 sockp->sa_family = AF_IEEE80211;
628 case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */
629 sockp->sa_family = AF_IEEE80211;
630 sockp->sa_len = 12; /* XXX != 0 */
631 hlen = sizeof(struct ieee80211_bpf_params);
638 len = uio->uio_resid;
639 if (len < hlen || len - hlen > ifp->if_mtu)
642 m = m_get2(len, M_WAITOK, MT_DATA, M_PKTHDR);
645 m->m_pkthdr.len = m->m_len = len;
648 error = uiomove(mtod(m, u_char *), len, uio);
652 slen = bpf_filter(d->bd_wfilter, mtod(m, u_char *), len, len);
658 /* Check for multicast destination */
661 eh = mtod(m, struct ether_header *);
662 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
663 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
664 ETHER_ADDR_LEN) == 0)
665 m->m_flags |= M_BCAST;
667 m->m_flags |= M_MCAST;
669 if (d->bd_hdrcmplt == 0) {
670 memcpy(eh->ether_shost, IF_LLADDR(ifp),
671 sizeof(eh->ether_shost));
677 * Make room for link header, and copy it to sockaddr
680 if (sockp->sa_family == AF_IEEE80211) {
682 * Collect true length from the parameter header
683 * NB: sockp is known to be zero'd so if we do a
684 * short copy unspecified parameters will be
686 * NB: packet may not be aligned after stripping
690 p = mtod(m, const struct ieee80211_bpf_params *);
692 if (hlen > sizeof(sockp->sa_data)) {
697 bcopy(mtod(m, const void *), sockp->sa_data, hlen);
708 * Attach descriptor to the bpf interface, i.e. make d listen on bp,
709 * then reset its buffers and counters with reset_d().
712 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
719 * Save sysctl value to protect from sysctl change
722 op_w = V_bpf_optimize_writers || d->bd_writer;
724 if (d->bd_bif != NULL)
725 bpf_detachd_locked(d, false);
727 * Point d at bp, and add d to the interface's list.
728 * Since there are many applications using BPF for
729 * sending raw packets only (dhcpd, cdpd are good examples)
730 * we can delay adding d to the list of active listeners until
731 * some filter is configured.
736 * Hold reference to bpif while descriptor uses this interface.
741 /* Add to writers-only list */
742 CK_LIST_INSERT_HEAD(&bp->bif_wlist, d, bd_next);
744 * We decrement bd_writer on every filter set operation.
745 * First BIOCSETF is done by pcap_open_live() to set up
746 * snap length. After that appliation usually sets its own
751 CK_LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
757 CTR3(KTR_NET, "%s: bpf_attach called by pid %d, adding to %s list",
758 __func__, d->bd_pid, d->bd_writer ? "writer" : "active");
761 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
765 * Check if we need to upgrade our descriptor @d from write-only mode.
768 bpf_check_upgrade(u_long cmd, struct bpf_d *d, struct bpf_insn *fcode,
771 int is_snap, need_upgrade;
774 * Check if we've already upgraded or new filter is empty.
776 if (d->bd_writer == 0 || fcode == NULL)
782 * Check if cmd looks like snaplen setting from
783 * pcap_bpf.c:pcap_open_live().
784 * Note we're not checking .k value here:
785 * while pcap_open_live() definitely sets to non-zero value,
786 * we'd prefer to treat k=0 (deny ALL) case the same way: e.g.
787 * do not consider upgrading immediately
789 if (cmd == BIOCSETF && flen == 1 &&
790 fcode[0].code == (BPF_RET | BPF_K))
797 * We're setting first filter and it doesn't look like
798 * setting snaplen. We're probably using bpf directly.
799 * Upgrade immediately.
804 * Do not require upgrade by first BIOCSETF
805 * (used to set snaplen) by pcap_open_live().
808 if (--d->bd_writer == 0) {
810 * First snaplen filter has already
811 * been set. This is probably catch-all
819 "%s: filter function set by pid %d, "
820 "bd_writer counter %d, snap %d upgrade %d",
821 __func__, d->bd_pid, d->bd_writer,
822 is_snap, need_upgrade);
824 return (need_upgrade);
828 * Detach a file from its interface.
831 bpf_detachd(struct bpf_d *d)
834 bpf_detachd_locked(d, false);
839 bpf_detachd_locked(struct bpf_d *d, bool detached_ifp)
846 CTR2(KTR_NET, "%s: detach required by pid %d", __func__, d->bd_pid);
848 /* Check if descriptor is attached */
849 if ((bp = d->bd_bif) == NULL)
853 /* Remove d from the interface's descriptor list. */
854 CK_LIST_REMOVE(d, bd_next);
855 /* Save bd_writer value */
856 error = d->bd_writer;
861 * Notify descriptor as it's detached, so that any
862 * sleepers wake up and get ENXIO.
869 /* Call event handler iff d is attached */
871 EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0);
874 * Check if this descriptor had requested promiscuous mode.
875 * If so and ifnet is not detached, turn it off.
877 if (d->bd_promisc && !detached_ifp) {
879 CURVNET_SET(ifp->if_vnet);
880 error = ifpromisc(ifp, 0);
882 if (error != 0 && error != ENXIO) {
884 * ENXIO can happen if a pccard is unplugged
885 * Something is really wrong if we were able to put
886 * the driver into promiscuous mode, but can't
889 if_printf(bp->bif_ifp,
890 "bpf_detach: ifpromisc failed (%d)\n", error);
897 * Close the descriptor by detaching it from its interface,
898 * deallocating its buffers, and marking it free.
903 struct bpf_d *d = data;
906 if (d->bd_state == BPF_WAITING)
907 callout_stop(&d->bd_callout);
908 d->bd_state = BPF_IDLE;
910 funsetown(&d->bd_sigio);
913 mac_bpfdesc_destroy(d);
915 seldrain(&d->bd_sel);
916 knlist_destroy(&d->bd_sel.si_note);
917 callout_drain(&d->bd_callout);
922 * Open ethernet device. Returns ENXIO for illegal minor device number,
923 * EBUSY if file is open by another process.
927 bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
932 d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
933 error = devfs_set_cdevpriv(d, bpf_dtor);
940 d->bd_rcount = counter_u64_alloc(M_WAITOK);
941 d->bd_dcount = counter_u64_alloc(M_WAITOK);
942 d->bd_fcount = counter_u64_alloc(M_WAITOK);
943 d->bd_wcount = counter_u64_alloc(M_WAITOK);
944 d->bd_wfcount = counter_u64_alloc(M_WAITOK);
945 d->bd_wdcount = counter_u64_alloc(M_WAITOK);
946 d->bd_zcopy = counter_u64_alloc(M_WAITOK);
949 * For historical reasons, perform a one-time initialization call to
950 * the buffer routines, even though we're not yet committed to a
951 * particular buffer method.
954 if ((flags & FREAD) == 0)
956 d->bd_hbuf_in_use = 0;
957 d->bd_bufmode = BPF_BUFMODE_BUFFER;
959 d->bd_direction = BPF_D_INOUT;
961 BPF_PID_REFRESH(d, td);
964 mac_bpfdesc_create(td->td_ucred, d);
966 mtx_init(&d->bd_lock, devtoname(dev), "bpf cdev lock", MTX_DEF);
967 callout_init_mtx(&d->bd_callout, &d->bd_lock, 0);
968 knlist_init_mtx(&d->bd_sel.si_note, &d->bd_lock);
974 * bpfread - read next chunk of packets from buffers
977 bpfread(struct cdev *dev, struct uio *uio, int ioflag)
984 error = devfs_get_cdevpriv((void **)&d);
989 * Restrict application to use a buffer the same size as
992 if (uio->uio_resid != d->bd_bufsize)
995 non_block = ((ioflag & O_NONBLOCK) != 0);
998 BPF_PID_REFRESH_CUR(d);
999 if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
1001 return (EOPNOTSUPP);
1003 if (d->bd_state == BPF_WAITING)
1004 callout_stop(&d->bd_callout);
1005 timed_out = (d->bd_state == BPF_TIMED_OUT);
1006 d->bd_state = BPF_IDLE;
1007 while (d->bd_hbuf_in_use) {
1008 error = mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock,
1009 PRINET|PCATCH, "bd_hbuf", 0);
1016 * If the hold buffer is empty, then do a timed sleep, which
1017 * ends when the timeout expires or when enough packets
1018 * have arrived to fill the store buffer.
1020 while (d->bd_hbuf == NULL) {
1021 if (d->bd_slen != 0) {
1023 * A packet(s) either arrived since the previous
1024 * read or arrived while we were asleep.
1026 if (d->bd_immediate || non_block || timed_out) {
1028 * Rotate the buffers and return what's here
1029 * if we are in immediate mode, non-blocking
1030 * flag is set, or this descriptor timed out.
1038 * No data is available, check to see if the bpf device
1039 * is still pointed at a real interface. If not, return
1040 * ENXIO so that the userland process knows to rebind
1041 * it before using it again.
1043 if (d->bd_bif == NULL) {
1050 return (EWOULDBLOCK);
1052 error = msleep(d, &d->bd_lock, PRINET|PCATCH,
1053 "bpf", d->bd_rtout);
1054 if (error == EINTR || error == ERESTART) {
1058 if (error == EWOULDBLOCK) {
1060 * On a timeout, return what's in the buffer,
1061 * which may be nothing. If there is something
1062 * in the store buffer, we can rotate the buffers.
1066 * We filled up the buffer in between
1067 * getting the timeout and arriving
1068 * here, so we don't need to rotate.
1072 if (d->bd_slen == 0) {
1081 * At this point, we know we have something in the hold slot.
1083 d->bd_hbuf_in_use = 1;
1087 * Move data from hold buffer into user space.
1088 * We know the entire buffer is transferred since
1089 * we checked above that the read buffer is bpf_bufsize bytes.
1091 * We do not have to worry about simultaneous reads because
1092 * we waited for sole access to the hold buffer above.
1094 error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
1097 KASSERT(d->bd_hbuf != NULL, ("bpfread: lost bd_hbuf"));
1098 d->bd_fbuf = d->bd_hbuf;
1101 bpf_buf_reclaimed(d);
1102 d->bd_hbuf_in_use = 0;
1103 wakeup(&d->bd_hbuf_in_use);
1110 * If there are processes sleeping on this descriptor, wake them up.
1112 static __inline void
1113 bpf_wakeup(struct bpf_d *d)
1116 BPFD_LOCK_ASSERT(d);
1117 if (d->bd_state == BPF_WAITING) {
1118 callout_stop(&d->bd_callout);
1119 d->bd_state = BPF_IDLE;
1122 if (d->bd_async && d->bd_sig && d->bd_sigio)
1123 pgsigio(&d->bd_sigio, d->bd_sig, 0);
1125 selwakeuppri(&d->bd_sel, PRINET);
1126 KNOTE_LOCKED(&d->bd_sel.si_note, 0);
1130 bpf_timed_out(void *arg)
1132 struct bpf_d *d = (struct bpf_d *)arg;
1134 BPFD_LOCK_ASSERT(d);
1136 if (callout_pending(&d->bd_callout) ||
1137 !callout_active(&d->bd_callout))
1139 if (d->bd_state == BPF_WAITING) {
1140 d->bd_state = BPF_TIMED_OUT;
1141 if (d->bd_slen != 0)
1147 bpf_ready(struct bpf_d *d)
1150 BPFD_LOCK_ASSERT(d);
1152 if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
1154 if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1161 bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
1164 struct sockaddr dst;
1165 struct epoch_tracker et;
1169 struct mbuf *m, *mc;
1172 error = devfs_get_cdevpriv((void **)&d);
1176 NET_EPOCH_ENTER(et);
1178 BPF_PID_REFRESH_CUR(d);
1179 counter_u64_add(d->bd_wcount, 1);
1180 if ((bp = d->bd_bif) == NULL) {
1186 if ((ifp->if_flags & IFF_UP) == 0) {
1191 if (uio->uio_resid == 0)
1194 bzero(&dst, sizeof(dst));
1199 * Take extra reference, unlock d and exit from epoch section,
1200 * since bpf_movein() can sleep.
1206 error = bpf_movein(uio, (int)bp->bif_dlt, ifp,
1207 &m, &dst, &hlen, d);
1210 counter_u64_add(d->bd_wdcount, 1);
1217 * Check that descriptor is still attached to the interface.
1218 * This can happen on bpfdetach(). To avoid access to detached
1219 * ifnet, free mbuf and return ENXIO.
1221 if (d->bd_bif == NULL) {
1222 counter_u64_add(d->bd_wdcount, 1);
1228 counter_u64_add(d->bd_wfcount, 1);
1230 dst.sa_family = pseudo_AF_HDRCMPLT;
1232 if (d->bd_feedback) {
1233 mc = m_dup(m, M_NOWAIT);
1235 mc->m_pkthdr.rcvif = ifp;
1236 /* Set M_PROMISC for outgoing packets to be discarded. */
1237 if (d->bd_direction == BPF_D_INOUT)
1238 m->m_flags |= M_PROMISC;
1242 m->m_pkthdr.len -= hlen;
1244 m->m_data += hlen; /* XXX */
1246 CURVNET_SET(ifp->if_vnet);
1248 mac_bpfdesc_create_mbuf(d, m);
1250 mac_bpfdesc_create_mbuf(d, mc);
1253 bzero(&ro, sizeof(ro));
1255 ro.ro_prepend = (u_char *)&dst.sa_data;
1257 ro.ro_flags = RT_HAS_HEADER;
1260 error = (*ifp->if_output)(ifp, m, &dst, &ro);
1262 counter_u64_add(d->bd_wdcount, 1);
1266 (*ifp->if_input)(ifp, mc);
1276 counter_u64_add(d->bd_wdcount, 1);
1283 * Reset a descriptor by flushing its packet buffer and clearing the receive
1284 * and drop counts. This is doable for kernel-only buffers, but with
1285 * zero-copy buffers, we can't write to (or rotate) buffers that are
1286 * currently owned by userspace. It would be nice if we could encapsulate
1287 * this logic in the buffer code rather than here.
1290 reset_d(struct bpf_d *d)
1293 BPFD_LOCK_ASSERT(d);
1295 while (d->bd_hbuf_in_use)
1296 mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock, PRINET,
1298 if ((d->bd_hbuf != NULL) &&
1299 (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) {
1300 /* Free the hold buffer. */
1301 d->bd_fbuf = d->bd_hbuf;
1304 bpf_buf_reclaimed(d);
1306 if (bpf_canwritebuf(d))
1308 counter_u64_zero(d->bd_rcount);
1309 counter_u64_zero(d->bd_dcount);
1310 counter_u64_zero(d->bd_fcount);
1311 counter_u64_zero(d->bd_wcount);
1312 counter_u64_zero(d->bd_wfcount);
1313 counter_u64_zero(d->bd_wdcount);
1314 counter_u64_zero(d->bd_zcopy);
1318 * FIONREAD Check for read packet available.
1319 * BIOCGBLEN Get buffer len [for read()].
1320 * BIOCSETF Set read filter.
1321 * BIOCSETFNR Set read filter without resetting descriptor.
1322 * BIOCSETWF Set write filter.
1323 * BIOCFLUSH Flush read packet buffer.
1324 * BIOCPROMISC Put interface into promiscuous mode.
1325 * BIOCGDLT Get link layer type.
1326 * BIOCGETIF Get interface name.
1327 * BIOCSETIF Set interface.
1328 * BIOCSRTIMEOUT Set read timeout.
1329 * BIOCGRTIMEOUT Get read timeout.
1330 * BIOCGSTATS Get packet stats.
1331 * BIOCIMMEDIATE Set immediate mode.
1332 * BIOCVERSION Get filter language version.
1333 * BIOCGHDRCMPLT Get "header already complete" flag
1334 * BIOCSHDRCMPLT Set "header already complete" flag
1335 * BIOCGDIRECTION Get packet direction flag
1336 * BIOCSDIRECTION Set packet direction flag
1337 * BIOCGTSTAMP Get time stamp format and resolution.
1338 * BIOCSTSTAMP Set time stamp format and resolution.
1339 * BIOCLOCK Set "locked" flag
1340 * BIOCFEEDBACK Set packet feedback mode.
1341 * BIOCSETZBUF Set current zero-copy buffer locations.
1342 * BIOCGETZMAX Get maximum zero-copy buffer size.
1343 * BIOCROTZBUF Force rotation of zero-copy buffer
1344 * BIOCSETBUFMODE Set buffer mode.
1345 * BIOCGETBUFMODE Get current buffer mode.
1349 bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
1355 error = devfs_get_cdevpriv((void **)&d);
1360 * Refresh PID associated with this descriptor.
1363 BPF_PID_REFRESH(d, td);
1364 if (d->bd_state == BPF_WAITING)
1365 callout_stop(&d->bd_callout);
1366 d->bd_state = BPF_IDLE;
1369 if (d->bd_locked == 1) {
1375 #ifdef COMPAT_FREEBSD32
1376 case BIOCGDLTLIST32:
1380 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1381 case BIOCGRTIMEOUT32:
1392 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1393 case BIOCSRTIMEOUT32:
1403 #ifdef COMPAT_FREEBSD32
1405 * If we see a 32-bit compat ioctl, mark the stream as 32-bit so
1406 * that it will get 32-bit packet headers.
1412 case BIOCGDLTLIST32:
1413 case BIOCGRTIMEOUT32:
1414 case BIOCSRTIMEOUT32:
1415 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1423 CURVNET_SET(TD_TO_VNET(td));
1431 * Check for read packet available.
1439 while (d->bd_hbuf_in_use)
1440 mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock,
1441 PRINET, "bd_hbuf", 0);
1451 * Get buffer len [for read()].
1455 *(u_int *)addr = d->bd_bufsize;
1460 * Set buffer length.
1463 error = bpf_ioctl_sblen(d, (u_int *)addr);
1467 * Set link layer read filter.
1472 #ifdef COMPAT_FREEBSD32
1477 error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1481 * Flush read packet buffer.
1490 * Put interface into promiscuous mode.
1493 if (d->bd_bif == NULL) {
1495 * No interface attached yet.
1500 if (d->bd_promisc == 0) {
1501 error = ifpromisc(d->bd_bif->bif_ifp, 1);
1508 * Get current data link type.
1512 if (d->bd_bif == NULL)
1515 *(u_int *)addr = d->bd_bif->bif_dlt;
1520 * Get a list of supported data link types.
1522 #ifdef COMPAT_FREEBSD32
1523 case BIOCGDLTLIST32:
1525 struct bpf_dltlist32 *list32;
1526 struct bpf_dltlist dltlist;
1528 list32 = (struct bpf_dltlist32 *)addr;
1529 dltlist.bfl_len = list32->bfl_len;
1530 dltlist.bfl_list = PTRIN(list32->bfl_list);
1532 if (d->bd_bif == NULL)
1535 error = bpf_getdltlist(d, &dltlist);
1537 list32->bfl_len = dltlist.bfl_len;
1546 if (d->bd_bif == NULL)
1549 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
1554 * Set data link type.
1558 if (d->bd_bif == NULL)
1561 error = bpf_setdlt(d, *(u_int *)addr);
1566 * Get interface name.
1570 if (d->bd_bif == NULL)
1573 struct ifnet *const ifp = d->bd_bif->bif_ifp;
1574 struct ifreq *const ifr = (struct ifreq *)addr;
1576 strlcpy(ifr->ifr_name, ifp->if_xname,
1577 sizeof(ifr->ifr_name));
1587 int alloc_buf, size;
1590 * Behavior here depends on the buffering model. If
1591 * we're using kernel memory buffers, then we can
1592 * allocate them here. If we're using zero-copy,
1593 * then the user process must have registered buffers
1594 * by the time we get here.
1598 if (d->bd_bufmode == BPF_BUFMODE_BUFFER &&
1603 size = d->bd_bufsize;
1604 error = bpf_buffer_ioctl_sblen(d, &size);
1609 error = bpf_setif(d, (struct ifreq *)addr);
1618 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1619 case BIOCSRTIMEOUT32:
1622 struct timeval *tv = (struct timeval *)addr;
1623 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1624 struct timeval32 *tv32;
1625 struct timeval tv64;
1627 if (cmd == BIOCSRTIMEOUT32) {
1628 tv32 = (struct timeval32 *)addr;
1630 tv->tv_sec = tv32->tv_sec;
1631 tv->tv_usec = tv32->tv_usec;
1634 tv = (struct timeval *)addr;
1637 * Subtract 1 tick from tvtohz() since this isn't
1640 if ((error = itimerfix(tv)) == 0)
1641 d->bd_rtout = tvtohz(tv) - 1;
1649 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1650 case BIOCGRTIMEOUT32:
1654 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1655 struct timeval32 *tv32;
1656 struct timeval tv64;
1658 if (cmd == BIOCGRTIMEOUT32)
1662 tv = (struct timeval *)addr;
1664 tv->tv_sec = d->bd_rtout / hz;
1665 tv->tv_usec = (d->bd_rtout % hz) * tick;
1666 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1667 if (cmd == BIOCGRTIMEOUT32) {
1668 tv32 = (struct timeval32 *)addr;
1669 tv32->tv_sec = tv->tv_sec;
1670 tv32->tv_usec = tv->tv_usec;
1682 struct bpf_stat *bs = (struct bpf_stat *)addr;
1684 /* XXXCSJP overflow */
1685 bs->bs_recv = (u_int)counter_u64_fetch(d->bd_rcount);
1686 bs->bs_drop = (u_int)counter_u64_fetch(d->bd_dcount);
1691 * Set immediate mode.
1695 d->bd_immediate = *(u_int *)addr;
1701 struct bpf_version *bv = (struct bpf_version *)addr;
1703 bv->bv_major = BPF_MAJOR_VERSION;
1704 bv->bv_minor = BPF_MINOR_VERSION;
1709 * Get "header already complete" flag
1713 *(u_int *)addr = d->bd_hdrcmplt;
1718 * Set "header already complete" flag
1722 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1727 * Get packet direction flag
1729 case BIOCGDIRECTION:
1731 *(u_int *)addr = d->bd_direction;
1736 * Set packet direction flag
1738 case BIOCSDIRECTION:
1742 direction = *(u_int *)addr;
1743 switch (direction) {
1748 d->bd_direction = direction;
1758 * Get packet timestamp format and resolution.
1762 *(u_int *)addr = d->bd_tstamp;
1767 * Set packet timestamp format and resolution.
1773 func = *(u_int *)addr;
1774 if (BPF_T_VALID(func))
1775 d->bd_tstamp = func;
1783 d->bd_feedback = *(u_int *)addr;
1793 case FIONBIO: /* Non-blocking I/O */
1796 case FIOASYNC: /* Send signal on receive packets */
1798 d->bd_async = *(int *)addr;
1804 * XXX: Add some sort of locking here?
1805 * fsetown() can sleep.
1807 error = fsetown(*(int *)addr, &d->bd_sigio);
1812 *(int *)addr = fgetown(&d->bd_sigio);
1816 /* This is deprecated, FIOSETOWN should be used instead. */
1818 error = fsetown(-(*(int *)addr), &d->bd_sigio);
1821 /* This is deprecated, FIOGETOWN should be used instead. */
1823 *(int *)addr = -fgetown(&d->bd_sigio);
1826 case BIOCSRSIG: /* Set receive signal */
1830 sig = *(u_int *)addr;
1843 *(u_int *)addr = d->bd_sig;
1847 case BIOCGETBUFMODE:
1849 *(u_int *)addr = d->bd_bufmode;
1853 case BIOCSETBUFMODE:
1855 * Allow the buffering mode to be changed as long as we
1856 * haven't yet committed to a particular mode. Our
1857 * definition of commitment, for now, is whether or not a
1858 * buffer has been allocated or an interface attached, since
1859 * that's the point where things get tricky.
1861 switch (*(u_int *)addr) {
1862 case BPF_BUFMODE_BUFFER:
1865 case BPF_BUFMODE_ZBUF:
1866 if (bpf_zerocopy_enable)
1876 if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
1877 d->bd_fbuf != NULL || d->bd_bif != NULL) {
1882 d->bd_bufmode = *(u_int *)addr;
1887 error = bpf_ioctl_getzmax(td, d, (size_t *)addr);
1891 error = bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr);
1895 error = bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr);
1903 * Set d's packet filter program to fp. If this file already has a filter,
1904 * free it and replace it. Returns EINVAL for bogus requests.
1906 * Note we use global lock here to serialize bpf_setf() and bpf_setif()
1910 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1912 #ifdef COMPAT_FREEBSD32
1913 struct bpf_program fp_swab;
1914 struct bpf_program32 *fp32;
1916 struct bpf_program_buffer *fcode;
1917 struct bpf_insn *filter;
1919 bpf_jit_filter *jfunc;
1925 #ifdef COMPAT_FREEBSD32
1930 fp32 = (struct bpf_program32 *)fp;
1931 fp_swab.bf_len = fp32->bf_len;
1933 (struct bpf_insn *)(uintptr_t)fp32->bf_insns;
1952 * Check new filter validness before acquiring any locks.
1953 * Allocate memory for new filter, if needed.
1956 if (flen > bpf_maxinsns || (fp->bf_insns == NULL && flen != 0))
1958 size = flen * sizeof(*fp->bf_insns);
1960 /* We're setting up new filter. Copy and check actual data. */
1961 fcode = bpf_program_buffer_alloc(size, M_WAITOK);
1962 filter = (struct bpf_insn *)fcode->buffer;
1963 if (copyin(fp->bf_insns, filter, size) != 0 ||
1964 !bpf_validate(filter, flen)) {
1969 if (cmd != BIOCSETWF) {
1971 * Filter is copied inside fcode and is
1974 jfunc = bpf_jitter(filter, flen);
1979 track_event = false;
1984 /* Set up new filter. */
1985 if (cmd == BIOCSETWF) {
1986 if (d->bd_wfilter != NULL) {
1987 fcode = __containerof((void *)d->bd_wfilter,
1988 struct bpf_program_buffer, buffer);
1993 d->bd_wfilter = filter;
1995 if (d->bd_rfilter != NULL) {
1996 fcode = __containerof((void *)d->bd_rfilter,
1997 struct bpf_program_buffer, buffer);
1999 fcode->func = d->bd_bfilter;
2002 d->bd_rfilter = filter;
2004 d->bd_bfilter = jfunc;
2006 if (cmd == BIOCSETF)
2009 if (bpf_check_upgrade(cmd, d, filter, flen) != 0) {
2011 * Filter can be set several times without
2012 * specifying interface. In this case just mark d
2016 if (d->bd_bif != NULL) {
2018 * Remove descriptor from writers-only list
2019 * and add it to active readers list.
2021 CK_LIST_REMOVE(d, bd_next);
2022 CK_LIST_INSERT_HEAD(&d->bd_bif->bif_dlist,
2025 "%s: upgrade required by pid %d",
2026 __func__, d->bd_pid);
2034 epoch_call(net_epoch_preempt, &fcode->epoch_ctx,
2035 bpf_program_buffer_free);
2038 EVENTHANDLER_INVOKE(bpf_track,
2039 d->bd_bif->bif_ifp, d->bd_bif->bif_dlt, 1);
2046 * Detach a file from its current interface (if attached at all) and attach
2047 * to the interface indicated by the name stored in ifr.
2048 * Return an errno or 0.
2051 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
2054 struct ifnet *theywant;
2058 theywant = ifunit(ifr->ifr_name);
2059 if (theywant == NULL || theywant->if_bpf == NULL)
2062 bp = theywant->if_bpf;
2064 * At this point, we expect the buffer is already allocated. If not,
2067 switch (d->bd_bufmode) {
2068 case BPF_BUFMODE_BUFFER:
2069 case BPF_BUFMODE_ZBUF:
2070 if (d->bd_sbuf == NULL)
2075 panic("bpf_setif: bufmode %d", d->bd_bufmode);
2077 if (bp != d->bd_bif)
2088 * Support for select() and poll() system calls
2090 * Return true iff the specific operation will not block indefinitely.
2091 * Otherwise, return false but make a note that a selwakeup() must be done.
2094 bpfpoll(struct cdev *dev, int events, struct thread *td)
2099 if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL)
2101 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
2104 * Refresh PID associated with this descriptor.
2106 revents = events & (POLLOUT | POLLWRNORM);
2108 BPF_PID_REFRESH(d, td);
2109 if (events & (POLLIN | POLLRDNORM)) {
2111 revents |= events & (POLLIN | POLLRDNORM);
2113 selrecord(td, &d->bd_sel);
2114 /* Start the read timeout if necessary. */
2115 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
2116 callout_reset(&d->bd_callout, d->bd_rtout,
2118 d->bd_state = BPF_WAITING;
2127 * Support for kevent() system call. Register EVFILT_READ filters and
2128 * reject all others.
2131 bpfkqfilter(struct cdev *dev, struct knote *kn)
2135 if (devfs_get_cdevpriv((void **)&d) != 0 ||
2136 kn->kn_filter != EVFILT_READ)
2140 * Refresh PID associated with this descriptor.
2143 BPF_PID_REFRESH_CUR(d);
2144 kn->kn_fop = &bpfread_filtops;
2146 knlist_add(&d->bd_sel.si_note, kn, 1);
2153 filt_bpfdetach(struct knote *kn)
2155 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
2157 knlist_remove(&d->bd_sel.si_note, kn, 0);
2161 filt_bpfread(struct knote *kn, long hint)
2163 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
2166 BPFD_LOCK_ASSERT(d);
2167 ready = bpf_ready(d);
2169 kn->kn_data = d->bd_slen;
2171 * Ignore the hold buffer if it is being copied to user space.
2173 if (!d->bd_hbuf_in_use && d->bd_hbuf)
2174 kn->kn_data += d->bd_hlen;
2175 } else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
2176 callout_reset(&d->bd_callout, d->bd_rtout,
2178 d->bd_state = BPF_WAITING;
2184 #define BPF_TSTAMP_NONE 0
2185 #define BPF_TSTAMP_FAST 1
2186 #define BPF_TSTAMP_NORMAL 2
2187 #define BPF_TSTAMP_EXTERN 3
2190 bpf_ts_quality(int tstype)
2193 if (tstype == BPF_T_NONE)
2194 return (BPF_TSTAMP_NONE);
2195 if ((tstype & BPF_T_FAST) != 0)
2196 return (BPF_TSTAMP_FAST);
2198 return (BPF_TSTAMP_NORMAL);
2202 bpf_gettime(struct bintime *bt, int tstype, struct mbuf *m)
2207 quality = bpf_ts_quality(tstype);
2208 if (quality == BPF_TSTAMP_NONE)
2212 tag = m_tag_locate(m, MTAG_BPF, MTAG_BPF_TIMESTAMP, NULL);
2214 *bt = *(struct bintime *)(tag + 1);
2215 return (BPF_TSTAMP_EXTERN);
2218 if (quality == BPF_TSTAMP_NORMAL)
2227 * Incoming linkage from device drivers. Process the packet pkt, of length
2228 * pktlen, which is stored in a contiguous buffer. The packet is parsed
2229 * by each process' filter, and if accepted, stashed into the corresponding
2233 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2235 struct epoch_tracker et;
2244 gottime = BPF_TSTAMP_NONE;
2245 NET_EPOCH_ENTER(et);
2246 CK_LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
2247 counter_u64_add(d->bd_rcount, 1);
2249 * NB: We dont call BPF_CHECK_DIRECTION() here since there
2250 * is no way for the caller to indiciate to us whether this
2251 * packet is inbound or outbound. In the bpf_mtap() routines,
2252 * we use the interface pointers on the mbuf to figure it out.
2255 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
2257 slen = (*(bf->func))(pkt, pktlen, pktlen);
2260 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
2263 * Filter matches. Let's to acquire write lock.
2266 counter_u64_add(d->bd_fcount, 1);
2267 if (gottime < bpf_ts_quality(d->bd_tstamp))
2268 gottime = bpf_gettime(&bt, d->bd_tstamp,
2271 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
2273 catchpacket(d, pkt, pktlen, slen,
2274 bpf_append_bytes, &bt);
2281 #define BPF_CHECK_DIRECTION(d, r, i) \
2282 (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \
2283 ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
2286 * Incoming linkage from device drivers, when packet is in an mbuf chain.
2287 * Locking model is explained in bpf_tap().
2290 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2292 struct epoch_tracker et;
2301 /* Skip outgoing duplicate packets. */
2302 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
2303 m->m_flags &= ~M_PROMISC;
2307 pktlen = m_length(m, NULL);
2308 gottime = BPF_TSTAMP_NONE;
2310 NET_EPOCH_ENTER(et);
2311 CK_LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
2312 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
2314 counter_u64_add(d->bd_rcount, 1);
2316 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
2317 /* XXX We cannot handle multiple mbufs. */
2318 if (bf != NULL && m->m_next == NULL)
2319 slen = (*(bf->func))(mtod(m, u_char *), pktlen,
2323 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
2327 counter_u64_add(d->bd_fcount, 1);
2328 if (gottime < bpf_ts_quality(d->bd_tstamp))
2329 gottime = bpf_gettime(&bt, d->bd_tstamp, m);
2331 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
2333 catchpacket(d, (u_char *)m, pktlen, slen,
2334 bpf_append_mbuf, &bt);
2342 * Incoming linkage from device drivers, when packet is in
2343 * an mbuf chain and to be prepended by a contiguous header.
2346 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
2348 struct epoch_tracker et;
2355 /* Skip outgoing duplicate packets. */
2356 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
2357 m->m_flags &= ~M_PROMISC;
2361 pktlen = m_length(m, NULL);
2363 * Craft on-stack mbuf suitable for passing to bpf_filter.
2364 * Note that we cut corners here; we only setup what's
2365 * absolutely needed--this mbuf should never go anywhere else.
2372 gottime = BPF_TSTAMP_NONE;
2374 NET_EPOCH_ENTER(et);
2375 CK_LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
2376 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
2378 counter_u64_add(d->bd_rcount, 1);
2379 slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
2383 counter_u64_add(d->bd_fcount, 1);
2384 if (gottime < bpf_ts_quality(d->bd_tstamp))
2385 gottime = bpf_gettime(&bt, d->bd_tstamp, m);
2387 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
2389 catchpacket(d, (u_char *)&mb, pktlen, slen,
2390 bpf_append_mbuf, &bt);
2397 #undef BPF_CHECK_DIRECTION
2398 #undef BPF_TSTAMP_NONE
2399 #undef BPF_TSTAMP_FAST
2400 #undef BPF_TSTAMP_NORMAL
2401 #undef BPF_TSTAMP_EXTERN
2404 bpf_hdrlen(struct bpf_d *d)
2408 hdrlen = d->bd_bif->bif_hdrlen;
2409 #ifndef BURN_BRIDGES
2410 if (d->bd_tstamp == BPF_T_NONE ||
2411 BPF_T_FORMAT(d->bd_tstamp) == BPF_T_MICROTIME)
2412 #ifdef COMPAT_FREEBSD32
2414 hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr32);
2417 hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr);
2420 hdrlen += SIZEOF_BPF_HDR(struct bpf_xhdr);
2421 #ifdef COMPAT_FREEBSD32
2423 hdrlen = BPF_WORDALIGN32(hdrlen);
2426 hdrlen = BPF_WORDALIGN(hdrlen);
2428 return (hdrlen - d->bd_bif->bif_hdrlen);
2432 bpf_bintime2ts(struct bintime *bt, struct bpf_ts *ts, int tstype)
2434 struct bintime bt2, boottimebin;
2436 struct timespec tsn;
2438 if ((tstype & BPF_T_MONOTONIC) == 0) {
2440 getboottimebin(&boottimebin);
2441 bintime_add(&bt2, &boottimebin);
2444 switch (BPF_T_FORMAT(tstype)) {
2445 case BPF_T_MICROTIME:
2446 bintime2timeval(bt, &tsm);
2447 ts->bt_sec = tsm.tv_sec;
2448 ts->bt_frac = tsm.tv_usec;
2450 case BPF_T_NANOTIME:
2451 bintime2timespec(bt, &tsn);
2452 ts->bt_sec = tsn.tv_sec;
2453 ts->bt_frac = tsn.tv_nsec;
2456 ts->bt_sec = bt->sec;
2457 ts->bt_frac = bt->frac;
2463 * Move the packet data from interface memory (pkt) into the
2464 * store buffer. "cpfn" is the routine called to do the actual data
2465 * transfer. bcopy is passed in to copy contiguous chunks, while
2466 * bpf_append_mbuf is passed in to copy mbuf chains. In the latter case,
2467 * pkt is really an mbuf.
2470 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
2471 void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
2474 struct bpf_xhdr hdr;
2475 #ifndef BURN_BRIDGES
2476 struct bpf_hdr hdr_old;
2477 #ifdef COMPAT_FREEBSD32
2478 struct bpf_hdr32 hdr32_old;
2481 int caplen, curlen, hdrlen, totlen;
2486 BPFD_LOCK_ASSERT(d);
2489 * Detect whether user space has released a buffer back to us, and if
2490 * so, move it from being a hold buffer to a free buffer. This may
2491 * not be the best place to do it (for example, we might only want to
2492 * run this check if we need the space), but for now it's a reliable
2495 if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
2496 d->bd_fbuf = d->bd_hbuf;
2499 bpf_buf_reclaimed(d);
2503 * Figure out how many bytes to move. If the packet is
2504 * greater or equal to the snapshot length, transfer that
2505 * much. Otherwise, transfer the whole packet (unless
2506 * we hit the buffer size limit).
2508 hdrlen = bpf_hdrlen(d);
2509 totlen = hdrlen + min(snaplen, pktlen);
2510 if (totlen > d->bd_bufsize)
2511 totlen = d->bd_bufsize;
2514 * Round up the end of the previous packet to the next longword.
2516 * Drop the packet if there's no room and no hope of room
2517 * If the packet would overflow the storage buffer or the storage
2518 * buffer is considered immutable by the buffer model, try to rotate
2519 * the buffer and wakeup pending processes.
2521 #ifdef COMPAT_FREEBSD32
2523 curlen = BPF_WORDALIGN32(d->bd_slen);
2526 curlen = BPF_WORDALIGN(d->bd_slen);
2527 if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
2528 if (d->bd_fbuf == NULL) {
2530 * There's no room in the store buffer, and no
2531 * prospect of room, so drop the packet. Notify the
2535 counter_u64_add(d->bd_dcount, 1);
2538 KASSERT(!d->bd_hbuf_in_use, ("hold buffer is in use"));
2542 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
2544 * Immediate mode is set, or the read timeout has already
2545 * expired during a select call. A packet arrived, so the
2546 * reader should be woken up.
2549 caplen = totlen - hdrlen;
2550 tstype = d->bd_tstamp;
2551 do_timestamp = tstype != BPF_T_NONE;
2552 #ifndef BURN_BRIDGES
2553 if (tstype == BPF_T_NONE || BPF_T_FORMAT(tstype) == BPF_T_MICROTIME) {
2556 bpf_bintime2ts(bt, &ts, tstype);
2557 #ifdef COMPAT_FREEBSD32
2558 if (d->bd_compat32) {
2559 bzero(&hdr32_old, sizeof(hdr32_old));
2561 hdr32_old.bh_tstamp.tv_sec = ts.bt_sec;
2562 hdr32_old.bh_tstamp.tv_usec = ts.bt_frac;
2564 hdr32_old.bh_datalen = pktlen;
2565 hdr32_old.bh_hdrlen = hdrlen;
2566 hdr32_old.bh_caplen = caplen;
2567 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr32_old,
2572 bzero(&hdr_old, sizeof(hdr_old));
2574 hdr_old.bh_tstamp.tv_sec = ts.bt_sec;
2575 hdr_old.bh_tstamp.tv_usec = ts.bt_frac;
2577 hdr_old.bh_datalen = pktlen;
2578 hdr_old.bh_hdrlen = hdrlen;
2579 hdr_old.bh_caplen = caplen;
2580 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr_old,
2587 * Append the bpf header. Note we append the actual header size, but
2588 * move forward the length of the header plus padding.
2590 bzero(&hdr, sizeof(hdr));
2592 bpf_bintime2ts(bt, &hdr.bh_tstamp, tstype);
2593 hdr.bh_datalen = pktlen;
2594 hdr.bh_hdrlen = hdrlen;
2595 hdr.bh_caplen = caplen;
2596 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
2599 * Copy the packet data into the store buffer and update its length.
2601 #ifndef BURN_BRIDGES
2604 (*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, caplen);
2605 d->bd_slen = curlen + totlen;
2612 * Free buffers currently in use by a descriptor.
2616 bpfd_free(epoch_context_t ctx)
2619 struct bpf_program_buffer *p;
2622 * We don't need to lock out interrupts since this descriptor has
2623 * been detached from its interface and it yet hasn't been marked
2626 d = __containerof(ctx, struct bpf_d, epoch_ctx);
2628 if (d->bd_rfilter != NULL) {
2629 p = __containerof((void *)d->bd_rfilter,
2630 struct bpf_program_buffer, buffer);
2632 p->func = d->bd_bfilter;
2634 bpf_program_buffer_free(&p->epoch_ctx);
2636 if (d->bd_wfilter != NULL) {
2637 p = __containerof((void *)d->bd_wfilter,
2638 struct bpf_program_buffer, buffer);
2642 bpf_program_buffer_free(&p->epoch_ctx);
2645 mtx_destroy(&d->bd_lock);
2646 counter_u64_free(d->bd_rcount);
2647 counter_u64_free(d->bd_dcount);
2648 counter_u64_free(d->bd_fcount);
2649 counter_u64_free(d->bd_wcount);
2650 counter_u64_free(d->bd_wfcount);
2651 counter_u64_free(d->bd_wdcount);
2652 counter_u64_free(d->bd_zcopy);
2657 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
2658 * fixed size of the link header (variable length headers not yet supported).
2661 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2664 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2668 * Attach an interface to bpf. ifp is a pointer to the structure
2669 * defining the interface to be attached, dlt is the link layer type,
2670 * and hdrlen is the fixed size of the link header (variable length
2671 * headers are not yet supporrted).
2674 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen,
2675 struct bpf_if **driverp)
2679 KASSERT(*driverp == NULL,
2680 ("bpfattach2: driverp already initialized"));
2682 bp = malloc(sizeof(*bp), M_BPF, M_WAITOK | M_ZERO);
2684 CK_LIST_INIT(&bp->bif_dlist);
2685 CK_LIST_INIT(&bp->bif_wlist);
2688 bp->bif_hdrlen = hdrlen;
2689 bp->bif_bpf = driverp;
2693 * Reference ifnet pointer, so it won't freed until
2698 CK_LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
2701 if (bootverbose && IS_DEFAULT_VNET(curvnet))
2702 if_printf(ifp, "bpf attached\n");
2707 * When moving interfaces between vnet instances we need a way to
2708 * query the dlt and hdrlen before detach so we can re-attch the if_bpf
2709 * after the vmove. We unfortunately have no device driver infrastructure
2710 * to query the interface for these values after creation/attach, thus
2711 * add this as a workaround.
2714 bpf_get_bp_params(struct bpf_if *bp, u_int *bif_dlt, u_int *bif_hdrlen)
2719 if (bif_dlt == NULL && bif_hdrlen == NULL)
2722 if (bif_dlt != NULL)
2723 *bif_dlt = bp->bif_dlt;
2724 if (bif_hdrlen != NULL)
2725 *bif_hdrlen = bp->bif_hdrlen;
2732 * Detach bpf from an interface. This involves detaching each descriptor
2733 * associated with the interface. Notify each descriptor as it's detached
2734 * so that any sleepers wake up and get ENXIO.
2737 bpfdetach(struct ifnet *ifp)
2739 struct bpf_if *bp, *bp_temp;
2743 /* Find all bpf_if struct's which reference ifp and detach them. */
2744 CK_LIST_FOREACH_SAFE(bp, &bpf_iflist, bif_next, bp_temp) {
2745 if (ifp != bp->bif_ifp)
2748 CK_LIST_REMOVE(bp, bif_next);
2749 *bp->bif_bpf = (struct bpf_if *)&dead_bpf_if;
2752 "%s: sheduling free for encap %d (%p) for if %p",
2753 __func__, bp->bif_dlt, bp, ifp);
2755 /* Detach common descriptors */
2756 while ((d = CK_LIST_FIRST(&bp->bif_dlist)) != NULL) {
2757 bpf_detachd_locked(d, true);
2760 /* Detach writer-only descriptors */
2761 while ((d = CK_LIST_FIRST(&bp->bif_wlist)) != NULL) {
2762 bpf_detachd_locked(d, true);
2770 * Get a list of available data link type of the interface.
2773 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
2782 ifp = d->bd_bif->bif_ifp;
2784 CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2785 if (bp->bif_ifp == ifp)
2788 if (bfl->bfl_list == NULL) {
2792 if (n1 > bfl->bfl_len)
2795 lst = malloc(n1 * sizeof(u_int), M_TEMP, M_WAITOK);
2797 CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2798 if (bp->bif_ifp != ifp)
2800 lst[n++] = bp->bif_dlt;
2802 error = copyout(lst, bfl->bfl_list, sizeof(u_int) * n);
2809 * Set the data link type of a BPF instance.
2812 bpf_setdlt(struct bpf_d *d, u_int dlt)
2814 int error, opromisc;
2819 MPASS(d->bd_bif != NULL);
2822 * It is safe to check bd_bif without BPFD_LOCK, it can not be
2823 * changed while we hold global lock.
2825 if (d->bd_bif->bif_dlt == dlt)
2828 ifp = d->bd_bif->bif_ifp;
2829 CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2830 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2836 opromisc = d->bd_promisc;
2839 error = ifpromisc(bp->bif_ifp, 1);
2841 if_printf(bp->bif_ifp, "%s: ifpromisc failed (%d)\n",
2850 bpf_drvinit(void *unused)
2854 sx_init(&bpf_sx, "bpf global lock");
2855 CK_LIST_INIT(&bpf_iflist);
2857 dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf");
2858 /* For compatibility */
2859 make_dev_alias(dev, "bpf0");
2863 * Zero out the various packet counters associated with all of the bpf
2864 * descriptors. At some point, we will probably want to get a bit more
2865 * granular and allow the user to specify descriptors to be zeroed.
2868 bpf_zero_counters(void)
2875 * We are protected by global lock here, interfaces and
2876 * descriptors can not be deleted while we hold it.
2878 CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2879 CK_LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2880 counter_u64_zero(bd->bd_rcount);
2881 counter_u64_zero(bd->bd_dcount);
2882 counter_u64_zero(bd->bd_fcount);
2883 counter_u64_zero(bd->bd_wcount);
2884 counter_u64_zero(bd->bd_wfcount);
2885 counter_u64_zero(bd->bd_zcopy);
2892 * Fill filter statistics
2895 bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
2899 bzero(d, sizeof(*d));
2900 d->bd_structsize = sizeof(*d);
2901 d->bd_immediate = bd->bd_immediate;
2902 d->bd_promisc = bd->bd_promisc;
2903 d->bd_hdrcmplt = bd->bd_hdrcmplt;
2904 d->bd_direction = bd->bd_direction;
2905 d->bd_feedback = bd->bd_feedback;
2906 d->bd_async = bd->bd_async;
2907 d->bd_rcount = counter_u64_fetch(bd->bd_rcount);
2908 d->bd_dcount = counter_u64_fetch(bd->bd_dcount);
2909 d->bd_fcount = counter_u64_fetch(bd->bd_fcount);
2910 d->bd_sig = bd->bd_sig;
2911 d->bd_slen = bd->bd_slen;
2912 d->bd_hlen = bd->bd_hlen;
2913 d->bd_bufsize = bd->bd_bufsize;
2914 d->bd_pid = bd->bd_pid;
2915 strlcpy(d->bd_ifname,
2916 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
2917 d->bd_locked = bd->bd_locked;
2918 d->bd_wcount = counter_u64_fetch(bd->bd_wcount);
2919 d->bd_wdcount = counter_u64_fetch(bd->bd_wdcount);
2920 d->bd_wfcount = counter_u64_fetch(bd->bd_wfcount);
2921 d->bd_zcopy = counter_u64_fetch(bd->bd_zcopy);
2922 d->bd_bufmode = bd->bd_bufmode;
2926 * Handle `netstat -B' stats request
2929 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
2931 static const struct xbpf_d zerostats;
2932 struct xbpf_d *xbdbuf, *xbd, tempstats;
2938 * XXX This is not technically correct. It is possible for non
2939 * privileged users to open bpf devices. It would make sense
2940 * if the users who opened the devices were able to retrieve
2941 * the statistics for them, too.
2943 error = priv_check(req->td, PRIV_NET_BPF);
2947 * Check to see if the user is requesting that the counters be
2948 * zeroed out. Explicitly check that the supplied data is zeroed,
2949 * as we aren't allowing the user to set the counters currently.
2951 if (req->newptr != NULL) {
2952 if (req->newlen != sizeof(tempstats))
2954 memset(&tempstats, 0, sizeof(tempstats));
2955 error = SYSCTL_IN(req, &tempstats, sizeof(tempstats));
2958 if (bcmp(&tempstats, &zerostats, sizeof(tempstats)) != 0)
2960 bpf_zero_counters();
2963 if (req->oldptr == NULL)
2964 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
2965 if (bpf_bpfd_cnt == 0)
2966 return (SYSCTL_OUT(req, 0, 0));
2967 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
2969 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2971 free(xbdbuf, M_BPF);
2975 CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2976 /* Send writers-only first */
2977 CK_LIST_FOREACH(bd, &bp->bif_wlist, bd_next) {
2978 xbd = &xbdbuf[index++];
2979 bpfstats_fill_xbpf(xbd, bd);
2981 CK_LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2982 xbd = &xbdbuf[index++];
2983 bpfstats_fill_xbpf(xbd, bd);
2987 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2988 free(xbdbuf, M_BPF);
2992 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
2994 #else /* !DEV_BPF && !NETGRAPH_BPF */
2997 * NOP stubs to allow bpf-using drivers to load and function.
2999 * A 'better' implementation would allow the core bpf functionality
3000 * to be loaded at runtime.
3004 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
3009 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
3014 bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
3019 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
3022 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
3026 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
3029 *driverp = (struct bpf_if *)&dead_bpf_if;
3033 bpfdetach(struct ifnet *ifp)
3038 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
3040 return -1; /* "no filter" behaviour */
3044 bpf_validate(const struct bpf_insn *f, int len)
3046 return 0; /* false */
3049 #endif /* !DEV_BPF && !NETGRAPH_BPF */
3053 bpf_show_bpf_if(struct bpf_if *bpf_if)
3058 db_printf("%p:\n", bpf_if);
3059 #define BPF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, bpf_if->e);
3060 /* bif_ext.bif_next */
3061 /* bif_ext.bif_dlist */
3062 BPF_DB_PRINTF("%#x", bif_dlt);
3063 BPF_DB_PRINTF("%u", bif_hdrlen);
3065 BPF_DB_PRINTF("%p", bif_ifp);
3066 BPF_DB_PRINTF("%p", bif_bpf);
3067 BPF_DB_PRINTF("%u", bif_refcnt);
3070 DB_SHOW_COMMAND(bpf_if, db_show_bpf_if)
3074 db_printf("usage: show bpf_if <struct bpf_if *>\n");
3078 bpf_show_bpf_if((struct bpf_if *)addr);