xref: /freebsd/sys/net/bpf.c (revision d83e603ac7d5731331175d2f5328776cba896438)
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  *
36c3aac50fSPeter Wemm  * $FreeBSD$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
395bb5f2c9SPeter Wemm #include "opt_bpf.h"
4082f4445dSRobert Watson #include "opt_mac.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>
484d1d4912SBruce Evans #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50df8bae1dSRodney W. Grimes #include <sys/time.h>
51acd3428bSRobert Watson #include <sys/priv.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
530310c19fSBruce Evans #include <sys/signalvar.h>
54528f627fSBruce Evans #include <sys/filio.h>
55528f627fSBruce Evans #include <sys/sockio.h>
56528f627fSBruce Evans #include <sys/ttycom.h>
57e76eee55SPoul-Henning Kamp #include <sys/uio.h>
58df8bae1dSRodney W. Grimes 
5995aab9ccSJohn-Mark Gurney #include <sys/event.h>
6095aab9ccSJohn-Mark Gurney #include <sys/file.h>
61243ac7d8SPeter Wemm #include <sys/poll.h>
6295aab9ccSJohn-Mark Gurney #include <sys/proc.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes 
66fba9235dSBruce Evans #include <net/if.h>
67df8bae1dSRodney W. Grimes #include <net/bpf.h>
68ae275efcSJung-uk Kim #ifdef BPF_JITTER
69ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
70ae275efcSJung-uk Kim #endif
71df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <netinet/in.h>
74df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
75df8bae1dSRodney W. Grimes #include <sys/kernel.h>
76f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
777b778b5eSEivind Eklund 
78246b5467SSam Leffler #include <net80211/ieee80211_freebsd.h>
79246b5467SSam Leffler 
80aed55708SRobert Watson #include <security/mac/mac_framework.h>
81aed55708SRobert Watson 
82959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8387f6c662SJulian Elischer 
845bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8553ac6efbSJulian Elischer 
86df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
87df8bae1dSRodney W. Grimes 
88560a54e1SJung-uk Kim #define	M_SKIP_BPF	M_SKIP_FIREWALL
89560a54e1SJung-uk Kim 
90df8bae1dSRodney W. Grimes /*
91d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
92d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
93d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
94d1a67300SRobert Watson  * frames, ethernet frames, etc).
95df8bae1dSRodney W. Grimes  */
964a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
97e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
9869f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
99df8bae1dSRodney W. Grimes 
100a3594432SRobert Watson static void	bpf_allocbufs(struct bpf_d *);
10119ba8395SChristian S.J. Peron static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
10219ba8395SChristian S.J. Peron static void	bpf_detachd(struct bpf_d *);
103929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
104929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
105560a54e1SJung-uk Kim static int	bpf_movein(struct uio *, int, int, 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 *);
111929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
11291433904SDavid Malone 		    u_int, void (*)(const void *, void *, size_t),
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 *);
1216a113b3dSRobert Watson static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
12269f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
12369f7644bSChristian S.J. Peron 
12469f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12569f7644bSChristian S.J. Peron static int bpf_bufsize = 4096;
12669f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
1275d1f8283SRobert Watson     &bpf_bufsize, 0, "Default bpf buffer size");
12869f7644bSChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
12969f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
1305d1f8283SRobert Watson     &bpf_maxbufsize, 0, "Maximum bpf buffer size");
13169f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
13269f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
13369f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
13469f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13569f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
136df8bae1dSRodney W. Grimes 
13787f6c662SJulian Elischer static	d_open_t	bpfopen;
13887f6c662SJulian Elischer static	d_close_t	bpfclose;
13987f6c662SJulian Elischer static	d_read_t	bpfread;
14087f6c662SJulian Elischer static	d_write_t	bpfwrite;
14187f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
142243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
14395aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
14487f6c662SJulian Elischer 
1454e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
146dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1477ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1487ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1497ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1507ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1517ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1527ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1537ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
15495aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1554e2f199eSPoul-Henning Kamp };
15687f6c662SJulian Elischer 
15795aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
15895aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
15987f6c662SJulian Elischer 
160df8bae1dSRodney W. Grimes static int
16119ba8395SChristian S.J. Peron bpf_movein(struct uio *uio, int linktype, int mtu, struct mbuf **mp,
162560a54e1SJung-uk Kim     struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
163df8bae1dSRodney W. Grimes {
164246b5467SSam Leffler 	const struct ieee80211_bpf_params *p;
165df8bae1dSRodney W. Grimes 	struct mbuf *m;
166df8bae1dSRodney W. Grimes 	int error;
167df8bae1dSRodney W. Grimes 	int len;
168df8bae1dSRodney W. Grimes 	int hlen;
16993e39f0bSChristian S.J. Peron 	int slen;
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes 	/*
172df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
173df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
174df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
175df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
176df8bae1dSRodney W. Grimes 	 * is forwarded as is.
177df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
178df8bae1dSRodney W. Grimes 	 * for the link level header.
179df8bae1dSRodney W. Grimes 	 */
180df8bae1dSRodney W. Grimes 	switch (linktype) {
181df8bae1dSRodney W. Grimes 
182df8bae1dSRodney W. Grimes 	case DLT_SLIP:
183df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
184df8bae1dSRodney W. Grimes 		hlen = 0;
185df8bae1dSRodney W. Grimes 		break;
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
188df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
189df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
190797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
191df8bae1dSRodney W. Grimes 		break;
192df8bae1dSRodney W. Grimes 
193df8bae1dSRodney W. Grimes 	case DLT_FDDI:
194d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
195d41f24e7SDavid Greenman 		hlen = 0;
196df8bae1dSRodney W. Grimes 		break;
197df8bae1dSRodney W. Grimes 
19822f05c43SAndrey A. Chernov 	case DLT_RAW:
199df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
200df8bae1dSRodney W. Grimes 		hlen = 0;
201df8bae1dSRodney W. Grimes 		break;
202df8bae1dSRodney W. Grimes 
20301399f34SDavid Malone 	case DLT_NULL:
20401399f34SDavid Malone 		/*
20501399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
20601399f34SDavid Malone 		 * corresponds to the address family of the packet.
20701399f34SDavid Malone 		 */
20801399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
20901399f34SDavid Malone 		hlen = 4;
21001399f34SDavid Malone 		break;
21101399f34SDavid Malone 
2124f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
2134f53e3ccSKenjiro Cho 		/*
2144f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
2154f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
2164f53e3ccSKenjiro Cho 		 * specified anyway.
2174f53e3ccSKenjiro Cho 		 */
2184f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
2194f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
2204f53e3ccSKenjiro Cho 		break;
2214f53e3ccSKenjiro Cho 
22230fa52a6SBrian Somers 	case DLT_PPP:
22330fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
22430fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
22530fa52a6SBrian Somers 		break;
22630fa52a6SBrian Somers 
227246b5467SSam Leffler 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
228246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
229246b5467SSam Leffler 		hlen = 0;
230246b5467SSam Leffler 		break;
231246b5467SSam Leffler 
232246b5467SSam Leffler 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
233246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
234246b5467SSam Leffler 		sockp->sa_len = 12;	/* XXX != 0 */
235246b5467SSam Leffler 		hlen = sizeof(struct ieee80211_bpf_params);
236246b5467SSam Leffler 		break;
237246b5467SSam Leffler 
238df8bae1dSRodney W. Grimes 	default:
239df8bae1dSRodney W. Grimes 		return (EIO);
240df8bae1dSRodney W. Grimes 	}
241df8bae1dSRodney W. Grimes 
242df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
24301399f34SDavid Malone 
24401399f34SDavid Malone 	if (len - hlen > mtu)
24501399f34SDavid Malone 		return (EMSGSIZE);
24601399f34SDavid Malone 
247df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
248df8bae1dSRodney W. Grimes 		return (EIO);
249df8bae1dSRodney W. Grimes 
250963e4c2aSGarrett Wollman 	if (len > MHLEN) {
251a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
25224a229f4SSam Leffler 	} else {
253a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
254df8bae1dSRodney W. Grimes 	}
25524a229f4SSam Leffler 	if (m == NULL)
25624a229f4SSam Leffler 		return (ENOBUFS);
257963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
258963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
259df8bae1dSRodney W. Grimes 	*mp = m;
26024a229f4SSam Leffler 
26193e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
26293e39f0bSChristian S.J. Peron 		error = EPERM;
26393e39f0bSChristian S.J. Peron 		goto bad;
26493e39f0bSChristian S.J. Peron 	}
26593e39f0bSChristian S.J. Peron 
26693e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
26793e39f0bSChristian S.J. Peron 	if (error)
26893e39f0bSChristian S.J. Peron 		goto bad;
26993e39f0bSChristian S.J. Peron 
27093e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
27193e39f0bSChristian S.J. Peron 	if (slen == 0) {
27293e39f0bSChristian S.J. Peron 		error = EPERM;
27393e39f0bSChristian S.J. Peron 		goto bad;
27493e39f0bSChristian S.J. Peron 	}
27593e39f0bSChristian S.J. Peron 
276df8bae1dSRodney W. Grimes 	/*
27793e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
278df8bae1dSRodney W. Grimes 	 */
279df8bae1dSRodney W. Grimes 	if (hlen != 0) {
280246b5467SSam Leffler 		if (sockp->sa_family == AF_IEEE80211) {
281246b5467SSam Leffler 			/*
282246b5467SSam Leffler 			 * Collect true length from the parameter header
283246b5467SSam Leffler 			 * NB: sockp is known to be zero'd so if we do a
284246b5467SSam Leffler 			 *     short copy unspecified parameters will be
285246b5467SSam Leffler 			 *     zero.
286246b5467SSam Leffler 			 * NB: packet may not be aligned after stripping
287246b5467SSam Leffler 			 *     bpf params
288246b5467SSam Leffler 			 * XXX check ibp_vers
289246b5467SSam Leffler 			 */
290246b5467SSam Leffler 			p = mtod(m, const struct ieee80211_bpf_params *);
291246b5467SSam Leffler 			hlen = p->ibp_len;
292246b5467SSam Leffler 			if (hlen > sizeof(sockp->sa_data)) {
293246b5467SSam Leffler 				error = EINVAL;
294246b5467SSam Leffler 				goto bad;
295246b5467SSam Leffler 			}
296246b5467SSam Leffler 		}
29793e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
298df8bae1dSRodney W. Grimes 	}
299560a54e1SJung-uk Kim 	*hdrlen = hlen;
30093e39f0bSChristian S.J. Peron 
301df8bae1dSRodney W. Grimes 	return (0);
302df8bae1dSRodney W. Grimes bad:
303df8bae1dSRodney W. Grimes 	m_freem(m);
304df8bae1dSRodney W. Grimes 	return (error);
305df8bae1dSRodney W. Grimes }
306df8bae1dSRodney W. Grimes 
307df8bae1dSRodney W. Grimes /*
308df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
309df8bae1dSRodney W. Grimes  */
310df8bae1dSRodney W. Grimes static void
31119ba8395SChristian S.J. Peron bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
312df8bae1dSRodney W. Grimes {
313df8bae1dSRodney W. Grimes 	/*
314df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
315df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
316df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
317df8bae1dSRodney W. Grimes 	 */
318e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
319df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
3204a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
321df8bae1dSRodney W. Grimes 
32269f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
323e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
324df8bae1dSRodney W. Grimes }
325df8bae1dSRodney W. Grimes 
326df8bae1dSRodney W. Grimes /*
327df8bae1dSRodney W. Grimes  * Detach a file from its interface.
328df8bae1dSRodney W. Grimes  */
329df8bae1dSRodney W. Grimes static void
33019ba8395SChristian S.J. Peron bpf_detachd(struct bpf_d *d)
331df8bae1dSRodney W. Grimes {
3326e891d64SPoul-Henning Kamp 	int error;
333df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
33446448b5aSRobert Watson 	struct ifnet *ifp;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
33746448b5aSRobert Watson 	BPFIF_LOCK(bp);
33846448b5aSRobert Watson 	BPFD_LOCK(d);
33946448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
34046448b5aSRobert Watson 
34146448b5aSRobert Watson 	/*
34246448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
34346448b5aSRobert Watson 	 */
34446448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
34546448b5aSRobert Watson 
34669f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
347572bde2aSRobert Watson 	d->bd_bif = NULL;
34846448b5aSRobert Watson 	BPFD_UNLOCK(d);
34946448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
35046448b5aSRobert Watson 
351df8bae1dSRodney W. Grimes 	/*
352df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
353df8bae1dSRodney W. Grimes 	 * If so, turn it off.
354df8bae1dSRodney W. Grimes 	 */
355df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
356df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
35746448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3586e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
359df8bae1dSRodney W. Grimes 			/*
3606e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
361df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
362df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
363df8bae1dSRodney W. Grimes 			 * take it out.
364df8bae1dSRodney W. Grimes 			 */
3658eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3668eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3676e891d64SPoul-Henning Kamp 		}
368df8bae1dSRodney W. Grimes 	}
369df8bae1dSRodney W. Grimes }
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes /*
372df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
373df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
374df8bae1dSRodney W. Grimes  */
375df8bae1dSRodney W. Grimes /* ARGSUSED */
37687f6c662SJulian Elischer static	int
37719ba8395SChristian S.J. Peron bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
378df8bae1dSRodney W. Grimes {
379e7bb21b3SJonathan Lemon 	struct bpf_d *d;
380df8bae1dSRodney W. Grimes 
381e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
382bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
383df8bae1dSRodney W. Grimes 	/*
384df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
385df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
386df8bae1dSRodney W. Grimes 	 */
387d17d8184SRobert Watson 	if (d != NULL) {
388e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
389df8bae1dSRodney W. Grimes 		return (EBUSY);
390e7bb21b3SJonathan Lemon 	}
391e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
392e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
393e7bb21b3SJonathan Lemon 
394d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
395b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
396b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
397a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
398bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
399df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
40000a83887SPaul Traina 	d->bd_sig = SIGIO;
401560a54e1SJung-uk Kim 	d->bd_direction = BPF_D_INOUT;
40269f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
40382f4445dSRobert Watson #ifdef MAC
40482f4445dSRobert Watson 	mac_init_bpfdesc(d);
40582f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
40682f4445dSRobert Watson #endif
4076008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
40831199c84SGleb Smirnoff 	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
409571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	return (0);
412df8bae1dSRodney W. Grimes }
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes /*
415df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
416df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
417df8bae1dSRodney W. Grimes  */
418df8bae1dSRodney W. Grimes /* ARGSUSED */
41987f6c662SJulian Elischer static	int
42019ba8395SChristian S.J. Peron bpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
421df8bae1dSRodney W. Grimes {
422e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
423df8bae1dSRodney W. Grimes 
42481bda851SJohn Polstra 	BPFD_LOCK(d);
42581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42681bda851SJohn Polstra 		callout_stop(&d->bd_callout);
42781bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
42881bda851SJohn Polstra 	BPFD_UNLOCK(d);
429e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
430e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
431df8bae1dSRodney W. Grimes 	if (d->bd_bif)
432df8bae1dSRodney W. Grimes 		bpf_detachd(d);
433e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
4344549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
43582f4445dSRobert Watson #ifdef MAC
43682f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
43782f4445dSRobert Watson #endif /* MAC */
438ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
439df8bae1dSRodney W. Grimes 	bpf_freed(d);
440d17d8184SRobert Watson 	dev->si_drv1 = NULL;
441d722be54SLuigi Rizzo 	free(d, M_BPF);
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	return (0);
444df8bae1dSRodney W. Grimes }
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 
447df8bae1dSRodney W. Grimes /*
448df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
449df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
450df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
451df8bae1dSRodney W. Grimes  */
452df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
453df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
454df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
455df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
456df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
457d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
458df8bae1dSRodney W. Grimes /*
459df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
460df8bae1dSRodney W. Grimes  */
46187f6c662SJulian Elischer static	int
46219ba8395SChristian S.J. Peron bpfread(struct cdev *dev, struct uio *uio, int ioflag)
463df8bae1dSRodney W. Grimes {
464e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
46581bda851SJohn Polstra 	int timed_out;
466df8bae1dSRodney W. Grimes 	int error;
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes 	/*
469df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
470df8bae1dSRodney W. Grimes 	 * as kernel buffers.
471df8bae1dSRodney W. Grimes 	 */
472df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
473df8bae1dSRodney W. Grimes 		return (EINVAL);
474df8bae1dSRodney W. Grimes 
475e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
47681bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
47781bda851SJohn Polstra 		callout_stop(&d->bd_callout);
47881bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
47981bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
480df8bae1dSRodney W. Grimes 	/*
481df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
482df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
483df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
484df8bae1dSRodney W. Grimes 	 */
485572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
48681bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
487df8bae1dSRodney W. Grimes 			/*
488df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
489df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
490df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
491df8bae1dSRodney W. Grimes 			 */
492df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
493df8bae1dSRodney W. Grimes 			break;
494df8bae1dSRodney W. Grimes 		}
495de5d9935SRobert Watson 
496de5d9935SRobert Watson 		/*
497de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
498de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
499de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
500de5d9935SRobert Watson 		 * it before using it again.
501de5d9935SRobert Watson 		 */
502de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
503e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
504de5d9935SRobert Watson 			return (ENXIO);
505de5d9935SRobert Watson 		}
506de5d9935SRobert Watson 
507e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
508e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
509fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
510fba3cfdeSJohn Polstra 		}
511521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
512e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
513df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
514e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
515df8bae1dSRodney W. Grimes 			return (error);
516df8bae1dSRodney W. Grimes 		}
517df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
518df8bae1dSRodney W. Grimes 			/*
519df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
520df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
521df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
522df8bae1dSRodney W. Grimes 			 */
523df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
524df8bae1dSRodney W. Grimes 				/*
525df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
526df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
527df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
528df8bae1dSRodney W. Grimes 				 */
529df8bae1dSRodney W. Grimes 				break;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
532e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
533df8bae1dSRodney W. Grimes 				return (0);
534df8bae1dSRodney W. Grimes 			}
535df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
536df8bae1dSRodney W. Grimes 			break;
537df8bae1dSRodney W. Grimes 		}
538df8bae1dSRodney W. Grimes 	}
539df8bae1dSRodney W. Grimes 	/*
540df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
541df8bae1dSRodney W. Grimes 	 */
542e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes 	/*
545df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
546df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
547df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
548df8bae1dSRodney W. Grimes 	 */
549e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
550df8bae1dSRodney W. Grimes 
551e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
552df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
553572bde2aSRobert Watson 	d->bd_hbuf = NULL;
554df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
555e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 	return (error);
558df8bae1dSRodney W. Grimes }
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes /*
562df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
563df8bae1dSRodney W. Grimes  */
564e7bb21b3SJonathan Lemon static __inline void
56519ba8395SChristian S.J. Peron bpf_wakeup(struct bpf_d *d)
566df8bae1dSRodney W. Grimes {
567a3272e3cSChristian S.J. Peron 
568a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
56981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
57081bda851SJohn Polstra 		callout_stop(&d->bd_callout);
57181bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
57281bda851SJohn Polstra 	}
573521f364bSDag-Erling Smørgrav 	wakeup(d);
574831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
575f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
57600a83887SPaul Traina 
577512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
578ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
579df8bae1dSRodney W. Grimes }
580df8bae1dSRodney W. Grimes 
58181bda851SJohn Polstra static void
58219ba8395SChristian S.J. Peron bpf_timed_out(void *arg)
58381bda851SJohn Polstra {
58481bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
58581bda851SJohn Polstra 
58681bda851SJohn Polstra 	BPFD_LOCK(d);
58781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
58881bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
58981bda851SJohn Polstra 		if (d->bd_slen != 0)
59081bda851SJohn Polstra 			bpf_wakeup(d);
59181bda851SJohn Polstra 	}
59281bda851SJohn Polstra 	BPFD_UNLOCK(d);
59381bda851SJohn Polstra }
59481bda851SJohn Polstra 
59587f6c662SJulian Elischer static int
59619ba8395SChristian S.J. Peron bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
597df8bae1dSRodney W. Grimes {
598e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
599df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
600560a54e1SJung-uk Kim 	struct mbuf *m, *mc;
6018240bf1eSRobert Watson 	struct sockaddr dst;
602560a54e1SJung-uk Kim 	int error, hlen;
603df8bae1dSRodney W. Grimes 
604572bde2aSRobert Watson 	if (d->bd_bif == NULL)
605df8bae1dSRodney W. Grimes 		return (ENXIO);
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
608df8bae1dSRodney W. Grimes 
6093518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
6103518d220SSam Leffler 		return (ENETDOWN);
6113518d220SSam Leffler 
612df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
613df8bae1dSRodney W. Grimes 		return (0);
614df8bae1dSRodney W. Grimes 
6158240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
616d83e603aSChristian S.J. Peron 	m = NULL;
617d83e603aSChristian S.J. Peron 	hlen = 0;
61893e39f0bSChristian S.J. Peron 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu,
619560a54e1SJung-uk Kim 	    &m, &dst, &hlen, d->bd_wfilter);
620df8bae1dSRodney W. Grimes 	if (error)
621df8bae1dSRodney W. Grimes 		return (error);
622df8bae1dSRodney W. Grimes 
623114ae644SMike Smith 	if (d->bd_hdrcmplt)
624114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
625114ae644SMike Smith 
626560a54e1SJung-uk Kim 	if (d->bd_feedback) {
627560a54e1SJung-uk Kim 		mc = m_dup(m, M_DONTWAIT);
628560a54e1SJung-uk Kim 		if (mc != NULL)
629560a54e1SJung-uk Kim 			mc->m_pkthdr.rcvif = ifp;
630560a54e1SJung-uk Kim 		/* XXX Do not return the same packet twice. */
631560a54e1SJung-uk Kim 		if (d->bd_direction == BPF_D_INOUT)
632560a54e1SJung-uk Kim 			m->m_flags |= M_SKIP_BPF;
633560a54e1SJung-uk Kim 	} else
634560a54e1SJung-uk Kim 		mc = NULL;
635560a54e1SJung-uk Kim 
636560a54e1SJung-uk Kim 	m->m_pkthdr.len -= hlen;
637560a54e1SJung-uk Kim 	m->m_len -= hlen;
638560a54e1SJung-uk Kim 	m->m_data += hlen;	/* XXX */
639560a54e1SJung-uk Kim 
64082f4445dSRobert Watson #ifdef MAC
641f747d2ddSRobert Watson 	BPFD_LOCK(d);
64282f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
643560a54e1SJung-uk Kim 	if (mc != NULL)
644560a54e1SJung-uk Kim 		mac_create_mbuf_from_bpfdesc(d, mc);
645f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
64682f4445dSRobert Watson #endif
647560a54e1SJung-uk Kim 
648b8f9429dSRobert Watson 	NET_LOCK_GIANT();
649572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
650b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
651560a54e1SJung-uk Kim 
652560a54e1SJung-uk Kim 	if (mc != NULL) {
653560a54e1SJung-uk Kim 		if (error == 0) {
654560a54e1SJung-uk Kim 			NET_LOCK_GIANT();
655560a54e1SJung-uk Kim 			(*ifp->if_input)(ifp, mc);
656560a54e1SJung-uk Kim 			NET_UNLOCK_GIANT();
657560a54e1SJung-uk Kim 		} else
658560a54e1SJung-uk Kim 			m_freem(mc);
659560a54e1SJung-uk Kim 	}
660560a54e1SJung-uk Kim 
661df8bae1dSRodney W. Grimes 	return (error);
662df8bae1dSRodney W. Grimes }
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes /*
665df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
666e7bb21b3SJonathan Lemon  * receive and drop counts.
667df8bae1dSRodney W. Grimes  */
668df8bae1dSRodney W. Grimes static void
66919ba8395SChristian S.J. Peron reset_d(struct bpf_d *d)
670df8bae1dSRodney W. Grimes {
671e7bb21b3SJonathan Lemon 
672e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
673df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
674df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
675df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
676572bde2aSRobert Watson 		d->bd_hbuf = NULL;
677df8bae1dSRodney W. Grimes 	}
678df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
679df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
680df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
681df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
68269f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes /*
686df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
687df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
688df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
689df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
69093e39f0bSChristian S.J. Peron  *  BIOCSETWF		Set ethernet write filter.
691df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
692df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
693df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
694df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
695df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
696df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
697df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
698df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
699df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
700df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
701114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
702114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
703560a54e1SJung-uk Kim  *  BIOCGDIRECTION	Get packet direction flag
704560a54e1SJung-uk Kim  *  BIOCSDIRECTION	Set packet direction flag
70593e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
706560a54e1SJung-uk Kim  *  BIOCFEEDBACK	Set packet feedback mode.
707df8bae1dSRodney W. Grimes  */
708df8bae1dSRodney W. Grimes /* ARGSUSED */
70987f6c662SJulian Elischer static	int
71019ba8395SChristian S.J. Peron bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
71119ba8395SChristian S.J. Peron     struct thread *td)
712df8bae1dSRodney W. Grimes {
713e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
714e7bb21b3SJonathan Lemon 	int error = 0;
715df8bae1dSRodney W. Grimes 
716b75a24a0SChristian S.J. Peron 	/*
717b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
718b75a24a0SChristian S.J. Peron 	 */
71981bda851SJohn Polstra 	BPFD_LOCK(d);
720cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
72181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
72281bda851SJohn Polstra 		callout_stop(&d->bd_callout);
72381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
72481bda851SJohn Polstra 	BPFD_UNLOCK(d);
72581bda851SJohn Polstra 
72693e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
72793e39f0bSChristian S.J. Peron 		switch (cmd) {
72893e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
72993e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
73093e39f0bSChristian S.J. Peron 		case BIOCGDLT:
73193e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
73293e39f0bSChristian S.J. Peron 		case BIOCGETIF:
73393e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
73493e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
73593e39f0bSChristian S.J. Peron 		case BIOCVERSION:
73693e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
73793e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
738560a54e1SJung-uk Kim 		case BIOCFEEDBACK:
73993e39f0bSChristian S.J. Peron 		case FIONREAD:
74093e39f0bSChristian S.J. Peron 		case BIOCLOCK:
74193e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
74293e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
74393e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
74493e39f0bSChristian S.J. Peron 			break;
74593e39f0bSChristian S.J. Peron 		default:
74693e39f0bSChristian S.J. Peron 			return (EPERM);
74793e39f0bSChristian S.J. Peron 		}
74893e39f0bSChristian S.J. Peron 	}
749df8bae1dSRodney W. Grimes 	switch (cmd) {
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 	default:
752df8bae1dSRodney W. Grimes 		error = EINVAL;
753df8bae1dSRodney W. Grimes 		break;
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 	/*
756df8bae1dSRodney W. Grimes 	 * Check for read packet available.
757df8bae1dSRodney W. Grimes 	 */
758df8bae1dSRodney W. Grimes 	case FIONREAD:
759df8bae1dSRodney W. Grimes 		{
760df8bae1dSRodney W. Grimes 			int n;
761df8bae1dSRodney W. Grimes 
762e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
763df8bae1dSRodney W. Grimes 			n = d->bd_slen;
764df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
765df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
766e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes 			*(int *)addr = n;
769df8bae1dSRodney W. Grimes 			break;
770df8bae1dSRodney W. Grimes 		}
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
773df8bae1dSRodney W. Grimes 		{
774df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
775df8bae1dSRodney W. Grimes 
776572bde2aSRobert Watson 			if (d->bd_bif == NULL)
777df8bae1dSRodney W. Grimes 				error = EINVAL;
778df8bae1dSRodney W. Grimes 			else {
7795632c982SChristian S.J. Peron 				NET_LOCK_GIANT();
780df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
781df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
7825632c982SChristian S.J. Peron 				NET_UNLOCK_GIANT();
783df8bae1dSRodney W. Grimes 			}
784df8bae1dSRodney W. Grimes 			break;
785df8bae1dSRodney W. Grimes 		}
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	/*
788df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
789df8bae1dSRodney W. Grimes 	 */
790df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
791df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
792df8bae1dSRodney W. Grimes 		break;
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 	/*
795df8bae1dSRodney W. Grimes 	 * Set buffer length.
796df8bae1dSRodney W. Grimes 	 */
797df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
798572bde2aSRobert Watson 		if (d->bd_bif != NULL)
799df8bae1dSRodney W. Grimes 			error = EINVAL;
800df8bae1dSRodney W. Grimes 		else {
8018994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
802df8bae1dSRodney W. Grimes 
803eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
804eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
805df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
806df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
807df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
808df8bae1dSRodney W. Grimes 		}
809df8bae1dSRodney W. Grimes 		break;
810df8bae1dSRodney W. Grimes 
811df8bae1dSRodney W. Grimes 	/*
812df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
813df8bae1dSRodney W. Grimes 	 */
814df8bae1dSRodney W. Grimes 	case BIOCSETF:
81593e39f0bSChristian S.J. Peron 	case BIOCSETWF:
81693e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
817df8bae1dSRodney W. Grimes 		break;
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	/*
820df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
821df8bae1dSRodney W. Grimes 	 */
822df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
823e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
824df8bae1dSRodney W. Grimes 		reset_d(d);
825e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
826df8bae1dSRodney W. Grimes 		break;
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	/*
829df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
830df8bae1dSRodney W. Grimes 	 */
831df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
832572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
833df8bae1dSRodney W. Grimes 			/*
834df8bae1dSRodney W. Grimes 			 * No interface attached yet.
835df8bae1dSRodney W. Grimes 			 */
836df8bae1dSRodney W. Grimes 			error = EINVAL;
837df8bae1dSRodney W. Grimes 			break;
838df8bae1dSRodney W. Grimes 		}
839df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
8405632c982SChristian S.J. Peron 			NET_LOCK_GIANT();
841df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
8425632c982SChristian S.J. Peron 			NET_UNLOCK_GIANT();
843df8bae1dSRodney W. Grimes 			if (error == 0)
844df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
845df8bae1dSRodney W. Grimes 		}
846df8bae1dSRodney W. Grimes 		break;
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	/*
8498eab61f3SSam Leffler 	 * Get current data link type.
850df8bae1dSRodney W. Grimes 	 */
851df8bae1dSRodney W. Grimes 	case BIOCGDLT:
852572bde2aSRobert Watson 		if (d->bd_bif == NULL)
853df8bae1dSRodney W. Grimes 			error = EINVAL;
854df8bae1dSRodney W. Grimes 		else
855df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
856df8bae1dSRodney W. Grimes 		break;
857df8bae1dSRodney W. Grimes 
858df8bae1dSRodney W. Grimes 	/*
8598eab61f3SSam Leffler 	 * Get a list of supported data link types.
8608eab61f3SSam Leffler 	 */
8618eab61f3SSam Leffler 	case BIOCGDLTLIST:
862572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8638eab61f3SSam Leffler 			error = EINVAL;
8648eab61f3SSam Leffler 		else
8658eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
8668eab61f3SSam Leffler 		break;
8678eab61f3SSam Leffler 
8688eab61f3SSam Leffler 	/*
8698eab61f3SSam Leffler 	 * Set data link type.
8708eab61f3SSam Leffler 	 */
8718eab61f3SSam Leffler 	case BIOCSDLT:
872572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8738eab61f3SSam Leffler 			error = EINVAL;
8748eab61f3SSam Leffler 		else
8758eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
8768eab61f3SSam Leffler 		break;
8778eab61f3SSam Leffler 
8788eab61f3SSam Leffler 	/*
8791dd0feaaSArchie Cobbs 	 * Get interface name.
880df8bae1dSRodney W. Grimes 	 */
881df8bae1dSRodney W. Grimes 	case BIOCGETIF:
882572bde2aSRobert Watson 		if (d->bd_bif == NULL)
883df8bae1dSRodney W. Grimes 			error = EINVAL;
8841dd0feaaSArchie Cobbs 		else {
8851dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
8861dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
8871dd0feaaSArchie Cobbs 
8889bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
8899bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
8901dd0feaaSArchie Cobbs 		}
891df8bae1dSRodney W. Grimes 		break;
892df8bae1dSRodney W. Grimes 
893df8bae1dSRodney W. Grimes 	/*
894df8bae1dSRodney W. Grimes 	 * Set interface.
895df8bae1dSRodney W. Grimes 	 */
896df8bae1dSRodney W. Grimes 	case BIOCSETIF:
897df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
898df8bae1dSRodney W. Grimes 		break;
899df8bae1dSRodney W. Grimes 
900df8bae1dSRodney W. Grimes 	/*
901df8bae1dSRodney W. Grimes 	 * Set read timeout.
902df8bae1dSRodney W. Grimes 	 */
903df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
904df8bae1dSRodney W. Grimes 		{
905df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
906df8bae1dSRodney W. Grimes 
907bdc2cdc5SAlexander Langer 			/*
908bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
909bdc2cdc5SAlexander Langer 			 * a one-shot timer.
910bdc2cdc5SAlexander Langer 			 */
911bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
912bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
913df8bae1dSRodney W. Grimes 			break;
914df8bae1dSRodney W. Grimes 		}
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes 	/*
917df8bae1dSRodney W. Grimes 	 * Get read timeout.
918df8bae1dSRodney W. Grimes 	 */
919df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
920df8bae1dSRodney W. Grimes 		{
921df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
922df8bae1dSRodney W. Grimes 
923bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
924bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
925df8bae1dSRodney W. Grimes 			break;
926df8bae1dSRodney W. Grimes 		}
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes 	/*
929df8bae1dSRodney W. Grimes 	 * Get packet stats.
930df8bae1dSRodney W. Grimes 	 */
931df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
932df8bae1dSRodney W. Grimes 		{
933df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
934df8bae1dSRodney W. Grimes 
935df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
936df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
937df8bae1dSRodney W. Grimes 			break;
938df8bae1dSRodney W. Grimes 		}
939df8bae1dSRodney W. Grimes 
940df8bae1dSRodney W. Grimes 	/*
941df8bae1dSRodney W. Grimes 	 * Set immediate mode.
942df8bae1dSRodney W. Grimes 	 */
943df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
944df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
945df8bae1dSRodney W. Grimes 		break;
946df8bae1dSRodney W. Grimes 
947df8bae1dSRodney W. Grimes 	case BIOCVERSION:
948df8bae1dSRodney W. Grimes 		{
949df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
950df8bae1dSRodney W. Grimes 
951df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
952df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
953df8bae1dSRodney W. Grimes 			break;
954df8bae1dSRodney W. Grimes 		}
95500a83887SPaul Traina 
956114ae644SMike Smith 	/*
957114ae644SMike Smith 	 * Get "header already complete" flag
958114ae644SMike Smith 	 */
959114ae644SMike Smith 	case BIOCGHDRCMPLT:
960114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
961114ae644SMike Smith 		break;
962114ae644SMike Smith 
963114ae644SMike Smith 	/*
964114ae644SMike Smith 	 * Set "header already complete" flag
965114ae644SMike Smith 	 */
966114ae644SMike Smith 	case BIOCSHDRCMPLT:
967114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
968114ae644SMike Smith 		break;
969114ae644SMike Smith 
9708ed3828cSRobert Watson 	/*
971560a54e1SJung-uk Kim 	 * Get packet direction flag
9728ed3828cSRobert Watson 	 */
973560a54e1SJung-uk Kim 	case BIOCGDIRECTION:
974560a54e1SJung-uk Kim 		*(u_int *)addr = d->bd_direction;
9758ed3828cSRobert Watson 		break;
9768ed3828cSRobert Watson 
9778ed3828cSRobert Watson 	/*
978560a54e1SJung-uk Kim 	 * Set packet direction flag
9798ed3828cSRobert Watson 	 */
980560a54e1SJung-uk Kim 	case BIOCSDIRECTION:
981560a54e1SJung-uk Kim 		{
982560a54e1SJung-uk Kim 			u_int	direction;
983560a54e1SJung-uk Kim 
984560a54e1SJung-uk Kim 			direction = *(u_int *)addr;
985560a54e1SJung-uk Kim 			switch (direction) {
986560a54e1SJung-uk Kim 			case BPF_D_IN:
987560a54e1SJung-uk Kim 			case BPF_D_INOUT:
988560a54e1SJung-uk Kim 			case BPF_D_OUT:
989560a54e1SJung-uk Kim 				d->bd_direction = direction;
990560a54e1SJung-uk Kim 				break;
991560a54e1SJung-uk Kim 			default:
992560a54e1SJung-uk Kim 				error = EINVAL;
993560a54e1SJung-uk Kim 			}
994560a54e1SJung-uk Kim 		}
995560a54e1SJung-uk Kim 		break;
996560a54e1SJung-uk Kim 
997560a54e1SJung-uk Kim 	case BIOCFEEDBACK:
998560a54e1SJung-uk Kim 		d->bd_feedback = *(u_int *)addr;
999560a54e1SJung-uk Kim 		break;
1000560a54e1SJung-uk Kim 
1001560a54e1SJung-uk Kim 	case BIOCLOCK:
1002560a54e1SJung-uk Kim 		d->bd_locked = 1;
10038ed3828cSRobert Watson 		break;
10048ed3828cSRobert Watson 
100500a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
100600a83887SPaul Traina 		break;
100700a83887SPaul Traina 
100800a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
100900a83887SPaul Traina 		d->bd_async = *(int *)addr;
101000a83887SPaul Traina 		break;
101100a83887SPaul Traina 
1012831d27a9SDon Lewis 	case FIOSETOWN:
1013831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
101400a83887SPaul Traina 		break;
101500a83887SPaul Traina 
1016831d27a9SDon Lewis 	case FIOGETOWN:
101791e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
1018831d27a9SDon Lewis 		break;
1019831d27a9SDon Lewis 
1020831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
1021831d27a9SDon Lewis 	case TIOCSPGRP:
1022831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1023831d27a9SDon Lewis 		break;
1024831d27a9SDon Lewis 
1025831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
102600a83887SPaul Traina 	case TIOCGPGRP:
102791e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
102800a83887SPaul Traina 		break;
102900a83887SPaul Traina 
103000a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
103100a83887SPaul Traina 		{
103200a83887SPaul Traina 			u_int sig;
103300a83887SPaul Traina 
103400a83887SPaul Traina 			sig = *(u_int *)addr;
103500a83887SPaul Traina 
103600a83887SPaul Traina 			if (sig >= NSIG)
103700a83887SPaul Traina 				error = EINVAL;
103800a83887SPaul Traina 			else
103900a83887SPaul Traina 				d->bd_sig = sig;
104000a83887SPaul Traina 			break;
104100a83887SPaul Traina 		}
104200a83887SPaul Traina 	case BIOCGRSIG:
104300a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
104400a83887SPaul Traina 		break;
1045df8bae1dSRodney W. Grimes 	}
1046df8bae1dSRodney W. Grimes 	return (error);
1047df8bae1dSRodney W. Grimes }
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes /*
1050df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1051df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1052df8bae1dSRodney W. Grimes  */
1053f708ef1bSPoul-Henning Kamp static int
105419ba8395SChristian S.J. Peron bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1055df8bae1dSRodney W. Grimes {
1056df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
105793e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1058293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1059ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1060ae275efcSJung-uk Kim #endif
1061df8bae1dSRodney W. Grimes 
106293e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
106393e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
106493e39f0bSChristian S.J. Peron 		wfilter = 1;
1065293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1066ae275efcSJung-uk Kim 		ofunc = NULL;
1067ae275efcSJung-uk Kim #endif
106893e39f0bSChristian S.J. Peron 	} else {
106993e39f0bSChristian S.J. Peron 		wfilter = 0;
107093e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1071293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1072ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1073ae275efcSJung-uk Kim #endif
107493e39f0bSChristian S.J. Peron 	}
1075572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1076df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1077df8bae1dSRodney W. Grimes 			return (EINVAL);
1078e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
107993e39f0bSChristian S.J. Peron 		if (wfilter)
108093e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1081ae275efcSJung-uk Kim 		else {
108293e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1083293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1084ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1085ae275efcSJung-uk Kim #endif
1086ae275efcSJung-uk Kim 		}
1087df8bae1dSRodney W. Grimes 		reset_d(d);
1088e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1089572bde2aSRobert Watson 		if (old != NULL)
1090bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1091293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1092ae275efcSJung-uk Kim 		if (ofunc != NULL)
1093ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1094ae275efcSJung-uk Kim #endif
1095df8bae1dSRodney W. Grimes 		return (0);
1096df8bae1dSRodney W. Grimes 	}
1097df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
10980eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1099df8bae1dSRodney W. Grimes 		return (EINVAL);
1100df8bae1dSRodney W. Grimes 
1101df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1102a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1103df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1104df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1105e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
110693e39f0bSChristian S.J. Peron 		if (wfilter)
110793e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1108ae275efcSJung-uk Kim 		else {
110993e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1110293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1111ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1112ae275efcSJung-uk Kim #endif
1113ae275efcSJung-uk Kim 		}
1114df8bae1dSRodney W. Grimes 		reset_d(d);
1115e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1116572bde2aSRobert Watson 		if (old != NULL)
1117bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1118293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1119ae275efcSJung-uk Kim 		if (ofunc != NULL)
1120ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1121ae275efcSJung-uk Kim #endif
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 		return (0);
1124df8bae1dSRodney W. Grimes 	}
1125bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1126df8bae1dSRodney W. Grimes 	return (EINVAL);
1127df8bae1dSRodney W. Grimes }
1128df8bae1dSRodney W. Grimes 
1129df8bae1dSRodney W. Grimes /*
1130df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1131df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1132df8bae1dSRodney W. Grimes  * Return an errno or 0.
1133df8bae1dSRodney W. Grimes  */
1134df8bae1dSRodney W. Grimes static int
113519ba8395SChristian S.J. Peron bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1136df8bae1dSRodney W. Grimes {
1137df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
11389b44ff22SGarrett Wollman 	struct ifnet *theywant;
1139df8bae1dSRodney W. Grimes 
11409b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
114116d878ccSChristian S.J. Peron 	if (theywant == NULL || theywant->if_bpf == NULL)
114216d878ccSChristian S.J. Peron 		return (ENXIO);
11439b44ff22SGarrett Wollman 
114416d878ccSChristian S.J. Peron 	bp = theywant->if_bpf;
1145df8bae1dSRodney W. Grimes 	/*
1146df8bae1dSRodney W. Grimes 	 * Allocate the packet buffers if we need to.
1147df8bae1dSRodney W. Grimes 	 * If we're already attached to requested interface,
1148df8bae1dSRodney W. Grimes 	 * just flush the buffer.
1149df8bae1dSRodney W. Grimes 	 */
1150a3594432SRobert Watson 	if (d->bd_sbuf == NULL)
1151a3594432SRobert Watson 		bpf_allocbufs(d);
1152df8bae1dSRodney W. Grimes 	if (bp != d->bd_bif) {
1153df8bae1dSRodney W. Grimes 		if (d->bd_bif)
1154df8bae1dSRodney W. Grimes 			/*
1155df8bae1dSRodney W. Grimes 			 * Detach if attached to something else.
1156df8bae1dSRodney W. Grimes 			 */
1157df8bae1dSRodney W. Grimes 			bpf_detachd(d);
1158df8bae1dSRodney W. Grimes 
1159df8bae1dSRodney W. Grimes 		bpf_attachd(d, bp);
1160df8bae1dSRodney W. Grimes 	}
1161e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1162df8bae1dSRodney W. Grimes 	reset_d(d);
1163e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1164df8bae1dSRodney W. Grimes 	return (0);
1165df8bae1dSRodney W. Grimes }
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes /*
1168243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1169df8bae1dSRodney W. Grimes  *
1170df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1171df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1172df8bae1dSRodney W. Grimes  */
117337c84183SPoul-Henning Kamp static int
117419ba8395SChristian S.J. Peron bpfpoll(struct cdev *dev, int events, struct thread *td)
1175df8bae1dSRodney W. Grimes {
1176e7bb21b3SJonathan Lemon 	struct bpf_d *d;
11770832fc64SGarance A Drosehn 	int revents;
1178df8bae1dSRodney W. Grimes 
1179bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1180de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1181de5d9935SRobert Watson 		return (ENXIO);
1182de5d9935SRobert Watson 
1183b75a24a0SChristian S.J. Peron 	/*
1184b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1185b75a24a0SChristian S.J. Peron 	 */
11860832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1187e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1188cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
118975c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
119095aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1191243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
119281bda851SJohn Polstra 		else {
1193ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
119481bda851SJohn Polstra 			/* Start the read timeout if necessary. */
119581bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
119681bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
119781bda851SJohn Polstra 				    bpf_timed_out, d);
119881bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
119981bda851SJohn Polstra 			}
120081bda851SJohn Polstra 		}
120175c13541SPoul-Henning Kamp 	}
1202e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1203243ac7d8SPeter Wemm 	return (revents);
1204df8bae1dSRodney W. Grimes }
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes /*
120795aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
120895aab9ccSJohn-Mark Gurney  * reject all others.
120995aab9ccSJohn-Mark Gurney  */
121095aab9ccSJohn-Mark Gurney int
121119ba8395SChristian S.J. Peron bpfkqfilter(struct cdev *dev, struct knote *kn)
121295aab9ccSJohn-Mark Gurney {
121395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
121495aab9ccSJohn-Mark Gurney 
121595aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
121695aab9ccSJohn-Mark Gurney 		return (1);
121795aab9ccSJohn-Mark Gurney 
1218b75a24a0SChristian S.J. Peron 	/*
1219b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1220b75a24a0SChristian S.J. Peron 	 */
1221cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1222b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
122395aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
122495aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
12254b19419eSChristian S.J. Peron 	knlist_add(&d->bd_sel.si_note, kn, 1);
1226cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
122795aab9ccSJohn-Mark Gurney 
122895aab9ccSJohn-Mark Gurney 	return (0);
122995aab9ccSJohn-Mark Gurney }
123095aab9ccSJohn-Mark Gurney 
123195aab9ccSJohn-Mark Gurney static void
123219ba8395SChristian S.J. Peron filt_bpfdetach(struct knote *kn)
123395aab9ccSJohn-Mark Gurney {
123495aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
123595aab9ccSJohn-Mark Gurney 
1236ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
123795aab9ccSJohn-Mark Gurney }
123895aab9ccSJohn-Mark Gurney 
123995aab9ccSJohn-Mark Gurney static int
124019ba8395SChristian S.J. Peron filt_bpfread(struct knote *kn, long hint)
124195aab9ccSJohn-Mark Gurney {
124295aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
124395aab9ccSJohn-Mark Gurney 	int ready;
124495aab9ccSJohn-Mark Gurney 
124586c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
124695aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
124795aab9ccSJohn-Mark Gurney 	if (ready) {
124895aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
124995aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
125095aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
125195aab9ccSJohn-Mark Gurney 	}
125295aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
125395aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
125495aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
125595aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
125695aab9ccSJohn-Mark Gurney 	}
125795aab9ccSJohn-Mark Gurney 
125895aab9ccSJohn-Mark Gurney 	return (ready);
125995aab9ccSJohn-Mark Gurney }
126095aab9ccSJohn-Mark Gurney 
126195aab9ccSJohn-Mark Gurney /*
1262df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1263df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1264df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1265df8bae1dSRodney W. Grimes  * buffer.
1266df8bae1dSRodney W. Grimes  */
1267df8bae1dSRodney W. Grimes void
126819ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1269df8bae1dSRodney W. Grimes {
12708994a245SDag-Erling Smørgrav 	struct bpf_d *d;
12718994a245SDag-Erling Smørgrav 	u_int slen;
127291433904SDavid Malone 	int gottime;
127391433904SDavid Malone 	struct timeval tv;
1274e7bb21b3SJonathan Lemon 
127591433904SDavid Malone 	gottime = 0;
1276e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12774a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1278e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1279df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1280ae275efcSJung-uk Kim #ifdef BPF_JITTER
1281ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1282ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1283ae275efcSJung-uk Kim 		else
1284ae275efcSJung-uk Kim #endif
128593e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1286ec272d87SRobert Watson 		if (slen != 0) {
128769f7644bSChristian S.J. Peron 			d->bd_fcount++;
128891433904SDavid Malone 			if (!gottime) {
128991433904SDavid Malone 				microtime(&tv);
129091433904SDavid Malone 				gottime = 1;
129191433904SDavid Malone 			}
1292ec272d87SRobert Watson #ifdef MAC
129324a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1294ec272d87SRobert Watson #endif
129591433904SDavid Malone 				catchpacket(d, pkt, pktlen, slen, bcopy, &tv);
1296ec272d87SRobert Watson 		}
1297e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1298df8bae1dSRodney W. Grimes 	}
1299e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1300df8bae1dSRodney W. Grimes }
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes /*
1303df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1304df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1305df8bae1dSRodney W. Grimes  */
1306df8bae1dSRodney W. Grimes static void
130719ba8395SChristian S.J. Peron bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1308df8bae1dSRodney W. Grimes {
13098994a245SDag-Erling Smørgrav 	const struct mbuf *m;
13108994a245SDag-Erling Smørgrav 	u_int count;
1311df8bae1dSRodney W. Grimes 	u_char *dst;
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 	m = src_arg;
1314df8bae1dSRodney W. Grimes 	dst = dst_arg;
1315df8bae1dSRodney W. Grimes 	while (len > 0) {
1316572bde2aSRobert Watson 		if (m == NULL)
1317df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1318df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
13190453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1320df8bae1dSRodney W. Grimes 		m = m->m_next;
1321df8bae1dSRodney W. Grimes 		dst += count;
1322df8bae1dSRodney W. Grimes 		len -= count;
1323df8bae1dSRodney W. Grimes 	}
1324df8bae1dSRodney W. Grimes }
1325df8bae1dSRodney W. Grimes 
1326560a54e1SJung-uk Kim #define	BPF_CHECK_DIRECTION(d, m) \
1327560a54e1SJung-uk Kim 	if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \
1328560a54e1SJung-uk Kim 	    ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL))
1329560a54e1SJung-uk Kim 
1330df8bae1dSRodney W. Grimes /*
1331df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1332df8bae1dSRodney W. Grimes  */
1333df8bae1dSRodney W. Grimes void
133419ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1335df8bae1dSRodney W. Grimes {
1336df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1337df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
133891433904SDavid Malone 	int gottime;
133991433904SDavid Malone 	struct timeval tv;
134091433904SDavid Malone 
1341560a54e1SJung-uk Kim 	if (m->m_flags & M_SKIP_BPF) {
1342560a54e1SJung-uk Kim 		m->m_flags &= ~M_SKIP_BPF;
1343560a54e1SJung-uk Kim 		return;
1344560a54e1SJung-uk Kim 	}
1345560a54e1SJung-uk Kim 
134691433904SDavid Malone 	gottime = 0;
1347df8bae1dSRodney W. Grimes 
1348f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1349df8bae1dSRodney W. Grimes 
1350e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
13514a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1352560a54e1SJung-uk Kim 		BPF_CHECK_DIRECTION(d, m)
13538ed3828cSRobert Watson 			continue;
1354e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1355df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1356ae275efcSJung-uk Kim #ifdef BPF_JITTER
1357ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1358ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1359ae275efcSJung-uk Kim 		    m->m_next == NULL)
1360ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1361ae275efcSJung-uk Kim 			    pktlen, pktlen);
1362ae275efcSJung-uk Kim 		else
1363ae275efcSJung-uk Kim #endif
136493e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
13654ddfb531SChristian S.J. Peron 		if (slen != 0) {
136669f7644bSChristian S.J. Peron 			d->bd_fcount++;
136791433904SDavid Malone 			if (!gottime) {
136891433904SDavid Malone 				microtime(&tv);
136991433904SDavid Malone 				gottime = 1;
137091433904SDavid Malone 			}
13710c7fb534SRobert Watson #ifdef MAC
137224a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
13730c7fb534SRobert Watson #endif
13740c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
137591433904SDavid Malone 				    bpf_mcopy, &tv);
13764ddfb531SChristian S.J. Peron 		}
1377e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1378df8bae1dSRodney W. Grimes 	}
1379e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1380df8bae1dSRodney W. Grimes }
1381df8bae1dSRodney W. Grimes 
1382df8bae1dSRodney W. Grimes /*
1383437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1384437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1385437ffe18SSam Leffler  */
1386437ffe18SSam Leffler void
138719ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1388437ffe18SSam Leffler {
1389437ffe18SSam Leffler 	struct mbuf mb;
1390437ffe18SSam Leffler 	struct bpf_d *d;
1391437ffe18SSam Leffler 	u_int pktlen, slen;
139291433904SDavid Malone 	int gottime;
139391433904SDavid Malone 	struct timeval tv;
139491433904SDavid Malone 
1395560a54e1SJung-uk Kim 	if (m->m_flags & M_SKIP_BPF) {
1396560a54e1SJung-uk Kim 		m->m_flags &= ~M_SKIP_BPF;
1397560a54e1SJung-uk Kim 		return;
1398560a54e1SJung-uk Kim 	}
1399560a54e1SJung-uk Kim 
140091433904SDavid Malone 	gottime = 0;
1401437ffe18SSam Leffler 
1402437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1403437ffe18SSam Leffler 	/*
1404437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1405437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1406437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1407437ffe18SSam Leffler 	 */
1408437ffe18SSam Leffler 	mb.m_next = m;
1409437ffe18SSam Leffler 	mb.m_data = data;
1410437ffe18SSam Leffler 	mb.m_len = dlen;
1411437ffe18SSam Leffler 	pktlen += dlen;
1412437ffe18SSam Leffler 
1413437ffe18SSam Leffler 	BPFIF_LOCK(bp);
14144a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1415560a54e1SJung-uk Kim 		BPF_CHECK_DIRECTION(d, m)
1416437ffe18SSam Leffler 			continue;
1417437ffe18SSam Leffler 		BPFD_LOCK(d);
1418437ffe18SSam Leffler 		++d->bd_rcount;
141993e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
14204ddfb531SChristian S.J. Peron 		if (slen != 0) {
142169f7644bSChristian S.J. Peron 			d->bd_fcount++;
142291433904SDavid Malone 			if (!gottime) {
142391433904SDavid Malone 				microtime(&tv);
142491433904SDavid Malone 				gottime = 1;
142591433904SDavid Malone 			}
1426437ffe18SSam Leffler #ifdef MAC
1427437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1428437ffe18SSam Leffler #endif
1429437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
143091433904SDavid Malone 				    bpf_mcopy, &tv);
14314ddfb531SChristian S.J. Peron 		}
1432437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1433437ffe18SSam Leffler 	}
1434437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1435437ffe18SSam Leffler }
1436437ffe18SSam Leffler 
1437560a54e1SJung-uk Kim #undef	BPF_CHECK_DIRECTION
1438560a54e1SJung-uk Kim 
1439437ffe18SSam Leffler /*
1440df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
14419e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1442df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1443df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1444df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1445df8bae1dSRodney W. Grimes  */
1446df8bae1dSRodney W. Grimes static void
144719ba8395SChristian S.J. Peron catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
144891433904SDavid Malone     void (*cpfn)(const void *, void *, size_t), struct timeval *tv)
1449df8bae1dSRodney W. Grimes {
14508994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
14518994a245SDag-Erling Smørgrav 	int totlen, curlen;
14528994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
14537819da79SJohn-Mark Gurney 	int do_wakeup = 0;
14549e610888SDag-Erling Smørgrav 
1455a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1456df8bae1dSRodney W. Grimes 	/*
1457df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1458df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1459df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1460df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1461df8bae1dSRodney W. Grimes 	 */
1462df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1463df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1464df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1465df8bae1dSRodney W. Grimes 
1466df8bae1dSRodney W. Grimes 	/*
1467df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1468df8bae1dSRodney W. Grimes 	 */
1469df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1470df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1471df8bae1dSRodney W. Grimes 		/*
1472df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1473df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1474df8bae1dSRodney W. Grimes 		 * pending reads.
1475df8bae1dSRodney W. Grimes 		 */
1476572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1477df8bae1dSRodney W. Grimes 			/*
1478df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1479df8bae1dSRodney W. Grimes 			 * so drop the packet.
1480df8bae1dSRodney W. Grimes 			 */
1481df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1482df8bae1dSRodney W. Grimes 			return;
1483df8bae1dSRodney W. Grimes 		}
1484df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
14857819da79SJohn-Mark Gurney 		do_wakeup = 1;
1486df8bae1dSRodney W. Grimes 		curlen = 0;
1487df8bae1dSRodney W. Grimes 	}
148881bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1489df8bae1dSRodney W. Grimes 		/*
149081bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
149181bda851SJohn Polstra 		 * already expired during a select call.  A packet
149281bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1493df8bae1dSRodney W. Grimes 		 */
14947819da79SJohn-Mark Gurney 		do_wakeup = 1;
1495df8bae1dSRodney W. Grimes 
1496df8bae1dSRodney W. Grimes 	/*
1497df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1498df8bae1dSRodney W. Grimes 	 */
1499df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
150091433904SDavid Malone 	hp->bh_tstamp = *tv;
1501df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1502df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1503df8bae1dSRodney W. Grimes 	/*
1504df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1505df8bae1dSRodney W. Grimes 	 */
1506df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1507df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
15087819da79SJohn-Mark Gurney 
15097819da79SJohn-Mark Gurney 	if (do_wakeup)
15107819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1511df8bae1dSRodney W. Grimes }
1512df8bae1dSRodney W. Grimes 
1513df8bae1dSRodney W. Grimes /*
1514df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1515df8bae1dSRodney W. Grimes  */
1516a3594432SRobert Watson static void
151719ba8395SChristian S.J. Peron bpf_allocbufs(struct bpf_d *d)
1518df8bae1dSRodney W. Grimes {
1519df8bae1dSRodney W. Grimes 
1520a3594432SRobert Watson 	KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL"));
1521a3594432SRobert Watson 	KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL"));
1522a3594432SRobert Watson 	KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL"));
1523a3594432SRobert Watson 
1524a3594432SRobert Watson 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1525a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1526df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1527df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1528df8bae1dSRodney W. Grimes }
1529df8bae1dSRodney W. Grimes 
1530df8bae1dSRodney W. Grimes /*
1531df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1532df8bae1dSRodney W. Grimes  * Called on close.
1533df8bae1dSRodney W. Grimes  */
1534df8bae1dSRodney W. Grimes static void
153519ba8395SChristian S.J. Peron bpf_freed(struct bpf_d *d)
1536df8bae1dSRodney W. Grimes {
1537df8bae1dSRodney W. Grimes 	/*
1538df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1539df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1540df8bae1dSRodney W. Grimes 	 * free.
1541df8bae1dSRodney W. Grimes 	 */
1542572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1543bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1544572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1545bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1546572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1547bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1548df8bae1dSRodney W. Grimes 	}
1549ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
155093e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1551ae275efcSJung-uk Kim #ifdef BPF_JITTER
1552ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1553ae275efcSJung-uk Kim #endif
1554ae275efcSJung-uk Kim 	}
155593e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
155693e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1557e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1558df8bae1dSRodney W. Grimes }
1559df8bae1dSRodney W. Grimes 
1560df8bae1dSRodney W. Grimes /*
156124a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
156224a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1563df8bae1dSRodney W. Grimes  */
1564df8bae1dSRodney W. Grimes void
156519ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1566df8bae1dSRodney W. Grimes {
156724a229f4SSam Leffler 
156824a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
156924a229f4SSam Leffler }
157024a229f4SSam Leffler 
157124a229f4SSam Leffler /*
157224a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
157324a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
157424a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
157524a229f4SSam Leffler  * headers are not yet supporrted).
157624a229f4SSam Leffler  */
157724a229f4SSam Leffler void
157819ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
157924a229f4SSam Leffler {
1580df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
158119ba8395SChristian S.J. Peron 
158219ba8395SChristian S.J. Peron 	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1583572bde2aSRobert Watson 	if (bp == NULL)
1584df8bae1dSRodney W. Grimes 		panic("bpfattach");
1585df8bae1dSRodney W. Grimes 
15864a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
1587df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1588df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
15896008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
159016d878ccSChristian S.J. Peron 	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
159116d878ccSChristian S.J. Peron 	*driverp = bp;
1592df8bae1dSRodney W. Grimes 
1593e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
15944a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1595e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1596df8bae1dSRodney W. Grimes 
1597df8bae1dSRodney W. Grimes 	/*
1598df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1599df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1600df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1601df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1602df8bae1dSRodney W. Grimes 	 */
1603df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1604df8bae1dSRodney W. Grimes 
16052eeab939SGarrett Wollman 	if (bootverbose)
160624a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1607df8bae1dSRodney W. Grimes }
160853ac6efbSJulian Elischer 
1609de5d9935SRobert Watson /*
1610de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1611de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1612de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1613de5d9935SRobert Watson  * ENXIO.
1614de5d9935SRobert Watson  */
1615de5d9935SRobert Watson void
161619ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
1617de5d9935SRobert Watson {
16184a3feeaaSRobert Watson 	struct bpf_if	*bp;
1619de5d9935SRobert Watson 	struct bpf_d	*d;
1620de5d9935SRobert Watson 
1621de5d9935SRobert Watson 	/* Locate BPF interface information */
16228eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16234a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1624de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1625de5d9935SRobert Watson 			break;
1626de5d9935SRobert Watson 	}
1627de5d9935SRobert Watson 
1628de5d9935SRobert Watson 	/* Interface wasn't attached */
1629d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1630e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
16319bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1632de5d9935SRobert Watson 		return;
1633de5d9935SRobert Watson 	}
1634de5d9935SRobert Watson 
16354a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
16368eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1637de5d9935SRobert Watson 
16384a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1639e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1640e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1641e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1642e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1643e7bb21b3SJonathan Lemon 	}
1644e7bb21b3SJonathan Lemon 
1645e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1646de5d9935SRobert Watson 	free(bp, M_BPF);
16478eab61f3SSam Leffler }
1648de5d9935SRobert Watson 
16498eab61f3SSam Leffler /*
16508eab61f3SSam Leffler  * Get a list of available data link type of the interface.
16518eab61f3SSam Leffler  */
16528eab61f3SSam Leffler static int
165319ba8395SChristian S.J. Peron bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
16548eab61f3SSam Leffler {
16558eab61f3SSam Leffler 	int n, error;
16568eab61f3SSam Leffler 	struct ifnet *ifp;
16578eab61f3SSam Leffler 	struct bpf_if *bp;
16588eab61f3SSam Leffler 
16598eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
16608eab61f3SSam Leffler 	n = 0;
16618eab61f3SSam Leffler 	error = 0;
16628eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16634a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
16648eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
16658eab61f3SSam Leffler 			continue;
16668eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
16678eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1668e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
16698eab61f3SSam Leffler 				return (ENOMEM);
16708eab61f3SSam Leffler 			}
16718eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
16728eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
16738eab61f3SSam Leffler 		}
16748eab61f3SSam Leffler 		n++;
16758eab61f3SSam Leffler 	}
16768eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
16778eab61f3SSam Leffler 	bfl->bfl_len = n;
16788eab61f3SSam Leffler 	return (error);
16798eab61f3SSam Leffler }
16808eab61f3SSam Leffler 
16818eab61f3SSam Leffler /*
16828eab61f3SSam Leffler  * Set the data link type of a BPF instance.
16838eab61f3SSam Leffler  */
16848eab61f3SSam Leffler static int
168519ba8395SChristian S.J. Peron bpf_setdlt(struct bpf_d *d, u_int dlt)
16868eab61f3SSam Leffler {
16878eab61f3SSam Leffler 	int error, opromisc;
16888eab61f3SSam Leffler 	struct ifnet *ifp;
16898eab61f3SSam Leffler 	struct bpf_if *bp;
16908eab61f3SSam Leffler 
16918eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
16928eab61f3SSam Leffler 		return (0);
16938eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
16948eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16954a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
16968eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
16978eab61f3SSam Leffler 			break;
16988eab61f3SSam Leffler 	}
16998eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
17008eab61f3SSam Leffler 	if (bp != NULL) {
17018eab61f3SSam Leffler 		opromisc = d->bd_promisc;
17028eab61f3SSam Leffler 		bpf_detachd(d);
17038eab61f3SSam Leffler 		bpf_attachd(d, bp);
170493daabddSBrian Feldman 		BPFD_LOCK(d);
17058eab61f3SSam Leffler 		reset_d(d);
17068eab61f3SSam Leffler 		BPFD_UNLOCK(d);
17078eab61f3SSam Leffler 		if (opromisc) {
17088eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
17098eab61f3SSam Leffler 			if (error)
17108eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
17118eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
17128eab61f3SSam Leffler 					error);
17138eab61f3SSam Leffler 			else
17148eab61f3SSam Leffler 				d->bd_promisc = 1;
17158eab61f3SSam Leffler 		}
17168eab61f3SSam Leffler 	}
17178eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1718de5d9935SRobert Watson }
1719de5d9935SRobert Watson 
17203f54a085SPoul-Henning Kamp static void
172119ba8395SChristian S.J. Peron bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
172219ba8395SChristian S.J. Peron     struct cdev **dev)
17233f54a085SPoul-Henning Kamp {
17243f54a085SPoul-Henning Kamp 	int u;
17253f54a085SPoul-Henning Kamp 
1726f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
17273f54a085SPoul-Henning Kamp 		return;
1728db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
17293f54a085SPoul-Henning Kamp 		return;
1730b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1731b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1732f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1733b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
17343f54a085SPoul-Henning Kamp 	return;
17353f54a085SPoul-Henning Kamp }
17363f54a085SPoul-Henning Kamp 
1737514ede09SBruce Evans static void
173819ba8395SChristian S.J. Peron bpf_drvinit(void *unused)
173953ac6efbSJulian Elischer {
174053ac6efbSJulian Elischer 
17416008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
17424a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1743db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
17447198bf47SJulian Elischer }
174553ac6efbSJulian Elischer 
174669f7644bSChristian S.J. Peron static void
174769f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
174869f7644bSChristian S.J. Peron {
174969f7644bSChristian S.J. Peron 
175069f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
175169f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
175269f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
175369f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
175469f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1755560a54e1SJung-uk Kim 	d->bd_direction = bd->bd_direction;
1756560a54e1SJung-uk Kim 	d->bd_feedback = bd->bd_feedback;
175769f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
175869f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
175969f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
176069f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
176169f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
176269f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
176369f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
176469f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
176569f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
176669f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
176769f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
176893e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
176969f7644bSChristian S.J. Peron }
177069f7644bSChristian S.J. Peron 
177169f7644bSChristian S.J. Peron static int
177269f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
177369f7644bSChristian S.J. Peron {
1774422a63daSChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd;
1775422a63daSChristian S.J. Peron 	int index, error;
177669f7644bSChristian S.J. Peron 	struct bpf_if *bp;
177769f7644bSChristian S.J. Peron 	struct bpf_d *bd;
177869f7644bSChristian S.J. Peron 
177969f7644bSChristian S.J. Peron 	/*
178069f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
178169f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
178269f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
178369f7644bSChristian S.J. Peron 	 * the statistics for them, too.
178469f7644bSChristian S.J. Peron 	 */
1785acd3428bSRobert Watson 	error = priv_check(req->td, PRIV_NET_BPF);
178669f7644bSChristian S.J. Peron 	if (error)
178769f7644bSChristian S.J. Peron 		return (error);
178869f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
1789422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
179069f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
179169f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
1792422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
179369f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
1794422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1795422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
1796422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
1797422a63daSChristian S.J. Peron 		return (ENOMEM);
1798422a63daSChristian S.J. Peron 	}
1799422a63daSChristian S.J. Peron 	index = 0;
180069f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
18011fc9e387SChristian S.J. Peron 		BPFIF_LOCK(bp);
180269f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1803422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
180469f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
1805422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
180669f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
180769f7644bSChristian S.J. Peron 		}
18081fc9e387SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
180969f7644bSChristian S.J. Peron 	}
181069f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
1811422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1812422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
181369f7644bSChristian S.J. Peron 	return (error);
181469f7644bSChristian S.J. Peron }
181569f7644bSChristian S.J. Peron 
1816c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
181753ac6efbSJulian Elischer 
18185bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1819f8dc4716SMike Smith /*
1820f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1821f8dc4716SMike Smith  *
1822f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1823f8dc4716SMike Smith  * to be loaded at runtime.
1824f8dc4716SMike Smith  */
18257eae78a4SChristian S.J. Peron static struct bpf_if bp_null;
1826f8dc4716SMike Smith 
1827f8dc4716SMike Smith void
182819ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1829f8dc4716SMike Smith {
1830f8dc4716SMike Smith }
1831f8dc4716SMike Smith 
1832f8dc4716SMike Smith void
183319ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1834f8dc4716SMike Smith {
1835f8dc4716SMike Smith }
1836f8dc4716SMike Smith 
1837f8dc4716SMike Smith void
183819ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
1839437ffe18SSam Leffler {
1840437ffe18SSam Leffler }
1841437ffe18SSam Leffler 
1842437ffe18SSam Leffler void
184319ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1844f8dc4716SMike Smith {
18457eae78a4SChristian S.J. Peron 
18467eae78a4SChristian S.J. Peron 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1847f8dc4716SMike Smith }
1848f8dc4716SMike Smith 
1849da626c17SBill Paul void
185019ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
18515f7a7923SSam Leffler {
18527eae78a4SChristian S.J. Peron 
18537eae78a4SChristian S.J. Peron 	*driverp = &bp_null;
18545f7a7923SSam Leffler }
18555f7a7923SSam Leffler 
18565f7a7923SSam Leffler void
185719ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
1858da626c17SBill Paul {
1859da626c17SBill Paul }
1860da626c17SBill Paul 
1861f8dc4716SMike Smith u_int
186219ba8395SChristian S.J. Peron bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1863f8dc4716SMike Smith {
1864f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1865f8dc4716SMike Smith }
1866f8dc4716SMike Smith 
18675bb5f2c9SPeter Wemm int
186819ba8395SChristian S.J. Peron bpf_validate(const struct bpf_insn *f, int len)
18695bb5f2c9SPeter Wemm {
18705bb5f2c9SPeter Wemm 	return 0;		/* false */
18715bb5f2c9SPeter Wemm }
18725bb5f2c9SPeter Wemm 
18735bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1874