xref: /freebsd/sys/net/bpf.c (revision 8df67d77ed90f6ef486781fbf707271c768595a0)
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"
415bb5f2c9SPeter Wemm #include "opt_netgraph.h"
42df8bae1dSRodney W. Grimes 
4395aab9ccSJohn-Mark Gurney #include <sys/types.h>
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46ce7609a4SBruce Evans #include <sys/conf.h>
47e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
48ebd8672cSBjoern A. Zeeb #include <sys/jail.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>
75530c0060SRobert Watson #include <net/vnet.h>
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes #include <netinet/in.h>
78df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
79df8bae1dSRodney W. Grimes #include <sys/kernel.h>
80f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
817b778b5eSEivind Eklund 
82246b5467SSam Leffler #include <net80211/ieee80211_freebsd.h>
83246b5467SSam Leffler 
84aed55708SRobert Watson #include <security/mac/mac_framework.h>
85aed55708SRobert Watson 
864d621040SChristian S.J. Peron MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8787f6c662SJulian Elischer 
885bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8953ac6efbSJulian Elischer 
90df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
94d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
95d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
96d1a67300SRobert Watson  * frames, ethernet frames, etc).
97df8bae1dSRodney W. Grimes  */
984a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
99e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
10069f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
101df8bae1dSRodney W. Grimes 
10219ba8395SChristian S.J. Peron static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
10319ba8395SChristian S.J. Peron static void	bpf_detachd(struct bpf_d *);
104929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
105cb44b6dfSAndrew Thompson static int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
106560a54e1SJung-uk Kim 		    struct sockaddr *, int *, struct bpf_insn *);
107929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
108929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
109e7bb21b3SJonathan Lemon static __inline void
110929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
1114d621040SChristian S.J. Peron static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
1124d621040SChristian S.J. Peron 		    void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
11391433904SDavid Malone 		    struct timeval *);
114929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
11593e39f0bSChristian S.J. Peron static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
1168eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1178eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11895aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
11995aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
120a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
12169f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
12269f7644bSChristian S.J. Peron 
12369f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12412dc9582SJung-uk Kim int bpf_maxinsns = BPF_MAXINSNS;
12569f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
12669f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
127ffeeb924SChristian S.J. Peron static int bpf_zerocopy_enable = 0;
1284d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
1294d621040SChristian S.J. Peron     &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
13092709411SChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
13169f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
132df8bae1dSRodney W. Grimes 
13387f6c662SJulian Elischer static	d_open_t	bpfopen;
13487f6c662SJulian Elischer static	d_read_t	bpfread;
13587f6c662SJulian Elischer static	d_write_t	bpfwrite;
13687f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
137243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
13895aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
13987f6c662SJulian Elischer 
1404e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
141dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1427ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1437ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1447ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1457ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1467ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1477ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
14895aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1494e2f199eSPoul-Henning Kamp };
15087f6c662SJulian Elischer 
151e76d823bSRobert Watson static struct filterops bpfread_filtops = {
152e76d823bSRobert Watson 	.f_isfd = 1,
153e76d823bSRobert Watson 	.f_detach = filt_bpfdetach,
154e76d823bSRobert Watson 	.f_event = filt_bpfread,
155e76d823bSRobert Watson };
15687f6c662SJulian Elischer 
1574d621040SChristian S.J. Peron /*
1584d621040SChristian S.J. Peron  * Wrapper functions for various buffering methods.  If the set of buffer
1594d621040SChristian S.J. Peron  * modes expands, we will probably want to introduce a switch data structure
1604d621040SChristian S.J. Peron  * similar to protosw, et.
1614d621040SChristian S.J. Peron  */
1624d621040SChristian S.J. Peron static void
1634d621040SChristian S.J. Peron bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1644d621040SChristian S.J. Peron     u_int len)
1654d621040SChristian S.J. Peron {
1664d621040SChristian S.J. Peron 
1674d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1684d621040SChristian S.J. Peron 
1694d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
1704d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1714d621040SChristian S.J. Peron 		return (bpf_buffer_append_bytes(d, buf, offset, src, len));
1724d621040SChristian S.J. Peron 
1734d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
1744d621040SChristian S.J. Peron 		d->bd_zcopy++;
1754d621040SChristian S.J. Peron 		return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
1764d621040SChristian S.J. Peron 
1774d621040SChristian S.J. Peron 	default:
1784d621040SChristian S.J. Peron 		panic("bpf_buf_append_bytes");
1794d621040SChristian S.J. Peron 	}
1804d621040SChristian S.J. Peron }
1814d621040SChristian S.J. Peron 
1824d621040SChristian S.J. Peron static void
1834d621040SChristian S.J. Peron bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1844d621040SChristian S.J. Peron     u_int len)
1854d621040SChristian S.J. Peron {
1864d621040SChristian S.J. Peron 
1874d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1884d621040SChristian S.J. Peron 
1894d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
1904d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1914d621040SChristian S.J. Peron 		return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
1924d621040SChristian S.J. Peron 
1934d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
1944d621040SChristian S.J. Peron 		d->bd_zcopy++;
1954d621040SChristian S.J. Peron 		return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
1964d621040SChristian S.J. Peron 
1974d621040SChristian S.J. Peron 	default:
1984d621040SChristian S.J. Peron 		panic("bpf_buf_append_mbuf");
1994d621040SChristian S.J. Peron 	}
2004d621040SChristian S.J. Peron }
2014d621040SChristian S.J. Peron 
2024d621040SChristian S.J. Peron /*
20329f612ecSChristian S.J. Peron  * This function gets called when the free buffer is re-assigned.
20429f612ecSChristian S.J. Peron  */
20529f612ecSChristian S.J. Peron static void
20629f612ecSChristian S.J. Peron bpf_buf_reclaimed(struct bpf_d *d)
20729f612ecSChristian S.J. Peron {
20829f612ecSChristian S.J. Peron 
20929f612ecSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
21029f612ecSChristian S.J. Peron 
21129f612ecSChristian S.J. Peron 	switch (d->bd_bufmode) {
21229f612ecSChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
21329f612ecSChristian S.J. Peron 		return;
21429f612ecSChristian S.J. Peron 
21529f612ecSChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
21629f612ecSChristian S.J. Peron 		bpf_zerocopy_buf_reclaimed(d);
21729f612ecSChristian S.J. Peron 		return;
21829f612ecSChristian S.J. Peron 
21929f612ecSChristian S.J. Peron 	default:
22029f612ecSChristian S.J. Peron 		panic("bpf_buf_reclaimed");
22129f612ecSChristian S.J. Peron 	}
22229f612ecSChristian S.J. Peron }
22329f612ecSChristian S.J. Peron 
22429f612ecSChristian S.J. Peron /*
2254d621040SChristian S.J. Peron  * If the buffer mechanism has a way to decide that a held buffer can be made
2264d621040SChristian S.J. Peron  * free, then it is exposed via the bpf_canfreebuf() interface.  (1) is
2274d621040SChristian S.J. Peron  * returned if the buffer can be discarded, (0) is returned if it cannot.
2284d621040SChristian S.J. Peron  */
2294d621040SChristian S.J. Peron static int
2304d621040SChristian S.J. Peron bpf_canfreebuf(struct bpf_d *d)
2314d621040SChristian S.J. Peron {
2324d621040SChristian S.J. Peron 
2334d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
2344d621040SChristian S.J. Peron 
2354d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2364d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2374d621040SChristian S.J. Peron 		return (bpf_zerocopy_canfreebuf(d));
2384d621040SChristian S.J. Peron 	}
2394d621040SChristian S.J. Peron 	return (0);
2404d621040SChristian S.J. Peron }
2414d621040SChristian S.J. Peron 
242a7a91e65SRobert Watson /*
243a7a91e65SRobert Watson  * Allow the buffer model to indicate that the current store buffer is
244a7a91e65SRobert Watson  * immutable, regardless of the appearance of space.  Return (1) if the
245a7a91e65SRobert Watson  * buffer is writable, and (0) if not.
246a7a91e65SRobert Watson  */
247a7a91e65SRobert Watson static int
248a7a91e65SRobert Watson bpf_canwritebuf(struct bpf_d *d)
249a7a91e65SRobert Watson {
250a7a91e65SRobert Watson 
251a7a91e65SRobert Watson 	BPFD_LOCK_ASSERT(d);
252a7a91e65SRobert Watson 
253a7a91e65SRobert Watson 	switch (d->bd_bufmode) {
254a7a91e65SRobert Watson 	case BPF_BUFMODE_ZBUF:
255a7a91e65SRobert Watson 		return (bpf_zerocopy_canwritebuf(d));
256a7a91e65SRobert Watson 	}
257a7a91e65SRobert Watson 	return (1);
258a7a91e65SRobert Watson }
259a7a91e65SRobert Watson 
260a7a91e65SRobert Watson /*
261a7a91e65SRobert Watson  * Notify buffer model that an attempt to write to the store buffer has
262a7a91e65SRobert Watson  * resulted in a dropped packet, in which case the buffer may be considered
263a7a91e65SRobert Watson  * full.
264a7a91e65SRobert Watson  */
265a7a91e65SRobert Watson static void
266a7a91e65SRobert Watson bpf_buffull(struct bpf_d *d)
267a7a91e65SRobert Watson {
268a7a91e65SRobert Watson 
269a7a91e65SRobert Watson 	BPFD_LOCK_ASSERT(d);
270a7a91e65SRobert Watson 
271a7a91e65SRobert Watson 	switch (d->bd_bufmode) {
272a7a91e65SRobert Watson 	case BPF_BUFMODE_ZBUF:
273a7a91e65SRobert Watson 		bpf_zerocopy_buffull(d);
274a7a91e65SRobert Watson 		break;
275a7a91e65SRobert Watson 	}
276a7a91e65SRobert Watson }
277a7a91e65SRobert Watson 
278a7a91e65SRobert Watson /*
279a7a91e65SRobert Watson  * Notify the buffer model that a buffer has moved into the hold position.
280a7a91e65SRobert Watson  */
2814d621040SChristian S.J. Peron void
2824d621040SChristian S.J. Peron bpf_bufheld(struct bpf_d *d)
2834d621040SChristian S.J. Peron {
2844d621040SChristian S.J. Peron 
2854d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
2864d621040SChristian S.J. Peron 
2874d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2884d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2894d621040SChristian S.J. Peron 		bpf_zerocopy_bufheld(d);
2904d621040SChristian S.J. Peron 		break;
2914d621040SChristian S.J. Peron 	}
2924d621040SChristian S.J. Peron }
2934d621040SChristian S.J. Peron 
2944d621040SChristian S.J. Peron static void
2954d621040SChristian S.J. Peron bpf_free(struct bpf_d *d)
2964d621040SChristian S.J. Peron {
2974d621040SChristian S.J. Peron 
2984d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2994d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
3004d621040SChristian S.J. Peron 		return (bpf_buffer_free(d));
3014d621040SChristian S.J. Peron 
3024d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
3034d621040SChristian S.J. Peron 		return (bpf_zerocopy_free(d));
3044d621040SChristian S.J. Peron 
3054d621040SChristian S.J. Peron 	default:
3064d621040SChristian S.J. Peron 		panic("bpf_buf_free");
3074d621040SChristian S.J. Peron 	}
3084d621040SChristian S.J. Peron }
3094d621040SChristian S.J. Peron 
3104d621040SChristian S.J. Peron static int
3114d621040SChristian S.J. Peron bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
3124d621040SChristian S.J. Peron {
3134d621040SChristian S.J. Peron 
3144d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
3154d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3164d621040SChristian S.J. Peron 	return (bpf_buffer_uiomove(d, buf, len, uio));
3174d621040SChristian S.J. Peron }
3184d621040SChristian S.J. Peron 
3194d621040SChristian S.J. Peron static int
3204d621040SChristian S.J. Peron bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
3214d621040SChristian S.J. Peron {
3224d621040SChristian S.J. Peron 
3234d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
3244d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3254d621040SChristian S.J. Peron 	return (bpf_buffer_ioctl_sblen(d, i));
3264d621040SChristian S.J. Peron }
3274d621040SChristian S.J. Peron 
3284d621040SChristian S.J. Peron static int
3294d621040SChristian S.J. Peron bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
3304d621040SChristian S.J. Peron {
3314d621040SChristian S.J. Peron 
3324d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
3334d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3344d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_getzmax(td, d, i));
3354d621040SChristian S.J. Peron }
3364d621040SChristian S.J. Peron 
3374d621040SChristian S.J. Peron static int
3384d621040SChristian S.J. Peron bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
3394d621040SChristian S.J. Peron {
3404d621040SChristian S.J. Peron 
3414d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
3424d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3434d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
3444d621040SChristian S.J. Peron }
3454d621040SChristian S.J. Peron 
3464d621040SChristian S.J. Peron static int
3474d621040SChristian S.J. Peron bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
3484d621040SChristian S.J. Peron {
3494d621040SChristian S.J. Peron 
3504d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
3514d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
3524d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
3534d621040SChristian S.J. Peron }
3544d621040SChristian S.J. Peron 
3554d621040SChristian S.J. Peron /*
3564d621040SChristian S.J. Peron  * General BPF functions.
3574d621040SChristian S.J. Peron  */
358df8bae1dSRodney W. Grimes static int
359cb44b6dfSAndrew Thompson bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
360560a54e1SJung-uk Kim     struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
361df8bae1dSRodney W. Grimes {
362246b5467SSam Leffler 	const struct ieee80211_bpf_params *p;
363cb44b6dfSAndrew Thompson 	struct ether_header *eh;
364df8bae1dSRodney W. Grimes 	struct mbuf *m;
365df8bae1dSRodney W. Grimes 	int error;
366df8bae1dSRodney W. Grimes 	int len;
367df8bae1dSRodney W. Grimes 	int hlen;
36893e39f0bSChristian S.J. Peron 	int slen;
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	/*
371df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
372df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
373df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
374df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
375df8bae1dSRodney W. Grimes 	 * is forwarded as is.
376df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
377df8bae1dSRodney W. Grimes 	 * for the link level header.
378df8bae1dSRodney W. Grimes 	 */
379df8bae1dSRodney W. Grimes 	switch (linktype) {
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes 	case DLT_SLIP:
382df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
383df8bae1dSRodney W. Grimes 		hlen = 0;
384df8bae1dSRodney W. Grimes 		break;
385df8bae1dSRodney W. Grimes 
386df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
387df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
388df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
389797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
390df8bae1dSRodney W. Grimes 		break;
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes 	case DLT_FDDI:
393d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
394d41f24e7SDavid Greenman 		hlen = 0;
395df8bae1dSRodney W. Grimes 		break;
396df8bae1dSRodney W. Grimes 
39722f05c43SAndrey A. Chernov 	case DLT_RAW:
398df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
399df8bae1dSRodney W. Grimes 		hlen = 0;
400df8bae1dSRodney W. Grimes 		break;
401df8bae1dSRodney W. Grimes 
40201399f34SDavid Malone 	case DLT_NULL:
40301399f34SDavid Malone 		/*
40401399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
40501399f34SDavid Malone 		 * corresponds to the address family of the packet.
40601399f34SDavid Malone 		 */
40701399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
40801399f34SDavid Malone 		hlen = 4;
40901399f34SDavid Malone 		break;
41001399f34SDavid Malone 
4114f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
4124f53e3ccSKenjiro Cho 		/*
4134f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
4144f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
4154f53e3ccSKenjiro Cho 		 * specified anyway.
4164f53e3ccSKenjiro Cho 		 */
4174f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
4184f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
4194f53e3ccSKenjiro Cho 		break;
4204f53e3ccSKenjiro Cho 
42130fa52a6SBrian Somers 	case DLT_PPP:
42230fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
42330fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
42430fa52a6SBrian Somers 		break;
42530fa52a6SBrian Somers 
426246b5467SSam Leffler 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
427246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
428246b5467SSam Leffler 		hlen = 0;
429246b5467SSam Leffler 		break;
430246b5467SSam Leffler 
431246b5467SSam Leffler 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
432246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
433246b5467SSam Leffler 		sockp->sa_len = 12;	/* XXX != 0 */
434246b5467SSam Leffler 		hlen = sizeof(struct ieee80211_bpf_params);
435246b5467SSam Leffler 		break;
436246b5467SSam Leffler 
437df8bae1dSRodney W. Grimes 	default:
438df8bae1dSRodney W. Grimes 		return (EIO);
439df8bae1dSRodney W. Grimes 	}
440df8bae1dSRodney W. Grimes 
441df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
44201399f34SDavid Malone 
443cb44b6dfSAndrew Thompson 	if (len - hlen > ifp->if_mtu)
44401399f34SDavid Malone 		return (EMSGSIZE);
44501399f34SDavid Malone 
446968c88bcSJung-uk Kim 	if ((unsigned)len > MJUM16BYTES)
447df8bae1dSRodney W. Grimes 		return (EIO);
448df8bae1dSRodney W. Grimes 
449968c88bcSJung-uk Kim 	if (len <= MHLEN)
450968c88bcSJung-uk Kim 		MGETHDR(m, M_WAIT, MT_DATA);
451968c88bcSJung-uk Kim 	else if (len <= MCLBYTES)
452ea26d587SRuslan Ermilov 		m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
453ea26d587SRuslan Ermilov 	else
454968c88bcSJung-uk Kim 		m = m_getjcl(M_WAIT, MT_DATA, M_PKTHDR,
455968c88bcSJung-uk Kim #if (MJUMPAGESIZE > MCLBYTES)
456968c88bcSJung-uk Kim 		    len <= MJUMPAGESIZE ? MJUMPAGESIZE :
457968c88bcSJung-uk Kim #endif
458968c88bcSJung-uk Kim 		    (len <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES));
459963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
460963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
461df8bae1dSRodney W. Grimes 	*mp = m;
46224a229f4SSam Leffler 
46393e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
46493e39f0bSChristian S.J. Peron 		error = EPERM;
46593e39f0bSChristian S.J. Peron 		goto bad;
46693e39f0bSChristian S.J. Peron 	}
46793e39f0bSChristian S.J. Peron 
46893e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
46993e39f0bSChristian S.J. Peron 	if (error)
47093e39f0bSChristian S.J. Peron 		goto bad;
47193e39f0bSChristian S.J. Peron 
47293e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
47393e39f0bSChristian S.J. Peron 	if (slen == 0) {
47493e39f0bSChristian S.J. Peron 		error = EPERM;
47593e39f0bSChristian S.J. Peron 		goto bad;
47693e39f0bSChristian S.J. Peron 	}
47793e39f0bSChristian S.J. Peron 
478cb44b6dfSAndrew Thompson 	/* Check for multicast destination */
479cb44b6dfSAndrew Thompson 	switch (linktype) {
480cb44b6dfSAndrew Thompson 	case DLT_EN10MB:
481cb44b6dfSAndrew Thompson 		eh = mtod(m, struct ether_header *);
482cb44b6dfSAndrew Thompson 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
483cb44b6dfSAndrew Thompson 			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
484cb44b6dfSAndrew Thompson 			    ETHER_ADDR_LEN) == 0)
485cb44b6dfSAndrew Thompson 				m->m_flags |= M_BCAST;
486cb44b6dfSAndrew Thompson 			else
487cb44b6dfSAndrew Thompson 				m->m_flags |= M_MCAST;
488cb44b6dfSAndrew Thompson 		}
489cb44b6dfSAndrew Thompson 		break;
490cb44b6dfSAndrew Thompson 	}
491cb44b6dfSAndrew Thompson 
492df8bae1dSRodney W. Grimes 	/*
49393e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
494df8bae1dSRodney W. Grimes 	 */
495df8bae1dSRodney W. Grimes 	if (hlen != 0) {
496246b5467SSam Leffler 		if (sockp->sa_family == AF_IEEE80211) {
497246b5467SSam Leffler 			/*
498246b5467SSam Leffler 			 * Collect true length from the parameter header
499246b5467SSam Leffler 			 * NB: sockp is known to be zero'd so if we do a
500246b5467SSam Leffler 			 *     short copy unspecified parameters will be
501246b5467SSam Leffler 			 *     zero.
502246b5467SSam Leffler 			 * NB: packet may not be aligned after stripping
503246b5467SSam Leffler 			 *     bpf params
504246b5467SSam Leffler 			 * XXX check ibp_vers
505246b5467SSam Leffler 			 */
506246b5467SSam Leffler 			p = mtod(m, const struct ieee80211_bpf_params *);
507246b5467SSam Leffler 			hlen = p->ibp_len;
508246b5467SSam Leffler 			if (hlen > sizeof(sockp->sa_data)) {
509246b5467SSam Leffler 				error = EINVAL;
510246b5467SSam Leffler 				goto bad;
511246b5467SSam Leffler 			}
512246b5467SSam Leffler 		}
51393e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
514df8bae1dSRodney W. Grimes 	}
515560a54e1SJung-uk Kim 	*hdrlen = hlen;
51693e39f0bSChristian S.J. Peron 
517df8bae1dSRodney W. Grimes 	return (0);
518df8bae1dSRodney W. Grimes bad:
519df8bae1dSRodney W. Grimes 	m_freem(m);
520df8bae1dSRodney W. Grimes 	return (error);
521df8bae1dSRodney W. Grimes }
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes /*
524df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
525df8bae1dSRodney W. Grimes  */
526df8bae1dSRodney W. Grimes static void
52719ba8395SChristian S.J. Peron bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
528df8bae1dSRodney W. Grimes {
529df8bae1dSRodney W. Grimes 	/*
530df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
531df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
532df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
533df8bae1dSRodney W. Grimes 	 */
534e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
535df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
5364a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
537df8bae1dSRodney W. Grimes 
53869f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
539e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
540b743c310SSam Leffler 
5415ce8d970SSam Leffler 	EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
542df8bae1dSRodney W. Grimes }
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes /*
545df8bae1dSRodney W. Grimes  * Detach a file from its interface.
546df8bae1dSRodney W. Grimes  */
547df8bae1dSRodney W. Grimes static void
54819ba8395SChristian S.J. Peron bpf_detachd(struct bpf_d *d)
549df8bae1dSRodney W. Grimes {
5506e891d64SPoul-Henning Kamp 	int error;
551df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
55246448b5aSRobert Watson 	struct ifnet *ifp;
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
55546448b5aSRobert Watson 	BPFIF_LOCK(bp);
55646448b5aSRobert Watson 	BPFD_LOCK(d);
55746448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
55846448b5aSRobert Watson 
55946448b5aSRobert Watson 	/*
56046448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
56146448b5aSRobert Watson 	 */
56246448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
56346448b5aSRobert Watson 
56469f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
565572bde2aSRobert Watson 	d->bd_bif = NULL;
56646448b5aSRobert Watson 	BPFD_UNLOCK(d);
56746448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
56846448b5aSRobert Watson 
5695ce8d970SSam Leffler 	EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0);
570b743c310SSam Leffler 
571df8bae1dSRodney W. Grimes 	/*
572df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
573df8bae1dSRodney W. Grimes 	 * If so, turn it off.
574df8bae1dSRodney W. Grimes 	 */
575df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
576df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
57797021c24SMarko Zec 		CURVNET_SET(ifp->if_vnet);
57846448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
57997021c24SMarko Zec 		CURVNET_RESTORE();
5806e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
581df8bae1dSRodney W. Grimes 			/*
5826e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
583df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
584df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
585df8bae1dSRodney W. Grimes 			 * take it out.
586df8bae1dSRodney W. Grimes 			 */
5878eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
5888eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
5896e891d64SPoul-Henning Kamp 		}
590df8bae1dSRodney W. Grimes 	}
591df8bae1dSRodney W. Grimes }
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes /*
594136600feSEd Schouten  * Close the descriptor by detaching it from its interface,
595136600feSEd Schouten  * deallocating its buffers, and marking it free.
596136600feSEd Schouten  */
597136600feSEd Schouten static void
598136600feSEd Schouten bpf_dtor(void *data)
599136600feSEd Schouten {
600136600feSEd Schouten 	struct bpf_d *d = data;
601136600feSEd Schouten 
602136600feSEd Schouten 	BPFD_LOCK(d);
603136600feSEd Schouten 	if (d->bd_state == BPF_WAITING)
604136600feSEd Schouten 		callout_stop(&d->bd_callout);
605136600feSEd Schouten 	d->bd_state = BPF_IDLE;
606136600feSEd Schouten 	BPFD_UNLOCK(d);
607136600feSEd Schouten 	funsetown(&d->bd_sigio);
608136600feSEd Schouten 	mtx_lock(&bpf_mtx);
609136600feSEd Schouten 	if (d->bd_bif)
610136600feSEd Schouten 		bpf_detachd(d);
611136600feSEd Schouten 	mtx_unlock(&bpf_mtx);
612136600feSEd Schouten 	selwakeuppri(&d->bd_sel, PRINET);
613136600feSEd Schouten #ifdef MAC
614136600feSEd Schouten 	mac_bpfdesc_destroy(d);
615136600feSEd Schouten #endif /* MAC */
616136600feSEd Schouten 	knlist_destroy(&d->bd_sel.si_note);
617136600feSEd Schouten 	bpf_freed(d);
618136600feSEd Schouten 	free(d, M_BPF);
619136600feSEd Schouten }
620136600feSEd Schouten 
621136600feSEd Schouten /*
622df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
623df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
624df8bae1dSRodney W. Grimes  */
625df8bae1dSRodney W. Grimes /* ARGSUSED */
62687f6c662SJulian Elischer static	int
62719ba8395SChristian S.J. Peron bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
628df8bae1dSRodney W. Grimes {
629e7bb21b3SJonathan Lemon 	struct bpf_d *d;
630136600feSEd Schouten 	int error;
631df8bae1dSRodney W. Grimes 
6321ede983cSDag-Erling Smørgrav 	d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
633136600feSEd Schouten 	error = devfs_set_cdevpriv(d, bpf_dtor);
634136600feSEd Schouten 	if (error != 0) {
635136600feSEd Schouten 		free(d, M_BPF);
636136600feSEd Schouten 		return (error);
637136600feSEd Schouten 	}
6384d621040SChristian S.J. Peron 
6394d621040SChristian S.J. Peron 	/*
6404d621040SChristian S.J. Peron 	 * For historical reasons, perform a one-time initialization call to
6414d621040SChristian S.J. Peron 	 * the buffer routines, even though we're not yet committed to a
6424d621040SChristian S.J. Peron 	 * particular buffer method.
6434d621040SChristian S.J. Peron 	 */
6444d621040SChristian S.J. Peron 	bpf_buffer_init(d);
6454d621040SChristian S.J. Peron 	d->bd_bufmode = BPF_BUFMODE_BUFFER;
64600a83887SPaul Traina 	d->bd_sig = SIGIO;
647560a54e1SJung-uk Kim 	d->bd_direction = BPF_D_INOUT;
64869f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
64982f4445dSRobert Watson #ifdef MAC
65030d239bcSRobert Watson 	mac_bpfdesc_init(d);
65130d239bcSRobert Watson 	mac_bpfdesc_create(td->td_ucred, d);
65282f4445dSRobert Watson #endif
6536008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
654c6b28997SRobert Watson 	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
655d8b0556cSKonstantin Belousov 	knlist_init_mtx(&d->bd_sel.si_note, &d->bd_mtx);
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	return (0);
658df8bae1dSRodney W. Grimes }
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes /*
661df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
662df8bae1dSRodney W. Grimes  */
66387f6c662SJulian Elischer static	int
66419ba8395SChristian S.J. Peron bpfread(struct cdev *dev, struct uio *uio, int ioflag)
665df8bae1dSRodney W. Grimes {
666136600feSEd Schouten 	struct bpf_d *d;
667df8bae1dSRodney W. Grimes 	int error;
6688df67d77SJung-uk Kim 	int non_block;
6698df67d77SJung-uk Kim 	int timed_out;
670df8bae1dSRodney W. Grimes 
671136600feSEd Schouten 	error = devfs_get_cdevpriv((void **)&d);
672136600feSEd Schouten 	if (error != 0)
673136600feSEd Schouten 		return (error);
674136600feSEd Schouten 
675df8bae1dSRodney W. Grimes 	/*
676df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
677df8bae1dSRodney W. Grimes 	 * as kernel buffers.
678df8bae1dSRodney W. Grimes 	 */
679df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
680df8bae1dSRodney W. Grimes 		return (EINVAL);
681df8bae1dSRodney W. Grimes 
6828df67d77SJung-uk Kim 	non_block = ((ioflag & O_NONBLOCK) != 0);
6838df67d77SJung-uk Kim 
684e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
68550ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
6864d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
6874d621040SChristian S.J. Peron 		BPFD_UNLOCK(d);
6884d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
6894d621040SChristian S.J. Peron 	}
69081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
69181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
69281bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
69381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
694df8bae1dSRodney W. Grimes 	/*
695df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
696df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
697df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
698df8bae1dSRodney W. Grimes 	 */
699572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
7008df67d77SJung-uk Kim 		if (d->bd_slen != 0) {
701df8bae1dSRodney W. Grimes 			/*
702df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
703df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
7048df67d77SJung-uk Kim 			 */
7058df67d77SJung-uk Kim 			if (d->bd_immediate || non_block || timed_out) {
7068df67d77SJung-uk Kim 				/*
7078df67d77SJung-uk Kim 				 * Rotate the buffers and return what's here
7088df67d77SJung-uk Kim 				 * if we are in immediate mode, non-blocking
7098df67d77SJung-uk Kim 				 * flag is set, or this descriptor timed out.
710df8bae1dSRodney W. Grimes 				 */
711df8bae1dSRodney W. Grimes 				ROTATE_BUFFERS(d);
712df8bae1dSRodney W. Grimes 				break;
713df8bae1dSRodney W. Grimes 			}
7148df67d77SJung-uk Kim 		}
715de5d9935SRobert Watson 
716de5d9935SRobert Watson 		/*
717de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
718de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
719de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
720de5d9935SRobert Watson 		 * it before using it again.
721de5d9935SRobert Watson 		 */
722de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
723e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
724de5d9935SRobert Watson 			return (ENXIO);
725de5d9935SRobert Watson 		}
726de5d9935SRobert Watson 
7278df67d77SJung-uk Kim 		if (non_block) {
728e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
729fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
730fba3cfdeSJohn Polstra 		}
731521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
732e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
733df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
734e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
735df8bae1dSRodney W. Grimes 			return (error);
736df8bae1dSRodney W. Grimes 		}
737df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
738df8bae1dSRodney W. Grimes 			/*
739df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
740df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
741df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
742df8bae1dSRodney W. Grimes 			 */
743df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
744df8bae1dSRodney W. Grimes 				/*
745df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
746df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
747df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
748df8bae1dSRodney W. Grimes 				 */
749df8bae1dSRodney W. Grimes 				break;
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
752e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
753df8bae1dSRodney W. Grimes 				return (0);
754df8bae1dSRodney W. Grimes 			}
755df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
756df8bae1dSRodney W. Grimes 			break;
757df8bae1dSRodney W. Grimes 		}
758df8bae1dSRodney W. Grimes 	}
759df8bae1dSRodney W. Grimes 	/*
760df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
761df8bae1dSRodney W. Grimes 	 */
762e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes 	/*
765df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
766df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
767df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
76831b32e6dSRobert Watson 	 *
76931b32e6dSRobert Watson 	 * XXXRW: More synchronization needed here: what if a second thread
77031b32e6dSRobert Watson 	 * issues a read on the same fd at the same time?  Don't want this
77131b32e6dSRobert Watson 	 * getting invalidated.
772df8bae1dSRodney W. Grimes 	 */
7734d621040SChristian S.J. Peron 	error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
774df8bae1dSRodney W. Grimes 
775e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
776df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
777572bde2aSRobert Watson 	d->bd_hbuf = NULL;
778df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
77929f612ecSChristian S.J. Peron 	bpf_buf_reclaimed(d);
780e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
781df8bae1dSRodney W. Grimes 
782df8bae1dSRodney W. Grimes 	return (error);
783df8bae1dSRodney W. Grimes }
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes /*
786df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
787df8bae1dSRodney W. Grimes  */
788e7bb21b3SJonathan Lemon static __inline void
78919ba8395SChristian S.J. Peron bpf_wakeup(struct bpf_d *d)
790df8bae1dSRodney W. Grimes {
791a3272e3cSChristian S.J. Peron 
792a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
79381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
79481bda851SJohn Polstra 		callout_stop(&d->bd_callout);
79581bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
79681bda851SJohn Polstra 	}
797521f364bSDag-Erling Smørgrav 	wakeup(d);
798831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
799f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
80000a83887SPaul Traina 
801512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
802ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
803df8bae1dSRodney W. Grimes }
804df8bae1dSRodney W. Grimes 
80581bda851SJohn Polstra static void
80619ba8395SChristian S.J. Peron bpf_timed_out(void *arg)
80781bda851SJohn Polstra {
80881bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
80981bda851SJohn Polstra 
81081bda851SJohn Polstra 	BPFD_LOCK(d);
81181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
81281bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
81381bda851SJohn Polstra 		if (d->bd_slen != 0)
81481bda851SJohn Polstra 			bpf_wakeup(d);
81581bda851SJohn Polstra 	}
81681bda851SJohn Polstra 	BPFD_UNLOCK(d);
81781bda851SJohn Polstra }
81881bda851SJohn Polstra 
81987f6c662SJulian Elischer static int
8204d621040SChristian S.J. Peron bpf_ready(struct bpf_d *d)
8214d621040SChristian S.J. Peron {
8224d621040SChristian S.J. Peron 
8234d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
8244d621040SChristian S.J. Peron 
8254d621040SChristian S.J. Peron 	if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
8264d621040SChristian S.J. Peron 		return (1);
8274d621040SChristian S.J. Peron 	if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
8284d621040SChristian S.J. Peron 	    d->bd_slen != 0)
8294d621040SChristian S.J. Peron 		return (1);
8304d621040SChristian S.J. Peron 	return (0);
8314d621040SChristian S.J. Peron }
8324d621040SChristian S.J. Peron 
8334d621040SChristian S.J. Peron static int
83419ba8395SChristian S.J. Peron bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
835df8bae1dSRodney W. Grimes {
836136600feSEd Schouten 	struct bpf_d *d;
837df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
838560a54e1SJung-uk Kim 	struct mbuf *m, *mc;
8398240bf1eSRobert Watson 	struct sockaddr dst;
840560a54e1SJung-uk Kim 	int error, hlen;
841df8bae1dSRodney W. Grimes 
842136600feSEd Schouten 	error = devfs_get_cdevpriv((void **)&d);
843136600feSEd Schouten 	if (error != 0)
844136600feSEd Schouten 		return (error);
845136600feSEd Schouten 
84650ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
8474d621040SChristian S.J. Peron 	d->bd_wcount++;
8484d621040SChristian S.J. Peron 	if (d->bd_bif == NULL) {
8494d621040SChristian S.J. Peron 		d->bd_wdcount++;
850df8bae1dSRodney W. Grimes 		return (ENXIO);
8514d621040SChristian S.J. Peron 	}
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
854df8bae1dSRodney W. Grimes 
8554d621040SChristian S.J. Peron 	if ((ifp->if_flags & IFF_UP) == 0) {
8564d621040SChristian S.J. Peron 		d->bd_wdcount++;
8573518d220SSam Leffler 		return (ENETDOWN);
8584d621040SChristian S.J. Peron 	}
8593518d220SSam Leffler 
8604d621040SChristian S.J. Peron 	if (uio->uio_resid == 0) {
8614d621040SChristian S.J. Peron 		d->bd_wdcount++;
862df8bae1dSRodney W. Grimes 		return (0);
8634d621040SChristian S.J. Peron 	}
864df8bae1dSRodney W. Grimes 
8658240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
866d83e603aSChristian S.J. Peron 	m = NULL;
867d83e603aSChristian S.J. Peron 	hlen = 0;
868cb44b6dfSAndrew Thompson 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
869560a54e1SJung-uk Kim 	    &m, &dst, &hlen, d->bd_wfilter);
8704d621040SChristian S.J. Peron 	if (error) {
8714d621040SChristian S.J. Peron 		d->bd_wdcount++;
872df8bae1dSRodney W. Grimes 		return (error);
8734d621040SChristian S.J. Peron 	}
8744d621040SChristian S.J. Peron 	d->bd_wfcount++;
875114ae644SMike Smith 	if (d->bd_hdrcmplt)
876114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
877114ae644SMike Smith 
878560a54e1SJung-uk Kim 	if (d->bd_feedback) {
879560a54e1SJung-uk Kim 		mc = m_dup(m, M_DONTWAIT);
880560a54e1SJung-uk Kim 		if (mc != NULL)
881560a54e1SJung-uk Kim 			mc->m_pkthdr.rcvif = ifp;
8828cd892f7SJung-uk Kim 		/* Set M_PROMISC for outgoing packets to be discarded. */
8838cd892f7SJung-uk Kim 		if (d->bd_direction == BPF_D_INOUT)
8848cd892f7SJung-uk Kim 			m->m_flags |= M_PROMISC;
885560a54e1SJung-uk Kim 	} else
886560a54e1SJung-uk Kim 		mc = NULL;
887560a54e1SJung-uk Kim 
888560a54e1SJung-uk Kim 	m->m_pkthdr.len -= hlen;
889560a54e1SJung-uk Kim 	m->m_len -= hlen;
890560a54e1SJung-uk Kim 	m->m_data += hlen;	/* XXX */
891560a54e1SJung-uk Kim 
89221ca7b57SMarko Zec 	CURVNET_SET(ifp->if_vnet);
89382f4445dSRobert Watson #ifdef MAC
894f747d2ddSRobert Watson 	BPFD_LOCK(d);
89530d239bcSRobert Watson 	mac_bpfdesc_create_mbuf(d, m);
896560a54e1SJung-uk Kim 	if (mc != NULL)
89730d239bcSRobert Watson 		mac_bpfdesc_create_mbuf(d, mc);
898f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
89982f4445dSRobert Watson #endif
900560a54e1SJung-uk Kim 
901572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
9024d621040SChristian S.J. Peron 	if (error)
9034d621040SChristian S.J. Peron 		d->bd_wdcount++;
904560a54e1SJung-uk Kim 
905560a54e1SJung-uk Kim 	if (mc != NULL) {
9060bf686c1SRobert Watson 		if (error == 0)
907560a54e1SJung-uk Kim 			(*ifp->if_input)(ifp, mc);
9080bf686c1SRobert Watson 		else
909560a54e1SJung-uk Kim 			m_freem(mc);
910560a54e1SJung-uk Kim 	}
91121ca7b57SMarko Zec 	CURVNET_RESTORE();
912560a54e1SJung-uk Kim 
913df8bae1dSRodney W. Grimes 	return (error);
914df8bae1dSRodney W. Grimes }
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes /*
917e82669d9SRobert Watson  * Reset a descriptor by flushing its packet buffer and clearing the receive
918e82669d9SRobert Watson  * and drop counts.  This is doable for kernel-only buffers, but with
919e82669d9SRobert Watson  * zero-copy buffers, we can't write to (or rotate) buffers that are
920e82669d9SRobert Watson  * currently owned by userspace.  It would be nice if we could encapsulate
921e82669d9SRobert Watson  * this logic in the buffer code rather than here.
922df8bae1dSRodney W. Grimes  */
923df8bae1dSRodney W. Grimes static void
92419ba8395SChristian S.J. Peron reset_d(struct bpf_d *d)
925df8bae1dSRodney W. Grimes {
926e7bb21b3SJonathan Lemon 
927e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
928e82669d9SRobert Watson 
929e82669d9SRobert Watson 	if ((d->bd_hbuf != NULL) &&
930e82669d9SRobert Watson 	    (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) {
931df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
932df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
933572bde2aSRobert Watson 		d->bd_hbuf = NULL;
934e82669d9SRobert Watson 		d->bd_hlen = 0;
93529f612ecSChristian S.J. Peron 		bpf_buf_reclaimed(d);
936df8bae1dSRodney W. Grimes 	}
937e82669d9SRobert Watson 	if (bpf_canwritebuf(d))
938df8bae1dSRodney W. Grimes 		d->bd_slen = 0;
939df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
940df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
94169f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
9424d621040SChristian S.J. Peron 	d->bd_wcount = 0;
9434d621040SChristian S.J. Peron 	d->bd_wfcount = 0;
9444d621040SChristian S.J. Peron 	d->bd_wdcount = 0;
9454d621040SChristian S.J. Peron 	d->bd_zcopy = 0;
946df8bae1dSRodney W. Grimes }
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes /*
949df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
950df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
951df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
952f11c3508SDavid Malone  *  BIOCSETF		Set read filter.
953f11c3508SDavid Malone  *  BIOCSETFNR		Set read filter without resetting descriptor.
954f11c3508SDavid Malone  *  BIOCSETWF		Set write filter.
955df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
956df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
957df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
958df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
959df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
960df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
961df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
962df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
963df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
964df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
965114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
966114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
967560a54e1SJung-uk Kim  *  BIOCGDIRECTION	Get packet direction flag
968560a54e1SJung-uk Kim  *  BIOCSDIRECTION	Set packet direction flag
96993e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
970560a54e1SJung-uk Kim  *  BIOCFEEDBACK	Set packet feedback mode.
9714d621040SChristian S.J. Peron  *  BIOCSETZBUF		Set current zero-copy buffer locations.
9724d621040SChristian S.J. Peron  *  BIOCGETZMAX		Get maximum zero-copy buffer size.
9734d621040SChristian S.J. Peron  *  BIOCROTZBUF		Force rotation of zero-copy buffer
9744d621040SChristian S.J. Peron  *  BIOCSETBUFMODE	Set buffer mode.
9754d621040SChristian S.J. Peron  *  BIOCGETBUFMODE	Get current buffer mode.
976df8bae1dSRodney W. Grimes  */
977df8bae1dSRodney W. Grimes /* ARGSUSED */
97887f6c662SJulian Elischer static	int
97919ba8395SChristian S.J. Peron bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
98019ba8395SChristian S.J. Peron     struct thread *td)
981df8bae1dSRodney W. Grimes {
982136600feSEd Schouten 	struct bpf_d *d;
983136600feSEd Schouten 	int error;
984136600feSEd Schouten 
985136600feSEd Schouten 	error = devfs_get_cdevpriv((void **)&d);
986136600feSEd Schouten 	if (error != 0)
987136600feSEd Schouten 		return (error);
988df8bae1dSRodney W. Grimes 
989b75a24a0SChristian S.J. Peron 	/*
990b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
991b75a24a0SChristian S.J. Peron 	 */
99281bda851SJohn Polstra 	BPFD_LOCK(d);
993cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
99481bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
99581bda851SJohn Polstra 		callout_stop(&d->bd_callout);
99681bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
99781bda851SJohn Polstra 	BPFD_UNLOCK(d);
99881bda851SJohn Polstra 
99993e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
100093e39f0bSChristian S.J. Peron 		switch (cmd) {
100193e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
100293e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
100393e39f0bSChristian S.J. Peron 		case BIOCGDLT:
100493e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
100593e39f0bSChristian S.J. Peron 		case BIOCGETIF:
100693e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
100793e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
100893e39f0bSChristian S.J. Peron 		case BIOCVERSION:
100993e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
101093e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
1011560a54e1SJung-uk Kim 		case BIOCFEEDBACK:
101293e39f0bSChristian S.J. Peron 		case FIONREAD:
101393e39f0bSChristian S.J. Peron 		case BIOCLOCK:
101493e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
101593e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
101693e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
10174d621040SChristian S.J. Peron 		case BIOCROTZBUF:
101893e39f0bSChristian S.J. Peron 			break;
101993e39f0bSChristian S.J. Peron 		default:
102093e39f0bSChristian S.J. Peron 			return (EPERM);
102193e39f0bSChristian S.J. Peron 		}
102293e39f0bSChristian S.J. Peron 	}
102397021c24SMarko Zec 	CURVNET_SET(TD_TO_VNET(td));
1024df8bae1dSRodney W. Grimes 	switch (cmd) {
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes 	default:
1027df8bae1dSRodney W. Grimes 		error = EINVAL;
1028df8bae1dSRodney W. Grimes 		break;
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	/*
1031df8bae1dSRodney W. Grimes 	 * Check for read packet available.
1032df8bae1dSRodney W. Grimes 	 */
1033df8bae1dSRodney W. Grimes 	case FIONREAD:
1034df8bae1dSRodney W. Grimes 		{
1035df8bae1dSRodney W. Grimes 			int n;
1036df8bae1dSRodney W. Grimes 
1037e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
1038df8bae1dSRodney W. Grimes 			n = d->bd_slen;
1039df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
1040df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
1041e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
1042df8bae1dSRodney W. Grimes 
1043df8bae1dSRodney W. Grimes 			*(int *)addr = n;
1044df8bae1dSRodney W. Grimes 			break;
1045df8bae1dSRodney W. Grimes 		}
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
1048df8bae1dSRodney W. Grimes 		{
1049df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
1050df8bae1dSRodney W. Grimes 
1051572bde2aSRobert Watson 			if (d->bd_bif == NULL)
1052df8bae1dSRodney W. Grimes 				error = EINVAL;
1053df8bae1dSRodney W. Grimes 			else {
1054df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
1055df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
1056df8bae1dSRodney W. Grimes 			}
1057df8bae1dSRodney W. Grimes 			break;
1058df8bae1dSRodney W. Grimes 		}
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes 	/*
1061df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
1062df8bae1dSRodney W. Grimes 	 */
1063df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
1064df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
1065df8bae1dSRodney W. Grimes 		break;
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 	/*
1068df8bae1dSRodney W. Grimes 	 * Set buffer length.
1069df8bae1dSRodney W. Grimes 	 */
1070df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
10714d621040SChristian S.J. Peron 		error = bpf_ioctl_sblen(d, (u_int *)addr);
1072df8bae1dSRodney W. Grimes 		break;
1073df8bae1dSRodney W. Grimes 
1074df8bae1dSRodney W. Grimes 	/*
1075df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
1076df8bae1dSRodney W. Grimes 	 */
1077df8bae1dSRodney W. Grimes 	case BIOCSETF:
1078f11c3508SDavid Malone 	case BIOCSETFNR:
107993e39f0bSChristian S.J. Peron 	case BIOCSETWF:
108093e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1081df8bae1dSRodney W. Grimes 		break;
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes 	/*
1084df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
1085df8bae1dSRodney W. Grimes 	 */
1086df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
1087e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1088df8bae1dSRodney W. Grimes 		reset_d(d);
1089e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1090df8bae1dSRodney W. Grimes 		break;
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 	/*
1093df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
1094df8bae1dSRodney W. Grimes 	 */
1095df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
1096572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
1097df8bae1dSRodney W. Grimes 			/*
1098df8bae1dSRodney W. Grimes 			 * No interface attached yet.
1099df8bae1dSRodney W. Grimes 			 */
1100df8bae1dSRodney W. Grimes 			error = EINVAL;
1101df8bae1dSRodney W. Grimes 			break;
1102df8bae1dSRodney W. Grimes 		}
1103df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
1104df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
1105df8bae1dSRodney W. Grimes 			if (error == 0)
1106df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
1107df8bae1dSRodney W. Grimes 		}
1108df8bae1dSRodney W. Grimes 		break;
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	/*
11118eab61f3SSam Leffler 	 * Get current data link type.
1112df8bae1dSRodney W. Grimes 	 */
1113df8bae1dSRodney W. Grimes 	case BIOCGDLT:
1114572bde2aSRobert Watson 		if (d->bd_bif == NULL)
1115df8bae1dSRodney W. Grimes 			error = EINVAL;
1116df8bae1dSRodney W. Grimes 		else
1117df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
1118df8bae1dSRodney W. Grimes 		break;
1119df8bae1dSRodney W. Grimes 
1120df8bae1dSRodney W. Grimes 	/*
11218eab61f3SSam Leffler 	 * Get a list of supported data link types.
11228eab61f3SSam Leffler 	 */
11238eab61f3SSam Leffler 	case BIOCGDLTLIST:
1124572bde2aSRobert Watson 		if (d->bd_bif == NULL)
11258eab61f3SSam Leffler 			error = EINVAL;
11268eab61f3SSam Leffler 		else
11278eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
11288eab61f3SSam Leffler 		break;
11298eab61f3SSam Leffler 
11308eab61f3SSam Leffler 	/*
11318eab61f3SSam Leffler 	 * Set data link type.
11328eab61f3SSam Leffler 	 */
11338eab61f3SSam Leffler 	case BIOCSDLT:
1134572bde2aSRobert Watson 		if (d->bd_bif == NULL)
11358eab61f3SSam Leffler 			error = EINVAL;
11368eab61f3SSam Leffler 		else
11378eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
11388eab61f3SSam Leffler 		break;
11398eab61f3SSam Leffler 
11408eab61f3SSam Leffler 	/*
11411dd0feaaSArchie Cobbs 	 * Get interface name.
1142df8bae1dSRodney W. Grimes 	 */
1143df8bae1dSRodney W. Grimes 	case BIOCGETIF:
1144572bde2aSRobert Watson 		if (d->bd_bif == NULL)
1145df8bae1dSRodney W. Grimes 			error = EINVAL;
11461dd0feaaSArchie Cobbs 		else {
11471dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
11481dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
11491dd0feaaSArchie Cobbs 
11509bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
11519bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
11521dd0feaaSArchie Cobbs 		}
1153df8bae1dSRodney W. Grimes 		break;
1154df8bae1dSRodney W. Grimes 
1155df8bae1dSRodney W. Grimes 	/*
1156df8bae1dSRodney W. Grimes 	 * Set interface.
1157df8bae1dSRodney W. Grimes 	 */
1158df8bae1dSRodney W. Grimes 	case BIOCSETIF:
1159df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
1160df8bae1dSRodney W. Grimes 		break;
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes 	/*
1163df8bae1dSRodney W. Grimes 	 * Set read timeout.
1164df8bae1dSRodney W. Grimes 	 */
1165df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
1166df8bae1dSRodney W. Grimes 		{
1167df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
1168df8bae1dSRodney W. Grimes 
1169bdc2cdc5SAlexander Langer 			/*
1170bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
1171bdc2cdc5SAlexander Langer 			 * a one-shot timer.
1172bdc2cdc5SAlexander Langer 			 */
1173bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
1174bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
1175df8bae1dSRodney W. Grimes 			break;
1176df8bae1dSRodney W. Grimes 		}
1177df8bae1dSRodney W. Grimes 
1178df8bae1dSRodney W. Grimes 	/*
1179df8bae1dSRodney W. Grimes 	 * Get read timeout.
1180df8bae1dSRodney W. Grimes 	 */
1181df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
1182df8bae1dSRodney W. Grimes 		{
1183df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
1184df8bae1dSRodney W. Grimes 
1185bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
1186bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
1187df8bae1dSRodney W. Grimes 			break;
1188df8bae1dSRodney W. Grimes 		}
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes 	/*
1191df8bae1dSRodney W. Grimes 	 * Get packet stats.
1192df8bae1dSRodney W. Grimes 	 */
1193df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
1194df8bae1dSRodney W. Grimes 		{
1195df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
1196df8bae1dSRodney W. Grimes 
11974d621040SChristian S.J. Peron 			/* XXXCSJP overflow */
1198df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
1199df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
1200df8bae1dSRodney W. Grimes 			break;
1201df8bae1dSRodney W. Grimes 		}
1202df8bae1dSRodney W. Grimes 
1203df8bae1dSRodney W. Grimes 	/*
1204df8bae1dSRodney W. Grimes 	 * Set immediate mode.
1205df8bae1dSRodney W. Grimes 	 */
1206df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
1207df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
1208df8bae1dSRodney W. Grimes 		break;
1209df8bae1dSRodney W. Grimes 
1210df8bae1dSRodney W. Grimes 	case BIOCVERSION:
1211df8bae1dSRodney W. Grimes 		{
1212df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
1213df8bae1dSRodney W. Grimes 
1214df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
1215df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
1216df8bae1dSRodney W. Grimes 			break;
1217df8bae1dSRodney W. Grimes 		}
121800a83887SPaul Traina 
1219114ae644SMike Smith 	/*
1220114ae644SMike Smith 	 * Get "header already complete" flag
1221114ae644SMike Smith 	 */
1222114ae644SMike Smith 	case BIOCGHDRCMPLT:
1223114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
1224114ae644SMike Smith 		break;
1225114ae644SMike Smith 
1226114ae644SMike Smith 	/*
1227114ae644SMike Smith 	 * Set "header already complete" flag
1228114ae644SMike Smith 	 */
1229114ae644SMike Smith 	case BIOCSHDRCMPLT:
1230114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1231114ae644SMike Smith 		break;
1232114ae644SMike Smith 
12338ed3828cSRobert Watson 	/*
1234560a54e1SJung-uk Kim 	 * Get packet direction flag
12358ed3828cSRobert Watson 	 */
1236560a54e1SJung-uk Kim 	case BIOCGDIRECTION:
1237560a54e1SJung-uk Kim 		*(u_int *)addr = d->bd_direction;
12388ed3828cSRobert Watson 		break;
12398ed3828cSRobert Watson 
12408ed3828cSRobert Watson 	/*
1241560a54e1SJung-uk Kim 	 * Set packet direction flag
12428ed3828cSRobert Watson 	 */
1243560a54e1SJung-uk Kim 	case BIOCSDIRECTION:
1244560a54e1SJung-uk Kim 		{
1245560a54e1SJung-uk Kim 			u_int	direction;
1246560a54e1SJung-uk Kim 
1247560a54e1SJung-uk Kim 			direction = *(u_int *)addr;
1248560a54e1SJung-uk Kim 			switch (direction) {
1249560a54e1SJung-uk Kim 			case BPF_D_IN:
1250560a54e1SJung-uk Kim 			case BPF_D_INOUT:
1251560a54e1SJung-uk Kim 			case BPF_D_OUT:
1252560a54e1SJung-uk Kim 				d->bd_direction = direction;
1253560a54e1SJung-uk Kim 				break;
1254560a54e1SJung-uk Kim 			default:
1255560a54e1SJung-uk Kim 				error = EINVAL;
1256560a54e1SJung-uk Kim 			}
1257560a54e1SJung-uk Kim 		}
1258560a54e1SJung-uk Kim 		break;
1259560a54e1SJung-uk Kim 
1260560a54e1SJung-uk Kim 	case BIOCFEEDBACK:
1261560a54e1SJung-uk Kim 		d->bd_feedback = *(u_int *)addr;
1262560a54e1SJung-uk Kim 		break;
1263560a54e1SJung-uk Kim 
1264560a54e1SJung-uk Kim 	case BIOCLOCK:
1265560a54e1SJung-uk Kim 		d->bd_locked = 1;
12668ed3828cSRobert Watson 		break;
12678ed3828cSRobert Watson 
126800a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
126900a83887SPaul Traina 		break;
127000a83887SPaul Traina 
127100a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
127200a83887SPaul Traina 		d->bd_async = *(int *)addr;
127300a83887SPaul Traina 		break;
127400a83887SPaul Traina 
1275831d27a9SDon Lewis 	case FIOSETOWN:
1276831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
127700a83887SPaul Traina 		break;
127800a83887SPaul Traina 
1279831d27a9SDon Lewis 	case FIOGETOWN:
128091e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
1281831d27a9SDon Lewis 		break;
1282831d27a9SDon Lewis 
1283831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
1284831d27a9SDon Lewis 	case TIOCSPGRP:
1285831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1286831d27a9SDon Lewis 		break;
1287831d27a9SDon Lewis 
1288831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
128900a83887SPaul Traina 	case TIOCGPGRP:
129091e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
129100a83887SPaul Traina 		break;
129200a83887SPaul Traina 
129300a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
129400a83887SPaul Traina 		{
129500a83887SPaul Traina 			u_int sig;
129600a83887SPaul Traina 
129700a83887SPaul Traina 			sig = *(u_int *)addr;
129800a83887SPaul Traina 
129900a83887SPaul Traina 			if (sig >= NSIG)
130000a83887SPaul Traina 				error = EINVAL;
130100a83887SPaul Traina 			else
130200a83887SPaul Traina 				d->bd_sig = sig;
130300a83887SPaul Traina 			break;
130400a83887SPaul Traina 		}
130500a83887SPaul Traina 	case BIOCGRSIG:
130600a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
130700a83887SPaul Traina 		break;
13084d621040SChristian S.J. Peron 
13094d621040SChristian S.J. Peron 	case BIOCGETBUFMODE:
13104d621040SChristian S.J. Peron 		*(u_int *)addr = d->bd_bufmode;
13114d621040SChristian S.J. Peron 		break;
13124d621040SChristian S.J. Peron 
13134d621040SChristian S.J. Peron 	case BIOCSETBUFMODE:
13144d621040SChristian S.J. Peron 		/*
13154d621040SChristian S.J. Peron 		 * Allow the buffering mode to be changed as long as we
13164d621040SChristian S.J. Peron 		 * haven't yet committed to a particular mode.  Our
13174d621040SChristian S.J. Peron 		 * definition of commitment, for now, is whether or not a
13184d621040SChristian S.J. Peron 		 * buffer has been allocated or an interface attached, since
13194d621040SChristian S.J. Peron 		 * that's the point where things get tricky.
13204d621040SChristian S.J. Peron 		 */
13214d621040SChristian S.J. Peron 		switch (*(u_int *)addr) {
13224d621040SChristian S.J. Peron 		case BPF_BUFMODE_BUFFER:
13234d621040SChristian S.J. Peron 			break;
13244d621040SChristian S.J. Peron 
13254d621040SChristian S.J. Peron 		case BPF_BUFMODE_ZBUF:
13264d621040SChristian S.J. Peron 			if (bpf_zerocopy_enable)
13274d621040SChristian S.J. Peron 				break;
13284d621040SChristian S.J. Peron 			/* FALLSTHROUGH */
13294d621040SChristian S.J. Peron 
13304d621040SChristian S.J. Peron 		default:
13314d621040SChristian S.J. Peron 			return (EINVAL);
13324d621040SChristian S.J. Peron 		}
13334d621040SChristian S.J. Peron 
13344d621040SChristian S.J. Peron 		BPFD_LOCK(d);
13354d621040SChristian S.J. Peron 		if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
13364d621040SChristian S.J. Peron 		    d->bd_fbuf != NULL || d->bd_bif != NULL) {
13374d621040SChristian S.J. Peron 			BPFD_UNLOCK(d);
13384d621040SChristian S.J. Peron 			return (EBUSY);
13394d621040SChristian S.J. Peron 		}
13404d621040SChristian S.J. Peron 		d->bd_bufmode = *(u_int *)addr;
13414d621040SChristian S.J. Peron 		BPFD_UNLOCK(d);
13424d621040SChristian S.J. Peron 		break;
13434d621040SChristian S.J. Peron 
13444d621040SChristian S.J. Peron 	case BIOCGETZMAX:
13454d621040SChristian S.J. Peron 		return (bpf_ioctl_getzmax(td, d, (size_t *)addr));
13464d621040SChristian S.J. Peron 
13474d621040SChristian S.J. Peron 	case BIOCSETZBUF:
13484d621040SChristian S.J. Peron 		return (bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr));
13494d621040SChristian S.J. Peron 
13504d621040SChristian S.J. Peron 	case BIOCROTZBUF:
13514d621040SChristian S.J. Peron 		return (bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr));
1352df8bae1dSRodney W. Grimes 	}
135397021c24SMarko Zec 	CURVNET_RESTORE();
1354df8bae1dSRodney W. Grimes 	return (error);
1355df8bae1dSRodney W. Grimes }
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes /*
1358df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1359df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1360df8bae1dSRodney W. Grimes  */
1361f708ef1bSPoul-Henning Kamp static int
136219ba8395SChristian S.J. Peron bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1363df8bae1dSRodney W. Grimes {
1364df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
136593e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1366293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1367ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1368ae275efcSJung-uk Kim #endif
1369df8bae1dSRodney W. Grimes 
137093e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
137193e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
137293e39f0bSChristian S.J. Peron 		wfilter = 1;
1373293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1374ae275efcSJung-uk Kim 		ofunc = NULL;
1375ae275efcSJung-uk Kim #endif
137693e39f0bSChristian S.J. Peron 	} else {
137793e39f0bSChristian S.J. Peron 		wfilter = 0;
137893e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1379293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1380ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1381ae275efcSJung-uk Kim #endif
138293e39f0bSChristian S.J. Peron 	}
1383572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1384df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1385df8bae1dSRodney W. Grimes 			return (EINVAL);
1386e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
138793e39f0bSChristian S.J. Peron 		if (wfilter)
138893e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1389ae275efcSJung-uk Kim 		else {
139093e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1391293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1392ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1393ae275efcSJung-uk Kim #endif
1394f11c3508SDavid Malone 			if (cmd == BIOCSETF)
1395df8bae1dSRodney W. Grimes 				reset_d(d);
1396f11c3508SDavid Malone 		}
1397e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1398572bde2aSRobert Watson 		if (old != NULL)
1399bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1400293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1401ae275efcSJung-uk Kim 		if (ofunc != NULL)
1402ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1403ae275efcSJung-uk Kim #endif
1404df8bae1dSRodney W. Grimes 		return (0);
1405df8bae1dSRodney W. Grimes 	}
1406df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
14070eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1408df8bae1dSRodney W. Grimes 		return (EINVAL);
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1411a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1412df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1413df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1414e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
141593e39f0bSChristian S.J. Peron 		if (wfilter)
141693e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1417ae275efcSJung-uk Kim 		else {
141893e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1419293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1420ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1421ae275efcSJung-uk Kim #endif
1422f11c3508SDavid Malone 			if (cmd == BIOCSETF)
1423df8bae1dSRodney W. Grimes 				reset_d(d);
1424f11c3508SDavid Malone 		}
1425e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1426572bde2aSRobert Watson 		if (old != NULL)
1427bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1428293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1429ae275efcSJung-uk Kim 		if (ofunc != NULL)
1430ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1431ae275efcSJung-uk Kim #endif
1432df8bae1dSRodney W. Grimes 
1433df8bae1dSRodney W. Grimes 		return (0);
1434df8bae1dSRodney W. Grimes 	}
1435bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1436df8bae1dSRodney W. Grimes 	return (EINVAL);
1437df8bae1dSRodney W. Grimes }
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes /*
1440df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1441df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1442df8bae1dSRodney W. Grimes  * Return an errno or 0.
1443df8bae1dSRodney W. Grimes  */
1444df8bae1dSRodney W. Grimes static int
144519ba8395SChristian S.J. Peron bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1446df8bae1dSRodney W. Grimes {
1447df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14489b44ff22SGarrett Wollman 	struct ifnet *theywant;
1449df8bae1dSRodney W. Grimes 
14509b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
145116d878ccSChristian S.J. Peron 	if (theywant == NULL || theywant->if_bpf == NULL)
145216d878ccSChristian S.J. Peron 		return (ENXIO);
14539b44ff22SGarrett Wollman 
145416d878ccSChristian S.J. Peron 	bp = theywant->if_bpf;
14554d621040SChristian S.J. Peron 
1456df8bae1dSRodney W. Grimes 	/*
14574d621040SChristian S.J. Peron 	 * Behavior here depends on the buffering model.  If we're using
14584d621040SChristian S.J. Peron 	 * kernel memory buffers, then we can allocate them here.  If we're
14594d621040SChristian S.J. Peron 	 * using zero-copy, then the user process must have registered
14604d621040SChristian S.J. Peron 	 * buffers by the time we get here.  If not, return an error.
14614d621040SChristian S.J. Peron 	 *
14624d621040SChristian S.J. Peron 	 * XXXRW: There are locking issues here with multi-threaded use: what
14634d621040SChristian S.J. Peron 	 * if two threads try to set the interface at once?
1464df8bae1dSRodney W. Grimes 	 */
14654d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
14664d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1467a3594432SRobert Watson 		if (d->bd_sbuf == NULL)
14684d621040SChristian S.J. Peron 			bpf_buffer_alloc(d);
14694d621040SChristian S.J. Peron 		KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL"));
14704d621040SChristian S.J. Peron 		break;
14714d621040SChristian S.J. Peron 
14724d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
14734d621040SChristian S.J. Peron 		if (d->bd_sbuf == NULL)
14744d621040SChristian S.J. Peron 			return (EINVAL);
14754d621040SChristian S.J. Peron 		break;
14764d621040SChristian S.J. Peron 
14774d621040SChristian S.J. Peron 	default:
14784d621040SChristian S.J. Peron 		panic("bpf_setif: bufmode %d", d->bd_bufmode);
14794d621040SChristian S.J. Peron 	}
1480df8bae1dSRodney W. Grimes 	if (bp != d->bd_bif) {
1481df8bae1dSRodney W. Grimes 		if (d->bd_bif)
1482df8bae1dSRodney W. Grimes 			/*
1483df8bae1dSRodney W. Grimes 			 * Detach if attached to something else.
1484df8bae1dSRodney W. Grimes 			 */
1485df8bae1dSRodney W. Grimes 			bpf_detachd(d);
1486df8bae1dSRodney W. Grimes 
1487df8bae1dSRodney W. Grimes 		bpf_attachd(d, bp);
1488df8bae1dSRodney W. Grimes 	}
1489e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1490df8bae1dSRodney W. Grimes 	reset_d(d);
1491e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1492df8bae1dSRodney W. Grimes 	return (0);
1493df8bae1dSRodney W. Grimes }
1494df8bae1dSRodney W. Grimes 
1495df8bae1dSRodney W. Grimes /*
1496243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1497df8bae1dSRodney W. Grimes  *
1498df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1499df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1500df8bae1dSRodney W. Grimes  */
150137c84183SPoul-Henning Kamp static int
150219ba8395SChristian S.J. Peron bpfpoll(struct cdev *dev, int events, struct thread *td)
1503df8bae1dSRodney W. Grimes {
1504e7bb21b3SJonathan Lemon 	struct bpf_d *d;
15050832fc64SGarance A Drosehn 	int revents;
1506df8bae1dSRodney W. Grimes 
1507136600feSEd Schouten 	if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL)
1508136600feSEd Schouten 		return (events &
1509136600feSEd Schouten 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1510de5d9935SRobert Watson 
1511b75a24a0SChristian S.J. Peron 	/*
1512b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1513b75a24a0SChristian S.J. Peron 	 */
15140832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1515e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1516cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
151775c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
151895aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1519243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
152081bda851SJohn Polstra 		else {
1521ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
152281bda851SJohn Polstra 			/* Start the read timeout if necessary. */
152381bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
152481bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
152581bda851SJohn Polstra 				    bpf_timed_out, d);
152681bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
152781bda851SJohn Polstra 			}
152881bda851SJohn Polstra 		}
152975c13541SPoul-Henning Kamp 	}
1530e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1531243ac7d8SPeter Wemm 	return (revents);
1532df8bae1dSRodney W. Grimes }
1533df8bae1dSRodney W. Grimes 
1534df8bae1dSRodney W. Grimes /*
153595aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
153695aab9ccSJohn-Mark Gurney  * reject all others.
153795aab9ccSJohn-Mark Gurney  */
153895aab9ccSJohn-Mark Gurney int
153919ba8395SChristian S.J. Peron bpfkqfilter(struct cdev *dev, struct knote *kn)
154095aab9ccSJohn-Mark Gurney {
1541136600feSEd Schouten 	struct bpf_d *d;
154295aab9ccSJohn-Mark Gurney 
1543136600feSEd Schouten 	if (devfs_get_cdevpriv((void **)&d) != 0 ||
1544136600feSEd Schouten 	    kn->kn_filter != EVFILT_READ)
154595aab9ccSJohn-Mark Gurney 		return (1);
154695aab9ccSJohn-Mark Gurney 
1547b75a24a0SChristian S.J. Peron 	/*
1548b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1549b75a24a0SChristian S.J. Peron 	 */
1550cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1551b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
155295aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
155395aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
15544b19419eSChristian S.J. Peron 	knlist_add(&d->bd_sel.si_note, kn, 1);
1555cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
155695aab9ccSJohn-Mark Gurney 
155795aab9ccSJohn-Mark Gurney 	return (0);
155895aab9ccSJohn-Mark Gurney }
155995aab9ccSJohn-Mark Gurney 
156095aab9ccSJohn-Mark Gurney static void
156119ba8395SChristian S.J. Peron filt_bpfdetach(struct knote *kn)
156295aab9ccSJohn-Mark Gurney {
156395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
156495aab9ccSJohn-Mark Gurney 
1565ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
156695aab9ccSJohn-Mark Gurney }
156795aab9ccSJohn-Mark Gurney 
156895aab9ccSJohn-Mark Gurney static int
156919ba8395SChristian S.J. Peron filt_bpfread(struct knote *kn, long hint)
157095aab9ccSJohn-Mark Gurney {
157195aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
157295aab9ccSJohn-Mark Gurney 	int ready;
157395aab9ccSJohn-Mark Gurney 
157486c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
157595aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
157695aab9ccSJohn-Mark Gurney 	if (ready) {
157795aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
157895aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
157995aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
158095aab9ccSJohn-Mark Gurney 	}
158195aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
158295aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
158395aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
158495aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
158595aab9ccSJohn-Mark Gurney 	}
158695aab9ccSJohn-Mark Gurney 
158795aab9ccSJohn-Mark Gurney 	return (ready);
158895aab9ccSJohn-Mark Gurney }
158995aab9ccSJohn-Mark Gurney 
159095aab9ccSJohn-Mark Gurney /*
1591df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1592df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1593df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1594df8bae1dSRodney W. Grimes  * buffer.
1595df8bae1dSRodney W. Grimes  */
1596df8bae1dSRodney W. Grimes void
159719ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1598df8bae1dSRodney W. Grimes {
15998994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1600a36599ccSJung-uk Kim #ifdef BPF_JITTER
1601a36599ccSJung-uk Kim 	bpf_jit_filter *bf;
1602a36599ccSJung-uk Kim #endif
16038994a245SDag-Erling Smørgrav 	u_int slen;
160491433904SDavid Malone 	int gottime;
160591433904SDavid Malone 	struct timeval tv;
1606e7bb21b3SJonathan Lemon 
160791433904SDavid Malone 	gottime = 0;
1608e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
16094a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1610e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1611df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1612a05cf8c6SChristian S.J. Peron 		/*
1613a05cf8c6SChristian S.J. Peron 		 * NB: We dont call BPF_CHECK_DIRECTION() here since there is no
1614a05cf8c6SChristian S.J. Peron 		 * way for the caller to indiciate to us whether this packet
1615a05cf8c6SChristian S.J. Peron 		 * is inbound or outbound.  In the bpf_mtap() routines, we use
1616a05cf8c6SChristian S.J. Peron 		 * the interface pointers on the mbuf to figure it out.
1617a05cf8c6SChristian S.J. Peron 		 */
1618ae275efcSJung-uk Kim #ifdef BPF_JITTER
1619a36599ccSJung-uk Kim 		bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1620a36599ccSJung-uk Kim 		if (bf != NULL)
1621a36599ccSJung-uk Kim 			slen = (*(bf->func))(pkt, pktlen, pktlen);
1622ae275efcSJung-uk Kim 		else
1623ae275efcSJung-uk Kim #endif
162493e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1625ec272d87SRobert Watson 		if (slen != 0) {
162669f7644bSChristian S.J. Peron 			d->bd_fcount++;
162791433904SDavid Malone 			if (!gottime) {
162891433904SDavid Malone 				microtime(&tv);
162991433904SDavid Malone 				gottime = 1;
163091433904SDavid Malone 			}
1631ec272d87SRobert Watson #ifdef MAC
163230d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1633ec272d87SRobert Watson #endif
16344d621040SChristian S.J. Peron 				catchpacket(d, pkt, pktlen, slen,
16354d621040SChristian S.J. Peron 				    bpf_append_bytes, &tv);
1636ec272d87SRobert Watson 		}
1637e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1638df8bae1dSRodney W. Grimes 	}
1639e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1640df8bae1dSRodney W. Grimes }
1641df8bae1dSRodney W. Grimes 
1642f81a2a49SJung-uk Kim #define	BPF_CHECK_DIRECTION(d, r, i)				\
1643f81a2a49SJung-uk Kim 	    (((d)->bd_direction == BPF_D_IN && (r) != (i)) ||	\
1644f81a2a49SJung-uk Kim 	    ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
1645560a54e1SJung-uk Kim 
1646df8bae1dSRodney W. Grimes /*
1647df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1648df8bae1dSRodney W. Grimes  */
1649df8bae1dSRodney W. Grimes void
165019ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1651df8bae1dSRodney W. Grimes {
1652df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1653a36599ccSJung-uk Kim #ifdef BPF_JITTER
1654a36599ccSJung-uk Kim 	bpf_jit_filter *bf;
1655a36599ccSJung-uk Kim #endif
1656df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
165791433904SDavid Malone 	int gottime;
165891433904SDavid Malone 	struct timeval tv;
165991433904SDavid Malone 
16608cd892f7SJung-uk Kim 	/* Skip outgoing duplicate packets. */
16618cd892f7SJung-uk Kim 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
16628cd892f7SJung-uk Kim 		m->m_flags &= ~M_PROMISC;
16638cd892f7SJung-uk Kim 		return;
16648cd892f7SJung-uk Kim 	}
16658cd892f7SJung-uk Kim 
166691433904SDavid Malone 	gottime = 0;
1667df8bae1dSRodney W. Grimes 
1668f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1669df8bae1dSRodney W. Grimes 
1670e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
16714a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1672f81a2a49SJung-uk Kim 		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
16738ed3828cSRobert Watson 			continue;
1674e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1675df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1676ae275efcSJung-uk Kim #ifdef BPF_JITTER
1677a36599ccSJung-uk Kim 		bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1678ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1679a36599ccSJung-uk Kim 		if (bf != NULL && m->m_next == NULL)
1680a36599ccSJung-uk Kim 			slen = (*(bf->func))(mtod(m, u_char *), pktlen, pktlen);
1681ae275efcSJung-uk Kim 		else
1682ae275efcSJung-uk Kim #endif
168393e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
16844ddfb531SChristian S.J. Peron 		if (slen != 0) {
168569f7644bSChristian S.J. Peron 			d->bd_fcount++;
168691433904SDavid Malone 			if (!gottime) {
168791433904SDavid Malone 				microtime(&tv);
168891433904SDavid Malone 				gottime = 1;
168991433904SDavid Malone 			}
16900c7fb534SRobert Watson #ifdef MAC
169130d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
16920c7fb534SRobert Watson #endif
16930c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
16944d621040SChristian S.J. Peron 				    bpf_append_mbuf, &tv);
16954ddfb531SChristian S.J. Peron 		}
1696e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1697df8bae1dSRodney W. Grimes 	}
1698e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1699df8bae1dSRodney W. Grimes }
1700df8bae1dSRodney W. Grimes 
1701df8bae1dSRodney W. Grimes /*
1702437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1703437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1704437ffe18SSam Leffler  */
1705437ffe18SSam Leffler void
170619ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1707437ffe18SSam Leffler {
1708437ffe18SSam Leffler 	struct mbuf mb;
1709437ffe18SSam Leffler 	struct bpf_d *d;
1710437ffe18SSam Leffler 	u_int pktlen, slen;
171191433904SDavid Malone 	int gottime;
171291433904SDavid Malone 	struct timeval tv;
171391433904SDavid Malone 
17148cd892f7SJung-uk Kim 	/* Skip outgoing duplicate packets. */
17158cd892f7SJung-uk Kim 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
17168cd892f7SJung-uk Kim 		m->m_flags &= ~M_PROMISC;
17178cd892f7SJung-uk Kim 		return;
17188cd892f7SJung-uk Kim 	}
17198cd892f7SJung-uk Kim 
172091433904SDavid Malone 	gottime = 0;
1721437ffe18SSam Leffler 
1722437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1723437ffe18SSam Leffler 	/*
1724437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1725437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1726437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1727437ffe18SSam Leffler 	 */
1728437ffe18SSam Leffler 	mb.m_next = m;
1729437ffe18SSam Leffler 	mb.m_data = data;
1730437ffe18SSam Leffler 	mb.m_len = dlen;
1731437ffe18SSam Leffler 	pktlen += dlen;
1732437ffe18SSam Leffler 
1733437ffe18SSam Leffler 	BPFIF_LOCK(bp);
17344a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1735f81a2a49SJung-uk Kim 		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1736437ffe18SSam Leffler 			continue;
1737437ffe18SSam Leffler 		BPFD_LOCK(d);
1738437ffe18SSam Leffler 		++d->bd_rcount;
173993e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
17404ddfb531SChristian S.J. Peron 		if (slen != 0) {
174169f7644bSChristian S.J. Peron 			d->bd_fcount++;
174291433904SDavid Malone 			if (!gottime) {
174391433904SDavid Malone 				microtime(&tv);
174491433904SDavid Malone 				gottime = 1;
174591433904SDavid Malone 			}
1746437ffe18SSam Leffler #ifdef MAC
174730d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1748437ffe18SSam Leffler #endif
1749437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
17504d621040SChristian S.J. Peron 				    bpf_append_mbuf, &tv);
17514ddfb531SChristian S.J. Peron 		}
1752437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1753437ffe18SSam Leffler 	}
1754437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1755437ffe18SSam Leffler }
1756437ffe18SSam Leffler 
1757560a54e1SJung-uk Kim #undef	BPF_CHECK_DIRECTION
1758560a54e1SJung-uk Kim 
1759437ffe18SSam Leffler /*
1760df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
17619e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1762df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
17634d621040SChristian S.J. Peron  * bpf_append_mbuf is passed in to copy mbuf chains.  In the latter case,
1764df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1765df8bae1dSRodney W. Grimes  */
1766df8bae1dSRodney W. Grimes static void
176719ba8395SChristian S.J. Peron catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
17684d621040SChristian S.J. Peron     void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
17694d621040SChristian S.J. Peron     struct timeval *tv)
1770df8bae1dSRodney W. Grimes {
17714d621040SChristian S.J. Peron 	struct bpf_hdr hdr;
17728994a245SDag-Erling Smørgrav 	int totlen, curlen;
17738994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
17747819da79SJohn-Mark Gurney 	int do_wakeup = 0;
17759e610888SDag-Erling Smørgrav 
1776a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
17774d621040SChristian S.J. Peron 
17784d621040SChristian S.J. Peron 	/*
17794d621040SChristian S.J. Peron 	 * Detect whether user space has released a buffer back to us, and if
17804d621040SChristian S.J. Peron 	 * so, move it from being a hold buffer to a free buffer.  This may
17814d621040SChristian S.J. Peron 	 * not be the best place to do it (for example, we might only want to
17824d621040SChristian S.J. Peron 	 * run this check if we need the space), but for now it's a reliable
17834d621040SChristian S.J. Peron 	 * spot to do it.
17844d621040SChristian S.J. Peron 	 */
1785fa0c2b34SRobert Watson 	if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
17864d621040SChristian S.J. Peron 		d->bd_fbuf = d->bd_hbuf;
17874d621040SChristian S.J. Peron 		d->bd_hbuf = NULL;
17884d621040SChristian S.J. Peron 		d->bd_hlen = 0;
178929f612ecSChristian S.J. Peron 		bpf_buf_reclaimed(d);
17904d621040SChristian S.J. Peron 	}
17914d621040SChristian S.J. Peron 
1792df8bae1dSRodney W. Grimes 	/*
1793df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1794df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1795df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1796df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1797df8bae1dSRodney W. Grimes 	 */
1798df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1799df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1800df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1801df8bae1dSRodney W. Grimes 
1802df8bae1dSRodney W. Grimes 	/*
1803df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1804a7a91e65SRobert Watson 	 *
1805a7a91e65SRobert Watson 	 * Drop the packet if there's no room and no hope of room
1806a7a91e65SRobert Watson 	 * If the packet would overflow the storage buffer or the storage
1807a7a91e65SRobert Watson 	 * buffer is considered immutable by the buffer model, try to rotate
1808a7a91e65SRobert Watson 	 * the buffer and wakeup pending processes.
1809df8bae1dSRodney W. Grimes 	 */
1810df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1811a7a91e65SRobert Watson 	if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
1812572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1813df8bae1dSRodney W. Grimes 			/*
1814a7a91e65SRobert Watson 			 * There's no room in the store buffer, and no
1815a7a91e65SRobert Watson 			 * prospect of room, so drop the packet.  Notify the
1816a7a91e65SRobert Watson 			 * buffer model.
1817df8bae1dSRodney W. Grimes 			 */
1818a7a91e65SRobert Watson 			bpf_buffull(d);
1819df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1820df8bae1dSRodney W. Grimes 			return;
1821df8bae1dSRodney W. Grimes 		}
1822df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
18237819da79SJohn-Mark Gurney 		do_wakeup = 1;
1824df8bae1dSRodney W. Grimes 		curlen = 0;
1825a7a91e65SRobert Watson 	} else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1826df8bae1dSRodney W. Grimes 		/*
18274d621040SChristian S.J. Peron 		 * Immediate mode is set, or the read timeout has already
18284d621040SChristian S.J. Peron 		 * expired during a select call.  A packet arrived, so the
18294d621040SChristian S.J. Peron 		 * reader should be woken up.
1830df8bae1dSRodney W. Grimes 		 */
18317819da79SJohn-Mark Gurney 		do_wakeup = 1;
1832df8bae1dSRodney W. Grimes 
1833df8bae1dSRodney W. Grimes 	/*
18344d621040SChristian S.J. Peron 	 * Append the bpf header.  Note we append the actual header size, but
18354d621040SChristian S.J. Peron 	 * move forward the length of the header plus padding.
1836df8bae1dSRodney W. Grimes 	 */
18374d621040SChristian S.J. Peron 	bzero(&hdr, sizeof(hdr));
18384d621040SChristian S.J. Peron 	hdr.bh_tstamp = *tv;
18394d621040SChristian S.J. Peron 	hdr.bh_datalen = pktlen;
18404d621040SChristian S.J. Peron 	hdr.bh_hdrlen = hdrlen;
18414d621040SChristian S.J. Peron 	hdr.bh_caplen = totlen - hdrlen;
18424d621040SChristian S.J. Peron 	bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
18434d621040SChristian S.J. Peron 
1844df8bae1dSRodney W. Grimes 	/*
1845df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1846df8bae1dSRodney W. Grimes 	 */
18474d621040SChristian S.J. Peron 	(*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, hdr.bh_caplen);
1848df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
18497819da79SJohn-Mark Gurney 
18507819da79SJohn-Mark Gurney 	if (do_wakeup)
18517819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1852df8bae1dSRodney W. Grimes }
1853df8bae1dSRodney W. Grimes 
1854df8bae1dSRodney W. Grimes /*
1855df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1856df8bae1dSRodney W. Grimes  * Called on close.
1857df8bae1dSRodney W. Grimes  */
1858df8bae1dSRodney W. Grimes static void
185919ba8395SChristian S.J. Peron bpf_freed(struct bpf_d *d)
1860df8bae1dSRodney W. Grimes {
18614d621040SChristian S.J. Peron 
1862df8bae1dSRodney W. Grimes 	/*
1863df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1864df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1865df8bae1dSRodney W. Grimes 	 * free.
1866df8bae1dSRodney W. Grimes 	 */
18674d621040SChristian S.J. Peron 	bpf_free(d);
1868ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
186993e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1870ae275efcSJung-uk Kim #ifdef BPF_JITTER
1871ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1872ae275efcSJung-uk Kim #endif
1873ae275efcSJung-uk Kim 	}
187493e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
187593e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1876e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1877df8bae1dSRodney W. Grimes }
1878df8bae1dSRodney W. Grimes 
1879df8bae1dSRodney W. Grimes /*
188024a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
188124a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1882df8bae1dSRodney W. Grimes  */
1883df8bae1dSRodney W. Grimes void
188419ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1885df8bae1dSRodney W. Grimes {
188624a229f4SSam Leffler 
188724a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
188824a229f4SSam Leffler }
188924a229f4SSam Leffler 
189024a229f4SSam Leffler /*
189124a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
189224a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
189324a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
189424a229f4SSam Leffler  * headers are not yet supporrted).
189524a229f4SSam Leffler  */
189624a229f4SSam Leffler void
189719ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
189824a229f4SSam Leffler {
1899df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
190019ba8395SChristian S.J. Peron 
190119ba8395SChristian S.J. Peron 	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1902572bde2aSRobert Watson 	if (bp == NULL)
1903df8bae1dSRodney W. Grimes 		panic("bpfattach");
1904df8bae1dSRodney W. Grimes 
19054a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
1906df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1907df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
19086008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
190916d878ccSChristian S.J. Peron 	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
191016d878ccSChristian S.J. Peron 	*driverp = bp;
1911df8bae1dSRodney W. Grimes 
1912e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
19134a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1914e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1915df8bae1dSRodney W. Grimes 
1916df8bae1dSRodney W. Grimes 	/*
1917df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1918df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1919df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1920df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1921df8bae1dSRodney W. Grimes 	 */
1922df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1923df8bae1dSRodney W. Grimes 
19242eeab939SGarrett Wollman 	if (bootverbose)
192524a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1926df8bae1dSRodney W. Grimes }
192753ac6efbSJulian Elischer 
1928de5d9935SRobert Watson /*
1929de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1930de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1931de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1932de5d9935SRobert Watson  * ENXIO.
1933de5d9935SRobert Watson  */
1934de5d9935SRobert Watson void
193519ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
1936de5d9935SRobert Watson {
19374a3feeaaSRobert Watson 	struct bpf_if	*bp;
1938de5d9935SRobert Watson 	struct bpf_d	*d;
1939de5d9935SRobert Watson 
1940de5d9935SRobert Watson 	/* Locate BPF interface information */
19418eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
19424a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1943de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1944de5d9935SRobert Watson 			break;
1945de5d9935SRobert Watson 	}
1946de5d9935SRobert Watson 
1947de5d9935SRobert Watson 	/* Interface wasn't attached */
1948d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1949e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
19509bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1951de5d9935SRobert Watson 		return;
1952de5d9935SRobert Watson 	}
1953de5d9935SRobert Watson 
19544a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
19558eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1956de5d9935SRobert Watson 
19574a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1958e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1959e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1960e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1961e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1962e7bb21b3SJonathan Lemon 	}
1963e7bb21b3SJonathan Lemon 
1964e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1965de5d9935SRobert Watson 	free(bp, M_BPF);
19668eab61f3SSam Leffler }
1967de5d9935SRobert Watson 
19688eab61f3SSam Leffler /*
19698eab61f3SSam Leffler  * Get a list of available data link type of the interface.
19708eab61f3SSam Leffler  */
19718eab61f3SSam Leffler static int
197219ba8395SChristian S.J. Peron bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
19738eab61f3SSam Leffler {
19748eab61f3SSam Leffler 	int n, error;
19758eab61f3SSam Leffler 	struct ifnet *ifp;
19768eab61f3SSam Leffler 	struct bpf_if *bp;
19778eab61f3SSam Leffler 
19788eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
19798eab61f3SSam Leffler 	n = 0;
19808eab61f3SSam Leffler 	error = 0;
19818eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
19824a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
19838eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
19848eab61f3SSam Leffler 			continue;
19858eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
19868eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1987e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
19888eab61f3SSam Leffler 				return (ENOMEM);
19898eab61f3SSam Leffler 			}
19908eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
19918eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
19928eab61f3SSam Leffler 		}
19938eab61f3SSam Leffler 		n++;
19948eab61f3SSam Leffler 	}
19958eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
19968eab61f3SSam Leffler 	bfl->bfl_len = n;
19978eab61f3SSam Leffler 	return (error);
19988eab61f3SSam Leffler }
19998eab61f3SSam Leffler 
20008eab61f3SSam Leffler /*
20018eab61f3SSam Leffler  * Set the data link type of a BPF instance.
20028eab61f3SSam Leffler  */
20038eab61f3SSam Leffler static int
200419ba8395SChristian S.J. Peron bpf_setdlt(struct bpf_d *d, u_int dlt)
20058eab61f3SSam Leffler {
20068eab61f3SSam Leffler 	int error, opromisc;
20078eab61f3SSam Leffler 	struct ifnet *ifp;
20088eab61f3SSam Leffler 	struct bpf_if *bp;
20098eab61f3SSam Leffler 
20108eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
20118eab61f3SSam Leffler 		return (0);
20128eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
20138eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
20144a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
20158eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
20168eab61f3SSam Leffler 			break;
20178eab61f3SSam Leffler 	}
20188eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
20198eab61f3SSam Leffler 	if (bp != NULL) {
20208eab61f3SSam Leffler 		opromisc = d->bd_promisc;
20218eab61f3SSam Leffler 		bpf_detachd(d);
20228eab61f3SSam Leffler 		bpf_attachd(d, bp);
202393daabddSBrian Feldman 		BPFD_LOCK(d);
20248eab61f3SSam Leffler 		reset_d(d);
20258eab61f3SSam Leffler 		BPFD_UNLOCK(d);
20268eab61f3SSam Leffler 		if (opromisc) {
20278eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
20288eab61f3SSam Leffler 			if (error)
20298eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
20308eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
20318eab61f3SSam Leffler 					error);
20328eab61f3SSam Leffler 			else
20338eab61f3SSam Leffler 				d->bd_promisc = 1;
20348eab61f3SSam Leffler 		}
20358eab61f3SSam Leffler 	}
20368eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
2037de5d9935SRobert Watson }
2038de5d9935SRobert Watson 
20393f54a085SPoul-Henning Kamp static void
204019ba8395SChristian S.J. Peron bpf_drvinit(void *unused)
204153ac6efbSJulian Elischer {
2042136600feSEd Schouten 	struct cdev *dev;
204353ac6efbSJulian Elischer 
20446008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
20454a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
2046136600feSEd Schouten 
2047136600feSEd Schouten 	dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf");
2048136600feSEd Schouten 	/* For compatibility */
2049136600feSEd Schouten 	make_dev_alias(dev, "bpf0");
20507198bf47SJulian Elischer }
205153ac6efbSJulian Elischer 
20520e37f3e1SChristian S.J. Peron /*
20530e37f3e1SChristian S.J. Peron  * Zero out the various packet counters associated with all of the bpf
20540e37f3e1SChristian S.J. Peron  * descriptors.  At some point, we will probably want to get a bit more
20550e37f3e1SChristian S.J. Peron  * granular and allow the user to specify descriptors to be zeroed.
20560e37f3e1SChristian S.J. Peron  */
20570e37f3e1SChristian S.J. Peron static void
20580e37f3e1SChristian S.J. Peron bpf_zero_counters(void)
20590e37f3e1SChristian S.J. Peron {
20600e37f3e1SChristian S.J. Peron 	struct bpf_if *bp;
20610e37f3e1SChristian S.J. Peron 	struct bpf_d *bd;
20620e37f3e1SChristian S.J. Peron 
20630e37f3e1SChristian S.J. Peron 	mtx_lock(&bpf_mtx);
20640e37f3e1SChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
20650e37f3e1SChristian S.J. Peron 		BPFIF_LOCK(bp);
20660e37f3e1SChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
20670e37f3e1SChristian S.J. Peron 			BPFD_LOCK(bd);
20680e37f3e1SChristian S.J. Peron 			bd->bd_rcount = 0;
20690e37f3e1SChristian S.J. Peron 			bd->bd_dcount = 0;
20700e37f3e1SChristian S.J. Peron 			bd->bd_fcount = 0;
20710e37f3e1SChristian S.J. Peron 			bd->bd_wcount = 0;
20720e37f3e1SChristian S.J. Peron 			bd->bd_wfcount = 0;
20730e37f3e1SChristian S.J. Peron 			bd->bd_zcopy = 0;
20740e37f3e1SChristian S.J. Peron 			BPFD_UNLOCK(bd);
20750e37f3e1SChristian S.J. Peron 		}
20760e37f3e1SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
20770e37f3e1SChristian S.J. Peron 	}
20780e37f3e1SChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
20790e37f3e1SChristian S.J. Peron }
20800e37f3e1SChristian S.J. Peron 
208169f7644bSChristian S.J. Peron static void
208269f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
208369f7644bSChristian S.J. Peron {
208469f7644bSChristian S.J. Peron 
208569f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
208669f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
20874d621040SChristian S.J. Peron 	d->bd_structsize = sizeof(*d);
208869f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
208969f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
209069f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
2091560a54e1SJung-uk Kim 	d->bd_direction = bd->bd_direction;
2092560a54e1SJung-uk Kim 	d->bd_feedback = bd->bd_feedback;
209369f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
209469f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
209569f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
209669f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
209769f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
209869f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
209969f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
210069f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
210169f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
210269f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
210369f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
210493e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
21054d621040SChristian S.J. Peron 	d->bd_wcount = bd->bd_wcount;
21064d621040SChristian S.J. Peron 	d->bd_wdcount = bd->bd_wdcount;
21074d621040SChristian S.J. Peron 	d->bd_wfcount = bd->bd_wfcount;
21084d621040SChristian S.J. Peron 	d->bd_zcopy = bd->bd_zcopy;
21094d621040SChristian S.J. Peron 	d->bd_bufmode = bd->bd_bufmode;
211069f7644bSChristian S.J. Peron }
211169f7644bSChristian S.J. Peron 
211269f7644bSChristian S.J. Peron static int
211369f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
211469f7644bSChristian S.J. Peron {
21150e37f3e1SChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd, zerostats;
2116422a63daSChristian S.J. Peron 	int index, error;
211769f7644bSChristian S.J. Peron 	struct bpf_if *bp;
211869f7644bSChristian S.J. Peron 	struct bpf_d *bd;
211969f7644bSChristian S.J. Peron 
212069f7644bSChristian S.J. Peron 	/*
212169f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
212269f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
212369f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
212469f7644bSChristian S.J. Peron 	 * the statistics for them, too.
212569f7644bSChristian S.J. Peron 	 */
2126acd3428bSRobert Watson 	error = priv_check(req->td, PRIV_NET_BPF);
212769f7644bSChristian S.J. Peron 	if (error)
212869f7644bSChristian S.J. Peron 		return (error);
21290e37f3e1SChristian S.J. Peron 	/*
21300e37f3e1SChristian S.J. Peron 	 * Check to see if the user is requesting that the counters be
21310e37f3e1SChristian S.J. Peron 	 * zeroed out.  Explicitly check that the supplied data is zeroed,
21320e37f3e1SChristian S.J. Peron 	 * as we aren't allowing the user to set the counters currently.
21330e37f3e1SChristian S.J. Peron 	 */
21340e37f3e1SChristian S.J. Peron 	if (req->newptr != NULL) {
21350e37f3e1SChristian S.J. Peron 		if (req->newlen != sizeof(zerostats))
21360e37f3e1SChristian S.J. Peron 			return (EINVAL);
21370e37f3e1SChristian S.J. Peron 		bzero(&zerostats, sizeof(zerostats));
21380e37f3e1SChristian S.J. Peron 		xbd = req->newptr;
21390e37f3e1SChristian S.J. Peron 		if (bcmp(xbd, &zerostats, sizeof(*xbd)) != 0)
21400e37f3e1SChristian S.J. Peron 			return (EINVAL);
21410e37f3e1SChristian S.J. Peron 		bpf_zero_counters();
21420e37f3e1SChristian S.J. Peron 		return (0);
21430e37f3e1SChristian S.J. Peron 	}
214469f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
2145422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
214669f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
214769f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
2148422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
214969f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
2150422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2151422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
2152422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
2153422a63daSChristian S.J. Peron 		return (ENOMEM);
2154422a63daSChristian S.J. Peron 	}
2155422a63daSChristian S.J. Peron 	index = 0;
215669f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
21571fc9e387SChristian S.J. Peron 		BPFIF_LOCK(bp);
215869f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2159422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
216069f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
2161422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
216269f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
216369f7644bSChristian S.J. Peron 		}
21641fc9e387SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
216569f7644bSChristian S.J. Peron 	}
216669f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
2167422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2168422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
216969f7644bSChristian S.J. Peron 	return (error);
217069f7644bSChristian S.J. Peron }
217169f7644bSChristian S.J. Peron 
2172237fdd78SRobert Watson SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
217353ac6efbSJulian Elischer 
21745bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
2175f8dc4716SMike Smith /*
2176f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
2177f8dc4716SMike Smith  *
2178f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
2179f8dc4716SMike Smith  * to be loaded at runtime.
2180f8dc4716SMike Smith  */
21817eae78a4SChristian S.J. Peron static struct bpf_if bp_null;
2182f8dc4716SMike Smith 
2183f8dc4716SMike Smith void
218419ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2185f8dc4716SMike Smith {
2186f8dc4716SMike Smith }
2187f8dc4716SMike Smith 
2188f8dc4716SMike Smith void
218919ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2190f8dc4716SMike Smith {
2191f8dc4716SMike Smith }
2192f8dc4716SMike Smith 
2193f8dc4716SMike Smith void
219419ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
2195437ffe18SSam Leffler {
2196437ffe18SSam Leffler }
2197437ffe18SSam Leffler 
2198437ffe18SSam Leffler void
219919ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2200f8dc4716SMike Smith {
22017eae78a4SChristian S.J. Peron 
22027eae78a4SChristian S.J. Peron 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2203f8dc4716SMike Smith }
2204f8dc4716SMike Smith 
2205da626c17SBill Paul void
220619ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
22075f7a7923SSam Leffler {
22087eae78a4SChristian S.J. Peron 
22097eae78a4SChristian S.J. Peron 	*driverp = &bp_null;
22105f7a7923SSam Leffler }
22115f7a7923SSam Leffler 
22125f7a7923SSam Leffler void
221319ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
2214da626c17SBill Paul {
2215da626c17SBill Paul }
2216da626c17SBill Paul 
2217f8dc4716SMike Smith u_int
221819ba8395SChristian S.J. Peron bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
2219f8dc4716SMike Smith {
2220f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
2221f8dc4716SMike Smith }
2222f8dc4716SMike Smith 
22235bb5f2c9SPeter Wemm int
222419ba8395SChristian S.J. Peron bpf_validate(const struct bpf_insn *f, int len)
22255bb5f2c9SPeter Wemm {
22265bb5f2c9SPeter Wemm 	return 0;		/* false */
22275bb5f2c9SPeter Wemm }
22285bb5f2c9SPeter Wemm 
22295bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
2230