xref: /freebsd/sys/net/bpf.c (revision 136600fe596c2461359ae3ee5b9c1fc379aa71d0)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from the Stanford/CMU enet packet filter,
6df8bae1dSRodney W. Grimes  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7df8bae1dSRodney W. Grimes  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8df8bae1dSRodney W. Grimes  * Berkeley Laboratory.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
344f252c4dSRuslan Ermilov  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37c7866007SRobert Watson #include <sys/cdefs.h>
38c7866007SRobert Watson __FBSDID("$FreeBSD$");
39c7866007SRobert Watson 
405bb5f2c9SPeter Wemm #include "opt_bpf.h"
4182f4445dSRobert Watson #include "opt_mac.h"
425bb5f2c9SPeter Wemm #include "opt_netgraph.h"
43df8bae1dSRodney W. Grimes 
4495aab9ccSJohn-Mark Gurney #include <sys/types.h>
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47ce7609a4SBruce Evans #include <sys/conf.h>
48e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
494d1d4912SBruce Evans #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
540310c19fSBruce Evans #include <sys/signalvar.h>
55528f627fSBruce Evans #include <sys/filio.h>
56528f627fSBruce Evans #include <sys/sockio.h>
57528f627fSBruce Evans #include <sys/ttycom.h>
58e76eee55SPoul-Henning Kamp #include <sys/uio.h>
59df8bae1dSRodney W. Grimes 
6095aab9ccSJohn-Mark Gurney #include <sys/event.h>
6195aab9ccSJohn-Mark Gurney #include <sys/file.h>
62243ac7d8SPeter Wemm #include <sys/poll.h>
6395aab9ccSJohn-Mark Gurney #include <sys/proc.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <sys/socket.h>
66df8bae1dSRodney W. Grimes 
67fba9235dSBruce Evans #include <net/if.h>
68df8bae1dSRodney W. Grimes #include <net/bpf.h>
694d621040SChristian S.J. Peron #include <net/bpf_buffer.h>
70ae275efcSJung-uk Kim #ifdef BPF_JITTER
71ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
72ae275efcSJung-uk Kim #endif
734d621040SChristian S.J. Peron #include <net/bpf_zerocopy.h>
74df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <netinet/in.h>
77df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
78df8bae1dSRodney W. Grimes #include <sys/kernel.h>
79f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
807b778b5eSEivind Eklund 
81246b5467SSam Leffler #include <net80211/ieee80211_freebsd.h>
82246b5467SSam Leffler 
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84aed55708SRobert Watson 
854d621040SChristian S.J. Peron MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8687f6c662SJulian Elischer 
875bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8853ac6efbSJulian Elischer 
89df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes /*
92d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
93d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
94d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
95d1a67300SRobert Watson  * frames, ethernet frames, etc).
96df8bae1dSRodney W. Grimes  */
974a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
98e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
9969f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
100df8bae1dSRodney W. Grimes 
10119ba8395SChristian S.J. Peron static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
10219ba8395SChristian S.J. Peron static void	bpf_detachd(struct bpf_d *);
103929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
104cb44b6dfSAndrew Thompson static int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
105560a54e1SJung-uk Kim 		    struct sockaddr *, int *, struct bpf_insn *);
106929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
107929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
108e7bb21b3SJonathan Lemon static __inline void
109929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
1104d621040SChristian S.J. Peron static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
1114d621040SChristian S.J. Peron 		    void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
11291433904SDavid Malone 		    struct timeval *);
113929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
11493e39f0bSChristian S.J. Peron static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
1158eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1168eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11795aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
11895aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
119a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
12069f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
12169f7644bSChristian S.J. Peron 
12269f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12369f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
12469f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
12569f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
1264d621040SChristian S.J. Peron static int bpf_zerocopy_enable = 0;
1274d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
1284d621040SChristian S.J. Peron     &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
12969f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13069f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
131df8bae1dSRodney W. Grimes 
13287f6c662SJulian Elischer static	d_open_t	bpfopen;
13387f6c662SJulian Elischer static	d_read_t	bpfread;
13487f6c662SJulian Elischer static	d_write_t	bpfwrite;
13587f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
136243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
13795aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
13887f6c662SJulian Elischer 
1394e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
140dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1417ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1427ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1437ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1447ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1457ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1467ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
14795aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1484e2f199eSPoul-Henning Kamp };
14987f6c662SJulian Elischer 
15095aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
15195aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
15287f6c662SJulian Elischer 
1534d621040SChristian S.J. Peron /*
1544d621040SChristian S.J. Peron  * Wrapper functions for various buffering methods.  If the set of buffer
1554d621040SChristian S.J. Peron  * modes expands, we will probably want to introduce a switch data structure
1564d621040SChristian S.J. Peron  * similar to protosw, et.
1574d621040SChristian S.J. Peron  */
1584d621040SChristian S.J. Peron static void
1594d621040SChristian S.J. Peron bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1604d621040SChristian S.J. Peron     u_int len)
1614d621040SChristian S.J. Peron {
1624d621040SChristian S.J. Peron 
1634d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1644d621040SChristian S.J. Peron 
1654d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
1664d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1674d621040SChristian S.J. Peron 		return (bpf_buffer_append_bytes(d, buf, offset, src, len));
1684d621040SChristian S.J. Peron 
1694d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
1704d621040SChristian S.J. Peron 		d->bd_zcopy++;
1714d621040SChristian S.J. Peron 		return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
1724d621040SChristian S.J. Peron 
1734d621040SChristian S.J. Peron 	default:
1744d621040SChristian S.J. Peron 		panic("bpf_buf_append_bytes");
1754d621040SChristian S.J. Peron 	}
1764d621040SChristian S.J. Peron }
1774d621040SChristian S.J. Peron 
1784d621040SChristian S.J. Peron static void
1794d621040SChristian S.J. Peron bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1804d621040SChristian S.J. Peron     u_int len)
1814d621040SChristian S.J. Peron {
1824d621040SChristian S.J. Peron 
1834d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1844d621040SChristian S.J. Peron 
1854d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
1864d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1874d621040SChristian S.J. Peron 		return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
1884d621040SChristian S.J. Peron 
1894d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
1904d621040SChristian S.J. Peron 		d->bd_zcopy++;
1914d621040SChristian S.J. Peron 		return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
1924d621040SChristian S.J. Peron 
1934d621040SChristian S.J. Peron 	default:
1944d621040SChristian S.J. Peron 		panic("bpf_buf_append_mbuf");
1954d621040SChristian S.J. Peron 	}
1964d621040SChristian S.J. Peron }
1974d621040SChristian S.J. Peron 
1984d621040SChristian S.J. Peron /*
19929f612ecSChristian S.J. Peron  * This function gets called when the free buffer is re-assigned.
20029f612ecSChristian S.J. Peron  */
20129f612ecSChristian S.J. Peron static void
20229f612ecSChristian S.J. Peron bpf_buf_reclaimed(struct bpf_d *d)
20329f612ecSChristian S.J. Peron {
20429f612ecSChristian S.J. Peron 
20529f612ecSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
20629f612ecSChristian S.J. Peron 
20729f612ecSChristian S.J. Peron 	switch (d->bd_bufmode) {
20829f612ecSChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
20929f612ecSChristian S.J. Peron 		return;
21029f612ecSChristian S.J. Peron 
21129f612ecSChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
21229f612ecSChristian S.J. Peron 		bpf_zerocopy_buf_reclaimed(d);
21329f612ecSChristian S.J. Peron 		return;
21429f612ecSChristian S.J. Peron 
21529f612ecSChristian S.J. Peron 	default:
21629f612ecSChristian S.J. Peron 		panic("bpf_buf_reclaimed");
21729f612ecSChristian S.J. Peron 	}
21829f612ecSChristian S.J. Peron }
21929f612ecSChristian S.J. Peron 
22029f612ecSChristian S.J. Peron /*
2214d621040SChristian S.J. Peron  * If the buffer mechanism has a way to decide that a held buffer can be made
2224d621040SChristian S.J. Peron  * free, then it is exposed via the bpf_canfreebuf() interface.  (1) is
2234d621040SChristian S.J. Peron  * returned if the buffer can be discarded, (0) is returned if it cannot.
2244d621040SChristian S.J. Peron  */
2254d621040SChristian S.J. Peron static int
2264d621040SChristian S.J. Peron bpf_canfreebuf(struct bpf_d *d)
2274d621040SChristian S.J. Peron {
2284d621040SChristian S.J. Peron 
2294d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
2304d621040SChristian S.J. Peron 
2314d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2324d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2334d621040SChristian S.J. Peron 		return (bpf_zerocopy_canfreebuf(d));
2344d621040SChristian S.J. Peron 	}
2354d621040SChristian S.J. Peron 	return (0);
2364d621040SChristian S.J. Peron }
2374d621040SChristian S.J. Peron 
238a7a91e65SRobert Watson /*
239a7a91e65SRobert Watson  * Allow the buffer model to indicate that the current store buffer is
240a7a91e65SRobert Watson  * immutable, regardless of the appearance of space.  Return (1) if the
241a7a91e65SRobert Watson  * buffer is writable, and (0) if not.
242a7a91e65SRobert Watson  */
243a7a91e65SRobert Watson static int
244a7a91e65SRobert Watson bpf_canwritebuf(struct bpf_d *d)
245a7a91e65SRobert Watson {
246a7a91e65SRobert Watson 
247a7a91e65SRobert Watson 	BPFD_LOCK_ASSERT(d);
248a7a91e65SRobert Watson 
249a7a91e65SRobert Watson 	switch (d->bd_bufmode) {
250a7a91e65SRobert Watson 	case BPF_BUFMODE_ZBUF:
251a7a91e65SRobert Watson 		return (bpf_zerocopy_canwritebuf(d));
252a7a91e65SRobert Watson 	}
253a7a91e65SRobert Watson 	return (1);
254a7a91e65SRobert Watson }
255a7a91e65SRobert Watson 
256a7a91e65SRobert Watson /*
257a7a91e65SRobert Watson  * Notify buffer model that an attempt to write to the store buffer has
258a7a91e65SRobert Watson  * resulted in a dropped packet, in which case the buffer may be considered
259a7a91e65SRobert Watson  * full.
260a7a91e65SRobert Watson  */
261a7a91e65SRobert Watson static void
262a7a91e65SRobert Watson bpf_buffull(struct bpf_d *d)
263a7a91e65SRobert Watson {
264a7a91e65SRobert Watson 
265a7a91e65SRobert Watson 	BPFD_LOCK_ASSERT(d);
266a7a91e65SRobert Watson 
267a7a91e65SRobert Watson 	switch (d->bd_bufmode) {
268a7a91e65SRobert Watson 	case BPF_BUFMODE_ZBUF:
269a7a91e65SRobert Watson 		bpf_zerocopy_buffull(d);
270a7a91e65SRobert Watson 		break;
271a7a91e65SRobert Watson 	}
272a7a91e65SRobert Watson }
273a7a91e65SRobert Watson 
274a7a91e65SRobert Watson /*
275a7a91e65SRobert Watson  * Notify the buffer model that a buffer has moved into the hold position.
276a7a91e65SRobert Watson  */
2774d621040SChristian S.J. Peron void
2784d621040SChristian S.J. Peron bpf_bufheld(struct bpf_d *d)
2794d621040SChristian S.J. Peron {
2804d621040SChristian S.J. Peron 
2814d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
2824d621040SChristian S.J. Peron 
2834d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2844d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2854d621040SChristian S.J. Peron 		bpf_zerocopy_bufheld(d);
2864d621040SChristian S.J. Peron 		break;
2874d621040SChristian S.J. Peron 	}
2884d621040SChristian S.J. Peron }
2894d621040SChristian S.J. Peron 
2904d621040SChristian S.J. Peron static void
2914d621040SChristian S.J. Peron bpf_free(struct bpf_d *d)
2924d621040SChristian S.J. Peron {
2934d621040SChristian S.J. Peron 
2944d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2954d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
2964d621040SChristian S.J. Peron 		return (bpf_buffer_free(d));
2974d621040SChristian S.J. Peron 
2984d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2994d621040SChristian S.J. Peron 		return (bpf_zerocopy_free(d));
3004d621040SChristian S.J. Peron 
3014d621040SChristian S.J. Peron 	default:
3024d621040SChristian S.J. Peron 		panic("bpf_buf_free");
3034d621040SChristian S.J. Peron 	}
3044d621040SChristian S.J. Peron }
3054d621040SChristian S.J. Peron 
3064d621040SChristian S.J. Peron static int
3074d621040SChristian S.J. Peron bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
3084d621040SChristian S.J. Peron {
3094d621040SChristian S.J. Peron 
3104d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
3114d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3124d621040SChristian S.J. Peron 	return (bpf_buffer_uiomove(d, buf, len, uio));
3134d621040SChristian S.J. Peron }
3144d621040SChristian S.J. Peron 
3154d621040SChristian S.J. Peron static int
3164d621040SChristian S.J. Peron bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
3174d621040SChristian S.J. Peron {
3184d621040SChristian S.J. Peron 
3194d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
3204d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3214d621040SChristian S.J. Peron 	return (bpf_buffer_ioctl_sblen(d, i));
3224d621040SChristian S.J. Peron }
3234d621040SChristian S.J. Peron 
3244d621040SChristian S.J. Peron static int
3254d621040SChristian S.J. Peron bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
3264d621040SChristian S.J. Peron {
3274d621040SChristian S.J. Peron 
3284d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
3294d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3304d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_getzmax(td, d, i));
3314d621040SChristian S.J. Peron }
3324d621040SChristian S.J. Peron 
3334d621040SChristian S.J. Peron static int
3344d621040SChristian S.J. Peron bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
3354d621040SChristian S.J. Peron {
3364d621040SChristian S.J. Peron 
3374d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
3384d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3394d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
3404d621040SChristian S.J. Peron }
3414d621040SChristian S.J. Peron 
3424d621040SChristian S.J. Peron static int
3434d621040SChristian S.J. Peron bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
3444d621040SChristian S.J. Peron {
3454d621040SChristian S.J. Peron 
3464d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
3474d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3484d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
3494d621040SChristian S.J. Peron }
3504d621040SChristian S.J. Peron 
3514d621040SChristian S.J. Peron /*
3524d621040SChristian S.J. Peron  * General BPF functions.
3534d621040SChristian S.J. Peron  */
354df8bae1dSRodney W. Grimes static int
355cb44b6dfSAndrew Thompson bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
356560a54e1SJung-uk Kim     struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
357df8bae1dSRodney W. Grimes {
358246b5467SSam Leffler 	const struct ieee80211_bpf_params *p;
359cb44b6dfSAndrew Thompson 	struct ether_header *eh;
360df8bae1dSRodney W. Grimes 	struct mbuf *m;
361df8bae1dSRodney W. Grimes 	int error;
362df8bae1dSRodney W. Grimes 	int len;
363df8bae1dSRodney W. Grimes 	int hlen;
36493e39f0bSChristian S.J. Peron 	int slen;
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes 	/*
367df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
368df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
369df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
370df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
371df8bae1dSRodney W. Grimes 	 * is forwarded as is.
372df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
373df8bae1dSRodney W. Grimes 	 * for the link level header.
374df8bae1dSRodney W. Grimes 	 */
375df8bae1dSRodney W. Grimes 	switch (linktype) {
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes 	case DLT_SLIP:
378df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
379df8bae1dSRodney W. Grimes 		hlen = 0;
380df8bae1dSRodney W. Grimes 		break;
381df8bae1dSRodney W. Grimes 
382df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
383df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
384df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
385797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
386df8bae1dSRodney W. Grimes 		break;
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes 	case DLT_FDDI:
389d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
390d41f24e7SDavid Greenman 		hlen = 0;
391df8bae1dSRodney W. Grimes 		break;
392df8bae1dSRodney W. Grimes 
39322f05c43SAndrey A. Chernov 	case DLT_RAW:
394df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
395df8bae1dSRodney W. Grimes 		hlen = 0;
396df8bae1dSRodney W. Grimes 		break;
397df8bae1dSRodney W. Grimes 
39801399f34SDavid Malone 	case DLT_NULL:
39901399f34SDavid Malone 		/*
40001399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
40101399f34SDavid Malone 		 * corresponds to the address family of the packet.
40201399f34SDavid Malone 		 */
40301399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
40401399f34SDavid Malone 		hlen = 4;
40501399f34SDavid Malone 		break;
40601399f34SDavid Malone 
4074f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
4084f53e3ccSKenjiro Cho 		/*
4094f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
4104f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
4114f53e3ccSKenjiro Cho 		 * specified anyway.
4124f53e3ccSKenjiro Cho 		 */
4134f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
4144f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
4154f53e3ccSKenjiro Cho 		break;
4164f53e3ccSKenjiro Cho 
41730fa52a6SBrian Somers 	case DLT_PPP:
41830fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
41930fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
42030fa52a6SBrian Somers 		break;
42130fa52a6SBrian Somers 
422246b5467SSam Leffler 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
423246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
424246b5467SSam Leffler 		hlen = 0;
425246b5467SSam Leffler 		break;
426246b5467SSam Leffler 
427246b5467SSam Leffler 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
428246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
429246b5467SSam Leffler 		sockp->sa_len = 12;	/* XXX != 0 */
430246b5467SSam Leffler 		hlen = sizeof(struct ieee80211_bpf_params);
431246b5467SSam Leffler 		break;
432246b5467SSam Leffler 
433df8bae1dSRodney W. Grimes 	default:
434df8bae1dSRodney W. Grimes 		return (EIO);
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 
437df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
43801399f34SDavid Malone 
439cb44b6dfSAndrew Thompson 	if (len - hlen > ifp->if_mtu)
44001399f34SDavid Malone 		return (EMSGSIZE);
44101399f34SDavid Malone 
442968c88bcSJung-uk Kim 	if ((unsigned)len > MJUM16BYTES)
443df8bae1dSRodney W. Grimes 		return (EIO);
444df8bae1dSRodney W. Grimes 
445968c88bcSJung-uk Kim 	if (len <= MHLEN)
446968c88bcSJung-uk Kim 		MGETHDR(m, M_WAIT, MT_DATA);
447968c88bcSJung-uk Kim 	else if (len <= MCLBYTES)
448ea26d587SRuslan Ermilov 		m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
449ea26d587SRuslan Ermilov 	else
450968c88bcSJung-uk Kim 		m = m_getjcl(M_WAIT, MT_DATA, M_PKTHDR,
451968c88bcSJung-uk Kim #if (MJUMPAGESIZE > MCLBYTES)
452968c88bcSJung-uk Kim 		    len <= MJUMPAGESIZE ? MJUMPAGESIZE :
453968c88bcSJung-uk Kim #endif
454968c88bcSJung-uk Kim 		    (len <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES));
455963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
456963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
457df8bae1dSRodney W. Grimes 	*mp = m;
45824a229f4SSam Leffler 
45993e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
46093e39f0bSChristian S.J. Peron 		error = EPERM;
46193e39f0bSChristian S.J. Peron 		goto bad;
46293e39f0bSChristian S.J. Peron 	}
46393e39f0bSChristian S.J. Peron 
46493e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
46593e39f0bSChristian S.J. Peron 	if (error)
46693e39f0bSChristian S.J. Peron 		goto bad;
46793e39f0bSChristian S.J. Peron 
46893e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
46993e39f0bSChristian S.J. Peron 	if (slen == 0) {
47093e39f0bSChristian S.J. Peron 		error = EPERM;
47193e39f0bSChristian S.J. Peron 		goto bad;
47293e39f0bSChristian S.J. Peron 	}
47393e39f0bSChristian S.J. Peron 
474cb44b6dfSAndrew Thompson 	/* Check for multicast destination */
475cb44b6dfSAndrew Thompson 	switch (linktype) {
476cb44b6dfSAndrew Thompson 	case DLT_EN10MB:
477cb44b6dfSAndrew Thompson 		eh = mtod(m, struct ether_header *);
478cb44b6dfSAndrew Thompson 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
479cb44b6dfSAndrew Thompson 			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
480cb44b6dfSAndrew Thompson 			    ETHER_ADDR_LEN) == 0)
481cb44b6dfSAndrew Thompson 				m->m_flags |= M_BCAST;
482cb44b6dfSAndrew Thompson 			else
483cb44b6dfSAndrew Thompson 				m->m_flags |= M_MCAST;
484cb44b6dfSAndrew Thompson 		}
485cb44b6dfSAndrew Thompson 		break;
486cb44b6dfSAndrew Thompson 	}
487cb44b6dfSAndrew Thompson 
488df8bae1dSRodney W. Grimes 	/*
48993e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
490df8bae1dSRodney W. Grimes 	 */
491df8bae1dSRodney W. Grimes 	if (hlen != 0) {
492246b5467SSam Leffler 		if (sockp->sa_family == AF_IEEE80211) {
493246b5467SSam Leffler 			/*
494246b5467SSam Leffler 			 * Collect true length from the parameter header
495246b5467SSam Leffler 			 * NB: sockp is known to be zero'd so if we do a
496246b5467SSam Leffler 			 *     short copy unspecified parameters will be
497246b5467SSam Leffler 			 *     zero.
498246b5467SSam Leffler 			 * NB: packet may not be aligned after stripping
499246b5467SSam Leffler 			 *     bpf params
500246b5467SSam Leffler 			 * XXX check ibp_vers
501246b5467SSam Leffler 			 */
502246b5467SSam Leffler 			p = mtod(m, const struct ieee80211_bpf_params *);
503246b5467SSam Leffler 			hlen = p->ibp_len;
504246b5467SSam Leffler 			if (hlen > sizeof(sockp->sa_data)) {
505246b5467SSam Leffler 				error = EINVAL;
506246b5467SSam Leffler 				goto bad;
507246b5467SSam Leffler 			}
508246b5467SSam Leffler 		}
50993e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
510df8bae1dSRodney W. Grimes 	}
511560a54e1SJung-uk Kim 	*hdrlen = hlen;
51293e39f0bSChristian S.J. Peron 
513df8bae1dSRodney W. Grimes 	return (0);
514df8bae1dSRodney W. Grimes bad:
515df8bae1dSRodney W. Grimes 	m_freem(m);
516df8bae1dSRodney W. Grimes 	return (error);
517df8bae1dSRodney W. Grimes }
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes /*
520df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
521df8bae1dSRodney W. Grimes  */
522df8bae1dSRodney W. Grimes static void
52319ba8395SChristian S.J. Peron bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
524df8bae1dSRodney W. Grimes {
525df8bae1dSRodney W. Grimes 	/*
526df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
527df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
528df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
529df8bae1dSRodney W. Grimes 	 */
530e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
531df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
5324a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
533df8bae1dSRodney W. Grimes 
53469f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
535e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
536df8bae1dSRodney W. Grimes }
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes /*
539df8bae1dSRodney W. Grimes  * Detach a file from its interface.
540df8bae1dSRodney W. Grimes  */
541df8bae1dSRodney W. Grimes static void
54219ba8395SChristian S.J. Peron bpf_detachd(struct bpf_d *d)
543df8bae1dSRodney W. Grimes {
5446e891d64SPoul-Henning Kamp 	int error;
545df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
54646448b5aSRobert Watson 	struct ifnet *ifp;
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
54946448b5aSRobert Watson 	BPFIF_LOCK(bp);
55046448b5aSRobert Watson 	BPFD_LOCK(d);
55146448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
55246448b5aSRobert Watson 
55346448b5aSRobert Watson 	/*
55446448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
55546448b5aSRobert Watson 	 */
55646448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
55746448b5aSRobert Watson 
55869f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
559572bde2aSRobert Watson 	d->bd_bif = NULL;
56046448b5aSRobert Watson 	BPFD_UNLOCK(d);
56146448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
56246448b5aSRobert Watson 
563df8bae1dSRodney W. Grimes 	/*
564df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
565df8bae1dSRodney W. Grimes 	 * If so, turn it off.
566df8bae1dSRodney W. Grimes 	 */
567df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
568df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
56946448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
5706e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
571df8bae1dSRodney W. Grimes 			/*
5726e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
573df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
574df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
575df8bae1dSRodney W. Grimes 			 * take it out.
576df8bae1dSRodney W. Grimes 			 */
5778eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
5788eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
5796e891d64SPoul-Henning Kamp 		}
580df8bae1dSRodney W. Grimes 	}
581df8bae1dSRodney W. Grimes }
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes /*
584136600feSEd Schouten  * Close the descriptor by detaching it from its interface,
585136600feSEd Schouten  * deallocating its buffers, and marking it free.
586136600feSEd Schouten  */
587136600feSEd Schouten static void
588136600feSEd Schouten bpf_dtor(void *data)
589136600feSEd Schouten {
590136600feSEd Schouten 	struct bpf_d *d = data;
591136600feSEd Schouten 
592136600feSEd Schouten 	BPFD_LOCK(d);
593136600feSEd Schouten 	if (d->bd_state == BPF_WAITING)
594136600feSEd Schouten 		callout_stop(&d->bd_callout);
595136600feSEd Schouten 	d->bd_state = BPF_IDLE;
596136600feSEd Schouten 	BPFD_UNLOCK(d);
597136600feSEd Schouten 	funsetown(&d->bd_sigio);
598136600feSEd Schouten 	mtx_lock(&bpf_mtx);
599136600feSEd Schouten 	if (d->bd_bif)
600136600feSEd Schouten 		bpf_detachd(d);
601136600feSEd Schouten 	mtx_unlock(&bpf_mtx);
602136600feSEd Schouten 	selwakeuppri(&d->bd_sel, PRINET);
603136600feSEd Schouten #ifdef MAC
604136600feSEd Schouten 	mac_bpfdesc_destroy(d);
605136600feSEd Schouten #endif /* MAC */
606136600feSEd Schouten 	knlist_destroy(&d->bd_sel.si_note);
607136600feSEd Schouten 	bpf_freed(d);
608136600feSEd Schouten 	free(d, M_BPF);
609136600feSEd Schouten }
610136600feSEd Schouten 
611136600feSEd Schouten /*
612df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
613df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
614df8bae1dSRodney W. Grimes  */
615df8bae1dSRodney W. Grimes /* ARGSUSED */
61687f6c662SJulian Elischer static	int
61719ba8395SChristian S.J. Peron bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
618df8bae1dSRodney W. Grimes {
619e7bb21b3SJonathan Lemon 	struct bpf_d *d;
620136600feSEd Schouten 	int error;
621df8bae1dSRodney W. Grimes 
622a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
623136600feSEd Schouten 	error = devfs_set_cdevpriv(d, bpf_dtor);
624136600feSEd Schouten 	if (error != 0) {
625136600feSEd Schouten 		free(d, M_BPF);
626136600feSEd Schouten 		return (error);
627136600feSEd Schouten 	}
6284d621040SChristian S.J. Peron 
6294d621040SChristian S.J. Peron 	/*
6304d621040SChristian S.J. Peron 	 * For historical reasons, perform a one-time initialization call to
6314d621040SChristian S.J. Peron 	 * the buffer routines, even though we're not yet committed to a
6324d621040SChristian S.J. Peron 	 * particular buffer method.
6334d621040SChristian S.J. Peron 	 */
6344d621040SChristian S.J. Peron 	bpf_buffer_init(d);
6354d621040SChristian S.J. Peron 	d->bd_bufmode = BPF_BUFMODE_BUFFER;
63600a83887SPaul Traina 	d->bd_sig = SIGIO;
637560a54e1SJung-uk Kim 	d->bd_direction = BPF_D_INOUT;
63869f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
63982f4445dSRobert Watson #ifdef MAC
64030d239bcSRobert Watson 	mac_bpfdesc_init(d);
64130d239bcSRobert Watson 	mac_bpfdesc_create(td->td_ucred, d);
64282f4445dSRobert Watson #endif
6436008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
644c6b28997SRobert Watson 	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
645571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes 	return (0);
648df8bae1dSRodney W. Grimes }
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes /*
651df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
652df8bae1dSRodney W. Grimes  */
65387f6c662SJulian Elischer static	int
65419ba8395SChristian S.J. Peron bpfread(struct cdev *dev, struct uio *uio, int ioflag)
655df8bae1dSRodney W. Grimes {
656136600feSEd Schouten 	struct bpf_d *d;
65781bda851SJohn Polstra 	int timed_out;
658df8bae1dSRodney W. Grimes 	int error;
659df8bae1dSRodney W. Grimes 
660136600feSEd Schouten 	error = devfs_get_cdevpriv((void **)&d);
661136600feSEd Schouten 	if (error != 0)
662136600feSEd Schouten 		return (error);
663136600feSEd Schouten 
664df8bae1dSRodney W. Grimes 	/*
665df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
666df8bae1dSRodney W. Grimes 	 * as kernel buffers.
667df8bae1dSRodney W. Grimes 	 */
668df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
669df8bae1dSRodney W. Grimes 		return (EINVAL);
670df8bae1dSRodney W. Grimes 
671e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
67250ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
6734d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
6744d621040SChristian S.J. Peron 		BPFD_UNLOCK(d);
6754d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
6764d621040SChristian S.J. Peron 	}
67781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
67881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
67981bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
68081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
681df8bae1dSRodney W. Grimes 	/*
682df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
683df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
684df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
685df8bae1dSRodney W. Grimes 	 */
686572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
68781bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
688df8bae1dSRodney W. Grimes 			/*
689df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
690df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
691df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
692df8bae1dSRodney W. Grimes 			 */
693df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
694df8bae1dSRodney W. Grimes 			break;
695df8bae1dSRodney W. Grimes 		}
696de5d9935SRobert Watson 
697de5d9935SRobert Watson 		/*
698de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
699de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
700de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
701de5d9935SRobert Watson 		 * it before using it again.
702de5d9935SRobert Watson 		 */
703de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
704e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
705de5d9935SRobert Watson 			return (ENXIO);
706de5d9935SRobert Watson 		}
707de5d9935SRobert Watson 
708e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
709e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
710fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
711fba3cfdeSJohn Polstra 		}
712521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
713e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
714df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
715e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
716df8bae1dSRodney W. Grimes 			return (error);
717df8bae1dSRodney W. Grimes 		}
718df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
719df8bae1dSRodney W. Grimes 			/*
720df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
721df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
722df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
723df8bae1dSRodney W. Grimes 			 */
724df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
725df8bae1dSRodney W. Grimes 				/*
726df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
727df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
728df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
729df8bae1dSRodney W. Grimes 				 */
730df8bae1dSRodney W. Grimes 				break;
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
733e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
734df8bae1dSRodney W. Grimes 				return (0);
735df8bae1dSRodney W. Grimes 			}
736df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
737df8bae1dSRodney W. Grimes 			break;
738df8bae1dSRodney W. Grimes 		}
739df8bae1dSRodney W. Grimes 	}
740df8bae1dSRodney W. Grimes 	/*
741df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
742df8bae1dSRodney W. Grimes 	 */
743e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes 	/*
746df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
747df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
748df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
74931b32e6dSRobert Watson 	 *
75031b32e6dSRobert Watson 	 * XXXRW: More synchronization needed here: what if a second thread
75131b32e6dSRobert Watson 	 * issues a read on the same fd at the same time?  Don't want this
75231b32e6dSRobert Watson 	 * getting invalidated.
753df8bae1dSRodney W. Grimes 	 */
7544d621040SChristian S.J. Peron 	error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
755df8bae1dSRodney W. Grimes 
756e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
757df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
758572bde2aSRobert Watson 	d->bd_hbuf = NULL;
759df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
76029f612ecSChristian S.J. Peron 	bpf_buf_reclaimed(d);
761e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	return (error);
764df8bae1dSRodney W. Grimes }
765df8bae1dSRodney W. Grimes 
766df8bae1dSRodney W. Grimes /*
767df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
768df8bae1dSRodney W. Grimes  */
769e7bb21b3SJonathan Lemon static __inline void
77019ba8395SChristian S.J. Peron bpf_wakeup(struct bpf_d *d)
771df8bae1dSRodney W. Grimes {
772a3272e3cSChristian S.J. Peron 
773a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
77481bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
77581bda851SJohn Polstra 		callout_stop(&d->bd_callout);
77681bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
77781bda851SJohn Polstra 	}
778521f364bSDag-Erling Smørgrav 	wakeup(d);
779831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
780f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
78100a83887SPaul Traina 
782512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
783ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
784df8bae1dSRodney W. Grimes }
785df8bae1dSRodney W. Grimes 
78681bda851SJohn Polstra static void
78719ba8395SChristian S.J. Peron bpf_timed_out(void *arg)
78881bda851SJohn Polstra {
78981bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
79081bda851SJohn Polstra 
79181bda851SJohn Polstra 	BPFD_LOCK(d);
79281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
79381bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
79481bda851SJohn Polstra 		if (d->bd_slen != 0)
79581bda851SJohn Polstra 			bpf_wakeup(d);
79681bda851SJohn Polstra 	}
79781bda851SJohn Polstra 	BPFD_UNLOCK(d);
79881bda851SJohn Polstra }
79981bda851SJohn Polstra 
80087f6c662SJulian Elischer static int
8014d621040SChristian S.J. Peron bpf_ready(struct bpf_d *d)
8024d621040SChristian S.J. Peron {
8034d621040SChristian S.J. Peron 
8044d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
8054d621040SChristian S.J. Peron 
8064d621040SChristian S.J. Peron 	if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
8074d621040SChristian S.J. Peron 		return (1);
8084d621040SChristian S.J. Peron 	if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
8094d621040SChristian S.J. Peron 	    d->bd_slen != 0)
8104d621040SChristian S.J. Peron 		return (1);
8114d621040SChristian S.J. Peron 	return (0);
8124d621040SChristian S.J. Peron }
8134d621040SChristian S.J. Peron 
8144d621040SChristian S.J. Peron static int
81519ba8395SChristian S.J. Peron bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
816df8bae1dSRodney W. Grimes {
817136600feSEd Schouten 	struct bpf_d *d;
818df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
819560a54e1SJung-uk Kim 	struct mbuf *m, *mc;
8208240bf1eSRobert Watson 	struct sockaddr dst;
821560a54e1SJung-uk Kim 	int error, hlen;
822df8bae1dSRodney W. Grimes 
823136600feSEd Schouten 	error = devfs_get_cdevpriv((void **)&d);
824136600feSEd Schouten 	if (error != 0)
825136600feSEd Schouten 		return (error);
826136600feSEd Schouten 
82750ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
8284d621040SChristian S.J. Peron 	d->bd_wcount++;
8294d621040SChristian S.J. Peron 	if (d->bd_bif == NULL) {
8304d621040SChristian S.J. Peron 		d->bd_wdcount++;
831df8bae1dSRodney W. Grimes 		return (ENXIO);
8324d621040SChristian S.J. Peron 	}
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
835df8bae1dSRodney W. Grimes 
8364d621040SChristian S.J. Peron 	if ((ifp->if_flags & IFF_UP) == 0) {
8374d621040SChristian S.J. Peron 		d->bd_wdcount++;
8383518d220SSam Leffler 		return (ENETDOWN);
8394d621040SChristian S.J. Peron 	}
8403518d220SSam Leffler 
8414d621040SChristian S.J. Peron 	if (uio->uio_resid == 0) {
8424d621040SChristian S.J. Peron 		d->bd_wdcount++;
843df8bae1dSRodney W. Grimes 		return (0);
8444d621040SChristian S.J. Peron 	}
845df8bae1dSRodney W. Grimes 
8468240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
847d83e603aSChristian S.J. Peron 	m = NULL;
848d83e603aSChristian S.J. Peron 	hlen = 0;
849cb44b6dfSAndrew Thompson 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
850560a54e1SJung-uk Kim 	    &m, &dst, &hlen, d->bd_wfilter);
8514d621040SChristian S.J. Peron 	if (error) {
8524d621040SChristian S.J. Peron 		d->bd_wdcount++;
853df8bae1dSRodney W. Grimes 		return (error);
8544d621040SChristian S.J. Peron 	}
8554d621040SChristian S.J. Peron 	d->bd_wfcount++;
856114ae644SMike Smith 	if (d->bd_hdrcmplt)
857114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
858114ae644SMike Smith 
859560a54e1SJung-uk Kim 	if (d->bd_feedback) {
860560a54e1SJung-uk Kim 		mc = m_dup(m, M_DONTWAIT);
861560a54e1SJung-uk Kim 		if (mc != NULL)
862560a54e1SJung-uk Kim 			mc->m_pkthdr.rcvif = ifp;
8638cd892f7SJung-uk Kim 		/* Set M_PROMISC for outgoing packets to be discarded. */
8648cd892f7SJung-uk Kim 		if (d->bd_direction == BPF_D_INOUT)
8658cd892f7SJung-uk Kim 			m->m_flags |= M_PROMISC;
866560a54e1SJung-uk Kim 	} else
867560a54e1SJung-uk Kim 		mc = NULL;
868560a54e1SJung-uk Kim 
869560a54e1SJung-uk Kim 	m->m_pkthdr.len -= hlen;
870560a54e1SJung-uk Kim 	m->m_len -= hlen;
871560a54e1SJung-uk Kim 	m->m_data += hlen;	/* XXX */
872560a54e1SJung-uk Kim 
87382f4445dSRobert Watson #ifdef MAC
874f747d2ddSRobert Watson 	BPFD_LOCK(d);
87530d239bcSRobert Watson 	mac_bpfdesc_create_mbuf(d, m);
876560a54e1SJung-uk Kim 	if (mc != NULL)
87730d239bcSRobert Watson 		mac_bpfdesc_create_mbuf(d, mc);
878f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
87982f4445dSRobert Watson #endif
880560a54e1SJung-uk Kim 
881572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
8824d621040SChristian S.J. Peron 	if (error)
8834d621040SChristian S.J. Peron 		d->bd_wdcount++;
884560a54e1SJung-uk Kim 
885560a54e1SJung-uk Kim 	if (mc != NULL) {
8860bf686c1SRobert Watson 		if (error == 0)
887560a54e1SJung-uk Kim 			(*ifp->if_input)(ifp, mc);
8880bf686c1SRobert Watson 		else
889560a54e1SJung-uk Kim 			m_freem(mc);
890560a54e1SJung-uk Kim 	}
891560a54e1SJung-uk Kim 
892df8bae1dSRodney W. Grimes 	return (error);
893df8bae1dSRodney W. Grimes }
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes /*
896df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
897e7bb21b3SJonathan Lemon  * receive and drop counts.
898df8bae1dSRodney W. Grimes  */
899df8bae1dSRodney W. Grimes static void
90019ba8395SChristian S.J. Peron reset_d(struct bpf_d *d)
901df8bae1dSRodney W. Grimes {
902e7bb21b3SJonathan Lemon 
903e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
904df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
905df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
906df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
907572bde2aSRobert Watson 		d->bd_hbuf = NULL;
90829f612ecSChristian S.J. Peron 		bpf_buf_reclaimed(d);
909df8bae1dSRodney W. Grimes 	}
910df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
911df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
912df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
913df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
91469f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
9154d621040SChristian S.J. Peron 	d->bd_wcount = 0;
9164d621040SChristian S.J. Peron 	d->bd_wfcount = 0;
9174d621040SChristian S.J. Peron 	d->bd_wdcount = 0;
9184d621040SChristian S.J. Peron 	d->bd_zcopy = 0;
919df8bae1dSRodney W. Grimes }
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes /*
922df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
923df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
924df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
925f11c3508SDavid Malone  *  BIOCSETF		Set read filter.
926f11c3508SDavid Malone  *  BIOCSETFNR		Set read filter without resetting descriptor.
927f11c3508SDavid Malone  *  BIOCSETWF		Set write filter.
928df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
929df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
930df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
931df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
932df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
933df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
934df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
935df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
936df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
937df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
938114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
939114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
940560a54e1SJung-uk Kim  *  BIOCGDIRECTION	Get packet direction flag
941560a54e1SJung-uk Kim  *  BIOCSDIRECTION	Set packet direction flag
94293e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
943560a54e1SJung-uk Kim  *  BIOCFEEDBACK	Set packet feedback mode.
9444d621040SChristian S.J. Peron  *  BIOCSETZBUF		Set current zero-copy buffer locations.
9454d621040SChristian S.J. Peron  *  BIOCGETZMAX		Get maximum zero-copy buffer size.
9464d621040SChristian S.J. Peron  *  BIOCROTZBUF		Force rotation of zero-copy buffer
9474d621040SChristian S.J. Peron  *  BIOCSETBUFMODE	Set buffer mode.
9484d621040SChristian S.J. Peron  *  BIOCGETBUFMODE	Get current buffer mode.
949df8bae1dSRodney W. Grimes  */
950df8bae1dSRodney W. Grimes /* ARGSUSED */
95187f6c662SJulian Elischer static	int
95219ba8395SChristian S.J. Peron bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
95319ba8395SChristian S.J. Peron     struct thread *td)
954df8bae1dSRodney W. Grimes {
955136600feSEd Schouten 	struct bpf_d *d;
956136600feSEd Schouten 	int error;
957136600feSEd Schouten 
958136600feSEd Schouten 	error = devfs_get_cdevpriv((void **)&d);
959136600feSEd Schouten 	if (error != 0)
960136600feSEd Schouten 		return (error);
961df8bae1dSRodney W. Grimes 
962b75a24a0SChristian S.J. Peron 	/*
963b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
964b75a24a0SChristian S.J. Peron 	 */
96581bda851SJohn Polstra 	BPFD_LOCK(d);
966cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
96781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
96881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
96981bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
97081bda851SJohn Polstra 	BPFD_UNLOCK(d);
97181bda851SJohn Polstra 
97293e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
97393e39f0bSChristian S.J. Peron 		switch (cmd) {
97493e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
97593e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
97693e39f0bSChristian S.J. Peron 		case BIOCGDLT:
97793e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
97893e39f0bSChristian S.J. Peron 		case BIOCGETIF:
97993e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
98093e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
98193e39f0bSChristian S.J. Peron 		case BIOCVERSION:
98293e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
98393e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
984560a54e1SJung-uk Kim 		case BIOCFEEDBACK:
98593e39f0bSChristian S.J. Peron 		case FIONREAD:
98693e39f0bSChristian S.J. Peron 		case BIOCLOCK:
98793e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
98893e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
98993e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
9904d621040SChristian S.J. Peron 		case BIOCROTZBUF:
99193e39f0bSChristian S.J. Peron 			break;
99293e39f0bSChristian S.J. Peron 		default:
99393e39f0bSChristian S.J. Peron 			return (EPERM);
99493e39f0bSChristian S.J. Peron 		}
99593e39f0bSChristian S.J. Peron 	}
996df8bae1dSRodney W. Grimes 	switch (cmd) {
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	default:
999df8bae1dSRodney W. Grimes 		error = EINVAL;
1000df8bae1dSRodney W. Grimes 		break;
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	/*
1003df8bae1dSRodney W. Grimes 	 * Check for read packet available.
1004df8bae1dSRodney W. Grimes 	 */
1005df8bae1dSRodney W. Grimes 	case FIONREAD:
1006df8bae1dSRodney W. Grimes 		{
1007df8bae1dSRodney W. Grimes 			int n;
1008df8bae1dSRodney W. Grimes 
1009e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
1010df8bae1dSRodney W. Grimes 			n = d->bd_slen;
1011df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
1012df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
1013e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 			*(int *)addr = n;
1016df8bae1dSRodney W. Grimes 			break;
1017df8bae1dSRodney W. Grimes 		}
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
1020df8bae1dSRodney W. Grimes 		{
1021df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1022df8bae1dSRodney W. Grimes 
1023572bde2aSRobert Watson 			if (d->bd_bif == NULL)
1024df8bae1dSRodney W. Grimes 				error = EINVAL;
1025df8bae1dSRodney W. Grimes 			else {
1026df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
1027df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
1028df8bae1dSRodney W. Grimes 			}
1029df8bae1dSRodney W. Grimes 			break;
1030df8bae1dSRodney W. Grimes 		}
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes 	/*
1033df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
1034df8bae1dSRodney W. Grimes 	 */
1035df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
1036df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
1037df8bae1dSRodney W. Grimes 		break;
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 	/*
1040df8bae1dSRodney W. Grimes 	 * Set buffer length.
1041df8bae1dSRodney W. Grimes 	 */
1042df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
10434d621040SChristian S.J. Peron 		error = bpf_ioctl_sblen(d, (u_int *)addr);
1044df8bae1dSRodney W. Grimes 		break;
1045df8bae1dSRodney W. Grimes 
1046df8bae1dSRodney W. Grimes 	/*
1047df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
1048df8bae1dSRodney W. Grimes 	 */
1049df8bae1dSRodney W. Grimes 	case BIOCSETF:
1050f11c3508SDavid Malone 	case BIOCSETFNR:
105193e39f0bSChristian S.J. Peron 	case BIOCSETWF:
105293e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1053df8bae1dSRodney W. Grimes 		break;
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes 	/*
1056df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
1057df8bae1dSRodney W. Grimes 	 */
1058df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
1059e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1060df8bae1dSRodney W. Grimes 		reset_d(d);
1061e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1062df8bae1dSRodney W. Grimes 		break;
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes 	/*
1065df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
1066df8bae1dSRodney W. Grimes 	 */
1067df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
1068572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
1069df8bae1dSRodney W. Grimes 			/*
1070df8bae1dSRodney W. Grimes 			 * No interface attached yet.
1071df8bae1dSRodney W. Grimes 			 */
1072df8bae1dSRodney W. Grimes 			error = EINVAL;
1073df8bae1dSRodney W. Grimes 			break;
1074df8bae1dSRodney W. Grimes 		}
1075df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
1076df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
1077df8bae1dSRodney W. Grimes 			if (error == 0)
1078df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
1079df8bae1dSRodney W. Grimes 		}
1080df8bae1dSRodney W. Grimes 		break;
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes 	/*
10838eab61f3SSam Leffler 	 * Get current data link type.
1084df8bae1dSRodney W. Grimes 	 */
1085df8bae1dSRodney W. Grimes 	case BIOCGDLT:
1086572bde2aSRobert Watson 		if (d->bd_bif == NULL)
1087df8bae1dSRodney W. Grimes 			error = EINVAL;
1088df8bae1dSRodney W. Grimes 		else
1089df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
1090df8bae1dSRodney W. Grimes 		break;
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 	/*
10938eab61f3SSam Leffler 	 * Get a list of supported data link types.
10948eab61f3SSam Leffler 	 */
10958eab61f3SSam Leffler 	case BIOCGDLTLIST:
1096572bde2aSRobert Watson 		if (d->bd_bif == NULL)
10978eab61f3SSam Leffler 			error = EINVAL;
10988eab61f3SSam Leffler 		else
10998eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
11008eab61f3SSam Leffler 		break;
11018eab61f3SSam Leffler 
11028eab61f3SSam Leffler 	/*
11038eab61f3SSam Leffler 	 * Set data link type.
11048eab61f3SSam Leffler 	 */
11058eab61f3SSam Leffler 	case BIOCSDLT:
1106572bde2aSRobert Watson 		if (d->bd_bif == NULL)
11078eab61f3SSam Leffler 			error = EINVAL;
11088eab61f3SSam Leffler 		else
11098eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
11108eab61f3SSam Leffler 		break;
11118eab61f3SSam Leffler 
11128eab61f3SSam Leffler 	/*
11131dd0feaaSArchie Cobbs 	 * Get interface name.
1114df8bae1dSRodney W. Grimes 	 */
1115df8bae1dSRodney W. Grimes 	case BIOCGETIF:
1116572bde2aSRobert Watson 		if (d->bd_bif == NULL)
1117df8bae1dSRodney W. Grimes 			error = EINVAL;
11181dd0feaaSArchie Cobbs 		else {
11191dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
11201dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
11211dd0feaaSArchie Cobbs 
11229bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
11239bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
11241dd0feaaSArchie Cobbs 		}
1125df8bae1dSRodney W. Grimes 		break;
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes 	/*
1128df8bae1dSRodney W. Grimes 	 * Set interface.
1129df8bae1dSRodney W. Grimes 	 */
1130df8bae1dSRodney W. Grimes 	case BIOCSETIF:
1131df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
1132df8bae1dSRodney W. Grimes 		break;
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes 	/*
1135df8bae1dSRodney W. Grimes 	 * Set read timeout.
1136df8bae1dSRodney W. Grimes 	 */
1137df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
1138df8bae1dSRodney W. Grimes 		{
1139df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
1140df8bae1dSRodney W. Grimes 
1141bdc2cdc5SAlexander Langer 			/*
1142bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
1143bdc2cdc5SAlexander Langer 			 * a one-shot timer.
1144bdc2cdc5SAlexander Langer 			 */
1145bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
1146bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
1147df8bae1dSRodney W. Grimes 			break;
1148df8bae1dSRodney W. Grimes 		}
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes 	/*
1151df8bae1dSRodney W. Grimes 	 * Get read timeout.
1152df8bae1dSRodney W. Grimes 	 */
1153df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
1154df8bae1dSRodney W. Grimes 		{
1155df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
1156df8bae1dSRodney W. Grimes 
1157bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
1158bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
1159df8bae1dSRodney W. Grimes 			break;
1160df8bae1dSRodney W. Grimes 		}
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes 	/*
1163df8bae1dSRodney W. Grimes 	 * Get packet stats.
1164df8bae1dSRodney W. Grimes 	 */
1165df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
1166df8bae1dSRodney W. Grimes 		{
1167df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
1168df8bae1dSRodney W. Grimes 
11694d621040SChristian S.J. Peron 			/* XXXCSJP overflow */
1170df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
1171df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
1172df8bae1dSRodney W. Grimes 			break;
1173df8bae1dSRodney W. Grimes 		}
1174df8bae1dSRodney W. Grimes 
1175df8bae1dSRodney W. Grimes 	/*
1176df8bae1dSRodney W. Grimes 	 * Set immediate mode.
1177df8bae1dSRodney W. Grimes 	 */
1178df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
1179df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
1180df8bae1dSRodney W. Grimes 		break;
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 	case BIOCVERSION:
1183df8bae1dSRodney W. Grimes 		{
1184df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
1187df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
1188df8bae1dSRodney W. Grimes 			break;
1189df8bae1dSRodney W. Grimes 		}
119000a83887SPaul Traina 
1191114ae644SMike Smith 	/*
1192114ae644SMike Smith 	 * Get "header already complete" flag
1193114ae644SMike Smith 	 */
1194114ae644SMike Smith 	case BIOCGHDRCMPLT:
1195114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
1196114ae644SMike Smith 		break;
1197114ae644SMike Smith 
1198114ae644SMike Smith 	/*
1199114ae644SMike Smith 	 * Set "header already complete" flag
1200114ae644SMike Smith 	 */
1201114ae644SMike Smith 	case BIOCSHDRCMPLT:
1202114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1203114ae644SMike Smith 		break;
1204114ae644SMike Smith 
12058ed3828cSRobert Watson 	/*
1206560a54e1SJung-uk Kim 	 * Get packet direction flag
12078ed3828cSRobert Watson 	 */
1208560a54e1SJung-uk Kim 	case BIOCGDIRECTION:
1209560a54e1SJung-uk Kim 		*(u_int *)addr = d->bd_direction;
12108ed3828cSRobert Watson 		break;
12118ed3828cSRobert Watson 
12128ed3828cSRobert Watson 	/*
1213560a54e1SJung-uk Kim 	 * Set packet direction flag
12148ed3828cSRobert Watson 	 */
1215560a54e1SJung-uk Kim 	case BIOCSDIRECTION:
1216560a54e1SJung-uk Kim 		{
1217560a54e1SJung-uk Kim 			u_int	direction;
1218560a54e1SJung-uk Kim 
1219560a54e1SJung-uk Kim 			direction = *(u_int *)addr;
1220560a54e1SJung-uk Kim 			switch (direction) {
1221560a54e1SJung-uk Kim 			case BPF_D_IN:
1222560a54e1SJung-uk Kim 			case BPF_D_INOUT:
1223560a54e1SJung-uk Kim 			case BPF_D_OUT:
1224560a54e1SJung-uk Kim 				d->bd_direction = direction;
1225560a54e1SJung-uk Kim 				break;
1226560a54e1SJung-uk Kim 			default:
1227560a54e1SJung-uk Kim 				error = EINVAL;
1228560a54e1SJung-uk Kim 			}
1229560a54e1SJung-uk Kim 		}
1230560a54e1SJung-uk Kim 		break;
1231560a54e1SJung-uk Kim 
1232560a54e1SJung-uk Kim 	case BIOCFEEDBACK:
1233560a54e1SJung-uk Kim 		d->bd_feedback = *(u_int *)addr;
1234560a54e1SJung-uk Kim 		break;
1235560a54e1SJung-uk Kim 
1236560a54e1SJung-uk Kim 	case BIOCLOCK:
1237560a54e1SJung-uk Kim 		d->bd_locked = 1;
12388ed3828cSRobert Watson 		break;
12398ed3828cSRobert Watson 
124000a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
124100a83887SPaul Traina 		break;
124200a83887SPaul Traina 
124300a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
124400a83887SPaul Traina 		d->bd_async = *(int *)addr;
124500a83887SPaul Traina 		break;
124600a83887SPaul Traina 
1247831d27a9SDon Lewis 	case FIOSETOWN:
1248831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
124900a83887SPaul Traina 		break;
125000a83887SPaul Traina 
1251831d27a9SDon Lewis 	case FIOGETOWN:
125291e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
1253831d27a9SDon Lewis 		break;
1254831d27a9SDon Lewis 
1255831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
1256831d27a9SDon Lewis 	case TIOCSPGRP:
1257831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1258831d27a9SDon Lewis 		break;
1259831d27a9SDon Lewis 
1260831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
126100a83887SPaul Traina 	case TIOCGPGRP:
126291e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
126300a83887SPaul Traina 		break;
126400a83887SPaul Traina 
126500a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
126600a83887SPaul Traina 		{
126700a83887SPaul Traina 			u_int sig;
126800a83887SPaul Traina 
126900a83887SPaul Traina 			sig = *(u_int *)addr;
127000a83887SPaul Traina 
127100a83887SPaul Traina 			if (sig >= NSIG)
127200a83887SPaul Traina 				error = EINVAL;
127300a83887SPaul Traina 			else
127400a83887SPaul Traina 				d->bd_sig = sig;
127500a83887SPaul Traina 			break;
127600a83887SPaul Traina 		}
127700a83887SPaul Traina 	case BIOCGRSIG:
127800a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
127900a83887SPaul Traina 		break;
12804d621040SChristian S.J. Peron 
12814d621040SChristian S.J. Peron 	case BIOCGETBUFMODE:
12824d621040SChristian S.J. Peron 		*(u_int *)addr = d->bd_bufmode;
12834d621040SChristian S.J. Peron 		break;
12844d621040SChristian S.J. Peron 
12854d621040SChristian S.J. Peron 	case BIOCSETBUFMODE:
12864d621040SChristian S.J. Peron 		/*
12874d621040SChristian S.J. Peron 		 * Allow the buffering mode to be changed as long as we
12884d621040SChristian S.J. Peron 		 * haven't yet committed to a particular mode.  Our
12894d621040SChristian S.J. Peron 		 * definition of commitment, for now, is whether or not a
12904d621040SChristian S.J. Peron 		 * buffer has been allocated or an interface attached, since
12914d621040SChristian S.J. Peron 		 * that's the point where things get tricky.
12924d621040SChristian S.J. Peron 		 */
12934d621040SChristian S.J. Peron 		switch (*(u_int *)addr) {
12944d621040SChristian S.J. Peron 		case BPF_BUFMODE_BUFFER:
12954d621040SChristian S.J. Peron 			break;
12964d621040SChristian S.J. Peron 
12974d621040SChristian S.J. Peron 		case BPF_BUFMODE_ZBUF:
12984d621040SChristian S.J. Peron 			if (bpf_zerocopy_enable)
12994d621040SChristian S.J. Peron 				break;
13004d621040SChristian S.J. Peron 			/* FALLSTHROUGH */
13014d621040SChristian S.J. Peron 
13024d621040SChristian S.J. Peron 		default:
13034d621040SChristian S.J. Peron 			return (EINVAL);
13044d621040SChristian S.J. Peron 		}
13054d621040SChristian S.J. Peron 
13064d621040SChristian S.J. Peron 		BPFD_LOCK(d);
13074d621040SChristian S.J. Peron 		if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
13084d621040SChristian S.J. Peron 		    d->bd_fbuf != NULL || d->bd_bif != NULL) {
13094d621040SChristian S.J. Peron 			BPFD_UNLOCK(d);
13104d621040SChristian S.J. Peron 			return (EBUSY);
13114d621040SChristian S.J. Peron 		}
13124d621040SChristian S.J. Peron 		d->bd_bufmode = *(u_int *)addr;
13134d621040SChristian S.J. Peron 		BPFD_UNLOCK(d);
13144d621040SChristian S.J. Peron 		break;
13154d621040SChristian S.J. Peron 
13164d621040SChristian S.J. Peron 	case BIOCGETZMAX:
13174d621040SChristian S.J. Peron 		return (bpf_ioctl_getzmax(td, d, (size_t *)addr));
13184d621040SChristian S.J. Peron 
13194d621040SChristian S.J. Peron 	case BIOCSETZBUF:
13204d621040SChristian S.J. Peron 		return (bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr));
13214d621040SChristian S.J. Peron 
13224d621040SChristian S.J. Peron 	case BIOCROTZBUF:
13234d621040SChristian S.J. Peron 		return (bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr));
1324df8bae1dSRodney W. Grimes 	}
1325df8bae1dSRodney W. Grimes 	return (error);
1326df8bae1dSRodney W. Grimes }
1327df8bae1dSRodney W. Grimes 
1328df8bae1dSRodney W. Grimes /*
1329df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1330df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1331df8bae1dSRodney W. Grimes  */
1332f708ef1bSPoul-Henning Kamp static int
133319ba8395SChristian S.J. Peron bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1334df8bae1dSRodney W. Grimes {
1335df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
133693e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1337293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1338ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1339ae275efcSJung-uk Kim #endif
1340df8bae1dSRodney W. Grimes 
134193e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
134293e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
134393e39f0bSChristian S.J. Peron 		wfilter = 1;
1344293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1345ae275efcSJung-uk Kim 		ofunc = NULL;
1346ae275efcSJung-uk Kim #endif
134793e39f0bSChristian S.J. Peron 	} else {
134893e39f0bSChristian S.J. Peron 		wfilter = 0;
134993e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1350293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1351ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1352ae275efcSJung-uk Kim #endif
135393e39f0bSChristian S.J. Peron 	}
1354572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1355df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1356df8bae1dSRodney W. Grimes 			return (EINVAL);
1357e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
135893e39f0bSChristian S.J. Peron 		if (wfilter)
135993e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1360ae275efcSJung-uk Kim 		else {
136193e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1362293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1363ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1364ae275efcSJung-uk Kim #endif
1365f11c3508SDavid Malone 			if (cmd == BIOCSETF)
1366df8bae1dSRodney W. Grimes 				reset_d(d);
1367f11c3508SDavid Malone 		}
1368e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1369572bde2aSRobert Watson 		if (old != NULL)
1370bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1371293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1372ae275efcSJung-uk Kim 		if (ofunc != NULL)
1373ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1374ae275efcSJung-uk Kim #endif
1375df8bae1dSRodney W. Grimes 		return (0);
1376df8bae1dSRodney W. Grimes 	}
1377df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
13780eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1379df8bae1dSRodney W. Grimes 		return (EINVAL);
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1382a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1383df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1384df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1385e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
138693e39f0bSChristian S.J. Peron 		if (wfilter)
138793e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1388ae275efcSJung-uk Kim 		else {
138993e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1390293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1391ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1392ae275efcSJung-uk Kim #endif
1393f11c3508SDavid Malone 			if (cmd == BIOCSETF)
1394df8bae1dSRodney W. Grimes 				reset_d(d);
1395f11c3508SDavid Malone 		}
1396e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1397572bde2aSRobert Watson 		if (old != NULL)
1398bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1399293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1400ae275efcSJung-uk Kim 		if (ofunc != NULL)
1401ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1402ae275efcSJung-uk Kim #endif
1403df8bae1dSRodney W. Grimes 
1404df8bae1dSRodney W. Grimes 		return (0);
1405df8bae1dSRodney W. Grimes 	}
1406bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1407df8bae1dSRodney W. Grimes 	return (EINVAL);
1408df8bae1dSRodney W. Grimes }
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes /*
1411df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1412df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1413df8bae1dSRodney W. Grimes  * Return an errno or 0.
1414df8bae1dSRodney W. Grimes  */
1415df8bae1dSRodney W. Grimes static int
141619ba8395SChristian S.J. Peron bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1417df8bae1dSRodney W. Grimes {
1418df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14199b44ff22SGarrett Wollman 	struct ifnet *theywant;
1420df8bae1dSRodney W. Grimes 
14219b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
142216d878ccSChristian S.J. Peron 	if (theywant == NULL || theywant->if_bpf == NULL)
142316d878ccSChristian S.J. Peron 		return (ENXIO);
14249b44ff22SGarrett Wollman 
142516d878ccSChristian S.J. Peron 	bp = theywant->if_bpf;
14264d621040SChristian S.J. Peron 
1427df8bae1dSRodney W. Grimes 	/*
14284d621040SChristian S.J. Peron 	 * Behavior here depends on the buffering model.  If we're using
14294d621040SChristian S.J. Peron 	 * kernel memory buffers, then we can allocate them here.  If we're
14304d621040SChristian S.J. Peron 	 * using zero-copy, then the user process must have registered
14314d621040SChristian S.J. Peron 	 * buffers by the time we get here.  If not, return an error.
14324d621040SChristian S.J. Peron 	 *
14334d621040SChristian S.J. Peron 	 * XXXRW: There are locking issues here with multi-threaded use: what
14344d621040SChristian S.J. Peron 	 * if two threads try to set the interface at once?
1435df8bae1dSRodney W. Grimes 	 */
14364d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
14374d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1438a3594432SRobert Watson 		if (d->bd_sbuf == NULL)
14394d621040SChristian S.J. Peron 			bpf_buffer_alloc(d);
14404d621040SChristian S.J. Peron 		KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL"));
14414d621040SChristian S.J. Peron 		break;
14424d621040SChristian S.J. Peron 
14434d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
14444d621040SChristian S.J. Peron 		if (d->bd_sbuf == NULL)
14454d621040SChristian S.J. Peron 			return (EINVAL);
14464d621040SChristian S.J. Peron 		break;
14474d621040SChristian S.J. Peron 
14484d621040SChristian S.J. Peron 	default:
14494d621040SChristian S.J. Peron 		panic("bpf_setif: bufmode %d", d->bd_bufmode);
14504d621040SChristian S.J. Peron 	}
1451df8bae1dSRodney W. Grimes 	if (bp != d->bd_bif) {
1452df8bae1dSRodney W. Grimes 		if (d->bd_bif)
1453df8bae1dSRodney W. Grimes 			/*
1454df8bae1dSRodney W. Grimes 			 * Detach if attached to something else.
1455df8bae1dSRodney W. Grimes 			 */
1456df8bae1dSRodney W. Grimes 			bpf_detachd(d);
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 		bpf_attachd(d, bp);
1459df8bae1dSRodney W. Grimes 	}
1460e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1461df8bae1dSRodney W. Grimes 	reset_d(d);
1462e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1463df8bae1dSRodney W. Grimes 	return (0);
1464df8bae1dSRodney W. Grimes }
1465df8bae1dSRodney W. Grimes 
1466df8bae1dSRodney W. Grimes /*
1467243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1468df8bae1dSRodney W. Grimes  *
1469df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1470df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1471df8bae1dSRodney W. Grimes  */
147237c84183SPoul-Henning Kamp static int
147319ba8395SChristian S.J. Peron bpfpoll(struct cdev *dev, int events, struct thread *td)
1474df8bae1dSRodney W. Grimes {
1475e7bb21b3SJonathan Lemon 	struct bpf_d *d;
14760832fc64SGarance A Drosehn 	int revents;
1477df8bae1dSRodney W. Grimes 
1478136600feSEd Schouten 	if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL)
1479136600feSEd Schouten 		return (events &
1480136600feSEd Schouten 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1481de5d9935SRobert Watson 
1482b75a24a0SChristian S.J. Peron 	/*
1483b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1484b75a24a0SChristian S.J. Peron 	 */
14850832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1486e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1487cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
148875c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
148995aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1490243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
149181bda851SJohn Polstra 		else {
1492ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
149381bda851SJohn Polstra 			/* Start the read timeout if necessary. */
149481bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
149581bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
149681bda851SJohn Polstra 				    bpf_timed_out, d);
149781bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
149881bda851SJohn Polstra 			}
149981bda851SJohn Polstra 		}
150075c13541SPoul-Henning Kamp 	}
1501e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1502243ac7d8SPeter Wemm 	return (revents);
1503df8bae1dSRodney W. Grimes }
1504df8bae1dSRodney W. Grimes 
1505df8bae1dSRodney W. Grimes /*
150695aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
150795aab9ccSJohn-Mark Gurney  * reject all others.
150895aab9ccSJohn-Mark Gurney  */
150995aab9ccSJohn-Mark Gurney int
151019ba8395SChristian S.J. Peron bpfkqfilter(struct cdev *dev, struct knote *kn)
151195aab9ccSJohn-Mark Gurney {
1512136600feSEd Schouten 	struct bpf_d *d;
151395aab9ccSJohn-Mark Gurney 
1514136600feSEd Schouten 	if (devfs_get_cdevpriv((void **)&d) != 0 ||
1515136600feSEd Schouten 	    kn->kn_filter != EVFILT_READ)
151695aab9ccSJohn-Mark Gurney 		return (1);
151795aab9ccSJohn-Mark Gurney 
1518b75a24a0SChristian S.J. Peron 	/*
1519b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1520b75a24a0SChristian S.J. Peron 	 */
1521cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1522b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
152395aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
152495aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
15254b19419eSChristian S.J. Peron 	knlist_add(&d->bd_sel.si_note, kn, 1);
1526cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
152795aab9ccSJohn-Mark Gurney 
152895aab9ccSJohn-Mark Gurney 	return (0);
152995aab9ccSJohn-Mark Gurney }
153095aab9ccSJohn-Mark Gurney 
153195aab9ccSJohn-Mark Gurney static void
153219ba8395SChristian S.J. Peron filt_bpfdetach(struct knote *kn)
153395aab9ccSJohn-Mark Gurney {
153495aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
153595aab9ccSJohn-Mark Gurney 
1536ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
153795aab9ccSJohn-Mark Gurney }
153895aab9ccSJohn-Mark Gurney 
153995aab9ccSJohn-Mark Gurney static int
154019ba8395SChristian S.J. Peron filt_bpfread(struct knote *kn, long hint)
154195aab9ccSJohn-Mark Gurney {
154295aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
154395aab9ccSJohn-Mark Gurney 	int ready;
154495aab9ccSJohn-Mark Gurney 
154586c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
154695aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
154795aab9ccSJohn-Mark Gurney 	if (ready) {
154895aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
154995aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
155095aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
155195aab9ccSJohn-Mark Gurney 	}
155295aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
155395aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
155495aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
155595aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
155695aab9ccSJohn-Mark Gurney 	}
155795aab9ccSJohn-Mark Gurney 
155895aab9ccSJohn-Mark Gurney 	return (ready);
155995aab9ccSJohn-Mark Gurney }
156095aab9ccSJohn-Mark Gurney 
156195aab9ccSJohn-Mark Gurney /*
1562df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1563df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1564df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1565df8bae1dSRodney W. Grimes  * buffer.
1566df8bae1dSRodney W. Grimes  */
1567df8bae1dSRodney W. Grimes void
156819ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1569df8bae1dSRodney W. Grimes {
15708994a245SDag-Erling Smørgrav 	struct bpf_d *d;
15718994a245SDag-Erling Smørgrav 	u_int slen;
157291433904SDavid Malone 	int gottime;
157391433904SDavid Malone 	struct timeval tv;
1574e7bb21b3SJonathan Lemon 
157591433904SDavid Malone 	gottime = 0;
1576e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
15774a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1578e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1579df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1580a05cf8c6SChristian S.J. Peron 		/*
1581a05cf8c6SChristian S.J. Peron 		 * NB: We dont call BPF_CHECK_DIRECTION() here since there is no
1582a05cf8c6SChristian S.J. Peron 		 * way for the caller to indiciate to us whether this packet
1583a05cf8c6SChristian S.J. Peron 		 * is inbound or outbound.  In the bpf_mtap() routines, we use
1584a05cf8c6SChristian S.J. Peron 		 * the interface pointers on the mbuf to figure it out.
1585a05cf8c6SChristian S.J. Peron 		 */
1586ae275efcSJung-uk Kim #ifdef BPF_JITTER
1587ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1588ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1589ae275efcSJung-uk Kim 		else
1590ae275efcSJung-uk Kim #endif
159193e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1592ec272d87SRobert Watson 		if (slen != 0) {
159369f7644bSChristian S.J. Peron 			d->bd_fcount++;
159491433904SDavid Malone 			if (!gottime) {
159591433904SDavid Malone 				microtime(&tv);
159691433904SDavid Malone 				gottime = 1;
159791433904SDavid Malone 			}
1598ec272d87SRobert Watson #ifdef MAC
159930d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1600ec272d87SRobert Watson #endif
16014d621040SChristian S.J. Peron 				catchpacket(d, pkt, pktlen, slen,
16024d621040SChristian S.J. Peron 				    bpf_append_bytes, &tv);
1603ec272d87SRobert Watson 		}
1604e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1605df8bae1dSRodney W. Grimes 	}
1606e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1607df8bae1dSRodney W. Grimes }
1608df8bae1dSRodney W. Grimes 
1609f81a2a49SJung-uk Kim #define	BPF_CHECK_DIRECTION(d, r, i)				\
1610f81a2a49SJung-uk Kim 	    (((d)->bd_direction == BPF_D_IN && (r) != (i)) ||	\
1611f81a2a49SJung-uk Kim 	    ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
1612560a54e1SJung-uk Kim 
1613df8bae1dSRodney W. Grimes /*
1614df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1615df8bae1dSRodney W. Grimes  */
1616df8bae1dSRodney W. Grimes void
161719ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1618df8bae1dSRodney W. Grimes {
1619df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1620df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
162191433904SDavid Malone 	int gottime;
162291433904SDavid Malone 	struct timeval tv;
162391433904SDavid Malone 
16248cd892f7SJung-uk Kim 	/* Skip outgoing duplicate packets. */
16258cd892f7SJung-uk Kim 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
16268cd892f7SJung-uk Kim 		m->m_flags &= ~M_PROMISC;
16278cd892f7SJung-uk Kim 		return;
16288cd892f7SJung-uk Kim 	}
16298cd892f7SJung-uk Kim 
163091433904SDavid Malone 	gottime = 0;
1631df8bae1dSRodney W. Grimes 
1632f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1633df8bae1dSRodney W. Grimes 
1634e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
16354a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1636f81a2a49SJung-uk Kim 		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
16378ed3828cSRobert Watson 			continue;
1638e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1639df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1640ae275efcSJung-uk Kim #ifdef BPF_JITTER
1641ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1642ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1643ae275efcSJung-uk Kim 		    m->m_next == NULL)
1644ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1645ae275efcSJung-uk Kim 			    pktlen, pktlen);
1646ae275efcSJung-uk Kim 		else
1647ae275efcSJung-uk Kim #endif
164893e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
16494ddfb531SChristian S.J. Peron 		if (slen != 0) {
165069f7644bSChristian S.J. Peron 			d->bd_fcount++;
165191433904SDavid Malone 			if (!gottime) {
165291433904SDavid Malone 				microtime(&tv);
165391433904SDavid Malone 				gottime = 1;
165491433904SDavid Malone 			}
16550c7fb534SRobert Watson #ifdef MAC
165630d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
16570c7fb534SRobert Watson #endif
16580c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
16594d621040SChristian S.J. Peron 				    bpf_append_mbuf, &tv);
16604ddfb531SChristian S.J. Peron 		}
1661e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1662df8bae1dSRodney W. Grimes 	}
1663e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1664df8bae1dSRodney W. Grimes }
1665df8bae1dSRodney W. Grimes 
1666df8bae1dSRodney W. Grimes /*
1667437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1668437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1669437ffe18SSam Leffler  */
1670437ffe18SSam Leffler void
167119ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1672437ffe18SSam Leffler {
1673437ffe18SSam Leffler 	struct mbuf mb;
1674437ffe18SSam Leffler 	struct bpf_d *d;
1675437ffe18SSam Leffler 	u_int pktlen, slen;
167691433904SDavid Malone 	int gottime;
167791433904SDavid Malone 	struct timeval tv;
167891433904SDavid Malone 
16798cd892f7SJung-uk Kim 	/* Skip outgoing duplicate packets. */
16808cd892f7SJung-uk Kim 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
16818cd892f7SJung-uk Kim 		m->m_flags &= ~M_PROMISC;
16828cd892f7SJung-uk Kim 		return;
16838cd892f7SJung-uk Kim 	}
16848cd892f7SJung-uk Kim 
168591433904SDavid Malone 	gottime = 0;
1686437ffe18SSam Leffler 
1687437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1688437ffe18SSam Leffler 	/*
1689437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1690437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1691437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1692437ffe18SSam Leffler 	 */
1693437ffe18SSam Leffler 	mb.m_next = m;
1694437ffe18SSam Leffler 	mb.m_data = data;
1695437ffe18SSam Leffler 	mb.m_len = dlen;
1696437ffe18SSam Leffler 	pktlen += dlen;
1697437ffe18SSam Leffler 
1698437ffe18SSam Leffler 	BPFIF_LOCK(bp);
16994a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1700f81a2a49SJung-uk Kim 		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1701437ffe18SSam Leffler 			continue;
1702437ffe18SSam Leffler 		BPFD_LOCK(d);
1703437ffe18SSam Leffler 		++d->bd_rcount;
170493e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
17054ddfb531SChristian S.J. Peron 		if (slen != 0) {
170669f7644bSChristian S.J. Peron 			d->bd_fcount++;
170791433904SDavid Malone 			if (!gottime) {
170891433904SDavid Malone 				microtime(&tv);
170991433904SDavid Malone 				gottime = 1;
171091433904SDavid Malone 			}
1711437ffe18SSam Leffler #ifdef MAC
171230d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1713437ffe18SSam Leffler #endif
1714437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
17154d621040SChristian S.J. Peron 				    bpf_append_mbuf, &tv);
17164ddfb531SChristian S.J. Peron 		}
1717437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1718437ffe18SSam Leffler 	}
1719437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1720437ffe18SSam Leffler }
1721437ffe18SSam Leffler 
1722560a54e1SJung-uk Kim #undef	BPF_CHECK_DIRECTION
1723560a54e1SJung-uk Kim 
1724437ffe18SSam Leffler /*
1725df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
17269e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1727df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
17284d621040SChristian S.J. Peron  * bpf_append_mbuf is passed in to copy mbuf chains.  In the latter case,
1729df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1730df8bae1dSRodney W. Grimes  */
1731df8bae1dSRodney W. Grimes static void
173219ba8395SChristian S.J. Peron catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
17334d621040SChristian S.J. Peron     void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
17344d621040SChristian S.J. Peron     struct timeval *tv)
1735df8bae1dSRodney W. Grimes {
17364d621040SChristian S.J. Peron 	struct bpf_hdr hdr;
17378994a245SDag-Erling Smørgrav 	int totlen, curlen;
17388994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
17397819da79SJohn-Mark Gurney 	int do_wakeup = 0;
17409e610888SDag-Erling Smørgrav 
1741a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
17424d621040SChristian S.J. Peron 
17434d621040SChristian S.J. Peron 	/*
17444d621040SChristian S.J. Peron 	 * Detect whether user space has released a buffer back to us, and if
17454d621040SChristian S.J. Peron 	 * so, move it from being a hold buffer to a free buffer.  This may
17464d621040SChristian S.J. Peron 	 * not be the best place to do it (for example, we might only want to
17474d621040SChristian S.J. Peron 	 * run this check if we need the space), but for now it's a reliable
17484d621040SChristian S.J. Peron 	 * spot to do it.
17494d621040SChristian S.J. Peron 	 */
1750fa0c2b34SRobert Watson 	if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
17514d621040SChristian S.J. Peron 		d->bd_fbuf = d->bd_hbuf;
17524d621040SChristian S.J. Peron 		d->bd_hbuf = NULL;
17534d621040SChristian S.J. Peron 		d->bd_hlen = 0;
175429f612ecSChristian S.J. Peron 		bpf_buf_reclaimed(d);
17554d621040SChristian S.J. Peron 	}
17564d621040SChristian S.J. Peron 
1757df8bae1dSRodney W. Grimes 	/*
1758df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1759df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1760df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1761df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1762df8bae1dSRodney W. Grimes 	 */
1763df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1764df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1765df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1766df8bae1dSRodney W. Grimes 
1767df8bae1dSRodney W. Grimes 	/*
1768df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1769a7a91e65SRobert Watson 	 *
1770a7a91e65SRobert Watson 	 * Drop the packet if there's no room and no hope of room
1771a7a91e65SRobert Watson 	 * If the packet would overflow the storage buffer or the storage
1772a7a91e65SRobert Watson 	 * buffer is considered immutable by the buffer model, try to rotate
1773a7a91e65SRobert Watson 	 * the buffer and wakeup pending processes.
1774df8bae1dSRodney W. Grimes 	 */
1775df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1776a7a91e65SRobert Watson 	if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
1777572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1778df8bae1dSRodney W. Grimes 			/*
1779a7a91e65SRobert Watson 			 * There's no room in the store buffer, and no
1780a7a91e65SRobert Watson 			 * prospect of room, so drop the packet.  Notify the
1781a7a91e65SRobert Watson 			 * buffer model.
1782df8bae1dSRodney W. Grimes 			 */
1783a7a91e65SRobert Watson 			bpf_buffull(d);
1784df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1785df8bae1dSRodney W. Grimes 			return;
1786df8bae1dSRodney W. Grimes 		}
1787df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
17887819da79SJohn-Mark Gurney 		do_wakeup = 1;
1789df8bae1dSRodney W. Grimes 		curlen = 0;
1790a7a91e65SRobert Watson 	} else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1791df8bae1dSRodney W. Grimes 		/*
17924d621040SChristian S.J. Peron 		 * Immediate mode is set, or the read timeout has already
17934d621040SChristian S.J. Peron 		 * expired during a select call.  A packet arrived, so the
17944d621040SChristian S.J. Peron 		 * reader should be woken up.
1795df8bae1dSRodney W. Grimes 		 */
17967819da79SJohn-Mark Gurney 		do_wakeup = 1;
1797df8bae1dSRodney W. Grimes 
1798df8bae1dSRodney W. Grimes 	/*
17994d621040SChristian S.J. Peron 	 * Append the bpf header.  Note we append the actual header size, but
18004d621040SChristian S.J. Peron 	 * move forward the length of the header plus padding.
1801df8bae1dSRodney W. Grimes 	 */
18024d621040SChristian S.J. Peron 	bzero(&hdr, sizeof(hdr));
18034d621040SChristian S.J. Peron 	hdr.bh_tstamp = *tv;
18044d621040SChristian S.J. Peron 	hdr.bh_datalen = pktlen;
18054d621040SChristian S.J. Peron 	hdr.bh_hdrlen = hdrlen;
18064d621040SChristian S.J. Peron 	hdr.bh_caplen = totlen - hdrlen;
18074d621040SChristian S.J. Peron 	bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
18084d621040SChristian S.J. Peron 
1809df8bae1dSRodney W. Grimes 	/*
1810df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1811df8bae1dSRodney W. Grimes 	 */
18124d621040SChristian S.J. Peron 	(*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, hdr.bh_caplen);
1813df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
18147819da79SJohn-Mark Gurney 
18157819da79SJohn-Mark Gurney 	if (do_wakeup)
18167819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1817df8bae1dSRodney W. Grimes }
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes /*
1820df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1821df8bae1dSRodney W. Grimes  * Called on close.
1822df8bae1dSRodney W. Grimes  */
1823df8bae1dSRodney W. Grimes static void
182419ba8395SChristian S.J. Peron bpf_freed(struct bpf_d *d)
1825df8bae1dSRodney W. Grimes {
18264d621040SChristian S.J. Peron 
1827df8bae1dSRodney W. Grimes 	/*
1828df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1829df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1830df8bae1dSRodney W. Grimes 	 * free.
1831df8bae1dSRodney W. Grimes 	 */
18324d621040SChristian S.J. Peron 	bpf_free(d);
1833ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
183493e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1835ae275efcSJung-uk Kim #ifdef BPF_JITTER
1836ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1837ae275efcSJung-uk Kim #endif
1838ae275efcSJung-uk Kim 	}
183993e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
184093e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1841e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1842df8bae1dSRodney W. Grimes }
1843df8bae1dSRodney W. Grimes 
1844df8bae1dSRodney W. Grimes /*
184524a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
184624a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1847df8bae1dSRodney W. Grimes  */
1848df8bae1dSRodney W. Grimes void
184919ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1850df8bae1dSRodney W. Grimes {
185124a229f4SSam Leffler 
185224a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
185324a229f4SSam Leffler }
185424a229f4SSam Leffler 
185524a229f4SSam Leffler /*
185624a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
185724a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
185824a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
185924a229f4SSam Leffler  * headers are not yet supporrted).
186024a229f4SSam Leffler  */
186124a229f4SSam Leffler void
186219ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
186324a229f4SSam Leffler {
1864df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
186519ba8395SChristian S.J. Peron 
186619ba8395SChristian S.J. Peron 	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1867572bde2aSRobert Watson 	if (bp == NULL)
1868df8bae1dSRodney W. Grimes 		panic("bpfattach");
1869df8bae1dSRodney W. Grimes 
18704a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
1871df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1872df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
18736008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
187416d878ccSChristian S.J. Peron 	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
187516d878ccSChristian S.J. Peron 	*driverp = bp;
1876df8bae1dSRodney W. Grimes 
1877e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
18784a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1879e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1880df8bae1dSRodney W. Grimes 
1881df8bae1dSRodney W. Grimes 	/*
1882df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1883df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1884df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1885df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1886df8bae1dSRodney W. Grimes 	 */
1887df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1888df8bae1dSRodney W. Grimes 
18892eeab939SGarrett Wollman 	if (bootverbose)
189024a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1891df8bae1dSRodney W. Grimes }
189253ac6efbSJulian Elischer 
1893de5d9935SRobert Watson /*
1894de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1895de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1896de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1897de5d9935SRobert Watson  * ENXIO.
1898de5d9935SRobert Watson  */
1899de5d9935SRobert Watson void
190019ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
1901de5d9935SRobert Watson {
19024a3feeaaSRobert Watson 	struct bpf_if	*bp;
1903de5d9935SRobert Watson 	struct bpf_d	*d;
1904de5d9935SRobert Watson 
1905de5d9935SRobert Watson 	/* Locate BPF interface information */
19068eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
19074a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1908de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1909de5d9935SRobert Watson 			break;
1910de5d9935SRobert Watson 	}
1911de5d9935SRobert Watson 
1912de5d9935SRobert Watson 	/* Interface wasn't attached */
1913d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1914e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
19159bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1916de5d9935SRobert Watson 		return;
1917de5d9935SRobert Watson 	}
1918de5d9935SRobert Watson 
19194a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
19208eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1921de5d9935SRobert Watson 
19224a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1923e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1924e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1925e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1926e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1927e7bb21b3SJonathan Lemon 	}
1928e7bb21b3SJonathan Lemon 
1929e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1930de5d9935SRobert Watson 	free(bp, M_BPF);
19318eab61f3SSam Leffler }
1932de5d9935SRobert Watson 
19338eab61f3SSam Leffler /*
19348eab61f3SSam Leffler  * Get a list of available data link type of the interface.
19358eab61f3SSam Leffler  */
19368eab61f3SSam Leffler static int
193719ba8395SChristian S.J. Peron bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
19388eab61f3SSam Leffler {
19398eab61f3SSam Leffler 	int n, error;
19408eab61f3SSam Leffler 	struct ifnet *ifp;
19418eab61f3SSam Leffler 	struct bpf_if *bp;
19428eab61f3SSam Leffler 
19438eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
19448eab61f3SSam Leffler 	n = 0;
19458eab61f3SSam Leffler 	error = 0;
19468eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
19474a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
19488eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
19498eab61f3SSam Leffler 			continue;
19508eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
19518eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1952e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
19538eab61f3SSam Leffler 				return (ENOMEM);
19548eab61f3SSam Leffler 			}
19558eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
19568eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
19578eab61f3SSam Leffler 		}
19588eab61f3SSam Leffler 		n++;
19598eab61f3SSam Leffler 	}
19608eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
19618eab61f3SSam Leffler 	bfl->bfl_len = n;
19628eab61f3SSam Leffler 	return (error);
19638eab61f3SSam Leffler }
19648eab61f3SSam Leffler 
19658eab61f3SSam Leffler /*
19668eab61f3SSam Leffler  * Set the data link type of a BPF instance.
19678eab61f3SSam Leffler  */
19688eab61f3SSam Leffler static int
196919ba8395SChristian S.J. Peron bpf_setdlt(struct bpf_d *d, u_int dlt)
19708eab61f3SSam Leffler {
19718eab61f3SSam Leffler 	int error, opromisc;
19728eab61f3SSam Leffler 	struct ifnet *ifp;
19738eab61f3SSam Leffler 	struct bpf_if *bp;
19748eab61f3SSam Leffler 
19758eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
19768eab61f3SSam Leffler 		return (0);
19778eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
19788eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
19794a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
19808eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
19818eab61f3SSam Leffler 			break;
19828eab61f3SSam Leffler 	}
19838eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
19848eab61f3SSam Leffler 	if (bp != NULL) {
19858eab61f3SSam Leffler 		opromisc = d->bd_promisc;
19868eab61f3SSam Leffler 		bpf_detachd(d);
19878eab61f3SSam Leffler 		bpf_attachd(d, bp);
198893daabddSBrian Feldman 		BPFD_LOCK(d);
19898eab61f3SSam Leffler 		reset_d(d);
19908eab61f3SSam Leffler 		BPFD_UNLOCK(d);
19918eab61f3SSam Leffler 		if (opromisc) {
19928eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
19938eab61f3SSam Leffler 			if (error)
19948eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
19958eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
19968eab61f3SSam Leffler 					error);
19978eab61f3SSam Leffler 			else
19988eab61f3SSam Leffler 				d->bd_promisc = 1;
19998eab61f3SSam Leffler 		}
20008eab61f3SSam Leffler 	}
20018eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
2002de5d9935SRobert Watson }
2003de5d9935SRobert Watson 
20043f54a085SPoul-Henning Kamp static void
200519ba8395SChristian S.J. Peron bpf_drvinit(void *unused)
200653ac6efbSJulian Elischer {
2007136600feSEd Schouten 	struct cdev *dev;
200853ac6efbSJulian Elischer 
20096008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
20104a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
2011136600feSEd Schouten 
2012136600feSEd Schouten 	dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf");
2013136600feSEd Schouten 	/* For compatibility */
2014136600feSEd Schouten 	make_dev_alias(dev, "bpf0");
2015136600feSEd Schouten 
20167198bf47SJulian Elischer }
201753ac6efbSJulian Elischer 
201869f7644bSChristian S.J. Peron static void
201969f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
202069f7644bSChristian S.J. Peron {
202169f7644bSChristian S.J. Peron 
202269f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
202369f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
20244d621040SChristian S.J. Peron 	d->bd_structsize = sizeof(*d);
202569f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
202669f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
202769f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
2028560a54e1SJung-uk Kim 	d->bd_direction = bd->bd_direction;
2029560a54e1SJung-uk Kim 	d->bd_feedback = bd->bd_feedback;
203069f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
203169f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
203269f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
203369f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
203469f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
203569f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
203669f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
203769f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
203869f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
203969f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
204069f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
204193e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
20424d621040SChristian S.J. Peron 	d->bd_wcount = bd->bd_wcount;
20434d621040SChristian S.J. Peron 	d->bd_wdcount = bd->bd_wdcount;
20444d621040SChristian S.J. Peron 	d->bd_wfcount = bd->bd_wfcount;
20454d621040SChristian S.J. Peron 	d->bd_zcopy = bd->bd_zcopy;
20464d621040SChristian S.J. Peron 	d->bd_bufmode = bd->bd_bufmode;
204769f7644bSChristian S.J. Peron }
204869f7644bSChristian S.J. Peron 
204969f7644bSChristian S.J. Peron static int
205069f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
205169f7644bSChristian S.J. Peron {
2052422a63daSChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd;
2053422a63daSChristian S.J. Peron 	int index, error;
205469f7644bSChristian S.J. Peron 	struct bpf_if *bp;
205569f7644bSChristian S.J. Peron 	struct bpf_d *bd;
205669f7644bSChristian S.J. Peron 
205769f7644bSChristian S.J. Peron 	/*
205869f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
205969f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
206069f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
206169f7644bSChristian S.J. Peron 	 * the statistics for them, too.
206269f7644bSChristian S.J. Peron 	 */
2063acd3428bSRobert Watson 	error = priv_check(req->td, PRIV_NET_BPF);
206469f7644bSChristian S.J. Peron 	if (error)
206569f7644bSChristian S.J. Peron 		return (error);
206669f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
2067422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
206869f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
206969f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
2070422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
207169f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
2072422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2073422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
2074422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
2075422a63daSChristian S.J. Peron 		return (ENOMEM);
2076422a63daSChristian S.J. Peron 	}
2077422a63daSChristian S.J. Peron 	index = 0;
207869f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
20791fc9e387SChristian S.J. Peron 		BPFIF_LOCK(bp);
208069f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2081422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
208269f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
2083422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
208469f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
208569f7644bSChristian S.J. Peron 		}
20861fc9e387SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
208769f7644bSChristian S.J. Peron 	}
208869f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
2089422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2090422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
209169f7644bSChristian S.J. Peron 	return (error);
209269f7644bSChristian S.J. Peron }
209369f7644bSChristian S.J. Peron 
2094237fdd78SRobert Watson SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
209553ac6efbSJulian Elischer 
20965bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
2097f8dc4716SMike Smith /*
2098f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
2099f8dc4716SMike Smith  *
2100f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
2101f8dc4716SMike Smith  * to be loaded at runtime.
2102f8dc4716SMike Smith  */
21037eae78a4SChristian S.J. Peron static struct bpf_if bp_null;
2104f8dc4716SMike Smith 
2105f8dc4716SMike Smith void
210619ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2107f8dc4716SMike Smith {
2108f8dc4716SMike Smith }
2109f8dc4716SMike Smith 
2110f8dc4716SMike Smith void
211119ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2112f8dc4716SMike Smith {
2113f8dc4716SMike Smith }
2114f8dc4716SMike Smith 
2115f8dc4716SMike Smith void
211619ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
2117437ffe18SSam Leffler {
2118437ffe18SSam Leffler }
2119437ffe18SSam Leffler 
2120437ffe18SSam Leffler void
212119ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2122f8dc4716SMike Smith {
21237eae78a4SChristian S.J. Peron 
21247eae78a4SChristian S.J. Peron 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2125f8dc4716SMike Smith }
2126f8dc4716SMike Smith 
2127da626c17SBill Paul void
212819ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
21295f7a7923SSam Leffler {
21307eae78a4SChristian S.J. Peron 
21317eae78a4SChristian S.J. Peron 	*driverp = &bp_null;
21325f7a7923SSam Leffler }
21335f7a7923SSam Leffler 
21345f7a7923SSam Leffler void
213519ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
2136da626c17SBill Paul {
2137da626c17SBill Paul }
2138da626c17SBill Paul 
2139f8dc4716SMike Smith u_int
214019ba8395SChristian S.J. Peron bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
2141f8dc4716SMike Smith {
2142f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
2143f8dc4716SMike Smith }
2144f8dc4716SMike Smith 
21455bb5f2c9SPeter Wemm int
214619ba8395SChristian S.J. Peron bpf_validate(const struct bpf_insn *f, int len)
21475bb5f2c9SPeter Wemm {
21485bb5f2c9SPeter Wemm 	return 0;		/* false */
21495bb5f2c9SPeter Wemm }
21505bb5f2c9SPeter Wemm 
21515bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
2152