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_compat.h"
42 #include "opt_netgraph.h"
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/fcntl.h>
50 #include <sys/malloc.h>
55 #include <sys/signalvar.h>
56 #include <sys/filio.h>
57 #include <sys/sockio.h>
58 #include <sys/ttycom.h>
61 #include <sys/event.h>
66 #include <sys/socket.h>
70 #include <net/bpf_buffer.h>
72 #include <net/bpf_jitter.h>
74 #include <net/bpf_zerocopy.h>
75 #include <net/bpfdesc.h>
78 #include <netinet/in.h>
79 #include <netinet/if_ether.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
83 #include <net80211/ieee80211_freebsd.h>
85 #include <security/mac/mac_framework.h>
87 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
89 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
91 #define PRINET 26 /* interruptible */
93 #define SIZEOF_BPF_HDR(type) \
94 (offsetof(type, bh_hdrlen) + sizeof(((type *)0)->bh_hdrlen))
96 #ifdef COMPAT_FREEBSD32
97 #include <sys/mount.h>
98 #include <compat/freebsd32/freebsd32.h>
99 #define BPF_ALIGNMENT32 sizeof(int32_t)
100 #define BPF_WORDALIGN32(x) (((x)+(BPF_ALIGNMENT32-1))&~(BPF_ALIGNMENT32-1))
104 * 32-bit version of structure prepended to each packet. We use this header
105 * instead of the standard one for 32-bit streams. We mark the a stream as
106 * 32-bit the first time we see a 32-bit compat ioctl request.
109 struct timeval32 bh_tstamp; /* time stamp */
110 uint32_t bh_caplen; /* length of captured portion */
111 uint32_t bh_datalen; /* original length of packet */
112 uint16_t bh_hdrlen; /* length of bpf header (this struct
113 plus alignment padding) */
117 struct bpf_program32 {
122 struct bpf_dltlist32 {
127 #define BIOCSETF32 _IOW('B', 103, struct bpf_program32)
128 #define BIOCSRTIMEOUT32 _IOW('B', 109, struct timeval32)
129 #define BIOCGRTIMEOUT32 _IOR('B', 110, struct timeval32)
130 #define BIOCGDLTLIST32 _IOWR('B', 121, struct bpf_dltlist32)
131 #define BIOCSETWF32 _IOW('B', 123, struct bpf_program32)
132 #define BIOCSETFNR32 _IOW('B', 130, struct bpf_program32)
136 * bpf_iflist is a list of BPF interface structures, each corresponding to a
137 * specific DLT. The same network interface might have several BPF interface
138 * structures registered by different layers in the stack (i.e., 802.11
139 * frames, ethernet frames, etc).
141 static LIST_HEAD(, bpf_if) bpf_iflist;
142 static struct mtx bpf_mtx; /* bpf global lock */
143 static int bpf_bpfd_cnt;
145 static void bpf_attachd(struct bpf_d *, struct bpf_if *);
146 static void bpf_detachd(struct bpf_d *);
147 static void bpf_freed(struct bpf_d *);
148 static int bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
149 struct sockaddr *, int *, struct bpf_insn *);
150 static int bpf_setif(struct bpf_d *, struct ifreq *);
151 static void bpf_timed_out(void *);
153 bpf_wakeup(struct bpf_d *);
154 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
155 void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
157 static void reset_d(struct bpf_d *);
158 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
159 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
160 static int bpf_setdlt(struct bpf_d *, u_int);
161 static void filt_bpfdetach(struct knote *);
162 static int filt_bpfread(struct knote *, long);
163 static void bpf_drvinit(void *);
164 static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
166 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
167 int bpf_maxinsns = BPF_MAXINSNS;
168 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
169 &bpf_maxinsns, 0, "Maximum bpf program instructions");
170 static int bpf_zerocopy_enable = 0;
171 SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
172 &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
173 SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
174 bpf_stats_sysctl, "bpf statistics portal");
176 static d_open_t bpfopen;
177 static d_read_t bpfread;
178 static d_write_t bpfwrite;
179 static d_ioctl_t bpfioctl;
180 static d_poll_t bpfpoll;
181 static d_kqfilter_t bpfkqfilter;
183 static struct cdevsw bpf_cdevsw = {
184 .d_version = D_VERSION,
191 .d_kqfilter = bpfkqfilter,
194 static struct filterops bpfread_filtops = {
196 .f_detach = filt_bpfdetach,
197 .f_event = filt_bpfread,
201 * Wrapper functions for various buffering methods. If the set of buffer
202 * modes expands, we will probably want to introduce a switch data structure
203 * similar to protosw, et.
206 bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
212 switch (d->bd_bufmode) {
213 case BPF_BUFMODE_BUFFER:
214 return (bpf_buffer_append_bytes(d, buf, offset, src, len));
216 case BPF_BUFMODE_ZBUF:
218 return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
221 panic("bpf_buf_append_bytes");
226 bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
232 switch (d->bd_bufmode) {
233 case BPF_BUFMODE_BUFFER:
234 return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
236 case BPF_BUFMODE_ZBUF:
238 return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
241 panic("bpf_buf_append_mbuf");
246 * This function gets called when the free buffer is re-assigned.
249 bpf_buf_reclaimed(struct bpf_d *d)
254 switch (d->bd_bufmode) {
255 case BPF_BUFMODE_BUFFER:
258 case BPF_BUFMODE_ZBUF:
259 bpf_zerocopy_buf_reclaimed(d);
263 panic("bpf_buf_reclaimed");
268 * If the buffer mechanism has a way to decide that a held buffer can be made
269 * free, then it is exposed via the bpf_canfreebuf() interface. (1) is
270 * returned if the buffer can be discarded, (0) is returned if it cannot.
273 bpf_canfreebuf(struct bpf_d *d)
278 switch (d->bd_bufmode) {
279 case BPF_BUFMODE_ZBUF:
280 return (bpf_zerocopy_canfreebuf(d));
286 * Allow the buffer model to indicate that the current store buffer is
287 * immutable, regardless of the appearance of space. Return (1) if the
288 * buffer is writable, and (0) if not.
291 bpf_canwritebuf(struct bpf_d *d)
296 switch (d->bd_bufmode) {
297 case BPF_BUFMODE_ZBUF:
298 return (bpf_zerocopy_canwritebuf(d));
304 * Notify buffer model that an attempt to write to the store buffer has
305 * resulted in a dropped packet, in which case the buffer may be considered
309 bpf_buffull(struct bpf_d *d)
314 switch (d->bd_bufmode) {
315 case BPF_BUFMODE_ZBUF:
316 bpf_zerocopy_buffull(d);
322 * Notify the buffer model that a buffer has moved into the hold position.
325 bpf_bufheld(struct bpf_d *d)
330 switch (d->bd_bufmode) {
331 case BPF_BUFMODE_ZBUF:
332 bpf_zerocopy_bufheld(d);
338 bpf_free(struct bpf_d *d)
341 switch (d->bd_bufmode) {
342 case BPF_BUFMODE_BUFFER:
343 return (bpf_buffer_free(d));
345 case BPF_BUFMODE_ZBUF:
346 return (bpf_zerocopy_free(d));
349 panic("bpf_buf_free");
354 bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
357 if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
359 return (bpf_buffer_uiomove(d, buf, len, uio));
363 bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
366 if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
368 return (bpf_buffer_ioctl_sblen(d, i));
372 bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
375 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
377 return (bpf_zerocopy_ioctl_getzmax(td, d, i));
381 bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
384 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
386 return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
390 bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
393 if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
395 return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
399 * General BPF functions.
402 bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
403 struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
405 const struct ieee80211_bpf_params *p;
406 struct ether_header *eh;
414 * Build a sockaddr based on the data link layer type.
415 * We do this at this level because the ethernet header
416 * is copied directly into the data field of the sockaddr.
417 * In the case of SLIP, there is no header and the packet
418 * is forwarded as is.
419 * Also, we are careful to leave room at the front of the mbuf
420 * for the link level header.
425 sockp->sa_family = AF_INET;
430 sockp->sa_family = AF_UNSPEC;
431 /* XXX Would MAXLINKHDR be better? */
432 hlen = ETHER_HDR_LEN;
436 sockp->sa_family = AF_IMPLINK;
441 sockp->sa_family = AF_UNSPEC;
447 * null interface types require a 4 byte pseudo header which
448 * corresponds to the address family of the packet.
450 sockp->sa_family = AF_UNSPEC;
454 case DLT_ATM_RFC1483:
456 * en atm driver requires 4-byte atm pseudo header.
457 * though it isn't standard, vpi:vci needs to be
460 sockp->sa_family = AF_UNSPEC;
461 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
465 sockp->sa_family = AF_UNSPEC;
466 hlen = 4; /* This should match PPP_HDRLEN */
469 case DLT_IEEE802_11: /* IEEE 802.11 wireless */
470 sockp->sa_family = AF_IEEE80211;
474 case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */
475 sockp->sa_family = AF_IEEE80211;
476 sockp->sa_len = 12; /* XXX != 0 */
477 hlen = sizeof(struct ieee80211_bpf_params);
484 len = uio->uio_resid;
486 if (len - hlen > ifp->if_mtu)
489 if ((unsigned)len > MJUM16BYTES)
493 MGETHDR(m, M_WAIT, MT_DATA);
494 else if (len <= MCLBYTES)
495 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
497 m = m_getjcl(M_WAIT, MT_DATA, M_PKTHDR,
498 #if (MJUMPAGESIZE > MCLBYTES)
499 len <= MJUMPAGESIZE ? MJUMPAGESIZE :
501 (len <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES));
502 m->m_pkthdr.len = m->m_len = len;
503 m->m_pkthdr.rcvif = NULL;
506 if (m->m_len < hlen) {
511 error = uiomove(mtod(m, u_char *), len, uio);
515 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
521 /* Check for multicast destination */
524 eh = mtod(m, struct ether_header *);
525 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
526 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
527 ETHER_ADDR_LEN) == 0)
528 m->m_flags |= M_BCAST;
530 m->m_flags |= M_MCAST;
536 * Make room for link header, and copy it to sockaddr
539 if (sockp->sa_family == AF_IEEE80211) {
541 * Collect true length from the parameter header
542 * NB: sockp is known to be zero'd so if we do a
543 * short copy unspecified parameters will be
545 * NB: packet may not be aligned after stripping
549 p = mtod(m, const struct ieee80211_bpf_params *);
551 if (hlen > sizeof(sockp->sa_data)) {
556 bcopy(m->m_data, sockp->sa_data, hlen);
567 * Attach file to the bpf interface, i.e. make d listen on bp.
570 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
573 * Point d at bp, and add d to the interface's list of listeners.
574 * Finally, point the driver's bpf cookie at the interface so
575 * it will divert packets to bpf.
579 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
584 EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
588 * Detach a file from its interface.
591 bpf_detachd(struct bpf_d *d)
600 ifp = d->bd_bif->bif_ifp;
603 * Remove d from the interface's descriptor list.
605 LIST_REMOVE(d, bd_next);
612 EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0);
615 * Check if this descriptor had requested promiscuous mode.
616 * If so, turn it off.
620 CURVNET_SET(ifp->if_vnet);
621 error = ifpromisc(ifp, 0);
623 if (error != 0 && error != ENXIO) {
625 * ENXIO can happen if a pccard is unplugged
626 * Something is really wrong if we were able to put
627 * the driver into promiscuous mode, but can't
630 if_printf(bp->bif_ifp,
631 "bpf_detach: ifpromisc failed (%d)\n", error);
637 * Close the descriptor by detaching it from its interface,
638 * deallocating its buffers, and marking it free.
643 struct bpf_d *d = data;
646 if (d->bd_state == BPF_WAITING)
647 callout_stop(&d->bd_callout);
648 d->bd_state = BPF_IDLE;
650 funsetown(&d->bd_sigio);
654 mtx_unlock(&bpf_mtx);
656 mac_bpfdesc_destroy(d);
658 seldrain(&d->bd_sel);
659 knlist_destroy(&d->bd_sel.si_note);
660 callout_drain(&d->bd_callout);
666 * Open ethernet device. Returns ENXIO for illegal minor device number,
667 * EBUSY if file is open by another process.
671 bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
676 d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
677 error = devfs_set_cdevpriv(d, bpf_dtor);
684 * For historical reasons, perform a one-time initialization call to
685 * the buffer routines, even though we're not yet committed to a
686 * particular buffer method.
689 d->bd_bufmode = BPF_BUFMODE_BUFFER;
691 d->bd_direction = BPF_D_INOUT;
692 d->bd_pid = td->td_proc->p_pid;
695 mac_bpfdesc_create(td->td_ucred, d);
697 mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
698 callout_init_mtx(&d->bd_callout, &d->bd_mtx, 0);
699 knlist_init_mtx(&d->bd_sel.si_note, &d->bd_mtx);
705 * bpfread - read next chunk of packets from buffers
708 bpfread(struct cdev *dev, struct uio *uio, int ioflag)
715 error = devfs_get_cdevpriv((void **)&d);
720 * Restrict application to use a buffer the same size as
723 if (uio->uio_resid != d->bd_bufsize)
726 non_block = ((ioflag & O_NONBLOCK) != 0);
729 d->bd_pid = curthread->td_proc->p_pid;
730 if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
734 if (d->bd_state == BPF_WAITING)
735 callout_stop(&d->bd_callout);
736 timed_out = (d->bd_state == BPF_TIMED_OUT);
737 d->bd_state = BPF_IDLE;
739 * If the hold buffer is empty, then do a timed sleep, which
740 * ends when the timeout expires or when enough packets
741 * have arrived to fill the store buffer.
743 while (d->bd_hbuf == NULL) {
744 if (d->bd_slen != 0) {
746 * A packet(s) either arrived since the previous
747 * read or arrived while we were asleep.
749 if (d->bd_immediate || non_block || timed_out) {
751 * Rotate the buffers and return what's here
752 * if we are in immediate mode, non-blocking
753 * flag is set, or this descriptor timed out.
761 * No data is available, check to see if the bpf device
762 * is still pointed at a real interface. If not, return
763 * ENXIO so that the userland process knows to rebind
764 * it before using it again.
766 if (d->bd_bif == NULL) {
773 return (EWOULDBLOCK);
775 error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
777 if (error == EINTR || error == ERESTART) {
781 if (error == EWOULDBLOCK) {
783 * On a timeout, return what's in the buffer,
784 * which may be nothing. If there is something
785 * in the store buffer, we can rotate the buffers.
789 * We filled up the buffer in between
790 * getting the timeout and arriving
791 * here, so we don't need to rotate.
795 if (d->bd_slen == 0) {
804 * At this point, we know we have something in the hold slot.
809 * Move data from hold buffer into user space.
810 * We know the entire buffer is transferred since
811 * we checked above that the read buffer is bpf_bufsize bytes.
813 * XXXRW: More synchronization needed here: what if a second thread
814 * issues a read on the same fd at the same time? Don't want this
815 * getting invalidated.
817 error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
820 d->bd_fbuf = d->bd_hbuf;
823 bpf_buf_reclaimed(d);
830 * If there are processes sleeping on this descriptor, wake them up.
833 bpf_wakeup(struct bpf_d *d)
837 if (d->bd_state == BPF_WAITING) {
838 callout_stop(&d->bd_callout);
839 d->bd_state = BPF_IDLE;
842 if (d->bd_async && d->bd_sig && d->bd_sigio)
843 pgsigio(&d->bd_sigio, d->bd_sig, 0);
845 selwakeuppri(&d->bd_sel, PRINET);
846 KNOTE_LOCKED(&d->bd_sel.si_note, 0);
850 bpf_timed_out(void *arg)
852 struct bpf_d *d = (struct bpf_d *)arg;
856 if (callout_pending(&d->bd_callout) || !callout_active(&d->bd_callout))
858 if (d->bd_state == BPF_WAITING) {
859 d->bd_state = BPF_TIMED_OUT;
866 bpf_ready(struct bpf_d *d)
871 if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
873 if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
880 bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
888 error = devfs_get_cdevpriv((void **)&d);
892 d->bd_pid = curthread->td_proc->p_pid;
894 if (d->bd_bif == NULL) {
899 ifp = d->bd_bif->bif_ifp;
901 if ((ifp->if_flags & IFF_UP) == 0) {
906 if (uio->uio_resid == 0) {
911 bzero(&dst, sizeof(dst));
914 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
915 &m, &dst, &hlen, d->bd_wfilter);
922 dst.sa_family = pseudo_AF_HDRCMPLT;
924 if (d->bd_feedback) {
925 mc = m_dup(m, M_DONTWAIT);
927 mc->m_pkthdr.rcvif = ifp;
928 /* Set M_PROMISC for outgoing packets to be discarded. */
929 if (d->bd_direction == BPF_D_INOUT)
930 m->m_flags |= M_PROMISC;
934 m->m_pkthdr.len -= hlen;
936 m->m_data += hlen; /* XXX */
938 CURVNET_SET(ifp->if_vnet);
941 mac_bpfdesc_create_mbuf(d, m);
943 mac_bpfdesc_create_mbuf(d, mc);
947 error = (*ifp->if_output)(ifp, m, &dst, NULL);
953 (*ifp->if_input)(ifp, mc);
963 * Reset a descriptor by flushing its packet buffer and clearing the receive
964 * and drop counts. This is doable for kernel-only buffers, but with
965 * zero-copy buffers, we can't write to (or rotate) buffers that are
966 * currently owned by userspace. It would be nice if we could encapsulate
967 * this logic in the buffer code rather than here.
970 reset_d(struct bpf_d *d)
973 mtx_assert(&d->bd_mtx, MA_OWNED);
975 if ((d->bd_hbuf != NULL) &&
976 (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) {
977 /* Free the hold buffer. */
978 d->bd_fbuf = d->bd_hbuf;
981 bpf_buf_reclaimed(d);
983 if (bpf_canwritebuf(d))
995 * FIONREAD Check for read packet available.
996 * SIOCGIFADDR Get interface address - convenient hook to driver.
997 * BIOCGBLEN Get buffer len [for read()].
998 * BIOCSETF Set read filter.
999 * BIOCSETFNR Set read filter without resetting descriptor.
1000 * BIOCSETWF Set write filter.
1001 * BIOCFLUSH Flush read packet buffer.
1002 * BIOCPROMISC Put interface into promiscuous mode.
1003 * BIOCGDLT Get link layer type.
1004 * BIOCGETIF Get interface name.
1005 * BIOCSETIF Set interface.
1006 * BIOCSRTIMEOUT Set read timeout.
1007 * BIOCGRTIMEOUT Get read timeout.
1008 * BIOCGSTATS Get packet stats.
1009 * BIOCIMMEDIATE Set immediate mode.
1010 * BIOCVERSION Get filter language version.
1011 * BIOCGHDRCMPLT Get "header already complete" flag
1012 * BIOCSHDRCMPLT Set "header already complete" flag
1013 * BIOCGDIRECTION Get packet direction flag
1014 * BIOCSDIRECTION Set packet direction flag
1015 * BIOCGTSTAMP Get time stamp format and resolution.
1016 * BIOCSTSTAMP Set time stamp format and resolution.
1017 * BIOCLOCK Set "locked" flag
1018 * BIOCFEEDBACK Set packet feedback mode.
1019 * BIOCSETZBUF Set current zero-copy buffer locations.
1020 * BIOCGETZMAX Get maximum zero-copy buffer size.
1021 * BIOCROTZBUF Force rotation of zero-copy buffer
1022 * BIOCSETBUFMODE Set buffer mode.
1023 * BIOCGETBUFMODE Get current buffer mode.
1027 bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
1033 error = devfs_get_cdevpriv((void **)&d);
1038 * Refresh PID associated with this descriptor.
1041 d->bd_pid = td->td_proc->p_pid;
1042 if (d->bd_state == BPF_WAITING)
1043 callout_stop(&d->bd_callout);
1044 d->bd_state = BPF_IDLE;
1047 if (d->bd_locked == 1) {
1053 #ifdef COMPAT_FREEBSD32
1054 case BIOCGDLTLIST32:
1058 #ifdef COMPAT_FREEBSD32
1059 case BIOCGRTIMEOUT32:
1070 #ifdef COMPAT_FREEBSD32
1071 case BIOCSRTIMEOUT32:
1081 #ifdef COMPAT_FREEBSD32
1083 * If we see a 32-bit compat ioctl, mark the stream as 32-bit so
1084 * that it will get 32-bit packet headers.
1090 case BIOCGDLTLIST32:
1091 case BIOCGRTIMEOUT32:
1092 case BIOCSRTIMEOUT32:
1097 CURVNET_SET(TD_TO_VNET(td));
1105 * Check for read packet available.
1125 if (d->bd_bif == NULL)
1128 ifp = d->bd_bif->bif_ifp;
1129 error = (*ifp->if_ioctl)(ifp, cmd, addr);
1135 * Get buffer len [for read()].
1138 *(u_int *)addr = d->bd_bufsize;
1142 * Set buffer length.
1145 error = bpf_ioctl_sblen(d, (u_int *)addr);
1149 * Set link layer read filter.
1154 #ifdef COMPAT_FREEBSD32
1159 error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1163 * Flush read packet buffer.
1172 * Put interface into promiscuous mode.
1175 if (d->bd_bif == NULL) {
1177 * No interface attached yet.
1182 if (d->bd_promisc == 0) {
1183 error = ifpromisc(d->bd_bif->bif_ifp, 1);
1190 * Get current data link type.
1193 if (d->bd_bif == NULL)
1196 *(u_int *)addr = d->bd_bif->bif_dlt;
1200 * Get a list of supported data link types.
1202 #ifdef COMPAT_FREEBSD32
1203 case BIOCGDLTLIST32:
1205 struct bpf_dltlist32 *list32;
1206 struct bpf_dltlist dltlist;
1208 list32 = (struct bpf_dltlist32 *)addr;
1209 dltlist.bfl_len = list32->bfl_len;
1210 dltlist.bfl_list = PTRIN(list32->bfl_list);
1211 if (d->bd_bif == NULL)
1214 error = bpf_getdltlist(d, &dltlist);
1216 list32->bfl_len = dltlist.bfl_len;
1223 if (d->bd_bif == NULL)
1226 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
1230 * Set data link type.
1233 if (d->bd_bif == NULL)
1236 error = bpf_setdlt(d, *(u_int *)addr);
1240 * Get interface name.
1243 if (d->bd_bif == NULL)
1246 struct ifnet *const ifp = d->bd_bif->bif_ifp;
1247 struct ifreq *const ifr = (struct ifreq *)addr;
1249 strlcpy(ifr->ifr_name, ifp->if_xname,
1250 sizeof(ifr->ifr_name));
1258 error = bpf_setif(d, (struct ifreq *)addr);
1265 #ifdef COMPAT_FREEBSD32
1266 case BIOCSRTIMEOUT32:
1269 struct timeval *tv = (struct timeval *)addr;
1270 #ifdef COMPAT_FREEBSD32
1271 struct timeval32 *tv32;
1272 struct timeval tv64;
1274 if (cmd == BIOCSRTIMEOUT32) {
1275 tv32 = (struct timeval32 *)addr;
1277 tv->tv_sec = tv32->tv_sec;
1278 tv->tv_usec = tv32->tv_usec;
1281 tv = (struct timeval *)addr;
1284 * Subtract 1 tick from tvtohz() since this isn't
1287 if ((error = itimerfix(tv)) == 0)
1288 d->bd_rtout = tvtohz(tv) - 1;
1296 #ifdef COMPAT_FREEBSD32
1297 case BIOCGRTIMEOUT32:
1301 #ifdef COMPAT_FREEBSD32
1302 struct timeval32 *tv32;
1303 struct timeval tv64;
1305 if (cmd == BIOCGRTIMEOUT32)
1309 tv = (struct timeval *)addr;
1311 tv->tv_sec = d->bd_rtout / hz;
1312 tv->tv_usec = (d->bd_rtout % hz) * tick;
1313 #ifdef COMPAT_FREEBSD32
1314 if (cmd == BIOCGRTIMEOUT32) {
1315 tv32 = (struct timeval32 *)addr;
1316 tv32->tv_sec = tv->tv_sec;
1317 tv32->tv_usec = tv->tv_usec;
1329 struct bpf_stat *bs = (struct bpf_stat *)addr;
1331 /* XXXCSJP overflow */
1332 bs->bs_recv = d->bd_rcount;
1333 bs->bs_drop = d->bd_dcount;
1338 * Set immediate mode.
1341 d->bd_immediate = *(u_int *)addr;
1346 struct bpf_version *bv = (struct bpf_version *)addr;
1348 bv->bv_major = BPF_MAJOR_VERSION;
1349 bv->bv_minor = BPF_MINOR_VERSION;
1354 * Get "header already complete" flag
1357 *(u_int *)addr = d->bd_hdrcmplt;
1361 * Set "header already complete" flag
1364 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1368 * Get packet direction flag
1370 case BIOCGDIRECTION:
1371 *(u_int *)addr = d->bd_direction;
1375 * Set packet direction flag
1377 case BIOCSDIRECTION:
1381 direction = *(u_int *)addr;
1382 switch (direction) {
1386 d->bd_direction = direction;
1395 * Get packet timestamp format and resolution.
1398 *(u_int *)addr = d->bd_tstamp;
1402 * Set packet timestamp format and resolution.
1408 func = *(u_int *)addr;
1409 if (BPF_T_VALID(func))
1410 d->bd_tstamp = func;
1417 d->bd_feedback = *(u_int *)addr;
1424 case FIONBIO: /* Non-blocking I/O */
1427 case FIOASYNC: /* Send signal on receive packets */
1428 d->bd_async = *(int *)addr;
1432 error = fsetown(*(int *)addr, &d->bd_sigio);
1436 *(int *)addr = fgetown(&d->bd_sigio);
1439 /* This is deprecated, FIOSETOWN should be used instead. */
1441 error = fsetown(-(*(int *)addr), &d->bd_sigio);
1444 /* This is deprecated, FIOGETOWN should be used instead. */
1446 *(int *)addr = -fgetown(&d->bd_sigio);
1449 case BIOCSRSIG: /* Set receive signal */
1453 sig = *(u_int *)addr;
1462 *(u_int *)addr = d->bd_sig;
1465 case BIOCGETBUFMODE:
1466 *(u_int *)addr = d->bd_bufmode;
1469 case BIOCSETBUFMODE:
1471 * Allow the buffering mode to be changed as long as we
1472 * haven't yet committed to a particular mode. Our
1473 * definition of commitment, for now, is whether or not a
1474 * buffer has been allocated or an interface attached, since
1475 * that's the point where things get tricky.
1477 switch (*(u_int *)addr) {
1478 case BPF_BUFMODE_BUFFER:
1481 case BPF_BUFMODE_ZBUF:
1482 if (bpf_zerocopy_enable)
1492 if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
1493 d->bd_fbuf != NULL || d->bd_bif != NULL) {
1498 d->bd_bufmode = *(u_int *)addr;
1503 error = bpf_ioctl_getzmax(td, d, (size_t *)addr);
1507 error = bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr);
1511 error = bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr);
1519 * Set d's packet filter program to fp. If this file already has a filter,
1520 * free it and replace it. Returns EINVAL for bogus requests.
1523 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1525 struct bpf_insn *fcode, *old;
1526 u_int wfilter, flen, size;
1528 bpf_jit_filter *ofunc;
1530 #ifdef COMPAT_FREEBSD32
1531 struct bpf_program32 *fp32;
1532 struct bpf_program fp_swab;
1534 if (cmd == BIOCSETWF32 || cmd == BIOCSETF32 || cmd == BIOCSETFNR32) {
1535 fp32 = (struct bpf_program32 *)fp;
1536 fp_swab.bf_len = fp32->bf_len;
1537 fp_swab.bf_insns = (struct bpf_insn *)(uintptr_t)fp32->bf_insns;
1539 if (cmd == BIOCSETWF32)
1543 if (cmd == BIOCSETWF) {
1544 old = d->bd_wfilter;
1551 old = d->bd_rfilter;
1553 ofunc = d->bd_bfilter;
1556 if (fp->bf_insns == NULL) {
1557 if (fp->bf_len != 0)
1561 d->bd_wfilter = NULL;
1563 d->bd_rfilter = NULL;
1565 d->bd_bfilter = NULL;
1567 if (cmd == BIOCSETF)
1572 free((caddr_t)old, M_BPF);
1575 bpf_destroy_jit_filter(ofunc);
1580 if (flen > bpf_maxinsns)
1583 size = flen * sizeof(*fp->bf_insns);
1584 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1585 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1586 bpf_validate(fcode, (int)flen)) {
1589 d->bd_wfilter = fcode;
1591 d->bd_rfilter = fcode;
1593 d->bd_bfilter = bpf_jitter(fcode, flen);
1595 if (cmd == BIOCSETF)
1600 free((caddr_t)old, M_BPF);
1603 bpf_destroy_jit_filter(ofunc);
1608 free((caddr_t)fcode, M_BPF);
1613 * Detach a file from its current interface (if attached at all) and attach
1614 * to the interface indicated by the name stored in ifr.
1615 * Return an errno or 0.
1618 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1621 struct ifnet *theywant;
1623 theywant = ifunit(ifr->ifr_name);
1624 if (theywant == NULL || theywant->if_bpf == NULL)
1627 bp = theywant->if_bpf;
1630 * Behavior here depends on the buffering model. If we're using
1631 * kernel memory buffers, then we can allocate them here. If we're
1632 * using zero-copy, then the user process must have registered
1633 * buffers by the time we get here. If not, return an error.
1635 * XXXRW: There are locking issues here with multi-threaded use: what
1636 * if two threads try to set the interface at once?
1638 switch (d->bd_bufmode) {
1639 case BPF_BUFMODE_BUFFER:
1640 if (d->bd_sbuf == NULL)
1641 bpf_buffer_alloc(d);
1642 KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL"));
1645 case BPF_BUFMODE_ZBUF:
1646 if (d->bd_sbuf == NULL)
1651 panic("bpf_setif: bufmode %d", d->bd_bufmode);
1653 if (bp != d->bd_bif) {
1656 * Detach if attached to something else.
1669 * Support for select() and poll() system calls
1671 * Return true iff the specific operation will not block indefinitely.
1672 * Otherwise, return false but make a note that a selwakeup() must be done.
1675 bpfpoll(struct cdev *dev, int events, struct thread *td)
1680 if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL)
1682 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1685 * Refresh PID associated with this descriptor.
1687 revents = events & (POLLOUT | POLLWRNORM);
1689 d->bd_pid = td->td_proc->p_pid;
1690 if (events & (POLLIN | POLLRDNORM)) {
1692 revents |= events & (POLLIN | POLLRDNORM);
1694 selrecord(td, &d->bd_sel);
1695 /* Start the read timeout if necessary. */
1696 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1697 callout_reset(&d->bd_callout, d->bd_rtout,
1699 d->bd_state = BPF_WAITING;
1708 * Support for kevent() system call. Register EVFILT_READ filters and
1709 * reject all others.
1712 bpfkqfilter(struct cdev *dev, struct knote *kn)
1716 if (devfs_get_cdevpriv((void **)&d) != 0 ||
1717 kn->kn_filter != EVFILT_READ)
1721 * Refresh PID associated with this descriptor.
1724 d->bd_pid = curthread->td_proc->p_pid;
1725 kn->kn_fop = &bpfread_filtops;
1727 knlist_add(&d->bd_sel.si_note, kn, 1);
1734 filt_bpfdetach(struct knote *kn)
1736 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1738 knlist_remove(&d->bd_sel.si_note, kn, 0);
1742 filt_bpfread(struct knote *kn, long hint)
1744 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1747 BPFD_LOCK_ASSERT(d);
1748 ready = bpf_ready(d);
1750 kn->kn_data = d->bd_slen;
1752 kn->kn_data += d->bd_hlen;
1753 } else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1754 callout_reset(&d->bd_callout, d->bd_rtout,
1756 d->bd_state = BPF_WAITING;
1762 #define BPF_TSTAMP_NONE 0
1763 #define BPF_TSTAMP_FAST 1
1764 #define BPF_TSTAMP_NORMAL 2
1765 #define BPF_TSTAMP_EXTERN 3
1768 bpf_ts_quality(int tstype)
1771 if (tstype == BPF_T_NONE)
1772 return (BPF_TSTAMP_NONE);
1773 if ((tstype & BPF_T_FAST) != 0)
1774 return (BPF_TSTAMP_FAST);
1776 return (BPF_TSTAMP_NORMAL);
1780 bpf_gettime(struct bintime *bt, int tstype, struct mbuf *m)
1785 quality = bpf_ts_quality(tstype);
1786 if (quality == BPF_TSTAMP_NONE)
1790 tag = m_tag_locate(m, MTAG_BPF, MTAG_BPF_TIMESTAMP, NULL);
1792 *bt = *(struct bintime *)(tag + 1);
1793 return (BPF_TSTAMP_EXTERN);
1796 if (quality == BPF_TSTAMP_NORMAL)
1805 * Incoming linkage from device drivers. Process the packet pkt, of length
1806 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1807 * by each process' filter, and if accepted, stashed into the corresponding
1811 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1821 gottime = BPF_TSTAMP_NONE;
1823 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1827 * NB: We dont call BPF_CHECK_DIRECTION() here since there is no
1828 * way for the caller to indiciate to us whether this packet
1829 * is inbound or outbound. In the bpf_mtap() routines, we use
1830 * the interface pointers on the mbuf to figure it out.
1833 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1835 slen = (*(bf->func))(pkt, pktlen, pktlen);
1838 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1841 if (gottime < bpf_ts_quality(d->bd_tstamp))
1842 gottime = bpf_gettime(&bt, d->bd_tstamp, NULL);
1844 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1846 catchpacket(d, pkt, pktlen, slen,
1847 bpf_append_bytes, &bt);
1854 #define BPF_CHECK_DIRECTION(d, r, i) \
1855 (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \
1856 ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
1859 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1862 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1872 /* Skip outgoing duplicate packets. */
1873 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1874 m->m_flags &= ~M_PROMISC;
1878 pktlen = m_length(m, NULL);
1880 gottime = BPF_TSTAMP_NONE;
1882 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1883 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1888 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1889 /* XXX We cannot handle multiple mbufs. */
1890 if (bf != NULL && m->m_next == NULL)
1891 slen = (*(bf->func))(mtod(m, u_char *), pktlen, pktlen);
1894 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1897 if (gottime < bpf_ts_quality(d->bd_tstamp))
1898 gottime = bpf_gettime(&bt, d->bd_tstamp, m);
1900 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1902 catchpacket(d, (u_char *)m, pktlen, slen,
1903 bpf_append_mbuf, &bt);
1911 * Incoming linkage from device drivers, when packet is in
1912 * an mbuf chain and to be prepended by a contiguous header.
1915 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1923 /* Skip outgoing duplicate packets. */
1924 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1925 m->m_flags &= ~M_PROMISC;
1929 pktlen = m_length(m, NULL);
1931 * Craft on-stack mbuf suitable for passing to bpf_filter.
1932 * Note that we cut corners here; we only setup what's
1933 * absolutely needed--this mbuf should never go anywhere else.
1940 gottime = BPF_TSTAMP_NONE;
1942 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1943 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1947 slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1950 if (gottime < bpf_ts_quality(d->bd_tstamp))
1951 gottime = bpf_gettime(&bt, d->bd_tstamp, m);
1953 if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1955 catchpacket(d, (u_char *)&mb, pktlen, slen,
1956 bpf_append_mbuf, &bt);
1963 #undef BPF_CHECK_DIRECTION
1965 #undef BPF_TSTAMP_NONE
1966 #undef BPF_TSTAMP_FAST
1967 #undef BPF_TSTAMP_NORMAL
1968 #undef BPF_TSTAMP_EXTERN
1971 bpf_hdrlen(struct bpf_d *d)
1975 hdrlen = d->bd_bif->bif_hdrlen;
1976 #ifndef BURN_BRIDGES
1977 if (d->bd_tstamp == BPF_T_NONE ||
1978 BPF_T_FORMAT(d->bd_tstamp) == BPF_T_MICROTIME)
1979 #ifdef COMPAT_FREEBSD32
1981 hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr32);
1984 hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr);
1987 hdrlen += SIZEOF_BPF_HDR(struct bpf_xhdr);
1988 #ifdef COMPAT_FREEBSD32
1990 hdrlen = BPF_WORDALIGN32(hdrlen);
1993 hdrlen = BPF_WORDALIGN(hdrlen);
1995 return (hdrlen - d->bd_bif->bif_hdrlen);
1999 bpf_bintime2ts(struct bintime *bt, struct bpf_ts *ts, int tstype)
2003 struct timespec tsn;
2005 if ((tstype & BPF_T_MONOTONIC) == 0) {
2007 bintime_add(&bt2, &boottimebin);
2010 switch (BPF_T_FORMAT(tstype)) {
2011 case BPF_T_MICROTIME:
2012 bintime2timeval(bt, &tsm);
2013 ts->bt_sec = tsm.tv_sec;
2014 ts->bt_frac = tsm.tv_usec;
2016 case BPF_T_NANOTIME:
2017 bintime2timespec(bt, &tsn);
2018 ts->bt_sec = tsn.tv_sec;
2019 ts->bt_frac = tsn.tv_nsec;
2022 ts->bt_sec = bt->sec;
2023 ts->bt_frac = bt->frac;
2029 * Move the packet data from interface memory (pkt) into the
2030 * store buffer. "cpfn" is the routine called to do the actual data
2031 * transfer. bcopy is passed in to copy contiguous chunks, while
2032 * bpf_append_mbuf is passed in to copy mbuf chains. In the latter case,
2033 * pkt is really an mbuf.
2036 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
2037 void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
2040 struct bpf_xhdr hdr;
2041 #ifndef BURN_BRIDGES
2042 struct bpf_hdr hdr_old;
2043 #ifdef COMPAT_FREEBSD32
2044 struct bpf_hdr32 hdr32_old;
2047 int caplen, curlen, hdrlen, totlen;
2052 BPFD_LOCK_ASSERT(d);
2055 * Detect whether user space has released a buffer back to us, and if
2056 * so, move it from being a hold buffer to a free buffer. This may
2057 * not be the best place to do it (for example, we might only want to
2058 * run this check if we need the space), but for now it's a reliable
2061 if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
2062 d->bd_fbuf = d->bd_hbuf;
2065 bpf_buf_reclaimed(d);
2069 * Figure out how many bytes to move. If the packet is
2070 * greater or equal to the snapshot length, transfer that
2071 * much. Otherwise, transfer the whole packet (unless
2072 * we hit the buffer size limit).
2074 hdrlen = bpf_hdrlen(d);
2075 totlen = hdrlen + min(snaplen, pktlen);
2076 if (totlen > d->bd_bufsize)
2077 totlen = d->bd_bufsize;
2080 * Round up the end of the previous packet to the next longword.
2082 * Drop the packet if there's no room and no hope of room
2083 * If the packet would overflow the storage buffer or the storage
2084 * buffer is considered immutable by the buffer model, try to rotate
2085 * the buffer and wakeup pending processes.
2087 #ifdef COMPAT_FREEBSD32
2089 curlen = BPF_WORDALIGN32(d->bd_slen);
2092 curlen = BPF_WORDALIGN(d->bd_slen);
2093 if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
2094 if (d->bd_fbuf == NULL) {
2096 * There's no room in the store buffer, and no
2097 * prospect of room, so drop the packet. Notify the
2107 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
2109 * Immediate mode is set, or the read timeout has already
2110 * expired during a select call. A packet arrived, so the
2111 * reader should be woken up.
2114 caplen = totlen - hdrlen;
2115 tstype = d->bd_tstamp;
2116 do_timestamp = tstype != BPF_T_NONE;
2117 #ifndef BURN_BRIDGES
2118 if (tstype == BPF_T_NONE || BPF_T_FORMAT(tstype) == BPF_T_MICROTIME) {
2121 bpf_bintime2ts(bt, &ts, tstype);
2122 #ifdef COMPAT_FREEBSD32
2123 if (d->bd_compat32) {
2124 bzero(&hdr32_old, sizeof(hdr32_old));
2126 hdr32_old.bh_tstamp.tv_sec = ts.bt_sec;
2127 hdr32_old.bh_tstamp.tv_usec = ts.bt_frac;
2129 hdr32_old.bh_datalen = pktlen;
2130 hdr32_old.bh_hdrlen = hdrlen;
2131 hdr32_old.bh_caplen = caplen;
2132 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr32_old,
2137 bzero(&hdr_old, sizeof(hdr_old));
2139 hdr_old.bh_tstamp.tv_sec = ts.bt_sec;
2140 hdr_old.bh_tstamp.tv_usec = ts.bt_frac;
2142 hdr_old.bh_datalen = pktlen;
2143 hdr_old.bh_hdrlen = hdrlen;
2144 hdr_old.bh_caplen = caplen;
2145 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr_old,
2152 * Append the bpf header. Note we append the actual header size, but
2153 * move forward the length of the header plus padding.
2155 bzero(&hdr, sizeof(hdr));
2157 bpf_bintime2ts(bt, &hdr.bh_tstamp, tstype);
2158 hdr.bh_datalen = pktlen;
2159 hdr.bh_hdrlen = hdrlen;
2160 hdr.bh_caplen = caplen;
2161 bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
2164 * Copy the packet data into the store buffer and update its length.
2166 #ifndef BURN_BRIDGES
2169 (*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, caplen);
2170 d->bd_slen = curlen + totlen;
2177 * Free buffers currently in use by a descriptor.
2181 bpf_freed(struct bpf_d *d)
2185 * We don't need to lock out interrupts since this descriptor has
2186 * been detached from its interface and it yet hasn't been marked
2190 if (d->bd_rfilter != NULL) {
2191 free((caddr_t)d->bd_rfilter, M_BPF);
2193 if (d->bd_bfilter != NULL)
2194 bpf_destroy_jit_filter(d->bd_bfilter);
2197 if (d->bd_wfilter != NULL)
2198 free((caddr_t)d->bd_wfilter, M_BPF);
2199 mtx_destroy(&d->bd_mtx);
2203 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
2204 * fixed size of the link header (variable length headers not yet supported).
2207 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2210 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2214 * Attach an interface to bpf. ifp is a pointer to the structure
2215 * defining the interface to be attached, dlt is the link layer type,
2216 * and hdrlen is the fixed size of the link header (variable length
2217 * headers are not yet supporrted).
2220 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
2224 bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
2228 LIST_INIT(&bp->bif_dlist);
2231 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
2232 KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
2236 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
2237 mtx_unlock(&bpf_mtx);
2239 bp->bif_hdrlen = hdrlen;
2242 if_printf(ifp, "bpf attached\n");
2246 * Detach bpf from an interface. This involves detaching each descriptor
2247 * associated with the interface, and leaving bd_bif NULL. Notify each
2248 * descriptor as it's detached so that any sleepers wake up and get
2252 bpfdetach(struct ifnet *ifp)
2257 /* Locate BPF interface information */
2259 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2260 if (ifp == bp->bif_ifp)
2264 /* Interface wasn't attached */
2265 if ((bp == NULL) || (bp->bif_ifp == NULL)) {
2266 mtx_unlock(&bpf_mtx);
2267 printf("bpfdetach: %s was not attached\n", ifp->if_xname);
2271 LIST_REMOVE(bp, bif_next);
2272 mtx_unlock(&bpf_mtx);
2274 while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
2281 mtx_destroy(&bp->bif_mtx);
2286 * Get a list of available data link type of the interface.
2289 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
2295 ifp = d->bd_bif->bif_ifp;
2299 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2300 if (bp->bif_ifp != ifp)
2302 if (bfl->bfl_list != NULL) {
2303 if (n >= bfl->bfl_len) {
2304 mtx_unlock(&bpf_mtx);
2307 error = copyout(&bp->bif_dlt,
2308 bfl->bfl_list + n, sizeof(u_int));
2312 mtx_unlock(&bpf_mtx);
2318 * Set the data link type of a BPF instance.
2321 bpf_setdlt(struct bpf_d *d, u_int dlt)
2323 int error, opromisc;
2327 if (d->bd_bif->bif_dlt == dlt)
2329 ifp = d->bd_bif->bif_ifp;
2331 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2332 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2335 mtx_unlock(&bpf_mtx);
2337 opromisc = d->bd_promisc;
2344 error = ifpromisc(bp->bif_ifp, 1);
2346 if_printf(bp->bif_ifp,
2347 "bpf_setdlt: ifpromisc failed (%d)\n",
2353 return (bp == NULL ? EINVAL : 0);
2357 bpf_drvinit(void *unused)
2361 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
2362 LIST_INIT(&bpf_iflist);
2364 dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf");
2365 /* For compatibility */
2366 make_dev_alias(dev, "bpf0");
2370 * Zero out the various packet counters associated with all of the bpf
2371 * descriptors. At some point, we will probably want to get a bit more
2372 * granular and allow the user to specify descriptors to be zeroed.
2375 bpf_zero_counters(void)
2381 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2383 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2395 mtx_unlock(&bpf_mtx);
2399 bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
2402 bzero(d, sizeof(*d));
2403 BPFD_LOCK_ASSERT(bd);
2404 d->bd_structsize = sizeof(*d);
2405 d->bd_immediate = bd->bd_immediate;
2406 d->bd_promisc = bd->bd_promisc;
2407 d->bd_hdrcmplt = bd->bd_hdrcmplt;
2408 d->bd_direction = bd->bd_direction;
2409 d->bd_feedback = bd->bd_feedback;
2410 d->bd_async = bd->bd_async;
2411 d->bd_rcount = bd->bd_rcount;
2412 d->bd_dcount = bd->bd_dcount;
2413 d->bd_fcount = bd->bd_fcount;
2414 d->bd_sig = bd->bd_sig;
2415 d->bd_slen = bd->bd_slen;
2416 d->bd_hlen = bd->bd_hlen;
2417 d->bd_bufsize = bd->bd_bufsize;
2418 d->bd_pid = bd->bd_pid;
2419 strlcpy(d->bd_ifname,
2420 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
2421 d->bd_locked = bd->bd_locked;
2422 d->bd_wcount = bd->bd_wcount;
2423 d->bd_wdcount = bd->bd_wdcount;
2424 d->bd_wfcount = bd->bd_wfcount;
2425 d->bd_zcopy = bd->bd_zcopy;
2426 d->bd_bufmode = bd->bd_bufmode;
2430 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
2432 struct xbpf_d *xbdbuf, *xbd, zerostats;
2438 * XXX This is not technically correct. It is possible for non
2439 * privileged users to open bpf devices. It would make sense
2440 * if the users who opened the devices were able to retrieve
2441 * the statistics for them, too.
2443 error = priv_check(req->td, PRIV_NET_BPF);
2447 * Check to see if the user is requesting that the counters be
2448 * zeroed out. Explicitly check that the supplied data is zeroed,
2449 * as we aren't allowing the user to set the counters currently.
2451 if (req->newptr != NULL) {
2452 if (req->newlen != sizeof(zerostats))
2454 bzero(&zerostats, sizeof(zerostats));
2456 if (bcmp(xbd, &zerostats, sizeof(*xbd)) != 0)
2458 bpf_zero_counters();
2461 if (req->oldptr == NULL)
2462 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
2463 if (bpf_bpfd_cnt == 0)
2464 return (SYSCTL_OUT(req, 0, 0));
2465 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
2467 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2468 mtx_unlock(&bpf_mtx);
2469 free(xbdbuf, M_BPF);
2473 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2475 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2476 xbd = &xbdbuf[index++];
2478 bpfstats_fill_xbpf(xbd, bd);
2483 mtx_unlock(&bpf_mtx);
2484 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2485 free(xbdbuf, M_BPF);
2489 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
2491 #else /* !DEV_BPF && !NETGRAPH_BPF */
2493 * NOP stubs to allow bpf-using drivers to load and function.
2495 * A 'better' implementation would allow the core bpf functionality
2496 * to be loaded at runtime.
2498 static struct bpf_if bp_null;
2501 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2506 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2511 bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
2516 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2519 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2523 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
2526 *driverp = &bp_null;
2530 bpfdetach(struct ifnet *ifp)
2535 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
2537 return -1; /* "no filter" behaviour */
2541 bpf_validate(const struct bpf_insn *f, int len)
2543 return 0; /* false */
2546 #endif /* !DEV_BPF && !NETGRAPH_BPF */