xref: /freebsd/sys/net/bpf.c (revision 512824f8f7748f7bb54ef900bc8e68a95a3d0f4d)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
384f252c4dSRuslan Ermilov  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
39df8bae1dSRodney W. Grimes  *
40c3aac50fSPeter Wemm  * $FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
435bb5f2c9SPeter Wemm #include "opt_bpf.h"
4482f4445dSRobert Watson #include "opt_mac.h"
455bb5f2c9SPeter Wemm #include "opt_netgraph.h"
46df8bae1dSRodney W. Grimes 
4795aab9ccSJohn-Mark Gurney #include <sys/types.h>
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49df8bae1dSRodney W. Grimes #include <sys/systm.h>
50ce7609a4SBruce Evans #include <sys/conf.h>
5182f4445dSRobert Watson #include <sys/mac.h>
524d1d4912SBruce Evans #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
54df8bae1dSRodney W. Grimes #include <sys/time.h>
55df8bae1dSRodney W. Grimes #include <sys/proc.h>
560310c19fSBruce Evans #include <sys/signalvar.h>
57528f627fSBruce Evans #include <sys/filio.h>
58528f627fSBruce Evans #include <sys/sockio.h>
59528f627fSBruce Evans #include <sys/ttycom.h>
60831d27a9SDon Lewis #include <sys/filedesc.h>
61df8bae1dSRodney W. Grimes 
6295aab9ccSJohn-Mark Gurney #include <sys/event.h>
6395aab9ccSJohn-Mark Gurney #include <sys/file.h>
64243ac7d8SPeter Wemm #include <sys/poll.h>
6595aab9ccSJohn-Mark Gurney #include <sys/proc.h>
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes #include <sys/socket.h>
68fba9235dSBruce Evans #include <sys/vnode.h>
69df8bae1dSRodney W. Grimes 
70fba9235dSBruce Evans #include <net/if.h>
71df8bae1dSRodney W. Grimes #include <net/bpf.h>
72df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
75df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
76df8bae1dSRodney W. Grimes #include <sys/kernel.h>
77f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
787b778b5eSEivind Eklund 
79959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8087f6c662SJulian Elischer 
815bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8253ac6efbSJulian Elischer 
83df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes /*
86df8bae1dSRodney W. Grimes  * The default read buffer size is patchable.
87df8bae1dSRodney W. Grimes  */
88e7bb21b3SJonathan Lemon static int bpf_bufsize = 4096;
89f708ef1bSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
90f708ef1bSPoul-Henning Kamp 	&bpf_bufsize, 0, "");
91eba2a1aeSPoul-Henning Kamp static int bpf_maxbufsize = BPF_MAXBUFSIZE;
92eba2a1aeSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
93eba2a1aeSPoul-Henning Kamp 	&bpf_maxbufsize, 0, "");
94df8bae1dSRodney W. Grimes 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
97df8bae1dSRodney W. Grimes  */
98f708ef1bSPoul-Henning Kamp static struct bpf_if	*bpf_iflist;
99e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
100df8bae1dSRodney W. Grimes 
101929ddbbbSAlfred Perlstein static int	bpf_allocbufs(struct bpf_d *);
102929ddbbbSAlfred Perlstein static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
103929ddbbbSAlfred Perlstein static void	bpf_detachd(struct bpf_d *d);
104929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
105929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
106929ddbbbSAlfred Perlstein static int	bpf_movein(struct uio *, int,
107929ddbbbSAlfred Perlstein 		    struct mbuf **, struct sockaddr *, int *);
108929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
109929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
110e7bb21b3SJonathan Lemon static __inline void
111929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
112929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
113929ddbbbSAlfred Perlstein 		    u_int, void (*)(const void *, void *, size_t));
114929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
115929ddbbbSAlfred Perlstein static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
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);
120df8bae1dSRodney W. Grimes 
12187f6c662SJulian Elischer static	d_open_t	bpfopen;
12287f6c662SJulian Elischer static	d_close_t	bpfclose;
12387f6c662SJulian Elischer static	d_read_t	bpfread;
12487f6c662SJulian Elischer static	d_write_t	bpfwrite;
12587f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
126243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
12795aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
12887f6c662SJulian Elischer 
12987f6c662SJulian Elischer #define CDEV_MAJOR 23
1304e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
1317ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1327ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1337ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1347ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1357ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1367ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1377ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
1387ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
13995aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1404e2f199eSPoul-Henning Kamp };
14187f6c662SJulian Elischer 
14295aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
14395aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
14487f6c662SJulian Elischer 
145df8bae1dSRodney W. Grimes static int
146df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
1478994a245SDag-Erling Smørgrav 	struct uio *uio;
148df8bae1dSRodney W. Grimes 	int linktype, *datlen;
1498994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1508994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
151df8bae1dSRodney W. Grimes {
152df8bae1dSRodney W. Grimes 	struct mbuf *m;
153df8bae1dSRodney W. Grimes 	int error;
154df8bae1dSRodney W. Grimes 	int len;
155df8bae1dSRodney W. Grimes 	int hlen;
156df8bae1dSRodney W. Grimes 
157df8bae1dSRodney W. Grimes 	/*
158df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
159df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
160df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
161df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
162df8bae1dSRodney W. Grimes 	 * is forwarded as is.
163df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
164df8bae1dSRodney W. Grimes 	 * for the link level header.
165df8bae1dSRodney W. Grimes 	 */
166df8bae1dSRodney W. Grimes 	switch (linktype) {
167df8bae1dSRodney W. Grimes 
168df8bae1dSRodney W. Grimes 	case DLT_SLIP:
169df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
170df8bae1dSRodney W. Grimes 		hlen = 0;
171df8bae1dSRodney W. Grimes 		break;
172df8bae1dSRodney W. Grimes 
173df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
174df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
175df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
176797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
177df8bae1dSRodney W. Grimes 		break;
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	case DLT_FDDI:
180d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
181d41f24e7SDavid Greenman 		hlen = 0;
182df8bae1dSRodney W. Grimes 		break;
183df8bae1dSRodney W. Grimes 
18422f05c43SAndrey A. Chernov 	case DLT_RAW:
185df8bae1dSRodney W. Grimes 	case DLT_NULL:
186df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
187df8bae1dSRodney W. Grimes 		hlen = 0;
188df8bae1dSRodney W. Grimes 		break;
189df8bae1dSRodney W. Grimes 
1904f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1914f53e3ccSKenjiro Cho 		/*
1924f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1934f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1944f53e3ccSKenjiro Cho 		 * specified anyway.
1954f53e3ccSKenjiro Cho 		 */
1964f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1974f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
1984f53e3ccSKenjiro Cho 		break;
1994f53e3ccSKenjiro Cho 
20030fa52a6SBrian Somers 	case DLT_PPP:
20130fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
20230fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
20330fa52a6SBrian Somers 		break;
20430fa52a6SBrian Somers 
205df8bae1dSRodney W. Grimes 	default:
206df8bae1dSRodney W. Grimes 		return (EIO);
207df8bae1dSRodney W. Grimes 	}
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
210df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
211df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
212df8bae1dSRodney W. Grimes 		return (EIO);
213df8bae1dSRodney W. Grimes 
214963e4c2aSGarrett Wollman 	if (len > MHLEN) {
215a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
21624a229f4SSam Leffler 	} else {
217a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
218df8bae1dSRodney W. Grimes 	}
21924a229f4SSam Leffler 	if (m == NULL)
22024a229f4SSam Leffler 		return (ENOBUFS);
221963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
222963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
223df8bae1dSRodney W. Grimes 	*mp = m;
22424a229f4SSam Leffler 
225df8bae1dSRodney W. Grimes 	/*
226df8bae1dSRodney W. Grimes 	 * Make room for link header.
227df8bae1dSRodney W. Grimes 	 */
228df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2294f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
230df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
231df8bae1dSRodney W. Grimes #if BSD >= 199103
232df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
233df8bae1dSRodney W. Grimes #else
234df8bae1dSRodney W. Grimes 		m->m_off += hlen;
235df8bae1dSRodney W. Grimes #endif
236c9524588SDag-Erling Smørgrav 		error = uiomove(sockp->sa_data, hlen, uio);
237df8bae1dSRodney W. Grimes 		if (error)
238df8bae1dSRodney W. Grimes 			goto bad;
239df8bae1dSRodney W. Grimes 	}
240c9524588SDag-Erling Smørgrav 	error = uiomove(mtod(m, void *), len - hlen, uio);
241df8bae1dSRodney W. Grimes 	if (!error)
242df8bae1dSRodney W. Grimes 		return (0);
243df8bae1dSRodney W. Grimes bad:
244df8bae1dSRodney W. Grimes 	m_freem(m);
245df8bae1dSRodney W. Grimes 	return (error);
246df8bae1dSRodney W. Grimes }
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes /*
249df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
250df8bae1dSRodney W. Grimes  */
251df8bae1dSRodney W. Grimes static void
252df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
253df8bae1dSRodney W. Grimes 	struct bpf_d *d;
254df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
255df8bae1dSRodney W. Grimes {
256df8bae1dSRodney W. Grimes 	/*
257df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
258df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
259df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
260df8bae1dSRodney W. Grimes 	 */
261e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
262df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
263df8bae1dSRodney W. Grimes 	d->bd_next = bp->bif_dlist;
264df8bae1dSRodney W. Grimes 	bp->bif_dlist = d;
265df8bae1dSRodney W. Grimes 
26624a229f4SSam Leffler 	*bp->bif_driverp = bp;
267e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
268df8bae1dSRodney W. Grimes }
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes /*
271df8bae1dSRodney W. Grimes  * Detach a file from its interface.
272df8bae1dSRodney W. Grimes  */
273df8bae1dSRodney W. Grimes static void
274df8bae1dSRodney W. Grimes bpf_detachd(d)
275df8bae1dSRodney W. Grimes 	struct bpf_d *d;
276df8bae1dSRodney W. Grimes {
2776e891d64SPoul-Henning Kamp 	int error;
278df8bae1dSRodney W. Grimes 	struct bpf_d **p;
279df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
280df8bae1dSRodney W. Grimes 
281e0111e4dSSam Leffler 	/* XXX locking */
282df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
283e0111e4dSSam Leffler 	d->bd_bif = 0;
284df8bae1dSRodney W. Grimes 	/*
285df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
286df8bae1dSRodney W. Grimes 	 * If so, turn it off.
287df8bae1dSRodney W. Grimes 	 */
288df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
289df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
2906e891d64SPoul-Henning Kamp 		error = ifpromisc(bp->bif_ifp, 0);
2916e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
292df8bae1dSRodney W. Grimes 			/*
2936e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
294df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
295df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
296df8bae1dSRodney W. Grimes 			 * take it out.
297df8bae1dSRodney W. Grimes 			 */
2988eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
2998eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3006e891d64SPoul-Henning Kamp 		}
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes 	/* Remove d from the interface's descriptor list. */
303e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
304df8bae1dSRodney W. Grimes 	p = &bp->bif_dlist;
305df8bae1dSRodney W. Grimes 	while (*p != d) {
306df8bae1dSRodney W. Grimes 		p = &(*p)->bd_next;
307df8bae1dSRodney W. Grimes 		if (*p == 0)
308df8bae1dSRodney W. Grimes 			panic("bpf_detachd: descriptor not in list");
309df8bae1dSRodney W. Grimes 	}
310df8bae1dSRodney W. Grimes 	*p = (*p)->bd_next;
311df8bae1dSRodney W. Grimes 	if (bp->bif_dlist == 0)
312df8bae1dSRodney W. Grimes 		/*
313df8bae1dSRodney W. Grimes 		 * Let the driver know that there are no more listeners.
314df8bae1dSRodney W. Grimes 		 */
315e0111e4dSSam Leffler 		*bp->bif_driverp = 0;
316e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
317df8bae1dSRodney W. Grimes }
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes /*
320df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
321df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
322df8bae1dSRodney W. Grimes  */
323df8bae1dSRodney W. Grimes /* ARGSUSED */
32487f6c662SJulian Elischer static	int
325b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
326df8bae1dSRodney W. Grimes 	dev_t dev;
32760039670SBruce Evans 	int flags;
32860039670SBruce Evans 	int fmt;
329b40ce416SJulian Elischer 	struct thread *td;
330df8bae1dSRodney W. Grimes {
331e7bb21b3SJonathan Lemon 	struct bpf_d *d;
332df8bae1dSRodney W. Grimes 
333e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
334bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
335df8bae1dSRodney W. Grimes 	/*
336df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
337df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
338df8bae1dSRodney W. Grimes 	 */
339e7bb21b3SJonathan Lemon 	if (d) {
340e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
341df8bae1dSRodney W. Grimes 		return (EBUSY);
342e7bb21b3SJonathan Lemon 	}
343e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
344e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
345e7bb21b3SJonathan Lemon 
346d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
347b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
348b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
349a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
350bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
351df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
35200a83887SPaul Traina 	d->bd_sig = SIGIO;
3538ed3828cSRobert Watson 	d->bd_seesent = 1;
35482f4445dSRobert Watson #ifdef MAC
35582f4445dSRobert Watson 	mac_init_bpfdesc(d);
35682f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
35782f4445dSRobert Watson #endif
3586008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
359c06eb4e2SSam Leffler 	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes 	return (0);
362df8bae1dSRodney W. Grimes }
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes /*
365df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
366df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
367df8bae1dSRodney W. Grimes  */
368df8bae1dSRodney W. Grimes /* ARGSUSED */
36987f6c662SJulian Elischer static	int
370b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
371df8bae1dSRodney W. Grimes 	dev_t dev;
37260039670SBruce Evans 	int flags;
37360039670SBruce Evans 	int fmt;
374b40ce416SJulian Elischer 	struct thread *td;
375df8bae1dSRodney W. Grimes {
376e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
377df8bae1dSRodney W. Grimes 
37881bda851SJohn Polstra 	BPFD_LOCK(d);
37981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
38081bda851SJohn Polstra 		callout_stop(&d->bd_callout);
38181bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
38281bda851SJohn Polstra 	BPFD_UNLOCK(d);
383e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
384e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
385df8bae1dSRodney W. Grimes 	if (d->bd_bif)
386df8bae1dSRodney W. Grimes 		bpf_detachd(d);
387e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
38882f4445dSRobert Watson #ifdef MAC
38982f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
39082f4445dSRobert Watson #endif /* MAC */
391df8bae1dSRodney W. Grimes 	bpf_freed(d);
392bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
393d722be54SLuigi Rizzo 	free(d, M_BPF);
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes 	return (0);
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes /*
400df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
401df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
402df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
403df8bae1dSRodney W. Grimes  */
404df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
405df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
406df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
407df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
408df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
409df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
410df8bae1dSRodney W. Grimes /*
411df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
412df8bae1dSRodney W. Grimes  */
41387f6c662SJulian Elischer static	int
41460039670SBruce Evans bpfread(dev, uio, ioflag)
415df8bae1dSRodney W. Grimes 	dev_t dev;
4168994a245SDag-Erling Smørgrav 	struct uio *uio;
41760039670SBruce Evans 	int ioflag;
418df8bae1dSRodney W. Grimes {
419e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
42081bda851SJohn Polstra 	int timed_out;
421df8bae1dSRodney W. Grimes 	int error;
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes 	/*
424df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
425df8bae1dSRodney W. Grimes 	 * as kernel buffers.
426df8bae1dSRodney W. Grimes 	 */
427df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
428df8bae1dSRodney W. Grimes 		return (EINVAL);
429df8bae1dSRodney W. Grimes 
430e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
43181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
43281bda851SJohn Polstra 		callout_stop(&d->bd_callout);
43381bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
43481bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
435df8bae1dSRodney W. Grimes 	/*
436df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
437df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
438df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
439df8bae1dSRodney W. Grimes 	 */
440df8bae1dSRodney W. Grimes 	while (d->bd_hbuf == 0) {
44181bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
442df8bae1dSRodney W. Grimes 			/*
443df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
444df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
445df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
446df8bae1dSRodney W. Grimes 			 */
447df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
448df8bae1dSRodney W. Grimes 			break;
449df8bae1dSRodney W. Grimes 		}
450de5d9935SRobert Watson 
451de5d9935SRobert Watson 		/*
452de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
453de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
454de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
455de5d9935SRobert Watson 		 * it before using it again.
456de5d9935SRobert Watson 		 */
457de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
458e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
459de5d9935SRobert Watson 			return (ENXIO);
460de5d9935SRobert Watson 		}
461de5d9935SRobert Watson 
462fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
463e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
464fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
465fba3cfdeSJohn Polstra 		}
466521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
467e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
468df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
469e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
470df8bae1dSRodney W. Grimes 			return (error);
471df8bae1dSRodney W. Grimes 		}
472df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
473df8bae1dSRodney W. Grimes 			/*
474df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
475df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
476df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
477df8bae1dSRodney W. Grimes 			 */
478df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
479df8bae1dSRodney W. Grimes 				/*
480df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
481df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
482df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
483df8bae1dSRodney W. Grimes 				 */
484df8bae1dSRodney W. Grimes 				break;
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
487e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
488df8bae1dSRodney W. Grimes 				return (0);
489df8bae1dSRodney W. Grimes 			}
490df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
491df8bae1dSRodney W. Grimes 			break;
492df8bae1dSRodney W. Grimes 		}
493df8bae1dSRodney W. Grimes 	}
494df8bae1dSRodney W. Grimes 	/*
495df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
496df8bae1dSRodney W. Grimes 	 */
497e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 	/*
500df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
501df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
502df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
503df8bae1dSRodney W. Grimes 	 */
504e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
505df8bae1dSRodney W. Grimes 
506e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
507df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
508df8bae1dSRodney W. Grimes 	d->bd_hbuf = 0;
509df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
510e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes 	return (error);
513df8bae1dSRodney W. Grimes }
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes 
516df8bae1dSRodney W. Grimes /*
517df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
518df8bae1dSRodney W. Grimes  */
519e7bb21b3SJonathan Lemon static __inline void
520df8bae1dSRodney W. Grimes bpf_wakeup(d)
5218994a245SDag-Erling Smørgrav 	struct bpf_d *d;
522df8bae1dSRodney W. Grimes {
52381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
52481bda851SJohn Polstra 		callout_stop(&d->bd_callout);
52581bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
52681bda851SJohn Polstra 	}
527521f364bSDag-Erling Smørgrav 	wakeup(d);
528831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
529f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
53000a83887SPaul Traina 
531512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
53295aab9ccSJohn-Mark Gurney 	KNOTE(&d->bd_sel.si_note, 0);
533df8bae1dSRodney W. Grimes }
534df8bae1dSRodney W. Grimes 
53581bda851SJohn Polstra static void
53681bda851SJohn Polstra bpf_timed_out(arg)
53781bda851SJohn Polstra 	void *arg;
53881bda851SJohn Polstra {
53981bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
54081bda851SJohn Polstra 
54181bda851SJohn Polstra 	BPFD_LOCK(d);
54281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
54381bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
54481bda851SJohn Polstra 		if (d->bd_slen != 0)
54581bda851SJohn Polstra 			bpf_wakeup(d);
54681bda851SJohn Polstra 	}
54781bda851SJohn Polstra 	BPFD_UNLOCK(d);
54881bda851SJohn Polstra }
54981bda851SJohn Polstra 
55087f6c662SJulian Elischer static	int
55160039670SBruce Evans bpfwrite(dev, uio, ioflag)
552df8bae1dSRodney W. Grimes 	dev_t dev;
553df8bae1dSRodney W. Grimes 	struct uio *uio;
55460039670SBruce Evans 	int ioflag;
555df8bae1dSRodney W. Grimes {
556e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
557df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
558df8bae1dSRodney W. Grimes 	struct mbuf *m;
559e7bb21b3SJonathan Lemon 	int error;
560df8bae1dSRodney W. Grimes 	static struct sockaddr dst;
561df8bae1dSRodney W. Grimes 	int datlen;
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes 	if (d->bd_bif == 0)
564df8bae1dSRodney W. Grimes 		return (ENXIO);
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
569df8bae1dSRodney W. Grimes 		return (0);
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
572df8bae1dSRodney W. Grimes 	if (error)
573df8bae1dSRodney W. Grimes 		return (error);
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
576df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
577df8bae1dSRodney W. Grimes 
578114ae644SMike Smith 	if (d->bd_hdrcmplt)
579114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
580114ae644SMike Smith 
581e7bb21b3SJonathan Lemon 	mtx_lock(&Giant);
58282f4445dSRobert Watson #ifdef MAC
58382f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
58482f4445dSRobert Watson #endif
585df8bae1dSRodney W. Grimes 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
586e7bb21b3SJonathan Lemon 	mtx_unlock(&Giant);
587df8bae1dSRodney W. Grimes 	/*
588df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
589df8bae1dSRodney W. Grimes 	 */
590df8bae1dSRodney W. Grimes 	return (error);
591df8bae1dSRodney W. Grimes }
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes /*
594df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
595e7bb21b3SJonathan Lemon  * receive and drop counts.
596df8bae1dSRodney W. Grimes  */
597df8bae1dSRodney W. Grimes static void
598df8bae1dSRodney W. Grimes reset_d(d)
599df8bae1dSRodney W. Grimes 	struct bpf_d *d;
600df8bae1dSRodney W. Grimes {
601e7bb21b3SJonathan Lemon 
602e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
603df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
604df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
605df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
606df8bae1dSRodney W. Grimes 		d->bd_hbuf = 0;
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
609df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
610df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
611df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
612df8bae1dSRodney W. Grimes }
613df8bae1dSRodney W. Grimes 
614df8bae1dSRodney W. Grimes /*
615df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
616df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
617df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
618df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
619df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
620df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
621df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
622df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
623df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
624df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
625df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
626df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
627df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
628df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
629114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
630114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6318ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6328ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
633df8bae1dSRodney W. Grimes  */
634df8bae1dSRodney W. Grimes /* ARGSUSED */
63587f6c662SJulian Elischer static	int
636b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
637df8bae1dSRodney W. Grimes 	dev_t dev;
638ecbb00a2SDoug Rabson 	u_long cmd;
639df8bae1dSRodney W. Grimes 	caddr_t addr;
64060039670SBruce Evans 	int flags;
641b40ce416SJulian Elischer 	struct thread *td;
642df8bae1dSRodney W. Grimes {
643e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
644e7bb21b3SJonathan Lemon 	int error = 0;
645df8bae1dSRodney W. Grimes 
64681bda851SJohn Polstra 	BPFD_LOCK(d);
64781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
64881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
64981bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
65081bda851SJohn Polstra 	BPFD_UNLOCK(d);
65181bda851SJohn Polstra 
652df8bae1dSRodney W. Grimes 	switch (cmd) {
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes 	default:
655df8bae1dSRodney W. Grimes 		error = EINVAL;
656df8bae1dSRodney W. Grimes 		break;
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	/*
659df8bae1dSRodney W. Grimes 	 * Check for read packet available.
660df8bae1dSRodney W. Grimes 	 */
661df8bae1dSRodney W. Grimes 	case FIONREAD:
662df8bae1dSRodney W. Grimes 		{
663df8bae1dSRodney W. Grimes 			int n;
664df8bae1dSRodney W. Grimes 
665e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
666df8bae1dSRodney W. Grimes 			n = d->bd_slen;
667df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
668df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
669e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 			*(int *)addr = n;
672df8bae1dSRodney W. Grimes 			break;
673df8bae1dSRodney W. Grimes 		}
674df8bae1dSRodney W. Grimes 
675df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
676df8bae1dSRodney W. Grimes 		{
677df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 			if (d->bd_bif == 0)
680df8bae1dSRodney W. Grimes 				error = EINVAL;
681df8bae1dSRodney W. Grimes 			else {
682df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
683df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
684df8bae1dSRodney W. Grimes 			}
685df8bae1dSRodney W. Grimes 			break;
686df8bae1dSRodney W. Grimes 		}
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 	/*
689df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
690df8bae1dSRodney W. Grimes 	 */
691df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
692df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
693df8bae1dSRodney W. Grimes 		break;
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	/*
696df8bae1dSRodney W. Grimes 	 * Set buffer length.
697df8bae1dSRodney W. Grimes 	 */
698df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
699df8bae1dSRodney W. Grimes 		if (d->bd_bif != 0)
700df8bae1dSRodney W. Grimes 			error = EINVAL;
701df8bae1dSRodney W. Grimes 		else {
7028994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
703df8bae1dSRodney W. Grimes 
704eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
705eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
706df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
707df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
708df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
709df8bae1dSRodney W. Grimes 		}
710df8bae1dSRodney W. Grimes 		break;
711df8bae1dSRodney W. Grimes 
712df8bae1dSRodney W. Grimes 	/*
713df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
714df8bae1dSRodney W. Grimes 	 */
715df8bae1dSRodney W. Grimes 	case BIOCSETF:
716df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
717df8bae1dSRodney W. Grimes 		break;
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	/*
720df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
721df8bae1dSRodney W. Grimes 	 */
722df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
723e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
724df8bae1dSRodney W. Grimes 		reset_d(d);
725e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
726df8bae1dSRodney W. Grimes 		break;
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 	/*
729df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
730df8bae1dSRodney W. Grimes 	 */
731df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
732df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0) {
733df8bae1dSRodney W. Grimes 			/*
734df8bae1dSRodney W. Grimes 			 * No interface attached yet.
735df8bae1dSRodney W. Grimes 			 */
736df8bae1dSRodney W. Grimes 			error = EINVAL;
737df8bae1dSRodney W. Grimes 			break;
738df8bae1dSRodney W. Grimes 		}
739df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
740e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
741df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
742e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
743df8bae1dSRodney W. Grimes 			if (error == 0)
744df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
745df8bae1dSRodney W. Grimes 		}
746df8bae1dSRodney W. Grimes 		break;
747df8bae1dSRodney W. Grimes 
748df8bae1dSRodney W. Grimes 	/*
7498eab61f3SSam Leffler 	 * Get current data link type.
750df8bae1dSRodney W. Grimes 	 */
751df8bae1dSRodney W. Grimes 	case BIOCGDLT:
752df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
753df8bae1dSRodney W. Grimes 			error = EINVAL;
754df8bae1dSRodney W. Grimes 		else
755df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
756df8bae1dSRodney W. Grimes 		break;
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	/*
7598eab61f3SSam Leffler 	 * Get a list of supported data link types.
7608eab61f3SSam Leffler 	 */
7618eab61f3SSam Leffler 	case BIOCGDLTLIST:
7628eab61f3SSam Leffler 		if (d->bd_bif == 0)
7638eab61f3SSam Leffler 			error = EINVAL;
7648eab61f3SSam Leffler 		else
7658eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7668eab61f3SSam Leffler 		break;
7678eab61f3SSam Leffler 
7688eab61f3SSam Leffler 	/*
7698eab61f3SSam Leffler 	 * Set data link type.
7708eab61f3SSam Leffler 	 */
7718eab61f3SSam Leffler 	case BIOCSDLT:
7728eab61f3SSam Leffler 		if (d->bd_bif == 0)
7738eab61f3SSam Leffler 			error = EINVAL;
7748eab61f3SSam Leffler 		else
7758eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7768eab61f3SSam Leffler 		break;
7778eab61f3SSam Leffler 
7788eab61f3SSam Leffler 	/*
7791dd0feaaSArchie Cobbs 	 * Get interface name.
780df8bae1dSRodney W. Grimes 	 */
781df8bae1dSRodney W. Grimes 	case BIOCGETIF:
782df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
783df8bae1dSRodney W. Grimes 			error = EINVAL;
7841dd0feaaSArchie Cobbs 		else {
7851dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7861dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7871dd0feaaSArchie Cobbs 
7889bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
7899bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
7901dd0feaaSArchie Cobbs 		}
791df8bae1dSRodney W. Grimes 		break;
792df8bae1dSRodney W. Grimes 
793df8bae1dSRodney W. Grimes 	/*
794df8bae1dSRodney W. Grimes 	 * Set interface.
795df8bae1dSRodney W. Grimes 	 */
796df8bae1dSRodney W. Grimes 	case BIOCSETIF:
797df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
798df8bae1dSRodney W. Grimes 		break;
799df8bae1dSRodney W. Grimes 
800df8bae1dSRodney W. Grimes 	/*
801df8bae1dSRodney W. Grimes 	 * Set read timeout.
802df8bae1dSRodney W. Grimes 	 */
803df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
804df8bae1dSRodney W. Grimes 		{
805df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
806df8bae1dSRodney W. Grimes 
807bdc2cdc5SAlexander Langer 			/*
808bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
809bdc2cdc5SAlexander Langer 			 * a one-shot timer.
810bdc2cdc5SAlexander Langer 			 */
811bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
812bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
813df8bae1dSRodney W. Grimes 			break;
814df8bae1dSRodney W. Grimes 		}
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
817df8bae1dSRodney W. Grimes 	 * Get read timeout.
818df8bae1dSRodney W. Grimes 	 */
819df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
820df8bae1dSRodney W. Grimes 		{
821df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
822df8bae1dSRodney W. Grimes 
823bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
824bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
825df8bae1dSRodney W. Grimes 			break;
826df8bae1dSRodney W. Grimes 		}
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	/*
829df8bae1dSRodney W. Grimes 	 * Get packet stats.
830df8bae1dSRodney W. Grimes 	 */
831df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
832df8bae1dSRodney W. Grimes 		{
833df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
834df8bae1dSRodney W. Grimes 
835df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
836df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
837df8bae1dSRodney W. Grimes 			break;
838df8bae1dSRodney W. Grimes 		}
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 	/*
841df8bae1dSRodney W. Grimes 	 * Set immediate mode.
842df8bae1dSRodney W. Grimes 	 */
843df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
844df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
845df8bae1dSRodney W. Grimes 		break;
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 	case BIOCVERSION:
848df8bae1dSRodney W. Grimes 		{
849df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
850df8bae1dSRodney W. Grimes 
851df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
852df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
853df8bae1dSRodney W. Grimes 			break;
854df8bae1dSRodney W. Grimes 		}
85500a83887SPaul Traina 
856114ae644SMike Smith 	/*
857114ae644SMike Smith 	 * Get "header already complete" flag
858114ae644SMike Smith 	 */
859114ae644SMike Smith 	case BIOCGHDRCMPLT:
860114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
861114ae644SMike Smith 		break;
862114ae644SMike Smith 
863114ae644SMike Smith 	/*
864114ae644SMike Smith 	 * Set "header already complete" flag
865114ae644SMike Smith 	 */
866114ae644SMike Smith 	case BIOCSHDRCMPLT:
867114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
868114ae644SMike Smith 		break;
869114ae644SMike Smith 
8708ed3828cSRobert Watson 	/*
8718ed3828cSRobert Watson 	 * Get "see sent packets" flag
8728ed3828cSRobert Watson 	 */
8738ed3828cSRobert Watson 	case BIOCGSEESENT:
8748ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8758ed3828cSRobert Watson 		break;
8768ed3828cSRobert Watson 
8778ed3828cSRobert Watson 	/*
8788ed3828cSRobert Watson 	 * Set "see sent packets" flag
8798ed3828cSRobert Watson 	 */
8808ed3828cSRobert Watson 	case BIOCSSEESENT:
8818ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8828ed3828cSRobert Watson 		break;
8838ed3828cSRobert Watson 
88400a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
88500a83887SPaul Traina 		break;
88600a83887SPaul Traina 
88700a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
88800a83887SPaul Traina 		d->bd_async = *(int *)addr;
88900a83887SPaul Traina 		break;
89000a83887SPaul Traina 
891831d27a9SDon Lewis 	case FIOSETOWN:
892831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
89300a83887SPaul Traina 		break;
89400a83887SPaul Traina 
895831d27a9SDon Lewis 	case FIOGETOWN:
89691e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
897831d27a9SDon Lewis 		break;
898831d27a9SDon Lewis 
899831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
900831d27a9SDon Lewis 	case TIOCSPGRP:
901831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
902831d27a9SDon Lewis 		break;
903831d27a9SDon Lewis 
904831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
90500a83887SPaul Traina 	case TIOCGPGRP:
90691e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
90700a83887SPaul Traina 		break;
90800a83887SPaul Traina 
90900a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
91000a83887SPaul Traina 		{
91100a83887SPaul Traina 			u_int sig;
91200a83887SPaul Traina 
91300a83887SPaul Traina 			sig = *(u_int *)addr;
91400a83887SPaul Traina 
91500a83887SPaul Traina 			if (sig >= NSIG)
91600a83887SPaul Traina 				error = EINVAL;
91700a83887SPaul Traina 			else
91800a83887SPaul Traina 				d->bd_sig = sig;
91900a83887SPaul Traina 			break;
92000a83887SPaul Traina 		}
92100a83887SPaul Traina 	case BIOCGRSIG:
92200a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
92300a83887SPaul Traina 		break;
924df8bae1dSRodney W. Grimes 	}
925df8bae1dSRodney W. Grimes 	return (error);
926df8bae1dSRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes /*
929df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
930df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
931df8bae1dSRodney W. Grimes  */
932f708ef1bSPoul-Henning Kamp static int
933df8bae1dSRodney W. Grimes bpf_setf(d, fp)
934df8bae1dSRodney W. Grimes 	struct bpf_d *d;
935df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
936df8bae1dSRodney W. Grimes {
937df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
938df8bae1dSRodney W. Grimes 	u_int flen, size;
939df8bae1dSRodney W. Grimes 
940df8bae1dSRodney W. Grimes 	old = d->bd_filter;
941df8bae1dSRodney W. Grimes 	if (fp->bf_insns == 0) {
942df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
943df8bae1dSRodney W. Grimes 			return (EINVAL);
944e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
945df8bae1dSRodney W. Grimes 		d->bd_filter = 0;
946df8bae1dSRodney W. Grimes 		reset_d(d);
947e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
948df8bae1dSRodney W. Grimes 		if (old != 0)
949bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
950df8bae1dSRodney W. Grimes 		return (0);
951df8bae1dSRodney W. Grimes 	}
952df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
953df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
954df8bae1dSRodney W. Grimes 		return (EINVAL);
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
957a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
958df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
959df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
960e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
961df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
962df8bae1dSRodney W. Grimes 		reset_d(d);
963e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
964df8bae1dSRodney W. Grimes 		if (old != 0)
965bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
966df8bae1dSRodney W. Grimes 
967df8bae1dSRodney W. Grimes 		return (0);
968df8bae1dSRodney W. Grimes 	}
969bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
970df8bae1dSRodney W. Grimes 	return (EINVAL);
971df8bae1dSRodney W. Grimes }
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes /*
974df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
975df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
976df8bae1dSRodney W. Grimes  * Return an errno or 0.
977df8bae1dSRodney W. Grimes  */
978df8bae1dSRodney W. Grimes static int
979df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
980df8bae1dSRodney W. Grimes 	struct bpf_d *d;
981df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
982df8bae1dSRodney W. Grimes {
983df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
984e7bb21b3SJonathan Lemon 	int error;
9859b44ff22SGarrett Wollman 	struct ifnet *theywant;
986df8bae1dSRodney W. Grimes 
9879b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
9889b44ff22SGarrett Wollman 	if (theywant == 0)
9899b44ff22SGarrett Wollman 		return ENXIO;
9909b44ff22SGarrett Wollman 
991df8bae1dSRodney W. Grimes 	/*
992df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
993df8bae1dSRodney W. Grimes 	 */
994e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
995df8bae1dSRodney W. Grimes 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
996df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
997df8bae1dSRodney W. Grimes 
9989b44ff22SGarrett Wollman 		if (ifp == 0 || ifp != theywant)
999df8bae1dSRodney W. Grimes 			continue;
100024a229f4SSam Leffler 		/* skip additional entry */
100124a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
100224a229f4SSam Leffler 			continue;
1003e7bb21b3SJonathan Lemon 
1004e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1005df8bae1dSRodney W. Grimes 		/*
1006df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1007df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
1008df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1009df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1010df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1011df8bae1dSRodney W. Grimes 		 */
1012df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
1013df8bae1dSRodney W. Grimes 			return (ENETDOWN);
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 		if (d->bd_sbuf == 0) {
1016df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1017df8bae1dSRodney W. Grimes 			if (error != 0)
1018df8bae1dSRodney W. Grimes 				return (error);
1019df8bae1dSRodney W. Grimes 		}
1020df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1021df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1022df8bae1dSRodney W. Grimes 				/*
1023df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1024df8bae1dSRodney W. Grimes 				 */
1025df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1028df8bae1dSRodney W. Grimes 		}
1029e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1030df8bae1dSRodney W. Grimes 		reset_d(d);
1031e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1032df8bae1dSRodney W. Grimes 		return (0);
1033df8bae1dSRodney W. Grimes 	}
1034e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1035df8bae1dSRodney W. Grimes 	/* Not found. */
1036df8bae1dSRodney W. Grimes 	return (ENXIO);
1037df8bae1dSRodney W. Grimes }
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes /*
1040243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1041df8bae1dSRodney W. Grimes  *
1042df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1043df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1044df8bae1dSRodney W. Grimes  */
104537c84183SPoul-Henning Kamp static int
1046b40ce416SJulian Elischer bpfpoll(dev, events, td)
10478994a245SDag-Erling Smørgrav 	dev_t dev;
1048243ac7d8SPeter Wemm 	int events;
1049b40ce416SJulian Elischer 	struct thread *td;
1050df8bae1dSRodney W. Grimes {
1051e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10520832fc64SGarance A Drosehn 	int revents;
1053df8bae1dSRodney W. Grimes 
1054bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1055de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1056de5d9935SRobert Watson 		return (ENXIO);
1057de5d9935SRobert Watson 
10580832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1059e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
106075c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
106195aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1062243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
106381bda851SJohn Polstra 		else {
1064ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
106581bda851SJohn Polstra 			/* Start the read timeout if necessary. */
106681bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
106781bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
106881bda851SJohn Polstra 				    bpf_timed_out, d);
106981bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
107081bda851SJohn Polstra 			}
107181bda851SJohn Polstra 		}
107275c13541SPoul-Henning Kamp 	}
1073e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1074243ac7d8SPeter Wemm 	return (revents);
1075df8bae1dSRodney W. Grimes }
1076df8bae1dSRodney W. Grimes 
1077df8bae1dSRodney W. Grimes /*
107895aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
107995aab9ccSJohn-Mark Gurney  * reject all others.
108095aab9ccSJohn-Mark Gurney  */
108195aab9ccSJohn-Mark Gurney int
108295aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
108395aab9ccSJohn-Mark Gurney 	dev_t dev;
108495aab9ccSJohn-Mark Gurney 	struct knote *kn;
108595aab9ccSJohn-Mark Gurney {
108695aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
108795aab9ccSJohn-Mark Gurney 
108895aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
108995aab9ccSJohn-Mark Gurney 		return (1);
109095aab9ccSJohn-Mark Gurney 
109195aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
109295aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
109395aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
109495aab9ccSJohn-Mark Gurney 	SLIST_INSERT_HEAD(&d->bd_sel.si_note, kn, kn_selnext);
109595aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
109695aab9ccSJohn-Mark Gurney 
109795aab9ccSJohn-Mark Gurney 	return (0);
109895aab9ccSJohn-Mark Gurney }
109995aab9ccSJohn-Mark Gurney 
110095aab9ccSJohn-Mark Gurney static void
110195aab9ccSJohn-Mark Gurney filt_bpfdetach(kn)
110295aab9ccSJohn-Mark Gurney 	struct knote *kn;
110395aab9ccSJohn-Mark Gurney {
110495aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
110595aab9ccSJohn-Mark Gurney 
110695aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
110795aab9ccSJohn-Mark Gurney 	SLIST_REMOVE(&d->bd_sel.si_note, kn, knote, kn_selnext);
110895aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
110995aab9ccSJohn-Mark Gurney }
111095aab9ccSJohn-Mark Gurney 
111195aab9ccSJohn-Mark Gurney static int
111295aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
111395aab9ccSJohn-Mark Gurney 	struct knote *kn;
111495aab9ccSJohn-Mark Gurney 	long hint;
111595aab9ccSJohn-Mark Gurney {
111695aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
111795aab9ccSJohn-Mark Gurney 	int ready;
111895aab9ccSJohn-Mark Gurney 
111995aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
112095aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
112195aab9ccSJohn-Mark Gurney 	if (ready) {
112295aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
112395aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
112495aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
112595aab9ccSJohn-Mark Gurney 	}
112695aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
112795aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
112895aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
112995aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
113095aab9ccSJohn-Mark Gurney 	}
113195aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
113295aab9ccSJohn-Mark Gurney 
113395aab9ccSJohn-Mark Gurney 	return (ready);
113495aab9ccSJohn-Mark Gurney }
113595aab9ccSJohn-Mark Gurney 
113695aab9ccSJohn-Mark Gurney /*
1137df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1138df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1139df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1140df8bae1dSRodney W. Grimes  * buffer.
1141df8bae1dSRodney W. Grimes  */
1142df8bae1dSRodney W. Grimes void
114324a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
114424a229f4SSam Leffler 	struct bpf_if *bp;
11458994a245SDag-Erling Smørgrav 	u_char *pkt;
11468994a245SDag-Erling Smørgrav 	u_int pktlen;
1147df8bae1dSRodney W. Grimes {
11488994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11498994a245SDag-Erling Smørgrav 	u_int slen;
1150e7bb21b3SJonathan Lemon 
1151e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1152df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1153e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1154df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1155df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1156ec272d87SRobert Watson 		if (slen != 0) {
1157ec272d87SRobert Watson #ifdef MAC
115824a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1159ec272d87SRobert Watson #endif
1160df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1161ec272d87SRobert Watson 		}
1162e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1163df8bae1dSRodney W. Grimes 	}
1164e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1165df8bae1dSRodney W. Grimes }
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes /*
1168df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1169df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1170df8bae1dSRodney W. Grimes  */
1171df8bae1dSRodney W. Grimes static void
1172df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1173df8bae1dSRodney W. Grimes 	const void *src_arg;
1174df8bae1dSRodney W. Grimes 	void *dst_arg;
11758994a245SDag-Erling Smørgrav 	size_t len;
1176df8bae1dSRodney W. Grimes {
11778994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11788994a245SDag-Erling Smørgrav 	u_int count;
1179df8bae1dSRodney W. Grimes 	u_char *dst;
1180df8bae1dSRodney W. Grimes 
1181df8bae1dSRodney W. Grimes 	m = src_arg;
1182df8bae1dSRodney W. Grimes 	dst = dst_arg;
1183df8bae1dSRodney W. Grimes 	while (len > 0) {
1184df8bae1dSRodney W. Grimes 		if (m == 0)
1185df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1186df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11870453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1188df8bae1dSRodney W. Grimes 		m = m->m_next;
1189df8bae1dSRodney W. Grimes 		dst += count;
1190df8bae1dSRodney W. Grimes 		len -= count;
1191df8bae1dSRodney W. Grimes 	}
1192df8bae1dSRodney W. Grimes }
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes /*
1195df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1196df8bae1dSRodney W. Grimes  */
1197df8bae1dSRodney W. Grimes void
119824a229f4SSam Leffler bpf_mtap(bp, m)
119924a229f4SSam Leffler 	struct bpf_if *bp;
1200df8bae1dSRodney W. Grimes 	struct mbuf *m;
1201df8bae1dSRodney W. Grimes {
1202df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1203df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1204df8bae1dSRodney W. Grimes 
1205f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
12067b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
120724a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
12087b831242SPoul-Henning Kamp 		return;
12097b831242SPoul-Henning Kamp 	}
1210df8bae1dSRodney W. Grimes 
1211e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1212df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
12138ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
12148ed3828cSRobert Watson 			continue;
1215e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1216df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1217df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1218df8bae1dSRodney W. Grimes 		if (slen != 0)
12190c7fb534SRobert Watson #ifdef MAC
122024a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
12210c7fb534SRobert Watson #endif
12220c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
12230c7fb534SRobert Watson 				    bpf_mcopy);
1224e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1225df8bae1dSRodney W. Grimes 	}
1226e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1227df8bae1dSRodney W. Grimes }
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes /*
1230df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
1231df8bae1dSRodney W. Grimes  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1232df8bae1dSRodney W. Grimes  * otherwise 0.  "copy" is the routine called to do the actual data
1233df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1234df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1235df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1236df8bae1dSRodney W. Grimes  */
1237df8bae1dSRodney W. Grimes static void
1238df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
12398994a245SDag-Erling Smørgrav 	struct bpf_d *d;
12408994a245SDag-Erling Smørgrav 	u_char *pkt;
12418994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
12428994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1243df8bae1dSRodney W. Grimes {
12448994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
12458994a245SDag-Erling Smørgrav 	int totlen, curlen;
12468994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
1247df8bae1dSRodney W. Grimes 	/*
1248df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1249df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1250df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1251df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1252df8bae1dSRodney W. Grimes 	 */
1253df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1254df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1255df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1256df8bae1dSRodney W. Grimes 
1257df8bae1dSRodney W. Grimes 	/*
1258df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1259df8bae1dSRodney W. Grimes 	 */
1260df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1261df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1262df8bae1dSRodney W. Grimes 		/*
1263df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1264df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1265df8bae1dSRodney W. Grimes 		 * pending reads.
1266df8bae1dSRodney W. Grimes 		 */
1267df8bae1dSRodney W. Grimes 		if (d->bd_fbuf == 0) {
1268df8bae1dSRodney W. Grimes 			/*
1269df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1270df8bae1dSRodney W. Grimes 			 * so drop the packet.
1271df8bae1dSRodney W. Grimes 			 */
1272df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1273df8bae1dSRodney W. Grimes 			return;
1274df8bae1dSRodney W. Grimes 		}
1275df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1276df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1277df8bae1dSRodney W. Grimes 		curlen = 0;
1278df8bae1dSRodney W. Grimes 	}
127981bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1280df8bae1dSRodney W. Grimes 		/*
128181bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
128281bda851SJohn Polstra 		 * already expired during a select call.  A packet
128381bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1284df8bae1dSRodney W. Grimes 		 */
1285df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1286df8bae1dSRodney W. Grimes 
1287df8bae1dSRodney W. Grimes 	/*
1288df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1289df8bae1dSRodney W. Grimes 	 */
1290df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1291df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1292df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1293df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1294df8bae1dSRodney W. Grimes 	/*
1295df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1296df8bae1dSRodney W. Grimes 	 */
1297df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1298df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1299df8bae1dSRodney W. Grimes }
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes /*
1302df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1303df8bae1dSRodney W. Grimes  */
1304df8bae1dSRodney W. Grimes static int
1305df8bae1dSRodney W. Grimes bpf_allocbufs(d)
13068994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1307df8bae1dSRodney W. Grimes {
1308a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1309df8bae1dSRodney W. Grimes 	if (d->bd_fbuf == 0)
1310df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1311df8bae1dSRodney W. Grimes 
1312a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1313df8bae1dSRodney W. Grimes 	if (d->bd_sbuf == 0) {
1314bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1315df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1316df8bae1dSRodney W. Grimes 	}
1317df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1318df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1319df8bae1dSRodney W. Grimes 	return (0);
1320df8bae1dSRodney W. Grimes }
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes /*
1323df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1324df8bae1dSRodney W. Grimes  * Called on close.
1325df8bae1dSRodney W. Grimes  */
1326df8bae1dSRodney W. Grimes static void
1327df8bae1dSRodney W. Grimes bpf_freed(d)
13288994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1329df8bae1dSRodney W. Grimes {
1330df8bae1dSRodney W. Grimes 	/*
1331df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1332df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1333df8bae1dSRodney W. Grimes 	 * free.
1334df8bae1dSRodney W. Grimes 	 */
1335df8bae1dSRodney W. Grimes 	if (d->bd_sbuf != 0) {
1336bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1337df8bae1dSRodney W. Grimes 		if (d->bd_hbuf != 0)
1338bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1339df8bae1dSRodney W. Grimes 		if (d->bd_fbuf != 0)
1340bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1341df8bae1dSRodney W. Grimes 	}
1342df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1343bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1344e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1345df8bae1dSRodney W. Grimes }
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes /*
134824a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
134924a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1350df8bae1dSRodney W. Grimes  */
1351df8bae1dSRodney W. Grimes void
13529b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1353df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1354df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1355df8bae1dSRodney W. Grimes {
135624a229f4SSam Leffler 
135724a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
135824a229f4SSam Leffler }
135924a229f4SSam Leffler 
136024a229f4SSam Leffler /*
136124a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
136224a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
136324a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
136424a229f4SSam Leffler  * headers are not yet supporrted).
136524a229f4SSam Leffler  */
136624a229f4SSam Leffler void
136724a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
136824a229f4SSam Leffler 	struct ifnet *ifp;
136924a229f4SSam Leffler 	u_int dlt, hdrlen;
137024a229f4SSam Leffler 	struct bpf_if **driverp;
137124a229f4SSam Leffler {
1372df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
13736a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1374df8bae1dSRodney W. Grimes 	if (bp == 0)
1375df8bae1dSRodney W. Grimes 		panic("bpfattach");
1376df8bae1dSRodney W. Grimes 
137724a229f4SSam Leffler 	bp->bif_dlist = 0;
137824a229f4SSam Leffler 	bp->bif_driverp = driverp;
1379df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1380df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
13816008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1382df8bae1dSRodney W. Grimes 
1383e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1384df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1385df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1386e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1387df8bae1dSRodney W. Grimes 
138824a229f4SSam Leffler 	*bp->bif_driverp = 0;
1389df8bae1dSRodney W. Grimes 
1390df8bae1dSRodney W. Grimes 	/*
1391df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1392df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1393df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1394df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1395df8bae1dSRodney W. Grimes 	 */
1396df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1397df8bae1dSRodney W. Grimes 
13982eeab939SGarrett Wollman 	if (bootverbose)
139924a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1400df8bae1dSRodney W. Grimes }
140153ac6efbSJulian Elischer 
1402de5d9935SRobert Watson /*
1403de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1404de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1405de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1406de5d9935SRobert Watson  * ENXIO.
1407de5d9935SRobert Watson  */
1408de5d9935SRobert Watson void
1409de5d9935SRobert Watson bpfdetach(ifp)
1410de5d9935SRobert Watson 	struct ifnet *ifp;
1411de5d9935SRobert Watson {
1412de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1413de5d9935SRobert Watson 	struct bpf_d	*d;
1414de5d9935SRobert Watson 
1415de5d9935SRobert Watson 	/* Locate BPF interface information */
1416de5d9935SRobert Watson 	bp_prev = NULL;
14178eab61f3SSam Leffler 
14188eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
1419de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1420de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1421de5d9935SRobert Watson 			break;
1422de5d9935SRobert Watson 		bp_prev = bp;
1423de5d9935SRobert Watson 	}
1424de5d9935SRobert Watson 
1425de5d9935SRobert Watson 	/* Interface wasn't attached */
1426d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1427e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
14289bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1429de5d9935SRobert Watson 		return;
1430de5d9935SRobert Watson 	}
1431de5d9935SRobert Watson 
1432de5d9935SRobert Watson 	if (bp_prev) {
1433de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1434de5d9935SRobert Watson 	} else {
1435de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1436de5d9935SRobert Watson 	}
14378eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1438de5d9935SRobert Watson 
1439e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1440e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1441e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1442e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1443e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1444e7bb21b3SJonathan Lemon 	}
1445e7bb21b3SJonathan Lemon 
1446e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1447de5d9935SRobert Watson 	free(bp, M_BPF);
14488eab61f3SSam Leffler }
1449de5d9935SRobert Watson 
14508eab61f3SSam Leffler /*
14518eab61f3SSam Leffler  * Get a list of available data link type of the interface.
14528eab61f3SSam Leffler  */
14538eab61f3SSam Leffler static int
14548eab61f3SSam Leffler bpf_getdltlist(d, bfl)
14558eab61f3SSam Leffler 	struct bpf_d *d;
14568eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
14578eab61f3SSam Leffler {
14588eab61f3SSam Leffler 	int n, error;
14598eab61f3SSam Leffler 	struct ifnet *ifp;
14608eab61f3SSam Leffler 	struct bpf_if *bp;
14618eab61f3SSam Leffler 
14628eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
14638eab61f3SSam Leffler 	n = 0;
14648eab61f3SSam Leffler 	error = 0;
14658eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14668eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
14678eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
14688eab61f3SSam Leffler 			continue;
14698eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
14708eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1471e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
14728eab61f3SSam Leffler 				return (ENOMEM);
14738eab61f3SSam Leffler 			}
14748eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
14758eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
14768eab61f3SSam Leffler 		}
14778eab61f3SSam Leffler 		n++;
14788eab61f3SSam Leffler 	}
14798eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
14808eab61f3SSam Leffler 	bfl->bfl_len = n;
14818eab61f3SSam Leffler 	return (error);
14828eab61f3SSam Leffler }
14838eab61f3SSam Leffler 
14848eab61f3SSam Leffler /*
14858eab61f3SSam Leffler  * Set the data link type of a BPF instance.
14868eab61f3SSam Leffler  */
14878eab61f3SSam Leffler static int
14888eab61f3SSam Leffler bpf_setdlt(d, dlt)
14898eab61f3SSam Leffler 	struct bpf_d *d;
14908eab61f3SSam Leffler 	u_int dlt;
14918eab61f3SSam Leffler {
14928eab61f3SSam Leffler 	int error, opromisc;
14938eab61f3SSam Leffler 	struct ifnet *ifp;
14948eab61f3SSam Leffler 	struct bpf_if *bp;
14958eab61f3SSam Leffler 
14968eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
14978eab61f3SSam Leffler 		return (0);
14988eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
14998eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15008eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
15018eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
15028eab61f3SSam Leffler 			break;
15038eab61f3SSam Leffler 	}
15048eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15058eab61f3SSam Leffler 	if (bp != NULL) {
15068eab61f3SSam Leffler 		BPFD_LOCK(d);
15078eab61f3SSam Leffler 		opromisc = d->bd_promisc;
15088eab61f3SSam Leffler 		bpf_detachd(d);
15098eab61f3SSam Leffler 		bpf_attachd(d, bp);
15108eab61f3SSam Leffler 		reset_d(d);
15118eab61f3SSam Leffler 		BPFD_UNLOCK(d);
15128eab61f3SSam Leffler 		if (opromisc) {
15138eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
15148eab61f3SSam Leffler 			if (error)
15158eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
15168eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
15178eab61f3SSam Leffler 					error);
15188eab61f3SSam Leffler 			else
15198eab61f3SSam Leffler 				d->bd_promisc = 1;
15208eab61f3SSam Leffler 		}
15218eab61f3SSam Leffler 	}
15228eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1523de5d9935SRobert Watson }
1524de5d9935SRobert Watson 
1525929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1526bd3a5320SPoul-Henning Kamp 
1527929ddbbbSAlfred Perlstein static void bpf_clone(void *arg, char *name, int namelen, dev_t *dev);
15283f54a085SPoul-Henning Kamp 
15293f54a085SPoul-Henning Kamp static void
15303f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
15313f54a085SPoul-Henning Kamp 	void *arg;
15323f54a085SPoul-Henning Kamp 	char *name;
15333f54a085SPoul-Henning Kamp 	int namelen;
15343f54a085SPoul-Henning Kamp 	dev_t *dev;
15353f54a085SPoul-Henning Kamp {
15363f54a085SPoul-Henning Kamp 	int u;
15373f54a085SPoul-Henning Kamp 
15383f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
15393f54a085SPoul-Henning Kamp 		return;
1540db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
15413f54a085SPoul-Henning Kamp 		return;
1542b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1543b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1544b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
15453f54a085SPoul-Henning Kamp 	return;
15463f54a085SPoul-Henning Kamp }
15473f54a085SPoul-Henning Kamp 
1548514ede09SBruce Evans static void
1549514ede09SBruce Evans bpf_drvinit(unused)
1550514ede09SBruce Evans 	void *unused;
155153ac6efbSJulian Elischer {
155253ac6efbSJulian Elischer 
15536008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1554db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
15557198bf47SJulian Elischer }
155653ac6efbSJulian Elischer 
155753ac6efbSJulian Elischer SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
155853ac6efbSJulian Elischer 
15595bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1560f8dc4716SMike Smith /*
1561f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1562f8dc4716SMike Smith  *
1563f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1564f8dc4716SMike Smith  * to be loaded at runtime.
1565f8dc4716SMike Smith  */
1566f8dc4716SMike Smith 
1567f8dc4716SMike Smith void
1568e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1569e5562beeSSam Leffler 	struct bpf_if *bp;
15708994a245SDag-Erling Smørgrav 	u_char *pkt;
15718994a245SDag-Erling Smørgrav 	u_int pktlen;
1572f8dc4716SMike Smith {
1573f8dc4716SMike Smith }
1574f8dc4716SMike Smith 
1575f8dc4716SMike Smith void
1576e5562beeSSam Leffler bpf_mtap(bp, m)
1577e5562beeSSam Leffler 	struct bpf_if *bp;
1578f8dc4716SMike Smith 	struct mbuf *m;
1579f8dc4716SMike Smith {
1580f8dc4716SMike Smith }
1581f8dc4716SMike Smith 
1582f8dc4716SMike Smith void
1583f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1584f8dc4716SMike Smith 	struct ifnet *ifp;
1585f8dc4716SMike Smith 	u_int dlt, hdrlen;
1586f8dc4716SMike Smith {
1587f8dc4716SMike Smith }
1588f8dc4716SMike Smith 
1589da626c17SBill Paul void
15905f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
15915f7a7923SSam Leffler 	struct ifnet *ifp;
15925f7a7923SSam Leffler 	u_int dlt, hdrlen;
15935f7a7923SSam Leffler 	struct bpf_if **driverp;
15945f7a7923SSam Leffler {
15955f7a7923SSam Leffler }
15965f7a7923SSam Leffler 
15975f7a7923SSam Leffler void
1598da626c17SBill Paul bpfdetach(ifp)
1599da626c17SBill Paul 	struct ifnet *ifp;
1600da626c17SBill Paul {
1601da626c17SBill Paul }
1602da626c17SBill Paul 
1603f8dc4716SMike Smith u_int
1604f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
16058994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
16068994a245SDag-Erling Smørgrav 	u_char *p;
1607f8dc4716SMike Smith 	u_int wirelen;
16088994a245SDag-Erling Smørgrav 	u_int buflen;
1609f8dc4716SMike Smith {
1610f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1611f8dc4716SMike Smith }
1612f8dc4716SMike Smith 
16135bb5f2c9SPeter Wemm int
16145bb5f2c9SPeter Wemm bpf_validate(f, len)
16155bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
16165bb5f2c9SPeter Wemm 	int len;
16175bb5f2c9SPeter Wemm {
16185bb5f2c9SPeter Wemm 	return 0;		/* false */
16195bb5f2c9SPeter Wemm }
16205bb5f2c9SPeter Wemm 
16215bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1622