xref: /freebsd/sys/net/bpf.c (revision ad3b9257c2f09a6f1c0e56c1100aa4e43fb95e24)
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  * 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>
4782f4445dSRobert Watson #include <sys/mac.h>
484d1d4912SBruce Evans #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50df8bae1dSRodney W. Grimes #include <sys/time.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
520310c19fSBruce Evans #include <sys/signalvar.h>
53528f627fSBruce Evans #include <sys/filio.h>
54528f627fSBruce Evans #include <sys/sockio.h>
55528f627fSBruce Evans #include <sys/ttycom.h>
56831d27a9SDon Lewis #include <sys/filedesc.h>
57df8bae1dSRodney W. Grimes 
5895aab9ccSJohn-Mark Gurney #include <sys/event.h>
5995aab9ccSJohn-Mark Gurney #include <sys/file.h>
60243ac7d8SPeter Wemm #include <sys/poll.h>
6195aab9ccSJohn-Mark Gurney #include <sys/proc.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <sys/socket.h>
64fba9235dSBruce Evans #include <sys/vnode.h>
65df8bae1dSRodney W. Grimes 
66fba9235dSBruce Evans #include <net/if.h>
67df8bae1dSRodney W. Grimes #include <net/bpf.h>
68df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <netinet/in.h>
71df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
72df8bae1dSRodney W. Grimes #include <sys/kernel.h>
73f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
747b778b5eSEivind Eklund 
75959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
7687f6c662SJulian Elischer 
775bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
7853ac6efbSJulian Elischer 
79df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /*
82df8bae1dSRodney W. Grimes  * The default read buffer size is patchable.
83df8bae1dSRodney W. Grimes  */
84e7bb21b3SJonathan Lemon static int bpf_bufsize = 4096;
85f708ef1bSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
86f708ef1bSPoul-Henning Kamp 	&bpf_bufsize, 0, "");
87eba2a1aeSPoul-Henning Kamp static int bpf_maxbufsize = BPF_MAXBUFSIZE;
88eba2a1aeSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
89eba2a1aeSPoul-Henning Kamp 	&bpf_maxbufsize, 0, "");
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
93df8bae1dSRodney W. Grimes  */
94f708ef1bSPoul-Henning Kamp static struct bpf_if	*bpf_iflist;
95e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
96df8bae1dSRodney W. Grimes 
97929ddbbbSAlfred Perlstein static int	bpf_allocbufs(struct bpf_d *);
98929ddbbbSAlfred Perlstein static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
99929ddbbbSAlfred Perlstein static void	bpf_detachd(struct bpf_d *d);
100929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
101929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
102929ddbbbSAlfred Perlstein static int	bpf_movein(struct uio *, int,
103929ddbbbSAlfred Perlstein 		    struct mbuf **, struct sockaddr *, int *);
104929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
105929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
106e7bb21b3SJonathan Lemon static __inline void
107929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
108929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
109929ddbbbSAlfred Perlstein 		    u_int, void (*)(const void *, void *, size_t));
110929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
111929ddbbbSAlfred Perlstein static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
1128eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1138eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11495aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
11595aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
116df8bae1dSRodney W. Grimes 
11787f6c662SJulian Elischer static	d_open_t	bpfopen;
11887f6c662SJulian Elischer static	d_close_t	bpfclose;
11987f6c662SJulian Elischer static	d_read_t	bpfread;
12087f6c662SJulian Elischer static	d_write_t	bpfwrite;
12187f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
122243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
12395aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
12487f6c662SJulian Elischer 
1254e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
126dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
127dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
1287ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1297ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1307ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1317ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1327ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1337ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1347ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
13595aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1364e2f199eSPoul-Henning Kamp };
13787f6c662SJulian Elischer 
13895aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
13995aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
14087f6c662SJulian Elischer 
141df8bae1dSRodney W. Grimes static int
142df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
1438994a245SDag-Erling Smørgrav 	struct uio *uio;
144df8bae1dSRodney W. Grimes 	int linktype, *datlen;
1458994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1468994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
147df8bae1dSRodney W. Grimes {
148df8bae1dSRodney W. Grimes 	struct mbuf *m;
149df8bae1dSRodney W. Grimes 	int error;
150df8bae1dSRodney W. Grimes 	int len;
151df8bae1dSRodney W. Grimes 	int hlen;
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes 	/*
154df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
155df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
156df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
157df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
158df8bae1dSRodney W. Grimes 	 * is forwarded as is.
159df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
160df8bae1dSRodney W. Grimes 	 * for the link level header.
161df8bae1dSRodney W. Grimes 	 */
162df8bae1dSRodney W. Grimes 	switch (linktype) {
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes 	case DLT_SLIP:
165df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
166df8bae1dSRodney W. Grimes 		hlen = 0;
167df8bae1dSRodney W. Grimes 		break;
168df8bae1dSRodney W. Grimes 
169df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
170df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
171df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
172797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
173df8bae1dSRodney W. Grimes 		break;
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	case DLT_FDDI:
176d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
177d41f24e7SDavid Greenman 		hlen = 0;
178df8bae1dSRodney W. Grimes 		break;
179df8bae1dSRodney W. Grimes 
18022f05c43SAndrey A. Chernov 	case DLT_RAW:
181df8bae1dSRodney W. Grimes 	case DLT_NULL:
182df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
183df8bae1dSRodney W. Grimes 		hlen = 0;
184df8bae1dSRodney W. Grimes 		break;
185df8bae1dSRodney W. Grimes 
1864f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1874f53e3ccSKenjiro Cho 		/*
1884f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1894f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1904f53e3ccSKenjiro Cho 		 * specified anyway.
1914f53e3ccSKenjiro Cho 		 */
1924f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1934f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
1944f53e3ccSKenjiro Cho 		break;
1954f53e3ccSKenjiro Cho 
19630fa52a6SBrian Somers 	case DLT_PPP:
19730fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
19830fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
19930fa52a6SBrian Somers 		break;
20030fa52a6SBrian Somers 
201df8bae1dSRodney W. Grimes 	default:
202df8bae1dSRodney W. Grimes 		return (EIO);
203df8bae1dSRodney W. Grimes 	}
204df8bae1dSRodney W. Grimes 
205df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
206df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
207df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
208df8bae1dSRodney W. Grimes 		return (EIO);
209df8bae1dSRodney W. Grimes 
210963e4c2aSGarrett Wollman 	if (len > MHLEN) {
211a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
21224a229f4SSam Leffler 	} else {
213a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
214df8bae1dSRodney W. Grimes 	}
21524a229f4SSam Leffler 	if (m == NULL)
21624a229f4SSam Leffler 		return (ENOBUFS);
217963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
218963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
219df8bae1dSRodney W. Grimes 	*mp = m;
22024a229f4SSam Leffler 
221df8bae1dSRodney W. Grimes 	/*
222df8bae1dSRodney W. Grimes 	 * Make room for link header.
223df8bae1dSRodney W. Grimes 	 */
224df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2254f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
226df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
227df8bae1dSRodney W. Grimes #if BSD >= 199103
228df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
229df8bae1dSRodney W. Grimes #else
230df8bae1dSRodney W. Grimes 		m->m_off += hlen;
231df8bae1dSRodney W. Grimes #endif
232c9524588SDag-Erling Smørgrav 		error = uiomove(sockp->sa_data, hlen, uio);
233df8bae1dSRodney W. Grimes 		if (error)
234df8bae1dSRodney W. Grimes 			goto bad;
235df8bae1dSRodney W. Grimes 	}
236c9524588SDag-Erling Smørgrav 	error = uiomove(mtod(m, void *), len - hlen, uio);
237df8bae1dSRodney W. Grimes 	if (!error)
238df8bae1dSRodney W. Grimes 		return (0);
239df8bae1dSRodney W. Grimes bad:
240df8bae1dSRodney W. Grimes 	m_freem(m);
241df8bae1dSRodney W. Grimes 	return (error);
242df8bae1dSRodney W. Grimes }
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes /*
245df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
246df8bae1dSRodney W. Grimes  */
247df8bae1dSRodney W. Grimes static void
248df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
249df8bae1dSRodney W. Grimes 	struct bpf_d *d;
250df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
251df8bae1dSRodney W. Grimes {
252df8bae1dSRodney W. Grimes 	/*
253df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
254df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
255df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
256df8bae1dSRodney W. Grimes 	 */
257e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
258df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
259df8bae1dSRodney W. Grimes 	d->bd_next = bp->bif_dlist;
260df8bae1dSRodney W. Grimes 	bp->bif_dlist = d;
261df8bae1dSRodney W. Grimes 
26224a229f4SSam Leffler 	*bp->bif_driverp = bp;
263e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
264df8bae1dSRodney W. Grimes }
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes /*
267df8bae1dSRodney W. Grimes  * Detach a file from its interface.
268df8bae1dSRodney W. Grimes  */
269df8bae1dSRodney W. Grimes static void
270df8bae1dSRodney W. Grimes bpf_detachd(d)
271df8bae1dSRodney W. Grimes 	struct bpf_d *d;
272df8bae1dSRodney W. Grimes {
2736e891d64SPoul-Henning Kamp 	int error;
274df8bae1dSRodney W. Grimes 	struct bpf_d **p;
275df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
276df8bae1dSRodney W. Grimes 
277e0111e4dSSam Leffler 	/* XXX locking */
278df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
279572bde2aSRobert Watson 	d->bd_bif = NULL;
280df8bae1dSRodney W. Grimes 	/*
281df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
282df8bae1dSRodney W. Grimes 	 * If so, turn it off.
283df8bae1dSRodney W. Grimes 	 */
284df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
285df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
2866e891d64SPoul-Henning Kamp 		error = ifpromisc(bp->bif_ifp, 0);
2876e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
288df8bae1dSRodney W. Grimes 			/*
2896e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
290df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
291df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
292df8bae1dSRodney W. Grimes 			 * take it out.
293df8bae1dSRodney W. Grimes 			 */
2948eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
2958eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
2966e891d64SPoul-Henning Kamp 		}
297df8bae1dSRodney W. Grimes 	}
298df8bae1dSRodney W. Grimes 	/* Remove d from the interface's descriptor list. */
299e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
300df8bae1dSRodney W. Grimes 	p = &bp->bif_dlist;
301df8bae1dSRodney W. Grimes 	while (*p != d) {
302df8bae1dSRodney W. Grimes 		p = &(*p)->bd_next;
303572bde2aSRobert Watson 		if (*p == NULL)
304df8bae1dSRodney W. Grimes 			panic("bpf_detachd: descriptor not in list");
305df8bae1dSRodney W. Grimes 	}
306df8bae1dSRodney W. Grimes 	*p = (*p)->bd_next;
307572bde2aSRobert Watson 	if (bp->bif_dlist == NULL)
308df8bae1dSRodney W. Grimes 		/*
309df8bae1dSRodney W. Grimes 		 * Let the driver know that there are no more listeners.
310df8bae1dSRodney W. Grimes 		 */
311572bde2aSRobert Watson 		*bp->bif_driverp = NULL;
312e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
313df8bae1dSRodney W. Grimes }
314df8bae1dSRodney W. Grimes 
315df8bae1dSRodney W. Grimes /*
316df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
317df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
318df8bae1dSRodney W. Grimes  */
319df8bae1dSRodney W. Grimes /* ARGSUSED */
32087f6c662SJulian Elischer static	int
321b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
32289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
32360039670SBruce Evans 	int flags;
32460039670SBruce Evans 	int fmt;
325b40ce416SJulian Elischer 	struct thread *td;
326df8bae1dSRodney W. Grimes {
327e7bb21b3SJonathan Lemon 	struct bpf_d *d;
328df8bae1dSRodney W. Grimes 
329e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
330bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
331df8bae1dSRodney W. Grimes 	/*
332df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
333df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
334df8bae1dSRodney W. Grimes 	 */
335e7bb21b3SJonathan Lemon 	if (d) {
336e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
337df8bae1dSRodney W. Grimes 		return (EBUSY);
338e7bb21b3SJonathan Lemon 	}
339e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
340e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
341e7bb21b3SJonathan Lemon 
342d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
343b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
344b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
345a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
346bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
347df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
34800a83887SPaul Traina 	d->bd_sig = SIGIO;
3498ed3828cSRobert Watson 	d->bd_seesent = 1;
35082f4445dSRobert Watson #ifdef MAC
35182f4445dSRobert Watson 	mac_init_bpfdesc(d);
35282f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
35382f4445dSRobert Watson #endif
3546008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
35528b86052SRobert Watson 	callout_init(&d->bd_callout, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
356ad3b9257SJohn-Mark Gurney 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes 	return (0);
359df8bae1dSRodney W. Grimes }
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes /*
362df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
363df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
364df8bae1dSRodney W. Grimes  */
365df8bae1dSRodney W. Grimes /* ARGSUSED */
36687f6c662SJulian Elischer static	int
367b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
36889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
36960039670SBruce Evans 	int flags;
37060039670SBruce Evans 	int fmt;
371b40ce416SJulian Elischer 	struct thread *td;
372df8bae1dSRodney W. Grimes {
373e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
374df8bae1dSRodney W. Grimes 
37581bda851SJohn Polstra 	BPFD_LOCK(d);
37681bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
37781bda851SJohn Polstra 		callout_stop(&d->bd_callout);
37881bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
37981bda851SJohn Polstra 	BPFD_UNLOCK(d);
380e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
381e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
382df8bae1dSRodney W. Grimes 	if (d->bd_bif)
383df8bae1dSRodney W. Grimes 		bpf_detachd(d);
384e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
38582f4445dSRobert Watson #ifdef MAC
38682f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
38782f4445dSRobert Watson #endif /* MAC */
388ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
389df8bae1dSRodney W. Grimes 	bpf_freed(d);
390bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
391d722be54SLuigi Rizzo 	free(d, M_BPF);
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 	return (0);
394df8bae1dSRodney W. Grimes }
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes /*
398df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
399df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
400df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
401df8bae1dSRodney W. Grimes  */
402df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
403df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
404df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
405df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
406df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
407df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
408df8bae1dSRodney W. Grimes /*
409df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
410df8bae1dSRodney W. Grimes  */
41187f6c662SJulian Elischer static	int
41260039670SBruce Evans bpfread(dev, uio, ioflag)
41389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4148994a245SDag-Erling Smørgrav 	struct uio *uio;
41560039670SBruce Evans 	int ioflag;
416df8bae1dSRodney W. Grimes {
417e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
41881bda851SJohn Polstra 	int timed_out;
419df8bae1dSRodney W. Grimes 	int error;
420df8bae1dSRodney W. Grimes 
421df8bae1dSRodney W. Grimes 	/*
422df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
423df8bae1dSRodney W. Grimes 	 * as kernel buffers.
424df8bae1dSRodney W. Grimes 	 */
425df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
426df8bae1dSRodney W. Grimes 		return (EINVAL);
427df8bae1dSRodney W. Grimes 
428e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
42981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
43081bda851SJohn Polstra 		callout_stop(&d->bd_callout);
43181bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
43281bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
433df8bae1dSRodney W. Grimes 	/*
434df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
435df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
436df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
437df8bae1dSRodney W. Grimes 	 */
438572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
43981bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
440df8bae1dSRodney W. Grimes 			/*
441df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
442df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
443df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
444df8bae1dSRodney W. Grimes 			 */
445df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
446df8bae1dSRodney W. Grimes 			break;
447df8bae1dSRodney W. Grimes 		}
448de5d9935SRobert Watson 
449de5d9935SRobert Watson 		/*
450de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
451de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
452de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
453de5d9935SRobert Watson 		 * it before using it again.
454de5d9935SRobert Watson 		 */
455de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
456e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
457de5d9935SRobert Watson 			return (ENXIO);
458de5d9935SRobert Watson 		}
459de5d9935SRobert Watson 
460fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
461e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
462fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
463fba3cfdeSJohn Polstra 		}
464521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
465e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
466df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
467e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
468df8bae1dSRodney W. Grimes 			return (error);
469df8bae1dSRodney W. Grimes 		}
470df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
471df8bae1dSRodney W. Grimes 			/*
472df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
473df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
474df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
475df8bae1dSRodney W. Grimes 			 */
476df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
477df8bae1dSRodney W. Grimes 				/*
478df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
479df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
480df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
481df8bae1dSRodney W. Grimes 				 */
482df8bae1dSRodney W. Grimes 				break;
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
485e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
486df8bae1dSRodney W. Grimes 				return (0);
487df8bae1dSRodney W. Grimes 			}
488df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
489df8bae1dSRodney W. Grimes 			break;
490df8bae1dSRodney W. Grimes 		}
491df8bae1dSRodney W. Grimes 	}
492df8bae1dSRodney W. Grimes 	/*
493df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
494df8bae1dSRodney W. Grimes 	 */
495e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
496df8bae1dSRodney W. Grimes 
497df8bae1dSRodney W. Grimes 	/*
498df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
499df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
500df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
501df8bae1dSRodney W. Grimes 	 */
502e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
503df8bae1dSRodney W. Grimes 
504e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
505df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
506572bde2aSRobert Watson 	d->bd_hbuf = NULL;
507df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
508e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 	return (error);
511df8bae1dSRodney W. Grimes }
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes /*
515df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
516df8bae1dSRodney W. Grimes  */
517e7bb21b3SJonathan Lemon static __inline void
518df8bae1dSRodney W. Grimes bpf_wakeup(d)
5198994a245SDag-Erling Smørgrav 	struct bpf_d *d;
520df8bae1dSRodney W. Grimes {
52181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
52281bda851SJohn Polstra 		callout_stop(&d->bd_callout);
52381bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
52481bda851SJohn Polstra 	}
525521f364bSDag-Erling Smørgrav 	wakeup(d);
526831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
527f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
52800a83887SPaul Traina 
529512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
530ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
531df8bae1dSRodney W. Grimes }
532df8bae1dSRodney W. Grimes 
53381bda851SJohn Polstra static void
53481bda851SJohn Polstra bpf_timed_out(arg)
53581bda851SJohn Polstra 	void *arg;
53681bda851SJohn Polstra {
53781bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
53881bda851SJohn Polstra 
53981bda851SJohn Polstra 	BPFD_LOCK(d);
54081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
54181bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
54281bda851SJohn Polstra 		if (d->bd_slen != 0)
54381bda851SJohn Polstra 			bpf_wakeup(d);
54481bda851SJohn Polstra 	}
54581bda851SJohn Polstra 	BPFD_UNLOCK(d);
54681bda851SJohn Polstra }
54781bda851SJohn Polstra 
54887f6c662SJulian Elischer static	int
54960039670SBruce Evans bpfwrite(dev, uio, ioflag)
55089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
551df8bae1dSRodney W. Grimes 	struct uio *uio;
55260039670SBruce Evans 	int ioflag;
553df8bae1dSRodney W. Grimes {
554e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
555df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
556df8bae1dSRodney W. Grimes 	struct mbuf *m;
557e7bb21b3SJonathan Lemon 	int error;
5588240bf1eSRobert Watson 	struct sockaddr dst;
559df8bae1dSRodney W. Grimes 	int datlen;
560df8bae1dSRodney W. Grimes 
561572bde2aSRobert Watson 	if (d->bd_bif == NULL)
562df8bae1dSRodney W. Grimes 		return (ENXIO);
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
567df8bae1dSRodney W. Grimes 		return (0);
568df8bae1dSRodney W. Grimes 
5698240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
570df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
571df8bae1dSRodney W. Grimes 	if (error)
572df8bae1dSRodney W. Grimes 		return (error);
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
575df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
576df8bae1dSRodney W. Grimes 
577114ae644SMike Smith 	if (d->bd_hdrcmplt)
578114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
579114ae644SMike Smith 
58082f4445dSRobert Watson #ifdef MAC
581f747d2ddSRobert Watson 	BPFD_LOCK(d);
58282f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
583f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
58482f4445dSRobert Watson #endif
585b8f9429dSRobert Watson 	NET_LOCK_GIANT();
586572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
587b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
588df8bae1dSRodney W. Grimes 	/*
589df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
590df8bae1dSRodney W. Grimes 	 */
591df8bae1dSRodney W. Grimes 	return (error);
592df8bae1dSRodney W. Grimes }
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes /*
595df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
596e7bb21b3SJonathan Lemon  * receive and drop counts.
597df8bae1dSRodney W. Grimes  */
598df8bae1dSRodney W. Grimes static void
599df8bae1dSRodney W. Grimes reset_d(d)
600df8bae1dSRodney W. Grimes 	struct bpf_d *d;
601df8bae1dSRodney W. Grimes {
602e7bb21b3SJonathan Lemon 
603e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
604df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
605df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
606df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
607572bde2aSRobert Watson 		d->bd_hbuf = NULL;
608df8bae1dSRodney W. Grimes 	}
609df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
610df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
611df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
612df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
613df8bae1dSRodney W. Grimes }
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes /*
616df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
617df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
618df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
619df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
620df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
621df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
622df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
623df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
624df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
625df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
626df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
627df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
628df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
629df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
630114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
631114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6328ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6338ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
634df8bae1dSRodney W. Grimes  */
635df8bae1dSRodney W. Grimes /* ARGSUSED */
63687f6c662SJulian Elischer static	int
637b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
63889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
639ecbb00a2SDoug Rabson 	u_long cmd;
640df8bae1dSRodney W. Grimes 	caddr_t addr;
64160039670SBruce Evans 	int flags;
642b40ce416SJulian Elischer 	struct thread *td;
643df8bae1dSRodney W. Grimes {
644e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
645e7bb21b3SJonathan Lemon 	int error = 0;
646df8bae1dSRodney W. Grimes 
64781bda851SJohn Polstra 	BPFD_LOCK(d);
64881bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
64981bda851SJohn Polstra 		callout_stop(&d->bd_callout);
65081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
65181bda851SJohn Polstra 	BPFD_UNLOCK(d);
65281bda851SJohn Polstra 
653df8bae1dSRodney W. Grimes 	switch (cmd) {
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes 	default:
656df8bae1dSRodney W. Grimes 		error = EINVAL;
657df8bae1dSRodney W. Grimes 		break;
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes 	/*
660df8bae1dSRodney W. Grimes 	 * Check for read packet available.
661df8bae1dSRodney W. Grimes 	 */
662df8bae1dSRodney W. Grimes 	case FIONREAD:
663df8bae1dSRodney W. Grimes 		{
664df8bae1dSRodney W. Grimes 			int n;
665df8bae1dSRodney W. Grimes 
666e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
667df8bae1dSRodney W. Grimes 			n = d->bd_slen;
668df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
669df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
670e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes 			*(int *)addr = n;
673df8bae1dSRodney W. Grimes 			break;
674df8bae1dSRodney W. Grimes 		}
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
677df8bae1dSRodney W. Grimes 		{
678df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
679df8bae1dSRodney W. Grimes 
680572bde2aSRobert Watson 			if (d->bd_bif == NULL)
681df8bae1dSRodney W. Grimes 				error = EINVAL;
682df8bae1dSRodney W. Grimes 			else {
683df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
684df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
685df8bae1dSRodney W. Grimes 			}
686df8bae1dSRodney W. Grimes 			break;
687df8bae1dSRodney W. Grimes 		}
688df8bae1dSRodney W. Grimes 
689df8bae1dSRodney W. Grimes 	/*
690df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
691df8bae1dSRodney W. Grimes 	 */
692df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
693df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
694df8bae1dSRodney W. Grimes 		break;
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 	/*
697df8bae1dSRodney W. Grimes 	 * Set buffer length.
698df8bae1dSRodney W. Grimes 	 */
699df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
700572bde2aSRobert Watson 		if (d->bd_bif != NULL)
701df8bae1dSRodney W. Grimes 			error = EINVAL;
702df8bae1dSRodney W. Grimes 		else {
7038994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
704df8bae1dSRodney W. Grimes 
705eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
706eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
707df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
708df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
709df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
710df8bae1dSRodney W. Grimes 		}
711df8bae1dSRodney W. Grimes 		break;
712df8bae1dSRodney W. Grimes 
713df8bae1dSRodney W. Grimes 	/*
714df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
715df8bae1dSRodney W. Grimes 	 */
716df8bae1dSRodney W. Grimes 	case BIOCSETF:
717df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
718df8bae1dSRodney W. Grimes 		break;
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 	/*
721df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
722df8bae1dSRodney W. Grimes 	 */
723df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
724e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
725df8bae1dSRodney W. Grimes 		reset_d(d);
726e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
727df8bae1dSRodney W. Grimes 		break;
728df8bae1dSRodney W. Grimes 
729df8bae1dSRodney W. Grimes 	/*
730df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
731df8bae1dSRodney W. Grimes 	 */
732df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
733572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
734df8bae1dSRodney W. Grimes 			/*
735df8bae1dSRodney W. Grimes 			 * No interface attached yet.
736df8bae1dSRodney W. Grimes 			 */
737df8bae1dSRodney W. Grimes 			error = EINVAL;
738df8bae1dSRodney W. Grimes 			break;
739df8bae1dSRodney W. Grimes 		}
740df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
741e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
742df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
743e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
744df8bae1dSRodney W. Grimes 			if (error == 0)
745df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
746df8bae1dSRodney W. Grimes 		}
747df8bae1dSRodney W. Grimes 		break;
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	/*
7508eab61f3SSam Leffler 	 * Get current data link type.
751df8bae1dSRodney W. Grimes 	 */
752df8bae1dSRodney W. Grimes 	case BIOCGDLT:
753572bde2aSRobert Watson 		if (d->bd_bif == NULL)
754df8bae1dSRodney W. Grimes 			error = EINVAL;
755df8bae1dSRodney W. Grimes 		else
756df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
757df8bae1dSRodney W. Grimes 		break;
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes 	/*
7608eab61f3SSam Leffler 	 * Get a list of supported data link types.
7618eab61f3SSam Leffler 	 */
7628eab61f3SSam Leffler 	case BIOCGDLTLIST:
763572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7648eab61f3SSam Leffler 			error = EINVAL;
7658eab61f3SSam Leffler 		else
7668eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7678eab61f3SSam Leffler 		break;
7688eab61f3SSam Leffler 
7698eab61f3SSam Leffler 	/*
7708eab61f3SSam Leffler 	 * Set data link type.
7718eab61f3SSam Leffler 	 */
7728eab61f3SSam Leffler 	case BIOCSDLT:
773572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7748eab61f3SSam Leffler 			error = EINVAL;
7758eab61f3SSam Leffler 		else
7768eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7778eab61f3SSam Leffler 		break;
7788eab61f3SSam Leffler 
7798eab61f3SSam Leffler 	/*
7801dd0feaaSArchie Cobbs 	 * Get interface name.
781df8bae1dSRodney W. Grimes 	 */
782df8bae1dSRodney W. Grimes 	case BIOCGETIF:
783572bde2aSRobert Watson 		if (d->bd_bif == NULL)
784df8bae1dSRodney W. Grimes 			error = EINVAL;
7851dd0feaaSArchie Cobbs 		else {
7861dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7871dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7881dd0feaaSArchie Cobbs 
7899bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
7909bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
7911dd0feaaSArchie Cobbs 		}
792df8bae1dSRodney W. Grimes 		break;
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 	/*
795df8bae1dSRodney W. Grimes 	 * Set interface.
796df8bae1dSRodney W. Grimes 	 */
797df8bae1dSRodney W. Grimes 	case BIOCSETIF:
798df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
799df8bae1dSRodney W. Grimes 		break;
800df8bae1dSRodney W. Grimes 
801df8bae1dSRodney W. Grimes 	/*
802df8bae1dSRodney W. Grimes 	 * Set read timeout.
803df8bae1dSRodney W. Grimes 	 */
804df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
805df8bae1dSRodney W. Grimes 		{
806df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
807df8bae1dSRodney W. Grimes 
808bdc2cdc5SAlexander Langer 			/*
809bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
810bdc2cdc5SAlexander Langer 			 * a one-shot timer.
811bdc2cdc5SAlexander Langer 			 */
812bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
813bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
814df8bae1dSRodney W. Grimes 			break;
815df8bae1dSRodney W. Grimes 		}
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes 	/*
818df8bae1dSRodney W. Grimes 	 * Get read timeout.
819df8bae1dSRodney W. Grimes 	 */
820df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
821df8bae1dSRodney W. Grimes 		{
822df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
823df8bae1dSRodney W. Grimes 
824bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
825bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
826df8bae1dSRodney W. Grimes 			break;
827df8bae1dSRodney W. Grimes 		}
828df8bae1dSRodney W. Grimes 
829df8bae1dSRodney W. Grimes 	/*
830df8bae1dSRodney W. Grimes 	 * Get packet stats.
831df8bae1dSRodney W. Grimes 	 */
832df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
833df8bae1dSRodney W. Grimes 		{
834df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
837df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
838df8bae1dSRodney W. Grimes 			break;
839df8bae1dSRodney W. Grimes 		}
840df8bae1dSRodney W. Grimes 
841df8bae1dSRodney W. Grimes 	/*
842df8bae1dSRodney W. Grimes 	 * Set immediate mode.
843df8bae1dSRodney W. Grimes 	 */
844df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
845df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
846df8bae1dSRodney W. Grimes 		break;
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	case BIOCVERSION:
849df8bae1dSRodney W. Grimes 		{
850df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
851df8bae1dSRodney W. Grimes 
852df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
853df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
854df8bae1dSRodney W. Grimes 			break;
855df8bae1dSRodney W. Grimes 		}
85600a83887SPaul Traina 
857114ae644SMike Smith 	/*
858114ae644SMike Smith 	 * Get "header already complete" flag
859114ae644SMike Smith 	 */
860114ae644SMike Smith 	case BIOCGHDRCMPLT:
861114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
862114ae644SMike Smith 		break;
863114ae644SMike Smith 
864114ae644SMike Smith 	/*
865114ae644SMike Smith 	 * Set "header already complete" flag
866114ae644SMike Smith 	 */
867114ae644SMike Smith 	case BIOCSHDRCMPLT:
868114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
869114ae644SMike Smith 		break;
870114ae644SMike Smith 
8718ed3828cSRobert Watson 	/*
8728ed3828cSRobert Watson 	 * Get "see sent packets" flag
8738ed3828cSRobert Watson 	 */
8748ed3828cSRobert Watson 	case BIOCGSEESENT:
8758ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8768ed3828cSRobert Watson 		break;
8778ed3828cSRobert Watson 
8788ed3828cSRobert Watson 	/*
8798ed3828cSRobert Watson 	 * Set "see sent packets" flag
8808ed3828cSRobert Watson 	 */
8818ed3828cSRobert Watson 	case BIOCSSEESENT:
8828ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8838ed3828cSRobert Watson 		break;
8848ed3828cSRobert Watson 
88500a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
88600a83887SPaul Traina 		break;
88700a83887SPaul Traina 
88800a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
88900a83887SPaul Traina 		d->bd_async = *(int *)addr;
89000a83887SPaul Traina 		break;
89100a83887SPaul Traina 
892831d27a9SDon Lewis 	case FIOSETOWN:
893831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
89400a83887SPaul Traina 		break;
89500a83887SPaul Traina 
896831d27a9SDon Lewis 	case FIOGETOWN:
89791e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
898831d27a9SDon Lewis 		break;
899831d27a9SDon Lewis 
900831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
901831d27a9SDon Lewis 	case TIOCSPGRP:
902831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
903831d27a9SDon Lewis 		break;
904831d27a9SDon Lewis 
905831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
90600a83887SPaul Traina 	case TIOCGPGRP:
90791e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
90800a83887SPaul Traina 		break;
90900a83887SPaul Traina 
91000a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
91100a83887SPaul Traina 		{
91200a83887SPaul Traina 			u_int sig;
91300a83887SPaul Traina 
91400a83887SPaul Traina 			sig = *(u_int *)addr;
91500a83887SPaul Traina 
91600a83887SPaul Traina 			if (sig >= NSIG)
91700a83887SPaul Traina 				error = EINVAL;
91800a83887SPaul Traina 			else
91900a83887SPaul Traina 				d->bd_sig = sig;
92000a83887SPaul Traina 			break;
92100a83887SPaul Traina 		}
92200a83887SPaul Traina 	case BIOCGRSIG:
92300a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
92400a83887SPaul Traina 		break;
925df8bae1dSRodney W. Grimes 	}
926df8bae1dSRodney W. Grimes 	return (error);
927df8bae1dSRodney W. Grimes }
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes /*
930df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
931df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
932df8bae1dSRodney W. Grimes  */
933f708ef1bSPoul-Henning Kamp static int
934df8bae1dSRodney W. Grimes bpf_setf(d, fp)
935df8bae1dSRodney W. Grimes 	struct bpf_d *d;
936df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
937df8bae1dSRodney W. Grimes {
938df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
939df8bae1dSRodney W. Grimes 	u_int flen, size;
940df8bae1dSRodney W. Grimes 
941df8bae1dSRodney W. Grimes 	old = d->bd_filter;
942572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
943df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
944df8bae1dSRodney W. Grimes 			return (EINVAL);
945e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
946572bde2aSRobert Watson 		d->bd_filter = NULL;
947df8bae1dSRodney W. Grimes 		reset_d(d);
948e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
949572bde2aSRobert Watson 		if (old != NULL)
950bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
951df8bae1dSRodney W. Grimes 		return (0);
952df8bae1dSRodney W. Grimes 	}
953df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
954df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
955df8bae1dSRodney W. Grimes 		return (EINVAL);
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
958a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
959df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
960df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
961e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
962df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
963df8bae1dSRodney W. Grimes 		reset_d(d);
964e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
965572bde2aSRobert Watson 		if (old != NULL)
966bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
967df8bae1dSRodney W. Grimes 
968df8bae1dSRodney W. Grimes 		return (0);
969df8bae1dSRodney W. Grimes 	}
970bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
971df8bae1dSRodney W. Grimes 	return (EINVAL);
972df8bae1dSRodney W. Grimes }
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes /*
975df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
976df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
977df8bae1dSRodney W. Grimes  * Return an errno or 0.
978df8bae1dSRodney W. Grimes  */
979df8bae1dSRodney W. Grimes static int
980df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
981df8bae1dSRodney W. Grimes 	struct bpf_d *d;
982df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
983df8bae1dSRodney W. Grimes {
984df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
985e7bb21b3SJonathan Lemon 	int error;
9869b44ff22SGarrett Wollman 	struct ifnet *theywant;
987df8bae1dSRodney W. Grimes 
9889b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
989572bde2aSRobert Watson 	if (theywant == NULL)
9909b44ff22SGarrett Wollman 		return ENXIO;
9919b44ff22SGarrett Wollman 
992df8bae1dSRodney W. Grimes 	/*
993df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
994df8bae1dSRodney W. Grimes 	 */
995e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
996572bde2aSRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
997df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
998df8bae1dSRodney W. Grimes 
999572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
1000df8bae1dSRodney W. Grimes 			continue;
100124a229f4SSam Leffler 		/* skip additional entry */
100224a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
100324a229f4SSam Leffler 			continue;
1004e7bb21b3SJonathan Lemon 
1005e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1006df8bae1dSRodney W. Grimes 		/*
1007df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1008df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
1009df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1010df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1011df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1012df8bae1dSRodney W. Grimes 		 */
1013df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
1014df8bae1dSRodney W. Grimes 			return (ENETDOWN);
1015df8bae1dSRodney W. Grimes 
1016572bde2aSRobert Watson 		if (d->bd_sbuf == NULL) {
1017df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1018df8bae1dSRodney W. Grimes 			if (error != 0)
1019df8bae1dSRodney W. Grimes 				return (error);
1020df8bae1dSRodney W. Grimes 		}
1021df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1022df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1023df8bae1dSRodney W. Grimes 				/*
1024df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1025df8bae1dSRodney W. Grimes 				 */
1026df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1029df8bae1dSRodney W. Grimes 		}
1030e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1031df8bae1dSRodney W. Grimes 		reset_d(d);
1032e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1033df8bae1dSRodney W. Grimes 		return (0);
1034df8bae1dSRodney W. Grimes 	}
1035e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1036df8bae1dSRodney W. Grimes 	/* Not found. */
1037df8bae1dSRodney W. Grimes 	return (ENXIO);
1038df8bae1dSRodney W. Grimes }
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes /*
1041243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1042df8bae1dSRodney W. Grimes  *
1043df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1044df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1045df8bae1dSRodney W. Grimes  */
104637c84183SPoul-Henning Kamp static int
1047b40ce416SJulian Elischer bpfpoll(dev, events, td)
104889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1049243ac7d8SPeter Wemm 	int events;
1050b40ce416SJulian Elischer 	struct thread *td;
1051df8bae1dSRodney W. Grimes {
1052e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10530832fc64SGarance A Drosehn 	int revents;
1054df8bae1dSRodney W. Grimes 
1055bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1056de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1057de5d9935SRobert Watson 		return (ENXIO);
1058de5d9935SRobert Watson 
10590832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1060e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
106175c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
106295aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1063243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
106481bda851SJohn Polstra 		else {
1065ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
106681bda851SJohn Polstra 			/* Start the read timeout if necessary. */
106781bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
106881bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
106981bda851SJohn Polstra 				    bpf_timed_out, d);
107081bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
107181bda851SJohn Polstra 			}
107281bda851SJohn Polstra 		}
107375c13541SPoul-Henning Kamp 	}
1074e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1075243ac7d8SPeter Wemm 	return (revents);
1076df8bae1dSRodney W. Grimes }
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes /*
107995aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
108095aab9ccSJohn-Mark Gurney  * reject all others.
108195aab9ccSJohn-Mark Gurney  */
108295aab9ccSJohn-Mark Gurney int
108395aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
108489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
108595aab9ccSJohn-Mark Gurney 	struct knote *kn;
108695aab9ccSJohn-Mark Gurney {
108795aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
108895aab9ccSJohn-Mark Gurney 
108995aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
109095aab9ccSJohn-Mark Gurney 		return (1);
109195aab9ccSJohn-Mark Gurney 
109295aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
109395aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
1094ad3b9257SJohn-Mark Gurney 	knlist_add(&d->bd_sel.si_note, kn, 0);
109595aab9ccSJohn-Mark Gurney 
109695aab9ccSJohn-Mark Gurney 	return (0);
109795aab9ccSJohn-Mark Gurney }
109895aab9ccSJohn-Mark Gurney 
109995aab9ccSJohn-Mark Gurney static void
110095aab9ccSJohn-Mark Gurney filt_bpfdetach(kn)
110195aab9ccSJohn-Mark Gurney 	struct knote *kn;
110295aab9ccSJohn-Mark Gurney {
110395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
110495aab9ccSJohn-Mark Gurney 
1105ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
110695aab9ccSJohn-Mark Gurney }
110795aab9ccSJohn-Mark Gurney 
110895aab9ccSJohn-Mark Gurney static int
110995aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
111095aab9ccSJohn-Mark Gurney 	struct knote *kn;
111195aab9ccSJohn-Mark Gurney 	long hint;
111295aab9ccSJohn-Mark Gurney {
111395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
111495aab9ccSJohn-Mark Gurney 	int ready;
111595aab9ccSJohn-Mark Gurney 
111695aab9ccSJohn-Mark Gurney 	BPFD_LOCK(d);
111795aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
111895aab9ccSJohn-Mark Gurney 	if (ready) {
111995aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
112095aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
112195aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
112295aab9ccSJohn-Mark Gurney 	}
112395aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
112495aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
112595aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
112695aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
112795aab9ccSJohn-Mark Gurney 	}
112895aab9ccSJohn-Mark Gurney 	BPFD_UNLOCK(d);
112995aab9ccSJohn-Mark Gurney 
113095aab9ccSJohn-Mark Gurney 	return (ready);
113195aab9ccSJohn-Mark Gurney }
113295aab9ccSJohn-Mark Gurney 
113395aab9ccSJohn-Mark Gurney /*
1134df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1135df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1136df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1137df8bae1dSRodney W. Grimes  * buffer.
1138df8bae1dSRodney W. Grimes  */
1139df8bae1dSRodney W. Grimes void
114024a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
114124a229f4SSam Leffler 	struct bpf_if *bp;
11428994a245SDag-Erling Smørgrav 	u_char *pkt;
11438994a245SDag-Erling Smørgrav 	u_int pktlen;
1144df8bae1dSRodney W. Grimes {
11458994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11468994a245SDag-Erling Smørgrav 	u_int slen;
1147e7bb21b3SJonathan Lemon 
114846691dd8SRobert Watson 	/*
114946691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
115046691dd8SRobert Watson 	 * no descriptors attached.
115146691dd8SRobert Watson 	 */
115246691dd8SRobert Watson 	if (bp->bif_dlist == NULL)
115346691dd8SRobert Watson 		return;
115446691dd8SRobert Watson 
1155e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1156572bde2aSRobert Watson 	for (d = bp->bif_dlist; d != NULL; d = d->bd_next) {
1157e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1158df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1159df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1160ec272d87SRobert Watson 		if (slen != 0) {
1161ec272d87SRobert Watson #ifdef MAC
116224a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1163ec272d87SRobert Watson #endif
1164df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1165ec272d87SRobert Watson 		}
1166e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1167df8bae1dSRodney W. Grimes 	}
1168e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1169df8bae1dSRodney W. Grimes }
1170df8bae1dSRodney W. Grimes 
1171df8bae1dSRodney W. Grimes /*
1172df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1173df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1174df8bae1dSRodney W. Grimes  */
1175df8bae1dSRodney W. Grimes static void
1176df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1177df8bae1dSRodney W. Grimes 	const void *src_arg;
1178df8bae1dSRodney W. Grimes 	void *dst_arg;
11798994a245SDag-Erling Smørgrav 	size_t len;
1180df8bae1dSRodney W. Grimes {
11818994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11828994a245SDag-Erling Smørgrav 	u_int count;
1183df8bae1dSRodney W. Grimes 	u_char *dst;
1184df8bae1dSRodney W. Grimes 
1185df8bae1dSRodney W. Grimes 	m = src_arg;
1186df8bae1dSRodney W. Grimes 	dst = dst_arg;
1187df8bae1dSRodney W. Grimes 	while (len > 0) {
1188572bde2aSRobert Watson 		if (m == NULL)
1189df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1190df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11910453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1192df8bae1dSRodney W. Grimes 		m = m->m_next;
1193df8bae1dSRodney W. Grimes 		dst += count;
1194df8bae1dSRodney W. Grimes 		len -= count;
1195df8bae1dSRodney W. Grimes 	}
1196df8bae1dSRodney W. Grimes }
1197df8bae1dSRodney W. Grimes 
1198df8bae1dSRodney W. Grimes /*
1199df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1200df8bae1dSRodney W. Grimes  */
1201df8bae1dSRodney W. Grimes void
120224a229f4SSam Leffler bpf_mtap(bp, m)
120324a229f4SSam Leffler 	struct bpf_if *bp;
1204df8bae1dSRodney W. Grimes 	struct mbuf *m;
1205df8bae1dSRodney W. Grimes {
1206df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1207df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1208df8bae1dSRodney W. Grimes 
120946691dd8SRobert Watson 	/*
121046691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
121146691dd8SRobert Watson 	 * no descriptors attached.
121246691dd8SRobert Watson 	 */
121346691dd8SRobert Watson 	if (bp->bif_dlist == NULL)
121446691dd8SRobert Watson 		return;
121546691dd8SRobert Watson 
1216f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
12177b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
121824a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
12197b831242SPoul-Henning Kamp 		return;
12207b831242SPoul-Henning Kamp 	}
1221df8bae1dSRodney W. Grimes 
1222e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1223572bde2aSRobert Watson 	for (d = bp->bif_dlist; d != NULL; d = d->bd_next) {
12248ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
12258ed3828cSRobert Watson 			continue;
1226e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1227df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1228df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1229df8bae1dSRodney W. Grimes 		if (slen != 0)
12300c7fb534SRobert Watson #ifdef MAC
123124a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
12320c7fb534SRobert Watson #endif
12330c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
12340c7fb534SRobert Watson 				    bpf_mcopy);
1235e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1236df8bae1dSRodney W. Grimes 	}
1237e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1238df8bae1dSRodney W. Grimes }
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes /*
1241437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1242437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1243437ffe18SSam Leffler  */
1244437ffe18SSam Leffler void
1245437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1246437ffe18SSam Leffler 	struct bpf_if *bp;
1247437ffe18SSam Leffler 	void *data;
1248437ffe18SSam Leffler 	u_int dlen;
1249437ffe18SSam Leffler 	struct mbuf *m;
1250437ffe18SSam Leffler {
1251437ffe18SSam Leffler 	struct mbuf mb;
1252437ffe18SSam Leffler 	struct bpf_d *d;
1253437ffe18SSam Leffler 	u_int pktlen, slen;
1254437ffe18SSam Leffler 
125546691dd8SRobert Watson 	/*
125646691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
125746691dd8SRobert Watson 	 * no descriptors attached.
125846691dd8SRobert Watson 	 */
125946691dd8SRobert Watson 	if (bp->bif_dlist == NULL)
126046691dd8SRobert Watson 		return;
126146691dd8SRobert Watson 
1262437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1263437ffe18SSam Leffler 	/*
1264437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1265437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1266437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1267437ffe18SSam Leffler 	 */
1268437ffe18SSam Leffler 	mb.m_next = m;
1269437ffe18SSam Leffler 	mb.m_data = data;
1270437ffe18SSam Leffler 	mb.m_len = dlen;
1271437ffe18SSam Leffler 	pktlen += dlen;
1272437ffe18SSam Leffler 
1273437ffe18SSam Leffler 	BPFIF_LOCK(bp);
1274572bde2aSRobert Watson 	for (d = bp->bif_dlist; d != NULL; d = d->bd_next) {
1275437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1276437ffe18SSam Leffler 			continue;
1277437ffe18SSam Leffler 		BPFD_LOCK(d);
1278437ffe18SSam Leffler 		++d->bd_rcount;
1279437ffe18SSam Leffler 		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1280437ffe18SSam Leffler 		if (slen != 0)
1281437ffe18SSam Leffler #ifdef MAC
1282437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1283437ffe18SSam Leffler #endif
1284437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1285437ffe18SSam Leffler 				    bpf_mcopy);
1286437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1287437ffe18SSam Leffler 	}
1288437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1289437ffe18SSam Leffler }
1290437ffe18SSam Leffler 
1291437ffe18SSam Leffler /*
1292df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
12939e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1294df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1295df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1296df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1297df8bae1dSRodney W. Grimes  */
1298df8bae1dSRodney W. Grimes static void
1299df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
13008994a245SDag-Erling Smørgrav 	struct bpf_d *d;
13018994a245SDag-Erling Smørgrav 	u_char *pkt;
13028994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
13038994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1304df8bae1dSRodney W. Grimes {
13058994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
13068994a245SDag-Erling Smørgrav 	int totlen, curlen;
13078994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
13089e610888SDag-Erling Smørgrav 
1309df8bae1dSRodney W. Grimes 	/*
1310df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1311df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1312df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1313df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1314df8bae1dSRodney W. Grimes 	 */
1315df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1316df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1317df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1318df8bae1dSRodney W. Grimes 
1319df8bae1dSRodney W. Grimes 	/*
1320df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1321df8bae1dSRodney W. Grimes 	 */
1322df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1323df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1324df8bae1dSRodney W. Grimes 		/*
1325df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1326df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1327df8bae1dSRodney W. Grimes 		 * pending reads.
1328df8bae1dSRodney W. Grimes 		 */
1329572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1330df8bae1dSRodney W. Grimes 			/*
1331df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1332df8bae1dSRodney W. Grimes 			 * so drop the packet.
1333df8bae1dSRodney W. Grimes 			 */
1334df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1335df8bae1dSRodney W. Grimes 			return;
1336df8bae1dSRodney W. Grimes 		}
1337df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1338df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1339df8bae1dSRodney W. Grimes 		curlen = 0;
1340df8bae1dSRodney W. Grimes 	}
134181bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1342df8bae1dSRodney W. Grimes 		/*
134381bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
134481bda851SJohn Polstra 		 * already expired during a select call.  A packet
134581bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1346df8bae1dSRodney W. Grimes 		 */
1347df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1348df8bae1dSRodney W. Grimes 
1349df8bae1dSRodney W. Grimes 	/*
1350df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1351df8bae1dSRodney W. Grimes 	 */
1352df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1353df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1354df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1355df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1356df8bae1dSRodney W. Grimes 	/*
1357df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1358df8bae1dSRodney W. Grimes 	 */
1359df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1360df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1361df8bae1dSRodney W. Grimes }
1362df8bae1dSRodney W. Grimes 
1363df8bae1dSRodney W. Grimes /*
1364df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1365df8bae1dSRodney W. Grimes  */
1366df8bae1dSRodney W. Grimes static int
1367df8bae1dSRodney W. Grimes bpf_allocbufs(d)
13688994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1369df8bae1dSRodney W. Grimes {
1370a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1371572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1372df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1373df8bae1dSRodney W. Grimes 
1374a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1375572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1376bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1377df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1378df8bae1dSRodney W. Grimes 	}
1379df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1380df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1381df8bae1dSRodney W. Grimes 	return (0);
1382df8bae1dSRodney W. Grimes }
1383df8bae1dSRodney W. Grimes 
1384df8bae1dSRodney W. Grimes /*
1385df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1386df8bae1dSRodney W. Grimes  * Called on close.
1387df8bae1dSRodney W. Grimes  */
1388df8bae1dSRodney W. Grimes static void
1389df8bae1dSRodney W. Grimes bpf_freed(d)
13908994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1391df8bae1dSRodney W. Grimes {
1392df8bae1dSRodney W. Grimes 	/*
1393df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1394df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1395df8bae1dSRodney W. Grimes 	 * free.
1396df8bae1dSRodney W. Grimes 	 */
1397572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1398bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1399572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1400bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1401572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1402bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1403df8bae1dSRodney W. Grimes 	}
1404df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1405bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1406e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1407df8bae1dSRodney W. Grimes }
1408df8bae1dSRodney W. Grimes 
1409df8bae1dSRodney W. Grimes /*
141024a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
141124a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1412df8bae1dSRodney W. Grimes  */
1413df8bae1dSRodney W. Grimes void
14149b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1415df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1416df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1417df8bae1dSRodney W. Grimes {
141824a229f4SSam Leffler 
141924a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
142024a229f4SSam Leffler }
142124a229f4SSam Leffler 
142224a229f4SSam Leffler /*
142324a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
142424a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
142524a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
142624a229f4SSam Leffler  * headers are not yet supporrted).
142724a229f4SSam Leffler  */
142824a229f4SSam Leffler void
142924a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
143024a229f4SSam Leffler 	struct ifnet *ifp;
143124a229f4SSam Leffler 	u_int dlt, hdrlen;
143224a229f4SSam Leffler 	struct bpf_if **driverp;
143324a229f4SSam Leffler {
1434df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14356a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1436572bde2aSRobert Watson 	if (bp == NULL)
1437df8bae1dSRodney W. Grimes 		panic("bpfattach");
1438df8bae1dSRodney W. Grimes 
1439572bde2aSRobert Watson 	bp->bif_dlist = NULL;
144024a229f4SSam Leffler 	bp->bif_driverp = driverp;
1441df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1442df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
14436008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1444df8bae1dSRodney W. Grimes 
1445e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1446df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1447df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1448e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1449df8bae1dSRodney W. Grimes 
1450572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1451df8bae1dSRodney W. Grimes 
1452df8bae1dSRodney W. Grimes 	/*
1453df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1454df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1455df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1456df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1457df8bae1dSRodney W. Grimes 	 */
1458df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1459df8bae1dSRodney W. Grimes 
14602eeab939SGarrett Wollman 	if (bootverbose)
146124a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1462df8bae1dSRodney W. Grimes }
146353ac6efbSJulian Elischer 
1464de5d9935SRobert Watson /*
1465de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1466de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1467de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1468de5d9935SRobert Watson  * ENXIO.
1469de5d9935SRobert Watson  */
1470de5d9935SRobert Watson void
1471de5d9935SRobert Watson bpfdetach(ifp)
1472de5d9935SRobert Watson 	struct ifnet *ifp;
1473de5d9935SRobert Watson {
1474de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1475de5d9935SRobert Watson 	struct bpf_d	*d;
1476de5d9935SRobert Watson 
1477de5d9935SRobert Watson 	/* Locate BPF interface information */
1478de5d9935SRobert Watson 	bp_prev = NULL;
14798eab61f3SSam Leffler 
14808eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
1481de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1482de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1483de5d9935SRobert Watson 			break;
1484de5d9935SRobert Watson 		bp_prev = bp;
1485de5d9935SRobert Watson 	}
1486de5d9935SRobert Watson 
1487de5d9935SRobert Watson 	/* Interface wasn't attached */
1488d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1489e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
14909bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1491de5d9935SRobert Watson 		return;
1492de5d9935SRobert Watson 	}
1493de5d9935SRobert Watson 
1494de5d9935SRobert Watson 	if (bp_prev) {
1495de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1496de5d9935SRobert Watson 	} else {
1497de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1498de5d9935SRobert Watson 	}
14998eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1500de5d9935SRobert Watson 
1501e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1502e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1503e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1504e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1505e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1506e7bb21b3SJonathan Lemon 	}
1507e7bb21b3SJonathan Lemon 
1508e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1509de5d9935SRobert Watson 	free(bp, M_BPF);
15108eab61f3SSam Leffler }
1511de5d9935SRobert Watson 
15128eab61f3SSam Leffler /*
15138eab61f3SSam Leffler  * Get a list of available data link type of the interface.
15148eab61f3SSam Leffler  */
15158eab61f3SSam Leffler static int
15168eab61f3SSam Leffler bpf_getdltlist(d, bfl)
15178eab61f3SSam Leffler 	struct bpf_d *d;
15188eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
15198eab61f3SSam Leffler {
15208eab61f3SSam Leffler 	int n, error;
15218eab61f3SSam Leffler 	struct ifnet *ifp;
15228eab61f3SSam Leffler 	struct bpf_if *bp;
15238eab61f3SSam Leffler 
15248eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15258eab61f3SSam Leffler 	n = 0;
15268eab61f3SSam Leffler 	error = 0;
15278eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15288eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
15298eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
15308eab61f3SSam Leffler 			continue;
15318eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
15328eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1533e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
15348eab61f3SSam Leffler 				return (ENOMEM);
15358eab61f3SSam Leffler 			}
15368eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
15378eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
15388eab61f3SSam Leffler 		}
15398eab61f3SSam Leffler 		n++;
15408eab61f3SSam Leffler 	}
15418eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15428eab61f3SSam Leffler 	bfl->bfl_len = n;
15438eab61f3SSam Leffler 	return (error);
15448eab61f3SSam Leffler }
15458eab61f3SSam Leffler 
15468eab61f3SSam Leffler /*
15478eab61f3SSam Leffler  * Set the data link type of a BPF instance.
15488eab61f3SSam Leffler  */
15498eab61f3SSam Leffler static int
15508eab61f3SSam Leffler bpf_setdlt(d, dlt)
15518eab61f3SSam Leffler 	struct bpf_d *d;
15528eab61f3SSam Leffler 	u_int dlt;
15538eab61f3SSam Leffler {
15548eab61f3SSam Leffler 	int error, opromisc;
15558eab61f3SSam Leffler 	struct ifnet *ifp;
15568eab61f3SSam Leffler 	struct bpf_if *bp;
15578eab61f3SSam Leffler 
15588eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
15598eab61f3SSam Leffler 		return (0);
15608eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15618eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15628eab61f3SSam Leffler 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
15638eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
15648eab61f3SSam Leffler 			break;
15658eab61f3SSam Leffler 	}
15668eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15678eab61f3SSam Leffler 	if (bp != NULL) {
15688eab61f3SSam Leffler 		BPFD_LOCK(d);
15698eab61f3SSam Leffler 		opromisc = d->bd_promisc;
15708eab61f3SSam Leffler 		bpf_detachd(d);
15718eab61f3SSam Leffler 		bpf_attachd(d, bp);
15728eab61f3SSam Leffler 		reset_d(d);
15738eab61f3SSam Leffler 		BPFD_UNLOCK(d);
15748eab61f3SSam Leffler 		if (opromisc) {
15758eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
15768eab61f3SSam Leffler 			if (error)
15778eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
15788eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
15798eab61f3SSam Leffler 					error);
15808eab61f3SSam Leffler 			else
15818eab61f3SSam Leffler 				d->bd_promisc = 1;
15828eab61f3SSam Leffler 		}
15838eab61f3SSam Leffler 	}
15848eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1585de5d9935SRobert Watson }
1586de5d9935SRobert Watson 
1587929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1588bd3a5320SPoul-Henning Kamp 
158989c9c53dSPoul-Henning Kamp static void bpf_clone(void *arg, char *name, int namelen, struct cdev **dev);
15903f54a085SPoul-Henning Kamp 
15913f54a085SPoul-Henning Kamp static void
15923f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
15933f54a085SPoul-Henning Kamp 	void *arg;
15943f54a085SPoul-Henning Kamp 	char *name;
15953f54a085SPoul-Henning Kamp 	int namelen;
159689c9c53dSPoul-Henning Kamp 	struct cdev **dev;
15973f54a085SPoul-Henning Kamp {
15983f54a085SPoul-Henning Kamp 	int u;
15993f54a085SPoul-Henning Kamp 
1600f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
16013f54a085SPoul-Henning Kamp 		return;
1602db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
16033f54a085SPoul-Henning Kamp 		return;
1604b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1605b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1606b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
16073f54a085SPoul-Henning Kamp 	return;
16083f54a085SPoul-Henning Kamp }
16093f54a085SPoul-Henning Kamp 
1610514ede09SBruce Evans static void
1611514ede09SBruce Evans bpf_drvinit(unused)
1612514ede09SBruce Evans 	void *unused;
161353ac6efbSJulian Elischer {
161453ac6efbSJulian Elischer 
16156008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1616db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
16177198bf47SJulian Elischer }
161853ac6efbSJulian Elischer 
1619c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
162053ac6efbSJulian Elischer 
16215bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1622f8dc4716SMike Smith /*
1623f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1624f8dc4716SMike Smith  *
1625f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1626f8dc4716SMike Smith  * to be loaded at runtime.
1627f8dc4716SMike Smith  */
1628f8dc4716SMike Smith 
1629f8dc4716SMike Smith void
1630e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1631e5562beeSSam Leffler 	struct bpf_if *bp;
16328994a245SDag-Erling Smørgrav 	u_char *pkt;
16338994a245SDag-Erling Smørgrav 	u_int pktlen;
1634f8dc4716SMike Smith {
1635f8dc4716SMike Smith }
1636f8dc4716SMike Smith 
1637f8dc4716SMike Smith void
1638e5562beeSSam Leffler bpf_mtap(bp, m)
1639e5562beeSSam Leffler 	struct bpf_if *bp;
1640f8dc4716SMike Smith 	struct mbuf *m;
1641f8dc4716SMike Smith {
1642f8dc4716SMike Smith }
1643f8dc4716SMike Smith 
1644f8dc4716SMike Smith void
1645437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1646437ffe18SSam Leffler 	struct bpf_if *bp;
1647c1dae2f0STim J. Robbins 	void *d;
1648437ffe18SSam Leffler 	u_int l;
1649437ffe18SSam Leffler 	struct mbuf *m;
1650437ffe18SSam Leffler {
1651437ffe18SSam Leffler }
1652437ffe18SSam Leffler 
1653437ffe18SSam Leffler void
1654f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1655f8dc4716SMike Smith 	struct ifnet *ifp;
1656f8dc4716SMike Smith 	u_int dlt, hdrlen;
1657f8dc4716SMike Smith {
1658f8dc4716SMike Smith }
1659f8dc4716SMike Smith 
1660da626c17SBill Paul void
16615f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
16625f7a7923SSam Leffler 	struct ifnet *ifp;
16635f7a7923SSam Leffler 	u_int dlt, hdrlen;
16645f7a7923SSam Leffler 	struct bpf_if **driverp;
16655f7a7923SSam Leffler {
16665f7a7923SSam Leffler }
16675f7a7923SSam Leffler 
16685f7a7923SSam Leffler void
1669da626c17SBill Paul bpfdetach(ifp)
1670da626c17SBill Paul 	struct ifnet *ifp;
1671da626c17SBill Paul {
1672da626c17SBill Paul }
1673da626c17SBill Paul 
1674f8dc4716SMike Smith u_int
1675f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
16768994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
16778994a245SDag-Erling Smørgrav 	u_char *p;
1678f8dc4716SMike Smith 	u_int wirelen;
16798994a245SDag-Erling Smørgrav 	u_int buflen;
1680f8dc4716SMike Smith {
1681f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1682f8dc4716SMike Smith }
1683f8dc4716SMike Smith 
16845bb5f2c9SPeter Wemm int
16855bb5f2c9SPeter Wemm bpf_validate(f, len)
16865bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
16875bb5f2c9SPeter Wemm 	int len;
16885bb5f2c9SPeter Wemm {
16895bb5f2c9SPeter Wemm 	return 0;		/* false */
16905bb5f2c9SPeter Wemm }
16915bb5f2c9SPeter Wemm 
16925bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1693