xref: /freebsd/sys/net/bpf.c (revision e76eee55629fc65205c1a033df22d589b1838f2d)
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>
47e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
4882f4445dSRobert Watson #include <sys/mac.h>
494d1d4912SBruce Evans #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
530310c19fSBruce Evans #include <sys/signalvar.h>
54528f627fSBruce Evans #include <sys/filio.h>
55528f627fSBruce Evans #include <sys/sockio.h>
56528f627fSBruce Evans #include <sys/ttycom.h>
57e76eee55SPoul-Henning Kamp #include <sys/uio.h>
58df8bae1dSRodney W. Grimes 
5995aab9ccSJohn-Mark Gurney #include <sys/event.h>
6095aab9ccSJohn-Mark Gurney #include <sys/file.h>
61243ac7d8SPeter Wemm #include <sys/poll.h>
6295aab9ccSJohn-Mark Gurney #include <sys/proc.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes 
66fba9235dSBruce Evans #include <net/if.h>
67df8bae1dSRodney W. Grimes #include <net/bpf.h>
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  */
944a3feeaaSRobert Watson static LIST_HEAD(, 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;
2594a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
260df8bae1dSRodney W. Grimes 
26124a229f4SSam Leffler 	*bp->bif_driverp = bp;
262e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes /*
266df8bae1dSRodney W. Grimes  * Detach a file from its interface.
267df8bae1dSRodney W. Grimes  */
268df8bae1dSRodney W. Grimes static void
269df8bae1dSRodney W. Grimes bpf_detachd(d)
270df8bae1dSRodney W. Grimes 	struct bpf_d *d;
271df8bae1dSRodney W. Grimes {
2726e891d64SPoul-Henning Kamp 	int error;
273df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
27446448b5aSRobert Watson 	struct ifnet *ifp;
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
27746448b5aSRobert Watson 	BPFIF_LOCK(bp);
27846448b5aSRobert Watson 	BPFD_LOCK(d);
27946448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
28046448b5aSRobert Watson 
28146448b5aSRobert Watson 	/*
28246448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
28346448b5aSRobert Watson 	 */
28446448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
28546448b5aSRobert Watson 
28646448b5aSRobert Watson 	/*
28746448b5aSRobert Watson 	 * Let the driver know that there are no more listeners.
28846448b5aSRobert Watson 	 */
28946448b5aSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
29046448b5aSRobert Watson 		*bp->bif_driverp = NULL;
29146448b5aSRobert Watson 
292572bde2aSRobert Watson 	d->bd_bif = NULL;
29346448b5aSRobert Watson 	BPFD_UNLOCK(d);
29446448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
29546448b5aSRobert Watson 
296df8bae1dSRodney W. Grimes 	/*
297df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
298df8bae1dSRodney W. Grimes 	 * If so, turn it off.
299df8bae1dSRodney W. Grimes 	 */
300df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
301df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
30246448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3036e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
304df8bae1dSRodney W. Grimes 			/*
3056e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
306df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
307df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
308df8bae1dSRodney W. Grimes 			 * take it out.
309df8bae1dSRodney W. Grimes 			 */
3108eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3118eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3126e891d64SPoul-Henning Kamp 		}
313df8bae1dSRodney W. Grimes 	}
314df8bae1dSRodney W. Grimes }
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes /*
317df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
318df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
319df8bae1dSRodney W. Grimes  */
320df8bae1dSRodney W. Grimes /* ARGSUSED */
32187f6c662SJulian Elischer static	int
322b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
32389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
32460039670SBruce Evans 	int flags;
32560039670SBruce Evans 	int fmt;
326b40ce416SJulian Elischer 	struct thread *td;
327df8bae1dSRodney W. Grimes {
328e7bb21b3SJonathan Lemon 	struct bpf_d *d;
329df8bae1dSRodney W. Grimes 
330e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
331bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
332df8bae1dSRodney W. Grimes 	/*
333df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
334df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
335df8bae1dSRodney W. Grimes 	 */
336d17d8184SRobert Watson 	if (d != NULL) {
337e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
338df8bae1dSRodney W. Grimes 		return (EBUSY);
339e7bb21b3SJonathan Lemon 	}
340e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
341e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
342e7bb21b3SJonathan Lemon 
343d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
344b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
345b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
346a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
347bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
348df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
34900a83887SPaul Traina 	d->bd_sig = SIGIO;
3508ed3828cSRobert Watson 	d->bd_seesent = 1;
35182f4445dSRobert Watson #ifdef MAC
35282f4445dSRobert Watson 	mac_init_bpfdesc(d);
35382f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
35482f4445dSRobert Watson #endif
3556008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
35628b86052SRobert Watson 	callout_init(&d->bd_callout, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
357ad3b9257SJohn-Mark Gurney 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes 	return (0);
360df8bae1dSRodney W. Grimes }
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes /*
363df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
364df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
365df8bae1dSRodney W. Grimes  */
366df8bae1dSRodney W. Grimes /* ARGSUSED */
36787f6c662SJulian Elischer static	int
368b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
36989c9c53dSPoul-Henning Kamp 	struct cdev *dev;
37060039670SBruce Evans 	int flags;
37160039670SBruce Evans 	int fmt;
372b40ce416SJulian Elischer 	struct thread *td;
373df8bae1dSRodney W. Grimes {
374e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
375df8bae1dSRodney W. Grimes 
37681bda851SJohn Polstra 	BPFD_LOCK(d);
37781bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
37881bda851SJohn Polstra 		callout_stop(&d->bd_callout);
37981bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
38081bda851SJohn Polstra 	BPFD_UNLOCK(d);
381e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
382e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
383df8bae1dSRodney W. Grimes 	if (d->bd_bif)
384df8bae1dSRodney W. Grimes 		bpf_detachd(d);
385e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
38682f4445dSRobert Watson #ifdef MAC
38782f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
38882f4445dSRobert Watson #endif /* MAC */
389ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
390df8bae1dSRodney W. Grimes 	bpf_freed(d);
391d17d8184SRobert Watson 	dev->si_drv1 = NULL;
392d722be54SLuigi Rizzo 	free(d, M_BPF);
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 	return (0);
395df8bae1dSRodney W. Grimes }
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes /*
399df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
400df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
401df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
402df8bae1dSRodney W. Grimes  */
403df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
404df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
405df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
406df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
407df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
408d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
409df8bae1dSRodney W. Grimes /*
410df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
411df8bae1dSRodney W. Grimes  */
41287f6c662SJulian Elischer static	int
41360039670SBruce Evans bpfread(dev, uio, ioflag)
41489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4158994a245SDag-Erling Smørgrav 	struct uio *uio;
41660039670SBruce Evans 	int ioflag;
417df8bae1dSRodney W. Grimes {
418e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
41981bda851SJohn Polstra 	int timed_out;
420df8bae1dSRodney W. Grimes 	int error;
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 	/*
423df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
424df8bae1dSRodney W. Grimes 	 * as kernel buffers.
425df8bae1dSRodney W. Grimes 	 */
426df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
427df8bae1dSRodney W. Grimes 		return (EINVAL);
428df8bae1dSRodney W. Grimes 
429e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
43081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
43181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
43281bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
43381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
434df8bae1dSRodney W. Grimes 	/*
435df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
436df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
437df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
438df8bae1dSRodney W. Grimes 	 */
439572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
44081bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
441df8bae1dSRodney W. Grimes 			/*
442df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
443df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
444df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
445df8bae1dSRodney W. Grimes 			 */
446df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
447df8bae1dSRodney W. Grimes 			break;
448df8bae1dSRodney W. Grimes 		}
449de5d9935SRobert Watson 
450de5d9935SRobert Watson 		/*
451de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
452de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
453de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
454de5d9935SRobert Watson 		 * it before using it again.
455de5d9935SRobert Watson 		 */
456de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
457e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
458de5d9935SRobert Watson 			return (ENXIO);
459de5d9935SRobert Watson 		}
460de5d9935SRobert Watson 
461e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
462e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
463fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
464fba3cfdeSJohn Polstra 		}
465521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
466e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
467df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
468e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
469df8bae1dSRodney W. Grimes 			return (error);
470df8bae1dSRodney W. Grimes 		}
471df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
472df8bae1dSRodney W. Grimes 			/*
473df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
474df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
475df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
476df8bae1dSRodney W. Grimes 			 */
477df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
478df8bae1dSRodney W. Grimes 				/*
479df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
480df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
481df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
482df8bae1dSRodney W. Grimes 				 */
483df8bae1dSRodney W. Grimes 				break;
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
486e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
487df8bae1dSRodney W. Grimes 				return (0);
488df8bae1dSRodney W. Grimes 			}
489df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
490df8bae1dSRodney W. Grimes 			break;
491df8bae1dSRodney W. Grimes 		}
492df8bae1dSRodney W. Grimes 	}
493df8bae1dSRodney W. Grimes 	/*
494df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
495df8bae1dSRodney W. Grimes 	 */
496e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 	/*
499df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
500df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
501df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
502df8bae1dSRodney W. Grimes 	 */
503e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
504df8bae1dSRodney W. Grimes 
505e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
506df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
507572bde2aSRobert Watson 	d->bd_hbuf = NULL;
508df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
509e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 	return (error);
512df8bae1dSRodney W. Grimes }
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes /*
516df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
517df8bae1dSRodney W. Grimes  */
518e7bb21b3SJonathan Lemon static __inline void
519df8bae1dSRodney W. Grimes bpf_wakeup(d)
5208994a245SDag-Erling Smørgrav 	struct bpf_d *d;
521df8bae1dSRodney W. Grimes {
52281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
52381bda851SJohn Polstra 		callout_stop(&d->bd_callout);
52481bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
52581bda851SJohn Polstra 	}
526521f364bSDag-Erling Smørgrav 	wakeup(d);
527831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
528f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
52900a83887SPaul Traina 
530512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
531ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes 
53481bda851SJohn Polstra static void
53581bda851SJohn Polstra bpf_timed_out(arg)
53681bda851SJohn Polstra 	void *arg;
53781bda851SJohn Polstra {
53881bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
53981bda851SJohn Polstra 
54081bda851SJohn Polstra 	BPFD_LOCK(d);
54181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
54281bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
54381bda851SJohn Polstra 		if (d->bd_slen != 0)
54481bda851SJohn Polstra 			bpf_wakeup(d);
54581bda851SJohn Polstra 	}
54681bda851SJohn Polstra 	BPFD_UNLOCK(d);
54781bda851SJohn Polstra }
54881bda851SJohn Polstra 
54987f6c662SJulian Elischer static	int
55060039670SBruce Evans bpfwrite(dev, uio, ioflag)
55189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
552df8bae1dSRodney W. Grimes 	struct uio *uio;
55360039670SBruce Evans 	int ioflag;
554df8bae1dSRodney W. Grimes {
555e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
556df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
557df8bae1dSRodney W. Grimes 	struct mbuf *m;
558e7bb21b3SJonathan Lemon 	int error;
5598240bf1eSRobert Watson 	struct sockaddr dst;
560df8bae1dSRodney W. Grimes 	int datlen;
561df8bae1dSRodney W. Grimes 
562572bde2aSRobert Watson 	if (d->bd_bif == NULL)
563df8bae1dSRodney W. Grimes 		return (ENXIO);
564df8bae1dSRodney W. Grimes 
565df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
566df8bae1dSRodney W. Grimes 
5673518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
5683518d220SSam Leffler 		return (ENETDOWN);
5693518d220SSam Leffler 
570df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
571df8bae1dSRodney W. Grimes 		return (0);
572df8bae1dSRodney W. Grimes 
5738240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
574df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
575df8bae1dSRodney W. Grimes 	if (error)
576df8bae1dSRodney W. Grimes 		return (error);
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
579df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
580df8bae1dSRodney W. Grimes 
581114ae644SMike Smith 	if (d->bd_hdrcmplt)
582114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
583114ae644SMike Smith 
58482f4445dSRobert Watson #ifdef MAC
585f747d2ddSRobert Watson 	BPFD_LOCK(d);
58682f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
587f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
58882f4445dSRobert Watson #endif
589b8f9429dSRobert Watson 	NET_LOCK_GIANT();
590572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
591b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
592df8bae1dSRodney W. Grimes 	/*
593df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
594df8bae1dSRodney W. Grimes 	 */
595df8bae1dSRodney W. Grimes 	return (error);
596df8bae1dSRodney W. Grimes }
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes /*
599df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
600e7bb21b3SJonathan Lemon  * receive and drop counts.
601df8bae1dSRodney W. Grimes  */
602df8bae1dSRodney W. Grimes static void
603df8bae1dSRodney W. Grimes reset_d(d)
604df8bae1dSRodney W. Grimes 	struct bpf_d *d;
605df8bae1dSRodney W. Grimes {
606e7bb21b3SJonathan Lemon 
607e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
608df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
609df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
610df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
611572bde2aSRobert Watson 		d->bd_hbuf = NULL;
612df8bae1dSRodney W. Grimes 	}
613df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
614df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
615df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
616df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
617df8bae1dSRodney W. Grimes }
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes /*
620df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
621df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
622df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
623df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
624df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
625df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
626df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
627df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
628df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
629df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
630df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
631df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
632df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
633df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
634114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
635114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6368ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6378ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
638df8bae1dSRodney W. Grimes  */
639df8bae1dSRodney W. Grimes /* ARGSUSED */
64087f6c662SJulian Elischer static	int
641b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
64289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
643ecbb00a2SDoug Rabson 	u_long cmd;
644df8bae1dSRodney W. Grimes 	caddr_t addr;
64560039670SBruce Evans 	int flags;
646b40ce416SJulian Elischer 	struct thread *td;
647df8bae1dSRodney W. Grimes {
648e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
649e7bb21b3SJonathan Lemon 	int error = 0;
650df8bae1dSRodney W. Grimes 
65181bda851SJohn Polstra 	BPFD_LOCK(d);
65281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
65381bda851SJohn Polstra 		callout_stop(&d->bd_callout);
65481bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
65581bda851SJohn Polstra 	BPFD_UNLOCK(d);
65681bda851SJohn Polstra 
657df8bae1dSRodney W. Grimes 	switch (cmd) {
658df8bae1dSRodney W. Grimes 
659df8bae1dSRodney W. Grimes 	default:
660df8bae1dSRodney W. Grimes 		error = EINVAL;
661df8bae1dSRodney W. Grimes 		break;
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 	/*
664df8bae1dSRodney W. Grimes 	 * Check for read packet available.
665df8bae1dSRodney W. Grimes 	 */
666df8bae1dSRodney W. Grimes 	case FIONREAD:
667df8bae1dSRodney W. Grimes 		{
668df8bae1dSRodney W. Grimes 			int n;
669df8bae1dSRodney W. Grimes 
670e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
671df8bae1dSRodney W. Grimes 			n = d->bd_slen;
672df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
673df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
674e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 			*(int *)addr = n;
677df8bae1dSRodney W. Grimes 			break;
678df8bae1dSRodney W. Grimes 		}
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
681df8bae1dSRodney W. Grimes 		{
682df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
683df8bae1dSRodney W. Grimes 
684572bde2aSRobert Watson 			if (d->bd_bif == NULL)
685df8bae1dSRodney W. Grimes 				error = EINVAL;
686df8bae1dSRodney W. Grimes 			else {
687df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
688df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
689df8bae1dSRodney W. Grimes 			}
690df8bae1dSRodney W. Grimes 			break;
691df8bae1dSRodney W. Grimes 		}
692df8bae1dSRodney W. Grimes 
693df8bae1dSRodney W. Grimes 	/*
694df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
695df8bae1dSRodney W. Grimes 	 */
696df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
697df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
698df8bae1dSRodney W. Grimes 		break;
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 	/*
701df8bae1dSRodney W. Grimes 	 * Set buffer length.
702df8bae1dSRodney W. Grimes 	 */
703df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
704572bde2aSRobert Watson 		if (d->bd_bif != NULL)
705df8bae1dSRodney W. Grimes 			error = EINVAL;
706df8bae1dSRodney W. Grimes 		else {
7078994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
708df8bae1dSRodney W. Grimes 
709eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
710eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
711df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
712df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
713df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
714df8bae1dSRodney W. Grimes 		}
715df8bae1dSRodney W. Grimes 		break;
716df8bae1dSRodney W. Grimes 
717df8bae1dSRodney W. Grimes 	/*
718df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
719df8bae1dSRodney W. Grimes 	 */
720df8bae1dSRodney W. Grimes 	case BIOCSETF:
721df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
722df8bae1dSRodney W. Grimes 		break;
723df8bae1dSRodney W. Grimes 
724df8bae1dSRodney W. Grimes 	/*
725df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
726df8bae1dSRodney W. Grimes 	 */
727df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
728e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
729df8bae1dSRodney W. Grimes 		reset_d(d);
730e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
731df8bae1dSRodney W. Grimes 		break;
732df8bae1dSRodney W. Grimes 
733df8bae1dSRodney W. Grimes 	/*
734df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
735df8bae1dSRodney W. Grimes 	 */
736df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
737572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
738df8bae1dSRodney W. Grimes 			/*
739df8bae1dSRodney W. Grimes 			 * No interface attached yet.
740df8bae1dSRodney W. Grimes 			 */
741df8bae1dSRodney W. Grimes 			error = EINVAL;
742df8bae1dSRodney W. Grimes 			break;
743df8bae1dSRodney W. Grimes 		}
744df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
745e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
746df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
747e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
748df8bae1dSRodney W. Grimes 			if (error == 0)
749df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
750df8bae1dSRodney W. Grimes 		}
751df8bae1dSRodney W. Grimes 		break;
752df8bae1dSRodney W. Grimes 
753df8bae1dSRodney W. Grimes 	/*
7548eab61f3SSam Leffler 	 * Get current data link type.
755df8bae1dSRodney W. Grimes 	 */
756df8bae1dSRodney W. Grimes 	case BIOCGDLT:
757572bde2aSRobert Watson 		if (d->bd_bif == NULL)
758df8bae1dSRodney W. Grimes 			error = EINVAL;
759df8bae1dSRodney W. Grimes 		else
760df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
761df8bae1dSRodney W. Grimes 		break;
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	/*
7648eab61f3SSam Leffler 	 * Get a list of supported data link types.
7658eab61f3SSam Leffler 	 */
7668eab61f3SSam Leffler 	case BIOCGDLTLIST:
767572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7688eab61f3SSam Leffler 			error = EINVAL;
7698eab61f3SSam Leffler 		else
7708eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
7718eab61f3SSam Leffler 		break;
7728eab61f3SSam Leffler 
7738eab61f3SSam Leffler 	/*
7748eab61f3SSam Leffler 	 * Set data link type.
7758eab61f3SSam Leffler 	 */
7768eab61f3SSam Leffler 	case BIOCSDLT:
777572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7788eab61f3SSam Leffler 			error = EINVAL;
7798eab61f3SSam Leffler 		else
7808eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
7818eab61f3SSam Leffler 		break;
7828eab61f3SSam Leffler 
7838eab61f3SSam Leffler 	/*
7841dd0feaaSArchie Cobbs 	 * Get interface name.
785df8bae1dSRodney W. Grimes 	 */
786df8bae1dSRodney W. Grimes 	case BIOCGETIF:
787572bde2aSRobert Watson 		if (d->bd_bif == NULL)
788df8bae1dSRodney W. Grimes 			error = EINVAL;
7891dd0feaaSArchie Cobbs 		else {
7901dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7911dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7921dd0feaaSArchie Cobbs 
7939bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
7949bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
7951dd0feaaSArchie Cobbs 		}
796df8bae1dSRodney W. Grimes 		break;
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes 	/*
799df8bae1dSRodney W. Grimes 	 * Set interface.
800df8bae1dSRodney W. Grimes 	 */
801df8bae1dSRodney W. Grimes 	case BIOCSETIF:
802df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
803df8bae1dSRodney W. Grimes 		break;
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 	/*
806df8bae1dSRodney W. Grimes 	 * Set read timeout.
807df8bae1dSRodney W. Grimes 	 */
808df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
809df8bae1dSRodney W. Grimes 		{
810df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
811df8bae1dSRodney W. Grimes 
812bdc2cdc5SAlexander Langer 			/*
813bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
814bdc2cdc5SAlexander Langer 			 * a one-shot timer.
815bdc2cdc5SAlexander Langer 			 */
816bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
817bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
818df8bae1dSRodney W. Grimes 			break;
819df8bae1dSRodney W. Grimes 		}
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	/*
822df8bae1dSRodney W. Grimes 	 * Get read timeout.
823df8bae1dSRodney W. Grimes 	 */
824df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
825df8bae1dSRodney W. Grimes 		{
826df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
827df8bae1dSRodney W. Grimes 
828bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
829bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
830df8bae1dSRodney W. Grimes 			break;
831df8bae1dSRodney W. Grimes 		}
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes 	/*
834df8bae1dSRodney W. Grimes 	 * Get packet stats.
835df8bae1dSRodney W. Grimes 	 */
836df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
837df8bae1dSRodney W. Grimes 		{
838df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
841df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
842df8bae1dSRodney W. Grimes 			break;
843df8bae1dSRodney W. Grimes 		}
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	/*
846df8bae1dSRodney W. Grimes 	 * Set immediate mode.
847df8bae1dSRodney W. Grimes 	 */
848df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
849df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
850df8bae1dSRodney W. Grimes 		break;
851df8bae1dSRodney W. Grimes 
852df8bae1dSRodney W. Grimes 	case BIOCVERSION:
853df8bae1dSRodney W. Grimes 		{
854df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
855df8bae1dSRodney W. Grimes 
856df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
857df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
858df8bae1dSRodney W. Grimes 			break;
859df8bae1dSRodney W. Grimes 		}
86000a83887SPaul Traina 
861114ae644SMike Smith 	/*
862114ae644SMike Smith 	 * Get "header already complete" flag
863114ae644SMike Smith 	 */
864114ae644SMike Smith 	case BIOCGHDRCMPLT:
865114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
866114ae644SMike Smith 		break;
867114ae644SMike Smith 
868114ae644SMike Smith 	/*
869114ae644SMike Smith 	 * Set "header already complete" flag
870114ae644SMike Smith 	 */
871114ae644SMike Smith 	case BIOCSHDRCMPLT:
872114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
873114ae644SMike Smith 		break;
874114ae644SMike Smith 
8758ed3828cSRobert Watson 	/*
8768ed3828cSRobert Watson 	 * Get "see sent packets" flag
8778ed3828cSRobert Watson 	 */
8788ed3828cSRobert Watson 	case BIOCGSEESENT:
8798ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8808ed3828cSRobert Watson 		break;
8818ed3828cSRobert Watson 
8828ed3828cSRobert Watson 	/*
8838ed3828cSRobert Watson 	 * Set "see sent packets" flag
8848ed3828cSRobert Watson 	 */
8858ed3828cSRobert Watson 	case BIOCSSEESENT:
8868ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8878ed3828cSRobert Watson 		break;
8888ed3828cSRobert Watson 
88900a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
89000a83887SPaul Traina 		break;
89100a83887SPaul Traina 
89200a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
89300a83887SPaul Traina 		d->bd_async = *(int *)addr;
89400a83887SPaul Traina 		break;
89500a83887SPaul Traina 
896831d27a9SDon Lewis 	case FIOSETOWN:
897831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
89800a83887SPaul Traina 		break;
89900a83887SPaul Traina 
900831d27a9SDon Lewis 	case FIOGETOWN:
90191e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
902831d27a9SDon Lewis 		break;
903831d27a9SDon Lewis 
904831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
905831d27a9SDon Lewis 	case TIOCSPGRP:
906831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
907831d27a9SDon Lewis 		break;
908831d27a9SDon Lewis 
909831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
91000a83887SPaul Traina 	case TIOCGPGRP:
91191e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
91200a83887SPaul Traina 		break;
91300a83887SPaul Traina 
91400a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
91500a83887SPaul Traina 		{
91600a83887SPaul Traina 			u_int sig;
91700a83887SPaul Traina 
91800a83887SPaul Traina 			sig = *(u_int *)addr;
91900a83887SPaul Traina 
92000a83887SPaul Traina 			if (sig >= NSIG)
92100a83887SPaul Traina 				error = EINVAL;
92200a83887SPaul Traina 			else
92300a83887SPaul Traina 				d->bd_sig = sig;
92400a83887SPaul Traina 			break;
92500a83887SPaul Traina 		}
92600a83887SPaul Traina 	case BIOCGRSIG:
92700a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
92800a83887SPaul Traina 		break;
929df8bae1dSRodney W. Grimes 	}
930df8bae1dSRodney W. Grimes 	return (error);
931df8bae1dSRodney W. Grimes }
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes /*
934df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
935df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
936df8bae1dSRodney W. Grimes  */
937f708ef1bSPoul-Henning Kamp static int
938df8bae1dSRodney W. Grimes bpf_setf(d, fp)
939df8bae1dSRodney W. Grimes 	struct bpf_d *d;
940df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
941df8bae1dSRodney W. Grimes {
942df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
943df8bae1dSRodney W. Grimes 	u_int flen, size;
944df8bae1dSRodney W. Grimes 
945df8bae1dSRodney W. Grimes 	old = d->bd_filter;
946572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
947df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
948df8bae1dSRodney W. Grimes 			return (EINVAL);
949e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
950572bde2aSRobert Watson 		d->bd_filter = NULL;
951df8bae1dSRodney W. Grimes 		reset_d(d);
952e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
953572bde2aSRobert Watson 		if (old != NULL)
954bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
955df8bae1dSRodney W. Grimes 		return (0);
956df8bae1dSRodney W. Grimes 	}
957df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
958df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
959df8bae1dSRodney W. Grimes 		return (EINVAL);
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
962a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
963df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
964df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
965e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
966df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
967df8bae1dSRodney W. Grimes 		reset_d(d);
968e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
969572bde2aSRobert Watson 		if (old != NULL)
970bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 		return (0);
973df8bae1dSRodney W. Grimes 	}
974bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
975df8bae1dSRodney W. Grimes 	return (EINVAL);
976df8bae1dSRodney W. Grimes }
977df8bae1dSRodney W. Grimes 
978df8bae1dSRodney W. Grimes /*
979df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
980df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
981df8bae1dSRodney W. Grimes  * Return an errno or 0.
982df8bae1dSRodney W. Grimes  */
983df8bae1dSRodney W. Grimes static int
984df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
985df8bae1dSRodney W. Grimes 	struct bpf_d *d;
986df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
987df8bae1dSRodney W. Grimes {
988df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
989e7bb21b3SJonathan Lemon 	int error;
9909b44ff22SGarrett Wollman 	struct ifnet *theywant;
991df8bae1dSRodney W. Grimes 
9929b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
993572bde2aSRobert Watson 	if (theywant == NULL)
9949b44ff22SGarrett Wollman 		return ENXIO;
9959b44ff22SGarrett Wollman 
996df8bae1dSRodney W. Grimes 	/*
997df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
998df8bae1dSRodney W. Grimes 	 */
999e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
10004a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1001df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
1002df8bae1dSRodney W. Grimes 
1003572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
1004df8bae1dSRodney W. Grimes 			continue;
100524a229f4SSam Leffler 		/* skip additional entry */
100624a229f4SSam Leffler 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
100724a229f4SSam Leffler 			continue;
1008e7bb21b3SJonathan Lemon 
1009e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1010df8bae1dSRodney W. Grimes 		/*
1011df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1012df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1013df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1014df8bae1dSRodney W. Grimes 		 * just flush the buffer.
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 
111686c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(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 
112995aab9ccSJohn-Mark Gurney 	return (ready);
113095aab9ccSJohn-Mark Gurney }
113195aab9ccSJohn-Mark Gurney 
113295aab9ccSJohn-Mark Gurney /*
1133df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1134df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1135df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1136df8bae1dSRodney W. Grimes  * buffer.
1137df8bae1dSRodney W. Grimes  */
1138df8bae1dSRodney W. Grimes void
113924a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
114024a229f4SSam Leffler 	struct bpf_if *bp;
11418994a245SDag-Erling Smørgrav 	u_char *pkt;
11428994a245SDag-Erling Smørgrav 	u_int pktlen;
1143df8bae1dSRodney W. Grimes {
11448994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11458994a245SDag-Erling Smørgrav 	u_int slen;
1146e7bb21b3SJonathan Lemon 
114746691dd8SRobert Watson 	/*
114846691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
114946691dd8SRobert Watson 	 * no descriptors attached.
115046691dd8SRobert Watson 	 */
11514a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
115246691dd8SRobert Watson 		return;
115346691dd8SRobert Watson 
1154e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
11554a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1156e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1157df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1158df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1159ec272d87SRobert Watson 		if (slen != 0) {
1160ec272d87SRobert Watson #ifdef MAC
116124a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1162ec272d87SRobert Watson #endif
1163df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1164ec272d87SRobert Watson 		}
1165e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1166df8bae1dSRodney W. Grimes 	}
1167e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1168df8bae1dSRodney W. Grimes }
1169df8bae1dSRodney W. Grimes 
1170df8bae1dSRodney W. Grimes /*
1171df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1172df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1173df8bae1dSRodney W. Grimes  */
1174df8bae1dSRodney W. Grimes static void
1175df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1176df8bae1dSRodney W. Grimes 	const void *src_arg;
1177df8bae1dSRodney W. Grimes 	void *dst_arg;
11788994a245SDag-Erling Smørgrav 	size_t len;
1179df8bae1dSRodney W. Grimes {
11808994a245SDag-Erling Smørgrav 	const struct mbuf *m;
11818994a245SDag-Erling Smørgrav 	u_int count;
1182df8bae1dSRodney W. Grimes 	u_char *dst;
1183df8bae1dSRodney W. Grimes 
1184df8bae1dSRodney W. Grimes 	m = src_arg;
1185df8bae1dSRodney W. Grimes 	dst = dst_arg;
1186df8bae1dSRodney W. Grimes 	while (len > 0) {
1187572bde2aSRobert Watson 		if (m == NULL)
1188df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1189df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
11900453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1191df8bae1dSRodney W. Grimes 		m = m->m_next;
1192df8bae1dSRodney W. Grimes 		dst += count;
1193df8bae1dSRodney W. Grimes 		len -= count;
1194df8bae1dSRodney W. Grimes 	}
1195df8bae1dSRodney W. Grimes }
1196df8bae1dSRodney W. Grimes 
1197df8bae1dSRodney W. Grimes /*
1198df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1199df8bae1dSRodney W. Grimes  */
1200df8bae1dSRodney W. Grimes void
120124a229f4SSam Leffler bpf_mtap(bp, m)
120224a229f4SSam Leffler 	struct bpf_if *bp;
1203df8bae1dSRodney W. Grimes 	struct mbuf *m;
1204df8bae1dSRodney W. Grimes {
1205df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1206df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1207df8bae1dSRodney W. Grimes 
120846691dd8SRobert Watson 	/*
120946691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
121046691dd8SRobert Watson 	 * no descriptors attached.
121146691dd8SRobert Watson 	 */
12124a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
121346691dd8SRobert Watson 		return;
121446691dd8SRobert Watson 
1215f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
12167b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
121724a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
12187b831242SPoul-Henning Kamp 		return;
12197b831242SPoul-Henning Kamp 	}
1220df8bae1dSRodney W. Grimes 
1221e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12224a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
12238ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
12248ed3828cSRobert Watson 			continue;
1225e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1226df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1227df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1228df8bae1dSRodney W. Grimes 		if (slen != 0)
12290c7fb534SRobert Watson #ifdef MAC
123024a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
12310c7fb534SRobert Watson #endif
12320c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
12330c7fb534SRobert Watson 				    bpf_mcopy);
1234e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1235df8bae1dSRodney W. Grimes 	}
1236e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1237df8bae1dSRodney W. Grimes }
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes /*
1240437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1241437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1242437ffe18SSam Leffler  */
1243437ffe18SSam Leffler void
1244437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1245437ffe18SSam Leffler 	struct bpf_if *bp;
1246437ffe18SSam Leffler 	void *data;
1247437ffe18SSam Leffler 	u_int dlen;
1248437ffe18SSam Leffler 	struct mbuf *m;
1249437ffe18SSam Leffler {
1250437ffe18SSam Leffler 	struct mbuf mb;
1251437ffe18SSam Leffler 	struct bpf_d *d;
1252437ffe18SSam Leffler 	u_int pktlen, slen;
1253437ffe18SSam Leffler 
125446691dd8SRobert Watson 	/*
125546691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
125646691dd8SRobert Watson 	 * no descriptors attached.
125746691dd8SRobert Watson 	 */
12584a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
125946691dd8SRobert Watson 		return;
126046691dd8SRobert Watson 
1261437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1262437ffe18SSam Leffler 	/*
1263437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1264437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1265437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1266437ffe18SSam Leffler 	 */
1267437ffe18SSam Leffler 	mb.m_next = m;
1268437ffe18SSam Leffler 	mb.m_data = data;
1269437ffe18SSam Leffler 	mb.m_len = dlen;
1270437ffe18SSam Leffler 	pktlen += dlen;
1271437ffe18SSam Leffler 
1272437ffe18SSam Leffler 	BPFIF_LOCK(bp);
12734a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1274437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1275437ffe18SSam Leffler 			continue;
1276437ffe18SSam Leffler 		BPFD_LOCK(d);
1277437ffe18SSam Leffler 		++d->bd_rcount;
1278437ffe18SSam Leffler 		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1279437ffe18SSam Leffler 		if (slen != 0)
1280437ffe18SSam Leffler #ifdef MAC
1281437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1282437ffe18SSam Leffler #endif
1283437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1284437ffe18SSam Leffler 				    bpf_mcopy);
1285437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1286437ffe18SSam Leffler 	}
1287437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1288437ffe18SSam Leffler }
1289437ffe18SSam Leffler 
1290437ffe18SSam Leffler /*
1291df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
12929e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1293df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1294df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1295df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1296df8bae1dSRodney W. Grimes  */
1297df8bae1dSRodney W. Grimes static void
1298df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
12998994a245SDag-Erling Smørgrav 	struct bpf_d *d;
13008994a245SDag-Erling Smørgrav 	u_char *pkt;
13018994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
13028994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1303df8bae1dSRodney W. Grimes {
13048994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
13058994a245SDag-Erling Smørgrav 	int totlen, curlen;
13068994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
13079e610888SDag-Erling Smørgrav 
1308df8bae1dSRodney W. Grimes 	/*
1309df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1310df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1311df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1312df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1313df8bae1dSRodney W. Grimes 	 */
1314df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1315df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1316df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1317df8bae1dSRodney W. Grimes 
1318df8bae1dSRodney W. Grimes 	/*
1319df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1320df8bae1dSRodney W. Grimes 	 */
1321df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1322df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1323df8bae1dSRodney W. Grimes 		/*
1324df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1325df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1326df8bae1dSRodney W. Grimes 		 * pending reads.
1327df8bae1dSRodney W. Grimes 		 */
1328572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1329df8bae1dSRodney W. Grimes 			/*
1330df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1331df8bae1dSRodney W. Grimes 			 * so drop the packet.
1332df8bae1dSRodney W. Grimes 			 */
1333df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1334df8bae1dSRodney W. Grimes 			return;
1335df8bae1dSRodney W. Grimes 		}
1336df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1337df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1338df8bae1dSRodney W. Grimes 		curlen = 0;
1339df8bae1dSRodney W. Grimes 	}
134081bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1341df8bae1dSRodney W. Grimes 		/*
134281bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
134381bda851SJohn Polstra 		 * already expired during a select call.  A packet
134481bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1345df8bae1dSRodney W. Grimes 		 */
1346df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes 	/*
1349df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1350df8bae1dSRodney W. Grimes 	 */
1351df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1352df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1353df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1354df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1355df8bae1dSRodney W. Grimes 	/*
1356df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1357df8bae1dSRodney W. Grimes 	 */
1358df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1359df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1360df8bae1dSRodney W. Grimes }
1361df8bae1dSRodney W. Grimes 
1362df8bae1dSRodney W. Grimes /*
1363df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1364df8bae1dSRodney W. Grimes  */
1365df8bae1dSRodney W. Grimes static int
1366df8bae1dSRodney W. Grimes bpf_allocbufs(d)
13678994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1368df8bae1dSRodney W. Grimes {
1369a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1370572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1371df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1372df8bae1dSRodney W. Grimes 
1373a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1374572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1375bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1376df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1377df8bae1dSRodney W. Grimes 	}
1378df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1379df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1380df8bae1dSRodney W. Grimes 	return (0);
1381df8bae1dSRodney W. Grimes }
1382df8bae1dSRodney W. Grimes 
1383df8bae1dSRodney W. Grimes /*
1384df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1385df8bae1dSRodney W. Grimes  * Called on close.
1386df8bae1dSRodney W. Grimes  */
1387df8bae1dSRodney W. Grimes static void
1388df8bae1dSRodney W. Grimes bpf_freed(d)
13898994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1390df8bae1dSRodney W. Grimes {
1391df8bae1dSRodney W. Grimes 	/*
1392df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1393df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1394df8bae1dSRodney W. Grimes 	 * free.
1395df8bae1dSRodney W. Grimes 	 */
1396572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1397bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1398572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1399bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1400572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1401bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1402df8bae1dSRodney W. Grimes 	}
1403df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1404bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1405e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1406df8bae1dSRodney W. Grimes }
1407df8bae1dSRodney W. Grimes 
1408df8bae1dSRodney W. Grimes /*
140924a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
141024a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1411df8bae1dSRodney W. Grimes  */
1412df8bae1dSRodney W. Grimes void
14139b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1414df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1415df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1416df8bae1dSRodney W. Grimes {
141724a229f4SSam Leffler 
141824a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
141924a229f4SSam Leffler }
142024a229f4SSam Leffler 
142124a229f4SSam Leffler /*
142224a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
142324a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
142424a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
142524a229f4SSam Leffler  * headers are not yet supporrted).
142624a229f4SSam Leffler  */
142724a229f4SSam Leffler void
142824a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
142924a229f4SSam Leffler 	struct ifnet *ifp;
143024a229f4SSam Leffler 	u_int dlt, hdrlen;
143124a229f4SSam Leffler 	struct bpf_if **driverp;
143224a229f4SSam Leffler {
1433df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14346a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1435572bde2aSRobert Watson 	if (bp == NULL)
1436df8bae1dSRodney W. Grimes 		panic("bpfattach");
1437df8bae1dSRodney W. Grimes 
14384a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
143924a229f4SSam Leffler 	bp->bif_driverp = driverp;
1440df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1441df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
14426008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1443df8bae1dSRodney W. Grimes 
1444e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
14454a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1446e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1447df8bae1dSRodney W. Grimes 
1448572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 	/*
1451df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1452df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1453df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1454df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1455df8bae1dSRodney W. Grimes 	 */
1456df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1457df8bae1dSRodney W. Grimes 
14582eeab939SGarrett Wollman 	if (bootverbose)
145924a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1460df8bae1dSRodney W. Grimes }
146153ac6efbSJulian Elischer 
1462de5d9935SRobert Watson /*
1463de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1464de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1465de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1466de5d9935SRobert Watson  * ENXIO.
1467de5d9935SRobert Watson  */
1468de5d9935SRobert Watson void
1469de5d9935SRobert Watson bpfdetach(ifp)
1470de5d9935SRobert Watson 	struct ifnet *ifp;
1471de5d9935SRobert Watson {
14724a3feeaaSRobert Watson 	struct bpf_if	*bp;
1473de5d9935SRobert Watson 	struct bpf_d	*d;
1474de5d9935SRobert Watson 
1475de5d9935SRobert Watson 	/* Locate BPF interface information */
14768eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
14774a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1478de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1479de5d9935SRobert Watson 			break;
1480de5d9935SRobert Watson 	}
1481de5d9935SRobert Watson 
1482de5d9935SRobert Watson 	/* Interface wasn't attached */
1483d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1484e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
14859bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1486de5d9935SRobert Watson 		return;
1487de5d9935SRobert Watson 	}
1488de5d9935SRobert Watson 
14894a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
14908eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1491de5d9935SRobert Watson 
14924a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1493e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1494e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1495e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1496e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1497e7bb21b3SJonathan Lemon 	}
1498e7bb21b3SJonathan Lemon 
1499e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1500de5d9935SRobert Watson 	free(bp, M_BPF);
15018eab61f3SSam Leffler }
1502de5d9935SRobert Watson 
15038eab61f3SSam Leffler /*
15048eab61f3SSam Leffler  * Get a list of available data link type of the interface.
15058eab61f3SSam Leffler  */
15068eab61f3SSam Leffler static int
15078eab61f3SSam Leffler bpf_getdltlist(d, bfl)
15088eab61f3SSam Leffler 	struct bpf_d *d;
15098eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
15108eab61f3SSam Leffler {
15118eab61f3SSam Leffler 	int n, error;
15128eab61f3SSam Leffler 	struct ifnet *ifp;
15138eab61f3SSam Leffler 	struct bpf_if *bp;
15148eab61f3SSam Leffler 
15158eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15168eab61f3SSam Leffler 	n = 0;
15178eab61f3SSam Leffler 	error = 0;
15188eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15194a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
15208eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
15218eab61f3SSam Leffler 			continue;
15228eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
15238eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1524e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
15258eab61f3SSam Leffler 				return (ENOMEM);
15268eab61f3SSam Leffler 			}
15278eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
15288eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
15298eab61f3SSam Leffler 		}
15308eab61f3SSam Leffler 		n++;
15318eab61f3SSam Leffler 	}
15328eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15338eab61f3SSam Leffler 	bfl->bfl_len = n;
15348eab61f3SSam Leffler 	return (error);
15358eab61f3SSam Leffler }
15368eab61f3SSam Leffler 
15378eab61f3SSam Leffler /*
15388eab61f3SSam Leffler  * Set the data link type of a BPF instance.
15398eab61f3SSam Leffler  */
15408eab61f3SSam Leffler static int
15418eab61f3SSam Leffler bpf_setdlt(d, dlt)
15428eab61f3SSam Leffler 	struct bpf_d *d;
15438eab61f3SSam Leffler 	u_int dlt;
15448eab61f3SSam Leffler {
15458eab61f3SSam Leffler 	int error, opromisc;
15468eab61f3SSam Leffler 	struct ifnet *ifp;
15478eab61f3SSam Leffler 	struct bpf_if *bp;
15488eab61f3SSam Leffler 
15498eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
15508eab61f3SSam Leffler 		return (0);
15518eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15528eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15534a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
15548eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
15558eab61f3SSam Leffler 			break;
15568eab61f3SSam Leffler 	}
15578eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15588eab61f3SSam Leffler 	if (bp != NULL) {
15598eab61f3SSam Leffler 		opromisc = d->bd_promisc;
15608eab61f3SSam Leffler 		bpf_detachd(d);
15618eab61f3SSam Leffler 		bpf_attachd(d, bp);
156293daabddSBrian Feldman 		BPFD_LOCK(d);
15638eab61f3SSam Leffler 		reset_d(d);
15648eab61f3SSam Leffler 		BPFD_UNLOCK(d);
15658eab61f3SSam Leffler 		if (opromisc) {
15668eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
15678eab61f3SSam Leffler 			if (error)
15688eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
15698eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
15708eab61f3SSam Leffler 					error);
15718eab61f3SSam Leffler 			else
15728eab61f3SSam Leffler 				d->bd_promisc = 1;
15738eab61f3SSam Leffler 		}
15748eab61f3SSam Leffler 	}
15758eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1576de5d9935SRobert Watson }
1577de5d9935SRobert Watson 
1578929ddbbbSAlfred Perlstein static void bpf_drvinit(void *unused);
1579bd3a5320SPoul-Henning Kamp 
158089c9c53dSPoul-Henning Kamp static void bpf_clone(void *arg, char *name, int namelen, struct cdev **dev);
15813f54a085SPoul-Henning Kamp 
15823f54a085SPoul-Henning Kamp static void
15833f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
15843f54a085SPoul-Henning Kamp 	void *arg;
15853f54a085SPoul-Henning Kamp 	char *name;
15863f54a085SPoul-Henning Kamp 	int namelen;
158789c9c53dSPoul-Henning Kamp 	struct cdev **dev;
15883f54a085SPoul-Henning Kamp {
15893f54a085SPoul-Henning Kamp 	int u;
15903f54a085SPoul-Henning Kamp 
1591f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
15923f54a085SPoul-Henning Kamp 		return;
1593db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
15943f54a085SPoul-Henning Kamp 		return;
1595b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1596b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1597b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
15983f54a085SPoul-Henning Kamp 	return;
15993f54a085SPoul-Henning Kamp }
16003f54a085SPoul-Henning Kamp 
1601514ede09SBruce Evans static void
1602514ede09SBruce Evans bpf_drvinit(unused)
1603514ede09SBruce Evans 	void *unused;
160453ac6efbSJulian Elischer {
160553ac6efbSJulian Elischer 
16066008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
16074a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1608db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
16097198bf47SJulian Elischer }
161053ac6efbSJulian Elischer 
1611c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
161253ac6efbSJulian Elischer 
16135bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1614f8dc4716SMike Smith /*
1615f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1616f8dc4716SMike Smith  *
1617f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1618f8dc4716SMike Smith  * to be loaded at runtime.
1619f8dc4716SMike Smith  */
1620f8dc4716SMike Smith 
1621f8dc4716SMike Smith void
1622e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1623e5562beeSSam Leffler 	struct bpf_if *bp;
16248994a245SDag-Erling Smørgrav 	u_char *pkt;
16258994a245SDag-Erling Smørgrav 	u_int pktlen;
1626f8dc4716SMike Smith {
1627f8dc4716SMike Smith }
1628f8dc4716SMike Smith 
1629f8dc4716SMike Smith void
1630e5562beeSSam Leffler bpf_mtap(bp, m)
1631e5562beeSSam Leffler 	struct bpf_if *bp;
1632f8dc4716SMike Smith 	struct mbuf *m;
1633f8dc4716SMike Smith {
1634f8dc4716SMike Smith }
1635f8dc4716SMike Smith 
1636f8dc4716SMike Smith void
1637437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1638437ffe18SSam Leffler 	struct bpf_if *bp;
1639c1dae2f0STim J. Robbins 	void *d;
1640437ffe18SSam Leffler 	u_int l;
1641437ffe18SSam Leffler 	struct mbuf *m;
1642437ffe18SSam Leffler {
1643437ffe18SSam Leffler }
1644437ffe18SSam Leffler 
1645437ffe18SSam Leffler void
1646f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1647f8dc4716SMike Smith 	struct ifnet *ifp;
1648f8dc4716SMike Smith 	u_int dlt, hdrlen;
1649f8dc4716SMike Smith {
1650f8dc4716SMike Smith }
1651f8dc4716SMike Smith 
1652da626c17SBill Paul void
16535f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
16545f7a7923SSam Leffler 	struct ifnet *ifp;
16555f7a7923SSam Leffler 	u_int dlt, hdrlen;
16565f7a7923SSam Leffler 	struct bpf_if **driverp;
16575f7a7923SSam Leffler {
16585f7a7923SSam Leffler }
16595f7a7923SSam Leffler 
16605f7a7923SSam Leffler void
1661da626c17SBill Paul bpfdetach(ifp)
1662da626c17SBill Paul 	struct ifnet *ifp;
1663da626c17SBill Paul {
1664da626c17SBill Paul }
1665da626c17SBill Paul 
1666f8dc4716SMike Smith u_int
1667f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
16688994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
16698994a245SDag-Erling Smørgrav 	u_char *p;
1670f8dc4716SMike Smith 	u_int wirelen;
16718994a245SDag-Erling Smørgrav 	u_int buflen;
1672f8dc4716SMike Smith {
1673f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1674f8dc4716SMike Smith }
1675f8dc4716SMike Smith 
16765bb5f2c9SPeter Wemm int
16775bb5f2c9SPeter Wemm bpf_validate(f, len)
16785bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
16795bb5f2c9SPeter Wemm 	int len;
16805bb5f2c9SPeter Wemm {
16815bb5f2c9SPeter Wemm 	return 0;		/* false */
16825bb5f2c9SPeter Wemm }
16835bb5f2c9SPeter Wemm 
16845bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1685