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"
43 #include "opt_netgraph.h"
45 #include <sys/types.h>
46 #include <sys/param.h>
48 #include <sys/rwlock.h>
49 #include <sys/systm.h>
51 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
58 #include <sys/signalvar.h>
59 #include <sys/filio.h>
60 #include <sys/sockio.h>
61 #include <sys/ttycom.h>
64 #include <sys/event.h>
69 #include <sys/socket.h>
76 #include <net/if_var.h>
77 #include <net/if_dl.h>
79 #include <net/bpf_buffer.h>
81 #include <net/bpf_jitter.h>
83 #include <net/bpf_zerocopy.h>
84 #include <net/bpfdesc.h>
85 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/if_ether.h>
90 #include <sys/kernel.h>
91 #include <sys/sysctl.h>
93 #include <net80211/ieee80211_freebsd.h>
95 #include <security/mac/mac_framework.h>
97 MALLOC_DEFINE(M_BPF
, "BPF", "BPF data");
100 #define bif_next bif_ext.bif_next
101 #define bif_dlist bif_ext.bif_dlist
102 struct bpf_if_ext bif_ext
; /* public members */
103 u_int bif_dlt
; /* link layer type */
104 u_int bif_hdrlen
; /* length of link header */
105 struct ifnet
*bif_ifp
; /* corresponding interface */
106 struct rwlock bif_lock
; /* interface lock */
107 LIST_HEAD(, bpf_d
) bif_wlist
; /* writer-only list */
108 int bif_flags
; /* Interface flags */
111 CTASSERT(offsetof(struct bpf_if
, bif_ext
) == 0);
113 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
115 #define PRINET 26 /* interruptible */
117 #define SIZEOF_BPF_HDR(type) \
118 (offsetof(type, bh_hdrlen) + sizeof(((type *)0)->bh_hdrlen))
120 #ifdef COMPAT_FREEBSD32
121 #include <sys/mount.h>
122 #include <compat/freebsd32/freebsd32.h>
123 #define BPF_ALIGNMENT32 sizeof(int32_t)
124 #define BPF_WORDALIGN32(x) roundup2(x, BPF_ALIGNMENT32)
128 * 32-bit version of structure prepended to each packet. We use this header
129 * instead of the standard one for 32-bit streams. We mark the a stream as
130 * 32-bit the first time we see a 32-bit compat ioctl request.
133 struct timeval32 bh_tstamp
; /* time stamp */
134 uint32_t bh_caplen
; /* length of captured portion */
135 uint32_t bh_datalen
; /* original length of packet */
136 uint16_t bh_hdrlen
; /* length of bpf header (this struct
137 plus alignment padding) */
141 struct bpf_program32
{
146 struct bpf_dltlist32
{
151 #define BIOCSETF32 _IOW('B', 103, struct bpf_program32)
152 #define BIOCSRTIMEOUT32 _IOW('B', 109, struct timeval32)
153 #define BIOCGRTIMEOUT32 _IOR('B', 110, struct timeval32)
154 #define BIOCGDLTLIST32 _IOWR('B', 121, struct bpf_dltlist32)
155 #define BIOCSETWF32 _IOW('B', 123, struct bpf_program32)
156 #define BIOCSETFNR32 _IOW('B', 130, struct bpf_program32)
160 * bpf_iflist is a list of BPF interface structures, each corresponding to a
161 * specific DLT. The same network interface might have several BPF interface
162 * structures registered by different layers in the stack (i.e., 802.11
163 * frames, ethernet frames, etc).
165 static LIST_HEAD(, bpf_if
) bpf_iflist
, bpf_freelist
;
166 static struct mtx bpf_mtx
; /* bpf global lock */
167 static int bpf_bpfd_cnt
;
169 static void bpf_attachd(struct bpf_d
*, struct bpf_if
*);
170 static void bpf_detachd(struct bpf_d
*);
171 static void bpf_detachd_locked(struct bpf_d
*);
172 static void bpf_freed(struct bpf_d
*);
173 static int bpf_movein(struct uio
*, int, struct ifnet
*, struct mbuf
**,
174 struct sockaddr
*, int *, struct bpf_d
*);
175 static int bpf_setif(struct bpf_d
*, struct ifreq
*);
176 static void bpf_timed_out(void *);
178 bpf_wakeup(struct bpf_d
*);
179 static void catchpacket(struct bpf_d
*, u_char
*, u_int
, u_int
,
180 void (*)(struct bpf_d
*, caddr_t
, u_int
, void *, u_int
),
182 static void reset_d(struct bpf_d
*);
183 static int bpf_setf(struct bpf_d
*, struct bpf_program
*, u_long cmd
);
184 static int bpf_getdltlist(struct bpf_d
*, struct bpf_dltlist
*);
185 static int bpf_setdlt(struct bpf_d
*, u_int
);
186 static void filt_bpfdetach(struct knote
*);
187 static int filt_bpfread(struct knote
*, long);
188 static void bpf_drvinit(void *);
189 static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS
);
191 SYSCTL_NODE(_net
, OID_AUTO
, bpf
, CTLFLAG_RW
, 0, "bpf sysctl");
192 int bpf_maxinsns
= BPF_MAXINSNS
;
193 SYSCTL_INT(_net_bpf
, OID_AUTO
, maxinsns
, CTLFLAG_RW
,
194 &bpf_maxinsns
, 0, "Maximum bpf program instructions");
195 static int bpf_zerocopy_enable
= 0;
196 SYSCTL_INT(_net_bpf
, OID_AUTO
, zerocopy_enable
, CTLFLAG_RW
,
197 &bpf_zerocopy_enable
, 0, "Enable new zero-copy BPF buffer sessions");
198 static SYSCTL_NODE(_net_bpf
, OID_AUTO
, stats
, CTLFLAG_MPSAFE
| CTLFLAG_RW
,
199 bpf_stats_sysctl
, "bpf statistics portal");
201 static VNET_DEFINE(int, bpf_optimize_writers
) = 0;
202 #define V_bpf_optimize_writers VNET(bpf_optimize_writers)
203 SYSCTL_INT(_net_bpf
, OID_AUTO
, optimize_writers
, CTLFLAG_VNET
| CTLFLAG_RW
,
204 &VNET_NAME(bpf_optimize_writers
), 0,
205 "Do not send packets until BPF program is set");
207 static d_open_t bpfopen
;
208 static d_read_t bpfread
;
209 static d_write_t bpfwrite
;
210 static d_ioctl_t bpfioctl
;
211 static d_poll_t bpfpoll
;
212 static d_kqfilter_t bpfkqfilter
;
214 static struct cdevsw bpf_cdevsw
= {
215 .d_version
= D_VERSION
,
222 .d_kqfilter
= bpfkqfilter
,
225 static struct filterops bpfread_filtops
= {
227 .f_detach
= filt_bpfdetach
,
228 .f_event
= filt_bpfread
,
231 eventhandler_tag bpf_ifdetach_cookie
= NULL
;
234 * LOCKING MODEL USED BY BPF:
236 * 1) global lock (BPF_LOCK). Mutex, used to protect interface addition/removal,
237 * some global counters and every bpf_if reference.
238 * 2) Interface lock. Rwlock, used to protect list of BPF descriptors and their filters.
239 * 3) Descriptor lock. Mutex, used to protect BPF buffers and various structure fields
240 * used by bpf_mtap code.
244 * Global lock, interface lock, descriptor lock
246 * We have to acquire interface lock before descriptor main lock due to BPF_MTAP[2]
247 * working model. In many places (like bpf_detachd) we start with BPF descriptor
248 * (and we need to at least rlock it to get reliable interface pointer). This
249 * gives us potential LOR. As a result, we use global lock to protect from bpf_if
250 * change in every such place.
252 * Changing d->bd_bif is protected by 1) global lock, 2) interface lock and
253 * 3) descriptor main wlock.
254 * Reading bd_bif can be protected by any of these locks, typically global lock.
256 * Changing read/write BPF filter is protected by the same three locks,
257 * the same applies for reading.
259 * Sleeping in global lock is not allowed due to bpfdetach() using it.
263 * Wrapper functions for various buffering methods. If the set of buffer
264 * modes expands, we will probably want to introduce a switch data structure
265 * similar to protosw, et.
268 bpf_append_bytes(struct bpf_d
*d
, caddr_t buf
, u_int offset
, void *src
,
274 switch (d
->bd_bufmode
) {
275 case BPF_BUFMODE_BUFFER
:
276 return (bpf_buffer_append_bytes(d
, buf
, offset
, src
, len
));
278 case BPF_BUFMODE_ZBUF
:
280 return (bpf_zerocopy_append_bytes(d
, buf
, offset
, src
, len
));
283 panic("bpf_buf_append_bytes");
288 bpf_append_mbuf(struct bpf_d
*d
, caddr_t buf
, u_int offset
, void *src
,
294 switch (d
->bd_bufmode
) {
295 case BPF_BUFMODE_BUFFER
:
296 return (bpf_buffer_append_mbuf(d
, buf
, offset
, src
, len
));
298 case BPF_BUFMODE_ZBUF
:
300 return (bpf_zerocopy_append_mbuf(d
, buf
, offset
, src
, len
));
303 panic("bpf_buf_append_mbuf");
308 * This function gets called when the free buffer is re-assigned.
311 bpf_buf_reclaimed(struct bpf_d
*d
)
316 switch (d
->bd_bufmode
) {
317 case BPF_BUFMODE_BUFFER
:
320 case BPF_BUFMODE_ZBUF
:
321 bpf_zerocopy_buf_reclaimed(d
);
325 panic("bpf_buf_reclaimed");
330 * If the buffer mechanism has a way to decide that a held buffer can be made
331 * free, then it is exposed via the bpf_canfreebuf() interface. (1) is
332 * returned if the buffer can be discarded, (0) is returned if it cannot.
335 bpf_canfreebuf(struct bpf_d
*d
)
340 switch (d
->bd_bufmode
) {
341 case BPF_BUFMODE_ZBUF
:
342 return (bpf_zerocopy_canfreebuf(d
));
348 * Allow the buffer model to indicate that the current store buffer is
349 * immutable, regardless of the appearance of space. Return (1) if the
350 * buffer is writable, and (0) if not.
353 bpf_canwritebuf(struct bpf_d
*d
)
357 switch (d
->bd_bufmode
) {
358 case BPF_BUFMODE_ZBUF
:
359 return (bpf_zerocopy_canwritebuf(d
));
365 * Notify buffer model that an attempt to write to the store buffer has
366 * resulted in a dropped packet, in which case the buffer may be considered
370 bpf_buffull(struct bpf_d
*d
)
375 switch (d
->bd_bufmode
) {
376 case BPF_BUFMODE_ZBUF
:
377 bpf_zerocopy_buffull(d
);
383 * Notify the buffer model that a buffer has moved into the hold position.
386 bpf_bufheld(struct bpf_d
*d
)
391 switch (d
->bd_bufmode
) {
392 case BPF_BUFMODE_ZBUF
:
393 bpf_zerocopy_bufheld(d
);
399 bpf_free(struct bpf_d
*d
)
402 switch (d
->bd_bufmode
) {
403 case BPF_BUFMODE_BUFFER
:
404 return (bpf_buffer_free(d
));
406 case BPF_BUFMODE_ZBUF
:
407 return (bpf_zerocopy_free(d
));
410 panic("bpf_buf_free");
415 bpf_uiomove(struct bpf_d
*d
, caddr_t buf
, u_int len
, struct uio
*uio
)
418 if (d
->bd_bufmode
!= BPF_BUFMODE_BUFFER
)
420 return (bpf_buffer_uiomove(d
, buf
, len
, uio
));
424 bpf_ioctl_sblen(struct bpf_d
*d
, u_int
*i
)
427 if (d
->bd_bufmode
!= BPF_BUFMODE_BUFFER
)
429 return (bpf_buffer_ioctl_sblen(d
, i
));
433 bpf_ioctl_getzmax(struct thread
*td
, struct bpf_d
*d
, size_t *i
)
436 if (d
->bd_bufmode
!= BPF_BUFMODE_ZBUF
)
438 return (bpf_zerocopy_ioctl_getzmax(td
, d
, i
));
442 bpf_ioctl_rotzbuf(struct thread
*td
, struct bpf_d
*d
, struct bpf_zbuf
*bz
)
445 if (d
->bd_bufmode
!= BPF_BUFMODE_ZBUF
)
447 return (bpf_zerocopy_ioctl_rotzbuf(td
, d
, bz
));
451 bpf_ioctl_setzbuf(struct thread
*td
, struct bpf_d
*d
, struct bpf_zbuf
*bz
)
454 if (d
->bd_bufmode
!= BPF_BUFMODE_ZBUF
)
456 return (bpf_zerocopy_ioctl_setzbuf(td
, d
, bz
));
460 * General BPF functions.
463 bpf_movein(struct uio
*uio
, int linktype
, struct ifnet
*ifp
, struct mbuf
**mp
,
464 struct sockaddr
*sockp
, int *hdrlen
, struct bpf_d
*d
)
466 const struct ieee80211_bpf_params
*p
;
467 struct ether_header
*eh
;
475 * Build a sockaddr based on the data link layer type.
476 * We do this at this level because the ethernet header
477 * is copied directly into the data field of the sockaddr.
478 * In the case of SLIP, there is no header and the packet
479 * is forwarded as is.
480 * Also, we are careful to leave room at the front of the mbuf
481 * for the link level header.
486 sockp
->sa_family
= AF_INET
;
491 sockp
->sa_family
= AF_UNSPEC
;
492 /* XXX Would MAXLINKHDR be better? */
493 hlen
= ETHER_HDR_LEN
;
497 sockp
->sa_family
= AF_IMPLINK
;
502 sockp
->sa_family
= AF_UNSPEC
;
508 * null interface types require a 4 byte pseudo header which
509 * corresponds to the address family of the packet.
511 sockp
->sa_family
= AF_UNSPEC
;
515 case DLT_ATM_RFC1483
:
517 * en atm driver requires 4-byte atm pseudo header.
518 * though it isn't standard, vpi:vci needs to be
521 sockp
->sa_family
= AF_UNSPEC
;
522 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
526 sockp
->sa_family
= AF_UNSPEC
;
527 hlen
= 4; /* This should match PPP_HDRLEN */
530 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
531 sockp
->sa_family
= AF_IEEE80211
;
535 case DLT_IEEE802_11_RADIO
: /* IEEE 802.11 wireless w/ phy params */
536 sockp
->sa_family
= AF_IEEE80211
;
537 sockp
->sa_len
= 12; /* XXX != 0 */
538 hlen
= sizeof(struct ieee80211_bpf_params
);
545 len
= uio
->uio_resid
;
546 if (len
< hlen
|| len
- hlen
> ifp
->if_mtu
)
549 m
= m_get2(len
, M_WAITOK
, MT_DATA
, M_PKTHDR
);
552 m
->m_pkthdr
.len
= m
->m_len
= len
;
555 error
= uiomove(mtod(m
, u_char
*), len
, uio
);
559 slen
= bpf_filter(d
->bd_wfilter
, mtod(m
, u_char
*), len
, len
);
565 /* Check for multicast destination */
568 eh
= mtod(m
, struct ether_header
*);
569 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
570 if (bcmp(ifp
->if_broadcastaddr
, eh
->ether_dhost
,
571 ETHER_ADDR_LEN
) == 0)
572 m
->m_flags
|= M_BCAST
;
574 m
->m_flags
|= M_MCAST
;
576 if (d
->bd_hdrcmplt
== 0) {
577 memcpy(eh
->ether_shost
, IF_LLADDR(ifp
),
578 sizeof(eh
->ether_shost
));
584 * Make room for link header, and copy it to sockaddr
587 if (sockp
->sa_family
== AF_IEEE80211
) {
589 * Collect true length from the parameter header
590 * NB: sockp is known to be zero'd so if we do a
591 * short copy unspecified parameters will be
593 * NB: packet may not be aligned after stripping
597 p
= mtod(m
, const struct ieee80211_bpf_params
*);
599 if (hlen
> sizeof(sockp
->sa_data
)) {
604 bcopy(mtod(m
, const void *), sockp
->sa_data
, hlen
);
615 * Attach file to the bpf interface, i.e. make d listen on bp.
618 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
625 * Save sysctl value to protect from sysctl change
628 op_w
= V_bpf_optimize_writers
|| d
->bd_writer
;
630 if (d
->bd_bif
!= NULL
)
631 bpf_detachd_locked(d
);
633 * Point d at bp, and add d to the interface's list.
634 * Since there are many applications using BPF for
635 * sending raw packets only (dhcpd, cdpd are good examples)
636 * we can delay adding d to the list of active listeners until
637 * some filter is configured.
646 /* Add to writers-only list */
647 LIST_INSERT_HEAD(&bp
->bif_wlist
, d
, bd_next
);
649 * We decrement bd_writer on every filter set operation.
650 * First BIOCSETF is done by pcap_open_live() to set up
651 * snap length. After that appliation usually sets its own filter
655 LIST_INSERT_HEAD(&bp
->bif_dlist
, d
, bd_next
);
662 CTR3(KTR_NET
, "%s: bpf_attach called by pid %d, adding to %s list",
663 __func__
, d
->bd_pid
, d
->bd_writer
? "writer" : "active");
666 EVENTHANDLER_INVOKE(bpf_track
, bp
->bif_ifp
, bp
->bif_dlt
, 1);
670 * Check if we need to upgrade our descriptor @d from write-only mode.
673 bpf_check_upgrade(u_long cmd
, struct bpf_d
*d
, struct bpf_insn
*fcode
, int flen
)
675 int is_snap
, need_upgrade
;
678 * Check if we've already upgraded or new filter is empty.
680 if (d
->bd_writer
== 0 || fcode
== NULL
)
686 * Check if cmd looks like snaplen setting from
687 * pcap_bpf.c:pcap_open_live().
688 * Note we're not checking .k value here:
689 * while pcap_open_live() definitely sets to to non-zero value,
690 * we'd prefer to treat k=0 (deny ALL) case the same way: e.g.
691 * do not consider upgrading immediately
693 if (cmd
== BIOCSETF
&& flen
== 1 && fcode
[0].code
== (BPF_RET
| BPF_K
))
700 * We're setting first filter and it doesn't look like
701 * setting snaplen. We're probably using bpf directly.
702 * Upgrade immediately.
707 * Do not require upgrade by first BIOCSETF
708 * (used to set snaplen) by pcap_open_live().
711 if (--d
->bd_writer
== 0) {
713 * First snaplen filter has already
714 * been set. This is probably catch-all
722 "%s: filter function set by pid %d, "
723 "bd_writer counter %d, snap %d upgrade %d",
724 __func__
, d
->bd_pid
, d
->bd_writer
,
725 is_snap
, need_upgrade
);
727 return (need_upgrade
);
731 * Add d to the list of active bp filters.
732 * Requires bpf_attachd() to be called before.
735 bpf_upgraded(struct bpf_d
*d
)
744 * Filter can be set several times without specifying interface.
745 * Mark d as reader and exit.
757 /* Remove from writers-only list */
758 LIST_REMOVE(d
, bd_next
);
759 LIST_INSERT_HEAD(&bp
->bif_dlist
, d
, bd_next
);
760 /* Mark d as reader */
766 CTR2(KTR_NET
, "%s: upgrade required by pid %d", __func__
, d
->bd_pid
);
768 EVENTHANDLER_INVOKE(bpf_track
, bp
->bif_ifp
, bp
->bif_dlt
, 1);
772 * Detach a file from its interface.
775 bpf_detachd(struct bpf_d
*d
)
778 bpf_detachd_locked(d
);
783 bpf_detachd_locked(struct bpf_d
*d
)
789 CTR2(KTR_NET
, "%s: detach required by pid %d", __func__
, d
->bd_pid
);
793 /* Check if descriptor is attached */
794 if ((bp
= d
->bd_bif
) == NULL
)
800 /* Save bd_writer value */
801 error
= d
->bd_writer
;
804 * Remove d from the interface's descriptor list.
806 LIST_REMOVE(d
, bd_next
);
815 /* Call event handler iff d is attached */
817 EVENTHANDLER_INVOKE(bpf_track
, ifp
, bp
->bif_dlt
, 0);
820 * Check if this descriptor had requested promiscuous mode.
821 * If so, turn it off.
825 CURVNET_SET(ifp
->if_vnet
);
826 error
= ifpromisc(ifp
, 0);
828 if (error
!= 0 && error
!= ENXIO
) {
830 * ENXIO can happen if a pccard is unplugged
831 * Something is really wrong if we were able to put
832 * the driver into promiscuous mode, but can't
835 if_printf(bp
->bif_ifp
,
836 "bpf_detach: ifpromisc failed (%d)\n", error
);
842 * Close the descriptor by detaching it from its interface,
843 * deallocating its buffers, and marking it free.
848 struct bpf_d
*d
= data
;
851 if (d
->bd_state
== BPF_WAITING
)
852 callout_stop(&d
->bd_callout
);
853 d
->bd_state
= BPF_IDLE
;
855 funsetown(&d
->bd_sigio
);
858 mac_bpfdesc_destroy(d
);
860 seldrain(&d
->bd_sel
);
861 knlist_destroy(&d
->bd_sel
.si_note
);
862 callout_drain(&d
->bd_callout
);
868 * Open ethernet device. Returns ENXIO for illegal minor device number,
869 * EBUSY if file is open by another process.
873 bpfopen(struct cdev
*dev
, int flags
, int fmt
, struct thread
*td
)
878 d
= malloc(sizeof(*d
), M_BPF
, M_WAITOK
| M_ZERO
);
879 error
= devfs_set_cdevpriv(d
, bpf_dtor
);
886 * For historical reasons, perform a one-time initialization call to
887 * the buffer routines, even though we're not yet committed to a
888 * particular buffer method.
891 if ((flags
& FREAD
) == 0)
893 d
->bd_hbuf_in_use
= 0;
894 d
->bd_bufmode
= BPF_BUFMODE_BUFFER
;
896 d
->bd_direction
= BPF_D_INOUT
;
897 BPF_PID_REFRESH(d
, td
);
900 mac_bpfdesc_create(td
->td_ucred
, d
);
902 mtx_init(&d
->bd_lock
, devtoname(dev
), "bpf cdev lock", MTX_DEF
);
903 callout_init_mtx(&d
->bd_callout
, &d
->bd_lock
, 0);
904 knlist_init_mtx(&d
->bd_sel
.si_note
, &d
->bd_lock
);
910 * bpfread - read next chunk of packets from buffers
913 bpfread(struct cdev
*dev
, struct uio
*uio
, int ioflag
)
920 error
= devfs_get_cdevpriv((void **)&d
);
925 * Restrict application to use a buffer the same size as
928 if (uio
->uio_resid
!= d
->bd_bufsize
)
931 non_block
= ((ioflag
& O_NONBLOCK
) != 0);
934 BPF_PID_REFRESH_CUR(d
);
935 if (d
->bd_bufmode
!= BPF_BUFMODE_BUFFER
) {
939 if (d
->bd_state
== BPF_WAITING
)
940 callout_stop(&d
->bd_callout
);
941 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
942 d
->bd_state
= BPF_IDLE
;
943 while (d
->bd_hbuf_in_use
) {
944 error
= mtx_sleep(&d
->bd_hbuf_in_use
, &d
->bd_lock
,
945 PRINET
|PCATCH
, "bd_hbuf", 0);
952 * If the hold buffer is empty, then do a timed sleep, which
953 * ends when the timeout expires or when enough packets
954 * have arrived to fill the store buffer.
956 while (d
->bd_hbuf
== NULL
) {
957 if (d
->bd_slen
!= 0) {
959 * A packet(s) either arrived since the previous
960 * read or arrived while we were asleep.
962 if (d
->bd_immediate
|| non_block
|| timed_out
) {
964 * Rotate the buffers and return what's here
965 * if we are in immediate mode, non-blocking
966 * flag is set, or this descriptor timed out.
974 * No data is available, check to see if the bpf device
975 * is still pointed at a real interface. If not, return
976 * ENXIO so that the userland process knows to rebind
977 * it before using it again.
979 if (d
->bd_bif
== NULL
) {
986 return (EWOULDBLOCK
);
988 error
= msleep(d
, &d
->bd_lock
, PRINET
|PCATCH
,
990 if (error
== EINTR
|| error
== ERESTART
) {
994 if (error
== EWOULDBLOCK
) {
996 * On a timeout, return what's in the buffer,
997 * which may be nothing. If there is something
998 * in the store buffer, we can rotate the buffers.
1002 * We filled up the buffer in between
1003 * getting the timeout and arriving
1004 * here, so we don't need to rotate.
1008 if (d
->bd_slen
== 0) {
1017 * At this point, we know we have something in the hold slot.
1019 d
->bd_hbuf_in_use
= 1;
1023 * Move data from hold buffer into user space.
1024 * We know the entire buffer is transferred since
1025 * we checked above that the read buffer is bpf_bufsize bytes.
1027 * We do not have to worry about simultaneous reads because
1028 * we waited for sole access to the hold buffer above.
1030 error
= bpf_uiomove(d
, d
->bd_hbuf
, d
->bd_hlen
, uio
);
1033 KASSERT(d
->bd_hbuf
!= NULL
, ("bpfread: lost bd_hbuf"));
1034 d
->bd_fbuf
= d
->bd_hbuf
;
1037 bpf_buf_reclaimed(d
);
1038 d
->bd_hbuf_in_use
= 0;
1039 wakeup(&d
->bd_hbuf_in_use
);
1046 * If there are processes sleeping on this descriptor, wake them up.
1048 static __inline
void
1049 bpf_wakeup(struct bpf_d
*d
)
1052 BPFD_LOCK_ASSERT(d
);
1053 if (d
->bd_state
== BPF_WAITING
) {
1054 callout_stop(&d
->bd_callout
);
1055 d
->bd_state
= BPF_IDLE
;
1058 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
1059 pgsigio(&d
->bd_sigio
, d
->bd_sig
, 0);
1061 selwakeuppri(&d
->bd_sel
, PRINET
);
1062 KNOTE_LOCKED(&d
->bd_sel
.si_note
, 0);
1066 bpf_timed_out(void *arg
)
1068 struct bpf_d
*d
= (struct bpf_d
*)arg
;
1070 BPFD_LOCK_ASSERT(d
);
1072 if (callout_pending(&d
->bd_callout
) || !callout_active(&d
->bd_callout
))
1074 if (d
->bd_state
== BPF_WAITING
) {
1075 d
->bd_state
= BPF_TIMED_OUT
;
1076 if (d
->bd_slen
!= 0)
1082 bpf_ready(struct bpf_d
*d
)
1085 BPFD_LOCK_ASSERT(d
);
1087 if (!bpf_canfreebuf(d
) && d
->bd_hlen
!= 0)
1089 if ((d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) &&
1096 bpfwrite(struct cdev
*dev
, struct uio
*uio
, int ioflag
)
1100 struct mbuf
*m
, *mc
;
1101 struct sockaddr dst
;
1105 error
= devfs_get_cdevpriv((void **)&d
);
1109 BPF_PID_REFRESH_CUR(d
);
1111 /* XXX: locking required */
1112 if (d
->bd_bif
== NULL
) {
1117 ifp
= d
->bd_bif
->bif_ifp
;
1119 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1124 if (uio
->uio_resid
== 0) {
1129 bzero(&dst
, sizeof(dst
));
1132 /* XXX: bpf_movein() can sleep */
1133 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, ifp
,
1134 &m
, &dst
, &hlen
, d
);
1141 dst
.sa_family
= pseudo_AF_HDRCMPLT
;
1143 if (d
->bd_feedback
) {
1144 mc
= m_dup(m
, M_NOWAIT
);
1146 mc
->m_pkthdr
.rcvif
= ifp
;
1147 /* Set M_PROMISC for outgoing packets to be discarded. */
1148 if (d
->bd_direction
== BPF_D_INOUT
)
1149 m
->m_flags
|= M_PROMISC
;
1153 m
->m_pkthdr
.len
-= hlen
;
1155 m
->m_data
+= hlen
; /* XXX */
1157 CURVNET_SET(ifp
->if_vnet
);
1160 mac_bpfdesc_create_mbuf(d
, m
);
1162 mac_bpfdesc_create_mbuf(d
, mc
);
1166 bzero(&ro
, sizeof(ro
));
1168 ro
.ro_prepend
= (u_char
*)&dst
.sa_data
;
1170 ro
.ro_flags
= RT_HAS_HEADER
;
1173 error
= (*ifp
->if_output
)(ifp
, m
, &dst
, &ro
);
1179 (*ifp
->if_input
)(ifp
, mc
);
1189 * Reset a descriptor by flushing its packet buffer and clearing the receive
1190 * and drop counts. This is doable for kernel-only buffers, but with
1191 * zero-copy buffers, we can't write to (or rotate) buffers that are
1192 * currently owned by userspace. It would be nice if we could encapsulate
1193 * this logic in the buffer code rather than here.
1196 reset_d(struct bpf_d
*d
)
1199 BPFD_LOCK_ASSERT(d
);
1201 while (d
->bd_hbuf_in_use
)
1202 mtx_sleep(&d
->bd_hbuf_in_use
, &d
->bd_lock
, PRINET
,
1204 if ((d
->bd_hbuf
!= NULL
) &&
1205 (d
->bd_bufmode
!= BPF_BUFMODE_ZBUF
|| bpf_canfreebuf(d
))) {
1206 /* Free the hold buffer. */
1207 d
->bd_fbuf
= d
->bd_hbuf
;
1210 bpf_buf_reclaimed(d
);
1212 if (bpf_canwritebuf(d
))
1224 * FIONREAD Check for read packet available.
1225 * BIOCGBLEN Get buffer len [for read()].
1226 * BIOCSETF Set read filter.
1227 * BIOCSETFNR Set read filter without resetting descriptor.
1228 * BIOCSETWF Set write filter.
1229 * BIOCFLUSH Flush read packet buffer.
1230 * BIOCPROMISC Put interface into promiscuous mode.
1231 * BIOCGDLT Get link layer type.
1232 * BIOCGETIF Get interface name.
1233 * BIOCSETIF Set interface.
1234 * BIOCSRTIMEOUT Set read timeout.
1235 * BIOCGRTIMEOUT Get read timeout.
1236 * BIOCGSTATS Get packet stats.
1237 * BIOCIMMEDIATE Set immediate mode.
1238 * BIOCVERSION Get filter language version.
1239 * BIOCGHDRCMPLT Get "header already complete" flag
1240 * BIOCSHDRCMPLT Set "header already complete" flag
1241 * BIOCGDIRECTION Get packet direction flag
1242 * BIOCSDIRECTION Set packet direction flag
1243 * BIOCGTSTAMP Get time stamp format and resolution.
1244 * BIOCSTSTAMP Set time stamp format and resolution.
1245 * BIOCLOCK Set "locked" flag
1246 * BIOCFEEDBACK Set packet feedback mode.
1247 * BIOCSETZBUF Set current zero-copy buffer locations.
1248 * BIOCGETZMAX Get maximum zero-copy buffer size.
1249 * BIOCROTZBUF Force rotation of zero-copy buffer
1250 * BIOCSETBUFMODE Set buffer mode.
1251 * BIOCGETBUFMODE Get current buffer mode.
1255 bpfioctl(struct cdev
*dev
, u_long cmd
, caddr_t addr
, int flags
,
1261 error
= devfs_get_cdevpriv((void **)&d
);
1266 * Refresh PID associated with this descriptor.
1269 BPF_PID_REFRESH(d
, td
);
1270 if (d
->bd_state
== BPF_WAITING
)
1271 callout_stop(&d
->bd_callout
);
1272 d
->bd_state
= BPF_IDLE
;
1275 if (d
->bd_locked
== 1) {
1281 #ifdef COMPAT_FREEBSD32
1282 case BIOCGDLTLIST32
:
1286 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1287 case BIOCGRTIMEOUT32
:
1298 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1299 case BIOCSRTIMEOUT32
:
1309 #ifdef COMPAT_FREEBSD32
1311 * If we see a 32-bit compat ioctl, mark the stream as 32-bit so
1312 * that it will get 32-bit packet headers.
1318 case BIOCGDLTLIST32
:
1319 case BIOCGRTIMEOUT32
:
1320 case BIOCSRTIMEOUT32
:
1327 CURVNET_SET(TD_TO_VNET(td
));
1335 * Check for read packet available.
1343 while (d
->bd_hbuf_in_use
)
1344 mtx_sleep(&d
->bd_hbuf_in_use
, &d
->bd_lock
,
1345 PRINET
, "bd_hbuf", 0);
1355 * Get buffer len [for read()].
1359 *(u_int
*)addr
= d
->bd_bufsize
;
1364 * Set buffer length.
1367 error
= bpf_ioctl_sblen(d
, (u_int
*)addr
);
1371 * Set link layer read filter.
1376 #ifdef COMPAT_FREEBSD32
1381 error
= bpf_setf(d
, (struct bpf_program
*)addr
, cmd
);
1385 * Flush read packet buffer.
1394 * Put interface into promiscuous mode.
1397 if (d
->bd_bif
== NULL
) {
1399 * No interface attached yet.
1404 if (d
->bd_promisc
== 0) {
1405 error
= ifpromisc(d
->bd_bif
->bif_ifp
, 1);
1412 * Get current data link type.
1416 if (d
->bd_bif
== NULL
)
1419 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
1424 * Get a list of supported data link types.
1426 #ifdef COMPAT_FREEBSD32
1427 case BIOCGDLTLIST32
:
1429 struct bpf_dltlist32
*list32
;
1430 struct bpf_dltlist dltlist
;
1432 list32
= (struct bpf_dltlist32
*)addr
;
1433 dltlist
.bfl_len
= list32
->bfl_len
;
1434 dltlist
.bfl_list
= PTRIN(list32
->bfl_list
);
1436 if (d
->bd_bif
== NULL
)
1439 error
= bpf_getdltlist(d
, &dltlist
);
1441 list32
->bfl_len
= dltlist
.bfl_len
;
1450 if (d
->bd_bif
== NULL
)
1453 error
= bpf_getdltlist(d
, (struct bpf_dltlist
*)addr
);
1458 * Set data link type.
1462 if (d
->bd_bif
== NULL
)
1465 error
= bpf_setdlt(d
, *(u_int
*)addr
);
1470 * Get interface name.
1474 if (d
->bd_bif
== NULL
)
1477 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1478 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1480 strlcpy(ifr
->ifr_name
, ifp
->if_xname
,
1481 sizeof(ifr
->ifr_name
));
1491 int alloc_buf
, size
;
1494 * Behavior here depends on the buffering model. If
1495 * we're using kernel memory buffers, then we can
1496 * allocate them here. If we're using zero-copy,
1497 * then the user process must have registered buffers
1498 * by the time we get here.
1502 if (d
->bd_bufmode
== BPF_BUFMODE_BUFFER
&&
1507 size
= d
->bd_bufsize
;
1508 error
= bpf_buffer_ioctl_sblen(d
, &size
);
1513 error
= bpf_setif(d
, (struct ifreq
*)addr
);
1522 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1523 case BIOCSRTIMEOUT32
:
1526 struct timeval
*tv
= (struct timeval
*)addr
;
1527 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1528 struct timeval32
*tv32
;
1529 struct timeval tv64
;
1531 if (cmd
== BIOCSRTIMEOUT32
) {
1532 tv32
= (struct timeval32
*)addr
;
1534 tv
->tv_sec
= tv32
->tv_sec
;
1535 tv
->tv_usec
= tv32
->tv_usec
;
1538 tv
= (struct timeval
*)addr
;
1541 * Subtract 1 tick from tvtohz() since this isn't
1544 if ((error
= itimerfix(tv
)) == 0)
1545 d
->bd_rtout
= tvtohz(tv
) - 1;
1553 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1554 case BIOCGRTIMEOUT32
:
1558 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1559 struct timeval32
*tv32
;
1560 struct timeval tv64
;
1562 if (cmd
== BIOCGRTIMEOUT32
)
1566 tv
= (struct timeval
*)addr
;
1568 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1569 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1570 #if defined(COMPAT_FREEBSD32) && !defined(__mips__)
1571 if (cmd
== BIOCGRTIMEOUT32
) {
1572 tv32
= (struct timeval32
*)addr
;
1573 tv32
->tv_sec
= tv
->tv_sec
;
1574 tv32
->tv_usec
= tv
->tv_usec
;
1586 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1588 /* XXXCSJP overflow */
1589 bs
->bs_recv
= d
->bd_rcount
;
1590 bs
->bs_drop
= d
->bd_dcount
;
1595 * Set immediate mode.
1599 d
->bd_immediate
= *(u_int
*)addr
;
1605 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1607 bv
->bv_major
= BPF_MAJOR_VERSION
;
1608 bv
->bv_minor
= BPF_MINOR_VERSION
;
1613 * Get "header already complete" flag
1617 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1622 * Set "header already complete" flag
1626 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1631 * Get packet direction flag
1633 case BIOCGDIRECTION
:
1635 *(u_int
*)addr
= d
->bd_direction
;
1640 * Set packet direction flag
1642 case BIOCSDIRECTION
:
1646 direction
= *(u_int
*)addr
;
1647 switch (direction
) {
1652 d
->bd_direction
= direction
;
1662 * Get packet timestamp format and resolution.
1666 *(u_int
*)addr
= d
->bd_tstamp
;
1671 * Set packet timestamp format and resolution.
1677 func
= *(u_int
*)addr
;
1678 if (BPF_T_VALID(func
))
1679 d
->bd_tstamp
= func
;
1687 d
->bd_feedback
= *(u_int
*)addr
;
1697 case FIONBIO
: /* Non-blocking I/O */
1700 case FIOASYNC
: /* Send signal on receive packets */
1702 d
->bd_async
= *(int *)addr
;
1708 * XXX: Add some sort of locking here?
1709 * fsetown() can sleep.
1711 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1716 *(int *)addr
= fgetown(&d
->bd_sigio
);
1720 /* This is deprecated, FIOSETOWN should be used instead. */
1722 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1725 /* This is deprecated, FIOGETOWN should be used instead. */
1727 *(int *)addr
= -fgetown(&d
->bd_sigio
);
1730 case BIOCSRSIG
: /* Set receive signal */
1734 sig
= *(u_int
*)addr
;
1747 *(u_int
*)addr
= d
->bd_sig
;
1751 case BIOCGETBUFMODE
:
1753 *(u_int
*)addr
= d
->bd_bufmode
;
1757 case BIOCSETBUFMODE
:
1759 * Allow the buffering mode to be changed as long as we
1760 * haven't yet committed to a particular mode. Our
1761 * definition of commitment, for now, is whether or not a
1762 * buffer has been allocated or an interface attached, since
1763 * that's the point where things get tricky.
1765 switch (*(u_int
*)addr
) {
1766 case BPF_BUFMODE_BUFFER
:
1769 case BPF_BUFMODE_ZBUF
:
1770 if (bpf_zerocopy_enable
)
1780 if (d
->bd_sbuf
!= NULL
|| d
->bd_hbuf
!= NULL
||
1781 d
->bd_fbuf
!= NULL
|| d
->bd_bif
!= NULL
) {
1786 d
->bd_bufmode
= *(u_int
*)addr
;
1791 error
= bpf_ioctl_getzmax(td
, d
, (size_t *)addr
);
1795 error
= bpf_ioctl_setzbuf(td
, d
, (struct bpf_zbuf
*)addr
);
1799 error
= bpf_ioctl_rotzbuf(td
, d
, (struct bpf_zbuf
*)addr
);
1807 * Set d's packet filter program to fp. If this file already has a filter,
1808 * free it and replace it. Returns EINVAL for bogus requests.
1810 * Note we need global lock here to serialize bpf_setf() and bpf_setif() calls
1811 * since reading d->bd_bif can't be protected by d or interface lock due to
1814 * Additionally, we have to acquire interface write lock due to bpf_mtap() uses
1815 * interface read lock to read all filers.
1819 bpf_setf(struct bpf_d
*d
, struct bpf_program
*fp
, u_long cmd
)
1821 #ifdef COMPAT_FREEBSD32
1822 struct bpf_program fp_swab
;
1823 struct bpf_program32
*fp32
;
1825 struct bpf_insn
*fcode
, *old
;
1827 bpf_jit_filter
*jfunc
, *ofunc
;
1833 #ifdef COMPAT_FREEBSD32
1838 fp32
= (struct bpf_program32
*)fp
;
1839 fp_swab
.bf_len
= fp32
->bf_len
;
1840 fp_swab
.bf_insns
= (struct bpf_insn
*)(uintptr_t)fp32
->bf_insns
;
1856 jfunc
= ofunc
= NULL
;
1861 * Check new filter validness before acquiring any locks.
1862 * Allocate memory for new filter, if needed.
1865 if (flen
> bpf_maxinsns
|| (fp
->bf_insns
== NULL
&& flen
!= 0))
1867 size
= flen
* sizeof(*fp
->bf_insns
);
1869 /* We're setting up new filter. Copy and check actual data. */
1870 fcode
= malloc(size
, M_BPF
, M_WAITOK
);
1871 if (copyin(fp
->bf_insns
, fcode
, size
) != 0 ||
1872 !bpf_validate(fcode
, flen
)) {
1877 /* Filter is copied inside fcode and is perfectly valid. */
1878 jfunc
= bpf_jitter(fcode
, flen
);
1885 * Set up new filter.
1886 * Protect filter change by interface lock.
1887 * Additionally, we are protected by global lock here.
1889 if (d
->bd_bif
!= NULL
)
1890 BPFIF_WLOCK(d
->bd_bif
);
1892 if (cmd
== BIOCSETWF
) {
1893 old
= d
->bd_wfilter
;
1894 d
->bd_wfilter
= fcode
;
1896 old
= d
->bd_rfilter
;
1897 d
->bd_rfilter
= fcode
;
1899 ofunc
= d
->bd_bfilter
;
1900 d
->bd_bfilter
= jfunc
;
1902 if (cmd
== BIOCSETF
)
1905 need_upgrade
= bpf_check_upgrade(cmd
, d
, fcode
, flen
);
1908 if (d
->bd_bif
!= NULL
)
1909 BPFIF_WUNLOCK(d
->bd_bif
);
1914 bpf_destroy_jit_filter(ofunc
);
1917 /* Move d to active readers list. */
1918 if (need_upgrade
!= 0)
1926 * Detach a file from its current interface (if attached at all) and attach
1927 * to the interface indicated by the name stored in ifr.
1928 * Return an errno or 0.
1931 bpf_setif(struct bpf_d
*d
, struct ifreq
*ifr
)
1934 struct ifnet
*theywant
;
1938 theywant
= ifunit(ifr
->ifr_name
);
1939 if (theywant
== NULL
|| theywant
->if_bpf
== NULL
)
1942 bp
= theywant
->if_bpf
;
1944 /* Check if interface is not being detached from BPF */
1946 if (bp
->bif_flags
& BPFIF_FLAG_DYING
) {
1953 * At this point, we expect the buffer is already allocated. If not,
1956 switch (d
->bd_bufmode
) {
1957 case BPF_BUFMODE_BUFFER
:
1958 case BPF_BUFMODE_ZBUF
:
1959 if (d
->bd_sbuf
== NULL
)
1964 panic("bpf_setif: bufmode %d", d
->bd_bufmode
);
1966 if (bp
!= d
->bd_bif
)
1975 * Support for select() and poll() system calls
1977 * Return true iff the specific operation will not block indefinitely.
1978 * Otherwise, return false but make a note that a selwakeup() must be done.
1981 bpfpoll(struct cdev
*dev
, int events
, struct thread
*td
)
1986 if (devfs_get_cdevpriv((void **)&d
) != 0 || d
->bd_bif
== NULL
)
1988 (POLLHUP
|POLLIN
|POLLRDNORM
|POLLOUT
|POLLWRNORM
));
1991 * Refresh PID associated with this descriptor.
1993 revents
= events
& (POLLOUT
| POLLWRNORM
);
1995 BPF_PID_REFRESH(d
, td
);
1996 if (events
& (POLLIN
| POLLRDNORM
)) {
1998 revents
|= events
& (POLLIN
| POLLRDNORM
);
2000 selrecord(td
, &d
->bd_sel
);
2001 /* Start the read timeout if necessary. */
2002 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
2003 callout_reset(&d
->bd_callout
, d
->bd_rtout
,
2005 d
->bd_state
= BPF_WAITING
;
2014 * Support for kevent() system call. Register EVFILT_READ filters and
2015 * reject all others.
2018 bpfkqfilter(struct cdev
*dev
, struct knote
*kn
)
2022 if (devfs_get_cdevpriv((void **)&d
) != 0 ||
2023 kn
->kn_filter
!= EVFILT_READ
)
2027 * Refresh PID associated with this descriptor.
2030 BPF_PID_REFRESH_CUR(d
);
2031 kn
->kn_fop
= &bpfread_filtops
;
2033 knlist_add(&d
->bd_sel
.si_note
, kn
, 1);
2040 filt_bpfdetach(struct knote
*kn
)
2042 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2044 knlist_remove(&d
->bd_sel
.si_note
, kn
, 0);
2048 filt_bpfread(struct knote
*kn
, long hint
)
2050 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2053 BPFD_LOCK_ASSERT(d
);
2054 ready
= bpf_ready(d
);
2056 kn
->kn_data
= d
->bd_slen
;
2058 * Ignore the hold buffer if it is being copied to user space.
2060 if (!d
->bd_hbuf_in_use
&& d
->bd_hbuf
)
2061 kn
->kn_data
+= d
->bd_hlen
;
2062 } else if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
2063 callout_reset(&d
->bd_callout
, d
->bd_rtout
,
2065 d
->bd_state
= BPF_WAITING
;
2071 #define BPF_TSTAMP_NONE 0
2072 #define BPF_TSTAMP_FAST 1
2073 #define BPF_TSTAMP_NORMAL 2
2074 #define BPF_TSTAMP_EXTERN 3
2077 bpf_ts_quality(int tstype
)
2080 if (tstype
== BPF_T_NONE
)
2081 return (BPF_TSTAMP_NONE
);
2082 if ((tstype
& BPF_T_FAST
) != 0)
2083 return (BPF_TSTAMP_FAST
);
2085 return (BPF_TSTAMP_NORMAL
);
2089 bpf_gettime(struct bintime
*bt
, int tstype
, struct mbuf
*m
)
2094 quality
= bpf_ts_quality(tstype
);
2095 if (quality
== BPF_TSTAMP_NONE
)
2099 tag
= m_tag_locate(m
, MTAG_BPF
, MTAG_BPF_TIMESTAMP
, NULL
);
2101 *bt
= *(struct bintime
*)(tag
+ 1);
2102 return (BPF_TSTAMP_EXTERN
);
2105 if (quality
== BPF_TSTAMP_NORMAL
)
2114 * Incoming linkage from device drivers. Process the packet pkt, of length
2115 * pktlen, which is stored in a contiguous buffer. The packet is parsed
2116 * by each process' filter, and if accepted, stashed into the corresponding
2120 bpf_tap(struct bpf_if
*bp
, u_char
*pkt
, u_int pktlen
)
2130 gottime
= BPF_TSTAMP_NONE
;
2134 LIST_FOREACH(d
, &bp
->bif_dlist
, bd_next
) {
2136 * We are not using any locks for d here because:
2137 * 1) any filter change is protected by interface
2139 * 2) destroying/detaching d is protected by interface
2143 /* XXX: Do not protect counter for the sake of performance. */
2146 * NB: We dont call BPF_CHECK_DIRECTION() here since there is no
2147 * way for the caller to indiciate to us whether this packet
2148 * is inbound or outbound. In the bpf_mtap() routines, we use
2149 * the interface pointers on the mbuf to figure it out.
2152 bf
= bpf_jitter_enable
!= 0 ? d
->bd_bfilter
: NULL
;
2154 slen
= (*(bf
->func
))(pkt
, pktlen
, pktlen
);
2157 slen
= bpf_filter(d
->bd_rfilter
, pkt
, pktlen
, pktlen
);
2160 * Filter matches. Let's to acquire write lock.
2165 if (gottime
< bpf_ts_quality(d
->bd_tstamp
))
2166 gottime
= bpf_gettime(&bt
, d
->bd_tstamp
, NULL
);
2168 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) == 0)
2170 catchpacket(d
, pkt
, pktlen
, slen
,
2171 bpf_append_bytes
, &bt
);
2178 #define BPF_CHECK_DIRECTION(d, r, i) \
2179 (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \
2180 ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
2183 * Incoming linkage from device drivers, when packet is in an mbuf chain.
2184 * Locking model is explained in bpf_tap().
2187 bpf_mtap(struct bpf_if
*bp
, struct mbuf
*m
)
2197 /* Skip outgoing duplicate packets. */
2198 if ((m
->m_flags
& M_PROMISC
) != 0 && m
->m_pkthdr
.rcvif
== NULL
) {
2199 m
->m_flags
&= ~M_PROMISC
;
2203 pktlen
= m_length(m
, NULL
);
2204 gottime
= BPF_TSTAMP_NONE
;
2208 LIST_FOREACH(d
, &bp
->bif_dlist
, bd_next
) {
2209 if (BPF_CHECK_DIRECTION(d
, m
->m_pkthdr
.rcvif
, bp
->bif_ifp
))
2213 bf
= bpf_jitter_enable
!= 0 ? d
->bd_bfilter
: NULL
;
2214 /* XXX We cannot handle multiple mbufs. */
2215 if (bf
!= NULL
&& m
->m_next
== NULL
)
2216 slen
= (*(bf
->func
))(mtod(m
, u_char
*), pktlen
, pktlen
);
2219 slen
= bpf_filter(d
->bd_rfilter
, (u_char
*)m
, pktlen
, 0);
2224 if (gottime
< bpf_ts_quality(d
->bd_tstamp
))
2225 gottime
= bpf_gettime(&bt
, d
->bd_tstamp
, m
);
2227 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) == 0)
2229 catchpacket(d
, (u_char
*)m
, pktlen
, slen
,
2230 bpf_append_mbuf
, &bt
);
2238 * Incoming linkage from device drivers, when packet is in
2239 * an mbuf chain and to be prepended by a contiguous header.
2242 bpf_mtap2(struct bpf_if
*bp
, void *data
, u_int dlen
, struct mbuf
*m
)
2250 /* Skip outgoing duplicate packets. */
2251 if ((m
->m_flags
& M_PROMISC
) != 0 && m
->m_pkthdr
.rcvif
== NULL
) {
2252 m
->m_flags
&= ~M_PROMISC
;
2256 pktlen
= m_length(m
, NULL
);
2258 * Craft on-stack mbuf suitable for passing to bpf_filter.
2259 * Note that we cut corners here; we only setup what's
2260 * absolutely needed--this mbuf should never go anywhere else.
2267 gottime
= BPF_TSTAMP_NONE
;
2271 LIST_FOREACH(d
, &bp
->bif_dlist
, bd_next
) {
2272 if (BPF_CHECK_DIRECTION(d
, m
->m_pkthdr
.rcvif
, bp
->bif_ifp
))
2275 slen
= bpf_filter(d
->bd_rfilter
, (u_char
*)&mb
, pktlen
, 0);
2280 if (gottime
< bpf_ts_quality(d
->bd_tstamp
))
2281 gottime
= bpf_gettime(&bt
, d
->bd_tstamp
, m
);
2283 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) == 0)
2285 catchpacket(d
, (u_char
*)&mb
, pktlen
, slen
,
2286 bpf_append_mbuf
, &bt
);
2293 #undef BPF_CHECK_DIRECTION
2295 #undef BPF_TSTAMP_NONE
2296 #undef BPF_TSTAMP_FAST
2297 #undef BPF_TSTAMP_NORMAL
2298 #undef BPF_TSTAMP_EXTERN
2301 bpf_hdrlen(struct bpf_d
*d
)
2305 hdrlen
= d
->bd_bif
->bif_hdrlen
;
2306 #ifndef BURN_BRIDGES
2307 if (d
->bd_tstamp
== BPF_T_NONE
||
2308 BPF_T_FORMAT(d
->bd_tstamp
) == BPF_T_MICROTIME
)
2309 #ifdef COMPAT_FREEBSD32
2311 hdrlen
+= SIZEOF_BPF_HDR(struct bpf_hdr32
);
2314 hdrlen
+= SIZEOF_BPF_HDR(struct bpf_hdr
);
2317 hdrlen
+= SIZEOF_BPF_HDR(struct bpf_xhdr
);
2318 #ifdef COMPAT_FREEBSD32
2320 hdrlen
= BPF_WORDALIGN32(hdrlen
);
2323 hdrlen
= BPF_WORDALIGN(hdrlen
);
2325 return (hdrlen
- d
->bd_bif
->bif_hdrlen
);
2329 bpf_bintime2ts(struct bintime
*bt
, struct bpf_ts
*ts
, int tstype
)
2333 struct timespec tsn
;
2335 if ((tstype
& BPF_T_MONOTONIC
) == 0) {
2337 bintime_add(&bt2
, &boottimebin
);
2340 switch (BPF_T_FORMAT(tstype
)) {
2341 case BPF_T_MICROTIME
:
2342 bintime2timeval(bt
, &tsm
);
2343 ts
->bt_sec
= tsm
.tv_sec
;
2344 ts
->bt_frac
= tsm
.tv_usec
;
2346 case BPF_T_NANOTIME
:
2347 bintime2timespec(bt
, &tsn
);
2348 ts
->bt_sec
= tsn
.tv_sec
;
2349 ts
->bt_frac
= tsn
.tv_nsec
;
2352 ts
->bt_sec
= bt
->sec
;
2353 ts
->bt_frac
= bt
->frac
;
2359 * Move the packet data from interface memory (pkt) into the
2360 * store buffer. "cpfn" is the routine called to do the actual data
2361 * transfer. bcopy is passed in to copy contiguous chunks, while
2362 * bpf_append_mbuf is passed in to copy mbuf chains. In the latter case,
2363 * pkt is really an mbuf.
2366 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
2367 void (*cpfn
)(struct bpf_d
*, caddr_t
, u_int
, void *, u_int
),
2370 struct bpf_xhdr hdr
;
2371 #ifndef BURN_BRIDGES
2372 struct bpf_hdr hdr_old
;
2373 #ifdef COMPAT_FREEBSD32
2374 struct bpf_hdr32 hdr32_old
;
2377 int caplen
, curlen
, hdrlen
, totlen
;
2382 BPFD_LOCK_ASSERT(d
);
2385 * Detect whether user space has released a buffer back to us, and if
2386 * so, move it from being a hold buffer to a free buffer. This may
2387 * not be the best place to do it (for example, we might only want to
2388 * run this check if we need the space), but for now it's a reliable
2391 if (d
->bd_fbuf
== NULL
&& bpf_canfreebuf(d
)) {
2392 d
->bd_fbuf
= d
->bd_hbuf
;
2395 bpf_buf_reclaimed(d
);
2399 * Figure out how many bytes to move. If the packet is
2400 * greater or equal to the snapshot length, transfer that
2401 * much. Otherwise, transfer the whole packet (unless
2402 * we hit the buffer size limit).
2404 hdrlen
= bpf_hdrlen(d
);
2405 totlen
= hdrlen
+ min(snaplen
, pktlen
);
2406 if (totlen
> d
->bd_bufsize
)
2407 totlen
= d
->bd_bufsize
;
2410 * Round up the end of the previous packet to the next longword.
2412 * Drop the packet if there's no room and no hope of room
2413 * If the packet would overflow the storage buffer or the storage
2414 * buffer is considered immutable by the buffer model, try to rotate
2415 * the buffer and wakeup pending processes.
2417 #ifdef COMPAT_FREEBSD32
2419 curlen
= BPF_WORDALIGN32(d
->bd_slen
);
2422 curlen
= BPF_WORDALIGN(d
->bd_slen
);
2423 if (curlen
+ totlen
> d
->bd_bufsize
|| !bpf_canwritebuf(d
)) {
2424 if (d
->bd_fbuf
== NULL
) {
2426 * There's no room in the store buffer, and no
2427 * prospect of room, so drop the packet. Notify the
2434 KASSERT(!d
->bd_hbuf_in_use
, ("hold buffer is in use"));
2438 } else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
)
2440 * Immediate mode is set, or the read timeout has already
2441 * expired during a select call. A packet arrived, so the
2442 * reader should be woken up.
2445 caplen
= totlen
- hdrlen
;
2446 tstype
= d
->bd_tstamp
;
2447 do_timestamp
= tstype
!= BPF_T_NONE
;
2448 #ifndef BURN_BRIDGES
2449 if (tstype
== BPF_T_NONE
|| BPF_T_FORMAT(tstype
) == BPF_T_MICROTIME
) {
2452 bpf_bintime2ts(bt
, &ts
, tstype
);
2453 #ifdef COMPAT_FREEBSD32
2454 if (d
->bd_compat32
) {
2455 bzero(&hdr32_old
, sizeof(hdr32_old
));
2457 hdr32_old
.bh_tstamp
.tv_sec
= ts
.bt_sec
;
2458 hdr32_old
.bh_tstamp
.tv_usec
= ts
.bt_frac
;
2460 hdr32_old
.bh_datalen
= pktlen
;
2461 hdr32_old
.bh_hdrlen
= hdrlen
;
2462 hdr32_old
.bh_caplen
= caplen
;
2463 bpf_append_bytes(d
, d
->bd_sbuf
, curlen
, &hdr32_old
,
2468 bzero(&hdr_old
, sizeof(hdr_old
));
2470 hdr_old
.bh_tstamp
.tv_sec
= ts
.bt_sec
;
2471 hdr_old
.bh_tstamp
.tv_usec
= ts
.bt_frac
;
2473 hdr_old
.bh_datalen
= pktlen
;
2474 hdr_old
.bh_hdrlen
= hdrlen
;
2475 hdr_old
.bh_caplen
= caplen
;
2476 bpf_append_bytes(d
, d
->bd_sbuf
, curlen
, &hdr_old
,
2483 * Append the bpf header. Note we append the actual header size, but
2484 * move forward the length of the header plus padding.
2486 bzero(&hdr
, sizeof(hdr
));
2488 bpf_bintime2ts(bt
, &hdr
.bh_tstamp
, tstype
);
2489 hdr
.bh_datalen
= pktlen
;
2490 hdr
.bh_hdrlen
= hdrlen
;
2491 hdr
.bh_caplen
= caplen
;
2492 bpf_append_bytes(d
, d
->bd_sbuf
, curlen
, &hdr
, sizeof(hdr
));
2495 * Copy the packet data into the store buffer and update its length.
2497 #ifndef BURN_BRIDGES
2500 (*cpfn
)(d
, d
->bd_sbuf
, curlen
+ hdrlen
, pkt
, caplen
);
2501 d
->bd_slen
= curlen
+ totlen
;
2508 * Free buffers currently in use by a descriptor.
2512 bpf_freed(struct bpf_d
*d
)
2516 * We don't need to lock out interrupts since this descriptor has
2517 * been detached from its interface and it yet hasn't been marked
2521 if (d
->bd_rfilter
!= NULL
) {
2522 free((caddr_t
)d
->bd_rfilter
, M_BPF
);
2524 if (d
->bd_bfilter
!= NULL
)
2525 bpf_destroy_jit_filter(d
->bd_bfilter
);
2528 if (d
->bd_wfilter
!= NULL
)
2529 free((caddr_t
)d
->bd_wfilter
, M_BPF
);
2530 mtx_destroy(&d
->bd_lock
);
2534 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
2535 * fixed size of the link header (variable length headers not yet supported).
2538 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
2541 bpfattach2(ifp
, dlt
, hdrlen
, &ifp
->if_bpf
);
2545 * Attach an interface to bpf. ifp is a pointer to the structure
2546 * defining the interface to be attached, dlt is the link layer type,
2547 * and hdrlen is the fixed size of the link header (variable length
2548 * headers are not yet supporrted).
2551 bpfattach2(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
, struct bpf_if
**driverp
)
2555 bp
= malloc(sizeof(*bp
), M_BPF
, M_NOWAIT
| M_ZERO
);
2559 LIST_INIT(&bp
->bif_dlist
);
2560 LIST_INIT(&bp
->bif_wlist
);
2563 rw_init(&bp
->bif_lock
, "bpf interface lock");
2564 KASSERT(*driverp
== NULL
, ("bpfattach2: driverp already initialized"));
2568 LIST_INSERT_HEAD(&bpf_iflist
, bp
, bif_next
);
2571 bp
->bif_hdrlen
= hdrlen
;
2573 if (bootverbose
&& IS_DEFAULT_VNET(curvnet
))
2574 if_printf(ifp
, "bpf attached\n");
2579 * When moving interfaces between vnet instances we need a way to
2580 * query the dlt and hdrlen before detach so we can re-attch the if_bpf
2581 * after the vmove. We unfortunately have no device driver infrastructure
2582 * to query the interface for these values after creation/attach, thus
2583 * add this as a workaround.
2586 bpf_get_bp_params(struct bpf_if
*bp
, u_int
*bif_dlt
, u_int
*bif_hdrlen
)
2591 if (bif_dlt
== NULL
&& bif_hdrlen
== NULL
)
2594 if (bif_dlt
!= NULL
)
2595 *bif_dlt
= bp
->bif_dlt
;
2596 if (bif_hdrlen
!= NULL
)
2597 *bif_hdrlen
= bp
->bif_hdrlen
;
2604 * Detach bpf from an interface. This involves detaching each descriptor
2605 * associated with the interface. Notify each descriptor as it's detached
2606 * so that any sleepers wake up and get ENXIO.
2609 bpfdetach(struct ifnet
*ifp
)
2611 struct bpf_if
*bp
, *bp_temp
;
2618 /* Find all bpf_if struct's which reference ifp and detach them. */
2619 LIST_FOREACH_SAFE(bp
, &bpf_iflist
, bif_next
, bp_temp
) {
2620 if (ifp
!= bp
->bif_ifp
)
2623 LIST_REMOVE(bp
, bif_next
);
2624 /* Add to to-be-freed list */
2625 LIST_INSERT_HEAD(&bpf_freelist
, bp
, bif_next
);
2629 * Delay freeing bp till interface is detached
2630 * and all routes through this interface are removed.
2631 * Mark bp as detached to restrict new consumers.
2634 bp
->bif_flags
|= BPFIF_FLAG_DYING
;
2637 CTR4(KTR_NET
, "%s: sheduling free for encap %d (%p) for if %p",
2638 __func__
, bp
->bif_dlt
, bp
, ifp
);
2640 /* Free common descriptors */
2641 while ((d
= LIST_FIRST(&bp
->bif_dlist
)) != NULL
) {
2642 bpf_detachd_locked(d
);
2648 /* Free writer-only descriptors */
2649 while ((d
= LIST_FIRST(&bp
->bif_wlist
)) != NULL
) {
2650 bpf_detachd_locked(d
);
2660 printf("bpfdetach: %s was not attached\n", ifp
->if_xname
);
2665 * Interface departure handler.
2666 * Note departure event does not guarantee interface is going down.
2667 * Interface renaming is currently done via departure/arrival event set.
2669 * Departure handled is called after all routes pointing to
2670 * given interface are removed and interface is in down state
2671 * restricting any packets to be sent/received. We assume it is now safe
2672 * to free data allocated by BPF.
2675 bpf_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
2677 struct bpf_if
*bp
, *bp_temp
;
2682 * Find matching entries in free list.
2683 * Nothing should be found if bpfdetach() was not called.
2685 LIST_FOREACH_SAFE(bp
, &bpf_freelist
, bif_next
, bp_temp
) {
2686 if (ifp
!= bp
->bif_ifp
)
2689 CTR3(KTR_NET
, "%s: freeing BPF instance %p for interface %p",
2692 LIST_REMOVE(bp
, bif_next
);
2694 rw_destroy(&bp
->bif_lock
);
2702 * Note that we cannot zero other pointers to
2703 * custom DLTs possibly used by given interface.
2710 * Get a list of available data link type of the interface.
2713 bpf_getdltlist(struct bpf_d
*d
, struct bpf_dltlist
*bfl
)
2722 ifp
= d
->bd_bif
->bif_ifp
;
2725 LIST_FOREACH(bp
, &bpf_iflist
, bif_next
) {
2726 if (bp
->bif_ifp
== ifp
)
2729 if (bfl
->bfl_list
== NULL
) {
2733 if (n1
> bfl
->bfl_len
)
2736 lst
= malloc(n1
* sizeof(u_int
), M_TEMP
, M_WAITOK
);
2739 LIST_FOREACH(bp
, &bpf_iflist
, bif_next
) {
2740 if (bp
->bif_ifp
!= ifp
)
2746 lst
[n
] = bp
->bif_dlt
;
2750 error
= copyout(lst
, bfl
->bfl_list
, sizeof(u_int
) * n
);
2758 * Set the data link type of a BPF instance.
2761 bpf_setdlt(struct bpf_d
*d
, u_int dlt
)
2763 int error
, opromisc
;
2769 if (d
->bd_bif
->bif_dlt
== dlt
)
2771 ifp
= d
->bd_bif
->bif_ifp
;
2773 LIST_FOREACH(bp
, &bpf_iflist
, bif_next
) {
2774 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
)
2779 opromisc
= d
->bd_promisc
;
2785 error
= ifpromisc(bp
->bif_ifp
, 1);
2787 if_printf(bp
->bif_ifp
,
2788 "bpf_setdlt: ifpromisc failed (%d)\n",
2794 return (bp
== NULL
? EINVAL
: 0);
2798 bpf_drvinit(void *unused
)
2802 mtx_init(&bpf_mtx
, "bpf global lock", NULL
, MTX_DEF
);
2803 LIST_INIT(&bpf_iflist
);
2804 LIST_INIT(&bpf_freelist
);
2806 dev
= make_dev(&bpf_cdevsw
, 0, UID_ROOT
, GID_WHEEL
, 0600, "bpf");
2807 /* For compatibility */
2808 make_dev_alias(dev
, "bpf0");
2810 /* Register interface departure handler */
2811 bpf_ifdetach_cookie
= EVENTHANDLER_REGISTER(
2812 ifnet_departure_event
, bpf_ifdetach
, NULL
,
2813 EVENTHANDLER_PRI_ANY
);
2817 * Zero out the various packet counters associated with all of the bpf
2818 * descriptors. At some point, we will probably want to get a bit more
2819 * granular and allow the user to specify descriptors to be zeroed.
2822 bpf_zero_counters(void)
2828 LIST_FOREACH(bp
, &bpf_iflist
, bif_next
) {
2830 LIST_FOREACH(bd
, &bp
->bif_dlist
, bd_next
) {
2846 * Fill filter statistics
2849 bpfstats_fill_xbpf(struct xbpf_d
*d
, struct bpf_d
*bd
)
2852 bzero(d
, sizeof(*d
));
2853 BPFD_LOCK_ASSERT(bd
);
2854 d
->bd_structsize
= sizeof(*d
);
2855 /* XXX: reading should be protected by global lock */
2856 d
->bd_immediate
= bd
->bd_immediate
;
2857 d
->bd_promisc
= bd
->bd_promisc
;
2858 d
->bd_hdrcmplt
= bd
->bd_hdrcmplt
;
2859 d
->bd_direction
= bd
->bd_direction
;
2860 d
->bd_feedback
= bd
->bd_feedback
;
2861 d
->bd_async
= bd
->bd_async
;
2862 d
->bd_rcount
= bd
->bd_rcount
;
2863 d
->bd_dcount
= bd
->bd_dcount
;
2864 d
->bd_fcount
= bd
->bd_fcount
;
2865 d
->bd_sig
= bd
->bd_sig
;
2866 d
->bd_slen
= bd
->bd_slen
;
2867 d
->bd_hlen
= bd
->bd_hlen
;
2868 d
->bd_bufsize
= bd
->bd_bufsize
;
2869 d
->bd_pid
= bd
->bd_pid
;
2870 strlcpy(d
->bd_ifname
,
2871 bd
->bd_bif
->bif_ifp
->if_xname
, IFNAMSIZ
);
2872 d
->bd_locked
= bd
->bd_locked
;
2873 d
->bd_wcount
= bd
->bd_wcount
;
2874 d
->bd_wdcount
= bd
->bd_wdcount
;
2875 d
->bd_wfcount
= bd
->bd_wfcount
;
2876 d
->bd_zcopy
= bd
->bd_zcopy
;
2877 d
->bd_bufmode
= bd
->bd_bufmode
;
2881 * Handle `netstat -B' stats request
2884 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS
)
2886 static const struct xbpf_d zerostats
;
2887 struct xbpf_d
*xbdbuf
, *xbd
, tempstats
;
2893 * XXX This is not technically correct. It is possible for non
2894 * privileged users to open bpf devices. It would make sense
2895 * if the users who opened the devices were able to retrieve
2896 * the statistics for them, too.
2898 error
= priv_check(req
->td
, PRIV_NET_BPF
);
2902 * Check to see if the user is requesting that the counters be
2903 * zeroed out. Explicitly check that the supplied data is zeroed,
2904 * as we aren't allowing the user to set the counters currently.
2906 if (req
->newptr
!= NULL
) {
2907 if (req
->newlen
!= sizeof(tempstats
))
2909 memset(&tempstats
, 0, sizeof(tempstats
));
2910 error
= SYSCTL_IN(req
, &tempstats
, sizeof(tempstats
));
2913 if (bcmp(&tempstats
, &zerostats
, sizeof(tempstats
)) != 0)
2915 bpf_zero_counters();
2918 if (req
->oldptr
== NULL
)
2919 return (SYSCTL_OUT(req
, 0, bpf_bpfd_cnt
* sizeof(*xbd
)));
2920 if (bpf_bpfd_cnt
== 0)
2921 return (SYSCTL_OUT(req
, 0, 0));
2922 xbdbuf
= malloc(req
->oldlen
, M_BPF
, M_WAITOK
);
2924 if (req
->oldlen
< (bpf_bpfd_cnt
* sizeof(*xbd
))) {
2926 free(xbdbuf
, M_BPF
);
2930 LIST_FOREACH(bp
, &bpf_iflist
, bif_next
) {
2932 /* Send writers-only first */
2933 LIST_FOREACH(bd
, &bp
->bif_wlist
, bd_next
) {
2934 xbd
= &xbdbuf
[index
++];
2936 bpfstats_fill_xbpf(xbd
, bd
);
2939 LIST_FOREACH(bd
, &bp
->bif_dlist
, bd_next
) {
2940 xbd
= &xbdbuf
[index
++];
2942 bpfstats_fill_xbpf(xbd
, bd
);
2948 error
= SYSCTL_OUT(req
, xbdbuf
, index
* sizeof(*xbd
));
2949 free(xbdbuf
, M_BPF
);
2953 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
,bpf_drvinit
,NULL
);
2955 #else /* !DEV_BPF && !NETGRAPH_BPF */
2957 * NOP stubs to allow bpf-using drivers to load and function.
2959 * A 'better' implementation would allow the core bpf functionality
2960 * to be loaded at runtime.
2962 static struct bpf_if bp_null
;
2965 bpf_tap(struct bpf_if
*bp
, u_char
*pkt
, u_int pktlen
)
2970 bpf_mtap(struct bpf_if
*bp
, struct mbuf
*m
)
2975 bpf_mtap2(struct bpf_if
*bp
, void *d
, u_int l
, struct mbuf
*m
)
2980 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
2983 bpfattach2(ifp
, dlt
, hdrlen
, &ifp
->if_bpf
);
2987 bpfattach2(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
, struct bpf_if
**driverp
)
2990 *driverp
= &bp_null
;
2994 bpfdetach(struct ifnet
*ifp
)
2999 bpf_filter(const struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
3001 return -1; /* "no filter" behaviour */
3005 bpf_validate(const struct bpf_insn
*f
, int len
)
3007 return 0; /* false */
3010 #endif /* !DEV_BPF && !NETGRAPH_BPF */
3014 bpf_show_bpf_if(struct bpf_if
*bpf_if
)
3019 db_printf("%p:\n", bpf_if
);
3020 #define BPF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, bpf_if->e);
3021 /* bif_ext.bif_next */
3022 /* bif_ext.bif_dlist */
3023 BPF_DB_PRINTF("%#x", bif_dlt
);
3024 BPF_DB_PRINTF("%u", bif_hdrlen
);
3025 BPF_DB_PRINTF("%p", bif_ifp
);
3028 BPF_DB_PRINTF("%#x", bif_flags
);
3031 DB_SHOW_COMMAND(bpf_if
, db_show_bpf_if
)
3035 db_printf("usage: show bpf_if <struct bpf_if *>\n");
3039 bpf_show_bpf_if((struct bpf_if
*)addr
);